GeezerVison is showing github.com — fix(server): Claude threads no longer get stuck after background comm… · shardfluxdev/t3code@efecd3c
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/claudeModelOptions.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,7 +37,7 @@ export function compileClaudeModelSelection( | 37 | 37 | const resolvedEffort = resolveClaudeCatalogEffort(catalog, selection.model, rawEffort); | 38 | 38 | const effort = normalizeClaudeCatalogEffort(catalog, resolvedEffort, selection.model); | 39 | 39 | const fastMode = supportsBoolean("fastMode") | 40 | | - ? getModelSelectionBooleanOptionValue(selection, "fastMode") | | 40 | + ? (getModelSelectionBooleanOptionValue(selection, "fastMode") ?? false) | 41 | 41 | : undefined; | 42 | 42 | const thinking = supportsBoolean("thinking") | 43 | 43 | ? getModelSelectionBooleanOptionValue(selection, "thinking") | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +76-1Lines changed: 76 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -126,6 +126,7 @@ function makeClaudeTestTurnInput(input: { | 126 | 126 | readonly text: string; | 127 | 127 | readonly attachments: ProviderAdapterV2TurnInput["message"]["attachments"]; | 128 | 128 | readonly providerTurnOrdinal?: number; | | 129 | + readonly nativeThreadHasTurns?: boolean; | 129 | 130 | readonly messageCreatedBy?: ProviderAdapterV2TurnInput["message"]["createdBy"]; | 130 | 131 | readonly messageCreationSource?: ProviderAdapterV2TurnInput["message"]["creationSource"]; | 131 | 132 | readonly modelSelection?: ModelSelection; | @@ -137,6 +138,9 @@ function makeClaudeTestTurnInput(input: { | 137 | 138 | runId: RunId.make(`run-${input.attemptId}`), | 138 | 139 | runOrdinal: 1, | 139 | 140 | providerTurnOrdinal: input.providerTurnOrdinal ?? 1, | | 141 | + ...(input.nativeThreadHasTurns === undefined | | 142 | + ? {} | | 143 | + : { nativeThreadHasTurns: input.nativeThreadHasTurns }), | 140 | 144 | attemptId: input.attemptId, | 141 | 145 | rootNodeId: NodeId.make(`node-${input.attemptId}`), | 142 | 146 | providerThread: input.providerThread, | @@ -1783,7 +1787,7 @@ describe("ClaudeAdapterV2 native fork", () => { | 1783 | 1787 | }); | 1784 | 1788 |
| 1785 | 1789 | describe("ClaudeAdapterV2 native session identity", () => { | 1786 | | - const openTurnWithOrdinal = (providerTurnOrdinal: number) => | | 1790 | + const openTurnWithOrdinal = (providerTurnOrdinal: number, nativeThreadHasTurns?: boolean) => | 1787 | 1791 | Effect.scoped( | 1788 | 1792 | Effect.gen(function* () { | 1789 | 1793 | const fileSystem = yield* FileSystem.FileSystem; | @@ -1842,6 +1846,7 @@ describe("ClaudeAdapterV2 native session identity", () => { | 1842 | 1846 | text: "Respond with identity ok", | 1843 | 1847 | attachments: [], | 1844 | 1848 | providerTurnOrdinal, | | 1849 | + ...(nativeThreadHasTurns === undefined ? {} : { nativeThreadHasTurns }), | 1845 | 1850 | }), | 1846 | 1851 | ); | 1847 | 1852 | return openedQueries; | @@ -1867,6 +1872,14 @@ describe("ClaudeAdapterV2 native session identity", () => { | 1867 | 1872 | assert.equal(openedQueries[0]?.options.sessionId, undefined); | 1868 | 1873 | }), | 1869 | 1874 | ); | | 1875 | + | | 1876 | + it.effect("creates a fresh native session despite earlier provider-thread turns", () => | | 1877 | + Effect.gen(function* () { | | 1878 | + const openedQueries = yield* openTurnWithOrdinal(4, false); | | 1879 | + assert.equal(openedQueries[0]?.options.sessionId, "native-session-identity"); | | 1880 | + assert.equal(openedQueries[0]?.options.resume, undefined); | | 1881 | + }), | | 1882 | + ); | 1870 | 1883 | }); | 1871 | 1884 |
| 1872 | 1885 | const encodeJsonString = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown)); | @@ -2182,6 +2195,68 @@ describe("ClaudeAdapterV2 background wake turns", () => { | 2182 | 2195 | }); | 2183 | 2196 | const makeWakeHarness = makeWakeHarnessWithOptions(); | 2184 | 2197 |
| | 2198 | + it.effect( | | 2199 | + "reuses a background shell's query for omitted and explicit Normal, but blocks Fast", | | 2200 | + () => | | 2201 | + Effect.scoped( | | 2202 | + Effect.gen(function* () { | | 2203 | + const harness = yield* makeWakeHarness; | | 2204 | + const now = yield* DateTime.now; | | 2205 | + const normal = { | | 2206 | + instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID, | | 2207 | + model: "claude-opus-5-5", | | 2208 | + } satisfies ModelSelection; | | 2209 | + const turn = (ordinal: number, modelSelection: ModelSelection) => | | 2210 | + makeClaudeTestTurnInput({ | | 2211 | + threadId: harness.threadId, | | 2212 | + providerThread: harness.providerThread, | | 2213 | + now, | | 2214 | + attemptId: RunAttemptId.make(`attempt-normal-background:${ordinal}`), | | 2215 | + text: `Request ${ordinal}`, | | 2216 | + attachments: [], | | 2217 | + providerTurnOrdinal: ordinal, | | 2218 | + modelSelection, | | 2219 | + }); | | 2220 | + yield* harness.runtime.startTurn(turn(1, normal)); | | 2221 | + const originalOptions = harness.getOpenedOptions(); | | 2222 | + yield* harness.offerAndWait(wakeTaskStarted); | | 2223 | + yield* harness.offerAndWait(turnOneResult); | | 2224 | + yield* Queue.take(harness.terminalReceipts); | | 2225 | + assert.isTrue(yield* harness.hasPendingBackgroundWork); | | 2226 | + | | 2227 | + yield* harness.runtime.startTurn( | | 2228 | + turn(2, { | | 2229 | + ...normal, | | 2230 | + options: [{ id: "fastMode", value: false }], | | 2231 | + }), | | 2232 | + ); | | 2233 | + assert.strictEqual(harness.getOpenedOptions(), originalOptions); | | 2234 | + assert.lengthOf(harness.offeredMessages, 2); | | 2235 | + yield* harness.offerAndWait(turnOneResult); | | 2236 | + yield* Queue.take(harness.terminalReceipts); | | 2237 | + | | 2238 | + const refused = yield* harness.runtime | | 2239 | + .startTurn( | | 2240 | + turn(3, { | | 2241 | + ...normal, | | 2242 | + options: [{ id: "fastMode", value: true }], | | 2243 | + }), | | 2244 | + ) | | 2245 | + .pipe(Effect.result); | | 2246 | + assert.equal(refused._tag, "Failure"); | | 2247 | + if (refused._tag === "Failure") { | | 2248 | + assert.instanceOf( | | 2249 | + refused.failure.cause, | | 2250 | + ClaudeAdapterV2.ClaudeBackgroundWorkBlocksQueryReplacementError, | | 2251 | + ); | | 2252 | + } | | 2253 | + assert.strictEqual(harness.getOpenedOptions(), originalOptions); | | 2254 | + assert.lengthOf(harness.offeredMessages, 2); | | 2255 | + assert.isTrue(yield* harness.hasPendingBackgroundWork); | | 2256 | + }).pipe(Effect.provide(Layer.merge(IdAllocator.layer, NodeServices.layer))), | | 2257 | + ), | | 2258 | + ); | | 2259 | + | 2185 | 2260 | it.effect.each(["completed", "interrupted"] as const)( | 2186 | 2261 | "projects Claude thinking blocks when %s", | 2187 | 2262 | (status) => | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +6-5Lines changed: 6 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6969,11 +6969,12 @@ export function makeClaudeAdapterV2( | 6969 | 6969 |
| 6970 | 6970 | const openedWithResume = (yield* Ref.get(openedNativeThreads)).has(nativeThreadId); | 6971 | 6971 | // openedNativeThreads is per session instance and is lost when the | 6972 | | - // provider session is idle-released. A prior persisted provider turn | 6973 | | - // proves the native session already exists, so the query must resume | 6974 | | - // it; reopening with a fixed session id makes the CLI fail fast with | 6975 | | - // "Session ID ... is already in use". | 6976 | | - const hasPersistedProviderTurn = turnInput.providerTurnOrdinal > 1; | | 6972 | + // provider session is idle-released. A prior turn on this native id | | 6973 | + // requires resume; sessionId would fail with "already in use". | | 6974 | + // A fresh-session fallback keeps provider-thread history but binds | | 6975 | + // a new native id, which must be created before it can be resumed. | | 6976 | + const hasPersistedProviderTurn = | | 6977 | + turnInput.nativeThreadHasTurns ?? turnInput.providerTurnOrdinal > 1; | 6977 | 6978 | const shouldResume = | 6978 | 6979 | resumeSessionAt !== undefined || openedWithResume || hasPersistedProviderTurn; | 6979 | 6980 | const queryOptions = makeClaudeQueryOptions({ | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/BackgroundWorkStop.integration.test.ts +45-4Lines changed: 45 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,7 +41,7 @@ const modelSelection = { instanceId, model: "test-model" }; | 41 | 41 | // Codex turns leave commands running, then the thread moves to another | 42 | 42 | // provider thread (a provider switch). Stop on the newer, settled run must | 43 | 43 | // reach both provider threads and end all of the Codex work. | 44 | | -it.effect("Stop reaches background work an earlier provider thread still runs", () => | | 44 | +const stopEarlierBackgroundWork = (failedStart: boolean) => | 45 | 45 | Effect.scoped( | 46 | 46 | Effect.gen(function* () { | 47 | 47 | const cwd = yield* checkpointWorkspace("background-work-stop"); | @@ -233,7 +233,10 @@ it.effect("Stop reaches background work an earlier provider thread still runs", | 233 | 233 | const settledRun = (input: { | 234 | 234 | readonly ordinal: number; | 235 | 235 | readonly providerThreadId: ProviderThreadId; | 236 | | - readonly runningItem?: { readonly id: TurnItemId; readonly kind: "command" | "subagent" }; | | 236 | + readonly runningItem?: { | | 237 | + readonly id: TurnItemId; | | 238 | + readonly kind: "command" | "subagent"; | | 239 | + }; | 237 | 240 | }) => { | 238 | 241 | const runId = RunId.make(`run:${input.ordinal}`); | 239 | 242 | const attemptId = RunAttemptId.make(`attempt:${input.ordinal}`); | @@ -407,11 +410,41 @@ it.effect("Stop reaches background work an earlier provider thread still runs", | 407 | 410 | ], | 408 | 411 | }); | 409 | 412 |
| | 413 | + const failedRun = settledRun({ ordinal: 5, providerThreadId: otherProviderThreadId }); | | 414 | + if (failedStart) { | | 415 | + yield* sink.write({ | | 416 | + events: failedRun.events.flatMap((event): Array<OrchestrationV2DomainEvent> => { | | 417 | + switch (event.type) { | | 418 | + case "provider-turn.updated": | | 419 | + return []; | | 420 | + case "run.created": | | 421 | + return [{ ...event, payload: { ...event.payload, status: "failed" } }]; | | 422 | + case "node.updated": | | 423 | + return [ | | 424 | + { | | 425 | + ...event, | | 426 | + payload: { ...event.payload, status: "failed", providerTurnId: null }, | | 427 | + }, | | 428 | + ]; | | 429 | + case "run-attempt.created": | | 430 | + return [ | | 431 | + { | | 432 | + ...event, | | 433 | + payload: { ...event.payload, status: "failed", providerTurnId: null }, | | 434 | + }, | | 435 | + ]; | | 436 | + default: | | 437 | + return [event]; | | 438 | + } | | 439 | + }), | | 440 | + }); | | 441 | + } | | 442 | + | 410 | 443 | yield* orchestrator.dispatch({ | 411 | 444 | type: "run.interrupt", | 412 | 445 | commandId: CommandId.make("stop-background-work"), | 413 | 446 | threadId, | 414 | | - runId: latestRun.runId, | | 447 | + runId: failedStart ? failedRun.runId : latestRun.runId, | 415 | 448 | }); | 416 | 449 | yield* worker.drain(); | 417 | 450 |
| @@ -442,5 +475,13 @@ it.effect("Stop reaches background work an earlier provider thread still runs", | 442 | 475 | ), | 443 | 476 | ); | 444 | 477 | }), | 445 | | - ), | | 478 | + ); | | 479 | + | | 480 | +it.effect("Stop reaches background work an earlier provider thread still runs", () => | | 481 | + stopEarlierBackgroundWork(false), | | 482 | +); | | 483 | + | | 484 | +it.effect( | | 485 | + "Stop reaches earlier background work after the newest run fails before provider start", | | 486 | + () => stopEarlierBackgroundWork(true), | 446 | 487 | ); |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Orchestrator.ts +13-5Lines changed: 13 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8040,11 +8040,19 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio | 8040 | 8040 | activeProviderThreadId: projection.thread.activeProviderThreadId, | 8041 | 8041 | runs: projection.runs, | 8042 | 8042 | }).length > 0; | 8043 | | - const providerTurn = projection.providerTurns.findLast( | 8044 | | - (candidate) => | 8045 | | - candidate.runAttemptId === run?.activeAttemptId && | 8046 | | - (candidate.status === "running" || hasBackgroundWork), | 8047 | | - ); | | 8043 | + // A failed start has no provider turn. Background work still belongs | | 8044 | + // to the provider thread, so Stop reaches its latest accepted turn. | | 8045 | + const providerTurn = | | 8046 | + projection.providerTurns.findLast( | | 8047 | + (candidate) => | | 8048 | + candidate.runAttemptId === run?.activeAttemptId && | | 8049 | + (candidate.status === "running" || hasBackgroundWork), | | 8050 | + ) ?? | | 8051 | + (hasBackgroundWork | | 8052 | + ? projection.providerTurns.findLast( | | 8053 | + (candidate) => candidate.providerThreadId === run?.providerThreadId, | | 8054 | + ) | | 8055 | + : undefined); | 8048 | 8056 | if (run === undefined || rootNode === undefined || providerThread === undefined) { | 8049 | 8057 | return yield* new OrchestratorDispatchError({ | 8050 | 8058 | commandId: command.commandId, | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProviderAdapter.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -398,6 +398,8 @@ export interface ProviderAdapterV2TurnInput { | 398 | 398 | readonly threadId: ThreadId; | 399 | 399 | readonly runId: RunId; | 400 | 400 | readonly runOrdinal: number; | | 401 | + /** Whether the current native session has an accepted turn; omitted when unknown. */ | | 402 | + readonly nativeThreadHasTurns?: boolean; | 401 | 403 | readonly providerTurnOrdinal: number; | 402 | 404 | readonly restartContinuationOfRunId?: RunId; | 403 | 405 | readonly attemptId: RunAttemptId; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProviderTurnStartService.ts +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1231,6 +1231,13 @@ export const layer: Layer.Layer< | 1231 | 1231 | .filter((turn) => turn.providerThreadId === providerThread.id) | 1232 | 1232 | .map((turn) => turn.ordinal), | 1233 | 1233 | ) + 1, | | 1234 | + // Legacy accepted attempts have no native id. They count only before | | 1235 | + // a replacement, while no accepted attempt records a native identity. | | 1236 | + nativeThreadHasTurns: | | 1237 | + nativeInputRunIds.size > 0 || | | 1238 | + (legacyInputRunIds.size > 0 && | | 1239 | + sameNativeThread && | | 1240 | + !acceptedAttempts.some((source) => source.nativeThreadId !== undefined)), | 1234 | 1241 | shouldStartProviderTurn: runControls.shouldStartProviderTurn, | 1235 | 1242 | shouldFinalizeRun: runControls.shouldFinalizeRun, | 1236 | 1243 | hasUnpairedRunInterruptRequest: runControls.hasUnpairedRunInterruptRequest, | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/RunExecutionService.ts +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -507,6 +507,7 @@ export interface RunExecutionServiceV2StartRootRunInput { | 507 | 507 | readonly attempt: OrchestrationV2RunAttempt; | 508 | 508 | readonly attemptId: RunAttemptId; | 509 | 509 | readonly providerTurnOrdinal: number; | | 510 | + readonly nativeThreadHasTurns?: boolean; | 510 | 511 | readonly loadInheritedBackgroundTurnItems?: () => Effect.Effect< | 511 | 512 | ReadonlyArray<InheritedBackgroundTurnItemRoute>, | 512 | 513 | unknown | @@ -1348,6 +1349,9 @@ export const layer: Layer.Layer< | 1348 | 1349 | runId: input.run.id, | 1349 | 1350 | runOrdinal: input.run.ordinal, | 1350 | 1351 | providerTurnOrdinal: input.providerTurnOrdinal, | | 1352 | + ...(input.nativeThreadHasTurns === undefined | | 1353 | + ? {} | | 1354 | + : { nativeThreadHasTurns: input.nativeThreadHasTurns }), | 1351 | 1355 | ...(input.run.restartContinuationOfRunId === undefined | 1352 | 1356 | ? {} | 1353 | 1357 | : { | |
|