Agent Zero AI framework
Restore internal Browser tabs automatically
Alessandro1fb9363b49b7
diff --git a/plugins/_browser/AGENTS.md b/plugins/_browser/AGENTS.mdindex 81f41bdb5..688cec007 100644--- a/plugins/_browser/AGENTS.md+++ b/plugins/_browser/AGENTS.md@@ -21,6 +21,8 @@- Default the visible WebUI Browser to the authenticated Xpra HTML5 viewer for its existing Patchright page. Keep live CDP screencast and lightweight snapshots as automatic fallbacks.- Do not block an available interactive viewer on a redundant Chromium screenshot; capture initial snapshots only for fallback transports.- Keep headful Chromium in a normal window with its own toolbar clipped above the private display; do not use browser fullscreen, which shows Chromium's exit warning.+- Persist open-tab ownership and URLs through the shared KVP store; automatically restore the current chat when its Browser surface opens in per-chat mode and every saved chat in shared mode, then hide Chromium's redundant crash-restore advisory.+- When no Browser tab manifest exists yet, use Chromium's last session once to migrate open tabs into the owned manifest.- Throttle interactive resize updates throughout a drag and let the native-sized Chromium viewport follow the private display; do not defer all layout updates until resizing stops.- Keep exactly one interactive viewer iframe connected during canvas/modal handoff so hidden surfaces cannot compete to resize the same display.- Notify the active Xpra client of its new frame geometry before resizing the backing display; after an interactive canvas/modal handoff, reconcile once after Xpra's deferred resize so Chromium cannot retain the previous surface size.diff --git a/plugins/_browser/api/ws_browser.py b/plugins/_browser/api/ws_browser.pyindex 837bba129..41c5011e3 100644--- a/plugins/_browser/api/ws_browser.py+++ b/plugins/_browser/api/ws_browser.py@@ -14,7 +14,11 @@ from plugins._browser.helpers.config import (TAB_SCOPE_KEY,get_browser_config,)-from plugins._browser.helpers.runtime import get_runtime, list_runtime_sessions+from plugins._browser.helpers.runtime import (+ get_runtime,+ has_restorable_browser_tabs,+ list_runtime_sessions,+)FRAME_READ_TIMEOUT_SECONDS = 0.5@@ -80,6 +84,8 @@ class WsBrowser(WsHandler):create_browser = self._bool(data.get("create_browser", data.get("createBrowser")))runtime = await get_runtime(context_id, create=create_browser)+ if not runtime and not create_browser and has_restorable_browser_tabs(context_id):+ runtime = await get_runtime(context_id)listing = {"browsers": [], "last_interacted_browser_id": None}browsers: list[dict[str, Any]] = []if runtime:diff --git a/plugins/_browser/helpers/config.py b/plugins/_browser/helpers/config.pyindex ea4de4e4b..f000a6f3c 100644--- a/plugins/_browser/helpers/config.py+++ b/plugins/_browser/helpers/config.py@@ -408,7 +408,7 @@ def describe_browser_extensions(settings: dict[str, Any] | None) -> dict[str, Andef build_browser_launch_config(settings: dict[str, Any] | None) -> dict[str, Any]:config = normalize_browser_config(settings)extensions = describe_browser_extensions(config)- args: list[str] = []+ args = ["--hide-crash-restore-bubble"]channel: str | None = Nonebrowser_mode = "chromium"proxy = Nonediff --git a/plugins/_browser/helpers/runtime.py b/plugins/_browser/helpers/runtime.pyindex cc1441af4..0911eb441 100644--- a/plugins/_browser/helpers/runtime.py+++ b/plugins/_browser/helpers/runtime.py@@ -16,15 +16,17 @@ from dataclasses import dataclassfrom pathlib import Pathfrom typing import Any-from helpers import chat_media, files+from helpers import chat_media, files, kvpfrom helpers.defer import DeferredTaskfrom helpers.errors import RepairableExceptionfrom helpers.print_style import PrintStylefrom plugins._browser.helpers.config import (+ DEFAULT_BROWSER_TAB_SCOPE,DEFAULT_HOMEPAGE_KEY,DEFAULT_MAX_OPEN_TABS,MAX_OPEN_TABS_KEY,+ TAB_SCOPE_KEY,build_browser_launch_config,get_browser_config,)@@ -37,6 +39,8 @@ DOM_HELPER_PATH = PLUGIN_DIR / "assets" / "browser-dom-helper.js"CONTENT_HELPER_PATH = PLUGIN_DIR / "assets" / "browser-page-content.js"RUNTIME_DATA_KEY = "_browser_runtime"SHARED_RUNTIME_ID = "shared"+BROWSER_TABS_KEY = "browser_open_tabs"+BROWSER_TABS_VERSION = 1DEFAULT_VIEWPORT = {"width": 1024, "height": 768}CHROME_SINGLETON_FILES = ("SingletonLock", "SingletonCookie", "SingletonSocket")SCREENCAST_MAX_WIDTH = 4096@@ -287,6 +291,85 @@ def _safe_context_id(context_id: str) -> str:return _SAFE_CONTEXT_RE.sub("_", str(context_id or "default")).strip("._") or "default"+def _load_browser_tabs() -> tuple[bool, list[dict[str, Any]]]:+ try:+ payload = kvp.get_persistent(BROWSER_TABS_KEY, None)+ except Exception as exc:+ PrintStyle.warning(f"Browser tab recovery state could not be read: {exc}")+ return False, []+ if payload is None:+ return False, []+ if not isinstance(payload, dict) or not isinstance(payload.get("tabs"), list):+ PrintStyle.warning("Browser tab recovery state is invalid; starting without it.")+ return False, []++ tabs: list[dict[str, Any]] = []+ for entry in payload["tabs"]:+ if not isinstance(entry, dict):+ continue+ context_id = str(entry.get("context_id") or "").strip()+ url = str(entry.get("url") or "").strip()+ if not context_id or not url:+ continue+ tabs.append(+ {+ "context_id": context_id,+ "url": url,+ "active": bool(entry.get("active")),+ }+ )+ return True, tabs+++def _save_browser_tabs(tabs: list[dict[str, Any]]) -> None:+ kvp.set_persistent(+ BROWSER_TABS_KEY,+ {"version": BROWSER_TABS_VERSION, "tabs": tabs},+ )+++def _forget_browser_context(context_id: str) -> None:+ exists, tabs = _load_browser_tabs()+ if not exists:+ return+ remaining = [entry for entry in tabs if entry["context_id"] != context_id]+ if len(remaining) != len(tabs):+ try:+ _save_browser_tabs(remaining)+ except Exception as exc:+ PrintStyle.warning(f"Browser tab recovery state could not be updated: {exc}")+++def has_restorable_browser_tabs(context_id: str) -> bool:+ exists, tabs = _load_browser_tabs()+ if not exists:+ for runtime_id in (SHARED_RUNTIME_ID, _safe_context_id(context_id)):+ session_dir = Path(+ files.get_abs_path(+ "tmp",+ "browser",+ "sessions",+ runtime_id,+ "Default",+ "Sessions",+ )+ )+ try:+ if any(session_dir.glob("Session_*")) or any(session_dir.glob("Tabs_*")):+ return True+ except OSError:+ continue+ return False+ if not tabs:+ return False+ if str(+ get_browser_config().get(TAB_SCOPE_KEY, DEFAULT_BROWSER_TAB_SCOPE)+ or DEFAULT_BROWSER_TAB_SCOPE+ ) == "shared":+ return True+ return any(entry["context_id"] == str(context_id) for entry in tabs)++@dataclassclass BrowserPage:id: int@@ -625,6 +708,12 @@ class _BrowserRuntimeCore:self._pending_popup_contexts: dict[asyncio.Future[int], str] = {}self._background_popup_pages: set[int] = set()self._bootstrap_page: Any | None = None+ self._restore_state_exists = False+ self._restore_state_loaded = False+ self._restore_entries: list[dict[str, Any]] = []+ self._restored_context_ids: set[str] = set()+ self._restored_all = False+ self._restoring_tabs = Falseself._browser_chrome_height: int | None = Noneself._browser_window_page: Any | None = Noneself._browser_window_session: Any | None = None@@ -650,6 +739,123 @@ class _BrowserRuntimeCore:else:self._last_interacted_browser_ids[context_id] = int(browser_id)+ def _load_restore_state(self) -> None:+ self._restore_state_exists, self._restore_entries = _load_browser_tabs()+ self._restore_state_loaded = True+ self._restored_context_ids.clear()+ self._restored_all = False+ self._restoring_tabs = False++ def _tab_scope(self) -> str:+ return str(+ get_browser_config().get(TAB_SCOPE_KEY, DEFAULT_BROWSER_TAB_SCOPE)+ or DEFAULT_BROWSER_TAB_SCOPE+ )++ def _persist_browser_tabs(self) -> None:+ if not self._restore_state_loaded or self._restoring_tabs:+ return++ replaced_contexts = set(self._restored_context_ids)+ live_tabs: list[dict[str, Any]] = []+ for browser_id in sorted(self.pages):+ browser_page = self.pages[browser_id]+ context_id = self._page_context_id(browser_page)+ replaced_contexts.add(context_id)+ try:+ url = str(browser_page.page.url or "about:blank").strip()+ except Exception:+ continue+ if not url:+ url = "about:blank"+ live_tabs.append(+ {+ "context_id": context_id,+ "url": url,+ "active": (+ self._last_interacted_browser_ids.get(context_id) == browser_id+ ),+ }+ )++ preserved_tabs = (+ []+ if self._restored_all+ else [+ entry+ for entry in self._restore_entries+ if entry["context_id"] not in replaced_contexts+ ]+ )+ tabs = preserved_tabs + live_tabs+ try:+ _save_browser_tabs(tabs)+ except Exception as exc:+ PrintStyle.warning(f"Browser tab recovery state could not be saved: {exc}")+ return+ self._restore_state_exists = True+ self._restore_entries = tabs++ async def _restore_tabs_for_scope(self) -> None:+ if not self._restore_state_loaded or self._restoring_tabs or not self.context:+ return++ tab_scope = self._tab_scope()+ if tab_scope == "shared":+ if self._restored_all:+ return+ entries = [+ entry+ for entry in self._restore_entries+ if entry["context_id"] not in self._restored_context_ids+ ]+ else:+ context_id = self.current_context_id+ if self._restored_all or context_id in self._restored_context_ids:+ return+ entries = [+ entry for entry in self._restore_entries if entry["context_id"] == context_id+ ]++ restored_ids: dict[str, list[int]] = {}+ active_ids: dict[str, int] = {}+ navigations: list[tuple[Any, str]] = []+ self._restoring_tabs = True+ try:+ for entry in entries:+ context_id = entry["context_id"]+ if len(self._context_browser_ids(context_id)) >= self._max_open_tabs():+ continue+ page = self._bootstrap_page+ self._bootstrap_page = None+ if not page or page.is_closed():+ page = await self.context.new_page()+ browser_page = await self._register_page(page, context_id)+ navigations.append((page, normalize_url(entry["url"])))+ restored_ids.setdefault(context_id, []).append(browser_page.id)+ if entry["active"]:+ active_ids[context_id] = browser_page.id+ await asyncio.gather(+ *(+ self._goto(page, url, wait_until="commit")+ for page, url in navigations+ )+ )+ finally:+ self._restoring_tabs = False++ for context_id, browser_ids in restored_ids.items():+ self._set_last_interacted(+ context_id,+ active_ids.get(context_id, browser_ids[0]),+ )+ if tab_scope == "shared":+ self._restored_all = True+ self._restored_context_ids.update(entry["context_id"] for entry in entries)+ else:+ self._restored_context_ids.add(self.current_context_id)+ self._persist_browser_tabs()+def _page_context_id(self, browser_page: BrowserPage) -> str:return str(browser_page.context_id or self.context_id)@@ -803,6 +1009,7 @@ class _BrowserRuntimeCore:async def ensure_started(self) -> None:if self._context_is_alive():+ await self._restore_tabs_for_scope()returnif self.context:await self._discard_stale_context("Browser context is stale; restarting.")@@ -812,12 +1019,14 @@ class _BrowserRuntimeCore:async with self._start_lock:if self._context_is_alive():+ await self._restore_tabs_for_scope()returnif self.context:await self._discard_stale_context("Browser context is stale; restarting.")elif self.playwright and not self._closing:await self._stop_playwright("Browser context closed; restarting Playwright.")await self._start()+ await self._restore_tabs_for_scope()def _context_is_alive(self) -> bool:if not self.context:@@ -875,6 +1084,7 @@ class _BrowserRuntimeCore:async def _start(self) -> None:from plugins._browser import hooks+ self._load_restore_state()preparation = hooks.prepare_playwright_cache()if preparation.get("errors") or not preparation.get("binary"):problem = preparation.get("errors") or "missing binary"@@ -891,6 +1101,8 @@ class _BrowserRuntimeCore:browser_binary = Path(preparation["binary"])browser_display = self.interactive_view.ensure_display()launch_args = list(launch_config["args"])+ if not self._restore_state_exists:+ launch_args.append("--restore-last-session")if browser_display:launch_args.extend([@@ -939,7 +1151,18 @@ class _BrowserRuntimeCore:self.context.on("close", self._on_context_closed)self.context.on("page", self._on_new_page_sync)- for page in list(self.context.pages):+ existing_pages = list(self.context.pages)+ if self._restore_state_exists:+ for page in existing_pages:+ if self._bootstrap_page is None:+ self._bootstrap_page = page+ await self._fit_browser_window(page)+ continue+ with contextlib.suppress(Exception):+ await page.close()+ return++ for page in existing_pages:if page.url == "about:blank":if browser_display and self._bootstrap_page is None:self._bootstrap_page = page@@ -1043,6 +1266,7 @@ class _BrowserRuntimeCore:await self._goto(page, normalize_url(target_url))else:await self._settle(page)+ self._persist_browser_tabs()return {"id": browser_page.id, "state": await self._state(browser_page.id)}def _initial_url(self, url: str = "") -> str:@@ -1101,6 +1325,16 @@ class _BrowserRuntimeCore:"last_interacted_browser_id": self.last_interacted_browser_id,}+ async def list_all(self) -> dict[str, Any]:+ await self.ensure_started()+ browser_ids = sorted(self.pages)+ return {+ "browsers": await asyncio.gather(+ *(self._state(browser_id) for browser_id in browser_ids)+ ),+ "last_interacted_browser_ids": dict(self._last_interacted_browser_ids),+ }+async def multi(self, calls: list[dict[str, Any]]) -> list[dict[str, Any]]:if not isinstance(calls, list) or not calls:raise ValueError("multi requires a non-empty list of calls")@@ -1347,6 +1581,7 @@ class _BrowserRuntimeCore:with contextlib.suppress(Exception):await page.bring_to_front()await self._fit_browser_window(page)+ self._persist_browser_tabs()return await self._state(resolved_id)async def state(self, browser_id: int | str | None = None) -> dict[str, Any]:@@ -1365,6 +1600,7 @@ class _BrowserRuntimeCore:page = self._page(resolved_id)await self._goto(page, normalize_url(url), wait_until=wait_until)self._maybe_promote(resolved_id)+ self._persist_browser_tabs()return await self._state(resolved_id)async def back(@@ -1379,6 +1615,7 @@ class _BrowserRuntimeCore:await page.go_back(wait_until=wait_until, timeout=10000)await self._settle(page, short=wait_until == "commit")self._maybe_promote(resolved_id)+ self._persist_browser_tabs()return await self._state(resolved_id)async def forward(@@ -1393,6 +1630,7 @@ class _BrowserRuntimeCore:await page.go_forward(wait_until=wait_until, timeout=10000)await self._settle(page, short=wait_until == "commit")self._maybe_promote(resolved_id)+ self._persist_browser_tabs()return await self._state(resolved_id)async def reload(@@ -1407,6 +1645,7 @@ class _BrowserRuntimeCore:await page.reload(wait_until=wait_until, timeout=15000)await self._settle(page, short=wait_until == "commit")self._maybe_promote(resolved_id)+ self._persist_browser_tabs()return await self._state(resolved_id)async def content(@@ -1690,6 +1929,7 @@ class _BrowserRuntimeCore:self.pages.pop(resolved_id, None)if self.last_interacted_browser_id == resolved_id:self.last_interacted_browser_id = next(iter(self._context_browser_ids()), None)+ self._persist_browser_tabs()return await self.list()async def close_all_browsers(self) -> dict[str, Any]:@@ -1706,6 +1946,8 @@ class _BrowserRuntimeCore:passself.pages.pop(browser_id, None)self.last_interacted_browser_id = None+ self._restored_context_ids.add(self.current_context_id)+ self._persist_browser_tabs()async def screenshot(self,@@ -2361,6 +2603,13 @@ class _BrowserRuntimeCore:return Trueasync def close(self, delete_profile: bool = False) -> None:+ if delete_profile:+ with contextlib.suppress(Exception):+ kvp.remove_persistent(BROWSER_TABS_KEY)+ self._restore_entries.clear()+ self._restore_state_exists = False+ else:+ self._persist_browser_tabs()self._closing = Truefor waiter in self._pending_popups:if not waiter.done():@@ -2516,6 +2765,17 @@ class _BrowserRuntimeCore:self.pages.pop(browser_id, None)page.on("close", on_close)++ def on_navigated(frame: Any) -> None:+ main_frame = getattr(page, "main_frame", None)+ if main_frame is not None and frame is not main_frame:+ return+ try:+ asyncio.create_task(self._persist_page_change_async(browser_id))+ except RuntimeError:+ return++ page.on("framenavigated", on_navigated)return browser_pageasync def _register_page(@@ -2604,6 +2864,12 @@ class _BrowserRuntimeCore:except Exception as exc:PrintStyle.warning(f"Page unregister failed: {exc}")+ async def _persist_page_change_async(self, browser_id: int) -> None:+ await asyncio.sleep(0)+ if self._closing or self._restoring_tabs or browser_id not in self.pages:+ return+ self._persist_browser_tabs()+def _on_new_page_sync(self, page: Any) -> None:if self._closing or self.context is None:return@@ -2647,6 +2913,7 @@ class _BrowserRuntimeCore:await page.close()else:await self._fit_browser_window(page)+ self._persist_browser_tabs()except Exception as exc:PrintStyle.warning(f"Popup registration failed: {exc}")@@ -2767,7 +3034,11 @@ async def get_runtime(raise ValueError("context_id is required")with _runtime_lock:runtime = _runtimes.get(context_id)- if runtime is None and create:+ if runtime is None and _shared_runtime is not None:+ runtime = BrowserRuntimeSession(context_id, _shared_runtime)+ if create:+ _runtimes[context_id] = runtime+ elif runtime is None and create:if _shared_runtime is None:_shared_runtime = BrowserRuntime(SHARED_RUNTIME_ID)runtime = BrowserRuntimeSession(context_id, _shared_runtime)@@ -2779,10 +3050,14 @@ async def close_runtime(context_id: str, *, delete_profile: bool = True) -> Nonecontext_id = str(context_id or "").strip()if not context_id:return+ _forget_browser_context(context_id)with _runtime_lock:runtime = _runtimes.pop(context_id, None)+ shared_runtime = _shared_runtimeif runtime:await runtime.call("close_context")+ elif shared_runtime:+ await shared_runtime.call_for(context_id, "close_context")def close_runtime_sync(context_id: str, *, delete_profile: bool = True) -> None:@@ -2800,6 +3075,9 @@ async def close_all_runtimes(*, delete_profiles: bool = False) -> None:_runtimes.clear()runtime = _shared_runtime_shared_runtime = None+ if delete_profiles:+ with contextlib.suppress(Exception):+ kvp.remove_persistent(BROWSER_TABS_KEY)if runtime:try:await runtime.close(delete_profile=delete_profiles)@@ -2824,6 +3102,31 @@ def known_context_ids() -> list[str]:async def list_runtime_sessions() -> list[dict[str, Any]]:with _runtime_lock:runtimes = list(_runtimes.items())+ shared_runtime = _shared_runtime++ if shared_runtime and str(+ get_browser_config().get(TAB_SCOPE_KEY, DEFAULT_BROWSER_TAB_SCOPE)+ or DEFAULT_BROWSER_TAB_SCOPE+ ) == "shared":+ request_context_id = runtimes[0][0] if runtimes else SHARED_RUNTIME_ID+ try:+ listing = await shared_runtime.call_for(request_context_id, "list_all")+ except Exception as exc:+ PrintStyle.warning(f"Shared Browser runtime list failed: {exc}")+ return []+ grouped: dict[str, list[dict[str, Any]]] = {}+ for browser in listing.get("browsers") or []:+ context_id = str(browser.get("context_id") or SHARED_RUNTIME_ID)+ grouped.setdefault(context_id, []).append(browser)+ active_ids = listing.get("last_interacted_browser_ids") or {}+ return [+ {+ "context_id": context_id,+ "browsers": browsers,+ "last_interacted_browser_id": active_ids.get(context_id),+ }+ for context_id, browsers in grouped.items()+ ]sessions: list[dict[str, Any]] = []for context_id, runtime in runtimes:diff --git a/plugins/_browser/webui/browser-panel.html b/plugins/_browser/webui/browser-panel.htmlindex 673d57ec5..8270393ff 100644--- a/plugins/_browser/webui/browser-panel.html+++ b/plugins/_browser/webui/browser-panel.html@@ -255,11 +255,20 @@<textarea x-model="$store.browserPage.annotationDraftText" placeholder="Comment"maxlength="1200" x-init="$nextTick(() => $el.focus())"></textarea><div class="browser-annotation-actions">- <button type="button" class="btn btn-ok browser-annotation-add"+ <button type="button" class="browser-annotation-mic mic-inactive"+ data-whisper-microphone title="Record annotation comment"+ aria-label="Record annotation comment"+ x-init="$nextTick(() => $store.browserPage.syncAnnotationMicrophoneUI())"+ @click="$store.browserPage.startAnnotationVoice(true)">+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 18" fill="currentColor" aria-hidden="true">+ <path d="m8,12c1.66,0,3-1.34,3-3V3c0-1.66-1.34-3-3-3s-3,1.34-3,3v6c0,1.66,1.34,3,3,3Zm-1,1.9c-2.7-.4-4.8-2.6-5-5.4H0c.2,3.8,3.1,6.9,7,7.5v2h2v-2c3.9-.6,6.8-3.7,7-7.5h-2c-.2,2.8-2.3,5-5,5.4h-2Z" />+ </svg>+ </button>+ <button type="button" class="browser-annotation-send"title="Add annotation" aria-label="Add annotation":disabled="!String($store.browserPage.annotationDraftText || '').trim()"@click="$store.browserPage.addAnnotationComment()">- <x-icon name="add_comment"></x-icon>+ <x-icon name="arrow_forward"></x-icon></button></div></div>@@ -1208,23 +1217,12 @@gap: 7px;}- .browser-annotation-actions .btn,.browser-annotation-tray-actions .btn {min-height: 30px;padding: 0 10px;white-space: nowrap;}- .browser-annotation-add {- width: 34px;- min-width: 34px;- padding: 0;- }-- .browser-annotation-add .material-symbols-outlined {- font-size: 18px;- }-.browser-annotation-tray {z-index: 20;right: 10px;@@ -1319,6 +1317,7 @@line-height: 1.25;}+ .browser-annotation-actions,.browser-annotation-tray-actions {align-items: center;gap: 0.58rem;@@ -1382,6 +1381,11 @@.browser-annotation-send:hover { background: #353bc5; }.browser-annotation-send:active { background: #2b309c; transform: translateY(1px) scale(0.98); }+ .browser-annotation-send:disabled {+ background: #4248f1;+ cursor: not-allowed;+ opacity: 0.5;+ }.browser-annotation-mic svg,.browser-annotation-send .material-symbols-outlined {diff --git a/plugins/_browser/webui/browser-store.js b/plugins/_browser/webui/browser-store.jsindex fff5f6e4b..1b59f6838 100644--- a/plugins/_browser/webui/browser-store.js+++ b/plugins/_browser/webui/browser-store.js@@ -2718,13 +2718,23 @@ const model = {}},- async startAnnotationVoice() {+ async startAnnotationVoice(draftComment = false) {try {const { store: whisperStore } = await import("/plugins/_whisper_stt/webui/whisper-stt-store.js");await whisperStore.handleMicrophoneClick(async (text, options = {}) => {- if (options.sendImmediately) {+ if (draftComment) {+ const transcript = String(text || "").trim();+ if (transcript && this.annotationDraft) {+ const existing = String(this.annotationDraftText || "").trim();+ this.annotationDraftText = existing ? `${existing}\n${transcript}` : transcript;+ if (options.sendImmediately) {+ this.addAnnotationComment();+ await this.sendAnnotationsToChat();+ }+ }+ } else if (options.sendImmediately) {await this.sendAnnotationsToChat(text);} else {this.draftAnnotationsToChat(text);diff --git a/tests/test_browser_agent_regressions.py b/tests/test_browser_agent_regressions.pyindex 36b5964bd..4bece6f9d 100644--- a/tests/test_browser_agent_regressions.py+++ b/tests/test_browser_agent_regressions.py@@ -395,6 +395,7 @@ def test_browser_launch_config_uses_full_chromium_for_all_sessions(tmp_path):assert default_launch["channel"] is Noneassert default_launch["requires_full_browser"] is Trueassert default_launch["proxy"] is None+ assert "--hide-crash-restore-bubble" in default_launch["args"]assert not any(arg.startswith("--load-extension=") for arg in default_launch["args"])assert "--no-sandbox" not in default_launch["args"]assert "--disable-dev-shm-usage" not in default_launch["args"]@@ -1783,12 +1784,13 @@ def test_browser_annotate_mode_ui_and_prompt_hooks():assert "Draft to chat" not in panel_htmlassert "Send now" not in panel_htmlassert 'class="browser-annotation-popover-close"' in panel_html- assert 'class="btn btn-ok browser-annotation-add"' in panel_html- assert 'class="browser-annotation-mic mic-inactive"' in panel_html+ assert panel_html.count('class="browser-annotation-mic mic-inactive"') == 2assert 'class="browser-annotation-send"' in panel_htmlassert 'name="arrow_forward"' in panel_html+ assert 'name="add_comment"' not in panel_htmlassert "data-whisper-microphone" in panel_htmlassert "syncAnnotationMicrophoneUI()" in panel_html+ assert "startAnnotationVoice(true)" in panel_htmlassert "@pointerdown.stop.prevent=\"$store.browserPage.startAnnotationSelection($event)\"" in panel_htmlassert "clearAnnotationHover()" in panel_htmlassert "@keydown.window=\"$store.browserPage.handleKeydown($event)\"" in panel_html@@ -1798,7 +1800,8 @@ def test_browser_annotate_mode_ui_and_prompt_hooks():assert "pendingAnnotations()" in browser_storeassert "annotationBatchLabel()" in browser_storeassert "updateAnnotationHover(event)" in browser_store- assert "startAnnotationVoice()" in browser_store+ assert "startAnnotationVoice(draftComment = false)" in browser_store+ assert "this.annotationDraftText = existing" in browser_storeassert "options.sendImmediately" in browser_storeassert "clampAnnotationTrayPosition" in browser_storeassert '"browser_viewer_annotation"' in browser_store@@ -3397,6 +3400,7 @@ async def test_browser_viewer_subscribe_without_runtime_does_not_create_runtime(return Nonemonkeypatch.setattr(ws_browser_module, "get_runtime", fake_get_runtime)+ monkeypatch.setattr(ws_browser_module, "has_restorable_browser_tabs", lambda context_id: False)monkeypatch.setattr(ws_browser_module, "get_browser_config", lambda: {"browser_tab_scope": "per_context"})monkeypatch.setattr(ws_browser_module.AgentContext,@@ -3421,6 +3425,49 @@ async def test_browser_viewer_subscribe_without_runtime_does_not_create_runtime(assert ("sid-empty", "ctx") not in ws_browser_module.WsBrowser._streams+async def test_browser_viewer_subscribe_starts_runtime_for_saved_tabs(monkeypatch):+ calls = []++ class FakeRuntime:+ async def call(self, method, *args, **kwargs):+ if method == "list":+ return {+ "browsers": [+ {"id": 4, "context_id": "ctx", "currentUrl": "https://example.com/"}+ ],+ "last_interacted_browser_id": 4,+ }+ if method == "interactive_viewer":+ return {"available": True, "browser_id": 4, "url": "/desktop/session/test/"}+ raise AssertionError(method)++ async def fake_get_runtime(context_id, create=True):+ calls.append(create)+ return FakeRuntime() if create else None++ monkeypatch.setattr(ws_browser_module, "get_runtime", fake_get_runtime)+ monkeypatch.setattr(ws_browser_module, "has_restorable_browser_tabs", lambda context_id: True)+ monkeypatch.setattr(ws_browser_module, "get_browser_config", lambda: {"browser_tab_scope": "per_context"})+ monkeypatch.setattr(+ ws_browser_module.AgentContext,+ "get",+ staticmethod(lambda context_id: SimpleNamespace(id=context_id)),+ )++ handler = ws_browser_module.WsBrowser(SimpleNamespace(), threading.RLock(), manager=None)+ result = await handler.process(+ "browser_viewer_subscribe",+ {"context_id": "ctx", "viewer_transport": "interactive"},+ "sid-restore",+ )++ assert calls[:2] == [False, True]+ assert result["active_browser_id"] == 4+ assert result["browsers"][0]["currentUrl"] == "https://example.com/"+ await handler.on_disconnect("sid-restore")++@pytest.mark.anyioasync def test_browser_runtime_sessions_are_context_qualified(monkeypatch):class FakeRuntime:@@ -3451,6 +3498,42 @@ async def test_browser_runtime_sessions_are_context_qualified(monkeypatch):]+async def test_shared_runtime_session_list_includes_restored_inactive_chats(monkeypatch):+ class FakeSharedRuntime:+ async def call_for(self, context_id, method):+ assert context_id == browser_runtime_module.SHARED_RUNTIME_ID+ assert method == "list_all"+ return {+ "browsers": [+ {"id": 1, "context_id": "ctx-a", "currentUrl": "https://example.com/"},+ {"id": 2, "context_id": "ctx-b", "currentUrl": "https://example.org/"},+ ],+ "last_interacted_browser_ids": {"ctx-a": 1, "ctx-b": 2},+ }++ monkeypatch.setattr(+ browser_runtime_module,+ "get_browser_config",+ lambda: {"browser_tab_scope": "shared"},+ )+ with browser_runtime_module._runtime_lock:+ previous_runtimes = dict(browser_runtime_module._runtimes)+ previous_shared = browser_runtime_module._shared_runtime+ browser_runtime_module._runtimes.clear()+ browser_runtime_module._shared_runtime = FakeSharedRuntime()+ try:+ sessions = await list_runtime_sessions()+ finally:+ with browser_runtime_module._runtime_lock:+ browser_runtime_module._runtimes.clear()+ browser_runtime_module._runtimes.update(previous_runtimes)+ browser_runtime_module._shared_runtime = previous_shared++ assert [session["context_id"] for session in sessions] == ["ctx-a", "ctx-b"]+ assert [session["last_interacted_browser_id"] for session in sessions] == [1, 2]++@pytest.mark.anyioasync def test_browser_context_handles_share_one_runtime(monkeypatch):class FakeSharedRuntime:@@ -3584,7 +3667,7 @@ def test_explicit_open_claims_page_registered_by_playwright_event():class Page:@staticmethoddef on(event, callback):- assert event == "close"+ assert event in {"close", "framenavigated"}core = _BrowserRuntimeCore(browser_runtime_module.SHARED_RUNTIME_ID)page = Page()@@ -3605,6 +3688,99 @@ def test_explicit_open_claims_page_registered_by_playwright_event():assert core._last_interacted_browser_ids.get("ctx-a") is None+async def test_browser_restores_per_context_then_all_shared_tabs(monkeypatch):+ saved = []+ browser_config = {"browser_tab_scope": "per_context", "max_open_tabs": 32}++ class Page:+ def __init__(self):+ self.url = "about:blank"++ @staticmethod+ def is_closed():+ return False++ @staticmethod+ def on(event, callback):+ assert event in {"close", "framenavigated"}++ class Context:+ async def new_page(self):+ return Page()++ async def goto(page, url, **kwargs):+ page.url = url++ monkeypatch.setattr(+ browser_runtime_module,+ "get_browser_config",+ lambda: browser_config,+ )+ monkeypatch.setattr(+ browser_runtime_module,+ "_save_browser_tabs",+ lambda tabs: saved.append(tabs),+ )++ core = _BrowserRuntimeCore(browser_runtime_module.SHARED_RUNTIME_ID)+ core.context = Context()+ core._restore_state_loaded = True+ core._restore_state_exists = True+ core._restore_entries = [+ {"context_id": "ctx-a", "url": "https://example.com/one", "active": True},+ {"context_id": "ctx-b", "url": "https://example.org/two", "active": True},+ ]+ monkeypatch.setattr(core, "_goto", goto)++ token = core.request_context_id.set("ctx-a")+ try:+ await core._restore_tabs_for_scope()+ finally:+ core.request_context_id.reset(token)++ assert [page.context_id for page in core.pages.values()] == ["ctx-a"]+ assert [entry["context_id"] for entry in saved[-1]] == ["ctx-b", "ctx-a"]++ browser_config["browser_tab_scope"] = "shared"+ token = core.request_context_id.set("ctx-a")+ try:+ await core._restore_tabs_for_scope()+ finally:+ core.request_context_id.reset(token)++ assert [page.context_id for page in core.pages.values()] == ["ctx-a", "ctx-b"]+ assert {entry["url"] for entry in saved[-1]} == {+ "https://example.com/one",+ "https://example.org/two",+ }+ assert core._restored_all is True+++def test_unexpected_browser_exit_keeps_last_saved_tabs(monkeypatch):+ saved = []+ restore_entries = [+ {"context_id": "ctx-a", "url": "https://example.com/", "active": True}+ ]+ monkeypatch.setattr(+ browser_runtime_module,+ "_save_browser_tabs",+ lambda tabs: saved.append(tabs),+ )++ core = _BrowserRuntimeCore(browser_runtime_module.SHARED_RUNTIME_ID)+ core.context = object()+ core._restore_state_loaded = True+ core._restore_entries = restore_entries+ core.pages = {1: BrowserPage(1, SimpleNamespace(url="https://example.com/"), "ctx-a")}++ core._on_context_closed()++ assert saved == []+ assert core._restore_entries == restore_entries+ assert core.pages == {}++def test_shared_browser_runtime_adopts_first_requesting_legacy_profile(monkeypatch, tmp_path):monkeypatch.setattr(browser_runtime_module.files,