openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 4.16 kB
JavaScript
import{Cn as e,Tt as t,rt as n,t as r,xt as i}from"./openclaw-state-read-subsystem-Dwoo5bcz.mjs";import{Lt as a,Pt as o}from"./openclaw-state-read-src-RL5xtYG2.mjs";import{t as s}from"./openclaw-state-read-errors-D4PLrhBz.mjs";import c from"node:path";import l from"node:fs/promises";import u from"node:os";async function runBestEffortCleanup(e){try{return await e.cleanup()}catch(t){try{e.onError?.(t)}catch{}return}}const d=r(`infra:temp-artifacts`),f=e(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(c.sep),r=n.indexOf(`node_modules`),i=[n.slice(0,r>=0?r+1:n.length).join(c.sep),...t],o=c.dirname(e),s=[o];for(;i.some(e=>a(e,o));){let e=c.dirname(o);if(e===o)break;o=e,s.push(o)}return s}async function isRetainedRuntimeMarker(e){if(await l.realpath(e).catch(()=>void 0)!==e)return!1;let t=JSON.parse(await l.readFile(e,`utf8`));return i(t)&&t.name===`openclaw`}async function recognizesRetainedRuntime(e,t){let n=c.parse(t).root,r=c.join(e,`tree`,Buffer.from(n).toString(`hex`)),i=c.join(r,c.relative(n,t));if(await isRetainedRuntimeMarker(c.join(i,`package.json`)))return!0;let a=c.dirname(i),o=c.dirname(a),s=c.dirname(o);if(c.basename(i)!==`openclaw`||c.basename(a)!==`node_modules`||!c.basename(o).startsWith(`openclaw@`)||c.basename(s)!==`.pnpm`||await l.realpath(s).catch(()=>void 0)!==s)return!1;let u=0;for await(let e of await l.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(c.join(s,e.name,`node_modules/openclaw/package.json`)))return!0}return!1}async function maintainRetainedUpdateRuntimes(e){let n=[],r=e.packageRoots.map(t),i=new Set([u.tmpdir(),...e.temporaryDirectories??[],...r.flatMap(e=>resolveRetainedUpdateRuntimeParents(e))].map(t));for(let t of i)try{for(let i of await l.readdir(t,{withFileTypes:!0})){if(!/^openclaw-update-runtime-[A-Za-z0-9]{6}$/u.test(i.name))continue;let a=c.join(t,i.name);try{let t=await l.lstat(a);if(!t.isDirectory()||process.getuid&&t.uid!==process.getuid())throw Error(`directory ownership is unknown; inspect it before manual cleanup`);if(f.has(a))throw Error(`the creating update still owns this runtime`);let i=!1;for(let e of r)if(await recognizesRetainedRuntime(a,e)){i=!0;break}if(!i)throw Error(`no recognized runtime marker; inspect it before manual cleanup`);if(!e.repair){n.push(`Runtime retained at ${a}: run \`openclaw doctor --fix\` after its workers stop`);continue}let{inspectOtherOpenClawProcesses:o}=await import(`./openclaw-state-read-openclaw-process-census-B4kNjdiA.mjs`),c=await l.lstat(a);if(c.dev!==t.dev||c.ino!==t.ino||c.ctimeMs!==t.ctimeMs||f.has(a))throw Error(`directory identity or custody changed`);e.assertCurrent();let u=o(a);if(`error`in u)throw Error(u.error);for(let e of u.warnings??[])n.includes(e)||n.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?.(a),e.assertCurrent();let d;await removeTemporaryArtifacts(a,`Updater runtime`,e=>{d=`cleanup failed: ${s(e)}`}),n.push(d?reportRetainedUpdateRuntime(a,d):`Removed abandoned updater runtime: ${a}`)}catch(e){o(e,`ENOENT`)||n.push(reportRetainedUpdateRuntime(a,s(e)))}}}catch(e){o(e,`ENOENT`)||n.push(`Cannot inspect updater runtimes in ${t}: ${s(e)}`)}return n}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:()=>l.rm(e,{recursive:!0,force:!0}),onError:r})}export{maintainRetainedUpdateRuntimes,registerRetainedUpdateRuntime,removeTemporaryArtifacts,reportRetainedUpdateRuntime,resolveRetainedUpdateRuntimeParents};