UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.59 kB
import{c as openLocalFileSafely}from"./fs-safe-Bxzu1j4h.mjs";import{o as hashConfigIncludeRaw}from"./includes-gwK90iIZ.mjs";import{v as withOpenClawStateDatabaseReadSnapshot}from"./openclaw-state-db-readonly-Cd5_cxBs.mjs";import{r as selectDatabaseEngine}from"./registry-ByN-O5dB.mjs";import{n as containsConfigIncludeDirective}from"./io.read-helpers-B0XuPBY2.mjs";import{t as createConfigIO}from"./io.factory-y6nIrp_4.mjs";import path from"node:path";import fs from"node:fs/promises";import{isDeepStrictEqual}from"node:util";import{readFileHandleBounded}from"@openclaw/fs-safe/advanced"; //!src/infra/backup-config-capture.ts var BackupConfigCaptureError=class extends Error{constructor(...e){super(...e);this.name=`BackupConfigCaptureError`}};function captureError(e,t,n){return new BackupConfigCaptureError(`Cannot capture required config file ${e}: ${t}. Fix the include graph or stop concurrent edits, then retry backup.`,{cause:n})}async function resolveBackupConfigCapture({snapshot:e,writeOptions:t},n={}){if((containsConfigIncludeDirective(e.parsed)||Boolean(e.includedPaths?.length))&&e.includeProvenance===void 0||e.exists&&e.raw===null)throw captureError(e.path,`include graph could not be resolved`);const r=t.includeFileHashesForWrite??{};const i=t.includeFileTargetsForWrite??{};const a=new Map(e.exists?[[e.path,hashConfigIncludeRaw(e.raw)],...Object.entries(r)]:[]);const o=[];let assertRootAlias;const s=a.size?await fs.realpath(path.dirname(e.path)).catch(t=>{throw captureError(e.path,`config directory became unavailable`,t)}):path.dirname(e.path);for(const[t,r]of a)try{const a=await fs.realpath(t);const c=await fs.lstat(t,{bigint:true});const l=t===e.path&&c.isSymbolicLink();const u=t!==e.path&&n.allowIncludeAliases===true;const d=l||u?await fs.stat(t,{bigint:true}):c;if(l)assertRootAlias=async()=>{const e=await fs.lstat(t,{bigint:true});if(e.dev!==c.dev||e.ino!==c.ino||e.ctimeMs!==c.ctimeMs||await fs.realpath(t)!==a)throw captureError(t,`config alias changed during capture`)};const f=path.resolve(s,path.relative(path.dirname(e.path),t));if(!d.isFile()||!l&&!u&&a!==t&&a!==f)throw captureError(t,`include alias cannot be represented in this archive`);if(t!==e.path&&i[t]!==a)throw captureError(t,`include target changed or is unavailable`);o.push({sourcePath:l?a:t,canonicalPath:a,hash:r,dev:d.dev,ino:d.ino})}catch(e){throw captureError(t,`file identity could not be pinned`,e)}return{files:o,assertRootAlias,revalidate:async()=>{await assertRootAlias?.();const read=()=>createConfigIO({configPath:e.path,env:n.env,pluginValidation:n.pluginValidation,observe:false}).readConfigFileSnapshotForWrite();const t=await(selectDatabaseEngine({id:`state`,postgres:true},e.sourceConfig)===`postgres`&&n.pluginValidation===`core-only`?read():withOpenClawStateDatabaseReadSnapshot(read,{env:n.env}));if(t.snapshot.exists!==e.exists||t.snapshot.raw!==e.raw||t.snapshot.valid!==e.valid||!isDeepStrictEqual(t.snapshot.sourceConfig,e.sourceConfig)||!isDeepStrictEqual(t.snapshot.includedPaths,e.includedPaths)||!isDeepStrictEqual(t.snapshot.includeProvenance,e.includeProvenance)||!isDeepStrictEqual(t.writeOptions.includeFileHashesForWrite??{},r)||!isDeepStrictEqual(t.writeOptions.includeFileTargetsForWrite??{},i))throw captureError(e.path,`include graph changed during capture`)}}}async function readBackupConfigCaptureFile(e){try{if(await fs.realpath(e.sourcePath)!==e.canonicalPath)throw new Error(`file identity changed`);const t=await openLocalFileSafely({filePath:e.canonicalPath});const{handle:n,exactIdentity:r}=t;try{if(r.dev!==e.dev||r.ino!==e.ino||t.realPath!==e.canonicalPath)throw new Error(`file identity changed`);const i=await readFileHandleBounded(n,Infinity);const a=i.toString(`utf8`);if(!i.equals(Buffer.from(a))||hashConfigIncludeRaw(a)!==e.hash)throw new Error(`file contents changed`);const o=await fs.lstat(e.canonicalPath,{bigint:true});if(!o.isFile()||o.dev!==e.dev||o.ino!==e.ino||await fs.realpath(e.sourcePath)!==e.canonicalPath)throw new Error(`file identity changed during read`);return i}finally{await n.close()}}catch(t){throw captureError(e.sourcePath,`source changed or became unreadable`,t)}}async function stageBackupConfigCapture(e,t){const n=/* @__PURE__ */ new Map;for(const[r,i]of(e?.files??[]).entries()){const e=await readBackupConfigCaptureFile(i);const a=path.join(t,`config-${r}`);await fs.writeFile(a,e,{flag:`wx`,mode:384});n.set(a,i.canonicalPath)}await e?.assertRootAlias?.();return n}export{stageBackupConfigCapture as i,readBackupConfigCaptureFile as n,resolveBackupConfigCapture as r,BackupConfigCaptureError as t};