openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
314 lines (313 loc) • 9.18 kB
JavaScript
import { u as observeDiagnosticsTimelinePhases } from "./diagnostics-timeline-CSwiG24r.mjs";
import { t as _usingCtx } from "./usingCtx-rhpPzaNq.mjs";
import { a as withWorktreeMutationLease, f as WorktreeCapacityContentionError, r as waitForWorktreeCapacity, t as WORKTREE_CREATE_LEASE_WAIT_MS } from "./allocation-DPkHxKtM.mjs";
import { n as setWorktreePreparationTemplate, r as startWorktreePreparationPhase, t as markManagedWorktreePreparation } from "./preparation-timing-YO2wGpby.mjs";
import { t as acquireSandboxRuntimeUse } from "./registry-lifecycle-DLOlVYoU.mjs";
import { r as getSandboxPersistentExecCapability, t as bindSandboxPersistentExecCapability } from "./persistent-exec.types-DoJqKDH2.mjs";
import { r as getSandboxInputStager, t as bindSandboxInputStager } from "./input-staging-CTBgfklv.mjs";
import { r as localWorkspaceProjectionPath } from "./local-workspace-checkout-D7yp8kUm.mjs";
import { i as withLocalWorkspaceProjection, r as resolveLocalWorkspaceOwner, t as checkpointLocalWorkspace } from "./local-workspace-projection-d69eRBoO.mjs";
import path from "node:path";
//#region src/agents/sandbox/local-workspace.ts
function assertLocalSandboxWorkspaceBackend(e) {
if (e !== "docker" && e !== "podman") throw new Error("Managed guest projects require a local Docker or Podman sandbox; this backend cannot safely reconcile a local managed checkout");
}
/** A writable project is an exact managed projection, never a role-policy override. */
async function prepareLocalSandboxWorkspace(e) {
try {
var t = _usingCtx();
const n = t.u(observeDiagnosticsTimelinePhases("attempt.setup.sandbox.workspace_selection.owner"));
e.assertCurrent?.();
const r = await resolveLocalWorkspaceOwner({
...e,
assertCurrent: e.assertHost
});
if (!r) {
e.assertCurrent?.();
return;
}
let i = false;
let a = 0;
const o = new DisposableStack();
o.use(r);
const releaseOwner = () => {
if (i && a === 0) o.dispose();
};
const retainOwner = () => {
r.assertCurrent();
a++;
let e = false;
return () => {
if (!e) {
e = true;
a--;
releaseOwner();
}
};
};
const assertCurrent = () => {
e.assertCurrent?.();
if (i) throw new Error("Local sandbox workspace authority changed");
r.assertCurrent();
};
const s = {
...r,
assertCurrent,
workerAuthority: {
...r.workerAuthority,
assertCurrent
}
};
const checkpoint = () => checkpointLocalWorkspace(r);
try {
o.use(await acquireSandboxRuntimeUse({ workspaceDir: localWorkspaceProjectionPath(r) }));
assertCurrent();
markManagedWorktreePreparation();
assertLocalSandboxWorkspaceBackend(e.sandbox.backend);
n.mark("attempt.setup.sandbox.workspace_selection.reuse.acquire");
const t = await withLocalWorkspaceProjection(s, async (e) => {
n.mark("attempt.setup.sandbox.workspace_selection.reuse.reconcile");
try {
return await e.reuse();
} finally {
n.mark("attempt.setup.sandbox.workspace_selection.reuse.release");
}
}, { signal: e.signal });
let a;
if (t) {
setWorktreePreparationTemplate("reused");
a = t;
} else if (e.cfg.worktreeAcceleration === false) {
n.mark("attempt.setup.sandbox.workspace_selection.prepare.acquire");
a = await withLocalWorkspaceProjection(s, async (e) => {
n.mark("attempt.setup.sandbox.workspace_selection.prepare.reconcile");
try {
return await e.prepare();
} finally {
n.mark("attempt.setup.sandbox.workspace_selection.prepare.release");
}
}, { signal: e.signal });
} else {
n.mark("attempt.setup.sandbox.workspace_selection.allocation");
const t = startWorktreePreparationPhase("allocate");
const i = {
env: r.env ?? process.env,
id: r.worktree.id,
signal: e.signal,
commitGuard: assertCurrent,
workerAuthority: s.workerAuthority,
waitBudget: { remainingMs: WORKTREE_CREATE_LEASE_WAIT_MS }
};
try {
for (;;) try {
a = await withWorktreeMutationLease(i, (r) => {
t();
n.mark("attempt.setup.sandbox.workspace_selection.prepare.acquire");
return withLocalWorkspaceProjection({
...s,
assertCurrent: r.commitGuard,
workerAuthority: r.workerAuthority
}, async (t) => {
n.mark("attempt.setup.sandbox.workspace_selection.prepare.reconcile");
try {
return await t.prepare({
sandbox: e.sandbox,
allocation: r
});
} finally {
n.mark("attempt.setup.sandbox.workspace_selection.prepare.release");
}
}, { signal: e.signal });
});
break;
} catch (e) {
if (!(e instanceof WorktreeCapacityContentionError)) throw e;
await waitForWorktreeCapacity(e, i);
}
} finally {
t();
}
}
assertCurrent();
return {
workspaceDir: a,
workspaceCwd: path.join(a, path.relative(r.worktree.path, e.workspaceDir ?? r.worktree.path)),
assertCurrent,
protectRuntimeScope: async (e) => {
const t = await acquireSandboxRuntimeUse({ scopeKey: e });
try {
assertCurrent();
o.use(t);
} catch (e) {
t[Symbol.dispose]();
throw e;
}
},
retain: () => {
assertCurrent();
return retainOwner();
},
provision: async (e, t) => await withLocalWorkspaceProjection(s, async () => {
assertCurrent();
const t = await e();
assertCurrent();
return t;
}, {
provision: true,
signal: t
}),
terminate: async (e) => {
const t = retainOwner();
try {
await withLocalWorkspaceProjection(r, e, { provision: true });
await checkpoint();
} finally {
t();
}
},
checkpoint,
[Symbol.dispose]: () => {
i = true;
releaseOwner();
}
};
} catch (e) {
o.dispose();
throw e;
}
} catch (e) {
t.e = e;
} finally {
t.d();
}
}
/** All harnesses use the same backend/bridge, including native Codex execution. */
function bindLocalSandboxWorkspace(e, t) {
const n = e.backend;
if (!n) throw new Error("Managed guest project has no sandbox execution owner");
const r = getSandboxPersistentExecCapability(n);
if (r) bindSandboxPersistentExecCapability(n, {
...r,
prepare: async (e) => {
const n = await t.provision(() => r.prepare({
...e,
assertCurrent: () => {
t.assertCurrent();
e.assertCurrent();
}
}));
return {
...n,
start: (e) => t.provision(() => n.start(e))
};
}
});
const i = n.prepareProcessCleanup?.bind(n);
if (i) n.prepareProcessCleanup = (e) => {
t.assertCurrent();
const n = i(e);
return {
env: n.env,
terminate: () => t.terminate(() => n.terminate()),
interrupt: (e) => {
const r = AbortSignal.timeout(e);
const i = performance.now() + e;
return t.provision(() => {
r.throwIfAborted();
return n.interrupt(Math.max(0, Math.ceil(i - performance.now())));
}, r);
}
};
};
const a = n.runShellCommand.bind(n);
n.runShellCommand = (e) => t.provision(async () => {
e.signal?.throwIfAborted();
t.assertCurrent();
return await a(e);
}, e.signal);
const o = n.buildExecSpec.bind(n);
const s = n.finalizeExec?.bind(n);
const c = /* @__PURE__ */ new Map();
n.buildExecSpec = async (e) => {
const n = t.retain();
let r;
try {
const i = await t.provision(async () => {
t.assertCurrent();
r = await o(e);
t.assertCurrent();
return r;
});
const a = {};
c.set(a, {
token: i.finalizeToken,
release: n
});
const s = i.assertCurrent;
return {
...i,
finalizeToken: a,
assertCurrent: () => {
t.assertCurrent();
s?.();
}
};
} catch (e) {
try {
if (r) await s?.({
status: "failed",
exitCode: null,
timedOut: false,
token: r.finalizeToken
});
} finally {
n();
}
throw e;
}
};
n.finalizeExec = async (e) => {
const n = c.get(e.token);
if (!n) throw new Error("Managed workspace command finalization has no prepared owner");
c.delete(e.token);
try {
await s?.({
...e,
token: n.token
});
await t.checkpoint();
} finally {
n.release();
}
};
const l = e.fsBridge;
if (!l) throw new Error("Managed guest project has no filesystem bridge");
const mutate = async (e) => {
const n = t.retain();
try {
return await e();
} finally {
try {
await t.checkpoint();
} finally {
n();
}
}
};
const u = l.writeFile.bind(l);
const d = l.mkdirp.bind(l);
const f = l.remove.bind(l);
const p = l.rename.bind(l);
const m = l.copyFile?.bind(l);
const h = l.createFileExclusive?.bind(l);
l.writeFile = (e) => mutate(() => u(e));
l.mkdirp = (e) => mutate(() => d(e));
l.remove = (e) => mutate(() => f(e));
l.rename = (e) => mutate(() => p(e));
if (m) l.copyFile = (e) => mutate(() => m(e));
if (h) l.createFileExclusive = (e) => mutate(() => h(e));
const g = getSandboxInputStager(l);
if (g) bindSandboxInputStager(l, (e) => mutate(() => t.provision(() => g(e), e.signal)));
}
//#endregion
export { bindLocalSandboxWorkspace as n, prepareLocalSandboxWorkspace as r, assertLocalSandboxWorkspaceBackend as t };