Skip to content

refactor: settle remaining single-deadline races through settleWithin - #4788

Merged
thymikee merged 2 commits into
t3/q7-hostkit-settlewithinfrom
t3/q7b-settlewithin-src-sites
Nov 20, 2026
Merged

thymikee merged 2 commits into
t3/q7-hostkit-settlewithinfrom
t3/q7b-settlewithin-src-sites

Conversation

@thymikee

@thymikee thymikee commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Summary

Finishes the settleWithin migration that #4766 started in the daemon. Stacked on #4766; retarget to main once it merges.

Seven more sites raced work against a hand-rolled timer and now settle through settleWithin (@agent-device/host-kit/retry). Three of them left a timer armed for the full budget after the work had settled: the screen-recording stop wait (settlesWithin), the snapshot bridge exit wait (waitForProcess), and the CLI exit flush (sleep(FLUSH_TIMEOUT_MS)).

  • 8 files. No public API, wire, or docs change.
  • Promise.race( count (R88): 41 → 34.
  • Not migrated on purpose: races with an abort signal or a poll tick as a racer, sites on an injected clock, and provider-limrun (no host-kit dependency).
  • Tried and reverted: capture-fact-grace.ts and perf-xctrace.ts. Importing host-kit/retry there grows a startup closure and the eager-closure budget refuses it.

Validation

Tested head: 3581b99 (base 844e719). Device run and vitest related were at 584e2ac; the later commit changes only process-exit.test.ts, which fails when the drain wait is removed.

  • pnpm typecheck: pass.
  • vitest related on the 7 files: 1,230 files, 12,213 tests pass (the eager-closure budget passes after the two reverts).
  • scripts/layering/check.ts: OK, R88 34 remaining.
  • Device, branch CLI 0.21.42-dev, iPhone 17e simulator (iOS 27.0), isolated state dir: open com.apple.Preferences, record start --scope device --hide-touches, record stop (MP4 exported, 41 kB), snapshot -i, close, daemon stop (graceful). All succeeded.
  • CI owns the full gate for this head.

Risk: the abort-rollback path in the simulator recording host has unit coverage only.

🤖 Generated with Claude Code


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

Seven sites outside the daemon still raced work against a hand-rolled
timer. Two of them never cleared the timer: the screen-recording stop
wait and the snapshot bridge exit wait kept it armed for the full budget
after the process had already exited.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.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.43 MB 6.43 MB -303 B
Package (unpacked) 6.43 MB 6.43 MB -303 B
Package (download) 1.95 MB 1.95 MB -130 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 23.2 ms 22.9 ms -0.3 ms
CLI --help 50.2 ms 47.2 ms -3.0 ms

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

The change in 584e2ac looks right, but one test no longer guards what it says it guards. CI is green with 14 checks and none failing, and there are no conflicts.

src/cli/process-exit.test.ts:4 mocks only sleep from @agent-device/host-kit/retry. After this PR, src/cli/process-exit.ts imports settleWithin and never sleep, so the mock and the mockImplementationOnce(() => new Promise(() => {})) override near line 72 no longer reach the code under test. That override exists so the drain test can only pass through the drain callback. Now, if the drain wait broke in production, the real 2000 ms FLUSH_TIMEOUT_MS arm would fire, settleWithin would return { kind: 'deadline' }, process.exit(2) would still run, and both assertions would still pass. So the #1596 guard against truncated CLI output no longer fails when it should. The "never drains" case also now waits the real 2 s, which breaks the no-real-time-waits rule in the unit lane (2.5 s budget in scripts/vitest-slow-test-reporter.ts). Please drop the inert mock and put the deadline under fake-timer control in this file: whichever arm a case claims to prove must be the only arm that can fire. For example, use vi.useFakeTimers(), assert no exit yet, then either assert exit after the drain callback or after vi.advanceTimersByTimeAsync(FLUSH_TIMEOUT_MS). The drain case should fail when the drain wait is removed.

Not blocking, take or leave: the PR body gives the R88 count as 38 -> 31, but the rule's own count (the "remaining" number scripts/layering/check.ts prints) is 41 -> 34, because the totals omit packages/replay-test and packages/react-native; the body says two sites never cleared the timer, but the removed sleep(FLUSH_TIMEOUT_MS) arm in process-exit.ts also left its timer armed, so it is three; and resolvedWithinGrace in capture-fact-grace.ts:12 and the compile deadline in recording/video.ts:178 remain hand-rolled deadline races, so would a function-scoped await import('@agent-device/host-kit/retry') at those two sites, or a leaf subpath for settleWithin, fit within the eager-closure budget?

Once the process-exit test discriminates between the two arms again, this is ready to merge.

The test still mocked sleep, which exitAfterFlush no longer calls, so the
drain case would have passed on the real 2 s deadline. It now fails when
the drain wait is removed, and the never-drains case no longer spends 2 s.

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

Copy link
Copy Markdown
Member Author

Fixed at 3581b99b94.

Test. process-exit.test.ts drops the inert sleep mock and uses fake timers. The drain case asserts no exit at FLUSH_TIMEOUT_MS - 1, then exits on the drain callback. The never-drains case asserts no exit before the deadline and exit at it. With the settleWithin line removed from exitAfterFlush, both cases fail; the file now runs in well under a second.

PR body. Corrected to 41 → 34, and to three sites that left a timer armed, including the removed sleep(FLUSH_TIMEOUT_MS).

capture-fact-grace.ts:12 and recording/video.ts:178. Not in this PR. Both already clear their timer in finally, so there is no leak to fix. A function-scoped import would put an await import on a capture path to save a few lines. A leaf subpath is the right fix, but it moves settleWithin out of the module #4766 just placed it in, so it belongs in its own change after #4766 merges. The two perf-xctrace.ts waits, which do leave a timer armed, would move with it.

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 3581b99

Since the review of 584e2ac: 0 new · 0 still open · 0 no longer raised

No open findings.

Reviewed with this repository's own Sentinel configuration (854bba6c5a18).

💬 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 3581b99. The process-exit test now runs on fake timers: the drain case advances to just short of FLUSH_TIMEOUT_MS and can only exit through the drain callback, and the never-drains case proves the deadline arm fires at exactly the deadline, without waiting real time. The inert sleep mock is gone.

Not blocking: the PR body's R88 count reads 38 -> 31, while the rule itself prints 41 -> 34 because it also counts packages/replay-test and packages/react-native.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 20, 2026
@thymikee
thymikee added this pull request to stack #4795 November 20, 2026 10:15
@thymikee
thymikee merged commit b1b9fda into main Nov 20, 2026
17 checks passed
@thymikee
thymikee deleted the t3/q7b-settlewithin-src-sites branch November 20, 2026 10:15
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
The primitive sat in retry.ts, so a caller that wanted only the deadline
race also evaluated the retry policy, the deadline class, the
diagnostics edge and the kernel error vocabulary. That is why #4788
reverted the capture-kit grace wait and both perf-xctrace waits: the
eager-closure budget went up.

settleWithin and its outcome type now live in host-kit/settle-within,
which imports nothing. retry.ts re-exports them, so every importer of
host-kit/retry keeps working and its closure is unchanged.

Both migrated sites import the leaf. The capture-kit grace wait reaches
it through the first call, which keeps that facade's closure at one
module. The perf waits previously left the 45 s grace timer armed for
its whole budget after xctrace had exited; the primitive clears it on
every settle path. Behaviour is otherwise the same.
thymikee added a commit that referenced this pull request Nov 20, 2026
#4816)

* refactor(host-kit): move settleWithin to a dependency-free leaf module

The primitive sat in retry.ts, so a caller that wanted only the deadline
race also evaluated the retry policy, the deadline class, the
diagnostics edge and the kernel error vocabulary. That is why #4788
reverted the capture-kit grace wait and both perf-xctrace waits: the
eager-closure budget went up.

settleWithin and its outcome type now live in host-kit/settle-within,
which imports nothing. retry.ts re-exports them, so every importer of
host-kit/retry keeps working and its closure is unchanged.

Both migrated sites import the leaf. The capture-kit grace wait reaches
it through the first call, which keeps that facade's closure at one
module. The perf waits previously left the 45 s grace timer armed for
its whole budget after xctrace had exited; the primitive clears it on
every settle path. Behaviour is otherwise the same.

* chore(gates): name the settle-within subpath in the export surface and race guidance

R11's host-kit export-surface enumeration lists the new subpath, and
R88's guidance names the leaf as the deadline primitive's home.

* refactor: import settleWithin from its leaf subpath and settle the validator compile wait through it

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

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
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