Skip to content

feat: warn when an iOS snapshot falls back from axbridge - #67

Merged
cryptotavares merged 1 commit into
mainfrom
cryptotavares/ax-tree-fallback-warning
Dec 4, 2026
Merged

cryptotavares merged 1 commit into
mainfrom
cryptotavares/ax-tree-fallback-warning

Conversation

@cryptotavares

Copy link
Copy Markdown
Contributor

Part of the mm CLI v2 effort (ticket P1-53).

Summary: When the preferred iOS accessibility backend fails, IdbBackend.snapshot() now adds an ax-tree-fallback warning with the backend that served the read and the reason. An agent can tell a partial fallback read from a complete one.

Problem

idb ui describe-all --api axbridge reads the tree as one XCTest snapshot. iOS rejects that snapshot when the tree is deeper than its limit (about 62 levels), and idb reports "did not respond to an accessibility request". The MetaMask Mobile wallet home reaches 64 levels, so the preferred read fails.

IdbBackend.#describeAll then tries the other --api and serves ax. ax returns a flat tree. On the wallet home that is depth 2, 45 nodes and 35 of the 97 test IDs. snapshot() returns it without a warning, so an agent cannot tell this tree from a full read.

idb ui describe-all --api axbridge  → exit 1: did not respond to an accessibility request
IdbBackend.#describeAll             → try the other --api
idb ui describe-all --api ax        → flat tree: depth 2, 45 nodes
IdbBackend.snapshot()               → hierarchy and no warnings

A recorded live investigation on an iOS 27 simulator confirmed the cause: at depth 64, single-fetch fails at every depth limit while the per-node walk of the same screen succeeds. Login (34 levels) and the hamburger menu (49) read with axbridge in the same session.

Solution

#describeAll reports the preferred backend's error when a later backend serves the read. snapshot() compares the serving backend with apis[0], the version preference, and adds:

ax-tree-fallback(from=<preferred>,to=<used>,reason=<not-responding|error>)

reason is not-responding when the preferred error contains "did not respond to an accessibility request", otherwise error.

flowchart TD
    A[snapshot] --> B["#describeAll: try apis[0]"]
    B -->|ok| N[no fallback warning]
    B -->|fails| C[try the other --api]
    C -->|fails| X[Reject: every backend failed]
    C -->|ok| D{"preferred error has<br/>'did not respond...'?"}
    D -->|yes| E["ax-tree-fallback(...,reason=not-responding)"]
    D -->|no| F["ax-tree-fallback(...,reason=error)"]
    E --> G{"served read truncated?"}
    F --> G
    G -->|yes| H["also ax-tree-truncated(...)"]
    G -->|no| I[only the fallback warning]

    classDef new fill:#fff3cd,stroke:#d39e00
    classDef stop fill:#f8d7da,stroke:#c82333
    class D,E,F,G,H new
    class X stop
Loading

Yellow boxes are new. Red boxes stop the snapshot.

What changes

Condition Result
idb client without --api (single null attempt) No fallback warning
Preferred backend serves the read, with or without the ax supplement No fallback warning
Preferred fails with "did not respond to an accessibility request" ax-tree-fallback(...,reason=not-responding)
Preferred fails with another error ax-tree-fallback(...,reason=error)
ax is preferred and axbridge serves the read ax-tree-fallback(from=ax,to=axbridge,reason=error)
Fallback read is also truncated Both warnings, fallback first
Every backend fails Snapshot rejects, as today
device_snapshot Prints the warning on the existing Warnings: line; no tool change

Core PR #141 passes SnapshotResult.warnings on to agents. This PR needs no core change.

Decisions for reviewers

Decision Reason
Warn only when the serving backend is not apis[0] snapshot() can read ax in parallel with axbridge and merge missing identified elements into the axbridge tree. A successful merge is not a fallback.
Compare with apis[0] instead of a fixed axbridge preference chooseAxApi prefers ax before iOS 18 and axbridge from iOS 18. The warning names the actual --api pair.
No warning for the null attempt An idb client without --api has no alternative backend, so nothing fell back.
reason comes from the error text idb maps the guest's snapshot rejection to "did not respond to an accessibility request". Other failures get error.
No CHANGELOG.md entry Release automation generates the changelog, as for #59 and #65.

