UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 5.01 kB
import{D as e,L as t,R as n,V as r,t as i,tt as a}from"./openclaw-state-lease-heartbeat.worker-subsystem-DoPLR_ni.mjs";import{r as o}from"./openclaw-state-lease-heartbeat.worker-path-existence-CuWhT9zy.mjs";import{t as s}from"./openclaw-state-lease-heartbeat.worker-errors-u9cPpOZi.mjs";import c from"node:path";import l from"node:os";import u from"node:fs/promises";async function runBestEffortCleanup(e){try{return await e.cleanup()}catch(t){try{e.onError?.(t)}catch{}return}}const d=i(`infra:temp-artifacts`),f=a(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(c.sep),i=r.indexOf(`node_modules`),a=[r.slice(0,i>=0?i+1:r.length).join(c.sep),...t],o=c.dirname(e),s=[o];for(;a.some(e=>n(e,o));){let e=c.dirname(o);if(e===o)break;o=e,s.push(o)}return s}async function isRetainedRuntimeMarker(e){if(await u.realpath(e).catch(()=>void 0)!==e)return!1;let n=JSON.parse(await u.readFile(e,`utf8`));return t(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 u.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 u.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 u.realpath(a).catch(()=>void 0)!==a)return!1;let o=0;for await(let e of await u.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),i=new Set([l.tmpdir(),...e.temporaryDirectories??[],...n.flatMap(e=>resolveRetainedUpdateRuntimeParents(e))].map(o));for(let a of i)try{for(let i of await u.readdir(a,{withFileTypes:!0})){if(!/^openclaw-update-runtime-[A-Za-z0-9]{6}$/u.test(i.name))continue;let o=c.join(a,i.name);try{let r=await u.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(`./openclaw-state-lease-heartbeat.worker-openclaw-process-census-BjzvV4zl.mjs`),c=await u.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 l=a(o);if(`error`in l)throw Error(l.error);for(let e of l.warnings??[])t.includes(e)||t.push(e);if(l.pids.length)throw Error(`other OpenClaw processes are still running (PIDs: ${l.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){r(e,`ENOENT`)||t.push(reportRetainedUpdateRuntime(o,s(e)))}}}catch(e){r(e,`ENOENT`)||t.push(`Cannot inspect updater runtimes in ${a}: ${s(e)}`)}return t}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:()=>u.rm(t,{recursive:!0,force:!0}),onError:r})}export{maintainRetainedUpdateRuntimes,registerRetainedUpdateRuntime,removeTemporaryArtifacts,reportRetainedUpdateRuntime,resolveRetainedUpdateRuntimeParents};