openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
7 lines • 8.78 kB
JavaScript
import{a as resolveRequiredHomeDir,c as resolveUserPath,t as expandHomePrefix}from"./home-dir-DEN5DisQ.mjs";import{r as isPathInside}from"./path-guards-DGttsOJI.mjs";import{n as normalizeAgentId}from"./agent-id-cOfqmfln.mjs";import"./session-key-BPgKfU2m.mjs";import{i as listAgentIds}from"./agent-roster-4XrgUgPK.mjs";import{c as resolveIncognitoOpenClawAgentSqlitePath,l as resolveOpenClawAgentSqlitePath}from"./openclaw-agent-db.paths-SkzMsgQ0.mjs";import{n as listSqliteTargetCandidatePathsForSessionStorePath}from"./session-sqlite-target-paths-CA4CNfL-.mjs";import{l as resolveSessionStorePathCore}from"./paths-CcxgiC3g.mjs";import{F as resolveRegisteredAgentIdForDir,L as unregisterResolvedAgentDir,M as isPathOwnedByAnotherRegisteredAgent,N as normalizeAgentDirRegistryPath,P as registerResolvedAgentDir,a as resolveAgentDir}from"./agent-scope-config-CG9CFpGX.mjs";import{m as readDatabasePathIdentitySync}from"./sqlite-worker-identity-CikEUuqF.mjs";import{n as executeExistingOpenClawStateRead}from"./openclaw-state-db-readonly-Cd5_cxBs.mjs";import{_ as withSqliteReadOnlyWorkerScope,f as runSqliteReadOnlyOperation}from"./sqlite-readonly-worker-C1mJaAX6.mjs";import{a as resolveSqliteDatabaseFilePaths}from"./sqlite-files-731tEtYd.mjs";import{r as cloneEnvWithPlatformSemantics}from"./config-env-vars-DCDGIBtT.mjs";import{n as captureOpenClawStateReadWorkerContext}from"./openclaw-state-worker-context-Dx8YjqO2.mjs";import"./agent-scope-Dw5XRSEh.mjs";import{n as resolveSessionStoreCompatibilityAgentId}from"./legacy.default-agent-owner-DUgZJ5NH.mjs";import{r as assertNoAgentDatabaseLeasesAsync}from"./agent-deletion-journal-Dee50E62.mjs";import{d as prepareOpenClawAgentDatabaseRegistrySnapshotRead}from"./openclaw-agent-db-registry-listing-Bnnfy4SV.mjs";import{a as closeOpenClawAgentDatabaseByPathAsync}from"./openclaw-agent-db-lifecycle-CWZDU3Sk.mjs";import"./openclaw-agent-db-BUym2U31.mjs";import{r as closeAuthProfileReadPool}from"./sqlite-read-pool-YeZ4G2Ax.mjs";import{t as memorySessionActorOwners}from"./session-actor-memory-owner-yGnpeolI.mjs";import{t as findOverlappingWorkspaceAgentIds}from"./agent-delete-safety-tdYvLPcq.mjs";import path from"node:path";import{isDeepStrictEqual}from"node:util";
//!src/agents/agent-delete-session-store-safety.ts
var AgentSharedStoreOwnerError=class extends Error{};function assertAgentSessionStoreDeletionBlocker(e,t){if(t!==void 0)throw new AgentSharedStoreOwnerError(`Agent "${normalizeAgentId(e)}" owns the session database still used by agent "${t}" and cannot be deleted. Keep this owner configured until shared history can be moved with a supported migration; no such migration is currently available.`)}
//!src/agents/agent-delete-session-store-safety.targets.ts
function captureCandidates(e){return[...new Set(e.flatMap(e=>listSqliteTargetCandidatePathsForSessionStorePath(e)))].toSorted().map(e=>({path:e,identity:readDatabasePathIdentitySync(e)}))}function prepareAgentSessionStoreDeletionSafety(e,t,n){const r=normalizeAgentId(t);const i=cloneEnvWithPlatformSemantics(n);const a=structuredClone(e);const o=e.session?.store;if(o?.trim()){a.agents={...a.agents,defaults:{...a.agents?.defaults,sessionStore:{agentId:resolveSessionStoreCompatibilityAgentId(e)}}};a.session={...a.session,store:path.resolve(resolveUserPath(`.`),o.startsWith(`~`)?expandHomePrefix(o,{home:resolveRequiredHomeDir(i),env:i}):o)}}const s=o?.trim()?listAgentIds(a).filter(e=>normalizeAgentId(e)!==r).map(e=>resolveSessionStorePathCore(a.session?.store,{agentId:e,env:i})):[];return{config:a,agentId:r,env:i,targets:{stores:s,candidates:captureCandidates(s)}}}function assertAgentSessionStoreDeletionTargetsCurrent(e){if(!isDeepStrictEqual(captureCandidates(e.stores),e.candidates))throw new Error(`Agent session database changed during deletion planning.`)}
//!src/agents/agent-delete-databases.ts
function resolveAgentDeleteRuntimeDirs(e,t,n){const r=normalizeAgentId(e);return[t,...n.filter(e=>normalizeAgentId(e.agentId)===r).map(e=>path.dirname(e.path))]}function resolveAgentDeleteDatabasePaths(e,t,n,r){return/* @__PURE__ */ new Set([resolveOpenClawAgentSqlitePath({agentId:e,env:r}),resolveOpenClawAgentSqlitePath({agentId:e,env:r,path:path.join(t,`openclaw-agent.sqlite`)}),...n])}async function drainAgentDeleteDatabaseWork(e,t={}){const{retireSessionHistoryWorkerDatabases:n}=await import(`./session-transcript-worker-resources-Cfc2yf8R.mjs`);const{agentId:r,agentDir:i,databasePaths:a}=e.entry;await e.assertCurrentAsync();await n({agentId:r,paths:[...resolveAgentDeleteDatabasePaths(r,i,a,t.env),resolveIncognitoOpenClawAgentSqlitePath({agentId:r,env:t.env})]});await e.assertCurrentAsync()}async function retireAgentDeleteRuntime(e,t){const n=e.entry.agentId;const{retirePreparedModelRuntimeAgent:r}=await import(`./prepared-model-runtime-CkWDDB-R.mjs`);await e.assertCurrentAsync();await r({agentId:n,agentDirs:t});await e.assertCurrentAsync()}async function finishAgentDeleteDatabases(e){const{deletion:t,agentDir:n,deleteFiles:r,complete:i}=e;await t.assertCurrentAsync();if(!i)return;const a=t.entry.agentId;unregisterResolvedAgentDir({agentId:a,agentDir:n});await t.finish({unregisterDatabases:r})}async function readAgentDeleteDatabaseRegistry(e={}){const t=await prepareOpenClawAgentDatabaseRegistrySnapshotRead({...e,includeIncompatibleSchemaVersions:true,fresh:true}).read();t.assertCurrent();if(t.result.status!==`available`)throw new Error(`OpenClaw agent database registry is unavailable during deletion.`);return t.result.entries}function prepareJournaledAgentDirOwnership(e,t,n){for(const t of listAgentIds(e))resolveAgentDir(e,t);if(resolveRegisteredAgentIdForDir(n)!==void 0)return;registerResolvedAgentDir({agentId:t,agentDir:n})}async function assertAgentSessionStoreDeletionSafe(e,t,n={}){if(!e.session?.store?.trim())return;const r=prepareAgentSessionStoreDeletionSafety(e,t,n.env??process.env);const i=captureOpenClawStateReadWorkerContext({path:n.database?.path??n.path,env:r.env});const a=await executeExistingOpenClawStateRead({path:i.admission.databasePath,env:i.environment},{type:`agentDeletion.sessionStoreBlocker`,input:{...r,databasePath:i.admission.databasePath,env:i.environment}},{context:i,current:true});i.admission.assertCurrent();assertAgentSessionStoreDeletionTargetsCurrent(r.targets);let o=a?.blocker;if(!a){const e=r.targets.candidates.find(({identity:e})=>e.key.startsWith(`file:`));if(e){const t=await withSqliteReadOnlyWorkerScope(()=>runSqliteReadOnlyOperation(e.identity.canonicalPath,{type:`agentRetirement.sessionStoreBlocker`,input:r},{source:`canonical`,expectedIdentity:e.identity.key,env:r.env}));i.admission.assertCurrent();assertAgentSessionStoreDeletionTargetsCurrent(r.targets);o=t.blocker}}assertAgentSessionStoreDeletionBlocker(r.agentId,o)}function resolveSurvivingDatabaseFilePaths(e,t,n){return[...new Set(e.filter(e=>normalizeAgentId(e.agentId)!==t).flatMap(e=>resolveSqliteDatabaseFilePaths(e.path)).map(e=>normalizeAgentDirRegistryPath(e,n)))]}function isPathOwnedBySurvivingAgent(e,t,n,r=[],i){const a=normalizeAgentDirRegistryPath(n,i);return isPathOwnedByAnotherRegisteredAgent({agentId:t,pathname:n,env:i})||findOverlappingWorkspaceAgentIds(e,t,n,i).length>0||r.some(e=>e===a||isPathInside(e,a)||isPathInside(a,e))}async function prepareAgentDeleteDatabases(e,t,n,r={},i){const a=await readAgentDeleteDatabaseRegistry(r);const o=resolveSurvivingDatabaseFilePaths(a,t,r.env);const s=resolveAgentDeleteDatabasePaths(t,n,a.filter(e=>normalizeAgentId(e.agentId)===t).map(e=>e.path),r.env);for(const e of s){if(i)await i.assertCurrentAsync();await closeOpenClawAgentDatabaseByPathAsync(e,t)}if(i)await i.assertCurrentAsync();memorySessionActorOwners.closeDatabase({agentId:t,path:resolveIncognitoOpenClawAgentSqlitePath({agentId:t,env:r.env})});const c=[...s].filter(n=>resolveSqliteDatabaseFilePaths(n).every(n=>!isPathOwnedBySurvivingAgent(e,t,n,o,r.env)));if(i)await i.assertCurrentAsync();for(const e of c)closeAuthProfileReadPool({kind:`database`,databasePath:e});if(i)await i.runWithWorker((e,t)=>e.execute({type:`agentDeletion.assertNoDatabaseLeases`,input:{guard:t}}));else await assertNoAgentDatabaseLeasesAsync(t,r);const l=c.map(resolveSqliteDatabaseFilePaths);const u=l.filter(e=>{const t=path.relative(n,e[0]??n);return t.startsWith(`..`)||path.isAbsolute(t)});return{agentDirs:resolveAgentDeleteRuntimeDirs(t,n,a),registrationPaths:[...s],readerPaths:c,fileGroups:l,relocatedFileGroups:u}}export{prepareAgentDeleteDatabases as a,resolveSurvivingDatabaseFilePaths as c,isPathOwnedBySurvivingAgent as i,retireAgentDeleteRuntime as l,drainAgentDeleteDatabaseWork as n,prepareJournaledAgentDirOwnership as o,finishAgentDeleteDatabases as r,readAgentDeleteDatabaseRegistry as s,assertAgentSessionStoreDeletionSafe as t,AgentSharedStoreOwnerError as u};