|
| 1 | +// Memory Core tests cover doctor migration of legacy dreaming state. |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { |
| 6 | + createPluginStateKeyedStoreForTests, |
| 7 | + resetPluginStateStoreForTests, |
| 8 | +} from "openclaw/plugin-sdk/plugin-state-test-runtime"; |
| 9 | +import type { |
| 10 | + OpenKeyedStoreOptions, |
| 11 | + PluginDoctorStateMigrationContext, |
| 12 | +} from "openclaw/plugin-sdk/runtime-doctor"; |
| 13 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 14 | +import { stateMigrations } from "./doctor-contract-api.js"; |
| 15 | +import { testing as dreamingTesting } from "./src/dreaming-phases.js"; |
| 16 | +import { |
| 17 | + configureMemoryCoreDreamingState, |
| 18 | + resetMemoryCoreDreamingStateForTests, |
| 19 | +} from "./src/dreaming-state.js"; |
| 20 | +import { testing as shortTermTesting } from "./src/short-term-promotion.js"; |
| 21 | + |
| 22 | +function createDoctorContext(env: NodeJS.ProcessEnv): PluginDoctorStateMigrationContext { |
| 23 | + return { |
| 24 | + openPluginStateKeyedStore<T>(options: OpenKeyedStoreOptions) { |
| 25 | + return createPluginStateKeyedStoreForTests<T>("memory-core", { |
| 26 | + ...options, |
| 27 | + env: options.env ?? env, |
| 28 | + }); |
| 29 | + }, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +describe("memory-core doctor dreaming migration", () => { |
| 34 | + let rootDir = ""; |
| 35 | + let workspaceDir = ""; |
| 36 | + let env: NodeJS.ProcessEnv; |
| 37 | + |
| 38 | + beforeEach(async () => { |
| 39 | + resetPluginStateStoreForTests(); |
| 40 | + rootDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-memory-core-doctor-")); |
| 41 | + workspaceDir = path.join(rootDir, "workspace"); |
| 42 | + await fs.mkdir(path.join(workspaceDir, "memory", ".dreams"), { recursive: true }); |
| 43 | + env = { ...process.env, OPENCLAW_STATE_DIR: path.join(rootDir, "state") }; |
| 44 | + }); |
| 45 | + |
| 46 | + afterEach(async () => { |
| 47 | + resetMemoryCoreDreamingStateForTests(); |
| 48 | + await fs.rm(rootDir, { recursive: true, force: true }); |
| 49 | + }); |
| 50 | + |
| 51 | + function context(): PluginDoctorStateMigrationContext { |
| 52 | + return createDoctorContext(env); |
| 53 | + } |
| 54 | + |
| 55 | + function migrationParams( |
| 56 | + config: unknown = { |
| 57 | + agents: { |
| 58 | + list: [{ id: "main", workspace: workspaceDir }], |
| 59 | + }, |
| 60 | + }, |
| 61 | + ) { |
| 62 | + return { |
| 63 | + config, |
| 64 | + env, |
| 65 | + stateDir: path.join(rootDir, "state"), |
| 66 | + oauthDir: path.join(rootDir, "oauth"), |
| 67 | + context: context(), |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | + it("imports persistent legacy dreaming state and ignores transient locks", async () => { |
| 72 | + const dreamsDir = path.join(workspaceDir, "memory", ".dreams"); |
| 73 | + const dailyPath = path.join(dreamsDir, "daily-ingestion.json"); |
| 74 | + const sessionPath = path.join(dreamsDir, "session-ingestion.json"); |
| 75 | + const recallPath = path.join(dreamsDir, "short-term-recall.json"); |
| 76 | + const phasePath = path.join(dreamsDir, "phase-signals.json"); |
| 77 | + const lockPath = path.join(dreamsDir, "short-term-promotion.lock"); |
| 78 | + |
| 79 | + await fs.writeFile( |
| 80 | + dailyPath, |
| 81 | + JSON.stringify({ |
| 82 | + version: 1, |
| 83 | + files: { |
| 84 | + "memory/2026-04-05.md": { |
| 85 | + size: 42, |
| 86 | + mtimeMs: 1, |
| 87 | + contentHash: "daily-hash", |
| 88 | + ingestedAt: "2026-04-05T10:00:00.000Z", |
| 89 | + }, |
| 90 | + }, |
| 91 | + }), |
| 92 | + "utf8", |
| 93 | + ); |
| 94 | + await fs.writeFile( |
| 95 | + sessionPath, |
| 96 | + JSON.stringify({ |
| 97 | + version: 1, |
| 98 | + files: { |
| 99 | + "main/session.jsonl": { |
| 100 | + size: 91, |
| 101 | + mtimeMs: 2, |
| 102 | + lineCount: 3, |
| 103 | + lastContentLine: 3, |
| 104 | + contentHash: "session-hash", |
| 105 | + ingestedAt: "2026-04-05T11:00:00.000Z", |
| 106 | + }, |
| 107 | + }, |
| 108 | + seenMessages: { |
| 109 | + "main/session.jsonl": ["seen-a", "seen-b"], |
| 110 | + }, |
| 111 | + }), |
| 112 | + "utf8", |
| 113 | + ); |
| 114 | + await fs.writeFile( |
| 115 | + recallPath, |
| 116 | + JSON.stringify({ |
| 117 | + version: 1, |
| 118 | + updatedAt: "2026-04-05T12:00:00.000Z", |
| 119 | + entries: { |
| 120 | + "memory:memory/2026-04-05.md:1:1": { |
| 121 | + key: "memory:memory/2026-04-05.md:1:1", |
| 122 | + path: "memory/2026-04-05.md", |
| 123 | + startLine: 1, |
| 124 | + endLine: 1, |
| 125 | + source: "memory", |
| 126 | + snippet: "Move backups to S3 Glacier.", |
| 127 | + recallCount: 1, |
| 128 | + totalScore: 0.9, |
| 129 | + maxScore: 0.9, |
| 130 | + firstRecalledAt: "2026-04-05T12:00:00.000Z", |
| 131 | + lastRecalledAt: "2026-04-05T12:00:00.000Z", |
| 132 | + queryHashes: ["hash-a"], |
| 133 | + }, |
| 134 | + }, |
| 135 | + }), |
| 136 | + "utf8", |
| 137 | + ); |
| 138 | + await fs.writeFile( |
| 139 | + phasePath, |
| 140 | + JSON.stringify({ |
| 141 | + version: 1, |
| 142 | + updatedAt: "2026-04-05T13:00:00.000Z", |
| 143 | + entries: { |
| 144 | + "memory:memory/2026-04-05.md:1:1": { |
| 145 | + key: "memory:memory/2026-04-05.md:1:1", |
| 146 | + lightHits: 1, |
| 147 | + remHits: 2, |
| 148 | + lastLightAt: "2026-04-05T12:00:00.000Z", |
| 149 | + lastRemAt: "2026-04-05T13:00:00.000Z", |
| 150 | + }, |
| 151 | + }, |
| 152 | + }), |
| 153 | + "utf8", |
| 154 | + ); |
| 155 | + await fs.writeFile(lockPath, `${process.pid}:${Date.now()}\n`, "utf8"); |
| 156 | + |
| 157 | + const migration = stateMigrations[0]; |
| 158 | + const preview = await migration.detectLegacyState(migrationParams()); |
| 159 | + expect(preview?.preview).toEqual([ |
| 160 | + expect.stringContaining("Memory Core daily ingestion"), |
| 161 | + expect.stringContaining("Memory Core session ingestion"), |
| 162 | + expect.stringContaining("Memory Core short-term recall"), |
| 163 | + expect.stringContaining("Memory Core phase signals"), |
| 164 | + ]); |
| 165 | + expect(preview?.preview.join("\n")).not.toContain("short-term-promotion.lock"); |
| 166 | + |
| 167 | + const result = await migration.migrateLegacyState(migrationParams()); |
| 168 | + expect(result.warnings).toEqual([]); |
| 169 | + expect(result.changes).toEqual([ |
| 170 | + "Migrated Memory Core daily ingestion -> SQLite plugin state (1 row(s))", |
| 171 | + expect.stringContaining("Archived Memory Core daily ingestion legacy source"), |
| 172 | + "Migrated Memory Core session ingestion -> SQLite plugin state (2 row(s))", |
| 173 | + expect.stringContaining("Archived Memory Core session ingestion legacy source"), |
| 174 | + "Migrated Memory Core short-term recall -> SQLite plugin state (1 row(s))", |
| 175 | + expect.stringContaining("Archived Memory Core short-term recall legacy source"), |
| 176 | + "Migrated Memory Core phase signals -> SQLite plugin state (1 row(s))", |
| 177 | + expect.stringContaining("Archived Memory Core phase signals legacy source"), |
| 178 | + ]); |
| 179 | + |
| 180 | + configureMemoryCoreDreamingState(context().openPluginStateKeyedStore); |
| 181 | + await expect(fs.access(`${dailyPath}.migrated`)).resolves.toBeUndefined(); |
| 182 | + await expect(fs.access(`${sessionPath}.migrated`)).resolves.toBeUndefined(); |
| 183 | + await expect(fs.access(`${recallPath}.migrated`)).resolves.toBeUndefined(); |
| 184 | + await expect(fs.access(`${phasePath}.migrated`)).resolves.toBeUndefined(); |
| 185 | + await expect(fs.access(lockPath)).resolves.toBeUndefined(); |
| 186 | + |
| 187 | + const daily = await dreamingTesting.readDailyIngestionState(workspaceDir); |
| 188 | + expect(daily.files["memory/2026-04-05.md"]?.mtimeMs).toBe(1); |
| 189 | + const session = await dreamingTesting.readSessionIngestionState(workspaceDir); |
| 190 | + expect(session.files["main/session.jsonl"]?.contentHash).toBe("session-hash"); |
| 191 | + expect(session.seenMessages["main/session.jsonl"]).toEqual(["seen-a", "seen-b"]); |
| 192 | + const recall = await shortTermTesting.readRecallStore(workspaceDir, "2026-04-05T12:00:00.000Z"); |
| 193 | + expect(recall.entries["memory:memory/2026-04-05.md:1:1"]?.conceptTags).toContain("glacier"); |
| 194 | + const phase = await shortTermTesting.readPhaseSignalStore( |
| 195 | + workspaceDir, |
| 196 | + "2026-04-05T13:00:00.000Z", |
| 197 | + ); |
| 198 | + expect(phase.entries["memory:memory/2026-04-05.md:1:1"]?.remHits).toBe(2); |
| 199 | + }); |
| 200 | + |
| 201 | + it("leaves invalid legacy JSON in place", async () => { |
| 202 | + const recallPath = path.join(workspaceDir, "memory", ".dreams", "short-term-recall.json"); |
| 203 | + await fs.writeFile(recallPath, "{", "utf8"); |
| 204 | + |
| 205 | + const result = await stateMigrations[0].migrateLegacyState(migrationParams()); |
| 206 | + |
| 207 | + expect(result.changes).toEqual([]); |
| 208 | + expect(result.warnings).toEqual([ |
| 209 | + expect.stringContaining("Skipped Memory Core short-term recall import"), |
| 210 | + ]); |
| 211 | + await expect(fs.access(recallPath)).resolves.toBeUndefined(); |
| 212 | + await expect(fs.access(`${recallPath}.migrated`)).rejects.toThrow(); |
| 213 | + configureMemoryCoreDreamingState(context().openPluginStateKeyedStore); |
| 214 | + const recall = await shortTermTesting.readRecallStore(workspaceDir, new Date().toISOString()); |
| 215 | + expect(recall.entries).toEqual({}); |
| 216 | + }); |
| 217 | + |
| 218 | + it("uses migration env when resolving default workspaces", async () => { |
| 219 | + env = { ...env, OPENCLAW_WORKSPACE_DIR: workspaceDir }; |
| 220 | + const recallPath = path.join(workspaceDir, "memory", ".dreams", "short-term-recall.json"); |
| 221 | + await fs.writeFile( |
| 222 | + recallPath, |
| 223 | + JSON.stringify({ |
| 224 | + version: 1, |
| 225 | + updatedAt: "2026-04-05T12:00:00.000Z", |
| 226 | + entries: { |
| 227 | + "memory:memory/2026-04-05.md:1:1": { |
| 228 | + key: "memory:memory/2026-04-05.md:1:1", |
| 229 | + path: "memory/2026-04-05.md", |
| 230 | + startLine: 1, |
| 231 | + endLine: 1, |
| 232 | + source: "memory", |
| 233 | + snippet: "Move backups to S3 Glacier.", |
| 234 | + recallCount: 1, |
| 235 | + totalScore: 0.9, |
| 236 | + maxScore: 0.9, |
| 237 | + firstRecalledAt: "2026-04-05T12:00:00.000Z", |
| 238 | + lastRecalledAt: "2026-04-05T12:00:00.000Z", |
| 239 | + queryHashes: ["hash-a"], |
| 240 | + }, |
| 241 | + }, |
| 242 | + }), |
| 243 | + "utf8", |
| 244 | + ); |
| 245 | + const config = { agents: { list: [{ id: "main", default: true }] } }; |
| 246 | + |
| 247 | + const preview = await stateMigrations[0].detectLegacyState(migrationParams(config)); |
| 248 | + expect(preview?.preview).toEqual([expect.stringContaining("Memory Core short-term recall")]); |
| 249 | + |
| 250 | + const result = await stateMigrations[0].migrateLegacyState(migrationParams(config)); |
| 251 | + |
| 252 | + expect(result.warnings).toEqual([]); |
| 253 | + expect(result.changes).toEqual([ |
| 254 | + "Migrated Memory Core short-term recall -> SQLite plugin state (1 row(s))", |
| 255 | + expect.stringContaining("Archived Memory Core short-term recall legacy source"), |
| 256 | + ]); |
| 257 | + configureMemoryCoreDreamingState(context().openPluginStateKeyedStore); |
| 258 | + const recall = await shortTermTesting.readRecallStore(workspaceDir, "2026-04-05T12:00:00.000Z"); |
| 259 | + expect(recall.entries["memory:memory/2026-04-05.md:1:1"]?.conceptTags).toContain("glacier"); |
| 260 | + }); |
| 261 | +}); |
0 commit comments