The best way to control Claude Code, Codex and more from any computer or phone
fix(server): Claude turns whose CLI exits before its first frame fail instead of running forever (#33008)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 10, 2026 at 02:08 UTC
bcee0f5d2872ba193c66ed9ba979bbe424f89ff9
2 files changed
+146
-12
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+131
-2
@@ -56,6 +56,7 @@ import {
56
} from "@t3tools/contracts";
57
import { mcpAppFromToolItem } from "@t3tools/shared/toolOutput";
58
import { assert, describe, it } from "@effect/vitest";
59
+import * as Cause from "effect/Cause";
60
import * as Context from "effect/Context";
61
import * as DateTime from "effect/DateTime";
62
import * as Deferred from "effect/Deferred";
@@ -2466,7 +2467,7 @@ describe("ClaudeAdapterV2 native fork", () => {
2467
Effect.sync(() => {
2468
openedQueries.push(input);
2469
return {
2469
- messages: Stream.empty,
2470
+ messages: Stream.never,
2471
offer: () => Effect.void,
2472
setModel: () => Effect.void,
2473
applyFlagSettings: () => Effect.void,
@@ -2808,7 +2809,7 @@ describe("ClaudeAdapterV2 native session identity", () => {
2809
Effect.sync(() => {
2810
openedQueries.push(input);
2811
return {
2811
- messages: Stream.empty,
2812
+ messages: Stream.never,
2813
offer: () => Effect.void,
2814
setModel: () => Effect.void,
2815
applyFlagSettings: () => Effect.void,
@@ -10979,6 +10980,134 @@ describe("ClaudeAdapterV2 background wake turns", () => {
10980
),
10981
);
10982
10983
+ const openAdapterWhoseFirstQueryEndsBeforeItsFirstFrame = Effect.fnUntraced(function* (
10984
+ name: string,
10985
+ ) {
10986
+ const fileSystem = yield* FileSystem.FileSystem;
10987
+ const idAllocator = yield* IdAllocator.IdAllocatorV2;
10988
+ const attachmentsDir = yield* fileSystem.makeTempDirectoryScoped({
10989
+ prefix: `t3-claude-v2-${name}-`,
10990
+ });
10991
+ const deadStreamEnded = yield* Deferred.make<void>();
10992
+ let opened = 0;
10993
+ const adapter = yield* ClaudeAdapterV2.makeClaudeAdapterV2({
10994
+ instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID,
10995
+ settings: DEFAULT_CLAUDE_SETTINGS,
10996
+ environment: {},
10997
+ attachmentsDir,
10998
+ // startTurn reads the auto-compact window from settings between opening the query and
10999
+ // attaching the turn; holding that read until the CLI's stream has ended pins the race.
11000
+ fileSystem: {
11001
+ ...fileSystem,
11002
+ readFileString: (filePath, encoding) =>
11003
+ opened === 1 && filePath.endsWith("settings.json")
11004
+ ? Deferred.await(deadStreamEnded).pipe(
11005
+ Effect.andThen(fileSystem.readFileString(filePath, encoding)),
11006
+ )
11007
+ : fileSystem.readFileString(filePath, encoding),
11008
+ },
11009
+ path: yield* Path.Path,
11010
+ random: yield* SecureRandom.SecureRandom,
11011
+ idAllocator,
11012
+ continuationRequests: { offer: () => Effect.void },
11013
+ queryRunner: {
11014
+ allocateSessionId: Effect.succeed(`native-${name}`),
11015
+ open: () =>
11016
+ Effect.gen(function* () {
11017
+ opened += 1;
11018
+ const sdkMessages = yield* Queue.unbounded<SDKMessage>();
11019
+ return {
11020
+ // The first CLI exits without writing a frame.
11021
+ messages:
11022
+ opened === 1
11023
+ ? Stream.empty.pipe(Stream.ensuring(Deferred.succeed(deadStreamEnded, undefined)))
11024
+ : Stream.fromQueue(sdkMessages),
11025
+ offer: () => Effect.void,
11026
+ setModel: () => Effect.void,
11027
+ applyFlagSettings: () => Effect.void,
11028
+ setPermissionMode: () => Effect.void,
11029
+ backgroundTasks: () => Effect.succeed(false),
11030
+ mcpServerStatus: Effect.succeed([]),
11031
+ readMcpResource: () => Effect.die("unexpected MCP resource read"),
11032
+ callMcpTool: () => Effect.die("unexpected MCP tool call"),
11033
+ listPermissionRules: Effect.die("unexpected permission rules read"),
11034
+ stopTask: () => Effect.void,
11035
+ interrupt: Effect.void,
11036
+ close: Queue.shutdown(sdkMessages),
11037
+ };
11038
+ }),
11039
+ forkSession: () => Effect.die("unused forkSession"),
11040
+ subagentLaunchToolUseId: () => Effect.succeed(null),
11041
+ assertComplete: Effect.void,
11042
+ },
11043
+ });
11044
+ const threadId = ThreadId.make(`thread-${name}`);
11045
+ const runtime = yield* adapter.openSession({
11046
+ threadId,
11047
+ providerSessionId: ProviderSessionId.make(`provider-session-${name}`),
11048
+ modelSelection: CLAUDE_TEST_MODEL_SELECTION,
11049
+ runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY,
11050
+ });
11051
+ const providerThread = yield* runtime.ensureThread({
11052
+ threadId,
11053
+ modelSelection: CLAUDE_TEST_MODEL_SELECTION,
11054
+ runtimePolicy: CLAUDE_TEST_RUNTIME_POLICY,
11055
+ });
11056
+ const now = yield* DateTime.now;
11057
+ const turnInput = (attempt: string, providerTurnOrdinal: number) =>
11058
+ makeClaudeTestTurnInput({
11059
+ threadId,
11060
+ providerThread,
11061
+ now,
11062
+ attemptId: RunAttemptId.make(`attempt-${name}-${attempt}`),
11063
+ text: "Continue with the plan.",
11064
+ attachments: [],
11065
+ providerTurnOrdinal,
11066
+ });
11067
+ return { runtime, turnInput };
11068
+ });
11069
+
11070
+ it.effect("fails a turn whose Claude query ends before its first frame", () =>
11071
+ Effect.scoped(
11072
+ Effect.gen(function* () {
11073
+ const harness = yield* openAdapterWhoseFirstQueryEndsBeforeItsFirstFrame(
11074
+ "claude-query-dies-before-frame",
11075
+ );
11076
+ const started = yield* harness.runtime
11077
+ .startTurn(harness.turnInput("a", 1))
11078
+ .pipe(Effect.exit);
11079
+ assert.isTrue(
11080
+ Exit.isFailure(started),
11081
+ "a turn whose Claude query ended before its first frame started running on the dead query",
11082
+ );
11083
+ if (Exit.isFailure(started)) {
11084
+ assert.include(
11085
+ Cause.pretty(started.cause),
11086
+ "Claude provider query ended before the turn could start.",
11087
+ );
11088
+ }
11089
+ const followUp = yield* harness.runtime
11090
+ .startTurn(harness.turnInput("b", 2))
11091
+ .pipe(Effect.exit);
11092
+ assert.isTrue(
11093
+ Exit.isSuccess(followUp),
11094
+ `the turn whose query ended before its first frame is still active: ${
11095
+ Exit.isFailure(followUp) ? Cause.pretty(followUp.cause) : ""
11096
+ }`,
11097
+ );
11098
+ }).pipe(
11099
+ Effect.provide(
11100
+ Layer.mergeAll(
11101
+ IdAllocator.layer,
11102
+ McpProviderSessions.layer,
11103
+ NodeServices.layer,
11104
+ NodeCrypto.layer,
11105
+ ),
11106
+ ),
11107
+ ),
11108
+ ),
11109
+ );
11110
+
11111
it.effect(
11112
"clears the replaced sibling native thread roster when openQuery switches processes",
11113
() =>
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+15
-10
@@ -11311,8 +11311,22 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
11311
executedModel: undefined,
11312
};
11313
const attachTurn = Effect.gen(function* () {
11314
- querySession.lastTurn = context;
11314
+ // Set before claiming: a query that exits after the claim finalizes this turn; one that exits before it fails the start.
11315
yield* Ref.set(activeTurn, context);
11316
+ const attached = yield* Ref.modify(queryContext, (current) => {
11317
+ if (current?.query !== querySession.query || querySession.stopping) {
11318
+ return [false, current];
11319
+ }
11320
+ querySession.lastTurn = context;
11321
+ return [true, current];
11322
+ });
11323
+ if (!attached) {
11324
+ yield* Ref.update(activeTurn, (current) => (current === context ? null : current));
11325
+ return yield* new ProviderAdapter.ProviderAdapterProtocolError({
11326
+ driver: CLAUDE_PROVIDER,
11327
+ detail: "Claude provider query ended before the turn could start.",
11328
+ });
11329
+ }
11330
if (
11331
!isClaudeProviderContinuationTurn(turnInput) &&
11332
permissionModeForClaudeRuntimePolicy(turnInput.runtimePolicy) ===
@@ -11389,15 +11403,6 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
11403
// Transcript reads suspend replay; hold SDK frames until the buffer is applied to preserve order.
11404
yield* sdkMessageLock.withPermits(1)(
11405
Effect.gen(function* () {
11392
- if (
11393
- (yield* Ref.get(queryContext))?.query !== querySession.query ||
11394
- querySession.stopping
11395
- ) {
11396
- return yield* new ProviderAdapter.ProviderAdapterProtocolError({
11397
- driver: CLAUDE_PROVIDER,
11398
- detail: "Claude provider query ended before the continuation could start.",
11399
- });
11400
- }
11406
yield* attachTurn;
11407
const drained = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages ?? [];
11408
yield* Ref.update(requestedContinuations, (current) => {