openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 4.93 kB
JavaScript
import{J as e,N as t,Z as n,d as r,t as i,vt as a}from"./read-only.worker-subsystem-UGB_-INE.mjs";import{t as o}from"./read-only.worker-errors-B4_gOnq5.mjs";import{r as s}from"./read-only.worker-path-existence-D3jH5IBa.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=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,n=[]){let r=e.split(l.sep),i=r.indexOf(`node_modules`),a=[r.slice(0,i>=0?i+1:r.length).join(l.sep),...n],o=l.dirname(e),s=[o];for(;a.some(e=>t(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 a(t)&&t.name===`openclaw`}function projectRetainedSource(e,t){let n=l.parse(t).root;return l.join(e,`tree`,Buffer.from(n).toString(`hex`),l.relative(n,t))}const p=`retained-files.json`,m=/* @__PURE__ */ new Set([p,`tree`,`update-native-gate.mjs`]);async function recognizesShippedRuntime(e){let t=await c.readdir(e,{withFileTypes:!0});if(t.length!==m.size||!t.every(e=>m.has(e.name)&&(e.name===`tree`?e.isDirectory():e.isFile())))return!1;let n=await c.open(l.join(e,p),`r`),r;try{let e=Buffer.alloc(65536),{bytesRead:t}=await n.read(e,0,e.length,0);r=e.toString(`utf8`,0,t)}finally{await n.close()}let i=/^\[("(?:[^"\\]|\\.)*")/u.exec(r)?.[1],a=i?JSON.parse(i):void 0;return typeof a==`string`&&l.isAbsolute(a)&&l.normalize(a)===a&&l.basename(a)===`package.json`&&await isRetainedRuntimeMarker(projectRetainedSource(e,a))}async function recognizesRetainedRuntime(e,t){let n=projectRetainedSource(e,t);if(await isRetainedRuntimeMarker(l.join(n,`package.json`)))return!0;let r=l.dirname(n),i=l.dirname(r),a=l.dirname(i);if(l.basename(n)!==`openclaw`||l.basename(r)!==`node_modules`||!l.basename(i).startsWith(`openclaw@`)||l.basename(a)!==`.pnpm`||await c.realpath(a).catch(()=>void 0)!==a)return!1;let o=0;for await(let e of await c.opendir(a)){if(++o>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(a,e.name,`node_modules/openclaw/package.json`)))return!0}return!1}async function maintainRetainedUpdateRuntimes(e){let t=[],n=e.packageRoots.map(s),i=new Set([u.tmpdir(),...e.temporaryDirectories??[],...n.flatMap(e=>resolveRetainedUpdateRuntimeParents(e))].map(s));for(let a of i)try{for(let i of await c.readdir(a,{withFileTypes:!0})){if(!/^openclaw-update-runtime-[A-Za-z0-9]{6}$/u.test(i.name))continue;let s=l.join(a,i.name);try{let r=await c.lstat(s);if(!r.isDirectory()||process.getuid&&r.uid!==process.getuid())throw Error(`directory ownership is unknown; inspect it before manual cleanup`);if(f.has(s))throw Error(`the creating update still owns this runtime`);let i=!1;for(let e of n)if(await recognizesRetainedRuntime(s,e)){i=!0;break}if(i||=await recognizesShippedRuntime(s),!i)throw Error(`it does not match a runtime an OpenClaw updater created, so it is kept`);if(!e.repair){t.push(`Runtime retained at ${s}: run \`openclaw doctor --fix\` after its workers stop`);continue}let{inspectOtherOpenClawProcesses:a}=await import(`./read-only.worker-openclaw-process-census-DXAYxwM4.mjs`),l=await c.lstat(s);if(l.dev!==r.dev||l.ino!==r.ino||l.ctimeMs!==r.ctimeMs||f.has(s))throw Error(`directory identity or custody changed`);e.assertCurrent();let u=a(s);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?.(s),e.assertCurrent();let d;await removeTemporaryArtifacts(s,`Updater runtime`,e=>{d=`cleanup failed: ${o(e)}`}),t.push(d?reportRetainedUpdateRuntime(s,d):`Removed abandoned updater runtime: ${s}`)}catch(e){r(e,`ENOENT`)||t.push(reportRetainedUpdateRuntime(s,o(e)))}}}catch(e){r(e,`ENOENT`)||t.push(`Cannot inspect updater runtimes in ${a}: ${o(e)}`)}return t}function removeTemporaryArtifacts(e,t,r=r=>d.warn(n(o(`${t} cleanup failed; files may remain in ${e}. After the worker or session stops, check permissions and remove the retained directory: ${o(r)}`),1024))){return runBestEffortCleanup({cleanup:()=>c.rm(e,{recursive:!0,force:!0}),onError:r})}export{maintainRetainedUpdateRuntimes,registerRetainedUpdateRuntime,removeTemporaryArtifacts,reportRetainedUpdateRuntime,resolveRetainedUpdateRuntimeParents};