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.

revert(ego-browser): restore async page url

0x0ed424a2fe621
diff --git a/package/ego-browser/scripts/real-browser-e2e/cases/playwright-page-url.mjs b/package/ego-browser/scripts/real-browser-e2e/cases/playwright-page-url.mjs
index 80cd0f2..a0bda9f 100644
--- a/package/ego-browser/scripts/real-browser-e2e/cases/playwright-page-url.mjs
+++ b/package/ego-browser/scripts/real-browser-e2e/cases/playwright-page-url.mjs
@@ -2,10 +2,10 @@ import { homeCase } from "./shared.mjs";
export const playwrightPageUrlCases = [
{
- name: "regression PWB-03 page.url synchronous contract",
+ name: "regression PWB-03 page.url asynchronous contract",
body: homeCase(`
- const initialUrl = page.url();
- assertEqual(typeof initialUrl, "string", "PWB-03 page.url returns a string synchronously");
+ const initialUrl = await page.url();
+ assertEqual(typeof initialUrl, "string", "PWB-03 awaited page.url returns a string");
assertIncludes(initialUrl, baseUrl, "PWB-03 page.url reflects the current tab");
const navigation = page.waitForURL(
@@ -14,7 +14,7 @@ export const playwrightPageUrlCases = [
);
await page.getByRole("link", { name: "Go to nav target" }).click();
assert(await navigation, "PWB-03 link navigation reaches nav target");
- assertIncludes(page.url(), "/nav-target", "PWB-03 page.url refreshes after page navigation");
+ assertIncludes(await page.url(), "/nav-target", "PWB-03 page.url reads the URL after navigation");
`),
},
];
diff --git a/package/ego-browser/src/browser-runtime.test.mjs b/package/ego-browser/src/browser-runtime.test.mjs
index 7e7270e..ff7b0b8 100644
--- a/package/ego-browser/src/browser-runtime.test.mjs
+++ b/package/ego-browser/src/browser-runtime.test.mjs
@@ -82,9 +82,6 @@ function cleanup() {
drainBrowserEvents();
state.cdpOverride = null;
state.sessionInflight = null;
- state.pageUrl = "";
- state.pageUrlTargetId = null;
- state.pageMainFrameId = null;
}
/* ------------------------------------------------------------------ */
@@ -584,82 +581,6 @@ test("ensureSession selects the preferred tab when available", async () => {
}
});
-test("ensureSession caches the selected tab URL for synchronous page.url", async () => {
- installAutoEgo({
- tabs: [
- {
- targetId: "tab-current",
- active: true,
- url: "https://example.com/current",
- },
- ],
- });
- try {
- await ensureSession();
- assert.equal(state.pageUrl, "https://example.com/current");
- assert.equal(state.pageUrlTargetId, "tab-current");
- } finally {
- cleanup();
- }
-});
-
-test("main-frame navigation events refresh the synchronous page URL", async () => {
- installAutoEgo({
- tabs: [
- {
- targetId: "tab-current",
- active: true,
- url: "https://example.com/start",
- },
- ],
- });
- try {
- await ensureSession();
- const sessionId = state.sessionId;
-
- fireEvent(
- "Page.frameNavigated",
- {
- frame: {
- id: "main-frame",
- url: "https://example.com/next",
- },
- },
- sessionId,
- );
- assert.equal(state.pageUrl, "https://example.com/next");
-
- fireEvent(
- "Page.frameNavigated",
- {
- frame: {
- id: "child-frame",
- parentId: "main-frame",
- url: "https://example.com/frame",
- },
- },
- sessionId,
- );
- assert.equal(
- state.pageUrl,
- "https://example.com/next",
- "subframe navigation does not replace the page URL",
- );
-
- fireEvent(
- "Page.navigatedWithinDocument",
- {
- frameId: "main-frame",
- url: "https://example.com/next#section",
- },
- sessionId,
- );
- assert.equal(state.pageUrl, "https://example.com/next#section");
- } finally {
- cleanup();
- }
-});
-
test("ensureSession falls back to the last tab when no active tab is found", async () => {
const calls = installAutoEgo({
tabs: [
diff --git a/package/ego-browser/src/browser-runtime.ts b/package/ego-browser/src/browser-runtime.ts
index d6b2853..3c85bb7 100644
--- a/package/ego-browser/src/browser-runtime.ts
+++ b/package/ego-browser/src/browser-runtime.ts
@@ -1,4 +1,4 @@
-import { cachePageUrl, clearPageUrl, state } from "./state.js";
+import { state } from "./state.js";
import { assertNoEgoError, buildEgoError } from "./ego-errors.js";
const RESPONSE_TIMEOUT_MS = 15000;
@@ -118,9 +118,6 @@ export async function ensureSession() {
throw new Error("no active tab to attach session");
}
const targetId = active.targetId;
- if (typeof active.url === "string") {
- cachePageUrl(active.url, targetId);
- }
if (targetId !== state.sessionTargetId || !state.sessionId) {
const attached = await rawCdp(
"Target.attachToTarget",
@@ -246,46 +243,10 @@ function handleMessage(message) {
pendingDialogs.delete(sessionId);
}
const targetId = data.params?.targetId || data.params?.targetInfo?.targetId;
- if (targetId && targetId === state.pageUrlTargetId) {
- clearPageUrl(targetId);
- }
if (targetId && targetId === state.sessionTargetId) {
invalidateSession();
}
}
- if (data.method === "Target.targetInfoChanged") {
- const targetInfo = data.params?.targetInfo;
- const currentTargetId =
- state.preferredTargetId || state.sessionTargetId || state.pageUrlTargetId;
- if (
- targetInfo?.targetId === currentTargetId &&
- typeof targetInfo.url === "string"
- ) {
- cachePageUrl(targetInfo.url, targetInfo.targetId);
- }
- }
- if (
- data.method === "Page.frameNavigated" &&
- (!data.sessionId || data.sessionId === state.sessionId)
- ) {
- const frame = data.params?.frame;
- if (frame && !frame.parentId && typeof frame.url === "string") {
- state.pageMainFrameId = frame.id || null;
- cachePageUrl(frame.url, state.sessionTargetId);
- }
- } else if (
- data.method === "Page.navigatedWithinDocument" &&
- (!data.sessionId || data.sessionId === state.sessionId)
- ) {
- const frameId = data.params?.frameId;
- if (
- typeof data.params?.url === "string" &&
- (!state.pageMainFrameId || frameId === state.pageMainFrameId)
- ) {
- state.pageMainFrameId = frameId || state.pageMainFrameId;
- cachePageUrl(data.params.url, state.sessionTargetId);
- }
- }
if (data.method === "Page.javascriptDialogOpening") {
const sessionId = data.sessionId || state.sessionId;
if (sessionId) {
diff --git a/package/ego-browser/src/driver/nav.test.mjs b/package/ego-browser/src/driver/nav.test.mjs
index 3a86286..362f955 100644
--- a/package/ego-browser/src/driver/nav.test.mjs
+++ b/package/ego-browser/src/driver/nav.test.mjs
@@ -177,7 +177,7 @@ test("openOrReuseTab settles a newly opened tab in milliseconds, not seconds", a
assert.deepEqual(sleeps, [500]);
});
-test("switchTab refreshes the target list and caches the selected URL", async () => {
+test("switchTab refreshes the target list before activating it", async () => {
const calls = [];
await withEgo(
{
@@ -209,8 +209,6 @@ test("switchTab refreshes the target list and caches the selected URL", async ()
});
try {
assert.equal(await switchTab({ targetId: "target-2" }), "target-2");
- assert.equal(state.pageUrl, "https://example.com/docs");
- assert.equal(state.pageUrlTargetId, "target-2");
} finally {
restore();
}
@@ -299,13 +297,9 @@ test("closeTab closes an explicit current target and returns its id", async () =
calls.push({ method, params, sessionId });
return { success: true };
},
- pageUrl: "https://example.com/",
- pageUrlTargetId: "target-2",
});
try {
assert.equal(await closeTab("target-2"), "target-2");
- assert.equal(state.pageUrl, "");
- assert.equal(state.pageUrlTargetId, null);
} finally {
restore();
}
@@ -397,14 +391,10 @@ test("closeTab waits for a closed target to disappear from listTabs", async () =
sleeps.push(ms);
now += ms;
},
- pageUrl: "https://example.com/scratch",
- pageUrlTargetId: "target-scratch",
});
try {
assert.equal(await closeTab("target-scratch"), "target-scratch");
assert.deepEqual(sleeps, [50]);
- assert.equal(state.pageUrl, "https://example.com/current");
- assert.equal(state.pageUrlTargetId, "target-current");
} finally {
restore();
}
diff --git a/package/ego-browser/src/driver/nav.ts b/package/ego-browser/src/driver/nav.ts
index 88c5f2e..64a055b 100644
--- a/package/ego-browser/src/driver/nav.ts
+++ b/package/ego-browser/src/driver/nav.ts
@@ -9,7 +9,7 @@ import {
} from "../browser-runtime.js";
import { cdp, evaluate } from "../cdp-eval.js";
import { assertNoEgoError } from "../ego-errors.js";
-import { cachePageUrl, clearPageUrl, state } from "../state.js";
+import { state } from "../state.js";
import { waitForDocumentLoad } from "./load.js";
export const INTERNAL_URL_PREFIXES = [
@@ -59,7 +59,6 @@ type TabTarget = string | { targetId: string };
*/
export async function goto(url: string, options: GotoOptions = {}) {
const navigation = await cdp("Page.navigate", { url });
- cachePageUrl(url);
const loaded =
options.waitUntil === "commit"
? false
@@ -102,9 +101,7 @@ export async function pageInfo() {
ph: root?.scrollHeight ?? innerHeight,
});
})()`;
- const info = JSON.parse(await evaluate(expression));
- cachePageUrl(info.url);
- return info;
+ return JSON.parse(await evaluate(expression));
}
/**
@@ -145,7 +142,6 @@ export async function currentTab() {
if (!active) {
throw new Error("no active browser tab");
}
- cachePageUrl(active.url, active.targetId);
return { targetId: active.targetId, url: active.url, title: active.title };
}
@@ -157,11 +153,10 @@ export async function currentTab() {
export async function switchTab(target: string | { targetId: string }) {
const targetId = targetIdFrom(target, "switchTab");
const tabs = await listTabs();
- const tab = currentTargetFrom(tabs, targetId, "switchTab");
+ currentTargetFrom(tabs, targetId, "switchTab");
await cdp("Target.activateTarget", { targetId });
invalidateSession();
setPreferredTarget(targetId);
- cachePageUrl(tab.url, targetId);
return targetId;
}
@@ -203,7 +198,6 @@ export async function openOrReuseTab(
return { ...existing, active: true, reused: true };
}
const targetId = await newTab(url);
- cachePageUrl(url, targetId);
if (options.wait !== false) {
await waitForDocumentLoad({ timeout: options.timeout ?? 20000 });
}
@@ -232,11 +226,8 @@ export async function closeTab(target: TabTarget | undefined = undefined) {
if (state.preferredTargetId === targetId) {
clearPreferredTarget();
}
- clearPageUrl(targetId);
if (tabs.length > 1) {
- const remaining = await waitForClosedTarget(targetId);
- const active = remaining.find((tab) => tab.active) || remaining[0];
- if (active) cachePageUrl(active.url, active.targetId);
+ await waitForClosedTarget(targetId);
}
return targetId;
}
diff --git a/package/ego-browser/src/driver/waits.ts b/package/ego-browser/src/driver/waits.ts
index d270483..c588d8d 100644
--- a/package/ego-browser/src/driver/waits.ts
+++ b/package/ego-browser/src/driver/waits.ts
@@ -1,4 +1,4 @@
-import { cachePageUrl, state } from "../state.js";
+import { state } from "../state.js";
import { cdp, runtimeValue } from "../cdp-eval.js";
import { resolveHandle, releaseHandle } from "./element-ops.js";
import { ElementResolutionError } from "../element-resolver.js";
@@ -100,7 +100,6 @@ export async function waitForURL(url, options: WaitForURLOptions = {}) {
}),
"location.href",
);
- cachePageUrl(current);
if (urlMatches(current, url)) {
const waitUntil = options.waitUntil ?? "load";
return waitUntil === "commit"
diff --git a/package/ego-browser/src/format.test.mjs b/package/ego-browser/src/format.test.mjs
index 01f6cbe..5f58ecb 100644
--- a/package/ego-browser/src/format.test.mjs
+++ b/package/ego-browser/src/format.test.mjs
@@ -41,13 +41,17 @@ test("formatCliLogValue renders documented function properties in object output"
assert.equal(parsed.helpers.site.runTool.params[0].name, "siteId");
});
-test("formatCliLogValue documents page.url as synchronous", () => {
+test("formatCliLogValue documents page.url as asynchronous", () => {
const formatted = formatCliLogValue({
helpers: { page: { url() {} } },
});
const parsed = JSON.parse(formatted);
- assert.equal(parsed.helpers.page.url.signature, "page.url() => string");
+ assert.equal(
+ parsed.helpers.page.url.signature,
+ "page.url() => Promise<string>",
+ );
+ assert.match(parsed.helpers.page.url.example, /await page\.url\(\)/);
});
test("formatCliLogValue documents ego.learnings as the site facade alias", () => {
diff --git a/package/ego-browser/src/format.ts b/package/ego-browser/src/format.ts
index e91891e..a51bedd 100644
--- a/package/ego-browser/src/format.ts
+++ b/package/ego-browser/src/format.ts
@@ -70,11 +70,10 @@ const FUNCTION_DOCS: Record<string, FunctionDoc> = {
example: "console.log(await page.info())",
},
"page.url": {
- signature: "page.url() => string",
- description:
- "Synchronously return the latest browser-confirmed URL for the current page.",
- returns: "string",
- example: "console.log(page.url())",
+ signature: "page.url() => Promise<string>",
+ description: "Asynchronously return the current page URL.",
+ returns: "Promise<string>",
+ example: "console.log(await page.url())",
},
"page.title": {
signature: "page.title() => Promise<string>",
diff --git a/package/ego-browser/src/helpers.test.mjs b/package/ego-browser/src/helpers.test.mjs
index 1fc861f..bda8f70 100644
--- a/package/ego-browser/src/helpers.test.mjs
+++ b/package/ego-browser/src/helpers.test.mjs
@@ -2,7 +2,7 @@ import test from "node:test";
import assert from "node:assert/strict";
import * as helperExports from "../dist/src/helpers.js";
-import { state } from "../dist/src/state.js";
+import { setOverrides } from "../dist/src/state.js";
import {
claimTaskSpace,
completeTaskSpace,
@@ -221,15 +221,32 @@ test("helper surface exposes Playwright-style object facades", () => {
assert.equal("elementEval" in context, false);
});
-test("page.url returns the cached current URL synchronously", () => {
- const previousUrl = state.pageUrl;
- state.pageUrl = "https://example.com/current";
+test("page.url reads the current URL asynchronously", async () => {
+ const restore = setOverrides({
+ cdpOverride: async (method) => {
+ assert.equal(method, "Runtime.evaluate");
+ return {
+ result: {
+ value: JSON.stringify({
+ url: "https://example.com/current",
+ title: "Current",
+ w: 800,
+ h: 600,
+ sx: 0,
+ sy: 0,
+ pw: 800,
+ ph: 600,
+ }),
+ },
+ };
+ },
+ });
try {
const value = helperContext().page.url();
- assert.equal(typeof value, "string");
- assert.equal(value, "https://example.com/current");
+ assert.equal(typeof value.then, "function");
+ assert.equal(await value, "https://example.com/current");
} finally {
- state.pageUrl = previousUrl;
+ restore();
}
});
diff --git a/package/ego-browser/src/helpers.ts b/package/ego-browser/src/helpers.ts
index a0814d7..0fefa5c 100644
--- a/package/ego-browser/src/helpers.ts
+++ b/package/ego-browser/src/helpers.ts
@@ -701,7 +701,7 @@ function createPageFacade() {
});
},
info: nav.pageInfo,
- url: () => state.pageUrl,
+ url: async () => (await nav.pageInfo()).url,
title: async () => (await nav.pageInfo()).title,
locator: createLocator,
getByRole: (role, options: any = {}) => {
@@ -801,7 +801,7 @@ function createSiteFacade() {
}
const FACADE_HELP: Record<string, string> = {
- page: 'page: Playwright-style page facade. page.url() synchronously returns the latest browser-confirmed URL. Use page.goto(url), page.locator(selector), page.getByText(text), page.getByLabel(text), page.getByPlaceholder(text), page.getByTestId(testId), page.setDefaultTimeout(ms), page.waitForEvent("download"), page.waitForLoadState(state, options), page.waitForURL(url, options), page.waitForRequest(urlOrPredicate, options), page.waitForResponse(urlOrPredicate, options), page.evaluate(expression), page.screenshot(options), page.keyboard.press(key), page.keyboard.type(text), and page.mouse.click(x, y). waitForURL predicates receive URL objects and waitUntil defaults to load.',
+ page: 'page: Playwright-style page facade. page.url() asynchronously returns the current URL; always call await page.url() before using the string. Use page.goto(url), page.locator(selector), page.getByText(text), page.getByLabel(text), page.getByPlaceholder(text), page.getByTestId(testId), page.setDefaultTimeout(ms), page.waitForEvent("download"), page.waitForLoadState(state, options), page.waitForURL(url, options), page.waitForRequest(urlOrPredicate, options), page.waitForResponse(urlOrPredicate, options), page.evaluate(expression), page.screenshot(options), page.keyboard.press(key), page.keyboard.type(text), and page.mouse.click(x, y). waitForURL predicates receive URL objects and waitUntil defaults to load.',
locator:
"page.locator(selector): returns a strict, auto-waiting locator facade with locator(), getByRole(), getByText(), filter(), first(), nth(index), last(), click(), fill(value), clear(), press(key), check(), selectOption(value), textContent(), innerText(), innerHTML(), isVisible(), isEnabled(), getAttribute(name), screenshot(), count(), evaluate(fn, arg), evaluateAll(fn, arg), and waitFor(options). Narrow multiple matches; use first()/nth() only for confirmed legitimate duplicates.",
browser:
diff --git a/package/ego-browser/src/index.test.mjs b/package/ego-browser/src/index.test.mjs
index 22a6f80..0720542 100644
--- a/package/ego-browser/src/index.test.mjs
+++ b/package/ego-browser/src/index.test.mjs
@@ -2,7 +2,6 @@ import test from "node:test";
import assert from "node:assert/strict";
import { installEgoSdk } from "../dist/src/index.js";
-import { state } from "../dist/src/state.js";
test("installEgoSdk keeps page.locator chainable while wrapping locator methods", () => {
const originalLog = console.log;
@@ -44,7 +43,7 @@ test("installEgoSdk keeps page.locator chainable while wrapping locator methods"
}
});
-test("installEgoSdk preserves the synchronous page.url contract", () => {
+test("installEgoSdk preserves the asynchronous page.url contract", async () => {
const originalLog = console.log;
const target = {};
try {
@@ -52,43 +51,18 @@ test("installEgoSdk preserves the synchronous page.url contract", () => {
cliLog() {},
context: {
page: {
- url: () => "https://example.com/current",
+ url: async () => "https://example.com/current",
},
},
});
const value = target.page.url();
- assert.equal(typeof value, "string");
- assert.equal(value, "https://example.com/current");
+ assert.equal(typeof value.then, "function");
+ assert.equal(await value, "https://example.com/current");
} finally {
console.log = originalLog;
}
});
-test("task-space changes invalidate the synchronous page URL projection", async () => {
- const originalLog = console.log;
- const previousUrl = state.pageUrl;
- const previousTargetId = state.pageUrlTargetId;
- const target = {
- ego: {
- async useTaskSpace() {
- return { ok: true };
- },
- },
- };
- try {
- state.pageUrl = "https://example.com/previous-space";
- state.pageUrlTargetId = "previous-target";
- installEgoSdk(target, { cliLog() {} });
- await target.ego.useTaskSpace("next-space");
- assert.equal(state.pageUrl, "");
- assert.equal(state.pageUrlTargetId, null);
- } finally {
- state.pageUrl = previousUrl;
- state.pageUrlTargetId = previousTargetId;
- console.log = originalLog;
- }
-});
-
test("installEgoSdk exposes the site facade under ego.learnings", () => {
const originalLog = console.log;
const target = { ego: {} };
diff --git a/package/ego-browser/src/index.ts b/package/ego-browser/src/index.ts
index f2f09c7..99f8f68 100644
--- a/package/ego-browser/src/index.ts
+++ b/package/ego-browser/src/index.ts
@@ -7,7 +7,6 @@ import {
invalidateSession,
setPreferredTarget,
} from "./browser-runtime.js";
-import { clearPageUrl } from "./state.js";
import { formatCliLogValue } from "./format.js";
import {
bufferOutput,
@@ -38,7 +37,6 @@ export * from "./helpers.js";
export { runMain } from "./run.js";
const SYNC_HELPERS = new Set(["help"]);
-const SYNC_VALUE_HELPERS = new Set(["page.url"]);
const SYNC_FACTORY_HELPERS = new Set([
"page.locator",
"page.getByRole",
@@ -225,9 +223,6 @@ function wrapReady(
path: string[] = [],
): unknown {
if (typeof value === "function") {
- if (SYNC_VALUE_HELPERS.has(path.join("."))) {
- return (...args: unknown[]) => value(...args);
- }
if (isSyncFactoryHelper(path)) {
return (...args: unknown[]) =>
wrapReady(value(...args), readySignal, readyError, path);
@@ -290,7 +285,6 @@ function wrapInvalidating(ego: EgoRuntime, methodNames: string[]) {
const after = () => {
invalidateSession();
clearPreferredTarget();
- clearPageUrl();
};
ego[name] = function (...args: unknown[]) {
const result = original.apply(this, args);
diff --git a/package/ego-browser/src/state.ts b/package/ego-browser/src/state.ts
index b0eb5ee..3225188 100644
--- a/package/ego-browser/src/state.ts
+++ b/package/ego-browser/src/state.ts
@@ -34,38 +34,11 @@ export const state = {
sessionAt: 0,
sessionInflight: null,
preferredTargetId: null,
- // Synchronous projection used by Playwright-style page.url(). The browser
- // remains authoritative; navigation/tab operations and CDP events refresh it.
- pageUrl: "",
- pageUrlTargetId: null,
- pageMainFrameId: null,
defaultTimeout: 10000,
// Last observed Network domain state on the default session (tracked in cdp()).
networkDomainEnabled: false,
};
-export function cachePageUrl(
- url: string,
- targetId = state.sessionTargetId ||
- state.preferredTargetId ||
- state.pageUrlTargetId ||
- null,
-) {
- if (typeof url !== "string") return;
- if (targetId && targetId !== state.pageUrlTargetId) {
- state.pageMainFrameId = null;
- }
- state.pageUrl = url;
- state.pageUrlTargetId = targetId;
-}
-
-export function clearPageUrl(targetId: string | null = null) {
- if (targetId && targetId !== state.pageUrlTargetId) return;
- state.pageUrl = "";
- state.pageUrlTargetId = null;
- state.pageMainFrameId = null;
-}
-
export async function send(req) {
return state.send(req);
}
diff --git a/skills/ego-browser/SKILL.md b/skills/ego-browser/SKILL.md
index 2f93fc5..39a7579 100644
--- a/skills/ego-browser/SKILL.md
+++ b/skills/ego-browser/SKILL.md
@@ -79,7 +79,7 @@ await page.waitForURL(
- `await fetch.server(url, options)` — issues a request from Node and returns the response body.
- `await fetch.browser(url, options)` — issues a request from the current browser page context and returns the response body.
- `help(name)` — prints usage for a facade, e.g. `console.log(help('page'))` or `console.log(help('locator'))`.
-- `page.url()` — synchronously returns the latest browser-confirmed URL string, matching Playwright's call shape. In a fresh heredoc, first select the task space and await a tab/navigation helper such as `browser.currentTab()`, `browser.switchTab(...)`, `browser.openOrReuseTab(...)`, or `page.goto(...)` so the current-tab projection is initialized.
+- `await page.url()` — asynchronously returns the current URL string. Always await it before reading or using the URL.
### Task spaces