Skip to content

refactor(lexbench): split report validate into named typed checks - #232

Merged
lanyue-llk merged 4 commits into
mainfrom
refactor/lexbench-validator-checks
Nov 4, 2026
Merged

lanyue-llk merged 4 commits into
mainfrom
refactor/lexbench-validator-checks

Conversation

@lanyue-llk

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

Copy link
Copy Markdown
Contributor

What, why and how of this PR

Issues

Closes #219

Part of #104 and #87

Summary

validate in src/harness/lexbench/verification.py was one function of about 780 lines. It is now a 56-line sequence of 37 named, typed checks in the same file, plus one module-level helper (_resource_document, previously nested). The only other additions are the Json and ModeCall aliases and annotations on near and fail.

Check bodies are moved, not rewritten, so every rejection keeps its message and order. Rendered reports do not change: the golden route hashes are untouched. verify_evidence, validate_release and the command-line entry are not part of this change, and verify_evidence remains long (178 lines).

CI fix outside the split

7bcb275 is a separate fix that this PR needs to pass CI; it does not touch verification.py. After the rebase onto #228, "Tests and static checks" failed twice in a row on tests/test_native_cdp_port.py, in different tests, each with PermissionError: [Errno 13] on /proc/<pid>/fd from _linux_listens_on. Both tests have a server that exits while it is being polled. Main passed the same tests on its own run, so the failure depends on timing.

The likely cause is that Linux makes root the owner of that directory for a process that is exiting, while its socket table can still be read. That reading comes from kernel behaviour. The lookup caught only FileNotFoundError and ProcessLookupError, so the error ended the call instead of letting the poll loop see the exit. PermissionError now means the listener is not proved, so no connection is made on that basis. This was not reproduced on Linux locally; CI is the Linux check.

Frozen bindings

Nothing current binds this file's bytes, so the issue's stop condition does not apply.

  • The base file's SHA-256 appears in no Git-tracked file and in no JSON, JSONL, Markdown or text file under this machine's data home.
  • harness/lexbench/verification.py is named in the harness_files map of one Web Fetch pre-call contract and its two later versions, with a hash from an earlier revision. As PR fix(lexbench): write chart SVGs as explicit UTF-8 #211 established, harness_files in private pre-call contracts is a point-in-time record of the source tree, not a frozen binding. No code under src, scripts or tests reads that field, and the contracts were not edited.
  • The report build and packager do not publish this file. The search covered this machine only; packages published elsewhere were not opened.

Before/after evidence

Statement identity. A script inlines every check back into validate and compares the result with the base function's AST. All 84 top-level statements are identical, the nested helper body is identical, and the only module-level differences are the three additions above (annotations ignored). Because this compares every statement, it also covers the rejections the runs below did not reach. This is the strongest evidence here and was rerun on the final file.

Differential runs. Each altered input ran through both versions, once normally and once with fail recording and continuing, so later checks also ran. A run compares the outcome, the failure sequence and a trace of calls and file reads. Inputs alter the model, report files or language metadata.

Site Seed Inputs run / planned Accepted ValueError Other error Mismatches Distinct messages
fixture 31 91 / 3,322 8 65 18 0 36
macOS 21 278 / 27,419 0 276 2 0 59
minimal 21 1,521 / 11,155 0 1,521 0 0 64

These runs were cut off by a time budget, so they are a shuffled sample and not the full plan. Limits to read them by:

  • The macOS and minimal runs used the commit before the formatting pass below; the fixture run came after it. All three predate the last formatting restoration in main. Formatting left the AST unchanged.
  • Only the fixture model passes unaltered, so it is the only site that exercises acceptance. The macOS and minimal baselines reject at fixed functional methods and denominator. The accepted evidence is therefore 8 inputs in this table.
  • The planned seed 11 results for macOS and minimal were never written because those runs did not finish, so these are new runs with seed 21.
  • Earlier runs from the draft (fixture seed 11, Linux seeds 1, 7 and 11, macOS seed 1; 834 inputs, 0 mismatches) predate its commit, so I cannot show they ran on these exact bytes. Linux was not rerun.
  • Across all of these runs, 96 of the 132 constant rejection messages were reached. The other 36 rest on the statement identity above, for example highlight contract, correction scope and unsafe evidence target.

Negative controls. Four altered copies of the new file were all caught by both the differential and the statement comparison: two checks swapped, one message changed, one check dropped, and two checks swapped inside a check.

Test suite comparison. Running the Lexbench tests with both versions called on every validate call gave 113 calls and 0 differences.

