From 9230f698b34f34c683449fbe34f87fbbbfe34324 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sat, 21 Nov 2026 14:47:52 -0500 Subject: [PATCH] fix(web): retain Chromium navigation kind in session evidence (#13194) * fix(web): retain Chromium navigation kind in session evidence * test(web): use native reload event shape in the shared timeline fixture --- docs/guides/web/browser-observation.md | 3 +- src/browser/CdpObservationSession.ts | 4 +- src/browser/protocol/CdpEvents.test.ts | 92 +---------- src/browser/protocol/CdpEvents.ts | 10 +- .../webSessionLifecycleWindow.test.ts | 146 +++++++++--------- ...BrowserProvider.lifecycle_failures.test.ts | 4 + tests/fixtures/fakeCdpBrowserEvents.ts | 3 +- 7 files changed, 90 insertions(+), 172 deletions(-) diff --git a/docs/guides/web/browser-observation.md b/docs/guides/web/browser-observation.md index f756f8543e..54b389b814 100644 --- a/docs/guides/web/browser-observation.md +++ b/docs/guides/web/browser-observation.md @@ -430,7 +430,8 @@ document load failure that occurs inside the window. A buffered event without a comparable timestamp may remain as contextual evidence with `timestamp: null`, accompanied by a limitation stating that it does not establish activity in the selected window. Main-frame scope enforcement still applies to contextual -navigation events. +navigation events. Commit details include the browser-reported navigation kind, +such as `backforwardcacherestore`. A missing kind remains unknown. Network redirect events retain their paired prior-hop response context when the timestamped next-request event falls inside the window. Every redirect row, and only a redirect row, has a diff --git a/src/browser/CdpObservationSession.ts b/src/browser/CdpObservationSession.ts index 0154e1bb5f..f9b8fe8545 100644 --- a/src/browser/CdpObservationSession.ts +++ b/src/browser/CdpObservationSession.ts @@ -421,7 +421,7 @@ class TimelineCapture { } #navigationCommitted( - { facts, url: observation, transitionType }: CdpFrameEvent, + { facts, url: observation, navigationType }: CdpFrameEvent, contextOnly: boolean, ): void { const identity = selectCdpFrameIdentity(facts, this.#mainFrameId); @@ -451,7 +451,7 @@ class TimelineCapture { destinationScope: destination.scope === "unknown" ? null : destination.scope, requestId: null, - detail: safeDetail(transitionType), + detail: safeDetail(navigationType), }); if (observation.kind !== "observed") this.invalidateFinalUrl(); if (this.#stopIfOutOfScope(destination)) return; diff --git a/src/browser/protocol/CdpEvents.test.ts b/src/browser/protocol/CdpEvents.test.ts index b1ee7d6862..54e3a341bc 100644 --- a/src/browser/protocol/CdpEvents.test.ts +++ b/src/browser/protocol/CdpEvents.test.ts @@ -13,33 +13,6 @@ expectTypeOf().not.toHaveProperty("params"); expectTypeOf().not.toHaveProperty("method"); describe("registered CDP event payloads", () => { - it("keeps a committed frame's identity, fragment and transition type", () => { - const params = { - frame: { - id: "main", - loaderId: "loader", - url: "https://app.test/page", - urlFragment: "#section", - transitionType: "link", - }, - }; - const event = receiveCdpEvent({ method: "Page.frameNavigated", params }); - expect(cdpFrameEvent(event)).toEqual({ - facts: expect.objectContaining({ - kind: "navigated", - frameId: "main", - loaderId: "loader", - identityIssue: null, - }), - url: { - kind: "observed", - url: "https://app.test/page#section", - scopeUrl: "https://app.test/page", - }, - transitionType: "link", - }); - }); - it.each([ ["a missing payload", undefined, "missing_params"], ["a non-object payload", "frame", "invalid_params"], @@ -57,28 +30,13 @@ describe("registered CDP event payloads", () => { expect(frame?.facts.identityIssue).toBe(issue); }, ); - - it("records a malformed fragment as unreadable identity with its scope URL", () => { - const frame = cdpFrameEvent( - receiveCdpEvent({ - method: "Page.frameNavigated", - params: { - frame: { id: "main", url: "https://app.test/", urlFragment: 3 }, - }, - }), - ); - expect(frame?.url).toEqual({ - kind: "invalid_fragment", - scopeUrl: "https://app.test/", - }); - }); }); describe("registered Runtime and Debugger event payloads", () => { it("derives tolerant script facts and the strict runtime declaration from one parse", () => { const params = { scriptId: "script-1", - url: "https://app.test/app.js", + url: "https://app.test/café.js", executionContextId: 3, startLine: 0, startColumn: 0, @@ -95,7 +53,7 @@ describe("registered Runtime and Debugger event payloads", () => { expect(event.registered.payload.runtime.success).toBe(true); expect(event.registered.payload.declarationBytes).toEqual({ ok: true, - value: Buffer.byteLength(JSON.stringify(params)), + value: 136, // The compact fixture has 135 code units and one two-byte character. }); const partial = receiveCdpEvent({ @@ -198,23 +156,9 @@ describe("registered Network event payloads", () => { }); expect(event.registered.payload.runtime.success).toBe(false); }); - - it("reads a WebSocket frame's identity, opcode and payload without policy", () => { - const event = receiveCdpEvent({ - method: "Network.webSocketFrameReceived", - params: { - requestId: "socket-1", - response: { opcode: 99, payloadData: 7 }, - }, - }); - expect(event.registered).toEqual({ - method: "Network.webSocketFrameReceived", - payload: { requestId: "socket-1", opcode: 99, payloadData: undefined }, - }); - }); }); -describe("registered Target, lifecycle and WebMCP event payloads", () => { +describe("registered Target and WebMCP event payloads", () => { it.each([ [{ sessionId: "session-1" }, "session-1"], [{ sessionId: 7 }, undefined], @@ -248,25 +192,6 @@ describe("registered Target, lifecycle and WebMCP event payloads", () => { }); }); - it("keeps a lifecycle milestone's malformed timestamp unknown", () => { - expect( - receiveCdpEvent({ - method: "Page.lifecycleEvent", - params: { - frameId: "main", - loaderId: "l", - name: "load", - timestamp: "1", - }, - }).registered?.payload, - ).toEqual({ - frameId: "main", - loaderId: "l", - name: "load", - timestamp: undefined, - }); - }); - it.each([ ["a missing inventory", {}, undefined], ["a non-list inventory", { tools: {} }, undefined], @@ -281,15 +206,4 @@ describe("registered Target, lifecycle and WebMCP event payloads", () => { ?.payload, ).toEqual({ tools }); }); - - it("carries a requested navigation's reason", () => { - expect( - cdpFrameEvent( - receiveCdpEvent({ - method: "Page.frameRequestedNavigation", - params: { frameId: "main", reason: "reload", url: "https://a.test/" }, - }), - )?.reason, - ).toBe("reload"); - }); }); diff --git a/src/browser/protocol/CdpEvents.ts b/src/browser/protocol/CdpEvents.ts index 6f28751a2d..1eacff078f 100644 --- a/src/browser/protocol/CdpEvents.ts +++ b/src/browser/protocol/CdpEvents.ts @@ -66,8 +66,8 @@ export interface CdpFrameEvent { readonly url: CdpFrameUrlObservation; /** The reported reason of a requested navigation. */ readonly reason: string | undefined; - /** The committed frame's reported transition type. */ - readonly transitionType: string | undefined; + /** The navigation kind reported beside the committed frame. */ + readonly navigationType: string | undefined; } const frameEvent = (kind: CdpFrameEventKind) => @@ -83,10 +83,8 @@ const frameEvent = (kind: CdpFrameEventKind) => facts: cdpFrameEventPayloadFacts(kind, params), url: observeCdpFrameUrl(urlSource), reason: kind === "requested" ? cdpStringValue(record?.reason) : undefined, - transitionType: - kind === "navigated" - ? cdpStringValue(recordValue(record?.frame)?.transitionType) - : undefined, + navigationType: + kind === "navigated" ? cdpStringValue(record?.type) : undefined, }; }); diff --git a/tests/acceptance/webSessionLifecycleWindow.test.ts b/tests/acceptance/webSessionLifecycleWindow.test.ts index dbf294f229..9bdef022a9 100644 --- a/tests/acceptance/webSessionLifecycleWindow.test.ts +++ b/tests/acceptance/webSessionLifecycleWindow.test.ts @@ -7,69 +7,64 @@ import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js"; import { startFakeCdpBrowser } from "../fixtures/fakeCdpBrowser.js"; import { cliTest } from "../support/cli/cliFixture.js"; +const startLifecycleBrowser = () => + startFakeCdpBrowser({ + eventsBeforeReply: [ + "Performance.getMetrics", + "Page.setLifecycleEventsEnabled", + ], + commandResult: ({ method }) => { + if (method === "Performance.enable") return {}; + if (method === "Performance.getMetrics") + return { metrics: [{ name: "Timestamp", value: 20 }] }; + return undefined; + }, + commandEvents: ({ method, sessionId }, origin) => { + const milestone = (loaderId: string, timestamp: number | string) => ({ + method: "Page.lifecycleEvent", + sessionId, + params: { frameId: "frame-main", loaderId, name: "load", timestamp }, + }); + if (method === "Performance.getMetrics") + return [ + milestone("loader-before-window", 19), + { + method: "Page.navigatedWithinDocument", + sessionId, + params: { + frameId: "frame-main", + url: `${origin}#during-clock-response`, + navigationType: "historyApi", + }, + }, + ]; + if (method === "Page.setLifecycleEventsEnabled") + return [ + milestone("loader-before-window-replayed-after-arm", 19), + milestone("loader-current-window", 21), + milestone("loader-malformed-clock", "21"), + { + method: "Page.frameNavigated", + sessionId, + params: { + type: "BackForwardCacheRestore", + frame: { + id: "frame-main", + loaderId: "restored-loader", + url: `${origin}/app`, + urlFragment: "#restored", + }, + }, + }, + ]; + return undefined; + }, + }); + cliTest.for(["CLI", "stdio MCP"] as const)( - "filters replayed lifecycle milestones before the observation window through %s", + "retains in-window navigation and lifecycle evidence through %s", async (surface, { cli }) => { - const browser = await startFakeCdpBrowser({ - eventsBeforeReply: [ - "Performance.getMetrics", - "Page.setLifecycleEventsEnabled", - ], - commandResult: (command) => { - if (command.method === "Performance.enable") return {}; - if (command.method === "Performance.getMetrics") - return { metrics: [{ name: "Timestamp", value: 20 }] }; - return undefined; - }, - commandEvents: (command, origin) => { - if (command.method === "Performance.getMetrics") - return [ - { - method: "Page.lifecycleEvent", - sessionId: command.sessionId, - params: { - frameId: "frame-main", - loaderId: "loader-before-window", - name: "load", - timestamp: 19, - }, - }, - { - method: "Page.navigatedWithinDocument", - sessionId: command.sessionId, - params: { - frameId: "frame-main", - url: `${origin}#during-clock-response`, - navigationType: "historyApi", - }, - }, - ]; - if (command.method === "Page.setLifecycleEventsEnabled") - return [ - { - method: "Page.lifecycleEvent", - sessionId: command.sessionId, - params: { - frameId: "frame-main", - loaderId: "loader-before-window-replayed-after-arm", - name: "load", - timestamp: 19, - }, - }, - { - method: "Page.lifecycleEvent", - sessionId: command.sessionId, - params: { - frameId: "frame-main", - loaderId: "loader-current-window", - name: "load", - timestamp: 21, - }, - }, - ]; - return undefined; - }, - }); + const browser = await startLifecycleBrowser(); onTestFinished(() => browser.close()); let evidence: unknown; @@ -105,21 +100,14 @@ cliTest.for(["CLI", "stdio MCP"] as const)( const normalized = z .object({ normalized_result: webObservationSessionSchema }) .parse(sourceEvidence).normalized_result; - expect(normalized.timeline).toContainEqual( + expect( + normalized.timeline.filter(({ type }) => type === "lifecycle"), + ).toEqual([ expect.objectContaining({ - type: "lifecycle", loader_id: "loader-current-window", timestamp: 21, }), - ); - expect(normalized.timeline).not.toContainEqual( - expect.objectContaining({ loader_id: "loader-before-window" }), - ); - expect(normalized.timeline).not.toContainEqual( - expect.objectContaining({ - loader_id: "loader-before-window-replayed-after-arm", - }), - ); + ]); expect(normalized.timeline).toContainEqual( expect.objectContaining({ type: "same_document_navigation", @@ -127,6 +115,20 @@ cliTest.for(["CLI", "stdio MCP"] as const)( timestamp: null, }), ); + expect(normalized.completeness.excluded).toContainEqual({ + section: "timeline", + reason: "invalid_protocol_value", + count: 1, + }); + expect(normalized.timeline).toContainEqual( + expect.objectContaining({ + type: "navigation_committed", + frame_id: "frame-main", + loader_id: "restored-loader", + url: `${browser.allowedOrigin}/app#restored`, + detail: "backforwardcacherestore", + }), + ); expect(normalized.limitations).toContainEqual( expect.stringMatching( /Buffered events.*unknown timing.*context only.*do not establish.*observation window/iu, diff --git a/tests/boundary/browser/cdpBrowserProvider.lifecycle_failures.test.ts b/tests/boundary/browser/cdpBrowserProvider.lifecycle_failures.test.ts index fda673c5e8..d42d15ca9c 100644 --- a/tests/boundary/browser/cdpBrowserProvider.lifecycle_failures.test.ts +++ b/tests/boundary/browser/cdpBrowserProvider.lifecycle_failures.test.ts @@ -271,6 +271,10 @@ describe("CdpBrowserProvider: cancellation and session events", () => { ]); expect(result.value.timeline).toEqual( expect.arrayContaining([ + expect.objectContaining({ + type: "same_origin_reload", + detail: "navigation", + }), expect.objectContaining({ type: "same_document_navigation", url: `${browser.allowedOrigin}/reloaded?token=session-secret#spa-secret`, diff --git a/tests/fixtures/fakeCdpBrowserEvents.ts b/tests/fixtures/fakeCdpBrowserEvents.ts index 716f65808d..b4bad80f6a 100644 --- a/tests/fixtures/fakeCdpBrowserEvents.ts +++ b/tests/fixtures/fakeCdpBrowserEvents.ts @@ -154,13 +154,12 @@ const emitSessionTimeline = ( timestamp: 10, }); event(socket, "Page.frameNavigated", command.sessionId, { + type: "Navigation", frame: { id: "frame-main", loaderId: "loader-reload", url: allowed, - transitionType: "reload", }, - timestamp: 11, }); event(socket, "Page.navigatedWithinDocument", command.sessionId, { frameId: "frame-main",