Skip to content

Commit bcee0f5

Browse files
fix(server): Claude turns whose CLI exits before its first frame fail instead of running forever (#33008)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 0ca73a1 commit bcee0f5

2 files changed

Lines changed: 146 additions & 12 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 131 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import {
5656
} from "@t3tools/contracts";
5757
import { mcpAppFromToolItem } from "@t3tools/shared/toolOutput";
5858
import { assert, describe, it } from "@effect/vitest";
59+
import * as Cause from "effect/Cause";
5960
import * as Context from "effect/Context";
6061
import * as DateTime from "effect/DateTime";
6162
import * as Deferred from "effect/Deferred";
@@ -2466,7 +2467,7 @@ describe("ClaudeAdapterV2 native fork", () => {
24662467
Effect.sync(() => {
24672468
openedQueries.push(input);
24682469
return {
2469-
messages: Stream.empty,
2470+
messages: Stream.never,
24702471
offer: () => Effect.void,
24712472
setModel: () => Effect.void,
24722473
applyFlagSettings: () => Effect.void,
@@ -2808,7 +2809,7 @@ describe("ClaudeAdapterV2 native session identity", () => {
28082809
Effect.sync(() => {
28092810
openedQueries.push(input);
28102811
return {
2811-
messages: Stream.empty,
2812+
messages: Stream.never,
28122813
offer: () => Effect.void,
28132814
setModel: () => Effect.void,
28142815
applyFlagSettings: () => Effect.void,
@@ -10979,6 +10980,134 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1097910980
),
1098010981
);
1098110982

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+
1098211111
it.effect(
1098311112
"clears the replaced sibling native thread roster when openQuery switches processes",
1098411113
() =>

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11311,8 +11311,22 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1131111311
executedModel: undefined,
1131211312
};
1131311313
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.
1131511315
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+
}
1131611330
if (
1131711331
!isClaudeProviderContinuationTurn(turnInput) &&
1131811332
permissionModeForClaudeRuntimePolicy(turnInput.runtimePolicy) ===
@@ -11389,15 +11403,6 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1138911403
// Transcript reads suspend replay; hold SDK frames until the buffer is applied to preserve order.
1139011404
yield* sdkMessageLock.withPermits(1)(
1139111405
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-
}
1140111406
yield* attachTurn;
1140211407
const drained = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages ?? [];
1140311408
yield* Ref.update(requestedContinuations, (current) => {

0 commit comments

Comments
 (0)