UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 4.23 kB
import{F as e,N as t,f as n,h as r,t as i}from"./git-operation.worker-subsystem-DibpW_lB.mjs";import{n as a}from"./git-operation.worker-record-coerce-B9rMjEA7.mjs";import{n as o}from"./git-operation.worker-path-guards-B3dt-SRh.mjs";import{t as s}from"./git-operation.worker-errors-6WvbxYXj.mjs";import c from"node:fs/promises";import l from"node:path";import u from"node:os";async function runBestEffortCleanup(e){try{return await e.cleanup()}catch(t){try{e.onError?.(t)}catch{}return}}const d=i(`infra:temp-artifacts`),f=t(Symbol.for(`openclaw.retainedUpdateRuntimes`),()=>/* @__PURE__ */ new Set);function registerRetainedUpdateRuntime(e){return f.add(e),()=>void f.delete(e)}function reportRetainedUpdateRuntime(e,t){let n=`Runtime retained at ${e}: ${t}`;try{d.warn(n)}catch{}return n}function resolveRetainedUpdateRuntimeParents(e,t=[]){let n=e.split(l.sep),r=n.indexOf(`node_modules`),i=[n.slice(0,r>=0?r+1:n.length).join(l.sep),...t],a=l.dirname(e),s=[a];for(;i.some(e=>o(e,a));){let e=l.dirname(a);if(e===a)break;a=e,s.push(a)}return s}async function isRetainedRuntimeMarker(e){if(await c.realpath(e).catch(()=>void 0)!==e)return!1;let t=JSON.parse(await c.readFile(e,`utf8`));return a(t)&&t.name===`openclaw`}async function recognizesRetainedRuntime(e,t){let n=l.parse(t).root,r=l.join(e,`tree`,Buffer.from(n).toString(`hex`)),i=l.join(r,l.relative(n,t));if(await isRetainedRuntimeMarker(l.join(i,`package.json`)))return!0;let a=l.dirname(i),o=l.dirname(a),s=l.dirname(o);if(l.basename(i)!==`openclaw`||l.basename(a)!==`node_modules`||!l.basename(o).startsWith(`openclaw@`)||l.basename(s)!==`.pnpm`||await c.realpath(s).catch(()=>void 0)!==s)return!1;let u=0;for await(let e of await c.opendir(s)){if(++u>4096)throw Error(`retained pnpm version inventory exceeds the bounded lookup; inspect it before manual cleanup`);if(e.isDirectory()&&e.name.startsWith(`openclaw@`)&&await isRetainedRuntimeMarker(l.join(s,e.name,`node_modules/openclaw/package.json`)))return!0}return!1}async function maintainRetainedUpdateRuntimes(e){let t=[],i=e.packageRoots.map(n),a=new Set([u.tmpdir(),...e.temporaryDirectories??[],...i.flatMap(e=>resolveRetainedUpdateRuntimeParents(e))].map(n));for(let n of a)try{for(let a of await c.readdir(n,{withFileTypes:!0})){if(!/^openclaw-update-runtime-[A-Za-z0-9]{6}$/u.test(a.name))continue;let o=l.join(n,a.name);try{let n=await c.lstat(o);if(!n.isDirectory()||process.getuid&&n.uid!==process.getuid())throw Error(`directory ownership is unknown; inspect it before manual cleanup`);if(f.has(o))throw Error(`the creating update still owns this runtime`);let r=!1;for(let e of i)if(await recognizesRetainedRuntime(o,e)){r=!0;break}if(!r)throw Error(`no recognized runtime marker; inspect it before manual cleanup`);if(!e.repair){t.push(`Runtime retained at ${o}: run \`openclaw doctor --fix\` after its workers stop`);continue}let{inspectOtherOpenClawProcesses:a}=await import(`./git-operation.worker-openclaw-process-census-DHPpO5Qq.mjs`),l=await c.lstat(o);if(l.dev!==n.dev||l.ino!==n.ino||l.ctimeMs!==n.ctimeMs||f.has(o))throw Error(`directory identity or custody changed`);e.assertCurrent();let u=a(o);if(`error`in u)throw Error(u.error);for(let e of u.warnings??[])t.includes(e)||t.push(e);if(u.pids.length)throw Error(`other OpenClaw processes are still running (PIDs: ${u.pids.join(`, `)}); let these holders finish, then rerun openclaw doctor --fix`);e.assertResourcesSettled?.(o),e.assertCurrent();let d;await removeTemporaryArtifacts(o,`Updater runtime`,e=>{d=`cleanup failed: ${s(e)}`}),t.push(d?reportRetainedUpdateRuntime(o,d):`Removed abandoned updater runtime: ${o}`)}catch(e){r(e,`ENOENT`)||t.push(reportRetainedUpdateRuntime(o,s(e)))}}}catch(e){r(e,`ENOENT`)||t.push(`Cannot inspect updater runtimes in ${n}: ${s(e)}`)}return t}function removeTemporaryArtifacts(t,n,r=r=>d.warn(e(s(`${n} cleanup failed; files may remain in ${t}. After the worker or session stops, check permissions and remove the retained directory: ${s(r)}`),1024))){return runBestEffortCleanup({cleanup:()=>c.rm(t,{recursive:!0,force:!0}),onError:r})}export{maintainRetainedUpdateRuntimes,registerRetainedUpdateRuntime,removeTemporaryArtifacts,reportRetainedUpdateRuntime,resolveRetainedUpdateRuntimeParents};