Skip to content

fix: one canonical account-name rule; validate FFI ingress; warn on bad name_* roots - #1739

Merged
robcohen merged 3 commits into
mainfrom
fix/account-name-single-rule
Jul 10, 2026
Merged

robcohen merged 3 commits into
mainfrom
fix/account-name-single-rule

Conversation

@robcohen

@robcohen robcohen commented Jul 9, 2026

Copy link
Copy Markdown
Member

What

Establishes ONE canonical account-name rule — the lexer itself — and makes every surface that admits account names use it: the validator's Open check, the loader's option guards, and (new) the FFI directive-builder ingress. Also adds an E7008 warning for name_* account-type renames that can never head a parsable account.

Why

Phase-1 correctness sweep, finding L4: three hand-written validators accepted three different character sets:

Surface Rule
lexer (Account token regex) [\p{Lu}\p{Lo}\p{Lt}] roots, sub-components may start with a digit, letters/digits/hyphens, ≥2 components
validator (validate_account_name) digit-start roots allowed; any non-ASCII char allowed mid-component (the reject condition required c.is_ascii() — №, emoji passed)
loader (is_valid_account) a third first-char rule, no per-character check at all

Parsed ledgers were safe (the lexer gates them). The live holes were the synthesized surfaces:

  • FFI builder (entry.create/createBatch) bypasses the loader pipeline and took raw account: String with no validation whatsoever — directives whose accounts can never round-trip through text could enter programmatically.
  • name_* renames were the one unguarded option family: option "name_assets" "1Assets" was accepted silently even though every account under a digit-start root is unparsable (in both rledger and Python beancount — verified during the probe), while account_rounding etc. always had the E7002 guard.

How

  • rustledger_parser::is_valid_account_name — the canonical predicate, implemented by running the lexer and requiring exactly one Account token spanning the whole input. Drift from the parser is impossible by construction; there is no second encoding of the rule to keep in sync.
  • Validator keeps its root-type membership check (ledger semantics, knows about renamed roots) and delegates the character/structure rule. This tightens it — digit-start roots, №/emoji mid-component, and single-component names are now rejected, matching the parser and Python.
  • Loader is_valid_account delegates. name_* roots are checked by running the predicate on "{root}:X" (a root is valid exactly when it can head a real account) → E7008 warning, value still accepted for option-handling parity.
  • FFI builder validates all eight account ingress sites (posting, open, close, balance, pad + source, note, document) with a typed error naming the field — same pattern as the existing date/cost-invariant checks in input_entry_to_directive (review B-3.1 precedent).

All three consumers already depended on rustledger-parser; no new edges in the crate graph.

Testing

  • Predicate unit tests: Cyrillic/CJK roots, digit-start sub-components, hyphens; rejection classes (single component, digit-start root, lowercase starts incl. non-ASCII é, №/emoji/underscore/space, structural ::/trailing colon/whitespace).
  • Validator tests pin the tightening and that renamed roots still work; loader tests pin E7008 (digit-start, embedded colon) and no-warning for valid unicode renames; FFI tests pin rejection (lowercase root, embedded space) and acceptance (資産:現金).
  • Full suites green: parser, validate, loader, ffi-wasi, ffi-component. Clippy --all-features --all-targets -D warnings, fmt, rustdoc clean.

🤖 Generated with Claude Code

…ad name_* roots

Three surfaces hand-implemented "is this a valid account name" with
three different accepted character sets (sweep finding L4):

- the lexer: [Lu Lo Lt] roots, [Lu Lo Lt 0-9] sub-component starts,
  letters/digits/hyphens, >=2 components — the rule that decides what
  can actually round-trip through the language;
- the validator's Open check: accepted digit-start ROOTS and ANY
  non-ASCII character mid-component (its reject condition required
  c.is_ascii(), so numero signs and emoji passed);
- the loader's option guard (account_rounding etc.): a third
  first-char rule with no per-character check at all.

Parsed ledgers were safe (the lexer gates them), but synthesized
surfaces were not: the FFI builder path (entry.create/createBatch)
bypasses the loader pipeline and accepted raw strings as accounts
with NO validation — weaker than any of the three rules — so
directives whose accounts can never be parsed back could enter
programmatically. And name_* account-type renames were the one
unguarded option family: option "name_assets" "1Assets" was accepted
silently even though every account under it is unparsable (in both
rledger and Python beancount).

Fix, in one motion:

- rustledger_parser::is_valid_account_name — the canonical predicate,
  implemented by RUNNING THE LEXER (exactly one Account token spanning
  the whole input), so it cannot drift from what the parser accepts.
- validator delegates its character/structure rule to the predicate,
  keeping the root-type membership check (ledger semantics) and its
  error messages. Tightens: digit-start roots, non-ASCII garbage
  mid-component, and single-component names are now rejected —
  matching the parser and Python.
- loader's is_valid_account delegates; name_* options now get an
  E7008 warning when the root is un-lexable (checked by running the
  predicate on "root:X"), mirroring the E7002 guard the account_*
  options always had.
