UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

118 lines (117 loc) • 4.6 kB
import { p as toErrorObject } from "./error-coercion-hDrHOSGy.mjs"; import { i as createAbortError } from "./src-BPmQBLJp.mjs"; import "./abort-signal-D4Q6SEw7.mjs"; import "./errors-BKzCeTYb.mjs"; import { o as isPlainCommandExitFailure } from "./exec-result-DJMhztYN.mjs"; import { c as spawnCommand } from "./exec-spawn-DMKoejF-.mjs"; import "./exec-aRIvS9s4.mjs"; import { x as SANDBOX_COMMAND_MAX_BUFFER_BYTES } from "./constants-Dxjr8D5t.mjs"; import { t as stripGatewayAllocatorEnv } from "./child-env-CrZt0xN6.mjs"; //#region src/agents/sandbox/container-keyring.ts function isSandboxKeyringQuotaError(e) { return e instanceof Error && /create keyring/iu.test(e.message) && /disk quota exceeded|\bEDQUOT\b/iu.test(e.message); } function sandboxKeyringQuotaError(e) { return new Error("Sandbox cannot start: the container engine host's per-user kernel keyring quota is exhausted. Ask the host operator to raise sysctl kernel.keys.maxkeys and kernel.keys.maxbytes above their current values, persist both in /etc/sysctl.d, or reduce running sandboxes, then retry.", { cause: e }); } //#endregion //#region src/agents/sandbox/container-engine.ts /** * Shared local container-engine process execution and backend selection. */ const DOCKER_SANDBOX_ENGINE = { id: "docker", command: "docker", displayName: "Docker" }; const PODMAN_SANDBOX_ENGINE = { id: "podman", command: "podman", displayName: "Podman" }; function missingContainerEngineMessage(e) { if (e.id === "docker") return "Sandbox mode requires Docker, but the \"docker\" command was not found in PATH. Install Docker (and check that \"docker\" is available), or set `agents.defaults.sandbox.mode=off` to disable sandboxing."; return "Sandbox mode requires Podman, but the \"podman\" command was not found in PATH. Install Podman (and check that \"podman\" is available), choose another sandbox backend, or set `agents.defaults.sandbox.mode=off` to disable sandboxing."; } async function execContainerRaw(e, t, n) { let r; try { const i = { ...e.baseEnv ?? process.env }; stripGatewayAllocatorEnv(i); n?.assertCurrent?.(); r = await spawnCommand([ e.command, ...e.globalArgs ?? [], ...t ], { baseEnv: i, cancelSignal: n?.signal, encoding: "buffer", input: n?.input ?? Buffer.alloc(0), maxBuffer: SANDBOX_COMMAND_MAX_BUFFER_BYTES, reject: false, stripFinalNewline: false }); } catch (t) { if (n?.signal?.aborted) throw createAbortError("Aborted"); if (t.code === "ENOENT") throw Object.assign(new Error(missingContainerEngineMessage(e)), { code: "INVALID_CONFIG", cause: t }); throw t; } if (n?.signal?.aborted || r.isCanceled) throw createAbortError("Aborted"); if (r.failed && !isPlainCommandExitFailure(r)) { if (r.code === "ENOENT") throw Object.assign(new Error(missingContainerEngineMessage(e)), { code: "INVALID_CONFIG", cause: r }); throw toErrorObject(r, `${e.displayName} command execution failed`); } const i = Buffer.from(r.stdout); const a = Buffer.from(r.stderr); const o = r.exitCode ?? (r.failed ? 1 : 0); if (o !== 0 && !n?.allowFailure) { let n = a.length > 0 ? a.toString("utf8").trim() : ""; if (e.id === "podman" && t[0] === "create" && /^(?:Error: )?(?:lookup init binary|container-init binary not found on the host):/mu.test(n)) n += "\nInstall catatonit on the Podman engine host, or repair its configured init_path/helper_binaries_dir in containers.conf, then retry. The init executable must be available to the engine, not only inside the sandbox image. Keep --init and sandboxing enabled so orphaned processes are reaped."; throw Object.assign(new Error(n || `${e.displayName} command failed (exit ${o})`), { code: o, stdout: i, stderr: a }); } return { stdout: i, stderr: a, code: o }; } async function execContainer(e, t, n) { let r; try { r = await execContainerRaw(e, t, n); } catch (i) { if (!["create", "start"].includes(t[0] ?? "") || !isSandboxKeyringQuotaError(i)) throw i; const { reclaimIdleSandboxContainer: a } = await import("./container-keyring-recovery-BEInN4Dx.mjs"); try { await a({ engine: e, assertCurrent: n?.assertCurrent }); } catch {} n?.assertCurrent?.(); try { r = await execContainerRaw(e, t, n); } catch (e) { if (!isSandboxKeyringQuotaError(e)) throw e; throw sandboxKeyringQuotaError(e); } } return { stdout: r.stdout.toString("utf8"), stderr: r.stderr.toString("utf8"), code: r.code }; } //#endregion export { execContainerRaw as i, PODMAN_SANDBOX_ENGINE as n, execContainer as r, DOCKER_SANDBOX_ENGINE as t };