Skip to content

Commit e935afc

Browse files
committed
fix(checkpoints): reject an unverified final journal record on read
loadChanges discarded a parse failure on the final line as a torn tail without evidence. A corrupted (not merely torn) final record then made rollback report an incomplete history as valid: restore-latest no-oped over the lost write and returned success. Torn-tail repair is verified truncation owned by the append path (it re-terminates or truncates the tail before writing); a read now rejects instead, so rollbackFile, rollbackStep, and restoreLatestFile surface an explicit failure without restoring. Regression tests: a torn final line and an entirely torn journal reject at load; corrupt final records fail restore-latest, rollbackFile, and rollbackStep without a restore call. Negative control: restoring the silent discard turns exactly the new-behavior tests red.
1 parent 42dace5 commit e935afc

3 files changed

Lines changed: 52 additions & 11 deletions

File tree

‎src/core/checkpoints/__tests__/changeJournal.spec.ts‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,16 @@ describe("changeJournal", () => {
410410
expect(result[0].checkpointId).toBe("ok")
411411
})
412412

413-
it("discards a torn final line and returns the complete entries", async () => {
413+
it("rejects a torn final line instead of silently dropping it", async () => {
414414
await appendChange(tmpRoot, taskId, entry({ checkpointId: "ok" }))
415415

416-
// Append a second line truncated mid-content, with no trailing newline.
416+
// Append a second line truncated mid-content, with no trailing
417+
// newline. Without verified truncation there is no evidence this
418+
// is a crash tail rather than a corrupted record, and silently
419+
// dropping it would report an incomplete history as valid.
417420
await fs.appendFile(journalPath(tmpRoot, taskId), '{"path":"src/half.ts","operation":"upd')
418421

419-
const result = await loadChanges(tmpRoot, taskId)
420-
expect(result).toHaveLength(1)
421-
expect(result[0].checkpointId).toBe("ok")
422+
await expect(loadChanges(tmpRoot, taskId)).rejects.toThrow(/Corrupt change journal entry at line 2/)
422423
})
423424

424425
it("skips a corrupt middle line and still loads the later valid entries", async () => {
@@ -437,13 +438,15 @@ describe("changeJournal", () => {
437438
expect(result[0].checkpointId).toBe("after")
438439
})
439440

440-
it("does not throw when the entire journal is torn", async () => {
441+
it("rejects a journal whose only record is torn", async () => {
441442
await appendChange(tmpRoot, taskId, entry({ checkpointId: "first" }))
442443

443-
// Truncate to a single character — definitely invalid JSON.
444+
// Truncate to a single character — definitely invalid JSON. The
445+
// append path repairs such a tail on the next write; a read must
446+
// not report the lost record as a complete (empty) history.
444447
await fs.writeFile(journalPath(tmpRoot, taskId), "{")
445448

446-
expect(await loadChanges(tmpRoot, taskId)).toEqual([])
449+
await expect(loadChanges(tmpRoot, taskId)).rejects.toThrow(/Corrupt change journal entry at line 1/)
447450
})
448451

449452
it("includes diffStats when present", async () => {

‎src/core/checkpoints/__tests__/rollback.spec.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,38 @@ describe("restoreLatestFile (B3c: forward direction)", () => {
429429
expect(service.restoreFile).not.toHaveBeenCalled()
430430
})
431431

432+
it("fails restore-latest on a corrupt final journal record without restoring", async () => {
433+
// A committed write whose final journal record was truncated or
434+
// corrupted must surface as an explicit failure: reporting a
435+
// successful no-op would hide that rollback metadata is incomplete.
436+
await seedJournal([{ path: "src/a.ts", operation: "update", checkpointId: "sha-1" }])
437+
await fs.appendFile(journalPath(globalStorageDir, "task-rollback"), '{"path":"src/a.ts","ope')
438+
const service = serviceWith("base-0")
439+
mockedGetCheckpointService.mockResolvedValue(service)
440+
441+
const outcome = await restoreLatestFile(makeTask(), "src/a.ts")
442+
443+
expect(outcome.success).toBe(false)
444+
expect(outcome.error).toContain("Corrupt change journal entry at line 2")
445+
expect(service.restoreFile).not.toHaveBeenCalled()
446+
})
447+
448+
it("fails rollbackFile and rollbackStep on a corrupt final journal record without restoring", async () => {
449+
await seedJournal([{ path: "src/a.ts", operation: "update", checkpointId: "sha-1" }])
450+
await fs.appendFile(journalPath(globalStorageDir, "task-rollback"), '{"path":"src/a.ts","ope')
451+
const service = serviceWith("base-0")
452+
mockedGetCheckpointService.mockResolvedValue(service)
453+
454+
const single = await rollbackFile(makeTask(), "sha-1", "src/a.ts")
455+
expect(single.success).toBe(false)
456+
expect(single.error).toContain("Corrupt change journal entry at line 2")
457+
458+
const step = await rollbackStep(makeTask(), ["src/a.ts"], "sha-1")
459+
expect(step.files[0].success).toBe(false)
460+
expect(step.files[0].error).toContain("Corrupt change journal entry at line 2")
461+
expect(service.restoreFile).not.toHaveBeenCalled()
462+
})
463+
432464
it("fails cleanly when checkpoints are not enabled", async () => {
433465
mockedGetCheckpointService.mockResolvedValue(undefined)
434466

‎src/core/checkpoints/changeJournal.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,15 @@ export async function loadChanges(globalStorageDir: string, taskId: string): Pro
268268
}
269269
} catch {
270270
// A corrupt line before the final line (e.g. a partially flushed
271-
// append) must not hide the valid entries after it. The final line
272-
// is still discarded as a torn tail — `continue` at the last index
273-
// ends the loop either way.
271+
// append) must not hide the valid entries after it. The final
272+
// line is different: dropping an unverified record there can
273+
// report an incomplete rollback history as valid (restore-latest
274+
// would no-op over the lost write), and torn-tail repair is
275+
// verified truncation owned by the append path, so a read
276+
// rejects instead.
277+
if (i === lines.length - 1) {
278+
throw new Error(`Corrupt change journal entry at line ${i + 1} of ${filePath}`)
279+
}
274280
continue
275281
}
276282
}

0 commit comments

Comments
 (0)