Skip to content

Commit adbc9d3

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 4a0799b commit adbc9d3

2 files changed

Lines changed: 20 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
@@ -400,15 +400,16 @@ describe("changeJournal", () => {
400400
expect(result[0].checkpointId).toBe("ok")
401401
})
402402

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

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

409-
const result = await loadChanges(tmpRoot, taskId)
410-
expect(result).toHaveLength(1)
411-
expect(result[0].checkpointId).toBe("ok")
412+
await expect(loadChanges(tmpRoot, taskId)).rejects.toThrow(/Corrupt change journal entry at line 2/)
412413
})
413414

414415
it("skips a corrupt middle line and still loads the later valid entries", async () => {
@@ -427,13 +428,15 @@ describe("changeJournal", () => {
427428
expect(result[0].checkpointId).toBe("after")
428429
})
429430

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

433-
// Truncate to a single character — definitely invalid JSON.
434+
// Truncate to a single character — definitely invalid JSON. The
435+
// append path repairs such a tail on the next write; a read must
436+
// not report the lost record as a complete (empty) history.
434437
await fs.writeFile(journalPath(tmpRoot, taskId), "{")
435438

436-
expect(await loadChanges(tmpRoot, taskId)).toEqual([])
439+
await expect(loadChanges(tmpRoot, taskId)).rejects.toThrow(/Corrupt change journal entry at line 1/)
437440
})
438441

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

‎src/core/checkpoints/changeJournal.ts‎

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

0 commit comments

Comments
 (0)