Skip to content

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

Closed
TONiiV wants to merge 1 commit into
pingdotgg:mainfrom
TONiiV:fix/claude-interrupt-keep-session
Closed

fix(server): keep Claude CLI alive across Stop#8997
TONiiV wants to merge 1 commit into
pingdotgg:mainfrom
TONiiV:fix/claude-interrupt-keep-session

Conversation

@TONiiV

@TONiiV TONiiV commented Sep 1, 2026

Copy link
Copy Markdown

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 fix(server): stop kills lingering Claude work #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%):

turn cache_write cache_read
1 cold 24,538 13,694
2 --resume, git unchanged 26,841 13,694
3 --resume after one untracked file 26,944 13,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

Closing the query on Esc forced the next prompt through --resume, which
rewrites prompt cache. Interrupt the turn and live tasks instead, and
only close when background work cannot be stopped.

Co-authored-by: Cursor <cursoragent@cursor.com>
@TONiiV TONiiV mentioned this pull request Sep 1, 2026
2 tasks
@github-actions github-actions Bot added vouch:unvouched PR author is not yet trusted in the VOUCHED list. size:M 30-99 changed lines (additions + deletions). labels Sep 1, 2026
Effect.as(true),
Effect.catch(() => Effect.succeed(false)),
);
if (!stopAcknowledged || !context.liveTaskIds.delete(taskId)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 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:
In file @apps/server/src/provider/Layers/ClaudeAdapter.ts around line 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.

yield* Effect.tryPromise({
try: () => context.query.interrupt(),
catch: (cause) => toRequestError(threadId, "turn/interrupt", cause),
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 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:
In file @apps/server/src/provider/Layers/ClaudeAdapter.ts around line 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.

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 4b1c76f. Configure here.


if (context.turnState) {
yield* completeTurn(context, "interrupted");
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Fix in Cursor Fix in Web

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


if (context.turnState) {
yield* completeTurn(context, "interrupted");
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Fix in Cursor Fix in Web

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

@macroscopeapp

macroscopeapp Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

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.

@TONiiV TONiiV closed this Sep 1, 2026
razvandinculescu pushed a commit to razvandinculescu/t3code that referenced this pull request Sep 3, 2026
Stop (thread.turn.interrupt) closed the Claude query, so the next prompt went
through --resume and, because Claude Code keeps git status in the cached
prompt prefix, re-billed the whole conversation as cache writes on every stop
(anthropics/claude-code#78720, pingdotgg#7338).

Ported from the closed upstream PR pingdotgg#8997 (TONiiV) with its review findings
addressed: the turn identity is re-checked before interrupting, subagents
that start during the interrupt get a stop pass before the hard-stop
fallback, pending approvals and questions are cancelled on the cooperative
path too, and the SDK's own aborted result closes the turn (local close only
as a timed fallback) so no late result can close the next turn. A failing
interrupt() falls back to the hard stop.

Built by Claude Fable 5.1 in T3 Code through the Claude Code harness.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M 30-99 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant