fix(server): keep Claude CLI alive across Stop#8997

closed1
main
GitHub fork: fix/claude-interrupt-keep-session
openedSep 1, 2026byTTONiiV
View on GitHubTwo-way sync enabledPR last imported 6d ago

What Changed

Claude Stop (interruptTurn) no longer closes the provider query. It now:

  1. Stops live subagent tasks via Query.stopTask
  2. Calls Query.interrupt() and completes the local turn
  3. Keeps the CLI process so the next prompt is queued into the same session
  4. Falls back to close() only when live tasks remain (the #5891 runaway-fleet case)

thread.session.stop and the 30-minute idle reaper still tear the process down.

Why

#7338 reports ~2× Claude usage in T3 vs native Claude Code. After #5891, Esc killed the CLI. The next message --resumes a new process.

A cheap local check (Haiku, --max-turns 1, prompt reply with just the word ok, 5h usage 0% → 1%):

turncache_writecache_read
1 cold24,53813,694
2 --resume, git unchanged26,84113,694
3 --resume after one untracked file26,94413,694

Each resume rewrote ~27k cache tokens even when git status did not change. On a 300k thread that is the cache-write spike reported in #7338. Native Claude Code keeps one process alive across Esc.

This does not claim to erase the whole gap. Claude effort/fast-mode changes still restart the session, idle reaping still stops it after 30 minutes, and app restart still --resumes. Those are separate.

Test plan

  • vp test run apps/server/src/provider/Layers/ClaudeAdapter.test.ts (81 passed)
  • Esc mid-turn, send a follow-up: session stays, usage should not jump as a full resume
  • Esc while subagents are running: live tasks stop; if they cannot, process still closes
  • Changing Claude effort still restarts (unchanged)

Made with Cursor

Note

Keep Claude CLI alive across Stop by interrupting turn and stopping live tasks

  • Reworks interruptTurn in ClaudeAdapter to cooperatively stop live subagent tasks via query.stopTask and cancel the in-flight turn via query.interrupt, instead of unconditionally closing the CLI session.
  • Emits task.completed events with status 'stopped' for acknowledged task stops, and completes the open turn as 'interrupted'.
  • Falls back to stopSessionInternal (closing the CLI) only when live tasks remain and stopTask is unavailable or ineffective.
  • Extends the ClaudeQueryRuntime interface to require interrupt() and optionally support stopTask(taskId).
  • Updates tests in ClaudeAdapter.test.ts and documents the new Stop semantics in providers.md.
  • Risk: any out-of-tree implementation of ClaudeQueryRuntime must now provide interrupt(); without it, type checking and runtime calls will fail.
📊 Macroscope summarized 4b1c76f. 2 files reviewed, 2 issues evaluated, 0 issues filtered, 2 comments posted

🗂️ Filtered Issues

Added 1 commit

  • fix(server): keep Claude CLI alive across Stop4b1c76f
……@@ -4608,10 +4611,70 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
4633+ Effect.as(true),
4634+ Effect.catch(() => Effect.succeed(false)),
4635+ );
4636+ if (!stopAcknowledged || !context.liveTaskIds.delete(taskId)) {
MmacroscopeappBoton line 4636Sep 1, 2026

🟠 High Layers/ClaudeAdapter.ts:4636

A delayed concurrent interruptTurn can interrupt a newer turn after the original turn has already been stopped. The second call reaches context.query.interrupt() after its stopTask result is discarded, and _turnId is ignored; capture and re-check the requested turn identity before interrupting.

🤖 Copy this AI Prompt to have your agent fix this:
text
1In file @apps/server/src/provider/Layers/ClaudeAdapter.ts around line 4636:
2
3A delayed concurrent `interruptTurn` can interrupt a newer turn after the original turn has already been stopped. The second call reaches `context.query.interrupt()` after its `stopTask` result is discarded, and `_turnId` is ignored; capture and re-check the requested turn identity before interrupting.
……@@ -4608,10 +4611,70 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
4670+ yield* Effect.tryPromise({
4671+ try: () => context.query.interrupt(),
4672+ catch: (cause) => toRequestError(threadId, "turn/interrupt", cause),
4673+ });
MmacroscopeappBoton line 4673Sep 1, 2026

🟠 High Layers/ClaudeAdapter.ts:4673

A subagent that starts during query.interrupt() is left running, and the Claude CLI stays alive instead of taking the stopSessionInternal() fallback. The liveTaskIds check occurs only before the interrupt, so recheck it after query.interrupt() and close the session if a task was added.

🤖 Copy this AI Prompt to have your agent fix this:
text
1In file @apps/server/src/provider/Layers/ClaudeAdapter.ts around line 4673:
2
3A subagent that starts during `query.interrupt()` is left running, and the Claude CLI stays alive instead of taking the `stopSessionInternal()` fallback. The `liveTaskIds` check occurs only before the interrupt, so recheck it after `query.interrupt()` and close the session if a task was added.
CcursorBotreviewed · 2 inline threadsSep 1, 2026
Synced from GitHub

Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want fixes drafted automatically? Bugbot Autofix can create code changes for findings. A team admin can enable Autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 4b1c76f09c1d0c05d9c1ef2ccbcc24ac6941a1bc. Configure here.

……@@ -4608,10 +4611,70 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
4674+
4675+ if (context.turnState) {
4676+ yield* completeTurn(context, "interrupted");
4677+ }
CcursorBoton line 4677Sep 1, 2026

Interrupt leaves pending requests open

Medium Severity

Cooperative Stop no longer runs the pendingApprovals and pendingUserInputs teardown that lived in stopSessionInternal. After Esc, completeTurn marks the turn interrupted while request.opened and user-input.requested can stay unresolved, so approval or AskUserQuestion dialogs remain on a finished turn.

Additional Locations (1)

Reviewed by Cursor Bugbot for commit 4b1c76f09c1d0c05d9c1ef2ccbcc24ac6941a1bc. Configure here.

……@@ -4608,10 +4611,70 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
4674+
4675+ if (context.turnState) {
4676+ yield* completeTurn(context, "interrupted");
4677+ }
CcursorBoton line 4677Sep 1, 2026

Leftover interrupt result closes next turn

Medium Severity

interruptTurn locally completeTurns after Query.interrupt() acknowledges, but the SDK still emits the interrupted result (and sometimes a truncated assistant message) on the live stream. handleResultMessage completes whatever turnState exists, so a follow-up sendTurn can be closed as interrupted. If those leftovers arrive before the next send, handleAssistantMessage also auto-starts a synthetic working turn.

Additional Locations (2)

Reviewed by Cursor Bugbot for commit 4b1c76f09c1d0c05d9c1ef2ccbcc24ac6941a1bc. Configure here.

Synced from GitHub

Approvability

Verdict: Not approved

Macroscope's review found this PR not approvable — This changes the production Claude Stop path from closing the CLI to coordinating subagent termination, interrupting the turn, and retaining the process, with new concurrency and lifecycle behavior. Unresolved concerns involve delayed interrupts, late SDK events, runaway tasks, and pending interactive requests, so the effects require human review.

Not approved because:

  • 2 blocking correctness issues found at or above your repo's Minimum Blocking Severity

Adjust the Minimum Blocking Severity for this repo — including turning it Off — in Settings. You can add or adjust custom eligibility rules. Learn more.