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

Merged
TH
7d ago
+288-24

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.