fix(browser): use unmodified BrowserSkill upstream and remove downstream patches (#3563)

* fix(browser): move BrowserSkill baseline to upstream main with merged patches

Upstream merged the navigation response grace (#291), the preview/focus UI
channel (#296) and popup attribution (#297). Pin both ends to commit c61eb7e4
and drop patches 02, 04 and 06. WeKnora now calls the official ui.task_preview
and ui.task_focus methods.

Patch 05 (last-tab lifecycle) is replaced host-side: before tool.tab_close the
gateway lists the Agent Window's tabs and, when the target is the only one,
creates an agent-owned about:blank tab through tool.tab_create so Chrome does
not remove the window and report a user interruption. Patch 01 (bounded CDP
reads and screenshot fence) is rebased onto the new baseline and remains the
only downstream extension change.

The real-extension test follows the upstream borrow semantics: an unowned tab
inside the Agent Window is rejected in place and borrowed after being moved to
a regular window.

* fix(browser): narrow the BrowserSkill read-reliability patch

Keep isAbortError strict and add isCaptureTerminalError for the capture
pipeline, so a renderer read timeout stops fallback reads but tool handlers
report it as cdp_failed instead of cancelled; the gateway no longer pauses the
task on a read timeout.

Drop the CDP-level screenshot fence and the 3 s screenshot deadline: upstream
PR #296 already bounds preview captures and fences them per attachment, and
tool screenshots are serialized by the daemon queue. Stop routing the focus
emulation toggle through the deadline so upstream's raw-command serialization
keeps holding. Remove the duplicate background rendering browser test; the
upstream background-execution and background-screenshot tests cover it.

* docs(browser): link the read-reliability patch to upstream PR #318

* fix(browser): keep BrowserSkill frame discovery consistent after a read timeout

Sync patch 01 with the follow-up pushed to upstream PR #318: a timed-out child
Target.setAutoAttach keeps its session and is retried from getFrameGraph, and
timed-out frame-tree, frame-owner and root auto-attach reads reject the frame
graph instead of letting the coordinator queue a snapshot behind them.

* style(browser): gofmt focus.go

* fix(browser): roll back the last-tab placeholder and surface renderer read timeouts

The placeholder about:blank tab created before tool.tab_close stayed behind
when the extension refused the close (unauthorized or borrowed tab) or the
call was interrupted. After a failed close the gateway lists the Agent Window
again and removes the placeholder while the target tab still exists; when the
extension already closed the target before the reply was lost, the placeholder
is what keeps the window open and stays.

Sync patch 01 with upstream PR #318: renderer reads on a tab are refused while
a timed-out read is still running in Chrome, and observation errors carry
data.reason = renderer_read_timeout. Map that reason to a model hint that
stops repeated reads on the unresponsive tab instead of the generic
inspect-and-retry advice for cdp_failed.

* fix(browser): remove remaining BrowserSkill patches

* fix(browser): add official extension link and setup guidance
main
lyingbug 2026-09-23 00:22:34 +08:00 committed by GitHub
parent 4afda18ec1
commit a89a5a2c43
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
25 changed files with 670 additions and 1875 deletions

View File

@ -8,7 +8,6 @@ ENV RUSTUP_HOME=/usr/local/rustup CARGO_HOME=/usr/local/cargo
ENV PATH=/usr/local/cargo/bin:$PATH
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal --default-toolchain stable
COPY scripts/build_browserskill.sh scripts/browserskill-release.json ./scripts/
COPY patches/browserskill ./patches/browserskill
ARG TARGETOS
ARG TARGETARCH
RUN bash scripts/build_browserskill.sh /opt/weknora/browserskill "${TARGETOS}/${TARGETARCH}"

View File

@ -78,13 +78,13 @@ export default {
"pause": "Pause browser",
"start": "Start task",
"stop": "End browser task",
"pairHint": "Paste the pairing link into Remote connection in the extension and confirm the server.",
"pairHint": "Open Connection settings → Remote connection in the extension, paste the pairing link, and confirm the server. The link is valid for 5 minutes and can be used once.",
"copyPairing": "Copy pairing link",
"copied": "Copied",
"windowHint": "Tasks run in a labeled Chrome tab group. Existing tabs require your permission.",
"windowHint": "Tasks run in a separate Chrome Agent Window. Borrowing existing tabs requires your permission.",
"preview": "Local browser task preview",
"waiting": "Waiting for a task page",
"startHint": "A browser request creates labeled task tabs in the background.",
"startHint": "A browser request creates a separate Agent Window.",
"revoke": "Revoke device access",
"revokeConfirm": "You will need to pair again before using the local browser.",
"failed": "Operation failed. Please retry.",
@ -96,8 +96,10 @@ export default {
"reconnectHint": "Authorization is saved. Keep Chrome and BrowserSkill open to reconnect automatically.",
"replaceDevice": "Change browser",
"installExtension": "Install BrowserSkill",
"installHint": "Download the extension built for this server and install it in Chrome.",
"downloadExtension": "Download extension",
"installHint": "Use the matching package for this server; the official store version may receive updates later.",
"downloadExtension": "Download matching package",
"officialExtension": "Official extension (Chrome Web Store)",
"installGuide": "How do I install the downloaded package?",
"pairBrowser": "Connect this browser",
"packageUnavailable": "Ask your administrator for the matching extension package; a download is not configured yet.",
"manualCopy": "Select and copy the link below. It expires in 5 minutes and can be used once.",
@ -105,11 +107,11 @@ export default {
"copyAgain": "Copy again",
"usageTitle": "How to use",
"usageStep1Title": "Install the extension",
"usageStep1Text": "Unzip the package, enable Developer mode on the Chrome extensions page, and choose Load unpacked.",
"usageStep1Text": "Unzip the downloaded ZIP, enter chrome://extensions in the Chrome address bar, enable Developer mode, choose Load unpacked, and select the extracted extension folder.",
"usageStep2Title": "Pair this browser",
"usageStep2Text": "Copy the pairing link and paste it into Remote connection in the extension. Pair once for all conversations in this space.",
"usageStep2Text": "Copy the pairing link into Connection settings → Remote connection in the extension and confirm the server. Pair once for all conversations in this space.",
"usageStep3Title": "Describe the task in chat",
"usageStep3Text": "Turn on the local browser in the input bar and describe the web task. It runs in a labeled tab group.",
"usageStep3Text": "Turn on Local browser in the conversation input bar before sending a web task. Pairing does not enable this option automatically. Tasks run in a separate Agent Window.",
"usageStep4Title": "Preview, locate, and resume",
"usageStep4Text": "A small preview appears in the conversation. Click it to find the task tab. Interrupted tasks stay paused after reconnect — resume them from the preview. Existing tabs require your permission.",
"running": "Running",

View File

@ -78,13 +78,13 @@ export default {
"pause": "操作を一時停止",
"start": "タスクを開始",
"stop": "ブラウザータスクを終了",
"pairHint": "拡張機能の「リモート接続」にリンクを貼り付け、サーバーを確認します。",
"pairHint": "拡張機能の「接続設定 → リモート接続」にリンクを貼り付け、サーバーを確認します。リンクは 5 分間有効で、1 回限り使用できます。",
"copyPairing": "ペアリングリンクをコピー",
"copied": "コピーしました",
"windowHint": "タスクはラベル付きのタブグループで実行されます。既存タブには許可が必要です。",
"windowHint": "タスクは独立した Chrome タスクウィンドウで実行されます。既存のタブを借りるには許可が必要です。",
"preview": "ローカルブラウザーのプレビュー",
"waiting": "タスクのページを待っています",
"startHint": "ブラウザーのタスクを送ると、バックグラウンドにタスクタブを作成します。",
"startHint": "ブラウザーへのリクエストで独立したタスクウィンドウが作成されます。",
"revoke": "デバイスの認証を取り消す",
"revokeConfirm": "取り消すと、ローカルブラウザーを使うには再度ペアリングが必要です。",
"failed": "操作に失敗しました。再試行してください。",
@ -96,8 +96,10 @@ export default {
"reconnectHint": "認証は保存されています。Chrome と拡張機能を開くと自動的に再接続します。",
"replaceDevice": "ブラウザーを変更",
"installExtension": "BrowserSkill をインストール",
"installHint": "このサーバー用の拡張機能を Chrome にインストールします。",
"downloadExtension": "拡張機能をダウンロード",
"installHint": "このサーバーに対応するパッケージを推奨します。公式ストアへの更新反映は遅れる場合があります。",
"downloadExtension": "対応パッケージをダウンロード",
"officialExtension": "公式拡張機能(Chrome ウェブストア)",
"installGuide": "ダウンロードしたパッケージのインストール方法",
"pairBrowser": "ブラウザーを接続",
"packageUnavailable": "対応する拡張機能パッケージを管理者から入手してください。",
"manualCopy": "下のリンクをコピーしてください。有効期限は 5 分、使用は 1 回限りです。",
@ -105,11 +107,11 @@ export default {
"copyAgain": "再コピー",
"usageTitle": "使い方",
"usageStep1Title": "拡張機能をインストール",
"usageStep1Text": "拡張を解凍し、Chrome の拡張機能ページでデベロッパーモードを有効にして読み込みます。",
"usageStep1Text": "ZIP を解凍し、Chrome のアドレスバーに chrome://extensions と入力します。「デベロッパーモード」を有効にし、「パッケージ化されていない拡張機能を読み込む」から解凍した拡張機能フォルダーを選択します。",
"usageStep2Title": "ブラウザーをペアリング",
"usageStep2Text": "ペアリングリンクをコピーし、拡張機能の「リモート接続」に貼り付けてサーバーを確認します。同じスペースの会話で共有されます。",
"usageStep2Text": "ペアリングリンクを拡張機能の「接続設定 → リモート接続」に貼り付け、サーバーを確認します。同じスペースのすべての会話で共有されます。",
"usageStep3Title": "会話でタスクを伝える",
"usageStep3Text": "入力欄でローカルブラウザーをオンにし、行うウェブタスクを伝えてください。ラベル付きのタブグループで実行されます。",
"usageStep3Text": "ウェブタスクを送る前に、会話の入力欄で「ローカルブラウザー」をオンにしてください。ペアリングだけでは自動でオンになりません。タスクは独立したタスクウィンドウで実行されます。",
"usageStep4Title": "プレビューで確認・再開",
"usageStep4Text": "会話に小さなプレビューが表示されます。クリックするとタスクタブに移動できます。中断したタスクは再接続後も一時停止するので、プレビューから再開してください。既存タブには許可が必要です。",
"running": "実行中",

View File

@ -78,13 +78,13 @@ export default {
"pause": "조작 일시 중지",
"start": "작업 시작",
"stop": "브라우저 작업 종료",
"pairHint": "확장 프로그램의 원격 연결에 링크를 붙여넣고 서버를 확인하세요.",
"pairHint": "확장 프로그램의 연결 설정 → 원격 연결에 페어링 링크를 붙여넣고 서버를 확인하세요. 링크는 5분 동안 한 번만 사용할 수 있습니다.",
"copyPairing": "페어링 링크 복사",
"copied": "복사됨",
"windowHint": "작업은 표시된 탭 그룹에서 실행됩니다. 기존 탭에는 권한이 필요합니다.",
"windowHint": "작업은 별도의 Chrome 작업 창에서 실행됩니다. 기존 탭을 빌리려면 권한이 필요합니다.",
"preview": "로컬 브라우저 미리보기",
"waiting": "작업 페이지를 기다리는 중",
"startHint": "브라우저 작업을 보내면 백그라운드에 작업 탭을 만듭니다.",
"startHint": "브라우저 요청을 보내면 별도의 작업 창이 생성됩니다.",
"revoke": "기기 인증 취소",
"revokeConfirm": "취소하면 다시 연결해야 로컬 브라우저를 사용할 수 있습니다.",
"failed": "작업에 실패했습니다. 다시 시도하세요.",
@ -96,8 +96,10 @@ export default {
"reconnectHint": "인증이 저장되어 있습니다. Chrome과 확장 프로그램을 열면 자동으로 다시 연결됩니다.",
"replaceDevice": "브라우저 변경",
"installExtension": "BrowserSkill 설치",
"installHint": "이 서버용 확장 프로그램을 다운로드하여 Chrome에 설치하세요.",
"downloadExtension": "확장 프로그램 다운로드",
"installHint": "현재 서버와 호환되는 패키지를 권장합니다. 공식 스토어에는 업데이트가 늦게 반영될 수 있습니다.",
"downloadExtension": "호환 패키지 다운로드",
"officialExtension": "공식 확장 프로그램 (Chrome 웹 스토어)",
"installGuide": "다운로드한 패키지는 어떻게 설치하나요?",
"pairBrowser": "브라우저 연결",
"packageUnavailable": "관리자에게 호환되는 확장 프로그램 패키지를 요청하세요.",
"manualCopy": "아래 링크를 복사하세요. 5분 동안 한 번만 사용할 수 있습니다.",
@ -105,11 +107,11 @@ export default {
"copyAgain": "다시 복사",
"usageTitle": "사용 방법",
"usageStep1Title": "확장 프로그램 설치",
"usageStep1Text": "확장 압축을 풀고 Chrome 확장 프로그램 페이지에서 개발자 모드를 켜 로드하세요.",
"usageStep1Text": "ZIP 파일의 압축을 풀고 Chrome 주소창에 chrome://extensions를 입력하세요. 개발자 모드를 켜고 압축해제된 확장 프로그램을 로드합니다를 선택한 뒤 압축을 푼 확장 프로그램 폴더를 선택하세요.",
"usageStep2Title": "브라우저 연결",
"usageStep2Text": "페어링 링크를 복사해 확장 프로그램의 원격 연결에 붙여넣고 서버를 확인하세요. 한 번 연결하면 공간 내 모든 대화에서 공유됩니다.",
"usageStep2Text": "페어링 링크를 확장 프로그램의 연결 설정 → 원격 연결에 붙여넣고 서버를 확인하세요. 한 번 연결하면 공간 내 모든 대화에서 공유됩니다.",
"usageStep3Title": "대화에서 작업 요청",
"usageStep3Text": "입력창에서 로컬 브라우저를 켜고 웹 작업을 요청하세요. 표시된 탭 그룹에서 실행됩니다.",
"usageStep3Text": "웹 작업을 보내기 전에 대화 입력창에서 로컬 브라우저를 켜세요. 페어링만으로 자동 활성화되지는 않습니다. 작업은 별도의 작업 창에서 실행됩니다.",
"usageStep4Title": "미리보기로 확인하고 이어가기",
"usageStep4Text": "대화에 작은 미리보기가 나타납니다. 클릭하면 작업 탭으로 이동합니다. 중단된 작업은 다시 연결해도 일시정지 상태이므로 미리보기에서 계속하세요. 기존 탭에는 권한이 필요합니다.",
"running": "실행 중",

View File

@ -78,13 +78,13 @@ export default {
"pause": "Приостановить браузер",
"start": "Начать задачу",
"stop": "Завершить задачу браузера",
"pairHint": "Вставьте ссылку в раздел удалённого подключения расширения и проверьте сервер.",
"pairHint": "Откройте в расширении «Настройки подключения → Удалённое подключение», вставьте ссылку и подтвердите сервер. Ссылка действует 5 минут и используется один раз.",
"copyPairing": "Скопировать ссылку",
"copied": "Скопировано",
"windowHint": "Задачи работают в отмеченной группе вкладок. Для существующих вкладок требуется разрешение.",
"windowHint": "Задачи выполняются в отдельном окне Chrome. Заимствование существующих вкладок требует вашего разрешения.",
"preview": "Предпросмотр локального браузера",
"waiting": "Ожидание страницы задачи",
"startHint": "Запрос браузера создаёт фоновые вкладки задачи.",
"startHint": "Запрос к браузеру создаёт отдельное окно агента.",
"revoke": "Отозвать доступ устройства",
"revokeConfirm": "После отзыва потребуется повторное подключение, чтобы снова использовать локальный браузер.",
"failed": "Операция не удалась. Повторите попытку.",
@ -96,8 +96,10 @@ export default {
"reconnectHint": "Авторизация сохранена. Откройте Chrome и BrowserSkill для автоматического подключения.",
"replaceDevice": "Сменить браузер",
"installExtension": "Установите BrowserSkill",
"installHint": "Скачайте расширение для этого сервера и установите в Chrome.",
"downloadExtension": "Скачать расширение",
"installHint": "Рекомендуем пакет для этого сервера: обновления в официальном магазине могут появляться позже.",
"downloadExtension": "Скачать совместимый пакет",
"officialExtension": "Официальное расширение (Интернет-магазин Chrome)",
"installGuide": "Как установить скачанный пакет?",
"pairBrowser": "Подключите браузер",
"packageUnavailable": "Запросите совместимое расширение у администратора.",
"manualCopy": "Скопируйте ссылку ниже. Она действует 5 минут и используется один раз.",
@ -105,11 +107,11 @@ export default {
"copyAgain": "Копировать ещё раз",
"usageTitle": "Как пользоваться",
"usageStep1Title": "Установите расширение",
"usageStep1Text": "Распакуйте пакет, включите режим разработчика на странице расширений Chrome и загрузите папку.",
"usageStep1Text": "Распакуйте ZIP, введите chrome://extensions в адресной строке Chrome, включите режим разработчика, нажмите «Загрузить распакованное расширение» и выберите папку расширения.",
"usageStep2Title": "Подключите браузер",
"usageStep2Text": "Скопируйте ссылку и вставьте её в раздел удалённого подключения расширения. Одно подключение используется во всех диалогах пространства.",
"usageStep2Text": "Вставьте ссылку в «Настройки подключения → Удалённое подключение» расширения и подтвердите сервер. Одно подключение используется во всех диалогах пространства.",
"usageStep3Title": "Опишите задачу в диалоге",
"usageStep3Text": "Включите локальный браузер в поле ввода и опишите веб-задачу. Она выполняется в отмеченной группе вкладок.",
"usageStep3Text": "Перед отправкой веб-задачи включите локальный браузер в поле ввода диалога. Подключение не включает эту опцию автоматически. Задачи выполняются в отдельном окне агента.",
"usageStep4Title": "Просматривайте и продолжайте через предпросмотр",
"usageStep4Text": "В диалоге появится миниатюра. Нажмите её, чтобы перейти к вкладке задачи. Прерванные задачи остаются на паузе после переподключения — продолжите через предпросмотр. Для существующих вкладок нужно разрешение.",
"running": "Выполняется",

View File

@ -78,13 +78,13 @@ export default {
"pause": "暂停操作",
"start": "开始任务",
"stop": "结束浏览器任务",
"pairHint": "在扩展的「远程连接」中粘贴配对链接,确认服务器后连接。",
"pairHint": "打开扩展的「连接设置 → 远程连接」,粘贴配对链接并确认服务器。链接 5 分钟内有效,仅可使用一次。",
"copyPairing": "复制配对链接",
"copied": "已复制",
"windowHint": "任务在 Chrome 的 WeKnora 标签组内运行。借用已有标签页需确认。",
"windowHint": "任务在独立的 Chrome 任务窗口中运行。借用已有标签页需要你的授权。",
"preview": "本机浏览器任务预览",
"waiting": "等待任务页面",
"startHint": "发送浏览器任务后,会在后台创建带标识的任务标签。",
"startHint": "发起浏览器请求后,会创建独立的任务窗口。",
"revoke": "撤销设备授权",
"revokeConfirm": "撤销后需要重新配对,才能继续使用本机浏览器。",
"failed": "操作失败,请重试",
@ -96,8 +96,10 @@ export default {
"reconnectHint": "保留授权,等待扩展自动重连。请确认 Chrome 和 BrowserSkill 扩展已开启。",
"replaceDevice": "更换浏览器",
"installExtension": "安装 BrowserSkill 扩展",
"installHint": "下载与当前服务配套的扩展,安装到 Chrome。",
"downloadExtension": "下载扩展",
"installHint": "建议使用与当前服务匹配的配套扩展;官方商店版本可能稍晚更新。",
"downloadExtension": "下载配套扩展",
"officialExtension": "官方扩展(Chrome 应用商店)",
"installGuide": "如何安装下载的扩展包?",
"pairBrowser": "连接这台浏览器",
"packageUnavailable": "管理员尚未配置扩展下载包,请向管理员获取配套版本。",
"manualCopy": "点击下方输入框复制链接,5 分钟内有效,仅可配对一次。",
@ -105,11 +107,11 @@ export default {
"copyAgain": "再次复制",
"usageTitle": "使用方式",
"usageStep1Title": "安装扩展",
"usageStep1Text": "解压扩展包,在 Chrome 的扩展程序页面开启开发者模式,选择「加载已解压的扩展程序」。",
"usageStep1Text": "下载 ZIP 后解压,在 Chrome 地址栏输入 chrome://extensions,开启「开发者模式」,点击「加载已解压的扩展程序」,选择解压后的扩展目录。",
"usageStep2Title": "配对这台浏览器",
"usageStep2Text": "复制配对链接,在扩展的「远程连接」中粘贴并确认服务器。配对一次,同一空间的各个对话共用。",
"usageStep2Text": "复制配对链接,在扩展的「连接设置 → 远程连接」中粘贴并确认服务器。配对一次,同一空间的各个对话共用。",
"usageStep3Title": "回到对话描述任务",
"usageStep3Text": "在输入栏打开本机浏览器,直接描述要完成的网页任务。任务会在带标识的标签组中运行。",
"usageStep3Text": "在对话输入栏开启「本机浏览器」,再发送网页任务。配对不会自动开启此选项,任务会在独立的任务窗口中运行。",
"usageStep4Title": "通过预览查看和继续",
"usageStep4Text": "对话里会出现小预览,点击可定位到任务标签。中断或重连后任务仍会暂停,请在预览中点继续;借用已有标签页会先请你确认。",
"running": "运行中",

View File

@ -61,6 +61,16 @@
<div class="step-copy">
<strong>{{ t('localBrowser.installExtension') }}</strong>
<p>{{ t('localBrowser.installHint') }}</p>
<a class="official-extension-link"
href="https://chromewebstore.google.com/detail/hhcmgoofomhgciiibhipgmgkgnoenaoi"
target="_blank" rel="noopener noreferrer">
{{ t('localBrowser.officialExtension') }}
<t-icon name="jump" size="14px" />
</a>
<details class="install-guide">
<summary>{{ t('localBrowser.installGuide') }}</summary>
<p>{{ t('localBrowser.usageStep1Text') }}</p>
</details>
</div>
<t-button theme="default" variant="outline" size="small"
:disabled="!status.extension_available || downloading" @click="download">
@ -425,6 +435,34 @@ onBeforeUnmount(() => { alive = false; controller.abort(); clearTimeout(timer);
color: var(--td-text-color-secondary);
}
.official-extension-link {
display: inline-flex;
align-items: center;
gap: 4px;
margin-top: 4px;
color: var(--td-brand-color);
font-size: var(--app-text-sm);
line-height: 1.6;
text-decoration: none;
&:hover,
&:focus-visible {
text-decoration: underline;
}
}
.install-guide {
margin-top: 6px;
color: var(--td-text-color-secondary);
font-size: var(--app-text-sm);
line-height: 1.6;
summary {
cursor: pointer;
width: fit-content;
}
}
.pairing-feedback {
margin-top: 16px;
padding: 10px 12px;

View File

@ -75,11 +75,16 @@ Task tabs:
- tab_list scope=user means not authorized for this task. Call tab_borrow before
tab_select, reading, request_help or other operations on such a tab. Listing a tab
does not authorize it. Follow extension confirmation and return borrowed tabs when done.
- New tabs opened directly by your action on a controlled page belong to this task when
the extension can verify their source. List tabs and use the returned tab_id to continue.
- New tabs opened by native click/key input within the Agent Window can be controlled
when the extension verifies their source. They are observed tabs, preserved at task end.
Separate popup windows and unattributed tabs require tab_borrow before content access.
List tabs and use the returned tab_id; never infer authorization from window membership.
- If a tab is unauthorized, call tab_borrow once and let the extension request approval;
this also works inside the task window. Never ask the user to move it out merely to borrow.
During borrowing, the user must approve in the target page's BrowserSkill confirmation.
an unowned tab already inside the Agent Window must first be moved by the user to a
regular browser window. For a lookup that does not need its live state, navigate in an
owned task tab instead. Borrow confirmation appears on a regular browser window's
HTTP(S) page; if no page can display it, ask the user to open one before retrying.
Follow the extension's browser-controlled confirmation preference.
A denied or timed-out borrow requires user intervention, not repeated select/read/close
or request_help attempts. Continue operation only resumes a paused task; it does not
approve borrowing. After explicit resume, a fresh borrow still needs browser approval.

View File

@ -29,19 +29,30 @@ func browserRecoveryHint(method string, err *browserskill.RPCError) string {
Effect string `json:"effect_state"`
}
_ = json.Unmarshal(err.Data, &data)
if method == "tab_borrow" && data.Reason == "confirmation_ui_unavailable" {
return "Ask the user to open an HTTP(S) page in a regular browser window so BrowserSkill can " +
"display the borrow confirmation, then request tab_borrow again. Task windows, separate " +
"popups and extension settings cannot host that confirmation. Do not retry until a page is available."
}
if method == "tab_borrow" && err.Code == "invalid_params" &&
strings.Contains(err.Message, "not authorized and already lives in the Agent Window") {
return "Ask the user to move this unowned tab to a regular browser window before calling " +
"tab_borrow again. If its live state is unnecessary, navigate in an owned task tab instead. " +
"Being inside the Agent Window does not authorize access."
}
if method == "tab_borrow" && (data.Reason == "confirmation_timeout" ||
err.Code == "timeout" || err.Code == "cancelled" || err.Code == "user_aborted") {
return "Stop browser actions: tab borrowing was not approved. The confirmation is no longer pending. " +
"Ask the user to click Continue operation in the conversation preview " +
"before continuing in this same conversation. " +
"Continue operation only resumes the task; it does not grant tab access. After explicit resume, " +
"request tab_borrow once again and wait for Allow in the target page's BrowserSkill confirmation. " +
"request tab_borrow once again and wait for Allow in the browser's BrowserSkill confirmation. " +
"Do not call request_help on the unapproved tab or reconnect/pair again."
}
if err.Code == "permission_denied" && (strings.Contains(err.Message, "borrow this tab before") ||
strings.Contains(err.Message, "Authorize this tab with tab_borrow")) {
return "This tab is not authorized for the current task. If its existing page state is needed, " +
"call tab_borrow once and wait for the user's approval in the target page. " +
"call tab_borrow once and follow the extension's browser-controlled approval flow. " +
"Do not retry tab_select/read/close or call request_help before borrowing succeeds. " +
"For an ordinary website lookup that does not need this tab's state, navigate in the task's own tab."
}
@ -72,6 +83,11 @@ func browserRecoveryHint(method string, err *browserskill.RPCError) string {
case "target_not_select", "option_not_found", "single_select_value_count":
return "select requires a native select and its option values. Inspect current options; " +
"use click/observe for a custom dropdown."
case "renderer_read_timeout":
return "A browser read is unavailable and further reads may be refused while it is still pending. " +
"Do not repeat snapshot/observe on this tab. Navigation alone does not clear the pending read; " +
"it must settle or its debugger session detach. Switch to another tab, or finish the turn " +
"and report the unavailable read."
case "agent_window_scope", "borrow_conflict":
return "List actual user tabs and respect tab ownership. Borrow the intended available tab " +
"through extension confirmation; never guess its ID."

View File

@ -159,6 +159,19 @@ func TestBorrowRecoveryDoesNotConfuseResumeWithApproval(t *testing.T) {
require.Contains(t, hint, "does not grant tab access")
require.Contains(t, hint, "After explicit resume")
require.Contains(t, hint, "Do not call request_help")
hint = browserRecoveryHint("tab_borrow", &browserskill.RPCError{
Code: "unsupported", Message: "No user tab can display the borrow confirmation",
Data: json.RawMessage(`{"reason":"confirmation_ui_unavailable"}`),
})
require.Contains(t, hint, "HTTP(S) page in a regular browser window")
require.Contains(t, hint, "Do not retry until a page is available")
hint = browserRecoveryHint("tab_borrow", &browserskill.RPCError{
Code: "invalid_params",
Message: "tab_borrow: tab 7 is not authorized and already lives in the Agent Window; " +
"move it to a regular browser window, then borrow it",
})
require.Contains(t, hint, "Ask the user to move")
require.Contains(t, hint, "navigate in an owned task tab")
// Unrelated permission denials must not suggest a way to borrow around them.
hint = browserRecoveryHint("click", &browserskill.RPCError{Code: "permission_denied", Message: "policy denied"})
require.NotContains(t, hint, "call tab_borrow")

View File

@ -163,7 +163,7 @@ func TestRealExtension(t *testing.T) {
}
}()
if err = json.NewEncoder(input).Encode(map[string]string{
"pairing": link, "extension": extension,
"pairing": link, "extension": extension, "fixture": fixture.URL,
"chromium": os.Getenv("BROWSERSKILL_TEST_CHROMIUM"),
"playwright": os.Getenv("BROWSERSKILL_TEST_PLAYWRIGHT"),
}); err != nil {
@ -309,20 +309,92 @@ func TestRealExtension(t *testing.T) {
t.Fatalf("action result missing: %s", snap)
}
checkBackground()
// Agent-created navigation targets remain usable throughout their lifecycle.
// Cover both noopener links and script-created popup windows.
for _, selector := range []string{"#new-tab", "#popup"} {
if _, err = call(ctx, scope, "chat", "click", map[string]any{"selector": selector}); err != nil {
hostAck := func(command, expected string) {
t.Helper()
if _, err := fmt.Fprintln(input, command); err != nil {
t.Fatal(err)
}
select {
case line := <-hostLines:
if line != expected {
t.Fatalf("browser host %s: %s", command, line)
}
case <-ctx.Done():
t.Fatal(ctx.Err())
}
}
assertDenied := func(tabID int) {
t.Helper()
for _, method := range []string{"tab_select", "snapshot", "tab_close"} {
_, err := call(ctx, scope, "chat", method, map[string]any{"tab_id": tabID})
if err == nil || !strings.Contains(err.Error(), "permission_denied") {
t.Fatalf("unauthorized tab %d accepted %s: %v", tabID, method, err)
}
}
}
borrowTab := func(tabID int) {
t.Helper()
done := make(chan error, 1)
go func() {
_, err := call(ctx, scope, "chat", "tab_borrow", map[string]any{"tab_id": tabID})
done <- err
}()
for !m.Status(scope, "chat").NeedsHelp {
select {
case err := <-done:
t.Fatalf("borrow finished without browser confirmation: %v", err)
case <-tick.C:
case <-ctx.Done():
t.Fatal(ctx.Err())
}
}
if m.Status(scope, "chat").Action != "tab_borrow" {
t.Fatal("borrow confirmation must identify its handoff separately from login help")
}
hostAck("approve-borrow", "approve-borrow-done")
select {
case err := <-done:
if err != nil {
t.Fatal(err)
}
case <-ctx.Done():
t.Fatal(ctx.Err())
}
}
returnTab := func(tabID int) {
t.Helper()
hostAck(fmt.Sprintf("before-tab-return %d", tabID), "return-recorded")
result, err := call(ctx, scope, "chat", "tab_return", map[string]any{"tab_id": tabID})
if err != nil {
t.Fatal(err)
}
var compact bytes.Buffer
if err := json.Compact(&compact, result); err != nil {
t.Fatal(err)
}
hostAck("expect-returned-tab "+compact.String(), "returned-tab-preserved")
assertDenied(tabID)
}
// Same-window navigation targets are observed; independent popup windows
// require the official borrow/return flow, even when opened by native input.
for _, target := range []struct {
selector string
path string
borrow bool
}{
{"#new-tab", "/linked", false},
{"#popup", "/popup", true},
} {
t.Logf("navigation target %s (borrow=%t)", target.selector, target.borrow)
if _, err = call(ctx, scope, "chat", "click", map[string]any{"selector": target.selector}); err != nil {
t.Fatal(err)
}
popupID := 0
var e error
deadline := time.NewTimer(3 * time.Second)
waitPopup:
deadline := time.After(3 * time.Second)
for popupID == 0 {
listed, listErr := call(ctx, scope, "chat", "tab_list", map[string]any{"scope": "all"})
if listErr != nil {
t.Fatal(listErr)
listed, err := call(ctx, scope, "chat", "tab_list", map[string]any{"scope": "all"})
if err != nil {
t.Fatal(err)
}
var result struct {
Tabs []struct {
@ -330,38 +402,50 @@ func TestRealExtension(t *testing.T) {
URL string `json:"url"`
} `json:"tabs"`
}
if e = json.Unmarshal(listed, &result); e != nil {
t.Fatal(e)
if err := json.Unmarshal(listed, &result); err != nil {
t.Fatal(err)
}
for _, tab := range result.Tabs {
if strings.HasSuffix(tab.URL, "/linked") || strings.HasSuffix(tab.URL, "/popup") {
if tab.URL == fixture.URL+target.path {
popupID = tab.ID
}
}
if popupID != 0 {
break waitPopup
break
}
select {
case <-tick.C:
case <-deadline.C:
case <-deadline:
t.Fatalf("fixture popup missing: %s", listed)
case <-ctx.Done():
t.Fatal(ctx.Err())
}
}
deadline.Stop()
if _, e = call(ctx, scope, "chat", "tab_select", map[string]any{"tab_id": popupID}); e != nil {
t.Fatal(e)
if target.borrow {
assertDenied(popupID)
borrowTab(popupID)
}
if _, e = call(ctx, scope, "chat", "snapshot", map[string]any{"tab_id": popupID}); e != nil {
t.Fatal(e)
if _, err = call(ctx, scope, "chat", "tab_select", map[string]any{"tab_id": popupID}); err != nil {
t.Fatal(err)
}
if _, e = call(ctx, scope, "chat", "tab_close", map[string]any{"tab_id": popupID}); e != nil {
t.Fatal(e)
if _, err = call(ctx, scope, "chat", "snapshot", map[string]any{"tab_id": popupID}); err != nil {
t.Fatal(err)
}
if target.borrow {
if _, err = call(ctx, scope, "chat", "tab_close", map[string]any{"tab_id": popupID}); err == nil ||
!strings.Contains(err.Error(), "invalid_params") {
t.Fatalf("borrowed popup was closable: %v", err)
}
returnTab(popupID)
// Keep the returned page: later cleanup checks must prove that
// task stop preserves it and releases its debugger attachment.
} else if _, err = call(ctx, scope, "chat", "tab_close", map[string]any{"tab_id": popupID}); err != nil {
t.Fatal(err)
}
}
// A genuine user-created tab inside the task window remains unauthorized,
// but can be borrowed there with an actual browser confirmation.
// A genuine user-created tab inside the task window remains unauthorized and
// cannot be borrowed in place (upstream PR #297). Once moved to a regular
// window it can be borrowed with an actual browser confirmation.
_, _ = io.WriteString(input, "create-unowned-tab\n")
var userTab struct {
ID int `json:"tab_id"`
@ -380,48 +464,24 @@ func TestRealExtension(t *testing.T) {
t.Fatalf("unowned user tab accepted %s: %v", method, err)
}
}
borrowDone := make(chan error, 1)
go func() {
_, borrowErr := call(ctx, scope, "chat", "tab_borrow", map[string]any{"tab_id": userTab.ID})
borrowDone <- borrowErr
}()
for !m.Status(scope, "chat").NeedsHelp {
select {
case <-tick.C:
case <-ctx.Done():
t.Fatal(ctx.Err())
}
if _, err = call(ctx, scope, "chat", "tab_borrow", map[string]any{"tab_id": userTab.ID}); err == nil ||
!strings.Contains(err.Error(), "invalid_params") {
t.Fatalf("unowned tab inside the Agent Window was borrowed in place: %v", err)
}
if m.Status(scope, "chat").Action != "tab_borrow" {
t.Fatal("borrow confirmation must identify its handoff separately from login help")
}
_, _ = io.WriteString(input, "approve-borrow\n")
_, _ = fmt.Fprintf(input, "move-fixture-tab-out %d\n", userTab.ID)
select {
case line := <-hostLines:
if line != "approve-borrow-done" {
t.Fatalf("borrow confirmation: %s", line)
}
case <-ctx.Done():
t.Fatal(ctx.Err())
}
select {
case err = <-borrowDone:
if err != nil {
t.Fatal(err)
if line != "fixture-tab-moved" {
t.Fatalf("move fixture tab: %s", line)
}
case <-ctx.Done():
t.Fatal(ctx.Err())
}
borrowTab(userTab.ID)
if _, err = call(ctx, scope, "chat", "snapshot", map[string]any{"tab_id": userTab.ID}); err != nil {
t.Fatal(err)
}
if _, err = call(ctx, scope, "chat", "tab_return", map[string]any{"tab_id": userTab.ID}); err != nil {
t.Fatal(err)
}
if _, err = call(ctx, scope, "chat", "snapshot", map[string]any{"tab_id": userTab.ID}); err == nil ||
!strings.Contains(err.Error(), "permission_denied") {
t.Fatalf("returned tab remained readable: %v", err)
}
returnTab(userTab.ID)
_, _ = fmt.Fprintf(input, "remove-fixture-tab %d\n", userTab.ID)
select {
case line := <-hostLines:
@ -743,7 +803,7 @@ func TestRealExtension(t *testing.T) {
select {
case line := <-hostLines:
if line != `{"cleaned":true}` {
t.Fatalf("browser task leaked tabs or groups: %s", line)
t.Fatalf("browser cleanup changed user tabs or leaked task tabs: %s", line)
}
case <-ctx.Done():
t.Fatal(ctx.Err())
@ -816,8 +876,8 @@ func TestRealExtension(t *testing.T) {
t.Fatal(err)
}
checkCleanup()
// Repeated successful turns must return to the original user tabs, with
// no live task groups left over. The next turn can start without Resume.
// Repeated successful turns preserve the original and returned user tabs,
// with no agent-created tabs left over. The next turn needs no Resume.
for round := range 3 {
t.Logf("automatic cleanup round %d", round+1)
if err = m.Control(ctx, scope, "chat", "select"); err != nil {

View File

@ -12,9 +12,12 @@ type uiReply struct {
err error
}
// Extensions without the optional UI channel (BrowserSkill main after PR #296)
// route ui.* requests to the native dispatcher, which answers unknown_method.
var errGatewayUIUnsupported = &RPCError{
Code: "gateway_ui_unsupported",
Message: "BrowserSkill extension lacks WeKnora task controls; install the companion extension",
Code: "gateway_ui_unsupported",
Message: "BrowserSkill extension lacks the ui.task_preview/ui.task_focus channel; " +
"install the extension built from the pinned baseline",
}
// Focus is user initiated. Only the server-owned task ID is sent to Chrome.
@ -22,7 +25,7 @@ func (m *Manager) Focus(ctx context.Context, s Scope, session string) error {
if _, remote, err := m.route(ctx, s, session, "focus", "", nil); remote || err != nil {
return err
}
data, err := m.callUI(ctx, s, session, "gateway.task_focus")
data, err := m.callUI(ctx, s, session, "ui.task_focus")
if err != nil {
return err
}

View File

@ -17,6 +17,7 @@ import (
"net/url"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"time"
@ -710,6 +711,95 @@ func rpc(ctx context.Context, d *device, method string, params any) (json.RawMes
return reply.Result, nil
}
// agentWindowTabs lists every tab in the task's Agent Window, including tabs
// the browser user opened there; any of them keeps the window alive.
func agentWindowTabs(ctx context.Context, d *device, session string) ([]float64, error) {
list, err := rpc(ctx, d, "tool.tab_list", map[string]any{"session_id": session, "scope": "agent"})
if err != nil {
return nil, err
}
var listed struct {
Tabs []struct {
ID float64 `json:"tab_id"`
} `json:"tabs"`
}
if json.Unmarshal(list, &listed) != nil {
return nil, errors.New("invalid BrowserSkill tab list")
}
ids := make([]float64, 0, len(listed.Tabs))
for _, tab := range listed.Tabs {
ids = append(ids, tab.ID)
}
return ids, nil
}
// preserveAgentWindow keeps a retained task's Agent Window open when the agent
// closes its last tab there. Chrome removes a window together with its final
// tab, and the extension reports that removal as a user-closed window, which
// would pause the task and drop its session. A blank tab created through the
// native tab_create RPC is agent-owned, so session stop closes it with the
// other agent tabs. Only official RPCs are used; the extension needs no patch.
// It returns the placeholder's tab ID, or 0 when none was needed.
func preserveAgentWindow(ctx context.Context, d *device, session string, tabID any) (float64, error) {
target, ok := numericID(tabID)
if !ok {
return 0, nil // The extension reports the invalid parameter itself.
}
tabs, err := agentWindowTabs(ctx, d, session)
if err != nil {
return 0, err
}
if len(tabs) != 1 || tabs[0] != target {
return 0, nil
}
created, err := rpc(ctx, d, "tool.tab_create", map[string]any{
"session_id": session, "url": "about:blank", "active": false,
})
if err != nil {
return 0, err
}
var placeholder struct {
ID float64 `json:"tab_id"`
}
if json.Unmarshal(created, &placeholder) != nil || placeholder.ID == 0 {
return 0, errors.New("invalid BrowserSkill tab_create result")
}
return placeholder.ID, nil
}
// releasePlaceholder undoes preserveAgentWindow after tab_close failed or was
// interrupted, so a refused close (unauthorized or borrowed tab) does not leave
// an extra agent-owned blank tab behind. The extension may have closed the
// target before the reply was lost, in which case the placeholder is the tab
// keeping the Agent Window open: it is removed only while the target still
// exists. Best effort with its own deadline, since the failed call's context
// may already be cancelled.
func releasePlaceholder(d *device, session string, tabID any, placeholder float64) {
target, _ := numericID(tabID)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
tabs, err := agentWindowTabs(ctx, d, session)
if err != nil || !slices.Contains(tabs, target) {
return
}
_, _ = rpc(ctx, d, "tool.tab_close", map[string]any{"session_id": session, "tab_id": placeholder})
}
func numericID(v any) (float64, bool) {
switch n := v.(type) {
case float64:
return n, true
case int:
return float64(n), true
case int64:
return float64(n), true
case json.Number:
f, err := n.Float64()
return f, err == nil
}
return 0, false
}
var methods = map[string]bool{
"snapshot": true,
"observe": true,
@ -873,7 +963,18 @@ func (m *Manager) Call(
clean["timeout_ms"] = humanTimeoutMS(clean["timeout_ms"])
}
}
result, err := rpc(callCtx, d, "tool."+method, clean)
var result json.RawMessage
var err error
var placeholder float64
if method == "tab_close" {
placeholder, err = preserveAgentWindow(callCtx, d, id, clean["tab_id"])
}
if err == nil {
result, err = rpc(callCtx, d, "tool."+method, clean)
if err != nil && placeholder != 0 {
releasePlaceholder(d, id, clean["tab_id"], placeholder)
}
}
if method == "request_help" && err == nil {
var help struct {
Outcome string `json:"outcome"`
@ -1146,7 +1247,7 @@ func (m *Manager) Preview(ctx context.Context, s Scope, session string) (json.Ra
id := t.id
d.mu.Unlock()
defer func() { d.mu.Lock(); t.previewBusy = false; d.mu.Unlock() }()
frame, err := m.callUI(ctx, s, session, "gateway.task_preview")
frame, err := m.callUI(ctx, s, session, "ui.task_preview")
if err != nil {
return nil, err
}

View File

@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httptest"
"os"
"slices"
"strings"
"sync"
"sync/atomic"
@ -161,9 +162,9 @@ func connectSharedFixture(
return
}
result = map[string]any{"cancelled": true}
case "gateway.task_preview":
case "ui.task_preview":
result = map[string]any{"image_base64": "dGVzdA==", "format": "jpeg"}
case "gateway.task_focus":
case "ui.task_focus":
result = map[string]any{"focused": true}
case "system.ping":
result = map[string]any{"pong": true}
@ -736,7 +737,7 @@ func TestFinishTurnUsesOfficialLifecycleWithoutGatewayCalls(t *testing.T) {
connectSharedFixture(ctx, t, owner, scope, "", "browser", func(
f *sharedFixture, id, method string, _ map[string]any,
) bool {
if !strings.HasPrefix(method, "gateway.") {
if !strings.HasPrefix(method, "ui.") {
return false
}
gatewayCalls.Add(1)
@ -831,3 +832,111 @@ func TestBorrowConfirmationBudgetReachesExtension(t *testing.T) {
})
}
}
func TestAgentClosingLastTabKeepsAgentWindow(t *testing.T) {
m, ctx := sharedTestManager(t)
s := Scope{1, "last-tab"}
var mu sync.Mutex
var methods []string
var created []map[string]any
var closeError string // error code the fixture returns when closing tab 9
var closeDrops bool // the extension closed the tab before failing the reply
tabs := []map[string]any{{"tab_id": 7, "window_id": 100, "scope": "agent"}}
removeTab := func(id any) {
want, _ := numericID(id)
tabs = slices.DeleteFunc(tabs, func(tab map[string]any) bool {
got, _ := numericID(tab["tab_id"])
return got == want
})
}
connectSharedFixture(ctx, t, m, s, "", "browser", func(
f *sharedFixture, id, method string, params map[string]any,
) bool {
mu.Lock()
defer mu.Unlock()
var result any
switch method {
case "tool.tab_list":
if params["scope"] != "agent" {
_ = f.send(map[string]any{"id": id, "error": map[string]string{
"code": "invalid_params", "message": "fixture expects the agent scope",
}})
return true
}
result = map[string]any{"tabs": append([]map[string]any(nil), tabs...)}
case "tool.tab_create":
created = append(created, params)
tabs = append(tabs, map[string]any{"tab_id": 8, "window_id": 100, "scope": "agent"})
result = map[string]any{"tab_id": 8, "window_id": 100, "url": "about:blank"}
case "tool.tab_close":
methods = append(methods, fmt.Sprintf("tool.tab_close:%v", params["tab_id"]))
if target, _ := numericID(params["tab_id"]); closeError != "" && target == 9 {
if closeDrops {
removeTab(params["tab_id"])
}
_ = f.send(map[string]any{"id": id, "error": map[string]string{
"code": closeError, "message": "fixture refused",
}})
return true
}
removeTab(params["tab_id"])
_ = f.send(map[string]any{"id": id, "result": map[string]any{"tab_id": params["tab_id"]}})
return true
default:
return false
}
methods = append(methods, method)
_ = f.send(map[string]any{"id": id, "result": result})
return true
})
require.NoError(t, m.Control(ctx, s, "chat", "select"))
// The final agent tab gets a blank replacement so Chrome keeps the window.
_, err := m.Call(ctx, s, "chat", "tab_close", map[string]any{"tab_id": float64(7)})
require.NoError(t, err)
mu.Lock()
require.Equal(t, []string{"tool.tab_list", "tool.tab_create", "tool.tab_close:7"}, methods)
require.Len(t, created, 1)
require.Equal(t, "about:blank", created[0]["url"])
require.Equal(t, false, created[0]["active"])
require.NotEmpty(t, created[0]["session_id"])
methods = nil
tabs = []map[string]any{
{"tab_id": 8, "window_id": 100, "scope": "agent"},
{"tab_id": 9, "window_id": 100, "scope": "user"},
}
mu.Unlock()
// Any other tab in the Agent Window, even a user's, keeps it open.
_, err = m.Call(ctx, s, "chat", "tab_close", map[string]any{"tab_id": 8})
require.NoError(t, err)
mu.Lock()
require.Equal(t, []string{"tool.tab_list", "tool.tab_close:8"}, methods)
// A refused close (unauthorized or borrowed tab) must not leave the
// placeholder behind: the target is still there, so the blank tab goes.
methods, created = nil, nil
tabs = []map[string]any{{"tab_id": 9, "window_id": 100, "scope": "agent"}}
closeError = "permission_denied"
mu.Unlock()
_, err = m.Call(ctx, s, "chat", "tab_close", map[string]any{"tab_id": 9})
require.ErrorContains(t, err, "permission_denied")
mu.Lock()
require.Equal(t, []string{
"tool.tab_list", "tool.tab_create", "tool.tab_close:9", "tool.tab_list", "tool.tab_close:8",
}, methods)
require.Len(t, tabs, 1)
require.Equal(t, 9, tabs[0]["tab_id"])
require.False(t, m.Status(s, "chat").Paused)
// When the extension already removed the target before the reply failed,
// the placeholder is what keeps the window open and must stay.
methods, created = nil, nil
closeError, closeDrops = "cdp_failed", true
mu.Unlock()
_, err = m.Call(ctx, s, "chat", "tab_close", map[string]any{"tab_id": 9})
require.ErrorContains(t, err, "cdp_failed")
mu.Lock()
require.Equal(t, []string{
"tool.tab_list", "tool.tab_create", "tool.tab_close:9", "tool.tab_list",
}, methods)
require.Len(t, tabs, 1)
require.Equal(t, 8, tabs[0]["tab_id"])
mu.Unlock()
}

View File

@ -1,562 +0,0 @@
diff --git a/apps/extension/src/browser-driver/__tests__/chromium-cdp.test.ts b/apps/extension/src/browser-driver/__tests__/chromium-cdp.test.ts
index 8f99c94..16f7499 100644
--- a/apps/extension/src/browser-driver/__tests__/chromium-cdp.test.ts
+++ b/apps/extension/src/browser-driver/__tests__/chromium-cdp.test.ts
@@ -30,6 +30,77 @@ function fakeApi() {
}
describe("ChromiumCdp", () => {
+ it("configures frame auto-attach once per debugger session and reapplies after detach", async () => {
+ const { api, onDetach } = fakeApi();
+ vi.mocked(api.sendCommand).mockResolvedValue({ frameTree: { frame: { id: "main" } } });
+ const cdp = new ChromiumCdp(api);
+ const calls = () =>
+ vi.mocked(api.sendCommand).mock.calls.filter((c) => c[1] === "Target.setAutoAttach");
+ await cdp.ensureAttached(4);
+ await Promise.all([cdp.getFrameGraph(4), cdp.getFrameGraph(4)]);
+ await cdp.getFrameGraph(4);
+ expect(calls()).toHaveLength(1);
+ onDetach.fire({ tabId: 4 }, "canceled_by_user");
+ await cdp.getFrameGraph(4);
+ expect(calls()).toHaveLength(2);
+ await cdp.detachAll();
+ await cdp.getFrameGraph(4);
+ expect(calls()).toHaveLength(3);
+ cdp.dispose();
+ });
+
+ it("retries frame discovery after a failed configuration", async () => {
+ const { api } = fakeApi();
+ let attempts = 0;
+ vi.mocked(api.sendCommand).mockImplementation(async (_target, method) => {
+ if (method === "Target.setAutoAttach" && ++attempts === 1) throw new Error("transient");
+ return { frameTree: { frame: { id: "main" } } };
+ });
+ const cdp = new ChromiumCdp(api);
+ await cdp.ensureAttached(4);
+ await cdp.getFrameGraph(4);
+ await cdp.getFrameGraph(4);
+ expect(attempts).toBe(2);
+ cdp.dispose();
+ });
+
+ it("does not send another screenshot after a local timeout while Chrome is still working", async () => {
+ vi.useFakeTimers();
+ const warning = vi.spyOn(console, "warn").mockImplementation(() => {});
+ const { api } = fakeApi();
+ let finish!: (value: unknown) => void;
+ vi.mocked(api.sendCommand).mockImplementation(async (_target, method) => {
+ if (method === "Page.captureScreenshot")
+ return new Promise((resolve) => {
+ finish = resolve;
+ });
+ return {};
+ });
+ const cdp = new ChromiumCdp(api);
+ try {
+ await cdp.ensureAttached(4);
+ const first = expect(cdp.send(4, "Page.captureScreenshot")).rejects.toThrow("timed out");
+ await vi.advanceTimersByTimeAsync(3000);
+ await first;
+ expect(cdp.hasPendingScreenshot(4)).toBe(true);
+ await expect(cdp.send(4, "Page.captureScreenshot")).rejects.toThrow("still running");
+ expect(
+ vi.mocked(api.sendCommand).mock.calls.filter((c) => c[1] === "Page.captureScreenshot"),
+ ).toHaveLength(1);
+ finish({ data: "late" });
+ await vi.advanceTimersByTimeAsync(0);
+ expect(cdp.hasPendingScreenshot(4)).toBe(false);
+ const next = cdp.send(4, "Page.captureScreenshot");
+ await vi.advanceTimersByTimeAsync(0);
+ finish({ data: "new" });
+ await expect(next).resolves.toEqual({ data: "new" });
+ } finally {
+ cdp.dispose();
+ warning.mockRestore();
+ vi.useRealTimers();
+ }
+ });
+
it("changes attachment identity only across real debugger attachments", async () => {
const { api, onEvent, onDetach } = fakeApi();
const cdp = new ChromiumCdp(api);
@@ -430,6 +501,7 @@ describe("ChromiumCdp", () => {
expect(api.sendCommand).toHaveBeenCalledWith({ tabId: 3 }, "Page.handleJavaScriptDialog", {
accept: true,
});
+ await vi.waitFor(() => expect(cdp.dialogsSince(3, cursor)).toHaveLength(1));
const dialogs = cdp.dialogsSince(3, cursor);
expect(dialogs).toHaveLength(1);
expect(dialogs[0]).toMatchObject({
@@ -479,7 +551,7 @@ describe("ChromiumCdp", () => {
url: "https://example.com/",
});
await Promise.resolve();
- expect(cdp.dialogsSince(11, 0)).toHaveLength(1);
+ await vi.waitFor(() => expect(cdp.dialogsSince(11, 0)).toHaveLength(1));
await cdp.detach(11);
expect(cdp.dialogsSince(11, 0)).toHaveLength(0);
});
diff --git a/apps/extension/src/browser-driver/__tests__/command-deadline.test.ts b/apps/extension/src/browser-driver/__tests__/command-deadline.test.ts
new file mode 100644
index 0000000..7c1dd61
--- /dev/null
+++ b/apps/extension/src/browser-driver/__tests__/command-deadline.test.ts
@@ -0,0 +1,76 @@
+import { afterEach, expect, it, vi } from "vitest";
+import { captureObservationFacts } from "@/tools/vom/capture-coordinator";
+import { CdpReadTimeoutError, runCdpCommand } from "../command-deadline";
+
+afterEach(() => {
+ vi.useRealTimers();
+ vi.restoreAllMocks();
+});
+it("times out stuck snapshots with the actual method and stops fallback reads", async () => {
+ vi.useFakeTimers();
+ vi.spyOn(console, "warn").mockImplementation(() => {});
+ vi.spyOn(console, "debug").mockImplementation(() => {});
+ const send = vi.fn(async (_tab: number, method: string) =>
+ runCdpCommand({ tabId: 4 }, method, () =>
+ method === "DOMSnapshot.captureSnapshot" ? new Promise<never>(() => {}) : Promise.resolve({}),
+ ),
+ );
+ const work = captureObservationFacts({ send: send as never }, 4);
+ const checked = expect(work).rejects.toThrow(
+ "DOMSnapshot.captureSnapshot timed out after 10000ms (tab 4)",
+ );
+ await vi.advanceTimersByTimeAsync(10_000);
+ await checked;
+ expect(send.mock.calls.map((call) => call[1])).toEqual([
+ "DOMSnapshot.enable",
+ "DOMSnapshot.captureSnapshot",
+ ]);
+ expect(vi.getTimerCount()).toBe(0);
+});
+it("bounds screenshots and ignores their late reply", async () => {
+ vi.useFakeTimers();
+ vi.spyOn(console, "warn").mockImplementation(() => {});
+ vi.spyOn(console, "debug").mockImplementation(() => {});
+ let finish!: (value: string) => void;
+ const work = runCdpCommand(
+ { tabId: 4 },
+ "Page.captureScreenshot",
+ () =>
+ new Promise<string>((resolve) => {
+ finish = resolve;
+ }),
+ );
+ const checked = expect(work).rejects.toBeInstanceOf(CdpReadTimeoutError);
+ await vi.advanceTimersByTimeAsync(3_000);
+ await checked;
+ finish("late image");
+ await Promise.resolve();
+ expect(vi.getTimerCount()).toBe(0);
+});
+
+it("distinguishes caller timeout from a late Chrome completion", async () => {
+ vi.useFakeTimers();
+ const warning = vi.spyOn(console, "warn").mockImplementation(() => {});
+ let finish!: (value: string) => void;
+ const work = runCdpCommand(
+ { tabId: 4 },
+ "Page.captureScreenshot",
+ () =>
+ new Promise<string>((resolve) => {
+ finish = resolve;
+ }),
+ );
+ const checked = expect(work).rejects.toBeInstanceOf(CdpReadTimeoutError);
+ await vi.advanceTimersByTimeAsync(3000);
+ await checked;
+ expect(warning.mock.calls.filter((c) => c[0] === "[bsk cdp] slow command settled")).toHaveLength(
+ 0,
+ );
+ await vi.advanceTimersByTimeAsync(5000);
+ finish("image");
+ await vi.advanceTimersByTimeAsync(0);
+ expect(warning).toHaveBeenCalledWith(
+ "[bsk cdp] slow command settled",
+ expect.objectContaining({ elapsedMs: 8000, outcome: "returned", late: true }),
+ );
+});
diff --git a/apps/extension/src/browser-driver/__tests__/task-focus.browser.test.ts b/apps/extension/src/browser-driver/__tests__/task-focus.browser.test.ts
new file mode 100644
index 0000000..fe79f2a
--- /dev/null
+++ b/apps/extension/src/browser-driver/__tests__/task-focus.browser.test.ts
@@ -0,0 +1,96 @@
+// @vitest-environment node
+import { describe, expect, it } from "vitest";
+import { captureVomObservation } from "@/tools/observation";
+import { type CdpDebuggerApi, ChromiumCdp } from "../chromium-cdp";
+
+describe.skipIf(!process.env.BSK_GEOMETRY_CHROME)("real background task rendering", () => {
+ it.each([
+ 0, 2000,
+ ])("renders a background page with %i repeated controls and restores visibility on detach", async (controlCount) => {
+ // Raw CDP deliberately avoids Playwright, which enables focus emulation by default
+ // and would mask this exact regression even when a Chrome tab is inactive.
+ const { withChrome } = await import(
+ new URL(
+ "../../../../../evals/browser/cases/regression/snapshot-coordinates/chrome.mjs",
+ import.meta.url,
+ ).href
+ );
+ await withChrome(
+ { executable: process.env.BSK_GEOMETRY_CHROME, deviceScale: 1, zoom: 1 },
+ async (send: any) => {
+ const foreground = (await send("Target.getTargets")).targetInfos.find(
+ (t: any) => t.type === "page",
+ );
+ const { sessionId: foregroundSession } = await send("Target.attachToTarget", {
+ targetId: foreground.targetId,
+ flatten: true,
+ });
+ const url =
+ "data:text/html," +
+ encodeURIComponent(
+ '<title>Background task</title><p>Loading</p><script>requestAnimationFrame(()=>document.querySelector("p").textContent="Rendered")</script>' +
+ "<section><h2>Entry</h2><button>Open</button><p>Sample page content</p></section>".repeat(
+ controlCount,
+ ),
+ );
+ const { targetId } = await send("Target.createTarget", { url, background: true });
+ let sessionId = (await send("Target.attachToTarget", { targetId, flatten: true }))
+ .sessionId;
+ const evaluate = async (expression: string, session = sessionId) =>
+ (
+ await send(
+ "Runtime.evaluate",
+ { expression, awaitPromise: true, returnByValue: true },
+ session,
+ )
+ ).result.value;
+ // A newly created target may still expose the initial complete about:blank.
+ await expect.poll(() => evaluate("document.title")).toBe("Background task");
+ await expect.poll(() => evaluate("document.readyState")).toBe("complete");
+ expect(
+ await evaluate(
+ '({visibility:document.visibilityState,text:document.querySelector("p").textContent})',
+ ),
+ ).toEqual({ visibility: "hidden", text: "Loading" });
+ await send("Target.detachFromTarget", { sessionId });
+ const event = { addListener() {}, removeListener() {} };
+ const api = {
+ attach: async () => {
+ sessionId = (await send("Target.attachToTarget", { targetId, flatten: true }))
+ .sessionId;
+ },
+ detach: async () => {
+ await send("Target.detachFromTarget", { sessionId });
+ },
+ sendCommand: (_target: unknown, method: string, params: object) =>
+ send(method, params, sessionId),
+ onEvent: event,
+ onDetach: event,
+ } as unknown as CdpDebuggerApi;
+ const cdp = new ChromiumCdp(api);
+ try {
+ await cdp.acquireBackgroundExecution("task", 4);
+ expect(
+ await evaluate(
+ 'new Promise(resolve=>requestAnimationFrame(()=>resolve(document.querySelector("p").textContent)))',
+ ),
+ ).toBe("Rendered");
+ const observation = await captureVomObservation(cdp, 4, url);
+ expect(observation.text).toContain("Rendered");
+ expect(observation.refs).toHaveLength(controlCount);
+ const image = await cdp.send<{ data: string }>(4, "Page.captureScreenshot", {
+ format: "jpeg",
+ fromSurface: true,
+ });
+ expect(image.data.length).toBeGreaterThan(100);
+ expect(await evaluate("document.visibilityState", foregroundSession)).toBe("visible");
+ await cdp.releaseSessionTab("task", 4);
+ sessionId = (await send("Target.attachToTarget", { targetId, flatten: true })).sessionId;
+ expect(await evaluate("document.visibilityState")).toBe("hidden");
+ } finally {
+ cdp.dispose();
+ }
+ },
+ );
+ }, 20_000);
+});
diff --git a/apps/extension/src/browser-driver/chromium-cdp.ts b/apps/extension/src/browser-driver/chromium-cdp.ts
index 2779da7..eebb362 100644
--- a/apps/extension/src/browser-driver/chromium-cdp.ts
+++ b/apps/extension/src/browser-driver/chromium-cdp.ts
@@ -32,6 +32,7 @@ import type {
NetworkResult,
} from "@/transport/types";
import { BackgroundExecution } from "./background-execution";
+import { runCdpCommand } from "./command-deadline";
import {
buildFrameGraph,
type CdpFrameGraph,
@@ -141,6 +142,7 @@ interface FrameDiscoveryState {
sessions: Set<string>;
pending: Set<Promise<void>>;
generation: number;
+ enabled: Map<string, Promise<void>>;
}
async function settleBeforeDeadline(promises: Promise<void>[], deadline: number): Promise<boolean> {
@@ -165,13 +167,14 @@ async function settleBeforeDeadline(promises: Promise<void>[], deadline: number)
export class ChromiumCdp {
private readonly api: CdpDebuggerApi;
private readonly attachedTabs = new Set<number>();
+ private readonly pendingScreenshots = new Map<number, Promise<unknown>>();
private readonly attachmentIds = new Map<number, string>();
private readonly attachInFlight = new Map<number, Promise<void>>();
private readonly detachInFlight = new Map<number, Promise<void>>();
private readonly backgroundExecution = new BackgroundExecution(
(tabId) => this.attachmentIds.get(tabId),
(tabId, enabled) =>
- this.api.sendCommand({ tabId }, "Emulation.setFocusEmulationEnabled", { enabled }),
+ this.command({ tabId }, "Emulation.setFocusEmulationEnabled", { enabled }),
);
private readonly tabOwners = new Map<number, Set<string>>();
private readonly dialogBuffers = new Map<number, JavaScriptDialogInfo[]>();
@@ -295,7 +298,7 @@ export class ChromiumCdp {
async send<T = unknown>(tabId: number, method: string, params?: object): Promise<T> {
await this.ensureAttached(tabId);
try {
- const result = await this.api.sendCommand({ tabId }, method, params ?? {});
+ const result = await this.command({ tabId }, method, params ?? {});
return result as T;
} catch (err) {
throw normalizeError(err);
@@ -305,7 +308,7 @@ export class ChromiumCdp {
async sendToTarget<T = unknown>(target: CdpTarget, method: string, params?: object): Promise<T> {
await this.ensureAttached(target.tabId);
try {
- return (await this.api.sendCommand(target, method, params ?? {})) as T;
+ return (await this.command(target, method, params ?? {})) as T;
} catch (err) {
throw normalizeError(err);
}
@@ -563,6 +566,7 @@ export class ChromiumCdp {
this.networkDomainsEnabledTabs.clear();
this.networkRequestMeta.clear();
this.frameDiscovery.clear();
+ this.pendingScreenshots.clear();
await Promise.all(
tabs.map(async (tabId) => {
try {
@@ -574,8 +578,31 @@ export class ChromiumCdp {
);
}
+ hasPendingScreenshot(tabId: number): boolean {
+ return this.pendingScreenshots.has(tabId);
+ }
+
+ private command(target: CdpDebuggee, method: string, params: object): Promise<unknown> {
+ return runCdpCommand(target, method, () => {
+ const tabId = target.tabId;
+ if (method !== "Page.captureScreenshot" || tabId === undefined)
+ return this.api.sendCommand(target, method, params);
+ // A local deadline cannot cancel a Chrome command. Keep this fence until
+ // Chrome actually replies (or detaches), so polling cannot pile up captures.
+ if (this.pendingScreenshots.has(tabId))
+ throw new Error("Previous screenshot is still running in Chrome");
+ const pending = this.api.sendCommand(target, method, params);
+ this.pendingScreenshots.set(tabId, pending);
+ const clear = () => {
+ if (this.pendingScreenshots.get(tabId) === pending) this.pendingScreenshots.delete(tabId);
+ };
+ void pending.then(clear, clear);
+ return pending;
+ });
+ }
+
private async enablePageDomain(tabId: number): Promise<void> {
- await this.api.sendCommand({ tabId }, "Page.enable", {});
+ await this.command({ tabId }, "Page.enable", {});
}
private async enableConsoleDomains(tabId: number): Promise<void> {
@@ -588,7 +615,7 @@ export class ChromiumCdp {
let failed = false;
for (const method of ["Runtime.enable", "Log.enable"]) {
try {
- await this.api.sendCommand({ tabId }, method, {});
+ await this.command({ tabId }, method, {});
} catch (err) {
failed = true;
console.debug("[bsk cdp] console domain enable failed", { tabId, method, err });
@@ -601,7 +628,7 @@ export class ChromiumCdp {
private async enableNetworkDomain(tabId: number): Promise<void> {
if (this.networkDomainsEnabledTabs.has(tabId)) return;
- await this.api.sendCommand({ tabId }, "Network.enable", {});
+ await this.command({ tabId }, "Network.enable", {});
this.networkDomainsEnabledTabs.add(tabId);
}
@@ -613,13 +640,27 @@ export class ChromiumCdp {
}
}
- private async enableFrameDiscovery(target: CdpTarget): Promise<void> {
- await this.api.sendCommand(target, "Target.setAutoAttach", {
+ private enableFrameDiscovery(target: CdpTarget): Promise<void> {
+ const state = this.frameDiscoveryState(target.tabId);
+ const key = target.sessionId ?? "root";
+ const existing = state.enabled.get(key);
+ if (existing) return existing;
+ // Auto-attach remains enabled for this debugger session, including later
+ // navigation and new iframes. Reconfiguring it on every observation adds a
+ // browser round trip and needlessly repeats child-target discovery.
+ const work = this.command(target, "Target.setAutoAttach", {
autoAttach: true,
waitForDebuggerOnStart: false,
flatten: true,
filter: [{ type: "iframe", exclude: false }],
- });
+ })
+ .then(() => {})
+ .catch((error) => {
+ if (state.enabled.get(key) === work) state.enabled.delete(key);
+ throw error;
+ });
+ state.enabled.set(key, work);
+ return work;
}
private bindFrameTargetHandler(): void {
@@ -646,6 +687,7 @@ export class ChromiumCdp {
if (method === "Target.detachedFromTarget") {
const state = this.frameDiscovery.get(tabId);
+ state?.enabled.delete(sessionId);
if (state?.sessions.delete(sessionId)) state.generation += 1;
return;
}
@@ -712,6 +754,7 @@ export class ChromiumCdp {
sessions: new Set(),
pending: new Set(),
generation: 0,
+ enabled: new Map(),
};
this.frameDiscovery.set(tabId, created);
return created;
@@ -719,6 +762,7 @@ export class ChromiumCdp {
private clearFrameState(tabId: number): void {
this.frameDiscovery.delete(tabId);
+ this.pendingScreenshots.delete(tabId);
}
private bindDialogHandler(): void {
@@ -751,7 +795,7 @@ export class ChromiumCdp {
if (parsed.type === "prompt") {
handleParams.promptText = parsed.defaultPrompt ?? "";
}
- await this.api.sendCommand({ tabId }, "Page.handleJavaScriptDialog", handleParams);
+ await this.command({ tabId }, "Page.handleJavaScriptDialog", handleParams);
if (!this.attachedTabs.has(tabId) && !this.attachInFlight.has(tabId)) return;
const sequence = (this.dialogSequences.get(tabId) ?? 0) + 1;
this.dialogSequences.set(tabId, sequence);
diff --git a/apps/extension/src/browser-driver/command-deadline.ts b/apps/extension/src/browser-driver/command-deadline.ts
new file mode 100644
index 0000000..540d804
--- /dev/null
+++ b/apps/extension/src/browser-driver/command-deadline.ts
@@ -0,0 +1,78 @@
+import type { CdpDebuggee } from "./chromium-cdp";
+
+// Bound renderer reads, not user-requested waits or mutations. An expired
+// CDP read cannot be cancelled individually; its late reply is ignored and
+// observation must stop rather than issue fallback reads against the same page.
+const READ_COMMANDS = new Set([
+ "Target.setAutoAttach",
+ "DOMSnapshot.enable",
+ "DOMSnapshot.captureSnapshot",
+ "Accessibility.enable",
+ "Accessibility.getFullAXTree",
+ "Page.getLayoutMetrics",
+ "Page.getFrameTree",
+ "DOM.getFrameOwner",
+ "Page.captureScreenshot",
+ "Emulation.setFocusEmulationEnabled",
+]);
+export class CdpReadTimeoutError extends Error {
+ constructor(method: string, tabId: number | undefined, timeout: number) {
+ super(
+ `Browser read ${method} timed out after ${timeout}ms (tab ${tabId}). Do not repeat the same read in a loop; report the timeout and let the browser task release control before retrying.`,
+ );
+ this.name = "CdpReadTimeoutError";
+ }
+}
+
+export async function runCdpCommand<T>(
+ target: CdpDebuggee,
+ method: string,
+ run: () => Promise<T>,
+): Promise<T> {
+ const started = Date.now();
+ const details = () => ({
+ tabId: target.tabId,
+ frameSessionId: target.sessionId,
+ method,
+ elapsedMs: Date.now() - started,
+ });
+ const slow = setTimeout(() => console.warn("[bsk cdp] slow command", details()), 2_000);
+ let deadline: ReturnType<typeof setTimeout> | undefined;
+ let timedOut = false;
+ const settled = (outcome: "returned" | "failed") => {
+ if (Date.now() - started >= 2_000)
+ console.warn("[bsk cdp] slow command settled", { ...details(), outcome, late: timedOut });
+ };
+ try {
+ const pending = run().then(
+ (value) => {
+ settled("returned");
+ return value;
+ },
+ (error) => {
+ settled("failed");
+ throw error;
+ },
+ );
+ if (!READ_COMMANDS.has(method)) return await pending;
+ const timeout = method === "Page.captureScreenshot" ? 3_000 : 10_000;
+ return await Promise.race([
+ pending,
+ new Promise<never>((_, reject) => {
+ deadline = setTimeout(() => {
+ timedOut = true;
+ reject(new CdpReadTimeoutError(method, target.tabId, timeout));
+ }, timeout);
+ }),
+ ]);
+ } catch (error) {
+ console.warn("[bsk cdp] command failed", {
+ ...details(),
+ error: error instanceof Error ? error.message : String(error),
+ });
+ throw error;
+ } finally {
+ clearTimeout(slow);
+ clearTimeout(deadline);
+ }
+}
diff --git a/apps/extension/src/tools/vom/capture-abort.ts b/apps/extension/src/tools/vom/capture-abort.ts
index 7ca65bf..429c373 100644
--- a/apps/extension/src/tools/vom/capture-abort.ts
+++ b/apps/extension/src/tools/vom/capture-abort.ts
@@ -4,10 +4,12 @@ function captureAbortError(): Error {
return error;
}
+// Timed-out renderer reads are terminal too: AX/geometry fallback must not
+// keep issuing reads after the renderer has stopped answering.
export function isAbortError(error: unknown): boolean {
return (
typeof error === "object" &&
error !== null &&
- (error as { name?: string }).name === "AbortError"
+ ["AbortError", "CdpReadTimeoutError"].includes((error as { name?: string }).name ?? "")
);
}

View File

@ -1,505 +0,0 @@
diff --git a/apps/extension/src/entrypoints/background.ts b/apps/extension/src/entrypoints/background.ts
index 830bbbe..cb88179 100644
--- a/apps/extension/src/entrypoints/background.ts
+++ b/apps/extension/src/entrypoints/background.ts
@@ -4,6 +4,7 @@ import { getAuditEnabled } from "@/lib/audit";
import { attachAuditBridge } from "@/lib/audit-bridge";
import { ConnectionController } from "@/lib/connection-controller";
import { watchDaemonConnection } from "@/lib/daemon-connection-preference";
+import { attachGatewayUI } from "@/lib/gateway-ui";
import { startHeartbeat } from "@/lib/heartbeat";
import {
getConnectionEnabled,
@@ -27,6 +28,7 @@ import {
import { POPUP_PORT_NAME, type PopupInbound, type PopupOutbound } from "@/lib/popup-bridge";
import { recordFrameCoordinator } from "@/lib/recording/frame-coordinator";
import { attachSessionsLiveFlag } from "@/lib/sessions-live-flag";
+import { captureTaskPreview, taskTarget } from "@/lib/task-preview";
import { attachLongScreenshot } from "@/long-screenshot/background";
import { createDisconnectCleanup } from "@/session-manager/disconnect-cleanup";
import { attachSessionEventHandler } from "@/session-manager/event-handler";
@@ -63,7 +65,21 @@ export default defineBackground(() => {
url: __BSK_DAEMON_WS_URL__,
webSocketFactory: (url) => {
if (!connectionPreferenceValid) throw new Error("Connection settings are unavailable");
- return remoteSocket(url, remoteEndpoint);
+ const socket = remoteSocket(url, remoteEndpoint);
+ if (remoteEndpoint)
+ attachGatewayUI(socket, {
+ focus: async (sessionId) => {
+ const tabId = await taskTarget(sessions, sessionId);
+ const task = sessions.get(sessionId);
+ const tab = await chrome.tabs.get(tabId);
+ if (!task || !isAgentControlledTab(task, tabId)) throw new Error("Task unavailable");
+ await chrome.tabs.update(tabId, { active: true });
+ await chrome.windows.update(tab.windowId, { focused: true });
+ return { focused: true };
+ },
+ preview: (sessionId) => captureTaskPreview(sessions, cdp, sessionId),
+ });
+ return socket;
},
});
const sessions = new SessionManager({ remote: () => remoteEndpoint !== null });
diff --git a/apps/extension/src/lib/__tests__/gateway-ui.test.ts b/apps/extension/src/lib/__tests__/gateway-ui.test.ts
new file mode 100644
index 0000000..273c15b
--- /dev/null
+++ b/apps/extension/src/lib/__tests__/gateway-ui.test.ts
@@ -0,0 +1,51 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { attachGatewayUI } from "../gateway-ui";
+
+afterEach(() => vi.unstubAllGlobals());
+function fixture() {
+ vi.stubGlobal("WebSocket", { OPEN: 1 });
+ const socket = Object.assign(new EventTarget(), { readyState: 1, send: vi.fn() });
+ const ui = {
+ focus: vi.fn(async () => ({ focused: true })),
+ preview: vi.fn(async () => ({ image_base64: "jpeg" })),
+ };
+ attachGatewayUI(socket as unknown as WebSocket, ui);
+ const native = vi.fn();
+ socket.addEventListener("message", native);
+ const request = async (body: unknown) => {
+ socket.dispatchEvent(new MessageEvent("message", { data: JSON.stringify(body) }));
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ };
+ return { socket, ui, native, request };
+}
+describe("gateway UI side channel", () => {
+ it("routes UI requests independently and leaves native RPC untouched", async () => {
+ const f = fixture();
+ for (const name of ["preview", "focus"] as const) {
+ await f.request({ id: name, method: `gateway.task_${name}`, params: { session_id: "task" } });
+ expect(f.ui[name]).toHaveBeenCalledWith("task");
+ }
+ expect(f.native).not.toHaveBeenCalled();
+ await f.request({ id: "native", method: "tool.snapshot", params: {} });
+ expect(f.native).toHaveBeenCalledOnce();
+ });
+ it("rejects invalid session IDs without invoking browser work", async () => {
+ const f = fixture();
+ await f.request({ id: "bad", method: "gateway.task_focus", params: { session_id: 42 } });
+ expect(f.ui.focus).not.toHaveBeenCalled();
+ expect(JSON.parse(f.socket.send.mock.calls[0]![0])).toMatchObject({
+ error: { code: "invalid_params" },
+ });
+ });
+ it("reports failures and drops late replies on closed sockets", async () => {
+ const f = fixture();
+ f.ui.focus.mockRejectedValueOnce(new Error("unavailable"));
+ await f.request({ id: "focus", method: "gateway.task_focus", params: { session_id: "task" } });
+ expect(JSON.parse(f.socket.send.mock.calls[0]![0])).toMatchObject({
+ error: { code: "task_unavailable" },
+ });
+ f.socket.readyState = 3;
+ await f.request({ id: "late", method: "gateway.task_preview", params: { session_id: "task" } });
+ expect(f.socket.send).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/apps/extension/src/lib/__tests__/task-preview.test.ts b/apps/extension/src/lib/__tests__/task-preview.test.ts
new file mode 100644
index 0000000..3939bae
--- /dev/null
+++ b/apps/extension/src/lib/__tests__/task-preview.test.ts
@@ -0,0 +1,180 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import type { ChromiumCdp } from "@/browser-driver/chromium-cdp";
+import { runCdpCommand } from "@/browser-driver/command-deadline";
+import type { SessionManager } from "@/session-manager/manager";
+import { captureTaskPreview, withTaskPreviewStop } from "../task-preview";
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+ vi.useRealTimers();
+ vi.restoreAllMocks();
+});
+function fixture() {
+ const task = {
+ remote: true,
+ agentWindowId: 10,
+ refStore: { documentRevision: () => 1 },
+ agentCreatedTabs: new Set([5]),
+ borrowedTabs: new Map(),
+ };
+ const manager = { get: () => task } as unknown as SessionManager;
+ const cdp = {
+ getAttachmentId: vi.fn(() => "attachment"),
+ hasPendingScreenshot: vi.fn(() => false),
+ acquireBackgroundExecution: vi.fn(async () => {}),
+ send: vi.fn(async (_tab, method) =>
+ method === "Page.getLayoutMetrics"
+ ? { cssVisualViewport: { pageX: 0, pageY: 0, clientWidth: 1280, clientHeight: 720 } }
+ : { data: btoa("jpeg") },
+ ),
+ } as unknown as ChromiumCdp;
+ const draw = vi.fn();
+ const close = vi.fn();
+ const sizes: number[] = [];
+ vi.stubGlobal("chrome", {
+ tabs: {
+ query: vi.fn(async () => [{ id: 5, windowId: 10, active: true }]),
+ get: vi.fn(async () => ({ id: 5, windowId: 10, title: "task" })),
+ sendMessage: vi.fn(async () => ({ ok: true })),
+ },
+ });
+ vi.stubGlobal(
+ "createImageBitmap",
+ vi.fn(async () => ({ width: 1280, height: 720, close })),
+ );
+ vi.stubGlobal(
+ "OffscreenCanvas",
+ class {
+ width: number;
+ height: number;
+ constructor(w: number, h: number) {
+ this.width = w;
+ this.height = h;
+ sizes.push(w, h);
+ }
+ getContext() {
+ return { drawImage: draw };
+ }
+ async convertToBlob() {
+ return new Blob(["thumbnail"]);
+ }
+ },
+ );
+ return { task, manager, cdp, sizes, close };
+}
+describe("independent task preview", () => {
+ it("keeps control and help overlays visible across repeated preview captures", async () => {
+ const f = fixture();
+ for (let i = 0; i < 3; i++) {
+ await expect(captureTaskPreview(f.manager, f.cdp, "visible")).resolves.toMatchObject({
+ tab_id: 5,
+ image_base64: btoa("thumbnail"),
+ });
+ }
+ expect(f.cdp.acquireBackgroundExecution).toHaveBeenCalledWith("visible", 5);
+ expect(f.cdp.send).toHaveBeenCalledTimes(3);
+ expect(chrome.tabs.sendMessage).not.toHaveBeenCalled();
+ });
+
+ it("lets native stop proceed after a stuck screenshot times out", async () => {
+ vi.useFakeTimers();
+ vi.spyOn(console, "warn").mockImplementation(() => {});
+ vi.spyOn(console, "debug").mockImplementation(() => {});
+ const f = fixture();
+ const original = vi.mocked(f.cdp.send).getMockImplementation()!;
+ vi.mocked(f.cdp.send).mockImplementation((tab, method, params) =>
+ method === "Page.captureScreenshot"
+ ? runCdpCommand({ tabId: tab }, method, () => new Promise<never>(() => {}))
+ : original(tab, method, params),
+ );
+ const stop = vi.fn(async () => {});
+ const frame = captureTaskPreview(f.manager, f.cdp, "stuck");
+ const checked = expect(frame).rejects.toThrow("Page.captureScreenshot timed out");
+ const released = withTaskPreviewStop(f.manager, "stuck", stop);
+ await expect(captureTaskPreview(f.manager, f.cdp, "stuck")).rejects.toThrow("stopping");
+ expect(stop).not.toHaveBeenCalled();
+ await vi.advanceTimersByTimeAsync(3_000);
+ await checked;
+ await released;
+ expect(stop).toHaveBeenCalledOnce();
+ expect(vi.getTimerCount()).toBe(0);
+ vi.mocked(f.cdp.send).mockImplementation(original);
+ await expect(
+ captureTaskPreview(f.manager, f.cdp, "stuck"),
+ ).resolves.toMatchObject({ tab_id: 5 });
+ });
+
+ it("blocks previews throughout native cleanup and restores them after a failed stop", async () => {
+ const f = fixture();
+ let reject!: (error: Error) => void;
+ const pending = new Promise<never>((_, fail) => { reject = fail; });
+ const stopping = withTaskPreviewStop(f.manager, "retry", () => pending);
+ const checked = expect(stopping).rejects.toThrow("return failed");
+ await expect(captureTaskPreview(f.manager, f.cdp, "retry")).rejects.toThrow("stopping");
+ expect(f.cdp.acquireBackgroundExecution).not.toHaveBeenCalled();
+ reject(new Error("return failed"));
+ await checked;
+ await expect(captureTaskPreview(f.manager, f.cdp, "retry")).resolves.toMatchObject({ tab_id: 5 });
+ });
+
+ it("coalesces captures and bounds the physical bitmap on HiDPI screens", async () => {
+ const f = fixture();
+ const first = captureTaskPreview(f.manager, f.cdp, "one");
+ expect(captureTaskPreview(f.manager, f.cdp, "one")).toBe(first);
+ const frame = await first;
+ expect(frame).toMatchObject({ tab_id: 5, format: "jpeg", image_base64: btoa("thumbnail") });
+ expect(f.sizes).toEqual([640, 360]);
+ expect(f.close).toHaveBeenCalled();
+ expect(f.cdp.send).toHaveBeenCalledExactlyOnceWith(5, "Page.captureScreenshot", {
+ format: "jpeg",
+ quality: 50,
+ fromSurface: true,
+ captureBeyondViewport: false,
+ });
+ });
+ it("skips polling before any browser work when Chrome still has a screenshot pending", async () => {
+ const f = fixture();
+ vi.mocked(f.cdp.hasPendingScreenshot).mockReturnValue(true);
+ await expect(captureTaskPreview(f.manager, f.cdp, "pending")).rejects.toThrow("still running");
+ expect(f.cdp.acquireBackgroundExecution).not.toHaveBeenCalled();
+ expect(f.cdp.send).not.toHaveBeenCalled();
+ });
+ it("does not capture an unowned active user tab", async () => {
+ const f = fixture();
+ f.task.agentCreatedTabs.clear();
+ await expect(captureTaskPreview(f.manager, f.cdp, "empty")).rejects.toThrow(
+ "Task tab unavailable",
+ );
+ expect(f.cdp.send).not.toHaveBeenCalled();
+ });
+ it("discards the result if authorization ends during capture", async () => {
+ const f = fixture();
+ vi.mocked(f.cdp.acquireBackgroundExecution).mockImplementation(async () => {
+ f.task.agentCreatedTabs.clear();
+ });
+ await expect(captureTaskPreview(f.manager, f.cdp, "ended")).rejects.toThrow(
+ "Task ended during capture",
+ );
+ });
+ it("uses an owned tab when an unauthorized user tab is active", async () => {
+ const f = fixture();
+ vi.mocked(chrome.tabs.query).mockResolvedValue([
+ { id: 9, windowId: 10, active: true },
+ { id: 5, windowId: 10, active: false },
+ ] as chrome.tabs.Tab[]);
+ await expect(captureTaskPreview(f.manager, f.cdp, "user-active")).resolves.toMatchObject({
+ tab_id: 5,
+ });
+ expect(f.cdp.send).toHaveBeenCalledWith(5, "Page.captureScreenshot", expect.anything());
+ });
+ it("discards a frame after the debugger attachment changes", async () => {
+ const f = fixture();
+ vi.mocked(f.cdp.send).mockImplementation(async () => {
+ vi.mocked(f.cdp.getAttachmentId).mockReturnValue("replacement");
+ return { data: btoa("jpeg") } as never;
+ });
+ await expect(captureTaskPreview(f.manager, f.cdp, "replaced")).rejects.toThrow(
+ "Task ended during capture",
+ );
+ });
+});
diff --git a/apps/extension/src/lib/gateway-ui.ts b/apps/extension/src/lib/gateway-ui.ts
new file mode 100644
index 0000000..ef0443f
--- /dev/null
+++ b/apps/extension/src/lib/gateway-ui.ts
@@ -0,0 +1,40 @@
+interface GatewayUI {
+ focus(sessionId: string): Promise<unknown>;
+ preview(sessionId: string): Promise<unknown>;
+}
+
+/** WeKnora's optional UI protocol, attached only to authenticated remote sockets.
+ * Register before WSTransport so these requests bypass native automation. */
+export function attachGatewayUI(socket: WebSocket, ui: GatewayUI): void {
+ socket.addEventListener("message", (event) => {
+ let request: { id?: unknown; method?: unknown; params?: { session_id?: unknown } };
+ try {
+ request = JSON.parse(event.data);
+ if (!request || typeof request !== "object") return;
+ } catch {
+ return;
+ }
+ const methods: Record<string, (sessionId: string) => Promise<unknown>> = {
+ "gateway.task_focus": (id) => ui.focus(id),
+ "gateway.task_preview": (id) => ui.preview(id),
+ };
+ if (typeof request.method !== "string" || !Object.hasOwn(methods, request.method)) return;
+ event.stopImmediatePropagation();
+ if (typeof request.id !== "string" || !request.id) return;
+ const id = request.id;
+ const sessionId = request.params?.session_id;
+ const send = (body: object) => {
+ if (socket.readyState === WebSocket.OPEN) socket.send(JSON.stringify({ id, ...body }));
+ };
+ if (typeof sessionId !== "string" || !sessionId) {
+ send({ error: { code: "invalid_params", message: "session_id is required" } });
+ return;
+ }
+ void Promise.resolve()
+ .then(() => methods[request.method as string]!(sessionId))
+ .then(
+ (result) => send({ result }),
+ () => send({ error: { code: "task_unavailable", message: "Browser task unavailable" } }),
+ );
+ });
+}
diff --git a/apps/extension/src/lib/task-preview.ts b/apps/extension/src/lib/task-preview.ts
new file mode 100644
index 0000000..a6d2532
--- /dev/null
+++ b/apps/extension/src/lib/task-preview.ts
@@ -0,0 +1,143 @@
+import type { ChromiumCdp } from "@/browser-driver/chromium-cdp";
+import { isAgentControlledTab, type SessionManager } from "@/session-manager/manager";
+
+interface PreviewState {
+ pending?: Promise<unknown>;
+ stopping: number;
+}
+const previews = new WeakMap<object, PreviewState>();
+function previewState(task: object): PreviewState {
+ let state = previews.get(task);
+ if (!state) {
+ state = { stopping: 0 };
+ previews.set(task, state);
+ }
+ return state;
+}
+
+/** UI-only capture outside the automation queue, including human-help waits. */
+export function captureTaskPreview(
+ manager: SessionManager,
+ cdp: ChromiumCdp,
+ sessionId: string,
+): Promise<unknown> {
+ const task = manager.get(sessionId);
+ if (!task?.remote) return Promise.reject(new Error("Task unavailable"));
+ const state = previewState(task);
+ if (state.stopping) return Promise.reject(new Error("Task is stopping"));
+ if (state.pending) return state.pending;
+ const work = capture(manager, cdp, sessionId).finally(() => {
+ if (state.pending === work) state.pending = undefined;
+ });
+ state.pending = work;
+ return work;
+}
+
+/** Drain side-channel screenshots before the official stop handler detaches
+ * and closes tabs. No turn-end detach or custom session state is introduced. */
+export async function withTaskPreviewStop<T>(
+ manager: SessionManager,
+ sessionId: string | undefined,
+ stop: () => Promise<T>,
+): Promise<T> {
+ const task = sessionId ? manager.get(sessionId) : undefined;
+ if (!task?.remote) return stop();
+ const state = previewState(task);
+ state.stopping++;
+ try {
+ await state.pending?.catch(() => {});
+ return await stop();
+ } finally {
+ // A failed native stop can be retried; it must not permanently disable previews.
+ state.stopping--;
+ }
+}
+
+async function capture(manager: SessionManager, cdp: ChromiumCdp, sessionId: string) {
+ const task = manager.get(sessionId);
+ if (!task?.remote) throw new Error("Task unavailable");
+ const tabId = await taskTarget(manager, sessionId);
+ if (tabId === undefined || !isAgentControlledTab(task, tabId))
+ throw new Error("Task tab unavailable");
+ if (cdp.hasPendingScreenshot(tabId))
+ throw new Error("Previous screenshot is still running in Chrome");
+ const tab = await chrome.tabs.get(tabId);
+ // Preview bypasses the automation dispatcher, so acquire the same explicit
+ // control policy before reading a background page. Native session stop drains
+ // this capture before releasing the claim and closing tabs.
+ await cdp.acquireBackgroundExecution(sessionId, tabId);
+ if (manager.get(sessionId) !== task || !isAgentControlledTab(task, tabId))
+ throw new Error("Task ended during capture");
+ const attachment = cdp.getAttachmentId(tabId);
+ const revision = task.refStore.documentRevision(tabId);
+ // UI previews retain overlays: hiding them on every poll makes the user's
+ // control border and help prompts flicker. Agent screenshots hide them separately.
+ // Capture only the viewport and resize the bitmap below without a layout read.
+ const shot = await cdp.send<{ data: string }>(tabId, "Page.captureScreenshot", {
+ format: "jpeg",
+ quality: 50,
+ fromSurface: true,
+ captureBeyondViewport: false,
+ });
+ if (
+ manager.get(sessionId) !== task ||
+ !isAgentControlledTab(task, tabId) ||
+ cdp.getAttachmentId(tabId) !== attachment ||
+ task.refStore.documentRevision(tabId) !== revision
+ )
+ throw new Error("Task ended during capture");
+ // Viewport screenshots may use physical display pixels; HiDPI displays can
+ // return larger bitmaps. Bound the actual encoded image to the preview width.
+ let data = shot.data;
+ const bitmap = await createImageBitmap(
+ new Blob([Uint8Array.from(atob(data), (c) => c.charCodeAt(0))], { type: "image/jpeg" }),
+ );
+ try {
+ if (bitmap.width > 640) {
+ const canvas = new OffscreenCanvas(
+ 640,
+ Math.max(1, Math.round((bitmap.height * 640) / bitmap.width)),
+ );
+ const context = canvas.getContext("2d");
+ if (!context) throw new Error("Preview canvas unavailable");
+ context.drawImage(bitmap, 0, 0, canvas.width, canvas.height);
+ const jpeg = await canvas.convertToBlob({ type: "image/jpeg", quality: 0.5 });
+ data = btoa(
+ Array.from(new Uint8Array(await jpeg.arrayBuffer()), (b) => String.fromCharCode(b)).join(
+ "",
+ ),
+ );
+ }
+ } finally {
+ bitmap.close();
+ }
+ if (
+ manager.get(sessionId) !== task ||
+ !isAgentControlledTab(task, tabId) ||
+ cdp.getAttachmentId(tabId) !== attachment ||
+ task.refStore.documentRevision(tabId) !== revision
+ )
+ throw new Error("Task ended during capture");
+ return {
+ image_base64: data,
+ format: "jpeg",
+ tab_id: tabId,
+ title: tab.title ?? "",
+ captured_at: new Date().toISOString(),
+ };
+}
+
+/** Use the active owned tab, or another owned tab when the user has selected
+ * an unauthorized page. Window membership alone never grants access. */
+export async function taskTarget(manager: SessionManager, sessionId: string): Promise<number> {
+ const task = manager.get(sessionId);
+ if (!task?.remote) throw new Error("Task unavailable");
+ const tabs = await chrome.tabs.query({ windowId: task.agentWindowId });
+ if (manager.get(sessionId) !== task) throw new Error("Task unavailable");
+ const owned = tabs.filter(
+ (tab) => typeof tab.id === "number" && isAgentControlledTab(task, tab.id),
+ );
+ const tabId = (owned.find((tab) => tab.active) ?? owned[0])?.id;
+ if (tabId === undefined) throw new Error("Task tab unavailable");
+ return tabId;
+}
diff --git a/apps/extension/src/tools/session.ts b/apps/extension/src/tools/session.ts
index 4e0220b..ce13827 100644
--- a/apps/extension/src/tools/session.ts
+++ b/apps/extension/src/tools/session.ts
@@ -1,3 +1,4 @@
+import { withTaskPreviewStop } from "@/lib/task-preview";
import {
DEFAULT_INTERACTION_PREFERENCES,
type InteractionPreferenceStore,
@@ -196,6 +197,14 @@ export async function handleSessionStop(
manager: SessionManager,
params: SessionStopParams,
deps: SessionStopDeps = {},
+): Promise<SessionStopResult | RpcError> {
+ return withTaskPreviewStop(manager, params?.session_id, () => stopSession(manager, params, deps));
+}
+
+async function stopSession(
+ manager: SessionManager,
+ params: SessionStopParams,
+ deps: SessionStopDeps,
): Promise<SessionStopResult | RpcError> {
if (!params?.session_id) {
return {

View File

@ -1,337 +0,0 @@
diff --git a/apps/extension/src/entrypoints/background.ts b/apps/extension/src/entrypoints/background.ts
index cb88179..cba98b8 100644
--- a/apps/extension/src/entrypoints/background.ts
+++ b/apps/extension/src/entrypoints/background.ts
@@ -326,10 +326,12 @@ export default defineBackground(() => {
},
...(ctx.signal !== undefined ? { signal: ctx.signal } : {}),
deps: {
- // Skip every Agent Window when choosing where to render the
- // overlay — Agent Windows boot on about:blank, which has no
- // content script, so they cannot surface an authorization decision.
- isAgentWindowId: (windowId) => sessions.findByWindowId(windowId) !== null,
+ // The requesting window may host consent for an in-place borrow.
+ // Candidate selection still excludes about:blank/restricted pages.
+ isAgentWindowId: (windowId) => {
+ const owner = sessions.findByWindowId(windowId);
+ return owner !== null && owner.sessionId !== ctx.sessionId;
+ },
// Resolve i18n strings per-borrow so language switches take effect
// without re-creating the dispatcher.
notificationCopy: makeBorrowNotificationCopy(),
diff --git a/apps/extension/src/session-manager/__tests__/task-popups.test.ts b/apps/extension/src/session-manager/__tests__/task-popups.test.ts
new file mode 100644
index 0000000..f859bdc
--- /dev/null
+++ b/apps/extension/src/session-manager/__tests__/task-popups.test.ts
@@ -0,0 +1,79 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { SessionManager } from "../manager";
+import { withTaskPopups } from "../task-popups";
+
+afterEach(() => vi.unstubAllGlobals());
+async function fixture() {
+ let next = 10;
+ const manager = new SessionManager({
+ remote: () => true,
+ agentWindow: {
+ create: async () => next++,
+ remove: vi.fn(async () => {}),
+ ensureActiveTab: async (id) => id,
+ },
+ });
+ const task = await manager.start("one");
+ const other = await manager.start("two");
+ const tabs = new Map<number, { id: number; windowId: number; active?: boolean }>([
+ [10, { id: 10, windowId: 10, active: true }],
+ [11, { id: 11, windowId: 11, active: true }],
+ ]);
+ const listeners = new Set<(event: { sourceTabId: number; tabId: number }) => void>();
+ const move = vi.fn(async (id: number, props: { windowId: number }) =>
+ Object.assign(tabs.get(id)!, props),
+ );
+ vi.stubGlobal("chrome", {
+ tabs: { query: async () => [tabs.get(10)], get: async (id: number) => tabs.get(id), move },
+ webNavigation: {
+ onCreatedNavigationTarget: {
+ addListener: (fn: (event: { sourceTabId: number; tabId: number }) => void) =>
+ listeners.add(fn),
+ removeListener: (fn: (event: { sourceTabId: number; tabId: number }) => void) =>
+ listeners.delete(fn),
+ },
+ },
+ });
+ const create = (sourceTabId: number, tabId: number, windowId = 10) => {
+ tabs.set(tabId, { id: tabId, windowId });
+ for (const fn of listeners) fn({ sourceTabId, tabId });
+ };
+ return { manager, task, other, tabs, listeners, create, move };
+}
+describe("action-scoped popup ownership", () => {
+ it("claims noopener/nested targets and moves a popup window into its task", async () => {
+ const f = await fixture();
+ await withTaskPopups(f.manager, { session_id: "one" }, async () => {
+ f.create(10, 20);
+ f.create(20, 21, 200);
+ f.create(11, 22); // Another task's source.
+ f.tabs.set(23, { id: 23, windowId: 10 }); // User-created/moved tab, no navigation event.
+ });
+ expect(f.task.agentCreatedTabs).toEqual(new Set([10, 20, 21]));
+ expect(f.move).toHaveBeenCalledWith(21, { windowId: 10, index: -1 });
+ expect(f.listeners.size).toBe(0);
+ f.create(10, 24); // Outside an agent action.
+ expect(f.task.agentCreatedTabs.has(24)).toBe(false);
+ });
+ it("cannot take a tab from another task or use an unauthorized source", async () => {
+ const f = await fixture();
+ await withTaskPopups(f.manager, { session_id: "one", tab_id: 11 }, async () =>
+ f.create(11, 30),
+ );
+ await withTaskPopups(f.manager, { session_id: "one" }, async () => f.create(10, 11, 11));
+ expect(f.task.agentCreatedTabs).toEqual(new Set([10]));
+ expect(f.other.agentCreatedTabs).toEqual(new Set([11]));
+ });
+ it("cleans listeners on failure and rejects targets after source authorization ends", async () => {
+ const f = await fixture();
+ await expect(
+ withTaskPopups(f.manager, { session_id: "one" }, async () => {
+ f.task.agentCreatedTabs.delete(10);
+ f.create(10, 20);
+ throw new Error("input failed");
+ }),
+ ).rejects.toThrow("input failed");
+ expect(f.task.agentCreatedTabs.size).toBe(0);
+ expect(f.listeners.size).toBe(0);
+ });
+});
diff --git a/apps/extension/src/session-manager/task-popups.ts b/apps/extension/src/session-manager/task-popups.ts
new file mode 100644
index 0000000..53db1bf
--- /dev/null
+++ b/apps/extension/src/session-manager/task-popups.ts
@@ -0,0 +1,72 @@
+import { isAgentControlledTab, type SessionManager } from "./manager";
+
+/** Attribute only Chrome-reported navigation targets created during an agent
+ * action on an owned source. Window membership and openerTabId are insufficient.
+ * Delayed/unattributed popups can still use the ordinary consent-based borrow. */
+export async function withTaskPopups<T>(
+ manager: SessionManager,
+ params: { session_id?: string; tab_id?: number },
+ run: () => Promise<T>,
+ onClaimed?: (tabId: number, windowId: number) => void,
+): Promise<T> {
+ const task = params.session_id ? manager.get(params.session_id) : null;
+ const events = globalThis.chrome?.webNavigation?.onCreatedNavigationTarget;
+ if (!task?.remote || !events) return run();
+ const source =
+ params.tab_id ??
+ (
+ await chrome.tabs.query({
+ windowId: task.agentWindowId,
+ active: true,
+ })
+ )[0]?.id;
+ if (source === undefined || !isAgentControlledTab(task, source)) return run();
+ const sources = new Set([source]);
+ const pending = new Set<Promise<void>>();
+ const live = () => manager.get(task.sessionId) === task && !manager.isWindowCloseExpected(task);
+ const created = ({ sourceTabId, tabId }: { sourceTabId: number; tabId: number }) => {
+ if (
+ !live() ||
+ !sources.has(sourceTabId) ||
+ !isAgentControlledTab(task, sourceTabId) ||
+ manager.list().some((other) => isAgentControlledTab(other, tabId))
+ )
+ return;
+ // Record ownership synchronously so a nested popup and cleanup see it.
+ task.agentCreatedTabs.add(tabId);
+ sources.add(tabId);
+ const work = (async () => {
+ try {
+ let tab = await chrome.tabs.get(tabId);
+ if (!live() || !isAgentControlledTab(task, tabId)) return;
+ const other = manager.findByWindowId(tab.windowId);
+ if (other && other !== task) {
+ task.agentCreatedTabs.delete(tabId);
+ sources.delete(tabId);
+ return;
+ }
+ if (tab.windowId !== task.agentWindowId) {
+ await chrome.tabs.move(tabId, { windowId: task.agentWindowId, index: -1 });
+ tab = await chrome.tabs.get(tabId);
+ }
+ if (!live() || !isAgentControlledTab(task, tabId)) return;
+ onClaimed?.(tabId, tab.windowId);
+ } catch (error) {
+ // Preserve a live tab's ownership if moving it failed, so stop can clean
+ // it up; Chrome's removal event forgets tabs that have already closed.
+ console.warn("[bsk] task popup setup failed", error);
+ }
+ })();
+ pending.add(work);
+ void work.then(() => pending.delete(work));
+ };
+ events.addListener(created);
+ try {
+ return await run();
+ } finally {
+ // Chrome can deliver this event just after the CDP input acknowledgement.
+ await new Promise((resolve) => setTimeout(resolve, 100));
+ events.removeListener(created);
+ await Promise.all(pending);
+ }
+}
diff --git a/apps/extension/src/tools/__tests__/tabs.test.ts b/apps/extension/src/tools/__tests__/tabs.test.ts
index 5a7239e..b48a15e 100644
--- a/apps/extension/src/tools/__tests__/tabs.test.ts
+++ b/apps/extension/src/tools/__tests__/tabs.test.ts
@@ -298,7 +298,7 @@ describe("handleTabCreate", () => {
});
describe("handleTabClose", () => {
- it("does not infer remote popup ownership from its opener and explains how to borrow it", async () => {
+ it("does not infer ownership from opener metadata but permits an approved in-place borrow", async () => {
const sm = new SessionManager({ agentWindow: fakeAgentWindow([100]), remote: () => true });
const ctx = await sm.start("aa11");
const state: FakeTabState = {
@@ -310,6 +310,15 @@ describe("handleTabClose", () => {
expect(
await handleTabClose(sm, { session_id: "aa11", tab_id: 5 }, { tabs: api }),
).toMatchObject({ code: "permission_denied" });
+ expect(
+ await handleTabBorrow(
+ sm,
+ { session_id: "aa11", tab_id: 5 },
+ { tabs: api, approveBorrow: async () => false },
+ ),
+ ).toMatchObject({ code: "cancelled" });
+ expect(ctx.borrowedTabs.has(5)).toBe(false);
+ expect(ctx.agentCreatedTabs.has(5)).toBe(false);
const approval = vi.fn(async () => true);
expect(
await handleTabBorrow(
@@ -317,13 +326,32 @@ describe("handleTabClose", () => {
{ session_id: "aa11", tab_id: 5 },
{ tabs: api, approveBorrow: approval },
),
- ).toMatchObject({
- code: "invalid_params",
- message: expect.stringContaining("regular browser window"),
- });
+ ).toMatchObject({ tab_id: 5, original_window_id: 100, agent_window_id: 100 });
expect(ctx.agentCreatedTabs.has(5)).toBe(false);
+ expect(ctx.borrowedTabs.has(5)).toBe(true);
expect(spies.remove).not.toHaveBeenCalled();
- expect(approval).not.toHaveBeenCalled();
+ expect(spies.move).not.toHaveBeenCalled();
+ expect(approval).toHaveBeenCalledOnce();
+ const windows = makeWindowsApi(state).api;
+ const release = vi.fn(async () => {});
+ expect(
+ await handleTabReturn(
+ sm,
+ { session_id: "aa11", tab_id: 5 },
+ {
+ tabs: api,
+ windows,
+ cdp: { releaseSessionTab: release },
+ agentOverlayReset: { resetAgentOverlays: vi.fn(async () => {}) },
+ },
+ ),
+ ).toMatchObject({ tab_id: 5, returned_to_window_id: 100 });
+ expect(release).toHaveBeenCalledWith("aa11", 5);
+ expect(ctx.borrowedTabs.has(5)).toBe(false);
+ expect(state.tabs.has(5)).toBe(true);
+ expect(
+ await handleTabSelect(sm, { session_id: "aa11", tab_id: 5 }, { tabs: api }),
+ ).toMatchObject({ code: "permission_denied" });
});
it("removes a tab inside the Agent Window", async () => {
const sm = new SessionManager({ agentWindow: fakeAgentWindow([100]) });
diff --git a/apps/extension/src/tools/dispatcher.ts b/apps/extension/src/tools/dispatcher.ts
index b713f2f..93908bc 100644
--- a/apps/extension/src/tools/dispatcher.ts
+++ b/apps/extension/src/tools/dispatcher.ts
@@ -2,6 +2,7 @@ import type { InteractionPreferenceStore } from "@/lib/interaction-preferences";
import { OVERLAY_AUTOMATION_BYPASS } from "@/lib/overlay-bridge";
import { ScreenshotExports } from "@/long-screenshot/exports";
import type { SessionManager } from "@/session-manager/manager";
+import { withTaskPopups } from "@/session-manager/task-popups";
import type { Transport } from "@/transport/transport";
import type {
BlurParams,
@@ -276,7 +277,24 @@ export class ToolDispatcher {
} catch {
/* The daemon still has the original operation metadata. */
}
- const result = await this.invoke(req, ac.signal);
+ const createsPopup = [
+ "tool.click",
+ "tool.press_key",
+ "tool.evaluate",
+ "tool.navigate",
+ "tool.navigate_back",
+ "tool.navigate_forward",
+ "tool.fill",
+ "tool.select",
+ ].includes(req.method);
+ const result = createsPopup
+ ? await withTaskPopups(
+ this.sessions,
+ (req.params ?? {}) as { session_id?: string; tab_id?: number },
+ () => this.invoke(req, ac.signal),
+ this.onAgentTabClaimed,
+ )
+ : await this.invoke(req, ac.signal);
if (isRpcError(result)) {
body = { id: req.id, error: classifyCdpError(result) };
} else {
diff --git a/apps/extension/src/tools/tabs.ts b/apps/extension/src/tools/tabs.ts
index 4130ca4..86b5cfa 100644
--- a/apps/extension/src/tools/tabs.ts
+++ b/apps/extension/src/tools/tabs.ts
@@ -697,13 +697,12 @@ async function validateBorrowTarget(
if (typeof tab.id !== "number" || typeof tab.windowId !== "number") {
return { code: "not_found", message: `tab ${tabId} not found` };
}
- if (tab.windowId === ctx.agentWindowId) {
+ // Remote unowned tabs can request normal approval in place. Merely moving
+ // a tab into this window never authorizes it; confirmation still runs below.
+ if (tab.windowId === ctx.agentWindowId && (!ctx.remote || isAgentControlledTab(ctx, tabId))) {
return {
code: "invalid_params",
- message:
- ctx.remote && !isAgentControlledTab(ctx, tabId)
- ? `tab_borrow: tab ${tabId} is not authorized and already lives in the Agent Window; move it to a regular browser window, then borrow it`
- : `tab_borrow: tab ${tabId} already lives in the Agent Window`,
+ message: `tab_borrow: tab ${tabId} already lives in the Agent Window`,
};
}
for (const s of manager.list()) {
@@ -823,14 +822,17 @@ async function executeBorrowCore(
const originalWindowId = currentTab.windowId;
const originalIndex = typeof currentTab.index === "number" ? currentTab.index : 0;
- const moveErr = await moveTabForBorrow(
- p.tabsApi,
- p.tabId,
- p.ctx.agentWindowId,
- originalWindowId,
- originalIndex,
- p.signal,
- );
+ const moveErr =
+ originalWindowId === p.ctx.agentWindowId
+ ? null
+ : await moveTabForBorrow(
+ p.tabsApi,
+ p.tabId,
+ p.ctx.agentWindowId,
+ originalWindowId,
+ originalIndex,
+ p.signal,
+ );
if (moveErr) return moveErr;
return { originalWindowId, originalIndex };

View File

@ -1,174 +0,0 @@
diff --git a/apps/extension/src/tools/__tests__/tabs.test.ts b/apps/extension/src/tools/__tests__/tabs.test.ts
index ae9534d..71a0d56 100644
--- a/apps/extension/src/tools/__tests__/tabs.test.ts
+++ b/apps/extension/src/tools/__tests__/tabs.test.ts
@@ -297,6 +297,99 @@ describe("handleTabCreate", () => {
});
describe("handleTabClose", () => {
+ async function remoteCloseFixture() {
+ const sm = new SessionManager({ agentWindow: fakeAgentWindow([100]), remote: () => true });
+ const ctx = await sm.start("aa11");
+ ctx.agentCreatedTabs.clear();
+ ctx.agentCreatedTabs.add(5);
+ const state: FakeTabState = {
+ tabs: new Map([[5, { id: 5, windowId: 100 } as chrome.tabs.Tab]]),
+ nextTabId: 50,
+ windowsClosed: new Set(),
+ };
+ const { api, spies } = makeTabMutationApi(state);
+ const query = vi.fn(async (info: chrome.tabs.QueryInfo) =>
+ [...state.tabs.values()].filter((tab) => tab.windowId === info.windowId),
+ );
+ const claimed = vi.fn();
+ const deps = { tabs: api, tabsQuery: { query }, onAgentTabClaimed: claimed };
+ const close = (signal?: AbortSignal) =>
+ handleTabClose(sm, { session_id: "aa11", tab_id: 5 }, { ...deps, signal });
+ return { ctx, state, spies, query, claimed, close };
+ }
+
+ it("keeps the remote window alive when the agent closes its last tab", async () => {
+ const { ctx, state, spies, claimed, close } = await remoteCloseFixture();
+ spies.remove.mockImplementation(async (id: number) => {
+ state.tabs.delete(id);
+ if (state.tabs.size === 0) state.windowsClosed.add(100);
+ });
+ expect(await close()).toEqual({ tab_id: 5 });
+ expect(state.windowsClosed.size).toBe(0);
+ expect([...state.tabs.keys()]).toEqual([50]);
+ expect([...ctx.agentCreatedTabs]).toEqual([50]);
+ expect(spies.create).toHaveBeenCalledWith({ windowId: 100, url: "about:blank", active: false });
+ expect(claimed).toHaveBeenCalledWith(50, 100);
+ });
+
+ it("does not create a placeholder when another, even unowned, tab keeps the window open", async () => {
+ const { ctx, state, spies, close } = await remoteCloseFixture();
+ state.tabs.set(6, { id: 6, windowId: 100 } as chrome.tabs.Tab);
+ expect(await close()).toEqual({ tab_id: 5 });
+ expect(spies.create).not.toHaveBeenCalled();
+ expect([...state.tabs.keys()]).toEqual([6]);
+ expect(ctx.agentCreatedTabs.has(6)).toBe(false);
+ });
+
+ it("does not close the last tab if creating its replacement fails", async () => {
+ const { ctx, state, spies, close } = await remoteCloseFixture();
+ spies.create.mockRejectedValueOnce(new Error("cannot create"));
+ expect(await close()).toMatchObject({ code: "protocol_error", message: "cannot create" });
+ expect(spies.remove).not.toHaveBeenCalled();
+ expect([...state.tabs.keys()]).toEqual([5]);
+ expect([...ctx.agentCreatedTabs]).toEqual([5]);
+ });
+
+ it("honors a human interrupt while querying the remote window", async () => {
+ const { state, spies, query, close } = await remoteCloseFixture();
+ const controller = new AbortController();
+ query.mockImplementationOnce(async () => {
+ controller.abort();
+ return [...state.tabs.values()];
+ });
+ expect(await close(controller.signal)).toMatchObject({ code: "cancelled" });
+ expect(spies.create).not.toHaveBeenCalled();
+ expect(spies.remove).not.toHaveBeenCalled();
+ });
+
+ it("cleans the replacement and preserves the original on interruption during creation", async () => {
+ const { ctx, state, spies, claimed, close } = await remoteCloseFixture();
+ const controller = new AbortController();
+ spies.create.mockImplementationOnce(async () => {
+ const tab = { id: 50, windowId: 100 } as chrome.tabs.Tab;
+ state.tabs.set(50, tab);
+ controller.abort();
+ return tab;
+ });
+ expect(await close(controller.signal)).toMatchObject({ code: "cancelled" });
+ expect([...state.tabs.keys()]).toEqual([5]);
+ expect([...ctx.agentCreatedTabs]).toEqual([5]);
+ expect(claimed).not.toHaveBeenCalled();
+ expect(spies.remove).toHaveBeenCalledExactlyOnceWith(50);
+ });
+
+ it.each([
+ false,
+ true,
+ ])("handles failed close with failed placeholder cleanup=%s", async (cleanupFails) => {
+ const { ctx, state, spies, close } = await remoteCloseFixture();
+ spies.remove.mockRejectedValueOnce(new Error("cannot close"));
+ if (cleanupFails) spies.remove.mockRejectedValueOnce(new Error("cannot clean up"));
+ expect(await close()).toMatchObject({ code: "protocol_error", message: "cannot close" });
+ expect([...state.tabs.keys()]).toEqual(cleanupFails ? [5, 50] : [5]);
+ expect([...ctx.agentCreatedTabs]).toEqual(cleanupFails ? [5, 50] : [5]);
+ });
+
it("does not infer ownership from opener metadata but permits an approved in-place borrow", async () => {
const sm = new SessionManager({ agentWindow: fakeAgentWindow([100]), remote: () => true });
const ctx = await sm.start("aa11");
diff --git a/apps/extension/src/tools/dispatcher.ts b/apps/extension/src/tools/dispatcher.ts
index 8afdedf..3e167b1 100644
--- a/apps/extension/src/tools/dispatcher.ts
+++ b/apps/extension/src/tools/dispatcher.ts
@@ -414,7 +414,11 @@ export class ToolDispatcher {
case "tool.tab_close":
return this.withHoverReleaseForRequest(
req.params as TabCloseParams,
- () => handleTabClose(this.sessions, req.params as TabCloseParams, { signal }),
+ () =>
+ handleTabClose(this.sessions, req.params as TabCloseParams, {
+ signal,
+ onAgentTabClaimed: this.onAgentTabClaimed,
+ }),
signal,
);
case "tool.tab_select":
diff --git a/apps/extension/src/tools/tabs.ts b/apps/extension/src/tools/tabs.ts
index 2570c74..43cfb12 100644
--- a/apps/extension/src/tools/tabs.ts
+++ b/apps/extension/src/tools/tabs.ts
@@ -282,6 +282,8 @@ export async function handleTabList(
export interface TabManagementDeps {
tabs?: TabMutationApi;
+ tabsQuery?: Pick<ChromeTabsApi, "query">;
+ onAgentTabClaimed?: (tabId: number, windowId: number) => void;
windows?: ChromeWindowsApi;
/** Abort hook (M10 will wire the full chain). */
signal?: AbortSignal;
@@ -540,12 +542,39 @@ export async function handleTabClose(
if (aborted(deps.signal, "tab_close")) {
return { code: "cancelled", message: "tab_close aborted" };
}
+ let placeholder: number | undefined;
try {
+ if (ctx.remote) {
+ const tabs = await (deps.tabsQuery ?? chromeTabsApi).query({ windowId: ctx.agentWindowId });
+ if (aborted(deps.signal, "tab_close"))
+ return { code: "cancelled", message: "tab_close aborted" };
+ if (tabs.length === 1 && tabs[0]?.id === params.tab_id) {
+ // Keep the session window alive until session.stop. Otherwise Chrome
+ // reports the agent's last-tab removal as a user-closed window, which
+ // pauses the next turn. This concrete tab is owned and cleaned normally.
+ const replacement = await createTabAndCleanup(ctx, deps, {
+ windowId: ctx.agentWindowId,
+ url: "about:blank",
+ active: false,
+ });
+ if (isRpcError(replacement)) return replacement;
+ placeholder = replacement.id;
+ deps.onAgentTabClaimed?.(placeholder, ctx.agentWindowId);
+ }
+ }
await getTabsApi(deps).remove(params.tab_id);
// Keep the tracking set accurate so session_stop won't try to close a
// tab that's already gone (design §3.1).
ctx.agentCreatedTabs.delete(params.tab_id);
} catch (err) {
+ if (placeholder !== undefined) {
+ try {
+ await getTabsApi(deps).remove(placeholder);
+ ctx.agentCreatedTabs.delete(placeholder);
+ } catch {
+ /* Retain the claim so session.stop can retry cleanup. */
+ }
+ }
return {
code: "protocol_error",
message: err instanceof Error ? err.message : String(err),

View File

@ -1,48 +0,0 @@
diff --git a/crates/bsk-cli/src/daemon/ipc.rs b/crates/bsk-cli/src/daemon/ipc.rs
index 69daf58..4695d48 100644
--- a/crates/bsk-cli/src/daemon/ipc.rs
+++ b/crates/bsk-cli/src/daemon/ipc.rs
@@ -793,7 +793,14 @@ fn tool_dispatch_transport_timeout(method: &Method, params: &Value) -> Result<Du
timeout.map(|timeout| {
if matches!(
method,
- Method::ToolUpload | Method::ToolDownload | Method::ToolRequestHelp
+ Method::ToolUpload
+ | Method::ToolDownload
+ | Method::ToolRequestHelp
+ | Method::ToolNavigate
+ | Method::ToolNavigateBack
+ | Method::ToolNavigateForward
+ | Method::ToolReload
+ | Method::ToolWaitForNavigation
) {
timeout.saturating_add(EXTENSION_RESPONSE_GRACE)
} else {
@@ -1820,12 +1827,27 @@ mod tests {
Method::ToolUpload,
Method::ToolDownload,
Method::ToolRequestHelp,
+ Method::ToolNavigate,
+ Method::ToolNavigateBack,
+ Method::ToolNavigateForward,
+ Method::ToolReload,
+ Method::ToolWaitForNavigation,
] {
let dispatch_timeout = tool_dispatch_transport_timeout(&method, &params).unwrap();
assert_eq!(dispatch_timeout, Duration::from_secs(62));
}
}
+ #[test]
+ fn default_navigation_deadline_allows_extension_timeout_response() {
+ for method in [Method::ToolNavigate, Method::ToolWaitForNavigation] {
+ assert_eq!(
+ tool_dispatch_transport_timeout(&method, &serde_json::json!({})).unwrap(),
+ DEFAULT_TOOL_TIMEOUT + EXTENSION_RESPONSE_GRACE
+ );
+ }
+ }
+
#[test]
fn extension_response_grace_does_not_change_other_tools() {
let params = serde_json::json!({

View File

@ -1,119 +0,0 @@
# BrowserSkill downstream patches
The extension and daemon are based on official `main` commit
`fa953dc6fcd868827b93164e3bea26198e691224` (after `ext-v0.3.0`). Both still
report version `0.3.0`; the exact source baseline is recorded in
`scripts/browserskill-release.json`. The daemon is built with
`cargo build --locked --release -p bsk` and includes our navigation-response
deadline patch, so the official CLI 0.3.0 binary is not an equivalent replacement.
Native builds require Rust/Cargo and a C compiler (plus CMake on Linux).
Docker builds on the target architecture.
The patches include extension fixes and a daemon navigation-response deadline fix.
`scripts/build_browserskill.sh` applies the following patches in lexical order:
| Patch | Purpose | Removal condition |
| --- | --- | --- |
| `01-browser-read-reliability.patch` | Bound CDP reads, prevent overlapping timed-out screenshots, reuse frame discovery configuration | Equivalent upstream behavior passes the background rendering and timeout regressions |
| `02-gateway-task-controls.patch` | WeKnora preview and focus side channel; drain previews before native session stop | Upstream provides equivalent UI APIs and WeKnora migrates to them |
| `04-task-popup-ownership.patch` | Attribute navigation targets to the active agent action; allow consent-based in-place borrowing inside the task window | Upstream supports this popup lifecycle without weakening per-tab authorization |
| `05-last-tab-lifecycle.patch` | Keep a temporary owned blank tab when an agent closes the last remote task tab; normal session stop removes it | Upstream distinguishes agent last-tab cleanup from user window closure |
| `06-navigation-response-deadline.patch` | Allow navigation lifecycle timeout results to arrive before the daemon transport deadline | Upstream adds equivalent navigation response grace |
Background execution and viewport/full-page screenshot support use upstream
PRs #249, #250 and #253. VOM sibling-context caching is upstream PR #271, so
patch 03 was removed. Redirect document tracking and cancelled-navigation
reconciliation also use upstream code (including PR #280); patch 06 now changes
only the daemon deadline. The numbering gaps preserve existing patch identities.
The preview side channel explicitly acquires upstream background execution
because it bypasses the automation dispatcher. Native session stop drains
in-flight previews before the official handler releases control and closes tabs.
Retained tasks keep the official session and debugger; turn completion only
stops preview polling in WeKnora. Do not restore the old
`shouldKeepTaskActive` callback or a second focus-emulation cache.
Remote authentication, credential storage/migration, renewal, connection settings,
and dedicated Agent Windows use upstream code. Patch 04 extends the
upstream borrow/return flow while preserving explicit consent and per-tab isolation.
Do not restore the old `remote-extension-connection.patch`: upstream PR #227
already incorporates its remote connection support with different ownership and
window semantics.
## Companion UI contract
Only authenticated remote sockets handle these extra request frames. They do not
enter the native daemon's automation queue:
```json
{"id":"wk-ui-unique","method":"gateway.task_preview","params":{"session_id":"server-owned-session"}}
```
- `gateway.task_preview`: returns `image_base64`, `format: "jpeg"`, `tab_id`,
`title`, and `captured_at`. The encoded image is at most 640 pixels wide.
Captures are coalesced and restricted to a concrete owned tab. Authorization,
document revision and debugger identity are checked before returning a frame.
UI previews include the extension's control and help overlays so periodic
captures do not hide and restore them in the user's browser. Agent screenshots
still suppress overlays to preserve unobstructed page content.
- `gateway.task_focus`: explicitly activates an existing owned tab and its window;
returns `{ "focused": true }`. It never creates a session.
Errors use the native-shaped `{ "id": "…", "error": { "code": "…", "message": "…" } }`
envelope. These two methods are downstream APIs, not official BrowserSkill RPCs.
The official unmodified extension returns `unknown_method` for these UI requests.
Turn cleanup needs no custom RPC: retained tasks keep their session, and completed
tasks use native session stop. Keep serving the companion ZIP for preview/focus.
## Intentional behavior changes
- Remote tasks use official dedicated Agent Windows. The old background-tab-group
preference is no longer used; no `tabGroups` permission is added.
- Only Chrome navigation-target events from an owned source during an agent action
grant popup ownership (including `rel=noopener`). A bounded 100 ms event tail
follows the input acknowledgement. Window membership/opener metadata alone do
not authorize tabs; delayed or unattributed targets use the borrow flow.
- Unowned tabs already inside the task window can be borrowed with normal browser
approval. They remain borrowed user tabs, are returned without being closed,
and lose access immediately on return. Other task windows remain isolated.
- Closing the last owned tab through `tab_close` keeps a temporary owned blank tab
until session stop, preventing Chrome window removal from being misreported as
a human interruption. Actual user window closure still pauses the task.
- Human help and borrowing follow the upstream focus/confirmation behavior.
- Remote upload/download remain unsupported, as defined upstream.
Even though the version remains 0.3.0, users must install the rebuilt ZIP.
Upgrade users by replacing the existing unpacked extension directory and reloading
it after ending active tasks. Keeping the extension ID allows upstream migration
from legacy Chrome storage into extension-origin IndexedDB. Installing under a new
ID requires pairing again. Old saved tab groups are not removed automatically.
## Validation
Apply patches to a clean pinned checkout, install frozen dependencies and run:
```sh
pnpm --filter @browser-skill/extension exec wxt prepare
pnpm --filter @browser-skill/extension compile
pnpm --filter @browser-skill/extension test
pnpm --filter @browser-skill/vom test
pnpm ext:build:zip
BSK_GEOMETRY_CHROME=/path/to/test-chrome BSK_BACKGROUND_CHROME=/path/to/test-chrome \
pnpm --filter @browser-skill/extension exec vitest run --maxWorkers=1 \
src/browser-driver/__tests__/task-focus.browser.test.ts \
src/tools/__tests__/background-execution.browser.test.ts \
src/tools/__tests__/background-screenshot.browser.test.ts \
src/tools/__tests__/background-full-page.browser.test.ts
cargo test --locked -p bsk daemon::ipc::tests
```
Run WeKnora's `TestRealExtension` with the built extension, pinned daemon and an
isolated Chromium profile; environment variables are documented in
`website-docs/05-clients/09-local-browser.md`. It exercises actual pairing, screenshot and
input RPC, agent popup selection/read/close and in-place borrow approval/revocation, independent preview during help waits,
focus, pause/resume, retained-session continuity and completed-task cleanup.
It separately verifies that agent last-tab closure permits the next turn and
manual window closure blocks automation until explicit resume.
Upstream references: [PR #227](https://github.com/Tencent/BrowserSkill/pull/227),
[remote connection contract](https://github.com/Tencent/BrowserSkill/blob/fa953dc6fcd868827b93164e3bea26198e691224/docs/remote-extension-connection.md).

View File

@ -2,10 +2,10 @@
"version": "0.3.0",
"extension": {
"version": "0.3.0",
"source_commit": "fa953dc6fcd868827b93164e3bea26198e691224"
"source_commit": "3da449e9ff2a1f25adc7170fb41d5ed7a82d84d5"
},
"daemon": {
"source_commit": "fa953dc6fcd868827b93164e3bea26198e691224",
"source_commit": "3da449e9ff2a1f25adc7170fb41d5ed7a82d84d5",
"build": "cargo build --locked --release -p bsk"
}
}

View File

@ -0,0 +1,141 @@
# BrowserSkill upstream integration
The extension and daemon are based on official `main` commit
`3da449e9ff2a1f25adc7170fb41d5ed7a82d84d5` (after `ext-v0.3.0`, including the
merged upstream PRs #291, #296, #297 and #318). Both still report version `0.3.0`;
the exact source baseline is recorded in `scripts/browserskill-release.json`.
The daemon is built with `cargo build --locked --release -p bsk` from the same
commit. The published CLI 0.3.0 binary and the published 0.3.0 extension ZIP
predate this baseline and are not equivalent replacements. Native builds
require Rust/Cargo and a C compiler (plus CMake on Linux). Docker builds on the
target architecture.
`scripts/build_browserskill.sh` builds the unmodified upstream checkout. No
BrowserSkill patches are applied or maintained downstream.
Renderer read reliability is provided by upstream PR #318: snapshot/AX captures
have a 20 s deadline, other bounded reads use 10 s, and a pending timed-out read
blocks further guarded reads until it settles or its debugger session detaches.
Navigation alone does not clear the gate. Errors retain
`data.reason = renderer_read_timeout`; unavailable child frames can be omitted
without discarding healthy frame content.
## Retired patches
| Former patch | Replacement |
| --- | --- |
| `01-browser-read-reliability.patch` | Upstream PR #318: bounded renderer reads, per-session read gates, capture fallback termination and debugger frame discovery. |
| `02-gateway-task-controls.patch` | Upstream PR #296: the optional UI channel `ui.task_preview` / `ui.task_focus` (renamed from `gateway.*`). WeKnora calls the official methods. |
| `03-vom-render-performance.patch` | Upstream PR #271 |
| `04-task-popup-ownership.patch` | Upstream PR #297: popups opened by native click/key input inside the Agent Window become observed, controllable tabs that session stop preserves. |
| `05-last-tab-lifecycle.patch` | Host side. `Manager.Call` runs `tool.tab_list` with `scope: "agent"` before `tool.tab_close`; when the target is the only tab in the Agent Window it first creates an agent-owned `about:blank` tab through `tool.tab_create`. Only official RPCs are involved. |
| `06-navigation-response-deadline.patch` | Upstream PR #291 (daemon navigation response grace). |
Background execution and viewport/full-page screenshot support use upstream
PRs #249, #250 and #253. Redirect document tracking and cancelled-navigation
reconciliation use upstream code including PR #280. Remote authentication,
credential storage/migration, renewal, connection settings and dedicated Agent
Windows use upstream PR #227. Do not restore the old
`remote-extension-connection.patch`, `shouldKeepTaskActive` callback or a second
focus-emulation cache.
Retained tasks keep the official session and debugger; turn completion only
stops preview polling in WeKnora. Completed tasks use native session stop.
## UI channel
The preview and focus side channel is now the official optional UI channel
documented in the upstream
[remote connection contract](https://github.com/Tencent/BrowserSkill/blob/3da449e9ff2a1f25adc7170fb41d5ed7a82d84d5/docs/remote-extension-connection.md#optional-ui-channel).
Only authenticated remote sockets handle these request frames; they bypass the
native automation queue and never start a session:
```json
{"id":"wk-ui-unique","method":"ui.task_preview","params":{"session_id":"server-owned-session"}}
```
- `ui.task_preview`: returns `image_base64`, `format: "jpeg"`, `tab_id`,
`title` and `captured_at`; the frame is at most 640 pixels wide, captures are
coalesced per task and a poll that arrives while Chrome still holds one is
refused with `timeout` / `preview_busy`. Overlays stay visible in previews.
- `ui.task_focus`: activates the task's owned tab and raises its window,
returning `{ "focused": true }`.
Errors use the native envelope with typed codes (`not_found`, `timeout`,
`cancelled`, `cdp_failed`) and an optional `data.reason`. WeKnora maps every
error except `unknown_method` to a transient preview failure. Extensions built
before PR #296, including the published 0.3.0 ZIP, answer `unknown_method`;
WeKnora then disables preview polling and reports the extension as outdated.
## Intentional behavior changes versus the published 0.3.0 extension
- Remote tasks use official dedicated Agent Windows. No `tabGroups` permission.
- Popup attribution follows upstream PR #297: only a main-frame navigation
target from a controlled source during native click/key input, within the
same Agent Window, becomes an observed tab. Observed tabs are readable and
closable but never enter the agent-created set; session stop preserves them and their window. Independent
popup windows, late or unattributed targets use `tab_borrow` and `tab_return`.
An unowned tab that already sits in the Agent Window must be moved to a regular window before it
can be borrowed.
- Closing the last tab of the Agent Window through `tab_close` keeps an
agent-owned blank tab until session stop (host-side, see above), so Chrome's
window removal is not misreported as a human interruption. If the close is
refused or interrupted while the target still exists, the blank tab is removed
again. Actual user window closure still pauses the task.
- Borrow confirmation follows the browser's preference and uses an HTTP(S) page
in a regular user window. The integration fixture supplies such a page; neither
extension settings nor a popup window can host the prompt. Borrowed tabs cannot
be closed through `tab_close`. Return them with `tab_return`; if the original
window disappeared, upstream may create a normal fallback window with a new-tab
placeholder. These returned user pages are preserved when the task ends.
- Human help follows the upstream focus/confirmation behavior.
- Remote upload/download remain unsupported, as defined upstream.
Even though the version remains 0.3.0, users must install the rebuilt ZIP.
Upgrade users by replacing the existing unpacked extension directory and
reloading it after ending active tasks. Keeping the extension ID preserves the
migrated credentials; installing under a new ID requires pairing again.
## Validation
Use a clean pinned upstream checkout, install frozen dependencies and run:
```sh
pnpm --filter @browser-skill/extension exec wxt prepare
pnpm --filter @browser-skill/extension compile
pnpm --filter @browser-skill/extension test
pnpm --filter @browser-skill/vom test
pnpm ext:build:zip
BSK_GEOMETRY_CHROME=/path/to/test-chrome BSK_BACKGROUND_CHROME=/path/to/test-chrome \
pnpm --filter @browser-skill/extension exec vitest run --maxWorkers=1 \
src/tools/__tests__/background-execution.browser.test.ts \
src/tools/__tests__/background-screenshot.browser.test.ts \
src/tools/__tests__/background-full-page.browser.test.ts
cargo test --locked -p bsk daemon::ipc::tests
```
Run WeKnora's `TestRealExtension` with the built extension, pinned daemon and an
isolated Chromium profile. It exercises actual pairing, screenshot and input RPC,
same-window target selection/read/close, independent popup borrow/read/return and
access revocation, the in-window borrow rejection followed by borrow
approval/revocation of a tab moved to a regular window,
independent preview during help waits, focus, pause/resume, retained-session
continuity and completed-task cleanup. It separately verifies that agent
last-tab closure permits the next turn and manual window closure blocks
automation until explicit resume. Cleanup checks preserve the exact initial and
returned user tab IDs, permitting only the new-tab placeholder recorded from an
official fallback return; arbitrary extra tabs still fail the test.
```sh
BROWSERSKILL_TEST_EXTENSION=/path/to/unpacked/chrome-mv3 \
BROWSERSKILL_TEST_BINARY=/path/to/bsk \
BROWSERSKILL_TEST_CHROMIUM=/path/to/test-chromium \
BROWSERSKILL_TEST_PLAYWRIGHT=/path/to/playwright-core/index.mjs \
go test ./internal/browserskill -run '^TestRealExtension$' -count=1 -v
```
Upstream references: [PR #227](https://github.com/Tencent/BrowserSkill/pull/227),
[PR #291](https://github.com/Tencent/BrowserSkill/pull/291),
[PR #296](https://github.com/Tencent/BrowserSkill/pull/296),
[PR #297](https://github.com/Tencent/BrowserSkill/pull/297),
[PR #318](https://github.com/Tencent/BrowserSkill/pull/318).

View File

@ -34,10 +34,6 @@ trap 'rm -rf "$build_dir"' EXIT
git clone --no-checkout https://github.com/Tencent/BrowserSkill.git "$build_dir/source"
git -C "$build_dir/source" checkout --detach "$source_commit"
for patch in "$repo_root"/patches/browserskill/*.patch; do
git -C "$build_dir/source" apply --check "$patch"
git -C "$build_dir/source" apply "$patch"
done
(
cd "$build_dir/source"
npx --yes pnpm@10.17.0 install --frozen-lockfile
@ -46,8 +42,8 @@ done
cp "$build_dir/source/apps/extension/dist/browser-skillextension-${extension_version}-chrome.zip" "$output_dir/browser-skill-weknora-${extension_version}.zip"
cp "$build_dir/source/LICENSE" "$output_dir/BrowserSkill-LICENSE"
# Build the daemon from the same source and apply our navigation-response
# deadline fix; published CLI binaries do not include this downstream patch.
# Build the daemon from the same pinned source; the published CLI 0.3.0 binary
# predates the navigation-response grace fix (upstream PR #291).
cargo_target_dir="${CARGO_TARGET_DIR:-$build_dir/target}"
mkdir -p "$cargo_target_dir"
cargo_target_dir="$(cd "$cargo_target_dir" && pwd)"

View File

@ -6,7 +6,7 @@ import { pathToFileURL } from 'node:url';
const lines = createInterface({ input: process.stdin });
const first = await new Promise(resolve => lines.once('line', resolve));
const { pairing, extension, chromium: executablePath, playwright } = JSON.parse(first);
const { pairing, extension, chromium: executablePath, playwright, fixture } = JSON.parse(first);
const { chromium } = await import(pathToFileURL(playwright).href);
const profile = await mkdtemp('/tmp/wkb-chrome-');
let browser;
@ -34,12 +34,19 @@ try {
await popup.locator('form button[type="submit"]').click();
await popup.waitForFunction(() => document.querySelector('#remote-pairing')?.value === '' || document.querySelector('form [role=alert]'));
if (await popup.locator('form [role=alert]').count()) throw new Error('Extension authorization failed before browser tests');
// Official borrow confirmation needs an injectable page in a normal user
// window. Extension settings and an isolated popup cannot host that prompt.
const confirmationPage = await browser.newPage();
await confirmationPage.goto(fixture);
await confirmationPage.locator('browser-skill-overlay').waitFor({state:'attached'});
const initial = await worker.evaluate(async () => {
const window = await chrome.windows.getLastFocused();
const tabs = await chrome.tabs.query({windowId:window.id,active:true});
return {windowId:window.id,tabId:tabs[0].id,tabIds:(await chrome.tabs.query({})).map(t=>t.id)};
});
const originalWindow = { windowId: initial.windowId, tabId: initial.tabId };
const preservedTabs = new Set();
let beforeReturn;
process.stdout.write('ready\n');
for await (const command of lines) {
if (command === 'close') break;
@ -86,7 +93,7 @@ try {
await chrome.debugger.sendCommand({tabId:tab.id}, 'Runtime.evaluate', {expression:'0', returnByValue:true});
used.push(tab.id);
} catch (error) {
if (!/not attached|No tab|No target/i.test(String(error))) throw error;
if (!/not attached|No tab|No target|Cannot access a chrome:\/\/ URL/i.test(String(error))) throw error;
}
}
return used;
@ -95,7 +102,37 @@ try {
}
if (command === 'check-cleanup') {
const state = await worker.evaluate(async () => ({ids:(await chrome.tabs.query({})).map(t=>t.id)}));
process.stdout.write(JSON.stringify({cleaned:state.ids.length===initial.tabIds.length && state.ids.every(id=>initial.tabIds.includes(id))})+'\n');
const expected = new Set([...initial.tabIds, ...preservedTabs]);
const missing = [...expected].filter(id => !state.ids.includes(id));
const unexpected = state.ids.filter(id => !expected.has(id));
const cleaned = missing.length === 0 && unexpected.length === 0;
process.stdout.write(JSON.stringify(cleaned ? {cleaned} : {cleaned, missing, unexpected})+'\n');
}
if (command.startsWith('before-tab-return ')) {
const id = Number(command.split(' ')[1]);
beforeReturn = await worker.evaluate(async (tabId) => ({
tabId, tabs: await chrome.tabs.query({}), windows: await chrome.windows.getAll({}),
}), id);
process.stdout.write('return-recorded\n');
}
if (command.startsWith('expect-returned-tab ')) {
// Admit only the returned page and the official fallback window's new-tab
// placeholder. Never allow arbitrary extra tabs to hide task cleanup leaks.
const returned = JSON.parse(command.slice('expect-returned-tab '.length));
if (!beforeReturn || beforeReturn.tabId !== returned.tab_id) throw new Error('Missing return snapshot');
const tabs = await worker.evaluate(async () => chrome.tabs.query({}));
const target = tabs.find(tab => tab.id === returned.tab_id);
if (!target || target.windowId !== returned.returned_to_window_id) throw new Error('Returned tab missing or misplaced');
const added = tabs.filter(tab => !beforeReturn.tabs.some(previous => previous.id === tab.id));
const newWindow = !beforeReturn.windows.some(window => window.id === target.windowId);
if (added.length > 1 || added.some(tab => !returned.fallback || !newWindow ||
tab.windowId !== target.windowId || (tab.pendingUrl ?? tab.url) !== 'chrome://newtab/')) {
throw new Error('Unexpected tabs created during tab_return');
}
preservedTabs.add(target.id);
for (const tab of added) preservedTabs.add(tab.id);
beforeReturn = undefined;
process.stdout.write('returned-tab-preserved\n');
}
if (command === 'remember-foreground') {
const foreground = await worker.evaluate(async () => {
@ -114,7 +151,7 @@ try {
// Extension API creation simulates a user-created tab: no navigation
// source event, even though it is inside the Agent Window.
return chrome.tabs.create({windowId:source.windowId,url:source.url+'?user-tab=1',active:true});
}, initial.tabIds);
}, [...initial.tabIds, ...preservedTabs]);
const page = await createdPage;
await page.waitForURL('**/*?user-tab=1');
await page.locator('browser-skill-overlay').waitFor({state:'attached'});
@ -128,9 +165,19 @@ try {
}, id);
process.stdout.write('fixture-window-closed\n');
}
if (command.startsWith('move-fixture-tab-out ')) {
// Upstream refuses to borrow an unowned tab that already lives in the
// Agent Window; the user must move it to a regular window first.
const id = Number(command.split(' ')[1]);
await worker.evaluate(async ({tabId, windowId}) => {
await chrome.tabs.move(tabId, {windowId, index:-1});
}, {tabId:id, windowId:originalWindow.windowId});
process.stdout.write('fixture-tab-moved\n');
}
if (command.startsWith('remove-fixture-tab ')) {
const id = Number(command.split(' ')[1]);
await worker.evaluate(async (tabId) => chrome.tabs.remove(tabId), id);
preservedTabs.delete(id);
process.stdout.write('fixture-tab-removed\n');
}
if (command === 'check-background') {

View File

@ -15,7 +15,9 @@ WeKnora 通过 BrowserSkill daemon 和配套 Chrome 扩展,将用户电脑上
### 人工参与与任务恢复
任务可操作自己创建的标签;用户原有标签需要明确借用授权,手动拖入任务窗口也不等于授权。结束任务时关闭任务新建页面、归还借用页面。
任务可操作自己创建的标签;任务窗口内由点击或按键打开、且通过来源校验的新标签也可操作,结束任务时会保留。独立弹出窗口和用户原有标签通过 `tab_borrow` 借用,按浏览器设置确认授权后操作,完成后用 `tab_return` 归还。结束任务时关闭任务显式创建的页面、归还借用页面。
手动拖入任务窗口不等于授权;未授权页面已在任务窗口内时,需要用户先移到普通窗口再借用。借用确认提示需要普通窗口中的 HTTP(S) 页面承载,扩展设置页、新标签页和独立弹窗不能承载。若借用后原窗口消失,归还可能创建普通备用窗口;归还页面不会随任务结束被关闭。
登录、验证码或授权步骤由 `request_help` 发起。按预览提示进入浏览器,完成后在浏览器帮助提示中确认;Agent 再观察页面并继续。仅在聊天中说“请登录”不会创建人工接管提示。帮助等待最多五分钟,超时或取消会保留现场并暂停。
@ -33,7 +35,7 @@ WeKnora 通过 BrowserSkill daemon 和配套 Chrome 扩展,将用户电脑上
./scripts/build_browserskill.sh
```
需要 Git、Node.js、Python 3、Rust/Cargo 和 C 编译器,Linux 还需要 CMake。固定源码版本由 `scripts/browserskill-release.json` 管理,补丁位于 `patches/browserskill/`,产物输出到 `artifacts/browserskill/`。使用对应操作系统与架构的 daemon,并按实际安装路径配置:
需要 Git、Node.js、Python 3、Rust/Cargo 和 C 编译器,Linux 还需要 CMake。固定源码版本由 `scripts/browserskill-release.json` 管理,直接构建上游源码,不应用额外补丁,产物输出到 `artifacts/browserskill/`。使用对应操作系统与架构的 daemon,并按实际安装路径配置:
```dotenv
BROWSERSKILL_BINARY=/opt/weknora/browserskill/bsk
@ -49,7 +51,7 @@ BROWSERSKILL_PUBLIC_URL=wss://weknora.example.com/api/v1/local-browser/extension
本机可用 localhost WS,远端要求浏览器信任的 WSS 证书。内网可以使用受信任的企业 CA。显式设置 `BROWSERSKILL_BINARY=` 可关闭能力;修改 Docker 环境变量后使用 `docker compose up -d app frontend` 重建容器。
配套扩展和 daemon 必须一同升级,不能只按显示的 0.3.0 版本号判断兼容性;上游同版本原版不包含所有配套接口。覆盖原解压目录并重新加载可保留扩展 ID,重新安装导致 ID 改变时需要重新配对。分发时保留 `BrowserSkill-LICENSE`。
配套扩展和 daemon 必须一同升级,不能只按显示的 0.3.0 版本号判断兼容性;上游早期发布的 0.3.0 二进制和 ZIP 早于当前固定提交,不包含所需修复和 UI 接口。覆盖原解压目录并重新加载可保留扩展 ID,重新安装导致 ID 改变时需要重新配对。分发时保留 `BrowserSkill-LICENSE`。
## 多副本与入口代理