|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { createHash } from "node:crypto"; |
| 3 | +import fs from "node:fs"; |
| 4 | +import path from "node:path"; |
| 5 | +import { pathToFileURL } from "node:url"; |
| 6 | +import { openNodeSqliteDatabase } from "../infra/node-sqlite.js"; |
| 7 | +import { closeOpenClawAgentDatabasesForTest } from "../state/openclaw-agent-db.js"; |
| 8 | +import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js"; |
| 9 | +import { resolveTargetSqlitePath } from "./doctor-session-sqlite-readers.js"; |
| 10 | +import { runDoctorSessionSqlite } from "./doctor-session-sqlite.js"; |
| 11 | + |
| 12 | +export const sqliteImportMemorySupportUrl = import.meta.url; |
| 13 | + |
| 14 | +async function main() { |
| 15 | + const [stateDir, scenario] = process.argv.slice(2); |
| 16 | + assert(stateDir && scenario); |
| 17 | + process.env.OPENCLAW_STATE_DIR = stateDir; |
| 18 | + process.env.OPENCLAW_CONFIG_PATH = path.join(stateDir, "openclaw.json"); |
| 19 | + const sessionCount = scenario === "batch" ? 256 : 1; |
| 20 | + const eventCount = scenario === "deep" ? 100_000 : scenario === "batch" ? 64 : 8; |
| 21 | + const payloadBytes = scenario === "deep" ? 4096 : scenario === "batch" ? 32768 : 256; |
| 22 | + const sessionsDir = path.join(stateDir, "agents/main/sessions"); |
| 23 | + const storePath = path.join(sessionsDir, "sessions.json"); |
| 24 | + fs.mkdirSync(sessionsDir, { recursive: true }); |
| 25 | + const store: Record<string, { sessionId: string; updatedAt: number; sessionFile: string }> = {}; |
| 26 | + const expected = new Map<string, string>(); |
| 27 | + const timestamp = 1_800_000_000_000; |
| 28 | + for (let session = 0; session < sessionCount; session++) { |
| 29 | + const sessionId = `memory-${session}`; |
| 30 | + const transcriptPath = path.join(sessionsDir, `${sessionId}.jsonl`); |
| 31 | + store[`agent:main:memory-${session}`] = { |
| 32 | + sessionId, |
| 33 | + updatedAt: timestamp, |
| 34 | + sessionFile: transcriptPath, |
| 35 | + }; |
| 36 | + const fd = fs.openSync(transcriptPath, "wx", 0o600); |
| 37 | + const hash = createHash("sha256"); |
| 38 | + const write = (event: unknown) => { |
| 39 | + const json = JSON.stringify(event); |
| 40 | + hash.update(json).update("\n"); |
| 41 | + fs.writeSync(fd, `${json}\n`); |
| 42 | + }; |
| 43 | + try { |
| 44 | + write({ |
| 45 | + type: "session", |
| 46 | + version: 3, |
| 47 | + id: sessionId, |
| 48 | + timestamp: new Date(timestamp).toISOString(), |
| 49 | + cwd: "/fixture", |
| 50 | + }); |
| 51 | + for (let index = 0; index < eventCount; index++) { |
| 52 | + write({ |
| 53 | + type: "message", |
| 54 | + id: `e-${index}`, |
| 55 | + parentId: index === 0 ? null : `e-${index - 1}`, |
| 56 | + timestamp, |
| 57 | + message: { |
| 58 | + role: "toolResult", |
| 59 | + toolCallId: `call-${index}`, |
| 60 | + toolName: "exec", |
| 61 | + isError: false, |
| 62 | + content: [{ type: "text", text: `${session}:${index}:é🦞${"x".repeat(payloadBytes)}` }], |
| 63 | + timestamp, |
| 64 | + }, |
| 65 | + }); |
| 66 | + } |
| 67 | + // Force the full projection owner, including a deep selected ancestry. |
| 68 | + write({ |
| 69 | + type: "leaf", |
| 70 | + id: "selected-leaf", |
| 71 | + parentId: `e-${eventCount - 1}`, |
| 72 | + targetId: `e-${eventCount - 2}`, |
| 73 | + }); |
| 74 | + } finally { |
| 75 | + fs.closeSync(fd); |
| 76 | + } |
| 77 | + expected.set(sessionId, hash.digest("hex")); |
| 78 | + } |
| 79 | + fs.writeFileSync(storePath, JSON.stringify(store)); |
| 80 | + process.stderr.write(`seeded ${sessionCount} sessions x ${eventCount} events; importing\n`); |
| 81 | + const started = performance.now(); |
| 82 | + const report = await runDoctorSessionSqlite({ |
| 83 | + mode: "import", |
| 84 | + store: storePath, |
| 85 | + env: process.env, |
| 86 | + }); |
| 87 | + assert.equal(report.totals.issues, 0, JSON.stringify(report.targets.map((t) => t.issues))); |
| 88 | + assert.equal(report.totals.importedEntries, sessionCount); |
| 89 | + assert.equal(report.totals.importedTranscriptEvents, sessionCount * (eventCount + 2)); |
| 90 | + const db = openNodeSqliteDatabase(resolveTargetSqlitePath({ agentId: "main", storePath }), { |
| 91 | + readOnly: true, |
| 92 | + }); |
| 93 | + try { |
| 94 | + const read = db.prepare( |
| 95 | + "SELECT event_json FROM transcript_events WHERE session_id = ? ORDER BY seq", |
| 96 | + ); |
| 97 | + for (const [sessionId, digest] of expected) { |
| 98 | + const hash = createHash("sha256"); |
| 99 | + for (const row of read.iterate(sessionId)) { |
| 100 | + hash.update(String(row.event_json)).update("\n"); |
| 101 | + } |
| 102 | + assert.equal(hash.digest("hex"), digest); |
| 103 | + assert.deepEqual( |
| 104 | + { |
| 105 | + ...db |
| 106 | + .prepare( |
| 107 | + "SELECT active_event_count, active_message_count, leaf_event_id, needs_rebuild FROM session_transcript_index_state WHERE session_id = ?", |
| 108 | + ) |
| 109 | + .get(sessionId), |
| 110 | + }, |
| 111 | + { |
| 112 | + active_event_count: eventCount - 1, |
| 113 | + active_message_count: eventCount - 1, |
| 114 | + leaf_event_id: `e-${eventCount - 2}`, |
| 115 | + needs_rebuild: 0, |
| 116 | + }, |
| 117 | + ); |
| 118 | + } |
| 119 | + } finally { |
| 120 | + db.close(); |
| 121 | + } |
| 122 | + const rerun = await runDoctorSessionSqlite({ |
| 123 | + mode: "import", |
| 124 | + store: storePath, |
| 125 | + env: process.env, |
| 126 | + }); |
| 127 | + assert.equal(rerun.totals.importedEntries, 0); |
| 128 | + closeOpenClawAgentDatabasesForTest(); |
| 129 | + closeOpenClawStateDatabaseForTest(); |
| 130 | + process.stdout.write( |
| 131 | + JSON.stringify({ |
| 132 | + scenario, |
| 133 | + sessionCount, |
| 134 | + eventCount, |
| 135 | + elapsedMs: performance.now() - started, |
| 136 | + maxRssKiB: process.resourceUsage().maxRSS, |
| 137 | + }), |
| 138 | + ); |
| 139 | +} |
| 140 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 141 | + void main().catch((error: unknown) => { |
| 142 | + console.error(error); |
| 143 | + process.exitCode = 1; |
| 144 | + }); |
| 145 | +} |
0 commit comments