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): report a stale subtree root instead of retrying with it

Zhuowei Wang3c46f0d308ab
diff --git a/package/ego-browser/src/page-model.test.mjs b/package/ego-browser/src/page-model.test.mjs
index e417043..a3e6421 100644
--- a/package/ego-browser/src/page-model.test.mjs
+++ b/package/ego-browser/src/page-model.test.mjs
@@ -5942,6 +5942,56 @@ test("a navigation during snapshot capture is retried against the new document",
});
});
+test("a subtree snapshot whose root document commits away is stale, not retried with the old root", async () => {
+ await withFixture(async (fixture) => {
+ let loaderId = "before";
+ const requestedRoots = [];
+ const baseCdp = fixture.services.cdp;
+ const task = taskForRound(fixture, "round-a", {
+ async cdp(method, params, sessionId) {
+ if (method === "Page.getFrameTree")
+ return { frameTree: { frame: { id: "main", loaderId } } };
+ return baseCdp(method, params, sessionId);
+ },
+ async snapshot(options) {
+ if (options.scope !== "subtree") {
+ return {
+ content: 'button "First" [ref=21]',
+ refs: [{ refId: 21, backendNodeId: 21 }],
+ };
+ }
+ requestedRoots.push(options.root);
+ // The navigation commits while the subtree capture is in flight.
+ loaderId = "after";
+ return {
+ content: 'button "First" [ref=21]',
+ refs: [{ refId: 21, backendNodeId: 21 }],
+ };
+ },
+ });
+ const page = await openTestPage(task, "https://example.test/first");
+ await page.snapshot({ scope: "full_page" });
+
+ await assert.rejects(
+ () => page.snapshot({ scope: "subtree", root: "@21" }),
+ (error) => {
+ assert.match(error.message, /Stale ref: @21; take a new snapshot/);
+ assert.equal(error.kind, "permanent");
+ return true;
+ },
+ );
+ assert.deepEqual(
+ requestedRoots,
+ [21],
+ "the old document's root is not reused for a retry",
+ );
+ assert.equal(
+ fixture.services.pageRefs.isInvalidated(page.targetId, "21"),
+ true,
+ );
+ });
+});
+
test("a page that keeps navigating during snapshot captures fails after bounded retries", async () => {
await withFixture(async (fixture) => {
let loaderId = 0;
diff --git a/package/ego-browser/src/page-model.ts b/package/ego-browser/src/page-model.ts
index 875efd7..f9cab75 100644
--- a/package/ego-browser/src/page-model.ts
+++ b/package/ego-browser/src/page-model.ts
@@ -2002,6 +2002,19 @@ class Page {
const beforeDocuments = documents;
result = await capture();
documents = await this.#pageDocuments(page, sessionId);
+ if (rootRefId) {
+ // The subtree root is a node of the document the refs were taken
+ // from. Once that document is gone, a retry would capture whatever
+ // node now carries its backend id, so the root is stale instead.
+ const registry = this.#services.pageRefs;
+ registry.invalidateChangedDocuments(page.targetId, documents);
+ if (registry.isInvalidated(page.targetId, rootRefId)) {
+ throw new ElementResolutionError(
+ `Stale ref: @${rootRefId}; take a new snapshot`,
+ "permanent",
+ );
+ }
+ }
const changed = (result?.refs || []).some((ref) => {
const documentId = pageRefDocumentId(ref, documents);
return (