openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
157 lines (156 loc) • 7.77 kB
JavaScript
import { t as formatCliCommand } from "./command-format-CqdwUgPk.mjs";
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import { j as parseAgentSessionKey } from "./session-key-CqAyZeVk.mjs";
import { i as executeSqliteQuerySync } from "./kysely-sync-YJo2lmGV.mjs";
import { n as withOpenClawAgentDatabaseReadOnly } from "./openclaw-agent-db-readonly-COYfV-vn.mjs";
import { a as normalizeStoreSessionKey } from "./store-entry-BTUJi0xl.mjs";
import { t as getSessionKysely } from "./session-accessor.sqlite-scope-helpers-5_tKUULP.mjs";
import "./session-accessor.sqlite-scope-Cbj2BdoG.mjs";
import { l as readCanonicalSessionMainKey } from "./session-canonical-key-q6kSkP3K.mjs";
import { t as extractGeneratedTranscriptSessionId } from "./generated-transcript-session-id-CPClHaZf.mjs";
import { t as countTranscriptEventsForPath } from "./session-sqlite-migration-readers-DbmEQzSW.mjs";
import { d as listSessionSqliteMigrationManifestPaths, p as readSessionSqliteMigrationManifest } from "./session-sqlite-migration-manifest-DwBodX3i.mjs";
import { t as isHistoricalSessionSqliteMigrationIssue } from "./session-sqlite-migration-issues-aFS6aEaW.mjs";
import { t as formatMigrationWarningSummary } from "./migration-warning-summary-u0HGXTex.mjs";
import { t as resolveActiveSqliteTranscriptFile } from "./doctor-session-sqlite-transcript-file-B-CoR0xz.mjs";
import path from "node:path";
//#region src/commands/doctor-session-sqlite-warnings.ts
const TRANSCRIPT_FINDINGS = /* @__PURE__ */ new Map([
["active_sqlite_transcript_jsonl", /^SQLite-backed session has a legacy JSONL transcript awaiting verification: (.+)\. Run openclaw doctor /s],
["transcript_missing", /^Transcript file is missing: (.+)$/s],
["transcript_malformed", /^(.+\.jsonl): (?:SyntaxError|Error|TypeError|RangeError)\b/s]
]);
function currentMigrationIssues(e, t) {
if (!e.issues.some((e) => TRANSCRIPT_FINDINGS.has(e.code))) return e.issues;
const n = e.issues.filter((e) => e.code !== "active_sqlite_transcript_jsonl" && TRANSCRIPT_FINDINGS.has(e.code));
const r = [...new Set(n.flatMap((e) => e.sessionKey ? [e.sessionKey.toLowerCase()] : []))];
const i = [...new Set(n.flatMap((e) => {
const t = TRANSCRIPT_FINDINGS.get(e.code)?.exec(e.message)?.[1];
return t ? [extractGeneratedTranscriptSessionId(t) ?? path.basename(t, ".jsonl")] : [];
}))];
try {
const n = withOpenClawAgentDatabaseReadOnly((t) => {
const n = getSessionKysely(t.db);
const a = new Set(r.length ? executeSqliteQuerySync(t.db, n.selectFrom("session_nodes").select("session_key").where("entry_valid", "=", 1).where(n.fn("lower", ["session_key"]), "in", r)).rows.map((e) => e.session_key.toLowerCase()) : []);
const o = new Set(r.length && i.length ? executeSqliteQuerySync(t.db, n.selectFrom("session_windows").select(["session_key", "session_id"]).where(n.fn("lower", ["session_key"]), "in", r).where("session_id", "in", i).where((e) => e.exists(e.selectFrom("transcript_events").select("session_id").whereRef("transcript_events.session_id", "=", "session_windows.session_id")))).rows.map((e) => `${e.session_key.toLowerCase()}\0${e.session_id}`) : []);
const s = new Set(r.length ? ["main", readCanonicalSessionMainKey(t)] : []);
return e.issues.filter((t) => {
const n = TRANSCRIPT_FINDINGS.get(t.code)?.exec(t.message)?.[1];
if (!n || !path.isAbsolute(n)) return true;
if (t.code === "active_sqlite_transcript_jsonl") return Boolean(resolveActiveSqliteTranscriptFile(e, {
sessionId: "migration-warning",
sessionFile: n
}));
const r = parseAgentSessionKey(t.sessionKey);
if (!t.sessionKey || !r || normalizeAgentId(r.agentId) !== normalizeAgentId(e.agentId) || normalizeStoreSessionKey(t.sessionKey) !== t.sessionKey) return true;
const i = a.has(t.sessionKey.toLowerCase());
if (!i && s.has(r.rest.toLowerCase())) return true;
const c = countTranscriptEventsForPath(n);
if (c.status === "malformed") return true;
const l = extractGeneratedTranscriptSessionId(n) ?? path.basename(n, ".jsonl");
return c.status === "missing" && i && !o.has(`${t.sessionKey.toLowerCase()}\0${l}`);
});
}, {
agentId: e.agentId,
path: e.sqlitePath,
env: t
});
return n.found ? n.value : e.issues;
} catch {
return e.issues;
}
}
function formatSessionSqliteMigrationWarnings(e, t = process.env) {
return e.flatMap((e) => {
let n = 0;
const r = e.issues.flatMap((t) => {
if (t.code === "historical_transcript_deferred" && ++n > 5) return [];
return [`${e.storePath}: [${t.code}] ${t.message}`];
});
if (n > 5) r.unshift(formatMigrationWarningSummary({
summary: `${e.storePath}: Deferred ${n} historical transcript claim(s)`,
count: n,
detail: `Available originals and migration manifests remain protected. Inspect all findings with "${formatCliCommand("openclaw doctor --session-sqlite dry-run --session-sqlite-all-agents --json", t)}".`
}));
return r;
});
}
/** Published updaters may predate the warning result channel; Doctor owns this durable report. */
function readSessionSqliteMigrationWarnings(e = process.env) {
const t = [];
const n = /* @__PURE__ */ new Set();
for (const r of listSessionSqliteMigrationManifestPaths(e)) {
const i = readSessionSqliteMigrationManifest(r);
if (!i?.completedAt) continue;
for (const r of i.targets) {
const i = JSON.stringify([
r.agentId,
r.storePath,
r.sqlitePath
]);
if (n.has(i)) continue;
n.add(i);
for (const n of formatSessionSqliteMigrationWarnings([{
...r,
issues: currentMigrationIssues(r, e).map((e) => e.code === "active_sqlite_transcript_jsonl" ? {
code: e.code,
sessionKey: e.sessionKey,
message: e.message.replace(/Run openclaw doctor --fix or .*$/s, "Run openclaw doctor --session-sqlite inspect to check current originals before choosing recovery.")
} : e)
}], e)) t.push(n);
}
}
return t;
}
/** Bound human presentation; manifests and warning result channels retain their evidence. */
function collectSessionSqliteMigrationFindings(e, t = process.env) {
const n = formatCliCommand("openclaw doctor --session-sqlite dry-run --session-sqlite-all-agents --json", t);
const r = [];
const i = [];
const a = /* @__PURE__ */ new Set();
let o = 0;
const s = "https://docs.openclaw.ai/cli/doctor/sqlite-maintenance#session-sqlite-migration";
for (const t of e) {
const e = [];
for (const n of t.issues) if (isHistoricalSessionSqliteMigrationIssue(n)) {
o++;
a.add(t.storePath);
if (i.length < 5) i.push({
storePath: t.storePath,
message: n.message
});
} else e.push(n);
const c = {
checkId: "core/doctor/session-transcripts",
severity: "warning",
category: "fix-now",
path: t.storePath,
fixHint: `Inspect with "${n}" before attempting recovery.`,
docsUrl: s
};
for (const t of e) r.push({
...c,
errorCode: t.code,
message: t.message
});
}
if (o > 0) {
const e = o > 5 ? formatMigrationWarningSummary({
summary: `${o} historical recovery notice(s)`,
count: o,
detail: "Recovery evidence remains retained."
}) : `${o} historical recovery notice(s). Recovery evidence remains retained.`;
r.push({
checkId: "core/doctor/session-transcripts",
severity: "warning",
category: "historical",
...a.size === 1 ? { path: [...a][0] } : {},
message: [e, ...i.map(({ storePath: e, message: t }) => a.size > 1 ? ` - ${e}: ${t}` : ` - ${t}`)].join("\n"),
fixHint: `No action needed if expected conversations are visible. Keep recovery archives. If a conversation is missing, inspect with "${n}".`,
docsUrl: s
});
}
return r;
}
//#endregion
export { formatSessionSqliteMigrationWarnings as n, readSessionSqliteMigrationWarnings as r, collectSessionSqliteMigrationFindings as t };