The negative controls and this comparison ran on the commit before the last formatting restoration in main, and gave the same results on the draft's bytes. That restoration leaves the AST unchanged.

Review changes to the draft

Reviewed as someone else's draft and rebased onto current main. The only edit is ruff format applied to the new checks, which fixes over-long lines the move left behind. The rest of the file keeps its existing formatting.

Test plan

Run locally on macOS with the locked environment. CI covers the current head.

  • pytest on tests/test_golden_reports.py, tests/test_explicit_utf8_io.py and the Lexbench and Moli report, resource and verification tests, -n 2: 260 passed, 26 subtests passed. Golden snapshots were not regenerated.
  • tests/test_moli_provider.py, tests/test_golden_reports.py and tests/test_data_home.py on the rebased head, -n 2: 106 passed, 63 subtests passed.
  • An earlier tests/test_moli_provider.py run on the draft commit failed 1 subtest, test_answer_capacity_does_not_limit_native_resource_downloads, with a provider time budget timeout while other work loaded the machine. It passed alone and in the run above. That test exercises the provider, not this file.
  • ruff check --config 'include=["*.py"]' --select F,TID252 . and ruff check: all checks passed on the current head.
  • On the current head, rebased onto main with fix(web-fetch): prove native CDP server ownership before connecting #228 (which does not touch this file): tests/test_golden_reports.py, tests/test_explicit_utf8_io.py and the Lexbench and Moli report tests, -n 2: 218 passed. The statement identity script was rerun, and the code after validate is byte-identical to the base.
  • tests/test_native_cdp_port.py, tests/test_explicit_utf8_io.py and tests/test_golden_reports.py with the CI fix, -n 2: 97 passed, 1 skipped. The skipped test is the Linux-only unreaped-child test, which only CI runs. It probably does not reach the PermissionError branch, because a fully exited child likely has no socket table left; the two mocked denied-lookup tests guard that branch. With the fix removed, both denied-lookup tests fail with PermissionError.
  • After the round 2 comment-only change (1f7ecdc), the same three test files gave 97 passed, 1 skipped, and both ruff commands passed.
  • The full suite was not run locally; CI (Linux) is the full-suite gate.

@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 · be6dcd2

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

Reviewed head be6dcd27. No P0, P1 or P2 defects found. There are two P3 inaccuracies in the PR description; neither affects behaviour.

Findings

P3, src/harness/lexbench/verification.py:1228 (main). The ruff format pass also collapsed the --profile parser.add_argument call in main onto one line. The description says the formatting only touched the new checks, that the rest of the file keeps its formatting, and that the command-line entry is not part of this change. The AST is unchanged, so this is harmless. Suggested fix: restore the three-line form, or say in the description that this one line changed. Everything else after validate is byte-identical.

P3, PR description, "Across all of these runs, 95 of the 132 constant rejection messages were reached". The saved results give 96 of 132, leaving 36 unreached. locale shell is listed as an unreached example, but the fixture seed 31 result records it as reached. The summary appears to have been computed before that result was written. Suggested fix: change to 96 and 36, and use a different example. The error understates coverage, and the statement identity covers these messages either way.

What I verified

  • Statement identity. My own inliner, separate from the draft's script, expands every stage back into validate. All 84 top-level statements match the base function, and the nested helper body matches. All 132 constant messages are still present.
  • Scope changes the AST comparison cannot see:
    • Each stage's parameters match the names passed by its caller.
    • No stage reads a module global where the old function read a local, and no stage rebinds a parameter.
    • A definite-assignment pass finds no stage local that relied on a value left over from another stage.
    • Stage locals that would have overwritten a caller's variable in the old shared namespace are rebound before the caller reads them (summary, mode), or are returned to the caller (layouts, summary).
    • Conditionally bound variables (profiles, summary, method_id, candidate) are only passed inside the branch or loop that binds them, so no new UnboundLocalError is possible.
  • Module-level changes. The only differences before validate are the Any, NoReturn import, the Json and ModeCall aliases, and annotations on near and fail. Annotations are lazy under from __future__ import annotations. Every stage parameter and return is annotated, and the return types match what the stages return.
  • My own differential. Old and new validate ran on the test fixture with a fresh seed: 338 mutated models (delete, null, empty, bump or retype) out of 1,495 planned, cut off by a time budget, plus 50 file removals or truncations. Outcomes: 37 accepted, 242 ValueError, 109 other errors, 69 distinct messages, 0 mismatches.
  • Reported differential numbers. The saved results match the table (91/3,322, 278/27,419 and 1,521/11,155, with the same outcome counts and 0 mismatches), and the 834 earlier inputs also match. The table is correctly described as a partial shuffled sample.
  • Frozen bindings. The base file's SHA-256 appears in no tracked file and no JSON, JSONL, Markdown or text file in the data home on this machine. The path appears in the harness_files map of exactly the three pre-call contract versions described. No code under src, scripts, tests, evaluation or configs reads harness_files, which is consistent with PR #211. No contract or evidence was changed.
  • Scope. The PR meets each item in #219, so Closes #219 is justified. The split stays in the same file, no compatibility alias or parallel implementation was added, and only this one file changed.
  • Tests and lint at this head:
    • tests/test_golden_reports.py with the Lexbench tests: 156 passed. Golden snapshots are untouched.
    • tests/test_explicit_utf8_io.py and tests/test_moli_report_render_contract.py: 64 passed.
    • Both ruff check gates pass.
  • CI at this head. "Tests and static checks", "Installation and packaging", "Rendered reference and report checks", and "Python 3.11" all pass. Main has moved on by #228, which does not touch this file.

