Skip to content

fix(ffi-wasi,wasm): emit Document.tags and Document.links (closes #1208) - #1212

Merged
robcohen merged 1 commit into
mainfrom
fix/document-tags-links-1208
May 28, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/document-tags-links-1208

Conversation

@robcohen

Copy link
Copy Markdown
Member

Summary

Both rustledger-ffi-wasi and rustledger-wasm's DirectiveJson::Document variants dropped tags and links from the wire output. The fields were added to rustledger_core::Document in #1144 but never plumbed through to either binding's wire DTO. This is a lockstep-wrong drop — equivalence alone would have missed it, but the new field-presence helper from #1204 caught it on first run.

  • Add tags: Vec<String> and links: Vec<String> (with skip_serializing_if = "Vec::is_empty") to both bindings' Document variants.
  • Populate via d.tags.iter().map(ToString::to_string).collect() — mirrors how Transaction.tags/links are converted.
  • Declare tags?: string[] / links?: string[] on all three TS surfaces: beancount_wasm.d.ts, beancount.d.ts, typescript_custom_section.
  • Remove the #[ignore] on document_directive_with_tags_and_links_equivalence so the cross-binding harness from feat(test): cross-binding wire-format equivalence harness (#1200 item 1) #1204 now guards convergence in CI.

Why skip_serializing_if (not always-emit like Transaction)

Transaction.tags/links always emit as [] when empty — but Document's tags/links are absent from today's wire output entirely, so JS consumers parsing Document directives don't see those fields at all. skip-if-empty is backward-compatible: documents without tags continue to emit no tags field; documents with tags now emit them. Always-emit would add "tags": [] to every Document, which is a larger contract change for an audit fix.

Test plan

  • cargo test -p rustledger-wire-format-tests document_directive — both Document fixtures pass
  • cargo test -p rustledger-wasm — 78/78 pass
  • cargo test -p rustledger-ffi-wasi — 56/56 pass
  • cargo clippy -p rustledger-wasm -p rustledger-ffi-wasi -p rustledger-wire-format-tests --all-targets -- -D warnings — clean
  • cargo fmt --all -- --check — clean

Closes #1208.

🤖 Generated with Claude Code

Both bindings' DirectiveJson::Document variants dropped tags and
links entirely from the wire output. The fields were added to
rustledger_core::Document in #1144 but never plumbed through to
either binding's DTO. Lockstep-wrong drop -- equivalence alone
would have missed it, but the new field-presence helper from #1204
caught it on first run.

Mirror the established Vec<Tag> -> Vec<String> pattern:
- Add `tags: Vec<String>`, `links: Vec<String>` with
  `skip_serializing_if = "Vec::is_empty"` to both bindings'
  Document variants.
- Populate via `d.tags.iter().map(ToString::to_string).collect()`
  in each binding's converter -- same pattern Transaction already
  uses for its tags/links.

`skip_serializing_if` (rather than always-emit, which is what
Transaction does for these vec fields) chosen here because:
- The fields are absent in today's wire output, so existing JS
  consumers parsing Document directives don't see them at all.
- skip-if-empty is backward-compatible: documents without tags
  continue to emit no tags field.
- always-emit would add `"tags": []` to every Document, which is a
  larger contract change for an audit fix.

TS declarations updated on all three surfaces (`beancount_wasm.d.ts`,
`beancount.d.ts`, the `typescript_custom_section` inline shape) with
`tags?: string[]` / `links?: string[]` matching the skip-if-empty
serialization.

The cross-binding equivalence test
(`document_directive_with_tags_and_links_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 21:08
Copilot AI review requested due to automatic review settings May 27, 2026 21:08

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 a wire-format regression/omission where both rustledger-ffi-wasi and rustledger-wasm failed to emit Document.tags and Document.links in their JSON DTOs, and adds a cross-binding test to prevent the fields from being dropped again.

Changes:

  • Add tags/links to DirectiveJson::Document for both WASM and FFI-WASI, with skip_serializing_if = "Vec::is_empty" behavior.
  • Populate tags/links in both conversion layers (directive_to_json) by stringifying tag/link entries.
  • Update all TS surfaces to reflect tags?: string[] / links?: string[], and enable the previously-ignored equivalence test to guard convergence in CI.

Reviewed changes

Copilot reviewed 6 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
crates/rustledger-wire-format-tests/tests/equivalence.rs Un-ignores and documents the Document.tags/links fixture to enforce field presence going forward.
crates/rustledger-wasm/src/types.rs Extends WASM DirectiveJson::Document with tags/links (skip-if-empty + default).
crates/rustledger-wasm/src/lib.rs Updates the TS custom section Directive union to include optional tags/links on document.
crates/rustledger-wasm/src/convert.rs Plumbs doc.tags/doc.links into the WASM wire DTO conversion.
crates/rustledger-wasm/beancount.d.ts Adds optional tags/links to DocumentData in the JS/TS surface.
crates/rustledger-wasm/beancount_wasm.d.ts Adds optional tags/links to DocumentDirective in the wasm-facing TS definitions.
crates/rustledger-ffi-wasi/src/types/output.rs Extends FFI-WASI DirectiveJson::Document with tags/links (skip-if-empty).
crates/rustledger-ffi-wasi/src/convert.rs Plumbs d.tags/d.links into the FFI-WASI wire DTO conversion.

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

@github-actions

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 34.4ms ± 3.6ms 25.4 MB
beancount 778.8ms ± 2.4ms 50.7 MB
Comparison 22.6x faster 2.0x less

vs main branch (2026-05-27)

Metric main this PR Change
Time 36.1ms 34.4ms -4.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

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

@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
robcohen merged commit cd3d84a into main May 28, 2026
35 checks passed
@robcohen
robcohen deleted the fix/document-tags-links-1208 branch May 28, 2026 07:10
robcohen added a commit that referenced this pull request May 28, 2026
#1216)

The output wire shape of `Document.tags` and `Document.links` was
fixed for both bindings in #1212, but the FFI-WASI input side was
still missing the fields entirely. `InputEntry::Document` had no
`tags` / `links` slots, and the converter at line 542 hardcoded
`Vec::new()` -- so callers using `entry.create` / `entry.createBatch`
to construct Document directives could never attach tags or links,
even though the core type and the output wire shape both support
them.

Mirror the `InputEntry::Transaction` shape:

- Add `tags: Vec<String>` and `links: Vec<String>` with
  `#[serde(default)]` to `InputEntry::Document`. The default-empty
  serde behavior keeps the wire backward-compatible -- a payload
  without the new fields still deserializes cleanly.
- Update the converter to populate the core `Document.tags` and
  `Document.links` via the same `.iter().map(|t| t.clone().into())`
  pattern Transaction already uses.

Add two regression tests:

- `document_tags_and_links_round_trip_1213` -- a Document payload
  with tags + links round-trips through the bridge and arrives on
  the core `Document` with both fields populated.

- `document_tags_and_links_default_to_empty_1213` -- a legacy
  payload without `tags`/`links` fields still converts cleanly,
  producing empty vectors. Backward-compat guard for pre-#1213
  clients in the wild.

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.

Both bindings drop Document.tags and Document.links from JSON wire shape

2 participants