Skip to content

fix: replace assert with runtime guards in computer_use (browser_route, doctor) - #80861

Open
JonthanaHanh wants to merge 1 commit into
NousResearch:mainfrom
JonthanaHanh:fix/assert-runtime-guards-computer-use
Open

JonthanaHanh wants to merge 1 commit into
NousResearch:mainfrom
JonthanaHanh:fix/assert-runtime-guards-computer-use

Conversation

@JonthanaHanh

Copy link
Copy Markdown
Contributor

Summary

Replace assert statements with explicit runtime guards in tools/computer_use/ — two sites not covered by prior assert-cleanup PRs (#56736, #56866, #61983, #62001, #62659, #64818).

assert is stripped by python -O, silently removing invariant checks in production.

Changes

File Line Old New
tools/computer_use/browser_route.py 481 assert selected_tab is not None and self.state.target_id is not None if ... is None: return _refusal(...)
tools/computer_use/doctor.py 226 assert proc.stdin is not None and proc.stdout is not None if ... is None: raise RuntimeError(...)

Why these matter

  • browser_route.py: After _require_mutation() returns with no refusal, subsequent code dereferences selected_tab and self.state.target_id. Under python -O, if either is None, the code crashes with AttributeError deep in the browser tool dispatch — a confusing error with no context. The guard returns a clear refusal dict.

  • doctor.py: _mcp_rpc() writes to proc.stdin and reads from proc.stdout. If either is None (e.g. subprocess started with stdin=DEVNULL), the code crashes with AttributeError: 'NoneType' object has no attribute 'write'. The guard raises RuntimeError with a descriptive message.

Test Plan

  • python3 -c "import ast; ast.parse(open('tools/computer_use/browser_route.py').read())" — syntax OK
  • python3 -c "import ast; ast.parse(open('tools/computer_use/doctor.py').read())" — syntax OK
  • Both files pass ruff check

…e, doctor)

assert statements are stripped by python -O, silently removing invariant
checks in production. Replace with explicit if/raise guards:

- browser_route.py:481 — guard selected_tab and target_id after
  _require_mutation returns, returning a refusal dict instead of crashing
- doctor.py:226 — guard proc.stdin and proc.stdout before JSON-RPC
  communication, raising RuntimeError with descriptive message
@alt-glitch alt-glitch added type/refactor Code restructuring, no behavior change comp/tools Tool registry, model_tools, toolsets P3 Low — cosmetic, nice to have labels Aug 7, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/tools Tool registry, model_tools, toolsets P3 Low — cosmetic, nice to have type/refactor Code restructuring, no behavior change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants