Agent Zero AI framework
Fix Chrome extension install and Browser startup with extensions
Alessandrofb98c2f89a10
diff --git a/plugins/_browser/helpers/extension_manager.py b/plugins/_browser/helpers/extension_manager.pyindex 609fd630e..d56a428b5 100644--- a/plugins/_browser/helpers/extension_manager.py+++ b/plugins/_browser/helpers/extension_manager.py@@ -1,9 +1,12 @@from __future__ import annotationsimport json+import osimport reimport shutil+import subprocessimport tempfile+import urllib.errorimport urllib.requestimport zipfilefrom pathlib import Path@@ -15,10 +18,18 @@ from plugins._browser.helpers.config import PLUGIN_NAME, get_browser_configEXTENSION_ID_RE = re.compile(r"^[a-p]{32}$")WEB_STORE_ID_RE = re.compile(r"(?<![a-p])([a-p]{32})(?![a-p])")+CHROME_VERSION_RE = re.compile(r"(\d+(?:\.\d+){0,3})")+DEFAULT_CHROME_PRODVERSION = "140.0.0.0"+CHROME_VERSION_COMMANDS = (+ ("google-chrome", "--version"),+ ("chromium", "--version"),+ ("chromium-browser", "--version"),+)WEB_STORE_DOWNLOAD_URL = ("https://clients2.google.com/service/update2/crx""?response=redirect"- "&prodversion=120.0.0.0"+ "&prod=chromecrx"+ "&prodversion={prodversion}""&acceptformat=crx2,crx3""&x=id%3D{extension_id}%26installsource%3Dondemand%26uc")@@ -101,23 +112,79 @@ def install_chrome_web_store_extension(source: str) -> dict[str, Any]:def _download_crx(extension_id: str, archive_path: Path) -> None:- url = WEB_STORE_DOWNLOAD_URL.format(extension_id=extension_id)+ prodversion = _detect_chrome_prodversion()+ url = _build_web_store_download_url(extension_id, prodversion=prodversion)request = urllib.request.Request(url,headers={"User-Agent": ("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 "- "(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"+ f"(KHTML, like Gecko) Chrome/{prodversion} Safari/537.36")},)- with urllib.request.urlopen(request, timeout=30) as response:+ try:+ response = urllib.request.urlopen(request, timeout=120)+ except urllib.error.HTTPError as exc:+ raise ValueError(+ f"Chrome Web Store download failed with HTTP {exc.code} for Chrome {prodversion}."+ ) from exc+ except urllib.error.URLError as exc:+ reason = getattr(exc, "reason", exc)+ raise ValueError(f"Chrome Web Store download failed: {reason}.") from exc++ with response:+ status = response.getcode()data = response.read()if not data:- raise ValueError("Chrome Web Store returned an empty extension package.")+ raise ValueError(+ "Chrome Web Store did not return an extension package "+ f"(HTTP {status}, Chrome {prodversion})."+ )archive_path.write_bytes(data)+def _build_web_store_download_url(extension_id: str, *, prodversion: str | None = None) -> str:+ return WEB_STORE_DOWNLOAD_URL.format(+ extension_id=extension_id,+ prodversion=_normalize_chrome_prodversion(prodversion or "") or DEFAULT_CHROME_PRODVERSION,+ )+++def _detect_chrome_prodversion() -> str:+ env_version = _normalize_chrome_prodversion(os.environ.get("A0_BROWSER_EXTENSION_PRODVERSION", ""))+ if env_version:+ return env_version++ for command in CHROME_VERSION_COMMANDS:+ try:+ completed = subprocess.run(+ command,+ check=False,+ capture_output=True,+ text=True,+ timeout=5,+ )+ except (OSError, subprocess.TimeoutExpired):+ continue++ version = _normalize_chrome_prodversion(+ " ".join(part for part in (completed.stdout, completed.stderr) if part)+ )+ if version:+ return version++ return DEFAULT_CHROME_PRODVERSION+++def _normalize_chrome_prodversion(value: str) -> str:+ match = CHROME_VERSION_RE.search(str(value or ""))+ if not match:+ return ""+ parts = match.group(1).split(".")+ return ".".join((parts + ["0", "0", "0", "0"])[:4])++def _crx_zip_payload(data: bytes) -> bytes:if data.startswith(b"PK"):return datadiff --git a/plugins/_browser/helpers/runtime.py b/plugins/_browser/helpers/runtime.pyindex 99b777dde..a54f13908 100644--- a/plugins/_browser/helpers/runtime.py+++ b/plugins/_browser/helpers/runtime.py@@ -3,9 +3,12 @@ from __future__ import annotationsimport atexitimport asyncioimport base64+import osimport reimport shutil+import signalimport threading+import timefrom dataclasses import dataclassfrom pathlib import Pathfrom typing import Any@@ -23,6 +26,7 @@ PLUGIN_DIR = Path(__file__).resolve().parents[1]CONTENT_HELPER_PATH = PLUGIN_DIR / "assets" / "browser-page-content.js"RUNTIME_DATA_KEY = "_browser_runtime"DEFAULT_VIEWPORT = {"width": 1024, "height": 768}+CHROME_SINGLETON_FILES = ("SingletonLock", "SingletonCookie", "SingletonSocket")_SPECIAL_SCHEME_RE = re.compile(r"^(?:about|blob|data|file|mailto|tel):", re.I)_URL_SCHEME_RE = re.compile(r"^[a-z][a-z\d+\-.]*://", re.I)@@ -117,6 +121,7 @@ class _BrowserRuntimeCore:self.next_browser_id = 1self.last_interacted_browser_id: int | None = Noneself._content_helper_source: str | None = None+ self._start_lock: asyncio.Lock | None = None@propertydef profile_dir(self) -> Path:@@ -130,10 +135,20 @@ class _BrowserRuntimeCore:if self.context:return+ if self._start_lock is None:+ self._start_lock = asyncio.Lock()++ async with self._start_lock:+ if self.context:+ return+ await self._start()++ async def _start(self) -> None:from playwright.async_api import async_playwrightself.profile_dir.mkdir(parents=True, exist_ok=True)self.downloads_dir.mkdir(parents=True, exist_ok=True)+ self._release_orphaned_profile_singleton()browser_config = get_browser_config()launch_config = build_browser_launch_config(browser_config)configure_playwright_env()@@ -156,9 +171,18 @@ class _BrowserRuntimeCore:launch_kwargs["channel"] = launch_config["channel"]else:launch_kwargs["executable_path"] = str(browser_binary)- self.context = await self.playwright.chromium.launch_persistent_context(- **launch_kwargs- )+ try:+ self.context = await self.playwright.chromium.launch_persistent_context(+ **launch_kwargs+ )+ except Exception:+ if self.playwright:+ try:+ await self.playwright.stop()+ except Exception:+ pass+ self.playwright = None+ raiseself.context.set_default_timeout(30000)self.context.set_default_navigation_timeout(30000)await self.context.add_init_script(self._shadow_dom_script())@@ -173,6 +197,66 @@ class _BrowserRuntimeCore:continueself._register_page(page)+ def _release_orphaned_profile_singleton(self) -> None:+ lock_path = self.profile_dir / "SingletonLock"+ owner_pid = self._profile_singleton_owner_pid(lock_path)+ if owner_pid and self._process_owns_profile(owner_pid):+ PrintStyle.warning(+ f"Stopping orphaned Chromium process {owner_pid} for Browser profile {self.safe_context_id}."+ )+ self._terminate_process(owner_pid)++ for name in CHROME_SINGLETON_FILES:+ singleton_path = self.profile_dir / name+ try:+ if singleton_path.exists() or singleton_path.is_symlink():+ singleton_path.unlink()+ except OSError as exc:+ PrintStyle.warning(f"Could not remove stale Browser profile lock {singleton_path}: {exc}")++ @staticmethod+ def _profile_singleton_owner_pid(lock_path: Path) -> int | None:+ try:+ target = os.readlink(lock_path)+ except OSError:+ return None+ raw_pid = target.rsplit("-", 1)[-1]+ if not raw_pid.isdigit():+ return None+ return int(raw_pid)++ def _process_owns_profile(self, pid: int) -> bool:+ cmdline_path = Path("/proc") / str(pid) / "cmdline"+ try:+ raw = cmdline_path.read_bytes()+ except OSError:+ return False+ cmdline = raw.replace(b"\x00", b" ").decode("utf-8", errors="ignore")+ return "chrome" in cmdline.lower() and str(self.profile_dir) in cmdline++ @staticmethod+ def _terminate_process(pid: int) -> None:+ try:+ os.kill(pid, signal.SIGTERM)+ except ProcessLookupError:+ return+ except OSError as exc:+ PrintStyle.warning(f"Could not stop orphaned Chromium process {pid}: {exc}")+ return++ deadline = time.monotonic() + 3+ while time.monotonic() < deadline:+ if not Path("/proc", str(pid)).exists():+ return+ time.sleep(0.1)++ try:+ os.kill(pid, signal.SIGKILL)+ except ProcessLookupError:+ pass+ except OSError as exc:+ PrintStyle.warning(f"Could not force-stop orphaned Chromium process {pid}: {exc}")+async def open(self, url: str = "about:blank") -> dict[str, Any]:await self.ensure_started()page = await self.context.new_page()diff --git a/plugins/_browser/webui/browser-store.js b/plugins/_browser/webui/browser-store.jsindex 3184403fd..0b81c436d 100644--- a/plugins/_browser/webui/browser-store.js+++ b/plugins/_browser/webui/browser-store.js@@ -9,6 +9,7 @@ const websocket = getNamespacedClient("/ws");websocket.addHandlers(["ws_webui"]);const EXTENSIONS_ROOT_FALLBACK = "/a0/usr/browser-extensions";+const BROWSER_SUBSCRIBE_TIMEOUT_MS = 60000;function firstOk(response) {const result = response?.results?.find((item) => item?.ok);@@ -215,7 +216,7 @@ const model = {context_id: this.contextId,browser_id: this.activeBrowserId,},- { timeoutMs: 10000 },+ { timeoutMs: BROWSER_SUBSCRIBE_TIMEOUT_MS },);const data = firstOk(response);this.browsers = data.browsers || [];diff --git a/plugins/_browser/webui/config.html b/plugins/_browser/webui/config.htmlindex 7647e79be..4c6a54ee7 100644--- a/plugins/_browser/webui/config.html+++ b/plugins/_browser/webui/config.html@@ -114,8 +114,8 @@<div class="browser-config-note"><span class="material-symbols-outlined">info</span><span>- This first version supports unpacked extension folders only. Chrome Web Store installs- and `.crx` files are out of scope for now.+ Chrome Web Store URL installs are converted into unpacked folders under+ /a0/usr/browser-extensions/chrome-web-store, then loaded from the directory list above.</span></div>diff --git a/tests/test_browser_agent_regressions.py b/tests/test_browser_agent_regressions.pyindex 01aa32417..03dc67a42 100644--- a/tests/test_browser_agent_regressions.py+++ b/tests/test_browser_agent_regressions.py@@ -17,10 +17,14 @@ from plugins._browser.helpers.config import (resolve_browser_model_selection,)from plugins._browser.helpers.extension_manager import (+ _build_web_store_download_url,_crx_zip_payload,+ _detect_chrome_prodversion,+ _normalize_chrome_prodversion,parse_chrome_web_store_extension_id,)-from plugins._browser.helpers.runtime import normalize_url+from plugins._browser.helpers.runtime import _BrowserRuntimeCore, normalize_url+import plugins._browser.helpers.runtime as browser_runtime_moduleimport plugins._browser.hooks as browser_hooks_moduleimport plugins._browser.tools.browser as browser_tool_moduleimport plugins._browser.api.ws_browser as ws_browser_module@@ -158,6 +162,21 @@ def test_browser_extension_manager_extracts_crx3_zip_payload():assert _crx_zip_payload(crx) == payload+def test_browser_extension_manager_uses_modern_chrome_prodversion(monkeypatch):+ extension_id = "a" * 32++ assert _normalize_chrome_prodversion("Google Chrome 147.0.7727.55") == "147.0.7727.55"+ assert _normalize_chrome_prodversion("Chromium 124") == "124.0.0.0"++ monkeypatch.setenv("A0_BROWSER_EXTENSION_PRODVERSION", "147.0.7727.55")+ assert _detect_chrome_prodversion() == "147.0.7727.55"++ url = _build_web_store_download_url(extension_id, prodversion=_detect_chrome_prodversion())+ assert "prod=chromecrx" in url+ assert "prodversion=147.0.7727.55" in url+ assert "prodversion=120.0.0.0" not in url++def test_browser_extension_menu_exposes_agent_and_url_paths():html = (PROJECT_ROOT / "plugins" / "_browser" / "webui" / "main.html").read_text(encoding="utf-8"@@ -171,6 +190,35 @@ def test_browser_extension_menu_exposes_agent_and_url_paths():assert skill.exists()+def test_browser_viewer_allows_slow_extension_startup():+ js = (PROJECT_ROOT / "plugins" / "_browser" / "webui" / "browser-store.js").read_text(+ encoding="utf-8"+ )++ assert "const BROWSER_SUBSCRIBE_TIMEOUT_MS = 60000;" in js+ assert "{ timeoutMs: BROWSER_SUBSCRIBE_TIMEOUT_MS }" in js+++def test_browser_runtime_removes_stale_profile_singletons(monkeypatch, tmp_path):+ monkeypatch.setattr(+ browser_runtime_module.files,+ "get_abs_path",+ lambda *parts: str(tmp_path.joinpath(*parts)),+ )+ core = _BrowserRuntimeCore("stale-profile")+ core.profile_dir.mkdir(parents=True)++ for name in ("SingletonLock", "SingletonCookie", "SingletonSocket"):+ (core.profile_dir / name).symlink_to("missing-host-999999")++ core._release_orphaned_profile_singleton()++ assert not any(+ (core.profile_dir / name).exists() or (core.profile_dir / name).is_symlink()+ for name in ("SingletonLock", "SingletonCookie", "SingletonSocket")+ )++def test_browser_save_plugin_config_restarts_runtimes_on_change(monkeypatch, tmp_path):extension_dir = tmp_path / "extension"extension_dir.mkdir()