openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
66 lines (65 loc) • 3.81 kB
JavaScript
import { a as executeSqliteQueryTakeFirstSync, o as getNodeSqliteKysely } from "./kysely-sync-DUWzQvCU.mjs";
import { c as registerNodeSqliteDisposeCallback } from "./kysely-sync-cache-state-DbAMR_DD.mjs";
import { t as classifySqliteTableReadError } from "./openclaw-state-db-schema-helpers-DKz5xGgh.mjs";
import "./openclaw-state-db-contract-BYv0nUQr.mjs";
import { r as runSqliteDeferredTransactionSync } from "./sqlite-transaction-DBRLvCDM.mjs";
import { n as assertSupportedStateSchemaVersion, r as readStateSchemaContentVersion } from "./openclaw-state-db-schema-version-DZI_59qS.mjs";
import { n as assertNoLegacyStateRuntimeRepair, t as assertCurrentStateRuntimeSchema } from "./openclaw-state-db-fast-path-Kg79WElU.mjs";
import { s as createSqliteTableContractReader, u as readSqliteSchemaCookie } from "./sqlite-schema-contract-BXfI47YR.mjs";
import { t as assertOpenClawStateRuntimeIntegrity } from "./openclaw-state-db-integrity-admission-D2ZPZec-.mjs";
//#region src/state/openclaw-state-db-existing-schema.ts
const validatedSchemas = /* @__PURE__ */ new WeakMap();
/** Recheck mutable metadata within the caller's admission transaction. */
function assertExistingOpenClawStateRuntimeMetadata(database, pathname) {
const version = assertSupportedStateSchemaVersion(database, pathname);
if (readStateSchemaContentVersion(database) !== 20) throw new Error(`Existing shared-state database ${pathname} requires schema migration by its owning installation; run openclaw doctor --fix there before using it.`);
let metadata;
try {
metadata = executeSqliteQueryTakeFirstSync(database, getNodeSqliteKysely(database).selectFrom("schema_meta").select(["role", "schema_version"]).where("meta_key", "=", "primary").limit(1));
} catch (error) {
throw classifySqliteTableReadError(database, "schema_meta", [
"meta_key",
"role",
"schema_version"
], error);
}
if (metadata?.role !== "global" || metadata.schema_version !== version) throw new Error(`Existing shared-state database ${pathname} has inconsistent ownership or schema metadata.`);
return version;
}
/** Prove the existing runtime contract without certifying this release's repairs. */
function assertExistingOpenClawStateRuntimeSchema(database, pathname, integrity, integrityPolicy) {
let publishIntegrity;
const schemaCookie = runSqliteDeferredTransactionSync(database, () => {
const userVersion = assertExistingOpenClawStateRuntimeMetadata(database, pathname);
const currentCookie = readSqliteSchemaCookie(database);
if (typeof currentCookie !== "number") throw new Error(`Existing shared-state database ${pathname} schema version is unavailable.`);
const cached = validatedSchemas.get(database);
if (cached?.cookie !== currentCookie) {
cached?.unregister();
validatedSchemas.delete(database);
}
if (cached?.cookie !== currentCookie || integrityPolicy === "require-proof") publishIntegrity = assertOpenClawStateRuntimeIntegrity(database, pathname, {
schemaVersion: currentCookie,
userVersion
}, integrity, integrityPolicy);
if (cached?.cookie !== currentCookie) {
const readTable = createSqliteTableContractReader(database);
assertCurrentStateRuntimeSchema(database, pathname, readTable);
assertNoLegacyStateRuntimeRepair(database, pathname);
}
return currentCookie;
}, { operationLabel: "state.admission.existing-schema" });
publishIntegrity?.();
if (!database.isTransaction && validatedSchemas.get(database)?.cookie !== schemaCookie) {
const unregister = registerNodeSqliteDisposeCallback(database, () => {
validatedSchemas.delete(database);
unregister();
});
validatedSchemas.set(database, {
cookie: schemaCookie,
unregister
});
}
}
//#endregion
export { assertExistingOpenClawStateRuntimeSchema as n, assertExistingOpenClawStateRuntimeMetadata as t };