Skip to content

feat: persistent CDP sessions per target and mm cdp --target sw - #90

Merged
cryptotavares merged 12 commits into
cryptotavares/mmcli-p1-20-timeoutsfrom
cryptotavares/mmcli-p2-01-browser-cdp
Nov 18, 2026
Merged

cryptotavares merged 12 commits into
cryptotavares/mmcli-p1-20-timeoutsfrom
cryptotavares/mmcli-p2-01-browser-cdp

Conversation

@cryptotavares

@cryptotavares cryptotavares commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Description

withCdpSession (src/tools/utils/cdp.ts) opens and detaches a CDP session for every call, so domains and listeners enabled with mm cdp (Runtime.addBinding, Network.enable) die with it. The Extension background service worker, where the wallet state lives, cannot be reached at all.

  • New CdpManager (src/platform/cdp-manager.ts), one per BrowserContext: it opens one CDP session per target the first time it is needed and reuses it. Concurrent callers share one open; a dead session is opened again on the next call; a closed page drops its session; dispose() on cleanup detaches everything.
  • mm cdp --target sw reaches the Extension service worker. Playwright cannot open a CDP session on a Worker, so the manager attaches from the browser CDP session (Target.attachToTarget, not flattened) and keys the session on the CDP target id. A restarted worker (new target id) gets a new session.
  • A stopped MV3 worker (idle) fails after 2 s with a clear message ("The Extension service worker is not running ... Interact with the Extension ... then retry") instead of hanging until the timeout. A worker that dies mid-call rejects the pending command at once.
  • A command that may have run is never resent. Only a failure to open the session is retried, once.
  • --target is page|sw|hermes|android-webview. The default is the active page on the browser and hermes on mobile; each platform rejects the other's targets with MM_INVALID_INPUT. New mm cdp --fresh keeps today's one-shot session.
  • Clipboard uses the persistent page session. The trimmed a11y snapshot stays on withCdpSession, because it enables and disables Accessibility.

Part of the mm CLI v2 effort: ticket P2-01 (Tier 2).
Stacked on #89 (review that first).

Breaking changes

None. The default target and the result shape are unchanged.

Consumer impact

None.

Proof

Yalc build of this branch in the Extension (headless e2e build), proof/p2-01/ext.sh:

