Skip to content

refactor(lexbench): split report main into named typed stages - #207

Merged
lanyue-llk merged 6 commits into
mainfrom
refactor/lexbench-report-stages
Nov 4, 2026
Merged

lanyue-llk merged 6 commits into
mainfrom
refactor/lexbench-report-stages

Conversation

@lanyue-llk

@lanyue-llk lanyue-llk commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

What, why and how of this PR

Issues

Closes #203

Part of the #104 and #87 refactor series; the other functions listed in #104 stay with that issue.

Summary

src/harness/lexbench/report.py::main was about 1,520 lines. It is now a 119-line sequence of 48 named, typed stages in the same file: arguments and paths, certified cohort, rounds, cases and evidence, summaries, version comparison, candidates, resource cohorts, public model, then output. The longest stage other than main is build_public_model at 99 lines, most of which is the literal data/model.json layout. No template engine was added.

The code was moved mechanically from line ranges of the old file rather than retyped. Evidence error messages are unchanged, including those that quote the old variable names, because they are existing output.

Checked first: no frozen binding records this file. git grep finds neither its path nor its SHA-256 in any bound data, and the report's reproduction/ directory copies only reproduction.py and charts.py.

Byte-identity proof

The repository had no complete synthetic build of this report, so the first commit adds one and snapshots it against the unchanged main code. The second commit is the refactor and does not touch the snapshots. Review fixes follow; only the commit that changes the fixture's evidence moves snapshots, and the same snapshots were checked against the unchanged code (see the test plan).

  • tests/lexbench_report_fixture.py writes synthetic retained evidence for three scenarios: minimal (no candidates, Chinese only), macos (four fixed candidates with a serialized 11,140-call resource matrix) and linux (the same candidates with saved Linux profiles). A line trace of the three builds shows the only unexecuted lines in the stages are evidence-rejection guards, the --source and missing-template paths, and a few continue and else None arms.
  • tests/test_golden_reports.py::test_lexbench_report compares each build with tests/golden/lexbench-{minimal,macos,linux}.json. New files under tests/golden/ are the only snapshot changes, and no existing route changes.

Three fixture choices need a reviewer's eye:

  • The Mermaid bytes pinned by the report are not in Git, so the fixture supplies synthetic bytes and patches report.bytes_sha256 to accept them. The digest check itself is not exercised by this test.
  • data/cases, data/fixed-cases and data/evidence are folded into one route each before snapshotting. This keeps the three snapshots under 9 KB instead of about 285 KB. The folded route still contains every byte, but a change there names the folded route rather than the individual case.
  • charts.py writes SVGs with no explicit encoding, so the build crashes under an ASCII locale. It is copied byte for byte into the published reproduction/plot_version_summary.py, so this PR leaves it unchanged. The fixture renders charts as UTF-8 so the ASCII-locale snapshot check still covers the rest of the build. The production crash and the owner decision on the published copy are in fix(lexbench): version chart SVGs crash report builds under an ASCII locale #208.

Test plan

Run locally on macOS with Python 3.11 from the shared locked environment, with PYTHONPATH pointed at the checkout, at head 202bd31e.

  • tests/test_explicit_utf8_io.py, tests/test_public_tree_privacy.py, tests/test_golden_reports.py, tests/test_lexbench_report_runtime.py, tests/test_lexbench_cli.py, tests/test_lexbench_verification.py, tests/test_data_home.py: 187 passed. The first two files are the ones that failed CI in the previous round.
  • Before/after: the current fixture and snapshots were overlaid on commit 7e5a9610, whose src/ equals main. The three Lexbench snapshot tests pass there and on the head, and diff -r of the kept builds (including every case and evidence byte, folded) reports no differences for all three scenarios.
  • Failure-path differential, using a throwaway script outside the repository: 1,171 runs on both revisions, each with one change to the arguments or the synthetic evidence (delete, empty, garbage, dropped or null key, altered value, or an absent path for a CLI option). Exception type and message, stdout, stderr and the output-tree digest are identical in all 1,171 runs. 738 runs fail, with 131 distinct messages, and 433 complete.
    • Scope: 719 runs on minimal cover its input file classes in full. 340 on linux and 112 on macos are capped per class and cover only classes the earlier scenarios had not reached. The linux run was stopped by time before finishing its classes, so this is a sample, not exhaustive.
    • Limit: changed evidence is not re-hashed, so digest checks stop some changes before a deeper guard. This does not prove every evidence-rejection guard was executed.
  • Lint gate: ruff check --config 'include=["*.py"]' --select F,TID252 . and ruff check pass.
  • CI on Linux at 202bd31e: "Tests and static checks", "Installation and packaging" and "Rendered reference and report checks" pass.

@lexmount-ocr lexmount-ocr Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Review complete · 33c9255

No findings in this review.

@lanyue-llk lanyue-llk left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent review, round 1

