Skip to content

fix(parser): a link is not a valid metadata value - #1957

Merged
robcohen merged 2 commits into
mainfrom
fix/link-not-a-metadata-value
Aug 4, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/link-not-a-metadata-value

Conversation

@robcohen

@robcohen robcohen commented Aug 4, 2026

Copy link
Copy Markdown
Member

ref: ^inv-1 parsed here and is a syntax error in beancount — its grammar has no production for a link in value position at all. A #tag in the same place is valid there, and we already agreed on that.

The asymmetry is the whole point

Tags and links lex as sibling kinds and are handled as a pair almost everywhere in this file, so rejecting both is the natural reach and the wrong answer — it would break input beancount accepts. Same mistake #1953 avoided in the mirror-image case, where note and document legitimately take both.

Scope

META_ENTRY nodes only. A link on a transaction is a direct child of the transaction, not of a metadata entry, so it's untouched — that being the one place links belong.

Not extended to custom / pushmeta, which run through value_tokens_to_meta rather than this path. beancount rejects both a tag and a link there, so it's a different rule needing its own evidence. Measured but deliberately left for a separate change rather than folding a second rule in here.

The scan lives in parse_via_cst_inner, which the green and red paths share, so there's no dual-path divergence to mirror. Guarded by stripped.contains('^'), the same trick the adjacent unclosed-cost-brace scan uses.

The first version of the test was vacuous

Sabotaging the scan to reject TAG as well still passed. The cause was that guard: the tag fixture contained no ^, so the scan never ran, and "a tag must still be accepted" said nothing about the scan's contents.

The accept case now uses a file that does contain a link elsewhere, so the scan runs and the tag has to survive it. Both sabotages are caught now:

scan disabled       → a link as a metadata value must be rejected
widened to tags     → tag as a metadata value, in a file that DOES contain a link:
                      must still parse cleanly, got [SyntaxError("a link (#groceries) ...")]

Corpus

One baseline drifted: parser-lima/examples_data_full.beancount, which carries key-for-link: ^mylink at line 84. beancount reports exactly one "unexpected LINK" error on that file, and so do we now. Updated surgically.

Caches: loader 21 → 22, wasm 7 → 8.

Verification

  • parser, loader, wasm, validate, query suites: 0 failures
  • workspace clippy at 1.97.0 clean, cargo fmt --check clean
  • green/red error-count parity asserted for link, tag, and transaction-link inputs

Closes #1954.

@robcohen
robcohen requested a review from a team as a code owner August 4, 2026 16:08
Copilot AI review requested due to automatic review settings August 4, 2026 16: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 aligns rustledger-parser’s CST conversion with beancount’s grammar by rejecting ^link tokens when they appear as metadata values inside META_ENTRY nodes (while still accepting #tag values and still allowing transaction-level links). It also bumps loader/wasm cache versions and updates the parser corpus baseline to reflect the new diagnostic.

Changes:

  • Add a guarded CST tree scan that emits a syntax error when a LINK token appears inside a META_ENTRY (link-valued metadata).
  • Add integration tests covering the intended tag/link asymmetry and green/red parity.
  • Update corpus baselines and bump loader/wasm cache versions to avoid stale “previously clean” parses.

Reviewed changes

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

Show a summary per file
File Description
tests/baselines/parser-corpus.manifest Updates baseline hash for examples_data_full.beancount due to the new parser diagnostic.
crates/rustledger-wasm/src/cache.rs Bumps WASM cache version to v8 to invalidate cached parses affected by the new syntax error.
crates/rustledger-parser/tests/parser_integration_test.rs Adds regression tests asserting link-valued metadata is rejected while tag-valued metadata remains accepted, plus green/red parity checks.
crates/rustledger-parser/src/cst/convert.rs Implements the META_ENTRY scan that flags LINK tokens as invalid metadata values (guarded by contains('^')).
crates/rustledger-loader/src/cache.rs Bumps loader cache version to v22 and updates fixture version assertions accordingly.
Suppressed comments (1)

crates/rustledger-parser/src/cst/convert.rs:2493

  • After inserting extract_link_metadata_value_errors above this helper, extract_unclosed_cost_brace_errors no longer has its own rustdoc. Re-add the COST_SPEC documentation directly above this function so it remains discoverable and attached to the correct item.
    }
    out
}

