UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

269 lines (268 loc) • 12 kB
import { r as resolvePathViaExistingAncestorSync } from "./boundary-path-C2gXEiXe.mjs"; import { t as _usingCtx } from "./usingCtx-rhpPzaNq.mjs"; import { t as CLIENT_CREDENTIAL_CUTOVER_SCHEMA_VERSION } from "./openclaw-state-db-contract-DfPjvL0M.mjs"; import { n as openNodeSqliteDatabase } from "./node-sqlite-CU4puofF.mjs"; import { a as runSqliteImmediateTransactionSync } from "./sqlite-transaction-BHHu6P5-.mjs"; import { s as resolveOpenClawStateSqlitePath } from "./openclaw-state-db.paths-R3ArRCcz.mjs"; import { Jn as string, qn as strictObject } from "./schemas-Cy6abvsu.mjs"; import { n as readEngineAnchor } from "./open-jth9KEHn.mjs"; import { o as readStateSchemaContentVersion } from "./openclaw-state-db-schema-version-uNbDFl6T.mjs"; import { t as resolveClientCredentialStorePath } from "./client-credential-paths-CIYPaDgB.mjs"; import { i as readClientCredentialFileIdentity, r as parseClientCredentialReceipt } from "./client-credential-receipt-yx56NK9z.mjs"; import { c as writeClientCredentialReceipt, r as openClientCredentialCustody, s as readClientCredentialReceipt, u as readIndependentClientCredentialSource } from "./client-credential-store-rd7DZXgD.mjs"; import { t as buildBackupArchivePath } from "./backup-archive-path-policy-Cr8KWL_A.mjs"; import fs from "node:fs"; import path from "node:path"; import { isDeepStrictEqual } from "node:util"; //#region src/infra/client-credential-restore.ts const portBindingSchema = strictObject({ localFile: strictObject({ path: string(), dev: string().regex(/^\d+$/), ino: string().regex(/^\d+$/) }), receipt: string() }); function portSharedIdentity(e, t) { try { var n = _usingCtx(); const r = n.u(openNodeSqliteDatabase(e, { readOnly: true, readBigInts: true })); const i = readEngineAnchor(r)?.[0]; if (i) { if (typeof i.store_id !== "string") throw new Error("Database port PostgreSQL identity is missing."); return { engine: "postgres", identity: i.store_id }; } const a = r.prepare("SELECT created_at FROM schema_meta WHERE meta_key='primary'").get()?.created_at; if (typeof a !== "bigint") throw new Error("Database port shared identity is missing."); return { engine: "sqlite", identity: `${t}:${a}` }; } catch (e) { n.e = e; } finally { n.d(); } } var PortClientCredentialMismatchError = class extends Error {}; /** Capture only receipt metadata; credential rows remain in their existing local owner. */ function capturePortClientCredentialBinding(e) { try { var t = _usingCtx(); const n = t.u(openClientCredentialCustody(e.env, { migration: true })); if (!n) return null; const r = readClientCredentialReceipt(n.database); const i = portSharedIdentity(e.sourcePath, e.sourceFile.path); if (r?.phase !== "complete" || !isDeepStrictEqual(r.receipt.sharedFile, e.sourceFile) || r.receipt.source && (r.receipt.source.kind !== "canonical" || r.receipt.source.engine !== i.engine || r.receipt.source.identity !== i.identity)) throw new PortClientCredentialMismatchError("Database port credentials do not match the captured shared database."); const a = readClientCredentialFileIdentity(n.path); if (!a) throw new Error("Database port local credentials disappeared."); return { localFile: a, receipt: JSON.stringify(r.receipt) }; } catch (e) { t.e = e; } finally { t.d(); } } /** Called after durable port completion, when recovery can resume but cannot roll back. */ function rebindPortedClientCredentials(e) { try { var t = _usingCtx(); const n = portBindingSchema.nullable().parse(e.binding); const r = t.u(openClientCredentialCustody(e.env, { migration: true })); if (!n && !r) return; if (!n || !r || !isDeepStrictEqual(readClientCredentialFileIdentity(r.path), n.localFile)) throw new Error("Database port local credentials no longer match their capture."); const i = { phase: "complete", receipt: parseClientCredentialReceipt(n.receipt) }; const a = readClientCredentialFileIdentity(e.sharedPath); const o = portSharedIdentity(e.sharedPath, e.sharedPath); const s = e.engine === "postgres" ? e.storeId : portSharedIdentity(e.sqliteSnapshotPath, e.sharedPath).identity; if (!a || o.engine !== e.engine || o.identity !== s) throw new Error("Database port target does not match its verified shared identity."); const c = { ...i, receipt: { ...i.receipt, sharedFile: a, source: i.receipt.source ? { ...i.receipt.source, ...o } : null } }; r.transaction(() => { const t = readClientCredentialReceipt(r.database); if (isDeepStrictEqual(t, c)) return; if (!isDeepStrictEqual(t, i)) throw new Error("Database port credential receipt no longer matches its capture."); e.assertCurrent(); writeClientCredentialReceipt(r.database, c.receipt, "complete"); }); } catch (e) { t.e = e; } finally { t.d(); } } /** Doctor supplies the original identity only after a verified byte-identical rewrite. */ function rebindRewrittenClientCredentials(e) { try { var t = _usingCtx(); const n = t.u(openClientCredentialCustody(e.env, { migration: true })); if (!n) return; n.transaction(() => { const t = readClientCredentialReceipt(n.database); const r = t?.receipt.sharedFile; if (!t || !r) return; const i = e.previousSharedFile; if (r.path !== i.path || r.dev !== i.dev || r.ino !== i.ino) throw new Error("Rewritten credential source is not the recorded generation."); const a = readClientCredentialFileIdentity(i.path); if (!a) throw new Error("Rewritten credential source is missing."); t.receipt.sharedFile = a; if (t.receipt.source?.kind === "independent") t.receipt.source.identity = `${a.dev}:${a.ino}`; e.assertCurrent(); writeClientCredentialReceipt(n.database, t.receipt, t.phase); }); } catch (e) { t.e = e; } finally { t.d(); } } function prepareMatchedReceipt(e) { try { var t = _usingCtx(); const i = t.u(openNodeSqliteDatabase(e.localPath, { readOnly: true })); const a = readClientCredentialReceipt(i, e.originalStateRoot); if (!a?.receipt.sharedFile) return null; const o = a.receipt.sharedFile; const s = a.receipt.source; if (a.phase !== "complete" && !e.preserveCopiedPhase || o.path !== e.originalSharedPath) throw new Error("Restored client credentials do not match the captured shared database."); if (s?.kind === "independent") { if (readIndependentClientCredentialSource(e.sharedPath).sha256 !== s.sha256) throw new Error("Restored native credentials have a different source snapshot."); } else try { var n = _usingCtx(); const t = n.u(openNodeSqliteDatabase(e.sharedPath, { readOnly: true })); const i = readEngineAnchor(t)?.[0]; if (i) try { var r = _usingCtx(); if (!e.postgresExportDirectory) throw new Error("Restored client credentials are missing their PostgreSQL capture."); const t = r.u(openNodeSqliteDatabase(path.join(e.postgresExportDirectory, "anchor.sqlite"), { readOnly: true })); const n = readEngineAnchor(t)?.[0]; if (e.postgresStoreId && e.postgresStoreId !== i.store_id || n?.store_id !== i.store_id || n?.schema !== i.schema || s && (s.engine !== "postgres" || s.identity !== i.store_id)) throw new Error("Restored client credentials have a different PostgreSQL identity."); const a = r.u(openNodeSqliteDatabase(path.join(e.postgresExportDirectory, "database.sqlite"), { readOnly: true })); if (readStateSchemaContentVersion(a) < 29) return null; if (e.projectPostgresToSqlite && s) { const t = a.prepare("SELECT created_at FROM schema_meta WHERE meta_key='primary'").get()?.created_at; s.engine = "sqlite"; s.identity = `${e.restoredSharedPath}:${String(t)}`; } } catch (e) { r.e = e; } finally { r.d(); } else { if (e.postgresStoreId) throw new Error("Restored client credentials are missing their PostgreSQL anchor."); if (readStateSchemaContentVersion(t) < 29) return null; const n = t.prepare("SELECT created_at FROM schema_meta WHERE meta_key='primary'").get()?.created_at; if (s && (s.engine !== "sqlite" || s.identity !== `${o.path}:${String(n)}`)) throw new Error("Restored client credentials have a different shared database identity."); if (s) s.identity = `${e.restoredSharedPath}:${String(n)}`; } } catch (e) { n.e = e; } finally { n.d(); } if (e.originalSharedFile && (o.dev !== e.originalSharedFile.dev || o.ino !== e.originalSharedFile.ino)) throw new Error("Restored client credentials do not match the captured shared database."); return a; } catch (e) { t.e = e; } finally { t.d(); } } function publishRestoredReceipt(e, t, n) { try { var r = _usingCtx(); const { receipt: i } = e; const a = readClientCredentialFileIdentity(t); if (!a) throw new Error("Restored shared database is missing."); const o = r.u(openClientCredentialCustody(n, { createIfMissing: false, migration: true })); if (!o) throw new Error("Restored local credentials are missing."); if (i.source?.kind === "independent") i.source.identity = `${a.dev}:${a.ino}`; i.sharedFile = a; runSqliteImmediateTransactionSync(o.database, () => writeClientCredentialReceipt(o.database, i, e.phase)); } catch (e) { r.e = e; } finally { r.d(); } } /** The snapshot owner supplies only the admitted shared/local pair and its source witness. */ function rebindRehearsalClientCredentials(e) { const t = prepareMatchedReceipt({ localPath: e.localPath, sharedPath: e.postgresExportDirectory ? path.join(e.postgresExportDirectory, "anchor.sqlite") : e.sharedPath, originalSharedPath: e.sourceSharedFile.path, originalStateRoot: e.sourceStateRoot, originalSharedFile: e.sourceSharedFile, restoredSharedPath: e.sharedPath, postgresStoreId: e.postgresStoreId, postgresExportDirectory: e.postgresExportDirectory, projectPostgresToSqlite: Boolean(e.postgresExportDirectory), preserveCopiedPhase: true }); if (t) publishRestoredReceipt(t, e.sharedPath, { OPENCLAW_STATE_DIR: path.dirname(path.dirname(e.localPath)) }); } /** Validate archive captures before their owner publishes the final shared/local pair. */ function prepareRestoredClientCredentials(e) { const { manifest: t, targetPath: n } = e; const r = t.assets.find((e) => e.kind === "state"); const i = t.sqliteSnapshots?.find((e) => e.role === "global"); if (!r || !i) return; const a = { OPENCLAW_STATE_DIR: path.join(n, r.archivePath) }; const o = resolveClientCredentialStorePath(a); if (!fs.existsSync(o)) return; const s = path.join(n, buildBackupArchivePath(t.archiveRoot, i.sourcePath)); const c = prepareMatchedReceipt({ localPath: o, sharedPath: s, originalSharedPath: t.paths?.sharedDatabaseSourcePath ?? i.sourcePath, originalStateRoot: t.paths?.stateDir ?? r.sourcePath, restoredSharedPath: s, postgresExportDirectory: `${s}.postgres` }); if (c) return () => publishRestoredReceipt(c, s, a); } /** Validate captured bytes before replacement; publish only under the restore owner's custody. */ function prepareUpdateClientCredentialRestore(e) { const t = resolvePathViaExistingAncestorSync(resolveOpenClawStateSqlitePath(e.env)); const n = resolvePathViaExistingAncestorSync(resolveClientCredentialStorePath(e.env)); const r = e.backup.databases.find((e) => e.path === t); const i = e.backup.databases.find((e) => e.path === n); if (!r || !i) return; const a = prepareMatchedReceipt({ localPath: i.snapshotPath, sharedPath: r.snapshotPath, originalSharedPath: t, originalStateRoot: path.dirname(path.dirname(n)), restoredSharedPath: t, postgresStoreId: r.storeId, postgresExportDirectory: r.exportDirectory }); if (a) return () => publishRestoredReceipt(a, t, e.env); } //#endregion export { rebindPortedClientCredentials as a, prepareUpdateClientCredentialRestore as i, capturePortClientCredentialBinding as n, rebindRehearsalClientCredentials as o, prepareRestoredClientCredentials as r, rebindRewrittenClientCredentials as s, PortClientCredentialMismatchError as t };