Skip to content

feat: mm logs on mobile (Hermes console errors and uncaught exceptions) - #142

Open
cryptotavares wants to merge 9 commits into
mainfrom
cryptotavares/p2-29-mobile-logs
Open

cryptotavares wants to merge 9 commits into
mainfrom
cryptotavares/p2-29-mobile-logs

Conversation

@cryptotavares

@cryptotavares cryptotavares commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

Part of the mm CLI v2 effort: ticket P2-29 (Phase 2).

Summary: mm logs and mm events now work on iOS and Android. They capture Hermes console errors and uncaught JS exceptions into the same session buffer, cursor and output shape as the browser, adding only target.kind: "hermes" and optional fatal: true.

Problem

On mobile, mm logs, mm events and mm network return MM_TOOL_NOT_SUPPORTED_ON_PLATFORM. Agents fall back to ad-hoc console wrappers via mm cdp, which miss errors before install or after a JS reload and do not match the Extension output shape.

Hermes also never emits Runtime.exceptionThrown. Uncaught exceptions arrive only as the console.error React Native logs, indistinguishable from a caught console.error(err).

Before (mobile):
  mm events mark  -> MM_TOOL_NOT_SUPPORTED_ON_PLATFORM
  mm logs         -> MM_TOOL_NOT_SUPPORTED_ON_PLATFORM

Solution

HermesConsoleSource subscribes to the session's persistent HermesClient and writes into the shared EventBuffer.

  1. Session plumbing. SessionEvents takes a connection factory instead of a BrowserContext. getActiveSessionEvents selects the browser CdpManager or the mobile driver's HermesClient. ConsoleSource and the browser path stay unchanged. mm launch starts session events on mobile too and can wait up to SESSION_EVENTS_START_WAIT_MS for the inspector.
  2. Console capture. Runtime.consoleAPICalled goes through toConsoleRecord, level filtering and redaction. Pre-enable replays and #InstanceAgent synthetic messages are dropped. While a session is open, the app serializes every console.* call with previews, like an open DevTools.
  3. Uncaught errors. An observe-only ErrorUtils wrapper (globalThis.__mmLogsHookV1) reports { fatal, text } to a per-session binding, then invokes the previous handler with the same this and arguments and returns its result. The source joins the report and console echo into one record (newest-first, full text when matching an earlier console error, first line within 5 s for a trailing echo). The wrapper stays in the app after mm cleanup until the next JS reload, reporting to a binding nobody listens to (harmless).
  4. Reloads, reconnects and Runtime.disable. start keeps its listeners even if Metro is not ready and enables on contextCreated (connect/reconnect). A failed Runtime.enable is retried every 500 ms for up to 60 s and reports one capture error at the deadline. A new execution context reinstalls the wrapper. mm cdp Runtime.disable is refused with MM_CDP_BLOCKED on the session client; Runtime.enable and other methods pass through.
flowchart TD
    L["mm launch / mm logs / mm events"] --> G{"getActiveSessionEvents:<br/>mobile driver?"}
    G -->|browser| B["SessionEvents on CdpManager<br/>(ConsoleSource, NetworkSource)"]
    G -->|ios / android| H["SessionEvents on session HermesClient<br/>(HermesConsoleSource)"]
    H --> E["Runtime.addBinding + Runtime.enable<br/>(on start or contextCreated)"]
    E --> R["Pre-enable replay or #InstanceAgent"]
    R --> X["Dropped. Not recorded."]
    E --> K["Install ErrorUtils wrapper<br/>(__mmLogsHookV1)"]
    K --> C["consoleAPICalled -> console record<br/>target.kind: hermes"]
    K --> U["bindingCalled -> mark newest match<br/>uncaught, fatal"]
    C --> BUF[("Shared EventBuffer:<br/>seq, cursor, gap, summary")]
    U --> BUF
    B --> BUF
    RL["JS reload: executionContextCreated"] --> K
    RC["Socket lost: contextCreated reconnect"] --> E

    classDef new fill:#fff3cd,stroke:#d39e00
    classDef stop fill:#f8d7da,stroke:#c82333
    class G,H,E,K,C,U,RL,RC new
    class R,X stop
Loading

Yellow boxes are new. Red boxes stop a message.

What the agent sees

Live iOS run (console.error + uncaught throw with the same first line), then mm logs --cursor 45:

{"seq":46,"target":{"kind":"hermes","id":"9d2d6b8a…-1","url":""},"level":"error","text":"Error: mm-p2-29 dup\n    at caught (:1:1)","location":"http://localhost:8085/index.bundle//&platform=ios&…:13757:40"}
{"seq":47,"target":{"kind":"hermes","id":"9d2d6b8a…-1","url":""},"level":"error","text":"Error: mm-p2-29 dup\n    at anonymous (:1:100)","uncaught":true,"fatal":true}
"cursor":47,"hasMore":false,"gap":false,"summary":{…,"captureErrors":0}
Condition Result
mm logs / mm events on mobile Works. Returns target.kind: "hermes" records.
mm network on mobile MM_TOOL_NOT_SUPPORTED_ON_PLATFORM (P2-30).
console.error One record, redacted like the browser.
Uncaught error One record with uncaught: true (fatal: true when React Native passed isFatal). App error handling runs unchanged.
Other console levels Recorded only after mm logs configure --levels.
JS reload (DevSettings.reload()) Recording continues; wrapper reinstalls after the new bundle runs.
Socket lost ([CONNECTION_LOST]) captureErrors + 1; re-enables on contextCreated (reconnect).
mm cdp Runtime.disable on session client MM_CDP_BLOCKED. Runtime.enable and other methods pass through.

