fix(daemon-client): a client whose daemon lost the start race adopts the winner
Summary
Two clients that find no daemon both launch one. The daemon that loses the startup lock exits cleanly (Daemon lock is held by another process; exiting., code 0). Its client treated that as a failed start, and on each of its 2 attempts:
cleanupFailedDaemonStartupMetadata(..., 'start_error')deleteddaemon.jsonand stopped the live daemon it named. That daemon is the winner, which every other client is already using.- It then failed with
Failed to start daemon(startError: daemon process N exited before readiness with code 0).
The e2e mobile benchmark hit this when two runs started together against an empty state dir: one died in 1.3 s with Failed to start daemon. Two workers in one run race the same way, and the kill takes down the sessions the other worker holds.
The fix, in waitForDaemonStartup:
- It accepts any reachable daemon first, before looking at its own child's exit.
- When its own child exited because a live daemon other than it holds the startup lock, it keeps waiting for that daemon instead of cleaning up. A child that exits with no other lock holder still fails fast, as before.
- A reachable daemon that isn't this client's own launch goes through
readReusableLocalDaemon, the same takeover decision as any existing daemon: a compatible one is adopted, a newer one is refused, and an older or mismatched one is replaced. - This client's own daemon is recognized by pid plus the process start time recorded at spawn. An adopted daemon is not
startedByClient. Before, any daemon that became ready during startup was markedstartedByClient, so a one-shotreplay/testcould stop a daemon another client started.
Touched: 2 source files (daemon-client-lifecycle.ts, daemon-client-metadata.ts) and 1 new test file. The live repro below was rerun on the final commit.
Validation
-
Live repro, with N clients running
agent-device devicesat once against a fresh--state-dir, 10 trials each:Build Clients Failed Daemon gone after the trial main3 8/30 8/10 this branch 3 0/30 0/10 this branch 5 0/50 0/10 -
daemon-client-startup-race.test.ts:- A client whose own daemon exited on the lock uses the winner: one launch, one RPC, and
daemon.jsonplus the lock are left in place. - A one-shot
testrun that adopted another client's daemon leaves it running. - A race won by an older daemon replaces it instead of adopting it.
All three fail on
main. - A client whose own daemon exited on the lock uses the winner: one launch, one RPC, and
-
pnpm check:affected --run: passed.pnpm test:coverage:ci: passed. Changed-line gate: passed (92.3%).