Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ type ISessionManager = {

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 requires `@metamask/device-mcp` 0.8.0 or later. 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. The consumer must create its backend from device-mcp 0.8.0 or later to get companion teardown.
`MobilePlatformDriver.dispose()` delegates to its backend's optional `dispose()` hook. Core depends on `@metamask/device-mcp` ^0.9.0, so the consumer must create its backend from the same ^0.9.0 range: the Android 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.

Expand Down Expand Up @@ -710,7 +710,7 @@ The `params-json` argument must be a valid JSON object. The `--timeout` flag set
| Prerequisite | An active browser session | An active mobile session with a DEBUG build and Metro running |
| Errors | `MM_CDP_BLOCKED` / `MM_CDP_FAILED`; `MM_INVALID_INPUT` for a mobile-only target | `MM_CDP_BLOCKED` / `MM_CDP_FAILED` (the underlying `HERMES_*` code is preserved in the message, with the Metro port used); `MM_INVALID_INPUT` for a browser-only target |

On mobile, `Runtime.evaluate` nests its value at `result.result.value`. `cdp` and `hermes-targets` use the Metro port the session was launched with (`mm launch --metro-port`), then the `HERMES_METRO_PORT` env var, then `8081`; `--metro-port` / `--app-id` on a call only override it for that call, and such a call opens a one-shot connection. The app id defaults to `HERMES_APP_ID`, else the platform default. When device-mcp exports `HermesClient` (needs a device-mcp release with MetaMask/device-mcp P2-02), the session keeps one Hermes connection for its Metro port and app id and reuses it across calls; otherwise each call opens its own connection.
On mobile, `Runtime.evaluate` nests its value at `result.result.value`. `cdp` and `hermes-targets` use the Metro port the session was launched with (`mm launch --metro-port`), then the `HERMES_METRO_PORT` env var, then `8081`; `--metro-port` / `--app-id` on a call only override it for that call, and such a call opens a one-shot connection. The app id defaults to `HERMES_APP_ID`, else the platform default. The session keeps one Hermes connection for its Metro port and app id and reuses it across calls.

```bash
# Mobile (Hermes) — the session keeps the launch Metro port
Expand Down
6 changes: 3 additions & 3 deletions SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ If the page closes after clicking (e.g., confirmation popup), the response inclu

**Browser hidden checkbox or radio:** toggles often keep the real `<input type="checkbox">` (and its test id) at 1×1 pixel under decorative elements, for example `mm click --testid basic-functionality-toggle`. When the target is such an input and it has one label (a wrapping `<label>` or `<label for="<id>">`), the label is clicked instead, which toggles the input, and the response includes `clickedLabel: true`. A hidden input without a label, or whose label has a link or button inside (for example "I agree to the terms"), is clicked as before.

**Mobile:** before tapping, iOS hit-tests the tap point. When another element is on top of the target (the keyboard, a sheet, an overlay), nothing is tapped and the click fails with `MM_TARGET_OBSCURED`; the message names the covering element. A target outside the screen fails with `MM_TARGET_NOT_VISIBLE` (run `mm scroll-to-element` first). `--dismiss-keyboard` closes the keyboard when it is the cover and taps once more (the result has `keyboardDismissed: true`). With an observation (not `--observe none`) the result has `screenChanged`: `false` means the screen looks the same as before the click; that is normal for some taps (the open tab) but a hint for a button that should navigate.
**Mobile:** before tapping, the device hit-tests the tap point (iOS; Android with device-mcp 0.9.0 and later). When another element is on top of the target (the keyboard, a sheet, an overlay), nothing is tapped and the click fails with `MM_TARGET_OBSCURED`; the message names the covering element. A target outside the screen fails with `MM_TARGET_NOT_VISIBLE` (run `mm scroll-to-element` first). `--dismiss-keyboard` closes the keyboard when it is the cover and taps once more (the result has `keyboardDismissed: true`). With an observation (not `--observe none`) the result has `screenChanged`: `false` means the screen looks the same as before the click; that is normal for some taps (the open tab) but a hint for a button that should navigate.

```
mm click --testid log-in-button --dismiss-keyboard
Expand Down Expand Up @@ -736,7 +736,7 @@ mm cdp Runtime.evaluate '{"expression":"document.querySelector(\'#personalSign\'

Browser commands share one persistent CDP session per target, so a domain enabled by one call (e.g. `Network.enable`) stays enabled for the next calls on the same target. Use `--fresh` for a command that must run on its own session, detached afterwards. After an Extension service-worker restart the stale session is dropped and the next call re-attaches.

On mobile, `cdp` and `hermes-targets` use the Metro port the session was launched with (`mm launch --metro-port`), then the `HERMES_METRO_PORT` env var, then 8081. A per-call `--metro-port` / `--app-id` overrides it for that call only. When device-mcp exports `HermesClient` (needs a device-mcp release with MetaMask/device-mcp P2-02) the session keeps one Hermes connection for its Metro port and app id and reuses it across calls; otherwise each call opens its own connection. A mobile failure names the Metro port it used.
On mobile, `cdp` and `hermes-targets` use the Metro port the session was launched with (`mm launch --metro-port`), then the `HERMES_METRO_PORT` env var, then 8081. A per-call `--metro-port` / `--app-id` overrides it for that call only. The session keeps one Hermes connection for its Metro port and app id and reuses it across calls. A mobile failure names the Metro port it used.

Blocked methods return `MM_CDP_BLOCKED` on either platform; other failures return `MM_CDP_FAILED` (on mobile the underlying `HERMES_*` code is preserved in the message). Each platform rejects the other platform's `--target` values with `MM_INVALID_INPUT`. The tool returns no observation by default — pass `--observe full` (or run `describe-screen`) to re-sync if the call changed runtime/page state.

Expand Down Expand Up @@ -771,7 +771,7 @@ Taps raw screen coordinates. Prefer element targeting when possible.

#### `mm dismiss-keyboard`

Dismisses the on-screen keyboard.
Dismisses the on-screen keyboard. On Android it then checks that the keyboard is gone, and fails with `MM_DEVICE_ACTION_FAILED` when it is still shown.

#### `mm dismiss-alert [--accept]`

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"hono": "^4.12.25"
},
"dependencies": {
"@metamask/device-mcp": "^0.8.0",
"@metamask/device-mcp": "^0.9.0",
"cosmiconfig": "^9.0.0",
"express": "^5.2.1",
"zod": "^4.4.3"
Expand Down
4 changes: 1 addition & 3 deletions src/platform/mobile-platform-driver.hermes-port.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
const mocks = vi.hoisted(() => ({
runHermesCdp: vi.fn(),
HermesClient: vi.fn(),
hasHermesClient: true,
}));

// Uses the real `HermesSession` of device-mcp, so the port precedence is
Expand All @@ -15,7 +14,7 @@ vi.mock('./device-mcp-runtime.js', async () => {
loadDeviceMcp: async () => ({
...actual,
runHermesCdp: mocks.runHermesCdp,
HermesClient: mocks.hasHermesClient ? mocks.HermesClient : undefined,
HermesClient: mocks.HermesClient,
}),
};
});
Expand Down Expand Up @@ -47,7 +46,6 @@ const clients: HermesClientStub[] = [];
describe('MobilePlatformDriver Hermes port precedence', () => {
beforeEach(() => {
clients.length = 0;
mocks.hasHermesClient = true;
mocks.runHermesCdp.mockResolvedValue({ ok: true, result: { value: 2 } });
mocks.HermesClient.mockImplementation(function (
this: unknown,
Expand Down
110 changes: 64 additions & 46 deletions src/platform/mobile-platform-driver.hermes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ const mocks = vi.hoisted(() => ({
setPinnedHermesDeviceId: vi.fn(),
HermesSession: vi.fn(),
HermesClient: vi.fn(),
// device-mcp 0.7.0 has no HermesClient; only the tests that exercise the
// persistent client enable it.
hasHermesClient: false,
}));

// The driver loads device-mcp on first use through this module.
Expand All @@ -26,7 +23,7 @@ vi.mock('./device-mcp-runtime.js', () => ({
LEGACY_SYNTHETIC_TITLE:
'React Native Experimental (Improved Chrome Reloads)',
HermesSession: mocks.HermesSession,
...(mocks.hasHermesClient ? { HermesClient: mocks.HermesClient } : {}),
HermesClient: mocks.HermesClient,
}),
}));

Expand All @@ -40,19 +37,23 @@ function createBackend(
return { platform, ...overrides } as unknown as DeviceBackend;
}

// The cdp calls in this describe pass a per-call metroPort that differs from
// the session target, so they take the one-shot path; the persistent client
// has its own describe.
describe('MobilePlatformDriver hermes delegation', () => {
beforeEach(() => {
mocks.hasHermesClient = false;
mocks.HermesSession.mockImplementation(() => ({
resolve: mocks.resolve,
getPinnedHermesDeviceId: mocks.getPinnedHermesDeviceId,
setPinnedHermesDeviceId: mocks.setPinnedHermesDeviceId,
}));
mocks.resolve.mockReturnValue({
metroPort: 8081,
appId: 'io.metamask',
pinnedDeviceId: undefined,
});
mocks.resolve.mockImplementation(
({ metroPort, appId }: { metroPort?: number; appId?: string }) => ({
metroPort: metroPort ?? 8081,
appId: appId ?? 'io.metamask',
pinnedDeviceId: mocks.getPinnedHermesDeviceId(),
}),
);
mocks.getPinnedHermesDeviceId.mockReturnValue(undefined);
});

Expand All @@ -67,9 +68,21 @@ describe('MobilePlatformDriver hermes delegation', () => {
// Created on first use (device-mcp is loaded lazily), once per driver.
expect(mocks.HermesSession).not.toHaveBeenCalled();

await iosDriver.cdp({ method: 'Runtime.evaluate', timeoutMs: 1000 });
await iosDriver.cdp({ method: 'Runtime.evaluate', timeoutMs: 1000 });
await androidDriver.cdp({ method: 'Runtime.evaluate', timeoutMs: 1000 });
await iosDriver.cdp({
method: 'Runtime.evaluate',
timeoutMs: 1000,
metroPort: 8087,
});
await iosDriver.cdp({
method: 'Runtime.evaluate',
timeoutMs: 1000,
metroPort: 8087,
});
await androidDriver.cdp({
method: 'Runtime.evaluate',
timeoutMs: 1000,
metroPort: 8087,
});

expect(mocks.HermesSession).toHaveBeenCalledTimes(2);
expect(mocks.HermesSession).toHaveBeenNthCalledWith(1, {
Expand All @@ -85,8 +98,16 @@ describe('MobilePlatformDriver hermes delegation', () => {
const driver = new MobilePlatformDriver(createBackend('ios'));

await Promise.all([
driver.cdp({ method: 'Runtime.evaluate', timeoutMs: 1000 }),
driver.cdp({ method: 'Runtime.evaluate', timeoutMs: 1000 }),
driver.cdp({
method: 'Runtime.evaluate',
timeoutMs: 1000,
metroPort: 8087,
}),
driver.cdp({
method: 'Runtime.evaluate',
timeoutMs: 1000,
metroPort: 8087,
}),
]);

expect(mocks.HermesSession).toHaveBeenCalledTimes(1);
Expand All @@ -106,6 +127,7 @@ describe('MobilePlatformDriver hermes delegation', () => {
method: 'Runtime.evaluate',
params: { expression: '1+1' },
timeoutMs: 30_000,
metroPort: 8087,
});

expect(mocks.resolve).toHaveBeenCalledWith({
Expand All @@ -118,7 +140,7 @@ describe('MobilePlatformDriver hermes delegation', () => {
method: 'Runtime.evaluate',
params: { expression: '1+1' },
timeoutMs: 30_000,
metroPort: 8081,
metroPort: 8087,
appId: 'io.metamask',
pinnedDeviceId: undefined,
}),
Expand All @@ -133,7 +155,11 @@ describe('MobilePlatformDriver hermes delegation', () => {
});
const driver = new MobilePlatformDriver(createBackend());

await driver.cdp({ method: 'Runtime.evaluate', timeoutMs: 30_000 });
await driver.cdp({
method: 'Runtime.evaluate',
timeoutMs: 30_000,
metroPort: 8087,
});

expect(mocks.setPinnedHermesDeviceId).toHaveBeenCalledWith('device-42');
});
Expand All @@ -146,7 +172,11 @@ describe('MobilePlatformDriver hermes delegation', () => {
});
const driver = new MobilePlatformDriver(createBackend());

await driver.cdp({ method: 'Runtime.evaluate', timeoutMs: 30_000 });
await driver.cdp({
method: 'Runtime.evaluate',
timeoutMs: 30_000,
metroPort: 8087,
});

expect(mocks.setPinnedHermesDeviceId).not.toHaveBeenCalled();
});
Expand All @@ -163,6 +193,7 @@ describe('MobilePlatformDriver hermes delegation', () => {
const outcome = await driver.cdp({
method: 'Runtime.terminateExecution',
timeoutMs: 30_000,
metroPort: 8087,
});

expect(outcome.ok).toBe(false);
Expand All @@ -183,6 +214,7 @@ describe('MobilePlatformDriver hermes delegation', () => {
const outcome = await driver.cdp({
method: 'Runtime.evaluate',
timeoutMs: 30_000,
metroPort: 8087,
});

expect(outcome.ok).toBe(false);
Expand Down Expand Up @@ -411,7 +443,6 @@ describe('MobilePlatformDriver hermes delegation', () => {

describe('MobilePlatformDriver webview cdp routing', () => {
beforeEach(() => {
mocks.hasHermesClient = false;
mocks.HermesSession.mockImplementation(() => ({
resolve: mocks.resolve,
getPinnedHermesDeviceId: mocks.getPinnedHermesDeviceId,
Expand Down Expand Up @@ -450,19 +481,27 @@ describe('MobilePlatformDriver webview cdp routing', () => {
});

it('defaults to the Hermes path when target is omitted', async () => {
mocks.resolve.mockReturnValue({
metroPort: 8081,
appId: 'io.metamask',
pinnedDeviceId: undefined,
});
mocks.resolve.mockImplementation(
({ metroPort, appId }: { metroPort?: number; appId?: string }) => ({
metroPort: metroPort ?? 8081,
appId: appId ?? 'io.metamask',
pinnedDeviceId: undefined,
}),
);
mocks.getPinnedHermesDeviceId.mockReturnValue(undefined);
mocks.runHermesCdp.mockResolvedValue({ ok: true, result: { value: 2 } });
const webviewCdp = vi.fn();
const driver = new MobilePlatformDriver(
createBackend('android', { webviewCdp }),
);

await driver.cdp({ method: 'Runtime.evaluate', timeoutMs: 30_000 });
// A per-call port differs from the session target, so this takes the
// one-shot path; the omitted target still routes to Hermes.
await driver.cdp({
method: 'Runtime.evaluate',
timeoutMs: 30_000,
metroPort: 8087,
});

expect(mocks.runHermesCdp).toHaveBeenCalledTimes(1);
expect(webviewCdp).not.toHaveBeenCalled();
Expand Down Expand Up @@ -578,7 +617,6 @@ describe('MobilePlatformDriver persistent Hermes client', () => {

beforeEach(() => {
clients.length = 0;
mocks.hasHermesClient = true;
mocks.HermesSession.mockImplementation(() => ({
resolve: mocks.resolve,
getPinnedHermesDeviceId: mocks.getPinnedHermesDeviceId,
Expand Down Expand Up @@ -610,7 +648,6 @@ describe('MobilePlatformDriver persistent Hermes client', () => {

afterEach(() => {
vi.clearAllMocks();
mocks.hasHermesClient = false;
});

it('creates one client for the session port and reuses it across calls', async () => {
Expand Down Expand Up @@ -823,25 +860,6 @@ describe('MobilePlatformDriver persistent Hermes client', () => {
}
});

it('falls back to the one-shot path when device-mcp has no HermesClient', async () => {
mocks.hasHermesClient = false;
mocks.runHermesCdp.mockResolvedValue({ ok: true, result: { value: 2 } });
const driver = new MobilePlatformDriver(createBackend(), 'io.metamask', {
metroPort: 8085,
});

const outcome = await driver.cdp({
method: 'Runtime.evaluate',
timeoutMs: 30_000,
});

expect(outcome).toStrictEqual({ ok: true, result: { value: 2 } });
expect(mocks.HermesClient).not.toHaveBeenCalled();
expect(mocks.runHermesCdp).toHaveBeenCalledWith(
expect.objectContaining({ metroPort: 8085 }),
);
});

it('reports the session port, pin, connection and device in hermes-targets', async () => {
const target = {
id: 'page-1',
Expand Down
Loading
Loading