Files
adamfandClaude Opus 5.5 cfe17ecf5d
Test and publish / test (push) Has been cancelled
Test and publish / publish (push) Has been cancelled
Fix location races, run database tests in CI, add make check
Location: a leave decided its destination before taking the device lock,
so a "leave home" racing an "arrive gym" could undo the arrival. The
decision (now ingest.Destination) runs inside MoveTo under the lock.
Transition external IDs were device|label|time, which collided (500) when
out-of-order events were clamped to the same start; they now name the
interval being closed.

hypr-agent: a control command arriving during shutdown no longer blocks
its goroutine forever.

Tests: internal/testdb gives each database test its own throwaway
database, so they run in parallel and no longer truncate the dev data.
New store tests cover both location bugs. CI starts a Postgres container
so these tests actually run, and adds gofmt, staticcheck (pinned as a go
tool) and -race. `make check` runs the same locally.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-14 22:48:41 +01:00

95 lines
2.1 KiB
Go

package hypr
import (
"bufio"
"context"
"errors"
"fmt"
"net"
"os"
"path/filepath"
"strings"
"time"
)
// Control commands sent by hypridle hooks via `hypr-agent <command>`.
var Commands = map[string]bool{"idle": true, "resume": true, "lock": true, "unlock": true}
// ControlEvent is a control command stamped with when it was received.
type ControlEvent struct {
Command string
At time.Time
}
// ControlSocketPath is where the running agent listens for control commands.
func ControlSocketPath() string {
dir := os.Getenv("XDG_RUNTIME_DIR")
if dir == "" {
dir = os.TempDir()
}
return filepath.Join(dir, "hypr-agent.sock")
}
// ServeControl accepts one-line commands on the control socket until ctx is
// cancelled.
func ServeControl(ctx context.Context, out chan<- ControlEvent) error {
path := ControlSocketPath()
os.Remove(path) // stale socket from a previous run
ln, err := net.Listen("unix", path)
if err != nil {
return err
}
go func() {
<-ctx.Done()
ln.Close()
}()
for {
conn, err := ln.Accept()
if err != nil {
if ctx.Err() != nil {
return nil
}
return err
}
go func() {
defer conn.Close()
conn.SetDeadline(time.Now().Add(2 * time.Second))
line, _ := bufio.NewReader(conn).ReadString('\n')
cmd := strings.TrimSpace(line)
if !Commands[cmd] {
fmt.Fprintf(conn, "unknown command %q\n", cmd)
return
}
select {
case out <- ControlEvent{Command: cmd, At: time.Now()}:
fmt.Fprintln(conn, "ok")
case <-ctx.Done(): // the agent stopped reading
fmt.Fprintln(conn, "shutting down")
}
}()
}
}
// SendControl sends a command to the running agent.
func SendControl(cmd string) error {
conn, err := net.DialTimeout("unix", ControlSocketPath(), 2*time.Second)
if err != nil {
return fmt.Errorf("is hypr-agent running? %w", err)
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(2 * time.Second))
if _, err := fmt.Fprintln(conn, cmd); err != nil {
return err
}
reply, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
return err
}
if reply = strings.TrimSpace(reply); reply != "ok" {
return errors.New(reply)
}
return nil
}