|
1 | 1 | // Memory Wiki doctor contract migrates shipped source-sync state. |
2 | 2 | import fs from "node:fs/promises"; |
| 3 | +import path from "node:path"; |
3 | 4 | import type { OpenClawConfig } from "openclaw/plugin-sdk/plugin-entry"; |
4 | 5 | import type { PluginDoctorStateMigration } from "openclaw/plugin-sdk/runtime-doctor"; |
5 | 6 | import { resolveMemoryWikiConfig, type MemoryWikiPluginConfig } from "./src/config.js"; |
6 | 7 | export { legacyConfigRules, normalizeCompatibilityConfig } from "./src/config-compat.js"; |
| 8 | +import { |
| 9 | + countMemoryWikiImportRunStateRows, |
| 10 | + createMemoryWikiImportRunStateStore, |
| 11 | + listMemoryWikiImportRunRecords, |
| 12 | + MEMORY_WIKI_IMPORT_RUN_STATE_MAX_ENTRIES, |
| 13 | + MEMORY_WIKI_IMPORT_RUN_STATE_NAMESPACE, |
| 14 | + readLegacyMemoryWikiImportRunRecords, |
| 15 | + resolveMemoryWikiImportRunsDir, |
| 16 | + writeMemoryWikiImportRunRecord, |
| 17 | +} from "./src/import-runs-state.js"; |
7 | 18 | import { |
8 | 19 | createMemoryWikiSourceSyncStateStore, |
9 | 20 | MEMORY_WIKI_SOURCE_SYNC_STATE_MAX_ENTRIES, |
@@ -52,24 +63,61 @@ async function fileExists(filePath: string): Promise<boolean> { |
52 | 63 |
|
53 | 64 | async function archiveLegacySource(params: { |
54 | 65 | filePath: string; |
| 66 | + label: string; |
55 | 67 | changes: string[]; |
56 | 68 | warnings: string[]; |
57 | 69 | }): Promise<void> { |
58 | 70 | const archivedPath = `${params.filePath}.migrated`; |
59 | 71 | if (await fileExists(archivedPath)) { |
60 | 72 | params.warnings.push( |
61 | | - `Left migrated Memory Wiki source-sync source in place because ${archivedPath} already exists`, |
| 73 | + `Left migrated ${params.label} in place because ${archivedPath} already exists`, |
62 | 74 | ); |
63 | 75 | return; |
64 | 76 | } |
65 | 77 | try { |
66 | 78 | await fs.rename(params.filePath, archivedPath); |
67 | | - params.changes.push(`Archived Memory Wiki source-sync legacy source -> ${archivedPath}`); |
| 79 | + params.changes.push(`Archived ${params.label} -> ${archivedPath}`); |
68 | 80 | } catch (err) { |
69 | | - params.warnings.push(`Failed archiving Memory Wiki source-sync legacy source: ${String(err)}`); |
| 81 | + params.warnings.push(`Failed archiving ${params.label}: ${String(err)}`); |
70 | 82 | } |
71 | 83 | } |
72 | 84 |
|
| 85 | +async function archiveLegacyImportRunRecords(params: { |
| 86 | + vaultRoot: string; |
| 87 | + changes: string[]; |
| 88 | + warnings: string[]; |
| 89 | +}): Promise<void> { |
| 90 | + const importRunsDir = resolveMemoryWikiImportRunsDir(params.vaultRoot); |
| 91 | + const entries = await fs |
| 92 | + .readdir(importRunsDir, { withFileTypes: true }) |
| 93 | + .catch((error: unknown) => { |
| 94 | + if (isRecord(error) && error.code === "ENOENT") { |
| 95 | + return []; |
| 96 | + } |
| 97 | + throw error; |
| 98 | + }); |
| 99 | + for (const entry of entries) { |
| 100 | + if (!entry.isFile() || !entry.name.endsWith(".json")) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + await archiveLegacySource({ |
| 104 | + filePath: path.join(importRunsDir, entry.name), |
| 105 | + label: "Memory Wiki import-run legacy record", |
| 106 | + changes: params.changes, |
| 107 | + warnings: params.warnings, |
| 108 | + }); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +function countImportRunStateRows( |
| 113 | + records: Array<{ createdPaths: string[]; updatedPaths: unknown[] }>, |
| 114 | +): number { |
| 115 | + return records.reduce( |
| 116 | + (total, record) => total + 1 + record.createdPaths.length + record.updatedPaths.length, |
| 117 | + 0, |
| 118 | + ); |
| 119 | +} |
| 120 | + |
73 | 121 | export const stateMigrations: PluginDoctorStateMigration[] = [ |
74 | 122 | { |
75 | 123 | id: "memory-wiki-source-sync-json-to-plugin-state", |
@@ -131,7 +179,68 @@ export const stateMigrations: PluginDoctorStateMigration[] = [ |
131 | 179 | changes.push( |
132 | 180 | `Migrated Memory Wiki source sync -> plugin state (${importedCount} imported, ${existingCount} existing)`, |
133 | 181 | ); |
134 | | - await archiveLegacySource({ filePath, changes, warnings }); |
| 182 | + await archiveLegacySource({ |
| 183 | + filePath, |
| 184 | + label: "Memory Wiki source-sync legacy source", |
| 185 | + changes, |
| 186 | + warnings, |
| 187 | + }); |
| 188 | + } |
| 189 | + return { changes, warnings }; |
| 190 | + }, |
| 191 | + }, |
| 192 | + { |
| 193 | + id: "memory-wiki-import-runs-json-to-plugin-state", |
| 194 | + label: "Memory Wiki import run records", |
| 195 | + async detectLegacyState(params) { |
| 196 | + const previews: string[] = []; |
| 197 | + for (const vaultRoot of resolveConfiguredVaultRoots({ |
| 198 | + config: params.config, |
| 199 | + env: params.env, |
| 200 | + })) { |
| 201 | + const records = await readLegacyMemoryWikiImportRunRecords(vaultRoot); |
| 202 | + if (records.length === 0) { |
| 203 | + continue; |
| 204 | + } |
| 205 | + previews.push( |
| 206 | + `- Memory Wiki import runs: ${resolveMemoryWikiImportRunsDir(vaultRoot)}/*.json -> plugin state (${MEMORY_WIKI_IMPORT_RUN_STATE_NAMESPACE}, ${records.length} records)`, |
| 207 | + ); |
| 208 | + } |
| 209 | + return previews.length > 0 ? { preview: previews } : null; |
| 210 | + }, |
| 211 | + async migrateLegacyState(params) { |
| 212 | + const changes: string[] = []; |
| 213 | + const warnings: string[] = []; |
| 214 | + const store = createMemoryWikiImportRunStateStore(params.context.openPluginStateKeyedStore); |
| 215 | + for (const vaultRoot of resolveConfiguredVaultRoots({ |
| 216 | + config: params.config, |
| 217 | + env: params.env, |
| 218 | + })) { |
| 219 | + const records = await readLegacyMemoryWikiImportRunRecords(vaultRoot); |
| 220 | + if (records.length === 0) { |
| 221 | + continue; |
| 222 | + } |
| 223 | + const existingRecords = await listMemoryWikiImportRunRecords(vaultRoot, store); |
| 224 | + const existingRunIds = new Set(existingRecords.map((record) => record.runId)); |
| 225 | + const importedRecords = records.filter((record) => !existingRunIds.has(record.runId)); |
| 226 | + const nextRowCount = |
| 227 | + (await countMemoryWikiImportRunStateRows(store)) + |
| 228 | + countImportRunStateRows(importedRecords); |
| 229 | + if (nextRowCount > MEMORY_WIKI_IMPORT_RUN_STATE_MAX_ENTRIES) { |
| 230 | + warnings.push( |
| 231 | + `Skipped Memory Wiki import-run import for ${vaultRoot}: ${nextRowCount} state rows exceeds ${MEMORY_WIKI_IMPORT_RUN_STATE_MAX_ENTRIES}`, |
| 232 | + ); |
| 233 | + continue; |
| 234 | + } |
| 235 | + let importedCount = 0; |
| 236 | + for (const record of importedRecords) { |
| 237 | + await writeMemoryWikiImportRunRecord(vaultRoot, record, store); |
| 238 | + importedCount += 1; |
| 239 | + } |
| 240 | + changes.push( |
| 241 | + `Migrated Memory Wiki import runs -> plugin state (${importedCount} imported, ${existingRunIds.size} existing)`, |
| 242 | + ); |
| 243 | + await archiveLegacyImportRunRecords({ vaultRoot, changes, warnings }); |
135 | 244 | } |
136 | 245 | return { changes, warnings }; |
137 | 246 | }, |
|
0 commit comments