Skip to content

Auto-close tabs opened by browser-harness runs#354

Open
hamanori wants to merge 1 commit into
browser-use:mainfrom
hamanori:fix/auto-close-new-tabs
Open

Auto-close tabs opened by browser-harness runs#354
hamanori wants to merge 1 commit into
browser-use:mainfrom
hamanori:fix/auto-close-new-tabs

Conversation

@hamanori
Copy link
Copy Markdown

@hamanori hamanori commented May 13, 2026

[fix] browser-harness実行終了時に作成タブを自動クリーンアップする

変更内容(What)

  • new_tab() が作成した targetId を CLI プロセス内で追跡する _OPENED_TABS を追加した。
  • close_opened_tabs() / opened_tabs() / keep_opened_tabs() を追加し、今回の実行で開いたタブだけを閉じられるようにした。
  • browser-harness -cexec()try/finally で包み、例外終了時も既定では close_opened_tabs() を実行するようにした。
  • タブを意図的に残す場合の逃げ道として BH_KEEP_TABS=1keep_opened_tabs() を用意した。
  • tests/unit/test_tab_cleanup.py を追加し、通常 cleanup、明示 opt-out、例外時 cleanup、環境変数 opt-out を検証した。

変更理由(Why)

  • 汎用 CDP profile で Web 調査後のタブが閉じられず、ニュース調査や検索タブが蓄積してブラウザが使いにくくなっていたため。
  • 従来は skill / 運用ドキュメント上の「閉じる」手順に依存しており、エージェントや cron が途中失敗した場合に後片付けが漏れる構造だったため。

実装方法(How)

  • 既存タブや別エージェントのタブを巻き込まないよう、list_tabs() の差分ではなく、この Python プロセスの new_tab() 経由で作成した targetId だけを管理対象にした。
  • cleanup は Target.closeTarget の best-effort とし、個別 close 失敗は主処理の例外を上書きしない設計にした。
  • 長時間ユーザーに見せたいタブを残せるよう、明示的な opt-out だけを許可した。

影響範囲・注意事項

  • browser-harness -cnew_tab() を使ったタブは、既定で実行終了時に閉じられる。
  • タブを残したい調査では BH_KEEP_TABS=1 browser-harness -c ... またはスクリプト内で keep_opened_tabs() を呼ぶ必要がある。
  • switch_tab() で既存タブを操作した場合や、別プロセスが開いたタブは自動 cleanup 対象外。

Co-Authored-By: OpenAI Codex noreply@openai.com


Summary by cubic

Automatically closes tabs opened during browser-harness runs to prevent leftover clutter. Tabs created via new_tab() are tracked and closed on exit, including on errors, with an opt-out.

  • Bug Fixes

    • Track and close only tabs opened by this process via new_tab() using _OPENED_TABS; add opened_tabs(), keep_opened_tabs(), and close_opened_tabs(force=...).
    • Auto-close on -c exit using try/finally; skip when BH_KEEP_TABS is set to 1/true/yes (case-insensitive).
    • After closing, wait briefly so Chrome finishes shutting tabs to avoid “zombie” tabs in follow-up commands.
  • Migration

    • To keep tabs, run with BH_KEEP_TABS=1 or call keep_opened_tabs() in your script.
    • Existing tabs and tabs from other processes are never auto-closed.

Written for commit e83393c. Summary will update on new commits.

## 変更内容(What)
- `new_tab()` が作成した targetId を CLI プロセス内で追跡する `_OPENED_TABS` を追加した。
- `close_opened_tabs()` / `opened_tabs()` / `keep_opened_tabs()` を追加し、今回の実行で開いたタブだけを閉じられるようにした。
- `browser-harness -c` の `exec()` を `try/finally` で包み、例外終了時も既定では `close_opened_tabs()` を実行するようにした。
- タブを意図的に残す場合の逃げ道として `BH_KEEP_TABS=1` と `keep_opened_tabs()` を用意した。
- `tests/unit/test_tab_cleanup.py` を追加し、通常 cleanup、明示 opt-out、例外時 cleanup、環境変数 opt-out を検証した。

## 変更理由(Why)
- 汎用 CDP profile で Web 調査後のタブが閉じられず、ニュース調査や検索タブが蓄積してブラウザが使いにくくなっていたため。
- 従来は skill / 運用ドキュメント上の「閉じる」手順に依存しており、エージェントや cron が途中失敗した場合に後片付けが漏れる構造だったため。

## 実装方法(How)
- 既存タブや別エージェントのタブを巻き込まないよう、`list_tabs()` の差分ではなく、この Python プロセスの `new_tab()` 経由で作成した targetId だけを管理対象にした。
- cleanup は `Target.closeTarget` の best-effort とし、個別 close 失敗は主処理の例外を上書きしない設計にした。
- 長時間ユーザーに見せたいタブを残せるよう、明示的な opt-out だけを許可した。

## 影響範囲・注意事項
- `browser-harness -c` で `new_tab()` を使ったタブは、既定で実行終了時に閉じられる。
- タブを残したい調査では `BH_KEEP_TABS=1 browser-harness -c ...` またはスクリプト内で `keep_opened_tabs()` を呼ぶ必要がある。
- `switch_tab()` で既存タブを操作した場合や、別プロセスが開いたタブは自動 cleanup 対象外。

Co-Authored-By: OpenAI Codex <noreply@openai.com>
@hamanori hamanori force-pushed the fix/auto-close-new-tabs branch from bb4fafe to e83393c Compare May 13, 2026 14:02
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 3 files

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.

1 participant