UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 5.97 kB
import{B as e,T as t}from"./openclaw-state-read-sqlite-readonly-worker-umlSCyCR.mjs";import{Y as n}from"./openclaw-state-read-sqlite-database-admission-Ck6VG7rE.mjs";import{x as r}from"./openclaw-state-read-read-open-flags-CtNL1rJs.mjs";import"./openclaw-state-read-openclaw-state-db.paths-CFemYLpA.mjs";import{a as i,o as a}from"./openclaw-state-read-record-coerce-D1s3ReAs.mjs";import"./openclaw-state-read-sqlite-worker-operation-settlement-ogK5IsJy.mjs";import{r as o}from"./openclaw-state-read-sqlite-files-B6lm_PVC.mjs";import{a as s,r as c}from"./openclaw-state-read-kysely-sync-DQyYiAw5.mjs";import"./openclaw-state-read-sqlite-schema-facts-DxLfPKu-.mjs";import"./openclaw-state-read-sqlite-worker-operation-admission-DTfv_SKu.mjs";import{n as l}from"./openclaw-state-read-agent-id-DmscQLD6.mjs";import"./openclaw-state-read-openclaw-state-db-readonly-CDG18cNK.mjs";import{n as u}from"./openclaw-state-read-state-domain-publication-CN6b3HGd.mjs";import"./openclaw-state-read-session-key-DaKMXMbu.mjs";import{u as d}from"./openclaw-state-read-agent-scope-config-DBTVDK8I.mjs";import"./openclaw-state-read-openclaw-state-worker-context-Cvog3s4V.mjs";import"./openclaw-state-read-session-row-changes-C0Qzq-vO.mjs";import"./openclaw-state-read-sqlite-worker-store-CmnQY2hF.mjs";import{t as f}from"./openclaw-state-read-openclaw-state-db-schema-additive-DRfDFLLT.mjs";import"./openclaw-state-read-agent-deletion-journal-status-hTodcgwC.mjs";import{a as p,n as m}from"./openclaw-state-read-agent-deletion-journal.read-DWSUE9n3.mjs";import"./openclaw-state-read-agent-provenance-publication-BpEgy8tp.mjs";import"./openclaw-state-read-state-migrations.receipts-C4jnz6ia.mjs";import"./openclaw-state-read-agent-deletion-journal-recovery.kernel-DxhVBGkd.mjs";import h from"node:path";import"node:crypto";u({domain:`agent-deletion-journal`,keyOf:e=>e.agentId,isValue:e=>i(e)&&typeof e.agentId==`string`&&typeof e.operationId==`string`&&typeof e.cleanupCompleted==`boolean`});var AgentDatabaseDeletionRefusedError=class extends Error{};function assertAgentDeletionIdentityClaimAllowed(e,t){if(t&&l(e)===l(t))throw new AgentDatabaseDeletionRefusedError(`OpenClaw agent database is unavailable while agent ${l(t)} is deleted.`)}function readAgentDeletionPathFenceRows(e,t=`runtime`,r){t===`maintenance`&&f(e);try{let n=s(e),i=!0,a=n.selectFrom(`agent_deletion_journal`).selectAll();r!==void 0&&(a=a.where(`agent_id`,`=`,l(r)));let o=c(e,a).rows.map(e=>{let n=[],r=[];try{n=m(e.database_paths_json),r=parseCleanupPaths(e.cleanup_paths_json)}catch(e){if(t===`maintenance`)throw e;i=!1}return Object.assign(e,{databasePaths:n,cleanupPaths:r})});return{known:i,rows:o}}catch(e){if(t===`maintenance`||n(e))throw e;return{known:!1,rows:[]}}}function prepareAgentDeletionPathFence(e,t={},n=e.fenceAgentId?`maintenance`:`runtime`){let r=t.env??process.env;return{env:r,purpose:n,claimAgentId:l(e.agentId),claimPath:h.resolve(e.path),...e.fenceAgentId?{fenceAgentId:l(e.fenceAgentId)}:{},targetPaths:o(e.path).map(e=>d(e,r))}}function assertAgentDeletionPathFence(n,i){let a=n.db,{rows:o,known:s}=readAgentDeletionPathFenceRows(a,i.purpose);if(!s&&o.length===0)return;let c=i.fenceAgentId?void 0:t({agentId:i.claimAgentId,path:i.claimPath,statePath:n.path});c?.assertCurrent();let l=c?.assertJournal(n.path,o.map(e=>({agentId:e.agent_id,operationId:e.operation_id,cleanupCompleted:e.cleanup_completed===1}))),u=i.fenceAgentId?void 0:e(i.claimAgentId,n.path);for(let e of o){if(i.fenceAgentId&&i.fenceAgentId!==e.agent_id||e.agent_id===l||e.cleanup_completed===1&&e.agent_id===u||e.phase===`draining`&&e.cleanup_completed!==1&&e.agent_id===i.claimAgentId&&!i.fenceAgentId||(assertAgentDeletionIdentityClaimAllowed(i.claimAgentId,e.agent_id),e.cleanup_completed===1))continue;let t=[...[e.agent_dir,e.workspace_dir,e.sessions_dir,...e.databasePaths].map(e=>({path:e,canonicalPath:d(e,i.env)})),...e.cleanupPaths.map(e=>({path:e.path,canonicalPath:d(e.canonicalPath,i.env)}))];for(let n of t){let t=i.targetPaths.find(e=>e===n.canonicalPath||r(n.canonicalPath,e));if(t)throw new AgentDatabaseDeletionRefusedError(`OpenClaw agent database ${t} is unavailable while agent ${e.agent_id} deletion owns ${n.path}.`)}}}function fromRow(e){if(e.phase!=null&&e.phase!==`draining`&&e.phase!==`retiring`)throw Error(`Invalid agent deletion journal phase.`);return{agentId:e.agent_id,operationId:e.operation_id,phase:e.phase===`draining`?`draining`:`retiring`,agentDir:e.agent_dir,workspaceDir:e.workspace_dir,sessionsDir:e.sessions_dir,databasePaths:e.databasePaths??m(e.database_paths_json),cleanupPaths:e.cleanupPaths??parseCleanupPaths(e.cleanup_paths_json),createdAt:e.created_at,cleanupCompleted:e.cleanup_completed===1,deleteFiles:e.delete_files===1}}function parseCleanupPaths(e){let t=JSON.parse(e);if(!Array.isArray(t)||!t.every(e=>i(e)&&typeof e.path==`string`&&typeof e.canonicalPath==`string`&&typeof e.parentPath==`string`&&(e.kind===`target`||e.kind===`symlink`)&&(e.dev===null||typeof e.dev==`number`&&Number.isInteger(e.dev)||typeof e.dev==`string`&&/^(0|-?[1-9][0-9]*)$/u.test(e.dev))&&(e.ino===null||typeof e.ino==`number`&&Number.isInteger(e.ino)||typeof e.ino==`string`&&/^(0|-?[1-9][0-9]*)$/u.test(e.ino))&&typeof e.coversDescendants==`boolean`&&typeof e.done==`boolean`&&(e.note===void 0||typeof e.note==`string`)&&a(e.sourcePaths)))throw Error(`Invalid agent deletion cleanup path journal.`);return t}function readAgentDeletionJournalInDatabase(e,t,n=`maintenance`){let r=readAgentDeletionPathFenceRows(e.db,n,t).rows[0];return r?fromRow(r):void 0}function listPendingAgentDeletionJournalsInDatabase(e){let t=[],n=[];for(let r of readAgentDeletionPathFenceRows(e.db).rows)r.cleanup_completed!==1&&(r.phase==null&&p(e,r.agent_id)?n.push(r.agent_id):t.push(fromRow(r)));return{entries:t,manualTemplateAgentIds:n}}export{readAgentDeletionJournalInDatabase as i,listPendingAgentDeletionJournalsInDatabase as n,prepareAgentDeletionPathFence as r,assertAgentDeletionPathFence as t};