Skip to content

fix(wasm): emit Posting.flag on the wire (closes #1205) - #1209

Merged
robcohen merged 2 commits into
mainfrom
fix/wasm-posting-flag-1205
May 27, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/wasm-posting-flag-1205

Conversation

@robcohen

Copy link
Copy Markdown
Member

Summary

  • Adds flag: Option<String> to WASM's PostingJson (mirrors FFI-WASI's shape at crates/rustledger-ffi-wasi/src/types/output.rs:217).
  • Populates it from posting.flag.map(|c| c.to_string()) in the WASM converter.
  • Removes the #[ignore] on posting_with_flag_equivalence so the cross-binding harness from feat(test): cross-binding wire-format equivalence harness (#1200 item 1) #1204 now guards convergence in CI.

The TypeScript declaration in beancount_wasm.d.ts already had flag?: string on the Posting interface, so this fix aligns the Rust side with what JS consumers were already typed against.

Caught by the cross-binding wire-format harness (#1200 / #1204).

Test plan

  • cargo test -p rustledger-wasm — 78/78 pass
  • cargo test -p rustledger-wire-format-tests posting_with_flag_equivalence — passes (was previously #[ignore]d)
  • cargo clippy -p rustledger-wasm -p rustledger-wire-format-tests --all-targets -- -D warnings — clean
  • cargo fmt --all -- --check — clean

Closes #1205.

🤖 Generated with Claude Code

The WASM binding's PostingJson dropped the per-posting flag entirely;
FFI-WASI emitted it as `"flag": "!"`. JS consumers using
`@rustledger/wasm` had no way to read posting-level flags from
parsed ledgers.

Mirror the FFI-WASI shape: add `flag: Option<String>` (with
`skip_serializing_if = "Option::is_none"`) between `price` and
`meta`, and populate it via `p.flag.map(|c| c.to_string())`. The
`beancount_wasm.d.ts` already declared `flag?: string` on the
Posting interface, so the JS type surface was already correct --
this fix lines the Rust side up with what consumers expected.

The cross-binding equivalence test
(`posting_with_flag_equivalence`) had been `#[ignore]`-pinned with
this issue as the rationale; remove the ignore so the test now
guards convergence in CI.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner May 27, 2026 20:17
Copilot AI review requested due to automatic review settings May 27, 2026 20:17

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

This PR fixes WASM wire-format drift for posting-level flags by adding Posting.flag to the WASM JSON DTO and enabling the cross-binding regression test for #1205.

Changes:

  • Adds flag: Option<String> to WASM PostingJson.
  • Populates the field from core Posting.flag.
  • Re-enables the cross-binding posting_with_flag_equivalence test.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
crates/rustledger-wasm/src/types.rs Adds the posting-level flag field to the WASM wire DTO.
crates/rustledger-wasm/src/convert.rs Serializes core posting flags into the new DTO field.
crates/rustledger-wire-format-tests/tests/equivalence.rs Unignores and updates the regression test for posting flag equivalence.

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

Comment thread crates/rustledger-wasm/src/types.rs Resolved
@github-actions

github-actions Bot commented May 27, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 32.6ms ± 0.4ms 25.1 MB
beancount 879.3ms ± 13ms 50.7 MB
Comparison 27x faster 2.0x less

vs main branch (2026-05-27)

Metric main this PR Change
Time 36.1ms 32.6ms -9.7% 🟢
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

@github-actions

github-actions Bot commented May 27, 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

…unt.d.ts

Addresses Copilot inline review on #1209: adding flag to the Rust DTO
exposes it in the JSON output, but the public TypeScript declarations
need updating too. Three TS surfaces ship with the WASM build:

- crates/rustledger-wasm/src/lib.rs typescript_custom_section
  (wasm-bindgen embeds this in the generated .d.ts) -- Posting was
  missing flag (and meta, audited in the followup PR).

- crates/rustledger-wasm/beancount.d.ts (hand-maintained .d.ts) --
  Posting was missing flag.

- crates/rustledger-wasm/beancount_wasm.d.ts -- already declared
  flag? before this PR; no change needed here.

JS consumers using @rustledger/wasm now see flag?: string on Posting
regardless of which TS surface they pick up.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@codecov

codecov Bot commented May 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

robcohen added a commit that referenced this pull request May 27, 2026
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
robcohen merged commit 399053c into main May 27, 2026
34 checks passed
@robcohen
robcohen deleted the fix/wasm-posting-flag-1205 branch May 27, 2026 20:36
robcohen added a commit that referenced this pull request May 27, 2026
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]>
robcohen added a commit that referenced this pull request May 27, 2026
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 added a commit that referenced this pull request May 27, 2026
* docs(wasm): align beancount_wasm.d.ts with the actual wire shape

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]>

* docs(wasm): extend audit to typescript_custom_section + beancount.d.ts

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]>

---------

Co-authored-by: Claude Opus 4.7 <[email protected]>
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 drops Posting.flag from JSON wire shape

2 participants