diff --git a/server/src/__tests__/assigned-work-invariant.test.ts b/server/src/__tests__/assigned-work-invariant.test.ts index 12fec20f5c1..77334fb3c70 100644 --- a/server/src/__tests__/assigned-work-invariant.test.ts +++ b/server/src/__tests__/assigned-work-invariant.test.ts @@ -312,9 +312,9 @@ describePostgres("assigned-work invariant sweep", () => { + "The snapshot qualification is missing or stale."; // The shape two GitHub review tasks on staging were left in: the task's only // run, legacy, failed in setup about 43 hours earlier, before provider work. - async function blockedReviewTask(input: { hold: boolean; providerWorkStarted?: boolean; endedAgoMs?: number }) { + async function blockedReviewTask(input: { hold: boolean; providerWorkStarted?: boolean; endedAgoMs?: number; blockedAgoMs?: number }) { const endedAgoMs = input.endedAgoMs ?? 43 * HOUR; - const blockedAt = new Date(Date.now() - endedAgoMs + 1_000); + const blockedAt = new Date(Date.now() - (input.blockedAgoMs ?? endedAgoMs - 1_000)); const issueId = await f.issue({ status: "blocked", originKind: "chat_channel", responsibleUserId: "owner-user", blockedTransitionAt: blockedAt, updatedAt: blockedAt }); const run = await f.run(issueId, { status: "failed", runtimeMode: "legacy", errorCode: "setup_failed", livenessState: "failed", error: qualification, resultJson: { stopReason: "adapter_failed", failureCause: "lease_acquire_failed", timeoutFired: false, @@ -338,13 +338,13 @@ describePostgres("assigned-work invariant sweep", () => { const held = await blockedReviewTask({ hold: true }); const unheld = await blockedReviewTask({ hold: false }); const workStarted = await blockedReviewTask({ hold: false, providerWorkStarted: true }); + const runTooOld = await blockedReviewTask({ hold: false, endedAgoMs: 8 * 24 * HOUR, blockedAgoMs: 2 * HOUR }); const waitingForMessage = await f.issue({ status: "in_progress", originKind: "chat_channel" }); const queue = wakeQueue(); const recovery = recoveryService(db, { enqueueWakeup: queue.enqueueWakeup }); const suspects = (await recovery.assignedWork.listSuspects({ companyId: f.companyId }, null, 100)).map(row => row.id); expect(suspects).toEqual(expect.arrayContaining([held, unheld])); - expect(suspects).not.toContain(workStarted); - expect(suspects).not.toContain(waitingForMessage); + for (const id of [workStarted, runTooOld, waitingForMessage]) expect(suspects).not.toContain(id); for (const issueId of [held, unheld]) { expect(await recovery.assignedWork.checkIssue({ companyId: f.companyId, issueId })) @@ -352,8 +352,8 @@ describePostgres("assigned-work invariant sweep", () => { expect((await db.select().from(issues).where(eq(issues.id, issueId)))[0].status).not.toBe("blocked"); } expect(queue.calls.map(call => call.issueId).sort()).toEqual([held, unheld].sort()); - // A chat task whose agent may have started work still waits for its conversation. - expect(await recovery.assignedWork.checkIssue({ companyId: f.companyId, issueId: workStarted })) + // A chat task whose agent may have started work, or whose failure is too old, still waits for its conversation. + for (const issueId of [workStarted, runTooOld]) expect(await recovery.assignedWork.checkIssue({ companyId: f.companyId, issueId })) .toMatchObject({ outcome: "ok", verdict: { clause: "waiting", reason: "conversation" } }); expect(queue.calls).toHaveLength(2); diff --git a/server/src/services/recovery/assigned-work-sweep.ts b/server/src/services/recovery/assigned-work-sweep.ts index f96dd78a446..c44e5514197 100644 --- a/server/src/services/recovery/assigned-work-sweep.ts +++ b/server/src/services/recovery/assigned-work-sweep.ts @@ -503,17 +503,10 @@ export function assignedWorkSweep(db: Db, deps: AssignedWorkDeps) { )).orderBy(desc(issues.updatedAt)).limit(limit); } - /** - * The cheap, indexed pre-filter the sweep reconstructs from: open assigned - * tasks with no run that is plainly live, no pending wake and no pending - * question. Everything it lets through is judged by the full predicate. - * Chat conversations (except a blocked chat task whose run failed before - * provider work) and blocks older than the stale window are left out: - * the first wait on a person's message, the second is only reported. - */ /** A chat task whose latest run, its assignee's, failed in setup before any * provider work: the agent never received the message, so the task waits on - * the system, not on the person's next message. */ + * the system, not on the person's next message. SQL twin of + * `chatFailedBeforeProviderWork`; the chat-review test covers both. */ function latestRunFailedBeforeProviderWork(at: Date) { return sql`coalesce((select lr.status = 'failed' and lr.agent_id = ${issues.assigneeAgentId} and lr.finished_at > ${new Date(at.getTime() - STRANDED_PRE_PROVIDER_SETUP_MAX_AGE_MS).toISOString()}::timestamptz @@ -529,6 +522,14 @@ export function assignedWorkSweep(db: Db, deps: AssignedWorkDeps) { order by lr.created_at desc limit 1), false)`; } + /** + * The cheap, indexed pre-filter the sweep reconstructs from: open assigned + * tasks with no run that is plainly live, no pending wake and no pending + * question. Everything it lets through is judged by the full predicate. + * Chat conversations (except a blocked chat task whose run failed before + * provider work) and blocks older than the stale window are left out: + * the first wait on a person's message, the second is only reported. + */ async function listSuspects(scope: { companyId?: string; issueId?: string; issueIds?: string[]; createdAfter?: Date | null; changed?: SQL }, cursor: string | null, limit: number) {