@lanyue-llk
lanyue-llk force-pushed the refactor/lexbench-validator-checks branch from be6dcd2 to b81e999 Compare November 4, 2026 06:26
@lanyue-llk

lanyue-llk commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Round 1 review findings, both fixed. Nothing disputed.

  • P3, --profile formatting in main: restored the original three-line form in b81e999. The code after validate is now byte-identical to the base, and the AST is the same as the reviewed head. No test added, because this is formatting only.
  • P3, reached-message count in the description: the saved results give 96 of 132 reached and 36 unreached, and fixture seed 31 did reach locale shell. The description now says this and names highlight contract as the unreached example instead.

The branch is rebased onto main with #228, which does not touch this file, so the split commit is now bd09027. The description also notes which evidence predates the formatting restoration, and its test plan lists the rerun on the current head.

CI fix added after this reply was first posted: "Tests and static checks" failed twice on the rebased head, in tests/test_native_cdp_port.py, with PermissionError on /proc/<pid>/fd. That is a timing race in the code from #228, not in the split. 7bcb275 makes the lookup treat that error as "listener not proved" and adds tests, including a Linux-only one with a real unreaped child. The PR description has the details.

@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 · b81e999

No findings in this review.

@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 · 7bcb275

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 2

Head reviewed: 7bcb275. No P0, P1 or P2 defects found. There is one P3 finding, which does not block this PR.

Finding

P3: src/harness/web_fetch/providers/headless.py:998 and tests/test_native_cdp_port.py:347. The new comment and the Linux-only test probably describe a different state from the one CI hit.

  • Problem: The fix itself is sound. However, in a fully exited, unreaped child, the kernel has already released the namespaces, so open('/proc/<pid>/net/tcp') should raise FileNotFoundError before listdir runs. If so, test_exited_unreaped_server_is_not_reported_as_listening passes with or without the fix.
  • What CI showed: The reported PermissionError on /proc/<pid>/fd means net/tcp opened successfully. That points to a process partway through exit: memory already released, so root owns fd, but namespaces still present.
  • Confidence: This comes from reading the kernel's behaviour. I could not run it on Linux here because no Linux host or container was available.
  • Coverage: The two mocked denied-lookup tests do guard the fix, and the PR description already says the cause was not reproduced.
  • Suggested fix: Reword the comment to "a process that is exiting", and keep or drop the Linux test knowing that it does not exercise this branch.

What I verified

Split of validate

  • I reran the statement-identity script against base 0051691. All 84 top-level statements are identical and the nested helper body is identical. The only module-level additions are the typing import, Json and ModeCall.
  • That script cannot see scope changes caused by moving statements into functions, so I checked those separately with ast and symtable:
    • No check rebinds or mutates one of its parameters.
    • No name in a check resolves to a module global where the old function had a local of that name.
    • No name is undefined.
    • A definite-assignment pass over all 37 checks and validate found no read that could have relied on a stale local left by an earlier stage.
    • Names assigned more than once in the old function (summary, chart, mode and others) reach each check with the same binding.
  • All 37 checks and validate have annotations on every parameter and return.
  • Everything from validate_release to the end of the file is byte-identical to the base, so round 1's --profile finding is fixed.

PR description

  • The results for fixture seed 31, macOS seed 21 and minimal seed 21 match the table: inputs, planned counts, accepted, ValueError, other errors, 0 mismatches and distinct messages. I did not rerun the differential.
  • Frozen bindings claim:
    • The base file's SHA-256 is in no Git-tracked file and in no JSON, JSONL, Markdown or text file under this machine's data home.
    • The only structured binding of the path is harness_files in one pre-call contract and its two later versions, with a different, older hash.
    • No code under src, scripts or tests reads that field.
    • The only other JSON mentions are prose in two private GitHub text backups.

