From c8a0ff5620acf895570142443ac3551f3f563d1a Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 17 Nov 2026 05:51:08 +0000 Subject: [PATCH 1/5] test(server): a missed run's image reaches the retry turn Runs the provider never accepted are replayed into the next turn as a text-only history, so an image on one of them is dropped. Fails on main for #37933. Co-Authored-By: Claude Opus 5.5 --- .../testkit/ProviderSwitch.integration.test.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts b/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts index 289e99b414990..a6b4100e7d4ea 100644 --- a/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts +++ b/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts @@ -1987,6 +1987,13 @@ describe("orchestration v2 provider switching", () => { yield* Effect.gen(function* () { const orchestrator = yield* Orchestrator.OrchestratorV2; const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; + const missedImage: ChatAttachment = { + type: "image", + id: "refused-start-image", + name: "screenshot.png", + mimeType: "image/png", + sizeBytes: 100_000, + }; const run = Effect.fn("run")(function* (ordinal: number) { yield* orchestrator.dispatch({ type: "message.dispatch", @@ -1996,7 +2003,7 @@ describe("orchestration v2 provider switching", () => { createdBy: "user", creationSource: "web", text: `Request ${ordinal}`, - attachments: [], + attachments: ordinal === 2 ? [missedImage] : [], modelSelection: CLAUDE_MODEL_SELECTION, dispatchMode: { type: "start_immediately" }, }); @@ -2038,6 +2045,7 @@ describe("orchestration v2 provider switching", () => { assert.equal(turns[1]?.nativeThreadId, turns[0]?.nativeThreadId); assert.include(turns[1]?.text, "Request 2"); assert.include(turns[1]?.text, "Request 3"); + assert.deepEqual(turns[1]?.attachments, [missedImage]); }).pipe( Effect.provide( ProviderReplayHarness.layerWithRegistry( From 70e4ec97cbd84630981fa8b11d8e37841272fb38 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 17 Nov 2026 06:01:14 +0000 Subject: [PATCH 2/5] fix(server): images on runs the provider never started reach the next turn A run that failed or was interrupted before the provider accepted it is replayed into the next turn as a text-only history handoff, so an image on that message was dropped and the agent asked for it again. The retry handoff now also returns the missed messages' attachments, and the next prompt carries them ahead of its own (skipping any it already sends). Compaction and goal commands still defer them with the history. Fixes pingdotgg/t3code#37933 Co-Authored-By: Claude Opus 5.5 --- .../ProviderTurnStartService.ts | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts index 7764bca5cca0c..274443181971c 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts @@ -1100,7 +1100,7 @@ export const layer: Layer.Layer< !coveredItemIds.has(item.id) && historicalMessage(item) !== null, ); - const retryHandoff = + const handoffs = missedItems.length === 0 ? [] : [ @@ -1122,7 +1122,11 @@ export const layer: Layer.Layer< createdAt: yield* DateTime.now, }), ]; - return retryHandoff; + // The handoff carries only text; images the provider never received ride on this turn. + const attachments = missedItems.flatMap((item) => + item.type === "user_message" ? item.attachments : [], + ); + return { handoffs, attachments }; }, ); const prepareRecoveryHandoff = Effect.fn("ProviderTurnStartService.prepareRecoveryHandoff")( @@ -1181,7 +1185,7 @@ export const layer: Layer.Layer< ...effectiveHandoffs.filter((candidate) => candidate.targetRunId === run.id), ]; }; - let resumedRetryHandoff: ReadonlyArray | undefined; + let resumedRetryHandoff: Effect.Success> | undefined; let resumedHandoffContext: ReturnType | undefined; let portableForkFallback: OrchestrationV2ContextHandoff | undefined; let recoveryTurnItem: OrchestrationV2TurnItem | undefined; @@ -1421,7 +1425,7 @@ export const layer: Layer.Layer< resumedHandoffContext = context; resumedRetryHandoff = yield* prepareRetryHandoff(context); const fits = yield* deliverContextHandoffs({ - handoffs: [...handoffs, ...resumedRetryHandoff], + handoffs: [...handoffs, ...resumedRetryHandoff.handoffs], providerThread: { ...resumed.success, id: providerThread.id }, budget: context.budget, deferInline: isCompactionCommandMessage(providerMessage) || goalCommand, @@ -1468,7 +1472,7 @@ export const layer: Layer.Layer< } } resumedHandoffContext = undefined; - resumedRetryHandoff = []; + resumedRetryHandoff = { handoffs: [], attachments: [] }; if (!(yield* isCurrentAttemptInStatus("starting"))) return undefined; // Another client is still writing to the native thread. A fresh // session here would silently split the conversation from it. @@ -1793,7 +1797,7 @@ export const layer: Layer.Layer< const startWithHandoffs = Effect.fn("ProviderTurnStartService.startWithHandoffs")( function* (turnInput: Parameters[0], compact = false) { const delivery = yield* deliverContextHandoffs({ - handoffs: [...effectiveHandoffs, ...retryHandoff], + handoffs: [...effectiveHandoffs, ...retryHandoff.handoffs], deferInline: compact || goalCommand, providerThread: runningProviderThread, budget, @@ -1832,6 +1836,19 @@ export const layer: Layer.Layer< message: { ...turnInput.message, text, + ...(compact || goalCommand || retryHandoff.attachments.length === 0 + ? {} + : { + attachments: [ + ...retryHandoff.attachments.filter( + (missed) => + !turnInput.message.attachments.some( + (current) => current.id === missed.id, + ), + ), + ...turnInput.message.attachments, + ], + }), }, ...(text === userText ? {} @@ -1892,7 +1909,7 @@ export const layer: Layer.Layer< ); const deliverySession = effectiveHandoffs.length === 0 && - retryHandoff.length === 0 && + retryHandoff.handoffs.length === 0 && sideChatHandoff === undefined && restartNote === "" && !noteContinuation From 5d5a4e4cb116d488defb4ef5f560449f10b4cf2b Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 17 Nov 2026 06:26:18 +0000 Subject: [PATCH 3/5] fix(server): missed images survive a refused start after history injection Native history injection settles the missed messages' text before the prompt runs, so a refused prompt left their images covered and dropped. Missed images now ride on prompts until one is accepted, independent of text coverage; compaction and /goal turns do not count as delivering them. Co-Authored-By: Claude Opus 5.5 --- .../ProviderTurnStartService.ts | 43 +++++++++++++------ .../ProviderSwitch.integration.test.ts | 15 +++++-- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts index 274443181971c..a3e4c806cd3b8 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts @@ -1087,19 +1087,16 @@ export const layer: Layer.Layer< }; const prepareRetryHandoff = Effect.fn("ProviderTurnStartService.prepareRetryHandoff")( function* (context: ReturnType) { - const { missedRunIds, coveredItemIds, missedRuns } = context; - const missedItems = + const { missedRunIds, coveredItemIds, missedRuns, acceptedAttempts } = context; + const missedHistory = missedRunIds.size === 0 ? [] : (yield* projectionStore.getTurnStartHistory(input.threadId, [ ...missedRunIds, - ])).filter( - (item) => - item.runId !== null && - missedRunIds.has(item.runId) && - !coveredItemIds.has(item.id) && - historicalMessage(item) !== null, - ); + ])).filter((item) => item.runId !== null && missedRunIds.has(item.runId)); + const missedItems = missedHistory.filter( + (item) => !coveredItemIds.has(item.id) && historicalMessage(item) !== null, + ); const handoffs = missedItems.length === 0 ? [] @@ -1122,9 +1119,30 @@ export const layer: Layer.Layer< createdAt: yield* DateTime.now, }), ]; - // The handoff carries only text; images the provider never received ride on this turn. - const attachments = missedItems.flatMap((item) => - item.type === "user_message" ? item.attachments : [], + // History delivery (inline or injected) is text only, so missed images ride on prompts + // until one is accepted. Compaction and /goal starts carry none. + const lastPromptOrdinal = Math.max( + 0, + ...acceptedAttempts.flatMap((attempt) => { + const source = projection.runs.find((candidate) => candidate.id === attempt.runId); + const message = projection.messages.find( + (candidate) => candidate.id === source?.userMessageId, + ); + return source === undefined || + (message !== undefined && + (isCompactionCommandMessage(message) || + (goalDriver && parseGoalCommand(message) !== null))) + ? [] + : [source.ordinal]; + }), + ); + const attachments = missedHistory.flatMap((item) => + item.type === "user_message" && + missedRuns.some( + (source) => source.id === item.runId && source.ordinal > lastPromptOrdinal, + ) + ? item.attachments + : [], ); return { handoffs, attachments }; }, @@ -1910,6 +1928,7 @@ export const layer: Layer.Layer< const deliverySession = effectiveHandoffs.length === 0 && retryHandoff.handoffs.length === 0 && + retryHandoff.attachments.length === 0 && sideChatHandoff === undefined && restartNote === "" && !noteContinuation diff --git a/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts b/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts index a6b4100e7d4ea..7aa0a1a2631fb 100644 --- a/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts +++ b/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts @@ -1965,10 +1965,17 @@ describe("orchestration v2 provider switching", () => { detail: "Claude provider query ended before the turn could start.", }), ], - ] as const)("keeps the native session after turns refused for %s", ([, refusalCause]) => + // Injected history is text only, so a refusal after injection must not settle the image. + [ + "background work after native history injection", + new ClaudeBackgroundWorkBlocksQueryReplacementError(), + true, + ], + ] as const)("keeps the native session after turns refused for %s", ([, refusalCause, inject]) => Effect.gen(function* () { const cwd = yield* checkpointWorkspace("refused-start-keeps-native"); const capturedTurns = yield* Ref.make>([]); + const injectedHistory = yield* Ref.make>([]); const refuseStarts = yield* Ref.make(0); const generation = yield* Ref.make(0); const registry = ProviderAdapterRegistry.layerFromAdapters([ @@ -1982,6 +1989,7 @@ describe("orchestration v2 provider switching", () => { refuseStarts, refusalCause, nativeThreadGeneration: generation, + ...(inject === true ? { injectedHistory } : {}), }), ]); yield* Effect.gen(function* () { @@ -2043,8 +2051,9 @@ describe("orchestration v2 provider switching", () => { assert.equal(turns.length, 2); assert.equal(yield* Ref.get(generation), 1); assert.equal(turns[1]?.nativeThreadId, turns[0]?.nativeThreadId); - assert.include(turns[1]?.text, "Request 2"); - assert.include(turns[1]?.text, "Request 3"); + const delivered = `${turns[1]?.text}${JSON.stringify(yield* Ref.get(injectedHistory))}`; + assert.include(delivered, "Request 2"); + assert.include(delivered, "Request 3"); assert.deepEqual(turns[1]?.attachments, [missedImage]); }).pipe( Effect.provide( From d9c99c16666e2415f70d6e77fd0ef7b0706a5be7 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 17 Nov 2026 06:41:53 +0000 Subject: [PATCH 4/5] fix(server): Resume prompts when missed images are pending A native resume sends no prompt, so pending missed images were left behind while the accepted continuation marked them delivered. Pending images now take the prompted start path, as pending history already does. Co-Authored-By: Claude Opus 5.5 --- .../ProviderTurnStartService.ts | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts index a3e4c806cd3b8..b1f6393e86e72 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts @@ -1846,27 +1846,26 @@ export const layer: Layer.Layer< const startInput = noteContinuation ? promptedInput : turnInput; const { resume: _resume, ...contextInput } = startInput; const { message: resumeMessage, resume: _resumeIntent, ...resumeInput } = startInput; + const missedAttachments = + compact || goalCommand + ? [] + : retryHandoff.attachments.filter( + (missed) => + !turnInput.message.attachments.some((current) => current.id === missed.id), + ); + // Like pending history, pending images need a prompt; native resume carries none. + const prompted = context !== "" || missedAttachments.length > 0; const operation = - turnInput.resume && context === "" && session.resumeTurn !== undefined + turnInput.resume && !prompted && session.resumeTurn !== undefined ? session.resumeTurn({ ...resumeInput, messageId: resumeMessage.messageId }) : start({ - ...(context === "" ? startInput : contextInput), + ...(prompted ? contextInput : startInput), message: { ...turnInput.message, text, - ...(compact || goalCommand || retryHandoff.attachments.length === 0 + ...(missedAttachments.length === 0 ? {} - : { - attachments: [ - ...retryHandoff.attachments.filter( - (missed) => - !turnInput.message.attachments.some( - (current) => current.id === missed.id, - ), - ), - ...turnInput.message.attachments, - ], - }), + : { attachments: [...missedAttachments, ...turnInput.message.attachments] }), }, ...(text === userText ? {} From bff89c0f1aee00225d56891bd95c07f27c647e18 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 17 Nov 2026 06:55:07 +0000 Subject: [PATCH 5/5] fix(server): steering images on a missed run wait for a later accepted prompt A missed run can hold an accepted attempt and a later steering restart the provider never accepted. The accepted attempt no longer counts as delivering that run's images. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/orchestration-v2/ProviderTurnStartService.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts index b1f6393e86e72..942874f1e91f1 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts @@ -1120,10 +1120,12 @@ export const layer: Layer.Layer< }), ]; // History delivery (inline or injected) is text only, so missed images ride on prompts - // until one is accepted. Compaction and /goal starts carry none. + // until a later run's is accepted. Compaction and /goal starts carry none. A missed run's + // own accepted attempt predates its unsent steering input, so it does not count. const lastPromptOrdinal = Math.max( 0, ...acceptedAttempts.flatMap((attempt) => { + if (missedRunIds.has(attempt.runId)) return []; const source = projection.runs.find((candidate) => candidate.id === attempt.runId); const message = projection.messages.find( (candidate) => candidate.id === source?.userMessageId,