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.
JavaScript agent-skillsai-agentautomationbrowserbrowser-automationclaude-codecodexhermes-agentskillsskills-sh
fix(ego-browser): remind agents to release task spaces they still control
Zhuowei Wang2be246f50d8f
diff --git a/package/ego-browser/skill-body/SKILL.md b/package/ego-browser/skill-body/SKILL.mdindex d4d4545..2a0f519 100644--- a/package/ego-browser/skill-body/SKILL.md+++ b/package/ego-browser/skill-body/SKILL.md@@ -536,8 +536,17 @@ await task.finish({ keep: ["p2"] }); // Exception: keep only the result Page forUser-created and unmanaged tabs are protected; if any remain, `keep: []` doesnot close the whole space. Do not close unwanted Pages one by one at completion;list the Pages to keep instead.-Use `page.close()` only while the task is still in progress. Do not call-`finish()` when the task stops for user control or an error.+Use `page.close()` only while the task is still in progress.++Do not call `finish()` for a task that has not succeeded. If the user already+took control, the stop needs no call. If you stop on an error you cannot+recover from, or to wait for the user's answer, call `await task.handOff()`+before ending the round. Otherwise the browser keeps showing the space as+Agent-controlled after you stop.++`[ego-browser:task]` in the output means a space is still Agent-controlled at+the end of the round. Ignore it when the next round continues the task;+otherwise call `finish()` or `handOff()` as described above.If the final output contains `[ego-browser:notice]`, finish the current browsertask, tell the user an Ego Lite update is available, and rundiff --git a/package/ego-browser/src/agent-control-notice.ts b/package/ego-browser/src/agent-control-notice.tsnew file mode 100644index 0000000..ee79774--- /dev/null+++ b/package/ego-browser/src/agent-control-notice.ts@@ -0,0 +1,39 @@+/**+ * Round-local record of task spaces the Agent still controls when the round+ * ends. The browser shows its Agent-control indicator until the task calls+ * finish() or handOff(); every round is a separate process, so the browser+ * cannot tell a task that continues next round from one the Agent abandoned.+ * The round-end notice asks the Agent to release control explicitly.+ */++const controlledSpaces = new Set<number>();++/** Record a TaskSpace handle that holds Agent control in this round. */+export function recordAgentControlledSpace(spaceId: number): void {+ controlledSpaces.add(spaceId);+}++/** Forget a space whose control returned to the user or whose task ended. */+export function releaseAgentControlledSpace(spaceId: number): void {+ controlledSpaces.delete(spaceId);+}++/** Format the round-end reminder, or return "" when no space needs one. */+export function consumeAgentControlNotice(): string {+ const ids = [...controlledSpaces].sort((a, b) => a - b);+ controlledSpaces.clear();+ if (ids.length === 0) return "";+ const spaces =+ ids.length === 1 ? `task space ${ids[0]}` : `task spaces ${ids.join(", ")}`;+ return (+ `[ego-browser:task] The Agent still controls ${spaces}, so the browser ` ++ "shows it as Agent-controlled. If the task is complete, call " ++ "task.finish({ keep }). If you are stopping for the user or an " ++ "unrecoverable error, call task.handOff(). Otherwise continue in the next round.\n"+ );+}++/** Clear round state. Real runs get a new process; tests can reuse one process. */+export function resetAgentControlledSpaces(): void {+ controlledSpaces.clear();+}diff --git a/package/ego-browser/src/helpers.ts b/package/ego-browser/src/helpers.tsindex fa3345e..6609275 100644--- a/package/ego-browser/src/helpers.ts+++ b/package/ego-browser/src/helpers.ts@@ -3,6 +3,7 @@ import { join } from "node:path";import { pathToFileURL } from "node:url";import { setOverrides, state } from "./state.js";+import { releaseAgentControlledSpace } from "./agent-control-notice.js";import { buildEgoError, invokeEgo, probeAgentControl } from "./ego-errors.js";import { help as helpRuntime, formatHelp } from "./help-runtime.js";import { validatePublicApiOptions } from "./public-api-schema.js";@@ -454,6 +455,7 @@ export async function completeTaskSpace(}await invokeEgo("completeTaskSpace", () => ego.closeTaskSpace());}+ releaseAgentControlledSpace(match.id);return { done: true };}@@ -469,14 +471,16 @@ export async function handOffTaskSpace(nameOrId?: string | number) {if (!ego || typeof ego.handOffTaskSpace !== "function") {throw new Error("handOffTaskSpace requires ego.handOffTaskSpace");}+ let match;if (nameOrId !== undefined) {- const match = await findTaskSpace(nameOrId, "handOffTaskSpace");+ match = await findTaskSpace(nameOrId, "handOffTaskSpace");if (match.ownership === "user") {return { done: false, skipped: "user-owned" as const };}await selectTaskSpace(ego, match, "handOffTaskSpace");}await invokeEgo("handOffTaskSpace", () => ego.handOffTaskSpace());+ if (match) releaseAgentControlledSpace(match.id);return { done: true };}diff --git a/package/ego-browser/src/output-sink.test.mjs b/package/ego-browser/src/output-sink.test.mjsindex c83db0b..76324a4 100644--- a/package/ego-browser/src/output-sink.test.mjs+++ b/package/ego-browser/src/output-sink.test.mjs@@ -16,6 +16,10 @@ import {markPageObserved,recordUnhandledPage,} from "../dist/src/page-discovery.js";+import {+ recordAgentControlledSpace,+ releaseAgentControlledSpace,+} from "../dist/src/agent-control-notice.js";test("lifecycle flush writes synchronously when stdout exposes an fd", async () => {const directory = await mkdtemp(join(tmpdir(), "ego-output-sink-test-"));@@ -160,3 +164,50 @@ test("a hard stop discards unhandled-page notices with other output", () => {assert.equal(out.text(), "STOP\n");});++test("flushSink reminds the Agent about spaces it still controls", () => {+ resetSink();+ bufferOutput("done\n");+ recordUnhandledPage({+ spaceId: 7,+ targetId: "target-popup",+ label: "p4",+ url: "about:blank",+ });+ recordAgentControlledSpace(9);+ recordAgentControlledSpace(7);+ recordAgentControlledSpace(8);+ releaseAgentControlledSpace(8);+ const out = fakeStream();++ flushSink(out, false);++ const [output, pages, page, notice, rest] = out.text().split("\n");+ assert.deepEqual(+ [output, pages, page, rest],+ ["done", "[ego-browser:pages]", "Unhandled page p4: about:blank", ""],+ );+ assert.match(notice, /^\[ego-browser:task\] .* task spaces 7, 9,/);+ assert.match(notice, /task\.finish\(\{ keep \}\).*task\.handOff\(\)/);+});++test("a failed round still reminds the Agent to release control", () => {+ resetSink();+ recordAgentControlledSpace(7);+ const out = fakeStream();++ flushSink(out, true);++ assert.match(out.text(), /^\[ego-browser:task\] .* task space 7,/);+});++test("a hard stop drops the Agent-control reminder", () => {+ resetSink();+ recordAgentControlledSpace(7);+ markHardStop("STOP");+ const out = fakeStream();++ flushSink(out, false);++ assert.equal(out.text(), "STOP\n");+});diff --git a/package/ego-browser/src/output-sink.ts b/package/ego-browser/src/output-sink.tsindex 8cd84e8..4a4e60e 100644--- a/package/ego-browser/src/output-sink.ts+++ b/package/ego-browser/src/output-sink.ts@@ -1,5 +1,9 @@import { writeSync } from "node:fs";+import {+ consumeAgentControlNotice,+ resetAgentControlledSpaces,+} from "./agent-control-notice.js";import { formatCliLogValue } from "./format.js";import {consumeUnhandledPageNotices,@@ -71,6 +75,7 @@ export function flushSink(stream: WritableLike, thrown: boolean): void {if (flushed) return;flushed = true;const pageNotices = consumeUnhandledPageNotices();+ const controlNotice = consumeAgentControlNotice();if (hardStopMessage !== null) {// Drop every buffered line — business logs, success rows, and the repeated error// echoes — so the owned guidance is all that remains.@@ -86,6 +91,9 @@ export function flushSink(stream: WritableLike, thrown: boolean): void {if (pageNotices.length > 0) {stream.write(formatPageNotices(pageNotices));}+ // A hard stop means the user already holds control, so only ordinary+ // completions and failures remind the Agent to release it.+ if (controlNotice) stream.write(controlNotice);}buffer = [];}@@ -96,6 +104,7 @@ export function resetSink(): void {hardStopMessage = null;flushed = false;resetPageNotices();+ resetAgentControlledSpaces();}function formatPageNotices(notices: UnhandledPageNotice[]): string {diff --git a/package/ego-browser/src/page-model.test.mjs b/package/ego-browser/src/page-model.test.mjsindex 9d27027..8af02bd 100644--- a/package/ego-browser/src/page-model.test.mjs+++ b/package/ego-browser/src/page-model.test.mjs@@ -20,6 +20,10 @@ import {initializeTaskSpaceHandle,} from "../dist/src/page-model.js";import { PageRefRegistry } from "../dist/src/page-ref-registry.js";+import {+ consumeAgentControlNotice,+ resetAgentControlledSpaces,+} from "../dist/src/agent-control-notice.js";import {consumeUnhandledPageNotices,resetPageNotices,@@ -964,6 +968,28 @@ async function waitForLedgerTarget(ledger, spaceId, targetId) {throw new Error(`timed out waiting for ledger target ${targetId}`);}+test("finish and handOff release the round-end Agent-control reminder", async () => {+ await withFixture(async (fixture) => {+ const services = {+ async handOffTaskSpace() {},+ async completeTaskSpace() {},+ };+ resetAgentControlledSpaces();+ await initializeTaskSpaceHandle(taskForRound(fixture, "round-a", services));+ assert.match(consumeAgentControlNotice(), /task space 7,/);++ const handedOff = taskForRound(fixture, "round-b", services);+ await initializeTaskSpaceHandle(handedOff);+ await handedOff.handOff();+ assert.equal(consumeAgentControlNotice(), "");++ const finished = taskForRound(fixture, "round-c", services);+ await initializeTaskSpaceHandle(finished);+ await finished.finish({ keep: "all" });+ assert.equal(consumeAgentControlNotice(), "");+ });+});+test("background discovery does not adopt the pre-existing tab baseline", async () => {await withFixture(async (fixture) => {resetPageNotices();diff --git a/package/ego-browser/src/page-model.ts b/package/ego-browser/src/page-model.tsindex d4ffb12..ba304f6 100644--- a/package/ego-browser/src/page-model.ts+++ b/package/ego-browser/src/page-model.ts@@ -23,6 +23,10 @@ import {type FileChooserInterception,type FileChooserOpenedEvent,} from "./browser-runtime.js";+import {+ recordAgentControlledSpace,+ releaseAgentControlledSpace,+} from "./agent-control-notice.js";import { runtimeValue } from "./cdp-eval.js";import { withTemporaryClipboardText } from "./clipboard.js";import {@@ -826,6 +830,7 @@ export async function initializeTaskSpaceHandle(await task.initializeCreatedSpace(initializeCreatedSpaceToken);}await task.initializeBackgroundPageDiscovery(initializeTaskSpaceToken);+ if (task.ownership === "agent") recordAgentControlledSpace(task.spaceId);}/** Close a newly created space whose Page-model initialization did not commit. */@@ -1020,6 +1025,7 @@ class TaskSpace {try {await this.#services.handOffTaskSpace();this.#stopBackgroundPageDiscovery();+ releaseAgentControlledSpace(this.spaceId);} catch (error) {await this.#services.ledger.cancelUserControl(this.spaceId);throw error;@@ -1113,6 +1119,7 @@ class TaskSpace {await this.#services.ledger.discard(this.spaceId);this.#stopBackgroundPageDiscovery();clearSpacePageNotices(this.spaceId);+ releaseAgentControlledSpace(this.spaceId);}/**