UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

227 lines (226 loc) • 7.99 kB
import { r as captureOpenClawStateWorkerContext } from "./openclaw-state-worker-context-DAKL_dnZ.mjs"; import { i as timeWorktreePreparationPhase } from "./preparation-timing-YO2wGpby.mjs"; import { t as hashTextSha256 } from "./hash-CCe-yiZF.mjs"; import { a as withSandboxRegistrySettlement, t as acquireSandboxRuntimeUse } from "./registry-lifecycle-DLOlVYoU.mjs"; import { r as execContainer, t as DOCKER_SANDBOX_ENGINE } from "./container-engine-CscrTTHa.mjs"; import { h as containerHasTerminated, n as bindPodmanSandboxEngine, o as resolvePodmanSandboxRuntimeInfo, s as validateSandboxContainerEngineTarget } from "./podman-runtime-CUqzctoD.mjs"; import { _ as assertSandboxRegistryRetirementCurrent, g as withSandboxRegistryEntryLock, r as beginSandboxRegistryRemoval, s as readRegistry } from "./registry-kRRGa9R8.mjs"; import { t as resolveSandboxDockerUser } from "./docker-user-Cz5bZI1T.mjs"; import { n as scheduleSandboxPrune } from "./prune-BtXJ3Khz.mjs"; import path from "node:path"; //#region src/agents/sandbox/dependency-template.ts const CONTAINER_PREFIX = "openclaw-deps-"; const SCOPE_PREFIX = "worktree-dependencies:"; function dependencyDockerConfig(e, t) { return { ...e.docker, image: t, containerPrefix: CONTAINER_PREFIX, env: { CI: "1" }, setupCommand: void 0, binds: void 0 }; } var SandboxDependencyStorageError = class extends Error {}; /** The image pins the guest Node ABI and package manager, independently of the host. */ async function resolveSandboxDependencyTemplateIdentity(e, t) { if (e.backend !== "docker" && e.backend !== "podman") return; t.assertCurrent(); const n = e.backend === "podman" ? (await resolvePodmanSandboxRuntimeInfo()).target : void 0; const r = n ? bindPodmanSandboxEngine(n) : DOCKER_SANDBOX_ENGINE; t.assertCurrent(); const i = await execContainer(r, [ "image", "inspect", "--format", "{{.Id}}", e.docker.image ], { allowFailure: true, signal: t.signal }); t.assertCurrent(); const a = i.stdout.trim().replace(/^sha256:/u, ""); if (i.code !== 0 || !/^[a-f0-9]{64}$/u.test(a)) return; const o = `sha256:${a}`; const s = dependencyDockerConfig(e, o); return { key: hashTextSha256(JSON.stringify([ r.id, n ?? null, s, e.dockerTmpfsSource ])), image: o, docker: s, engine: r, podmanTarget: n }; } /** Stop every writer before publishing or deleting its template; removal is housekeeping. */ async function retireSandboxDependencyTemplate(e, t) { const n = captureOpenClawStateWorkerContext(); const assertOwner = () => { n.admission.assertCurrent(); t(); }; const { entries: r } = await readRegistry({ path: n.admission.databasePath, env: n.environment, workspaceDir: e }); assertOwner(); for (const e of r) if (e.sessionKey.startsWith(SCOPE_PREFIX)) await retireSandboxDependencyBuilder(e, assertOwner); } /** The registry retains failed disposal across shutdown and crash recovery. */ async function retireSandboxDependencyBuilder(e, t, n = false) { const r = captureOpenClawStateWorkerContext(); await withSandboxRegistryEntryLock(e, async () => { t(); const i = await beginSandboxRegistryRemoval(e, { beforeLegacyApply: t }); if (!i) return; const assertCurrent = () => { r.admission.assertCurrent(); t(); assertSandboxRegistryRetirementCurrent("container", i); }; if (i.backendId !== "docker" && i.backendId !== "podman" || i.backendId === "podman" && !i.backendTarget) throw new Error("Dependency template builder has no local engine owner; template retained"); const a = i.backendId === "podman" ? bindPodmanSandboxEngine(i.backendTarget) : DOCKER_SANDBOX_ENGINE; await validateSandboxContainerEngineTarget(a, i.backendTarget); assertCurrent(); const o = AbortSignal.timeout(12e4); const s = await execContainer(a, [ "inspect", "--format", "{{.Id}}", i.containerName ], { allowFailure: true, signal: o }); assertCurrent(); const c = s.code === 0 ? s.stdout.trim() : null; if (c !== null && !/^[a-f0-9]{64}$/u.test(c) || c === null && !/no such (?:container|object)|does not exist/iu.test(s.stderr)) throw new Error("Dependency template builder removal is unconfirmed; template retained"); if (n) { const { removeSandboxRuntimeGeneration: e } = await import("./manage-BblluQ1L.mjs"); await e({ runtime: { kind: "container", entry: i }, database: { path: r.admission.databasePath, env: r.environment, assertCurrent }, engine: a, id: c, bridges: [], assertCurrent, signal: o }); } else if (c !== null) { const e = await execContainer(a, ["kill", c], { allowFailure: true, signal: o, assertCurrent }); assertCurrent(); if (e.code === 0) await execContainer(a, ["wait", c], { signal: o, assertCurrent }); assertCurrent(); if (!await containerHasTerminated(a, c, o)) throw new Error("Dependency template builder is still running; template retained"); assertCurrent(); } }, r.admission.databasePath); } /** A disposable, credential-free sandbox is the only writer of shared dependency bytes. */ async function prepareSandboxDependencyTemplate(e) { const { identity: t, directory: n } = e; const assertCurrent = () => { e.signal?.throwIfAborted(); e.assertCurrent(); }; assertCurrent(); const r = await resolveSandboxDockerUser({ backend: e.cfg.backend, workspaceDir: n, docker: t.docker }); assertCurrent(); const i = captureOpenClawStateWorkerContext(); return await withSandboxRegistrySettlement(i, async () => { const a = await acquireSandboxRuntimeUse({ scopeKey: `${SCOPE_PREFIX}${e.scopeKey}`, workspaceDir: n }, i); let o; try { const { ensureSandboxContainer: i } = await import("./docker-B_tyYW4i.mjs"); assertCurrent(); const { containerId: a } = await timeWorktreePreparationPhase("containerStart", () => i({ engine: t.engine, podmanTarget: t.podmanTarget, scopeKey: `${SCOPE_PREFIX}${e.scopeKey}`, workspaceDir: n, agentWorkspaceDir: n, workspaceSource: "managed-worktree", readOnlyResourceMounts: [{ hostPath: path.join(n, ".git"), containerPath: path.posix.join(r.workdir, ".git") }], cfg: { ...e.cfg, scope: "session", workspaceAccess: "rw", docker: r }, assertCurrent })); assertCurrent(); await validateSandboxContainerEngineTarget(t.engine, t.podmanTarget); assertCurrent(); const s = AbortSignal.timeout(9e5); o = s; const c = await timeWorktreePreparationPhase("setup", () => execContainer(t.engine, [ "exec", "--workdir", r.workdir, a, "/bin/sh", "-c", r.network === "none" ? "exec pnpm install --offline --frozen-lockfile" : "exec pnpm install --frozen-lockfile" ], { allowFailure: true, signal: e.signal ? AbortSignal.any([e.signal, s]) : s })); assertCurrent(); if (c.code !== 0 && /ENOSPC|no\s+space\s+left\s+on\s+device/iu.test(c.stdout + c.stderr)) throw new SandboxDependencyStorageError("Sandbox dependency installation ran out of space. Free bytes and inodes in the workspace and the builder temporary package store, then retry."); return c.code === 0 ? { installed: true } : { installed: false, reason: `pnpm install failed (exit ${c.code})` }; } catch (e) { if (e instanceof SandboxDependencyStorageError) { assertCurrent(); throw e; } assertCurrent(); return { installed: false, reason: o?.aborted ? "sandbox dependency installation exceeded 15 minutes" : "sandbox dependency installation unavailable" }; } finally { try { await retireSandboxDependencyTemplate(n, e.rollbackGuard); } finally { a[Symbol.dispose](); scheduleSandboxPrune(() => i.admission.assertCurrent(), true); } } }); } //#endregion export { retireSandboxDependencyTemplate as i, resolveSandboxDependencyTemplateIdentity as n, retireSandboxDependencyBuilder as r, prepareSandboxDependencyTemplate as t };