- FFI builder validates every account field (posting, open, close,
  balance, pad + source, note, document) through the predicate,
  returning a typed error naming the field.

New tests pin the predicate's rule (unicode roots, digit-start
sub-components, all rejection classes), the validator tightening, the
E7008 warning, and the FFI rejection/acceptance paths.

Co-Authored-By: Claude Fable 5 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner July 9, 2026 20:44
Copilot AI review requested due to automatic review settings July 9, 2026 20:44
@github-actions

github-actions Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 37.4ms ± 3.3ms 23.7 MB
beancount 774.1ms ± 5.8ms 50.6 MB
Comparison 20.7x faster 2.1x less

vs main branch (2026-07-10)

Metric main this PR Change
Time 33.7ms 37.4ms +11.0% 🔴
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

Unifies account-name validation across the codebase by introducing a single canonical predicate (rustledger_parser::is_valid_account_name) implemented by running the lexer, then updating validator, loader option guards, and FFI directive-builder ingress to rely on that predicate. This closes gaps where synthesized/programmatic surfaces could previously admit account names that the parser could never round-trip.

Changes:

  • Added rustledger_parser::is_valid_account_name (lexer-based) and re-exported it from rustledger-parser.
  • Tightened validator and loader option validation to delegate account-name structure/character checks to the canonical predicate; added loader warning E7008 for invalid name_* roots.
  • Added FFI-WASI builder ingress validation for all account-bearing directive inputs with targeted error messages and tests.

Reviewed changes

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

Show a summary per file
File Description
crates/rustledger-validate/src/validators/helpers.rs Delegates account-name lexing/structure validation to the canonical parser predicate; adds tests for tightened behavior and renamed roots.
crates/rustledger-parser/src/logos_lexer.rs Introduces canonical is_valid_account_name implemented via the lexer; adds predicate unit tests.
crates/rustledger-parser/src/lib.rs Re-exports is_valid_account_name as part of the parser crate’s public API.
crates/rustledger-loader/src/options.rs Makes account_* option validation canonical; adds name_* root validation with new E7008 warning plus tests.
crates/rustledger-ffi-wasi/src/types/input.rs Validates all FFI directive-builder account ingress points using the canonical predicate; adds regression tests for reject/accept cases.

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

@github-actions

github-actions Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 811
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

robcohen and others added 2 commits July 10, 2026 04:23
The account-name ingress gate made input_entry_to_directive fallible
on account validation, so the pass-through surfaces' "conversion is
not expected to fail" comment was stale: a held directive with an
un-lexable account (plugin-synthesized or embedder-constructed) is
silently dropped. Documented at both sites with the trade-off and the
escape hatch (a warnings channel on the WIT signature) if it stops
being acceptable.

Co-Authored-By: Claude Fable 5 <[email protected]>
@codecov

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.82609% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-loader/src/options.rs 95.45% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@robcohen
robcohen merged commit 2f3ad42 into main Jul 10, 2026
52 of 53 checks passed
@robcohen
robcohen deleted the fix/account-name-single-rule branch July 10, 2026 09:14
robcohen added a commit that referenced this pull request Jul 10, 2026
…1753)

* feat(fuzz): fuzz the FFI ingress boundary (input_entry_to_directive)

The builder behind the component's entry.create/createBatch takes
wire JSON from untrusted embedders and hand-validates it — dates,
the account-name gate, cost-number arity, the BookedCost
per-unit x units == total invariant — and none of that validation
was fuzzed.

New cargo-fuzz crate for rustledger-ffi-wasi with one target:
arbitrary bytes driven through the REAL wire path
(serde_json -> InputEntry -> input_entry_to_directive), asserting
beyond no-panic that (1) any successfully converted directive
carries only accounts the canonical lexer predicate accepts — a
bypass would readmit the class #1739's gate blocks — and (2)
conversion disposition is deterministic.

Committed seeds (fuzz/seeds/, one JSON per InputEntry variant plus
booked-pair and bad-account edges) document the wire format by
example and are copied into the corpus before CI runs, so cold
caches start with structural coverage. fuzz.yml gains a
fuzz-ffi-ingress job cloned from fuzz-booking (max_len 4096 for
JSON) and the dispatch target list gains fuzz_input_entry.

Locally shaken: 9.65M executions over 120s, cov 6073, zero crashes,
both invariants held.

Co-Authored-By: Claude Fable 5 <[email protected]>

* fix(fuzz): length guard, ffi paths trigger, full variant seeds (review)

- explicit 64 KiB input guard in the harness so resource usage is
  bounded regardless of runner flags;
- fuzz.yml pull_request paths gain crates/rustledger-ffi-wasi/** (the
  new job now runs where the boundary changes) and the workflow file
  itself;
- seeds for the six missing InputEntry variants (commodity, price,
  event, document, query, custom) so the "one per variant" comment is
  true — 16 seeds total, re-shaken 60s locally with zero findings.

Co-Authored-By: Claude Fable 5 <[email protected]>

---------

Co-authored-by: Claude Fable 5 <[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.

2 participants