openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
121 lines (120 loc) • 6.91 kB
JavaScript
import { t as formatErrorMessage } from "./errors-BvOkmQM5.mjs";
import { d as readSqliteWriterAppVersion } from "./sqlite-readonly-location-DEj0l7LR.mjs";
import { t as SqliteSchemaMismatchError } from "./sqlite-schema-issues-BnRd2KGO.mjs";
import "./openclaw-state-db-contract-BYv0nUQr.mjs";
import { a as readSqliteUserVersion } from "./sqlite-user-version-DLMl1A7W.mjs";
import { r as normalizeOpenClawStateSchemaReadError } from "./openclaw-state-db-schema-migration-required-DBia8oMG.mjs";
import { r as readStateSchemaContentVersion } from "./openclaw-state-db-schema-version-DZI_59qS.mjs";
import { A as OPENCLAW_STATE_MAINTENANCE_SCHEMA_COMPATIBILITY, N as isOpenClawStateFirstUseSchemaIssue, P as isOpenClawStateStartupRepairableSchemaIssue, S as assertOpenClawStateDatabaseOwner, T as openClawStateMigrationAssertions, j as STATE_RUNTIME_SCHEMA_COMPATIBILITY, o as assertCanonicalStateSchemaShape, ot as readStateSchemaPublicationBlocker, x as assertOpenClawStateDatabaseForMaintenance } from "./openclaw-state-db-fast-path-Kg79WElU.mjs";
import { i as captureSqliteSchemaContracts, o as collectSqliteSchemaIssues, s as createSqliteTableContractReader } from "./sqlite-schema-contract-BXfI47YR.mjs";
import { n as assertSqliteIntegrity } from "./sqlite-integrity-D3yHPOet.mjs";
import { a as inspectOpenClawStateOwnershipFromDatabase } from "./openclaw-state-ownership-C6zxQPsi.mjs";
import { t as OPENCLAW_STATE_SCHEMA_SQL } from "./openclaw-state-schema-Cr2v_hbj.mjs";
import { m as readAgentDatabasePreflightTargets } from "./openclaw-agent-db-registry-listing-DIA5wIwi.mjs";
import { c as readRetainedAgentDeletionsFromDatabase } from "./agent-deletion-journal.read-BRl4UyPn.mjs";
import { n as describeDeferredStateSchemaPublication } from "./openclaw-database-preflight.messages-BMcrDOus.mjs";
//#region src/state/openclaw-state-schema-inspection.ts
function deduplicateSchemaIssues(issues) {
return [...new Map(issues.map((issue) => [`${issue.code}\0${issue.objectName}`, issue])).values()];
}
function inspectCurrentStateStartupSchema(database, databasePath, foundVersion) {
const metadata = assertOpenClawStateDatabaseOwner(database, { pathname: databasePath });
if (metadata?.schema_version !== foundVersion) throw new SqliteSchemaMismatchError(`OpenClaw state database ${databasePath} metadata schema version ${typeof metadata?.schema_version === "number" ? metadata.schema_version : "invalid"} does not match ${foundVersion}.`);
const readTable = createSqliteTableContractReader(database);
const issues = deduplicateSchemaIssues([...collectSqliteSchemaIssues(database, OPENCLAW_STATE_SCHEMA_SQL, OPENCLAW_STATE_MAINTENANCE_SCHEMA_COMPATIBILITY, readTable), ...collectSqliteSchemaIssues(database, OPENCLAW_STATE_SCHEMA_SQL, STATE_RUNTIME_SCHEMA_COMPATIBILITY, readTable)]);
return {
blockingIssues: issues.filter((issue) => !isOpenClawStateStartupRepairableSchemaIssue(issue) && !isOpenClawStateFirstUseSchemaIssue(issue)),
startupRepairableIssues: issues.filter(isOpenClawStateStartupRepairableSchemaIssue)
};
}
//#endregion
//#region src/state/openclaw-state-schema-preflight.ts
const canonicalStateInspectionSchemas = [OPENCLAW_STATE_SCHEMA_SQL];
/** Only trusted canonical definitions enter the expected-contract cache, never observed schemas. */
function captureStateSchemaInspectionContracts() {
return captureSqliteSchemaContracts(canonicalStateInspectionSchemas);
}
/** Boot/Doctor and restart readers inspect the same captured bytes under their existing owner. */
function inspectStateDatabaseSchema(database, input) {
const { pathname, supportedVersion } = input;
const schemas = {
incompatible: [],
indeterminate: []
};
const inspection = {
schemas,
inspectionErrors: []
};
try {
const stateVersion = readSqliteUserVersion(database);
const contentVersion = stateVersion > supportedVersion ? stateVersion : readStateSchemaContentVersion(database, stateVersion);
if (contentVersion > supportedVersion) {
const writerAppVersion = readSqliteWriterAppVersion(database);
schemas.incompatible.push({
kind: "state",
path: pathname,
foundVersion: contentVersion,
supportedVersion,
...writerAppVersion ? { writerAppVersion } : {}
});
if (contentVersion > 20) return inspection;
}
const migrationVersion = stateVersion > supportedVersion ? readStateSchemaContentVersion(database, stateVersion) : contentVersion;
if (migrationVersion < supportedVersion) schemas.pendingMigrations = [{
kind: "state",
path: pathname,
foundVersion: stateVersion,
supportedVersion
}];
if (stateVersion < contentVersion && migrationVersion === contentVersion) schemas.deferredSchemaPublications = [describeDeferredStateSchemaPublication(readStateSchemaPublicationBlocker(database), pathname, stateVersion, contentVersion)];
if (input.requireStartupMigrationReadiness && contentVersion <= 20) {
assertSqliteIntegrity(database, pathname);
assertCanonicalStateSchemaShape(database, pathname);
inspectOpenClawStateOwnershipFromDatabase(database, pathname);
if (migrationVersion === 20) {
const { blockingIssues } = inspectCurrentStateStartupSchema(database, pathname, stateVersion);
if (blockingIssues.length > 0) throw new SqliteSchemaMismatchError(`OpenClaw state database ${pathname} requires repair: ${blockingIssues.map((issue) => issue.message).join("; ")}; run openclaw doctor --fix.`);
} else openClawStateMigrationAssertions.get(migrationVersion)?.(database, { pathname });
} else if (input.verifyCurrentSchemaShape && migrationVersion === 20) try {
assertOpenClawStateDatabaseForMaintenance(database, {
pathname,
schemaVersions: {
userVersion: stateVersion,
contentVersion: migrationVersion
}
});
} catch (error) {
inspection.inspectionErrors.push(error);
schemas.indeterminate.push({
kind: "state",
path: pathname,
reason: formatErrorMessage(error)
});
}
if (input.scope === "state") return inspection;
try {
inspection.registeredDatabases = readAgentDatabasePreflightTargets(database, pathname);
inspection.deletionJournal = readRetainedAgentDeletionsFromDatabase(database, pathname, input.purpose);
} catch (error) {
inspection.inspectionErrors.push(error);
schemas.indeterminate.push({
kind: "state",
path: pathname,
reason: `agent database registry query failed: ${formatErrorMessage(error)}`
});
}
return inspection;
} catch (error) {
if (input.requireStartupMigrationReadiness) throw error;
const failure = normalizeOpenClawStateSchemaReadError(error, pathname);
inspection.inspectionErrors.push(failure);
schemas.indeterminate.push({
kind: "state",
path: pathname,
reason: formatErrorMessage(failure)
});
return inspection;
}
}
//#endregion
export { inspectStateDatabaseSchema as n, inspectCurrentStateStartupSchema as r, captureStateSchemaInspectionContracts as t };