Skip to content

refactor(web-search): split build into named typed stages - #223

Merged
lanyue-llk merged 2 commits into
mainfrom
refactor/web-search-report-stages
Nov 4, 2026
Merged

lanyue-llk merged 2 commits into
mainfrom
refactor/web-search-report-stages

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 #218

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

Summary

build in src/harness/web_search/report.py was 817 lines. It is now a 36-line sequence of named, typed stages in the same file: validate options, load inputs, write the public bundle, write each language edition, write the manifest, select the entry and run the evidence check. The longest stage is _default_home_body at 72 lines. ReportInputs carries the loaded evidence and ReportEdition carries one language's prefix, navigation and notice; both are frozen dataclasses.

The code was moved by script from line ranges of the old file rather than retyped. An AST comparison of the old function and the new stages shows these differences only:

  • Summaries are computed once. The old function had six aggregate calls, and the results of four were never read. ReportInputs.overview_summary and all_call_summary replace them. aggregate is pure, so output does not change. If it raised on malformed rows, the all-call failure would now happen before any page is written rather than after.
  • A local closure page became the module-level _write_page, and stages bind it with partial.
  • hit_key moved to _known_page_metric; reason_label and dataset rows moved to _reason_label and _dataset_row.
  • The ds loop became a list comprehension. A repeated-calls block became an early return in _physical_calls_detail.
  • New signatures, annotations, returns and locals read from the two dataclasses.

All other functions and module-level statements are AST-identical, apart from the three new imports.

One snapshot line moves

