feat(interaction): opt-in --verify evidence for press/click/fill

Merged
TH
5mo ago
+980-12

Summary

Implements the approved design from #1047: an opt-in --verify flag on press/click/fill that adds a cheap evidence object to the success payload instead of requiring agents to spend a full follow-up snapshot -i round trip to confirm a mutation had an effect.

"evidence": {
  "foregroundApp": "com.example.app",  // only if the capture already carries an app scope
  "nodeCount": 52,
  "interactiveNodeCount": 14,
  "digest": "ax1:9f3a…",
  "changedFromBefore": true
}

Token-saving rationale

Today the only way for an agent to confirm a press/click/fill actually changed the UI is a follow-up snapshot -i, which returns the whole tree and is the dominant token cost in real agent loops. --verify still pays one device-side capture (unavoidable — you need some post-action read), but the daemon computes a ~50-byte digest from that capture instead of serializing the node tree back to the client. The tree is discarded server-side and never crosses the wire. Off by default, so existing callers see byte-identical responses.

Mechanics (per the design)

  • New module src/utils/ax-digest.ts: order-independent multiset digest over each node's (type, label, identifier), plus a small evidence-summary helper (interactiveNodeCount, foregroundApp). Kept standalone so #1049's settle primitive can reuse it without coupling to the interaction runtime.
  • Pre-action: the interaction resolution path (src/commands/interaction/runtime/resolution.ts) already captures a snapshot to resolve ref/selector targets — that capture is hashed with zero extra device round trips. Point targets normally skip capture entirely, so --verify on a point target opts into one extra baseline capture (only when explicitly requested).
  • Post-action: when --verify is set, one interactive-only capture through the same captureInteractionSnapshot helper the runtime already uses, digested, then discarded — never serialized to the client.
  • foregroundApp: only populated when the capture's nodes already carry bundleId/appName; no separate appstate round trip is added.
  • Response plumbing: additive evidence field through src/contracts/interaction.ts, the interactionResultExtra allowlist in src/daemon/handlers/interaction-touch-targets.ts (per #1040's lesson — unknown fields are dropped silently), and src/mcp/command-output-schemas.ts. The --verify flag is threaded through the CLI flag schema / per-command allowlist / MCP input schema the same way --double-tap is.
  • Fast-path bypass safety: the native-ref backend primitive (tapTarget/fillTarget) and the direct-iOS-selector fast paths skip the resolution/capture machinery entirely, so both are gated off when --verify is set (mirroring the existing hasNonDefaultTapOptions/hasNonDefaultClickOptions gates).
  • Default behavior unchanged: no flag → no evidence key at all, byte-identical to today.

Deviations from the design

None of substance. One judgment call the design left open: nodeCount vs interactiveNodeCount — since the post-action capture is itself interactive-only, nodeCount is that capture's total node count and interactiveNodeCount is the subset the platform doesn't report as hittable: false, which is a cheap, meaningful split without any extra signal.

click doesn't have a dedicated CommandResultMap/MCP output-schema entry today (a pre-existing gap, unrelated to this issue — only press/fill/longpress are on the typed-result spine). evidence still flows through click's daemon response via the shared interactionResultExtra allowlist (the same way targetHittable/hint already do for click), just without MCP JSON-schema validation of the output, consistent with click's existing behavior. Migrating click onto the typed spine is out of scope here.

Closes #1047

Test plan

  • New unit tests for ax-digest.ts (order independence, sensitivity to label/type/identifier changes, stability, empty input)
  • Runtime tests (interactions.test.ts): --verify off → no evidence key at all; on → evidence present with changedFromBefore true when the mocked post-capture differs and false when identical; point-target baseline capture; native-ref fast path skipped when verifying
  • Daemon handler tests (interaction.test.ts): evidence survives the interactionResultExtra allowlist for both press and fill
  • pnpm test:integration:provider — 87 passed
  • Full daemon + interaction + mcp + client unit suites — all green (some unrelated android/adb process-spawn tests are flaky under contention per known repo issue; verified green in isolation)
  • pnpm format && pnpm check:quick — clean