openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 16.5 kB
JavaScript
import{i as e}from"./transcript-global-singleton-DIJgobpU.mjs";import{B as t,F as n,d as r}from"./transcript-plugin-instance-invocation-7y9lfaeO.mjs";import{T as i,f as a,g as o,s,w as c}from"./transcript-agent-creation-claim-Da8lP1e6.mjs";import{n as l}from"./transcript-path-guards-u0N7Z9_x.mjs";import{c as u,m as d,o as f}from"./transcript-sqlite-worker-identity-CA1Up8Es.mjs";import{t as p}from"./transcript-errors-B-oBGrpW.mjs";import{C as m}from"./transcript-sqlite-post-commit-B_pko32i.mjs";import{n as h}from"./transcript-agent-id-DmscQLD6.mjs";import{c as g}from"./transcript-openclaw-agent-db.paths-DbFl2s5M.mjs";import _ from"node:path";import{AsyncLocalStorage as v}from"node:async_hooks";import y from"node:fs";import"@openclaw/fs-safe/advanced";import{sha256FileSync as b}from"@openclaw/fs-safe/durability";const x=e(Symbol.for(`openclaw.maintenanceLiveAuthorityReads`),()=>/* @__PURE__ */ new WeakMap);function allowsMaintenanceLiveAuthorityReads(e,t){let n=r(t);for(let t=e;t;t=S.parents.get(t))if(x.get(t)?.has(n))return!0;return!1}function maintenanceOwnerHasSourceCustody(e,t){return e?.ownsSchemaMaintenance?(e.assertReadAdmission(),!allowsMaintenanceLiveAuthorityReads(e,t)):!1}const S=e(Symbol.for(`openclaw.databaseMaintenanceResources`),()=>({current:new v,claims:/* @__PURE__ */ new WeakMap,parents:/* @__PURE__ */ new WeakMap}));function getOpenClawDatabaseMaintenanceScope(){return S.current.getStore()?.scope}function isOpenClawDatabaseMaintenanceWorkerAccess(){return S.current.getStore()?.workerAccess===!0}function runOutsideOpenClawDatabaseMaintenanceScope(e){return S.current.exit(e)}function isOpenClawDatabaseMaintenanceResourceOwned(e,t){return S.claims.get(e)?.scope===t}function getOpenClawDatabaseMaintenanceResourceScope(e){return S.claims.get(e)?.scope}function observeOpenClawDatabaseMaintenanceResource(e){if(!e)return;let t=S.claims.get(e),n=getOpenClawDatabaseMaintenanceScope();if(!t)return;let r=commonMaintenanceAncestor(t.scope,n);r!==t.scope&&(t.scope.assertAdmission(),t.release(),S.claims.delete(e),r&&r.own(e,t.phase,t.close))}function commonMaintenanceAncestor(e,t){let n=/* @__PURE__ */ new Set;for(let t=e;t;t=S.parents.get(t))n.add(t);for(let e=t;e;e=S.parents.get(e))if(n.has(e))return e}function sameFileMutationFingerprint(e,t){return e.birthtimeNs===t.birthtimeNs&&e.ctimeNs===t.ctimeNs&&e.dev===t.dev&&e.ino===t.ino&&e.mtimeNs===t.mtimeNs&&e.size===t.size}function hashFileDescriptorSync(e,t){let{digest:n,bytes:r}=b(e,{maxBytes:t});return{sha256:n,sizeBytes:r}}function flushAndHashFileSync(e){let t=y.openSync(e,`r+`);try{return y.fsyncSync(t),hashFileDescriptorSync(t)}finally{y.closeSync(t)}}const C=e(Symbol.for(`openclaw.agentDatabaseExecutionAdmissionClosedError`),()=>class extends Error{}),w=e(Symbol.for(`openclaw.agentDatabaseSchemaAdmissionChangedError`),()=>class extends Error{constructor(){super(`Agent schema admission changed before publication; retry the operation`)}}),T=e(Symbol.for(`openclaw.agentDatabaseSchemaAdmissionInvalidError`),()=>class extends Error{constructor(){super(`Invalid agent schema admission receipt`)}}),E=e(Symbol.for(`openclaw.agentDatabaseStartupPublicationError`),()=>class extends Error{constructor(e){super(p(e),{cause:e})}}),D=`STATE_DATABASE_READ_ADMISSION_INVALIDATED`;var StateDatabaseReadAdmissionInvalidatedError=class extends Error{constructor(...e){super(...e),this.code=D}};function isStateDatabaseReadAdmissionInvalidatedError(e){return n(e)===D}function createOpenClawStateDatabaseAsyncLifecycle(){let e=/* @__PURE__ */ new Set,t=/* @__PURE__ */ new Map,n=/* @__PURE__ */ new Map,r=/* @__PURE__ */ new Set,i=/* @__PURE__ */ new Map,o=Promise.resolve(),proofFor=()=>{let e=new SharedArrayBuffer(BigInt64Array.BYTES_PER_ELEMENT);return new BigInt64Array(e)[0]=-1n,e},generationFor=e=>{let t=new SharedArrayBuffer(BigInt64Array.BYTES_PER_ELEMENT);return{integrity:{identity:e,lifetime:{revision:new SharedArrayBuffer(BigInt64Array.BYTES_PER_ELEMENT),epoch:0n},revision:t,epoch:0n,proof:proofFor()}}},publishCreatedIdentity=(e,n)=>{Atomics.add(new BigInt64Array(e.generation.integrity.revision),0,1n),Atomics.add(new BigInt64Array(e.generation.integrity.lifetime.revision),0,1n),t.delete(e.identity.key),e.identity=n,e.generation.integrity.identity=n,t.set(n.key,e)},known=e=>n.get(e)??n.get(a({path:e})),bindPath=(e,t)=>{e.paths.add(t),n.has(t)||n.set(t,e)},overlaps=(e,t)=>{if(e.identity.key===t.identity.key)return!0;for(let n of e.paths)if(t.paths.has(n))return!0;return!1},isSealed=e=>{if(r.size===0)return!1;for(let t of r)if(t.record===void 0||overlaps(t.record,e))return!0;return!1},hasCloseCustody=e=>isSealed(e)||i.has(e),assertOpen=e=>{if(isSealed(e))throw new StateDatabaseReadAdmissionInvalidatedError(`OpenClaw state database read admission is closed`)},findPhysicalRecord=e=>{let r=t.get(e.key);if(!r||!e.key.startsWith(`file:`)||hasCloseCustody(r))return r;for(let t of f(r.paths,e))r.admissions.delete(t),r.paths.delete(t),n.get(t)===r&&n.delete(t);if(r.paths.size>0)return r.paths.has(r.identity.canonicalPath)||(r.identity=e,r.generation.integrity.identity=e),r;invalidate(r),forget(r)},resolve=(e,r)=>{let i=n.get(e);if(i&&(!r||i.identity.key===r.key))return!r&&i.identity.key.startsWith(`path:`)?resolve(e,d(e)):i;let a=r??d(e),o=findPhysicalRecord(a);return!o&&a.key.startsWith(`file:`)&&(o=[...t.values()].find(e=>{if(!e.identity.key.startsWith(`path:`))return!1;try{return d(e.identity.canonicalPath).key===a.key}catch{return!1}}),o&&publishCreatedIdentity(o,a)),o||(o={coordinationKey:a.key,identity:a,paths:/* @__PURE__ */ new Set,generation:generationFor(a),admissions:/* @__PURE__ */ new Map},t.set(a.key,o)),bindPath(o,e),bindPath(o,a.canonicalPath),o},resolveForNative=e=>{let t=a({path:e}),r=n.get(t);if(r)return r;let i=u(t);return i?resolve(t,i):void 0},invalidate=e=>{for(let n of e?[e]:t.values())Atomics.store(new BigInt64Array(n.generation.integrity.revision),0,-1n),Atomics.store(new BigInt64Array(n.generation.integrity.lifetime.revision),0,-1n),n.generation=generationFor(n.identity),n.admissions.clear()},seal=e=>{invalidate(e);let t={record:e};return r.add(t),t},release=e=>{e.seal&&=(r.delete(e.seal),void 0)},forget=e=>{if(!hasCloseCustody(e)&&t.get(e.identity.key)===e){t.delete(e.identity.key);for(let r of e.paths)if(n.get(r)===e){n.delete(r);for(let e of t.values())if(e.paths.has(r)){n.set(r,e);break}}}},captureRecord=(e,n)=>{let r=e.admissions.get(n);if(r)return r;let i=e.generation,a=Object.freeze({databasePath:n,coordinationKey:e.coordinationKey,get identity(){return e.identity},captureIntegrity(){a.assertCurrent();let t=i.integrity,n=Atomics.load(new BigInt64Array(t.revision),0),r=Atomics.load(new BigInt64Array(t.lifetime.revision),0);return(n!==t.epoch||r!==t.lifetime.epoch)&&(i.integrity={...t,identity:e.identity,lifetime:{revision:t.lifetime.revision,epoch:r},epoch:n,proof:proofFor()}),i.integrity},assertCurrent(){if(assertOpen(e),t.get(e.identity.key)!==e||e.generation!==i||e.admissions.get(n)!==a)throw new StateDatabaseReadAdmissionInvalidatedError(`OpenClaw state database read admission changed`)}});return e.admissions.set(n,a),a};return{identity(e){return known(e)?.identity??u(e)},knownIdentity(e){return known(e)?.identity},integrity(e){let t=resolveForNative(e);return t&&!isSealed(t)?captureRecord(t,a({path:e})).captureIntegrity?.():void 0},publish(e,r){let o=a({path:e}),s=r??d(o),c=n.get(o),l=r&&c?.identity.key===s.key?t.get(s.key):findPhysicalRecord(s);if(c&&c.identity.key!==s.key){if(c.identity.key.startsWith(`path:`)&&!l)publishCreatedIdentity(c,s),l=c;else{let e=i.get(c);e?(e.superseded=!0,e.seal??=seal(c)):invalidate(c),forget(c)}}return l||=resolve(o,s),bindPath(l,o),bindPath(l,r?l.identity.canonicalPath:s.canonicalPath),{identity:s,admission:captureRecord(l,o)}},invalidate(e){if(e===void 0)invalidate();else{let t=known(e);t&&invalidate(t)}},register(t){e.add(t);for(let e of i.values())e.queue?.add(t);return()=>{e.delete(t)}},capture(e){let t=n.get(e),r=t?.admissions.get(e);if(t&&r&&t.identity.key.startsWith(`file:`))return r.assertCurrent(),r;let i=a({path:e}),o=resolve(i);return assertOpen(o),captureRecord(o,i)},holdExclusion(e){let n=resolve(a({path:e})),i=seal(n);return()=>{r.delete(i);for(let e of t.values())overlaps(n,e)&&forget(e)}},close(n,r){let a=n===void 0?void 0:resolveForNative(n);if(n!==void 0&&!a)return Promise.resolve(r());let s=i.get(a);if(s?.pending)return s.pending;s||(s={retained:/* @__PURE__ */ new Set},i.set(a,s)),s.seal??=seal(a);let c=s,l=o.then(async()=>{let n=/* @__PURE__ */ new Set([...e,...c.retained]);for(let e of i.values())for(let t of e.retained)n.add(t);c.queue=n;let o=[];for(;c.queue.size;){let e=[...c.queue].filter(e=>e.phase!==`after-resources`);if(!e.length&&o.length){for(let e of c.queue)c.retained.add(e);break}let t=e.length?e:[...c.queue];for(let e of t)c.queue.delete(e);await Promise.all(t.map(async e=>{try{let t=a?.identity;(!t||!e.admission||t.key===e.admission.identity.key)&&await e.close(t),c.retained.delete(e)}catch(t){c.retained.add(e),o.push(t)}}))}o.length&&!c.superseded&&release(c),m(o,`OpenClaw state resource drainage failed`);let s=await r(a?.identity);if(i.delete(a),release(c),a===void 0){for(let[e,t]of i)t.pending||(i.delete(e),release(t));for(let e of t.values())forget(e)}else forget(a);return s}).finally(()=>{c.queue=void 0});return c.pending=l,o=l.then(()=>void 0,()=>void 0),l.catch(()=>{c.pending=void 0}),l}}}const O=e(Symbol.for(`openclaw.agentDeletionDatabaseCleanup`),()=>new v),k=e(Symbol.for(`openclaw.agentDeletionDatabaseCleanupHandles`),()=>/* @__PURE__ */ new Map),A=e(Symbol.for(`openclaw.agentDeletionDatabaseCleanupExecutions`),()=>/* @__PURE__ */ new Map);function getAgentDeletionDatabaseCleanup(e){let t=O.getStore();if(!t)return;let n=g(e);if(t.agentId!==h(e.agentId)||t.path!==n&&t.canonicalPath!==n)return;let r=e.statePath??o(e.env??process.env);if(t.statePath!==_.resolve(r))throw Error(`Agent deletion database cleanup belongs to another state database.`);return t}function captureAgentDeletionResourceOwner(e){let t=O.getStore();if(t?.ownsDatabase&&(e(t)||t.canonicalPath&&e({agentId:t.agentId,path:t.canonicalPath})))return t.assertCurrentHost(),t}function assertAgentDeletionDatabaseCleanupAccess(e,t){let n=getAgentDeletionDatabaseCleanup(t),r=k.get(e);if(r&&r!==n)throw Error(`Agent database belongs to an active deletion cleanup.`);n?.assertCurrent()}function assertAgentDeletionCleanupAliases(e,t){let n=g(e);for(let r of k.keys())t(r.path,n)&&assertAgentDeletionDatabaseCleanupAccess(r,e);for(let r of A.values())if(t(r.path,n)&&getAgentDeletionDatabaseCleanup(e)!==r)throw Error(`Agent database belongs to an active deletion cleanup.`)}function assertAgentDeletionExecutionCleanupAccess(e,t){let n=A.get(e),r=getAgentDeletionDatabaseCleanup(t);if(n&&n!==r)throw Error(`Agent database belongs to an active deletion cleanup.`);r?.assertCurrentHost()}function registerAgentDeletionExecutionCleanup(e,t,n){let r=getAgentDeletionDatabaseCleanup(t);r?.worker&&r.ownsDatabase&&(r.assertCurrentHost(),A.get(e)!==r&&(A.set(e,r),r.registerClose(async()=>{await n(),A.delete(e)})))}function registerAgentDeletionDatabaseCleanup(e,t){let n=getAgentDeletionDatabaseCleanup(t);if(n?.assertCurrent(),n?.ownsDatabase&&!n.workerOwned)return k.set(e,n),n}function releaseAgentDeletionDatabaseCleanup(e){k.delete(e)}const j=e(Symbol.for(`openclaw.agentDatabaseAsyncResources`),()=>({active:/* @__PURE__ */ new Set,closing:/* @__PURE__ */ new Map,selections:/* @__PURE__ */ new Map}));i(async(e,t)=>{let n=(await Promise.allSettled([.../* @__PURE__ */ new Set([...j.active,...j.closing.keys()])].filter(n=>!t?.has(_.resolve(n.path))&&e.some(e=>c(e,n.path))).map(e=>closeAgentDatabaseResource(e)))).flatMap(e=>e.status===`rejected`?[e.reason]:[]);if(n.length>0)throw AggregateError(n,`Agent database resource cleanup failed`)});function matchesAgentDatabaseClose(e,t){return(e.path===void 0||e.path===t.path||t.ownership===`unresolved`&&c(t,e.path))&&(e.rootPath===void 0||l(e.rootPath,t.path)||t.ownership===`unresolved`&&c(t,e.rootPath))&&(e.agentId===void 0||t.ownership===`unresolved`||e.agentId===t.agentId)}function registerOpenClawAgentDatabaseAsyncResource(e,t){return registerAgentDatabaseResource({...e,ownership:`known`,agentId:h(e.agentId)},t)}function registerOpenClawAgentDatabaseSyncResource(e){return registerAgentDatabaseResource({...e,ownership:`known`,agentId:h(e.agentId),close:async()=>e.close(),closeSync:e.close})}function registerOpenClawAgentDatabaseReadCandidateResource(e){return registerAgentDatabaseResource({...e,ownership:`unresolved`})}function registerAgentDatabaseResource(e,t){let n={...e,path:_.resolve(e.path)};n.cleanupOwner=captureAgentDeletionResourceOwner(e=>matchesAgentDatabaseClose(e,n)),assertAgentDatabaseResourceAdmission(n);let r=t&&n.ownership===`known`?s(n,t,()=>closeAgentDatabaseResource(n)):void 0,unregister=()=>{j.active.delete(n),r?.()};return getOpenClawDatabaseMaintenanceScope()?.own(unregister,`agent-resources`,()=>closeAgentDatabaseResource(n)),j.active.add(n),unregister}function assertAgentDatabaseResourceAdmission(e){if([...j.selections.keys()].some(t=>matchesAgentDatabaseClose(t,e))||[...j.closing.keys()].some(t=>(t.path===e.path||t.ownership===`unresolved`&&c(t,e.path)||e.ownership===`unresolved`&&c(e,t.path))&&(t.ownership===`unresolved`||e.ownership===`unresolved`||t.agentId===e.agentId)))throw new C(`Agent database resources are closing: ${e.path}`)}function closeAgentDatabaseResource(e,n){if(e.ownership===`known`&&e.closeSync){j.closing.set(e,void 0);try{return e.revoke(),e.closeSync(),j.active.delete(e),j.closing.delete(e),Promise.resolve()}catch(r){n&&n(e.path,r);let i=Promise.reject(t(r,`Agent database cleanup failed`));return i.catch(()=>{}),i}}let r=j.closing.get(e);return r||(r=Promise.resolve().then(()=>e.close()),j.closing.set(e,r),r.then(()=>{j.active.delete(e),j.closing.delete(e)},()=>{j.closing.set(e,void 0)}).catch(()=>{})),n&&r.catch(t=>n(e.path,t)).catch(()=>{}),e.revoke(),r}function revokeAgentDatabaseResources(e,t){let n=/* @__PURE__ */ new Set([...j.active,...j.closing.keys()]),r=[];for(let i of n)matchesAgentDatabaseClose(e,i)&&r.push(closeAgentDatabaseResource(i,t));return r}async function drainAgentDatabaseResources(e,t,n){return withAgentDatabaseCloseFence(e,async()=>{let r=[.../* @__PURE__ */ new Set([...j.active,...j.closing.keys()])].filter(t=>matchesAgentDatabaseClose(e,t)&&(!n||t.cleanupOwner===n)),i=r.filter(e=>e.retireAfterResources);for(let e of i)j.closing.has(e)||j.closing.set(e,void 0),e.revoke();let a=(await Promise.allSettled(r.filter(e=>!e.retireAfterResources).map(e=>closeAgentDatabaseResource(e)))).flatMap(e=>e.status===`rejected`?[e.reason]:[]);if(a.length>0)throw AggregateError(a,`Agent database resource drainage failed`);let o=(await Promise.allSettled(i.map(e=>closeAgentDatabaseResource(e)))).flatMap(e=>e.status===`rejected`?[e.reason]:[]);if(o.length>0)throw AggregateError(o,`Agent database native retirement failed`);return t()})}async function withAgentDatabaseCloseFence(e,t){let n={...e};j.selections.set(n,void 0);try{let e=t([.../* @__PURE__ */ new Set([...j.active,...j.closing.keys()])].filter(e=>matchesAgentDatabaseClose(n,e)).map(e=>e.path));return j.selections.set(n,e),await e}finally{j.selections.delete(n)}}export{maintenanceOwnerHasSourceCustody as A,hashFileDescriptorSync as C,getOpenClawDatabaseMaintenanceScope as D,getOpenClawDatabaseMaintenanceResourceScope as E,runOutsideOpenClawDatabaseMaintenanceScope as M,isOpenClawDatabaseMaintenanceResourceOwned as O,flushAndHashFileSync as S,allowsMaintenanceLiveAuthorityReads as T,isStateDatabaseReadAdmissionInvalidatedError as _,registerOpenClawAgentDatabaseReadCandidateResource as a,T as b,assertAgentDeletionCleanupAliases as c,getAgentDeletionDatabaseCleanup as d,registerAgentDeletionDatabaseCleanup as f,createOpenClawStateDatabaseAsyncLifecycle as g,StateDatabaseReadAdmissionInvalidatedError as h,registerOpenClawAgentDatabaseAsyncResource as i,observeOpenClawDatabaseMaintenanceResource as j,isOpenClawDatabaseMaintenanceWorkerAccess as k,assertAgentDeletionDatabaseCleanupAccess as l,releaseAgentDeletionDatabaseCleanup as m,drainAgentDatabaseResources as n,registerOpenClawAgentDatabaseSyncResource as o,registerAgentDeletionExecutionCleanup as p,matchesAgentDatabaseClose as r,revokeAgentDatabaseResources as s,assertAgentDatabaseResourceAdmission as t,assertAgentDeletionExecutionCleanupAccess as u,C as v,sameFileMutationFingerprint as w,E as x,w as y};