UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 4.98 kB
import{a as isRecord}from"./record-coerce-DwDZngEy.mjs";import"./boundary-file-read-LFsZ_JrU.mjs";import{c as isPathInside}from"./state-dir-C22DK7p0.mjs";import{t as createSubsystemLogger}from"./subsystem-BMw5Czn-.mjs";import{i as resolveIdentityPathViaExistingAncestorSync}from"./path-existence-4c3xWldM.mjs";import{n as sha256FileSync}from"./crypto-digest-DDqGMAqd.mjs";import fsSync from"node:fs";import path from"node:path"; //!src/infra/sqlite-files.ts const SQLITE_DATABASE_FILE_SUFFIXES=[``,`-wal`,`-shm`,`-journal`];const SQLITE_SIDECAR_SUFFIXES=SQLITE_DATABASE_FILE_SUFFIXES.slice(1);const SQLITE_WAL_HEADER_BYTES=32;const APPLE_DOUBLE_MAGIC=Buffer.from([0,5,22,7]);const sqliteFilesLog=createSubsystemLogger(`state/sqlite`);var SqliteOrphanedSidecarsError=class extends Error{constructor(e,t,n){super(`SQLite database is missing at ${e}, and orphaned sidecars could not be copied: ${t.join(`, `)}. Refusing to open because SQLite could delete orphan WAL or journal state. Preserve the sidecar bytes, restore the main database, and pair it with the matching sidecar before retrying.`,{cause:n});this.name=`SqliteOrphanedSidecarsError`}};function resolveSqliteDatabaseFilePaths(e){return SQLITE_DATABASE_FILE_SUFFIXES.map(t=>`${e}${t}`)}function findMatchingOrphanedSidecarCopy(e,t){const n=path.dirname(e);const r=`${path.basename(e)}.orphaned-`;const i=fsSync.readdirSync(n,{withFileTypes:true}).filter(e=>e.isFile()&&e.name.startsWith(r)).map(e=>path.join(n,e.name)).filter(e=>fsSync.statSync(e).size===t);if(i.length===0)return;const a=sha256FileSync(e);for(const e of i)if(sha256FileSync(e)===a)return e}function copyOrphanedSidecar(e,t){const n=`${e}.orphaned-${t}`;for(let t=0;;t+=1){const r=t===0?n:`${n}-${t}`;try{fsSync.copyFileSync(e,r,fsSync.constants.COPYFILE_EXCL);return r}catch(e){if(e.code!==`EEXIST`)throw e}}}function quarantineOrphanedSqliteSidecars(e){if(fsSync.existsSync(e))return;const t=[{path:`${e}-wal`,minimumBytes:SQLITE_WAL_HEADER_BYTES},{path:`${e}-journal`,minimumBytes:0}].flatMap(e=>{const t=fsSync.statSync(e.path,{throwIfNoEntry:false});return t?.isFile()===true&&t.size>e.minimumBytes?[{path:e.path,size:t.size}]:[]});if(t.length===0)return;const n=Date.now();const r=[];try{for(const e of t){if(findMatchingOrphanedSidecarCopy(e.path,e.size))continue;const t=copyOrphanedSidecar(e.path,n);r.push({quarantinePath:t,sourcePath:e.path})}}catch(n){throw new SqliteOrphanedSidecarsError(e,t.map(e=>e.path),n)}if(r.length===0)return;const i=r.map(({sourcePath:e,quarantinePath:t})=>`${e} -> ${t}`);sqliteFilesLog.warn(`SQLite database is missing at ${e}; copied orphaned sidecars: ${i.join(`, `)}. Committed frames could not be applied because the main database is missing. The bytes are preserved. Recovery requires restoring the main database and pairing it with the quarantined file.`,{databasePath:e,copiedSidecars:r})} //!src/infra/update-rehearsal-paths.ts function buildUpdateRehearsalPathEnv(e){return{HOME:e,USERPROFILE:e,TMPDIR:e,TMP:e,TEMP:e,XDG_CONFIG_HOME:path.join(e,`config`),XDG_CACHE_HOME:path.join(e,`cache`),XDG_DATA_HOME:path.join(e,`data`),XDG_STATE_HOME:path.join(e,`state`),OPENCLAW_HOME:e,OPENCLAW_STATE_DIR:e,OPENCLAW_CONFIG_PATH:path.join(e,`openclaw.json`),OPENCLAW_WORKSPACE_DIR:path.join(e,`workspace`),OPENCLAW_SKIP_CHANNELS:`1`,OPENCLAW_SKIP_PROVIDERS:`1`,OPENCLAW_SKIP_CRON:`1`,OPENCLAW_SKIP_GMAIL_WATCHER:`1`,OPENCLAW_SKIP_CANVAS_HOST:`1`,OPENCLAW_SKIP_BROWSER_CONTROL_SERVER:`1`,OPENCLAW_SKIP_STARTUP_MODEL_PREWARM:`1`,OPENCLAW_NO_AUTO_UPDATE:`1`,NODE_DISABLE_COMPILE_CACHE:`1`}}function projectUpdateRehearsalDatabaseConfig(e,t){if(!isRecord(e)||!isRecord(e.database)||!resolveUpdateRehearsalRoot(t))return e;const{engine:n,families:r}=e.database;return n===`postgres`||isRecord(r)&&Object.values(r).some(e=>isRecord(e)&&e.engine===`postgres`)?{...e,database:{engine:`sqlite`}}:e}function resolveUpdateRehearsalRoot(e){const t=e.OPENCLAW_STATE_DIR;if(!t||!path.isAbsolute(t)||![`0`,`1`].includes(e.OPENCLAW_UPDATE_IN_PROGRESS??``)||Object.entries(buildUpdateRehearsalPathEnv(t)).some(([t,n])=>e[t]!==n)||e.OPENCLAW_SERVICE_REPAIR_POLICY!==`external`||e.OPENCLAW_UPDATE_PARENT_ALLOWS_GATEWAY_SERVICE_REPAIR!==`0`||e.OPENCLAW_UPDATE_PARENT_ALLOWS_GATEWAY_ACTIVATION!==`0`||Boolean(e.OPENCLAW_COMPATIBILITY_HOST_VERSION))return;return path.resolve(t)}function isUpdateRehearsalReadOnlyPath(e,t){const n=resolveUpdateRehearsalRoot(t);if(!n)return false;const r=path.resolve(e);return!isPathInside(n,r)||!isPathInside(resolveIdentityPathViaExistingAncestorSync(n),resolveIdentityPathViaExistingAncestorSync(r))}function isUpdateRehearsalPrivateDatabase(e,t){return resolveUpdateRehearsalRoot(t)!==void 0&&resolveSqliteDatabaseFilePaths(e).every(e=>!isUpdateRehearsalReadOnlyPath(e,t))}export{quarantineOrphanedSqliteSidecars as a,SQLITE_SIDECAR_SUFFIXES as i,projectUpdateRehearsalDatabaseConfig as n,resolveSqliteDatabaseFilePaths as o,resolveUpdateRehearsalRoot as r,isUpdateRehearsalPrivateDatabase as t};