openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 6.96 kB
JavaScript
import{n as e}from"./openclaw-state-read-rolldown-runtime-C7TU3oOp.mjs";import{Cn as t,St as n,xt as r}from"./openclaw-state-read-subsystem-Dwoo5bcz.mjs";import{H as i,a,g as o,o as s}from"./openclaw-state-read-sqlite-database-admission-CrkztOzk.mjs";import"./openclaw-state-read-openclaw-state-db-cache-DyomEqgV.mjs";import{a as c}from"./openclaw-state-read-openclaw-state-db.paths-7XLpcdiq.mjs";import{t as l}from"./openclaw-state-read-errors-D4PLrhBz.mjs";import{a as u,i as d,m as f,r as p}from"./openclaw-state-read-kysely-sync-Bcu0Qb7F.mjs";import{o as m,s as h}from"./openclaw-state-read-openclaw-state-db-schema-helpers-QFZd8aJI.mjs";import{a as g}from"./openclaw-state-read-sqlite-transaction-Bt-IwVhZ.mjs";import{u as _}from"./openclaw-state-read-openclaw-state-db-B3dGXm7m.mjs";import{n as v}from"./openclaw-state-read-agent-id-DmscQLD6.mjs";import"./openclaw-state-read-session-key-H2_GWFw8.mjs";import{i as y}from"./openclaw-state-read-openclaw-state-db-readonly-Bv32Owin.mjs";import"./openclaw-state-read-session-row-changes-C0E3v5wT.mjs";import{r as b}from"./openclaw-state-read-openclaw-agent-db-registry.read-DCfsbJGg.mjs";import"./openclaw-state-read-openclaw-agent-db-registry-listing-BSp1z3ZT.mjs";import x from"node:path";import"node:crypto";const S={name:`agent.deletion-discovery`,schemaDependent:!0,read:e=>r(e)?e:void 0};function getPreparedAgentDeletionDiscovery(e){return a(e,S,()=>void 0)??void 0}function statusKey(e){return{name:`agent.deletion-status:${v(e)}`,read:e=>e===`absent`||e===`pending`||e===`complete`?e:void 0}}function getAgentDeletionJournalStatus(e,t){return s(e,statusKey(t))??void 0}function prepareAgentDeletionJournalStatus(e,t){let n=statusKey(t),r=o(e,n);return e=>(e?s(e,n):r())??void 0}t(Symbol.for(`openclaw.agentDeletionJournalStatusLoads`),()=>/* @__PURE__ */ new Map);const C=[`template_package_refs`,`template_mcp_server_refs`,`template_cron_refs`];function readTemplateSecondaryReferenceTables(e,t){return C.filter(n=>m(e,n)?!!e.prepare(`SELECT 1 FROM ${n} WHERE agent_id = ? LIMIT 1`).get(t):!1)}function hasTemplateDeletionOwnership(e,t){let n=u(e.db);return[`template_installs`,`template_workspace_files`].some(r=>m(e.db,r)&&d(e.db,n.selectFrom(r).select(`agent_id`).where(`agent_id`,`=`,t).limit(1))!==void 0)||readTemplateSecondaryReferenceTables(e.db,t).length>0}function readLegacyMigrationReceiptFromDatabase(e,t){let n=d(e,u(e).selectFrom(`migration_sources`).select([`source_sha256`,`removed_source`,`report_json`]).where(`source_key`,`=`,t));return n?{sourceKey:t,sourceSha256:n.source_sha256,removedSource:n.removed_source===1,reportJson:n.report_json}:null}function parseHolds(e){return e.map(e=>{if(!r(e)||typeof e.agentId!=`string`||e.agentId!==v(e.agentId)||typeof e.path!=`string`||!e.path.trim())throw Error(`Invalid agent database hold in deletion journal reconstruction receipt.`);return{agentId:e.agentId,path:e.path}})}function readReport(e){let t=readLegacyMigrationReceiptFromDatabase(e.db,`agent-deletion-journal-reconstruction`);if(!t)return;let n=JSON.parse(t.reportJson);if(!r(n)||typeof n.description!=`string`||!Array.isArray(n.held))throw Error(`Invalid agent deletion journal reconstruction receipt.`);let i=parseHolds(n.held);if(n.missingJournal!==void 0&&!Array.isArray(n.missingJournal))throw Error(`Invalid missing-journal provenance in deletion journal reconstruction receipt.`);let a=Array.isArray(n.missingJournal)?parseHolds(n.missingJournal):n.description===`Reconstructed the missing agent deletion journal; held databases require explicit agent restore or delete.`?i:[];if(a.some(e=>!i.some(t=>t.agentId===e.agentId&&t.path===e.path)))throw Error(`Missing-journal provenance refers to an unheld agent database.`);return{...n,description:n.description,held:i,missingJournal:a}}function decodeHolds(e,t){return t.map(t=>({agentId:t.agentId,path:c(e.path,t.path)}))}function readAgentDeletionRecoveryHolds(e){return decodeHolds(e,readReport(e)?.held??[])}var w=/* @__PURE__ */ e({parseAgentDeletionDatabasePaths:()=>parseAgentDeletionDatabasePaths,readAgentDatabaseDeletionSnapshot:()=>readAgentDatabaseDeletionSnapshot,readAgentDeletionJournalStatusInDatabase:()=>readAgentDeletionJournalStatusInDatabase,readRetainedAgentDeletionsFromDatabase:()=>readRetainedAgentDeletionsFromDatabase});function readAgentDeletionJournalStatusInDatabase(e,t){let n=getAgentDeletionJournalStatus(e,t);if(n)return n;if(!m(e,`agent_deletion_journal`))return`absent`;let r=d(e,u(e).selectFrom(`agent_deletion_journal`).select(`cleanup_completed`).where(`agent_id`,`=`,v(t)));return r?r.cleanup_completed===1?`complete`:`pending`:`absent`}function parseAgentDeletionDatabasePaths(e){let t=JSON.parse(e);if(n(t))return t;throw Error(`Invalid agent deletion database path journal.`)}function readRetainedAgentDeletionsFromDatabase(e,t,n=`maintenance`,r){let a=[],o=!1,s;try{if(o=!m(e,`agent_deletion_journal`),!o){let t=f(),i=t.and([t(`cleanup_completed`,`=`,1),t(`delete_files`,`=`,0)]),o=n===`runtime`?i:t.or([i,t(`cleanup_completed`,`=`,0)]);a=p(e,u(e).selectFrom(`agent_deletion_journal`).select([`agent_id`,`cleanup_completed`]).select(h(e,`agent_deletion_journal`,`phase`)?`phase`:t.lit(null).as(`phase`)).select([t.case().when(o).then(t.ref(`agent_dir`)).else(null).end().as(`agent_dir`),t.case().when(o).then(t.ref(`database_paths_json`)).else(`[]`).end().as(`database_paths_json`)]).$if(!r,e=>e.where(o)).orderBy(`agent_id`,`asc`)).rows.flatMap(t=>{if(r?.push({agentId:t.agent_id,status:t.cleanup_completed===1?`complete`:`pending`}),t.agent_dir===null)return[];let i=[];try{i=parseAgentDeletionDatabasePaths(t.database_paths_json)}catch(e){n===`maintenance`&&(s??=l(e))}let a={agentId:t.agent_id,agentDir:t.agent_dir,databasePaths:[x.join(t.agent_dir,`openclaw-agent.sqlite`),...i],cleanupCompleted:t.cleanup_completed===1};return t.cleanup_completed===0&&t.phase==null&&hasTemplateDeletionOwnership({db:e},t.agent_id)&&(a.manualTemplateRemoval=!0),a})}}catch(e){if(i(e))throw e;s=l(e)}let c=[];try{m(e,`migration_sources`)&&(c=readAgentDeletionRecoveryHolds({db:e,path:t}))}catch(e){if(n===`maintenance`||i(e))throw e}return o&&c.length===0&&_(e)?{status:`empty`}:o||s!==void 0?{status:`unavailable`,cause:o?`missing`:`unreadable`,reason:o?`deletion journal missing`:`deletion journal unreadable: ${s}`,...a.length||c.length?{known:{entries:a,held:c}}:{}}:a.length||c.length?{status:`present`,entries:a,held:c}:{status:`empty`}}function readAgentDatabaseDeletionSnapshot(e,t=`maintenance`){return y(({db:e,path:n})=>g(e,()=>({retainedDeletions:readRetainedAgentDeletionsFromDatabase(e,n,t),registeredAgentDatabases:b(e,n,!1)}),{operationLabel:`agentDeletionJournal.snapshot`}),{env:e})}export{readAgentDeletionRecoveryHolds as a,prepareAgentDeletionJournalStatus as c,readRetainedAgentDeletionsFromDatabase as i,parseAgentDeletionDatabasePaths as n,hasTemplateDeletionOwnership as o,readAgentDatabaseDeletionSnapshot as r,getPreparedAgentDeletionDiscovery as s,w as t};