openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
165 lines (164 loc) • 7.44 kB
JavaScript
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import "./session-key-CqAyZeVk.mjs";
import { t as OPENCLAW_AGENT_SCHEMA_SQL } from "./openclaw-agent-schema-BDRJZxWF.mjs";
import { t as OPENCLAW_AGENT_SCHEMA_VERSION } from "./openclaw-schema-versions-E_10NoI6.mjs";
import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs";
import "./kysely-sync-YJo2lmGV.mjs";
import { a as enableNodeSqliteKyselyStatementCache, n as clearNodeSqliteKyselyCacheForDatabase } from "./kysely-sync-cache-state-Bb1gOqQO.mjs";
import { d as invalidateSqliteSchemaFacts } 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 } from "./node-sqlite-CU4puofF.mjs";
import { r as readExistingAgentSchemaMeta } from "./openclaw-agent-db-metadata-C3YvwHLh.mjs";
import { o as getAgentDeletionDatabaseCleanup } from "./agent-deletion-cleanup-BweMIhGD.mjs";
import "./openclaw-agent-db-contract-aCH9b8Uc.mjs";
import { y as repairDoctorSqliteIndexCorruption } from "./openclaw-state-db-ARhwdPJg.mjs";
import { t as configureSqliteMaintenanceCache } from "./sqlite-maintenance-cache-E5FQa1nK.mjs";
import { r as assertAgentDatabaseMaintenanceAuthority } from "./openclaw-agent-db-maintenance-authority-C4Yhyki9.mjs";
import { i as assertSupportedAgentSchemaVersion, r as assertExistingAgentSchemaOwner } from "./openclaw-agent-db-schema-read-U5-_81Kg.mjs";
import { l as invalidateOpenClawAgentDatabaseIntegrityBeforeMutation } from "./openclaw-agent-db-lease-BZhGt_sk.mjs";
import { a as assertOpenClawAgentSchemaContains } from "./openclaw-agent-outbox-sequence-migration-ATXcQD7n.mjs";
import { t as runSqliteIntegrityOperationInWorker } from "./sqlite-integrity-operation-Cqe4C-U7.mjs";
import { i as ensureOpenClawAgentDatabaseSchemaSteps } from "./openclaw-agent-db-schema-D-iWGl31.mjs";
import { t as AGENT_FTS_SCHEMAS } from "./openclaw-agent-fts-schema-CoPVC2Y6.mjs";
//#region src/state/openclaw-agent-db-maintenance.ts
const agentDbLog = createSubsystemLogger("state/agent-db");
/** Require exact agent ownership without requiring the latest schema. */
function assertOpenClawAgentDatabaseOwner(e, t) {
invalidateSqliteSchemaFacts(e);
const n = normalizeAgentId(t.agentId);
const r = readExistingAgentSchemaMeta(e);
if (!r) throw new SqliteSchemaMismatchError(`OpenClaw agent database ${t.pathname} has no schema ownership metadata. Run openclaw doctor --fix to inspect and repair its ownership.`);
assertExistingAgentSchemaOwner(r, n, t.pathname);
if (r.agentId !== n) throw new SqliteSchemaMismatchError(`OpenClaw agent database ${t.pathname} belongs to agent ${r.agentId}; requested agent ${n}.`);
return r;
}
/** Require the exact agent owner and schema before offline file maintenance. */
function assertOpenClawAgentDatabaseForMaintenance(e, t) {
const n = assertOpenClawAgentDatabaseOwner(e, t);
const r = assertSupportedAgentSchemaVersion(e, t.pathname);
if (r !== 38) throw new SqliteSchemaMismatchError(`OpenClaw agent database ${t.pathname} uses schema version ${r}; run openclaw doctor --fix before compacting it.`);
if (n.schemaVersion !== 38) throw new SqliteSchemaMismatchError(`OpenClaw agent database ${t.pathname} metadata schema version ${n.schemaVersion ?? "invalid"} does not match ${38}; run openclaw doctor --fix before compacting it.`);
return assertOpenClawAgentSchemaContains(e, t.pathname, OPENCLAW_AGENT_SCHEMA_SQL, "current", t.allowStartupIndexRepair);
}
/** Upgrade or repair a supported owned schema before strict offline maintenance. */
async function migrateOpenClawAgentDatabaseForMaintenance(e, t) {
const n = normalizeAgentId(e.agentId);
const r = e.pathname;
const i = { ...e.env ?? process.env };
const a = getAgentDeletionDatabaseCleanup({
agentId: n,
path: r,
env: i
});
const assertOwned = () => {
t.signal.throwIfAborted();
assertAgentDatabaseMaintenanceAuthority(t);
a?.assertCurrent();
};
assertOwned();
invalidateOpenClawAgentDatabaseIntegrityBeforeMutation(r);
const o = openNodeSqliteDatabase(r);
try {
invalidateSqliteSchemaFacts(o);
configureSqliteMaintenanceCache(o);
enableNodeSqliteKyselyStatementCache(o);
o.exec(`PRAGMA busy_timeout = ${OPENCLAW_SQLITE_BUSY_TIMEOUT_MS};`);
const a = readExistingAgentSchemaMeta(o);
if (!a) {
if (e.register) assertOpenClawAgentDatabaseForMaintenance(o, {
agentId: n,
pathname: r
});
return;
}
assertExistingAgentSchemaOwner(a, n, r);
const s = assertSupportedAgentSchemaVersion(o, r);
const c = a.schemaVersion;
if (!(s === 38 && c === 38) && !(s >= 1 && s < 38 && c === s)) {
if (e.register) assertOpenClawAgentDatabaseForMaintenance(o, {
agentId: n,
pathname: r
});
return;
}
const repairIndexes = () => {
const e = repairDoctorSqliteIndexCorruption(o, r, {
ftsSchemas: AGENT_FTS_SCHEMAS,
label: `agent ${n}`,
assertCurrent: () => {
assertOwned();
assertOpenClawAgentDatabaseOwner(o, {
agentId: n,
pathname: r
});
assertSupportedAgentSchemaVersion(o, r);
}
});
for (const t of e) agentDbLog.warn(t);
return e.length > 0;
};
if (s === 17) repairIndexes();
const l = ensureOpenClawAgentDatabaseSchemaSteps(o, {
agentId: n,
path: r,
env: i,
register: e.register
});
await runSqliteIntegrityOperationInWorker(l, {
busyTimeoutMs: OPENCLAW_SQLITE_BUSY_TIMEOUT_MS,
signal: t.signal,
beforeResume: () => {
assertOwned();
invalidateSqliteSchemaFacts(o);
assertExistingAgentSchemaOwner(readExistingAgentSchemaMeta(o), n, r);
assertSupportedAgentSchemaVersion(o, r);
},
repairIntegrityError: repairIndexes
});
assertOwned();
assertOpenClawAgentDatabaseForMaintenance(o, {
agentId: n,
pathname: r
});
} finally {
clearNodeSqliteKyselyCacheForDatabase(o);
o.close();
}
}
/** Doctor prepares pending stores through their deletion owner without completing cleanup. */
async function preparePendingAgentDeletionDatabasesForMaintenance(e, t) {
const [{ withAgentDeletion: n }, { listPendingAgentDeletionJournalsAsync: r }] = await Promise.all([import("./agent-lifecycle-registry-CNlzzWmz.mjs"), import("./agent-deletion-journal-DuW0PNFF.mjs")]);
const { env: i } = e;
const a = await r({ env: i });
const o = /* @__PURE__ */ new Map();
for (const t of e.targets) {
const e = o.get(t.agentId) ?? [];
e.push(t.path);
o.set(t.agentId, e);
}
const s = [];
for (const [e, r] of o) {
t.assertOwned();
const o = a.entries.find((t) => t.agentId === e);
if (!o) throw new Error(`Agent ${e} deletion changed before schema preparation.`);
await n(e, async (n) => {
const a = await n(o, { recoveryOperationId: o.operationId });
for (const n of r) {
await a.runDatabaseCleanup({
agentId: e,
path: n
}, () => migrateOpenClawAgentDatabaseForMaintenance({
agentId: e,
pathname: n,
env: i,
register: true
}, t));
s.push(`Prepared agent database schema in ${n} for pending deletion recovery.`);
}
}, { env: i });
}
return s;
}
//#endregion
export { preparePendingAgentDeletionDatabasesForMaintenance as i, assertOpenClawAgentDatabaseOwner as n, migrateOpenClawAgentDatabaseForMaintenance as r, assertOpenClawAgentDatabaseForMaintenance as t };