UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 6.24 kB
import"./sqlite-readonly-sqlite-schema-catalog-8883sMXx.mjs";import{C as e,K as t,T as n,w as r}from"./sqlite-readonly-sqlite-database-admission-Bj6bmeyX.mjs";import{r as i}from"./sqlite-readonly-openclaw-state-maintenance-context-BTKm0Xem.mjs";import{d as a,f as o,h as s}from"./sqlite-readonly-node-sqlite-C-jA8uc4.mjs";import{a as c,r as l}from"./sqlite-readonly-sqlite-schema-sql-BnVx4b5A.mjs";import"node:path";import"node:fs";import"node:perf_hooks";const u=/* @__PURE__ */ new WeakMap;function getMaintenanceSqliteTableChecks(t){let n=i();if(!n?.ownsSchemaMaintenance||n.sqliteIntegrityCheck!==`quick`)return;let s=a(t),c=t.location();if(s===void 0||!c||e(t)===void 0)return;let l=r(c);if(l===void 0)return;let d=u.get(n);if(d||(d={committed:/* @__PURE__ */ new Map,transactions:/* @__PURE__ */ new WeakMap},u.set(n,d)),!t.isTransaction){let e=d.committed.get(l);return e||(e=/* @__PURE__ */ new Set,d.committed.set(l,e)),e}let f=`${l}:${s}`,p=d.transactions.get(t);if(p)p.token!==f&&(p.token=f,p.tables.clear());else{let e=/* @__PURE__ */ new Set;p={token:f,tables:e},d.transactions.set(t,p),n.own(p,`shared-resources`,o(t,()=>e.clear()))}return p.tables}var SqliteRepairableForeignKeyError=class extends Error{constructor(e,t){super(`SQLite foreign_key_check failed for ${e}: repairable task_delivery_state.task_id references task_runs.task_id cascade-owned orphans (${t} rows). Run openclaw doctor --fix to migrate this legacy state before retrying.`),this.name=`SqliteRepairableForeignKeyError`,this.repair={kind:`task-delivery-orphans`,relation:`task_delivery_state.task_id`,parentTable:`task_runs`,orphanCount:t}}};function isTerminalSqliteIntegrityError(e){return e.name===`SqliteIntegrityError`?!e.cause||t(e.cause):!1}function assertSqliteIntegrity(e,t,r={}){if(r.check===`quick`&&r.schemaOnly)return runSqliteCheck(e,t,`quick_check`,`schema_meta`),{integrityCheck:`ok`};let i=r.check===`quick`?`quick_check`:`integrity_check`,a=!1;try{runSqliteCheck(e,t,i,void 0,r.allFindings||r.ftsSchemas!==void 0)}catch(n){if(!r.ftsSchemas||!(n instanceof SqliteIntegrityError)||!n.findings||!classifySqliteFtsIntegrityFindings(e,n.findings,r.ftsSchemas))throw n;if(i===`quick_check`)return assertSqliteIntegrity(e,t,{check:`full`,ftsSchemas:r.ftsSchemas});a=!0}if(i===`quick_check`&&r.tables?.length){let n=new Set(s(e).filter(e=>e.type===`table`).map(e=>e.name));for(let i of r.tables)n.has(i)&&assertSqliteTableIntegrity(e,t,i)}return runSqliteForeignKeyCheck(e,t),i===`integrity_check`&&!a&&n(e),a?{integrityCheck:`ok`,staleFts:!0}:{integrityCheck:`ok`}}function classifySqliteFtsIntegrityFindings(e,t,n){let r=/* @__PURE__ */ new Set;for(let e of t){if(e===`ok`)continue;let t=typeof e==`string`?/^malformed inverted index for FTS5 table main\.([A-Za-z_][A-Za-z0-9_]*)$/u.exec(e):null;if(!t?.[1])return;r.add(t[1])}if(r.size===0)return;let i=/* @__PURE__ */ new Map;for(let e of n)for(let t of e.matchAll(/CREATE VIRTUAL TABLE (?:IF NOT EXISTS )?([A-Za-z_][A-Za-z0-9_]*) USING fts5\s*\(/gu)){let n=t[1],r=l(e,t.index+t[0].length-1),a=i.get(n)??/* @__PURE__ */ new Set;a.add(c(e.slice(t.index,r+1))),i.set(n,a)}let a=s(e).filter(e=>e.type===`table`);for(let e of r){let t=a.find(t=>t.name===e)?.sql;if(typeof t!=`string`||!i.get(e)?.has(c(t)))return}return[...r].toSorted()}function assertSqliteTableIntegrity(e,t,n){let r=getMaintenanceSqliteTableChecks(e);r?.has(n)||(runSqliteCheck(e,`${t} table ${n}`,`integrity_check`,n),r?.add(n))}function readSqliteCheckFindings(e,t,n,r=!1){let i=n?`('${n.replaceAll(`'`,`''`)}')`:r?`(2147483647)`:``;return e.prepare(`PRAGMA ${t}${i};`).all().map(e=>e[t]??Object.values(e)[0])}function runSqliteCheck(e,t,n,r,i=!1){let a;try{a=readSqliteCheckFindings(e,n,r,i)}catch(e){throw new SqliteIntegrityError(`SQLite ${n} failed for ${t}: ${e instanceof Error?e.message:String(e)}`,e)}if(a.length===1&&a[0]===`ok`)return`ok`;throw new SqliteIntegrityError(`SQLite ${n} failed for ${t}: ${a.map(e=>String(e)).join(`; `)||`no result`}`,void 0,a)}function runSqliteForeignKeyCheck(e,t){let n=0,r=!0,i,a=[];try{let t=e.prepare(`PRAGMA foreign_key_check;`);t.setReadBigInts(!0);for(let o of t.iterate())n+=1,retainSortedForeignKeyViolation(a,o),r&&(o.table===`task_delivery_state`&&o.parent===`task_runs`?(i??=readTaskDeliveryCascadeForeignKeyId(e),r=o.fkid===i):r=!1)}catch(e){throw new SqliteIntegrityError(`SQLite foreign_key_check failed for ${t}: ${e instanceof Error?e.message:String(e)}`,e)}if(a.length===0)return;if(r)throw new SqliteRepairableForeignKeyError(t,n);let o=a.map(formatSqliteForeignKeyViolation);throw n>5&&o.push(`additional violations omitted`),new SqliteIntegrityError(`SQLite foreign_key_check failed for ${t}: ${o.join(`; `)}`)}function readTaskDeliveryCascadeForeignKeyId(e){let t=e.prepare(`PRAGMA foreign_key_list(task_delivery_state);`);t.setReadBigInts(!0);let n=t.all(),r=n.find(e=>e.table===`task_runs`&&e.from===`task_id`&&e.to===`task_id`&&e.on_delete===`CASCADE`);return r&&typeof r.id==`bigint`&&n.filter(e=>e.id===r.id).length===1?r.id:void 0}var SqliteIntegrityError=class extends Error{constructor(e,t,n){super(t===void 0?`${e}. Stop the Gateway, run "openclaw doctor --fix" to inspect and repair this database, and restart. Only if Doctor still cannot repair the offline database, preserve the database and WAL and restore a verified backup.`:e,t===void 0?void 0:{cause:t}),this.findings=n,this.name=`SqliteIntegrityError`}};function retainSortedForeignKeyViolation(e,t){e.push(t),e.sort(compareSqliteForeignKeyViolations),e.length>5&&e.pop()}function compareSqliteForeignKeyViolations(e,t){let n=Buffer.compare(Buffer.from(e.table),Buffer.from(t.table));if(n!==0)return n;if(e.rowid===null||t.rowid===null){if(e.rowid!==t.rowid)return e.rowid===null?-1:1}else if(e.rowid!==t.rowid)return e.rowid<t.rowid?-1:1;let r=Buffer.compare(Buffer.from(e.parent),Buffer.from(t.parent));return r===0?e.fkid===t.fkid?0:e.fkid<t.fkid?-1:1:r}function formatSqliteForeignKeyViolation(e){let t=e.rowid===null?`row without rowid`:`row ${e.rowid.toString()}`;return`${e.table} ${t} references ${e.parent} (foreign key ${e.fkid.toString()})`}export{isTerminalSqliteIntegrityError as i,assertSqliteIntegrity as n,assertSqliteTableIntegrity as r,SqliteRepairableForeignKeyError as t};