Skip to content

refactor(browser): decide target_changed from one document identity owner - #10703

Merged
morluto merged 9 commits into
mainfrom
refactor/cdp-target-changed-owner
Nov 15, 2026
Merged

morluto merged 9 commits into
mainfrom
refactor/cdp-target-changed-owner

Conversation

@morluto

@morluto morluto commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

Summary

One owner decides whether the selected browser target changed document. CdpTargetSession.ts now holds CdpDocumentIdentity (root frame, loader, URL), compareCdpDocuments, cdpRootDocument and CdpDocumentTracker. Page capture, WebMCP discovery, screenshot capture and the runtime session use it instead of four separate rules. Stacked on #10666.

Problem and expected behavior

target_changed was decided in five places with different rules. Some treated a URL change with unknown loader IDs as a document change. WebMCP and screenshot capture returned protocol_error when a known loader was later missing. Page capture checked origin before identity, and its final check compared URLs only. The runtime session reported an out-of-scope frame with no loader ID as target_changed.

The owner's rule is that only a different root frame ID or a different known loader ID proves a change. A missing loader ID means the identity is unknown. A known, unchanged loader ID at a new URL means a same_document_navigation. Each consumer decides what each outcome means for its evidence.

Change and scope

Behaviour changes (decided by the coordinator; each one is a row in tests/boundary/browser/cdpTargetChanged.test.ts):

  1. Runtime (CdpRuntimeSession.assertDocument): when there is no loader ID, an out-of-scope or unsupported frame is now a scope refusal (target_not_allowed) for the current document instead of target_changed. A proven change is still checked first.
  2. Page capture and WebMCP: a URL or frame change with unknown loader IDs now completes with limited coverage. The frames or webmcp_tools section is marked attach_limited and a limitation is added; it is no longer target_changed. A URL change with a known, unchanged loader is a same-document navigation, so the result is accepted with full coverage.
  3. WebMCP: a known loader that is missing from the final reply now gives limited coverage instead of protocol_error. The first document stays the result's target. Screenshot capture had the same rule and now behaves the same way.
  4. frameNavigated during page capture: protocol_error remains for malformed root identity: a missing or invalid frame ID or parent, and now also a non-string loaderId. A well-formed root event without a loader ID gives limited coverage.
  5. Ordering: during page capture and screenshot capture, a proven document change is reported as target_changed before any scope decision, and it carries the failure it caused as cause. Scope checks apply to the current document. A loader-less (unproven) navigation to another origin is still target_not_allowed.
  6. Page capture final check: this check now compares root frame and loader identity, not only the URL. A loader change at the same URL is therefore target_changed; before, it was accepted. A document change observed during the observation window now fails at the end of that window instead of after the full capture.

Unchanged by design:

  • Screenshot capture still treats any visible URL move as target_changed, including a same-document one. The image would not match the reported URL.
  • The runtime session still treats an unproven document whose URL moved, or that gained a loader, as target_changed. Runtime evidence has no limited-coverage form.
  • The screenshot viewport check, the execution-window reason in CdpExecutionCapture, and Electron's generation counter remain separate checks. Electron is not touched because the D7 isolation work is in progress elsewhere.

Not included: WebMCP subframe tool retention (hasNewDocument) and the timeline main-frame tracking in CdpObservationSession keep their own rules. They do not produce target_changed.

Contract and boundary impact

No schema change. The visible effects are the result reasons and coverage listed above. The new limitation texts are "The initial/final document loader identity was unavailable, so document stability could not be verified. Retry after the page finishes loading."

Evidence and regression coverage

  • Commit 1 adds cdpTargetChanged.test.ts and two runtime rows. They characterize the current rule of each consumer and pass on the unchanged code: 56/56 with the runtime file.
  • Commit 2 changes only the rows for the decisions above and adds rows for duplicate events, unproven navigation to another origin, and same-document moves.
  • Existing tests whose expected results changed because of decisions 3 and 5 are updated with comments. Where a refusal-explanation test lost its case, a loader-less variant now covers that case. The capture-navigation fake now reports a loader ID, as Chrome does.

