Skip to content

feat: report truncated iOS accessibility trees as snapshot warnings - #59

Merged
cryptotavares merged 6 commits into
mainfrom
cryptotavares/mmcli-p1-38-a11y-truncation
Nov 19, 2026
Merged

cryptotavares merged 6 commits into
mainfrom
cryptotavares/mmcli-p1-38-a11y-truncation

Conversation

@cryptotavares

@cryptotavares cryptotavares commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Part of the mm CLI v2 effort (ticket P1-38, device-mcp part). Ships in device-mcp 0.9.0.

Summary: iOS snapshots now say when the idb companion cut the accessibility tree. Callers can then tell that elements on screen may be missing.

Problem

The idb companion stops reading the accessibility tree at a size bound. MetaMask Mobile wallet home already reaches it (depth 51). device-mcp asks for --nested, and that format does not report the cut. So the hierarchy silently misses elements, and the agent cannot find visible controls.

idb ui describe-all --api axbridge --format complete   # wallet home, header only
backend: axbridge-exclusive   truncated: true   walked: 166

Solution

When the installed idb client lists complete under --format, snapshots request --format complete instead of --nested. That document has the same nested tree plus truncated and backend. A cut read adds a warning to the new optional SnapshotResult.warnings.

flowchart TD
    A[snapshot] --> B{"idb help lists --format complete?"}
    B -->|no| L["--nested, as in 0.8.0"]
    B -->|yes| C["--format complete"]
    C --> D{"answer is a complete document?"}
    D -->|"no: flat tree"| F["read again with --nested, stop requesting complete"]
    D -->|yes| E{"truncated?"}
    E -->|yes| W["warnings: ax-tree-truncated(…)"]
    E -->|no| N[no warnings field]

    classDef new fill:#fff3cd,stroke:#d39e00
    class B,C,D,F,E,W new
Loading

Yellow boxes are new.

What changes

ax-tree-truncated(backend=axbridge-exclusive,depth=51,nodes=166)
Condition Result
idb without --format complete Same arguments and same output as 0.8.0
Tree not cut Same hierarchy. No warnings key.
Tree cut Same hierarchy, plus warnings: ["ax-tree-truncated(backend=<name>,depth=<n>,nodes=<n>)"]
Companion answers complete with a flat tree One more read with --nested. This backend does not request complete again.
device_snapshot MCP tool, with warnings A Warnings: … line before the elements

Decisions for reviewers

Decision Reason
Warning format ax-tree-truncated(backend=…,depth=…,nodes=…) instead of the ticket's ax-tree-truncated(depth>=50) The complete document does not report its limits. The warning gives what came back. The format is documented on the type and stays stable for core.
Count depth and nodes on the main read, before the ax merge The merged elements come from a second read, so they would hide the size of the cut read.
Any object with a backend key is a complete document Same check as idb's own _warn_if_complete_downgraded. An odd document then does not switch complete off for the session.
SnapshotResult.raw is the complete document on clients with complete raw has no documented format. The only user in this repo (scroll.ts) compares it for equality only.

Review guide

Read the files in this order:

# File What to check
1 src/utils/platform.ts idbSupportsCompleteDescribe: the help-text probe
2 src/backends/idb-backend.ts #describeAllArgs, #describeAllOnce (the flat-answer fallback), parseIdbDescribeAll, truncationWarning
3 src/backends/types.ts SnapshotResult.warnings and its format
4 src/tools/snapshot.ts The Warnings: line
5 Tests, fixture and README Old-client output unchanged, parsing of odd documents, the ax fallback
Testing
  • yarn build, yarn lint and yarn test pass (36 files, 852 tests, 6 skipped). Coverage thresholds are unchanged.
  • Live: iOS simulator, idb 1.7.1, MetaMask Mobile dev build, IdbBackend.snapshot() called directly. Before = npm 0.8.0, after = this branch. Run on the first commit; later commits change only edge-case parsing, the MCP tool text and docs.
Check 0.8.0 This branch
Login: truncated / warnings not reported false / none (depth 34, 67 nodes)
Wallet home: truncated / warnings not reported true / warning on 20 of 20 reads
Hierarchy, login and wallet home 67 and 189 nodes equal (deep compare)
Wallet home snapshot() p50, 20 runs 687 ms 686 ms
Wallet home raw size 63 kB 51 kB
mm describe-screen on login (core 0.14.0) output A equal to A

When the idb client supports `ui describe-all --format complete`, read
the consolidated document instead of `--nested`. Its `elements` is the
same nested tree, and it also reports `truncated` when the companion cut
the tree at its size bound. A truncated read adds
`ax-tree-truncated(backend=<name>,depth=<n>,nodes=<n>)` to the new
optional `SnapshotResult.warnings`.

An idb client without `--format complete` keeps today's arguments and
output. A companion that ignores the format (answers with a flat tree)
is read again with the previous format, once, and not asked again.
Match idb's own check: an object with a `backend` key is the complete
document, even when `backend` is null or `elements` is missing. Before,
such a document was read as a legacy tree, which built a one-element
hierarchy from the root and stopped requesting `--format complete`.
The warning names the backend only when it is a non-empty string.

A JSON root that is neither an array nor an object (for example `null`)
is now invalid output, as before this branch, and no longer turns
`--format complete` off.
When a snapshot has warnings (for example ax-tree-truncated), the
device_snapshot text adds a `Warnings: …` line before the elements, so
an agent that uses the device-mcp MCP server sees that the tree is
incomplete. Without warnings the output is unchanged.
@cryptotavares
cryptotavares marked this pull request as ready for review November 17, 2026 14:31
@cryptotavares
cryptotavares requested a review from a team as a code owner November 17, 2026 14:31

@itsyoboieltr itsyoboieltr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@cryptotavares
cryptotavares merged commit c08fc23 into main Nov 19, 2026
35 checks passed
@cryptotavares
cryptotavares deleted the cryptotavares/mmcli-p1-38-a11y-truncation branch November 19, 2026 08:52
cryptotavares added a commit that referenced this pull request Dec 4, 2026
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.

```text
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:

```text
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`.

```mermaid
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
```

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 |

<details>
<summary>Testing</summary>

- `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.

</details>
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.

3 participants