The trimming loop counted brackets across the whole candidate on every
iteration, so a candidate ending in many unmatched brackets rescanned it
once per bracket. 50,000 trailing ')' took 0.9s, where the regex this
replaced was linear.
Count the four bracket characters once and decrement as characters are
trimmed, tracking the end index instead of reslicing. Same results, and
200,000 trailing ')' now takes 0.011s.
sanitize_url_candidate() strips trailing prose punctuation so that
"Go to https://example.com/docs." does not navigate with the sentence's
period attached. It also stripped every trailing ) and ], including the
ones the URL opened itself, so a task like
Summarize https://en.wikipedia.org/wiki/Python_(programming_language)
auto-navigated to .../Python_(programming_language and landed on the wrong
page. Wikipedia disambiguation links are the common case.
Whether the bracket belongs to the URL is decided by balance: a closing
bracket with a matching opener inside the candidate is part of the path,
while one the prose opened, as in "(see https://example.com/guide)", is not.
Strip trailing punctuation as before, and only drop a closing bracket when
the candidate has more of them than openers.
Fixes#5575
The iterative str.replace loop corrupts output when a shorter secret
is a substring of a <secret> placeholder tag written by an earlier
iteration (e.g. 'secret' inside '<secret>password</secret>').
Replace with a single-pass re.sub using a regex alternation of all
secret values (longest first). Each position in the input is consumed
at most once, so earlier replacements cannot be corrupted by later
ones.
Closes#5135
Add disabled parameter to SignalHandler class to allow opting out of
signal handling. This enables browser-use to be embedded in applications
like uvicorn/FastAPI that need to manage their own signal lifecycle.
Add enable_signal_handler parameter to Agent class (default True for
backward compatibility). When set to False, the Agent will not register
signal handlers, allowing the host application to control graceful
shutdown and signal handling.
Fixes#4385
- agent/views.py: Move RateLimitError import inside format_error() function
to avoid loading openai SDK (~800ms) at module level
- llm/messages.py: Replace openai.BaseModel with pydantic.BaseModel directly
to remove unnecessary openai dependency
- filesystem/file_system.py: Move reportlab imports inside sync_to_disk_sync()
to avoid ~40ms startup cost when PDF generation is not used
- utils.py: Convert OpenAIBadRequestError and GroqBadRequestError to lazy
loaders to avoid loading SDKs at module level
This improves import time for users who don't use OpenAI provider,
especially when using Anthropic, Google, or other providers.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Introduced a variable to hold exceptions for re-raising outside the try-except block, ensuring proper propagation to the event loop.
- Updated comments for clarity on the exception handling process in background tasks.
- Updated the logging behavior for exceptions in background tasks to log at warning level and re-raise exceptions for better propagation to the event loop's exception handler.
- Enhanced clarity in comments regarding exception retrieval and handling.
- Improved the `create_task_with_error_handling` function to allow for better exception logging and retrieval based on the `suppress_exceptions` parameter.
- Updated `SessionManager` to implement event-driven recovery for stale agent focus, replacing polling with efficient event handling.
- Refactored session retrieval methods to ensure focus validation and recovery are handled automatically.
- Enhanced logging for recovery processes and session management to provide clearer insights into state changes and errors.
- Adjusted various watchdogs to utilize the new session management methods, ensuring they correctly handle focus validation and session retrieval.
- Introduced a utility function `create_task_with_error_handling` to manage asyncio tasks with proper exception logging and suppression options.
- Updated multiple components, including `SessionManager`, `BrowserSession`, and various watchdogs, to utilize the new error handling mechanism for background tasks.
- Enhanced overall robustness of asynchronous operations by ensuring exceptions are logged and handled appropriately, improving maintainability and debugging capabilities.