diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts index 7764bca5cca0c..942874f1e91f1 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts @@ -1087,20 +1087,17 @@ 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, - ); - const retryHandoff = + ])).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,7 +1119,34 @@ export const layer: Layer.Layer< createdAt: yield* DateTime.now, }), ]; - return retryHandoff; + // History delivery (inline or injected) is text only, so missed images ride on prompts + // 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, + ); + 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 }; }, ); const prepareRecoveryHandoff = Effect.fn("ProviderTurnStartService.prepareRecoveryHandoff")( @@ -1181,7 +1205,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 +1445,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 +1492,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 +1817,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, @@ -1824,14 +1848,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, + ...(missedAttachments.length === 0 + ? {} + : { attachments: [...missedAttachments, ...turnInput.message.attachments] }), }, ...(text === userText ? {} @@ -1892,7 +1928,8 @@ export const layer: Layer.Layer< ); const deliverySession = effectiveHandoffs.length === 0 && - retryHandoff.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 289e99b414990..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,11 +1989,19 @@ describe("orchestration v2 provider switching", () => { refuseStarts, refusalCause, nativeThreadGeneration: generation, + ...(inject === true ? { injectedHistory } : {}), }), ]); 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 +2011,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" }, }); @@ -2036,8 +2051,10 @@ 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( ProviderReplayHarness.layerWithRegistry(