Skip to content

fix: refuse an ambiguous entry hash instead of picking a winner - #273

Merged
robcohen merged 1 commit into
mainfrom
fix/entry-hash-ambiguity
Aug 8, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/entry-hash-ambiguity

Conversation

@robcohen

@robcohen robcohen commented Aug 7, 2026

Copy link
Copy Markdown
Member

The consumer half of rustledger#1968. Pairs with rustledger#1984, but stands
alone — it is a fix regardless of what that hash contains.

The bug

get_entry is how every mutating endpoint finds the line it is about to
rewrite or delete: save_entry_slice, delete_entry_slice, insert_metadata.
It was:

next(e for e in self.all_entries if entry_hash == hash_entry(e))

First match wins — and the docstring above it asserted the result was "the
(unique) entry" without anything checking that.

Nothing guarantees uniqueness. Two transactions identical in every field
rustledger hashes collide, and duplicates like that are ordinary in real ledgers
(two identical coffees on the same day). When it happens, the UI edits a
different transaction than the one the user clicked, with no indication
anything went wrong.

Two of the three mutating paths carry a second factor — the sha256sum of the
displayed source slice — so they fail with "changed externally" rather than
corrupting anything. insert_metadata carries none, and it is the endpoint
most likely to be operating on entries that differ only by metadata.

The fix

Collect all matches; raise AmbiguousEntryHashError on more than one. A silent
wrong-target edit becomes a visible error.

Deliberately not short-circuiting — knowing whether a hash is ambiguous means
looking at every entry, so this is O(n) where it used to average less.
hash_entry is a dict lookup on rustledger entries, so that's microseconds even
on a large ledger, and the alternative is being fast at editing the wrong line.

The message names the remedy, not just the dead end: adding a metadata key to
one of the two makes them distinct (true as of rustledger#1984). An error that
only tells the user they're stuck sends them back to the same click.

Verification

Sabotage-verified twice — restoring first-match-wins fails the test, and so does
dropping the remedy from the message.

The test duplicates the same object rather than constructing a lookalike,
which is the strongest form of the case: no hash function over directive content
can tell those apart, so it can't be papered over by hashing more fields.

Full suite: 665 passed, 1 skipped. The two locale failures
(test_get_translations, test_fava_options_language) reproduce identically on
pristine origin/main — no compiled .mo files when running from src/.

Two incidental fixes in the same file, both pre-existing and both flagged by ruff
only because this commit touches it: an unused to_string import (verified
nothing imports it from rustfava.core) and an out-of-order is_encrypted_file
import.

`get_entry` is how every mutating endpoint finds the line it is about
to rewrite or delete - save_entry_slice, delete_entry_slice,
insert_metadata. It was:

    next(e for e in self.all_entries if entry_hash == hash_entry(e))

First match wins, and the docstring above it asserted the result was
"the (unique) entry" without anything checking that. Nothing guarantees
uniqueness: two transactions identical in every field rustledger hashes
collide, and duplicates like that are ordinary in real ledgers. When
that happens the UI edits a DIFFERENT transaction than the one the user
clicked, with no indication anything went wrong.

Two of the three mutating paths have a second factor - the sha256sum of
the displayed source slice - so they fail with "changed externally"
rather than corrupting anything. `insert_metadata` has none, and it is
the endpoint most likely to be operating on entries that differ only by
metadata.

Now collects all matches and raises `AmbiguousEntryHashError`, turning
a silent wrong-target edit into a visible error. Deliberately not
short-circuiting: knowing whether a hash is ambiguous means looking at
every entry, so this is O(n) where it used to average less. `hash_entry`
is a dict lookup on rustledger entries, so that is microseconds even on
a large ledger - and the alternative is being fast at editing the wrong
line.

The message names the remedy rather than just the dead end: adding a
metadata key to one of the two makes them distinct. That is true as of
rustledger#1984, which puts metadata into the hash - an error telling
the user only that they are stuck sends them back to the same click.

Sabotage-verified twice: restoring first-match-wins fails the test, and
so does dropping the remedy from the message.

The test duplicates the SAME object rather than constructing a
lookalike, which is the strongest form of the case - no hash function
over directive content can tell those apart, so it cannot be papered
over by hashing more fields.

Two incidental fixes in the same file, both pre-existing and both
flagged by ruff only because this commit touches it: an unused
`to_string` import (verified nothing imports it from `rustfava.core`)
and an out-of-order `is_encrypted_file` import.
@robcohen

robcohen commented Aug 7, 2026

Copy link
Copy Markdown
Member Author

Self-review — one risk this change surfaces rather than creates

Refusing an ambiguous hash is right, but it converts a silent wrong-target edit into a hard failure, and it's worth being explicit about where that failure will actually land.

hash_entry falls back when an entry has no meta["hash"] — i.e. entries built inside rustfava rather than loaded through rustledger:

if hasattr(entry, "__dataclass_fields__"):
    content = f"{type(entry).__name__}|{entry.date}|{getattr(entry, 'account', '')}"
    return hashlib.sha256(content.encode()).hexdigest()[:16]

A Transaction has no .account, so that reduces to Transaction|2026-08-07| for every transaction on a date — narration, postings and amounts all absent, then truncated to 16 hex chars on top. Any two such entries on the same day collide by construction.

Before this PR those collisions resolved to the first match and nobody saw anything. After it, they raise. That is the correct direction — the alternative was editing the wrong transaction — but it means the fallback's weakness stops being invisible, and it will show up as AmbiguousEntryHashError on entries the user can't obviously disambiguate (the remedy in the message, adding a metadata key, doesn't apply to an entry that isn't in the file yet).

The window is narrow in practice: entries created through the add-entry flow are written to the file and picked up with real hashes on reload. But it is not empty, and extensions can hold non-persisted entries.

Worth doing as a follow-up rather than in this PR: give the fallback the fields that actually distinguish entries — narration, payee, the postings — and drop the [:16] truncation, which is throwing away 48 hex chars of a digest for no benefit. That way the two paths have comparable collision behavior instead of one being orders of magnitude weaker than the other.

Happy to fold it in here if you'd rather it not be a separate PR.

@robcohen
robcohen merged commit c722771 into main Aug 8, 2026
31 checks passed
@robcohen
robcohen deleted the fix/entry-hash-ambiguity branch August 8, 2026 23:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant