Skip to content

feat(returns): --by-group partial report on unvaluable scopes (#1850 §4) - #1852

Merged
robcohen merged 4 commits into
mainfrom
feat/returns-by-group-partial
Jul 23, 2026
Merged

robcohen merged 4 commits into
mainfrom
feat/returns-by-group-partial

Conversation

@robcohen

@robcohen robcohen commented Jul 23, 2026 •

Copy link
Copy Markdown
Member

Summary

report returns --by-group now renders a partial report when a scope hits an unvaluable input, instead of aborting the whole report on the first error (#1850 §4). Stacked work; base is now main (#1851 merged).

Behavior

  • Each scope is valued independently (net-units isolates them), so a group that hits an unvaluable input — an unpriced commodity, or an elided posting — becomes an n/a row with a stderr warning: naming the reason, while the computable groups still report their figures.
  • Exit status (updated in review): when any row is unvaluable the report is still rendered, but rledger exits non-zero — an incomplete report is not a full success, so a script gating on the exit code (rledger … && …) stops rather than consuming a report with silent n/a holes. The partial output is written to stdout before the non-zero exit (pinned by the by_group_partial_report_renders_but_exits_nonzero subprocess test). Only when every row is unvaluable is nothing rendered.
  • JSON keeps a stable schema: every row gains an error field (null when computed, the reason string when not) with null figures on an unvaluable row — machine-distinguishable in all formats (CSV/text signal via the exit code + n/a).

Changes

  • report_returns: build Vec<GroupRow> (Ok | Unvaluable { label, reason }) instead of ?-aborting; warn per unavailable scope (label routed through sanitize_display — it is user-controlled returns-group metadata); return Err (non-zero exit) when the report is partial; bail before rendering only when all rows are unvaluable.
  • render_grouped: n/a in every figure column (text/CSV); JSON gains the stable error field.
  • Tests: render_grouped (text + structural JSON error-field assertion), an end-to-end report_returns unit test (partial output written and Err), and a subprocess test pinning the non-zero exit + partial stdout + stderr reason.

🤖 Generated with Claude Code

@robcohen
robcohen requested a review from a team as a code owner July 23, 2026 01:20
@robcohen
robcohen force-pushed the feat/returns-by-group-partial branch 2 times, most recently from 6c83db0 to d115ff6 Compare July 23, 2026 13:58
Base automatically changed from feat/returns-net-units to main July 23, 2026 14:45
robcohen and others added 3 commits July 23, 2026 14:46
Net-units valuation isolates scopes — each sums only its own accounts — so one
group hitting an unvaluable input (an in-scope account with an elided posting, or
a commodity with no price) no longer has to fail the whole report. This finishes
the #1850 §4 CLI rendering that the net-units core (#1851) unblocked.

Previously the `--by-group` path `?`-aborted on the first per-scope error (the
#1849 seventh-review finding), nuking the TOTAL and every other group. Now each
scope is computed independently and a failure becomes a row-level `n/a` marker
with a stderr warning naming the group and reason, while the computable groups
render their figures. Only if EVERY row (TOTAL included) is unvaluable does the
report fail loudly — there is then nothing to show.

- report_returns: build `Vec<GroupRow>` (Ok | Unvaluable{label, reason}) instead
  of `?`-aborting; warn per unavailable scope; bail only when all are unvaluable.
- render_grouped: render `n/a` in every figure column for an unvaluable row
  (text/CSV); JSON keeps a stable schema — every row gains an `error` field
  (`null` when computed, the reason string when not) with `null` figures.
- Tests: a direct render_grouped test (text + JSON schema) and an end-to-end
  report_returns --by-group test (one valuable group + one elided-in-scope group
  → partial render, TOTAL n/a, report succeeds).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013gJH7uBBF9Xw9et9QFRcsU
The render_grouped_marks_unvaluable_rows JSON check ended in a substring OR
(... || json.contains "un-booked") — the reason text matched anywhere in the
blob, so a malformed or misplaced error key would still pass green (the
substring-proxy anti-pattern CLAUDE.md calls out). Parse the JSON and assert the
error FIELD structurally: the computed row's error is null with figures present,
the unvaluable row and the TOTAL carry the exact reason on error with every
figure null — the stable schema a consumer actually branches on.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013gJH7uBBF9Xw9et9QFRcsU
Second review pass, finding [0]: the §4 partial render turned EVERY per-scope
error non-fatal, including MissingPrice — so a --by-group report with one
unpriceable group emitted an n/a row and exited 0, where the single-scope path
(and origin/main) exited non-zero. CSV/text carry no error column, so a pipeline
gating on exit status treated a silently-incomplete report as full success.

Now report_returns still RENDERS the computable rows + n/a markers, then returns
Err so the CLI exits non-zero — the incomplete-report signal for pipelines (JSON
also carries a per-row `error` field; the exit code is the only machine-readable
signal for CSV/text). Only a fully-computable report exits 0; an all-unvaluable
report still bails before rendering as before.

Tests: the end-to-end unit test now asserts the partial output is written AND the
call returns Err; a new subprocess test (by_group_partial_report_renders_but_exits_nonzero)
pins the actual non-zero exit + partial stdout + stderr reason.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013gJH7uBBF9Xw9et9QFRcsU
Copilot AI review requested due to automatic review settings July 23, 2026 14:47
@robcohen
robcohen force-pushed the feat/returns-by-group-partial branch from d115ff6 to c19aa6d Compare July 23, 2026 14:47
@github-actions

github-actions Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 34.3ms ± 1.1ms 24.2 MB
beancount 730.6ms ± 3.4ms 50.7 MB
Comparison 21.3x faster 2.1x less

vs main branch (2026-07-23)

Metric main this PR Change
Time 36.8ms 34.3ms -6.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

Updates the report returns --by-group CLI path to tolerate per-scope “unvaluable” inputs by rendering a partial grouped report (with n/a rows) instead of aborting on the first failing scope, aligning with the net-units isolation work from #1851.

Changes:

  • Add a GroupRow (Ok vs Unvaluable { label, reason }) pipeline to keep rendering computable groups while marking failing rows as n/a and warning on stderr.
  • Extend grouped rendering so text/CSV show n/a figures for unvaluable rows, and JSON keeps a stable schema via a per-row error field and null figures.
  • Add/extend tests covering partial grouped rendering behavior (both renderer-level and CLI end-to-end).

Reviewed changes

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

File Description
crates/rustledger/src/cmd/report_cmd/returns.rs Implements partial by-group computation/rendering with GroupRow, per-scope warnings, n/a rows, and JSON error field; adds unit tests for the new rendering semantics.
crates/rustledger/tests/report_returns_test.rs Adds an end-to-end CLI regression test asserting partial grouped output is still produced when one group is unvaluable.

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

@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
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
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

@codecov

codecov Bot commented Jul 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.06726% with 11 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger/src/cmd/report_cmd/returns.rs 95.06% 11 Missing ⚠️

📢 Thoughts on this report? Let us know!

… warning (copilot)

The per-scope unavailable warning printed the raw returns-group label, which is
user-controlled and can carry control chars / newlines (log/terminal injection —
spoofed lines, escape sequences). Route it through sanitize_display, the same
guard the grouping warnings already use. The error text is grammar-constrained
(account names / currencies), so it needs no sanitizing.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_013gJH7uBBF9Xw9et9QFRcsU
Copilot AI review requested due to automatic review settings July 23, 2026 17:05

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 2 out of 2 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

crates/rustledger/src/cmd/report_cmd/returns.rs:527

  • The PR description states that a partial report returns --by-group render (some groups computable, some unvaluable) should still succeed unless every row is unvaluable. However, the implementation currently returns Err (non-zero exit) whenever unvaluable > 0, and the new integration test asserts non-zero status. Please decide the intended contract (exit 0 with warnings + per-row error field vs exit non-zero for incomplete output) and align the behavior + tests + PR description accordingly, since this is a user-visible CLI/API behavior change.
    if unvaluable > 0 {
        anyhow::bail!(
            "returns report is incomplete: {unvaluable} of {} {} could not be valued \
             (see the n/a rows and the warnings above)",
            rows.len(),

@robcohen
robcohen merged commit 5da8542 into main Jul 23, 2026
56 checks passed
@robcohen
robcohen deleted the feat/returns-by-group-partial branch July 23, 2026 17:32
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