Skip to content

fix(wasm): converge Transaction.payee/narration wire shape with FFI-WASI (closes #1221) - #1222

Merged
robcohen merged 2 commits into
mainfrom
fix/wasm-payee-narration-1221
May 28, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/wasm-payee-narration-1221

Conversation

@robcohen

Copy link
Copy Markdown
Member

Summary

Surfaced by the ts-rs spike in #1220. FFI-WASI already uses `skip_serializing_if = "Option::is_none"` on both `payee` and `narration`; the WASM binding diverged on both fields:

  • `payee`: WASM emitted `"payee": null` when None; FFI-WASI omitted.
  • `narration`: WASM wrapped `txn.narration` unconditionally in `Some(...)`, so empty narration always emitted `""` on the wire; FFI-WASI's converter normalized empty-string-to-None and elided via `skip_serializing_if`.

The harness from #1204 documented this exact convergence path in its module docstring (item 3) — the null-stripping normalization was masking the divergence so the equivalence test would pass while the underlying bug persisted.

Changes

  • `types.rs`: add `skip_serializing_if = "Option::is_none"` to both fields.
  • `convert.rs`: empty narration → None (mirrors FFI-WASI's pattern at `ffi-wasi/src/convert.rs:133-137`).
  • Three TS surfaces aligned:
    • `beancount.d.ts`: `payee: string | null` → `payee?: string`; `narration: string` → `narration?: string`.
    • `beancount_wasm.d.ts`: `narration: string` → `narration?: string` (payee was already correct).
    • `typescript_custom_section`: already had both as optional; no change needed.
  • Harness module docstring updated — item 3 marked converged.

Two new regression tests pin both directions (omit-on-empty and emit-on-present).

Test plan

  • `cargo test -p rustledger-wasm` — 80/80 pass (was 78; +2 new)
  • `cargo test -p rustledger-wire-format-tests` — 18/18 pass
  • `cargo check --target wasm32-unknown-unknown -p rustledger-wasm --tests` — clean (catches the JS-side test surface)
  • `cargo clippy -p rustledger-wasm -p rustledger-wire-format-tests --all-targets -- -D warnings` — clean
  • `cargo fmt --all -- --check` — clean

Notes

The .d.ts audit (#1210) didn't catch this because it looked for missing/extra fields, not type mismatches on existing fields. The ts-rs spike (#1220) surfaced it by comparing generated TS against the hand-written shape — concrete evidence for ADR-0004's CI gate proposal.

Closes #1221.

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings May 28, 2026 13:06
@robcohen
robcohen requested a review from a team as a code owner May 28, 2026 13:06
@codecov

codecov Bot commented May 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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

Converges the WASM binding's Transaction.payee/narration wire shape with FFI-WASI by adding skip_serializing_if = "Option::is_none" on both fields and normalizing empty narration to None in the converter. TypeScript surfaces and the equivalence harness docstring are updated to match.

Changes:

  • Add skip_serializing_if to payee/narration in WASM DirectiveJson::Transaction and normalize empty narration to None in convert.rs.
  • Update beancount.d.ts and beancount_wasm.d.ts so payee/narration are ?: string optional rather than string | null / required string.
  • Add two regression tests pinning omit-on-empty and emit-on-present, and mark item 3 of the equivalence harness docstring as converged.

Reviewed changes

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

Show a summary per file
File Description
crates/rustledger-wasm/src/types.rs Adds skip_serializing_if to payee and narration.
crates/rustledger-wasm/src/convert.rs Normalizes empty narration to None and adds two regression tests.
crates/rustledger-wasm/beancount.d.ts Updates payee/narration to optional.
crates/rustledger-wasm/beancount_wasm.d.ts Updates narration to optional and refreshes payee doc.
crates/rustledger-wire-format-tests/tests/equivalence.rs Marks divergence item 3 as converged.

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

@github-actions

github-actions Bot commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 791
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

…WASI (closes #1221)

BREAKING CHANGE: the WASM binding's wire shape for `Transaction.payee`
and `Transaction.narration` now matches FFI-WASI. Pre-fix WASM emitted
`"payee": null` when None and `"narration": ""` when empty; post-fix
both fields are absent on the wire via `skip_serializing_if`. JS
consumers using `"payee" in t` (now `false` instead of `true`) or
`t.narration === ""` (now `undefined` instead of `""`) must update.
See `crates/rustledger-wasm/CHANGELOG.md` for details.

Surfaced by the ts-rs spike in #1220. FFI-WASI already used
`skip_serializing_if = "Option::is_none"` on both fields, but the
WASM binding diverged:

- `payee`: WASM emitted `"payee": null` when None; FFI-WASI omitted.
- `narration`: WASM wrapped `txn.narration` unconditionally in
  `Some(...)`, so an empty narration always emitted as `""` on the
  wire; FFI-WASI's converter normalized empty-string to None and
  thus elided the field via `skip_serializing_if`.

This divergence was masked in the wire-format equivalence harness by
the `strip_empty_meta_and_directive_nulls` normalization helper --
the harness was documented as a known divergence (item 3 in its
module docstring) with the recommended convergence being exactly
what this PR does.

## Changes

- `src/types.rs`: add `#[serde(skip_serializing_if = "Option::is_none")]`
  to both `Transaction.payee` and `Transaction.narration`.
- `src/convert.rs`: change `narration: Some(txn.narration.to_string())`
  to elide on empty string -- matches FFI-WASI's converter pattern at
  `crates/rustledger-ffi-wasi/src/convert.rs:133-137`.
- Three TS surfaces aligned:
  - `beancount.d.ts`: `payee: string | null` → `payee?: string`;
    `narration: string` → `narration?: string`.
  - `beancount_wasm.d.ts`: `narration: string` → `narration?: string`
    (payee was already correct).
  - `typescript_custom_section` in `src/lib.rs`: already had
    `payee?: string` and `narration?: string`; no change needed.
- `CHANGELOG.md`: BREAKING CHANGES entry under [Unreleased].
- Wire-format harness docstring updated -- divergence item 3 marked
  as ~~converged~~ with a pointer to this fix.

## Tests

Two new regressions in `src/convert.rs`:

- `transaction_omits_payee_and_empty_narration_1221` -- no-payee +
  empty narration transaction must have neither field on the wire.
- `transaction_emits_payee_and_nonempty_narration_1221` -- present
  payee + non-empty narration must both appear on the wire.
  Counterpoint so a future regression that elides unconditionally
  also fails CI.

The existing wire-format-tests harness already covered Transaction
equivalence (`metadata_equivalence_on_transaction_directive`) and
continues to pass; the null-stripping in
`strip_empty_meta_and_directive_nulls` is no longer load-bearing for
the payee/narration case but stays as a defensive guard against
future regression.

## Notes

The `.d.ts` audit that fixed Posting.price and 7 missing variants
(#1210) didn't catch this because it looked for missing/extra fields,
not type-mismatches on existing fields. The ts-rs spike (#1220)
surfaced it as a side-effect of comparing generated TS against the
hand-written shape -- evidence for ADR-0004's CI gate proposal.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@robcohen
robcohen force-pushed the fix/wasm-payee-narration-1221 branch from e0d2748 to a4b2498 Compare May 28, 2026 13:15
@robcohen
robcohen merged commit e39b41a into main May 28, 2026
36 of 40 checks passed
@robcohen
robcohen deleted the fix/wasm-payee-narration-1221 branch May 28, 2026 13:29
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.

wasm: Transaction.payee/narration TS types drift from wire shape

2 participants