Check Result
AC1: Runtime.addBinding on the page, then a later call sees it (also after a --fresh call) PASS: typeof binding = function
AC2: mm cdp Runtime.evaluate 'typeof globalThis.stateHooks' --target sw PASS: object; the default target is still the Extension page
AC3: stop the worker (ServiceWorker.stopAllWorkers) → --target sw call PASS: exit 1 after 2002 ms with the clear message (before the fix: hung until the timeout)
AC3: wake the worker from the page, then --target sw PASS: re-attaches to the new worker (a global set before the stop is gone, stateHooks is there)
Extension smoke all steps exit 0
Unit tests
Test Files  58 passed (58)
     Tests  2153 passed (2153)    (with the updated #89 merged)
All files  |  97.92 | 95.51 | 97.58 | 98.02 |

cdp-manager.ts 100% statements, branches, functions and lines. CI=1 thresholds not lowered; tsc -p tsconfig.build.json, yarn lint and prettier clean.

…mmcli-p2-01-browser-cdp

# Conflicts:
#	README.md
…ts' into cryptotavares/mmcli-p2-01-browser-cdp

# Conflicts:
#	README.md
#	SKILL.md
#	src/platform/playwright-driver.ts
The changelog is generated when a release is created.
…ts' into cryptotavares/mmcli-p2-01-browser-cdp
@cryptotavares
cryptotavares marked this pull request as ready for review November 16, 2026 18:15
@cryptotavares
cryptotavares requested a review from a team as a code owner November 16, 2026 18:15
seaona
seaona previously approved these changes Nov 18, 2026

@seaona seaona left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Changes look good, just found one caveat: any command that changes how the page behaves now stays in effect until cleanup. Ie:

  • Fetch.enable pauses every request with nobody to continue it.
  • Debugger.pause freezes all Playwright interaction.
  • Emulation.* overrides and Network.setBlockedURLs stay applied.

Before this PR, detaching the session undid all of these. There's no reset path short of mm cleanup or knowing the matching disable call

Suggestion (can be follow-up): add some guard rail, ie, a line in SKILL telling agents to undo what they change (or use --fresh)

…ts' into cryptotavares/mmcli-p2-01-browser-cdp

# Conflicts:
#	README.md
#	src/cli/mm.ts
#	src/tools/utils/discovery.ts
@cryptotavares
cryptotavares merged commit cd1a49f into main Nov 18, 2026
27 checks passed
@cryptotavares
cryptotavares deleted the cryptotavares/mmcli-p2-01-browser-cdp branch November 18, 2026 14:49
cryptotavares added a commit that referenced this pull request Nov 18, 2026
…nection (#91)

## Description

`mm launch --platform ios|android --metro-port 8085` stored the port,
but `mm cdp` and `mm hermes-targets` resolved it again for each call and
fell back to `HERMES_METRO_PORT` or 8081 (baseline B5). On a machine
with a second Metro on 8081 they reached the wrong app; without one they
failed. Every Hermes call also opened its own WebSocket.

- `MobilePlatformDriver` options gain `metroPort`, `deviceId` and
`appId`. New optional `IPlatformDriver.setHermesDefaults()`: the launch
tool applies `--metro-port` / `--device-id` to the driver after a
successful mobile launch, so B5 is fixed with no consumer change.
- Precedence for each call: per-call `--metro-port` / `--app-id`, then
the driver (launch) value, then `HERMES_METRO_PORT` / `HERMES_APP_ID`,
then 8081 / the platform default. CDP failures name the port they tried.
- When the loaded device-mcp exports `HermesClient`
(MetaMask/device-mcp#54), `cdp` keeps one persistent client per session
target. A per-call port or app id that differs uses a one-shot call, so
the persistent client is never retargeted. The client is closed on
`dispose()`. With device-mcp 0.7.0 (no `HermesClient`), core keeps the
one-shot `runHermesCdp` path (feature-detected).
- `hermes-targets` also reports `sessionMetroPort`, `pinnedDeviceId`,
`connected` and `deviceId` (additive).

Part of the mm CLI v2 effort: ticket `P2-03` (Tier 2).
Stacked on #90 (review that first).
Depends on MetaMask/device-mcp#54: the persistent connection needs a
device-mcp release with `HermesClient`, then a dependency bump here. The
B5 fix works with the released 0.7.0.

## Breaking changes

None. New options and output fields only.

## Consumer impact

| Consumer | Change needed |
|---|---|
| Mobile `tests/llm-workflow/` | Optional: pass `{ metroPort, deviceId,
appId }` when constructing `MobilePlatformDriver` (patch ready). Core
already applies the launch values itself. |

## Proof

Yalc build in Mobile (dev client 8.19.0, iOS sim + Android emulator,
Metro 8085, launched with `--metro-port 8085`), `mm cdp Runtime.evaluate
1+1` without `--metro-port`, 10 runs:

| Check | Before (#79 stack) | After |
|---|---|---|
| Android, 10 calls | 0/10: `hermes-targets` says `metroPort: 8081,
metroDown: true` | **10/10**, `sessionMetroPort: 8085`, p50 15–40 ms |
| iOS, 10 calls | — | **10/10**, p50 2 ms |
| `hermes-targets` after a call | — | `connected: true`,
`pinnedDeviceId` set, `deviceId` = launch device |

The before build reached Metro only with an explicit `--metro-port 8085`
(90 ms per call on Android). The raw client bench is in
MetaMask/device-mcp#54 (Android p50 165 → 5 ms).

<details>
<summary>Unit tests</summary>

```
Test Files  58 passed (58)
     Tests  2178 passed (2178)    (with #89 and #90 merged)
All files  |  97.94 | 95.5 | 97.59 | 98.04 |
```

Covers the precedence table, launch applies the port, one client for two
calls, one-shot on a per-call override, fallback without `HermesClient`,
close on dispose, the pin persisting, and the new `hermes-targets`
fields. `CI=1` thresholds not lowered; `tsc -p tsconfig.build.json` and
`yarn lint` clean.

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