Skip to content

The fastest browser for AI agents to run browser automation, built for sharing your logged-in browser state with your AI agents, like Codex or Claude Code, without disturbing you. Zero cost, zero config.

fix(ego-browser): trust native frame provenance for snapshot refs

JackWangf614ef1538db
diff --git a/package/ego-browser/src/snapshot-result.test.mjs b/package/ego-browser/src/snapshot-result.test.mjs
index 62b1bfe..2e6aaa9 100644
--- a/package/ego-browser/src/snapshot-result.test.mjs
+++ b/package/ego-browser/src/snapshot-result.test.mjs
@@ -4,7 +4,6 @@ import assert from "node:assert/strict";
import {
compactSnapshotContent,
compactSnapshotResult,
- enrichSnapshotRefFrames,
preparePageSnapshotResult,
sanitizeSnapshotLocators,
validateSnapshotLocator,
@@ -378,88 +377,34 @@ test("stable locator validation requires one match for the original backend node
}
});
-test("snapshot refs inherit their same-process and OOPIF frame ids", async () => {
- const refs = [
- { backendNodeId: 10, role: "button", name: "Top" },
- { backendNodeId: 20, role: "button", name: "Same process" },
- { backendNodeId: 30, role: "button", name: "OOPIF" },
- ];
- const calls = [];
- const services = {
- async cdp(method, params, sessionId) {
- calls.push([method, params, sessionId]);
- assert.equal(method, "Accessibility.getFullAXTree");
- if (params.frameId === "frame-same") {
- return {
- nodes: [
- {
- backendDOMNodeId: 20,
- role: { value: "button" },
- name: { value: "Same process" },
- },
- ],
- };
- }
- if (sessionId === "session:oopif") {
- return {
- nodes: [
- {
- backendDOMNodeId: 30,
- role: { value: "button" },
- name: { value: "OOPIF" },
- },
- ],
- };
- }
- return { nodes: [] };
- },
+test("Page snapshots preserve native ref provenance", async () => {
+ const result = {
+ refs: [
+ {
+ refId: 1,
+ backendNodeId: 6,
+ role: "textbox",
+ name: "",
+ },
+ {
+ refId: 17,
+ backendNodeId: 6,
+ role: "textbox",
+ name: "",
+ frameId: "frame-oopif",
+ },
+ ],
};
-
- await enrichSnapshotRefFrames(
- services,
- "session:page",
- new Map([
- ["frame-same", "session:page"],
- ["frame-oopif", "session:oopif"],
- ]),
- refs,
- );
-
- assert.equal(refs[0].frameId, undefined);
- assert.equal(refs[1].frameId, "frame-same");
- assert.equal(refs[2].frameId, "frame-oopif");
- assert.deepEqual(calls, [
- ["Accessibility.getFullAXTree", {}, "session:page"],
- ["Accessibility.getFullAXTree", { frameId: "frame-same" }, "session:page"],
- ["Accessibility.getFullAXTree", {}, "session:oopif"],
- ]);
-});
-
-test("snapshot refs stay top-level when a frame repeats their backend node id", async () => {
- const refs = [
- { backendNodeId: 21, role: "button", name: "Increment counter" },
- ];
+ const expected = structuredClone(result.refs);
const services = {
- async cdp(method, params, sessionId) {
- assert.equal(method, "Accessibility.getFullAXTree");
- if (sessionId === "session:page" && !params.frameId) {
- return {
- nodes: [
- {
- backendDOMNodeId: 21,
- role: { value: "button" },
- name: { value: "Increment counter" },
- },
- ],
- };
- }
+ async cdp(_method, _params, sessionId) {
if (sessionId === "session:oopif") {
return {
nodes: [
{
- backendDOMNodeId: 21,
- role: { value: "button" },
- name: { value: "Increment counter" },
+ backendDOMNodeId: 6,
+ role: { value: "textbox" },
+ name: { value: "" },
},
],
};
@@ -468,16 +413,12 @@ test("snapshot refs stay top-level when a frame repeats their backend node id",
},
};
- await enrichSnapshotRefFrames(
+ await preparePageSnapshotResult(
services,
"session:page",
new Map([["frame-oopif", "session:oopif"]]),
- refs,
+ result,
);
- assert.equal(
- refs[0].frameId,
- undefined,
- "a renderer-local id collision must not reroute a top-level ref",
- );
+ assert.deepEqual(result.refs, expected);
});
diff --git a/package/ego-browser/src/snapshot-result.ts b/package/ego-browser/src/snapshot-result.ts
index 9c66154..2a2ce91 100644
--- a/package/ego-browser/src/snapshot-result.ts
+++ b/package/ego-browser/src/snapshot-result.ts
@@ -162,105 +162,6 @@ function renderSnapshotTree(
return output;
}
-/**
- * Add frame provenance that older native snapshots omit.
- *
- * One AX-tree read per frame is enough to map every unique backend node in the
- * snapshot. Repeated backend ids cannot be disambiguated without native
- * `refId`/`frameId` support, so those refs deliberately remain unscoped.
- */
-export async function enrichSnapshotRefFrames(
- services: SnapshotServices,
- pageSessionId: string,
- iframeSessions: Map<string, string>,
- refs: SnapshotRef[] = [],
-): Promise<void> {
- if (!(iframeSessions instanceof Map) || iframeSessions.size === 0) return;
-
- const refsByBackendNode = new Map<number, SnapshotRef[]>();
- for (const ref of refs) {
- if (ref.frameId || !Number.isInteger(ref.backendNodeId)) continue;
- const matches = refsByBackendNode.get(ref.backendNodeId!) || [];
- matches.push(ref);
- refsByBackendNode.set(ref.backendNodeId!, matches);
- }
- if (refsByBackendNode.size === 0) return;
-
- const pageOwner = "";
- const ownersByRef = new Map(
- [...refsByBackendNode.values()]
- .flat()
- .map((ref) => [ref, new Set<string>()]),
- );
- const contexts = [
- { frameId: pageOwner, sessionId: pageSessionId, params: {} },
- ...[...iframeSessions].map(([frameId, frameSessionId]) => ({
- frameId,
- sessionId: frameSessionId,
- params: frameSessionId === pageSessionId ? { frameId } : {},
- })),
- ];
-
- for (const context of contexts) {
- let tree;
- try {
- tree = await services.cdp(
- "Accessibility.getFullAXTree",
- context.params,
- context.sessionId,
- );
- } catch {
- // Incomplete ownership data cannot safely disambiguate renderer-local ids.
- return;
- }
- for (const node of tree?.nodes || []) {
- const backendNodeId = node?.backendDOMNodeId;
- const matches = refsByBackendNode.get(backendNodeId);
- if (!matches || node?.ignored) continue;
- for (const ref of matches) {
- if (!snapshotRefMatchesAxNode(ref, node)) continue;
- ownersByRef.get(ref)?.add(context.frameId);
- }
- }
- }
-
- for (const [ref, owners] of ownersByRef) {
- if (owners.size !== 1) continue;
- const [frameId] = owners;
- if (frameId) ref.frameId = frameId;
- }
-}
-
-function snapshotRefMatchesAxNode(ref: SnapshotRef, node: any): boolean {
- if (
- typeof ref.role === "string" &&
- normalizeSnapshotRole(axValue(node?.role)) !==
- normalizeSnapshotRole(ref.role)
- ) {
- return false;
- }
- return typeof ref.name !== "string" || axValue(node?.name) === ref.name;
-}
-
-function normalizeSnapshotRole(value: unknown): string {
- const role = String(value || "").toLowerCase();
- return (
- {
- listboxoption: "option",
- textfield: "textbox",
- }[role] || role
- );
-}
-
-function axValue(value: any): string {
- const raw = value?.value ?? value;
- return typeof raw === "string" ||
- typeof raw === "number" ||
- typeof raw === "boolean"
- ? String(raw)
- : "";
-}
-
/** Return whether one advertised locator resolves uniquely to its source ref. */
export async function validateSnapshotLocator(
cdp: CdpAdapter,
@@ -349,19 +250,13 @@ function omitSnapshotLineLocator(
return text;
}
-/** Add frame provenance and validate stable locators for the Page API. */
+/** Preserve native frame provenance and validate stable locators for the Page API. */
export async function preparePageSnapshotResult(
services: SnapshotServices,
pageSessionId: string,
iframeSessions: Map<string, string>,
result: SnapshotResult,
): Promise<SnapshotResult> {
- await enrichSnapshotRefFrames(
- services,
- pageSessionId,
- iframeSessions,
- result?.refs || [],
- );
const adapter: CdpAdapter = {
sendRaw: (method, params = {}, sessionId) =>
services.cdp(method, params, sessionId),