Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(server): stamp squad handoff fire_at with the database clock (#10963)
adai5c3b47ee3480
diff --git a/server/internal/handler/heartbeat_test.go b/server/internal/handler/heartbeat_test.goindex 6dbedd0eff..6d8aaa509f 100644--- a/server/internal/handler/heartbeat_test.go+++ b/server/internal/handler/heartbeat_test.go@@ -87,6 +87,18 @@ func readRuntimeRow(t *testing.T, runtimeID string) (status string, lastSeen timreturn}+// pinRuntimeLastSeenAtToDBNow stamps last_seen_at with the database clock, the+// clock the heartbeat write uses, so "did the write move it forward" does not+// depend on the test process and the database agreeing on the time.+func pinRuntimeLastSeenAtToDBNow(t *testing.T, runtimeID string) {+ t.Helper()+ if _, err := testPool.Exec(context.Background(),+ `UPDATE agent_runtime SET last_seen_at = now() WHERE id = $1`, runtimeID,+ ); err != nil {+ t.Fatalf("pin last_seen_at: %v", err)+ }+}+func setRuntimeLastSeenAt(t *testing.T, runtimeID string, when time.Time) {t.Helper()if _, err := testPool.Exec(context.Background(),@@ -215,7 +227,7 @@ func TestRecordHeartbeat_NoopStoreAlwaysWritesDB(t *testing.T) {// Pin last_seen_at to "just now" to ensure the DB-flush condition is not// what's driving the write.- setRuntimeLastSeenAt(t, runtimeID, time.Now())+ pinRuntimeLastSeenAtToDBNow(t, runtimeID)rt := loadRuntime(t, runtimeID)before := rt.LastSeenAt.Time@@ -403,7 +415,7 @@ func TestRecordHeartbeat_TouchErrorFallsBackToDB(t *testing.T) {testHandler.LivenessStore = faket.Cleanup(func() { testHandler.LivenessStore = orig })- setRuntimeLastSeenAt(t, runtimeID, time.Now())+ pinRuntimeLastSeenAtToDBNow(t, runtimeID)rt := loadRuntime(t, runtimeID)before := rt.LastSeenAt.Timediff --git a/server/internal/service/workflow_squad_handoff.go b/server/internal/service/workflow_squad_handoff.goindex f72fc5c752..d04218e02a 100644--- a/server/internal/service/workflow_squad_handoff.go+++ b/server/internal/service/workflow_squad_handoff.go@@ -3,7 +3,6 @@ package serviceimport ("context""errors"- "time""github.com/jackc/pgx/v5""github.com/jackc/pgx/v5/pgtype"@@ -23,15 +22,16 @@ func (s *TaskService) createWorkflowSquadHandoffTask(ctx context.Context, issue// Deferred rows can coexist with an occupied queued/dispatched slot. The// existing promoter releases them when that slot frees; never rewrite the// binding, role, comments or attribution of a task already offered to a daemon.- params.FireAt = pgtype.Timestamptz{Time: time.Now(), Valid: true}params.WorkflowStep = pgtype.Text{String: issue.Status, Valid: true}params.WorkflowHandoffRevision = pgtype.Int8{Int64: issue.Revision, Valid: true}var task db.AgentTaskQueueerr := s.runInTx(ctx, func(q *db.Queries) error {- if err := q.LockWorkflowSquadHandoff(ctx, issue.ID); err != nil {+ lockedAt, err := q.LockWorkflowSquadHandoff(ctx, issue.ID)+ if err != nil {return err}- _, err := q.GetWorkflowSquadHandoffTask(ctx, db.GetWorkflowSquadHandoffTaskParams{+ params.FireAt = lockedAt+ _, err = q.GetWorkflowSquadHandoffTask(ctx, db.GetWorkflowSquadHandoffTaskParams{IssueID: issue.ID, AgentID: params.AgentID, SquadID: params.SquadID,IssueRevision: issue.Revision,})diff --git a/server/pkg/db/generated/workflow_handoff.sql.go b/server/pkg/db/generated/workflow_handoff.sql.goindex 956950a7cb..7f79dde53e 100644--- a/server/pkg/db/generated/workflow_handoff.sql.go+++ b/server/pkg/db/generated/workflow_handoff.sql.go@@ -97,13 +97,18 @@ func (q *Queries) ListAssigneeRunsForHandoff(ctx context.Context, arg ListAssignreturn items, nil}-const lockWorkflowSquadHandoff = `-- name: LockWorkflowSquadHandoff :exec-SELECT pg_advisory_xact_lock(hashtextextended($1::uuid::text || ':workflow_squad_handoff', 0))+const lockWorkflowSquadHandoff = `-- name: LockWorkflowSquadHandoff :one+SELECT now()::timestamptz AS locked_at+FROM (SELECT pg_advisory_xact_lock(hashtextextended($1::uuid::text || ':workflow_squad_handoff', 0))) AS handoff_lock`// Serialize admission for one issue without taking its owner-row locks out of// the order lock_task_owner_rows uses to fence workspace teardown.-func (q *Queries) LockWorkflowSquadHandoff(ctx context.Context, issueID pgtype.UUID) error {- _, err := q.db.Exec(ctx, lockWorkflowSquadHandoff, issueID)- return err+// Returns the database clock: the handoff row's fire_at must be due by the same+// clock the promoter compares it against (fire_at <= now()).+func (q *Queries) LockWorkflowSquadHandoff(ctx context.Context, issueID pgtype.UUID) (pgtype.Timestamptz, error) {+ row := q.db.QueryRow(ctx, lockWorkflowSquadHandoff, issueID)+ var locked_at pgtype.Timestamptz+ err := row.Scan(&locked_at)+ return locked_at, err}diff --git a/server/pkg/db/queries/workflow_handoff.sql b/server/pkg/db/queries/workflow_handoff.sqlindex 12a67b35b5..c1a934d1ae 100644--- a/server/pkg/db/queries/workflow_handoff.sql+++ b/server/pkg/db/queries/workflow_handoff.sql@@ -1,7 +1,10 @@--- name: LockWorkflowSquadHandoff :exec+-- name: LockWorkflowSquadHandoff :one-- Serialize admission for one issue without taking its owner-row locks out of-- the order lock_task_owner_rows uses to fence workspace teardown.-SELECT pg_advisory_xact_lock(hashtextextended(sqlc.arg('issue_id')::uuid::text || ':workflow_squad_handoff', 0));+-- Returns the database clock: the handoff row's fire_at must be due by the same+-- clock the promoter compares it against (fire_at <= now()).+SELECT now()::timestamptz AS locked_at+FROM (SELECT pg_advisory_xact_lock(hashtextextended(sqlc.arg('issue_id')::uuid::text || ':workflow_squad_handoff', 0))) AS handoff_lock;-- name: GetWorkflowSquadHandoffTask :one-- A replay of the same status write is already covered, including after claim