Skip to content

refactor(web-fetch): select providers and headless engines through registries - #184

Merged
lanyue-llk merged 4 commits into
mainfrom
refactor/web-fetch-provider-registry
Nov 3, 2026
Merged

lanyue-llk merged 4 commits into
mainfrom
refactor/web-fetch-provider-registry

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

Remaining engine branches outside this scope are tracked in #183.

Summary

  • Provider registry: PROVIDER_TYPES in providers/base.py replaces the build_provider if-chain. A new provider is one entry. Modules still load on demand.
  • Engine registry: ENGINES in providers/headless.py now maps each engine name to a HeadlessEngine definition. It holds command-line templates, CDP server arguments, completion semantics, task routes, recorded metadata and the hosted backend. All engine-name branches in that file are gone.
  • Command lines pinned first: the first commit records tests/golden/headless-engine-routes.jsonl from the unchanged if-chain code. The second commit does the refactor and leaves that snapshot untouched. A later test commit adds an all-options CDP server variant that pins argument order; those lines were also recorded from the if-chain code. The snapshot covers CLI commands, CDP servers, completion metadata, output modes, fetch routes, validation, implementation identity and cancellation.

Frozen bindings

  • headless_worker.py is deliberately unchanged, and its SHA-256 is the same. It is recorded as worker_sha256 and execution_sha256 on every call and as adapter_input_sha256 in the cohort comparison. ContainerResourceRunner.validate also rejects a resource image whose worker bytes differ from the host, so editing it means rebuilding every declared image. Its engine branches therefore remain; Move remaining headless engine branches into the engine registry #183 tracks them, along with the literals in task_outputs.py and network_preflight.py.
  • New runs get a new runtime_source_sha256. It hashes all of web_fetch/**/*.py, so any source edit changes it. A cohort already in flight must finish on its frozen source, as with any other change here.
  • No evidence, profile, receipt or rendered report changed.

One behaviour change

HeadlessProvider.fetch now raises ValueError for an unregistered engine. Before, an unvalidated provider continued, recorded made-up metadata such as moli_cdp_response, and launched the binary through Playwright. validate() already rejected such a configuration with engine, so a validated run never reached this path. Unregistered names stay accepted by output_mode_for_case, because reports use them as labels.

Test plan

  • ruff check --select F,TID252 and ruff check: pass.
  • tests/test_headless_engine_routes.py: pass on both commits, with the same snapshot.
  • All-options CDP server lines: recorded at the if-chain commit and matched by the registry code. Swapping the last two slots for Moli, Lightpanda or Obscura fails the snapshot test (one-off check, not committed).
  • tests/test_provider_registry.py (new): pass.
  • tests/test_golden_reports.py: pass, with no snapshot changes.
  • One-off comparison against the code at the base commit, not committed:
    • cli_command gave identical results on 221,184 combinations of engine, mode, plan, native metadata, timeout and options.
    • All 19 provider types and the unknown-type error match the old if-chain.
  • Full suite on an extracted copy without .git: 19,636 passed, 60 skipped, 27 failed.
    • 15 of the 27 are on the known local macOS failure list.
    • The other 12 are in test_optional_reports, test_packaging_hygiene, test_public_tree_privacy, test_report_software_versions, trajectory_replay/test_hf_inputs and trajectory_replay/test_run_webarena_catalog.
    • I re-ran those six files in the git worktree: only the 3 test_optional_reports tests still fail, with a dataset lock error. The worktree sits inside an older checkout, and dataset path resolution finds that checkout's binding. The 4 test_headless_runner and test_selected_native_policy failures seen only in the worktree have the same cause, and those two files pass on an extracted copy of the base commit.
  • CI on Linux is the full-suite gate for these local-only failures.

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

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: af99f1d6. Two findings, both about what the change pins or documents. I found no behaviour regression in the code.

Findings

P2: tests/test_headless_engine_routes.py:140 does not pin native CDP server argument order

  • Problem: _server_commands only exercises one option at a time, so no scenario has two optional arguments together. The relative order of the server template slots is therefore unpinned, though #180 asks to pin every command line and the PR body lists CDP servers as covered.
  • Evidence: I swapped these slots in ENGINES, one engine at a time. Each mutant passed the route snapshot and seven other headless and native test files (323 passed, and the only failure is on the known local list):
    • Moli: _LOG_LEVEL, _BLOCK_PRIVATE_NETWORKS, _CA_CERT, _PROXY
    • Lightpanda: _CA_CERT, _PROXY, _PROJECTION_IFRAMES
    • Obscura: --quiet, _PROXY, _PRIVATE_NETWORK
  • Fix: Add an all-options variant to _server_commands, as _cli_commands already does with all-network. It should set proxy, CA certificate, private network, block private networks, log level and output_mode: resource. Record its snapshot lines from the base commit's code, so the pin still proves the old order. The current order is correct (see below), so only the pin is missing.

P2: docs/provider-contract.md:89-92 overstates what ENGINES owns

  • Problem: "Add or change engine behaviour there" is not yet true. headless_worker.py still selects routes by engine name (lines 233, 258, 281, 299, 353, 357, 386 and 423). For example, line 281 only runs the CLI route for moli, obscura and lightpanda. task_outputs.py:183 rejects any other engine, and network_preflight.py:78 tests kitesurf by name. Someone adding an engine from this paragraph would get a command line the worker never runs. The PR body and #183 disclose this, but the permanent doc does not.
  • Fix: Say that a new engine still needs matching changes in those three files until #183 lands, and that editing the worker means rebuilding the declared resource images.

Verified with no defect

  • Old against new behaviour: I loaded the base commit's headless.py next to the new one and compared results, including error type and message:

    Function or path Inputs compared Differences
    cli_command 124,131 sampled combinations 0
    native_cdp_browser argv and environment, with all option combinations 960 0
    completion_metadata 432 0
    output_mode_for_case 2,880 0
    fetch, complete metadata, worker request and Cloudflare config 5,940 0
    _resolve_binary 60 0
    validate 1,792 16
  • The 16 validate differences: All have engine: None with native_metadata: {}. The old code raised TypeError and the new code reports engine. This is harmless, though the PR body does not mention it.

  • Snapshot provenance: The route test passes on the first commit, which has the unchanged code, and the second commit changes neither the snapshot nor that test.

  • Mutation check: 19 of 21 ENGINES field mutants fail the route test. The other two are the Moli slot order above and retain_cdp_server_log, whose removal is caught by test_native_cdp_port.py.

  • Provider registry: All 19 types resolve to Provider subclasses, and modules still load on demand. The unknown-type error is unchanged. I read the mapping against the removed if-chain and it matches.

  • Frozen bindings:

    • headless_worker.py is byte-identical, and the worker imports only functions that still exist.
    • No file under evaluation/ or configs/, and no report snapshot, changed.
    • runtime_source_sha256 does change for new runs, because it hashes the source tree. The PR discloses this.
    • Request fields sent to resource containers match the old code, so existing images stay compatible.
  • Scope: Closes #180 holds. No engine-name branch remains in headless.py, and the items left over are outside the two files the issue targets or are bound by the worker hash. #183 tracks them.

  • Local checks: Both ruff commands pass. tests/test_golden_reports.py gives 10 passed. New and neighbouring headless tests pass, apart from two failures on the known local macOS list. The snapshot contains no private paths.

CI

All checks on af99f1d6 pass: Tests and static checks, Installation and packaging, Rendered reference and report checks, Python 3.11, Select check scope, OCR dispatch and ocr-review. main has not moved from the PR's base f877f6a6.

@lanyue-llk

Copy link
Copy Markdown
Contributor Author

Review round 1 follow-up. Both findings are fixed; none are disputed.

  • P2, CDP server argument order (11b28efa): _server_commands now has an all-options variant with proxy, CA certificate, private network, block private networks, log level and output_mode: resource. I recorded its snapshot lines by running the new test against the if-chain code at 84e4fa25. The registry code matches them, and no existing snapshot output changed. Swapping the last two template slots for Moli, Lightpanda or Obscura now fails the snapshot test (one-off check, not committed).
  • P2, docs/provider-contract.md (2cc44a88): the paragraph no longer says all engine behaviour belongs in ENGINES. It now says a new engine also needs matching changes in providers/headless_worker.py, task_outputs.py and network_preflight.py until Move remaining headless engine branches into the engine registry #183 lands, and that editing the worker means rebuilding every declared resource image.

Local checks: ruff passes. test_headless_engine_routes, test_provider_registry, test_native_route_contract and test_golden_reports pass (189 tests). test_documented_paths, test_headless and test_resource_contract_lifecycle have one failure, test_converter_shared_and_nonzero_not_converted, which is on the known local macOS failure list.

@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 skipped · 2cc44a8

No reviewable content was selected for this run.

@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: 2cc44a88, on base f877f6a6, which is still the tip of main. No defects found. Both round 1 findings are fixed.

Round 1 fixes

  • CDP server argument order: the all-options variant is in _server_commands. Its snapshot lines match the if-chain code (see the base-commit run below).
  • docs/provider-contract.md: the paragraph now names the worker, task_outputs.py and network_preflight.py, and all three claims hold. The engine-set check is at task_outputs.py:183, the kitesurf test is at network_preflight.py:78, and the resource image worker check is in ContainerResourceRunner.validate.

What I verified

  • Snapshot against the old code: I copied the head's test file and headless-engine-routes.jsonl onto the unmodified base commit. All 8 tests pass, so the snapshot does describe the if-chain behaviour, including the all-options lines.
  • Old and new code compared directly: I loaded both versions of headless.py side by side and found 0 differences across 403,072 randomized or enumerated comparisons.
    • cli_command and completion_metadata, including error type and message.
    • output_mode_for_case, including unregistered names and a third provider type.
    • Inputs included frame selectors, delays around the whole-second boundary, an empty proxy string and fractional timeouts. This was a one-off check and is not committed.
  • Line-by-line reading: the wait_until and extractor chains, the subdocument interface and support values, validate, implementation_identity, the hosted Kitesurf fields and the native CDP server arguments all match the old branches. Every template slot is supplied on the route that uses it, so no placeholder can reach a command line.
  • Do the tests catch changes? I made 16 one-off changes to the ENGINES data. The route snapshot caught 15. The other, turning off retain_cdp_server_log, is caught by tests/test_native_cdp_port.py.
  • Disclosed behaviour change: fetch now raises for an unregistered engine. validate() already rejected those engines, and the only other caller I checked, providers/moli.py, passes engine: moli.
  • Frozen bindings: headless_worker.py is byte-identical to base. The PR changes nothing under evaluation/ or research/, or the report data under src/harness/web_fetch/data/. The only golden file touched is the new route snapshot. tests/test_golden_reports.py passes with no snapshot changes.
  • Scope: #180 names build_provider and the branches in providers/headless.py. The only engine-name check left in that file is engine in ENGINES, and the registry now owns the rest. The remaining branches in other files are tracked in #183, and the worker's hash binding is disclosed as the issue requires. Closes #180 is accurate.
  • Public repository check: I found no local usernames, paths or credential values in the golden file. The only hit is the api_token field name in a validation error list.

Test runs

  • Both ruff gates pass.
  • Local targets: test_headless_engine_routes, test_provider_registry, test_golden_reports, test_headless, test_headless_completion, test_headless_files, test_headless_cancellation, test_native_cdp_port, test_native_task_outputs and test_report_method_registry. Result: 201 passed, 11 skipped, 1 failed. The failure, test_headless.py::HeadlessTests::test_converter_shared_and_nonzero_not_converted, is on the known local macOS list.
  • I did not run the full suite locally. CI on this head is the full-suite gate, and all checks pass, including "Tests and static checks" and "Installation and packaging".

lanyue-llk added a commit that referenced this pull request Nov 3, 2026
@lanyue-llk
lanyue-llk merged commit 8384f9c into main Nov 3, 2026
7 checks passed
@lanyue-llk
lanyue-llk deleted the refactor/web-fetch-provider-registry branch November 3, 2026 19:44
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.

Select Web Fetch providers and headless engines through a registry

1 participant