Skip to content

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

Merged
thymikee merged 4 commits into
mainfrom
t3/daemon-startup-progress-channel
Nov 19, 2026
Merged

thymikee merged 4 commits into
mainfrom
t3/daemon-startup-progress-channel

Conversation

@thymikee

@thymikee thymikee commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

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.

View guided diff Turn on auto-fix

…tead of marker files

The detached daemon used to append JSON lines to daemon-startup-phases-<pid>.jsonl
in the state dir: 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, tail parsing, and a reproduced append-through-symlink bug.

The launcher (launchDaemonProcess) now spawns the detached child with
stdio ['ignore', logFd, logFd, 'ipc'] and the child reports each of the six phases
as one typed message over that channel, closing it 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 its own child's pid; unknown or malformed
messages are ignored. The shrunken daemon-startup-phases module keeps only the
vocabulary, the message type, its parser and the child-side sender, so no eager
closure grows (module counts identical, weights flat).

Sending is a no-op without a marked launch channel (embedded mode, direct fixture
spawns, manual starts) and a failed send (launcher gone) is ignored, optionally
one stderr line; a send failure never affects startup. All marker-file functions,
the sweep, the claim-by-delete, the marker reads/writes in the client and the
fixtures are deleted; no production code touches daemon-startup-phases-*.jsonl
anymore, so a planted link with that name is never followed or modified.

Kept unchanged: the launch handle and child join, the 60 s stall window, the
default 120 s ceiling and env override, exit codes 75/78, the two-attempt launch
budget, the contender path, the phases timings in daemon_startup_failed details,
and the stall/ceiling hints.

Co-Authored-By: Mistral Vibe <noreply@mistral.ai>
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.43 MB 6.41 MB -15.0 kB
Package (unpacked) 6.42 MB 6.41 MB -15.0 kB
Package (download) 1.95 MB 1.95 MB -4.2 kB

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 50.0 ms 33.3 ms -16.7 ms
CLI --help 80.5 ms 74.3 ms -6.3 ms

thymikee and others added 3 commits November 19, 2026 19:12
…artup progress message

The IPC channel connects one launcher to the child it spawned, so the message needs no pid.
The parser now returns the phase; the vocabulary and message type stay module-private.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The R86 process-globals gate keeps process.env and the IPC channel at declared process
entries. src/daemon.ts now opens the launch channel and passes it to the runtime as
startupProgress; embedded hosts pass none. daemon-contracts keeps only the vocabulary,
the message and its parser.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
openDaemonStartupProgress takes the env and the process it reports for, so the entry is
one call and the channel rules (mark, unref, one failure line, close) have unit tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 0b49b19

1 finding(s): 1 minor.

🟡 Minor (1)

Where Finding
packages/daemon-contracts/src/daemon-registration-owner.ts:95 Stale daemon-startup-phases-*.jsonl files in the state dir are now never cleaned up

1 finding below minor hidden (user_settings.min_severity).

Reviewed with this repository's own Sentinel configuration (0ba31d87a14d).

💬 Talking to Sentinel

In a finding's thread, reply with:

Command What it does
@apex-sentinel-staging note <context> Adds context; Sentinel re-checks the cited code
@apex-sentinel-staging pivot <why> Says the finding is wrong; Sentinel re-checks the cited code
@apex-sentinel-staging like Marks the finding as useful
@apex-sentinel-staging dislike <why> Marks it as not useful, so it is not raised again

On this conversation, comment @apex-sentinel-staging review to run a full review once again.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 1 new finding on 0b49b19. The verdict and the full list are in the summary comment.

