UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

233 lines (232 loc) • 9.62 kB
import { n as readGlobalSingleton } from "./global-singleton-D7yf05nR.mjs"; import { t as assertEngineStoreExecutionAllowed } from "./admission-scope-DmxaPNhm.mjs"; import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs"; import { c as resolveOpenClawAgentSqlitePath, r as isIncognitoOpenClawAgentSqlitePath } from "./openclaw-agent-db.paths-DJxv80ws.mjs"; import { n as runtimeProcessEntrypoints } from "./runtime-process-entrypoints-qiyJvDDT.mjs"; import { a as resolveRuntimeWorkerUrl, n as resolveRuntimeWorkerArgv } from "./runtime-worker-url-DRDkoS4J.mjs"; import { mt as hasPendingAgentStartupColumns } from "./sqlite-database-admission-DQIqro3d.mjs"; import { h as sqlitePrimaryResultCode } from "./sqlite-error-diagnostics-6_hREUnq.mjs"; import { a as enableNodeSqliteKyselyStatementCache } from "./kysely-sync-cache-state-Bb1gOqQO.mjs"; import { h as runSqliteReadOperationSync, o as getAdmittedSqliteSchemaFacts, t as admitSqliteSchema } from "./sqlite-schema-facts-B1bVE8IB.mjs"; import { c as SqliteSchemaMismatchError } from "./openclaw-state-db-schema-helpers-nImZNMq_.mjs"; import { l as OPENCLAW_SQLITE_BUSY_TIMEOUT_MS } from "./openclaw-state-db-contract-DfPjvL0M.mjs"; import { n as openNodeSqliteDatabase, r as resolveExistingSqliteFileUri } from "./node-sqlite-CU4puofF.mjs"; import { n as isArtifactPreservingStateRead } from "./artifact-preserving-state-reads-BfhmBr-V.mjs"; import { r as readExistingAgentSchemaMeta } from "./openclaw-agent-db-metadata-C3YvwHLh.mjs"; import { o as runSqliteReadSnapshotSync } from "./sqlite-transaction-BHHu6P5-.mjs"; import { s as resolveOpenClawStateSqlitePath } from "./openclaw-state-db.paths-R3ArRCcz.mjs"; import { n as openSqliteReadOnlyDatabase } from "./sqlite-snapshot-source-Q9S7_uQN.mjs"; import { a as readSqliteInspectionBudget } from "./sqlite-readonly-worker-D3pRJU7r.mjs"; import { o as isDeletedAgentDatabasePath } from "./agent-database-readers-kAORIt-A.mjs"; import { S as resolveAgentCreationClaimAgentId, c as registerOpenClawAgentDatabaseSyncResource } from "./openclaw-agent-db-resources-Dp8HvLsg.mjs"; import { i as readOpenClawDatabaseQuarantineFailure } from "./openclaw-quarantine-store-Bi4DSTmN.mjs"; import { o as registerOpenClawAgentDatabaseIdentity } from "./openclaw-agent-db-identity-C8MGU8lp.mjs"; import { i as assertSupportedAgentSchemaVersion, r as assertExistingAgentSchemaOwner, t as assertCanonicalAgentPersistenceVersion } from "./openclaw-agent-db-schema-read-U5-_81Kg.mjs"; import { i as openAgentEngineStore, n as agentEngineInput } from "./openclaw-agent-db-engine-CFD02117.mjs"; import { c as prepareAgentDeletionJournalStatus } from "./agent-deletion-journal-status-CZ5XyH3P.mjs"; import { D as recordOpenClawAgentDatabaseReadOpenFailure, T as classifyOpenClawAgentDatabaseReadError, t as adoptOpenClawAgentDatabaseSchema, u as hasOpenClawAgentCanonicalValidation, w as assertCanonicalSessionValidationSchema, x as loadOpenClawAgentCanonicalValidationReceipt } from "./openclaw-agent-db-validation-cache-lnjWuypr.mjs"; import { t as assertSqliteIntegrityInWorker } from "./sqlite-integrity-worker-D_lHVvZY.mjs"; import fs from "node:fs"; import { isMainThread } from "node:worker_threads"; import { spawnSync } from "node:child_process"; //#region src/state/openclaw-agent-db-terminal.ts function terminalLatch() { return readGlobalSingleton(Symbol.for("openclaw.agentDatabaseLifecycle"))?.terminal; } /** Read the existing damage latch without acquiring the writable database lifecycle. */ function assertAgentDatabaseTerminalOpenAllowed(e) { const t = terminalLatch()?.get(e); if (t) throw t; } /** Only latched integrity failures need a fresh native check after external repair. */ function revalidateAgentDatabaseTerminalOpen(e) { const t = terminalLatch(); const n = t?.peek(e); if (t && n?.name === "SqliteIntegrityError") { const r = resolveRuntimeWorkerUrl(runtimeProcessEntrypoints.databaseVerify); const i = spawnSync(process.execPath, [ ...resolveRuntimeWorkerArgv(r), "--openclaw-database-verify-sync", e ], { timeout: readSqliteInspectionBudget("integrity check", e).timeoutMs, killSignal: "SIGKILL", stdio: "ignore" }); if (i.error || i.status !== 0) throw n; t.clear(e); } assertAgentDatabaseTerminalOpenAllowed(e); } async function revalidateAgentDatabaseTerminalOpenAsync(e, t, n = new AbortController().signal) { const r = terminalLatch(); const i = r?.peek(e); if (r && i?.name === "SqliteIntegrityError") { await assertSqliteIntegrityInWorker(e, OPENCLAW_SQLITE_BUSY_TIMEOUT_MS, n); t?.(); if (r.peek(e) === i) r.clear(e); } t?.(); assertAgentDatabaseTerminalOpenAllowed(e); } //#endregion //#region src/state/openclaw-agent-db-readonly-open.ts function readOpenClawAgentDatabase(e, t) { try { return { found: true, value: t(e) }; } catch (t) { throw sqlitePrimaryResultCode(t) === 1 ? classifyOpenClawAgentDatabaseReadError(e.db, t) : t; } } function hasAdmittedAgentReadOnlySchema(e) { const t = assertSupportedAgentSchemaVersion(e.db, e.path); const n = readExistingAgentSchemaMeta(e.db); assertCanonicalAgentPersistenceVersion(e.db, e.path, t); if (!n) return false; assertExistingAgentSchemaOwner(n, e.agentId, e.path); assertCanonicalSessionValidationSchema(e.db); return true; } /** Recheck admission and consume retained rows in the same fresh synchronous scope. */ function hasOpenClawAgentReadOnlySchema(e, t) { return runSqliteReadOperationSync(e.db, () => { if (!hasAdmittedAgentReadOnlySchema(e)) return false; t?.(); return true; }); } /** Admit ownership and consume its rows in the same freshly pinned snapshot. */ function readOpenClawAgentDatabaseSnapshot(e, t) { return { found: true, value: runSqliteReadSnapshotSync(e.db, () => readOpenClawAgentDatabase(e, t).value) }; } /** Fresh-only callers do not need the writable runtime's process-held connection cache. */ function withFreshOpenClawAgentDatabaseReadOnly(e, t, n = {}) { const r = openOpenClawAgentDatabaseReadOnly(t, n); if (!r.found) return r; try { return n.snapshot ? readOpenClawAgentDatabaseSnapshot(r.database, e) : readOpenClawAgentDatabase(r.database, e); } finally { r.database.close(); } } /** Open one existing agent database without creating, registering, migrating, or adopting it. */ function openOpenClawAgentDatabaseReadOnly(e, t = {}) { const n = agentEngineInput(); if (n) assertEngineStoreExecutionAllowed(); const r = normalizeAgentId(e.agentId); const i = resolveOpenClawAgentSqlitePath({ ...e, agentId: r }); if (isIncognitoOpenClawAgentSqlitePath(i, { agentId: r, env: e.env })) return { found: false, reason: "database-missing" }; if (isDeletedAgentDatabasePath(i)) { const t = resolveOpenClawStateSqlitePath(e.env ?? process.env); const n = resolveAgentCreationClaimAgentId(r, t); if (!n || !isDeletedAgentDatabasePath(i, n) || prepareAgentDeletionJournalStatus(t, r)() !== "complete") return { found: false, reason: "database-missing" }; } if (!fs.existsSync(i)) return { found: false, reason: "database-missing" }; revalidateAgentDatabaseTerminalOpen(i); const a = readOpenClawDatabaseQuarantineFailure("agent", i, { env: e.env }); if (a) { recordOpenClawAgentDatabaseReadOpenFailure(a); throw a; } let o; try { o = n ? openNodeSqliteDatabase(resolveExistingSqliteFileUri(i), { timeout: OPENCLAW_SQLITE_BUSY_TIMEOUT_MS }) : openSqliteReadOnlyDatabase(i, { timeout: OPENCLAW_SQLITE_BUSY_TIMEOUT_MS, ...t.allowExtension ? { allowExtension: true } : {} }); } catch (e) { recordOpenClawAgentDatabaseReadOpenFailure(e); throw e; } let s = false; let c = o; let l; const close = () => { if (s) return; if (c !== o && c.isOpen) c.close(); if (o.isOpen) o.close(); s = true; l?.(); l = void 0; }; try { let e; if (n) { c = openAgentEngineStore(o, r, i, true, (e, t) => { c = e; throw t; }, true); registerOpenClawAgentDatabaseIdentity(c); e = { agentId: r, db: c, path: i, close }; } else { enableNodeSqliteKyselyStatementCache(o); registerOpenClawAgentDatabaseIdentity(o); const t = isArtifactPreservingStateRead("agent", i) ? o.location() : void 0; e = { agentId: r, db: o, path: t ?? i, close }; if (!(adoptOpenClawAgentDatabaseSchema(e) || runSqliteReadOperationSync(o, () => { admitSqliteSchema(o); if (!hasAdmittedAgentReadOnlySchema(e)) return false; const t = getAdmittedSqliteSchemaFacts(e.db); if (t && hasPendingAgentStartupColumns(t.tableSql)) throw new SqliteSchemaMismatchError(`OpenClaw agent database ${e.path} requires writable startup admission before session reads; restart the Gateway or run openclaw doctor --fix.`); return true; }))) { close(); return { found: false, reason: "schema-missing" }; } if (!isMainThread) { loadOpenClawAgentCanonicalValidationReceipt(e); hasOpenClawAgentCanonicalValidation(e); } } if (t.lifecycle === "agent") l = registerOpenClawAgentDatabaseSyncResource({ agentId: r, path: i, revoke: close, close }); return { found: true, database: e }; } catch (e) { close(); recordOpenClawAgentDatabaseReadOpenFailure(e); throw e; } } //#endregion export { withFreshOpenClawAgentDatabaseReadOnly as a, revalidateAgentDatabaseTerminalOpenAsync as c, readOpenClawAgentDatabaseSnapshot as i, openOpenClawAgentDatabaseReadOnly as n, assertAgentDatabaseTerminalOpenAllowed as o, readOpenClawAgentDatabase as r, revalidateAgentDatabaseTerminalOpen as s, hasOpenClawAgentReadOnlySchema as t };