Multiplayer agent harness for work.
perf: reduce ordinary-turn setup work for Sprites and Modal (#1229)
Regan Belld552850d5ce7
diff --git a/src/core/orchestrator.ts b/src/core/orchestrator.tsindex 3295f15c6..4275bbae7 100644--- a/src/core/orchestrator.ts+++ b/src/core/orchestrator.ts@@ -59,13 +59,12 @@ import {} from "../credentials/device-flow-persist.ts";import type { CredentialPathSpec } from "../credentials/resident-paths.ts";import type { DeviceFlowCutoverMode } from "../credentials/device-flow-cutover.ts";-import { type ResidentAuthConnector, RESIDENT_AUTH_CONNECTORS, mergeConnectors } from "../credentials/resident-auth.ts";import {configuredConnectorProviders,connectorStatusIsStale,refreshConnectorStatus,} from "../credentials/connector-status.ts";-import { renderComputerBlock, renderResidentLoginsBlock, renderConnectedAppsBlock } from "./environment-facts.ts";+import { renderComputerBlock, renderConnectedAppsBlock } from "./environment-facts.ts";import { PROVIDERS } from "../connectors/oauth.ts";import { estimateCostUsd } from "../ratelimit/budget.ts";import {@@ -244,8 +243,6 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {}const leaseKeepaliveMs = Math.floor(deps.sessions.leaseTtlMs / 3);const skillMaterializer = createSkillMaterializer(deps.advisoryLock);- const residentAuthConnectors = (): ResidentAuthConnector[] =>- mergeConnectors(RESIDENT_AUTH_CONNECTORS, deps.deploymentLayer?.connectors ?? []);const pending = deps.approvals ?? createMemoryMap<PendingApprovalRecord>();const transcripts = createTranscriptSource(deps.sessions);const approvalGrants = deps.approvalGrants ?? createMemoryMap<CommandApprovalGrant>();@@ -1635,7 +1632,6 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {visibleSkills,visibleSkillsForTurn,skillMaterializer,- residentAuthConnectors,emitGapWork,perf,});@@ -1897,17 +1893,6 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {...(audienceMembers.some((p) => samePerson(p.id, actor.id)) ? [] : [actor]),...audienceMembers,].map((p) => ({ id: p.id, ...(p.displayName ? { displayName: p.displayName } : {}) }));- const detectedByOwner = new Map<string, string[]>();- if (deps.livenessCache) {- for (const m of members) {- const rec = await deps.livenessCache.get(personalScope(m.id)).catch(() => null);- if (!rec) continue;- const labels = residentAuthConnectors()- .filter((c) => rec.connectors[c.id] === "active")- .map((c) => c.label);- if (labels.length) detectedByOwner.set(m.id, labels);- }- }const [entriesByOwner, connectorsByOwner, scopeGrants, scopeAsks, ownerAsks] = await Promise.all([deps.keychain.listByOwners(members.map((m) => m.id)),deps.keychain.listConnectorsByOwners(members.map((m) => m.id)),@@ -1924,25 +1909,11 @@ export function createOrchestrator(deps: OrchestratorDeps): Orchestrator {connectorsByOwner,scopeGrants,injected: keychainInjected,- detectedByOwner,scopeAsks,ownerAsks,});if (keychainBlock) systemPrompt += `\n\n${keychainBlock}`;}- if (!strictReadOnly && deps.livenessCache) {- const liveness = await deps.livenessCache- .get(memoryScopeId)- .catch(swallowAs("orchestrator: liveness cache read", null));- const loginsBlock = renderResidentLoginsBlock(liveness, residentAuthConnectors());- if (loginsBlock) {- systemPrompt += `\n\n${loginsBlock}`;- if (deps.scratchExec) {- systemPrompt +=- '\nThese logins live on your scoped computer — `execute` reaches them only with scope:"scoped"; a scratch run has none of them.';- }- }- }if (!strictReadOnly && deps.resolveConnectorClient && conversation.kind === "dm") {let status = null;try {diff --git a/src/core/orchestrator/sandboxes.ts b/src/core/orchestrator/sandboxes.tsindex e36ec9237..6266122eb 100644--- a/src/core/orchestrator/sandboxes.ts+++ b/src/core/orchestrator/sandboxes.ts@@ -8,11 +8,6 @@ import {removeDeviceFlowLogins,} from "../../credentials/device-flow-persist.ts";import type { DeviceFlowCutoverMode } from "../../credentials/device-flow-cutover.ts";-import {- probeResidentAuth,- residentAuthProbeIsStale,- type ResidentAuthConnector,-} from "../../credentials/resident-auth.ts";import { expandServiceAliases } from "../../credentials/resident-paths.ts";import { shq } from "../../util/shell.ts";import { createSkillMaterializer, safeSkillDirName } from "../../skills/materialize.ts";@@ -50,7 +45,6 @@ export interface TurnSandboxContext {visibleSkills: SkillResolution[];visibleSkillsForTurn: () => Promise<SkillResolution[]>;skillMaterializer: ReturnType<typeof createSkillMaterializer>;- residentAuthConnectors: () => ResidentAuthConnector[];emitGapWork: (phase: GapPhase, start: number, end: number) => void;perf: { credsMs: number };}@@ -81,7 +75,6 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {visibleSkills,visibleSkillsForTurn,skillMaterializer,- residentAuthConnectors,emitGapWork,perf,} = ctx;@@ -124,7 +117,6 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {used: boolean;provisionMs?: number;materializeMs?: number;- residentAuthProbe?: Promise<void>;} = { handle: null, pending: null, used: false };const scratchBox: { handle: SandboxHandle | null; provisionMs?: number } = { handle: null };const ownerAuthBox: { handle: SandboxHandle | null; pending: SandboxHandle | null; provisionMs?: number } = {@@ -278,8 +270,7 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {box.provisionMs = Date.now() - provisionStart;await prepareCredentials(handle, emit);const dirCleanupStart = Date.now();- await deps.sandbox.removeDir(handle, turnSessionDir);- await sweepStaleTurnFiles(handle);+ await prepareTurnFiles(handle);emit("dir_cleanup", dirCleanupStart, Date.now());if (deps.processes && supportsProcessSessions(deps.sandbox)) {const procReconcileStart = Date.now();@@ -297,32 +288,6 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {emit("proc_reconcile", procReconcileStart, Date.now());}}- if (deps.livenessCache) {- const cache = deps.livenessCache;- box.residentAuthProbe = (async () => {- try {- const cached = await cache.get(memoryScopeId);- if (residentAuthProbeIsStale(cached, Date.now())) {- await probeResidentAuth({- sandbox: deps.sandbox,- handle,- cache,- scopeId: memoryScopeId,- now: Date.now(),- connectors: residentAuthConnectors(),- });- }- } catch (err) {- deps.errors?.record({- category: "agent_computer",- code: "resident_auth_probe_failed",- message: errMessage(err),- scopeLabel: scopeId,- sessionId: session.id,- });- }- })();- }if (deps.skills) {const materializeStart = Date.now();try {@@ -390,8 +355,7 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {});resourcePendingHandles.set(id, handle);await prepareCredentials(handle, emitGapWork);- await deps.sandbox.removeDir(handle, turnSessionDir);- await sweepStaleTurnFiles(handle);+ await prepareTurnFiles(handle);if (deps.skills)await skillMaterializer.materializeIndex(deps.sandbox, handle, visibleSkills, visibleSkillsForTurn);resourceHandles.set(id, handle);@@ -529,10 +493,16 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {}}};- const sweepStaleTurnFiles = async (handle: SandboxHandle): Promise<void> => {+ const prepareTurnFiles = async (handle: SandboxHandle): Promise<void> => {const cutoff = Date.now() - TURN_FILES_MAX_AGE_MS;+ const paths = deps.sandbox.removeDirAndList+ ? await deps.sandbox.removeDirAndList(handle, turnSessionDir, TURN_FILES_DIR)+ : await (async () => {+ await deps.sandbox.removeDir(handle, turnSessionDir);+ return deps.sandbox.listDir(handle, TURN_FILES_DIR);+ })();const stale = new Set<string>();- for (const path of await deps.sandbox.listDir(handle, TURN_FILES_DIR)) {+ for (const path of paths) {const parts = path.split("/");const startedAt = Number.parseInt(parts[2]?.split("-")[0] ?? "", 36);if (parts[0] === TURN_FILES_DIR && parts[1] && parts[2] && Number.isFinite(startedAt) && startedAt < cutoff) {@@ -566,10 +536,7 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {await deps.sandbox.teardown(h, { keepWarm: true }).catch(() => {});return;}- const roomHasComputer = !!(await deps.livenessCache- ?.get(target)- .catch(swallowAs("orchestrator: reach computer check", null)));- await deps.sandbox.teardown(h, roomHasComputer ? undefined : { destroy: true }).catch(() => {});+ await deps.sandbox.teardown(h).catch(() => {});}),);const ownerHandle = ownerAuthBox.handle ?? ownerAuthBox.pending;@@ -640,7 +607,6 @@ export function createTurnSandboxes(ctx: TurnSandboxContext) {return;}await clearTurnFiles(handle);- if (box.residentAuthProbe) await box.residentAuthProbe.catch(() => {});let keepWarm = false;if (deps.processes && supportsProcessSessions(deps.sandbox)) {try {diff --git a/src/credentials/resident-auth.ts b/src/credentials/resident-auth.tsindex 9b048a91e..5fe7b9297 100644--- a/src/credentials/resident-auth.ts+++ b/src/credentials/resident-auth.ts@@ -19,14 +19,6 @@ export const RESIDENT_AUTH_CONNECTORS: readonly ResidentAuthConnector[] = [},];-export function mergeConnectors(- base: readonly ResidentAuthConnector[],- extra: readonly ResidentAuthConnector[],-): ResidentAuthConnector[] {- const extraIds = new Set(extra.map((c) => c.id));- return [...base.filter((c) => !extraIds.has(c.id)), ...extra];-}-type ResidentAuthState = "active" | "inactive" | "absent";export interface ScopeLivenessRecord {diff --git a/src/sandbox/exec-file-ops.ts b/src/sandbox/exec-file-ops.tsindex 46db801d3..34f239184 100644--- a/src/sandbox/exec-file-ops.ts+++ b/src/sandbox/exec-file-ops.ts@@ -28,6 +28,7 @@ export function posixJoin(base: string, rel: string): string {export interface ExecFileOpsDeps {label: string;+ combineRemoveAndList?: boolean;exec(id: string, script: string, timeoutSec: number): Promise<{ code: number; stdout: string; stderr: string }>;writeInline(id: string, abs: string, data: Uint8Array, label: string): Promise<void>;}@@ -39,9 +40,10 @@ export interface ExecFileOps {): Promise<void>;listDir(handle: SandboxHandle, relDir: string): Promise<string[]>;removeDir(handle: SandboxHandle, relDir: string): Promise<void>;+ removeDirAndList?(handle: SandboxHandle, removeRelDir: string, listRelDir: string): Promise<string[]>;}-export function createExecFileOps({ label, exec, writeInline }: ExecFileOpsDeps): ExecFileOps {+export function createExecFileOps({ label, exec, writeInline, combineRemoveAndList }: ExecFileOpsDeps): ExecFileOps {return {async importFiles(handle, entries): Promise<void> {const list = [...entries];@@ -73,6 +75,26 @@ export function createExecFileOps({ label, exec, writeInline }: ExecFileOpsDeps).filter(Boolean);},+ ...(combineRemoveAndList+ ? {+ async removeDirAndList(handle: SandboxHandle, removeRelDir: string, listRelDir: string): Promise<string[]> {+ const remove = removeRelDir.replace(/^\/+/, "");+ const abs = posixJoin(handle.rootDir, remove);+ const prep = remove && abs !== handle.rootDir ? `rm -rf ${shq(abs)} || exit $?; ` : "";+ const rel = listRelDir.replace(/^\/+/, "") || ".";+ const r = await exec(+ handle.id,+ `${prep}listing=$(cd ${shq(handle.rootDir)} 2>/dev/null && find ${shq(rel)} -type f 2>/dev/null) && printf '%s\\n' "$listing"; exit 0`,+ 60,+ );+ if (r.code !== 0) throw new Error(`${label} removeDir ${removeRelDir} failed: ${r.stderr}`);+ return r.stdout+ .split("\n")+ .map((s) => s.trim().replace(/^\.\//, ""))+ .filter(Boolean);+ },+ }+ : {}),async removeDir(handle, relDir): Promise<void> {const rel = relDir.replace(/^\/+/, "");if (!rel) return;diff --git a/src/sandbox/exec-sandbox-base.ts b/src/sandbox/exec-sandbox-base.tsindex af01bf42a..837a6368d 100644--- a/src/sandbox/exec-sandbox-base.ts+++ b/src/sandbox/exec-sandbox-base.ts@@ -47,6 +47,7 @@ export interface ExecSandboxBaseDeps {forgetInstance?(name: string): void;ensureEgress?(name: string): Promise<void>;installLayerTools?: LayerToolInstaller;+ combineLayerToolPrep?: boolean;}export interface ExecSandboxBase {@@ -163,9 +164,18 @@ export function createExecSandboxBase(deps: ExecSandboxBaseDeps): ExecSandboxBas};if (Object.keys(env).length) handle.env = env;const credLinks = scratch ? "" : ` && ${ephemeralCredLinkScript(homeDir, deps.credentialPaths)}`;- const prep = await deps.exec(name, `mkdir -p ${shq(workspaceDir)}${credLinks}`, PREP_TIMEOUT_SEC);- if (prep.code !== 0)- throw new Error(`${label} provision prep failed: ${execFailureDetail(prep, PREP_TIMEOUT_SEC).slice(0, 200)}`);+ const prepare = `mkdir -p ${shq(workspaceDir)}${credLinks}`;+ const toolIo = {+ exec: (script: string, t: number) => deps.exec(name, script, t),+ writeAbs: (abs: string, data: Uint8Array) => deps.writeAbsBytes(name, abs, data),+ };+ if (deps.combineLayerToolPrep && deps.installLayerTools) {+ await deps.installLayerTools(toolIo, prepare);+ } else {+ const prep = await deps.exec(name, prepare, PREP_TIMEOUT_SEC);+ if (prep.code !== 0)+ throw new Error(`${label} provision prep failed: ${execFailureDetail(prep, PREP_TIMEOUT_SEC).slice(0, 200)}`);+ }await materializeRoLayers(workspace,@@ -178,10 +188,7 @@ export function createExecSandboxBase(deps: ExecSandboxBaseDeps): ExecSandboxBas},{ manifest: RO_LAYERS_MANIFEST, tar: RO_LAYERS_TAR, label },);- await deps.installLayerTools?.({- exec: (script, t) => deps.exec(name, script, t),- writeAbs: (abs, data) => deps.writeAbsBytes(name, abs, data),- });+ if (!deps.combineLayerToolPrep) await deps.installLayerTools?.(toolIo);return handle;} catch (err) {diff --git a/src/sandbox/layer-tool-install.ts b/src/sandbox/layer-tool-install.tsindex b51faaa83..47cb02d3e 100644--- a/src/sandbox/layer-tool-install.ts+++ b/src/sandbox/layer-tool-install.ts@@ -8,7 +8,7 @@ export interface LayerToolInstallIo {writeAbs(absPath: string, data: Uint8Array): Promise<void>;}-export type LayerToolInstaller = (io: LayerToolInstallIo) => Promise<void>;+export type LayerToolInstaller = (io: LayerToolInstallIo, prepare?: string) => Promise<void>;const STEP_TIMEOUT_SEC = 60;@@ -44,11 +44,14 @@ export function layerToolContentSha(content: string): string {}export function createLayerToolInstaller(files: () => readonly LayerInstallFile[]): LayerToolInstaller {- return async (io) => {+ return async (io, prepare) => {const wanted = staged(files());- if (wanted.length === 0) return;- const probe = layerToolProbeScript(wanted);- if ((await io.exec(probe, STEP_TIMEOUT_SEC)).code === 0) return;+ if (wanted.length === 0 && !prepare) return;+ const probe = wanted.length ? layerToolProbeScript(wanted) : "true";+ const result = await io.exec(prepare ? `(${prepare}) || exit 125; ${probe}` : probe, STEP_TIMEOUT_SEC);+ if (prepare && result.code !== 0 && result.code !== 1)+ throw new Error(`sandbox provision prep failed (rc=${result.code}): ${result.stderr.slice(0, 200)}`);+ if (result.code === 0) return;const dirs = [...new Set(wanted.map((file) => dirname(file.to)))];const prep = await io.exec(`mkdir -p ${dirs.map(shq).join(" ")}`, STEP_TIMEOUT_SEC);if (prep.code !== 0) {diff --git a/src/sandbox/modal-sandbox.ts b/src/sandbox/modal-sandbox.tsindex 332bfedde..ebe19a4ef 100644--- a/src/sandbox/modal-sandbox.ts+++ b/src/sandbox/modal-sandbox.ts@@ -455,9 +455,10 @@ export function createModalSandbox(workspace: WorkspaceStore, opts: ModalSandboxwithSession(name, (session) => session.writeFileBytes(absPath, data));const readAbsBytes = (name: string, absPath: string): Promise<Uint8Array | null> =>withSession(name, (session) => session.readFileBytes(absPath));- const installLayerTools = opts.layerToolFiles ? createLayerToolInstaller(opts.layerToolFiles) : null;+ const installLayerTools = createLayerToolInstaller(opts.layerToolFiles ?? (() => []));const execFileOps = createExecFileOps({+ combineRemoveAndList: true,label: "modal",exec: (id, script, t) => execRaw(id, script, t),writeInline: (id, abs, data) => writeAbsBytes(id, abs, data),@@ -539,9 +540,13 @@ export function createModalSandbox(workspace: WorkspaceStore, opts: ModalSandboxtry {const credLinks = scratch ? "" : ` && ${ephemeralCredLinkScript(HOME_DIR, opts.credentialPaths ?? [])}`;- const prep = await execRaw(name, `mkdir -p ${shq(workspaceDir)}${credLinks}`, 60);- if (prep.code !== 0)- throw new Error(`modal provision prep failed: ${(prep.stderr || prep.stdout).slice(0, 200)}`);+ await installLayerTools(+ {+ exec: (script, t) => execRaw(name, script, t),+ writeAbs: (abs, data) => writeAbsBytes(name, abs, data),+ },+ `mkdir -p ${shq(workspaceDir)}${credLinks}`,+ );await materializeRoLayers(workspace,@@ -554,10 +559,6 @@ export function createModalSandbox(workspace: WorkspaceStore, opts: ModalSandbox},{ manifest: RO_LAYERS_MANIFEST, tar: RO_LAYERS_TAR, label: "modal" },);- await installLayerTools?.({- exec: (script, t) => execRaw(name, script, t),- writeAbs: (abs, data) => writeAbsBytes(name, abs, data),- });return handle;} catch (err) {diff --git a/src/sandbox/sandbox-routing.ts b/src/sandbox/sandbox-routing.tsindex 8bff4349b..60be520c1 100644--- a/src/sandbox/sandbox-routing.ts+++ b/src/sandbox/sandbox-routing.ts@@ -204,6 +204,14 @@ export function createSandboxRouter(opts: RoutingSandboxOptions): Sandbox {const remove = () => forHandle(handle).removeDir(handle, relDir);return useHandle(handle, remove);},+ removeDirAndList(handle, removeRelDir, listRelDir) {+ return useHandle(handle, async () => {+ const sandbox = forHandle(handle);+ if (sandbox.removeDirAndList) return sandbox.removeDirAndList(handle, removeRelDir, listRelDir);+ await sandbox.removeDir(handle, removeRelDir);+ return sandbox.listDir(handle, listRelDir);+ });+ },teardown(handle, tdOpts?: TeardownOptions): Promise<void> {return useHandle(handle, () => forHandle(handle).teardown(handle, tdOpts));},diff --git a/src/sandbox/sandbox.ts b/src/sandbox/sandbox.tsindex 6d07f8791..9d9f77e30 100644--- a/src/sandbox/sandbox.ts+++ b/src/sandbox/sandbox.ts@@ -188,6 +188,7 @@ export interface Sandbox {importFiles?(handle: SandboxHandle, entries: ReadonlyArray<{ path: string; data: Uint8Array }>): Promise<void>;listDir(handle: SandboxHandle, relDir: string): Promise<string[]>;removeDir(handle: SandboxHandle, relDir: string): Promise<void>;+ removeDirAndList?(handle: SandboxHandle, removeRelDir: string, listRelDir: string): Promise<string[]>;exportFiles?(handle: SandboxHandle, opts?: AgentComputerExportOptions): Promise<AgentComputerExportEntry[]>;startRegisteredProcess?(handle: SandboxHandle,diff --git a/src/sandbox/sprites-sandbox.ts b/src/sandbox/sprites-sandbox.tsindex 3880807af..20de650d1 100644--- a/src/sandbox/sprites-sandbox.ts+++ b/src/sandbox/sprites-sandbox.ts@@ -268,7 +268,8 @@ export function createSpritesSandbox(workspace: WorkspaceStore, opts: SpritesSanhomeDir: HOME_DIR,defaultTimeoutSec,credentialPaths: opts.credentialPaths ?? [],- ...(opts.layerToolFiles ? { installLayerTools: createLayerToolInstaller(opts.layerToolFiles) } : {}),+ installLayerTools: createLayerToolInstaller(opts.layerToolFiles ?? (() => [])),+ combineLayerToolPrep: true,egressProxyUrl: opts.egressProxyUrl,deleteFailureCode: "sprite_delete_failed",onError: opts.onError,@@ -355,6 +356,7 @@ export function createSpritesSandbox(workspace: WorkspaceStore, opts: SpritesSanconst procSessions = createExecProcessSessions(procIo);const execFileOps = createExecFileOps({+ combineRemoveAndList: true,label: "sprites",exec: (id, script, t) => execRaw(id, script, t),writeInline: (id, abs, data) => writeAbsBytes(id, abs, data),diff --git a/test/exec-file-ops.test.ts b/test/exec-file-ops.test.tsnew file mode 100644index 000000000..f8505f502--- /dev/null+++ b/test/exec-file-ops.test.ts@@ -0,0 +1,72 @@+import { test } from "node:test";+import assert from "node:assert/strict";+import { mkdtemp, mkdir, writeFile, rm, access } from "node:fs/promises";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { execFile } from "node:child_process";+import { promisify } from "node:util";+import { createExecFileOps } from "../src/sandbox/exec-file-ops.ts";++const exec = promisify(execFile);++test("combined file cleanup removes only its target and lists remaining files in one command", async () => {+ const rootDir = await mkdtemp(join(tmpdir(), "qm-cleanup-'"));+ let calls = 0;+ const ops = createExecFileOps({+ label: "test",+ combineRemoveAndList: true,+ exec: async (_id, script) => {+ calls++;+ const r = await exec("sh", ["-c", script]);+ return { ...r, code: 0 };+ },+ writeInline: async () => {},+ });+ try {+ await mkdir(join(rootDir, "turn/current"), { recursive: true });+ await mkdir(join(rootDir, "turn/keep"), { recursive: true });+ await writeFile(join(rootDir, "turn/current/file"), "remove");+ await writeFile(join(rootDir, "turn/keep/file"), "keep");+ assert.deepEqual(await ops.removeDirAndList!({ id: "test", rootDir }, "turn/current", "turn"), ["turn/keep/file"]);+ assert.equal(calls, 1);+ await assert.rejects(access(join(rootDir, "turn/current")));+ await access(join(rootDir, "turn/keep/file"));+ assert.deepEqual(await ops.removeDirAndList!({ id: "test", rootDir }, "missing", "missing"), []);+ } finally {+ await rm(rootDir, { recursive: true, force: true });+ }+});++test("combined file cleanup propagates removal and transport failures", async () => {+ const ops = createExecFileOps({+ label: "test",+ combineRemoveAndList: true,+ exec: async () => ({ code: 1, stdout: "", stderr: "permission denied" }),+ writeInline: async () => {},+ });+ await assert.rejects(+ ops.removeDirAndList!({ id: "test", rootDir: "/workspace" }, "turn", "other"),+ /permission denied/,+ );+ const broken = createExecFileOps({+ label: "test",+ combineRemoveAndList: true,+ exec: async () => {+ throw new Error("connection lost");+ },+ writeInline: async () => {},+ });+ await assert.rejects(+ broken.removeDirAndList!({ id: "test", rootDir: "/workspace" }, "turn", "other"),+ /connection lost/,+ );+});++test("combined cleanup is opt-in for provider implementations", () => {+ const ops = createExecFileOps({+ label: "test",+ exec: async () => ({ code: 0, stdout: "", stderr: "" }),+ writeInline: async () => {},+ });+ assert.equal(ops.removeDirAndList, undefined);+});diff --git a/test/layer-tool-install.test.ts b/test/layer-tool-install.test.tsindex 4d7629f5f..bd0ee3d35 100644--- a/test/layer-tool-install.test.ts+++ b/test/layer-tool-install.test.ts@@ -104,3 +104,44 @@ test("a layer with no install files never touches the machine", async () => {assert.deepEqual(io.scripts, []);assert.deepEqual(io.writes, []);});++test("combined preparation still runs with no deployment tools", async () => {+ const io = fakeIo({ probe: 0 });+ await createLayerToolInstaller(() => [])(io, "mkdir -p /workspace");+ assert.deepEqual(io.scripts, ["(mkdir -p /workspace) || exit 125; true"]);+ assert.equal(io.writes.length, 0);+});++test("failed preparation cannot be mistaken for an outdated tool", async () => {+ const io = fakeIo({ probe: 0 });+ io.exec = async () => ({ code: 125, stdout: "", stderr: "permission denied" });+ await assert.rejects(createLayerToolInstaller(() => FILES)(io, "false"), /provision prep failed.*permission denied/);+ assert.equal(io.writes.length, 0);+});++test("combined preparation installs outdated tools and snapshots the desired files once", async () => {+ let reads = 0;+ const io = fakeIo({ probe: 1 });+ const exec = io.exec;+ io.exec = async (script, timeout) => {+ if (script.startsWith("(mkdir")) {+ io.scripts.push(script);+ return { code: 1, stdout: "", stderr: "" };+ }+ return exec(script, timeout);+ };+ await createLayerToolInstaller(() => {+ reads++;+ return FILES;+ })(io, "mkdir -p /workspace");+ assert.equal(reads, 1);+ assert.equal(io.writes.length, FILES.length);+ assert.ok(io.scripts.at(-1)!.includes("chmod 0755"));+});++test("a timed-out combined preparation fails without writing deployment tools", async () => {+ const io = fakeIo({ probe: 0 });+ io.exec = async () => ({ code: 124, stdout: "", stderr: "timeout" });+ await assert.rejects(createLayerToolInstaller(() => FILES)(io, "sleep 100"), /provision prep failed/);+ assert.equal(io.writes.length, 0);+});diff --git a/test/orchestrator.test.ts b/test/orchestrator.test.tsindex f1fe21715..73bc59303 100644--- a/test/orchestrator.test.ts+++ b/test/orchestrator.test.ts@@ -3934,3 +3934,38 @@ test("default screening does not invoke a model for inbound data or tool resultsassert.equal(claims?.egress?.denyPrivateNetworks, true);assert.equal(built.modelGateway.audit().filter((rec) => rec.model === "mock-security").length, 0);});++test("ordinary turns neither probe native logins nor advertise cached login state", async () => {+ const built = freshApp();+ const checkedAt = 1;+ await built.livenessCache.put({ scopeId: scopeId("personal", "U1"), checkedAt, connectors: { gh: "active" } });+ const commands: string[] = [];+ const run = built.sandbox.run.bind(built.sandbox);+ built.sandbox.run = async (handle, command, opts) => {+ commands.push(command);+ return run(handle, command, opts);+ };+ const prompt = await built.app.turn(dm("!sysprompt"));+ assert.doesNotMatch(prompt.reply ?? "", /## Your logins|GitHub — ✓ signed in/);+ await built.app.turn(dm("!run printf ready"));+ assert.ok(commands.some((c) => c.includes("printf ready")));+ assert.ok(commands.every((c) => !c.includes("gh auth status") && !c.includes("gcloud auth print-access-token")));+ assert.equal((await built.livenessCache.get(scopeId("personal", "U1")))?.checkedAt, checkedAt);+});++for (const combined of [true, false]) {+ test(`turn cleanup retains recent and malformed paths and removes stale files (combined=${combined})`, async () => {+ const built = freshApp();+ if (!combined) built.sandbox.removeDirAndList = undefined;+ const handle = await built.sandbox.provision([{ scopeId: scopeId("personal", "U1"), mountPath: "", mode: "rw" }]);+ const old = `.agent-turn/owner/${(Date.now() - 48 * 3600_000).toString(36)}-nonce/file`;+ const recent = `.agent-turn/owner/${Date.now().toString(36)}-nonce/file`;+ const malformed = ".agent-turn/owner/!invalid/file";+ for (const path of [old, recent, malformed]) await built.sandbox.writeFile(handle, path, "retained");+ const result = await built.app.turn(dm("!run true"));+ assert.equal(result.status, "ok", result.reason);+ assert.equal(await built.sandbox.readFile(handle, old), null);+ assert.equal(await built.sandbox.readFile(handle, recent), "retained");+ assert.equal(await built.sandbox.readFile(handle, malformed), "retained");+ });+}diff --git a/test/sandbox-routing.test.ts b/test/sandbox-routing.test.tsindex 064ca7df6..a493986e6 100644--- a/test/sandbox-routing.test.ts+++ b/test/sandbox-routing.test.ts@@ -320,3 +320,54 @@ test("missing scope provider refuses substitution", async () => {});await assert.rejects(router.provision(layersFor("personal:new")), /unavailable: modal/);});++for (const combined of [true, false]) {+ test(`cleanup and listing share one resource lock (provider combined=${combined})`, async () => {+ const backend = fakeBackend("sprites");+ let locked = false;+ let acquisitions = 0;+ const calls: string[] = [];+ backend.removeDir = async (_h, path) => {+ assert.ok(locked);+ calls.push(`remove:${path}`);+ };+ backend.listDir = async (_h, path) => {+ assert.ok(locked);+ calls.push(`list:${path}`);+ return ["keep/file"];+ };+ if (combined)+ backend.removeDirAndList = async (_h, remove, list) => {+ assert.ok(locked);+ calls.push(`combined:${remove}:${list}`);+ return ["keep/file"];+ };+ const resources = {+ use: async (_id: string, action: () => Promise<unknown>) => {+ acquisitions++;+ locked = true;+ try {+ return await action();+ } finally {+ locked = false;+ }+ },+ } as unknown as import("../src/sandbox/sandbox-resources.ts").SandboxResources;+ const router = createSandboxRouter({+ backends: { sprites: backend },+ routes: createMemoryMap(),+ defaultBackend: "sprites",+ resources,+ });+ assert.deepEqual(+ await router.removeDirAndList!(+ { id: "box", rootDir: "/workspace", backend: "sprites", resourceId: "resource" },+ "old",+ "keep",+ ),+ ["keep/file"],+ );+ assert.equal(acquisitions, 1);+ assert.deepEqual(calls, combined ? ["combined:old:keep"] : ["remove:old", "list:keep"]);+ });+}diff --git a/test/scope-reach.test.ts b/test/scope-reach.test.tsindex 8c22516ed..ea5bf3319 100644--- a/test/scope-reach.test.ts+++ b/test/scope-reach.test.ts@@ -353,15 +353,13 @@ test("DM + directory + flag: execute(scope:#room) runs on that channel's own com);});-test("reach teardown destroys a visited room that has no computer of its own (no leaked box)", async () => {+test("reach preserves a durable workspace even without a login-probe cache", async () => {const built = freshApp({ reachExecEnabled: true });await built.directory.replaceChannels([{ channelId: "C-ph", name: "project-alpha" }]);- await built.app.turn(dm("!reach #project-alpha echo hi"));- assert.equal(- fakeSprites.names().some((n) => n.startsWith("qm-channel-c-ph-")),- false,- "the visitor box is destroyed",- );+ await built.app.turn(dm("!reach #project-alpha printf retained > keep.txt"));+ assert.ok(fakeSprites.names().some((n) => n.startsWith("qm-channel-c-ph-")));+ const result = await built.app.turn(dm("!reach #project-alpha cat keep.txt"));+ assert.equal(result.reply, "retained");});test("reach teardown keeps (does not destroy) a room with its own computer", async () => {diff --git a/test/swarms.test.ts b/test/swarms.test.tsindex 270875748..9df6b191d 100644--- a/test/swarms.test.ts+++ b/test/swarms.test.ts@@ -891,7 +891,6 @@ test("the orchestrator sandbox path selects worker storage without changing authquarantinedServices: [],visibleSkills: [],visibleSkillsForTurn: async () => [],- residentAuthConnectors: () => [],emitGapWork: () => {},perf: { credsMs: 0 },} as unknown as TurnSandboxContext);diff --git a/test/system-prompt-order.test.ts b/test/system-prompt-order.test.tsindex 4c19c133c..fb09f51d3 100644--- a/test/system-prompt-order.test.ts+++ b/test/system-prompt-order.test.ts@@ -189,10 +189,10 @@ test("system prompt is ordered cached-prefix → volatile tail, with memory LAST"Skills","Where you are","Where scheduled tasks post",- "Your logins","Connected apps","What you remember",];+ assert.doesNotMatch(prompt, /\n## Your logins\n/);const positions = ordered.map((title) => ({ title, at: headingAt(title) }));positions.reduce((prev, cur) => {assert.ok(cur.at > prev.at, `"## ${cur.title}" must come AFTER "## ${prev.title}" (got ${cur.at} vs ${prev.at})`);@@ -469,12 +469,11 @@ test("the system prompt is byte-identical across two turns a minute apart; the c"Skills","Where you are","Where scheduled tasks post",- "Your logins","Connected apps",]) {assert.ok(systemOf(first.reply ?? "").includes(`\n## ${title}\n`), `expected "## ${title}" in the system prompt`);}- for (const title of ["The user's local time", "What you remember"]) {+ for (const title of ["The user's local time", "What you remember", "Your logins"]) {assert.ok(!systemOf(first.reply ?? "").includes(`\n## ${title}\n`),`"## ${title}" must not be in the system prompt`,