Skip to content

Claude: keep the turn open until background work drains - #140

Closed
BunsDev wants to merge 4 commits into
mainfrom
claude-background-turns-fix
Closed

BunsDev wants to merge 4 commits into
mainfrom
claude-background-turns-fix

Conversation

@BunsDev

@BunsDev BunsDev commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Bug

When Claude starts work in the background (Bash or Agent with run_in_background), Modex ended the turn and closed stdin at the first result. Once that work finished, the CLI started a follow-up turn by itself, and every permission prompt in it failed with Tool permission request failed: AbortError: Stream closed.

On main (a79c0f3), backends/claude.ts returns a finished turn for every result (L270–273), and any finished turn runs child.stdin?.end(); finish(done) (L134–138). The background set that #134 added only drives the subagent display. It never delays the end of a turn.

Fix

A successful result that arrives while background work is outstanding is held. The turn ends at the first result with nothing outstanding. #134's background set is now the single record of outstanding work.

How each review risk is handled:

  • Missed cases. The set is fed by run_in_background on any tool_use, Agent's default background, task_started/task_updated is_backgrounded, and background_tasks_changed. Terminal task statuses, failed launches, and tasks leaving the CLI's background list clear it.
  • Hang. The wait is capped at 30 min (BACKGROUND_CAP_MS). When the cap fires, Modex shows a warning notice, ends the turn and stops the CLI.
  • system/status stranding. After work drains, a 15 s idle window (BACKGROUND_IDLE_MS) is re-armed by every message, so a turn can't wait forever for a result that never comes. The window never fires while a permission prompt is open or a follow-up turn is running.
  • Held errors. An error result ends the turn at once, with a notice if background work was still running.
  • Stop. Stop still kills the CLI, clears every timer and reports the turn as interrupted.

Evidence

  • The event shapes are confirmed by a live claude 2.1.289 capture of a background Bash run: task_started with is_backgrounded: true, background_tasks_changed, task_notification, and two results around a follow-up init. The capture stays in the git-ignored .probes/ directory.
  • Not captured: a foreground Agent's task_started with is_backgrounded: false. That test assumes the CLI sends it. If it doesn't, the worst case is that the turn stays open with a "still running" notice until Stop or the 30 min cap. It does not fail with "Stream closed".

Tests

  • New file apps/desktop/test/claude-background.test.ts with 12 tests:
    • the regression;
    • the captured stream;
    • Agent in the background and in the foreground;
    • run_in_background on the tool call alone;
    • a failed launch;
    • the cap, including a follow-up turn that never replies;
    • status stranding;
    • a permission prompt waiting during the idle window;
    • an error result;
    • Stop during the wait.
  • subagents.test.ts: one test now drains its background shell before the final result.
  • Fails on main: with main's claude.ts, the regression test fails: AssertionError: stdin must stay open while background work is outstanding.
  • Passes with the fix: npm run build && npm test gives core 15/15 and desktop 306/306, plus 1/1, 10/10 and 9/9 in the other suites. The new file passed 5 out of 5 runs, 12/12 each time.
  • e2e was not run locally; the required unit + e2e (macOS) check covers it.

This replaces the uncommitted claude-background-turns worktree, which is left untouched.

Follow-up verification and lifecycle fix:

  • Background termination now escalates from SIGTERM to SIGKILL after 1.5 seconds, even after the held turn settles; only child close cancels the escalation.

  • A regression reproduces a Claude process that ignores SIGTERM: it fails before the fix and passes afterward. Independent review found no remaining blockers in the lifecycle fix.

  • Status documentation records the background-turn behavior and bounds.

  • Full verification on 4c801e1: npm run build && npm test && npm run test:e2e passed (345 unit tests, 125 desktop E2E and 7 site E2E). The signed merge includes current main; hosted exact-head gates are pending.

Delivery reconciliation: superseded by #144, merged on main as f5c347ba14c6691132420a7fe7086eb10109623a. The signed squash has complete tree equality with verified integration head 23600b5a95d2884ef57210f1ba6e93a77c90f5e5, after all four desktop/iPhone checks passed. The original native failure and unchanged-head targeted retry are retained in the maintainer audit. This PR is closed because its application changes have landed through #144.

A Claude turn ended, and stdin closed, at the first `result`, even while
Bash or Agent work started with run_in_background was still running. When
that work finished the CLI began a follow-up turn on its own, and its
permission prompts failed with "Tool permission request failed:
AbortError: Stream closed".

A successful `result` that arrives while background work is outstanding
is now held; the turn ends at the first `result` with nothing outstanding.
The existing `background` set (#134) is the single record of outstanding
work, now fed by every signal the CLI sends: run_in_background on any
tool_use, Agent's default background, task_started/task_updated
is_backgrounded, and background_tasks_changed; terminal task statuses,
failed launches and leaving the CLI's background list clear it.

- The wait is capped at 30 min (BACKGROUND_CAP_MS) with a warning
  notice, after which the CLI is stopped.
- Once work drains, a 15 s idle window (BACKGROUND_IDLE_MS) re-armed by
  every message lets the held reply stand if no follow-up turn starts,
  so system/status cannot strand a turn; it never fires while a
  permission prompt is open or a follow-up turn (system/init) is running.
- An error result ends the turn at once, with a notice when background
  work was still running.
- Stop still kills the CLI, clears every timer and reports interrupted.

Event shapes in the tests come from a live claude 2.1.289 capture.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@vercel

vercel Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
modex Ready Ready Preview Oct 8, 2026 3:28pm UTC

Request Review

@BunsDev BunsDev closed this in #144 Oct 8, 2026
BunsDev added a commit that referenced this pull request Oct 8, 2026
…rns (#144)

Complete reviewed desktop delivery: background-turn draining and termination, linked Sources retaining citations, Settings display polish, and persistent Space with local OpenKnowledge. Apply new-page focus during render so it cannot interrupt content typing. Canonicalize Knowledge fixture folders, await child-page selection, wait for iPhone controls and the keyboard before input, and control the Mac fixture outage through rescan cancellation and offline retry.

Verified build, typecheck, 407 unit tests, 151 desktop and 7 site E2E tests with the real companion enabled, native build-for-testing, and all desktop and iPhone hosted checks. Includes reviewed delivery from #140 and #143.
@BunsDev
BunsDev deleted the claude-background-turns-fix branch October 8, 2026 19:07

This branch was successfully deployed

1 active deployment
Preview — a3410afa Deployed Oct 8, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant