Skip to content

fix(browser-use): align resume retries and keep failed-attempt evidence - #124

Merged
lanyue-llk merged 7 commits into
mainfrom
fix/browser-use-runner-bugs
Nov 2, 2026
Merged

lanyue-llk merged 7 commits into
mainfrom
fix/browser-use-runner-bugs

Conversation

@lanyue-llk

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

Copy link
Copy Markdown
Contributor

Issues

Closes #94. This PR covers only the Browser Use items. Still open in #94 and handled in other PRs: Rainbond 4xx readiness, the unbounded process.wait() in the headless/moli providers, the three P50/P95 definitions, the public WebArena audit fail-open, and the suspected web_search/review.py, _wait_cdp, catalog_runner/remote_execution port races, har.py id(request), import-time tiktoken and webarena_replay.py locals() items.

Summary

One commit per bug, each with a regression test:

  1. Resume used a different retry rule from a fresh run (src/harness/browser_use/runner.py). On resume, attempt 2 was skipped when the stored primary had evaluator status unsupported. A fresh run retries any primary with a score below 1.0. I kept the fresh-run rule because it is the one the project declares in three places. The manifest protocol records failure_retries: 1 / task_failure_retries: 1. report_check.py accepts a retry after any primary with score != 1.0. No profile or doc gives unsupported an exception. Both paths now call one _retry_required(primary) helper, so interrupted and uninterrupted cohorts record the same calls. The resume-skip print is unchanged.
  2. Failed attempts lost timing. agent_run_ms and evidence_finalize_ms were assigned only on success. Both are now measured in finally blocks. The failure-path HAR/trace stop counts toward evidence_finalize_ms.
  3. The HAR/trace stop exception was swallowed. The failure-path stop now appends HAR/trace stop failed: <error> to cleanup_errors. The list is created before the attempt starts.
  4. Docker clients were never closed (suspected, confirmed). This code has moved from browser_use/runtime.py to src/harness/environments/webarena/runtime.py. site_container_manager, ensure_site and verify_arm_php_compatibility each opened a docker.from_env() client per call and never closed it. In prepare_sites, that adds up to several clients per site on every attempt. Each one is now wrapped in contextlib.closing. Function signatures are unchanged, so trajectory_replay/webarena_replay.py callers are not touched.
  5. assert used as a runtime precondition (suspected, confirmed). login_context asserted that the browser context and page exist. Under python -O the assert is removed and the failure turns into an AttributeError. It now raises an explicit RuntimeError.

Not changed:

  • Bind-port-0-then-close in browser_use/backend.py. This is now src/harness/environments/webarena/backend.py (DockerBackend.find_free_port). The race is real in principle but nothing can trigger it. The method exists only to satisfy the webarena_verified container-backend protocol. Neither the pinned webarena_verified package nor this repository calls it: site ports are fixed by SITE_PORTS, and browser containers use Docker-assigned published ports. Removing it would break protocol conformance, so I left it alone.

The new attempt-level tests run _run_attempt_locked end to end. They use in-process fakes for CowAgent (sys.modules), the browser service, the HAR recorder and the Docker resource sampler. I checked that the timing test fails on the code before the fix.

Test plan

  • ruff check --config 'include=["*.py"]' --select F,TID252 . and ruff check: pass.
  • pytest tests/browser_use tests/trajectory_replay tests/test_golden_reports.py -n 2: 2311 passed, 5 skipped. Golden route hashes are unchanged.
  • I ran the timing regression test against the pre-fix runner.py and it failed, as expected.
  • CI: see the "Tests and static checks" result on this PR.

🤖 Generated with Claude Code

lanyue-llk and others added 5 commits October 30, 2026 20:11
Resume skipped attempt 2 when the stored primary had evaluator status
"unsupported", while a fresh run retries every primary below score 1.0.
The protocol declares one failure retry and the release check accepts a
retry after any non-passing primary, so both paths now share one
_retry_required rule and record the same calls.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
agent_run_ms and evidence_finalize_ms were assigned only after the agent
and evidence stop succeeded, so a failed attempt reported 0 ms of agent
time. Measure both phases in finally blocks and count the failure-path
HAR/trace stop as evidence finalization.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The failure-path HAR recorder and trace stop swallowed its exception, so
an attempt could lose its network trace without any recorded reason.
Append the failure to the attempt's cleanup_errors like the other
cleanup steps.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
site_container_manager, ensure_site and verify_arm_php_compatibility each
created a docker.from_env() client per call and never closed it, so every
attempt leaked several HTTP connection pools to the daemon. Close each
client once its calls finish, including on failure.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
login_context checked its browser context and page with assert, which
python -O strips, turning the precondition into an AttributeError deep in
login. Raise a RuntimeError that names the missing session state.

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 · a209018

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: a2090188 (base 8b8aa30f). I found no P0, P1 or P2 defects. Two P3 notes follow; neither blocks merge.

What I verified

  • Resume retry rule. report_check.py:513-519 allows attempt 2 only after a primary with score != 1.0. report_check.py:854-861 rejects a complete publication when any non-passing primary has no retry. Under the old resume path, an unsupported primary with score 0 was never retried, so a resumed cohort could not pass the release check. Using the fresh-run rule is correct. run() stores fresh rows in completed before attempt 2, so the guard on completed[primary_key] covers both the fresh and the resumed path.
  • Failed-attempt timing. agent_run_ms and evidence_finalize_ms are now set in finally blocks. The failure-path stop adds to evidence_finalize_ms with +=, so an error after the success-path finalize does not lose the earlier time. Both values reach row["timings"].
  • HAR/trace stop errors. cleanup_errors is now created before the try, and the failure-path stop error is appended to it. Nothing outside the runner reads cleanup_errors in a way that changes a verdict.
  • Docker clients. I found no unclosed per-call docker.from_env() left in environments/webarena/runtime.py. The ContainerManager and EnvCtrlDockerClient objects from upstream webarena_verified do not call from_env.
  • assert → RuntimeError in login_context: correct.
  • Bind-port-0 race. I agree with the decision not to change it. DockerBackend.find_free_port is a protocol method. Its only caller in the repository, lexbench/resource_matrix.py:70, calls run.find_free_port() on a different object.
  • Tests fail without the fix. I restored runner.py and runtime.py from 8b8aa30f, adding a stub _retry_required that keeps the old unsupported exception, and ran tests/browser_use/test_runner.py. 9 tests failed: both login tests, all four client-closing tests, the unsupported resume case, and both attempt-level tests. All of them pass on the PR head.
  • ruff (both gates): pass. pytest tests/browser_use tests/test_golden_reports.py -n 2: 270 passed. Golden hashes are unchanged.
  • Commits: 5 commits, one per bug, each with the Co-Authored-By trailer. Part of #94 matches the remaining scope listed in the body.
  • CI on a2090188: every check passes, including "Tests and static checks".

Findings

  1. P3, tests/browser_use/test_runner.py:1108. The resume regression test calls only _retry_required. It does not exercise the run() loop where the old divergence was, so a future edit that bypasses the helper in the resume branch would still pass. Suggested fix: add a small loop-level test. Seed attempts.jsonl with an unsupported, score-0 primary, stub _run_attempt, and assert that attempt 2 runs. Alternatively, move the per-engine attempt loop into a small function that can be tested.
  2. P3, src/harness/environments/webarena/runtime.py:330. No test checks that ensure_site closes its client, unlike the other two call sites. Suggested fix: add an assertion with the existing _patch_docker_client helper if that is cheap. This one is optional.

This review does not approve the PR or request changes.

lanyue-llk and others added 2 commits October 30, 2026 20:48
…t loop

Move the per-engine primary/retry loop out of run() into
_run_engine_attempts so fresh and resumed invocations share one testable
loop, and cover it with resume and fresh-run cases that assert which
attempts actually execute.

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

Copy link
Copy Markdown
Contributor Author

Review round 1 follow-up:

  • Resume loop coverage (P3) — 27a4fe1: moved the per-engine primary/retry loop out of run() into _run_engine_attempts, which both fresh and resumed invocations now use. New tests seed attempts.jsonl with a success, an unsupported score-0 primary and a failure primary, call the loop with a stubbed attempt executor, and assert which attempts actually run (none / attempt 2 / attempt 2) and that the retry is appended to the evidence file. A fresh-run case checks that an unsupported primary gets one retry.
  • ensure_site Docker client closing (P3) — a0130a1: added success-path and health-check-failure tests built on _patch_docker_client that assert client.closed. I checked that both tests fail when the closing(...) wrapper is removed.

Nothing disputed. Locally: ruff gates pass, and tests/browser_use plus tests/test_golden_reports.py give 276 passed. Golden snapshots are unchanged.

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

No findings in this review.

@lanyue-llk
lanyue-llk merged commit 2e7ab0a into main Nov 2, 2026
6 checks passed
@lanyue-llk
lanyue-llk deleted the fix/browser-use-runner-bugs branch November 2, 2026 03:03
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