Multiplayer agent harness for work.
Avoid redundant sandbox readiness and credential policy reads (#1221)
Regan Bellbd0abd088483
diff --git a/src/credentials/device-flow-cutover.ts b/src/credentials/device-flow-cutover.tsindex f44ca494b..027f00b9d 100644--- a/src/credentials/device-flow-cutover.ts+++ b/src/credentials/device-flow-cutover.ts@@ -107,10 +107,12 @@ export function createDeviceFlowCutoverStore(resolvePolicy,resolve,async residentResetGeneration(scope, service, computerId) {- if ((await resolve(scope, service)) !== "legacy") return null;const policy = await resolvePolicy(scope, service);- const requested = await getReset(resetKey("request", scope, service));- const orgRequested = scope === orgScope ? null : await getReset(resetKey("request", orgScope, service));+ if ((policy?.mode ?? "legacy") !== "legacy") return null;+ const [requested, orgRequested] = await Promise.all([+ getReset(resetKey("request", scope, service)),+ scope === orgScope ? null : getReset(resetKey("request", orgScope, service)),+ ]);const generation = [policy?.resetResident ? policy.resetGeneration : undefined,orgRequested?.generation,diff --git a/src/sandbox/aws-microvm-api.ts b/src/sandbox/aws-microvm-api.tsindex 240969757..48d16e4d1 100644--- a/src/sandbox/aws-microvm-api.ts+++ b/src/sandbox/aws-microvm-api.ts@@ -281,7 +281,7 @@ export interface MicrovmClient {execRaw(id: string, endpoint: string, cmd: string, timeoutSec: number): Promise<MicrovmExecResult>;writeAbs(id: string, endpoint: string, absPath: string, data: Uint8Array): Promise<void>;waitDaemon(id: string, endpoint: string): Promise<void>;- ensureRunning(id: string, endpoint: string): Promise<void>;+ ensureRunning(id: string, endpoint: string, observed?: MicrovmDescription): Promise<void>;evict(id: string): void;}@@ -362,8 +362,9 @@ export function createMicrovmClient(api: AwsMicrovmApi, opts: MicrovmClientOptiothrow new Error(`microVM ${id} exec daemon never became reachable: ${lastErr}`);}- async function ensureRunning(id: string, endpoint: string): Promise<void> {- const desc = await api.getMicrovm(id);+ async function ensureRunning(id: string, endpoint: string, observed?: MicrovmDescription): Promise<void> {+ const desc = observed ?? (await api.getMicrovm(id));+ if (desc.microvmId !== id) throw new Error("microVM state belongs to another body");if (desc.state === "RUNNING") return;if (desc.state === "TERMINATED" || desc.state === "TERMINATING")throw new Error(`microVM ${id} is ${desc.state}, cannot run`);diff --git a/src/sandbox/aws-sandbox.ts b/src/sandbox/aws-sandbox.tsindex 7ec8db2c3..d36cd6820 100644--- a/src/sandbox/aws-sandbox.ts+++ b/src/sandbox/aws-sandbox.ts@@ -170,8 +170,8 @@ export function createAwsSandbox(workspace: WorkspaceStore, opts: AwsSandboxOptireturn Buffer.from((JSON.parse(res.text) as { b64: string }).b64, "base64");}- async function ensureRunning(id: string): Promise<void> {- await client.ensureRunning(id, await resolveEndpoint(id));+ async function ensureRunning(id: string, observed?: Awaited<ReturnType<AwsMicrovmApi["getMicrovm"]>>): Promise<void> {+ await client.ensureRunning(id, await resolveEndpoint(id), observed);}const homeSnapshots = createHomeSnapshotOps<string>({@@ -260,7 +260,7 @@ export function createAwsSandbox(workspace: WorkspaceStore, opts: AwsSandboxOptiif (alive && !stale) {endpointById.set(stored.microvmId, stored.endpoint);scopeByMicrovm.set(stored.microvmId, scope);- await ensureRunning(stored.microvmId);+ await ensureRunning(stored.microvmId, desc);return { id: stored.microvmId, endpoint: stored.endpoint, coldStart: false };}if (alive && stale) {@@ -306,7 +306,7 @@ export function createAwsSandbox(workspace: WorkspaceStore, opts: AwsSandboxOpticonst desc = await api.tryGetMicrovm(existing.microvmId);if (desc && desc.state !== "TERMINATED" && desc.state !== "TERMINATING") {endpointById.set(existing.microvmId, existing.endpoint);- await ensureRunning(existing.microvmId);+ await ensureRunning(existing.microvmId, desc);return { id: existing.microvmId, endpoint: existing.endpoint, coldStart: false };}}diff --git a/test/aws-microvm-api.test.ts b/test/aws-microvm-api.test.tsindex 6b4c7f40c..6cffe702f 100644--- a/test/aws-microvm-api.test.ts+++ b/test/aws-microvm-api.test.ts@@ -167,3 +167,19 @@ test("waitDaemon: a transient 429 blip still proves readiness on the next probe"await client.waitDaemon("mvm-1", "mvm-1.example.on.aws");assert.equal(calls.length, 2, "the blip is ridden out and the 200 proves the daemon");});++test("readiness reuses only the supplied observation and checks again on the next call", async () => {+ let reads = 0;+ const api = {+ getMicrovm: async (id: string) => {+ reads++;+ return { microvmId: id, state: "TERMINATED" };+ },+ } as AwsMicrovmApi;+ const client = createMicrovmClient(api, { agentPort: 8080, tokenTtlMinutes: 30 });+ await client.ensureRunning("one", "endpoint", { microvmId: "one", state: "RUNNING" });+ assert.equal(reads, 0);+ await assert.rejects(client.ensureRunning("one", "endpoint"), /TERMINATED/);+ assert.equal(reads, 1);+ await assert.rejects(client.ensureRunning("one", "endpoint", { microvmId: "two", state: "RUNNING" }), /another body/);+});