openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 5.17 kB
JavaScript
import{t as sleep}from"./sleep-B4CgCyZu.mjs";import{n as ignoreMissingPath}from"./errno-DUBt6SQQ.mjs";import{a as sameFileMutationFingerprint}from"./file-descriptor-CRkf5tcV.mjs";import{a as resolveCommandProcessSignal}from"./exec-spawn-DNCbDcyJ.mjs";import{t as assertNotUpdateCapturePath}from"./update-capture-paths-C5GOuESg.mjs";import fs from"node:fs/promises";import{sameFileIdentity}from"@openclaw/fs-safe/advanced";
//!src/infra/backup-sqlite-discovery-retry.ts
const MAX_CAPTURE_ATTEMPTS=3;var BackupSqliteSourceAppearedError=class extends Error{constructor(e){super(`SQLite state appeared after snapshot discovery: ${e}. Retry backup so it can be snapshotted.`);this.sourcePath=e}};async function withBackupSqliteDiscoveryRetry(e,t){const n=/* @__PURE__ */ new Set;for(let r=1;;r++)try{return await e(n)}catch(e){if(!(e instanceof BackupSqliteSourceAppearedError)||r===MAX_CAPTURE_ATTEMPTS)throw e;n.add(e.sourcePath);t?.(`SQLite state appeared during backup: ${e.sourcePath}. Retrying snapshot discovery (${r+1}/${MAX_CAPTURE_ATTEMPTS}).`);await sleep(r*1e3,resolveCommandProcessSignal())}}
//!src/infra/backup-sqlite-source-groups.ts
async function planBackupSqliteSources(e,t){const n=[];for(const r of t){const t=await fs.stat(r,{bigint:true});const i=e.resolveSqliteSource(r,t);if(!i||i.role===`unresolvable-link`)throw new Error(`SQLite ownership changed after discovery: ${r}`);let a;if(i.role!==`plugin`){if(!i.identity)throw new BackupSqliteSourceAppearedError(r);if(!sameFileIdentity(i.identity,t))throw new Error(`Canonical SQLite path changed after discovery: ${r}`);a={...i,archiveSourcePath:i.sourcePath,sourcePath:await fs.realpath(i.sourcePath),identity:i.identity};assertNotUpdateCapturePath(a.sourcePath,e.stateDir)}n.push({archiveSourcePath:r,path:a?await fs.realpath(r):r,identity:t,canonicalSource:a})}return{sources:n,groups:await planBackupSqliteSourceGroups(n)}}function hasKnownIdentity(e){return process.platform!==`win32`||e.dev!==0n&&e.ino!==0n}async function readRegularSidecar(e){const t=await fs.lstat(e,{bigint:true}).catch(ignoreMissingPath);if(!t)return null;if(!t.isFile())throw new Error(`SQLite hardlink sidecar must be a regular file: ${e}`);return t}async function assertGroupPaths(e){for(const t of e.sources){const n=await fs.lstat(t.path,{bigint:true});if(!n.isFile()||!sameFileIdentity(t.identity,n)||e.sources.length>1&&!hasKnownIdentity(n)||await fs.realpath(t.path)!==t.path)throw new Error(`SQLite backup source identity changed: ${t.path}`);if(n.nlink!==BigInt(e.sources.length))throw new Error(`SQLite hardlink journal owner may be outside the backup inventory: ${t.path} has ${n.nlink} links, but ${e.sources.length} paths were admitted. Include every database hardlink before retrying.`)}}async function readGroupWalOwners(e){const t=[];for(const n of e.sources){const e=await readRegularSidecar(`${n.path}-wal`);const r=await readRegularSidecar(`${n.path}-journal`);await readRegularSidecar(`${n.path}-shm`);if(r&&r.size>0n)throw new Error(`SQLite hardlink journal ownership cannot be established safely while a rollback journal is present: ${n.path}. Close the database cleanly before retrying.`);if(e&&e.size>0n){if(!hasKnownIdentity(e))throw new Error(`SQLite hardlink WAL identity cannot be verified: ${n.path}-wal`);t.push({path:n.path,identity:e})}}if(t.length>1)throw new Error(`Ambiguous SQLite hardlink journal ownership: multiple non-empty WAL files for ${e.sources.map(e=>e.path).join(`, `)}. Close the database writers before retrying.`);return t}async function assertBackupSqliteSourceGroup(e){await assertGroupPaths(e);if(e.sources.length===1)return;const[t]=await readGroupWalOwners(e);if(e.walIdentity?!t||t.path!==e.sourcePath||!sameFileIdentity(e.walIdentity,t.identity):t!==void 0)throw new Error(`SQLite hardlink journal ownership changed during backup: ${e.sourcePath}`);if(!sameFileMutationFingerprint(e.databaseFingerprint,await fs.stat(e.sourcePath,{bigint:true})))throw new Error(`SQLite hardlink database changed during backup: ${e.sourcePath}. Close database writers cleanly before retrying.`)}async function planBackupSqliteSourceGroups(e){const t=/* @__PURE__ */ new Map;for(const n of e){const e=hasKnownIdentity(n.identity);if(!e&&n.identity.nlink>1n)throw new Error(`SQLite hardlink identity cannot be verified: ${n.path}`);const r=e?`${n.identity.dev}:${n.identity.ino}`:n.path;const i=t.get(r);if(i){if(!i.sources.some(e=>e.path===n.path))i.sources.push(n)}else t.set(r,{sources:[n],sourcePath:n.path,walIdentity:null,databaseFingerprint:await fs.stat(n.path,{bigint:true})})}const n=/* @__PURE__ */ new Map;for(const e of t.values()){await assertGroupPaths(e);if(e.sources.length>1){const[t]=await readGroupWalOwners(e);if(t){e.sourcePath=t.path;e.walIdentity=t.identity}}for(const t of e.sources)n.set(t.path,e)}return n}async function captureBackupSqliteSourceGroup(e,t){await assertBackupSqliteSourceGroup(e);await t();await assertBackupSqliteSourceGroup(e)}export{BackupSqliteSourceAppearedError as a,planBackupSqliteSources as i,captureBackupSqliteSourceGroup as n,withBackupSqliteDiscoveryRetry as o,planBackupSqliteSourceGroups as r,assertBackupSqliteSourceGroup as t};