Skip to content

fix(core): tags and links are a sorted, deduplicated set - #2565

Merged
robcohen merged 5 commits into
mainfrom
fix/tags-links-sorted-set
Oct 10, 2026
Merged

robcohen merged 5 commits into
mainfrom
fix/tags-links-sorted-set

Conversation

@robcohen

@robcohen robcohen commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Implements the decision on #2545 (option B) and #2544.

What was wrong

What changed

  • New rustledger_core::SortedSet<T> (sorted_set.rs), with TagSet / LinkSet aliases. Its storage is private; every way to build or change one — from_iter, From<Vec>, From<[T; N]>, insert, extend, for_each_mut, serde Deserialize — sorts and dedups. Reads go through Deref<Target = [T]>. Transaction, Document and Note tags/links are now TagSet/LinkSet, so the invariant holds by type on every path: parser (red and green conversion), pushtag, entries converted back from plugins (from_wrapper), the FFI input (input_entry_to_directive), JSON. The order is Ord on the text = byte order = code-point order, which is how Python sorts str.
  • apply_inherited_state unions the pushtag stack into transactions, documents and notes.
  • meta.hash (ffi-wasi/src/hash.rs) needed no code change: it hashes the set, so it is now order-independent. One-time meta.hash change for rustfava: entries whose tags or links were not already sorted and unique (and documents/notes inside a pushtag block, which gain the pushed tag) hash differently once. An entry whose tags and links were already sorted and unique, outside any pushtag that newly reaches it, hashes unchanged. The WIT and plugin wire stay list<string>, now always sorted.
  • The loader's cross-file re-interning walks the sets through for_each_mut (and now also interns Note tags/links, which it skipped).
  • ops/fingerprint.rs and core/extract.rs keep their own sort/dedup: they are not made redundant by the invariant. fingerprint hashes the plugin-wire TransactionData (plain Vec<String> an importer or plugin fills), and extract_tags/extract_links take the union across directives and metadata values.
  • rledger format is unaffected: it rewrites source text from the CST, so written order (duplicates included) is kept. Pinned by tags_and_links_keep_written_order.
  • Compat §16: removed the "tags and links | source order | sorted" row.
  • tests/baselines/parser-corpus.manifest: regenerated for the 30 corpus files whose parsed tags/links change (intentional drift; hashes match CI).
  • CACHE_VERSION: loader 37 → 38, wasm 33 → 34 (pin reconciled). The archived layout is unchanged (the set archives its backing Vec), but old blobs hold unsorted/duplicated tags and untagged pushtag'd documents/notes.
  • API break (CHANGELOG ### Breaking): the field types changed; .push → insert, vec![..] → .into() / collect().

How tested

  • crates/rustledger-ffi-wasi/tests/tags_links_set_properties.rs (proptest, across paths): parser + pushtag (transaction, document and note each hold exactly the sorted union of written and pushed tags), plugin round trip with arbitrary order/duplicates, FFI input, and meta.hash invariant under written order.
  • sorted_set unit + property tests (every constructor and mutator yields a strictly increasing list of the distinct inputs).
  • tags_and_links_keep_written_order (formatter).
  • Fail-without-fix: with SortedSet's normalization disabled, all 4 cross-path properties fail; with pushtag restricted to transactions again, parsed_entries_hold_the_set fails. (The formatter test is a guard that the formatter does NOT change; it passes before and after by design.)
  • Oracle (beancount 3.2.3 / beanquery) on the A tag written or pushed twice is kept twice; beancount's tags are a set #2545 + pushtag is not applied to document and note entries #2544 ledger plus written tags on document/note: SELECT type, tags, links FROM #entries, SELECT date, tags, links and PRINT agree on tags and links for every entry (food trip/aa zz, alpha trip zeta/l1 l2 on document and note). Remaining PRINT differences are §16's existing layout rows.
  • cargo test --workspace --all-features: all pass except python_plugin_wasi::host_time_budget_applies_to_python_plugins, a wall-clock 3-second CPython budget that timed out under a machine load average of ~55 on 8 cores (unrelated to this change; CI will show). Clippy workspace -D warnings, cargo doc -D warnings for core/parser/loader/ffi-wasi/plugin.

Follow-ups, not in this PR: the plugin WIT note record and FFI InputEntry::Note / WIT note-dir carry no tags or links (documented gaps from #2160), so a note a plugin modifies loses its tags, which this PR makes more visible since pushtag now tags notes.

Closes #2545
Closes #2544

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

Transaction, Document and Note hold tags and links as
rustledger_core::SortedSet (TagSet / LinkSet), whose every constructor
sorts and dedups, so the parser, pushtag, plugin conversion back and the
FFI input can no longer build an unsorted or duplicated list. PRINT and
BQL now match bean-query; meta.hash is order-independent.

pushtag also tags document and note entries, as beancount 3 does.

Bumps the loader cache to v38 and the wasm cache to v34.

Closes #2545
Closes #2544

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen
robcohen requested a review from a team as a code owner October 9, 2026 20:43
@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 44.9ms ± 0.9ms 807.8ms ± 9.1ms 18.0x 51.1 MB / 50.0 MB
investment 69.0ms ± 0.5ms 2974.9ms ± 50.6ms 43.1x 57.0 MB / 58.1 MB
fifo 68.4ms ± 0.8ms 15403.8ms ± 104.6ms 225.2x 55.2 MB / 57.9 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 44.3ms ± 0.2ms 44.9ms ± 0.9ms +1.4% ✅ within noise 51.2 MB → 51.1 MB (-0.3%)
investment 68.8ms ± 0.5ms 69.0ms ± 0.5ms +0.4% ✅ within noise 57.2 MB → 57.0 MB (-0.3%)
fifo 68.0ms ± 0.5ms 68.4ms ± 0.8ms +0.6% ✅ within noise 55.1 MB → 55.2 MB (+0.2%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.4ms, investment 0.3ms, fifo 0.4ms), and is at least 5%.
10,000 transactions per shape · 20 runs, 3 warmup · hyperfine

What do these numbers mean?
  • simple — Two-posting transfers, one currency. The historical workload — keep as the floor, not as the benchmark.
  • investment — Cost specs, per-unit prices and lot reductions. Exercises booking and inventory, which simple leaves at 0.3%.
  • fifo — Bare {} reductions against a FIFO account — how FIFO sells are normally written. The other four shapes reach the ordered-selection path twice in 2k transactions between them, so it was effectively unprofiled until it turned out to be quadratic (plan_ordered scans every lot per reduction: FIFO ledgers are quadratic (8.8s at 20k transactions) #2083).
  • vs base: the PR and the base-branch commit it is built on, built and measured in the same job
  • RSS: peak resident set size, a single sample — reported without a verdict because there is no spread to test it against
  • A shape with no beancount column is one bean-check reports errors on; it is timed for rledger only

@codecov

codecov Bot commented Oct 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.74359% with 28 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-core/src/sorted_set.rs 81.25% 24 Missing ⚠️
crates/rustledger-core/src/directive.rs 75.00% 4 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 179 masked)
Full AST Match 100%
Total Files 819
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
    The headline is the EFFECTIVE rate, after pairs where beanquery
    itself is wrong are masked. raw is the same run with nothing
    masked. A widening gap means the registry grew, not that rledger
    changed.
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

robcohen and others added 2 commits October 9, 2026 21:37
The 30 corpus files whose parsed tags or links were unsorted, duplicated,
or pushed onto a document or note now parse to the set (#2545, #2544).
The new hashes match the ones CI reported.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen

Copy link
Copy Markdown
Member Author

Review loop

Round Scope Checks Findings
1 Whole diff: SortedSet constructors/mutators, rkyv/serde paths, ShiftSpans, parser red + green conversion, pushtag application, loader re-interning, plugin and FFI conversions, meta.hash, fingerprint/extract redundancy question, formatter workspace suite; fail-without-fix (invariant disabled → 4/4 cross-path properties fail; pushtag transactions-only → parser property fails); bean-query oracle on the #2544/#2545 ledgers; differential SELECT date, type, tags, links FROM #entries vs bean-query over the 89 corpus files with tags or pushtag, harness self-checked: base (main) 54 same / 26 differ, branch 78 / 2 (both pre-existing and unrelated: an extra open row, and a #tag line after the first posting) CI's non-required Corpus output baseline failed: 30 corpus files now parse to sorted/deduped tags (intentional). Regenerated tests/baselines/parser-corpus.manifest (8568460104); exactly those 30 lines moved, hashes equal to the ones CI reported
2 The baseline commit; the main merges; plugin-side wire (core→wrapper now always sorted), LSP/wasm consumers reading tags full workspace suite on the merged head (5427 passed, 0 failed); Corpus output baseline green none

Converged after round 2. Required checks green on 8568460104; rustfava check shows the known 8 failed / 671 passed.

…-set

# Conflicts:
#	CHANGELOG.md
#	crates/rustledger-wasm/src/cache.rs
@robcohen
robcohen merged commit 5497f57 into main Oct 10, 2026
72 of 73 checks passed
@robcohen
robcohen deleted the fix/tags-links-sorted-set branch October 10, 2026 18:59
robcohen added a commit that referenced this pull request Oct 11, 2026
…FI (#2588)

A note's tags and links (its own and, since #2565, a pushtag block's)
had no place in the plugin wire's NoteData, the component's note-dir
and input-note, or the FFI's InputEntry::Note, so a note passing through
any plugin or the FFI came back without them. All of them carry tags
and links now, as documents do, as does the WASM build's JSON note.

Structs cross the plugin wire as MessagePack arrays, so a version-1
guest cannot decode the longer note: ABI_VERSION is 2 and a version-1
WASM plugin or importer is refused at load. The component contract
moves to 3.14.0 and the plugin WIT sketch to beancount:[email protected].

The Python compat layer's Note gains beancount 3's tags and links, and
a document keeps its path through a Python plugin: the layer read it
from the key holding the entry's source file and wrote it back under one
the host rejects, so any Python plugin returning a ledger with a
document failed.

Closes #2573


Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

Co-authored-by: Claude Opus 5.5 (1M context) <[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.

A tag written or pushed twice is kept twice; beancount's tags are a set pushtag is not applied to document and note entries

1 participant