fix(settle): resolve the default quiet window from the capture it settles

Merged
TH
2mo ago
+813-179

Part of #3470's follow-up work; does not close it. #3482 and #3512 have both merged, so this now targets main directly and carries only its own 8 files.

Summary

--settle asked every capture the same 500 ms quiet window. The iOS simulator snapshot bridge answers well inside that, so the loop spends a whole extra capture holding a window the tree has already satisfied — and the window is paid twice per settled command (the settle's final window, then again as the next command's baseline capture).

The loop now prices the window from the capture it performs, not the one the action was performed against: an unrequested --settle resolves 300 ms when that read came from the simulator snapshot bridge, and 500 ms for every other read — the Apple runner's own tree read, an Android capture from either producer (android-uiautomator over ADB or appium-source over Appium), a web agent-browser read, an iOS tree from a provider acquisition, or one that declares no provenance at all — because only the bridge carries the measurement. It re-prices on every capture, so a mid-run fallback to a more expensive read widens the remaining wait, and it reports the window it ran on (settle.quietMs) so the response states which default it applied.

Keying on the baseline's producer was the first cut here and is wrong: the window governs the loop's own reads, and a stored ref frame or session tree can be several commands stale or served by another producer. Keying on the channel would be wrong too — the bridge and the Apple runner share xctest and read at different costs. --settle-quiet still outranks both, and wait stable keeps the general default of its own.

Evidence for 300 ms

iPhone 17 simulator, one run per window per rep, order alternating so host load falls on both arms alike:

Screen500 ms300 mscaptures
text entry, Catalog search859 ms664 ms3 → 2
press, product Add to cart844 ms647 ms3 → 2

Accuracy held: diff snapshot -i two seconds after the settled response found no further change for the text entry at either window (0/6 both), and one further change in three of six runs for the press at either window — that screen keeps animating after the press whichever window is asked for, so the shorter window changes the wait, not the verdict's accuracy. (These A/B runs, and the live runs below, were taken on the pre-split build that still carried the presented-surface trigger since lifted to #3570; both arms request their window explicitly, so the comparison is this layer's window arithmetic alone.)

Validation

Tested at fd4c0c7cd (d371573d2 plus the review follow-up that folds the quiet-window closure into the loop, no behaviour or test change). settle-default-quiet.test.ts pins the window per capture producer in both mismatch directions (bridge baseline + runner re-read widens to 500 ms; runner baseline + bridge re-read narrows to 300 ms) and pins flag precedence; mutation check, measured when the key moved (6f43f54e6) and unchanged in shape here: keying the rule back on the baseline fails three of those cases plus one daemon case. generic-settle.test.ts pins the three default-window routes on an injected fake clock (a 25 ms poll, a 30 ms sleep, deterministic elapsed arithmetic — no real-time budget races).

Arming is re-checked on every capture until one qualifies, and only the caller passing no pre-action projection rules the confirmation out for the run — the two per-capture inputs (viewport-root completeness, declared capture channel) decide nothing on their own. Three cases pin it, each with the 7-node settings table as the baseline so the tiny-baseline hatch cannot carry it, and each requiring waitedMs >= 1500: after a rootless, an empty, or an other-channel first capture. All three fail at 84f170e31 (settling at 1004 / 802 / 502 ms).

The per-capture re-pricing briefly broke the layer beneath it: requiredQuietMs received the freshly resolved default each poll instead of the previous requirement, so the 1.5 s dismissal floor survived only while each consecutive capture kept qualifying. The comparison refuses a node-less side, so one empty mid-animation projection dropped the floor and the next empty read settled the run, well before the dismissal had met its floor. The floor is now latched once a capture proves the dismissal and maxed against the current window rather than replacing it (pinned by a dismissal confirmation is not dropped by a later empty projection; it fails against the pre-fix head, and passes against layer 1's source, which held the floor only by accumulating).

The live run for this head — default window, three reps, armed on all three, with the late diff and the producer evidence — is in the review-response comment on this PR (## The live run, head d371573d2, iPhone 17 Pro). The runs below predate it and are kept for the window-arithmetic comparison they carry.

Live on the simulator after the earlier rework: default press --settle reports quietMs: 300 at 658–664 ms over two captures; --settle-quiet 700 still wins at 1050–1076 ms; wait stable is unaffected. Those runs were taken on the pre-split build, which still carried the presented-surface trigger since lifted to #3570; at this head the trigger is main's, which arms on a superset of transitions, so the floor can only hold at least as well here — the window arithmetic they measure (quietMs, flag precedence, capture counts) is this layer's and unchanged.

Gate

pnpm check:affected --run --base origin/main at 84f170e31: all runnable checks passed (exit 0) — 514 related test files / 3,998 tests, Fallow clean on the 8 changed files — held under the machine-wide lock, started 2026-10-13T10:56:14Z. format, lint, tsc -b, check:layering, build and vitest related all pass; the 1500 → 1500ms unit fix in the MCP/Node field description is covered by the schema/parser/registry suites (47 files, 309 tests) with no text pin against it.

Note on scope

An earlier revision of this stack carried, one layer down, a presented-surface arm that keyed on the baseline's node count — the defect found in #3512's review (https://github.com/callstack/agent-device/pull/3512#issuecomment-6127522577), where a dismissal whose baseline is a full tree stops confirming. That arm is not in this stack anymore: #3512 is now the window decoupling alone and keeps main's trigger, and the narrowing (with that rule and the measured route table) sits on top of this layer as the draft https://github.com/callstack/agent-device/pull/3570, blocked by #3565. So this layer arms exactly where main arms, only at every requested window, and nothing here depends on how #3565 resolves the fact.