fn extract_unclosed_cost_brace_errors(

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

`ref: ^inv-1` parsed here and is a syntax error in beancount - its grammar has
no production for a link in value position at all. A `#tag` in the same place
IS valid there, and we already agreed on that.

THE ASYMMETRY IS THE WHOLE POINT

Tags and links lex as sibling kinds and are handled as a pair almost
everywhere in this file, so rejecting both is the natural reach and the wrong
answer - it would break input beancount accepts. Same mistake #1953 avoided in
the mirror-image case, where `note` and `document` legitimately take both.

SCOPE

`META_ENTRY` nodes only. A link on a TRANSACTION is a direct child of the
transaction, not of a metadata entry, so it is untouched - that being the one
place links belong.

NOT extended to `custom` / `pushmeta`, which run through
`value_tokens_to_meta` rather than this path. beancount rejects BOTH a tag and
a link there, so it is a different rule needing its own evidence. Measured but
deliberately left: filing separately rather than folding a second rule into
this change.

The scan lives in `parse_via_cst_inner`, which both the green and red paths
share, so there is no dual-path divergence to mirror. Guarded by
`stripped.contains('^')`, the same trick the adjacent unclosed-cost-brace scan
uses: no caret in the source means no LINK token can exist.

THE FIRST VERSION OF THE TEST WAS VACUOUS

Sabotaging the scan to reject TAG as well still passed. The reason is that
guard: the tag fixture contained no `^`, so the scan never ran, and the
"a tag must still be accepted" assertion said nothing about the scan's
contents. The accept case now uses a file that DOES contain a link elsewhere,
so the scan runs and the tag has to survive it. Both sabotages are caught now
- disabling the scan, and widening it to tags.

CORPUS

One baseline drifted: `parser-lima/examples_data_full.beancount`, which
carries `key-for-link: ^mylink` at line 84. beancount reports exactly one
"unexpected LINK" error on that file and so do we now. Updated surgically.

Caches: loader 21 -> 22, wasm 7 -> 8.

Closes #1954.
…s function

Two fixes, both mine.

The new scan was inserted directly above ,
which put it BETWEEN that function and its doc comment: the cost-spec rustdoc
then documented the link scan, and the cost-spec helper had none. Anchoring an
insertion on a  line orphans whatever rustdoc sits above it, and nothing
in the build or tests notices.

And resolving the rebase conflict in the test file, I cut through the middle
of the #1955 test and dropped its closing brace - the conflict boundary sat
inside the function, not between the two. Caught by clippy reporting an
unclosed delimiter, not by me reading the merge. Both tests are present and
the file has 63 passing again.
@robcohen
robcohen force-pushed the fix/link-not-a-metadata-value branch from 8daa573 to 8f814c4 Compare August 4, 2026 18:05
Copilot AI review requested due to automatic review settings August 4, 2026 18:05
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 36.8ms ± 1.3ms 24.2 MB
beancount 917.3ms ± 6.6ms 50.8 MB
Comparison 24.9x faster 2.1x less

vs main branch (2026-08-04)

Metric main this PR Change
Time 31.5ms 36.8ms +16.8% 🔴
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

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

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

Suppressed comments (2)

crates/rustledger-loader/src/cache.rs:407

  • PR description says the cache bumps are loader 21→22 and wasm 7→8, but this diff bumps loader to v23 and wasm to v9. Please update the PR description so reviewers/users don’t take the wrong cache version numbers away from this change.
/// v23: a `^link` is no longer accepted as a metadata VALUE (#1954), so a
///     file using `ref: ^x` moves from clean to erroring. A stale cache would
///     serve the old clean parse.
const CACHE_VERSION: u32 = 23;

crates/rustledger-parser/src/cst/convert.rs:155

  • stripped.contains('^') will be true for any file that contains transaction links (a common shape), which means we now do an additional full-tree syntax().descendants() scan that allocates red nodes even when there are no metadata entries with link values. This could be a noticeable parse-time regression on large ledgers that use links heavily. Consider folding the check into an existing traversal (e.g., the fused descendant/top-level walks or metadata-entry conversion) so link-in-metadata is detected without a second full-tree walk.
    // Same guard trick as above: no '^' in the source means no LINK token can
    // exist, so the whole-tree scan is skipped on the common case.
    if stripped.contains('^') {
        errors.extend(extract_link_metadata_value_errors(&source_file, bom_offset));
    }

@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

⚠️ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 95% (raw 89%, 162 masked)
Full AST Match 100%
Total Files 812
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

@codecov

codecov Bot commented Aug 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.77419% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-parser/src/cst/convert.rs 96.77% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@robcohen
robcohen merged commit 128226a into main Aug 4, 2026
71 of 72 checks passed
@robcohen
robcohen deleted the fix/link-not-a-metadata-value branch August 4, 2026 18:27
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.

[Bug]: a LINK as a metadata value is accepted; beancount rejects it (a TAG is fine in both)

2 participants