Skip to content

Self-improving AI Employees for everyone

refactor: invoke agent-browser via npx --no-install

trohitg5204a51a3492
diff --git a/CLAUDE.md b/CLAUDE.md
index 8856eb10..b2a8f80a 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -1293,9 +1293,9 @@ Interactive browser automation via the [agent-browser](https://www.npmjs.com/pac
| `server/services/handlers/browser.py` | Handler dispatcher mapping operation+params to agent-browser CLI args |
| `server/skills/web_agent/browser-skill/SKILL.md` | AI agent skill documenting snapshot-act-snapshot loop |
-**Installation Requirement:** The `agent-browser` CLI must be installed: `npm install -g agent-browser && agent-browser install`. The handler returns an installation instruction error if the binary is missing.
+**Installation Requirement:** `agent-browser` is a pinned project dependency in [package.json](../package.json). `pnpm install` places it under `node_modules/.pnpm/`, and `scripts/install.js` runs `npx agent-browser install` during postinstall to download the Chromium runtime. The handler returns an installation-instruction error if the binary cannot be located.
-**Implementation Detail:** `browser_service.py` reads only the **first JSON output line** from agent-browser (which runs a persistent daemon keeping stdout open), then immediately kills the subprocess. Output is truncated at 100KB with ellipsis. Windows uses `shell=True` for the `.CMD` wrapper.
+**Implementation Detail:** `browser_service.py` invokes the binary via `[shutil.which("npx"), "--no-install", "agent-browser", ...args]` -- the same pattern used by [claude_code_service.py](../server/services/claude_code_service.py). This avoids custom cross-platform shim detection: `npx --no-install` handles local `node_modules/.bin/` resolution, Windows/POSIX shim differences, and shebang interpretation internally. The service reads only the first JSON output line from agent-browser (the daemon holds stdout open), truncates at 100KB, and kills the process tree via `psutil`. All subprocess calls use `shell=False` with list argv to avoid BatBadBut (CVE-2024-1874) on Windows.
---
diff --git a/scripts/install.js b/scripts/install.js
index 2e01abb7..cb595eb0 100755
--- a/scripts/install.js
+++ b/scripts/install.js
@@ -133,20 +133,18 @@ if (temporalVersion) {
}
}
-// Check/Install agent-browser (global CLI for browser automation)
-let agentBrowserVersion = getVersion('agent-browser --version');
+// agent-browser is a project dependency (see package.json); pnpm/npm
+// already placed it in node_modules/.pnpm/ before this postinstall ran.
+// Download its Chromium runtime once via `npx agent-browser install`.
+// This is version-pinned via the lockfile; no global state.
+const agentBrowserVersion = getVersion('npx --no-install agent-browser --version');
if (agentBrowserVersion) {
- console.log(` agent-browser: ${agentBrowserVersion}`);
-} else {
- console.log(' agent-browser: not found, installing...');
- run('npm install -g agent-browser');
- agentBrowserVersion = getVersion('agent-browser --version');
- if (agentBrowserVersion) {
- console.log(` agent-browser: ${agentBrowserVersion}`);
- run('agent-browser install');
- } else {
- console.log(' Warning: agent-browser install failed. Browser automation unavailable.');
+ console.log(` agent-browser: ${agentBrowserVersion} (local)`);
+ if (!runSilent('npx --no-install agent-browser install')) {
+ console.log(' Warning: agent-browser runtime install failed. Browser automation may be unavailable.');
}
+} else {
+ console.log(' Warning: agent-browser not found in node_modules. Run `pnpm install` first.');
}
console.log('');
diff --git a/server/config/model_registry.json b/server/config/model_registry.json
index a54d7fc9..d3df9c59 100644
--- a/server/config/model_registry.json
+++ b/server/config/model_registry.json
@@ -1,7 +1,7 @@
{
- "_generated": "2026-04-09T16:14:16.145470+00:00",
+ "_generated": "2026-04-10T20:32:20.583411+00:00",
"_source": "openrouter",
- "_model_count": 351,
+ "_model_count": 350,
"models": {
"anthropic/claude-opus-4.6-fast": {
"id": "anthropic/claude-opus-4.6-fast",
@@ -39,9 +39,9 @@
"provider": "z-ai",
"local_id": "glm-5.1",
"context_length": 202752,
- "max_output_tokens": 131072,
- "input_price_per_mtok": 1.395,
- "output_price_per_mtok": 4.4,
+ "max_output_tokens": 65535,
+ "input_price_per_mtok": 0.95,
+ "output_price_per_mtok": 3.15,
"supports_thinking": false,
"thinking_type": "none",
"temperature_range": [
@@ -106,7 +106,7 @@
"local_id": "gemma-4-26b-a4b-it",
"context_length": 262144,
"max_output_tokens": 262144,
- "input_price_per_mtok": 0.13,
+ "input_price_per_mtok": 0.12,
"output_price_per_mtok": 0.4,
"supports_thinking": false,
"thinking_type": "none",
@@ -2736,35 +2736,6 @@
"top_p"
]
},
- "arcee-ai/trinity-mini:free": {
- "id": "arcee-ai/trinity-mini:free",
- "name": "Arcee AI: Trinity Mini (free)",
- "provider": "arcee-ai",
- "local_id": "trinity-mini:free",
- "context_length": 131072,
- "max_output_tokens": 0,
- "input_price_per_mtok": 0.0,
- "output_price_per_mtok": 0.0,
- "supports_thinking": false,
- "thinking_type": "none",
- "temperature_range": [
- 0.0,
- 2.0
- ],
- "is_reasoning_model": false,
- "supported_parameters": [
- "include_reasoning",
- "max_tokens",
- "reasoning",
- "response_format",
- "structured_outputs",
- "temperature",
- "tool_choice",
- "tools",
- "top_k",
- "top_p"
- ]
- },
"arcee-ai/trinity-mini": {
"id": "arcee-ai/trinity-mini",
"name": "Arcee AI: Trinity Mini",
diff --git a/server/services/browser_service.py b/server/services/browser_service.py
index 20a51872..96b06338 100644
--- a/server/services/browser_service.py
+++ b/server/services/browser_service.py
@@ -1,14 +1,33 @@
"""Thin wrapper around agent-browser CLI.
-Stateless client — finds the binary, runs commands via subprocess, parses JSON output.
-The agent-browser daemon manages its own lifecycle (auto-starts, persists between commands).
+Stateless client — finds the binary via npx, runs commands via subprocess,
+parses JSON output. The agent-browser daemon manages its own lifecycle
+(auto-starts, persists between commands).
+
+Invocation strategy
+-------------------
+agent-browser is a pinned project dependency (see package.json). Rather than
+locating its shim manually per-platform, we invoke it via ``npx --no-install``:
+
+ [shutil.which("npx"), "--no-install", "agent-browser", ...args]
+
+This matches the pattern used by ``claude_code_service.py`` and is how npm
+intends locally-pinned CLIs to be invoked. npx handles all the
+cross-platform concerns (local ``node_modules/.bin/`` resolution, Windows
+.CMD vs POSIX shell shim, shebang interpretation) so we don't have to.
+
+``--no-install`` makes the call fail loudly if agent-browser isn't in the
+lockfile, instead of silently pulling from the registry at runtime.
+
+All subprocess calls use ``shell=False`` with list argv — Python handles
+Windows .CMD files natively via ``CreateProcessW``, avoiding the BatBadBut
+(CVE-2024-1874) argument-escaping vulnerabilities that plague shell=True.
"""
import asyncio
import json
import shutil
import subprocess
-import sys
from typing import Any, Dict, List, Optional
import psutil
@@ -21,14 +40,21 @@ _MAX_OUTPUT = 100_000
def _kill_process_tree(pid: int) -> None:
- """Terminate a process and all its descendants (cross-platform via psutil)."""
+ """Terminate a process and all its descendants (cross-platform via psutil).
+
+ Fast-exiting processes can race between ``Process(pid)`` and
+ ``children()``, so every psutil call is defensively guarded.
+ """
try:
parent = psutil.Process(pid)
except psutil.NoSuchProcess:
return
- # Collect descendants before killing parent so we don't lose the tree.
- descendants = parent.children(recursive=True)
+ try:
+ descendants = parent.children(recursive=True)
+ except psutil.NoSuchProcess:
+ descendants = []
+
for child in descendants:
try:
child.kill()
@@ -42,10 +68,15 @@ def _kill_process_tree(pid: int) -> None:
class BrowserService:
- """Subprocess wrapper for the agent-browser CLI."""
+ """Subprocess wrapper for the agent-browser CLI.
+
+ Holds a frozen argv prefix (typically ``[npx_path, --no-install,
+ agent-browser]``) plus the logic to spawn the daemon, read its first
+ JSON line, and kill the tree.
+ """
- def __init__(self, binary: str) -> None:
- self._bin = binary
+ def __init__(self, argv_prefix: List[str]) -> None:
+ self._prefix = list(argv_prefix)
async def run(
self,
@@ -63,20 +94,22 @@ class BrowserService:
daemon process alive. We read just the first line via Popen in a
thread, then kill the process — never wait for exit.
"""
- parts = [self._bin, "--session", session, "--json"]
+ argv = [
+ *self._prefix,
+ "--session", session,
+ "--json",
+ ]
if headed:
- parts.append("--headed")
+ argv.append("--headed")
if user_agent:
- parts.extend(["--user-agent", user_agent])
+ argv.extend(["--user-agent", user_agent])
if proxy:
- parts.extend(["--proxy", proxy])
- parts.extend(args)
+ argv.extend(["--proxy", proxy])
+ argv.extend(args)
- logger.debug("agent-browser exec", cmd=" ".join(parts))
+ logger.debug("agent-browser exec", argv=argv)
- raw = await asyncio.to_thread(
- self._run_sync, parts, timeout, stdin
- )
+ raw = await asyncio.to_thread(self._run_sync, argv, timeout, stdin)
if len(raw) > _MAX_OUTPUT:
raw = raw[:_MAX_OUTPUT] + "\n...(truncated)"
@@ -88,25 +121,26 @@ class BrowserService:
@staticmethod
def _run_sync(
- parts: List[str],
+ argv: List[str],
timeout: int,
stdin_data: Optional[bytes],
) -> str:
- """Run agent-browser, read first JSON line, kill process.
+ """Spawn agent-browser, read first JSON line, kill the process tree.
- agent-browser daemon keeps stdout open after printing the result.
- communicate() would hang forever. Instead: readline() + kill().
- On Windows, shell=True is required (.CMD wrapper).
- """
- is_win = sys.platform == "win32"
- cmd = " ".join(parts) if is_win else parts
+ The daemon holds stdout open after emitting its result, so we cannot
+ use communicate() or wait() — either would hang forever. Instead we
+ readline() and then force-kill the tree.
+ Uses ``shell=False`` unconditionally with a list argv. This is safe
+ on every platform including .CMD files on Windows (handled natively
+ by CreateProcessW since Python 3.7, hardened against BatBadBut in 3.12+).
+ """
proc = subprocess.Popen(
- cmd,
+ argv,
stdin=subprocess.PIPE if stdin_data else None,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
- shell=is_win,
+ shell=False,
)
try:
@@ -115,35 +149,47 @@ class BrowserService:
proc.stdin.close()
# Read the first line — that's the JSON result.
- # The daemon keeps the process alive after this, so
- # we must not call communicate() or wait().
+ # The daemon keeps the process alive after this, so we must
+ # not call communicate() or wait() before killing it.
line = proc.stdout.readline().decode(errors="replace").strip()
if not line:
- # No output — check stderr for errors
+ # No output — check stderr for errors.
err = proc.stderr.read().decode(errors="replace").strip()
raise RuntimeError(err or "agent-browser returned empty output")
return line
finally:
- # With shell=True on Windows, proc is the shell wrapper (cmd.exe)
- # and the real agent-browser daemon runs as its child. Killing only
- # proc leaves the daemon orphaned. psutil.children(recursive=True)
- # walks the process tree natively on every platform.
+ # npx -> node -> agent-browser daemon -> Chromium. Killing only
+ # proc.pid leaves the daemon orphaned. psutil.children(recursive=True)
+ # walks the tree natively on every platform.
_kill_process_tree(proc.pid)
proc.wait()
-# -- Module-level lazy singleton (NodeJSClient pattern) --
+# -- Module-level lazy singleton (NodeJSClient pattern) -----------------
_instance: Optional[BrowserService] = None
+def _find_agent_browser_cmd() -> Optional[List[str]]:
+ """Locate agent-browser via npx (pinned to project lockfile).
+
+ Uses ``npx --no-install`` so the call fails loudly if the package is
+ missing, rather than silently pulling from the registry at runtime.
+ Returns None if npx itself is not on PATH (Node.js not installed).
+ """
+ npx = shutil.which("npx")
+ if not npx:
+ return None
+ return [npx, "--no-install", "agent-browser"]
+
+
def get_browser_service() -> Optional[BrowserService]:
- """Get the BrowserService singleton, or None if agent-browser is not installed."""
+ """Return the BrowserService singleton, or None if agent-browser cannot be located."""
global _instance
if _instance is None:
- path = shutil.which("agent-browser")
- if path:
- _instance = BrowserService(path)
+ cmd = _find_agent_browser_cmd()
+ if cmd:
+ _instance = BrowserService(cmd)
return _instance
diff --git a/server/services/handlers/browser.py b/server/services/handlers/browser.py
index 470fd696..f5f58d56 100644
--- a/server/services/handlers/browser.py
+++ b/server/services/handlers/browser.py
@@ -20,7 +20,7 @@ async def handle_browser(
t0 = time.time()
svc = get_browser_service()
if not svc:
- return _fail("agent-browser not installed. Run: npm install -g agent-browser && agent-browser install", t0)
+ return _fail("agent-browser not installed. Run: pnpm install && npx agent-browser install", t0)
op = parameters.get("operation") or "navigate"
session = (