From 9140db553a53b24318843f4475cb6c7b8d462f86 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 15 Nov 2026 17:29:06 +0000 Subject: [PATCH 1/3] fix(server): ACP restart continuations no longer reopen a cut turn in a second session When a restart continuation's session/load fails on an agent that can restore sessions (canResumeAfterRestart, set only by the ACP adapter), the old turn may still be running agent-side and the load error cannot tell busy from gone. The continuation now fails with a message saying so instead of replacing the native session; the next message the user sends falls back as before. Agents that cannot reload sessions, and other providers, keep the fresh-session fallback. Refs pingdotgg/t3code#37287 Co-Authored-By: Claude Opus 5.5 (cherry picked from commit f9481951982411d1ab84b757afcbe56fe4700785) --- .../ProviderTurnStartService.test.ts | 41 +++++++++++++++++++ .../ProviderTurnStartService.ts | 22 ++++++++++ 2 files changed, 63 insertions(+) diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts index 0fc98a0256c29..747ea289750ed 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts @@ -476,6 +476,7 @@ it.each([ function makeLocalCommandHarness(input: { readonly text: string; readonly manualContinuation?: boolean; + readonly restartContinuation?: { readonly canResumeAfterRestart: boolean }; readonly nativeResumeUnsupported?: boolean; readonly steeringRestart?: boolean; readonly nativeGoals?: boolean; @@ -558,6 +559,7 @@ function makeLocalCommandHarness(input: { const messageId = MessageId.make("message-native-account-command"); const run: OrchestrationV2ThreadProjection["runs"][number] = { ...(input.manualContinuation ? { manualContinuationOfRunId: RunId.make("stopped-run") } : {}), + ...(input.restartContinuation ? { restartContinuationOfRunId: RunId.make("cut-run") } : {}), id: runId, threadId, ordinal: 2, @@ -1218,6 +1220,7 @@ function makeLocalCommandHarness(input: { const resumeFallbackSession = { driver: providerThread.driver, providerSession, + canResumeAfterRestart: input.restartContinuation?.canResumeAfterRestart, resumeThread: () => Effect.fail( input.resumeFailure ?? @@ -3670,6 +3673,44 @@ effectIt.effect.each(["previous", "current", "previous-failover"] as const)( }), ); +for (const canResumeAfterRestart of [true, false]) { + effectIt.effect( + `${canResumeAfterRestart ? "fails" : "falls back for"} a restart continuation whose agent ${canResumeAfterRestart ? "can" : "cannot"} reload sessions`, + () => + Effect.gen(function* () { + const harness = makeLocalCommandHarness({ + text: "Continue", + restartContinuation: { canResumeAfterRestart }, + resumeFailure: new ProviderAdapter.ProviderAdapterResumeThreadError({ + driver: ProviderDriverKind.make("acp"), + providerSessionId: ProviderSessionId.make("new-provider-session"), + providerThreadId: ProviderThreadId.make("new-provider-thread"), + cause: new Error("Internal error"), + }), + }); + + yield* Effect.exit(harness.start); + + if (!canResumeAfterRestart) { + expect(harness.fallbackEnsureThread).toHaveBeenCalledOnce(); + return; + } + expect(harness.fallbackEnsureThread).not.toHaveBeenCalled(); + expect(harness.startRootRun).not.toHaveBeenCalled(); + expect(harness.projection().runs.at(-1)).toMatchObject({ status: "failed" }); + expect(harness.projection().turnItems).toMatchObject([ + { + type: "error", + failure: { + message: + "The agent could not reopen this conversation after the restart, and its last turn may still be running there. Send a message to continue in a new session.", + }, + }, + ]); + }), + ); +} + effectIt.effect("does not mistake a failed state read for a superseded run", () => Effect.gen(function* () { const harness = makeLocalCommandHarness({ text: "Continue", failReadsAfterRunning: true }); diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts index 13751036181d9..03ae7d64d56c9 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts @@ -305,6 +305,9 @@ const RESUME_PROMPT = "Continue where you left off."; const START_GAVE_UP_MESSAGE = "T3 Code could not start this turn. Send the message again; if it keeps failing, check the server logs."; +const RESTART_RESUME_FAILED_MESSAGE = + "The agent could not reopen this conversation after the restart, and its last turn may still be running there. Send a message to continue in a new session."; + const startFailureMessage = (error: Error): string => { const nestedCause = "cause" in error ? error.cause : undefined; return ( @@ -2017,6 +2020,25 @@ export const layer: Layer.Layer< ) { return yield* loadFromProvider(Effect.fail(resumed.failure)); } + // ACP agents that restore sessions can outlive a backend restart, and their load errors + // cannot tell busy from gone. A fresh session could repeat the cut turn beside it. + if ( + resumed._tag === "Failure" && + !uncertainDelivery && + run.restartContinuationOfRunId !== undefined && + session.canResumeAfterRestart === true + ) { + return yield* loadFromProvider( + Effect.fail( + new ProviderAdapter.ProviderAdapterThreadInUseError({ + driver: session.driver, + providerThreadId: providerThread.id, + detail: RESTART_RESUME_FAILED_MESSAGE, + cause: resumed.failure, + }), + ), + ); + } yield* Effect.logWarning("Recovering provider context in a fresh native session", { driver: session.driver, From 9d3b2451221f0f99c7c86fa3cdd23f33ea605b06 Mon Sep 17 00:00:00 2001 From: Chris OHearn <137103567+chrisohearn@users.noreply.github.com> Date: Mon, 30 Nov 2026 20:03:52 -0700 Subject: [PATCH 2/3] fix(server): the ACP restart guard applies only after a real resume attempt A restart continuation whose native session lost the kept turns (a rolled-back turn on an agent that cannot truncate) gets a synthetic resume failure without calling resumeThread. The guard treated it as an ambiguous load error and blocked the intended fresh-session recovery. It now fires only when the resume was actually attempted. Co-Authored-By: Claude Opus 5.5 --- .../ProviderTurnStartService.test.ts | 97 ++++++++++++++++++- .../ProviderTurnStartService.ts | 9 +- 2 files changed, 102 insertions(+), 4 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts index 747ea289750ed..157b483f51117 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts @@ -476,7 +476,11 @@ it.each([ function makeLocalCommandHarness(input: { readonly text: string; readonly manualContinuation?: boolean; - readonly restartContinuation?: { readonly canResumeAfterRestart: boolean }; + readonly restartContinuation?: { + readonly canResumeAfterRestart: boolean; + /** The newest accepted turn on the native session was rolled back, and the agent cannot truncate. */ + readonly lacksKeptTurns?: boolean; + }; readonly nativeResumeUnsupported?: boolean; readonly steeringRestart?: boolean; readonly nativeGoals?: boolean; @@ -992,6 +996,59 @@ function makeLocalCommandHarness(input: { ), }; } + if (input.restartContinuation?.lacksKeptTurns === true) { + const keptAttemptId = RunAttemptId.make("kept-attempt"); + const rolledBackAttemptId = RunAttemptId.make("rolled-back-attempt"); + const priorRun = ( + id: string, + ordinal: number, + status: "completed" | "rolled_back", + attempt: RunAttemptId, + ) => ({ + ...run, + id: RunId.make(id), + ordinal, + status, + activeAttemptId: attempt, + providerThreadId, + }); + const priorAttempt = (id: RunAttemptId, runIdValue: string) => ({ + ...projection.attempts[0]!, + id, + runId: RunId.make(runIdValue), + providerThreadId, + status: "completed" as const, + }); + const priorTurn = (id: string, attempt: RunAttemptId, ordinal: number) => ({ + id: ProviderTurnId.make(id), + providerThreadId, + nodeId: rootNodeId, + runAttemptId: attempt, + ordinal, + nativeTurnRef: null, + status: "completed" as const, + startedAt: now, + completedAt: now, + }); + projection = { + ...projection, + runs: [ + priorRun("kept-run", 0, "completed", keptAttemptId), + priorRun("rolled-back-run", 1, "rolled_back", rolledBackAttemptId), + ...projection.runs, + ], + attempts: [ + priorAttempt(keptAttemptId, "kept-run"), + priorAttempt(rolledBackAttemptId, "rolled-back-run"), + ...projection.attempts, + ], + providerTurns: [ + ...projection.providerTurns, + priorTurn("kept-turn", keptAttemptId, 0), + priorTurn("rolled-back-turn", rolledBackAttemptId, 1), + ], + }; + } const earlierRunId = RunId.make("earlier-native-run"); const roleText = "You are the inbox. Reply only 'Queued for Jarvis: ' and never use tools."; @@ -1268,7 +1325,22 @@ function makeLocalCommandHarness(input: { : input.interruptOpen === true ? Effect.interrupt : "historyReadFailureAfterFallback" in input || "resumeFailure" in input - ? Effect.succeed({ ...resumeFallbackSession, providerSession } as never) + ? Effect.succeed({ + ...resumeFallbackSession, + providerSession: + input.restartContinuation?.lacksKeptTurns === true + ? { + ...providerSession, + capabilities: { + ...providerSession.capabilities, + checkpointing: { + ...providerSession.capabilities.checkpointing, + providerCanRollbackConversation: false, + }, + }, + } + : providerSession, + } as never) : "ensureThreadFailure" in input ? Effect.succeed({ driver: providerThread.driver, ensureThread } as never) : "openFailure" in input @@ -3711,6 +3783,27 @@ for (const canResumeAfterRestart of [true, false]) { ); } +effectIt.effect( + "falls back for a restart continuation whose native session lost the kept turns", + () => + Effect.gen(function* () { + const harness = makeLocalCommandHarness({ + text: "Continue", + restartContinuation: { canResumeAfterRestart: true, lacksKeptTurns: true }, + resumeFailure: new ProviderAdapter.ProviderAdapterResumeThreadError({ + driver: ProviderDriverKind.make("acp"), + providerSessionId: ProviderSessionId.make("new-provider-session"), + providerThreadId: ProviderThreadId.make("new-provider-thread"), + cause: new Error("Internal error"), + }), + }); + + yield* Effect.exit(harness.start); + + expect(harness.fallbackEnsureThread).toHaveBeenCalledOnce(); + }), +); + effectIt.effect("does not mistake a failed state read for a superseded run", () => Effect.gen(function* () { const harness = makeLocalCommandHarness({ text: "Continue", failReadsAfterRunning: true }); diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts index 03ae7d64d56c9..f8f40f69d97b4 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts @@ -1949,8 +1949,13 @@ export const layer: Layer.Layer< handoff.toProviderThreadId === providerThread.id && handoff.strategy === "manual_context", ); + const resumeAttempted = !( + uncertainDelivery || + nativeSessionLacksKeptTurns || + rejectedHistory + ); const resumed = yield* Effect.result( - uncertainDelivery || nativeSessionLacksKeptTurns || rejectedHistory + !resumeAttempted ? Effect.fail( new ProviderAdapter.ProviderAdapterTurnStartError({ driver: session.driver, @@ -2024,7 +2029,7 @@ export const layer: Layer.Layer< // cannot tell busy from gone. A fresh session could repeat the cut turn beside it. if ( resumed._tag === "Failure" && - !uncertainDelivery && + resumeAttempted && run.restartContinuationOfRunId !== undefined && session.canResumeAfterRestart === true ) { From 768335d5e2bdc63787be4bcad03640da37832e73 Mon Sep 17 00:00:00 2001 From: Chris OHearn <137103567+chrisohearn@users.noreply.github.com> Date: Mon, 30 Nov 2026 20:16:18 -0700 Subject: [PATCH 3/3] fix(server): the ACP restart guard skips note-only continuations and keeps actionable refusals A restart continuation that only delivers a restart note has no cut turn to repeat, so it keeps the fresh-session recovery. A resume refusal with a known remedy (for example an unaccepted model) now reaches the user as is instead of the generic restart message; neither case opens a second session. Co-Authored-By: Claude Opus 5.5 --- .../ProviderTurnStartService.test.ts | 67 +++++++++++++++++++ .../ProviderTurnStartService.ts | 5 ++ 2 files changed, 72 insertions(+) diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts index 157b483f51117..c369b5a17da65 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.test.ts @@ -480,6 +480,8 @@ function makeLocalCommandHarness(input: { readonly canResumeAfterRestart: boolean; /** The newest accepted turn on the native session was rolled back, and the agent cannot truncate. */ readonly lacksKeptTurns?: boolean; + /** The cut run only left a restart note to deliver (cancelled background work), no cut turn. */ + readonly noteOnly?: boolean; }; readonly nativeResumeUnsupported?: boolean; readonly steeringRestart?: boolean; @@ -1049,6 +1051,24 @@ function makeLocalCommandHarness(input: { ], }; } + if (input.restartContinuation?.noteOnly === true) { + projection = { + ...projection, + runs: [ + { + ...run, + id: RunId.make("cut-run"), + ordinal: 1, + status: "completed" as const, + providerThreadId, + restartCancelledBackgroundWork: [ + { kind: "shell" as const, label: "sleep 25", awaited: true as const }, + ], + }, + ...projection.runs, + ], + }; + } const earlierRunId = RunId.make("earlier-native-run"); const roleText = "You are the inbox. Reply only 'Queued for Jarvis: ' and never use tools."; @@ -3804,6 +3824,53 @@ effectIt.effect( }), ); +effectIt.effect("falls back for a restart continuation that only delivers a restart note", () => + Effect.gen(function* () { + const harness = makeLocalCommandHarness({ + text: "Continue", + restartContinuation: { canResumeAfterRestart: true, noteOnly: true }, + resumeFailure: new ProviderAdapter.ProviderAdapterResumeThreadError({ + driver: ProviderDriverKind.make("acp"), + providerSessionId: ProviderSessionId.make("new-provider-session"), + providerThreadId: ProviderThreadId.make("new-provider-thread"), + cause: new Error("Internal error"), + }), + }); + + yield* Effect.exit(harness.start); + + expect(harness.fallbackEnsureThread).toHaveBeenCalledOnce(); + }), +); + +effectIt.effect("keeps an actionable resume refusal for a restart continuation", () => + Effect.gen(function* () { + const refusal = Object.assign( + new Error( + 'The agent did not accept the model "gone". Choose another model for this provider, then retry.', + ), + { _tag: "AcpModelSelectionError" }, + ); + const harness = makeLocalCommandHarness({ + text: "Continue", + restartContinuation: { canResumeAfterRestart: true }, + resumeFailure: new ProviderAdapter.ProviderAdapterResumeThreadError({ + driver: ProviderDriverKind.make("acp"), + providerSessionId: ProviderSessionId.make("new-provider-session"), + providerThreadId: ProviderThreadId.make("new-provider-thread"), + cause: refusal, + }), + }); + + yield* Effect.exit(harness.start); + + expect(harness.fallbackEnsureThread).not.toHaveBeenCalled(); + expect(harness.projection().turnItems).toMatchObject([ + { type: "error", failure: { message: refusal.message } }, + ]); + }), +); + effectIt.effect("does not mistake a failed state read for a superseded run", () => Effect.gen(function* () { const harness = makeLocalCommandHarness({ text: "Continue", failReadsAfterRunning: true }); diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts index f8f40f69d97b4..ab7f211460695 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts @@ -2031,8 +2031,13 @@ export const layer: Layer.Layer< resumed._tag === "Failure" && resumeAttempted && run.restartContinuationOfRunId !== undefined && + !noteContinuation && session.canResumeAfterRestart === true ) { + // An actionable refusal, such as a model the agent no longer accepts, keeps its own remedy. + if (knownProviderFailureMessage(resumed.failure) !== undefined) { + return yield* loadFromProvider(Effect.fail(resumed.failure)); + } return yield* loadFromProvider( Effect.fail( new ProviderAdapter.ProviderAdapterThreadInUseError({