From 933037b86312b5fe78b6b1530f4842bf5d810134 Mon Sep 17 00:00:00 2001 From: cryptotavares Date: Thu, 3 Dec 2026 15:42:23 +0000 Subject: [PATCH 1/3] fix: match mobile test IDs and refs exactly Ref queries ask device-mcp for an exact match (ElementQuery.exact), so a label or value ref never falls back to a partial match. --testid relies on device-mcp matching identifiers exactly. Needs the device-mcp release with MetaMask/device-mcp#68. --- SKILL.md | 2 +- src/platform/mobile-platform-driver.test.ts | 100 ++++++++++++++++---- src/platform/mobile-platform-driver.ts | 3 +- 3 files changed, 84 insertions(+), 21 deletions(-) diff --git a/SKILL.md b/SKILL.md index 206a93f7..b9656950 100644 --- a/SKILL.md +++ b/SKILL.md @@ -261,7 +261,7 @@ On the browser, `path` lists the `role:name` of the dialogs and headings that co When 3+ consecutive identical nodes appear (same role, name, and path), they are collapsed into a summary like `… 3 more "maskicon" (refs e2–e4)` to reduce token waste. Individual refs still work for targeting. -On mobile (iOS, Android), the tree is a flat list in screen order. A node is a native element: `role` is its type (`Button`, `StaticText`, `android.widget.Button`, `android.view.ViewGroup`), and `bounds` is its frame (`{ x, y, width, height }`). Layout-only elements are left out: elements with no label or text, no testId, and a type that is not a control (button, link, text field, switch, slider, checkbox and similar). Their children stay in the list. Containers with a testId stay. Plain unlabelled views of at least 24×24 with nothing kept inside also stay: they are often icon buttons (for example the iOS wallet home action buttons). A node with a testId, label or text that no other node on the screen shares has a ref that every target command can use. Two kinds of ref only work with `mm click`, which taps the listed element at its `bounds`: refs of nodes without a testId, label or text (unlabelled views, spinners, unnamed controls), and refs marked `"ambiguous": true` (another node has the same testId, or the same label and type). The click result's `target` ends in ` (bounds)`. If the element is no longer at those bounds, the click fails with `MM_TARGET_NOT_FOUND` and taps nothing: run `mm describe-screen` again. `type`, `wait-for`, `get-text`, `scroll-to-element` and `long-press` refuse such a ref (`MM_TARGET_NOT_FOUND`, exit 4, nothing is sent to the device); the `hint` says what to use instead: a unique `--testid`, `mm click `, or `mm tap-coordinates ` at the center of `bounds`. +On mobile (iOS, Android), the tree is a flat list in screen order. A node is a native element: `role` is its type (`Button`, `StaticText`, `android.widget.Button`, `android.view.ViewGroup`), and `bounds` is its frame (`{ x, y, width, height }`). Layout-only elements are left out: elements with no label or text, no testId, and a type that is not a control (button, link, text field, switch, slider, checkbox and similar). Their children stay in the list. Containers with a testId stay. Plain unlabelled views of at least 24×24 with nothing kept inside also stay: they are often icon buttons (for example the iOS wallet home action buttons). A node with a testId, label or text that no other node on the screen shares has a ref that every target command can use. A `--testid` and these refs match only the whole, case-sensitive testId, label or text: when that element is not on the screen, the command fails and never acts on another element whose testId or label contains it. Two kinds of ref only work with `mm click`, which taps the listed element at its `bounds`: refs of nodes without a testId, label or text (unlabelled views, spinners, unnamed controls), and refs marked `"ambiguous": true` (another node has the same testId, or the same label and type). The click result's `target` ends in ` (bounds)`. If the element is no longer at those bounds, the click fails with `MM_TARGET_NOT_FOUND` and taps nothing: run `mm describe-screen` again. `type`, `wait-for`, `get-text`, `scroll-to-element` and `long-press` refuse such a ref (`MM_TARGET_NOT_FOUND`, exit 4, nothing is sent to the device); the `hint` says what to use instead: a unique `--testid`, `mm click `, or `mm tap-coordinates ` at the center of `bounds`. Use the `ref` value (`e3`) for click/type/get-text/wait-for commands. On the browser, each ref resolves to exactly one element, or the action fails instead of acting on another element. The snapshot stamps each element with a token in its `data-mm-ref` attribute, and the ref selects that token, so two "Send" buttons get two refs that each click their own button, and a price in the name can change without breaking the ref. A ref fails at once with `MM_TARGET_NOT_FOUND` when its element was removed or re-mounted, or when the element now shows other text (a "Next" button became "Confirm"); changing numbers such as `$2.42`, `3.92%` or `00:59` do not count, plain integers (`Account 1`) do. Take a new snapshot when that happens. An element that could not be stamped gets a `role=`/`text=` ref with the exact, case-sensitive name; if a click on it fails with a strict mode violation, target the element by `--testid` or `--selector` instead. diff --git a/src/platform/mobile-platform-driver.test.ts b/src/platform/mobile-platform-driver.test.ts index a4126b38..8b0eb654 100644 --- a/src/platform/mobile-platform-driver.test.ts +++ b/src/platform/mobile-platform-driver.test.ts @@ -1,4 +1,4 @@ -import { TargetObscuredError } from '@metamask/device-mcp'; +import { findElement, TargetObscuredError } from '@metamask/device-mcp'; import type { DeviceBackend, UIElement } from '@metamask/device-mcp'; import { describe, it, expect, vi, afterEach } from 'vitest'; @@ -664,11 +664,11 @@ describe('MobilePlatformDriver', () => { const result = await driver.click('a11yRef', 'e1', refMap, 5000); expect(backend.waitForElement).toHaveBeenCalledWith( - { identifier: 'submit-btn' }, + { identifier: 'submit-btn', exact: true }, 5000, ); expect(backend.tapElement).toHaveBeenCalledWith( - { identifier: 'submit-btn' }, + { identifier: 'submit-btn', exact: true }, { element: makeElement() }, ); expect(result).toStrictEqual({ @@ -721,7 +721,7 @@ describe('MobilePlatformDriver', () => { await driver.click('a11yRef', 'e1', refMap, 5000); expect(backend.tapElement).toHaveBeenCalledWith( - { identifier: 'bare-id' }, + { identifier: 'bare-id', exact: true }, { element: makeElement() }, ); }); @@ -734,7 +734,7 @@ describe('MobilePlatformDriver', () => { await driver.click('a11yRef', 'e1', refMap, 5000); expect(backend.tapElement).toHaveBeenCalledWith( - { identifier: 'custom:some-value' }, + { identifier: 'custom:some-value', exact: true }, { element: makeElement() }, ); }); @@ -758,7 +758,7 @@ describe('MobilePlatformDriver', () => { await driver.click('a11yRef', 'e1', refMap, 5000); expect(backend.tapElement).toHaveBeenCalledWith( - { label: 'OK', type: 'Button' }, + { label: 'OK', type: 'Button', exact: true }, { element: makeElement() }, ); }); @@ -773,13 +773,13 @@ describe('MobilePlatformDriver', () => { await driver.click('a11yRef', 'e1', refMap, 5000); expect(backend.tapElement).toHaveBeenCalledWith( - { label: 'OK', type: 'Button' }, + { label: 'OK', type: 'Button', exact: true }, { element: makeElement() }, ); await driver.click('a11yRef', 'e2', refMap, 5000); expect(backend.tapElement).toHaveBeenCalledWith( - { label: 'OK', type: 'StaticText' }, + { label: 'OK', type: 'StaticText', exact: true }, { element: makeElement() }, ); }); @@ -792,7 +792,7 @@ describe('MobilePlatformDriver', () => { await driver.click('a11yRef', 'e1', refMap, 5000); expect(backend.tapElement).toHaveBeenCalledWith( - { text: '0.5 ETH', type: 'StaticText' }, + { text: '0.5 ETH', type: 'StaticText', exact: true }, { element: makeElement() }, ); }); @@ -805,7 +805,7 @@ describe('MobilePlatformDriver', () => { await driver.click('a11yRef', 'e1', refMap, 5000); expect(backend.tapElement).toHaveBeenCalledWith( - { label: 'OK' }, + { label: 'OK', exact: true }, { element: makeElement() }, ); }); @@ -818,7 +818,7 @@ describe('MobilePlatformDriver', () => { await driver.click('a11yRef', 'e1', refMap, 5000); expect(backend.tapElement).toHaveBeenCalledWith( - { identifier: 'custom:something', type: 'Button' }, + { identifier: 'custom:something', type: 'Button', exact: true }, { element: makeElement() }, ); }); @@ -831,7 +831,7 @@ describe('MobilePlatformDriver', () => { await driver.click('a11yRef', 'e1', refMap, 5000); expect(backend.tapElement).toHaveBeenCalledWith( - { identifier: 'submit-btn' }, + { identifier: 'submit-btn', exact: true }, { element: makeElement() }, ); }); @@ -844,7 +844,7 @@ describe('MobilePlatformDriver', () => { await driver.click('a11yRef', 'e1', refMap, 5000); expect(backend.tapElement).toHaveBeenCalledWith( - { identifier: 'rawvalue' }, + { identifier: 'rawvalue', exact: true }, { element: makeElement() }, ); }); @@ -857,7 +857,7 @@ describe('MobilePlatformDriver', () => { await driver.click('a11yRef', 'e1', refMap, 5000); expect(backend.tapElement).toHaveBeenCalledWith( - { label: 'OK', type: 'Button' }, + { label: 'OK', type: 'Button', exact: true }, { element: makeElement() }, ); }); @@ -1038,11 +1038,71 @@ describe('MobilePlatformDriver', () => { await driver.waitForElement('a11yRef', 'e5', refMap, 10000); expect(backend.waitForElement).toHaveBeenCalledWith( - { label: 'Settings' }, + { label: 'Settings', exact: true }, 10000, ); }); + it('finds a test ID or ref only by its exact identity', async () => { + // device-mcp's own lookup, on wallet home without the breakdown + // container, as in a partial iOS accessibility tree. + const hierarchy = [ + makeElement({ + identifier: 'homepage-balance-breakdown-hero', + label: '$1.00, Today', + }), + makeElement({ type: 'StaticText', label: 'Send feedback' }), + makeElement({ type: 'StaticText', value: '$2.43' }), + ]; + const backend = createMockBackend({ + waitForElement: vi.fn(async (query) => { + const found = findElement(hierarchy, query); + if (!found) { + throw new Error( + `Timed out waiting for element: ${JSON.stringify(query)}`, + ); + } + return found; + }), + }); + const driver = new MobilePlatformDriver(backend); + const refMap = new Map([ + ['e1', 'label:Send|type:StaticText'], + ['e2', 'value:2.4|type:StaticText'], + ]); + + await expect( + driver.waitForElement( + 'testId', + 'homepage-balance-breakdown', + refMap, + 1000, + ), + ).rejects.toThrowError('Timed out waiting for element'); + await expect( + driver.waitForElement( + 'testId', + 'HOMEPAGE-BALANCE-BREAKDOWN-HERO', + refMap, + 1000, + ), + ).rejects.toThrowError('Timed out waiting for element'); + await expect( + driver.waitForElement('a11yRef', 'e1', refMap, 1000), + ).rejects.toThrowError('Timed out waiting for element'); + await expect( + driver.waitForElement('a11yRef', 'e2', refMap, 1000), + ).rejects.toThrowError('Timed out waiting for element'); + expect( + await driver.waitForElement( + 'testId', + 'homepage-balance-breakdown-hero', + refMap, + 1000, + ), + ).toBeUndefined(); + }); + it('throws when within scope is provided', async () => { const driver = new MobilePlatformDriver(createMockBackend()); @@ -1064,11 +1124,12 @@ describe('MobilePlatformDriver', () => { const result = await driver.getText('a11yRef', 'e3', refMap, 5000); expect(backend.waitForElement).toHaveBeenCalledWith( - { identifier: 'balance-label' }, + { identifier: 'balance-label', exact: true }, 5000, ); expect(backend.getElementText).toHaveBeenCalledWith({ identifier: 'balance-label', + exact: true, }); expect(result).toStrictEqual({ text: 'Hello World', @@ -1086,6 +1147,7 @@ describe('MobilePlatformDriver', () => { expect(backend.getElementText).toHaveBeenCalledWith({ text: '0.5 ETH', + exact: true, }); }); @@ -1397,7 +1459,7 @@ describe('MobilePlatformDriver', () => { expect([...refMap]).toStrictEqual([['e1', 'identifier:send']]); await driver.click('a11yRef', 'e1', refMap, 1000); expect(backend.tapElement).toHaveBeenCalledWith( - { identifier: 'send' }, + { identifier: 'send', exact: true }, { element: makeElement() }, ); await expect( @@ -2094,8 +2156,8 @@ describe('MobilePlatformDriver', () => { await driver.scrollToElement('a11yRef', 'e1', refMap, 'up', 5); expect(vi.mocked(backend.scrollToElement).mock.calls).toStrictEqual([ - [{ identifier: 'footer' }, 'up', 5], - [{ identifier: 'footer' }, 'down', 5], + [{ identifier: 'footer', exact: true }, 'up', 5], + [{ identifier: 'footer', exact: true }, 'down', 5], ]); }); diff --git a/src/platform/mobile-platform-driver.ts b/src/platform/mobile-platform-driver.ts index e3e53dfe..a4b25d58 100644 --- a/src/platform/mobile-platform-driver.ts +++ b/src/platform/mobile-platform-driver.ts @@ -1203,7 +1203,8 @@ function resolveTargetToQuery( if (boundsRef) { throw boundsRefError(targetValue, boundsRef); } - return parseStableIdentifier(resolution); + // The ref was unique under `matchesExactly`: no partial-match fallback. + return { ...parseStableIdentifier(resolution), exact: true }; } case 'testId': return { identifier: targetValue }; From b56aa8a851056c71956e9a13ee6d3c32a47a02fe Mon Sep 17 00:00:00 2001 From: cryptotavares Date: Fri, 4 Dec 2026 11:58:10 +0000 Subject: [PATCH 2/3] refactor: drop redundant comments --- src/platform/mobile-platform-driver.test.ts | 2 -- src/platform/mobile-platform-driver.ts | 1 - 2 files changed, 3 deletions(-) diff --git a/src/platform/mobile-platform-driver.test.ts b/src/platform/mobile-platform-driver.test.ts index 8b0eb654..8ba3fbb0 100644 --- a/src/platform/mobile-platform-driver.test.ts +++ b/src/platform/mobile-platform-driver.test.ts @@ -1044,8 +1044,6 @@ describe('MobilePlatformDriver', () => { }); it('finds a test ID or ref only by its exact identity', async () => { - // device-mcp's own lookup, on wallet home without the breakdown - // container, as in a partial iOS accessibility tree. const hierarchy = [ makeElement({ identifier: 'homepage-balance-breakdown-hero', diff --git a/src/platform/mobile-platform-driver.ts b/src/platform/mobile-platform-driver.ts index a4b25d58..ad4c97e1 100644 --- a/src/platform/mobile-platform-driver.ts +++ b/src/platform/mobile-platform-driver.ts @@ -1203,7 +1203,6 @@ function resolveTargetToQuery( if (boundsRef) { throw boundsRefError(targetValue, boundsRef); } - // The ref was unique under `matchesExactly`: no partial-match fallback. return { ...parseStableIdentifier(resolution), exact: true }; } case 'testId': From 1a7d203ea114c6f1bd55034f22536a5fb99a93c0 Mon Sep 17 00:00:00 2001 From: cryptotavares Date: Fri, 4 Dec 2026 17:09:44 +0000 Subject: [PATCH 3/3] chore: bump @metamask/device-mcp to ^0.11.0 --- README.md | 2 +- package.json | 2 +- yarn.lock | 10 +++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index de818f7c..8f396bc2 100644 --- a/README.md +++ b/README.md @@ -250,7 +250,7 @@ either, those tools fall back to `HERMES_METRO_PORT`, then `8081`. Core captures `sessionManager.getPlatformDriver?.()` before calling the consumer's `cleanup()`, then calls the driver's optional `dispose()` hook. Disposal runs even when consumer cleanup fails. This applies to `mm cleanup`, forced relaunch, failed launch, daemon shutdown, and server startup rollback. A failed disposal fails that cleanup call and is retained for a later attempt. The daemon owns that retention (one per `createServer` instance), so a later `mm cleanup`, forced relaunch, or daemon stop retries it, whatever wrapper of the session manager a tool receives. Only the current session's cleanup and disposal can fail a call. Drivers retained from earlier sessions are retried best-effort and in parallel. When they fail again, the daemon writes a `[WARN]` line to its log, and the current cleanup or launch continues. Tools reach this retention through `ToolContext.cleanupSession()`; without it (a context built outside the daemon), the tools fall back to retention per session-manager object. -`MobilePlatformDriver.dispose()` delegates to its backend's optional `dispose()` hook. Core depends on `@metamask/device-mcp` ^0.10.0, so the consumer must create its backend from the same ^0.10.0 range: the device-mcp fixes and the `dispose()` hook live in the consumer's copy of device-mcp. On iOS, the device-mcp `IdbBackend` uses a private IDB companion per backend, and disposal stops only that backend's companion and recording. The ADB backend has no disposal hook, so disposal does nothing on Android. +`MobilePlatformDriver.dispose()` delegates to its backend's optional `dispose()` hook. Core depends on `@metamask/device-mcp` ^0.11.0, so the consumer must create its backend from the same ^0.11.0 range: the device-mcp fixes and the `dispose()` hook live in the consumer's copy of device-mcp. On iOS, the device-mcp `IdbBackend` uses a private IDB companion per backend, and disposal stops only that backend's companion and recording. The ADB backend has no disposal hook, so disposal does nothing on Android. Consumers must expose a session-owned driver through `getPlatformDriver()` and create a fresh driver/backend for each launch. If launch fails before the driver is exposed, the consumer must dispose that locally held driver in its own failure path. Direct calls to a consumer's `cleanup()` also remain the consumer's responsibility. diff --git a/package.json b/package.json index 111ae557..1822546e 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,7 @@ "hono": "^4.12.25" }, "dependencies": { - "@metamask/device-mcp": "^0.10.0", + "@metamask/device-mcp": "^0.11.0", "cosmiconfig": "^9.0.0", "express": "^5.2.1", "zod": "^4.4.3" diff --git a/yarn.lock b/yarn.lock index 877f74fa..e20d5c71 100644 --- a/yarn.lock +++ b/yarn.lock @@ -926,7 +926,7 @@ __metadata: "@lavamoat/allow-scripts": "npm:^3.0.4" "@lavamoat/preinstall-always-fail": "npm:^2.0.0" "@metamask/auto-changelog": "npm:^5.3.0" - "@metamask/device-mcp": "npm:^0.10.0" + "@metamask/device-mcp": "npm:^0.11.0" "@metamask/eslint-config": "npm:^15.0.0" "@metamask/eslint-config-nodejs": "npm:^15.0.0" "@metamask/eslint-config-typescript": "npm:^15.0.0" @@ -968,15 +968,15 @@ __metadata: languageName: unknown linkType: soft -"@metamask/device-mcp@npm:^0.10.0": - version: 0.10.0 - resolution: "@metamask/device-mcp@npm:0.10.0" +"@metamask/device-mcp@npm:^0.11.0": + version: 0.11.0 + resolution: "@metamask/device-mcp@npm:0.11.0" dependencies: "@modelcontextprotocol/sdk": "npm:^1.32.0" zod: "npm:^4.6.6" bin: device-mcp: ./dist/cli/device-mcp.mjs - checksum: 10/6e028567cb1d63d0777be410cf6f31838d5a645bf1c53d738ef895b105bd47abf361350625d7a93dba374c8a27b62546838e571d2a890ad1eb27a749d0bc015e + checksum: 10/891d97afabef373b51f6745ad291d4b44842433cd30f22ecf1eafffa8f0245c2dcfd0520c333e3a46722a43301c60312c5007a9b206038f041292b6cba2cac8f languageName: node linkType: hard