diff --git a/server/src/services/native-preclaim-continuation.test.ts b/server/src/services/native-preclaim-continuation.test.ts index 95929b02e1f..6e1b2af2610 100644 --- a/server/src/services/native-preclaim-continuation.test.ts +++ b/server/src/services/native-preclaim-continuation.test.ts @@ -136,6 +136,36 @@ describe("native run that failed before its first coordinator claim", () => { expect(await successorsOf(f.runId)).toHaveLength(0); }, 60_000); + // Staging 2026-11-20: a user's exact chat Retry failed before its first + // claim. Its authority is bound to its own chat action and Retry intent, so + // a system successor carrying the copied selector is always refused at + // setup. The saved hold keeps the task for the user's next Retry or message. + it("does not continue a chat Retry automatically under its consumed exact-request authority", async () => { + const f = await preClaimNativeFailure(); + const current = await run(f.runId); + // The failed run the user retried. It precedes the Retry, so it does not + // supersede it. + const [failedSource] = await db.insert(heartbeatRuns).values({ companyId: f.companyId, agentId: f.agentId, + status: "failed", contextSnapshot: { issueId: f.issueId }, createdAt: new Date(current.createdAt.getTime() - 60_000) }) + .returning(); + const retryOfRunId = failedSource!.id; + await db.update(heartbeatRuns).set({ + errorCode: "chat_failed_run_retry_not_authorized", retryOfRunId, + contextSnapshot: { ...current.contextSnapshot, retryOfRunId, forceFreshSession: true, taskRetryRequestId: randomUUID(), + chatFailedRunRetry: { version: 1, actionId: current.wakeupRequestId ?? randomUUID(), failedRunId: retryOfRunId } }, + }).where(eq(heartbeatRuns.id, f.runId)); + await recoverRetainedWorkspace(f.input); + expect(readWorkspaceRecovery((await action(f.actionId)).evidence)).toMatchObject({ status: "recovered" }); + + await sweep(); + + expect(await successorsOf(f.runId)).toHaveLength(0); + const hold = await action(f.actionId); + expect(hold).toMatchObject({ outcome: "blocked", evidence: { automaticRecovery: { replay: "blocked" } } }); + const [task] = await db.select().from(issues).where(eq(issues.id, f.issueId)); + expect(task.status).toBe("blocked"); + }, 60_000); + it.each(["claimed", "provider_event", "tool_call", "uncertain_side_effect", "process", "superseded"] as const)( "keeps reconciliation when the run may have acted: %s", async gate => { const f = await preClaimNativeFailure(); diff --git a/server/src/services/native-runtime/native-safe-replacement.test.ts b/server/src/services/native-runtime/native-safe-replacement.test.ts index e7c4b033609..5ffa2adee33 100644 --- a/server/src/services/native-runtime/native-safe-replacement.test.ts +++ b/server/src/services/native-runtime/native-safe-replacement.test.ts @@ -527,6 +527,24 @@ const support = externalDatabaseUrl const [action] = await db.select().from(issueRecoveryActions).where(eq(issueRecoveryActions.sourceIssueId, source.issueId)); expect(action.status).toBe("active"); }); + it.each([["an ordinary run", false, 1], ["an exact chat Retry", true, 0]] as const)( + "replaces a provider terminal failure only without consumed chat Retry authority (%s)", async (_label, chatRetry, expected) => { + const source = await seed(); + if (chatRetry) { + const [failed] = await db.insert(heartbeatRuns).values({ companyId: source.companyId, agentId: source.agentId, + status: "failed", contextSnapshot: { issueId: source.issueId }, createdAt: new Date(Date.now() - 60_000) }).returning(); + const [current] = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.id, source.runId)); + await db.update(heartbeatRuns).set({ retryOfRunId: failed!.id, contextSnapshot: { ...current!.contextSnapshot, + retryOfRunId: failed!.id, taskRetryRequestId: randomUUID(), + chatFailedRunRetry: { version: 1, actionId: randomUUID(), failedRunId: failed!.id } } }) + .where(eq(heartbeatRuns.id, source.runId)); + } + invalidateNativeReplacementDiscovery(db); + await reconcileSafeNativeReplacements(db); + const successors = await db.select().from(heartbeatRuns).where(eq(heartbeatRuns.retryOfRunId, source.runId)); + expect(successors).toHaveLength(expected); + for (const successor of successors) expect(successor.contextSnapshot).not.toHaveProperty("chatFailedRunRetry"); + }); async function seedUnprovenOwner(finishedAgoMs: number) { const source = await seed(); await db.update(heartbeatRuns).set({ finishedAt: sql`now() - ${finishedAgoMs}::integer * interval '1 millisecond'`, diff --git a/server/src/services/native-runtime/native-safe-replacement.ts b/server/src/services/native-runtime/native-safe-replacement.ts index bcb960876bc..7e619c53537 100644 --- a/server/src/services/native-runtime/native-safe-replacement.ts +++ b/server/src/services/native-runtime/native-safe-replacement.ts @@ -240,6 +240,10 @@ export async function remoteWorkspaceReplacementEvidence( export function nativeReplacementPending() { return and( eq(heartbeatRuns.status, "failed"), + // An exact chat Retry is authorized only through its own chat action and + // the user's Retry intent. A system successor cannot hold that authority, + // so admission would always refuse it. Keep the task's hold for the user. + sql`not (coalesce(${heartbeatRuns.contextSnapshot}, '{}'::jsonb) ? 'chatFailedRunRetry')`, eq(nativeRunFinalizations.phase, "terminal_failure"), or(inArray( nativeRunFinalizations.failureCode,