Validation performed

  • npm run test:focused -- <44 browser/MCP test files touching frame identity, frameNavigated, getFrameTree or target_changed>: 920 passed.
  • npm run check:fast: passed.
  • npx oxfmt on changed files.
  • Review follow-up (4d6e3e6: authorization retries and the pre-capture read compare the selected document before scope; screenshot command failures after a proven change report target_changed): the 3 new regressions in tests/boundary/browser/cdpTargetChanged.test.ts failed on the previous head and pass now; npm run test:focused over 12 touched browser test files: 298 passed; npm run check:fast: passed.
  • Not run: real Chrome or Electron. All coverage uses the fake CDP browser.

Compatibility

There are no API or schema changes. Callers that relied on protocol_error for a missing final loader, or on target_changed for loader-less URL moves, now get a completed result with attach_limited coverage.

Author readiness

Human owner:

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T05:48:48.112637Z e02d2f8 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8d22061e9c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/browser/CdpPageCapture.ts Resolved
@greptile-apps

greptile-apps Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the earlier findings are fixed and no new blocking issues were found.

Summary

Centralizes document identity checks across page capture, screenshots, WebMCP discovery, and runtime inspection.

  • Page capture checks one document identity through the full capture.
  • WebMCP discovery keeps the selected page when loader identity is missing.
  • Screenshot capture separates document changes from missing identity.
  • Runtime inspection uses the shared document identity rule.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Selected document] --> B[Read frame tree or receive navigation event]
  B --> C[Compare frame and loader]
  C --> D{Proven document change?}
  D -->|Yes| E[Report target_changed]
  D -->|No| F[Check current origin]
  F --> G{Origin allowed?}
  G -->|No| H[Refuse capture]
  G -->|Yes| I[Apply consumer coverage and URL rules]
Loading

Reviews (3) · Last reviewed commit: "fix(browser): compare the selected docum..." · Reviewed by Greptile

Comment thread src/browser/CdpPageCapture.ts Resolved
Comment thread src/browser/CdpPageCapture.ts Resolved
Comment thread src/browser/CdpScreenshot.ts Resolved
@morluto
morluto force-pushed the refactor/cdp-target-changed-owner branch from 4d6e3e6 to ff6751a Compare November 15, 2026 01:23
@morluto
morluto force-pushed the refactor/typed-cdp-final-commands branch from d614d04 to 37e5133 Compare November 15, 2026 01:45
@morluto
morluto force-pushed the refactor/cdp-target-changed-owner branch from ff6751a to 7e5d555 Compare November 15, 2026 01:45
@morluto
morluto force-pushed the refactor/typed-cdp-final-commands branch from 487d125 to c31aed9 Compare November 15, 2026 02:21
@morluto
morluto force-pushed the refactor/cdp-target-changed-owner branch 2 times, most recently from 00ae917 to d9ef2d2 Compare November 15, 2026 02:25
…h a malformed reply

A schema-rejected success reply means the browser applied the Emulation
override, so the attached environment owner records the reset obligation
before reporting the protocol error. Only a rejected or unanswered command
leaves no obligation.
The verifier scripts send Browser.close, Page.createIsolatedWorld, Page.navigate, Page.reload, Target.activateTarget, Target.closeTarget and Target.createTarget through CdpConnection, which now admits every reply through its command's result schema. Register those schemas, refuse an unregistered run-time name as a typed malformed reply instead of a TypeError, and check script send literals against the registry.
Final-capture retries and the pre-capture frame-tree read now observe each
reply through the selected document tracker before scope, and a screenshot
command failure after a proven document change reports target_changed with
the failure as its cause.
@morluto
morluto force-pushed the refactor/typed-cdp-final-commands branch from 9244f80 to 6dc4280 Compare November 15, 2026 05:46
@morluto
morluto force-pushed the refactor/cdp-target-changed-owner branch from d9ef2d2 to e02d2f8 Compare November 15, 2026 05:46
@morluto
morluto changed the base branch from refactor/typed-cdp-final-commands to main November 15, 2026 06:49
@morluto
morluto merged commit cc4f00d into main Nov 15, 2026
3 checks passed
@morluto
morluto deleted the refactor/cdp-target-changed-owner branch November 15, 2026 07:51
@morluto morluto added the area: runtime Process capture and runtime observation label Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: runtime Process capture and runtime observation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant