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): avoid cross-frame ref collisions

JackWangbb93c3900a3b
diff --git a/package/ego-browser/src/snapshot-result.test.mjs b/package/ego-browser/src/snapshot-result.test.mjs
index b6baf8e..62b1bfe 100644
--- a/package/ego-browser/src/snapshot-result.test.mjs
+++ b/package/ego-browser/src/snapshot-result.test.mjs
@@ -390,10 +390,26 @@ test("snapshot refs inherit their same-process and OOPIF frame ids", async () =>
calls.push([method, params, sessionId]);
assert.equal(method, "Accessibility.getFullAXTree");
if (params.frameId === "frame-same") {
- return { nodes: [{ backendDOMNodeId: 20 }] };
+ return {
+ nodes: [
+ {
+ backendDOMNodeId: 20,
+ role: { value: "button" },
+ name: { value: "Same process" },
+ },
+ ],
+ };
}
if (sessionId === "session:oopif") {
- return { nodes: [{ backendDOMNodeId: 30 }] };
+ return {
+ nodes: [
+ {
+ backendDOMNodeId: 30,
+ role: { value: "button" },
+ name: { value: "OOPIF" },
+ },
+ ],
+ };
}
return { nodes: [] };
},
@@ -413,7 +429,55 @@ test("snapshot refs inherit their same-process and OOPIF frame ids", async () =>
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 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" },
+ },
+ ],
+ };
+ }
+ if (sessionId === "session:oopif") {
+ return {
+ nodes: [
+ {
+ backendDOMNodeId: 21,
+ role: { value: "button" },
+ name: { value: "Increment counter" },
+ },
+ ],
+ };
+ }
+ return { nodes: [] };
+ },
+ };
+
+ await enrichSnapshotRefFrames(
+ services,
+ "session:page",
+ new Map([["frame-oopif", "session:oopif"]]),
+ refs,
+ );
+
+ assert.equal(
+ refs[0].frameId,
+ undefined,
+ "a renderer-local id collision must not reroute a top-level ref",
+ );
+});
diff --git a/package/ego-browser/src/snapshot-result.ts b/package/ego-browser/src/snapshot-result.ts
index d406aa3..9c66154 100644
--- a/package/ego-browser/src/snapshot-result.ts
+++ b/package/ego-browser/src/snapshot-result.ts
@@ -186,25 +186,79 @@ export async function enrichSnapshotRefFrames(
}
if (refsByBackendNode.size === 0) return;
- for (const [frameId, frameSessionId] of iframeSessions) {
- const sameProcess = frameSessionId === pageSessionId;
+ 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",
- sameProcess ? { frameId } : {},
- frameSessionId,
+ context.params,
+ context.sessionId,
);
} catch {
- continue;
+ // 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?.length !== 1 || matches[0].frameId) continue;
- matches[0].frameId = frameId;
+ 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. */