Skip to content

fix(daemon-client): report a newer daemon that won a startup race as daemon-incompatible - #4801

Merged
thymikee merged 4 commits into
mainfrom
t3/ensure-newer-daemon-typed-reason
Nov 20, 2026
Merged

thymikee merged 4 commits into
mainfrom
t3/ensure-newer-daemon-typed-reason

Conversation

@thymikee

@thymikee thymikee commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Summary

ensureDaemon reports a daemon newer than the client with details.reason: 'daemon-incompatible' and details.mismatch: 'newer', so callers can branch on the reason. When it lost a startup race to that newer daemon, it reported the same condition with no typed reason: start catches only daemon-contended, and the refusal that reached the caller had none, so an SDK caller branched wrong.

readReusableLocalDaemon threw newerDaemonRefusedError before consulting settings.retireIncompatibleContender, so a client that may not retire contenders got that untyped refusal from observeContendingDaemon. The fix moves that check above the refuseNewer throw: such a client reports the race, ensureDaemon judges the winner again, and the canonical daemon-incompatible error answers. Two files, no new wire reason literal.

What each caller of readReusableLocalDaemon sees:

  • observeContendingDaemon → ensureDaemon.start (retireIncompatibleContender: false, the only route where it is false): a newer winner now reaches the existing daemon-contended branch, so the next ensureOnce judges it and throws daemon-incompatible / mismatch: 'newer'. Every other non-reuse winner behaved this way already.
  • ensureLocalDaemon (retireIncompatibleContender: true; CLI, MCP, proxy upstream, SDK managed client): unchanged — keeps newerDaemonRefusedError and its wording, so the existing newer-daemon tests pass unchanged.

Part of #4649.

Validation

Base 74771e1919, head 4a05c1b3f7.

  • New daemon-client-ensure-startup-race.test.ts (real launch plus a real newer child that wins the lock). pnpm exec vitest run --project unit-core packages/daemon-client/src/__tests__/daemon-client-ensure-startup-race.test.ts: base → fails, details.reason undefined; head → passes. Gross +140/-3; production net 0 (3/3, reordering only) against the ≤ +3 budget.
  • packages/daemon-client (348 tests) and packages/daemon-contracts (240) pass; the daemon-fixture-launch and mutation-scope guards pass.
  • pnpm build, pnpm typecheck, pnpm lint, pnpm format and the layering check (R84, R89 quiet) pass. CI owns full validation; no new allowlist, baseline or budget row.

🤖 Generated with OpenCode (Callstack Apex)


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

…daemon-incompatible

`ensureDaemon` judges a daemon it observes directly and reports a newer one
with `details.reason: 'daemon-incompatible'`. In a startup race it reported
the same condition with no typed reason: the launch lost the registration
lock, `observeContendingDaemon` read the winner through
`readReusableLocalDaemon`, and that threw the newer-daemon refusal before it
consulted `retireIncompatibleContender`. `start` catches only
`daemon-contended`, so the caller received an error with no `details.reason`
and branched wrong.

A client that may not retire contenders now reports the race for any winner,
so `ensureDaemon` judges it again and answers with the reason it owns. The
managed route keeps its refusal and its wording.

Co-Authored-By: Callstack Apex <noreply@callstack.com>
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.45 MB 6.45 MB +77 B
Package (unpacked) 6.45 MB 6.45 MB +77 B
Package (download) 1.97 MB 1.97 MB +15 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 22.7 ms 22.5 ms -0.1 ms
CLI --help 44.3 ms 46.9 ms +2.5 ms

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

The fix at 4a05c1b reads correctly, and CI is green (19 checks, none failing), and there are no conflicts. One small duplication in the new test should go before merge.

The new startup-race test re-declares two helpers that already exist in daemon-client-ensure-explicit-lifecycle.test.ts. failureOf is byte-identical to the sibling's copy at lines 84-92. identityIn is the sibling's lines 80-82 with the return type widened to Record<string, unknown>. Both tests already import explicit-lifecycle-fixture.ts, so a fix to the failure-capture path would land in one copy and leave the other on the old behavior. Please move both helpers into that fixture module, keep the existing narrower return type, and import them from both test files. The new test only reads .version, which the narrower type already allows. Once that is done, this is ready for another look.