Not captured: native crashes and native logs (use mm device-logs), pre-connect Hermes replays, messages sent while disconnected, and release builds without Metro.

Decisions for reviewers

Decision Reason
No device-mcp change (trackContexts: false) The source already receives contextCreated (connect/reconnect) and sends its own Runtime.enable and Runtime.addBinding, avoiding a duplicate enable from trackContexts.
Keep Runtime.enable pass-through; block only Runtime.disable Hermes's RuntimeDomainAgent::enable() returns early when already enabled on the same socket, so a second enable does not replay console messages (verified live on iOS). Blocking only Runtime.disable avoids masking a failed start.
Optional fatal?: true on ConsoleRecord Requested by P2-29. Reflects React Native's isFatal flag; the app's own handler may still swallow the error (MetaMask does this for Ledger and Keystone errors).
Observe-only ErrorUtils wrapper (__mmLogsHookV1) ErrorUtils exists in every React Native build (unlike LogBox). Versioned global lets future cores install a new wrapper cleanly. Wrapper stays after mm cleanup until reload, calling an inert binding name.
No ConsoleRecorder extraction; retry inside the source Browser ConsoleSource stays untouched on main. HermesConsoleSource keeps its listeners on a failed connect, retries a failed Runtime.enable every 500 ms for up to 60 s, and enables again on contextCreated.
Release ConsoleObjectGroup every 200 console events Hermes retains logged remote objects until released.

Review guide

Read the files in this order:

# File What to check
1 src/events/hermes-console-source.ts hookScript (__mmLogsHookV1, observe-only), #enabling replay drop, contextCreated connect/reconnect guard, #takeRecent join
2 src/events/session-events.ts Connection factory, getActiveSessionEvents
3 src/platform/mobile-platform-driver.ts, types.ts getHermesEventClient, Runtime.disable block
4 src/tools/{logs,events,launch,registry}.ts, types/events.ts, cli/mm.ts Un-gating events/logs, mobile launch start, fatal field
5 *.test.ts Hook VM sandbox tests, Hermes source tests, driver/session/gating tests
6 SKILL.md, README.md "mm logs on mobile" section and logs table row
Testing
  • yarn build, yarn lint, attw and CI=1 yarn test pass (77 files, 3010 tests). Coverage: 98.12% statements, 95.40% branches, 97.36% functions, 98.21% lines (floors: 97 / 94 / 96 / 97).
  • hermes-console-source.test.ts (28 tests): VM sandbox tests for hookScript (not-ready, idempotent install, forwards this/args/return value, multi-binding dedupe, re-wraps app handler, survives throwing binding); source tests for redaction, levels, replay/#InstanceAgent drop, both join orders, same-head caught + uncaught errors and two console errors, reload reinstall + retry + timeout, failed enable retry and deadline, connect-after-failed-start, reconnect + failed re-enable, start failure, unsubscribe and object-group release.
  • session-events.test.ts, mobile-platform-driver.hermes.test.ts, logs.test.ts, launch.test.ts, platform-gating.test.ts, registry.test.ts: mobile routing, Runtime.disable block + Runtime.enable pass-through, platform gating.
  • Live iOS simulator (iPhone 17 Pro, read-only login screen, no unlock, Metro 8085, device-mcp 0.10.0):
Check Result
__mmLogsHookV1 installed after mm launch Pass
console.error('mm-p2-29 before') -> mm logs Pass (1 record, seq: 44)
mm cdp Runtime.enable sent live -> mm logs --cursor 44 Pass (result: {}, 0 replayed duplicates)
mm cdp Runtime.callFunctionOn after second Runtime.enable Pass (value: 42)
mm cdp Runtime.disable on session client Pass (MM_CDP_BLOCKED)
Uncaught Error('mm-p2-29 uncaught') Pass (1 record, uncaught: true, fatal: true)
Caught + uncaught with same first line (mm-p2-29 dup) Pass (caught unmarked; newest marked uncaught: true, fatal: true)
DevSettings.reload() -> wrapper reinstalled (+10 s), new console.error + uncaught captured Pass (captureErrors: 0)
Docs changed
  • SKILL.md: un-gated Session events, added concise "mm logs on mobile" section covering uncaught/fatal accuracy, reloads/reconnects, DevTools serialization overhead, Runtime.disable guard and "Not captured".
  • README.md: updated events and logs rows to reference mobile Hermes runtime (and SKILL.md).
  • src/cli/mm.ts: updated mm --help session events heading and logs description.
Follow-ups
  • P2-30 (mm network on mobile) adds a network source on the same SessionEvents<HermesEventClient> and removes network from BROWSER_ONLY_TOOLS.

@cryptotavares
cryptotavares requested a review from a team as a code owner December 3, 2026 13:34

This branch has not been deployed

No deployments
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.

1 participant