Skip to content

refactor(host-kit): add request Scope and use it for the wait capture deadline - #4794

Merged
thymikee merged 4 commits into
mainfrom
refactor/request-scope
Nov 20, 2026
Merged

thymikee merged 4 commits into
mainfrom
refactor/request-scope

Conversation

@thymikee

@thymikee thymikee commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Summary

Adds a small request Scope to host-kit and uses it for the wait capture deadline. Part of #4789.

openScope(signal, limit, now) returns { signal, commit, close }. It has one controller and one lazily armed, unref'd timer. commit checks the inherited abort, then the existing Deadline. The timer and commit call one expiry operation. child, defer and seal propagation are not in this step. They land with request-cancel.ts, where a real caller tests them.

Behavior changes:

  • wait: a capture that answers after its cutoff is now a timeout. Before, runWithinWaitDeadline read a flag that only the timer callback set, so a capture that finished after the cutoff, but before that callback ran, counted as readable.
  • --settle and wait stable use the same function (captureStableSignalWithinDeadline). A capture that the deadline cancelled in flight is still stalled. A capture that answered after the cutoff is now expired: the loop reports that it never settled, and the late capture does not reach pacing.observe or the budget recovery, because it is not evidence for this budget.
  • The scope reads a monotonic clock (performance.now()) unless the caller injected a runtime clock, so a wall-clock jump cannot refuse a capture.

Parent-cancel precedence, the TimeoutError reason, and waiting for the capture to settle are unchanged.

Placement: Scope is in packages/host-kit/src/internal/retry.ts, beside the Deadline it uses, and is exported through @agent-device/host-kit/retry. A separate module failed the eager-closure gate (ADR 0027) on nine entry surfaces. The placement question for request-cancel.ts is on #4789.

7 files changed, 288 insertions(+), 24 deletions(-). Production: +114/−24 lines. No wire declaration changed.

Validation

Base 854bba6c5a. Head d806b4472c.

  • Red first: "a capture that answers after the cutoff is a timeout and not a readable capture" fails with the base wait-deadline.ts (1 failed, 16 passed) and passes at the head.
  • Red first: "a settle capture that answers after the budget reports never settled, not a stall" fails without the stable-capture.ts change and passes at the head. It drives the stable loop with a fake clock.
  • pnpm check:affected --run: passed with an exact-head receipt.
  • scripts/__tests__/eager-closure-budgets.test.ts: passed. pnpm run check:production-exports: no issues.
  • Scope unit tests use an injected clock: commit refuses after the deadline with no timer fired, an inherited abort keeps its reason, the timer aborts the signal at the deadline, close seals publication and stops the timer.
  • Coverage failed at 51f66a2 on settle-observation.test.ts. That was this PR: a late settle capture was reported as stalled. The second behavior change above fixes it.
  • Smoke Tests and Coverage must finish green at this head before merge.

…off must be a timeout

Regression for #4789 step 1. Fails on base 854bba6: the capture returns
before the deadline timer callback runs, so runWithinWaitDeadline reports
{ timedOut: false } and the poll is counted readable.
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.44 MB 6.44 MB +522 B
Package (unpacked) 6.44 MB 6.44 MB +522 B
Package (download) 1.96 MB 1.96 MB +209 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 23.0 ms 21.3 ms -1.6 ms
CLI --help 43.3 ms 49.5 ms +6.2 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 51f66a2. The verdict and the full list are in the summary comment.

Comment thread packages/host-kit/src/internal/retry.ts Resolved
@thymikee

Copy link
Copy Markdown
Member Author

No blocking code problems at 51f66a2. Smoke Tests (android, ios, macos), Coverage and the remaining checks are still running, so none has failed yet. The diff changes how every wait and --settle/stable capture is published through runWithinWaitDeadline, so a smoke failure at this head would count as related until shown otherwise. Please let those lanes finish green on the wait and settle/stable routes. One unmeasured change: commit() now reads runtime.clock, which falls back to Date.now in production, so a forward wall-clock jump during a capture can make it refuse the capture where the monotonic timer did not. There are no conflicts.

