UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

166 lines (165 loc) • 9.39 kB
import { i as executeSqliteQuerySync, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.mjs"; import { a as tableExists } from "./openclaw-state-db-schema-helpers-nImZNMq_.mjs"; import { a as deferSqliteWorkerCommitReceipt } from "./sqlite-worker-operation-settlement-OZQM-wtd.mjs"; import { f as requestSqliteWorkerOperationAdmission } from "./sqlite-worker-operation-admission-9E_X-oRy.mjs"; import { d as readAgentDeletionJournalAuthorityInDatabase, f as retireAgentDeletionJournalInDatabase } from "./agent-deletion-journal.read-DjOO2RZM.mjs"; import { t as agentProvenancePublication } from "./agent-provenance-publication-C-OU6fX0.mjs"; import { a as completeAgentDeletionJournalInDatabase, i as beginAgentDeletionJournalInDatabase, m as updateAgentDeletionJournalPathsInDatabase, o as deleteAgentDeletionJournalInDatabase, p as readAgentDeletionJournalInDatabase, s as handoffAgentDeletionJournalInDatabase, y as agentDeletionJournalPublication } from "./agent-deletion-journal-DgNbVVSp.mjs"; import { i as assertNoOpenClawAgentDatabaseLeases } from "./openclaw-agent-db-lease-BZhGt_sk.mjs"; import { r as unregisterOpenClawAgentDatabases } from "./openclaw-agent-db-registry-DobcJNwx.mjs"; import { n as assertOpenClawStateLeaseWorkerOwnedInTransaction } from "./openclaw-state-lease-worker-DlXbLNzC.mjs"; import { t as readTemplateInstallRecordFromDatabase } from "./provenance-read.kernel-CB9d0U8A.mjs"; import { t as ensureAgentProvenanceSchema } from "./agent-provenance.schema-Cl2g9xZI.mjs"; import { isDeepStrictEqual } from "node:util"; //#region src/state/agent-deletion.worker.ts /** The caller holds the real lease grant and this transaction's shared-state snapshot. */ function assertAgentDeletionWorkerPredicate(e, t, n) { const r = n === void 0 ? readAgentDeletionJournalAuthorityInDatabase(e.db, t.agentId) : n; if (!r || r.agentId !== t.agentId || r.operationId !== t.operationId || r.cleanupCompleted) throw new Error(`Agent ${t.agentId} deletion no longer owns database cleanup.`); assertTemplateInstall(e, t); } function assertTemplateInstall(e, t) { if (t.expectedTemplateInstall !== void 0 && !isDeepStrictEqual(readTemplateInstallRecordFromDatabase(e.db, t.agentId) ?? null, t.expectedTemplateInstall)) throw Object.assign(/* @__PURE__ */ new Error(`Template removal no longer owns agent ${t.agentId}.`), { code: "TEMPLATE_INSTALL_CHANGED" }); } function assertLease(e, t, n, r = "transaction") { if (t.scope !== "core:agent-deletion" || t.key !== n) throw new Error("Agent deletion requires its original target lease"); assertOpenClawStateLeaseWorkerOwnedInTransaction(e.db, t, "write", r); } function guarded(e, t, n, r) { return t.write((t) => { assertLease(t, e.lease, e.predicate.agentId); assertAgentDeletionWorkerPredicate(t, e.predicate); const { result: i, receipt: a } = agentDeletionJournalPublication.capture(t.db, () => agentProvenancePublication.capture(t.db, () => n(t))); const { result: o, receipt: s } = i; assertLease(t, e.lease, e.predicate.agentId, "commit"); if (r?.journalChanged) deferSqliteWorkerCommitReceipt(t.db, { kind: "agent-deletion-mutated", agentId: e.predicate.agentId, operationId: e.predicate.operationId, unregisterDatabases: r.unregisterDatabases === true, journalAuthority: agentDeletionJournalPublication.bound(a), provenanceAuthority: agentProvenancePublication.bound(s) }); return o; }, { operationLabel: "agent.deletion" }); } const agentDeletionOperations = { "agentDeletion.claimCompleted": (e, t) => t.write((t) => { requestSqliteWorkerOperationAdmission({ stage: "transaction", facts: void 0 }); const { result: n, receipt: r } = agentDeletionJournalPublication.capture(t.db, () => deleteAgentDeletionJournalInDatabase(t, e.agentId, e.operationId, true)); requestSqliteWorkerOperationAdmission({ stage: "commit", facts: void 0 }); deferSqliteWorkerCommitReceipt(t.db, { kind: "agent-deletion-claimed", agentId: e.agentId, operationId: e.operationId, claimed: n, journalAuthority: agentDeletionJournalPublication.bound(r) }); return n; }, { operationLabel: "agent.deletion.claim" }), "agentDeletion.read": (e, { open: t }) => readAgentDeletionJournalInDatabase(t(), e.agentId), "agentDeletion.begin": (e, t) => t.write((n) => { assertLease(n, e.lease, e.entry.agentId); assertTemplateInstall(n, { agentId: e.entry.agentId, expectedTemplateInstall: e.expectedTemplateInstall }); ensureAgentProvenanceSchema({ database: n, ...t.stateOptions() }); const { result: r, receipt: i } = agentDeletionJournalPublication.capture(n.db, () => beginAgentDeletionJournalInDatabase(n, e.entry, e.preserveDeleteFiles, e.recoveryOperationId)); assertLease(n, e.lease, e.entry.agentId, "commit"); deferSqliteWorkerCommitReceipt(n.db, { kind: "agent-deletion-began", agentId: e.entry.agentId, operationId: e.entry.operationId, journalAuthority: agentDeletionJournalPublication.bound(i) }); return r; }, { operationLabel: "agent.deletion.begin" }), "agentDeletion.assertCurrent": (e, t) => guarded(e.guard, t, () => void 0), "agentDeletion.retire": (e, t) => guarded(e.guard, t, (t) => { const { agentId: n, operationId: r } = e.guard.predicate; if (!retireAgentDeletionJournalInDatabase(t, n, r)) throw new Error(`Failed to retire deletion journal for agent ${n}.`); }, { journalChanged: true }), "agentDeletion.assertNoDatabaseLeasesUnowned": (e, t) => assertNoOpenClawAgentDatabaseLeases(e.agentId, { database: t.open(), ...t.stateOptions() }), "agentDeletion.assertNoDatabaseLeases": (e, t) => guarded(e.guard, t, (n) => assertNoOpenClawAgentDatabaseLeases(e.guard.predicate.agentId, { database: n, ...t.stateOptions() })), "agentDeletion.fencePaths": (e, t) => guarded(e.guard, t, (t) => { const { agentId: n, operationId: r } = e.guard.predicate; if (!updateAgentDeletionJournalPathsInDatabase(t, n, r, ...e.paths.kind === "database" ? ["database_paths_json", e.paths.paths] : ["cleanup_paths_json", e.paths.paths])) throw new Error(`Failed to fence cleanup paths for agent ${n}.`); }, { journalChanged: true }), "agentDeletion.finish": (e, t) => guarded(e.guard, t, (n) => { const { agentId: r, operationId: i } = e.guard.predicate; if (e.unregisterDatabases) unregisterOpenClawAgentDatabases({ agentId: r, database: n, env: t.stateOptions().env }); if (!completeAgentDeletionJournalInDatabase(n, r, i)) throw new Error(`Failed to complete deletion journal for agent ${r}.`); }, { journalChanged: true, unregisterDatabases: e.unregisterDatabases }), "agentDeletion.rollback": (e, t) => guarded(e.guard, t, (t) => { const { agentId: n, operationId: r } = e.guard.predicate; if (!deleteAgentDeletionJournalInDatabase(t, n, r, false)) throw new Error(`Failed to roll back deletion journal for agent ${n}.`); }, { journalChanged: true }), "agentDeletion.releaseTemplateRows": (e, t) => guarded(e.guard, t, (n) => { const { agentId: r, operationId: i } = e.guard.predicate; if (e.complete) unregisterOpenClawAgentDatabases({ agentId: r, database: n, env: t.stateOptions().env }); const a = getNodeSqliteKysely(n.db); const o = [...new Set(e.files.filter((e) => e.action !== "error").map((e) => e.path))]; if (o.length && tableExists(n.db, "template_workspace_files")) executeSqliteQuerySync(n.db, a.deleteFrom("template_workspace_files").where("agent_id", "=", r).where("target_path", "in", o)); if (!e.complete) return false; if (tableExists(n.db, "template_package_refs")) executeSqliteQuerySync(n.db, a.deleteFrom("template_package_refs").where("agent_id", "=", r)); if (tableExists(n.db, "template_installs")) executeSqliteQuerySync(n.db, a.deleteFrom("template_installs").where("agent_id", "=", r)); if (!completeAgentDeletionJournalInDatabase(n, r, i)) throw new Error(`Failed to complete deletion journal for agent ${r}.`); return true; }, { journalChanged: true, unregisterDatabases: e.complete }), "agentDeletion.handoffTemplateRetry": (e, t) => t.write((t) => { const { agentId: n, operationId: r } = e.guard.predicate; assertLease(t, e.guard.lease, n); const i = readAgentDeletionJournalInDatabase(t, n); if (!i || i.operationId !== r || i.cleanupCompleted || e.guard.predicate.expectedTemplateInstall !== void 0 && !isDeepStrictEqual(readTemplateInstallRecordFromDatabase(t.db, n) ?? null, e.guard.predicate.expectedTemplateInstall)) { assertLease(t, e.guard.lease, n, "commit"); return false; } const a = executeSqliteQuerySync(t.db, getNodeSqliteKysely(t.db).updateTable("template_installs").set({ status: "partial", updated_at_ms: e.nowMs }).where("agent_id", "=", n)); const { result: o, receipt: s } = agentDeletionJournalPublication.capture(t.db, () => a.numAffectedRows === 1n && handoffAgentDeletionJournalInDatabase(t, n, r, e.retryOperationId)); if (!o) throw new Error(`Failed to hand off deletion journal for agent ${n}.`); assertLease(t, e.guard.lease, n, "commit"); deferSqliteWorkerCommitReceipt(t.db, { kind: "agent-deletion-mutated", agentId: n, operationId: r, unregisterDatabases: false, journalAuthority: agentDeletionJournalPublication.bound(s) }); return true; }, { operationLabel: "agent.deletion.handoff" }) }; //#endregion export { assertAgentDeletionWorkerPredicate as n, agentDeletionOperations as t };