Multiplayer agent harness for work.
Default sandbox network to private; clean up test fixture servers (#2583)
Joshua Francef9f2aafd5a60
diff --git a/src/deploy/e2b-deploy-provider.ts b/src/deploy/e2b-deploy-provider.tsindex f45fa36e6..cc7816e46 100644--- a/src/deploy/e2b-deploy-provider.ts+++ b/src/deploy/e2b-deploy-provider.ts@@ -159,7 +159,6 @@ export function createE2bDeployProvider(opts: E2bDeployProviderOptions): DeployPtimeoutMs: d.alwaysOn ? ALWAYS_ON_TIMEOUT_MS : idleTimeoutMs,autoPause: true,autoResume: true,- privateTraffic: true,});try {await install(d, session, version);diff --git a/src/sandbox/e2b-client.ts b/src/sandbox/e2b-client.tsindex 8662ac8b8..11fda8192 100644--- a/src/sandbox/e2b-client.ts+++ b/src/sandbox/e2b-client.ts@@ -70,7 +70,6 @@ interface E2bCreateOpts {timeoutMs?: number;autoPause?: boolean;autoResume?: boolean;- privateTraffic?: boolean;fromSnapshot?: string;}@@ -323,9 +322,7 @@ export function createSdkE2bClient(opts: SdkE2bClientOptions): E2bClient {onTimeout: createOpts.autoPause ? "pause" : "kill",autoResume: !!(createOpts.autoPause && createOpts.autoResume),},- ...(network || createOpts.privateTraffic- ? { network: { ...network, ...(createOpts.privateTraffic ? { allowPublicTraffic: false } : {}) } }- : {}),+ network: { ...network, allowPublicTraffic: false },});return wrap(lib, sbx, timeoutMs);},diff --git a/test/e2b-deploy-provider.test.ts b/test/e2b-deploy-provider.test.tsindex bde1e57cd..6ea2e4ad7 100644--- a/test/e2b-deploy-provider.test.ts+++ b/test/e2b-deploy-provider.test.ts@@ -1,6 +1,5 @@import { test, afterEach } from "node:test";import assert from "node:assert/strict";-import { spawnSync } from "node:child_process";import { mkdtempSync, writeFileSync } from "node:fs";import { createServer } from "node:net";import { tmpdir } from "node:os";@@ -20,14 +19,13 @@ const SERVER = [" if (q.url === '/write') { fs.writeFileSync(process.env.DATA_DIR + '/note.txt', 'kept'); return s.end('ok'); }"," if (q.url === '/data') return s.end(fs.readFileSync(process.env.DATA_DIR + '/note.txt', 'utf8'));"," s.end('v' + process.env.APP_VERSION);",- "}).listen(process.env.PORT);",+ "}).listen(process.env.PORT, '127.0.0.1');",].join(" ");let fake: FakeE2b | undefined;let appPort = 0;afterEach(() => {- if (appPort) spawnSync("sh", ["-c", `lsof -ti tcp:${appPort} -sTCP:LISTEN | xargs kill 2>/dev/null; true`]);fake?.cleanup();fake = undefined;});@@ -92,7 +90,6 @@ test("e2b deploy runs each app in its own auto-pausing, auto-resuming private saassert.deepEqual(fake.createOptions(name), {autoPause: true,autoResume: true,- privateTraffic: true,timeoutMs: 300_000,});assert.equal(fake.current(name)!.metadata["qm-deploy"], d.id);diff --git a/test/e2b-sandbox.test.ts b/test/e2b-sandbox.test.tsindex cd82b8ffb..01353c20e 100644--- a/test/e2b-sandbox.test.ts+++ b/test/e2b-sandbox.test.ts@@ -1,6 +1,6 @@import { SandboxProvisionCleanupError } from "../src/sandbox/sandbox.ts";import { pollProcess } from "../src/sandbox/process-poll.ts";-import { test, after, beforeEach } from "node:test";+import { test, afterEach, beforeEach } from "node:test";import assert from "node:assert/strict";import { mkdtempSync } from "node:fs";import { tmpdir } from "node:os";@@ -52,7 +52,7 @@ const nativeClient = (): typeof fake.client => ({return { state: current.state, expiresAtMs: Date.now() + 60_000, onTimeout: "pause" };},});-after(() => fake?.cleanup());+afterEach(() => fake.cleanup());test("provision runs commands with env and cwd", async () => {const h = await sandbox.provision(layers, { env: { MY_VAR: "v1" } });diff --git a/test/native-provider-clients.test.ts b/test/native-provider-clients.test.tsindex ff64f4906..b8034be9d 100644--- a/test/native-provider-clients.test.ts+++ b/test/native-provider-clients.test.ts@@ -391,7 +391,7 @@ test("E2B sends explicit pause lifecycle, reads state without connect, and surfaconst createOpts = e2bCalls[0]![2] as { lifecycle: unknown; timeoutMs: number; network?: unknown };assert.deepEqual(createOpts.lifecycle, { onTimeout: "pause", autoResume: false });assert.equal(createOpts.timeoutMs, 3600_000 + E2B_EXEC_MARGIN_MS, "default TTL covers the longest command");- assert.equal(createOpts.network, undefined, "no egress proxy means no network rules");+ assert.deepEqual(createOpts.network, { allowPublicTraffic: false }, "ports need the traffic token");const info = await client.info!(session.sandboxId);assert.equal(info.state, "paused");assert.equal(info.cpuCount, 2);@@ -474,7 +474,7 @@ test("E2B turns the egress proxy into host-level network rules on create and onconst client = createSdkE2bClient({ apiKey: "test", egressProxyUrl: "https://egress.example.com" });const rules = { allowOut: ["egress.example.com"], denyOut: ["0.0.0.0/0"] };await client.create({ metadata: {}, autoPause: true });- assert.deepEqual((e2bCalls.at(-1)![2] as { network: unknown }).network, rules);+ assert.deepEqual((e2bCalls.at(-1)![2] as { network: unknown }).network, { ...rules, allowPublicTraffic: false });await client.connect("e2b-native");assert.deepEqual(e2bCalls.at(-1), ["updateNetwork", rules]);assert.deepEqual(e2bEgressNetwork("http://10.0.0.5:3128"), { allowOut: ["10.0.0.5"], denyOut: ["0.0.0.0/0"] });diff --git a/test/support/fake-e2b.ts b/test/support/fake-e2b.tsindex 4c0d5850a..eb639e21d 100644--- a/test/support/fake-e2b.ts+++ b/test/support/fake-e2b.ts@@ -1,5 +1,5 @@import { spawnSync } from "node:child_process";-import { cpSync, existsSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";+import { cpSync, existsSync, mkdtempSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { dirname, join } from "node:path";import {@@ -23,7 +23,6 @@ interface FakeRecord {timeoutMs: number[];loseNextCommand: boolean;autoResume: boolean;- privateTraffic: boolean;createTimeoutMs?: number;}@@ -37,7 +36,7 @@ export interface FakeE2b {expirePaused(): void;loseNextCommand(name: string): void;timeouts(name: string): number[];- createOptions(name: string): { autoPause: boolean; autoResume: boolean; privateTraffic: boolean; timeoutMs?: number };+ createOptions(name: string): { autoPause: boolean; autoResume: boolean; timeoutMs?: number };snapshots(): string[];restoredFrom(name: string): string | undefined;metrics: E2bMetrics | null;@@ -45,8 +44,52 @@ export interface FakeE2b {cleanup(): void;}+const OWNER_ENV = "QM_FAKE_E2B_OWNER";++function ownedPids(owner: string): number[] {+ const tag = `${OWNER_ENV}=${owner}`;+ const owned = (kv: string): boolean => kv === tag || kv.startsWith(`${tag}/`);+ if (!existsSync("/proc/self/environ")) {+ const ps = spawnSync("ps", ["-AEww", "-o", "pid=,command="], { encoding: "utf8" }).stdout ?? "";+ return ps+ .split("\n")+ .filter((line) => line.split(" ").some(owned))+ .map((line) => Number.parseInt(line, 10));+ }+ return readdirSync("/proc")+ .map(Number)+ .filter((pid) => {+ if (!Number.isInteger(pid)) return false;+ try {+ return readFileSync(`/proc/${pid}/environ`, "latin1").split("\0").some(owned);+ } catch {+ return false;+ }+ });+}++function killOwned(owner: string): void {+ for (let pass = 0; pass < 50; pass++) {+ const pids = ownedPids(owner);+ if (!pids.length) return;+ for (const pid of pids) {+ try {+ process.kill(pid, "SIGKILL");+ } catch (err) {+ if ((err as NodeJS.ErrnoException).code !== "ESRCH") throw err;+ }+ }+ }+ throw new Error(`fake-e2b: processes survived cleanup: ${ownedPids(owner).join(", ")}`);+}+export function installFakeE2b(fakeOpts: { info?: boolean } = {}): FakeE2b {const root = mkdtempSync(join(tmpdir(), "fake-e2b-"));+ const destroy = (r: FakeRecord): void => {+ r.expired = true;+ killOwned(`${root}/${r.sandboxId}`);+ rmSync(r.home, { recursive: true, force: true });+ };const records = new Map<string, FakeRecord>();const snapshots = new Map<string, string>();const execScripts: string[] = [];@@ -99,10 +142,7 @@ export function installFakeE2b(fakeOpts: { info?: boolean } = {}): FakeE2b {},expirePaused: () => {for (const r of records.values()) {- if (r.state === "paused" && !r.expired) {- r.expired = true;- rmSync(r.home, { recursive: true, force: true });- }+ if (r.state === "paused" && !r.expired) destroy(r);}},loseNextCommand: (name) => {@@ -116,19 +156,21 @@ export function installFakeE2b(fakeOpts: { info?: boolean } = {}): FakeE2b {return {autoPause: r.autoPause,autoResume: r.autoResume,- privateTraffic: r.privateTraffic,...(r.createTimeoutMs ? { timeoutMs: r.createTimeoutMs } : {}),};},snapshots: () => [...snapshots.keys()],restoredFrom: (name) => byName(name)?.fromSnapshot,execScripts: () => [...execScripts],- cleanup: () => rmSync(root, { recursive: true, force: true }),+ cleanup: () => {+ killOwned(root);+ rmSync(root, { recursive: true, force: true });+ },};const session = (r: FakeRecord): E2bSession => ({sandboxId: r.sandboxId,- ...(r.privateTraffic ? { trafficAccessToken: `traffic-${r.sandboxId}` } : {}),+ trafficAccessToken: `traffic-${r.sandboxId}`,host: (port) => `${port}-${r.sandboxId}.e2b.test`,async runCommand(command, opts): Promise<E2bCommandResult> {alive(r);@@ -143,7 +185,7 @@ export function installFakeE2b(fakeOpts: { info?: boolean } = {}): FakeE2b {const spawned = spawnSync("sh", ["-c", remap(r, command)], {encoding: "buffer",maxBuffer: 128 * 1024 * 1024,- env: { ...process.env, COPYFILE_DISABLE: "1" },+ env: { ...process.env, COPYFILE_DISABLE: "1", [OWNER_ENV]: `${root}/${r.sandboxId}` },});return {stdout: (spawned.stdout ?? Buffer.alloc(0)).toString("utf8"),@@ -185,8 +227,7 @@ export function installFakeE2b(fakeOpts: { info?: boolean } = {}): FakeE2b {return fake.metrics;},async kill(): Promise<void> {- r.expired = true;- rmSync(r.home, { recursive: true, force: true });+ destroy(r);},});@@ -215,7 +256,6 @@ export function installFakeE2b(fakeOpts: { info?: boolean } = {}): FakeE2b {...(opts.timeoutMs ? { createTimeoutMs: opts.timeoutMs } : {}),loseNextCommand: false,autoResume: opts.autoResume ?? false,- privateTraffic: opts.privateTraffic ?? false,...(opts.fromSnapshot ? { fromSnapshot: opts.fromSnapshot } : {}),};if (opts.fromSnapshot) {@@ -242,8 +282,7 @@ export function installFakeE2b(fakeOpts: { info?: boolean } = {}): FakeE2b {async kill(sandboxId): Promise<void> {const r = records.get(sandboxId);if (!r) return;- r.expired = true;- rmSync(r.home, { recursive: true, force: true });+ destroy(r);},async deleteSnapshot(snapshotId): Promise<void> {const dir = snapshots.get(snapshotId);