fix(hermes): superseded v1 ledger file was left on disk forever

The loader reads only the v3 path, so a hermes-session-ledger.v1.json from an
earlier install sat in the cache dir unread and undeleted. It is dead weight
that can only mislead: the parse-version bump forces a cold re-parse that
rebuilds every cursor from source, so nothing in it is recoverable. Unlink it
when the v3 file is absent.

The v2 test this replaces was vacuous. It wrote a v2 file and asserted the
ledger was empty, but the loader never looks at a v2 path, so it passed
whatever the code did. The replacement uses a real v1 fixture and asserts both
halves: the ledger loads empty AND the v1 file is gone afterwards.
This commit is contained in:
iamtoruk 2026-09-02 07:32:02 -07:00
parent 9c432bb105
commit a8bddef84e
2 changed files with 23 additions and 7 deletions

View file

@ -1,5 +1,5 @@
import { open, rename, unlink, mkdir } from 'fs/promises'
import { existsSync, readFileSync } from 'fs'
import { existsSync, readFileSync, unlinkSync } from 'fs'
import { randomBytes } from 'crypto'
import { join } from 'path'
@ -349,7 +349,17 @@ function validateLedger(raw: unknown): raw is HermesSessionLedger {
function readLedgerFromDisk(): HermesSessionLedger {
const path = hermesSessionLedgerPath()
if (!existsSync(path)) return emptyHermesSessionLedger()
if (!existsSync(path)) {
// The v1 file is superseded and can never be read again: the parse-version
// bump forces a cold re-parse that rebuilds every cursor from source.
// Left behind it is dead bytes that only invite a future reader to trust
// observations recorded under an accounting this branch replaced.
const v1 = join(getCodeburnCacheDir(), 'hermes-session-ledger.v1.json')
if (existsSync(v1)) {
try { unlinkSync(v1) } catch { /* another process got there first */ }
}
return emptyHermesSessionLedger()
}
try {
const parsed: unknown = JSON.parse(readFileSync(path, 'utf-8'))
if (!validateLedger(parsed)) return emptyHermesSessionLedger()