openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 5.03 kB
JavaScript
import{I as e,M as t,x as n,y as r}from"./image-processor.worker-tmp-openclaw-dir-Db8ivo-u.mjs";import{m as i,t as a}from"./image-processor.worker-subsystem-DA-Yv6Hs.mjs";import{r as o}from"./image-processor.worker-path-existence-BcctHMlK.mjs";import{n as s}from"./image-processor.worker-logger-file-worker-BbQmW2jU.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=a(`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(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(e){if(await c.realpath(e).catch(()=>void 0)!==e)return!1;let n=JSON.parse(await c.readFile(e,`utf8`));return t(n)&&n.name===`openclaw`}function projectRetainedSource(e,t){let n=u.parse(t).root;return u.join(e,`tree`,Buffer.from(n).toString(`hex`),u.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(u.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`&&u.isAbsolute(a)&&u.normalize(a)===a&&u.basename(a)===`package.json`&&await isRetainedRuntimeMarker(projectRetainedSource(e,a))}async function recognizesRetainedRuntime(e,t){let n=projectRetainedSource(e,t);if(await isRetainedRuntimeMarker(u.join(n,`package.json`)))return!0;let r=u.dirname(n),i=u.dirname(r),a=u.dirname(i);if(u.basename(n)!==`openclaw`||u.basename(r)!==`node_modules`||!u.basename(i).startsWith(`openclaw@`)||u.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(u.join(a,e.name,`node_modules/openclaw/package.json`)))return!0}return!1}async function maintainRetainedUpdateRuntimes(e){let t=[],r=e.packageRoots.map(o),i=new Set([l.tmpdir(),...e.temporaryDirectories??[],...r.flatMap(e=>resolveRetainedUpdateRuntimeParents(e))].map(o));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 o=u.join(a,i.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||=await recognizesShippedRuntime(o),!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 ${o}: run \`openclaw doctor --fix\` after its workers stop`);continue}let{inspectOtherOpenClawProcesses:a}=await import(`./image-processor.worker-openclaw-process-census-MhOuC0qO.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 ${a}: ${s(e)}`)}return t}function removeTemporaryArtifacts(e,t,n=n=>d.warn(i(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};