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.

feat(ego-browser): own error wording for known codes across runtime paths

0x03a427dca7db8
diff --git a/package/ego-browser/src/browser-runtime.test.mjs b/package/ego-browser/src/browser-runtime.test.mjs
new file mode 100644
index 0000000..cf9fa72
--- /dev/null
+++ b/package/ego-browser/src/browser-runtime.test.mjs
@@ -0,0 +1,62 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+
+import { browserCdp } from "../dist/src/browser-runtime.js";
+
+// Gap A: ensureSession() calls the raw listTabs binding to attach a session.
+// When the task is blocked it returns { error, error_code }; the result must
+// surface the ego-browser-owned wording for the code (not the live text) and
+// carry error_code, instead of throwing bare live text.
+test("browserCdp surfaces the owned message and error_code when ensureSession is blocked", async () => {
+ globalThis.ego = {
+ async listTabs() {
+ return {
+ error: "The task is under user control: 7",
+ error_code: "EGO_TASK_SPACE_USER_IN_CONTROL",
+ };
+ },
+ };
+ try {
+ await assert.rejects(
+ () => browserCdp("Runtime.evaluate", {}, undefined, 1000),
+ (err) => {
+ assert.equal(err.error_code, "EGO_TASK_SPACE_USER_IN_CONTROL");
+ assert.equal(err.message, "The task is under user control.");
+ return true;
+ },
+ );
+ } finally {
+ delete globalThis.ego;
+ }
+});
+
+// Gap B: a raw CDP send that fails locally is reported through
+// ego.onSendCDPMessageError, not as a CDP response. Without wiring the request
+// would hang until the 15s timeout; wired, it rejects immediately with the
+// owned message and error_code.
+test("browserCdp rejects the in-flight request via onSendCDPMessageError", async () => {
+ globalThis.ego = {
+ sendCDPMessage() {
+ // The binding delivers the failure asynchronously through the event loop.
+ queueMicrotask(() =>
+ globalThis.ego.onSendCDPMessageError(
+ "native reconstructed text",
+ "EGO_TASK_SPACE_USER_IN_CONTROL",
+ ),
+ );
+ },
+ };
+ try {
+ await assert.rejects(
+ // Browser-level method skips ensureSession; short timeout bounds a regression.
+ () => browserCdp("Browser.getVersion", {}, undefined, 1000),
+ (err) => {
+ assert.equal(err.error_code, "EGO_TASK_SPACE_USER_IN_CONTROL");
+ assert.equal(err.message, "The task is under user control.");
+ return true;
+ },
+ );
+ } finally {
+ delete globalThis.ego;
+ }
+});
diff --git a/package/ego-browser/src/browser-runtime.ts b/package/ego-browser/src/browser-runtime.ts
index afa1f89..cfa88a3 100644
--- a/package/ego-browser/src/browser-runtime.ts
+++ b/package/ego-browser/src/browser-runtime.ts
@@ -1,4 +1,5 @@
import { state } from "./state.js";
+import { assertNoEgoError, resolveEgoError } from "./ego-errors.js";
const RESPONSE_TIMEOUT_MS = 15000;
const SESSION_TTL_MS = 2000;
@@ -35,6 +36,7 @@ function rawCdp(
) {
const runtime = browserEgo();
runtime.onCDPMessage = handleMessage;
+ runtime.onSendCDPMessageError = handleSendError;
const id = nextMessageId++;
const payload = JSON.stringify({
id,
@@ -104,10 +106,7 @@ export async function ensureSession() {
}
state.sessionInflight = (async () => {
try {
- const result = await browserEgo().listTabs();
- if (result?.error) {
- throw new Error(result.error);
- }
+ const result = assertNoEgoError(await browserEgo().listTabs());
const tabs = result?.tabs || result?.targetInfos || [];
const preferred = state.preferredTargetId
? tabs.find((t) => t.targetId === state.preferredTargetId)
@@ -180,6 +179,25 @@ async function enablePageEvents(sessionId) {
}
}
+// Local send failures for ego.sendCDPMessage() arrive here (task inactive,
+// user-controlled, not selected/claimed, host gone) instead of as a CDP
+// 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.
+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 entries = [...pending.values()];
+ pending.clear();
+ for (const entry of entries) entry.reject(error);
+}
+
function handleMessage(message) {
let data;
try {
diff --git a/package/ego-browser/src/ego-errors.test.mjs b/package/ego-browser/src/ego-errors.test.mjs
index 417ade9..e9e8bbb 100644
--- a/package/ego-browser/src/ego-errors.test.mjs
+++ b/package/ego-browser/src/ego-errors.test.mjs
@@ -41,17 +41,30 @@ test("isEgoErrorCode narrows to known codes only", () => {
assert.equal(isEgoErrorCode(undefined), false);
});
-test("resolveEgoError prefers the live browser text", () => {
+test("resolveEgoError overrides live text with the owned wording for a known code", () => {
const resolved = resolveEgoError({
error: "Task space not found: 7",
error_code: "EGO_TASK_SPACE_NOT_FOUND",
});
assert.deepEqual(resolved, {
code: "EGO_TASK_SPACE_NOT_FOUND",
- message: "Task space not found: 7",
+ message: "Task space not found.",
});
});
+test("resolveEgoError keeps live text for an unknown future code", () => {
+ assert.deepEqual(
+ resolveEgoError({
+ error: "Some build-specific detail",
+ error_code: "EGO_FUTURE_CODE",
+ }),
+ {
+ code: "EGO_FUTURE_CODE",
+ message: "Some build-specific detail",
+ },
+ );
+});
+
test("resolveEgoError falls back to the ego-browser message for a bare code", () => {
assert.deepEqual(resolveEgoError("EGO_TASK_SPACE_USER_IN_CONTROL"), {
code: "EGO_TASK_SPACE_USER_IN_CONTROL",
@@ -90,7 +103,7 @@ test("isEgoUserControlError keys on the stable code, not wording", () => {
);
});
-test("assertNoEgoError preserves the message and attaches error_code", () => {
+test("assertNoEgoError resolves the message via the code and attaches error_code", () => {
try {
assertNoEgoError(
{
@@ -101,11 +114,24 @@ test("assertNoEgoError preserves the message and attaches error_code", () => {
);
assert.fail("expected assertNoEgoError to throw");
} catch (err) {
- assert.equal(err.message, "listTabs: The task is under user control");
+ assert.equal(err.message, "listTabs: The task is under user control.");
assert.equal(err.error_code, "EGO_TASK_SPACE_USER_IN_CONTROL");
}
});
+test("assertNoEgoError omits the prefix when no op is given", () => {
+ try {
+ assertNoEgoError({
+ error: "The task space is inactive: 10",
+ error_code: "EGO_TASK_SPACE_INACTIVE",
+ });
+ assert.fail("expected assertNoEgoError to throw");
+ } catch (err) {
+ assert.equal(err.message, "The task space is inactive.");
+ assert.equal(err.error_code, "EGO_TASK_SPACE_INACTIVE");
+ }
+});
+
test("assertNoEgoError passes through results with no error", () => {
const ok = { tabs: [] };
assert.equal(assertNoEgoError(ok, "listTabs"), ok);
diff --git a/package/ego-browser/src/ego-errors.ts b/package/ego-browser/src/ego-errors.ts
index 8812b4f..a3f872f 100644
--- a/package/ego-browser/src/ego-errors.ts
+++ b/package/ego-browser/src/ego-errors.ts
@@ -8,9 +8,9 @@
*
* The code is the durable contract; the wording can drift between builds. Branch
* on the code (isEgoUserControlError), not on the message. EGO_ERROR_MESSAGES is
- * the seam where ego-browser owns its own wording per code — today it only
- * supplies a fallback when the browser sent no text, but it is the place to
- * customize how each failure reads going forward.
+ * where ego-browser owns its own wording per code: for any code this build knows,
+ * its wording is authoritative and overrides the browser's live text. Unknown
+ * (future) codes fall back to the live text.
*
* Single source of truth — error handling was previously duplicated across
* helpers.ts and driver/nav.ts.
@@ -38,8 +38,9 @@ export const EGO_ERROR_CODES = [
export type EgoErrorCode = (typeof EGO_ERROR_CODES)[number];
/**
- * ego-browser-owned message for each stable code. Used only as a fallback when
- * the browser side supplied no human-readable text; customize wording here.
+ * ego-browser-owned message for each stable code. For codes this build knows,
+ * this wording is authoritative and overrides the browser's live text; customize
+ * wording here. Keep it free of dynamic values (no ids).
*/
const EGO_ERROR_MESSAGES: Record<EgoErrorCode, string> = {
EGO_BROWSER_UNAVAILABLE: "No active browser.",
@@ -87,9 +88,10 @@ export function egoErrorCode(err: unknown): string | undefined {
/**
* Resolve any ego error into a stable `{ code, message }` pair.
*
- * `message` prefers the live human-readable text the browser supplied, then the
- * ego-browser-owned message for the code, then the bare code, then a generic
- * string. `code` is the stable classifier and may be undefined.
+ * For a code this build knows, `message` is the ego-browser-owned wording for
+ * that code. Otherwise it falls back to the live human-readable text the browser
+ * supplied, then the bare code, then a generic string. `code` is the stable
+ * classifier and may be undefined.
*/
export function resolveEgoError(err: unknown): {
code?: string;
@@ -97,8 +99,8 @@ export function resolveEgoError(err: unknown): {
} {
const code = egoErrorCode(err);
const message =
- liveErrorText(err) ??
(isEgoErrorCode(code) ? EGO_ERROR_MESSAGES[code] : undefined) ??
+ liveErrorText(err) ??
code ??
"Unknown ego error";
return { code, message };
@@ -109,7 +111,7 @@ export function isEgoUserControlError(err: unknown): boolean {
return egoErrorCode(err) === "EGO_TASK_SPACE_USER_IN_CONTROL";
}
-export function assertNoEgoError(result, op: string) {
+export function assertNoEgoError(result, op?: string) {
if (
result &&
typeof result === "object" &&
@@ -118,7 +120,7 @@ export function assertNoEgoError(result, op: string) {
) {
const { code, message } = resolveEgoError(result);
const error: Error & { error_code?: string } = new Error(
- `${op}: ${message}`,
+ op ? `${op}: ${message}` : message,
);
if (code) error.error_code = code;
throw error;