UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

98 lines (97 loc) • 3.73 kB
import { p as invalidateSqliteDatabaseCleanClose } from "./sqlite-database-admission-DQIqro3d.mjs"; import { l as OPENCLAW_SQLITE_BUSY_TIMEOUT_MS } from "./openclaw-state-db-contract-DfPjvL0M.mjs"; import { n as openNodeSqliteDatabase, r as resolveExistingSqliteFileUri } from "./node-sqlite-CU4puofF.mjs"; import { r as readFiniteSqliteNumber } from "./sqlite-number-D8n3srri.mjs"; import { l as SqliteWalCheckpointBusyError, p as truncateSqliteWal } from "./sqlite-wal-DGPrWPl6.mjs"; import { r as assertSqliteIntegrity } from "./sqlite-integrity-CCmEgO_m.mjs"; import "./openclaw-state-db-ARhwdPJg.mjs"; import fs from "node:fs"; //#region src/commands/doctor-sqlite-compact.ts /** The initial checkpoint was busy, before conversion, and the connection has closed. */ var DoctorSqliteCompactionDeferredError = class extends Error {}; /** * Compact one SQLite file during an explicit offline doctor operation. * * Validation runs before the first checkpoint because checkpointing mutates * the database files. A busy checkpoint is a hard failure, never partial * success, so VACUUM cannot race an active reader or writer. */ function compactDoctorSqliteFile(e) { const t = openNodeSqliteDatabase(e.requireExisting ? resolveExistingSqliteFileUri(e.sqlitePath) : e.sqlitePath); let n; let r = false; let i; try { t.exec(`PRAGMA busy_timeout = ${e.busyTimeoutMs ?? 5e3};`); t.exec("PRAGMA trusted_schema = OFF;"); e.validateBeforeMutation?.(t); const n = readCompactSnapshot(t, e.sqlitePath); let { integrityCheck: a } = assertSqliteIntegrity(t, e.sqlitePath); if (!(e.operation === "import-finalize" && n.autoVacuum === 2 && n.freelistPages === 0 && n.walSizeBytes === 0)) { invalidateSqliteDatabaseCleanClose(e.sqlitePath); try { truncateSqliteWal(t, e.sqlitePath); } catch (e) { r = e instanceof SqliteWalCheckpointBusyError; throw e; } t.exec("PRAGMA auto_vacuum = INCREMENTAL;"); t.exec(e.operation === "import-finalize" && n.autoVacuum !== 0 ? "PRAGMA incremental_vacuum;" : "VACUUM;"); truncateSqliteWal(t, e.sqlitePath); ({integrityCheck: a} = assertSqliteIntegrity(t, e.sqlitePath)); } const o = readCompactSnapshot(t, e.sqlitePath); const s = n.dbSizeBytes + n.walSizeBytes; const c = o.dbSizeBytes + o.walSizeBytes; i = { after: o, before: n, integrityCheck: a, reclaimedBytes: Math.max(0, s - c) }; } catch (e) { n = e; } try { t.close(); } catch (e) { r = false; n = n !== void 0 ? new AggregateError([n, e], "SQLite compaction and close failed.") : e; } if (n === void 0 && i) try { e.afterSuccess?.(); } catch (e) { n ??= e; } if (n !== void 0) { if (r && n instanceof Error) throw new DoctorSqliteCompactionDeferredError(n.message, { cause: n }); throw n instanceof Error ? n : /* @__PURE__ */ new Error("SQLite compaction failed with a non-Error value."); } if (!i) throw new Error(`SQLite compaction produced no result for ${e.sqlitePath}.`); return i; } function readCompactSnapshot(e, t) { return { autoVacuum: readPragmaNumber(e, "auto_vacuum"), dbSizeBytes: fileSize(t), freelistPages: readPragmaNumber(e, "freelist_count"), pageSizeBytes: readPragmaNumber(e, "page_size"), walSizeBytes: fileSize(`${t}-wal`) }; } function readPragmaNumber(e, t) { const n = e.prepare(`PRAGMA ${t};`).get(); const r = readFiniteSqliteNumber(n?.[t] ?? (n ? Object.values(n)[0] : void 0)); if (r === void 0) throw new Error(`SQLite PRAGMA ${t} returned an invalid result.`); return r; } function fileSize(e) { try { return fs.statSync(e).size; } catch (e) { if (e.code === "ENOENT") return 0; throw e; } } //#endregion export { compactDoctorSqliteFile as n, DoctorSqliteCompactionDeferredError as t };