Skip to content

feat(extension): add --reuse-window flag for tab-mode automation#1202

Closed
young001 wants to merge 1 commit into
jackwener:mainfrom
young001:feat/reuse-window-flag
Closed

feat(extension): add --reuse-window flag for tab-mode automation#1202
young001 wants to merge 1 commit into
jackwener:mainfrom
young001:feat/reuse-window-flag

Conversation

@young001

Copy link
Copy Markdown

Summary

Adds --reuse-window (and OPENCLI_REUSE_WINDOW=1) flag that opens
automation as a new tab in the user's last-focused Chrome window instead
of spawning a separate window. Solves the "two windows" friction for
users who already keep a dedicated Chrome window for OpenCLI.

Motivation

Today, every adapter command creates a new automation window via
chrome.windows.create. Users who keep one Chrome window open as the
"OpenCLI host" end up with a second window popping up on every command,
which clutters the screen. bound:* workspaces help for opencli browser *
but not for adapter commands. This flag makes "in-place tab automation"
a first-class option for both.

Behaviour

Flag combo Result
(default) New automation window (unchanged)
--reuse-window New background tab in last-focused window
--reuse-window --focus Foreground tab in last-focused window
--reuse-window --live Tab kept open after command finishes
--reuse-window + zero Chrome windows Falls back to chrome.windows.create

The flag is orthogonal to --focus and --live. Default behaviour is
fully preserved when the flag is absent.

Implementation notes

  • Argv interception in src/main.ts mirrors the existing --focus /
    --live pattern (env var → daemon command field → extension global).
  • AutomationSession gains an ownsWindow boolean. Idle timeout,
    close-window, and chrome.tabs.onRemoved branch on this to close
    the tab (not the host window) in tab mode.
  • resolveTabId fails closed in tab mode when the preferred tab is
    gone, refusing to silently take over an unrelated user tab.
  • 3 new unit tests in extension/src/background.test.ts cover the
    happy path, the zero-window fallback, and idle-timeout cleanup.

Test plan

  • npx tsc --noEmit (root + extension) — clean
  • npm test — 247 files / 2002 passing / 0 new failures
  • npm run build (root + extension) — produced fresh dist/
  • Manual end-to-end: load unpacked extension, npm link, run
    opencli xiaohongshu search '纸尿裤' --reuse-window --focus --live
    and verify a new tab opens in the existing window instead of a new
    window. (Reviewer: please verify locally; I cannot reproduce in CI.)

Open automation as a new tab in the user's last-focused Chrome window
instead of spawning a separate window. Solves the "two windows" problem
when the user already has a Chrome window dedicated to OpenCLI.

Behaviour:
- `--reuse-window` (or OPENCLI_REUSE_WINDOW=1) is intercepted in
  src/main.ts and propagated through the daemon → extension protocol.
- The extension calls chrome.windows.getLastFocused({windowTypes:['normal']})
  and chrome.tabs.create on that window, instead of chrome.windows.create.
- Sessions track ownsWindow: when false (tab mode), idle timeout and
  close-window cleanup remove the tab via chrome.tabs.remove and never
  touch the host window.
- Falls back to the legacy new-window path when no normal Chrome window
  is available, preserving zero-window behaviour.
- Orthogonal to --focus (defaults to background tab) and --live (keeps
  the tab alive instead of the window).

Safety:
- resolveTabId fails closed in tab mode when the preferred tab is gone,
  refusing to silently take over an unrelated user tab in the host window.
- chrome.tabs.onRemoved listener cleans up tab-mode sessions when the
  user manually closes our owned tab.

Tests added in extension/src/background.test.ts:
- Opens a new tab in the last-focused window when --reuse-window is set.
- Falls back to chrome.windows.create when no normal window exists.
- Idle timeout closes the tab (not the host window) for reuse-window
  sessions.
@jackwener

Copy link
Copy Markdown
Owner

基于当前 main(43d0722) 重新核对后关闭这条。\n\n这条 PR 的目标是用 --reuse-window 把 tab-mode automation 放进用户最后聚焦的 Chrome window。当前 main 已经换成 explicit browser session + TargetLease 模型:\n- README 已要求 opencli browser --session <name> ...,同一个 session 的 tab lease 会持续复用到 browser close 或 idle cleanup;\n- extension 里已有 TargetLeaseownedContainers.interactive/automationsiteSession--keep-tab,adapter 默认走 background adapter container,interactive browser session 走 foreground container;\n- shared/current-tab 场景已经由 browser bind / bound lease 覆盖。\n\n继续合入这个 PR 会把旧的 window-level reuseWindow 分支重新引入,和当前 lease/container contract 冲突。按 obsolete PR 关闭。

@jackwener jackwener closed this May 11, 2026
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.

2 participants