Repository navigation
fix(query): rank chained price paths by evidence, not by spelling - #2158
Merged
Merged
Conversation
Two of the three items in #2152. The third is left deliberately, below. `get_chained_price` documents that "intermediates are tried in sorted order so the result is deterministic when several paths exist". Nothing tested it. The three existing chained-price tests each build a single chain, so the sort could have been dropped in a refactor and all three would still pass. Both `get_chained_price` and `get_chained_latest_price` carry their own copy of the sort line, so both were unheld. The new test builds two complete same-dated paths worth deliberately different amounts, 20.00 via AAA and 50.00 via MMM, and asserts the alphabetically first wins in both functions. Equal-valued paths would have passed whichever way iteration went, which is the property being detected. It also rebuilds the database eight times, because the underlying map is a hash map and insertion-order luck shows up across constructions rather than within one run. Confirmed it bites, once per function: removing the sort from `get_chained_price` fails it, and so does removing the sort from `get_chained_latest_price`. Two copies, two independent proofs. The doc now says what the guarantee is worth. Deterministic is not principled: 20.00 wins because `AAA` sorts before `MMM`, and nothing in the output mentions that 50.00 was equally available. A reader of the old sentence could reasonably assume the choice meant something. It also records that legs are dated independently, so a 2010 leg times a 2024 leg yields a current valuation with no diagnostic. NOT fixing the staleness half. There is no precedent to align with: the only age concept in the tree is the price-FETCH cache, which governs how long a downloaded quote is reused rather than whether a rate is too old to value with. A bound would be new user-facing policy -- someone has to choose a window, choose error versus warn, and choose whether reports differ -- and that is a decision to make deliberately, not to smuggle in as a bug fix. #2152 stays open for it. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Replaces the alphabetical tie-break in chained lookups. When several one-hop routes exist, the winner is now the one whose OLDEST leg is most recent, then the one whose legs sit closest together, then the currency name so the order stays total and independent of hash iteration. A chain is only as current as its oldest leg. Multiplying a 2010 `A -> B` by a 2024 `B -> C` yields a figure that looks 2024-dated and rests on 2010 data, so ranking by that date is what makes the chosen path the best-supported one rather than the alphabetically luckiest. The corpus made the case. Chaining is needed in 54 of 761 files across 1152 pairs, and multi-path ambiguity reaches only 2 of them -- but the real one is `ledger_prices.beancount`, BTC to CNY via USD or USDT, where the answers are 125,856 and 74,763 CNY. A 68% spread, and the paths do not disagree about BTC: one route's quotes stop 16 months earlier. We returned the right number because `USD` sorts before `USDT`. A currency named `AUSDT` would have flipped it. Both lookups now share one ranking through `best_chain`; the latest-rate variant used to carry its own copy of the sort and could drift from the as-of one. NOT refusing stale chains, after trying it. beancount's `prices.project()` anchors its second leg to the first leg's date and declines when no rate existed then, and porting that rule here was wrong for two reasons. It is built for a different question -- `project` synthesizes a price SERIES at the base commodity's own dates, while this answers "value as of D" -- and it refuses when the second leg is merely NEWER, which broke `test_chained_lookup_uses_newest_rate_per_leg`: valuing an 07-10 close with an 07-11 rate is ordinary, not suspect. More decisively, a stale DIRECT price is returned here without complaint, so refusing only the chained case would be an inconsistent rule rather than a stricter one. Verified: the era-mixing fixture from #2152 is unchanged in value, the freshness case now picks the 2020 route over the alphabetically-first 2019 one, and the booked-value gate against beancount reports `known 34, found 34, new 0` across 761 files, so no divergence moved. Both properties are pinned and both proofs are real: ranking the name ahead of freshness fails with `left: 74763, right: 125856`, and removing the name tie-break fails the stability assertions. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
📊 Benchmark Results
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
Reading the finished diff turned up a doc describing a design this branch tried and abandoned. `lookup_dated_at` said the chained lookup needs the rate's date "so it can value the second leg as of that same moment". That was true of the anchoring attempt and false once it was backed out: both legs are dated the caller's way now, and the date is used for RANKING paths rather than for pinning one leg to the other. A reader would have gone looking for anchoring logic that is not there. Same defect as the one an automated reviewer caught on #2155 and I caught on #2157: a comment that merely restates code is noise, but one explaining why something must be a certain way, after that stopped being true, actively misleads. Also folds two near-verbatim copies of the ranking rationale into one. `get_chained_price` and `best_chain` each carried the full explanation of path selection and the stale-chain policy; the caller now points at the function that owns the logic. Two copies of an argument drift the same way two copies of a sort do, which is what this branch already fixed in the code. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #2152, except the general staleness question, which the research below argues is not specific to chaining.
The PR changed shape partway through. It started as tests-and-docs pinning the existing alphabetical tie-break; measuring the problem showed that tie-break was the wrong behaviour to pin.
What the corpus says
Across all 761 corpus files:
The tie-break reaches two pairs in the whole corpus. But the real one is
ledger_prices.beancount, BTC to CNY:A 68% spread, and the paths do not disagree about BTC. One route's quotes stop 16 months earlier. We returned the better number because
USDsorts beforeUSDT-- right answer, wrong reason, and a currency namedAUSDTwould have flipped it.The change
A chain is only as current as its oldest leg: a 2010
A -> Btimes a 2024B -> Cproduces a figure that looks 2024-dated and rests on 2010 data. So paths are now ranked byBoth lookups now share one ranking through
best_chain. The latest-rate variant previously carried its own copy of the sort and could drift from the as-of one.What I tried and backed out
I first ported beancount's
prices.project()rule: anchor leg 2 to leg 1's date, and decline if no rate existed then. It is genuinely more conservative, and I had verified it refuses this issue's era-mixing fixture where we answer20.00 USD.A pre-existing test stopped it, correctly:
test_chained_lookup_uses_newest_rate_per_leg. Two reasons it was the wrong rule to import.Different question.
project()synthesises a price SERIES at the base commodity's own dates, so "no FX rate on 07-10" is coherent for it.get_chained_price(base, quote, date)answers "value as of D". Under the ported rule we refused when leg 2 was merely NEWER -- valuing an 07-10 close with an 07-11 exchange rate -- which is ordinary, not suspect. (beancount refuses that too; it is right forprojectand wrong here.)Inconsistent. A stale DIRECT price is returned without complaint:
get_price(XCOIN, BRIDGE, 2024)returns the 2010 rate and nobody objects. Refusing only the chained case would be an inconsistent rule rather than a stricter one. Staleness is a general concern, not a chaining one, and #2152 stays open for it.Prior art
get_priceproject()hledger returns
20.00 USDon the era-mixing fixture, identical to us, so we were never an outlier on leg dating. We were an outlier on the tie-break: hledger ranks by chain length and forward-over-reverse, a principled order, not by spelling.Verification
known 34, found 34, new 0across 761 files. No divergence moved.left: Some(74763.2350), right: Some(125856.951660), and removing the name tie-break fails the stability assertions.RUSTDOCFLAGS="-D warnings" cargo docclean (this PR is doc-heavy andcargo testdoes not catch rustdoc errors),typosclean, workspace builds.🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr