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.

refactor(ego-browser): share buildEgoError and fix user-control test fidelity

0x0cd2399e42312
diff --git a/package/ego-browser/src/browser-runtime.ts b/package/ego-browser/src/browser-runtime.ts
index cfa88a3..c9d17f4 100644
--- a/package/ego-browser/src/browser-runtime.ts
+++ b/package/ego-browser/src/browser-runtime.ts
@@ -1,5 +1,5 @@
import { state } from "./state.js";
-import { assertNoEgoError, resolveEgoError } from "./ego-errors.js";
+import { assertNoEgoError, buildEgoError } from "./ego-errors.js";
const RESPONSE_TIMEOUT_MS = 15000;
const SESSION_TTL_MS = 2000;
@@ -184,15 +184,10 @@ async function enablePageEvents(sessionId) {
// response, so the matching request would otherwise sit until the 15s timeout.
// The callback carries no request id; these failures are task-level (every
// in-flight send fails the same way), so reject all pending requests, routing
-// the stable code through resolveEgoError to use the ego-browser-owned wording.
+// the stable code through buildEgoError to use the ego-browser-owned wording.
function handleSendError(message, error_code) {
if (pending.size === 0) return;
- const { code, message: resolved } = resolveEgoError({
- error: message,
- error_code,
- });
- const error: Error & { error_code?: string } = new Error(resolved);
- if (code) error.error_code = code;
+ const error = buildEgoError({ error: message, error_code });
const entries = [...pending.values()];
pending.clear();
for (const entry of entries) entry.reject(error);
diff --git a/package/ego-browser/src/ego-errors.ts b/package/ego-browser/src/ego-errors.ts
index cb68916..4ce675c 100644
--- a/package/ego-browser/src/ego-errors.ts
+++ b/package/ego-browser/src/ego-errors.ts
@@ -107,6 +107,25 @@ export function isEgoUserControlError(err: unknown): boolean {
return egoErrorCode(err) === "EGO_TASK_SPACE_USER_IN_CONTROL";
}
+/**
+ * Build an Error carrying the resolved message and stable error_code from any ego
+ * error shape. `op`, when given, prefixes the message with the failing operation.
+ * Shared by assertNoEgoError (which throws it) and the CDP-send failure path (which
+ * rejects pending requests with it) so every ego failure surfaces an identical
+ * Error shape.
+ */
+export function buildEgoError(
+ err: unknown,
+ op?: string,
+): Error & { error_code?: string } {
+ const { code, message } = resolveEgoError(err);
+ const error: Error & { error_code?: string } = new Error(
+ op ? `${op}: ${message}` : message,
+ );
+ if (code) error.error_code = code;
+ return error;
+}
+
export function assertNoEgoError(result, op?: string) {
if (
result &&
@@ -114,12 +133,7 @@ export function assertNoEgoError(result, op?: string) {
"error" in result &&
result.error != null
) {
- const { code, message } = resolveEgoError(result);
- const error: Error & { error_code?: string } = new Error(
- op ? `${op}: ${message}` : message,
- );
- if (code) error.error_code = code;
- throw error;
+ throw buildEgoError(result, op);
}
return result;
}
diff --git a/package/ego-browser/src/helpers.test.mjs b/package/ego-browser/src/helpers.test.mjs
index a84c715..08350b7 100644
--- a/package/ego-browser/src/helpers.test.mjs
+++ b/package/ego-browser/src/helpers.test.mjs
@@ -319,7 +319,7 @@ 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 live native error", async () => {
+test("useOrCreateTaskSpace selects user-owned spaces without claiming and surfaces the owned user-control guidance", async () => {
const calls = [];
await withEgo(
{
@@ -342,13 +342,18 @@ test("useOrCreateTaskSpace selects user-owned spaces without claiming and surfac
},
useTaskSpace(taskId) {
calls.push(["useTaskSpace", taskId]);
- return { error: "The task is under user control" };
+ // Native attaches the stable code; resolveEgoError overrides the live
+ // text with ego-browser's owned EGO_TASK_SPACE_USER_IN_CONTROL guidance.
+ return {
+ error: "The task is under user control",
+ error_code: "EGO_TASK_SPACE_USER_IN_CONTROL",
+ };
},
},
async () => {
await assert.rejects(
() => useOrCreateTaskSpace("checkout-flow"),
- /useOrCreateTaskSpace: The task is under user control/,
+ /useOrCreateTaskSpace: The user is controlling this task space/,
);
},
);
diff --git a/package/ego-browser/src/helpers.ts b/package/ego-browser/src/helpers.ts
index 94f372d..212548a 100644
--- a/package/ego-browser/src/helpers.ts
+++ b/package/ego-browser/src/helpers.ts
@@ -153,8 +153,8 @@ export async function newTaskSpace(name) {
/**
* Use an existing agent-owned task space, or create it when missing. User-owned
- * spaces are selected but not claimed (the native bridge surfaces its live
- * user-control error) — call claimTaskSpace(nameOrId) to take ownership.
+ * spaces are selected but not claimed (the EGO_TASK_SPACE_USER_IN_CONTROL error
+ * surfaces) — call claimTaskSpace(nameOrId) to take ownership.
* @param {string|number} nameOrId Task space name or numeric id.
* @returns {Promise<{taskId:string,id:number,name:string,createdBy?:string,ownership?:string,recentTabTitles?:string[]}>}
*/
@@ -171,9 +171,10 @@ export async function useOrCreateTaskSpace(nameOrId) {
return selectTaskSpace(globalThis.ego, existing, "useOrCreateTaskSpace");
}
if (existing.ownership === "user") {
- // Don't claim user-owned spaces here. Select it and let the native bridge
- // surface its live user-control error verbatim; use claimTaskSpace(nameOrId)
- // to take ownership.
+ // 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.
return selectTaskSpace(globalThis.ego, existing, "useOrCreateTaskSpace");
}
throw new Error(
diff --git a/package/ego-browser/src/taskspace-e2e.test.mjs b/package/ego-browser/src/taskspace-e2e.test.mjs
index 825b2af..14cdf30 100644
--- a/package/ego-browser/src/taskspace-e2e.test.mjs
+++ b/package/ego-browser/src/taskspace-e2e.test.mjs
@@ -29,7 +29,10 @@ class FakeEgo {
this.calls.push(["useTaskSpace", id]);
const space = this.taskSpaces.find((candidate) => candidate.id === id);
if (space && space.ownership === "user") {
- return { error: "The task is under user control" };
+ return {
+ error: "The task is under user control",
+ error_code: "EGO_TASK_SPACE_USER_IN_CONTROL",
+ };
}
this.selectedId = id;
return id;
@@ -211,7 +214,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 live native error", async () => {
+test("taskspace e2e useOrCreateTaskSpace selects user-owned spaces without claiming and surfaces the owned user-control guidance", async () => {
const ego = new FakeEgo([
{
taskId: "checkout-flow",
@@ -222,10 +225,12 @@ 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")`),
- /The task is under user control/,
+ /controlling this task space/,
);
assert.deepEqual(ego.calls, [["listTaskSpaces"], ["useTaskSpace", 7]]);
});