@devries-cameron20 / t3code Mirror · read-only / commits / c2eb8f3369
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 08:30 UTC

fix(server): images on runs the provider never started reach the next turn (#37941)

Julius Marminge committed Nov 16, 2026 at 23:27 UTC c2eb8f3369de3cee2bd3b9e65a0cb807927972ae
2 files changed +76 -22
apps/server/src/orchestration-v2/ProviderTurnStartService.ts
+55 -18
@@ -1087,20 +1087,17 @@ export const layer: Layer.Layer<
1087 };
1088 const prepareRetryHandoff = Effect.fn("ProviderTurnStartService.prepareRetryHandoff")(
1089 function* (context: ReturnType<typeof handoffContextForThread>) {
1090 - const { missedRunIds, coveredItemIds, missedRuns } = context;
1091 - const missedItems =
1090 + const { missedRunIds, coveredItemIds, missedRuns, acceptedAttempts } = context;
1091 + const missedHistory =
1092 missedRunIds.size === 0
1093 ? []
1094 : (yield* projectionStore.getTurnStartHistory(input.threadId, [
1095 ...missedRunIds,
1096 - ])).filter(
1097 - (item) =>
1098 - item.runId !== null &&
1099 - missedRunIds.has(item.runId) &&
1100 - !coveredItemIds.has(item.id) &&
1101 - historicalMessage(item) !== null,
1102 - );
1103 - const retryHandoff =
1096 + ])).filter((item) => item.runId !== null && missedRunIds.has(item.runId));
1097 + const missedItems = missedHistory.filter(
1098 + (item) => !coveredItemIds.has(item.id) && historicalMessage(item) !== null,
1099 + );
1100 + const handoffs =
1101 missedItems.length === 0
1102 ? []
1103 : [
@@ -1122,7 +1119,34 @@ export const layer: Layer.Layer<
1119 createdAt: yield* DateTime.now,
1120 }),
1121 ];
1125 - return retryHandoff;
1122 + // History delivery (inline or injected) is text only, so missed images ride on prompts
1123 + // until a later run's is accepted. Compaction and /goal starts carry none. A missed run's
1124 + // own accepted attempt predates its unsent steering input, so it does not count.
1125 + const lastPromptOrdinal = Math.max(
1126 + 0,
1127 + ...acceptedAttempts.flatMap((attempt) => {
1128 + if (missedRunIds.has(attempt.runId)) return [];
1129 + const source = projection.runs.find((candidate) => candidate.id === attempt.runId);
1130 + const message = projection.messages.find(
1131 + (candidate) => candidate.id === source?.userMessageId,
1132 + );
1133 + return source === undefined ||
1134 + (message !== undefined &&
1135 + (isCompactionCommandMessage(message) ||
1136 + (goalDriver && parseGoalCommand(message) !== null)))
1137 + ? []
1138 + : [source.ordinal];
1139 + }),
1140 + );
1141 + const attachments = missedHistory.flatMap((item) =>
1142 + item.type === "user_message" &&
1143 + missedRuns.some(
1144 + (source) => source.id === item.runId && source.ordinal > lastPromptOrdinal,
1145 + )
1146 + ? item.attachments
1147 + : [],
1148 + );
1149 + return { handoffs, attachments };
1150 },
1151 );
1152 const prepareRecoveryHandoff = Effect.fn("ProviderTurnStartService.prepareRecoveryHandoff")(
@@ -1181,7 +1205,7 @@ export const layer: Layer.Layer<
1205 ...effectiveHandoffs.filter((candidate) => candidate.targetRunId === run.id),
1206 ];
1207 };
1184 - let resumedRetryHandoff: ReadonlyArray<OrchestrationV2ContextHandoff> | undefined;
1208 + let resumedRetryHandoff: Effect.Success<ReturnType<typeof prepareRetryHandoff>> | undefined;
1209 let resumedHandoffContext: ReturnType<typeof handoffContextForThread> | undefined;
1210 let portableForkFallback: OrchestrationV2ContextHandoff | undefined;
1211 let recoveryTurnItem: OrchestrationV2TurnItem | undefined;
@@ -1423,7 +1447,7 @@ export const layer: Layer.Layer<
1447 resumedHandoffContext = context;
1448 resumedRetryHandoff = yield* prepareRetryHandoff(context);
1449 const fits = yield* deliverContextHandoffs({
1426 - handoffs: [...handoffs, ...resumedRetryHandoff],
1450 + handoffs: [...handoffs, ...resumedRetryHandoff.handoffs],
1451 providerThread: { ...resumed.success, id: providerThread.id },
1452 budget: context.budget,
1453 deferInline: isCompactionCommandMessage(providerMessage) || goalCommand,
@@ -1470,7 +1494,7 @@ export const layer: Layer.Layer<
1494 }
1495 }
1496 resumedHandoffContext = undefined;
1473 - resumedRetryHandoff = [];
1497 + resumedRetryHandoff = { handoffs: [], attachments: [] };
1498 if (!(yield* isCurrentAttemptInStatus("starting"))) return undefined;
1499 // Another client is still writing to the native thread. A fresh
1500 // session here would silently split the conversation from it.
@@ -1795,7 +1819,7 @@ export const layer: Layer.Layer<
1819 const startWithHandoffs = Effect.fn("ProviderTurnStartService.startWithHandoffs")(
1820 function* (turnInput: Parameters<typeof session.startTurn>[0], compact = false) {
1821 const delivery = yield* deliverContextHandoffs({
1798 - handoffs: [...effectiveHandoffs, ...retryHandoff],
1822 + handoffs: [...effectiveHandoffs, ...retryHandoff.handoffs],
1823 deferInline: compact || goalCommand,
1824 providerThread: runningProviderThread,
1825 budget,
@@ -1826,14 +1850,26 @@ export const layer: Layer.Layer<
1850 const startInput = noteContinuation ? promptedInput : turnInput;
1851 const { resume: _resume, ...contextInput } = startInput;
1852 const { message: resumeMessage, resume: _resumeIntent, ...resumeInput } = startInput;
1853 + const missedAttachments =
1854 + compact || goalCommand
1855 + ? []
1856 + : retryHandoff.attachments.filter(
1857 + (missed) =>
1858 + !turnInput.message.attachments.some((current) => current.id === missed.id),
1859 + );
1860 + // Like pending history, pending images need a prompt; native resume carries none.
1861 + const prompted = context !== "" || missedAttachments.length > 0;
1862 const operation =
1830 - turnInput.resume && context === "" && session.resumeTurn !== undefined
1863 + turnInput.resume && !prompted && session.resumeTurn !== undefined
1864 ? session.resumeTurn({ ...resumeInput, messageId: resumeMessage.messageId })
1865 : start({
1833 - ...(context === "" ? startInput : contextInput),
1866 + ...(prompted ? contextInput : startInput),
1867 message: {
1868 ...turnInput.message,
1869 text,
1870 + ...(missedAttachments.length === 0
1871 + ? {}
1872 + : { attachments: [...missedAttachments, ...turnInput.message.attachments] }),
1873 },
1874 ...(text === userText
1875 ? {}
@@ -1894,7 +1930,8 @@ export const layer: Layer.Layer<
1930 );
1931 const deliverySession =
1932 effectiveHandoffs.length === 0 &&
1897 - retryHandoff.length === 0 &&
1933 + retryHandoff.handoffs.length === 0 &&
1934 + retryHandoff.attachments.length === 0 &&
1935 sideChatHandoff === undefined &&
1936 restartNote === "" &&
1937 !noteContinuation
apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts
+21 -4
@@ -1965,10 +1965,17 @@ describe("orchestration v2 provider switching", () => {
1965 detail: "Claude provider query ended before the turn could start.",
1966 }),
1967 ],
1968 - ] as const)("keeps the native session after turns refused for %s", ([, refusalCause]) =>
1968 + // Injected history is text only, so a refusal after injection must not settle the image.
1969 + [
1970 + "background work after native history injection",
1971 + new ClaudeBackgroundWorkBlocksQueryReplacementError(),
1972 + true,
1973 + ],
1974 + ] as const)("keeps the native session after turns refused for %s", ([, refusalCause, inject]) =>
1975 Effect.gen(function* () {
1976 const cwd = yield* checkpointWorkspace("refused-start-keeps-native");
1977 const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
1978 + const injectedHistory = yield* Ref.make<ReadonlyArray<unknown>>([]);
1979 const refuseStarts = yield* Ref.make(0);
1980 const generation = yield* Ref.make(0);
1981 const registry = ProviderAdapterRegistry.layerFromAdapters([
@@ -1982,11 +1989,19 @@ describe("orchestration v2 provider switching", () => {
1989 refuseStarts,
1990 refusalCause,
1991 nativeThreadGeneration: generation,
1992 + ...(inject === true ? { injectedHistory } : {}),
1993 }),
1994 ]);
1995 yield* Effect.gen(function* () {
1996 const orchestrator = yield* Orchestrator.OrchestratorV2;
1997 const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
1998 + const missedImage: ChatAttachment = {
1999 + type: "image",
2000 + id: "refused-start-image",
2001 + name: "screenshot.png",
2002 + mimeType: "image/png",
2003 + sizeBytes: 100_000,
2004 + };
2005 const run = Effect.fn("run")(function* (ordinal: number) {
2006 yield* orchestrator.dispatch({
2007 type: "message.dispatch",
@@ -1996,7 +2011,7 @@ describe("orchestration v2 provider switching", () => {
2011 createdBy: "user",
2012 creationSource: "web",
2013 text: `Request ${ordinal}`,
1999 - attachments: [],
2014 + attachments: ordinal === 2 ? [missedImage] : [],
2015 modelSelection: CLAUDE_MODEL_SELECTION,
2016 dispatchMode: { type: "start_immediately" },
2017 });
@@ -2036,8 +2051,10 @@ describe("orchestration v2 provider switching", () => {
2051 assert.equal(turns.length, 2);
2052 assert.equal(yield* Ref.get(generation), 1);
2053 assert.equal(turns[1]?.nativeThreadId, turns[0]?.nativeThreadId);
2039 - assert.include(turns[1]?.text, "Request 2");
2040 - assert.include(turns[1]?.text, "Request 3");
2054 + const delivered = `${turns[1]?.text}${JSON.stringify(yield* Ref.get(injectedHistory))}`;
2055 + assert.include(delivered, "Request 2");
2056 + assert.include(delivered, "Request 3");
2057 + assert.deepEqual(turns[1]?.attachments, [missedImage]);
2058 }).pipe(
2059 Effect.provide(
2060 ProviderReplayHarness.layerWithRegistry(