Repository navigation
feat: session.format renders held entries with ledger display precision (WIT 3.8.0) - #1808
Merged
Merged
Conversation
…on (WIT 3.8.0) The #1766 format surface: `session.format` renders the HELD directives to canonical beancount text honoring the ledger's per-currency display precision, built from the held entries plus the held options. - rustledger-core: `DisplayContext::from_directives` is now THE canonical builder (moved verbatim from the loader): amount-scan inference, then `option "display_precision"` overrides, then commodity `precision:` metadata. New `resolved_precisions()` exports the per-currency answer for the wire. The loader's `build_display_context` delegates. - rustledger-ffi-wasi: `PrecisionTracker` deleted — it was a re-derivation of DisplayContext inference that OVERWROTE the option-derived `display_precision`, so the user's explicit option never crossed the boundary, and the two load paths disagreed (`load_source` shipped inference-only, `load_file` option-only). `build_ledger_options` now takes the ledger's DisplayContext and ships the canonical resolution. - rustledger-ffi-component: WIT 3.8.0 + API_VERSION 3.8; `session.format` builds the context via `from_directives` and renders through the same `canonicalize_directives` path as the free format interface. Integer metadata now crosses the wire as `number`, not `text` — as text, a session round-trip turned `precision: 4` into `precision: "4"`. Part of #1766.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
📊 Benchmark Results
vs main branch (2026-07-18)
What do these numbers mean?
|
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an options-aware formatting surface to the WIT component so embedders can re-render held directives as canonical Beancount text using the same per-currency display precision resolution as the native loader/CLI, and fixes two cross-boundary precision/metadata round-trip bugs that prevented this from working correctly.
Changes:
- Introduces
session.formatin WITrustledger:[email protected]and wires it through the component to render held directives using a resolvedDisplayContext(inference →display_precisionoption → commodityprecision:metadata). - Fixes
ledger-options.display-precisioncrossing the FFI boundary by exporting the loader’s resolved per-currency precisions (removing the divergentPrecisionTrackerbehavior). - Fixes integer metadata round-tripping by encoding
MetaValue::Intas WITnumber(nottext), preservingprecision: 4without turning it intoprecision: "4".
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| crates/rustledger-loader/src/lib.rs | Delegates display-context construction to the new canonical DisplayContext::from_directives and keeps render_commas applied at the loader edge. |
| crates/rustledger-ffi-wasi/src/helpers.rs | Removes local precision inference (PrecisionTracker) and ships loader-resolved display precisions consistently for both load_source and load_file. |
| crates/rustledger-ffi-component/wit/world.wit | Bumps package to 3.8.0 and adds the session.format API + updated docs around options-aware formatting. |
| crates/rustledger-ffi-component/src/lib.rs | Updates component API version to 3.8 and exposes the new session format method. |
| crates/rustledger-ffi-component/src/convert.rs | Fixes MetaValue::Int to cross as number; implements SessionState::format; adds unit tests for option/metadata precedence and round-trip behavior. |
| crates/rustledger-ffi-component-tests/tests/parity.rs | Adds a real component-boundary parity test ensuring session.format honors display_precision and the info() → from-entries-with-options round trip. |
| crates/rustledger-core/src/display_context.rs | Adds canonical DisplayContext::from_directives and resolved_precisions() with tests pinning the three-stage precedence. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
The rustfava CI job caught the first cut shipping RESOLVED precisions (inference frozen as fixed entries for every observed currency), which starved rustfava's documented undeclared-currency fallback (its tests passed; only its 100% coverage gate tripped). Shipping inference as declarations was also semantically wrong: it would pin a consumer's precision against later entry-set changes, and inference is derivable from the entries themselves wherever they travel. `ledger-options.display-precision` now carries DECLARATIONS only — `option "display_precision"` entries plus commodity `precision:` metadata, precedence-resolved by the canonical DisplayContext (`declared_precisions()` replaces `resolved_precisions()`). `session.format` behavior is unchanged: its `from_directives` stage-1 scan re-infers from the held entries, so undeclared currencies land on the same mode the loader would pick.
…ield directly The declarations-only export broke rustfava for real (2 test failures, not just coverage): its loader treats a non-empty display_precision as THE per-currency precision map and only falls back to local inference when the field is empty — so a partial declarations map starved every undeclared currency. The de-facto wire contract since the tracker era is the full resolved map, and that is also what a rendering embedder actually wants. display_precision is again resolved_precisions(): fixed declarations where present, canonical inference otherwise, every observed currency listed. The rustfava-side coverage for its now-bypassed fallback lands in rustledger/rustfava#262.
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.
What
The #1766 format surface:
session.format(WIT 3.8.0) renders the held directives to canonical beancount text honoring the ledger's per-currency display precision — the options-aware renderingformat.format-loadeddocuments as out of its own scope (a bare directive list carries no options).Precision precedence matches the CLI loader exactly: inference from the held entries' own amounts (mode of observed decimal places), overridden by the held options'
display-precision, overridden by commodityprecision:metadata. Tracked currencies pad and never round; untracked render byte-faithful; ledger text never carries thousands separators.Two pre-existing bugs this surfaced and fixes
The user's
option "display_precision"never crossed the FFI boundary.load_sourceOVERWROTE the option-derived field with a localPrecisionTrackerre-derivation (inference only), whileload_fileshipped the raw option map (missing commodityprecision:metadata) — the two load paths disagreed about whatledger-options.display-precisioneven means.PrecisionTrackeris deleted; both paths now ship the ledger's DECLARED precisions (option entries + commodity metadata, precedence-resolved by the loader's ownDisplayContextvia the newdeclared_precisions()). Declarations only, deliberately not inference: inference stays derivable from the entries wherever they travel (session.formatre-infers viafrom_directives), and embedder-side fallbacks (rustfava's undeclared-currency inference) keep working.Integer metadata gained quotes over a session round-trip.
MetaValue::Intcrossed the wire astext, so re-rendering held entries turnedprecision: 4intoprecision: "4"(and broke commodity-precision handling insession.format). It now crosses asnumber, which the return path restores toInt.Canonical-function discipline
The directive-scan builder moved verbatim from the loader into
rustledger_core::DisplayContext::from_directives(the loader delegates), so the FFI shares the exact sampling rules — costtotal()-over-per_unit()preference, price-annotation inclusion, the three-stage precedence.declared_precisions()exports the declared entries so an embedder re-applying them as fixed overrides plus the canonical entry scan reproduces the loader's decisions.Versioning
package rustledger:[email protected]+API_VERSION "3.8"in lockstep (pinned by the existing parity test that derives the expected version from world.wit).How to test
cargo test -p rustledger-core display_context— three-stage precedence pinned currency-by-currencycargo test -p rustledger-ffi-component session_format— option padding,info()round-trip, commodity metadata surviving the entries round tripcargo build -p rustledger-ffi-component --target wasm32-wasip2 && cargo test -p rustledger-ffi-component-tests— real-wasmtimesession.formatparity (verified locally with the gate OPEN: 23 passed in 69s)Full workspace green, clippy/doc clean. Note:
corpus_paritydiverges undercargo test --workspacewhen the debug component wasm exists — pre-existing (reproduced on the unmodified base), caused by workspace feature unification changing native plugin error messages; filing separately.Part of #1766.
🤖 Generated with Claude Code
https://claude.ai/code/session_013gJH7uBBF9Xw9et9QFRcsU