Repository navigation
feat: deliver CDP events from pages and the service worker - #107
Open
cryptotavares wants to merge 1 commit into
Open
cryptotavares wants to merge 1 commit into
cryptotavares wants to merge 1 commit into
Conversation
Base automatically changed from
cryptotavares/mmcli-p2-01-browser-cdp
to
main
November 18, 2026 14:49
cryptotavares
force-pushed
the
cryptotavares/mmcli-p2-28-cdp-target-events
branch
3 times, most recently
from
November 18, 2026 17:56
c0dfe35 to
7d46869
Compare
Add `CdpManager.subscribe({ events, enable?, onEvent, onError? })`. It
attaches to every open page and the running Extension service worker, then
to each new page and each new or restarted worker. On each target it
enables the event domains once, shares the persistent session with `send`
and other subscribers, and delivers `{ target, method, params, t }`.
The service worker session now emits tunneled CDP events instead of
dropping every message without an `id`. New workers are found with
`Target.targetCreated` on the browser session (target discovery is
already on); new pages with the context `page` event. Unsubscribe removes
the listeners and disables domains no one else needs; `dispose()` ends
every subscription.
cryptotavares
force-pushed
the
cryptotavares/mmcli-p2-28-cdp-target-events
branch
from
November 18, 2026 18:39
7d46869 to
73122e8
Compare
cryptotavares
marked this pull request as ready for review
November 18, 2026 18:58
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Important
Ready to review on
main. The Tier 2 stack (#88–#91) is merged; this PR is rebased ontomainand contains one commit. #109 (P2-15) → #110 (P2-16) → #111 (P2-17) stack on it.CdpManager(#90) keeps one persistent CDP session per target, but it cannot be used to listen to a target:id. Those messages are the CDP events (src/platform/cdp-manager.ts,ServiceWorkerCdpSession.#onMessage).This PR adds an internal event API. Upcoming
mm logs/mm network(tickets P2-16 / P2-17) will build on it. This PR adds no CLI command and no new export from the package root.ServiceWorkerCdpSessionnow emits tunneled CDP events by name (on/off), like a PlaywrightCDPSessiondoes.CdpManager.subscribe({ events, enable?, onEvent, onError? }):<Domain>.enableonce per target and delivers{ target: { kind, targetId, url }, method, params, t }.sendshare one session per target. Each event name has one CDP listener, and each domain has one reference count. On unsubscribe, the manager removes listeners and disables domains that no other subscriber needs.dispose()ends every subscription.onErroronce and the page is no longer re-attached (a new subscription or page clears the budget). If Playwright reportsclosefor a worker whose CDP target is still live, the manager attaches to the worker again.onError, and a closed session stays quiet too. When the target is still alive, the dead session is dropped so the subscribers are re-attached at once on a fresh session; a gone target is left to the target lifecycle. The manager also re-attaches when the target comes back.sendandgetSessionprefer the worker target that subscribers are already attached to, so a restarted worker does not move a latersendback to the old target.send,--freshand the "never resend a command" rule do not change, except that a<Domain>.enable/.disableis refused withMM_CDP_BLOCKEDwhile a subscription holds that domain on the target: the subscription enabled it and would disable it on its last unsubscribe.--freshsessions are not refused. README/SKILL document it.How new targets are found
context.on('page')+context.newCDPSession(page)Target.targetCreatedon the browser session (target discovery is already on from #90), plus Playwrightserviceworkeras a backup. ThenTarget.attachToTarget(flatten: false), as #90 does.Worker.Target.setAutoAttachwas not used. At the browser level, Chrome supports onlyflatten: true("Only flatten protocol is supported with browser level auto-attach"). Flat sessions cannot be driven throughTarget.sendMessageToTarget("When using flat protocol, messages are routed to the target via the sessionId attribute"). Playwright'sCDPSessiondoes not expose a way to send on a childsessionId. Auto-attach would also attach to every target, including Playwright's own pages. Both probes are in the proof (probes/auto-attach-spike.txt).Breaking changes
None. The change is additive and internal (
CdpManageris not exported from the package root).Consumer impact
test/e2e/playwright/llm-workflow/tests/llm-workflow/Proof
Ticket
P2-28(Phase 2, outside the current stack). The proof drivesCdpManager.subscribefrom the builtdist/in a script, because this PR adds no CLI command. After the review fixes (head73122e8) the happy path is unchanged, so the live results below still apply; the behaviour fixes are unit tested (below).Network.requestWillBeSent/responseReceived/loadingFinished/loadingFailed,Runtime.consoleAPICalled/exceptionThrownfrom the page and from the service workersubscribe(eager attach)subscribe(auto attach)ServiceWorker.stopAllWorkers), then wake from a new Extension pageconsole.log+fetchsendonswwhile subscribed;--freshok/2; while the worker is stopped,sendstill fails fast after 2 s with #90's stopped-worker errorenableper domain; the second subscriber (Runtime only) receives exactly the Runtime events of the firsttunsubscribedist/chrome, no wallet created, first screen only)home.htmland the worker, before and after a worker stop and wake;--target swprobetypeof stateHooks=object, and itsconsole.warnarrives as answeventWorker events after stop and wake (MV3 test extension)
Findings for the consumers of this API (P2-15 / P2-16 / P2-17)
ServiceWorker.stopAllWorkers(orServiceWorker.stopWorker) and a wake, Chrome reused the same target ID, and the same CDP session kept working, with its domains still enabled. A restart is marked byInspector.targetCrashed→Runtime.executionContextsCleared→Runtime.executionContextCreatedon that session (probes/restart-signal.txt). If a worker comes back as a new target, the manager attaches to it and enables its domains again (unit tested).Target.targetDestroyed) with or without a subscription. An attached subscriber therefore does not keep the worker alive (probes/idle-*.txt).chrome.runtime.reload()closes Extension pages, andchrome-extension://URLs are then blocked (ERR_BLOCKED_BY_CLIENT). It is not a usable restart in tests (the same result as feat: persistent CDP sessions per target and mm cdp --target sw #90).console.logandfetch. Network traffic sent beforesubscribeis not delivered.Runtime.enablereplays console messages that Chrome kept, so a late subscriber can get older console events (probes/late-subscribe.txt).home.html+ worker, about 8 s per phase) gave about 330 events per phase for these 6 event names. Console events were the largest part.tisDate.now()in the daemon when the event is delivered (not the CDPtimestamp/wallTime, which are inparams). Events from different targets interleave by arrival time.requestIdis unique per target only. Use(target.kind, target.targetId, requestId)as the key.Unit tests
New tests in
src/platform/cdp-manager.test.ts,CdpManager subscribe:closeof a worker whose target is still liveenableper domain for several subscriberssend/--freshwhile subscribed (a command is never resent)unsubscribeanddisposeRuntime.enableis pending → no errorsendwhen the target list holds the old and the new one<Domain>.enable/.disablethroughsendrefused while subscribed (MM_CDP_BLOCKED), allowed with--freshenableor target-info call fails on a closed session is dropped and re-attached on a fresh session; a subscription whose first attach failed is retried on a later attach trigger; a page whose sessions keep closing is reported once and no longer re-attached (120 tests incdp-manager.test.ts)