UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

4 lines • 5.87 kB
import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{r as isPathInside}from"./path-guards-DGttsOJI.mjs";import{t as decodeMountInfoPath}from"./mountinfo-path-TifqQAkt.mjs";import{a as splitSandboxBindSpec,t as getSandboxHostPathPolicyKey}from"./host-paths-CmrZGmeh.mjs";import{t as isPathInsideContainerRoot}from"./path-utils-C1CmrIel.mjs";import{n as normalizeMountContainerPath}from"./workspace-mounts-DefzGu84.mjs";import{i as execContainer}from"./container-engine-BMQJ1AeL.mjs";import{n as resolveDockerSourceNamespace,r as translateSandboxMountSources,t as parseInspectedSandboxMounts}from"./docker-mount-source-jNl6WD29.mjs";import path from"node:path"; //!src/agents/sandbox/mount-plan.ts async function prepareSandboxMountPlan(e){const{engine:t,assertCurrent:n,...r}=e;const i=await resolveDockerSourceNamespace(t);const{prepareSandboxMounts:a}=await import(`./refresh-DDFbJ_aI.mjs`);return await a({...r,namespace:i},n,t.stateGuard?.stateOwnerPath)}async function inspectSandboxMounts(e){const t=await execContainer(e.engine,[`inspect`,`--format`,`{"Mounts":{{json .Mounts}},"Tmpfs":{{json .HostConfig.Tmpfs}}}`,e.containerName],{signal:AbortSignal.timeout(5e3)});const n=JSON.parse(t.stdout);if(!isRecord(n))throw new Error(`Container inspect did not return mount metadata.`);return parseInspectedSandboxMounts(n.Mounts,n.Tmpfs)}async function resolveSandboxContainerOnlyMounts(e){e.assertCurrent?.();const t=await inspectSandboxMounts(e);e.assertCurrent?.();const{stdout:n}=await execContainer(e.engine,[`exec`,e.containerName,`cat`,`/proc/self/mountinfo`],{signal:AbortSignal.timeout(5e3)});const r=/* @__PURE__ */ new Map;for(const e of n.split(` `)){if(!e)continue;const t=e.indexOf(` - `);const[n,i,a,o,s,c]=e.slice(0,t).split(` `);if(t<0||!n||!i||!a||!o||!s||!c)throw new Error(`Container mountinfo returned an invalid mount entry.`);r.set(n,{id:n,parent:i,destination:decodeMountInfoPath(s),backing:`${a}\0${decodeMountInfoPath(o)}`,writable:c.split(`,`).includes(`rw`)})}if(r.size===0)throw new Error(`Container mountinfo did not return a mount table.`);const i=/* @__PURE__ */ new Map;const a=/* @__PURE__ */ new Map;const o=/* @__PURE__ */ new Set;for(const e of r.values()){const t=i.get(e.destination)??[];t.push(e);i.set(e.destination,t);const n=a.get(e.parent)??[];n.push(e);a.set(e.parent,n);if(r.get(e.parent)?.destination===e.destination&&e.parent!==e.id)o.add(e.parent)}const visible=e=>{const t=/* @__PURE__ */ new Set;for(let n=e;!t.has(n.id);){t.add(n.id);const e=r.get(n.parent);if(!e||e.id===n.id)return true;if(e.destination!==n.destination&&o.has(e.id))return false;if(a.get(n.parent)?.some(t=>t.id!==e.id&&t.destination!==n.destination&&isPathInsideContainerRoot(t.destination,n.destination)))return false;n=e}return false};const s=new Set(i.keys());for(const e of t){const t=i.get(e.destination)??[];const n=t.filter(e=>!o.has(e.id)&&visible(e));const r=n[0];if(e.type===`bind`&&n.length===1&&r&&r.writable===e.writable&&t.every(e=>e.backing===r.backing))s.delete(e.destination);else s.add(e.destination)}return[...s]}async function compareSandboxMountPlan(e){const t=await inspectSandboxMounts(e);const n=new Map(e.plan.binds.flatMap(e=>{const t=splitSandboxBindSpec(e);if(!t)return[];return[[normalizeMountContainerPath(t.container),t]]}));const coversBind=e=>[...n.keys()].some(t=>isPathInside(t,e));const r=new Map(e.plan.tmpfs.filter(e=>coversBind(e.containerPath)).map(e=>[e.containerPath,e]));const i=t.filter(e=>e.type===`bind`||n.has(e.destination)||e.type===`tmpfs`&&coversBind(e.destination));const mountMatches=e=>{if(e.type===`tmpfs`){const t=r.get(e.destination);return t!==void 0&&e.writable===!t.readOnly}const t=n.get(e.destination);return t!==void 0&&e.type===`bind`&&getSandboxHostPathPolicyKey(e.source)===getSandboxHostPathPolicyKey(t.host)&&e.writable===!t.options.split(`,`).includes(`ro`)};if(i.length===n.size+r.size&&i.every(mountMatches))return`match`;const a=new Set(t.map(e=>e.destination));const o=[...e.plan.protectedTargets,...e.plan.pinnedAncestors??[]].filter(e=>!a.has(e));return t.every(e=>e.type===`bind`||e.type===`tmpfs`)&&i.length+o.length===n.size+r.size&&i.every(mountMatches)?`protected-mounts-added`:`changed`}async function sandboxContainerShowsWorkspacePaths(e){e.assertCurrent();const t=await execContainer(e.engine,[`inspect`,`-f`,`{{.Id}} {{.State.Running}} {{.State.Paused}}`,e.containerName],{signal:AbortSignal.timeout(5e3)});e.assertCurrent();const[n,r,i]=t.stdout.trim().split(/\s+/u);if(!n||!/^[a-f0-9]{64}$/u.test(n)||![`true`,`false`].includes(r??``)||i!==`false`)return false;const a={...e,containerName:n};const o=await resolveDockerSourceNamespace(e.engine);e.assertCurrent();const s=o?translateSandboxMountSources({source:e.workspaceDir,containerPath:e.containerRoot,allowedRoots:[e.workspaceDir],mounts:o,readOnly:true,shadowedTargets:[]}):[{hostPath:e.workspaceDir,containerPath:normalizeMountContainerPath(e.containerRoot)}];const c=await inspectSandboxMounts(a);e.assertCurrent();const l=r===`true`?await resolveSandboxContainerOnlyMounts(a):[];e.assertCurrent();return e.targets.every(e=>{const t=normalizeMountContainerPath(e);const n=c.filter(e=>isPathInsideContainerRoot(e.destination,t)).toSorted((e,t)=>t.destination.length-e.destination.length)[0];const r=n===void 0?void 0:s.filter(e=>isPathInsideContainerRoot(e.containerPath,n.destination)).toSorted((e,t)=>t.containerPath.length-e.containerPath.length).map(e=>({hostPath:path.posix.join(e.hostPath,path.posix.relative(e.containerPath,n.destination))}))[0];return n?.type===`bind`&&r!==void 0&&getSandboxHostPathPolicyKey(n.source)===getSandboxHostPathPolicyKey(r.hostPath)&&!l.some(e=>isPathInsideContainerRoot(e,t)&&e.length>=n.destination.length)})}export{sandboxContainerShowsWorkspacePaths as i,prepareSandboxMountPlan as n,resolveSandboxContainerOnlyMounts as r,compareSandboxMountPlan as t};