UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

263 lines (262 loc) • 10.4 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { u as observeDiagnosticsTimelinePhases } from "./diagnostics-timeline-CSwiG24r.mjs"; import { t as _usingCtx } from "./usingCtx-rhpPzaNq.mjs"; import { n as dockerPersistentExecContainerName } from "./persistent-exec.types-DoJqKDH2.mjs"; import { r as execContainer, t as DOCKER_SANDBOX_ENGINE } from "./container-engine-CscrTTHa.mjs"; import { n as bindPodmanSandboxEngine, s as validateSandboxContainerEngineTarget } from "./podman-runtime-CUqzctoD.mjs"; import { a as dockerPersistentTargetToEngineTarget, i as dockerPersistentTargetFromEngineTarget, n as bindDockerPersistentTarget } from "./docker-persistent-target-CUAQLHHF.mjs"; import "./docker-DFBRw_yx.mjs"; import { _ as assertSandboxRegistryRetirementCurrent, a as readBrowserRegistry, n as assertSandboxRegistryEntryCurrent, s as readRegistry, t as assertSandboxBrowserRegistryEntryCurrent } from "./registry-kRRGa9R8.mjs"; import { t as BROWSER_BRIDGES } from "./browser-bridges-B4Tr1hQM.mjs"; import { t as createSandboxPersistentExecStore } from "./persistent-exec-store-XhYjFtp1.mjs"; //#region src/agents/sandbox/local-workspace-quiescence.ts /** Registered runtimes and retained stream children can write the exact private mount. */ async function readLocalWorkspaceRuntimes(e, t = {}) { const n = createSandboxPersistentExecStore(t); const r = { path: n.databasePath, env: n.environment, assertCurrent: n.assertCurrent }; const [i, a, o] = await Promise.all([ readRegistry({ ...r, workspaceDir: e }), readBrowserRegistry({ ...r, workspaceDir: e }), n.list() ]); return [ ...i.entries.map((e) => ({ kind: "container", entry: e, database: r, assertCurrent: () => assertSandboxRegistryRetirementCurrent("container", e, r) })), ...a.entries.map((e) => ({ kind: "browser", entry: e, database: r, assertCurrent: () => assertSandboxRegistryRetirementCurrent("browser", e, r) })), ...o.flatMap((t) => { const r = t.receipt; if (t.workspaceBinding.workspaceDir !== e || !r) return []; return [{ kind: "persistent", record: t, receipt: r, assertCurrent: () => n.assertCustodyCurrent(t), assertRetired: async () => { if ((await n.list({ ownerKey: t.ownerKey })).length) throw new Error("Settle persistent streams before retiring their workspace."); } }]; }) ]; } /** Inspect existing allocation custody; file reads never provision a runtime. */ async function prepareLocalWorkspaceFileRead(e) { const t = (await readLocalWorkspaceRuntimes(e.runtimeWorkspaceDir ?? e.workspaceDir)).filter((e) => e.kind !== "persistent"); const assertCurrent = () => { e.assertCurrent(); for (const e of t) if (e.kind === "container") assertSandboxRegistryEntryCurrent(e.entry); else assertSandboxBrowserRegistryEntryCurrent(e.entry); }; assertCurrent(); if (!t.length) throw new Error("Sandbox file backing is unavailable"); const { sandboxContainerShowsWorkspacePaths: n } = await import("./mount-plan-jANPcdv-.mjs"); for (const r of t) { const { engine: t, backendTarget: i, containerName: a } = localWorkspaceRuntimeEngine(r); await validateSandboxContainerEngineTarget(t, i); assertCurrent(); if (!await n({ ...e, engine: t, containerName: a, assertCurrent })) throw new Error("Sandbox file backing is unavailable"); } assertCurrent(); return assertCurrent; } function localWorkspaceRuntimeEngine(e) { const t = e.kind === "persistent" ? dockerPersistentExecContainerName(e.record.attemptId) : e.entry.containerName; const n = e.kind === "persistent" ? e.receipt.containerId : void 0; const r = e.kind === "container" ? e.entry.backendId : "docker"; const i = e.kind === "persistent" ? dockerPersistentTargetToEngineTarget(e.record.backendTarget) : e.kind === "container" ? e.entry.backendTarget : void 0; if (r !== "docker" && r !== "podman") throw new Error("Local workspace backend cannot fence host projection writes"); if (r === "podman" && !i) throw new Error("Local workspace Podman engine owner is missing"); return { engine: r === "podman" ? bindPodmanSandboxEngine(i) : i ? bindDockerPersistentTarget(dockerPersistentTargetFromEngineTarget(i)) : DOCKER_SANDBOX_ENGINE, backendTarget: i, containerName: t, expectedId: n }; } function parseLocalWorkspacePausedRuntimes(e) { if (!e) return []; const t = JSON.parse(e); if (!Array.isArray(t) || t.length > 128) throw new Error("Invalid local workspace runtime custody"); return t.map((e) => { if (!isRecord(e) || typeof e.name !== "string" || !/^[A-Za-z0-9][A-Za-z0-9_.-]*$/u.test(e.name) || typeof e.id !== "string" || !/^[a-f0-9]{64}$/u.test(e.id)) throw new Error("Invalid local workspace runtime custody"); return { name: e.name, id: e.id }; }); } /** Caller holds the projection lease through pause, reconciliation, and resume. */ async function quiesceLocalWorkspace(e) { try { var t = _usingCtx(); const n = t.u(observeDiagnosticsTimelinePhases("attempt.setup.sandbox.workspace_selection.quiesce.registry")); const r = (await readLocalWorkspaceRuntimes(e.workspaceDir, { env: e.env })).map((e) => ({ runtime: e, bridges: e.kind === "browser" ? [...BROWSER_BRIDGES].filter(([, t]) => t.containerName === e.entry.containerName) : [] })); e.assertCurrent(); let i = [...e.retained]; let a = false; const o = []; const s = []; for (const { runtime: t, bridges: a } of r) { if (t.kind === "persistent" && t.record.phase === "stopped") continue; const { engine: r, backendTarget: c, containerName: l, expectedId: u } = localWorkspaceRuntimeEngine(t); const captureRetirement = (n) => { if (t.kind === "persistent") return; s.push(async () => { const { removeSandboxRuntimeGeneration: i } = await import("./manage-BblluQ1L.mjs"); await i({ runtime: t, database: t.database, engine: r, id: n, bridges: a, assertCurrent: e.assertCurrent, assertGenerationCurrent: t.assertCurrent }); }); }; n.mark("attempt.setup.sandbox.workspace_selection.quiesce.engine_target"); await validateSandboxContainerEngineTarget(r, c); e.assertCurrent(); n.mark("attempt.setup.sandbox.workspace_selection.quiesce.authority"); await t.assertCurrent(); n.mark("attempt.setup.sandbox.workspace_selection.quiesce.inspect"); const d = await execContainer(r, [ "inspect", "-f", "{{.Id}} {{.State.Running}} {{.State.Paused}}", u ?? l ], { allowFailure: true, signal: AbortSignal.timeout(3e4) }); e.assertCurrent(); if (d.code !== 0) { if (/no such (?:container|object)|does not exist/iu.test(d.stderr)) { n.mark("attempt.setup.sandbox.workspace_selection.quiesce.authority"); await t.assertCurrent(); captureRetirement(null); continue; } throw new Error("Local workspace runtime could not be inspected; workspace preserved"); } const [f, p, m] = d.stdout.trim().split(/\s+/u); if (!f || !/^[a-f0-9]{64}$/u.test(f) || u !== void 0 && f !== u || !["true", "false"].includes(p ?? "") || !["true", "false"].includes(m ?? "")) throw new Error("Invalid local workspace runtime inspection"); n.mark("attempt.setup.sandbox.workspace_selection.quiesce.authority"); await t.assertCurrent(); captureRetirement(f); if (p !== "true" && m !== "true") continue; const h = i.some((e) => e.name === l && e.id === f); if (m === "true" && !h) throw new Error("Local workspace runtime was paused by another owner; resume it before retrying"); if (!h) { i.push({ name: l, id: f }); n.mark("attempt.setup.sandbox.workspace_selection.quiesce.persist"); await e.persist(i); } if (m !== "true") { e.assertCurrent(); n.mark("attempt.setup.sandbox.workspace_selection.quiesce.authority"); await t.assertCurrent(); n.mark("attempt.setup.sandbox.workspace_selection.quiesce.pause"); await execContainer(r, ["pause", f], { signal: AbortSignal.timeout(3e4) }); } o.push(async () => { try { var n = _usingCtx(); const a = n.u(observeDiagnosticsTimelinePhases("attempt.setup.sandbox.workspace_selection.resume.engine_target")); await validateSandboxContainerEngineTarget(r, c); e.assertCurrent(); a.mark("attempt.setup.sandbox.workspace_selection.resume.inspect"); const o = await execContainer(r, [ "inspect", "-f", "{{.State.Paused}}", f ], { allowFailure: true, signal: AbortSignal.timeout(3e4) }); e.assertCurrent(); if (o.code !== 0) { if (!/no such (?:container|object)|does not exist/iu.test(o.stderr)) throw new Error("Local workspace runtime resume inspection failed; exact paused owner retained"); } else if (o.stdout.trim() === "true") { a.mark("attempt.setup.sandbox.workspace_selection.resume.authority"); await t.assertCurrent(); a.mark("attempt.setup.sandbox.workspace_selection.resume.unpause"); const e = await execContainer(r, ["unpause", f], { allowFailure: true, signal: AbortSignal.timeout(3e4) }); if (e.code !== 0 && !/no such (?:container|object)|does not exist/iu.test(e.stderr)) throw new Error("Local workspace runtime resume failed; exact paused owner retained"); } else if (o.stdout.trim() !== "false") throw new Error("Invalid local workspace runtime resume inspection"); e.assertCurrent(); i = i.filter((e) => e.name !== l || e.id !== f); a.mark("attempt.setup.sandbox.workspace_selection.resume.persist"); await e.persist(i); } catch (e) { n.e = e; } finally { n.d(); } }); } return { get retired() { return a; }, retire: async () => { e.assertCurrent(); for (const { runtime: e } of r) if (e.kind === "persistent") await e.assertRetired(); e.assertCurrent(); for (const e of s) await e(); e.assertCurrent(); if (i.length) { await e.persist([]); i = []; } a = true; }, resume: async () => { if (a) return; e.assertCurrent(); for (const e of o.toReversed()) await e(); e.assertCurrent(); if (i.length) await e.persist([]); } }; } catch (e) { t.e = e; } finally { t.d(); } } //#endregion export { parseLocalWorkspacePausedRuntimes, prepareLocalWorkspaceFileRead, quiesceLocalWorkspace, readLocalWorkspaceRuntimes };