fix(daemon): let a finishing mutation record the tree it acted through (#3471)
Summary
Closes #3471. Top layer (3 of 3), on #3548 → #3538. This is the history that makes the gate fire, plus the disclosure surfaces.
A press resolves a point from one capture, so a press issued right after an action that animated the UI can resolve a point the control has already vacated and report success: true on the vacancy.
SessionState.deferredSurface becomes a two-variant claim instead of postGestureStabilization: stabilize (the capture side still owes a quiet-window loop) or recheck (a mutation dispatched through a captured tree whose own effect may still be animating). A mutation that resolved a target records the tree it acted through, stamped at the instant it finished, and runtime-session.ts carries it to the command layer. ADR 0014's device side-effect seam retires it. One field, one owner (deferred-interaction-outcome.ts). A recheck never overwrites an owed stabilize: an @ref press spends no capture, so a scroll's quiet-window loop can still be pending when that press finishes.
Two judgement fixes from review:
- The baseline is a tree the caller can vouch for: the tree the action resolved against, or — only when
--settleconverged and stored its settled tree — the tree that proved the surface at rest. The signal issettle.diff, which the runtime attaches in exactly that conjunction;settledalone would license recording a tree the store refused as sparse. The old rule preferredsession.snapshotwhenever its node array differed from the resolution's, which could adopt a@refframe tree or a freshness capture: a surface the action never looked at.MovingTargetBaseline.tree's doc now states both routes, which is the widening #3538's review asked for once a writer existed. - A corroborated iOS tap failure passed the resolved target's own tree to the finalizer. Omitting it reached the "acted without a daemon tree" rule and retired the baseline a previous mutation had recorded, leaving the next press unscreened.
The trigger is deliberately wider than the guard: any command that acted through a tree records a baseline, while only press/click are guarded — a fill that opened a keyboard can leave the surface animating for the press that follows, and that is pinned by a test.
agent-device press 'label="Slide target"'
# error.details.reason = target_still_moving, dispatched = no
# rect {x:894,w:139} → movedToRect {x:755,w:278}, waitedMs ≈ 600
32 files, 1,000 gross (918 added + 82 deleted). This layer also lands the ADR 0011 targetStability cells and their Layer-3 scenarios (the root coverage gate refuses an enforced cell with no scenario), and it is where #3548's review pointed the live evidence: the gate is only reachable once a writer exists, so the cells and the device numbers could not honestly sit below here. Deviation: src/commands/schema/cli-help.ts does not name the refusal — the workflow topic is at a hard 9,100-byte cap; the recovery ships in the per-command usage and the website docs, which also scope the guarantee to daemon-resolved targets and name the two routes that hand off before the check.
Validation
Tested at a99b315cb. The live numbers below were collected at d7a95c683, a pre-rebase head of this branch; everything since is comments, docs wording, and the judgement fixes above, each with its own test.
Android emulator ReactNative_API_35, throwaway SettleProbe fixture (native slide-in + its own hit counter, so the outcome is read from the app, not the command result); press "Probe slide[ N]" then immediately press 'label="Slide target"', no --settle:
| case | base 46dae0f2e (pre-stack, unpatched) | head |
|---|---|---|
| 400 ms slide-in, 16 trials | landed 16 / miss 0 / refused 0 | landed 16 / miss 0 / refused 0, median press 724 ms |
| 3 s slide-in, 16 trials | landed 11 / miss 5 / refused 0 — five presses reported success on a point the control had vacated | refused 16 / landed 0 / miss 0, median 850 ms, waitedMs ≈ 600 |
| idle press, 12 trials | — | median 265 ms in-window stationary, 243 ms not-recent; 1 capture, none from the gate |
| iPhone 17 Pro sim, 8 guarded presses | — | 8/8 succeeded, 0 refusals |
The hit counter never moved on a refusal.
Live scope, stated rather than implied. The device runs above cover the ADB-backed Android helper and the iOS runner route. No Appium-backed Android run was made. The claim stays backend-neutral for two reasons that are checkable rather than asserted: the exposure is the daemon's own capture-to-dispatch distance (the gate sits in the shared runtime path, in front of runtime.backend.tap, with no backend branch anywhere in it), and the baseline writer is pinned on a non-ADB backend by the new daemon-route scenario, which drives the real iOS runner conversation through dispatchRuntimeInteraction and toRuntimeSessionRecord. The measurement claim that is backend-specific — "reporting frames in flight" — names ADB in the targetStability cell's own text. Open gaps, unchanged and stated plainly: (a) a guarded @ref press has not been re-verified live since the observation rule changed on #3548 (the probe fixture is gone and the shared emulator is held by another worker) — it is covered by the runtime-ref targetStability scenarios; (b) the live table has no delayed-start animation case (a control that has not begun moving when the press resolves, which is the case the stationary screening reads exactly like a finished transition). No device was available in this pass, so that row is owed by whoever next holds the emulator; #3538's review flagged it as needing no code change.
pnpm check:affected --run --base fix/3471-gate-measurements— result recorded in a comment on this PR.typecheck,lint,format:check,check:layering(R7 field ownership),check:production-exports,check:doc-prose,check:doc-links,check:mcp-metadata,eager-closure-budgets— clean.vitest run src/commands src/daemon src/interactions src/__tests__ test/integration/interaction-contract test/integration/provider-scenarios packages/contracts packages/selectors packages/ad-script— pass, including thetargetStabilityscenarios (refusal, re-aim, stationary on both runtime paths), the gesture→press→capture test that pins the owed-stabilization guard, and the provider-integration contract that a press after a converged settle spends a tap and nothing else.check:fallow --base origin/mainflaggeddispatchRuntimeInteractionat its complexity ceiling once both forwards were conditional spreads; passing the optional values directly clears it (7134070b7).- Mutation-checked for the wiring the review asked for: deleting
preActionTree: runtimeResult.preActionfromdispatchRuntimeInteraction, or themovingTargetBaselinecarrier fromtoRuntimeSessionRecord, each reddens the daemon-route scenario — press A through the real route, press B screened against what A left, refusing withtarget_still_movinganddispatched: noover four runner captures and one tap. That scenario is the Layer-3targetStabilityrefusal case, now run throughwithIosContractDaemonrather than an injected session record. Deleting the corroborated path'spreActionTreereddensa corroborated tap failure still records the tree it acted through; restoring the array-identity preference reddensthe baseline is the tree the action acted through, whatever the session now holds; readingsettle.settledinstead ofsettle.diffreddensa converged settle that stored nothing keeps the acted-through tree; narrowing the writer to press/click reddensthe trigger is wider than the guard on purpose. - Known flake surface, named in the test: the daemon-route scenario runs on the wall clock, because production installs no runtime clock on that route. A host loaded enough to starve the process past the 500 ms window sends press B idle and it taps — the same
Unexpected provider call: ios.runner.tapa wiring regression gives. Pinning the window would mean threading a clock through the router deps for one scenario, so the risk is documented at the test instead. - Residual risk: worst case inside the window is ~2 captures + ~550 ms on a press, bounded by the existing daemon timeout policy;
Maestroandnative-refare unguarded by declaration, not omission.