openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.7 kB
JavaScript
import{i as resolveGlobalSingleton}from"./global-singleton-CTnqrmaI.js";import{x as resolveIdentityPathViaExistingAncestorSync}from"./runtime-doctor-migrations-D8alsOFs.js";import fs from"node:fs";import{AsyncLocalStorage}from"node:async_hooks";
//!src/state/openclaw-state-maintenance-context.ts
const liveAuthorityReads=resolveGlobalSingleton(Symbol.for(`openclaw.maintenanceLiveAuthorityReads`),()=>/* @__PURE__ */ new WeakMap);function allowsMaintenanceLiveAuthorityReads(e,t){return hasAdmittedMaintenanceLiveAuthorityReads(e,resolveIdentityPathViaExistingAncestorSync(t))}function hasAdmittedMaintenanceLiveAuthorityReads(e,t){for(let n=e;n;n=maintenanceResources.parents.get(n))if(liveAuthorityReads.get(n)?.has(t))return true;return false}function maintenanceOwnerHasSourceCustody(e,t){if(!e?.ownsSchemaMaintenance)return false;e.assertReadAdmission();return!allowsMaintenanceLiveAuthorityReads(e,t)}function readCustodiedDrainedSqliteSource(e){if(!maintenanceOwnerHasSourceCustody(getOpenClawDatabaseMaintenanceScope(),e))return;const t=resolveIdentityPathViaExistingAncestorSync(e);if(fs.existsSync(`${t}-journal`)||(fs.statSync(`${t}-wal`,{throwIfNoEntry:false})?.size??0)>0)return;return fs.statSync(t,{bigint:true})}const maintenanceResources=resolveGlobalSingleton(Symbol.for(`openclaw.databaseMaintenanceResources`),()=>({current:new AsyncLocalStorage,claims:/* @__PURE__ */ new WeakMap,parents:/* @__PURE__ */ new WeakMap}));function getOpenClawDatabaseMaintenanceScope(){return maintenanceResources.current.getStore()?.scope}function isOpenClawDatabaseMaintenanceWorkerAccess(){return maintenanceResources.current.getStore()?.workerAccess===true}function runOutsideOpenClawDatabaseMaintenanceScope(e){return maintenanceResources.current.exit(e)}function isOpenClawDatabaseMaintenanceResourceOwned(e,t){return maintenanceResources.claims.get(e)?.scope===t}function getOpenClawDatabaseMaintenanceResourceScope(e){return maintenanceResources.claims.get(e)?.scope}function runMaintenance(e,t,n=isOpenClawDatabaseMaintenanceWorkerAccess()){const r={scope:e,active:true,workerAccess:n};try{const n=maintenanceResources.current.run(r,t);if(n instanceof Promise){const settled=()=>{r.active=false};n.then(settled,settled);e.track(n)}else r.active=false;return n}catch(e){r.active=false;throw e}}function captureOpenClawDatabaseMaintenanceResource(e,t){const n=maintenanceResources.claims.get(e);const assertCurrent=()=>{t.assertOwnerCurrent();if(n?.scope!==t||maintenanceResources.claims.get(e)!==n)throw new Error(`Database maintenance resource owner changed`)};assertCurrent();return{assertCurrent,async run(e){assertCurrent();return runMaintenance(t,e)},async runWorker(e){assertCurrent();return runMaintenance(t,e,true)}}}function observeOpenClawDatabaseMaintenanceResource(e){if(!e)return;const t=maintenanceResources.claims.get(e);const n=getOpenClawDatabaseMaintenanceScope();if(!t)return;const r=commonMaintenanceAncestor(t.scope,n);if(r===t.scope)return;t.scope.assertAdmission();t.release();maintenanceResources.claims.delete(e);if(r)r.own(e,t.phase,t.close)}function commonMaintenanceAncestor(e,t){const n=/* @__PURE__ */ new Set;for(let t=e;t;t=maintenanceResources.parents.get(t))n.add(t);for(let e=t;e;e=maintenanceResources.parents.get(e))if(n.has(e))return e}export{hasAdmittedMaintenanceLiveAuthorityReads as a,readCustodiedDrainedSqliteSource as c,getOpenClawDatabaseMaintenanceScope as i,runOutsideOpenClawDatabaseMaintenanceScope as l,captureOpenClawDatabaseMaintenanceResource as n,isOpenClawDatabaseMaintenanceResourceOwned as o,getOpenClawDatabaseMaintenanceResourceScope as r,observeOpenClawDatabaseMaintenanceResource as s,allowsMaintenanceLiveAuthorityReads as t};