openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 5 kB
JavaScript
import{Jt as e,Lt as t,ht as n,t as r}from"./sqlite-readonly-subsystem-Bfq7Sewc.mjs";import{Et as i,kt as a}from"./sqlite-readonly-runtime-process-manifest-DDSkIpjb.mjs";import{r as o}from"./sqlite-readonly-path-existence-lfal2A68.mjs";import{t as s}from"./sqlite-readonly-errors-_ACXy9m3.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=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(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(t){if(await l.realpath(t).catch(()=>void 0)!==t)return!1;let n=JSON.parse(await l.readFile(t,`utf8`));return e(n)&&n.name===`openclaw`}function projectRetainedSource(e,t){let n=c.parse(t).root;return c.join(e,`tree`,Buffer.from(n).toString(`hex`),c.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 l.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 l.open(c.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`&&c.isAbsolute(a)&&c.normalize(a)===a&&c.basename(a)===`package.json`&&await isRetainedRuntimeMarker(projectRetainedSource(e,a))}async function recognizesRetainedRuntime(e,t){let n=projectRetainedSource(e,t);if(await isRetainedRuntimeMarker(c.join(n,`package.json`)))return!0;let r=c.dirname(n),i=c.dirname(r),a=c.dirname(i);if(c.basename(n)!==`openclaw`||c.basename(r)!==`node_modules`||!c.basename(i).startsWith(`openclaw@`)||c.basename(a)!==`.pnpm`||await l.realpath(a).catch(()=>void 0)!==a)return!1;let o=0;for await(let e of await l.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(c.join(a,e.name,`node_modules/openclaw/package.json`)))return!0}return!1}async function maintainRetainedUpdateRuntimes(e){let t=[],n=e.packageRoots.map(o),r=new Set([u.tmpdir(),...e.temporaryDirectories??[],...n.flatMap(e=>resolveRetainedUpdateRuntimeParents(e))].map(o));for(let a of r)try{for(let r of await l.readdir(a,{withFileTypes:!0})){if(!/^openclaw-update-runtime-[A-Za-z0-9]{6}$/u.test(r.name))continue;let o=c.join(a,r.name);try{let r=await l.lstat(o);if(!r.isDirectory()||process.getuid&&r.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 n)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(`./sqlite-readonly-openclaw-process-census-BW2iJkj-.mjs`),c=await l.lstat(o);if(c.dev!==r.dev||c.ino!==r.ino||c.ctimeMs!==r.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){i(e,`ENOENT`)||t.push(reportRetainedUpdateRuntime(o,s(e)))}}}catch(e){i(e,`ENOENT`)||t.push(`Cannot inspect updater runtimes in ${a}: ${s(e)}`)}return t}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};