From 1480f716c92f43cf90776cde89ffa6569311c88f Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Thu, 26 Nov 2026 17:21:06 +0000 Subject: [PATCH] test(recovery): pin the 7-day window for chat setup failures; reattach listSuspects docs (#23063) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The assigned-work sweep now continues a blocked chat-channel task whose latest run failed in setup before provider work (#23059) > - Review on #23059 left three non-blocking points: the 7-day run-age limit had no test, the `listSuspects` doc comment was separated from its function, and the rule has SQL and TypeScript twins > - This pull request adds the missing boundary test and restores the doc placement > - The benefit is that widening or removing the age limit now fails a test, and the code reads as it did ## Linked Issues or Issue Description Refs #23059, #23051 **Describe the bug** Review follow-up, no user-visible bug. The 7-day limit on how old a setup failure may be for a chat task to continue automatically had no test, so a regression that widened or removed it would pass. The `listSuspects` doc comment was split from `listSuspects` by a new helper. **Steps to reproduce** 1. Remove the run-age condition from `latestRunFailedBeforeProviderWork` and `chatFailedBeforeProviderWork`. 2. The existing tests still pass. **Expected behavior** A test fails when the 7-day limit is removed. ## What Changed - `assigned-work-invariant.test.ts`: a recently blocked chat task whose setup failure ended 8 days ago is not a sweep suspect and is judged a conversation wait. The test helper takes an optional `blockedAgoMs` so the block can be recent while the run is old. - `assigned-work-sweep.ts`: the `listSuspects` pre-filter doc comment sits directly on `listSuspects` again; the SQL predicate's comment names its TypeScript twin and the test that covers both. ## Verification - `assigned-work-invariant.test.ts`: 35/35 pass. With the age condition removed from both twins, the chat setup-failure test fails (checked), then passes again with it restored. - Server typecheck is clean. ## Risks - Test and comment changes only; no behavior change. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use and code execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [ ] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 --- .../__tests__/assigned-work-invariant.test.ts | 12 ++++++------ .../services/recovery/assigned-work-sweep.ts | 19 ++++++++++--------- 2 files changed, 16 insertions(+), 15 deletions(-) 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) {