@@ -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.
343343const 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