Skip to content

feat: session.format renders held entries with ledger display precision (WIT 3.8.0) - #1808

Merged
robcohen merged 3 commits into
mainfrom
feat/wit-session-format
Jul 19, 2026
Merged

robcohen merged 3 commits into
mainfrom
feat/wit-session-format

Conversation

@robcohen

@robcohen robcohen commented Jul 18, 2026 •

Copy link
Copy Markdown
Member

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 rendering format.format-loaded documents 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 commodity precision: 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

  1. The user's option "display_precision" never crossed the FFI boundary. load_source OVERWROTE the option-derived field with a local PrecisionTracker re-derivation (inference only), while load_file shipped the raw option map (missing commodity precision: metadata) — the two load paths disagreed about what ledger-options.display-precision even means. PrecisionTracker is deleted; both paths now ship the ledger's DECLARED precisions (option entries + commodity metadata, precedence-resolved by the loader's own DisplayContext via the new declared_precisions()). Declarations only, deliberately not inference: inference stays derivable from the entries wherever they travel (session.format re-infers via from_directives), and embedder-side fallbacks (rustfava's undeclared-currency inference) keep working.

  2. Integer metadata gained quotes over a session round-trip. MetaValue::Int crossed the wire as text, so re-rendering held entries turned precision: 4 into precision: "4" (and broke commodity-precision handling in session.format). It now crosses as number, which the return path restores to Int.

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 — cost total()-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-currency
  • cargo test -p rustledger-ffi-component session_format — option padding, info() round-trip, commodity metadata surviving the entries round trip
  • cargo build -p rustledger-ffi-component --target wasm32-wasip2 && cargo test -p rustledger-ffi-component-tests — real-wasmtime session.format parity (verified locally with the gate OPEN: 23 passed in 69s)

Full workspace green, clippy/doc clean. Note: corpus_parity diverges under cargo test --workspace when 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

…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

codecov Bot commented Jul 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.01961% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-core/src/display_context.rs 98.95% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 31.7ms ± 0.7ms 23.7 MB
beancount 858.2ms ± 4.6ms 50.7 MB
Comparison 27.1x faster 2.1x less

vs main branch (2026-07-18)

Metric main this PR Change
Time 35.6ms 31.7ms -11.0% 🟢
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

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

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.format in WIT rustledger:[email protected] and wires it through the component to render held directives using a resolved DisplayContext (inference → display_precision option → commodity precision: metadata).
  • Fixes ledger-options.display-precision crossing the FFI boundary by exporting the loader’s resolved per-currency precisions (removing the divergent PrecisionTracker behavior).
  • Fixes integer metadata round-tripping by encoding MetaValue::Int as WIT number (not text), preserving precision: 4 without turning it into precision: "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.

@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 811
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 added 2 commits July 18, 2026 22:53
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.
@robcohen
robcohen merged commit 2844d8b into main Jul 19, 2026
56 of 57 checks passed
@robcohen
robcohen deleted the feat/wit-session-format branch July 19, 2026 03:12
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