Not blocking, and you can take or leave these: newerDaemonRefusedError in daemon-client-startup.ts is the only lifecycle refusal here without a details.reason, so a managed-launch caller (CLI, MCP, proxy, or an SDK client with daemonLifecycle: 'managed') that branches on the reason still gets undefined for a newer daemon. Giving it reason: DAEMON_UNAVAILABLE_REASONS.incompatible and mismatch: 'newer' would make the rule hold for every caller, and the existing assertions in index-newer-daemon.test.ts and index-stalled-probe.test.ts should keep passing. Also, would naming the new test file for the decision it owns (for example daemon-client-ensure-newer-winner.test.ts), or adding the case beside the race test in daemon-client-ensure-explicit-lifecycle.test.ts, keep race coverage easier to find than a third file whose name differs from daemon-client-startup-race.test.ts by one word?

thymikee and others added 2 commits November 20, 2026 13:51
* origin/main:
  feat(gesture): hold before the move with gesture pan --hold-ms (#4767)
  docs: clarify device verification and security proposals (#4805)
  fix(daemon-client): say that a hung daemon is stopped before ensureDaemon starts a new one (#4803)
  test: remove five timing races from CI (#4800)
  refactor(remote): save a remote script from memory with the local publisher (#4802)
  refactor(apple-runner): one RUNNER_ERROR_CODES row per runner error code with parity/docs gates (#4774)
  fix(android): require acknowledgements for test IME typing (#4722)
  feat(scroll): add scroll refresh for pull-to-refresh (#4770)
  refactor(host-kit): add request Scope and use it for the wait capture deadline (#4794)
  fix(android): let permission all change nothing on an app that declares no permissions (#4796)
  fix(cli): replace the CLI session and generated remote config instead of rewriting them in place (#4797)
  fix(host-kit): accept a group-writable script directory the user owns (#4799)
…eason and share the test helpers

Review follow-up: failureOf and identityIn move to the lifecycle fixture, the
race test is named for the decision it covers, and the managed route's
refusal of a newer daemon now carries daemon-incompatible / newer as well.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member Author

Addressed in 7695b8d (main merged in).

  • Duplicate helpers: failureOf and identityIn now live in explicit-lifecycle-fixture.ts with the narrower return type, and both test files import them.
  • Managed route reason (optional): taken. newerDaemonRefusedError now carries reason: DAEMON_UNAVAILABLE_REASONS.incompatible and mismatch: 'newer'; the table is exported from daemon-lifecycle-errors.ts so the literal stays in one place (R89). index-newer-daemon.test.ts asserts both fields; the message and hint are unchanged.
  • Test file name (optional): renamed to daemon-client-ensure-newer-winner.test.ts.

Production is now +7 −4, net +3 lines against main. packages/daemon-client passes locally (40 files, 349 tests), with lint, typecheck and the layering scan.

@apex-sentinel-staging

This comment has been minimized.

@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 7695b8d. The verdict and the full list are in the summary comment.

…n the managed newer-daemon refusal

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

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 29f871e

Since the review of 7695b8d: 0 new · 0 still open · 1 no longer raised (1 marked fixed)

No open findings.

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

💬 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.

@thymikee

Copy link
Copy Markdown
Member Author

This PR is ready at 29f871e. failureOf and identityIn now live in explicit-lifecycle-fixture.ts and both ensure tests import them, so the duplication is gone. The optional note landed too: the newer-daemon refusal now carries reason: daemon-incompatible, mismatch: newer, both identities and the state directory, built from the same daemonIncompatibleError details as the attach route, and the tests assert them.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 20, 2026
@thymikee
thymikee merged commit bfc0980 into main Nov 20, 2026
18 checks passed
@thymikee
thymikee deleted the t3/ensure-newer-daemon-typed-reason branch November 20, 2026 13:58
thymikee added a commit that referenced this pull request Nov 20, 2026
* origin/main:
  fix(daemon-client): report a newer daemon that won a startup race as daemon-incompatible (#4801)
  feat(shake): add a shake command for iOS simulators and Android emulators (#4741)
thymikee added a commit that referenced this pull request Nov 20, 2026
* origin/main:
  refactor(apple-runner): generate the Swift runner error-code enums from runner-contract.ts (#4810)
  fix(daemon-client): report a newer daemon that won a startup race as daemon-incompatible (#4801)
  feat(shake): add a shake command for iOS simulators and Android emulators (#4741)
  fix(daemon): give a closing daemon its full teardown before anything kills it (#4806)
  fix(android): use typed evidence in the readiness boot poller (#4717)
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