openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 4.55 kB
JavaScript
import{Lt as e}from"./sqlite-readonly-subsystem-Bfq7Sewc.mjs";import{Et as t}from"./sqlite-readonly-runtime-process-manifest-DDSkIpjb.mjs";import{Gt as n,Jt as r}from"./sqlite-readonly-sqlite-database-admission-Bj6bmeyX.mjs";import{t as i}from"./sqlite-readonly-errors-_ACXy9m3.mjs";import{n as a,r as o}from"./sqlite-readonly-sqlite-number-5mfxKD9j.mjs";import s from"node:fs";const c=e(Symbol.for(`openclaw.sqliteWalCheckpointListeners`),()=>/* @__PURE__ */ new Set);function observeSqliteWalCheckpointHealth(e,t){let{activeReaders:r,readerDiagnostics:i,...a}=t;if(t.state===`complete`)return a;let{activeReaders:o,...s}=n(e);return{...a,activeReaders:[...(r??[]).filter(e=>e.threadId!==s.threadId),...o].toSorted((e,t)=>t.ageMs-e.ageMs).slice(0,8),readerDiagnostics:[...(i??[]).filter(e=>e.threadId!==s.threadId),s].slice(-8)}}function notifyCheckpoint(e,t){for(let n of c)try{n({databasePath:r(e),health:structuredClone(t.health),observedAtNs:t.observedAtNs,lastCompletedAtNs:t.lastCompletedAtNs})}catch{}}function sqliteFileBytes(e){try{return s.statSync(e).size}catch(e){if(t(e,`ENOENT`))return 0;throw e}}function readCheckpointResult(e){let[t,n,r]=Object.values(e??{}).map(e=>a(typeof e==`number`||typeof e==`bigint`?e:null));if(t===void 0||n===void 0||r===void 0)throw Error(`SQLite returned an invalid WAL checkpoint result`);return{busy:t,logFrames:n,checkpointedFrames:r}}function checkpoint(e,t){return e.prepare(`PRAGMA wal_checkpoint(${t});`).get()}var SqliteWalCheckpointBusyError=class extends Error{};function truncateSqliteWal(e,t){let n=checkpoint(e,`TRUNCATE`),r=o(n?.busy??(n?Object.values(n)[0]:void 0));if(r===void 0)throw Error(`SQLite checkpoint returned an invalid result for ${t}.`);if(r!==0)throw new SqliteWalCheckpointBusyError(`SQLite checkpoint remained busy for ${t}. Stop OpenClaw and retry.`)}function createSqliteWalCheckpoint(e,t,n){let r,checkpointObservation=()=>({state:`error`,observedAtMs:Date.now(),walBytes:null,databaseBytes:null,logFrames:null,checkpointedFrames:null,lastCompletedAtMs:r?.health.lastCompletedAtMs??null,consecutiveBlocked:0,warning:!0}),recordCheckpointError=(e,n=checkpointObservation())=>{let a={...n,observedAtMs:Date.now(),state:`error`,consecutiveBlocked:0,warning:!0,error:i(e)};r={observedAtNs:process.hrtime.bigint(),lastCompletedAtNs:r?.lastCompletedAtNs,health:t.databasePath?observeSqliteWalCheckpointHealth(t.databasePath,a):a},t.databasePath&¬ifyCheckpoint(t.databasePath,r),t.onCheckpointError?.(e)},recordCheckpoint=(e,a,o)=>{let s=process.hrtime.bigint(),c=checkpointObservation(),l,u;try{let{busy:e,logFrames:o,checkpointedFrames:d}=readCheckpointResult(a);if(l=e!==0,c.logFrames=o,c.checkpointedFrames=d,c.state=l||d<o?`blocked`:`complete`,c.state===`complete`?c.lastCompletedAtMs=c.observedAtMs:c.consecutiveBlocked=(r?.health.consecutiveBlocked??0)+1,t.databasePath)try{c.databaseBytes=sqliteFileBytes(t.databasePath),c.walBytes=sqliteFileBytes(`${t.databasePath}-wal`)}catch(e){u=e,c.error=i(e)}c.warning=c.state===`blocked`&&(c.consecutiveBlocked>=2||c.walBytes!==null&&c.databaseBytes!==null&&c.walBytes>Math.max(2*c.databaseBytes,n)),r={observedAtNs:s,lastCompletedAtNs:c.state===`complete`?s:r?.lastCompletedAtNs,health:t.databasePath?observeSqliteWalCheckpointHealth(t.databasePath,c):c},t.databasePath&¬ifyCheckpoint(t.databasePath,r)}catch(e){return recordCheckpointError(e,c),!1}if(c.error!==void 0&&t.onCheckpointError?.(u),c.warning&&!o){let n=t.databaseLabel??`sqlite database`;t.onCheckpointError?.(/* @__PURE__ */ Error(`${n} WAL checkpoint ${e} ${l?`remained busy`:`blocked by a reader`}`))}return c.state===`complete`};return{adopt(e){if(r&&r.observedAtNs>=e.observedAtNs&&(r.lastCompletedAtNs??0n)>=(e.lastCompletedAtNs??0n))return;let n=(r?.lastCompletedAtNs??0n)>(e.lastCompletedAtNs??0n)?r:e;(!r||e.observedAtNs>r.observedAtNs)&&(r=structuredClone(e)),r.lastCompletedAtNs=n?.lastCompletedAtNs,r.health.lastCompletedAtMs=n?.health.lastCompletedAtMs??null,t.databasePath&&(r.health=observeSqliteWalCheckpointHealth(t.databasePath,r.health),notifyCheckpoint(t.databasePath,r))},checkpoint(t,n={}){try{return recordCheckpoint(t,checkpoint(e,t),n.quiet===!0)}catch(e){return recordCheckpointError(e),!1}},recordError:recordCheckpointError,inspectIdle(){let{busy:t,logFrames:n,checkpointedFrames:r}=readCheckpointResult(checkpoint(e,`PASSIVE`));return t===0&&n>=0&&r>=0&&r<=n},get health(){return r?structuredClone(r.health):void 0},get snapshot(){return r?structuredClone(r):void 0}}}export{truncateSqliteWal as n,createSqliteWalCheckpoint as t};