CDP fix

  • Treating PermissionError as "listener not proved" fails closed: no connection is made, and the poll loop still sees the exit or hits the deadline.

Scope and rules

  • Closes #219 holds: the split is in the same file, messages and order are preserved, and the before/after proof is present.
  • No evidence or profile changed, there are no aliases or parallel implementations, and the new text is in English.

Checks run on this head

  • ruff check (both invocations) passed.
  • tests/test_golden_reports.py and tests/test_native_cdp_port.py, -n 2: 35 passed, 1 skipped (the Linux-only test). Golden snapshots were not regenerated.
  • tests/test_lexbench_verification.py, test_lexbench_cli.py, test_explicit_utf8_io.py, test_moli_report_render_contract.py, test_moli_resource_matrix.py and test_lexbench_report_runtime.py, -n 2: 141 passed, 5 subtests passed.
  • CI on this head: all checks pass, including "Tests and static checks" and "Installation and packaging".

@lanyue-llk

Copy link
Copy Markdown
Contributor Author

Round 2 review finding, fixed in 1f7ecdc. Nothing disputed.

  • P3, comment in _linux_listens_on: the comment now says "a process that is exiting" instead of an exited, unreaped child. I kept test_exited_unreaped_server_is_not_reported_as_listening, because a real unreaped child should still not count as listening, and added a note that it probably does not reach the PermissionError branch. The two mocked denied-lookup tests guard that branch.

The new wording rests on reading kernel behaviour. It was not run on Linux here. The change is comments only, and the PR description now uses the same wording.

@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 · 1f7ecdc

No findings in this incremental 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 3

No defects found at head 1f7ecdc. The two earlier findings are fixed, and I am raising no new ones.

What I verified

Statement identity (my own script, not the author's).

  • I inlined all 37 checks and the nested helpers back into validate and compared the result with the base function's AST. All 84 top-level statements are identical.
  • The body of _resource_document is identical to the nested resource_document, which was only called from inside the Linux block.
  • near, fail, validate_release, verify_evidence, verify and main match once annotations are ignored. The only module-level additions are the typing import and the Json and ModeCall aliases.
  • Two altered copies, one with two checks swapped and one with a changed message, both failed this comparison.

Differences that inlining cannot show. I checked these separately:

  • Every call-site argument is a plain name, so no expression is evaluated earlier than it was in the base.
  • No helper has an early return, rebinds a parameter, has an unused parameter or reads a local before assigning it.
  • No variable written inside a check is read by a later check unless it is returned. Only cost_by_id, full and common cross between checks this way, and each is returned. I ran this analysis at the top level of validate; for the checks nested inside _check_platform_resources and the mode loop, I relied on the parameter and local-read checks above and on reading the code.

PR description.

  • The table matches result-fixture-31.json, result-macos-21.json and result-minimal-21.json, all with 0 mismatches. The earlier 834-input total also matches the saved draft results.
  • The line counts and the check count are correct. The base file's hash appears in no Git-tracked file, and nothing under src, scripts or tests reads harness_files.
  • I did not rerun the differential, the negative controls on the author's script or the data-home hash search.

Scope. Issue #219 asks for named typed checks in the same file, the same messages and order, a before/after run on altered inputs, and a statement on frozen bindings. All four are present, so Closes #219 holds. The runs are a sample cut off by a time budget, and the description says so. The statement identity covers the rest.

CDP fix.

  • With PermissionError removed from both handlers, both denied-lookup tests fail.
  • With the fix in place, a lookup that stays denied ends in the existing startup TimeoutError rather than a connection, so it fails closed.
  • The Linux-only test is skipped on macOS, so only CI runs it.

Local runs on macOS.

  • tests/test_golden_reports.py, tests/test_lexbench_verification.py, tests/test_native_cdp_port.py, tests/test_explicit_utf8_io.py and tests/test_data_home.py, -n 2: 132 passed, 1 skipped. Golden snapshots are unchanged.
  • Both ruff commands pass.

CI at 1f7ecdc. All checks pass, including Tests and static checks, Installation and packaging, and Rendered reference and report checks. The base is still the newest main.

lanyue-llk added a commit that referenced this pull request Nov 4, 2026
@lanyue-llk
lanyue-llk merged commit 85693b0 into main Nov 4, 2026
7 checks passed
@lanyue-llk
lanyue-llk deleted the refactor/lexbench-validator-checks branch November 4, 2026 07:28
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 validator into named checks

1 participant