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): isolate default screenshot paths per run

imleonef023b109b8e
diff --git a/package/ego-browser/src/driver/observe.test.mjs b/package/ego-browser/src/driver/observe.test.mjs
index fa1d7bf..244a494 100644
--- a/package/ego-browser/src/driver/observe.test.mjs
+++ b/package/ego-browser/src/driver/observe.test.mjs
@@ -2,7 +2,7 @@ import test from "node:test";
import assert from "node:assert/strict";
import { mkdtemp, readFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
-import { join } from "node:path";
+import { basename, dirname, join } from "node:path";
import {
browserCdp,
@@ -254,3 +254,36 @@ test("captureScreenshot creates missing parent directories", async () => {
await rm(root, { recursive: true, force: true });
}
});
+
+test("captureScreenshot without a path writes unique files to EGO_BROWSER_OUTPUT_DIR", async () => {
+ const root = await mkdtemp(join(tmpdir(), "ego-browser-output-dir-test-"));
+ const previous = globalThis.EGO_BROWSER_OUTPUT_DIR;
+ globalThis.EGO_BROWSER_OUTPUT_DIR = root;
+ try {
+ await withCdpRuntime(async () => {
+ const first = await captureScreenshot();
+ const second = await captureScreenshot();
+ assert.equal(dirname(first), root);
+ assert.equal(dirname(second), root);
+ assert.match(basename(first), /^ego-browser-shot-[0-9a-f-]{36}\.png$/);
+ assert.notEqual(first, second);
+ assert.equal(await readFile(first, "utf8"), "png");
+ });
+ } finally {
+ if (previous === undefined) delete globalThis.EGO_BROWSER_OUTPUT_DIR;
+ else globalThis.EGO_BROWSER_OUTPUT_DIR = previous;
+ await rm(root, { recursive: true, force: true });
+ }
+});
+
+test("captureScreenshot without a path falls back to tmpdir", async () => {
+ assert.equal(globalThis.EGO_BROWSER_OUTPUT_DIR, undefined);
+ const restore = setOverrides({ async writeFile() {} });
+ try {
+ await withCdpRuntime(async () => {
+ assert.equal(dirname(await captureScreenshot()), tmpdir());
+ });
+ } finally {
+ restore();
+ }
+});
diff --git a/package/ego-browser/src/driver/observe.ts b/package/ego-browser/src/driver/observe.ts
index f0389e8..6c8b15b 100644
--- a/package/ego-browser/src/driver/observe.ts
+++ b/package/ego-browser/src/driver/observe.ts
@@ -1,3 +1,4 @@
+import { randomUUID } from "node:crypto";
import { mkdir } from "node:fs/promises";
import { dirname, join } from "node:path";
import { tmpdir } from "node:os";
@@ -87,10 +88,24 @@ export async function elementCenter(selectorOrRef) {
);
}
-// Sequence number for default screenshot file names. Combined with the pid it
-// keeps concurrent agent processes (parallel task spaces) from overwriting each
-// other's shots in the shared tmpdir, and successive shots in one run distinct.
-let screenshotSeq = 0;
+/**
+ * Directory and name for a screenshot taken without an explicit path.
+ *
+ * The embedded host runs every script in one long-lived Node process, giving
+ * each run its own `globalThis` but sharing `process.env`, `os.tmpdir()` and
+ * the pid. A host that needs per-run output sets
+ * `globalThis.EGO_BROWSER_OUTPUT_DIR`; a random name keeps concurrent runs in
+ * the shared default directory from overwriting each other.
+ */
+function defaultScreenshotPath(): string {
+ const outputDir = (globalThis as { EGO_BROWSER_OUTPUT_DIR?: unknown })
+ .EGO_BROWSER_OUTPUT_DIR;
+ const directory =
+ typeof outputDir === "string" && outputDir.length > 0
+ ? outputDir
+ : tmpdir();
+ return join(directory, `ego-browser-shot-${randomUUID()}.png`);
+}
export async function captureScreenshot(
path?: string,
@@ -111,9 +126,7 @@ export async function captureScreenshotForSession(
sessionId?: string,
) {
assertScreenshotScale(options.scale);
- const outputPath =
- path ??
- join(tmpdir(), `ego-browser-shot-${process.pid}-${++screenshotSeq}.png`);
+ const outputPath = path ?? defaultScreenshotPath();
const full = options.full ?? false;
// CSS-pixel sizing is the default and the only declarative scale mode. Keep
// the legacy raw flag as an internal compatibility escape hatch.