Head reviewed: 33c92555. CI on this head fails: "Tests and static checks" reports 2 failed, 19774 passed. Both failures come from this PR's new test files, and both reproduce on macOS. The refactor itself held up in every comparison I ran. The blockers are in the new test and the fixture.

Findings

P0: tests/test_golden_reports.py:66 (test_lexbench_report), root cause in src/harness/lexbench/charts.py:453

  • Problem: tests/test_explicit_utf8_io.py::test_report_builds_are_byte_identical_under_an_ascii_locale re-runs the golden suite under LC_ALL=C, and all three new scenarios fail with UnicodeEncodeError. render_chart calls output.write_text(...) with no encoding, and the charts contain Chinese text. charts.py is an existing HASH_BOUND exemption (PUBLISHED_COPY) in that test. This is an existing defect on main that the new build exposes, so a real report build also crashes under an ASCII locale.
  • Probe: adding encoding="utf-8" at that line clears the error, and the only remaining snapshot difference is reproduction/plot_version_summary.py. That is the only locale defect in these builds. It also means the direct fix changes published bytes, which conflicts with this PR's byte-identity claim.
  • Suggested fix: do not edit charts.py in this PR. Make the Lexbench golden test independent of the locale, for example by having the fixture run report.render_chart with UTF-8 as the default write encoding. Open a follow-up issue for the production crash, with the owner deciding whether to change the published copy and move that snapshot route.

P0: tests/lexbench_report_fixture.py:107

  • Problem: the stderr.log fixture line contains a literal /Users/<name> path whose name is not in PLACEHOLDER_HOMES, so tests/test_public_tree_privacy.py::test_tracked_text_has_no_private_values fails with home: 1.
  • Suggested fix: use a name already in PLACEHOLDER_HOMES, such as fixture, and do not extend the allowlist. This line feeds the evidence bytes, so regenerate the three snapshots on the unchanged code (commit 1) and on the refactor, then confirm they still match.

P2: PR body, "Test plan"

  • Problem: "Failure-path differential" and "CI on Linux" are still unchecked and marked PENDING. The listed local targets also leave out the two test files that fail, so the plan gave no warning of these failures.
  • Suggested fix: rerun both failing test files after the fixes, record the CI result, and either complete the differential or remove that item.

P3: src/harness/lexbench/report.py:898

  • Problem: the nested timestamp(value) is the only function in the file without type hints, and #203 asks for hints on the new functions.
  • Suggested fix: use def timestamp(value: str) -> float:.

P3: tests/lexbench_report_fixture.py:3

  • Problem: the docstring says the scenarios "cover every report branch". The PR body correctly says they do not: evidence-rejection guards and the --source and missing-template paths are not executed.
  • Suggested fix: align the docstring with the PR body.

What I verified

  • Snapshot origin: report.py at commit 1 (7e5a9610) is unchanged from base d894600d, and commit 2 does not touch tests/.
  • Full builds: I built all three scenarios on both revisions with GOLDEN_KEEP, and diff -r reports no differences, including the unfolded case and evidence bytes in the folded routes.
  • Statement-level read: I compared the old main with every stage: statement order, side-effect order, public_candidate_files insertion order, functional_methods mutation timing, model.json key order and guard messages. I found no behavioural difference. Replacing corrections with a literal [] is safe because nothing ever appended to it.
  • Failure-path differential: I ran 1,015 cases against both revisions, comparing exception type, message, stderr, stdout and a hash of the partial output tree. There were no differences.
    • The cases were JSON and JSONL leaf mutations, key deletions, file deletions and CLI and profile errors. I replaced the digest functions with an always-equal stub so mutations could reach guards behind the hash checks.
    • Coverage by scenario: minimal 875 cases (all planned mutations), macos 77, linux 63. The last two are random samples of about 2,000 planned mutations each, so they are partial.
    • The stub means digest mismatches were not compared in this run; I checked those guards by reading the code.
  • Snapshot sensitivity: two temporary output mutations, one in build_public_model and one in safe_failure, each failed all three scenarios.
  • Coverage claim: a line trace of the three builds agrees with the PR body. The unexecuted lines are guards, the --source and missing-template paths, and a few continue arms.
  • Size claims: main is 119 lines, build_public_model is 99, and there are 48 new stages.
  • Frozen bindings: neither the path nor the base SHA-256 of report.py appears in tracked data. reproduction/ copies only reproduction.py and charts.py.
  • Rules: existing golden routes are unchanged, the diff adds no aliases or parallel implementation and no template engine, and the "Closes #203" scope is otherwise met. Ruff gates pass, and the 93 tests in the PR's listed targets pass locally.

CI on 33c92555

Check Result
Tests and static checks fail (the two P0s above)
Python 3.11 fail, 3s; I did not open its log
Installation and packaging pass
Rendered reference and report checks pass

