Files
browser-use/browser_use/code_use/namespace.py
Magnus Müller 4ac49de9cf fix(code-use): prevent done() execution inside conditional blocks
Previously, done() calls inside if/else/elif blocks would only show a warning
but still execute, potentially causing tasks to never complete if the condition
wasn't met. This changes the behavior to raise a RuntimeError, forcing the LLM
to restructure code properly.

The proper pattern is:
```python
# Validate and set result variables
if condition:
    result = "success"
else:
    result = "failure"

# Then call done() unconditionally
await done(result, success=True)
```

Changes:
- Upgraded warning to RuntimeError in namespace.py
- Added comprehensive test suite with 5 test cases
- Tests cover if/else/elif blocks and verify standalone done() still works

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-22 10:03:10 -07:00

22 KiB