UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

212 lines (211 loc) • 8.95 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { a as getOrLoadSqliteDatabaseAdmissionForPath, b as publishSqliteDatabaseAdmission, o as getSqliteDatabaseAdmission, p as invalidateSqliteDatabaseCleanClose } from "./sqlite-database-admission-DQIqro3d.mjs"; import { c as OPENCLAW_DATABASE_SCHEMA_DOCS_URL } 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 { c as resolveQuarantineStorePath } from "./openclaw-state-db.paths-R3ArRCcz.mjs"; import { t as applyPrivateModeSync } from "./private-mode-D8ql0tOD.mjs"; import { n as openSqliteReadOnlyDatabase } from "./sqlite-snapshot-source-Q9S7_uQN.mjs"; import { h as runSqliteReadOnlyWorkerSync } from "./sqlite-readonly-worker-D3pRJU7r.mjs"; import { S as OpenClawQuarantineReadCleanupError } from "./openclaw-state-worker-error-C8y1JGQN.mjs"; import { a as sameSqliteFileGeneration, n as parseSqliteFileGeneration } from "./sqlite-file-generation-DHPWG9de.mjs"; import { existsSync, mkdirSync } from "node:fs"; import path from "node:path"; //#region src/infra/sqlite-file-generation-worker.ts /** Raw descriptor closes must not release a sibling connection's process-wide SQLite locks. */ function readSqliteFileGenerationSync(e) { return parseSqliteFileGeneration(runSqliteReadOnlyWorkerSync(e, void 0, "file-generation")); } //#endregion //#region src/state/openclaw-quarantine-store.ts const OPENCLAW_QUARANTINE_SCHEMA_VERSION = 2; const OPENCLAW_QUARANTINE_BUSY_TIMEOUT_MS = 5e3; const OPENCLAW_QUARANTINE_DIR_MODE = 448; const OPENCLAW_QUARANTINE_FILE_MODE = 384; const quarantineSchemaAdmission = { name: "openclaw.quarantine.schema", lifetime: "process", schemaDependent: true, read(e) { if (typeof e !== "object" || e === null || !("version" in e) || typeof e.version !== "number") return; return { version: e.version }; } }; function quarantineDecisionAdmission(e) { return { name: `openclaw.quarantine.decision:${path.resolve(e)}`, lifetime: "process", read: (e) => e === null || isQuarantineDecision(e) ? e : void 0 }; } function isQuarantineDecision(e) { return isRecord(e) && (e.kind === "agent" || e.kind === "state") && typeof e.reason === "string" && typeof e.quarantinedAt === "number" && (e.verifiedGenerationJson === null || typeof e.verifiedGenerationJson === "string"); } function configureQuarantineWriter(e, t) { e.exec(` PRAGMA journal_mode = DELETE; PRAGMA synchronous = FULL; `); const n = readQuarantineSchemaVersion(e, t); if (n === 2) return; if (n === 1) runSqliteImmediateTransactionSync(e, () => e.exec(` ALTER TABLE quarantined_databases ADD COLUMN verified_generation TEXT; PRAGMA user_version = ${2}; `)); else if (n !== 2) runSqliteImmediateTransactionSync(e, () => e.exec(` CREATE TABLE IF NOT EXISTS quarantined_databases ( path TEXT NOT NULL PRIMARY KEY, kind TEXT NOT NULL, reason TEXT NOT NULL, quarantined_at INTEGER NOT NULL, writer_app_version TEXT, verified_generation TEXT ) STRICT; PRAGMA user_version = ${2}; `)); e.exec(`CREATE TABLE IF NOT EXISTS agent_integrity_verifications ( path TEXT NOT NULL PRIMARY KEY, dev TEXT NOT NULL, ino TEXT NOT NULL, app_version TEXT NOT NULL, verified_at INTEGER NOT NULL, clean_close INTEGER NOT NULL CHECK (clean_close IN (0, 1)) ) STRICT;`); publishSqliteDatabaseAdmission(e, quarantineSchemaAdmission, { version: 2 }); } function readQuarantineSchemaVersion(e, t, n = false) { const r = n ? void 0 : getSqliteDatabaseAdmission(e, quarantineSchemaAdmission); if (r) return r.version; const i = e.prepare("PRAGMA user_version").get()?.user_version; if (typeof i !== "number" || !Number.isInteger(i)) throw new Error(`OpenClaw quarantine store ${t} has an invalid schema version.`); if (i > 2) throw new Error(`OpenClaw quarantine store ${t} uses newer schema version ${i}.`); if (i !== 0) publishSqliteDatabaseAdmission(e, quarantineSchemaAdmission, { version: i }); return i; } function withQuarantineWriter(e, t) { const n = resolveQuarantineStorePath(e); const r = existsSync(n); const i = path.dirname(n); mkdirSync(i, { recursive: true, mode: OPENCLAW_QUARANTINE_DIR_MODE }); applyPrivateModeSync(i, OPENCLAW_QUARANTINE_DIR_MODE); const a = openNodeSqliteDatabase(n, { timeout: OPENCLAW_QUARANTINE_BUSY_TIMEOUT_MS }); let o = false; try { if (!r) applyPrivateModeSync(n, OPENCLAW_QUARANTINE_FILE_MODE); configureQuarantineWriter(a, n); const e = t(a); o = true; return e; } finally { if (a.isOpen) a.close(); if (o || !r) applyPrivateModeSync(n, OPENCLAW_QUARANTINE_FILE_MODE); } } /** Its existence preserves prior state ownership even when an external agent is not configured. */ function ensureOpenClawQuarantineStore(e) { if (!existsSync(resolveQuarantineStorePath(e))) withQuarantineWriter(e, () => {}); } /** Runtime guards share the owner's decision; explicit repair may inspect disk. */ function readOpenClawDatabaseQuarantine(e, t = {}) { const n = resolveQuarantineStorePath(t.env ?? process.env); const load = () => readPersistedQuarantineDecision(e, n, t.fresh) ?? null; const r = t.fresh ? load() : getOrLoadSqliteDatabaseAdmissionForPath(n, quarantineDecisionAdmission(e), load); return r ? resolveQuarantineDecision(r, e, n) : void 0; } function readPersistedQuarantineDecision(e, t, n = false) { if (!existsSync(t)) return; const r = openSqliteReadOnlyDatabase(t, { timeout: OPENCLAW_QUARANTINE_BUSY_TIMEOUT_MS }); let i; try { i = { value: readQuarantineDecision(r, e, t, n) }; } catch (e) { i = { error: e }; } try { r.close(); } catch (n) { let r; let a = "error" in i ? i.error : void 0; if ("value" in i && i.value) try { r = resolveQuarantineDecision(i.value, e, t); } catch (e) { a = e; } throw new OpenClawQuarantineReadCleanupError(a === void 0 ? [n] : [a, n], r); } if ("error" in i) throw i.error; return i.value; } function readQuarantineDecision(e, t, n, r = false) { const i = readQuarantineSchemaVersion(e, n, r); if (i === 0) return; const a = i >= 2 ? ", verified_generation" : ""; const o = e.prepare(`SELECT kind, reason, quarantined_at${a} FROM quarantined_databases WHERE path = ? LIMIT 1`).get(path.resolve(t)); if (!o) return; const s = i >= 2 ? o.verified_generation : void 0; if (o.kind !== "agent" && o.kind !== "state" || typeof o.reason !== "string" || typeof o.quarantined_at !== "number" || !Number.isInteger(o.quarantined_at) || s !== void 0 && s !== null && typeof s !== "string") throw new Error(`OpenClaw quarantine store ${n} contains an invalid row.`); return { kind: o.kind, quarantinedAt: o.quarantined_at, reason: o.reason, verifiedGenerationJson: typeof s === "string" ? s : null }; } function resolveQuarantineDecision(e, t, n) { if (e.verifiedGenerationJson !== null) { let r; try { r = parseSqliteFileGeneration(e.verifiedGenerationJson); } catch { throw new Error(`OpenClaw quarantine store ${n} contains an invalid row.`); } try { const e = readSqliteFileGenerationSync(path.resolve(t)); if (!sameSqliteFileGeneration(r, e)) return; } catch { return; } } return { kind: e.kind, quarantinedAt: e.quarantinedAt, reason: e.reason }; } /** Runtime opens refuse recorded damage while tolerating a broken quarantine index. */ function readOpenClawDatabaseQuarantineFailure(e, t, n = {}) { let r; let i; try { r = readOpenClawDatabaseQuarantine(t, n); } catch (e) { if (!(e instanceof OpenClawQuarantineReadCleanupError)) return; if (!e.quarantine) throw e; r = e.quarantine; i = e; } if (!r) return; const a = /* @__PURE__ */ new Error(`OpenClaw ${e} database ${t} is quarantined after integrity verification failed: ${r.reason ?? "unknown integrity error"}. Restore the database from a backup or repair it, then run openclaw doctor --fix to clear the quarantine. See ${OPENCLAW_DATABASE_SCHEMA_DOCS_URL}.`); a.name = "SqliteIntegrityError"; if (i) a.cause = i; return a; } function clearOpenClawDatabaseQuarantine(e, t = {}) { const n = t.env ?? process.env; try { invalidateSqliteDatabaseCleanClose(e); if (!existsSync(resolveQuarantineStorePath(n))) return true; return withQuarantineWriter(n, (t) => runSqliteImmediateTransactionSync(t, () => { t.prepare("DELETE FROM quarantined_databases WHERE path = ?").run(path.resolve(e)); publishSqliteDatabaseAdmission(t, quarantineDecisionAdmission(e), null); return true; }, { databaseLabel: resolveQuarantineStorePath(n), operationLabel: "quarantine.clear" })); } catch { return false; } } //#endregion export { readSqliteFileGenerationSync as a, readOpenClawDatabaseQuarantineFailure as i, clearOpenClawDatabaseQuarantine as n, ensureOpenClawQuarantineStore as r, OPENCLAW_QUARANTINE_SCHEMA_VERSION as t };