openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.11 kB
JavaScript
import{r as isPathInside}from"./path-guards-DGttsOJI.mjs";import{r as resolveStateDir}from"./state-dir-BwIClCab.mjs";import{i as resolveRootPathSync,t as resolvePathViaExistingAncestorSync}from"./boundary-path-BeuXNL_4.mjs";import{n as ignoreMissingPath}from"./errno-DUBt6SQQ.mjs";import"./paths-D_FOvGon.mjs";import{n as GatewayLockError,r as acquireGatewayLock}from"./gateway-lock-CQoO1usb.mjs";import{t as formatGatewayLockFailure}from"./gateway-lock-diagnostics-_MClHYaH.mjs";import fs from"node:fs";import path from"node:path";
//!src/commands/doctor-sqlite-maintenance-lock.ts
const MAINTENANCE_LOCK_TIMEOUT_MS=250;const MAINTENANCE_LOCK_POLL_INTERVAL_MS=25;var DoctorSqliteMaintenanceLockUnavailableError=class extends Error{constructor(e,t){super(`Cannot run ${e}: ${formatGatewayLockFailure(t)}. Resolve the lock failure and retry.`);this.cause=t;this.name=`DoctorSqliteMaintenanceLockUnavailableError`}};async function assertMaintenancePathsOwnedByStateDir(e,t,n,r){if(n.length===0)return;const i=path.resolve(resolveStateDir(e));const a=resolvePathViaExistingAncestorSync(i);for(const e of n){const n=path.resolve(e);try{if(!isPathInside(i,n)&&!isPathInside(a,n))throw new Error(`path is not lexically owned by the active state directory`);resolveRootPathSync({absolutePath:n,boundaryLabel:`OpenClaw state directory`,rootCanonicalPath:a,rootPath:i})}catch(n){throw new Error(`Cannot run ${t} for a path outside the active OpenClaw state directory: ${e}. Set OPENCLAW_STATE_DIR to the owning state directory and retry.`,{cause:n})}}if(r)for(const e of new Set(n.map(e=>path.resolve(e)))){const n=inspectMaintenancePath(t,e,[i]);if(n?.isFile()&&n.nlink>1)await r(e)}assertDoctorSqliteMaintenancePathsNotAliased(t,n,[i])}function assertDoctorSqliteMaintenancePathsNotAliased(e,t,n=[],r){const i=n.map(e=>path.resolve(e));const a=new Set(t.map(e=>path.resolve(e)));for(const t of a){const n=inspectMaintenancePath(e,t,i);if(n?.isFile()&&n.nlink>1)throw new Error(`Cannot run ${e} for a hard-linked path: ${t}. Remove the additional hard link and retry.`)}const o=r?.ownershipRoots.map(e=>path.resolve(e))??[];for(const t of new Set(r?.paths.map(e=>path.resolve(e))))if(!a.has(t))inspectMaintenancePath(e,t,o)}function readMaintenancePathStat(e){try{return fs.lstatSync(e)}catch(e){return ignoreMissingPath(e)}}function inspectMaintenancePath(e,t,n){assertPathComponentsNotSymbolicLinks(e,t,n);const r=readMaintenancePathStat(t);if(r?.isSymbolicLink())throw new Error(`Cannot run ${e} for a symbolic-link path: ${t}. Replace the symbolic link with an owned regular file and retry.`);return r}function assertPathComponentsNotSymbolicLinks(e,t,n){const r=n.find(e=>isPathInside(e,t));if(!r)return;const i=path.relative(r,t);let a=r;for(const t of i.split(path.sep).filter(Boolean)){a=path.join(a,t);const n=readMaintenancePathStat(a);if(!n)return;if(n.isSymbolicLink())throw new Error(`Cannot run ${e} through a symbolic-link path component: ${a}. Replace the symbolic link with an owned directory or regular file and retry.`)}}function isDestructiveDoctorSessionSqliteMode(e){return e===`import`||e===`compact`||e===`restore`||e===`recover`}async function withDoctorSqliteMaintenanceLock(e){const t=e.env??process.env;let n;try{n=await acquireGatewayLock({allowInTests:true,env:t,pollIntervalMs:MAINTENANCE_LOCK_POLL_INTERVAL_MS,role:`sqlite-maintenance`,timeoutMs:MAINTENANCE_LOCK_TIMEOUT_MS})}catch(t){if(t instanceof GatewayLockError)throw new DoctorSqliteMaintenanceLockUnavailableError(e.operation,t);throw t}if(!n)throw new Error(`Cannot run ${e.operation} without exclusive OpenClaw state ownership.`);let r=true;try{return await n.run(async()=>{await assertMaintenancePathsOwnedByStateDir(t,e.operation,e.protectedPaths??[],e.reconcileHardlink);return await e.run({assertCurrent(){if(!r)throw new Error(`Doctor SQLite maintenance authority has expired.`);n.assertCurrent()}})})}finally{r=false;await n.release()}}export{withDoctorSqliteMaintenanceLock as i,assertDoctorSqliteMaintenancePathsNotAliased as n,isDestructiveDoctorSessionSqliteMode as r,DoctorSqliteMaintenanceLockUnavailableError as t};