Fix location races, run database tests in CI, add make check
Test and publish / test (push) Has been cancelled
Test and publish / publish (push) Has been cancelled

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]>
This commit is contained in:
2026-10-14 22:48:41 +01:00
co-authored by Claude Opus 5.5
parent 0be97384fa
commit cfe17ecf5d
13 changed files with 285 additions and 80 deletions
+19 -23
View File
@@ -119,50 +119,48 @@ func (s *Store) InsertActivities(ctx context.Context, deviceID int64, acts []ing
}
// MoveTo records a location transition for a device: it closes the device's
// open place interval at `at` and opens one labelled `label`. Moving to the
// place the device is already in changes nothing, so repeated geofence
// triggers are harmless. It reports whether anything changed.
func (s *Store) MoveTo(ctx context.Context, deviceID int64, label string, at time.Time) (bool, error) {
// open place interval at `at` and opens one labelled dest(current place).
// dest is called under the device's lock, so concurrent events see each
// other's moves. Moving to the place the device is already in changes
// nothing, so repeated geofence triggers are harmless. It returns the place
// the device is now in and whether anything changed.
func (s *Store) MoveTo(ctx context.Context, deviceID int64, at time.Time, dest func(cur string) string) (string, bool, error) {
tx, err := s.pool.Begin(ctx)
if err != nil {
return false, err
return "", false, err
}
defer tx.Rollback(ctx)
if _, err := tx.Exec(ctx, `SELECT pg_advisory_xact_lock($1)`, deviceID); err != nil {
return false, err
return "", false, err
}
cur, err := currentPlace(ctx, tx, deviceID)
if err != nil {
return false, err
return "", false, err
}
label := dest(cur.Label)
if cur.ID != 0 {
if cur.Label == label {
return false, nil
return label, false, nil
}
// Out-of-order events would otherwise make a negative interval.
at = maxTime(at, cur.StartedAt)
if _, err := tx.Exec(ctx,
`UPDATE activities SET ended_at = $2, attrs = attrs - 'open' WHERE id = $1`,
cur.ID, at); err != nil {
return false, err
return "", false, err
}
}
// Each open interval is closed exactly once, so the one this move closes
// identifies it. (A timestamp wouldn't: clamped events can share one.)
if _, err := tx.Exec(ctx, `INSERT INTO activities
(device_id, source, kind, label, started_at, ended_at, attrs, external_id)
VALUES ($1, $2, $3, $4, $5, $5, '{"open": true}', $6)`,
deviceID, ingest.LocationSource, ingest.PlaceKind, label, at,
fmt.Sprintf("%d|%s|%s", deviceID, label, at.Format(time.RFC3339Nano))); err != nil {
return false, err
fmt.Sprintf("move|%d|%d", deviceID, cur.ID)); err != nil {
return "", false, err
}
return true, tx.Commit(ctx)
}
// CurrentPlace returns the label of the device's open place interval, or ""
// if it has none.
func (s *Store) CurrentPlace(ctx context.Context, deviceID int64) (string, error) {
cur, err := currentPlace(ctx, s.pool, deviceID)
return cur.Label, err
return label, true, tx.Commit(ctx)
}
type openPlace struct {
@@ -171,11 +169,9 @@ type openPlace struct {
StartedAt time.Time
}
func currentPlace(ctx context.Context, q interface {
QueryRow(context.Context, string, ...any) pgx.Row
}, deviceID int64) (openPlace, error) {
func currentPlace(ctx context.Context, tx pgx.Tx, deviceID int64) (openPlace, error) {
var p openPlace
err := q.QueryRow(ctx,
err := tx.QueryRow(ctx,
`SELECT id, label, started_at FROM activities
WHERE device_id = $1 AND source = $2 AND attrs ? 'open'`,
deviceID, ingest.LocationSource).Scan(&p.ID, &p.Label, &p.StartedAt)