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): explain unknown Page labels in task-lifecycle terms

Zhuowei Wangd1fca9933652
diff --git a/package/ego-browser/src/page-model.test.mjs b/package/ego-browser/src/page-model.test.mjs
index f914f0e..9d27027 100644
--- a/package/ego-browser/src/page-model.test.mjs
+++ b/package/ego-browser/src/page-model.test.mjs
@@ -1572,6 +1572,62 @@ test("TaskSpace finish validates every retained label before closing Pages", asy
});
});
+test("Page reports existing labels when an Agent uses one it never created", async () => {
+ await withFixture(async (fixture) => {
+ const task = taskForRound(fixture, "round-a");
+ await openTestPage(task, "https://example.test/one");
+
+ const guessed = task.page("p2");
+ assert.ok(guessed, "task.page returns a handle even for an unknown label");
+ await assert.rejects(
+ () => guessed.url(),
+ (error) => {
+ assert.match(error.message, /^page label not found: p2\. /);
+ assert.match(error.message, /Managed Pages in task space 7: p1\./);
+ assert.match(error.message, /await task\.newPage\(\)/);
+ return true;
+ },
+ );
+ });
+});
+
+test("Page reports a finished task when used after finish in the same round", async () => {
+ await withFixture(async (fixture) => {
+ const task = taskForRound(fixture, "round-a", {
+ async completeTaskSpace() {},
+ });
+ const page = await openTestPage(task, "https://example.test/one");
+ await task.finish({ keep: ["p1"] });
+
+ await assert.rejects(
+ () => page.url(),
+ /^Error: page label not found: p1\. Task space 7 already finished/,
+ );
+ });
+});
+
+test("Page reports user ownership when a later round reuses a finished task's label", async () => {
+ await withFixture(async (fixture) => {
+ const task = taskForRound(fixture, "round-a", {
+ async completeTaskSpace() {},
+ });
+ await openTestPage(task, "https://example.test/one");
+ await task.finish({ keep: ["p1"] });
+
+ const nextRound = createTaskSpaceHandle(
+ { id: 7, name: "research", ownership: "user" },
+ {
+ ledger: new PageLedgerStore({ rootDir: fixture.rootDir }),
+ ...fixture.services,
+ },
+ );
+ await assert.rejects(
+ () => nextRound.page("p1").url(),
+ /^Error: page label not found: p1\. Task space 7 is owned by the user/,
+ );
+ });
+});
+
test("handoff preserves user-created tabs as unknown and captures the active user page", async () => {
await withFixture(async (fixture) => {
const firstRound = taskForRound(fixture, "round-a", {
diff --git a/package/ego-browser/src/page-model.ts b/package/ego-browser/src/page-model.ts
index 1e4d4c3..a780501 100644
--- a/package/ego-browser/src/page-model.ts
+++ b/package/ego-browser/src/page-model.ts
@@ -671,6 +671,7 @@ const captureUserBoundaryToken = Symbol("captureUserBoundary");
const initializeTaskSpaceToken = Symbol("initializeTaskSpace");
const initializeCreatedSpaceToken = Symbol("initializeCreatedSpace");
const rollbackCreatedTaskSpaceToken = Symbol("rollbackCreatedTaskSpace");
+const describeMissingPageToken = Symbol("describeMissingPage");
const defaultGate: OperationGate = {
withSpace: defaultWithSpace,
@@ -842,6 +843,7 @@ class TaskSpace {
#userPage?: Page | UnmanagedPage;
#stopBrowserEvents?: () => void;
#backgroundDiscoveryInitialized = false;
+ #finished = false;
constructor(descriptor: TaskSpaceDescriptor, services: PageModelServices) {
this.taskId = descriptor.taskId;
@@ -1106,11 +1108,47 @@ class TaskSpace {
}
async #discardTerminalState(): Promise<void> {
+ this.#finished = true;
await this.#services.ledger.discard(this.spaceId);
this.#stopBackgroundPageDiscovery();
clearSpacePageNotices(this.spaceId);
}
+ /**
+ * Explain an unknown Page label in task-lifecycle terms: the same ledger miss
+ * means "never created" during a task but "already ended" after finish().
+ */
+ async [describeMissingPageToken](
+ label: string,
+ error: unknown,
+ ): Promise<unknown> {
+ if (
+ !(error instanceof Error) ||
+ error.message !== `page label not found: ${label}`
+ ) {
+ return error;
+ }
+ if (this.#finished) {
+ return new Error(
+ `${error.message}. Task space ${this.id} already finished, which ended its Page labels; do not use Pages after task.finish()`,
+ { cause: error },
+ );
+ }
+ if (this.ownership !== "agent") {
+ return new Error(
+ `${error.message}. Task space ${this.id} is owned by the user, so labels from a finished Agent task no longer exist; stop operating it unless the user asks you to continue`,
+ { cause: error },
+ );
+ }
+ const labels = Object.keys(
+ (await this.#services.ledger.read(this.id)).pages,
+ );
+ return new Error(
+ `${error.message}. Managed Pages in task space ${this.id}: ${labels.join(", ") || "none"}. task.page(label) only refers to an existing Page; create one with await task.newPage()`,
+ { cause: error },
+ );
+ }
+
/** Send a Target or Browser domain command within this selected space. */
async cdp(
method: string,
@@ -2453,7 +2491,11 @@ class Page {
}
async #resolve(): Promise<PageTarget> {
- const entry = await this.#services.ledger.getPage(this.spaceId, this.label);
+ const entry = await this.#services.ledger
+ .getPage(this.spaceId, this.label)
+ .catch(async (error) => {
+ throw await this.#task[describeMissingPageToken](this.label, error);
+ });
this.#targetId = entry.targetId;
this.#openedBy = entry.openedBy;
markPageObserved(this.spaceId, entry.targetId);
diff --git a/package/ego-browser/src/public-api-schema.ts b/package/ego-browser/src/public-api-schema.ts
index 4556620..1c020df 100644
--- a/package/ego-browser/src/public-api-schema.ts
+++ b/package/ego-browser/src/public-api-schema.ts
@@ -109,7 +109,8 @@ export const PUBLIC_API_SCHEMA: readonly PublicApiEntry[] = [
{
name: "TaskSpace.page",
signature: "task.page(label)",
- summary: "Create a lazy Page handle for a durable page label.",
+ summary:
+ "Create a lazy handle for an existing managed Page label; it neither creates a Page nor checks that the label exists.",
},
{
name: "TaskSpace.userPage",
diff --git a/skills/ego-browser/SKILL.md b/skills/ego-browser/SKILL.md
index 38ecdee..d60b331 100644
--- a/skills/ego-browser/SKILL.md
+++ b/skills/ego-browser/SKILL.md
@@ -148,6 +148,11 @@ reports the configured Page budget when it is reached.
`task.newPage()` creates another blank Page when multiple Pages must stay open.
Navigate it separately with `page.goto()`.
+`task.page(label)` only refers to a Page that already exists; it never creates
+one, and its handle is always truthy, so `task.page("p2") || await task.newPage()`
+and `if (!page)` do not detect a missing label. Use labels printed by earlier
+output or by `await task.pages()`, and call `await task.newPage()` to create one.
+
`await task.pages()` returns managed Pages. `await task.tabs()` returns every tab in the
space as `{ label?, page, targetId, title, url, active, openedBy }`. A tab
without a label is unmanaged; adopt it before operating:
@@ -494,6 +499,10 @@ When the task succeeds, close the TaskSpace by default with
`await task.finish({ keep: [] })`. Call `finish()` exactly once and wait for it
to resolve before reporting completion.
+`finish()` ends the task and all of its Page labels, including Pages kept for the
+user. Put it last in the final round only; do not operate any Page after it in
+the same script or in later rounds.
+
Keeping Pages is a rare exception: retain only necessary Pages when the user
explicitly asks, or when the result must remain in the browser for the user to
view or continue working with. Pages merely visited, search results, and
diff --git a/skills/ego-browser/references/api.md b/skills/ego-browser/references/api.md
index 707ba5c..589ba8d 100644
--- a/skills/ego-browser/references/api.md
+++ b/skills/ego-browser/references/api.md
@@ -29,7 +29,7 @@ Selectors accept refs, Ego locators, XPath, and raw CSS. A small compatibility s
| `task.spaceId` | — | Stable numeric identifier for this task space. |
| `task.name` | — | Human-readable task-space name. |
| `task.ownership` | — | Ownership state captured when this TaskSpace was created. |
-| `task.page(label)` | — | Create a lazy Page handle for a durable page label. |
+| `task.page(label)` | — | Create a lazy handle for an existing managed Page label; it neither creates a Page nor checks that the label exists. |
| `task.userPage()` | — | Return the tab active at the claim/takeover boundary, when one was captured. |
| `await task.pages()` | — | List the managed Page handles in this space. |
| `await task.tabs()` | — | List managed Pages and unmanaged tabs in this space. |