Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Take the shared computer lock when the computer is already running (#2161)

Regan Bellfa38ff6b8f2e
diff --git a/src/sandbox/e2b-sandbox.ts b/src/sandbox/e2b-sandbox.ts
index 15f5e0d24..545fcba35 100644
--- a/src/sandbox/e2b-sandbox.ts
+++ b/src/sandbox/e2b-sandbox.ts
@@ -4,6 +4,7 @@ import type { WorkspaceLayer } from "../types.ts";
import type { WorkspaceStore } from "../workspace/workspace-store.ts";
import type { DurableMap } from "../persistence/durable-map.ts";
import { createMemoryMap } from "../persistence/durable-map.ts";
+import { createNoopAdvisoryLock, type AdvisoryLock } from "../persistence/advisory-lock.ts";
import { createKeyedQueue } from "../util/async.ts";
import { swallowAs, errMessage } from "../util/errors.ts";
import { shq } from "../util/shell.ts";
@@ -75,6 +76,7 @@ export interface StoredE2bSandbox {
}
export interface E2bSandboxOptions extends BlobStagingOptions {
+ advisoryLock?: AdvisoryLock;
client: E2bClient;
namePrefix?: string;
defaultTimeoutSec?: number;
@@ -106,7 +108,10 @@ export function createE2bSandbox(workspace: WorkspaceStore, opts: E2bSandboxOpti
const workspaceDir = `${HOME_DIR}/${WORKSPACE_BASENAME}`;
const store = opts.store ?? createMemoryMap<StoredE2bSandbox>();
const snapshots = opts.snapshots ?? createMemorySnapshotStore();
- const provisionQueue = createKeyedQueue<string>();
+ const localQueue = createKeyedQueue<string>();
+ const advisoryLock = opts.advisoryLock ?? createNoopAdvisoryLock();
+ const provisionQueue = <T>(scope: string, action: () => Promise<T>): Promise<T> =>
+ localQueue(scope, () => advisoryLock.withLock(`e2b-provision:${scope}`, action));
const sessionByName = new Map<string, E2bSession>();
const scopeByName = new Map<string, string>();
diff --git a/src/sandbox/sandbox-routing.ts b/src/sandbox/sandbox-routing.ts
index 4960a78bd..49b517730 100644
--- a/src/sandbox/sandbox-routing.ts
+++ b/src/sandbox/sandbox-routing.ts
@@ -121,7 +121,7 @@ export function createSandboxRouter(opts: RoutingSandboxOptions): Sandbox {
const routedLayers = layers.map((layer) =>
layer.mode === "rw" ? { ...layer, scopeId: resource.backingScopeId } : layer,
);
- const handle = await resources.use(resource.id, () => provision(routedLayers), true);
+ const handle = await resources.use(resource.id, () => provision(routedLayers));
return { ...handle, backend: resource.backend, scopeId: resource.ownerScopeId, resourceId: resource.id };
},
diff --git a/src/wiring.ts b/src/wiring.ts
index 2e47c60e4..a7f697b87 100644
--- a/src/wiring.ts
+++ b/src/wiring.ts
@@ -905,6 +905,7 @@ export function buildApp(
const e2b = config.e2bSandbox;
if (!e2b.apiKey) throw new Error("SANDBOX_BACKEND=e2b requires E2B_API_KEY");
return createE2bSandbox(workspace, {
+ advisoryLock,
client: createSdkE2bClient({
apiKey: e2b.apiKey,
...(e2b.templateId ? { templateId: e2b.templateId } : {}),
diff --git a/test/e2b-sandbox.test.ts b/test/e2b-sandbox.test.ts
index af4cb262d..6e5a060a6 100644
--- a/test/e2b-sandbox.test.ts
+++ b/test/e2b-sandbox.test.ts
@@ -17,6 +17,7 @@ import { mintCapabilityToken, EGRESS_PROXY_AUD } from "../src/auth/capability-to
import { installFakeE2b, type FakeE2b } from "./support/fake-e2b.ts";
import type { Sandbox } from "../src/sandbox/sandbox.ts";
import { E2bSandboxGoneError } from "../src/sandbox/e2b-client.ts";
+import { createMemoryAdvisoryLock } from "../src/persistence/advisory-lock.ts";
let fake: FakeE2b;
let sandbox: Sandbox;
@@ -57,6 +58,12 @@ test("provision runs commands with env and cwd", async () => {
assert.match(r.stdout, /VAR=v1/);
});
+test("two cores provisioning the same stopped computer create one sandbox", async () => {
+ const advisoryLock = createMemoryAdvisoryLock();
+ await Promise.all([make({ advisoryLock }).provision(layers), make({ advisoryLock }).provision(layers)]);
+ assert.equal(fake.createdCount(scopeName()), 1);
+});
+
test("an already-aborted signal never executes a command", async () => {
const handle = await sandbox.provision(layers);
const before = fake.execScripts().length;
diff --git a/test/sandbox-resources.test.ts b/test/sandbox-resources.test.ts
index 12fd9ce6b..61c879fb9 100644
--- a/test/sandbox-resources.test.ts
+++ b/test/sandbox-resources.test.ts
@@ -116,6 +116,17 @@ test("blank sandbox identities coexist and default changes never copy files or r
assert.equal(a.resourceId, first.id);
});
+test("provisioning a running computer does not wait behind a long-running command on it", async () => {
+ const { resources, router, layers } = fixture();
+ const handle = await router.provision(layers);
+ const command = Promise.withResolvers<void>();
+ const running = resources.use(handle.resourceId!, () => command.promise);
+
+ assert.equal((await router.provision(layers)).id, handle.id);
+ command.resolve();
+ await running;
+});
+
test("explicit sandbox profiles follow selected storage rather than the parent's default provider", async () => {
const { backend, options } = fixture();
const modal: Sandbox = { ...backend, profile: { ...backend.profile, backend: "modal" } };
diff --git a/test/sandbox-routing.test.ts b/test/sandbox-routing.test.ts
index f7138c01a..580620bfc 100644
--- a/test/sandbox-routing.test.ts
+++ b/test/sandbox-routing.test.ts
@@ -130,7 +130,7 @@ test("provision uses the scope's default resource: backend, backing scope, lock,
assert.equal(h.resourceId, "r1");
assert.equal(h.scopeId, "personal:a");
assert.equal(sprites.provisioned[0]!.layers[0]!.scopeId, "sandbox:r1", "rw layer remapped to the backing scope");
- assert.deepEqual(resources.locks, [{ id: "r1", exclusive: true }]);
+ assert.deepEqual(resources.locks, [{ id: "r1", exclusive: false }]);
assert.deepEqual(aws.calls, []);
});