feat(savings-ledger): record lean-ctx version per event (v4) - #710
Merged
Conversation
SavingsEvent had no way to say which lean-ctx version recorded a given saving. stats.json (the display cache) tracked a per-day version tag separately, but if that cache is ever lost or corrupted, rebuilding it from the append-only savings ledger (the actual source of truth) couldn't recover the version column at all. Adds `version: String` (CARGO_PKG_VERSION at append time), following the same additive, backward-compatible pattern already used for the v2->v3 `mechanism` field: serde default for pre-v4 entries (empty, not a guessed version), a new v4 canonical-content hash that commits it, and the v3 canonical kept intact so old ledger entries keep verifying unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SavingsEventhad no way to say which lean-ctx version recorded a given saving.stats.json(the display cachelean-ctx gain --dailyreads) tracks a per-day version tag separately, but if that cache is ever lost or corrupted, rebuilding it from the append-only savings ledger (the real source of truth) can't recover the version column at all — there's nothing in the ledger to recover it from.What
version: Stringadded toSavingsEvent, populated withCARGO_PKG_VERSIONat append time (savings_ledger/mod.rs::new_event).canonical_content_v3(the pre-version format) kept intact so existing ledger entries keep verifying unchanged.hash_matchesnow checks v4, v3, v2, and the legacy v1 format, same as before plus one more tier.versiondefaulting to an empty string via serde, not a guessed current version — same convention already used byDayStats::versionincore/stats/model.rsfor the same "unknown, don't fabricate" reason.This follows the exact pattern this file already used for v2→v3 (the
mechanismfield, enterprise#19): additive field, versioned canonical-content format, old canonical method kept around, backward-compat regression test mirroring the existing one for that transition.Why
Hit this directly:
stats.jsongot corrupted (unrelated incident, a Windows-encoding bug in a different tool that truncated it mid-write) and had to be rebuilt by replayingsavings/ledger.jsonl. Everything reconstructed cleanly except the per-day version tag, because the ledger never recorded it. This closes that gap for next time.Test Plan
All pass locally.
v3_hash_still_verifies_and_v4_commits_versionversionkey in JSON) still verify viacanonical_content_v3; a v4 entry's hash breaks ifversionis rewrittenhash_is_deterministic,hash_changes_when_content_changes,hash_depends_on_prevlegacy_v1_hash_still_verifies,v2_hash_still_verifies_and_v3_commits_mechanismBackward Compatibility
No schema break. Existing ledger files verify unchanged (v1/v2/v3 canonical forms all still checked by
hash_matches). New appends use v4. Not a CLI/MCP tool surface change — no registry/schema/docs updates needed.Related Issue
No prior issue — small, mechanical, additive change following an established in-file pattern; happy to open one retroactively if you'd rather have that paper trail before merging.