Skip to content

fix(ag-rledger): answer a non-UTF-8 argument with a PARSE_ERROR envelope - #2875

Merged
robcohen merged 1 commit into
mainfrom
fix/ag-rledger-non-utf8-arg
Nov 6, 2026
Merged

robcohen merged 1 commit into
mainfrom
fix/ag-rledger-non-utf8-arg

Conversation

@robcohen

@robcohen robcohen commented Nov 6, 2026

Copy link
Copy Markdown
Member

What changed

ag-rledger's main reads std::env::args_os() instead of std::env::args(). When an argument is not valid UTF-8 it answers with agcli's own envelope for exactly that case: AgentCli::run_env reads the same process argv, finds the first argument that is not UTF-8, and returns a PARSE_ERROR envelope naming its position, with exit code 2 (USAGE), without dispatching anything. The printing and exit are factored into emit, so this envelope gets the same display-unsafe escaping (#2714) as every other run.

$ ag-rledger check $'\xff.beancount'
{"ok":false,"command":"ag-rledger",...,"exit_code":2,...,"error":{"message":"argument 2 is not valid UTF-8","code":"PARSE_ERROR","retryable":false},"fix":"Pass every argument as valid UTF-8 text.","next_actions":[...]}

Why

std::env::args() panics on a non-UTF-8 argument before agcli runs, so the caller got a Rust panic message and exit code 101 instead of a JSON envelope (#2860). ag-rledger calls cli.run(args) rather than run_env() because it rewrites the argv first (config aliases), so it never reached agcli's own non-UTF-8 handling. Calling run_env only in the non-UTF-8 case reuses agcli's envelope (message, fix, next actions, exit code) instead of re-deriving it.

How to test

cargo test -p rustledger --all-features --test ag_rledger_test test_main_non_utf8_argument_is_a_parse_error_envelope: a non-UTF-8 argument as the command word, after check, and after flags, each giving a PARSE_ERROR envelope naming its position and exit code 2.

Checks run locally: cargo test -p rustledger --all-features (one failure, python_plugins_back_to_back_after_a_timeout, a 2-second plugin time budget exceeded while the machine was under heavy parallel build load; it passes on rerun and does not touch ag-rledger), cargo clippy --all-features --all-targets -- -D warnings, RUSTDOCFLAGS=-Dwarnings cargo doc --workspace --no-deps, cargo fmt --check.

Closes #2860

🤖 Generated with Claude Code

`ag-rledger check $'\xff.beancount'` panicked inside std::env::args(),
before agcli ran, so the caller got a Rust panic and exit code 101 with
no JSON envelope.

main now reads std::env::args_os(). When an argument is not UTF-8 it
answers with agcli's own envelope for that case: AgentCli::run_env reads
the same process argv, finds the argument, and returns a PARSE_ERROR
envelope naming its position ("argument 2 is not valid UTF-8", exit code
2, USAGE) without dispatching anything. The envelope is printed and the
process exits through the same path as every other run.

Closes #2860

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner November 6, 2026 16:49
@codecov

codecov Bot commented Nov 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 50.1ms ± 1.0ms 797.0ms ± 5.0ms 15.9x 42.8 MB / 51.1 MB
investment 76.9ms ± 0.9ms 2918.1ms ± 32.2ms 38.0x 58.8 MB / 59.0 MB
fifo 73.3ms ± 0.5ms 14706.0ms ± 216.0ms 200.5x 52.7 MB / 58.8 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 49.9ms ± 0.3ms 50.1ms ± 1.0ms +0.3% ✅ within noise 42.7 MB → 42.8 MB (+0.3%)
investment 76.7ms ± 0.8ms 76.9ms ± 0.9ms +0.2% ✅ within noise 58.6 MB → 58.8 MB (+0.3%)
fifo 72.9ms ± 0.4ms 73.3ms ± 0.5ms +0.6% ✅ within noise 54.7 MB → 52.7 MB (-3.6%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.5ms, investment 0.5ms, fifo 0.3ms), 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

@github-actions

github-actions Bot commented Nov 6, 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 826
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
robcohen merged commit 5b93833 into main Nov 6, 2026
47 of 48 checks passed
@robcohen
robcohen deleted the fix/ag-rledger-non-utf8-arg branch November 6, 2026 18:24
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.

fix(ag-rledger): a non-UTF-8 argument crashes with no JSON envelope

1 participant