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 user-owned task space name as in use

Zhuowei Wangc19bd78b993d
diff --git a/package/ego-browser/skill-body/SKILL.md b/package/ego-browser/skill-body/SKILL.md
index 9713faa..590630d 100644
--- a/package/ego-browser/skill-body/SKILL.md
+++ b/package/ego-browser/skill-body/SKILL.md
@@ -120,6 +120,11 @@ and diagnose the CLI or installation only if it fails.
- Use exactly one TaskSpace for the entire user goal. Create it once, print its
`spaceId`, and resume that same space in later rounds. Use multiple spaces
only when the user explicitly requests them.
+- Task space names are unique. `taskSpace(name)` resumes your own space with
+ that name instead of creating a second one. If the name belongs to a
+ user-owned space, it fails with `EGO_TASK_SPACE_NAME_IN_USE`: ask the user
+ whether to work in that space (then `claimTaskSpace(id)`), or pick another
+ name.
- Never use a new TaskSpace to recover from a stuck, blocked, timed-out, or
unexpected Page. Recover within the existing space; if it cannot continue,
stop and ask the user.
diff --git a/package/ego-browser/src/ego-errors.ts b/package/ego-browser/src/ego-errors.ts
index b93d14b..e1d5692 100644
--- a/package/ego-browser/src/ego-errors.ts
+++ b/package/ego-browser/src/ego-errors.ts
@@ -29,6 +29,7 @@ export const EGO_ERROR_CODES = [
"EGO_SNAPSHOT_FAILED",
"EGO_TASK_HOST_DISCONNECTED",
"EGO_TASK_SPACE_INACTIVE",
+ "EGO_TASK_SPACE_NAME_IN_USE",
"EGO_TASK_SPACE_NOT_FOUND",
"EGO_TASK_SPACE_NOT_SELECTED",
"EGO_TASK_SPACE_UNAVAILABLE",
diff --git a/package/ego-browser/src/helpers.test.mjs b/package/ego-browser/src/helpers.test.mjs
index c524559..4cb89f6 100644
--- a/package/ego-browser/src/helpers.test.mjs
+++ b/package/ego-browser/src/helpers.test.mjs
@@ -740,27 +740,81 @@ test("useOrCreateTaskSpace reuses existing agent-owned spaces", async () => {
assert.deepEqual(calls, [["listTaskSpaces"], ["useTaskSpace", 7]]);
});
-test("useOrCreateTaskSpace selects user-owned spaces without claiming and surfaces the owned user-control guidance", async () => {
+function userOwnedCheckoutEgo(calls, extra = {}) {
+ return {
+ async listTaskSpaces() {
+ calls.push(["listTaskSpaces"]);
+ return {
+ taskSpaces: [
+ {
+ taskId: "checkout-flow",
+ id: 7,
+ name: "checkout-flow",
+ ownership: "user",
+ },
+ ],
+ };
+ },
+ async createTaskSpace(name) {
+ calls.push(["createTaskSpace", name]);
+ return { taskId: name, id: 8, name, ownership: "agent" };
+ },
+ async claimTaskSpace(id, name) {
+ calls.push(["claimTaskSpace", id, name]);
+ return { taskId: name, id, name, ownership: "agent" };
+ },
+ ...extra,
+ };
+}
+
+test("useOrCreateTaskSpace reports a user-owned name as EGO_TASK_SPACE_NAME_IN_USE", async () => {
const calls = [];
await withEgo(
- {
- async listTaskSpaces() {
- calls.push(["listTaskSpaces"]);
- return {
- taskSpaces: [
- {
- taskId: "checkout-flow",
- id: 7,
- name: "checkout-flow",
- ownership: "user",
- },
- ],
- };
+ userOwnedCheckoutEgo(calls, {
+ useTaskSpace(taskId) {
+ calls.push(["useTaskSpace", taskId]);
+ return taskId;
},
- async claimTaskSpace(id, name) {
- calls.push(["claimTaskSpace", id, name]);
- return { taskId: name, id, name, ownership: "agent" };
+ }),
+ async () => {
+ await assert.rejects(
+ () => useOrCreateTaskSpace("checkout-flow"),
+ (error) => {
+ assert.equal(error.error_code, "EGO_TASK_SPACE_NAME_IN_USE");
+ assert.match(
+ error.message,
+ /^useOrCreateTaskSpace: A task space named "checkout-flow" already exists and belongs to the user \(id 7\)/,
+ );
+ assert.match(error.message, /claimTaskSpace\(7\)/);
+ assert.doesNotMatch(error.message, /taken control/);
+ return true;
+ },
+ );
+ },
+ );
+ // Neither selected, claimed, nor duplicated.
+ assert.deepEqual(calls, [["listTaskSpaces"]]);
+});
+
+test("taskSpace reports a user-owned name as EGO_TASK_SPACE_NAME_IN_USE", async () => {
+ const calls = [];
+ await withEgo(userOwnedCheckoutEgo(calls), async () => {
+ await assert.rejects(
+ () => taskSpace("checkout-flow"),
+ (error) => {
+ assert.equal(error.error_code, "EGO_TASK_SPACE_NAME_IN_USE");
+ assert.match(error.message, /^taskSpace: A task space named/);
+ return true;
},
+ );
+ });
+ assert.deepEqual(calls, [["listTaskSpaces"]]);
+});
+
+test("useOrCreateTaskSpace selects a user-owned space targeted by id without claiming and surfaces the owned user-control guidance", async () => {
+ const calls = [];
+ await withEgo(
+ userOwnedCheckoutEgo(calls, {
useTaskSpace(taskId) {
calls.push(["useTaskSpace", taskId]);
// Native attaches the stable code; resolveEgoError overrides the live
@@ -770,10 +824,10 @@ test("useOrCreateTaskSpace selects user-owned spaces without claiming and surfac
error_code: "EGO_TASK_SPACE_USER_IN_CONTROL",
};
},
- },
+ }),
async () => {
await assert.rejects(
- () => useOrCreateTaskSpace("checkout-flow"),
+ () => useOrCreateTaskSpace(7),
/useOrCreateTaskSpace: The user has taken control of this task space/,
);
},
diff --git a/package/ego-browser/src/helpers.ts b/package/ego-browser/src/helpers.ts
index 0d3c3b7..fa3345e 100644
--- a/package/ego-browser/src/helpers.ts
+++ b/package/ego-browser/src/helpers.ts
@@ -3,7 +3,7 @@ import { join } from "node:path";
import { pathToFileURL } from "node:url";
import { setOverrides, state } from "./state.js";
-import { invokeEgo, probeAgentControl } from "./ego-errors.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";
import { createStaleEgoBrowserGuard } from "./skill-migration.js";
@@ -127,6 +127,8 @@ export async function profiles() {
* bridge when real commands run, not here. The rows below describe what each helper
* does when the target space is user-owned:
*
+ * taskSpace / useOrCreateTaskSpace -> by name: throws EGO_TASK_SPACE_NAME_IN_USE;
+ * by id: selects it unclaimed (user-control error surfaces)
* switchTaskSpace -> throws (agent-owned only)
* claimTaskSpace -> claims it (ownership transfers to the agent), then selects it
* handOffTaskSpace -> skipped, resolves { done: false, skipped: "user-owned" }
@@ -218,9 +220,11 @@ async function createTaskSpaceResolution(
}
/**
- * Use an existing agent-owned task space, or create it when missing. User-owned
- * spaces are selected but not claimed (the EGO_TASK_SPACE_USER_IN_CONTROL error
- * surfaces) — call claimTaskSpace(nameOrId) to take ownership.
+ * Use an existing agent-owned task space, or create it when missing. A name held
+ * by a user-owned space fails with EGO_TASK_SPACE_NAME_IN_USE; a user-owned space
+ * passed by numeric id is selected but not claimed (the
+ * EGO_TASK_SPACE_USER_IN_CONTROL error surfaces). Call claimTaskSpace(id) to take
+ * ownership once the user asks.
* @param {string|number} nameOrId Task space name or numeric id.
* @returns {Promise<{taskId:string,id:number,name:string,createdBy?:string,ownership?:string,recentTabTitles?:string[]}>}
*/
@@ -229,7 +233,11 @@ export async function useOrCreateTaskSpace(nameOrId) {
return (await resolveTaskSpace(nameOrId)).descriptor;
}
-async function resolveTaskSpace(nameOrId, options: { select?: boolean } = {}) {
+async function resolveTaskSpace(
+ nameOrId,
+ options: { select?: boolean; op?: string } = {},
+) {
+ const op = options.op ?? "useOrCreateTaskSpace";
const spaces = await listTaskSpaces();
const existing = findMatchingTaskSpace(spaces, nameOrId);
if (!existing) {
@@ -254,10 +262,25 @@ async function resolveTaskSpace(nameOrId, options: { select?: boolean } = {}) {
};
}
if (existing.ownership === "user") {
- // Don't claim user-owned spaces here. Select it as-is; the user stays in
- // control, so EGO_TASK_SPACE_USER_IN_CONTROL surfaces (as ego-browser's owned
- // guidance, not the raw native text). Call claimTaskSpace(nameOrId) to take
- // ownership.
+ // A name match on a user-owned space is a name collision, not the agent's
+ // task: the user is not "in control" of something the agent started. Report
+ // it the way native createTaskSpace reports a duplicate name.
+ if (typeof nameOrId === "string" && existing.name === nameOrId) {
+ throw buildEgoError(
+ {
+ error: [
+ `A task space named ${JSON.stringify(nameOrId)} already exists and belongs to the user (id ${existing.id}).`,
+ `Do not take it over on your own. Ask the user whether to work in it; only if they agree, call claimTaskSpace(${existing.id}).`,
+ "Otherwise use a different task space name.",
+ ].join("\n"),
+ error_code: "EGO_TASK_SPACE_NAME_IN_USE",
+ },
+ op,
+ );
+ }
+ // Explicitly targeted by id: don't claim it here. Select it as-is; the user
+ // stays in control, so EGO_TASK_SPACE_USER_IN_CONTROL surfaces (as
+ // ego-browser's owned guidance, not the raw native text).
return {
descriptor:
options.select === false
@@ -291,7 +314,7 @@ export async function taskSpace(
const { profileId } = options;
if (profileId === undefined) {
return initializeResolvedTaskSpace(
- await resolveTaskSpace(nameOrId, { select: false }),
+ await resolveTaskSpace(nameOrId, { select: false, op: "taskSpace" }),
);
}
if (typeof nameOrId !== "string") {
diff --git a/package/ego-browser/src/public-api-schema.ts b/package/ego-browser/src/public-api-schema.ts
index ac0c90d..f353b48 100644
--- a/package/ego-browser/src/public-api-schema.ts
+++ b/package/ego-browser/src/public-api-schema.ts
@@ -71,7 +71,7 @@ export const PUBLIC_API_SCHEMA: readonly PublicApiEntry[] = [
name: "taskSpace",
signature: "await taskSpace(nameOrId, { profileId? })",
summary:
- "Reuse or create an Agent-owned task space; a new space starts with managed Page p1, and profileId applies only when creating it.",
+ "Reuse or create an Agent-owned task space; a name held by a user-owned space fails with EGO_TASK_SPACE_NAME_IN_USE. A new space starts with managed Page p1, and profileId applies only when creating it.",
options: {
profileId: option(
"nonEmptyString",
diff --git a/package/ego-browser/src/taskspace-e2e.test.mjs b/package/ego-browser/src/taskspace-e2e.test.mjs
index 872dd40..826cf57 100644
--- a/package/ego-browser/src/taskspace-e2e.test.mjs
+++ b/package/ego-browser/src/taskspace-e2e.test.mjs
@@ -220,7 +220,7 @@ test("taskspace e2e claims and selects an existing user-owned task space", async
]);
});
-test("taskspace e2e useOrCreateTaskSpace selects user-owned spaces without claiming and surfaces the owned user-control guidance", async () => {
+test("taskspace e2e useOrCreateTaskSpace reports a user-owned name as in use without selecting or claiming it", async () => {
const ego = new FakeEgo([
{
taskId: "checkout-flow",
@@ -231,11 +231,29 @@ test("taskspace e2e useOrCreateTaskSpace selects user-owned spaces without claim
},
]);
- // Native rejects with error_code EGO_TASK_SPACE_USER_IN_CONTROL, so the agent
- // sees ego-browser's owned guidance block, not the raw native text.
await assert.rejects(
() =>
runTaskspaceScript(ego, `await useOrCreateTaskSpace("checkout-flow")`),
+ /already exists and belongs to the user \(id 7\)/,
+ );
+ assert.deepEqual(ego.calls, [["listTaskSpaces"]]);
+});
+
+test("taskspace e2e useOrCreateTaskSpace selects a user-owned space targeted by id without claiming and surfaces the owned user-control guidance", async () => {
+ const ego = new FakeEgo([
+ {
+ taskId: "checkout-flow",
+ id: 7,
+ name: "checkout-flow",
+ createdBy: "user",
+ ownership: "user",
+ },
+ ]);
+
+ // Native rejects with error_code EGO_TASK_SPACE_USER_IN_CONTROL, so the agent
+ // sees ego-browser's owned guidance block, not the raw native text.
+ await assert.rejects(
+ () => runTaskspaceScript(ego, `await useOrCreateTaskSpace(7)`),
/has taken control of this task space/,
);
assert.deepEqual(ego.calls, [["listTaskSpaces"], ["useTaskSpace", 7]]);