Skip to content

Commit d8e002f

Browse files
committed
fix(matrix): keep unreadable legacy dedupe JSON in place during doctor import
1 parent f84d207 commit d8e002f

3 files changed

Lines changed: 49 additions & 6 deletions

File tree

‎extensions/matrix/doctor-contract-api.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,35 @@ describe("matrix doctor contract state migrations", () => {
440440
await expect(migration.detectLegacyState(createMigrationParams(stateDir))).resolves.toBeNull();
441441
});
442442

443+
it("archives malformed inbound dedupe JSON without importing it", async () => {
444+
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-matrix-doctor-"));
445+
tempDirs.push(stateDir);
446+
const jsonRoot = path.join(
447+
stateDir,
448+
"matrix",
449+
"accounts",
450+
"home",
451+
"matrix.example.org__bot",
452+
"token-a",
453+
);
454+
fs.mkdirSync(jsonRoot, { recursive: true });
455+
const jsonPath = path.join(jsonRoot, "inbound-dedupe.json");
456+
fs.writeFileSync(jsonPath, "not-json");
457+
458+
const migration = migrationById("matrix-inbound-dedupe-to-claimable-dedupe");
459+
await expect(migration.migrateLegacyState(createMigrationParams(stateDir))).resolves.toEqual({
460+
changes: [
461+
"Migrated Matrix inbound dedupe markers to the claimable dedupe store (0 of 0 entries)",
462+
`Archived Matrix inbound dedupe legacy source -> ${jsonPath}.migrated`,
463+
],
464+
warnings: [
465+
`Matrix inbound dedupe JSON for ${jsonRoot} is malformed; archived without import`,
466+
],
467+
});
468+
expect(fs.existsSync(jsonPath)).toBe(false);
469+
expect(fs.existsSync(`${jsonPath}.migrated`)).toBe(true);
470+
});
471+
443472
it("keeps newer runtime dedupe rows when legacy imports hit capacity", async () => {
444473
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-matrix-doctor-"));
445474
tempDirs.push(stateDir);

‎extensions/matrix/doctor-contract-api.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,16 @@ export const stateMigrations: PluginDoctorStateMigration[] = [
202202
const jsonRootsToRetire: string[] = [];
203203
for (const storageRootDir of sources.jsonRoots) {
204204
try {
205-
gathered.push(...(await readLegacyInboundDedupeJsonSource(storageRootDir)));
205+
const markers = await readLegacyInboundDedupeJsonSource(storageRootDir);
206+
if (markers === null) {
207+
// Nothing recoverable, but archiving (rename, not delete) resolves
208+
// the pending detection while preserving the bytes for inspection.
209+
warnings.push(
210+
`Matrix inbound dedupe JSON for ${storageRootDir} is malformed; archived without import`,
211+
);
212+
} else {
213+
gathered.push(...markers);
214+
}
206215
jsonRootsToRetire.push(storageRootDir);
207216
} catch (err) {
208217
warnings.push(

‎extensions/matrix/src/matrix/monitor/inbound-dedupe-migration.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,23 +186,28 @@ async function resolveJsonRootAccountId(storageRootDir: string): Promise<string>
186186
return "default";
187187
}
188188

189-
/** Reads one storage root's legacy inbound-dedupe.json markers; throws on read errors. */
189+
/**
190+
* Reads one storage root's legacy inbound-dedupe.json markers. Throws on file
191+
* read errors so a transiently unreadable file is never retired unread, and
192+
* returns null for malformed content so the caller can archive it explicitly.
193+
*/
190194
export async function readLegacyInboundDedupeJsonSource(
191195
storageRootDir: string,
192-
): Promise<LegacyInboundDedupeMarker[]> {
196+
): Promise<LegacyInboundDedupeMarker[] | null> {
193197
const jsonPath = path.join(storageRootDir, MATRIX_LEGACY_INBOUND_DEDUPE_FILENAME);
198+
const raw = await fs.readFile(jsonPath, "utf8");
194199
let parsed: unknown;
195200
try {
196-
parsed = JSON.parse(await fs.readFile(jsonPath, "utf8")) as unknown;
201+
parsed = JSON.parse(raw) as unknown;
197202
} catch {
198-
return [];
203+
return null;
199204
}
200205
if (
201206
!isRecord(parsed) ||
202207
parsed.version !== LEGACY_JSON_VERSION ||
203208
!Array.isArray(parsed.entries)
204209
) {
205-
return [];
210+
return null;
206211
}
207212
const accountId = await resolveJsonRootAccountId(storageRootDir);
208213
const markers: LegacyInboundDedupeMarker[] = [];

0 commit comments

Comments
 (0)