Not blocking, and you can take or leave these: the stable-loop caller captureStableSignalWithinDeadline also uses runWithinWaitDeadline, so a capture that returns after deadlineMs but before the timer fires is now stalled instead of captured, which skips pacing.observe and the recoverStableCaptureBudget extension. The PR body names only wait captures, so it could name this caller and add one stable-loop case with fake timers that expects stalled. In retry.ts, child(), defer(), the release loop in close(), the deadline getter, ScopeClosedError and the parent walk have no production caller yet, and only unit tests exercise them. The block comment at retry.ts:568 explains the /retry placement by ADR 0027 module count, which already lives on #4789 and in the PR body, so it could leave the source.

Could step 1 be about 60 production lines: openScope(signal, limit, now) returning {signal, commit, close}, with one controller, one lazily armed unref'd timer, and commit() checking the inherited abort and then the clock through the existing Deadline? child(), defer() and seal propagation could then land with request-cancel.ts, where a real caller and regression test validate them. That would mean amending #4789 to add Scope members per step, and settling /request against /retry placement under ADR 0027 before step 2.

@thymikee

Copy link
Copy Markdown
Member Author

Pushed d806b4472c. This answers the review at 51f66a2.

  1. Smaller step 1: done. Scope is now { signal, commit, close } in 63 lines: one controller, one lazily armed unref'd timer, and commit checks the inherited abort and then the existing Deadline. child, defer and seal propagation are removed. They land with request-cancel.ts. Request Scope: one owner for deadlines, cancellation and late publication #4789 is amended for step 1.
  2. Late stable capture: captureStableSignalWithinDeadline now separates a capture cancelled in flight (stalled) from one that answered after the cutoff (expired). An expired read skips pacing.observe and the budget recovery on purpose, and the loop reports never settled. The PR body names this. A fake-clock stable-loop case covers it and fails without the change. This also explains the Coverage failure at 51f66a2.
  3. Monotonic clock: the scope uses performance.now() unless the caller injected a runtime clock.
  4. Comment: the ADR placement block is out of the source. The reasoning is in the PR body and on Request Scope: one owner for deadlines, cancellation and late publication #4789.
  5. Evidence: check:affected passed at this exact head. Smoke Tests and Coverage are running here.
  6. Placement: with Scope built on Deadline, /retry is its home for this step. The /request question for step 2 needs the maintainer's decision on Request Scope: one owner for deadlines, cancellation and late publication #4789.

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of d806b44

Since the review of 51f66a2: 0 new · 0 still open · 1 no longer raised

No open findings.

1 finding below minor hidden (user_settings.min_severity).

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 d806b44. The fix for the earlier review is in: all 18 checks now pass, including Coverage, which failed at 51f66a2 on the settle stall classification, and the iOS, macOS and other Smoke Tests lanes that exercise the wait and settle routes this change touches. The code matches the minimal design. The late-answer window (cutoff passed, timer callback not yet run) cannot be forced in a live device run, so the smoke lanes do not cover that race.

Not blocking: WaitDeadlineResult in wait-deadline.ts tells a cancelled capture from a late answer only by whether the error key is present, and stable-capture.ts reads it with 'error' in result. A later edit that spreads the error conditionally would silently turn a cancelled capture into expired, so an explicit cause: 'cancelled' | 'late' field would be safer. You can take or leave this.

On the open inline thread about ScopeClosedError (#4794 (comment)): it no longer applies. The delta removed ScopeClosedError, and no production route reaches the plain Error that close() now seals with, because wait-deadline.ts calls commit in try and close in finally. Please resolve it.

No conflicts are known, and nothing else needs to happen before merge.

@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 d8f5b96 into main Nov 20, 2026
18 checks passed
@thymikee
thymikee deleted the refactor/request-scope branch November 20, 2026 11:34
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
* 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)
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