UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

163 lines (162 loc) • 7.3 kB
import { m as toStringifiedError } from "./error-coercion-hDrHOSGy.mjs"; import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import { t as isValidAgentId } from "./agent-id-CQjtp2ft.mjs"; import "./session-key-CqAyZeVk.mjs"; import { t as OPENCLAW_AGENT_SCHEMA_VERSION } from "./openclaw-schema-versions-E_10NoI6.mjs"; import "./kysely-sync-YJo2lmGV.mjs"; import { n as clearNodeSqliteKyselyCacheForDatabase } from "./kysely-sync-cache-state-Bb1gOqQO.mjs"; import { o as readSqliteUserVersion, s as readSqliteWriterAppVersion } from "./sqlite-user-version-C02a4KwH.mjs"; import { l as OPENCLAW_SQLITE_BUSY_TIMEOUT_MS } from "./openclaw-state-db-contract-DfPjvL0M.mjs"; import { a as setSqliteBusyTimeout } from "./sqlite-busy-timeout-BEDmnGR_.mjs"; import { i as resolveImmutableSqliteFileUri, n as openNodeSqliteDatabase } from "./node-sqlite-CU4puofF.mjs"; import { f as readSqliteSchemaHeader } from "./sqlite-readonly-location-BMysMNE5.mjs"; import { r as readExistingAgentSchemaMeta } from "./openclaw-agent-db-metadata-C3YvwHLh.mjs"; import { r as configureSqliteReadOnlyPragmas } from "./sqlite-wal-DGPrWPl6.mjs"; import { r as assertSqliteIntegrity } from "./sqlite-integrity-CCmEgO_m.mjs"; import "./openclaw-agent-db-contract-aCH9b8Uc.mjs"; import { h as resolveSchemaMigrationIntegrityCheck } from "./openclaw-state-db-ARhwdPJg.mjs"; import { a as readMainTableList } from "./sqlite-strict-BeSerG73.mjs"; import { t as assertCanonicalAgentPersistenceVersion } from "./openclaw-agent-db-schema-read-U5-_81Kg.mjs"; import { _ as getOpenClawAgentMigrationSchema } from "./openclaw-agent-progress-card-schema-CDOtPrpA.mjs"; import { a as assertOpenClawAgentSchemaContains, i as assertOpenClawAgentCurrentRuntimeSchema, l as hasPendingCurrentVersionAgentDatabaseMigration } from "./openclaw-agent-outbox-sequence-migration-ATXcQD7n.mjs"; import { t as assertOpenClawAgentDatabaseForMaintenance } from "./openclaw-agent-db-maintenance-BjsfVelp.mjs"; import { existsSync, realpathSync, statSync } from "node:fs"; import path from "node:path"; //#region src/state/openclaw-agent-schema-inspection.ts /** Only schema/default index metadata may survive a turn-less first run. */ function isEmptyAgentStore(e, t, n, r) { if (!t.agentId || r?.role !== "agent" || r.agentId !== t.agentId || r.schemaVersion == null || r.schemaVersion !== n || r.schemaVersion < 24 || r.schemaVersion > 38) return false; assertSqliteIntegrity(e, t.pathname); assertOpenClawAgentSchemaContains(e, t.pathname, getOpenClawAgentMigrationSchema(r.schemaVersion)); for (const t of readMainTableList(e, { includeInternal: true })) { if (t.type === "shadow" || t.name === "sqlite_schema") continue; const n = t.name; if (n.startsWith("sqlite_stat")) continue; const r = n === "schema_meta" ? "meta_key = 'primary'" : n === "session_key_contract" ? "id = 1 AND updated_at = 0" : n === "memory_index_meta" ? "key = 'memory_index_meta_v1' OR (key = 'memory_vector_rebuild_v1' AND value = 'clean')" : n === "memory_index_state" ? "id = 1 AND revision = 0" : n === "sqlite_sequence" ? "seq = 0" : "0"; if (e.prepare(`SELECT 1 FROM "${n.replaceAll("\"", "\"\"")}" WHERE (${r}) IS NOT TRUE LIMIT 1`).get()) return false; } return true; } /** All facts belong to the caller's single read transaction or private snapshot. */ function inspectAgentDatabaseSchema(e, t) { if (!t.verifyCurrentSchemaShape && !t.requireStartupMigrationReadiness) try { const { userVersion: n, ...r } = readSqliteSchemaHeader(e, t.inspectOwnership ? t.supportedVersion : void 0); return { version: n, ...r, ...t.inspectEmptyStore ? { emptyStore: n <= t.supportedVersion && isEmptyAgentStore(e, t, n, r.agentSchemaMeta) } : {} }; } finally { clearNodeSqliteKyselyCacheForDatabase(e); } const n = readSqliteUserVersion(e); const r = { version: n }; let i = false; let a = true; try { if (n > t.supportedVersion) { const t = readSqliteWriterAppVersion(e); return { version: n, ...t ? { writerAppVersion: t } : {} }; } if (t.inspectOwnership) r.agentSchemaMeta = readExistingAgentSchemaMeta(e); if (t.requireStartupMigrationReadiness) { a = n !== t.supportedVersion || hasPendingCurrentVersionAgentDatabaseMigration(e); if (t.startupIntegrityVerification !== void 0 && !a) r.integrityGateOutcome = "cached"; else if (t.deferRuntimeIntegrity && !a) r.preparationPending = true; else { assertSqliteIntegrity(e, t.pathname, n < t.supportedVersion ? resolveSchemaMigrationIntegrityCheck({ kind: "agent", foundVersion: n, supportedVersion: t.supportedVersion }) : void 0); r.integrityGateOutcome = "healthy"; } if (a && n === t.supportedVersion) r.preparationPending = true; assertCanonicalAgentPersistenceVersion(e, t.pathname, n); } const o = t.agentId ?? (t.requireStartupMigrationReadiness ? readExistingAgentSchemaMeta(e)?.agentId : void 0); if (t.verifyCurrentSchemaShape && o != null && (!t.requireStartupMigrationReadiness || n > 0)) { i = true; if (assertOpenClawAgentDatabaseForMaintenance(e, { agentId: o, pathname: t.pathname, allowStartupIndexRepair: t.requireStartupMigrationReadiness })) r.preparationPending = true; else if (!a && n === t.supportedVersion) r.readReady = true; } return r; } catch (e) { if (t.requireStartupMigrationReadiness) return { ...r, failure: toStringifiedError(e), ...i ? { reason: formatErrorMessage(e) } : {} }; return { ...r, reason: formatErrorMessage(e) }; } finally { clearNodeSqliteKyselyCacheForDatabase(e); } } /** Validate one consolidated agent copy using this release's exact maintenance reader. * This never discovers, registers, migrates, or opens an ordinary runtime store. */ async function preflightOpenClawAgentDatabasePath(e, t) { const n = path.resolve(e); const r = { schema: "openclaw.agent-schema-preflight.v1", databasePath: n, agentId: t, targetVersion: 38, requiresWrite: false, issues: [] }; let i; let a = null; let o = "indeterminate"; try { if (!isValidAgentId(t) || t !== t.trim().toLowerCase()) throw new Error("Agent preflight requires an explicit canonical agent ID."); const e = realpathSync.native(n); if (e !== n || !statSync(e).isFile()) throw new Error("Agent preflight requires a canonical regular copied database path."); if ([ "-wal", "-shm", "-journal" ].some((t) => existsSync(e + t))) throw new Error("Agent preflight requires a consolidated snapshot with no SQLite sidecars."); i = openNodeSqliteDatabase(resolveImmutableSqliteFileUri(e), { readOnly: true }); setSqliteBusyTimeout(i, OPENCLAW_SQLITE_BUSY_TIMEOUT_MS); configureSqliteReadOnlyPragmas(i); assertSqliteIntegrity(i, n); a = readSqliteUserVersion(i); o = "incompatible"; assertOpenClawAgentDatabaseForMaintenance(i, { agentId: t, pathname: n }); assertOpenClawAgentCurrentRuntimeSchema(i, { agentId: t, pathname: n }); return { ...r, foundVersion: a, status: "exact" }; } catch (e) { return { ...r, foundVersion: a, status: o, reason: formatErrorMessage(e) }; } finally { if (i) clearNodeSqliteKyselyCacheForDatabase(i); i?.close(); } } //#endregion export { inspectAgentDatabaseSchema, preflightOpenClawAgentDatabasePath };