openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 4.11 kB
JavaScript
import{C as e,P as t,T as n,j as r,o as i,p as a,t as o}from"./attachment-processor.worker-subsystem-DWWW8u45.mjs";import{i as s}from"./attachment-processor.worker.js";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(t,n=[]){let r=t.split(u.sep),i=r.indexOf(`node_modules`),a=[r.slice(0,i>=0?i+1:r.length).join(u.sep),...n],o=u.dirname(t),s=[o];for(;a.some(t=>e(t,o));){let e=u.dirname(o);if(e===o)break;o=e,s.push(o)}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 r(t)&&t.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=[],r=e.packageRoots.map(i),a=new Set([l.tmpdir(),...e.temporaryDirectories??[],...r.flatMap(e=>resolveRetainedUpdateRuntimeParents(e))].map(i));for(let i of a)try{for(let a of await c.readdir(i,{withFileTypes:!0})){if(!/^openclaw-update-runtime-[A-Za-z0-9]{6}$/u.test(a.name))continue;let o=u.join(i,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 i=!1;for(let e of r)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(`./attachment-processor.worker-openclaw-process-census-upghnRe1.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){n(e,`ENOENT`)||t.push(reportRetainedUpdateRuntime(o,s(e)))}}}catch(e){n(e,`ENOENT`)||t.push(`Cannot inspect updater runtimes in ${i}: ${s(e)}`)}return t}function removeTemporaryArtifacts(e,t,n=n=>d.warn(a(s(`${t} cleanup failed; files may remain in ${e}. After the worker or session stops, check permissions and remove the retained directory: ${s(n)}`),1024))){return runBestEffortCleanup({cleanup:()=>c.rm(e,{recursive:!0,force:!0}),onError:n})}export{maintainRetainedUpdateRuntimes,registerRetainedUpdateRuntime,removeTemporaryArtifacts,reportRetainedUpdateRuntime,resolveRetainedUpdateRuntimeParents};