Agent Zero AI framework
Use persistent full Chromium runtime for Browser
Alessandrofa7eef191990
diff --git a/docker/run/fs/ins/install_playwright.sh b/docker/run/fs/ins/install_playwright.shindex 43a2b3bfe..2d89d685c 100644--- a/docker/run/fs/ins/install_playwright.sh+++ b/docker/run/fs/ins/install_playwright.sh@@ -7,9 +7,10 @@ set -e# install playwright if not installed (should be from requirements.txt)uv pip install playwright-# set PW installation path to /a0/tmp/playwright-export PLAYWRIGHT_BROWSERS_PATH=/a0/tmp/playwright+# set PW installation path to persistent Browser plugin user storage+export PLAYWRIGHT_BROWSERS_PATH=/a0/usr/plugins/_browser/playwright+mkdir -p "$PLAYWRIGHT_BROWSERS_PATH"# install chromium with dependenciesapt-get install -y fonts-unifont libnss3 libnspr4 libatk1.0-0 libatspi2.0-0 libxcomposite1 libxdamage1 libatk-bridge2.0-0 libcups2-playwright install chromium --only-shell+playwright install chromiumdiff --git a/plugins/_browser/api/status.py b/plugins/_browser/api/status.pyindex 922a314d1..d39931da5 100644--- a/plugins/_browser/api/status.py+++ b/plugins/_browser/api/status.py@@ -1,6 +1,10 @@from helpers.api import ApiHandler, Requestfrom plugins._browser.helpers.config import build_browser_launch_config, get_browser_config-from plugins._browser.helpers.playwright import get_playwright_binary, get_playwright_cache_dir+from plugins._browser.helpers.playwright import (+ get_playwright_binary,+ get_playwright_cache_dir,+ get_playwright_cache_dirs,+)from plugins._browser.helpers.runtime import known_context_ids@@ -11,15 +15,15 @@ class Status(ApiHandler):runtime_binary = get_playwright_binary(full_browser=launch_config["requires_full_browser"])- shell_binary = get_playwright_binary(full_browser=False)chromium_binary = get_playwright_binary(full_browser=True)return {"plugin": "_browser","playwright": {"cache_dir": get_playwright_cache_dir(),+ "cache_dirs": [str(path) for path in get_playwright_cache_dirs()],"binary_found": bool(runtime_binary),+ "install_required": not bool(runtime_binary),"binary_path": str(runtime_binary) if runtime_binary else "",- "headless_shell_binary_path": str(shell_binary) if shell_binary else "","chromium_binary_path": str(chromium_binary) if chromium_binary else "","launch_mode": launch_config["browser_mode"],},diff --git a/plugins/_browser/helpers/config.py b/plugins/_browser/helpers/config.pyindex a996da098..c8592194f 100644--- a/plugins/_browser/helpers/config.py+++ b/plugins/_browser/helpers/config.py@@ -248,7 +248,7 @@ def build_browser_launch_config(settings: dict[str, Any] | None) -> dict[str, Anextensions = describe_browser_extensions(settings)args = list(BASE_BROWSER_ARGS)channel: str | None = None- browser_mode = "headless_shell"+ browser_mode = "chromium"if extensions["active"]:joined_paths = ",".join(extensions["active_paths"])@@ -258,15 +258,11 @@ def build_browser_launch_config(settings: dict[str, Any] | None) -> dict[str, Anf"--load-extension={joined_paths}",])- channel = "chromium"- browser_mode = "chromium_extensions"- else:- args.insert(0, "--headless=new")return {"args": args,"browser_mode": browser_mode,"channel": channel,"extensions": extensions,- "requires_full_browser": bool(extensions["active"]),+ "requires_full_browser": True,}diff --git a/plugins/_browser/helpers/extension_manager.py b/plugins/_browser/helpers/extension_manager.pyindex d56a428b5..5f15b6fa7 100644--- a/plugins/_browser/helpers/extension_manager.py+++ b/plugins/_browser/helpers/extension_manager.py@@ -16,6 +16,8 @@ from helpers import files, pluginsfrom plugins._browser.helpers.config import PLUGIN_NAME, get_browser_config+EXTENSIONS_ROOT_DIR = ("usr", "plugins", PLUGIN_NAME, "extensions")+LEGACY_EXTENSIONS_ROOT_DIR = ("usr", "browser-extensions")EXTENSION_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})")@@ -36,11 +38,27 @@ WEB_STORE_DOWNLOAD_URL = (def get_extensions_root() -> Path:- root = Path(files.get_abs_path("usr/browser-extensions"))+ root = Path(files.get_abs_path(*EXTENSIONS_ROOT_DIR))root.mkdir(parents=True, exist_ok=True)return root+def get_extension_roots() -> list[Path]:+ roots = [+ get_extensions_root(),+ Path(files.get_abs_path(*LEGACY_EXTENSIONS_ROOT_DIR)),+ ]+ unique: list[Path] = []+ seen: set[str] = set()+ for root in roots:+ key = str(root)+ if key in seen:+ continue+ seen.add(key)+ unique.append(root)+ return unique++def parse_chrome_web_store_extension_id(value: str) -> str:source = str(value or "").strip()if EXTENSION_ID_RE.fullmatch(source):@@ -54,26 +72,28 @@ def parse_chrome_web_store_extension_id(value: str) -> str:def list_browser_extensions() -> list[dict[str, Any]]:- root = get_extensions_root()config = get_browser_config()enabled_paths = {str(Path(path).expanduser()) for path in config["extension_paths"]}entries: list[dict[str, Any]] = []- for manifest_path in sorted(root.glob("**/manifest.json")):- extension_dir = manifest_path.parent- try:- manifest = json.loads(manifest_path.read_text(encoding="utf-8"))- except Exception:- manifest = {}- extension_path = str(extension_dir)- entries.append(- {- "name": manifest.get("name") or extension_dir.name,- "version": manifest.get("version") or "",- "path": extension_path,- "enabled": extension_path in enabled_paths,- }- )+ for root in get_extension_roots():+ if not root.exists():+ continue+ for manifest_path in sorted(root.glob("**/manifest.json")):+ extension_dir = manifest_path.parent+ try:+ manifest = json.loads(manifest_path.read_text(encoding="utf-8"))+ except Exception:+ manifest = {}+ extension_path = str(extension_dir)+ entries.append(+ {+ "name": manifest.get("name") or extension_dir.name,+ "version": manifest.get("version") or "",+ "path": extension_path,+ "enabled": extension_path in enabled_paths,+ }+ )return entriesdiff --git a/plugins/_browser/helpers/playwright.py b/plugins/_browser/helpers/playwright.pyindex 59c79b059..a0d6bce07 100644--- a/plugins/_browser/helpers/playwright.py+++ b/plugins/_browser/helpers/playwright.py@@ -4,34 +4,58 @@ from pathlib import Pathfrom helpers import files-HEADLESS_SHELL_PATTERNS = (- "chromium_headless_shell-*/chrome-*/headless_shell",- "chromium_headless_shell-*/chrome-*/headless_shell.exe",-)-FULL_CHROMIUM_PATTERNS = ("chromium-*/chrome-linux/chrome","chromium-*/chrome-win/chrome.exe",)+PLAYWRIGHT_CACHE_ENV = "A0_BROWSER_PLAYWRIGHT_CACHE_DIR"+PLAYWRIGHT_CACHE_DIR = ("usr", "plugins", "_browser", "playwright")+PREVIOUS_PLAYWRIGHT_CACHE_DIR = ("usr", "browser", "playwright")+LEGACY_PLAYWRIGHT_CACHE_DIR = ("tmp", "playwright")+++def _primary_cache_dir() -> Path:+ override = os.environ.get(PLAYWRIGHT_CACHE_ENV, "").strip()+ if override:+ return Path(override).expanduser()+ return Path(files.get_abs_path(*PLAYWRIGHT_CACHE_DIR))def get_playwright_cache_dir() -> str:- return files.get_abs_path("tmp/playwright")+ return str(_primary_cache_dir())+++def get_playwright_cache_dirs() -> list[Path]:+ primary = _primary_cache_dir()+ candidates = [+ primary,+ Path(files.get_abs_path(*PREVIOUS_PLAYWRIGHT_CACHE_DIR)),+ Path(files.get_abs_path(*LEGACY_PLAYWRIGHT_CACHE_DIR)),+ ]+ seen: set[str] = set()+ unique: list[Path] = []+ for candidate in candidates:+ key = str(candidate)+ if key in seen:+ continue+ seen.add(key)+ unique.append(candidate)+ return uniquedef configure_playwright_env() -> str:cache_dir = get_playwright_cache_dir()+ Path(cache_dir).mkdir(parents=True, exist_ok=True)os.environ["PLAYWRIGHT_BROWSERS_PATH"] = cache_dirreturn cache_dirdef get_playwright_binary(*, full_browser: bool = False) -> Path | None:- cache_dir = Path(get_playwright_cache_dir())- patterns = FULL_CHROMIUM_PATTERNS if full_browser else (HEADLESS_SHELL_PATTERNS + FULL_CHROMIUM_PATTERNS)- for pattern in patterns:- binary = next(cache_dir.glob(pattern), None)- if binary and binary.exists():- return binary+ for cache_dir in get_playwright_cache_dirs():+ for pattern in FULL_CHROMIUM_PATTERNS:+ binary = next(cache_dir.glob(pattern), None)+ if binary and binary.exists():+ return binaryreturn None@@ -44,8 +68,6 @@ def ensure_playwright_binary(*, full_browser: bool = False) -> Path:env = os.environ.copy()env["PLAYWRIGHT_BROWSERS_PATH"] = cache_dirinstall_command = ["playwright", "install", "chromium"]- if not full_browser:- install_command.append("--only-shell")subprocess.check_call(install_command,env=env,diff --git a/plugins/_browser/webui/browser-store.js b/plugins/_browser/webui/browser-store.jsindex 0b81c436d..64db947c8 100644--- a/plugins/_browser/webui/browser-store.js+++ b/plugins/_browser/webui/browser-store.js@@ -8,8 +8,9 @@ import { store as pluginSettingsStore } from "/components/plugins/plugin-settingconst websocket = getNamespacedClient("/ws");websocket.addHandlers(["ws_webui"]);-const EXTENSIONS_ROOT_FALLBACK = "/a0/usr/browser-extensions";+const EXTENSIONS_ROOT_FALLBACK = "/a0/usr/plugins/_browser/extensions";const BROWSER_SUBSCRIBE_TIMEOUT_MS = 60000;+const BROWSER_FIRST_INSTALL_TIMEOUT_MS = 300000;function firstOk(response) {const result = response?.results?.find((item) => item?.ok);@@ -52,9 +53,11 @@ const model = {extensionActionError: "",extensionsRoot: "",extensionsList: [],+ browserInstallExpected: false,async refreshStatus() {this.status = await callJsonApi("/plugins/_browser/status", {});+ this.browserInstallExpected = Boolean(this.status?.playwright?.install_required);},async refreshExtensionsList() {@@ -216,12 +219,17 @@ const model = {context_id: this.contextId,browser_id: this.activeBrowserId,},- { timeoutMs: BROWSER_SUBSCRIBE_TIMEOUT_MS },+ {+ timeoutMs: this.browserInstallExpected+ ? BROWSER_FIRST_INSTALL_TIMEOUT_MS+ : BROWSER_SUBSCRIBE_TIMEOUT_MS,+ },);const data = firstOk(response);this.browsers = data.browsers || [];this.setActiveBrowserId(data.active_browser_id || this.activeBrowserId || null);this.connected = true;+ this.browserInstallExpected = false;this.queueViewportSync(true);},@@ -592,6 +600,14 @@ const model = {get activeUrl() {return this.frameState?.currentUrl || this.address || "about:blank";},++ loadingMessage() {+ if (this.browserInstallExpected) {+ const cacheDir = this.status?.playwright?.cache_dir || "/a0/usr/plugins/_browser/playwright";+ return `Installing Chromium for the first Browser run. This can take a few minutes; future starts reuse ${cacheDir}.`;+ }+ return "Connecting browser...";+ },};export const store = createStore("browserPage", model);diff --git a/plugins/_browser/webui/config.html b/plugins/_browser/webui/config.htmlindex 4c6a54ee7..a42051f44 100644--- a/plugins/_browser/webui/config.html+++ b/plugins/_browser/webui/config.html@@ -64,8 +64,8 @@<div class="section-title">Chrome Extensions</div><div class="section-description">Load unpacked Chromium extensions into the Browser tool. When extensions are active,- Browser switches from Playwright's lightweight headless shell to bundled Chromium so- the extensions can actually load.+ Browser loads them into the same persistent full Chromium runtime used for every+ Browser session.</div><div class="browser-config-warning">@@ -106,7 +106,7 @@:value="$store.browserConfig.extensionPathsText"@input="$store.browserConfig.setExtensionPathsText($event.target.value)"rows="6"- placeholder="/a0/usr/browser-extensions/my-extension"+ placeholder="/a0/usr/plugins/_browser/extensions/my-extension"></textarea></div></div>@@ -115,16 +115,15 @@<span class="material-symbols-outlined">info</span><span>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.+ /a0/usr/plugins/_browser/extensions/chrome-web-store, then loaded from the directory list above.</span></div><div class="browser-config-note"><span class="material-symbols-outlined">deployed_code</span><span>- Playwright currently requires a persistent Chromium context for extension loading, so- Browser stays in its faster headless-shell mode until valid extension folders are both- configured and enabled.+ Browser caches Playwright Chromium under /a0/usr/plugins/_browser/playwright. The first Browser+ run can take a few minutes while Chromium is installed; later starts reuse that cache.</span></div>@@ -196,6 +195,18 @@font-size: 20px;}+ .browser-config-sections .spinning {+ display: inline-block;+ transform-origin: center;+ animation: browser-config-spin 0.8s linear infinite;+ }++ @keyframes browser-config-spin {+ to {+ transform: rotate(360deg);+ }+ }+.browser-config-pill-row {display: flex;flex-wrap: wrap;diff --git a/plugins/_browser/webui/main.html b/plugins/_browser/webui/main.htmlindex 8fd4b5156..f752e819c 100644--- a/plugins/_browser/webui/main.html+++ b/plugins/_browser/webui/main.html@@ -79,7 +79,11 @@@click="$store.browserPage.installExtensionFromUrl()":disabled="$store.browserPage.extensionActionLoading">- <span class="material-symbols-outlined" x-text="$store.browserPage.extensionActionLoading ? 'progress_activity' : 'download'"></span>+ <span+ class="material-symbols-outlined"+ :class="{ spinning: $store.browserPage.extensionActionLoading }"+ x-text="$store.browserPage.extensionActionLoading ? 'progress_activity' : 'download'"+ ></span><span>Install URL</span></button><button type="button" class="btn btn-field" @click="$store.browserPage.askAgentInstallExtension()">@@ -135,7 +139,7 @@<div class="browser-status" x-show="$store.browserPage.loading"><span class="material-symbols-outlined spinning">progress_activity</span>- <span>Connecting browser...</span>+ <span x-text="$store.browserPage.loadingMessage()">Connecting browser...</span></div><div class="browser-error" x-show="$store.browserPage.error" x-text="$store.browserPage.error"></div>@@ -509,6 +513,19 @@padding: 0 12px;}+ .browser-modal .spinning,+ .browser-panel .spinning {+ display: inline-block;+ transform-origin: center;+ animation: browser-spin 0.8s linear infinite;+ }++ @keyframes browser-spin {+ to {+ transform: rotate(360deg);+ }+ }+.browser-error {color: #9f1239;}diff --git a/tests/test_browser_agent_regressions.py b/tests/test_browser_agent_regressions.pyindex 03dc67a42..d95b28bdd 100644--- a/tests/test_browser_agent_regressions.py+++ b/tests/test_browser_agent_regressions.py@@ -21,10 +21,18 @@ from plugins._browser.helpers.extension_manager import (_crx_zip_payload,_detect_chrome_prodversion,_normalize_chrome_prodversion,+ get_extension_roots,+ get_extensions_root,parse_chrome_web_store_extension_id,)+import plugins._browser.helpers.extension_manager as browser_extension_manager_modulefrom plugins._browser.helpers.runtime import _BrowserRuntimeCore, normalize_urlimport plugins._browser.helpers.runtime as browser_runtime_module+from plugins._browser.helpers.playwright import (+ get_playwright_binary,+ get_playwright_cache_dir,+)+import plugins._browser.helpers.playwright as browser_playwright_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@@ -117,7 +125,20 @@ def test_browser_model_preset_options_include_missing_selected(monkeypatch):assert options[-1]["missing"] is True-def test_browser_launch_config_switches_to_chromium_for_extensions(tmp_path):+def test_browser_launch_config_uses_full_chromium_for_all_sessions(tmp_path):+ default_launch = build_browser_launch_config(+ {+ "extensions_enabled": False,+ "extension_paths": [],+ }+ )++ assert default_launch["browser_mode"] == "chromium"+ assert default_launch["channel"] is None+ assert default_launch["requires_full_browser"] is True+ assert not any(arg.startswith("--load-extension=") for arg in default_launch["args"])+ assert "--headless=new" not in default_launch["args"]+extension_dir = tmp_path / "extension"extension_dir.mkdir()@@ -128,14 +149,52 @@ def test_browser_launch_config_switches_to_chromium_for_extensions(tmp_path):})- assert launch["browser_mode"] == "chromium_extensions"- assert launch["channel"] == "chromium"+ assert launch["browser_mode"] == "chromium"+ assert launch["channel"] is Noneassert launch["requires_full_browser"] is Trueassert launch["extensions"]["active"] is Trueassert any(arg.startswith("--load-extension=") for arg in launch["args"])assert "--headless=new" not in launch["args"]+def test_browser_playwright_cache_uses_persistent_usr_path(monkeypatch, tmp_path):+ monkeypatch.delenv("A0_BROWSER_PLAYWRIGHT_CACHE_DIR", raising=False)+ monkeypatch.setattr(+ browser_playwright_module.files,+ "get_abs_path",+ lambda *parts: str(tmp_path.joinpath(*parts)),+ )+ legacy_binary = (+ tmp_path+ / "tmp"+ / "playwright"+ / "chromium-1169"+ / "chrome-linux"+ / "chrome"+ )+ legacy_binary.parent.mkdir(parents=True)+ legacy_binary.write_text("#!/bin/sh\n", encoding="utf-8")++ assert get_playwright_cache_dir() == str(+ tmp_path / "usr" / "plugins" / "_browser" / "playwright"+ )+ assert get_playwright_binary() == legacy_binary+++def test_browser_extension_storage_uses_plugin_user_path(monkeypatch, tmp_path):+ monkeypatch.setattr(+ browser_extension_manager_module.files,+ "get_abs_path",+ lambda *parts: str(tmp_path.joinpath(*parts)),+ )++ assert get_extensions_root() == tmp_path / "usr" / "plugins" / "_browser" / "extensions"+ assert get_extension_roots() == [+ tmp_path / "usr" / "plugins" / "_browser" / "extensions",+ tmp_path / "usr" / "browser-extensions",+ ]++def test_browser_extension_manager_parses_web_store_urls():extension_id = "a" * 32@@ -196,7 +255,33 @@ def test_browser_viewer_allows_slow_extension_startup():)assert "const BROWSER_SUBSCRIBE_TIMEOUT_MS = 60000;" in js- assert "{ timeoutMs: BROWSER_SUBSCRIBE_TIMEOUT_MS }" in js+ assert "const BROWSER_FIRST_INSTALL_TIMEOUT_MS = 300000;" in js+ assert "? BROWSER_FIRST_INSTALL_TIMEOUT_MS" in js+ assert ": BROWSER_SUBSCRIBE_TIMEOUT_MS" in js+ assert "Installing Chromium for the first Browser run" in js+++def test_browser_ui_spinners_have_browser_local_animation():+ main_html = (PROJECT_ROOT / "plugins" / "_browser" / "webui" / "main.html").read_text(+ encoding="utf-8"+ )+ config_html = (PROJECT_ROOT / "plugins" / "_browser" / "webui" / "config.html").read_text(+ encoding="utf-8"+ )++ assert ":class=\"{ spinning: $store.browserPage.extensionActionLoading }\"" in main_html+ assert "@keyframes browser-spin" in main_html+ assert "@keyframes browser-config-spin" in config_html+++def test_browser_docker_installs_full_chromium_to_persistent_cache():+ script = (+ PROJECT_ROOT / "docker" / "run" / "fs" / "ins" / "install_playwright.sh"+ ).read_text(encoding="utf-8")++ assert "PLAYWRIGHT_BROWSERS_PATH=/a0/usr/plugins/_browser/playwright" in script+ assert "playwright install chromium" in script+ assert "--only-shell" not in scriptdef test_browser_runtime_removes_stale_profile_singletons(monkeypatch, tmp_path):