openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
6 lines • 4.66 kB
JavaScript
import{n as isPostgresSqlConnection}from"./sql-connection-nx8lLUJM.mjs";import{t as _usingCtx}from"./usingCtx-CUVyQ_FT.mjs";import{i as executeSqliteQuerySync,s as getNodeSqliteKysely}from"./kysely-sync-R08Pt8qn.mjs";import{D as requireNativeDatabaseFile,t as admitSqliteSchema}from"./sqlite-schema-facts-CWOeTpUB.mjs";import{i as resolveImmutableSqliteFileUri,n as openNodeSqliteDatabase}from"./node-sqlite-DXyF1fBG.mjs";import{a as resolveOpenClawRegisteredAgentDatabasePath,i as resolveOpenClawAgentDatabaseStoredPath}from"./openclaw-state-db.paths-CbgXfEg1.mjs";import{a as resolveSqliteDatabaseFilePaths}from"./sqlite-files-731tEtYd.mjs";import{n as assertOpenClawStateDatabaseForMaintenance}from"./openclaw-state-db-maintenance-BGNaiVRT.mjs";import{l as runOpenClawStateWriteTransaction}from"./openclaw-state-db-BlgxYkYw.mjs";import{a as readMainTableList}from"./sqlite-strict-C9s4I9nn.mjs";import{a as invalidateRegisteredAgentDatabasesMemo}from"./openclaw-agent-db-registry-listing-Bnnfy4SV.mjs";import{i as readActiveUpdateRun}from"./update-run-read.kernel-D9gumRXY.mjs";import{f as isUpdateDoctorLintPass}from"./update-phase-CCaxuca0.mjs";import{t as note}from"./note-e4DtVgg2.mjs";import fs from"node:fs";import path from"node:path";
//!src/state/openclaw-agent-db-path-repair.ts
function repairOpenClawAgentDatabasePaths(e,t=false){const{db:n,path:r}=e;const i=isPostgresSqlConnection(n);if(!i&&!t)assertOpenClawStateDatabaseForMaintenance(n,{pathname:r});const a=getNodeSqliteKysely(n);const o=executeSqliteQuerySync(n,a.selectFrom(`agent_databases`).selectAll().orderBy(`last_seen_at`,`desc`).orderBy(`path`)).rows;const s=/* @__PURE__ */ new Map;for(const e of o){const t=resolveOpenClawAgentDatabaseStoredPath(r,resolveOpenClawRegisteredAgentDatabasePath(r,e.path));const n=`${e.agent_id}\0${t}`;const i=s.get(n)??{stored:t,aliases:[]};i.aliases.push(e);s.set(n,i)}const c=[...s.values()].flatMap(({stored:e,aliases:t})=>{const n=t[0];const i=resolveOpenClawRegisteredAgentDatabasePath(r,n.path);const a=path.basename(i).replace(/\.\d+(?=\.sqlite$)/,``);const o=a===`(multiple).sqlite`||a===`(multiple).${n.agent_id}.sqlite`;const s=process.platform===`win32`&&!path.isAbsolute(e)&&t.some(e=>e.path.startsWith(`\\\\?\\`));return o||s?[{stored:e,aliases:t,newest:n,registeredPath:i,sentinel:o}]:[]});const l=c.reduce((e,{sentinel:t,aliases:n})=>e+(t?n.length:1),0);if(l===0)return{repaired:0,warnings:[]};const u=t?void 0:readActiveUpdateRun(n);const d=t?`during update Doctor. Run openclaw doctor --fix to repair agent database paths.`:u&&`while update ${u.runId} is in progress. Run openclaw doctor --fix after the update finishes.`;if(d)return{repaired:0,warnings:[`Skipped agent database path repair for ${l} registration(s) ${d}`]};if(i)requireNativeDatabaseFile(`postgres`,`repair`);const f=[];for(const{stored:e,aliases:t,newest:r,registeredPath:i,sentinel:o}of c){if(o)try{const e=resolveSqliteDatabaseFilePaths(i);const t=e.map(e=>fs.lstatSync(e,{throwIfNoEntry:false}));if([t[1],t[3]].some(e=>e?.size))throw new Error(`database or sidecar needs inspection`);if(t[0]?.size)try{var p=_usingCtx();const e=p.u(openNodeSqliteDatabase(resolveImmutableSqliteFileUri(i),{readOnly:true}));admitSqliteSchema(e);const t=getNodeSqliteKysely(e);const n=/* @__PURE__ */ new Set([`schema_meta`,`session_key_contract`,`memory_index_state`]);for(const{type:r,name:i}of readMainTableList(e)){if(r===`shadow`||n.has(i))continue;const a=t.selectFrom(i).selectAll().limit(1);if(executeSqliteQuerySync(e,a).rows.length)throw new Error(`contains user rows`)}}catch(e){p.e=e}finally{p.d()}for(const t of e)fs.rmSync(t,{force:true})}catch(e){f.push(`Removed invalid agent database registration; kept ${i} for inspection (${String(e)}).`)}else{const t={...r,path:e};executeSqliteQuerySync(n,a.insertInto(`agent_databases`).values(t).onConflict(e=>e.columns([`agent_id`,`path`]).doUpdateSet(t)))}const s=t.map(e=>e.path).filter(t=>o||t!==e);executeSqliteQuerySync(n,a.deleteFrom(`agent_databases`).where(`agent_id`,`=`,r.agent_id).where(`path`,`in`,s))}return{repaired:l,warnings:f}}
//!src/commands/doctor-agent-database-paths.ts
function noteDoctorAgentDatabasePathHealth(e){if(!e.shouldRepair)return[];const t=runOpenClawStateWriteTransaction(t=>repairOpenClawAgentDatabasePaths(t,isUpdateDoctorLintPass(e.env)),{env:e.env},{operationLabel:`doctor.agent-database-paths`});if(t.repaired>0){invalidateRegisteredAgentDatabasesMemo({env:e.env});note(`Repaired ${t.repaired} agent database path registration(s).`,`Doctor changes`)}if(t.warnings.length>0)note(t.warnings.join(`
`),`Doctor warnings`);return t.warnings}export{noteDoctorAgentDatabasePathHealth};