@@ -93,7 +97,6 @@ export async function tryAcquireDaemonRegistration(
try {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Minor · Stale daemon-startup-phases-*.jsonl files in the state dir are now never cleaned up

Upgrading from a previous version leaves daemon-startup-phases-<pid>.jsonl files in the state directory forever: the new test at daemon-client-startup-budget.test.ts:386 asserts the leftover is left "byte-identical: never read, swept or removed." This PR removes every cleanup path for them (sweepDeadDaemonStartupPhasesFiles, the claim-by-delete in src/daemon.ts, removeDaemonStartupPhasesFile, and the runtime's removal after registered) without a migration or cleanup step to replace them, so the state dir retains the very symlink-named artifacts the old sweep existed to neutralize.

Suggested change

Add a one-time removal of daemon-startup-phases-*.jsonl entries (unlink without following) in the registration owner, or document in a release/migration note that upgrading leaves these files and how an operator removes them.

Details

persona: maintainability, senior-developer, javascript

Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.

Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentional. The maintainer decided this redesign does no legacy migration or cleanup of old state (#4649). Leftover daemon-startup-phases-<pid>.jsonl files are a few hundred bytes, inert, and never read again: nothing opens, follows or parses them, and the new acceptance test plants one as a symlink and asserts it stays untouched. A sweep would bring back a directory scan, pid liveness checks and a deletion path inside the state dir, which is exactly the code this PR removes. Users who want them gone can delete them by hand. No change.

@thymikee

Copy link
Copy Markdown
Member Author

This PR is ready at 0b49b19. CI is green: all 18 checks pass. There are no conflicts, and nothing blocks merge.

Not blocking, and you can take or leave it: the test at src/daemon-entrypoint.test.ts:591 says the channel holds neither process alive, but the daemon disconnects right after registered, so the launcher exits at that disconnect even without the launcher-side child.channel?.unref(). Deleting that line keeps the test green. Could one case keep the child alive without it ever sending registered, and assert the launcher's event loop still drains? Otherwise, narrowing the test name and acceptance item 8 to what the test proves would also work.

Windows behavior and the ordering of the last IPC message against the child's exit event were not checked.

The open inline thread on leftover daemon-startup-phases-*.jsonl files does not apply, because nothing reads or writes them and the tests assert planted ones stay untouched. You can resolve it: #4777 (comment)

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 19, 2026
@thymikee
thymikee merged commit d33d752 into main Nov 19, 2026
18 checks passed
@thymikee
thymikee deleted the t3/daemon-startup-progress-channel branch November 19, 2026 19:04
thymikee added a commit that referenced this pull request Nov 19, 2026
…ent vocabulary

0.22 (#4621 bounded output) and main (#4777 startup progress) each grew
packages/host-kit/src/internal/exec.ts, so the merged file passed its legacy
1032-line cap and lint failed. Re-home the kill/settle ordering —
signalProcessGroupBestEffort, bindCommandLifecycle, the kill settlement, the abort
watch, killProcessTree and destroyCommandStreams — into exec-trace.ts, which already
owns the exec settlement vocabulary and sits in every closure that reaches exec.ts,
so no eager closure grows. exec.ts now fits the 1000-line rule and its legacy cap
entry is removed.
thymikee added a commit to pcbeingused333/agent-device that referenced this pull request Nov 20, 2026
* origin/main: (70 commits)
  refactor(apple-runner): one RUNNER_ERROR_CODES row per runner error code with parity/docs gates (callstack#4774)
  fix(android): require acknowledgements for test IME typing (callstack#4722)
  feat(scroll): add scroll refresh for pull-to-refresh (callstack#4770)
  refactor(host-kit): add request Scope and use it for the wait capture deadline (callstack#4794)
  fix(android): let permission all change nothing on an app that declares no permissions (callstack#4796)
  fix(cli): replace the CLI session and generated remote config instead of rewriting them in place (callstack#4797)
  fix(host-kit): accept a group-writable script directory the user owns (callstack#4799)
  fix(cli): escape control characters at the CLI's stdout and stderr writes (callstack#4778)
  fix(audit): do not report rows under a persistent bar that scrolling reveals (callstack#4775)
  fix(daemon): refuse an unsafe state directory before first use (callstack#4783)
  refactor: settle remaining single-deadline races through settleWithin (callstack#4788)
  refactor(host-kit): settle deadline races via settleWithin with R87 race ratchet (callstack#4766)
  fix(android): authenticate and bound persistent snapshot helper sessions (callstack#4729)
  fix(daemon): bound upload ingress and finalization (callstack#4721)
  chore(gates): ratchet wire-published reason literals by value (R89) (callstack#4779)
  feat(audit): evaluate role-less clickable views on Android (callstack#4780)
  fix(mutation): keep umask-setting tests out of Stryker's thread pool (callstack#4785)
  feat(audit): report controls that share one accessible name (callstack#4771)
  fix(contracts): pair open runtime hints with their apply operation (callstack#4754)
  refactor(daemon): report startup progress over the launch channel instead of marker files (callstack#4777)
  ...
thymikee added a commit that referenced this pull request Nov 20, 2026
…ent vocabulary

0.22 (#4621 bounded output) and main (#4777 startup progress) each grew
packages/host-kit/src/internal/exec.ts, so the merged file passed its legacy
1032-line cap and lint failed. Re-home the kill/settle ordering —
signalProcessGroupBestEffort, bindCommandLifecycle, the kill settlement, the abort
watch, killProcessTree and destroyCommandStreams — into exec-trace.ts, which already
owns the exec settlement vocabulary and sits in every closure that reaches exec.ts,
so no eager closure grows. exec.ts now fits the 1000-line rule and its legacy cap
entry is removed.
thymikee added a commit that referenced this pull request Nov 20, 2026
…ent vocabulary

0.22 (#4621 bounded output) and main (#4777 startup progress) each grew
packages/host-kit/src/internal/exec.ts, so the merged file passed its legacy
1032-line cap and lint failed. Re-home the kill/settle ordering —
signalProcessGroupBestEffort, bindCommandLifecycle, the kill settlement, the abort
watch, killProcessTree and destroyCommandStreams — into exec-trace.ts, which already
owns the exec settlement vocabulary and sits in every closure that reaches exec.ts,
so no eager closure grows. exec.ts now fits the 1000-line rule and its legacy cap
entry is removed.
thymikee added a commit that referenced this pull request Nov 21, 2026
…ent vocabulary

0.22 (#4621 bounded output) and main (#4777 startup progress) each grew
packages/host-kit/src/internal/exec.ts, so the merged file passed its legacy
1032-line cap and lint failed. Re-home the kill/settle ordering —
signalProcessGroupBestEffort, bindCommandLifecycle, the kill settlement, the abort
watch, killProcessTree and destroyCommandStreams — into exec-trace.ts, which already
owns the exec settlement vocabulary and sits in every closure that reaches exec.ts,
so no eager closure grows. exec.ts now fits the 1000-line rule and its legacy cap
entry is removed.
thymikee added a commit that referenced this pull request Nov 21, 2026
…ent vocabulary

0.22 (#4621 bounded output) and main (#4777 startup progress) each grew
packages/host-kit/src/internal/exec.ts, so the merged file passed its legacy
1032-line cap and lint failed. Re-home the kill/settle ordering —
signalProcessGroupBestEffort, bindCommandLifecycle, the kill settlement, the abort
watch, killProcessTree and destroyCommandStreams — into exec-trace.ts, which already
owns the exec settlement vocabulary and sits in every closure that reaches exec.ts,
so no eager closure grows. exec.ts now fits the 1000-line rule and its legacy cap
entry is removed.
thymikee added a commit that referenced this pull request Nov 23, 2026
…ent vocabulary

0.22 (#4621 bounded output) and main (#4777 startup progress) each grew
packages/host-kit/src/internal/exec.ts, so the merged file passed its legacy
1032-line cap and lint failed. Re-home the kill/settle ordering —
signalProcessGroupBestEffort, bindCommandLifecycle, the kill settlement, the abort
watch, killProcessTree and destroyCommandStreams — into exec-trace.ts, which already
owns the exec settlement vocabulary and sits in every closure that reaches exec.ts,
so no eager closure grows. exec.ts now fits the 1000-line rule and its legacy cap
entry is removed.
hannojg pushed a commit to hannojg/agent-device that referenced this pull request Nov 23, 2026
…ent vocabulary

0.22 (callstack#4621 bounded output) and main (callstack#4777 startup progress) each grew
packages/host-kit/src/internal/exec.ts, so the merged file passed its legacy
1032-line cap and lint failed. Re-home the kill/settle ordering —
signalProcessGroupBestEffort, bindCommandLifecycle, the kill settlement, the abort
watch, killProcessTree and destroyCommandStreams — into exec-trace.ts, which already
owns the exec settlement vocabulary and sits in every closure that reaches exec.ts,
so no eager closure grows. exec.ts now fits the 1000-line rule and its legacy cap
entry is removed.
thymikee added a commit that referenced this pull request Nov 24, 2026
…ent vocabulary

0.22 (#4621 bounded output) and main (#4777 startup progress) each grew
packages/host-kit/src/internal/exec.ts, so the merged file passed its legacy
1032-line cap and lint failed. Re-home the kill/settle ordering —
signalProcessGroupBestEffort, bindCommandLifecycle, the kill settlement, the abort
watch, killProcessTree and destroyCommandStreams — into exec-trace.ts, which already
owns the exec settlement vocabulary and sits in every closure that reaches exec.ts,
so no eager closure grows. exec.ts now fits the 1000-line rule and its legacy cap
entry is removed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant