Skip to content

fix(price): late Copilot review on PR #983 (commodity as-of + ordered attempts) - #984

Merged
robcohen merged 3 commits into
mainfrom
fix/price-discovery-asof-commodity-and-harness
May 3, 2026
Merged

robcohen merged 3 commits into
mainfrom
fix/price-discovery-asof-commodity-and-harness

Conversation

@robcohen

@robcohen robcohen commented May 3, 2026

Copy link
Copy Markdown
Member

Summary

PR #983 merged before three Copilot inline comments arrived. Following up here.

What's in this PR

1. Commodity directive walk now respects --date

as_of was only applied to the active-balance helper in discovery.rs; the Commodity directive walk itself was unfiltered. So a commodity declared after the cutoff (e.g. 2030-01-01 commodity FUTURECOIN with price: metadata) still triggered discovery on rledger price --date 2024-06-01.

Fixed to match upstream beanprice/price.py::find_currencies_declared's rule:

if date and entry.date >= date:
    break  # commodities declared on or after cutoff excluded

Verified against the actual upstream source. Comparison is exclusive (>= skips), so a commodity declared on the as-of date itself is excluded — matches bean-price exactly.

New test: as_of_filters_commodity_directives_before_walking_them.

2. Harness preserves fallback-chain order

The resolve-attempts comparison previously used BTreeSet<(symbol, currency, source, ticker)>, which threw away per-symbol ordering. A regression that swapped [ecbrates(GBP-EUR), ecb(GBP)] to [ecb(GBP), ecbrates(GBP-EUR)] would silently pass.

Switched to:

type Attempts = BTreeMap<(String, String), Vec<(String, String)>>;

BTreeMap keys keep cross-symbol ordering deterministic; the Vec value preserves chain order, so swaps fail the test.

3. Fallback-chain fixture: investigated, not feasible

Copilot's third comment asked for a fallback-chain fixture. Investigated — turns out bean-price and rledger have incompatible price: metadata syntaxes:

bean-price errors with Invalid source name: "EUR:ecb/GBP" when given rledger's syntax. Documented this in a comment where the fixture would otherwise live; chain order is unit-tested in cmd/price_cmd.rs::tests::describe_attempts_* instead.

The Vec-valued data structure remains load-bearing: when both binaries can speak the same chain syntax (extending rledger's parser to also accept bean-price's form is a follow-up), the harness will catch order regressions for free.

Test plan

  • cargo test -p rustledger --lib passes (262 + 1 new = 263)
  • cargo test -p rustledger --test bean_price_compat passes (5)
  • cargo clippy --all-features --all-targets -- -D warnings clean
  • cargo fmt --all -- --check clean
  • Pre-push hook ran the bean-price compat harness (passed)

PR #983 merged before three Copilot inline comments were addressed.
Following up here:

1. (discovery.rs) `as_of` was only applied to the active-balance walk;
   the Commodity directive walk was unfiltered, so a commodity declared
   after the cutoff with `price:` metadata still triggered discovery.
   Add the missing filter, matching beanprice's
   `find_currencies_declared`'s `entry.date >= date: break` rule
   (commodity declared ON OR AFTER cutoff is excluded). Verified
   against the actual upstream source.

   New test: as_of_filters_commodity_directives_before_walking_them.

2. (bean_price_compat.rs) The harness's resolve-attempts comparison
   used `BTreeSet`, which discarded fallback-chain order. A regression
   that swapped `[ecbrates(GBP-EUR), ecb(GBP)]` to
   `[ecb(GBP), ecbrates(GBP-EUR)]` would silently pass.

   Switch to `BTreeMap<(symbol, currency), Vec<(source, ticker)>>` —
   keys still sorted (deterministic comparison), value `Vec` preserves
   chain order so swaps fail the test.

3. (Copilot's third comment about adding fallback-chain fixtures)
   Investigated and documented as not currently feasible: bean-price
   parses `price:` as `<curr>:<src1>,<src2>,...` (one currency block
   with multiple sources), while rledger parses it as
   `<curr>:<src>/<ticker>,<curr>:<src>/<ticker>,...` (each entry
   repeats the currency, per #963/#970). The two formats are
   incompatible — bean-price errors on rledger's syntax with
   "Invalid source name". So the differential harness can't currently
   exercise rledger's fallback-chain behavior end-to-end.

   The `Vec`-valued data structure is still load-bearing: when both
   binaries can speak the same chain syntax (a follow-up to extend
   rledger's parser to also accept bean-price's form), the harness
   will catch order regressions for free. Documented this in a comment
   where the fixture would otherwise live.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner May 3, 2026 16:57
Copilot AI review requested due to automatic review settings May 3, 2026 16:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Follow-up fixes for the price command after PR #983 merged: ensures commodity discovery respects --date cutoffs consistently with upstream bean-price, and tightens the bean-price compatibility harness so fallback-chain ordering regressions are detectable.

Changes:

  • Filter Commodity directives by as_of (--date) during discovery so directives dated on/after the cutoff don’t influence historical fetch plans.
  • Change the compat harness’ resolved-attempt comparison from set semantics to an ordered per-(symbol,currency) chain representation.
  • Add a regression test covering the commodity-directive cutoff behavior, and document why fallback-chain fixtures aren’t currently feasible in the differential harness.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
crates/rustledger/tests/bean_price_compat.rs Preserves fallback-chain order in attempt comparisons by switching to a BTreeMap of ordered vectors.
crates/rustledger/src/cmd/price/discovery.rs Applies as_of filtering to commodity directives during discovery and adds a regression test for the cutoff semantics.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov

codecov Bot commented May 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.90722% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger/src/cmd/price/discovery.rs 96.90% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented May 3, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 30ms ± 0.8ms 21.5 MB
beancount 900.5ms ± 7.9ms 50.1 MB
Comparison 30x faster 2.3x less

vs main branch (2026-05-03)

Metric main this PR Change
Time 36.4ms 30ms -17.6% 🟢
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

@github-actions

github-actions Bot commented May 3, 2026 •

Copy link
Copy Markdown
Contributor

⚠️ Compatibility Test Results

Metric Result
Check Compatibility 99%
BQL Compatibility 91%
Full AST Match 99%
Total Files 782
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

robcohen and others added 2 commits May 3, 2026 11:07
…e syntaxes

After my previous comment claimed the formats were incompatible, the
maintainer pointed out that they could be unified. They were right.

Disambiguation rule: a comma-separated source entry is in rledger's
redundant per-entry form if and only if its part BEFORE the first `/`
contains `:`. Otherwise it's bean-price's bare `<source>/<ticker>` form
and inherits the surrounding currency block.

Strategy:
1. Split on whitespace/`;` → currency blocks (bean-price multi-currency)
2. Each block: `<currency>:<sources_part>`
3. sources_part: comma-separated entries, each either:
   - `<source>/<ticker>` (inherits block currency), or
   - `<currency>:<source>/<ticker>` (rledger's redundant form, overrides
     block currency)

Handles all of these natively without a mode flag:
  USD:yahoo/AAPL
  USD:yahoo/AAPL,google/AAPL                 (bean-price multi-source)
  USD:yahoo/AAPL,USD:google/AAPL             (rledger redundant)
  EUR:ecbrates/GBP-EUR,ecb/GBP               (bean-price chain)
  USD:yahoo/AAPL CAD:google/AAPL             (bean-price multi-currency)
  USD:google/NASDAQ:AAPL                     (ticker contains ':')

New parser tests cover each variant + the embedded-colon-in-ticker case.

Harness fallback fixture restored — both binaries now parse the same
chain syntax. Used yahoo/oanda for sources (both ship in beanprice's
sources/). Pass `--inactive` on both invocations so the resolution
test isn't gated on bean-price's discovery filter.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…urrency limitation

Two findings from a re-read of #984:

1. parse_price_metadata's match arm did a redundant double-split: the
   first match captured `before_slash` from `entry.split_once('/')` then
   immediately threw away its second component and re-split `entry` to
   recover an `after_first_colon`. Replaced with a single split that
   produces all three values (currency, source, ticker) directly. Same
   behavior, less work, easier to read.

2. Multi-currency `price:` metadata (e.g. `USD:yahoo/AAPL CAD:google/AAPL`)
   parses correctly into 2 specs, but downstream `build_mapping` and
   `DiscoveredCommodity.quote_currency` only retain the first spec's
   currency — so today rledger fetches a multi-currency commodity in
   only one of its declared quotes. Bean-price emits one job per
   (base, quote). Pre-existing limitation surfaced by my parser change;
   documented inline near the relevant test, not fixed here. Tracked
   as a follow-up.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@robcohen
robcohen merged commit 30856c3 into main May 3, 2026
30 checks passed
@robcohen
robcohen deleted the fix/price-discovery-asof-commodity-and-harness branch May 3, 2026 17:25
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.

2 participants