UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 4.1 kB
import{Bt as e,Kt as t,L as n,St as r,Tt as i,pt as a,t as o}from"./disk-budget.worker-subsystem-9qP7ka6r.mjs";import{t as s}from"./disk-budget.worker-errors-DTPr0dgo.mjs";import c from"node:fs/promises";import l from"node:os";import u from"node:path";async function runBestEffortCleanup(e){try{return await e.cleanup()}catch(t){try{e.onError?.(t)}catch{}return}}const d=o(`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(u.sep),i=n.indexOf(`node_modules`),a=[n.slice(0,i>=0?i+1:n.length).join(u.sep),...t],o=u.dirname(e),s=[o];for(;a.some(e=>r(e,o));){let e=u.dirname(o);if(e===o)break;o=e,s.push(o)}return s}async function isRetainedRuntimeMarker(t){if(await c.realpath(t).catch(()=>void 0)!==t)return!1;let n=JSON.parse(await c.readFile(t,`utf8`));return e(n)&&n.name===`openclaw`}async function recognizesRetainedRuntime(e,t){let n=u.parse(t).root,r=u.join(e,`tree`,Buffer.from(n).toString(`hex`)),i=u.join(r,u.relative(n,t));if(await isRetainedRuntimeMarker(u.join(i,`package.json`)))return!0;let a=u.dirname(i),o=u.dirname(a),s=u.dirname(o);if(u.basename(i)!==`openclaw`||u.basename(a)!==`node_modules`||!u.basename(o).startsWith(`openclaw@`)||u.basename(s)!==`.pnpm`||await c.realpath(s).catch(()=>void 0)!==s)return!1;let l=0;for await(let e of await c.opendir(s)){if(++l>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(u.join(s,e.name,`node_modules/openclaw/package.json`)))return!0}return!1}async function maintainRetainedUpdateRuntimes(e){let t=[],n=e.packageRoots.map(a),r=new Set([l.tmpdir(),...e.temporaryDirectories??[],...n.flatMap(e=>resolveRetainedUpdateRuntimeParents(e))].map(a));for(let a of r)try{for(let r of await c.readdir(a,{withFileTypes:!0})){if(!/^openclaw-update-runtime-[A-Za-z0-9]{6}$/u.test(r.name))continue;let o=u.join(a,r.name);try{let r=await c.lstat(o);if(!r.isDirectory()||process.getuid&&r.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 i=!1;for(let e of n)if(await recognizesRetainedRuntime(o,e)){i=!0;break}if(!i)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(`./disk-budget.worker-openclaw-process-census-P8ZezzLU.mjs`),l=await c.lstat(o);if(l.dev!==r.dev||l.ino!==r.ino||l.ctimeMs!==r.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){i(e,`ENOENT`)||t.push(reportRetainedUpdateRuntime(o,s(e)))}}}catch(e){i(e,`ENOENT`)||t.push(`Cannot inspect updater runtimes in ${a}: ${s(e)}`)}return t}function removeTemporaryArtifacts(e,t,r=r=>d.warn(n(s(`${t} cleanup failed; files may remain in ${e}. After the worker or session stops, check permissions and remove the retained directory: ${s(r)}`),1024))){return runBestEffortCleanup({cleanup:()=>c.rm(e,{recursive:!0,force:!0}),onError:r})}export{maintainRetainedUpdateRuntimes,registerRetainedUpdateRuntime,removeTemporaryArtifacts,reportRetainedUpdateRuntime,resolveRetainedUpdateRuntimeParents};