charts.py is copied byte for byte into the published report, so it keeps its
implicit write encoding until #208 decides. The fixture renders charts as UTF-8
so the ASCII-locale snapshot check exercises the rest of the build.
The stderr fixture line named a home outside PLACEHOLDER_HOMES, which the public
tree privacy check rejects. The snapshots move only because that evidence line,
and the case data derived from it, changed; the report code did not.

@lexmount-ocr lexmount-ocr Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Re-review complete · 202bd31

No findings in this incremental review.

@lanyue-llk

Copy link
Copy Markdown
Contributor Author

Review round 1 fixes, all pushed to head 202bd31e. Nothing is disputed.

Fixed:

  • P0, ASCII locale crash in the golden test (8783e183): the fixture now renders charts as UTF-8, scoped to the render_chart call. charts.py is unchanged, and the production crash and the owner decision on the published copy are in fix(lexbench): version chart SVGs crash report builds under an ASCII locale #208.
  • P0, private-home check (05a8fcc3): the stderr fixture line now uses /Users/fixture, with no allowlist change. Only data/cases.folded and data/fixed-cases.folded moved in the three snapshots. The new snapshots match both the head and the unchanged code from 7e5a9610, and diff -r of the kept builds is empty.
  • P2, PR test plan: updated. The two failing test files are now listed and pass (187 passed locally), CI is green on 202bd31e, and the failure-path differential was completed rather than removed: 1,171 runs, no differences. Its scope is a sample with limits, stated in the test plan.
  • P3, type hints (202bd31e): def timestamp(value: str) -> float:.
  • P3, fixture docstring (952983c2): it now names the paths the scenarios leave unexecuted.

@lanyue-llk lanyue-llk left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent review, round 2

Reviewed head 202bd31e against base d894600d. No defects were found. All five findings from round 1 are fixed. This is a comment only, not an approval.

What I verified

  • Before/after bytes. I overlaid the head's fixture, golden test and snapshots on an unchanged d894600d checkout and built all three scenarios on both revisions with GOLDEN_KEEP. diff -r of the kept builds (87 files, 21 MB) reports no differences, including the folded case and evidence routes. src/ differs only in report.py.
  • Moved code. I inlined the stages in the order main calls them and diffed the normalized AST statements against the old main.
    • The only differences are renames, extracted constants, return statements and the closure defaults removed from evidence_for and safe_failure.
    • One order change exists: resolve() of the source and output paths now runs before chart_population is built. It can only matter for a symlink loop combined with a profile missing resource_population, so it is not worth changing.
    • The corrections list was never appended to, so the literal [] is equivalent. The 39 data/model.json keys keep their order.
    • The eager runs[...][0] arguments cannot raise a new error, because earlier stages have already indexed those rounds.
    • Dataclass fields and positional stage arguments map correctly.
  • Failure paths. My own small differential ran 12 cases on both revisions, separate from the author's. It covered a missing resource_population, missing profile paths, a population mismatch, a bad --source, duplicate and unknown --fixed-summary, mixed macOS and Linux cohorts, missing matrix, official and Linux inputs, and an existing output directory. Exception text, stdout, stderr and the output digest were identical. This is a sample and does not reach most of the evidence-rejection guards.
  • Snapshot sensitivity. Three mutations, each to a different stage, made the Lexbench snapshot tests fail: a changed model value, a dropped safe_failure result, and a baseline/profile run root swap. The second one failed two of the three scenarios, and the others failed all three.
  • PR description claims.
    • main was 1,519 lines and is now 119, with 48 new stages. The longest stage, build_public_model, is 99 lines.
    • A line trace of the three scenarios matches the description: the unexecuted statements are mostly raise guards, plus parser.error, the --source path and a few continue arms.
    • No helper or nested function lacks type hints, and no template engine was added.
  • Scope and rules.
    • The pre-refactor file's SHA-256 and its path appear in no bound data. Git mentions of the path are limited to the Lexbench README, the CLI and tests.
    • charts.py is unchanged. The ASCII-locale workaround applies only to the fixture's render_chart call and is disclosed, with #208 open for the owner decision.
    • The only snapshot changes are three new files, and no existing route moved.
    • Closes #203 is accurate.
  • Tests and lint. test_explicit_utf8_io, test_public_tree_privacy, test_golden_reports, test_lexbench_report_runtime, test_lexbench_cli, test_lexbench_verification and test_data_home: 187 passed locally on Python 3.11. Both ruff gates pass.
  • CI at this head. "Tests and static checks", "Installation and packaging" and "Rendered reference and report checks" all pass.

Limits

  • I did not rerun the author's 1,171-run failure differential. The PR already states that it is a sample.
  • The synthetic Mermaid bytes mean the vendored digest check is not exercised by the snapshot, as the PR discloses.

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.

Split the Lexbench report main function into stages

1 participant