Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(wakeup): scope test scheduler passes to their own workspaces (#8901)
Bohan Jiang92649de29c33
diff --git a/server/internal/handler/issue_child_done_test.go b/server/internal/handler/issue_child_done_test.goindex 2ed1f75a7d..eb8d68cafd 100644--- a/server/internal/handler/issue_child_done_test.go+++ b/server/internal/handler/issue_child_done_test.go@@ -185,10 +185,12 @@ func childDoneRuns(t *testing.T, parentID string) []childDoneRunRow {return runs}-// runWakeupTick is one scheduler pass over ready wakeups.+// runWakeupTick is one scheduler pass over this package's ready wakeups. It is+// scoped to the test workspace: other packages' tests share the database, and+// dispatching their rules would consume receipts they are asserting on.func runWakeupTick(t *testing.T) {t.Helper()- if err := (&service.IssueWakeupService{Tasks: testHandler.TaskService}).Tick(context.Background()); err != nil {+ if err := (&service.IssueWakeupService{Tasks: testHandler.TaskService}).TickWorkspaces(context.Background(), parseUUID(testWorkspaceID)); err != nil {t.Logf("wakeup tick: %v", err)}}diff --git a/server/internal/service/issue_wakeup.go b/server/internal/service/issue_wakeup.goindex be17fa9053..01b37df9ca 100644--- a/server/internal/service/issue_wakeup.go+++ b/server/internal/service/issue_wakeup.go@@ -641,12 +641,28 @@ func (s *IssueWakeupService) authorize(ctx context.Context, q *db.Queries, ws, m// Tick uses the existing scheduler lease. Each config has a row lock as well,// so a manual retry or a second server cannot dispatch the same receipt twice.func (s *IssueWakeupService) Tick(ctx context.Context) error {+ return s.tick(ctx, nil)+}++// TickWorkspaces is one Tick pass over the given workspaces' ready wakeups.+// Tests use it: packages run concurrently against one database, and an+// unscoped pass would dispatch — and consume the pending receipts of — rules+// another package's test is still asserting on.+func (s *IssueWakeupService) TickWorkspaces(ctx context.Context, workspaceIDs ...pgtype.UUID) error {+ if len(workspaceIDs) == 0 {+ return nil+ }+ return s.tick(ctx, workspaceIDs)+}++// tick dispatches ready wakeups; nil workspaceIDs means every workspace.+func (s *IssueWakeupService) tick(ctx context.Context, workspaceIDs []pgtype.UUID) error {// Receipts are operational evidence, not the run history. Match the existing// event telemetry's seven-day retention; never expire unprocessed inputs.cleanupCtx, cleanupCancel := context.WithTimeout(ctx, 2*time.Second)_, cleanupErr := s.Tasks.Queries.DeleteExpiredWakeupReceipts(cleanupCtx, pgtype.Timestamptz{Time: time.Now().Add(-7 * 24 * time.Hour), Valid: true})cleanupCancel()- rows, err := s.Tasks.Queries.ListReadyWakeups(ctx)+ rows, err := s.Tasks.Queries.ListReadyWakeups(ctx, workspaceIDs)if err != nil {return err}diff --git a/server/internal/service/issue_wakeup_capacity_test.go b/server/internal/service/issue_wakeup_capacity_test.goindex f753dea674..c36279e8d5 100644--- a/server/internal/service/issue_wakeup_capacity_test.go+++ b/server/internal/service/issue_wakeup_capacity_test.go@@ -53,7 +53,7 @@ func TestIssueWakeupBusyRuleDoesNotBlockOtherWorkspace(t *testing.T) {}ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)defer cancel()- if err = s.Tick(ctx); err == nil {+ if err = s.TickWorkspaces(ctx, parseTestUUID(t, f.WorkspaceID), parseTestUUID(t, other.WorkspaceID)); err == nil {t.Fatal("expected busy-rule diagnostic")}if ctx.Err() != nil {diff --git a/server/internal/service/issue_wakeup_expiry_test.go b/server/internal/service/issue_wakeup_expiry_test.goindex abe335fa99..7de4dfa77e 100644--- a/server/internal/service/issue_wakeup_expiry_test.go+++ b/server/internal/service/issue_wakeup_expiry_test.go@@ -58,7 +58,7 @@ func TestIssueWakeupTimeoutWakesOnceAndEnds(t *testing.T) {t.Fatal("dispatched before the deadline")}f.Exec(t, "UPDATE issue_wakeup SET expires_at=now()-interval '1 second' WHERE id=$1", w.ID)- ready, err := f.q.ListReadyWakeups(ctx)+ ready, err := f.q.ListReadyWakeups(ctx, []pgtype.UUID{parseTestUUID(t, f.WorkspaceID)})if err != nil {t.Fatal(err)}diff --git a/server/pkg/db/generated/wakeup.sql.go b/server/pkg/db/generated/wakeup.sql.goindex d9b21632fa..7a1c6f3503 100644--- a/server/pkg/db/generated/wakeup.sql.go+++ b/server/pkg/db/generated/wakeup.sql.go@@ -984,11 +984,12 @@ WITH candidates AS (SELECT wakeup_id FROM issue_wakeup_receipt WHERE processed_at IS NULL)SELECT w.id, w.workspace_id, w.issue_id, w.agent_id, w.created_by, w.source_task_id, w.parent_comment_id, w.instruction, w.kind, w.mode, w.event_types, w.filter_agent_id, w.filter_task_id, w.interval_seconds, w.cron_expression, w.timezone, w.next_fire_at, w.enabled, w.disabled_at, w.revision, w.last_task_id, w.last_error, w.created_at, w.updated_at, w.filter_actor_type, w.filter_actor_id, w.expires_at, w.expiry_seconds, w.on_timeout, w.timed_out_at, w.system_rule, w.customized_at, w.condition, w.condition_state, w.max_fires, w.fire_count, w.paused_reason FROM candidates c JOIN issue_wakeup w ON w.id=c.id+WHERE $1::uuid[] IS NULL OR w.workspace_id = ANY($1::uuid[])ORDER BY w.updated_at,w.id LIMIT 100`-func (q *Queries) ListReadyWakeups(ctx context.Context) ([]IssueWakeup, error) {- rows, err := q.db.Query(ctx, listReadyWakeups)+func (q *Queries) ListReadyWakeups(ctx context.Context, workspaceIds []pgtype.UUID) ([]IssueWakeup, error) {+ rows, err := q.db.Query(ctx, listReadyWakeups, workspaceIds)if err != nil {return nil, err}diff --git a/server/pkg/db/queries/wakeup.sql b/server/pkg/db/queries/wakeup.sqlindex e14ae13772..e122400f35 100644--- a/server/pkg/db/queries/wakeup.sql+++ b/server/pkg/db/queries/wakeup.sql@@ -81,6 +81,7 @@ WITH candidates AS (SELECT wakeup_id FROM issue_wakeup_receipt WHERE processed_at IS NULL)SELECT w.* FROM candidates c JOIN issue_wakeup w ON w.id=c.id+WHERE sqlc.narg('workspace_ids')::uuid[] IS NULL OR w.workspace_id = ANY(sqlc.narg('workspace_ids')::uuid[])ORDER BY w.updated_at,w.id LIMIT 100;-- name: ListPendingWakeupReceipts :manySELECT * FROM issue_wakeup_receipt WHERE wakeup_id= @wakeup_id AND revision= @revision AND processed_at IS NULL ORDER BY created_at,id LIMIT 100 FOR UPDATE;