fix(daemon): close a detached daemon that finds its registration deleted or replaced
Problem
A daemon polls its own daemon.json every 15 s. When the record is deleted or names another process, the daemon writes daemon_metadata_lost to its log and keeps running. No new client can find it. An idle one leaves at the idle reap; one with an open session stays until someone kills it, with its sessions and runner.
This is reachable today by anything that removes or replaces a live daemon's record. It becomes easy to reach with #4682: an older CLI cannot compare the new start-time token, so its daemon stop reports "not running", deletes the record, and leaves the newer daemon alive. The older CLI's own error for a newer daemon tells the user to run that stop.
Change
When the watch reports the loss, a detached daemon closes through the same close() that a signal, a quiesce and the idle reap use. The log line is written first. An embedded runtime does not close: its host still uses it.
armDaemonMetadataLossWatch takes the callback; the runtime passes close for a detached daemon. The watch, its poll and the close path are unchanged. The existing fence still stops a closing daemon from removing a successor's record.
Evidence
Unit (daemon-runtime-metadata-ownership.test.ts):
- "a detached daemon whose registration is replaced by a successor closes itself" and "... is deleted closes itself": the runtime's
closedsettles, teardown ran, and its lock is released. Both fail onmain(the runtime never closes). - "an embedded runtime that loses its registration keeps serving its host": the loss is logged and no teardown runs.
Live, macOS, released 0.21.41 CLI against a build of this branch merged with #4682 (af8197c), one simulator, isolated claim, lease and state directories:
| Step | Without this change | With this change |
|---|---|---|
Daemon has an open session; 0.21.41 runs daemon stop in its state directory | Reports "not running", record deleted, daemon alive 3 minutes later | Same report; the daemon exits by itself after about 10 s and releases its device claim and lock |
| Same, then a 0.21.41 command starts a successor at once | Two daemons on one state directory | The first daemon exits after about 11 s; the successor's record and lock are intact |
Local: packages/daemon/src/server (125 files, 974 tests), typecheck, lint, the eager-closure gate and the layering scan pass. No allowlist, baseline or budget rows.
Size
Production: +22 ā6, net +16 lines, 5 of them comments. No new module, so the built CLI startup cost does not change.
Notes
- A client that still holds a connection to the daemon (a long-lived SDK or MCP client) loses its sessions when the daemon closes. Those sessions were already unsafe: a successor settles the device claims of the daemon it replaced.
- The close logs
daemon_shutdown_record_failedanddaemon_registration_finish_failedwhen the lock is already gone. Both are warnings from the existing close path.
Part of #4649. Recommended before #4682.
š¤ Generated with Claude Code
![Autofix with [code]smith](https://pr-comments-assets.blacksmith.sh/codesmith/autofix-with-codesmith-dark-3x.png)
Open in [code]smith Ā· Need help on this PR? Tag @codesmith-bot with what you need.