Skip to content

Commit bee9e1f

Browse files
committed
fix(settle): sample an extended confirmation on its own cadence
Poll cadence derived from the requested window once, before the loop ran, so the 1.5s confirmation this branch extends that window to was spent sampling at the caller's grid. At `--settle-quiet 25` that is 109 capture round trips to reach a verdict the default grid reaches in 10 — a confirmation whose cost is 11x the reads it needs, on the tightest windows where settle is asked for at all. Cadence now follows the window in force. Both directions hold: a tight window still gets a grid that can measure it, and an extended wait gets the default grid rather than the caller's. Pinned by the capture count in the 25ms case: 10 here, 109 with the recompute removed (mutation-checked).
1 parent 1f97dfc commit bee9e1f

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

‎src/commands/interaction/runtime/stable-capture.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,13 @@ test('settle confirms a broad replacement on a caller window shorter than the co
367367
outcome.waitedMs >= 1_500,
368368
`short quiet window switched the confirmation off: settled after ${outcome.waitedMs}ms`,
369369
);
370+
// Cadence follows the window in force, so the extended wait is sampled on the default grid:
371+
// 10 captures here, 109 when the loop keeps polling at the caller's 25 ms. Each of those is a
372+
// capture round trip, so the tight grid would turn a 1.5 s confirmation into ~1.5 s of reads.
373+
assert.ok(
374+
outcome.captures <= 12,
375+
`confirmation sampled on the caller's tight grid: ${outcome.captures} captures`,
376+
);
370377
});
371378

372379
test('settle keeps the default quiet window for a backend-less overlapping local mutation', async () => {

‎src/commands/interaction/runtime/stable-capture.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ import { withoutSettleChrome } from '@agent-device/capture-kit/snapshot-chrome';
3333

3434
const STABLE_POLL_INTERVAL_MS = 300;
3535
const STABLE_MIN_POLL_MS = 25;
36+
// Never slower than the default poll, never faster than the minimum: a tight window gets a cadence
37+
// that can measure it, a window the confirmation extends gets one that does not sample the whole
38+
// extended wait at the caller's original grid.
39+
function stableCapturePollMs(quietMs: number): number {
40+
return Math.min(STABLE_POLL_INTERVAL_MS, Math.max(STABLE_MIN_POLL_MS, quietMs));
41+
}
3642
// Wake PAST the quiet deadline rather than exactly on it. `setTimeout(n)` can
3743
// advance `Date.now()` by only n-1 — libuv times the sleep on the monotonic
3844
// loop clock while `now()` reads the wall clock — so a capture landing on the
@@ -88,10 +94,12 @@ export async function runStableCaptureLoop(
8894
const session = await runtime.sessions.get(options.session ?? 'default');
8995
let transitionBaseline: StableCaptureSignal | undefined;
9096
let preferredBackend = preferredSnapshotBackendForVerdict(session?.snapshot?.snapshotQuality);
91-
// Cadence derives from the quiet window (never slower than the default
97+
// Cadence derives from the quiet window in force (never slower than the default
9298
// poll): a caller asking for a 50ms quiet window should not be forced onto a
93-
// 300ms grid — and tests inject the budget instead of waiting real time.
94-
const pollMs = Math.min(STABLE_POLL_INTERVAL_MS, Math.max(STABLE_MIN_POLL_MS, quietMs));
99+
// 300ms grid, and a run the confirmation has extended to 1.5s should not spend
100+
// that wait on the caller's tight grid — every sample is a capture round trip.
101+
// Tests inject the budget instead of waiting real time.
102+
let pollMs = stableCapturePollMs(quietMs);
95103
let captures = 0;
96104
let lastSignal: StableCaptureSignal | undefined;
97105
let lastNodeCount = 0;
@@ -131,6 +139,9 @@ export async function runStableCaptureLoop(
131139
requestedQuietMs: quietMs,
132140
currentQuietMs: requiredQuietMs,
133141
});
142+
// Recomputed from the window in force rather than only when it changed: the window is a
143+
// monotonic max, so this is the same value, and the cadence cannot drift from it.
144+
pollMs = stableCapturePollMs(requiredQuietMs);
134145
preferredBackend ??= preferredSnapshotBackendForVerdict(capture.snapshot.snapshotQuality);
135146
const nowMs = now(runtime);
136147
const recoveredDeadlineMs = extendedDeadlineAfterPrivateAxRecovery({

0 commit comments

Comments
 (0)