Review guide

Read the files in this order:

# File What to check
1 src/backends/idb-backend.ts fallbackWarning, and the api !== apis[0] check in snapshot(); preferredError in #describeAll
2 src/backends/types.ts The ax-tree-fallback format on SnapshotResult.warnings
3 src/backends/idb-backend.test.ts The fallback conditions in the table above
4 README.md, src/tools/snapshot.test.ts The documented meaning and the Warnings: line
Testing
  • yarn build exits 0.
  • yarn lint exits 0.
  • yarn test exits 0: 38 files, 986 passed, 6 skipped.
  • Coverage does not drop. Compared with origin/main: statements 82.97% → 83.02%, branches 80.31% → 80.39%, functions 77.97% → 78.00%, lines 83.24% → 83.29%.
  • Tests cover: reason=not-responding, reason=error, the ax → axbridge order, no warning for an axbridge read with the ax merge, both warnings when the fallback read is truncated, and the existing rejection when every backend fails.
  • No live iOS run: the simulator was in use by other agents. The tests use the error text and the flat-tree shape from the recorded investigation.

@cryptotavares
cryptotavares merged commit 9103839 into main Dec 4, 2026
35 checks passed
@cryptotavares
cryptotavares deleted the cryptotavares/ax-tree-fallback-warning branch December 4, 2026 09:43
cryptotavares added a commit to MetaMask/client-mcp-core that referenced this pull request Dec 4, 2026
Part of the mm CLI v2 effort (P1-38, core part). The device-mcp part is
[device-mcp#59](MetaMask/device-mcp#59),
released in device-mcp 0.9.0.

> **Summary:** A truncated iOS accessibility tree now shows up as a
top-level `warnings` array in `describe_screen`,
`accessibility_snapshot` and observations. Each warning says what to do.
Output without warnings does not change.

## Problem

The idb accessibility read stops at a size bound. On the 2026-10-28 run,
iOS wallet home was 51 levels deep, so some visible controls were not in
the tree. device-mcp reports this as `SnapshotResult.warnings`, but core
reads only `hierarchy` and drops it. The agent sees a tree with no sign
that it is incomplete, and concludes that the element is not on screen.

```text
device-mcp snapshot:  { hierarchy: [...], warnings: ["ax-tree-truncated(backend=axbridge-exclusive,depth=51,nodes=166)"] }
mm describe-screen:   { "state": ..., "testIds": ..., "a11y": ..., "screenshot": null }   <- no warning
```

## Solution

The mobile driver keeps the snapshot warnings and adds advice for the
codes it knows. The tools and the observation copy them to the top level
of their output.

```mermaid
flowchart TD
    A["backend.snapshot()"] --> B{"SnapshotResult.warnings<br/>non-empty?"}
    B -->|no| C["tree and testIds as before<br/>(no warnings key)"]
    B -->|yes| D["withAdvice: add what to do<br/>for a known code"]
    D --> E["AccessibilityTree.warnings /<br/>ScreenCapture.warnings"]
    E --> F["describe_screen and<br/>accessibility_snapshot: result.warnings"]
    E --> G["observation.warnings<br/>(kept in diff observations)"]

    classDef new fill:#fff3cd,stroke:#d39e00
    class B,D,E,F,G new
```

Yellow boxes are new.

## What the agent sees

`mm describe-screen` on a truncated iOS tree (message from
`WARNING_ADVICE`, details from device-mcp):

```text
{"warnings":["ax-tree-truncated(backend=axbridge-exclusive,depth=51,nodes=166): the tree hit the device read limit, so elements on screen can be missing from a11y and testIds. See them with `mm screenshot`; tap one with `mm tap-coordinates <x> <y>` (same units as `bounds`)."],"state":{...},"testIds":{...},"a11y":{...},"screenshot":null}
```

| Condition | Result |
|---|---|
| Snapshot has no `warnings`, or an empty array | No `warnings` key
anywhere. Output is unchanged. |
| `ax-tree-truncated(...)` | `warnings: ["ax-tree-truncated(...): the
tree hit the device read limit, …"]` |
| `ax-tree-fallback(...)`
([device-mcp#67](MetaMask/device-mcp#67), not
released) | `warnings: ["ax-tree-fallback(...): the full read failed,
…"]`: confirm the screen with a control that is present, tap a missing
element by coordinates |
| An unknown code | `warnings: ["<code>(...)"]`, passed through without
advice |
| `click` (or any call) with an observation | `observations.warnings`,
also in a `diff` observation |
| Browser | No change. Playwright has no `warnings` source. |

## Decisions for reviewers

| Decision | Reason |
|---|---|
| No depth/node-count heuristic in core | The ticket asks for one only
"until device-mcp ships it". Core depends on device-mcp `^0.10.0`, which
reports the truncation from idb's own `truncated` flag. |
| Advice: `mm screenshot` and `mm tap-coordinates` | These work today.
`--root` (P1-39) is not on mobile. `--testid`, refs and
`scroll-to-element` search the same snapshot as the tree, so they do not
find an element that is missing from it. |
| Warning first, then advice, in one string | The device-mcp code stays
a stable prefix to match on. One `string[]` keeps the shape of
`SnapshotResult.warnings`. |
| `warnings` placed first in the output | An agent reads it before the
tree. The browser has no warning field to reuse. |
| Advice map keyed by code; unknown codes pass through | A new
device-mcp code still reaches the agent without a core change. |

## Review guide

Read the files in this order:

| # | File | What to check |
|---|---|---|
| 1 | `src/platform/types.ts` | New `AccessibilityTree` type;
`ScreenCapture` extends it. |
| 2 | `src/platform/mobile-platform-driver.ts` | `#snapshotApp` keeps
`warnings`; `WARNING_ADVICE` and `withAdvice`. |
| 3 | `src/tools/discovery-tools.ts` | `describe_screen` and
`accessibility_snapshot` add `warnings` only when present. |
| 4 | `src/server/create-server.ts`, `observation-compaction.ts` |
`snapshotMobile`, `collectObservation` and `diffObservation` carry
`warnings`. |
| 5 | `*.test.ts` | Driver, tools and HTTP tests (see Testing). |
| 6 | `SKILL.md`, `README.md` | The warning and what to do about it. |

<details>
<summary>Testing</summary>

- `yarn build`, `yarn lint` and `CI=1 yarn test` pass (76 files, 2975
tests). Coverage: statements 98.13, branches 95.54, functions 97.53,
lines 98.23 (floors 97 / 94 / 96 / 97).
- `mobile-platform-driver.test.ts`: the exact messages for
`ax-tree-truncated` and `ax-tree-fallback`; advice on each warning when
both are present; an unknown code passes through; no `warnings` key for
an absent or empty array (`captureScreen` and `getAccessibilityTree`).
- `discovery-tools.test.ts`: both tools return the driver warnings, and
no key without them.
- `create-server.test.ts` (real `MobilePlatformDriver`, fake backend):
the warning reaches `describe_screen`, `accessibility_snapshot` and a
`click` observation, also a `diff` observation. No `warnings` text in
any of the three bodies without warnings.
- Live, iOS sim (iPhone 17 Pro, dev client, Metro 8085, this branch via
yalc, consumer device-mcp 0.10.0):

  | Screen | Result |
  |---|---|
| Login | `describe-screen` and `accessibility-snapshot`: no `warnings`
key. idb read: depth 34, 67 nodes, `truncated: false`. |
| Wallet home | No `warnings` key. The full `axbridge` read fails ("did
not respond to an accessibility request"), and device-mcp 0.10.0 falls
back to the flat `ax` read (depth 2, 45 nodes) without a warning. |

On the current app build, wallet home is 64 levels deep. iOS rejects the
full read beyond about 62 levels, so device-mcp falls back to the flat
read. [device-mcp#67](MetaMask/device-mcp#67)
adds the `ax-tree-fallback` warning for that case. The truncation
warning is covered by the unit and HTTP tests above, not by a live read.

</details>

<details>
<summary>Docs changed</summary>

- `SKILL.md`: the `warnings` array, an example, and what to do
(screenshot, then `tap-coordinates`).
- `README.md`: the response shape, the observation fields, and where
device warnings appear.

</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants