Skip to content

Commit d57bb5b

Browse files
fix(server): a silent Claude run fails after 5 minutes and Stop always settles it
A resumed Claude CLI can take the prompt and then send nothing: no init, no result, no exit. The run stayed running for hours, and Stop failed with "has no live query" or waited on a CLI that never answered. - After a prompt goes to a query that has sent no frame, fail the turn if still nothing arrives within 5 minutes, then close the query. - Stop on a query that never sent a frame settles the turn locally and closes the CLI in the background. - Stop on the active turn with no live query settles it as interrupted instead of failing.
1 parent d756fed commit d57bb5b

2 files changed

Lines changed: 164 additions & 1 deletion

File tree

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

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10710,6 +10710,103 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1071010710
),
1071110711
);
1071210712

10713+
it.effect("fails a resumed query that never produces its first frame", () =>
10714+
Effect.scoped(
10715+
Effect.gen(function* () {
10716+
let closes = 0;
10717+
const harness = yield* makeWakeHarnessWithOptions({
10718+
freshQueueOnReopen: true,
10719+
close: () =>
10720+
Effect.sync(() => {
10721+
closes++;
10722+
}),
10723+
});
10724+
const turn = (attempt: string, ordinal: number) =>
10725+
makeClaudeTestTurnInput({
10726+
threadId: harness.threadId,
10727+
providerThread: harness.providerThread,
10728+
now: DateTime.makeUnsafe(0),
10729+
attemptId: RunAttemptId.make(attempt),
10730+
text: "Continue after idle release",
10731+
attachments: [],
10732+
providerTurnOrdinal: ordinal,
10733+
});
10734+
yield* harness.runtime.startTurn(turn("silent-resume", 2));
10735+
assert.equal(harness.getOpenedOptions()?.resume, WAKE_NATIVE_SESSION);
10736+
yield* TestClock.adjust("5 minutes");
10737+
const terminal = yield* Queue.take(harness.terminalReceipts);
10738+
assert.equal(terminal.status, "failed");
10739+
assert.include(terminal.failure?.message, "sent nothing");
10740+
assert.equal(closes, 1);
10741+
yield* harness.runtime.startTurn(turn("after-silent-resume", 3));
10742+
// A late frame/exit from the expired query must not settle the replacement.
10743+
yield* Queue.shutdown(harness.sdkMessages);
10744+
yield* harness.offerAndWait(
10745+
makeResultFrame({ uuid: "after-silent-resume-result", result: "Recovered" }),
10746+
);
10747+
assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "completed");
10748+
assert.lengthOf(harness.terminalEvents(), 2);
10749+
}).pipe(
10750+
Effect.provide(
10751+
Layer.mergeAll(
10752+
IdAllocator.layer,
10753+
McpProviderSessions.layer,
10754+
NodeServices.layer,
10755+
NodeCrypto.layer,
10756+
),
10757+
),
10758+
),
10759+
),
10760+
);
10761+
10762+
it.effect("settles Stop locally when a silent resumed query ignores close", () =>
10763+
Effect.scoped(
10764+
Effect.gen(function* () {
10765+
const interruptStarted = yield* Deferred.make<void>();
10766+
const harness = yield* makeWakeHarnessWithOptions({
10767+
interrupt: Deferred.succeed(interruptStarted, undefined).pipe(
10768+
Effect.andThen(Effect.never),
10769+
),
10770+
close: () => Effect.never,
10771+
});
10772+
const attemptId = RunAttemptId.make("silent-resume-stop");
10773+
yield* harness.runtime.startTurn(
10774+
makeClaudeTestTurnInput({
10775+
threadId: harness.threadId,
10776+
providerThread: harness.providerThread,
10777+
now: yield* DateTime.now,
10778+
attemptId,
10779+
text: "Resume then stop",
10780+
attachments: [],
10781+
providerTurnOrdinal: 2,
10782+
}),
10783+
);
10784+
const providerTurnId = (yield* IdAllocator.IdAllocatorV2).derive.providerTurn({
10785+
driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
10786+
nativeTurnId: `turn:${attemptId}`,
10787+
});
10788+
const stopping = yield* harness.runtime
10789+
.interruptTurn({ providerThread: harness.providerThread, providerTurnId })
10790+
.pipe(Effect.forkChild);
10791+
yield* Deferred.await(interruptStarted);
10792+
yield* TestClock.adjust("11 seconds");
10793+
yield* Fiber.join(stopping);
10794+
assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "interrupted");
10795+
yield* Queue.shutdown(harness.sdkMessages);
10796+
assert.lengthOf(harness.terminalEvents(), 1);
10797+
}).pipe(
10798+
Effect.provide(
10799+
Layer.mergeAll(
10800+
IdAllocator.layer,
10801+
McpProviderSessions.layer,
10802+
NodeServices.layer,
10803+
NodeCrypto.layer,
10804+
),
10805+
),
10806+
),
10807+
),
10808+
);
10809+
1071310810
it.effect("preserves settled background work until an explicit stop", () =>
1071410811
Effect.scoped(
1071510812
Effect.gen(function* () {

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

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,10 @@ const CLAUDE_MCP_REGISTRATION_TIMEOUT = "90 seconds";
341341
// Claude can consume a subagent result before the launching turn ends, leaving
342342
// its notification with no native wake turn to answer the continuation.
343343
const CLAUDE_WAKE_TURN_START_DEADLINE = "2 minutes";
344+
// A CLI that takes a prompt but never sends a frame holds its run forever. Resume work
345+
// (transcript load, MCP registration) finishes before open returns, so a healthy CLI has
346+
// frames waiting by the time the prompt is offered; this is only a backstop.
347+
const CLAUDE_QUERY_FIRST_MESSAGE_TIMEOUT = Duration.minutes(5);
344348
// Claude Code flushes transcript writes every 100 ms, so a workflow member's
345349
// reply can land after the progress frame that settles it. Its transcript
346350
// stays the source of its answer this long after it settles.
@@ -4444,6 +4448,7 @@ interface ClaudeLiveQueryContext {
44444448
// The CLI may fall back from the requested mode on its first init frame.
44454449
openedPermissionMode: PermissionMode;
44464450
receivedInit: boolean;
4451+
receivedMessage: boolean;
44474452
// From system/init; feature-detects control requests such as MCP Apps.
44484453
capabilities: ReadonlySet<string>;
44494454
permissionMode: PermissionMode;
@@ -8380,6 +8385,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
83808385
const finalizeActiveTurnAfterQueryExit = Effect.fnUntraced(function* (
83818386
closedQuery: ClaudeLiveQueryContext,
83828387
cause?: Cause.Cause<ClaudeAgentSdkQueryRunnerError>,
8388+
failureMessage?: string,
83838389
) {
83848390
const current = yield* Ref.get(activeTurn);
83858391
// A replacement query can reuse the native thread id; only this query's turn is ours.
@@ -8400,6 +8406,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
84008406
: {
84018407
failure: makeProviderFailure({
84028408
cause: cause === undefined ? undefined : Cause.squash(cause),
8409+
message: failureMessage,
84038410
class: "transport_error",
84048411
}),
84058412
}),
@@ -11076,6 +11083,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1107611083
promptEchoMode: "unknown",
1107711084
openedPermissionMode: queryOptions.permissionMode,
1107811085
receivedInit: false,
11086+
receivedMessage: false,
1107911087
capabilities: new Set(),
1108011088
permissionMode: queryOptions.permissionMode,
1108111089
stopping: false,
@@ -11105,6 +11113,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1110511113
return context;
1110611114
});
1110711115
yield* querySession.messages.pipe(
11116+
Stream.tap(() =>
11117+
Effect.sync(() => {
11118+
context.receivedMessage = true;
11119+
}),
11120+
),
1110811121
Stream.runForEach((message) =>
1110911122
sdkMessageLock.withPermits(1)(
1111011123
Effect.suspend(() => {
@@ -11180,6 +11193,32 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1118011193
return context;
1118111194
});
1118211195

11196+
// Detach before settling, so a late frame or exit from this CLI cannot touch a replacement.
11197+
// A query already detached without settling its turn is settled here too.
11198+
const failSilentQuery = Effect.fnUntraced(function* (context: ClaudeLiveQueryContext) {
11199+
yield* Effect.sleep(CLAUDE_QUERY_FIRST_MESSAGE_TIMEOUT);
11200+
const turn = context.lastTurn;
11201+
if (context.receivedMessage || turn === null || (yield* Ref.get(activeTurn)) !== turn) {
11202+
return;
11203+
}
11204+
const ownsQuery = yield* Ref.modify(queryContext, (current) =>
11205+
current === context || current === null ? [true, null] : [false, current],
11206+
);
11207+
if (!ownsQuery) return;
11208+
const waited = Duration.format(CLAUDE_QUERY_FIRST_MESSAGE_TIMEOUT);
11209+
yield* finalizeActiveTurnAfterQueryExit(
11210+
context,
11211+
Cause.fail(
11212+
queryRunnerError(new Error(`No Claude frame within ${waited}.`), "firstMessage"),
11213+
),
11214+
`Claude sent nothing for ${waited} after the prompt. Retry the turn.`,
11215+
);
11216+
yield* context.query.close.pipe(
11217+
Effect.timeoutOption(CLAUDE_STOP_CONFIRM_TIMEOUT_MS),
11218+
Effect.ignore,
11219+
);
11220+
});
11221+
1118311222
const startTurn = Effect.fn("ClaudeAdapterV2.startTurn")(
1118411223
function* (turnInput: ProviderAdapter.ProviderAdapterV2TurnInput) {
1118511224
modelCatalog = yield* loadModelCatalog;
@@ -11398,6 +11437,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1139811437
),
1139911438
),
1140011439
);
11440+
if (!querySession.receivedMessage) {
11441+
yield* failSilentQuery(querySession).pipe(Effect.forkIn(sessionScope));
11442+
}
1140111443
return;
1140211444
}
1140311445
// Transcript reads suspend replay; hold SDK frames until the buffer is applied to preserve order.
@@ -11554,6 +11596,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1155411596
}
1155511597
return;
1155611598
}
11599+
if (existing === null && currentTurn?.providerTurnId === turnInput.providerTurnId) {
11600+
yield* finalizeActiveTurn({
11601+
context: currentTurn,
11602+
status: "interrupted",
11603+
completedAt: yield* DateTime.now,
11604+
});
11605+
return;
11606+
}
1155711607
if (existing === null) {
1155811608
return yield* new ProviderAdapter.ProviderAdapterProtocolError({
1155911609
driver: CLAUDE_PROVIDER,
@@ -11572,7 +11622,23 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1157211622
return next;
1157311623
});
1157411624
yield* existing.query.interrupt.pipe(Effect.timeoutOption("10 seconds"));
11575-
yield* existing.query.close.pipe(Effect.ignore);
11625+
if (!existing.receivedMessage) {
11626+
// A silent SDK iterator cannot confirm Stop; detach it before settling locally.
11627+
const ownsQuery = yield* Ref.modify(queryContext, (current) =>
11628+
current === existing ? [true, null] : [false, current],
11629+
);
11630+
if (ownsQuery) yield* finalizeActiveTurnAfterQueryExit(existing);
11631+
yield* existing.query.close.pipe(
11632+
Effect.timeoutOption(CLAUDE_STOP_CONFIRM_TIMEOUT_MS),
11633+
Effect.ignore,
11634+
Effect.forkIn(sessionScope),
11635+
);
11636+
return;
11637+
}
11638+
yield* existing.query.close.pipe(
11639+
Effect.timeoutOption(CLAUDE_STOP_CONFIRM_TIMEOUT_MS),
11640+
Effect.ignore,
11641+
);
1157611642
const closed = yield* Deferred.await(existing.closed).pipe(
1157711643
Effect.timeoutOption(CLAUDE_STOP_CONFIRM_TIMEOUT_MS),
1157811644
);

0 commit comments

Comments
 (0)