Skip to content

feat(component): add clamp_entries to the component engine - #170

Merged
robcohen merged 2 commits into
mainfrom
feat/component-clamp-entries
Jun 17, 2026
Merged

robcohen merged 2 commits into
mainfrom
feat/component-clamp-entries

Conversation

@robcohen

Copy link
Copy Markdown
Member

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:

method caller component wrapper
load / load_full loader.py ✅
query query.py ✅
is_encrypted core/__init__.py ✅
clamp_entries core/filters.py (date-range filtering) ❌ → added here

The component couldn't be a true drop-in default until clamp_entries existed — a date-filtered request would AttributeError.

What

builder.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. This adds _unmarshal, the type-driven inverse of _marshal:

  • records → wasmtime.component.Record (fields set by WIT name)
  • tagged {"type": …} dicts → Variant
  • 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).

Verified the JSON-RPC surface also emits MetaValue::Number as a decimal string (json!(n.to_string())), so numeric-metadata round-trips stay in parity (no int-vs-string drift).

Tests

  • round-trip preserves user metadata, tags/links, and posting cost (the per_unit cost-number variant);
  • opening-balance synthesis at the window boundary;
  • cross-engine parity (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 found CLI-gated tests (identical with/without this change); the component tests run via in-process wasmtime-py and pass.

🤖 Generated with Claude Code

robcohen and others added 2 commits June 17, 2026 14:27
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]>
@robcohen

Copy link
Copy Markdown
Member Author

Self-review caught a correctness bug, now fixed in 955360d.

Bug: 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 cost numbers ({"type":"per_unit",…}) failed that lookup — the cost basis was silently dropped to None on every component load, and lost again on the clamp round-trip. The original tests missed it because they fed raw load output into clamp_entries, whereas the real caller (core/filters.py) feeds directives_to_json(entries), a different shape.

Fix: special-case the cost-number variant in both directions — _marshal emits the kind-tagged JSON-RPC shape, _unmarshal rebuilds it from _cost_to_json's flat per-unit string (or a kind dict). Added test_clamp_entries_via_directives_to_json_preserves_cost exercising the actual filters.py path. Verified end-to-end: cost number now survives load → from_json → to_json → clamp.

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.

1 participant