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
add serialized native operation gate
Aute28201011e932
diff --git a/package/ego-browser/src/native-gate.test.mjs b/package/ego-browser/src/native-gate.test.mjsnew file mode 100644index 0000000..23c5e6d--- /dev/null+++ b/package/ego-browser/src/native-gate.test.mjs@@ -0,0 +1,129 @@+import test from "node:test";+import assert from "node:assert/strict";++import { NativeOperationGate } from "../dist/src/native-gate.js";++function deferred() {+ let resolve;+ const promise = new Promise((done) => {+ resolve = done;+ });+ return { promise, resolve };+}++function turn() {+ return new Promise((resolve) => setImmediate(resolve));+}++test("NativeOperationGate holds the selected space for the full operation", async () => {+ const firstMayFinish = deferred();+ const log = [];+ let selectedSpace = null;+ let activeOperations = 0;+ let maxActiveOperations = 0;+ const gate = new NativeOperationGate({+ async selectSpace(spaceId) {+ selectedSpace = spaceId;+ log.push(`select:${spaceId}`);+ },+ async ensureSession() {+ throw new Error("not used");+ },+ });++ const first = gate.withSpace(1, async ({ spaceId }) => {+ activeOperations += 1;+ maxActiveOperations = Math.max(maxActiveOperations, activeOperations);+ log.push(`start:${spaceId}`);+ assert.equal(selectedSpace, 1);+ await firstMayFinish.promise;+ assert.equal(+ selectedSpace,+ 1,+ "space must not change while work is pending",+ );+ log.push(`end:${spaceId}`);+ activeOperations -= 1;+ return "first";+ });+ const second = gate.withSpace(2, async ({ spaceId }) => {+ activeOperations += 1;+ maxActiveOperations = Math.max(maxActiveOperations, activeOperations);+ log.push(`start:${spaceId}`);+ assert.equal(selectedSpace, 2);+ log.push(`end:${spaceId}`);+ activeOperations -= 1;+ return "second";+ });++ await turn();+ assert.deepEqual(log, ["select:1", "start:1"]);+ firstMayFinish.resolve();++ assert.deepEqual(await Promise.all([first, second]), ["first", "second"]);+ assert.equal(maxActiveOperations, 1);+ assert.deepEqual(log, [+ "select:1",+ "start:1",+ "end:1",+ "select:2",+ "start:2",+ "end:2",+ ]);+});++test("NativeOperationGate releases the queue after an operation fails", async () => {+ const selected = [];+ const gate = new NativeOperationGate({+ async selectSpace(spaceId) {+ selected.push(spaceId);+ },+ async ensureSession() {+ throw new Error("not used");+ },+ });++ const failed = gate.withSpace(1, async () => {+ throw new Error("operation failed");+ });+ const recovered = gate.withSpace(2, async () => "recovered");++ await assert.rejects(failed, /operation failed/);+ assert.equal(await recovered, "recovered");+ assert.deepEqual(selected, [1, 2]);+});++test("NativeOperationGate resolves an explicit page session inside the space lock", async () => {+ const log = [];+ let selectedSpace = null;+ const gate = new NativeOperationGate({+ async selectSpace(spaceId) {+ selectedSpace = spaceId;+ log.push(`select:${spaceId}`);+ },+ async ensureSession(targetId) {+ assert.equal(+ selectedSpace,+ 7,+ "session attach must run after space selection",+ );+ log.push(`attach:${targetId}`);+ return "session-a";+ },+ });++ const result = await gate.withPage(+ { spaceId: 7, targetId: "target-a" },+ async (page) => {+ log.push(`run:${page.sessionId}`);+ return page;+ },+ );++ assert.deepEqual(result, {+ spaceId: 7,+ targetId: "target-a",+ sessionId: "session-a",+ });+ assert.deepEqual(log, ["select:7", "attach:target-a", "run:session-a"]);+});diff --git a/package/ego-browser/src/native-gate.ts b/package/ego-browser/src/native-gate.tsnew file mode 100644index 0000000..a641035--- /dev/null+++ b/package/ego-browser/src/native-gate.ts@@ -0,0 +1,120 @@+import { browserEgo, ensureSession } from "./browser-runtime.js";+import { assertNoEgoError } from "./ego-errors.js";++type SpaceScope = {+ spaceId: number;+};++export type PageExecutionContext = SpaceScope & {+ targetId: string;+ sessionId: string;+};++type NativeGateServices = {+ selectSpace(spaceId: number): unknown | Promise<unknown>;+ ensureSession(targetId: string): Promise<string>;+};++type PageTarget = {+ spaceId: number;+ targetId: string;+};++/**+ * Serializes native operations that rely on Ego Lite's process-wide selected+ * task space. The queue covers the entire async operation, so another caller+ * cannot change the selected space while a request is still in flight.+ */+export class NativeOperationGate {+ readonly #services: NativeGateServices;+ #tail: Promise<void> = Promise.resolve();++ constructor(services: NativeGateServices) {+ if (!services || typeof services.selectSpace !== "function") {+ throw new TypeError("NativeOperationGate requires selectSpace");+ }+ if (typeof services.ensureSession !== "function") {+ throw new TypeError("NativeOperationGate requires ensureSession");+ }+ this.#services = services;+ }++ withSpace<T>(+ spaceId: number,+ operation: (scope: SpaceScope) => T | Promise<T>,+ ): Promise<T> {+ assertSpaceId(spaceId);+ if (typeof operation !== "function") {+ throw new TypeError("withSpace requires an operation function");+ }++ const run = async () => {+ await this.#services.selectSpace(spaceId);+ return operation({ spaceId });+ };+ const result = this.#tail.then(run);+ // A failed operation must reject its own caller without poisoning the FIFO+ // queue for every operation submitted after it.+ this.#tail = result.then(+ () => undefined,+ () => undefined,+ );+ return result;+ }++ withPage<T>(+ page: PageTarget,+ operation: (context: PageExecutionContext) => T | Promise<T>,+ ): Promise<T> {+ if (!page || typeof page !== "object") {+ throw new TypeError("withPage requires a page target");+ }+ assertSpaceId(page.spaceId);+ if (typeof page.targetId !== "string" || page.targetId.length === 0) {+ throw new TypeError("withPage requires a non-empty targetId");+ }+ if (typeof operation !== "function") {+ throw new TypeError("withPage requires an operation function");+ }++ return this.withSpace(page.spaceId, async () => {+ const sessionId = await this.#services.ensureSession(page.targetId);+ return operation({+ spaceId: page.spaceId,+ targetId: page.targetId,+ sessionId,+ });+ });+ }+}++function assertSpaceId(spaceId: number): void {+ if (!Number.isInteger(spaceId) || spaceId < 0) {+ throw new TypeError("spaceId must be a non-negative integer");+ }+}++const defaultGate = new NativeOperationGate({+ async selectSpace(spaceId) {+ const ego = browserEgo();+ if (typeof ego.useTaskSpace !== "function") {+ throw new Error("withSpace requires ego.useTaskSpace");+ }+ assertNoEgoError(await ego.useTaskSpace(spaceId), "withSpace");+ },+ ensureSession,+});++export function withSpace<T>(+ spaceId: number,+ operation: (scope: SpaceScope) => T | Promise<T>,+): Promise<T> {+ return defaultGate.withSpace(spaceId, operation);+}++export function withPage<T>(+ page: PageTarget,+ operation: (context: PageExecutionContext) => T | Promise<T>,+): Promise<T> {+ return defaultGate.withPage(page, operation);+}