UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 4.15 kB
import{$ as e,A as t,Ft as n,Ot as r,Pt as i,Rt as a,t as o}from"./session-transcript-reconcile.worker-subsystem-Bf1bEeg_.mjs";import{t as s}from"./session-transcript-reconcile.worker-errors-Crq5xdoD.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=o(`infra:temp-artifacts`),f=i(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 r=e.split(l.sep),i=r.indexOf(`node_modules`),a=[r.slice(0,i>=0?i+1:r.length).join(l.sep),...t],o=l.dirname(e),s=[o];for(;a.some(e=>n(e,o));){let e=l.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=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 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 c.readdir(t,{withFileTypes:!0})){if(!/^openclaw-update-runtime-[A-Za-z0-9]{6}$/u.test(i.name))continue;let o=l.join(t,i.name);try{let t=await c.lstat(o);if(!t.isDirectory()||process.getuid&&t.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){n.push(`Runtime retained at ${o}: run \`openclaw doctor --fix\` after its workers stop`);continue}let{inspectOtherOpenClawProcesses:a}=await import(`./session-transcript-reconcile.worker-openclaw-process-census-DKFYuhKt.mjs`),l=await c.lstat(o);if(l.dev!==t.dev||l.ino!==t.ino||l.ctimeMs!==t.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??[])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?.(o),e.assertCurrent();let d;await removeTemporaryArtifacts(o,`Updater runtime`,e=>{d=`cleanup failed: ${s(e)}`}),n.push(d?reportRetainedUpdateRuntime(o,d):`Removed abandoned updater runtime: ${o}`)}catch(e){a(e,`ENOENT`)||n.push(reportRetainedUpdateRuntime(o,s(e)))}}}catch(e){a(e,`ENOENT`)||n.push(`Cannot inspect updater runtimes in ${t}: ${s(e)}`)}return n}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};