UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 3.7 kB
import{r as isPathInside}from"./path-guards-DGttsOJI.mjs";import{c as readFileDescriptorBoundedSync,o as openRootFileSync}from"./boundary-file-read-bB9An35K.mjs";import{t as hasErrnoCode}from"./errno-DUBt6SQQ.mjs";import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{c as inspectUpdateRecoveries}from"./update-run-recovery-store-k_2_R9pM.mjs";import{i as isUpdateRecoveryPending}from"./update-run-recovery-schema-CreOtwf8.mjs";import"./update-run-recovery-oivDmH0_.mjs";import{i as getUpdateRun}from"./update-run-reader-Nkn3I5Fr.mjs";import{l as hasSymbolicLinkInDirectoryPath}from"./session-sqlite-migration-manifest-1oxXdbak.mjs";import{r as resolveUpdateCaptureRoot}from"./update-capture-paths-C5GOuESg.mjs";import{a as parseUpdateRecoveryBackupManifest}from"./backup-verify-manifest-B0AbwU0Y.mjs";import{n as hasPendingUpdateRecoverySeal}from"./update-recovery-capture-seal-BmJHeK3y.mjs";import fs from"node:fs";import path from"node:path"; //!src/infra/doctor-capture-retention.ts function collectStandaloneDoctorCaptures(e){const t=/* @__PURE__ */ new Map;const n=[];const r=resolveUpdateCaptureRoot(e.stateDir);const i=e.env?{env:e.env}:{path:path.join(e.stateDir,`state`,`openclaw.sqlite`)};try{if(hasSymbolicLinkInDirectoryPath(r))return{captures:t,warnings:n};const a=fs.readdirSync(r);const o=inspectUpdateRecoveries(i).filter(({record:e})=>isUpdateRecoveryPending(e)).flatMap(({record:e})=>[e.preimages?.ref.manifestPath,e.checkpoint?.ref.manifestPath,e.checkpoint?.preimageRef?.manifestPath,e.restore?.planPath].concat((e.afterImages??[]).flatMap(e=>[e.checkpointRef.manifestPath,e.afterUpdate.ref.manifestPath,e.afterUpdate.preimageRef?.manifestPath])));for(const s of a.toSorted()){if(!/^doctor-[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/u.test(s))continue;const a=path.join(r,s);try{if(!fs.lstatSync(a).isDirectory()||hasSymbolicLinkInDirectoryPath(a)||fs.lstatSync(path.join(a,`manifest.json.partial`),{throwIfNoEntry:false}))continue;const n=openRootFileSync({absolutePath:path.join(a,`manifest.json`),rootPath:a,boundaryLabel:`standalone Doctor capture`,maxBytes:134217728,rejectHardlinks:true});if(!n.ok)continue;let r;try{r=parseUpdateRecoveryBackupManifest(readFileDescriptorBoundedSync(n.fd,134217728).toString(`utf8`))}finally{fs.closeSync(n.fd)}const c=Date.parse(r.createdAt);if(r.runId!==s||r.schemaVersion!==2||r.generation?.kind!==`baseline`||!Number.isFinite(c)||fs.lstatSync(path.join(a,`outcome.json`),{throwIfNoEntry:false})||getUpdateRun(s,i)!==void 0)continue;const l=o.some(e=>e&&isPathInside(a,e));t.set(a,{createdAt:c,eligible:!l&&s!==e.keepRunId,reason:l?`pending-update-recovery`:`superseded-doctor-capture`})}catch(e){n.push(`Standalone Doctor capture retained at ${a}: ${formatErrorMessage(e)}`)}}}catch(e){if(!hasErrnoCode(e,`ENOENT`))n.push(`Standalone Doctor capture retirement unavailable at ${r}: ${formatErrorMessage(e)}`)}const a=[...t.values()].toSorted((e,t)=>t.createdAt-e.createdAt);for(const[t,n]of a.entries()){const r=(e.now??Date.now())-n.createdAt>2592e6;if(t===0||t<3&&!r){n.eligible=false;if(n.reason!==`pending-update-recovery`)n.reason=`retained-doctor-capture`}}return{captures:t,warnings:n}}async function retireExpiredStandaloneDoctorCaptures(e){const{captures:t,warnings:n}=collectStandaloneDoctorCaptures(e);const r=[];for(const[i,{eligible:a}]of t){if(!a)continue;try{if(await hasPendingUpdateRecoverySeal(i))continue;e.assertCurrent();await fs.promises.rm(i,{recursive:true});r.push(i)}catch(e){n.push(`Standalone Doctor capture retained at ${i}: ${formatErrorMessage(e)}`)}}return{retired:r,warnings:n}}export{retireExpiredStandaloneDoctorCaptures as n,collectStandaloneDoctorCaptures as t};