Repository navigation
docs(wasm): align beancount_wasm.d.ts with the actual wire shape - #1210
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Copilot wasn't able to review any files in this pull request.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Hand-maintained drift between the .d.ts and the Rust DTOs surfaced while reviewing #1209. Audit findings: 1. **`Posting` was missing `price?: Amount`** -- the `@`/`@@` price annotation is emitted by the Rust side but JS consumers could only see it via the `unknown` index signature on `Directive`. 2. **`Posting.flag` was declared in the wrong position** -- the .d.ts listed `flag?` between `cost` and `meta`, while `rustledger_wasm::types::PostingJson` (and the JSON output) puts it after `price`. Cosmetic only; reorder to match. 3. **7 of 12 directive variants had no narrowed interface**: commodity, pad, event, note, document, query, custom. The `DirectiveType` union listed them but consumers had to inspect the raw base `Directive` with its `[key: string]: unknown` index signature to read their fields. Add `CommodityDirective`, `PadDirective`, `EventDirective`, `NoteDirective`, `DocumentDirective`, `QueryDirective`, `CustomDirective`. Each new interface mirrors the corresponding variant on `DirectiveJson` (custom_type, source_account, event_type, etc.). Long term, hand-maintenance of the .d.ts is the root cause -- issue #1200 item 2 (ts-rs generation from the Rust DTOs) will eliminate this whole class of drift. This PR is a tactical fix to close the current gaps; doesn't change the runtime wire shape. Co-Authored-By: Claude Opus 4.7 <[email protected]>
PR #1209 surfaced via Copilot that the WASM crate ships three hand-maintained TypeScript surfaces, not one. Extend the audit (begun in the initial commit on beancount_wasm.d.ts) to the other two: **`src/lib.rs` typescript_custom_section** (wasm-bindgen embeds this in the generated .d.ts that ships with the npm package): - Declare the `MetaValueJson` type so directive/posting `meta?` fields are properly typed instead of relying on `unknown`. - Add `meta?: Record<string, MetaValueJson>` to `BaseDirective` (inherited by Transaction/Balance/Open/Close). - Add `meta?` to each inline directive shape in the `Directive` union (commodity, pad, event, note, document, price, query, custom), spelled out since these don't extend `BaseDirective`. - Add `values?: MetaValueJson[]` to the `custom` inline shape (the positional arguments after the type keyword -- wire shape since #1199, but the TS surface had been silently dropping them from the type system). - Add `meta?` to the `Posting` interface (posting-level metadata from #1168; wire-shape `meta?` was present but the type elided it). **`beancount.d.ts`**: - Add `price?: Amount` to `Posting` -- the `@`/`@@` annotation is emitted on the wire (and has been for ages) but the type didn't expose it. Note: `Posting.flag?` is added to both surfaces by the companion commit on #1209; this PR's edits stay clear of that line to avoid a merge conflict. Long term, hand-maintaining three TS surfaces is the structural problem -- issue #1200 item 2 (ts-rs generation) is the cure. Co-Authored-By: Claude Opus 4.7 <[email protected]>
robcohen
force-pushed
the
docs/wasm-dts-audit
branch
from
May 27, 2026 20:37
f2f6648 to
1c6162b
Compare
robcohen
enabled auto-merge (squash)
May 27, 2026 20:37
Contributor
📊 Benchmark Results
vs main branch (2026-05-27)
What do these numbers mean?
|
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
This was referenced May 28, 2026
robcohen
added a commit
that referenced
this pull request
May 28, 2026
…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
added a commit
that referenced
this pull request
May 28, 2026
…ASI (closes #1221) (#1222) * fix(wasm)!: converge Transaction.payee/narration wire shape with FFI-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]> * ci: retrigger after sccache outage --------- Co-authored-by: Claude Opus 4.7 <[email protected]>
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.
Summary
Audit pass over
crates/rustledger-wasm/beancount_wasm.d.tssurfaced drift between the hand-maintained TypeScript declarations and the actual RustDirectiveJson/PostingJsonshapes:Postingwas missingprice?: Amount— the@/@@price annotation is emitted by the WASM binding but JS consumers could only access it via the baseDirective's[key: string]: unknownindex signature.Posting.flagwas in the wrong position — declared betweencostandmeta, but the Rust struct (and the JSON output) puts it afterprice. Cosmetic.commodity,pad,event,note,document,query,custom. TheDirectiveTypeunion listed them but consumers had no IntelliSense for their fields. AddedCommodityDirective,PadDirective,EventDirective,NoteDirective,DocumentDirective,QueryDirective,CustomDirectivemirroring the correspondingDirectiveJsonvariants.No runtime / wire-shape change — this is type-definitions only.
Why hand-maintained?
The root cause of this whole class of drift is that the
.d.tsis hand-written. The proper long-term fix is #1200 item 2 (ts-rs generation from the Rust DTOs), which will make this kind of audit unnecessary. This PR is the tactical close on the current gaps.Test plan
.d.ts(verifiedtreefmt.toml).DirectiveJsonvariant incrates/rustledger-wasm/src/types.rs.Related: #1200, #1209.
🤖 Generated with Claude Code