Repository navigation
feat(component): add clamp_entries to the component engine - #170
Conversation
Completes the component backend's drop-in parity with the JSON-RPC RustledgerEngine for the methods rustfava actually uses on its hot paths (load / load_full / query / is_encrypted already existed; clamp_entries — used by core/filters.py for date-range filtering — was the remaining gap). clamp/filter take `list<directive>` (the same type `load` returns), so the already-loaded entries Fava holds must be marshalled back into typed component values. Adds `_unmarshal`, the type-driven inverse of `_marshal`: records -> wasmtime Record, tagged dicts -> Variant, and the flattened `meta.user` keys are re-nested and re-tagged via `_meta_value_json` (mirroring rustledger's `json_to_meta_value`, so the reconstruction matches what the JSON-RPC engine does on the Rust side — same result, different layer). The JSON-RPC engine emits numeric metadata as decimal strings too, so the round-trip stays in parity. Tests: round-trip preserves user metadata / tags / links / posting cost; opening-balance synthesis at the window boundary; plus a cross-engine parity test that skips when the wasmtime CLI (JSON-RPC backend) is absent. Prep for making the component the default backend (beancount#1384 Phase 4). Co-Authored-By: Claude Opus 4.8 <[email protected]>
The generic marshaller tags every variant `{"type": ...}`, but rustledger's
JSON surface tags `CostNumber` by `kind` (`#[serde(tag = "kind")]`), and
`types.py`'s `cost_number_values` reads `kind`. So component-marshalled cost
numbers (`{"type": "per_unit", ...}`) silently failed that lookup — the cost
basis was dropped to None on every component load, and lost again on the
clamp round-trip.
Special-case the `cost-number` variant (detected by its case-name set) in
both directions: `_marshal` now emits the `kind`-tagged JSON-RPC shape
(`per-unit-from-total` spreads `per_unit`/`total`), and `_unmarshal` rebuilds
it from what the real caller passes — `_cost_to_json`'s flat per-unit string,
or the `kind`-tagged dict.
Adds a regression test exercising the actual `core/filters.py` path
(`directives_to_json(entries)` -> clamp_entries), which the prior test missed
by feeding raw `load` output; updates the existing assertion to `kind`.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Self-review caught a correctness bug, now fixed in 955360d. Bug: the generic marshaller tags every variant Fix: special-case the |
First of two PRs to make the component the default rustledger backend during the dual-ship window (rustledger beancount#1384 Phase 4). This one completes the component wrapper; a follow-up flips the default + routes the call sites.
Why
Auditing the actual engine surface rustfava uses on its hot paths:
load/load_fullloader.pyqueryquery.pyis_encryptedcore/__init__.pyclamp_entriescore/filters.py(date-range filtering)The component couldn't be a true drop-in default until
clamp_entriesexisted — a date-filtered request wouldAttributeError.What
builder.clamp/filtertakelist<directive>— the same typeloadreturns — so the already-loaded entries Fava holds must be marshalled back into typed component values. This adds_unmarshal, the type-driven inverse of_marshal:wasmtime.component.Record(fields set by WIT name){"type": …}dicts →Variantmeta.userkeys are re-nested and re-tagged via_meta_value_json, mirroring rustledger'sjson_to_meta_value— so the reconstruction matches what the JSON-RPC engine does on the Rust side (same result, different layer).Verified the JSON-RPC surface also emits
MetaValue::Numberas a decimal string (json!(n.to_string())), so numeric-metadata round-trips stay in parity (no int-vs-string drift).Tests
per_unitcost-number variant);clamp_entries≡ JSON-RPC), skipped when the wasmtime CLI is unavailable.mypy + ruff clean. Full suite: the only failures are the pre-existing
wasmtime not foundCLI-gated tests (identical with/without this change); the component tests run via in-processwasmtime-pyand pass.🤖 Generated with Claude Code