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 task space selection helper flow

Tachikoma86a49ae7b148
diff --git a/package/ego-browser/src/helpers.test.mjs b/package/ego-browser/src/helpers.test.mjs
index ed6d8ac..e5c61bf 100644
--- a/package/ego-browser/src/helpers.test.mjs
+++ b/package/ego-browser/src/helpers.test.mjs
@@ -3,6 +3,7 @@ import assert from "node:assert/strict";
import {
completeTaskSpace,
+ handOffTaskSpace,
newTaskSpace,
helperContext,
listTaskSpaces,
@@ -132,6 +133,26 @@ test("switchTaskSpace rejects non-agent-owned task spaces", async () => {
});
});
+test("switchTaskSpace awaits useTaskSpace binding errors", async () => {
+ await withEgo({
+ async listTaskSpaces() {
+ return {
+ taskSpaces: [
+ { taskId: "checkout-flow", id: 7, name: "checkout-flow", ownership: "agent" }
+ ]
+ };
+ },
+ async useTaskSpace() {
+ return { error: "Task space not selected" };
+ }
+ }, async () => {
+ await assert.rejects(
+ () => switchTaskSpace("checkout-flow"),
+ /switchTaskSpace: Task space not selected/
+ );
+ });
+});
+
test("newTaskSpace creates and selects an agent task space", async () => {
const calls = [];
await withEgo({
@@ -378,6 +399,115 @@ test("completeTaskSpace selects by numeric id before completing", async () => {
]);
});
+test("completeTaskSpace waits for async useTaskSpace before completing", async () => {
+ const calls = [];
+ await withEgo({
+ async listTaskSpaces() {
+ calls.push(["listTaskSpaces"]);
+ return {
+ taskSpaces: [
+ { taskId: "checkout-flow", id: 7, name: "checkout-flow", ownership: "agent" }
+ ]
+ };
+ },
+ async useTaskSpace(id) {
+ calls.push(["useTaskSpace:start", id]);
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ calls.push(["useTaskSpace:end", id]);
+ },
+ async completeTaskSpace() {
+ calls.push(["completeTaskSpace"]);
+ return "7 task space completed.";
+ }
+ }, async () => {
+ await completeTaskSpace("checkout-flow", { keep: true });
+ });
+ assert.deepEqual(calls, [
+ ["listTaskSpaces"],
+ ["useTaskSpace:start", 7],
+ ["useTaskSpace:end", 7],
+ ["completeTaskSpace"]
+ ]);
+});
+
+test("completeTaskSpace claims user-owned spaces before closing", async () => {
+ const calls = [];
+ await withEgo({
+ async listTaskSpaces() {
+ calls.push(["listTaskSpaces"]);
+ return {
+ taskSpaces: [
+ { taskId: "checkout-flow", id: 7, name: "checkout-flow", ownership: "user" }
+ ]
+ };
+ },
+ async claimTaskSpace(id, name) {
+ calls.push(["claimTaskSpace", id, name]);
+ return { taskId: name, id, name, ownership: "agent" };
+ },
+ async useTaskSpace(id) {
+ calls.push(["useTaskSpace", id]);
+ return id;
+ },
+ async closeTaskSpace() {
+ calls.push(["closeTaskSpace"]);
+ return "7 task space closed.";
+ }
+ }, async () => {
+ await completeTaskSpace("checkout-flow", { keep: false });
+ });
+ assert.deepEqual(calls, [
+ ["listTaskSpaces"],
+ ["claimTaskSpace", 7, "checkout-flow"],
+ ["useTaskSpace", 7],
+ ["closeTaskSpace"]
+ ]);
+});
+
+test("completeTaskSpace keep true is a no-op for user-owned spaces", async () => {
+ const calls = [];
+ await withEgo({
+ async listTaskSpaces() {
+ calls.push(["listTaskSpaces"]);
+ return {
+ taskSpaces: [
+ { taskId: "checkout-flow", id: 7, name: "checkout-flow", ownership: "user" }
+ ]
+ };
+ },
+ async completeTaskSpace() {
+ calls.push(["completeTaskSpace"]);
+ }
+ }, async () => {
+ await completeTaskSpace("checkout-flow", { keep: true });
+ });
+ assert.deepEqual(calls, [
+ ["listTaskSpaces"]
+ ]);
+});
+
+test("handOffTaskSpace is a no-op for user-owned spaces", async () => {
+ const calls = [];
+ await withEgo({
+ async listTaskSpaces() {
+ calls.push(["listTaskSpaces"]);
+ return {
+ taskSpaces: [
+ { taskId: "checkout-flow", id: 7, name: "checkout-flow", ownership: "user" }
+ ]
+ };
+ },
+ async handOffTaskSpace() {
+ calls.push(["handOffTaskSpace"]);
+ }
+ }, async () => {
+ await handOffTaskSpace("checkout-flow");
+ });
+ assert.deepEqual(calls, [
+ ["listTaskSpaces"]
+ ]);
+});
+
test("useOrCreateTaskSpace rejects unknown ownership", async () => {
await withEgo({
async listTaskSpaces() {
diff --git a/package/ego-browser/src/helpers.ts b/package/ego-browser/src/helpers.ts
index d044b7a..369df5f 100644
--- a/package/ego-browser/src/helpers.ts
+++ b/package/ego-browser/src/helpers.ts
@@ -106,37 +106,37 @@ export async function useOrCreateTaskSpace(nameOrId) {
return selectTaskSpace(globalThis.ego, existing, "useOrCreateTaskSpace");
}
if (existing.ownership === "user") {
- return claimTaskSpace(existing);
+ return claimTaskSpace(existing, "useOrCreateTaskSpace");
}
throw new Error(`useOrCreateTaskSpace cannot use task space ${JSON.stringify(nameOrId)} with ownership ${JSON.stringify(existing.ownership)}`);
}
-async function claimTaskSpace(space) {
+async function claimTaskSpace(space, op = "claimTaskSpace") {
const ego = globalThis.ego;
if (!ego || typeof ego.claimTaskSpace !== "function") {
- throw new Error("useOrCreateTaskSpace requires ego.claimTaskSpace");
+ throw new Error(`${op} requires ego.claimTaskSpace`);
}
- const id = taskSpaceNumericId(space, "claimTaskSpace");
- const claimed = normalizeTaskSpace(assertNoEgoError(await ego.claimTaskSpace(id, space.name), "claimTaskSpace"));
+ const id = taskSpaceNumericId(space, op);
+ const claimed = normalizeTaskSpace(assertNoEgoError(await ego.claimTaskSpace(id, space.name), op));
if (!claimed) {
- throw new Error("claimTaskSpace returned an invalid task space");
+ throw new Error(`${op} returned an invalid task space`);
}
- taskSpaceNumericId(claimed, "claimTaskSpace");
- return selectTaskSpace(ego, claimed, "claimTaskSpace");
+ taskSpaceNumericId(claimed, op);
+ return selectTaskSpace(ego, claimed, op);
}
-function selectTaskSpace(ego, space, op: string) {
+async function selectTaskSpace(ego, space, op: string) {
if (!ego || typeof ego.useTaskSpace !== "function") {
throw new Error(`${op} requires ego.useTaskSpace`);
}
- ego.useTaskSpace(taskSpaceNumericId(space, op));
+ assertNoEgoError(await ego.useTaskSpace(taskSpaceNumericId(space, op)), op);
return space;
}
async function selectTaskSpaceIfProvided(ego, nameOrId?: string | number, op = "taskSpace") {
if (nameOrId === undefined) return;
const match = await findTaskSpace(nameOrId);
- ego.useTaskSpace(taskSpaceNumericId(match, op));
+ await selectTaskSpace(ego, match, op);
}
/**
@@ -163,13 +163,19 @@ export async function completeTaskSpace(nameOrId: string | number, options: { ke
if (!match) {
throw new Error(`task space not found: ${nameOrId}`);
}
- ego.useTaskSpace(taskSpaceNumericId(match, "completeTaskSpace"));
if (options.keep) {
+ if (match.ownership === "user") return;
+ await selectTaskSpace(ego, match, "completeTaskSpace");
if (typeof ego.completeTaskSpace !== "function") {
throw new Error("completeTaskSpace requires ego.completeTaskSpace");
}
assertNoEgoError(await ego.completeTaskSpace(), "completeTaskSpace");
} else {
+ if (match.ownership === "user") {
+ await claimTaskSpace(match, "completeTaskSpace");
+ } else {
+ await selectTaskSpace(ego, match, "completeTaskSpace");
+ }
if (typeof ego.closeTaskSpace !== "function") {
throw new Error("completeTaskSpace requires ego.closeTaskSpace");
}
@@ -187,7 +193,11 @@ export async function handOffTaskSpace(nameOrId?: string | number) {
if (!ego || typeof ego.handOffTaskSpace !== "function") {
throw new Error("handOffTaskSpace requires ego.handOffTaskSpace");
}
- await selectTaskSpaceIfProvided(ego, nameOrId, "handOffTaskSpace");
+ if (nameOrId !== undefined) {
+ const match = await findTaskSpace(nameOrId);
+ if (match.ownership === "user") return;
+ await selectTaskSpace(ego, match, "handOffTaskSpace");
+ }
assertNoEgoError(await ego.handOffTaskSpace(), "handOffTaskSpace");
}