fix: clean up tabs owned by each CLI invocation - #526
Open
kesava500 wants to merge 2 commits into
Open
Conversation
✅ Skill review passedReviewed 1 file(s) — no findings. |
Contributor
There was a problem hiding this comment.
All reported issues were addressed across 5 files
Tip: instead of fixing issues one by one fix them all with cubic
Re-trigger cubic
kesava500
force-pushed
the
fix/owned-tab-cleanup
branch
from
July 14, 2026 17:46
9d80b4b to
6f38468
Compare
new_tab() usually reuses the current blank tab instead of creating a new one via Target.createTarget, since the daemon is commonly parked on a blank keeper tab between invocations. close_opened_tabs() only ever checked _KEEP_OPENED_TABS after unconditionally restoring reused-blank tabs, so keep_opened_tabs() silently failed to protect exactly the common single-tab case callers rely on it for. Move the keep-mode early return above _restore_reused_blank_tabs() so both owned and reused-blank tabs are left untouched when keeping, matching the actual promise of the API. force=True still restores as before.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Automatically clean up tabs owned by each
browser-harnessCLI invocation without touching pre-existing tabs or targets opened by another process.This rebases and extends the approach proposed by @hamanori in #354, which is now conflicting with
main.What changed
new_tab().finallyblock, including when the browser task raises an error.new_tab()reuses it instead of closing that target.keep_opened_tabs()orBH_KEEP_TABS=1.SKILL.md.Why
The browser daemon intentionally survives between CLI invocations, so tabs also survive unless an agent remembers to close them. Long-running agents and scheduled research tasks therefore accumulate abandoned tabs and eventually consume substantial browser resources.
The original #354 ownership model avoids closing unrelated tabs. This version also accounts for current
mainreusing an existing blank tab and for the dangling-session failure described in #379 when the attached target closes.The same lifecycle applies when the CLI is connected to a remote or cloud browser. Cleanup remains limited to targets owned by that invocation.
Verification
uv run --with pytest python -m pytest -q(137 passed)uv run --with ruff ruff check tests/unit/test_tab_cleanup.pyuv run python -m compileall -q src testsgit diff --checkabout:blank, and the next CLI invocation remained attached and usable.Related work
close_tab()API addressed by fix: close_tab() recovers session state when closing the currently attached tab #393.Summary by cubic
Automatically cleans up tabs owned by each
browser-harnessCLI run and keeps the daemon attached to a live page. Cleanup runs after success or error and never touches unrelated tabs.Bug Fixes
new_tab()and do it in a finally block; print a warning if cleanup fails and never mask task errors.about:blankwhen not keeping; keep mode now protects reused-blank tabs too.force=Truestill restores/closes.window.open,target=_blank).opened_tabs(),keep_opened_tabs(), andclose_opened_tabs(force=False); the keep flag is scoped to one invocation. Documented lifecycle inSKILL.mdand added targeted tests.Migration
keep_opened_tabs()or setBH_KEEP_TABS=1(case-insensitive). In keep mode, owned and reused-blank tabs are left as-is; useclose_opened_tabs(force=True)to restore or close them.Written for commit d57c921. Summary will update on new commits.