Skip to content

fix(core): background subagents resume after a server restart instead of showing cancelled - #58261

Open
kitlangton wants to merge 7 commits into
restart-noticefrom
subagent-recovery
Open

kitlangton wants to merge 7 commits into
restart-noticefrom
subagent-recovery

Conversation

@kitlangton

@kitlangton kitlangton commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Background subagents now pick up where they left off after a server restart, instead of showing "Subagent cancelled" and losing their work.
A background subagent is a child agent a session starts and leaves running; when it finishes, its result lands in the parent session. Stopping a subagent on purpose still cancels it, as before.

🔍 Origin: seen in real use: after a clean server restart, every background subagent that was still working showed "Subagent cancelled" and none resumed.

❌ Before

sequenceDiagram
    participant P as Parent session
    participant S1 as Server (shutting down)
    participant DB as Database
    participant S2 as Server (next start)
    Note over S1: Clean shutdown, child still working
    S1->>P: ❌ "Subagent cancelled"
    S1->>DB: ❌ clear the child's "running" marker
    S2->>DB: find "running" markers
    DB-->>S2: none, child is never resumed
Loading

✅ After

sequenceDiagram
    participant P as Parent session
    participant S1 as Server (shutting down)
    participant DB as Database
    participant S2 as Server (next start)
    participant C as Background child
    rect rgb(220,245,220)
    Note over S1: Clean shutdown counts as a pause
    Note over DB: "running" marker kept, no result sent
    S2->>DB: find "running" markers
    S2->>C: resume the child once
    C-->>P: real result, delivered once
    end
Loading

Also, background subagent handling is now set up once for the whole server instead of once per project, so reloading a project no longer drops the watchers that deliver results.

  on shutdown, for a background job:
-   mark it "cancelled" in the database and tell the parent "Subagent cancelled"
+   tag it "cancelled for shutdown", keep the "running" marker, tell the parent nothing
  on next start:
    resume every job still marked "running", deliver its real result

📊 Proof

Case Before After
Two real server processes share one database; the first shuts down with 2 children running ❌ both cancelled, nothing resumed ✅ 2 markers kept, each child resumed once, parent gets 2 "completed" results
Background child waiting on a permission prompt at shutdown ❌ cancelled ✅ resumes, one "completed" result
Background shell command at shutdown ❌ marker cleared ✅ marker kept, parent sees "Command cancelled because the server restarted"

With the source changes reverted and the tests kept, the two-process test and the permission-prompt child case fail. 77 core tests pass.

👀 Read: packages/core/src/job.ts, the shutdown cancel path. While the old server is shutting down it sees the job as "cancelled" but the database still says "running", so any new code reacting to a cancellation must check its reason (subagent-completion.ts does). Not checked live in the desktop app. The rest is routine: shared result reading in subagent-completion.ts, a one-line call change in execution.ts, registering the service app-wide in packages/server/src/routes.ts, and tests.

Stacked on #58260: merge that first, then this retargets to dev · no schema or saved-format changes

Checks run

Run on this head, on top of #58260:

  • packages/core: bun typecheck passes. bun test test/permission-restart.test.ts test/session-projector.test.ts test/session-execution.test.ts test/job.test.ts test/session-restart-process.test.ts: 77 pass.
  • packages/server: bun typecheck passes.
  • bun script/lint-changed.ts origin/dev: 0 problems.
  • With this PR's source changes reverted and its tests kept, session-restart-process.test.ts and the pending-permission child case in permission-restart.test.ts fail.

@kitlangton kitlangton changed the title fix(core): resume background subagents after a server restart fix(core): background subagents pick up where they left off after a server restart Oct 26, 2026
@kitlangton kitlangton changed the title fix(core): background subagents pick up where they left off after a server restart fix(core): background subagents resume after a server restart instead of showing cancelled Oct 26, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant