Skip to content

fix(trajectory-replay): fix WebArena readiness, audit, CDP wait and import-time bugs - #125

Merged
lanyue-llk merged 5 commits into
mainfrom
fix/webarena-env-and-replay-bugs
Nov 2, 2026
Merged

lanyue-llk merged 5 commits into
mainfrom
fix/webarena-env-and-replay-bugs

Conversation

@lanyue-llk

@lanyue-llk lanyue-llk commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Issues

Closes #94. This PR covers the WebArena environment and trajectory-replay items. Other agents are handling the rest of #94 in separate PRs:

  • Unbounded process.wait() after a best-effort kill (web_fetch/providers/headless.py, moli.py)
  • Browser Use resume/fresh-run retry mismatch, lost timing on failed attempts, swallowed cleanup errors (browser_use/runner.py)
  • Three P50/P95 definitions (wcxb_scoring.py, lexbench/report.py)
  • Suspected: web_search/review.py kill/active cleanup, browser_use/backend.py port race, browser_use/runtime.py docker client leak and assert precondition
  • Deferred from this PR: the bind-port-0-then-close race in trajectory_replay/catalog_runner.py and remote_execution.py (see below)

Summary

There is one commit per bug, and each comes with a regression test. I confirmed that every test fails on the pre-fix code.

  • Rainbond readiness never accepted 4xx (environments/webarena/rainbond.py _wait_site). urlopen raises HTTPError for 4xx and 5xx, and the URLError handler retried it. The loop now checks the HTTPError code and treats 200 <= status < 500 as ready. 5xx still retries, and the timeout message now reads HTTP <status>.
  • Public WebArena report hid audit failures (trajectory_replay/public_webarena/report.py). _available_result_audit caught ResultAuditError and also skipped the audit when the task scope did not match. In both cases it rendered the results with the audit marked unavailable. I removed that helper. write_latest_report now runs audit_result_set once, before it writes any page, and lets the error propagate. It passes the passing audit to the home and review pages. The golden route hashes are unchanged. Three existing renderer tests use partial fixtures that cannot pass a full audit, so they now use an explicit audited fixture that stubs the audit. The new test test_report_fails_closed_when_result_audit_rejects_results covers the failure path.
  • _wait_cdp did not sleep on non-200 responses (trajectory_replay/remote_execution.py). Confirmed: the function only slept after an OSError, so a 2xx response other than 200 ran all 300 probes back to back and timed out almost at once instead of after about 30 s. It now sleeps after every probe that does not succeed.
  • tiktoken.get_encoding ran at import time (trajectory_replay/runner.py, public_webarena/runner.py). Confirmed: the CLIs import these runners, so with a cold tiktoken cache even --help had to download the BPE file. Each runner now loads the encoding on first use through a cached _encoding().

Suspected items not changed

  • environments/webarena/har.py keys requests by id(request): rejected. _record_playwright_request stores the request object itself in _playwright_request_objects[key] alongside the key. Responses keep their request reachable through the stored Response object. A tracked request therefore cannot be garbage-collected while the recorder holds its id, so the id cannot be reused.
  • trajectory_replay/webarena_replay.py locals().get("readiness_retries", 0) and service._submit: rejected as bugs. locals().get returns the bound value when the assignment ran and 0 when it did not, which is the intended behavior on CPython 3.11 through 3.13. It is a style issue and there is no failing test to write for it. service._submit is the only way the cowagent BrowserTool service exposes to run work on its Playwright thread, and about ten other call sites use it. Replacing it means changing the upstream API, not fixing a bug here.
  • Bind-port-0-then-close race in catalog_runner.py and remote_execution.py: confirmed, deferred. The port is released before the browser binds it, and catalog_runner reuses the same port for every browser restart during a run. If another process takes the port, _wait_cdp could accept that process's CDP endpoint. A real fix needs an ownership handoff for each engine. For Chromium that means --remote-debugging-port=0 plus reading DevToolsActivePort. Moli, Lightpanda and Obscura each need their own mechanism. This also overlaps the host/port rework in Use a single registry for WebArena hosts, ports and site credentials #96, so it stays open in Fix confirmed runtime bugs found in the codebase audit #94.

Test plan

  • ruff check --config 'include=["*.py"]' --select F,TID252 . and ruff check: pass
  • pytest tests/trajectory_replay/ tests/test_golden_reports.py tests/test_resource_accounting.py -n 2: 2061 passed, 5 skipped; golden route hashes unchanged
  • pytest tests/trajectory_replay/test_runner.py tests/trajectory_replay/public_webarena/test_runner.py tests/test_resource_accounting.py: 62 passed
  • Each new regression test fails against the pre-fix source: test_wait_site_treats_client_error_responses_as_ready (3 cases), test_report_fails_closed_when_result_audit_rejects_results, test_wait_cdp_pauses_between_non_ready_responses, test_runner_import_does_not_load_the_token_encoding (2 modules)
  • Linux CI "Tests and static checks": see the PR checks

🤖 Generated with Claude Code

lanyue-llk and others added 4 commits October 30, 2026 20:09
urlopen raises HTTPError for 4xx and 5xx, so the URLError handler retried
every 4xx until the readiness deadline. Inspect HTTPError status and treat
200 <= status < 500 as ready, as the loop intended.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…udit fails

The report caught ResultAuditError and a task-scope mismatch, then rendered
the result set with the audit marked unavailable. Run the audit once at the
write_latest_report boundary, let its error propagate before any page is
written, and pass the passing audit to the home and review pages.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… responses

_wait_cdp only slept after an OSError, so a non-200 /json/version response
ran its 300 probes back to back and timed out almost immediately instead of
waiting about 30 seconds.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Both replay runners called tiktoken.get_encoding at import time. A cold
tiktoken cache downloads the BPE file, so importing a runner (including CLI
--help) needed network access. Load the encoding lazily and cache it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

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

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 ce11dbdc. No P0-P2 defects found.

What I verified

  • Rainbond _wait_site (src/harness/environments/webarena/rainbond.py:588): the HTTPError handler comes before the (OSError, URLError) handler, so 4xx responses now count as ready and 5xx responses still retry. The timeout message now reports HTTP <code>.
  • Public WebArena report fails closed (src/harness/trajectory_replay/public_webarena/report.py:2034): audit_result_set runs before mkdir or any write, so a rejected audit leaves no pages behind. _available_result_audit has been removed, with no aliases left. Both the summary page and the review page now require the passing audit. The only production caller that was not already auditing first is public_webarena/runner.py:1142. That call is skipped for qualification and trajectory-authoring runs, and manifest["suite"] is built from runnable_tasks, so a filtered formal run still matches the audit's task scope. Core evidence is written before the report, so an audit failure now ends the run with an error and the evidence is kept. That is the intended fail-closed behavior. publication.py and manifest_repair.py already audited before rendering, and they still do.
  • _wait_cdp (src/harness/trajectory_replay/remote_execution.py:263): it now sleeps after every probe that does not succeed. The checks for process exit and for a 200 response are unchanged.
  • tiktoken loaded lazily (trajectory_replay/runner.py, public_webarena/runner.py): ENCODING has no other users (grep), and @cache gives the same single load.
  • Rejected or deferred suspects: I read har.py. _record_playwright_request stores the request object next to its id() key, so the rejection reasoning holds. The locals().get and service._submit rejections are also reasonable. The port-0 race is listed as remaining under "Part of #94", which matches the scope that is still open.
  • Tests: ruff gates pass. pytest tests/trajectory_replay tests/test_golden_reports.py tests/test_resource_accounting.py -n 2 gives 2061 passed and 5 skipped. The golden route hashes are unchanged, which fits a change that only alters behavior on failure. I put the base (8b8aa30f) src/ back under the new tests and all 9 new or updated regression tests failed (rainbond 3, report fail-closed plus 2 signature updates, wait_cdp 1, tiktoken import 2), so they do catch the bugs.
  • CI: every check passes on ce11dbdc, including "Tests and static checks".

Nits

  • P3 tests/trajectory_replay/public_webarena/test_rainbond_slices.py:1063: monkeypatch.setattr("harness.environments.webarena.rainbond.time.monotonic", ...) resolves to the global time module. For the whole test, it replaces time.monotonic everywhere with a 4-value iterator. Any extra call raises StopIteration and makes the test fragile. A cleaner option is to patch the module attribute with a stub namespace (for example monkeypatch.setattr(rainbond, "time", SimpleNamespace(monotonic=..., sleep=time.sleep))) or to make the clock injectable the way sleep already is.

🤖 Generated with Claude Code

Replace the global time.monotonic patch, backed by an exhaustible iterator, with a stub time namespace on the rainbond module whose clock holds past the deadline instead of raising StopIteration.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@lanyue-llk

Copy link
Copy Markdown
Contributor Author

Review round 1 follow-up:

Fixed

  • P3 (tests/trajectory_replay/public_webarena/test_rainbond_slices.py, global time.monotonic patched with an exhaustible iterator) in 3d72a81. The test now replaces the rainbond module's time attribute with a SimpleNamespace stub. Its clock returns the scripted readings and then stays past the deadline, so an extra call can no longer raise StopIteration. The global time.monotonic is left alone. I checked that the test still fails against the pre-fix rainbond.py from 8b8aa30f and passes on this branch.

Disputed

  • None.

Local runs: test_rainbond_slices.py 40 passed, tests/test_golden_reports.py 10 passed, ruff lint gate clean.

@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 · 3d72a81

No findings in this review.

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.

Fix confirmed runtime bugs found in the codebase audit

1 participant