fix(daemon): give a closing daemon its full teardown before anything kills it
Problem
A closing daemon stops listening at its first teardown step and removes daemon.json at its last. Until it exits, another client sees a live, registered process that refuses connections.
- A compatible client judged that daemon "unreachable" after about 1 s, sent SIGTERM (ignored, because the close is already running) and sent SIGKILL 3 s later.
daemon stopsent SIGKILL 10 s after SIGTERM.- The daemon bounds its own teardown at about 13 s when idle, and near 69 s for a web session that records (23 s of fixed steps, including the 4 s app-log stop, plus a 46 s session budget). Steps without a budget are not measured, so 75 s is a budget, not a measurement.
A killed teardown skips lease release, claim release, runner shutdown and the shutdown report. On a host shared by several agents, the trigger is another agent's command that arrives while a daemon closes (idle reap, an accepted drain, or daemon stop).
ensureDaemon() also judged a leaving daemon again for only 10 s (40 Ć 250 ms) and then failed with daemon-lifecycle-contended, although its comment says "for as long as a drain can take".
Change
One constant, DAEMON_TEARDOWN_GRACE_MS (75 s) in daemon-contracts/daemon-process:
- A graceful retirement (
stopAndRetireDaemon) waits this long after SIGTERM by default (was 3 s). The wait ends when the process exits. daemon stoppasses the same grace explicitly (was 10 s), and its test asserts 75 s.DAEMON_STOP_GRACE_TIMEOUT_MSis deleted.- The cleanup of a one-shot daemon after
replay(cleanupDaemonAfterRequestindaemon-client-lifecycle.ts, includingreplay --save-script) uses the default, so it now waits up to 75 s before SIGKILL (was 3 s). That daemon also tears down a session. - The
ensureDaemon()judge-again budget is derived from it (was a fixed 10 s). - A managed takeover inside a startup caps the wait at the remaining startup budget, so the startup ceiling still holds.
- The cleanup of a client's own failed launch keeps 3 s: that daemon never served, so it has no teardown to wait for.
Cost: a daemon that is stuck in its teardown delays the caller by up to 75 s before SIGKILL (was 4 s, or 12 s for daemon stop).
Not in this PR: the request-timeout path still decides with one 1 s liveness probe, where ensureDaemon() uses three.
Evidence
daemon-client-ensure-drain.test.ts, "a compatible daemon that is closing is waited for, not killed, by another client": a registered same-version daemon refuses connections and takes 30 virtual seconds to leave. On the branch the client sends one SIGTERM and no SIGKILL, then starts the successor. With the production change reverted the test fails:SIGKILLis recorded.session-teardown-budget.test.ts, "the grace a client gives a closing daemon covers the largest teardown the daemon bounds": fails if a teardown budget grows past the grace.packages/daemon-client,packages/daemon-contractsand the budget test: 64 files, 593 tests pass. Eager-closure gate (913 tests), typecheck, lint and the layering scan pass. No allowlist, baseline or budget rows.
Size
Production: +33 ā8, net +25 lines (12 are comments). No new module, so the built CLI startup cost does not change.
Part of #4649.
š¤ 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.