Skip to content

fix(daemon-client): bound the judge-again loop on a clock, not a wait count - #4899

Merged
thymikee merged 1 commit into
mainfrom
t3/injectable-ensure-retry-clock
Nov 21, 2026
Merged

thymikee merged 1 commit into
mainfrom
t3/injectable-ensure-retry-clock

Conversation

@thymikee

@thymikee thymikee commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Summary

ensureDaemon re-judged a contended state directory a fixed 300 times: ENSURE_ATTEMPTS divided the teardown grace by the 250 ms judge-again delay. A wait count understates the call, because the work inside a judgement is not waited for: the base held it for 300 judgements plus what each one cost. The budget is now one teardown grace of elapsed time on the monotonic clock this package's other budgets use, each wait clamped to what is left, with a wait ceiling backstopping the deadline so a clock that stops cannot spin. Production timing is unchanged. No new seam: the budget cases move the clock inside this suite, as the startup and drain suites already do. No timeout raised, no assertion dropped, no waiver.

Before / after

No observable change. A contended ensureDaemon still reports daemon-lifecycle-contended after the same waits; only how the budget is measured differs.

Validation

Head 5423fc00f7, base da3d3601bb. The file run 20 times in a row at each, per-run duration:

base  8.88 9.17 9.27 9.11 8.85 9.11 9.25 9.17 9.10 9.15
      9.10 9.14 9.26 9.28 9.17 8.99 9.30 9.21 9.15 8.95   20/20
head  9.04 9.09 9.06 9.02 9.10 9.00 9.02 9.18 9.00 8.76
      8.80 9.15 9.03 9.24 9.23 9.00 9.29 9.05 9.01 9.06   20/20

The two contended cases, worst run of each set: 329 ms base, 211 ms head.

Both pass on an idle host: the flake needs CPU starvation, and the base loop is the side that grows under load. pnpm check:affected --run passes at head; the durations above were measured at 89925aa275, the same change before it read the monotonic clock. CI owns coverage and device lanes. No device-facing change.

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.48 MB 6.48 MB -129 B
Package (unpacked) 6.48 MB 6.48 MB -129 B
Package (download) 1.97 MB 1.97 MB -54 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 17.2 ms 17.2 ms -0.0 ms
CLI --help 36.6 ms 38.4 ms +1.8 ms

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

…ait count

`ENSURE_ATTEMPTS` divided the teardown grace by the judge-again delay, so a contended
`ensureDaemon` always ran 300 judgements. A wait count understates what the call can
spend: the work inside a judgement is not waited for, so a state directory another
process keeps replacing held the call for 300 judgements plus what each judgement cost.
The budget is now one teardown grace of elapsed time on the monotonic clock the
package's other budgets use, each wait clamped to what is left; one grace of waits
backstops the deadline, which a clock that stops cannot pass.

`daemon-client-ensure-explicit-lifecycle.test.ts` mocked `sleep` and still ran those 300
iterations of real work, which is what pushed it past the test timeout on a busy host. It
now moves the budget clock, as the startup-budget and drain suites move theirs, so the
grace passes in five judgements. Production timing is unchanged, and the waits are still
asked for at the production cadence, so the budget assertion pins it.
@thymikee
thymikee force-pushed the t3/injectable-ensure-retry-clock branch from 89925aa to 5423fc0 Compare November 21, 2026 13:37
@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 5423fc0

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

No open findings.

Reviewed with this repository's own Sentinel configuration (55be96bb6018).

💬 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

The code in 5423fc0 is ready. CI is green with 22 checks and none failing, and there are no conflicts. The earlier inline thread on the wall-clock deadline is fixed at this head and can be resolved: #4899 (comment).

Not blocking, and fine to take or leave: the deadline is checked only after each judgement returns, so one in-flight judgement can run past it (see https://github.com/callstack/agent-device/blob/5423fc0/packages/daemon-client/src/daemon-client-ensure.ts#L69); the comment at line 62 says the budgets are "like the transport and lifecycle budgets" although the transport, liveness and startup budgets use Date.now, so it could name the specific budgets or drop the comparison; and maxWaits guards against a clock that stops, which performance.now cannot do, so it could go unless it is a deliberate guard against a frozen clock in tests.

Not verified here: the test file and repo gates were not run, the 20-run timing table in the PR body (measured one commit earlier, as the author states) was not reproduced, and the Date.now-versus-mtime reasoning for the spy seam was not checked against inspectProcessLock.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 21, 2026
@thymikee
thymikee merged commit b233d67 into main Nov 21, 2026
22 checks passed
@thymikee
thymikee deleted the t3/injectable-ensure-retry-clock branch November 21, 2026 14:01
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