refactor(daemon): report startup progress over the launch channel instead of marker files

Merged
TH
8d ago
+638-407

Problem

A detached daemon reported startup progress by appending JSON lines to daemon-startup-phases-<pid>.jsonl in the state directory. The client read the tail to tell "slow but progressing" from "stalled". That design needed a per-pid file name, a claim-by-delete at entry, a newline-repair read, a sweep of dead pids by every new registration owner, removal after a joined exit, and tail parsing — and had a reproduced bug where the append followed a planted link to an outside file.

Change

The launcher (launchDaemonProcess in packages/daemon-contracts/src/daemon-registration-owner.ts) now spawns the detached child with stdio: ['ignore', logFd, logFd, 'ipc'] (Node's cross-platform IPC channel). The child (src/daemon.ts, packages/daemon/src/server/http/daemon-runtime.ts) sends one typed message per phase — the six phase names are unchanged — and closes the channel after registered. The channel is unref'd on both sides, so it holds neither the CLI nor the daemon alive. The client records the receipt time of each phase on the launcher clock, keeping the first receipt per phase and only for this launch's own child pid; unknown or malformed messages are ignored. The shrunken daemon-startup-phases.ts keeps only the vocabulary, the message type, its parser and the child-side sender, so no eager closure grows. Sending is a no-op without a marked launch channel (embedded mode, direct fixture spawns, manual starts — the launcher marks its child with AGENT_DEVICE_DAEMON_PROGRESS_CHANNEL=1), and a failed send (launcher gone) is ignored, at most one stderr line; it never affects startup.

Deletions

resolveDaemonStartupPhasesPath, recordDaemonStartupMarker, endsWithoutNewline, removeDaemonStartupPhasesFile, sweepDeadDaemonStartupPhasesFiles, readDaemonStartupPhaseMarkers and their tests; the sweep call in the registration owner; the claim-by-delete, marker writes and the stderr marker-failure reporter in src/daemon.ts; refreshStartupPhaseMarkers, the marker removals in retireStartupAttempt and the whole discardExitedLaunchRecord in the client; marker reads/writes in the three test fixtures and the launch plan. Kept unchanged: launch handle and child join, 60 s stall window, default 120 s ceiling (AGENT_DEVICE_DAEMON_STARTUP_TIMEOUT_MS), exit codes 75/78, two-attempt budget, contender path, phases timings in daemon_startup_failed details, stall/ceiling hints. The APPROVED_CLOSURE_ADDITIONS row for the module stays (the module still exists, shrunken); no row was added.

Acceptance results

  1. git grep daemon-startup-phases now matches only the vocabulary module itself (source, package export, budget row) and test assertions that no daemon-startup-phases-*.jsonl entry exists. No production code writes, reads, sweeps or removes such files.
  2. A real detached start reports all six phases in order to the launcher (src/daemon-entrypoint.test.ts). A slow child that keeps sending phases is not stalled (budget test: 4 phases 20 s apart over 80 s virtual, success); a child that stops is startup-stalled at the 60 s window; the env ceiling still gives startup-ceiling.
  3. Parent loss (real process): the launcher SIGKILLs itself on the first phase; the orphaned daemon still registers, answers /health, writes at most one failed-send line and no Daemon error, then reaps itself.
  4. Contender: busy child exits 75, the losing launcher attaches to the winner (race tests); the contender's phases arrive only on its own launcher's channel and reset only that launch's stall window; no files are left.
  5. Early exit before registered is startup-exited with exit details (existing tests); a duplicate phase message keeps the first receipt (new budget test asserts the reported timing is the first one).
  6. Embedded mode and fixture-spawned daemons with no channel start with no error (in-process entrypoint tests assert empty stderr; a spawned child without IPC exits 0 and reports nothing; an unmarked channel receives no reports).
  7. Security: a planted symlink daemon-startup-phases-<pid>.jsonl pointing outside the state dir is byte-identical and still a link after a full start, in both a client-driven start (budget test, planted under the child's pid) and a real launcher-driven start (entrypoint test).
  8. The launcher process exits by itself with code 0 right after registered — the unref'd channel does not hold the event loop (entrypoint test).
  9. LOC on non-test files under packages/*/src and src (vs eeff795f47): gross removed 208, gross added 167, net −41.
  10. Startup cost: eager import closure module counts identical to base (src/cli.ts 259, src/daemon.ts 360, packages/daemon/src/index.ts 359, packages/daemon-client/src/index.ts 129; top-level statements 3871/4759/4755/2270 vs base 3871/4761/4754/2270 — flat). node bin/agent-device.mjs --version, median of 20 runs: base 110.4 ms (quiet host; 136.0 ms in a later quiet interleaved round), branch 134.0 ms (141.3 ms in the interleaved round); minimums 115.3 ms base vs 113.8 ms branch. The shared host ran another agent's vitest suite during measurement (medians swung ±3x on both trees), so the wall-clock delta is within host noise; closures are flat.

Validation: pnpm exec vitest run --project unit-core on the six touched test files (70 tests) and on packages/daemon-client packages/test-kit packages/daemon-contracts packages/host-kit src/__tests__ packages/daemon (915 files, 8175 tests) all pass; scripts/__tests__/eager-closure-budgets.test.ts (913 tests) passes; pnpm typecheck, pnpm lint (0 warnings), pnpm format clean; pnpm install --frozen-lockfile && pnpm build on base and branch. CI owns full validation.

Part of #4649. Replaces part of #4704.

🤖 Generated with Mistral Vibe


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.