Skip to content

Resume threads whose background commands a graceful daemon shutdown stopped - #7134

Merged
ymichael merged 2 commits into
mainfrom
bb/resume-threads-after-owner-stopped-tasks
Nov 5, 2026
Merged

ymichael merged 2 commits into
mainfrom
bb/resume-threads-after-owner-stopped-tasks

Conversation

@ymichael

@ymichael ymichael commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

Two PRs merged on Nov 4 conflict:

So after a graceful restart, such as an update, no task is left open. Idle threads waiting on background commands are never nudged. On Nov 5 an update at 18:07:09 UTC stopped four idle threads' commands (CI watchers and a dev server). The runtime recorded each as item/backgroundTask/completed with taskStatus: "stopped" at 18:07:13, before the old session closed. The new daemon instance connected at 18:07:31, planRestartResumes found no open tasks, and no resume was queued. Turns cut mid-flight still resumed, because that path doesn't depend on open tasks.

What changed

  • packages/domain: the background-task item gains an optional ownerStopped: true. It means bb settled the task because the process running it went away, not because the task, the agent or the user ended it.

  • packages/agent-runtime: the runtime's own settlements set the marker when they force a running task to stopped. These are the settlements on bridge exit and runtime shutdown (Settle background tasks when the bridge, Claude Code or the daemon that owns them stops #6124). A task already in a settled status keeps its status and gets no marker.

  • apps/server (background-task-reconciliation.ts):

    • Host-level settles mark the tasks they force-stop. These are host-daemon-restarted, host-connection-lost, host-removed and host-daemon-no-longer-runs-task.
    • The settle for a thread the user stopped does not mark them.
  • packages/db: listOwnerStoppedBackgroundTaskRowsForHost returns marked completions on a host's live active or idle threads since a given time. It is index-driven: thread status, then events_background_task_thread_type_item_sequence_idx. On a production DB it takes about 11 ms.

  • apps/server (restart-resume.ts, session-owner-side-effects.ts): planRestartResumes combines two sources:

    • tasks still open;
    • tasks marked ownerStopped since the replaced daemon session opened and after the thread's last turn request.

    Each stop nudges the thread once, and the agent and the user stopping a task still never trigger a resume. The wording sent to the agent is unchanged.

  • Wire: the marker is a new field in daemon-to-server events, so HOST_DAEMON_PROTOCOL_VERSION goes from 262 to 263. The plugin SDK goes to 0.6.126 because the domain item schema ships in its bundled types.

How you verified

  • New tests in apps/server/test/threads/restart-resume.test.ts:
    • An idle thread whose background command was settled with ownerStopped before the daemon reconnected is woken with the command named, and is not woken again after a second restart. This fails on main, where no open task remains and no resume is queued.
    • An idle thread whose command was stopped without the marker (by the agent or the user) is left alone.
  • Assertions added:
    • host settles mark ownerStopped (background-task-reconciliation.test.ts);
    • an idle-release thread stop does not mark it (public-thread-stop-runtime.test.ts);
    • runtime settlements on bridge exit and shutdown carry it (agent-runtime tests updated).
  • turbo run test: @bb/agent-runtime 309 passed. The @bb/server restart-resume, reconciliation and thread-stop suites have 66 passed.
  • turbo run typecheck lint --filter='...[origin/main]': 155 tasks pass.
  • Not yet checked live, by restarting a daemon while a thread's background command runs.

AGENT GENERATED

🤖 Generated with Claude Code

ymichael and others added 2 commits November 5, 2026 10:20
…topped

Restart resume (#7004) found the background work a restart stopped by
listing tasks still open when the new daemon instance connected. Since
#6124, a graceful daemon shutdown settles every open task as stopped
before the connection closes, and the server settles them when the
connection is lost. After an update restart no task is left open, so idle
threads waiting on background commands were never nudged. On Nov 5 an
update at 18:07 UTC stopped four threads' commands this way and none were
resumed.

Settlements made because the process running a task went away (the
runtime's settle on bridge exit and shutdown, and the server's host
settles) now mark the item ownerStopped. Restart resume also counts tasks
marked that way since the replaced daemon session opened and after the
thread's last turn request, so each stop nudges the thread once. Stops by
the agent or the user carry no marker and still never trigger a resume.

The marker is a new daemon-to-server event field, so this bumps
HOST_DAEMON_PROTOCOL_VERSION to 263, and the plugin SDK to 0.6.126
because the domain item schema ships in its types.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit dd60ad7 into main Nov 5, 2026
51 checks passed
@ymichael
ymichael deleted the bb/resume-threads-after-owner-stopped-tasks branch November 5, 2026 20:28
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