Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix: deflake wakeup and squad briefing tests, bound wakeup receipt expiry (MUL-5850) (#8633)

Bohan Jiang70f900c573eb
diff --git a/server/internal/handler/squad_briefing_test.go b/server/internal/handler/squad_briefing_test.go
index 5cc47d02a0..7341cedfb1 100644
--- a/server/internal/handler/squad_briefing_test.go
+++ b/server/internal/handler/squad_briefing_test.go
@@ -362,6 +362,14 @@ func TestBuildSquadLeaderBriefing_MentionsRoundTrip(t *testing.T) {
// returns the agent block of the response. Fails the test on non-200.
func claimAndDecodeAgent(t *testing.T, runtimeID string) *TaskAgentData {
t.Helper()
+ // The shared TestMain runtime heartbeats only at suite start, and claims
+ // skip runtimes unseen for RuntimeClaimFreshnessSeconds (150s). This file
+ // runs late enough to cross that on a slow -race runner, so refresh first.
+ if _, err := testPool.Exec(context.Background(),
+ `UPDATE agent_runtime SET status = 'online', last_seen_at = now() WHERE id = $1`, runtimeID,
+ ); err != nil {
+ t.Fatalf("refresh runtime heartbeat: %v", err)
+ }
w := httptest.NewRecorder()
req := newDaemonTokenRequest("POST", "/api/daemon/runtimes/"+runtimeID+"/claim", nil, testWorkspaceID, "test-claim-squad-briefing")
req = withURLParam(req, "runtimeId", runtimeID)
@@ -374,11 +382,12 @@ func claimAndDecodeAgent(t *testing.T, runtimeID string) *TaskAgentData {
Agent *TaskAgentData `json:"agent"`
} `json:"task"`
}
- if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
+ body := w.Body.String()
+ if err := json.Unmarshal([]byte(body), &resp); err != nil {
t.Fatalf("decode: %v", err)
}
if resp.Task == nil || resp.Task.Agent == nil {
- t.Fatalf("expected task.agent in response, got: %s", w.Body.String())
+ t.Fatalf("expected task.agent in response, got: %s", body)
}
return resp.Task.Agent
}
diff --git a/server/internal/service/issue_wakeup_test.go b/server/internal/service/issue_wakeup_test.go
index 0fd038c003..34bb380689 100644
--- a/server/internal/service/issue_wakeup_test.go
+++ b/server/internal/service/issue_wakeup_test.go
@@ -273,11 +273,22 @@ func TestIssueWakeupConcurrentDispatchIsIdempotent(t *testing.T) {
}
wg.Wait()
close(errs)
+ // dispatch fails fast (lock_timeout 50ms) instead of queueing behind the
+ // rule's lock holder; Tick retries it next round. So a loser may see
+ // lock_not_available when the winner's transaction runs long, as on a
+ // slow CI runner. The contract is one winner and one run.
+ winners := 0
for err := range errs {
- if err != nil {
+ switch {
+ case err == nil:
+ winners++
+ case !isLockTimeout(err):
t.Error(err)
}
}
+ if winners == 0 {
+ t.Fatal("no dispatch succeeded")
+ }
if n := f.Count(t, "SELECT count(*) FROM agent_task_queue WHERE context->>'wakeup_id'=$1", util.UUIDToString(w.ID)); n != 1 {
t.Fatalf("created %d runs", n)
}
diff --git a/server/pkg/db/generated/wakeup.sql.go b/server/pkg/db/generated/wakeup.sql.go
index 4d913b17e2..496c1d7498 100644
--- a/server/pkg/db/generated/wakeup.sql.go
+++ b/server/pkg/db/generated/wakeup.sql.go
@@ -388,13 +388,17 @@ func (q *Queries) CreateWakeupTask(ctx context.Context, arg CreateWakeupTaskPara
}
const deleteExpiredWakeupReceipts = `-- name: DeleteExpiredWakeupReceipts :execrows
-DELETE FROM issue_wakeup_receipt WHERE id IN (
+WITH batch AS MATERIALIZED (
SELECT expired.id FROM issue_wakeup_receipt expired WHERE expired.processed_at < $1
ORDER BY expired.processed_at,expired.id LIMIT 1000 FOR UPDATE SKIP LOCKED
)
+DELETE FROM issue_wakeup_receipt r USING batch WHERE r.id=batch.id
`
// Pending inputs are never expired. Bound work and avoid waiting on dispatch.
+// MATERIALIZED evaluates the batch once. As an IN subquery, a nested-loop plan
+// rescans it per outer row, skips the rows this DELETE already removed, and
+// slides the LIMIT window until every expired row is gone.
func (q *Queries) DeleteExpiredWakeupReceipts(ctx context.Context, cutoff pgtype.Timestamptz) (int64, error) {
result, err := q.db.Exec(ctx, deleteExpiredWakeupReceipts, cutoff)
if err != nil {
diff --git a/server/pkg/db/queries/wakeup.sql b/server/pkg/db/queries/wakeup.sql
index b4b17f8eeb..3d0b49d3e0 100644
--- a/server/pkg/db/queries/wakeup.sql
+++ b/server/pkg/db/queries/wakeup.sql
@@ -68,10 +68,14 @@ SELECT * FROM issue_wakeup_receipt WHERE wakeup_id= @wakeup_id AND revision= @re
-- name: DeleteExpiredWakeupReceipts :execrows
-- Pending inputs are never expired. Bound work and avoid waiting on dispatch.
-DELETE FROM issue_wakeup_receipt WHERE id IN (
+-- MATERIALIZED evaluates the batch once. As an IN subquery, a nested-loop plan
+-- rescans it per outer row, skips the rows this DELETE already removed, and
+-- slides the LIMIT window until every expired row is gone.
+WITH batch AS MATERIALIZED (
SELECT expired.id FROM issue_wakeup_receipt expired WHERE expired.processed_at < @cutoff
ORDER BY expired.processed_at,expired.id LIMIT 1000 FOR UPDATE SKIP LOCKED
-);
+)
+DELETE FROM issue_wakeup_receipt r USING batch WHERE r.id=batch.id;
-- name: RecordWakeupReceipt :one
INSERT INTO issue_wakeup_receipt(id,wakeup_id,revision,event_key,event_type,payload)
VALUES(@id,@wakeup_id,@revision,@event_key,@event_type,@payload)