openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 3.6 kB
JavaScript
import{Cn as e,Q as t}from"./openclaw-state-read-subsystem-Dwoo5bcz.mjs";import{Bt as n}from"./openclaw-state-read-src-RL5xtYG2.mjs";import r from"node:path";import{isMainThread as i,threadId as a}from"node:worker_threads";const o=new t(Symbol.for(`openclaw.sqliteReaderOwners`)),s=e(Symbol.for(`openclaw.sqliteReaderConnections`),()=>({nextId:0,byDatabase:/* @__PURE__ */ new WeakMap,byPath:/* @__PURE__ */ new Map,finalizer:new FinalizationRegistry(e=>forgetConnection(e))}));function sqliteReaderDatabasePathKey(e){let t=r.resolve(e);return process.platform===`win32`?n(t):t}function boundedOperation(e){return(e.trim()||`sqlite reader`).slice(0,120)}function withSqliteReaderOwner(e,t){return o.run({...e,operation:boundedOperation(e.operation),timing:{preparedAtMs:Date.now(),hostAdmissionWaitMs:0,databaseAdmissionWaitMs:0,transactions:o.getStore()?.timing.transactions??{count:0}}},t)}function captureSqliteReaderOwner(){let e=o.getStore();if(!e)return;let{timing:t,...n}=e;return n}function currentSqliteOperationTiming(){return o.getStore()?.timing}function currentOwner(e,t){let n=t??o.getStore();return{operation:boundedOperation(n?.operation??e),ownerKind:n?.ownerKind??(i?`main`:`worker`),...n?.actorId===void 0?{}:{actorId:n.actorId}}}function connectionFor(e){let t=s.byDatabase.get(e);if(!t){let n=Date.now(),r=e.location();t={id:++s.nextId,path:r?sqliteReaderDatabasePathKey(r):void 0,database:new WeakRef(e),owner:currentOwner(`sqlite connection`),openedAtMs:n,readers:/* @__PURE__ */ new Map},s.byDatabase.set(e,t),s.finalizer.register(e,t,t)}if(t.path){let e=s.byPath.get(t.path)??/* @__PURE__ */ new Map;e.set(t.id,t),s.byPath.set(t.path,e)}return t}function forgetConnection(e){if(e.path){let t=s.byPath.get(e.path);t?.delete(e.id),t?.size===0&&s.byPath.delete(e.path)}}function registerSqliteReaderConnection(e){e.isOpen&&connectionFor(e)}function retainSqliteReader(e,t,n){let r=connectionFor(e),i=Date.now(),o={...currentOwner(t,n),kind:`iterator`,connectionId:r.id,threadId:a,startedAtMs:i,lastProgressAtMs:i},s=Symbol(o.operation),c=r.readers;return c.set(s,o),{progress(){c.has(s)&&(o.lastProgressAtMs=Date.now())},release(){c.delete(s)}}}function diagnostics(e){let t=Date.now();return[...e].map(({startedAtMs:e,lastProgressAtMs:n,...r})=>Object.assign(r,{ageMs:Math.max(0,t-e),idleMs:Math.max(0,t-n)})).toSorted((e,t)=>t.ageMs-e.ageMs).slice(0,8)}function readSqliteReaderDiagnosticsForPath(e){let t=sqliteReaderDatabasePathKey(e),n=s.byPath.get(t),r=[],i=[],o=Date.now();for(let e of n?.values()??[]){let t=e.database.deref();t?.isOpen?(i.push(...e.readers.values()),r.push({...e.owner,connectionId:e.id,threadId:a,ageMs:Math.max(0,o-e.openedAtMs),transactionOpen:t.isTransaction,trackedReaders:e.readers.size})):forgetConnection(e)}return{scope:`current-thread`,blockingOwner:`unknown`,nativeStatements:`unobserved`,threadId:a,observedAtMs:o,connectionCount:r.length,readerCount:i.length,connections:r.toSorted((e,t)=>t.trackedReaders-e.trackedReaders||Number(t.transactionOpen)-Number(e.transactionOpen)).slice(0,8),activeReaders:diagnostics(i)}}function assertNoActiveSqliteReaders(e,t){let n=diagnostics(s.byDatabase.get(e)?.readers.values()??[]);if(n.length===0)return;let r=n[0];throw Error(`${t} retained ${n.length} active SQLite reader(s); oldest operation=${r.operation} ageMs=${r.ageMs}`)}export{registerSqliteReaderConnection as a,withSqliteReaderOwner as c,readSqliteReaderDiagnosticsForPath as i,captureSqliteReaderOwner as n,retainSqliteReader as o,currentSqliteOperationTiming as r,sqliteReaderDatabasePathKey as s,assertNoActiveSqliteReaders as t};