tests/golden/web-search.json changes on one line, manifest.json. This is unavoidable: the manifest records the SHA-256 of report.py itself as renderer_sha256, and the last six edits of this file each changed one line of this snapshot (#212, #192, #177, #163, #146, #137). The other 18 Web Search routes are unchanged. Replacing the new hash with the old one in the rebuilt manifest gives exactly the old snapshot digest, 7df3bd51..., so that field is the only difference.

Frozen bindings: none found

  • git grep finds neither the file's SHA-256 nor its Git blob ID.
  • configs/report-templates.json and src/harness/reporting/source_policy.py record only the path, which does not change.
  • Run contracts exclude this file; tests/web_search/test_runner.py asserts that. implementation_sources covers lexbench_websearch only.
  • renderer_sha256 is written per build. Nothing in the repository reads it back, so existing reports keep verifying.

Report instances outside Git were not available to inspect, but the only binding to this file a build writes is that unread field.

Test plan

Run locally on macOS with Python 3.11 from the shared locked environment.

  • Golden snapshots: tests/test_golden_reports.py passes with the one-line change above.
  • tests/test_golden_reports.py, tests/web_search, tests/test_report_presentation_contract.py, tests/test_explicit_utf8_io.py, tests/test_public_tree_privacy.py, tests/benchmarks/test_checkout_entrypoint_precedence.py: 660 passed, 5 failed. The failures are a local setup problem: the import path was set with pytest's -o pythonpath, which subprocesses do not inherit, so they import the shared environment's older installed harness or none. They are test_rubric_import_and_judge_help_are_cold_offline, test_report_builds_are_byte_identical_under_an_ascii_locale and three test_lexbench_report_builds_under_an_ascii_locale cases. The offline-import test failed the same way before the split. CI covers all five.
  • tests/test_source_language_policy.py, tests/test_public_report_language.py, tests/test_report_language_routes.py and tests/test_report_languages.py pass.
  • Lint gate: ruff check --config 'include=["*.py"]' --select F,TID252 . and ruff check pass.
  • Before/after differential, using throwaway scripts outside the repository: every build call made by tests/web_search ran on a copy of the old module and on the new one, on the same inputs in one process. 166 builds (164 complete, 2 fail) plus 1,408 replays across 22 input shapes, each raising from one helper the build called. The output-tree digest, return value, exception type and message, and ordered helper-call trace are identical in all of them, with 94 distinct error messages. The comparison normalizes renderer_sha256 and temporary paths, and leaves aggregate and scoring_rows out of the trace because of the deduplication above.
  • Two tests were excluded from that in-process run because they patch helpers with side effects, which a second build disturbs: test_projection_only_publishes_metadata_for_materialized_calls and test_content_hash_binds_rendered_snapshot_when_source_changes_during_build. A separate old-then-new sequential run included them, and their digests and traces matched.
  • Negative control: swapping _write_method_pages and _write_performance_page makes the differential report a mismatch in both the manifest and the trace.
  • Line trace: 465 of 493 statements in the stages ran under tests/web_search. Of the 28 not reached, 3 are docstrings. The rest are rare branches, such as profile-only builds, identity corrections and unsafe asset paths; they were moved verbatim, and the AST comparison covers them.

Limit: these builds use synthetic evidence. No real evidence was rebuilt.

  • CI on Linux at fc1a7d2d: "Tests and static checks" (which includes the five local-only failures above), "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 · a9fdd8b

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 a9fdd8b4 against base 332084e5. One finding (P3). The behaviour-preservation claims hold.

Findings

P3: cohorts is annotated as Iterable[Any], but the code needs a sized collection. src/harness/web_search/report.py:531, :624, :1537, :1604

  • Problem: _load_collection tests if cohorts: (line 534) and _write_manifest tests not cohorts (line 1581). Both rely on truthiness, which an iterator does not provide. The annotation this PR adds says any iterable is acceptable, so it misstates the contract. The behaviour itself is unchanged, and the CLI passes a list.
  • Reproduced on this head:
    • build(out, source=src, cohorts=iter(())) sets collection_receipt_sha256 to null, while cohorts=() records the hash.
    • build(out, cohorts=iter(())) raises "Additional cohorts require a primary source".
  • Fix: annotate all four sites as Sequence[tuple[str, Path]]. That is the shape load_report_runs unpacks and main builds. Then drop the Iterable import.

What I verified

  • AST: my own comparison of the old and new modules shows build is the only changed function. All other definitions and module statements are identical and in the same order, and the only added statements are the three imports. I read all 817 old lines against the new stages and found no reordering of side effects. That covers the independent-review.json write before the assessment profile, the bundle order, route removal before the public calls, the reason_label override after the inventory row, and the review and relevance rebinding.
  • aggregate deduplication: aggregate in core.py is pure, and overview_summary and all_call_summary are only read. The four dropped calls were never read. The all-call failure timing in the PR body is slightly understated: the failure now comes before the public bundle writes and before old routes are removed, not just before pages. That is harmless.
  • Independent differential: I wrapped build with a pytest plugin and ran all of tests/web_search on base and head, with builder identity pinned and renderer_sha256 and temporary paths normalized. That gave 166 paired builds and 3,391 files, with the same return values and exceptions. 15 builds differed, all in JSON fields:
    • manifest.json and independent-review.json hashes that derive from a review frozen against the fixture's first manifest, which carries the renderer hash.
    • Timestamps and receipt hashes from live-collection tests.
    • No route file differed. The pin made 71 of the 520 tests fail on both trees because they assert builder identity. Those builds were still compared up to the point of failure. correction was not exercised, so I checked that branch by reading it.
  • Snapshot: only manifest.json moves in tests/golden/web-search.json. This does not meet the issue's literal "snapshots do not move", but it is disclosed and unavoidable while the manifest records the file's own hash. All six earlier edits to this file since the snapshots were added moved that snapshot too. A direct old and new build of the same fixture differs only in renderer_sha256, browser_eval and the bundle hash of the version file, plus browser-eval-version.json itself.
  • Frozen bindings: none found.
    • The file's SHA-256 and blob ID do not appear in Git at base.
    • renderer_sha256 is written once in the Web Search code and never read there.
    • producer_sources in web_search/visual.py covers visual.py, pages.py and the shared reporting/visual.py, not this file.
    • A review frozen with base code builds and passes check under this head, because the review's source_bindings hash of the manifest is not compared with the current manifest.
  • Tests and lint at this head:
    • tests/web_search: 520 passed.
    • Golden, presentation contract, UTF-8, privacy, entrypoint precedence and language suites: 265 passed, with the correct PYTHONPATH. That includes the five failures the PR body attributes to its local import path.
    • Both ruff gates pass.
  • Scope and rules: Closes #218 is accurate. build is 36 lines and the longest stage is 72. There are no aliases or parallel implementations, no evidence files changed, and new text is in English.
  • CI at a9fdd8b4: "Tests and static checks", "Installation and packaging", "Rendered reference and report checks" and "Python 3.11" all pass.

@lanyue-llk

Copy link
Copy Markdown
Contributor Author

Review round 1: fixed the one finding, nothing disputed.

  • P3, cohorts annotation: fixed in fc1a7d2. All four sites (_load_collection, the input-loading stage, _write_manifest, build) now use Sequence[tuple[str, Path]], and the Iterable import is gone. This is annotation-only, so no behaviour test applies: the truthiness checks and the CLI's list are unchanged.

tests/golden/web-search.json moves on the manifest.json line only, because the manifest records the hash of report.py as renderer_sha256. A diff -r of normalized builds before and after this commit shows that field as the only difference.

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

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

Reviewed head fc1a7d2d against main 332084e5, which is still the current main. No defects found.

What I verified

  • Round 1 fix: all four cohorts sites now use Sequence[tuple[str, Path]], the shape that load_report_runs unpacks and main builds, and the Iterable import is gone. The commit changes only those annotations and one line of tests/golden/web-search.json.
  • Code against the old function: I read every stage against the old build.
    • Write order, route order, comparison chapters before index.html, and reason_label being overridden only after the inventory row is built all match.
    • A symbol-table check found no stage reading a module global that shares a name with an old build local, so no variable was silently rebound.
  • Summary deduplication: an AST check confirms the old summary was read only in the core table and the old physical only in the successful-call table.
    • At those points they held aggregate(scoring_rows(profile, overview_rows)) and aggregate(rows), which are exactly overview_summary and all_call_summary.
    • aggregate is pure. After each of 188 edition writes under tests/web_search and the golden tests, recomputing both summaries gave equal values, including 2 editions where the overview was narrower than all rows.
  • My own old/new differential (not the author's scripts): I wrapped build so each call also ran the base report.py from the same pre-state, then compared the file trees, return value and exception, with renderer_sha256 and temporary paths normalized.
    • 169 of 171 builds were identical, 2 of them raising the same error in both versions.
    • The builds covered zh and en editions, authored content, review and relevance.
    • The 2 mismatches come from the harness, not the code. test_projection_only_publishes_metadata_for_materialized_calls and test_content_hash_binds_rendered_snapshot_when_source_changes_during_build patch helpers on the new module only, so the base copy ran unpatched. Both tests pass on this head. These are the same two the PR excludes from its in-process run. I did not repeat its separate sequential comparison for them.
    • These builds use synthetic fixtures; no real evidence was rebuilt, as the PR also states.
  • Snapshot: only the manifest.json line moves in tests/golden/web-search.json, and the differential's normalized comparison shows no other change. I accept this as the one unavoidable move: the manifest records the hash of report.py, and the six earlier edits the PR names (#212, #192, #177, #163, #146, #137) appear in that snapshot's history. It is explained in the PR.
  • Frozen bindings: git grep at base finds neither the file's SHA-256 nor its blob ID. Under src/harness/web_search/, the only reference to renderer_sha256 is the line that writes it, and I found no reader of the Web Search value elsewhere. The other uses are web_fetch's own separate value. configs/report-templates.json and source_policy.py record only the path.
  • Size and scope claims: old build was 817 lines, new build is 36, and the longest stage is _default_home_body at 72. The diff touches only those two files. Closes #218 is accurate: #218 asks only for this split, and #104's other functions stay with #104.

Tests and CI

  • Lint: both ruff gates pass.
  • Local tests: tests/web_search and tests/test_golden_reports.py gave 536 passed. The language, presentation, UTF-8, privacy and checkout-entrypoint targets, run with the golden tests, gave 265 passed.
  • CI on fc1a7d2d: "Tests and static checks", "Installation and packaging", "Rendered reference and report checks", "Python 3.11" and "OCR dispatch" all pass. The PR is mergeable and clean.

lanyue-llk added a commit that referenced this pull request Nov 4, 2026
… pairs

The stages test cohorts for truthiness, so an iterator would change behaviour. The manifest records the renderer hash, so one snapshot line moves.
@lanyue-llk
lanyue-llk force-pushed the refactor/web-search-report-stages branch from fc1a7d2 to 0e032bf Compare November 4, 2026 04:40

@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 · 0e032bf

No findings in this review.

@lanyue-llk
lanyue-llk merged commit 7195a05 into main Nov 4, 2026
7 checks passed
@lanyue-llk
lanyue-llk deleted the refactor/web-search-report-stages branch November 4, 2026 04:48
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 Web Search report build into named stages

1 participant