openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
106 lines (105 loc) • 4.42 kB
JavaScript
import { c as readFileDescriptorBoundedSync, o as openRootFileSync } from "./boundary-file-read-BfaFLDqt.mjs";
import { t as hasErrnoCode } from "./errno-DFcoqIT9.mjs";
import { r as isPathInside } from "./path-guards-TIjxtj6H.mjs";
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs";
import { i as isUpdateRecoveryPending } from "./update-run-recovery-schema-C6o9exnN.mjs";
import { n as inspectUpdateRecoveries } from "./update-run-recovery-store-jirLrGiS.mjs";
import { i as getUpdateRun } from "./update-run-reader-7kFqqMpW.mjs";
import { l as hasSymbolicLinkInDirectoryPath } from "./session-sqlite-migration-manifest-DwBodX3i.mjs";
import { r as resolveUpdateCaptureRoot } from "./update-capture-paths-B2WcdTCG.mjs";
import "./update-run-recovery-7wvn_ggB.mjs";
import { o as parseUpdateRecoveryBackupManifest } from "./backup-verify-manifest-CVAAAU_5.mjs";
import { n as hasPendingUpdateRecoverySeal } from "./update-recovery-capture-seal-CmwtNTaM.mjs";
import fs from "node:fs";
import path from "node:path";
//#region src/infra/doctor-capture-retention.ts
/** Shared policy for automatic Doctor retention and explicit update cleanup. */
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
};
}
//#endregion
export { retireExpiredStandaloneDoctorCaptures as n, collectStandaloneDoctorCaptures as t };