JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
fix: 解决浏览器重启和多实例冲突的BUG
m056838700315ad8eff970
diff --git a/jiuwenclaw/agentserver/tools/browser-move/src/playwright_runtime/service.py b/jiuwenclaw/agentserver/tools/browser-move/src/playwright_runtime/service.pyindex 3f17a64e5b..d880938bc3 100644--- a/jiuwenclaw/agentserver/tools/browser-move/src/playwright_runtime/service.py+++ b/jiuwenclaw/agentserver/tools/browser-move/src/playwright_runtime/service.py@@ -16,6 +16,7 @@ import shutilimport uuidfrom pathlib import Pathfrom typing import Any, Dict, List, Optional, Tuple+from urllib.request import urlopenfrom jiuwenclaw.browser_timeout_policy import (allow_short_timeout_override,@@ -165,6 +166,80 @@ class BrowserService:return explicitreturn "remote"+ def _refresh_profile_store(self) -> None:+ self._profile_store = BrowserProfileStore(self._resolve_profile_store_path())++ @staticmethod+ def _is_cdp_endpoint_ready(endpoint: str) -> bool:+ base = str(endpoint or "").strip().rstrip("/")+ if not base:+ return False+ try:+ with urlopen(f"{base}/json/version", timeout=1.5) as response: # nosec B310+ payload = json.loads(response.read().decode("utf-8", errors="ignore"))+ if isinstance(payload, dict):+ return bool(payload.get("webSocketDebuggerUrl") or payload.get("Browser"))+ except (OSError, ValueError):+ return False+ return False++ def _resolve_existing_cdp_profile(self) -> Optional[BrowserProfile]:+ self._refresh_profile_store()+ candidates: List[BrowserProfile] = []+ selected = self._profile_store.selected_profile()+ if selected is not None:+ candidates.append(selected)+ named = self._profile_store.get_profile(self._profile_name)+ if named is not None and all(named.name != item.name for item in candidates):+ candidates.append(named)+ for profile in candidates:+ endpoint = str(profile.cdp_url or "").strip()+ if endpoint and self._is_cdp_endpoint_ready(endpoint):+ return profile+ return None++ def _should_replace_managed_driver(self, profile: BrowserProfile) -> bool:+ if self._managed_driver is None:+ return False+ if profile.driver_type != "managed":+ return True+ if self._active_profile is None:+ return True+ current_endpoint = str(self._active_profile.cdp_url or "").strip()+ target_endpoint = str(profile.cdp_url or "").strip()+ return current_endpoint != target_endpoint++ @staticmethod+ def _env_truthy(name: str, default: bool = False) -> bool:+ raw = (os.getenv(name) or "").strip().lower()+ if not raw:+ return default+ return raw in {"1", "true", "yes", "on"}++ def _should_close_browser_after_task(self) -> bool:+ return self._driver_mode == "managed" and self._env_truthy(+ "BROWSER_MANAGED_CLOSE_AFTER_TASK",+ default=False,+ )++ @staticmethod+ def _is_browser_connection_error_text(value: Any) -> bool:+ text = str(value or "").strip().lower()+ if not text:+ return False+ markers = (+ "connectovercdp",+ "econnrefused",+ "browsertype.connectovercdp",+ "????????",+ "cdp ??",+ "browser service may not be started",+ "browser has been closed",+ "target page, context or browser has been closed",+ "failed to connect to browser",+ )+ return any(marker in text for marker in markers)+def _resolve_effective_timeout(self, timeout_s: Optional[int]) -> int:effective_timeout = resolve_browser_task_timeout(timeout_s, self.guardrails.timeout_s)requested_timeout = None@@ -263,10 +338,27 @@ class BrowserService:self.mcp_cfg.params = paramsasync def _ensure_managed_driver_started(self) -> None:+ existing_profile = self._resolve_existing_cdp_profile()+ if existing_profile is not None:+ if self._should_replace_managed_driver(existing_profile):+ await self._stop_managed_driver()+ self._active_profile = existing_profile+ self._inject_cdp_endpoint(existing_profile.cdp_url)+ self._profile_store.upsert_profile(existing_profile, select=True)+ return+if self._driver_mode != "managed":returnif self._managed_driver is not None:- return+ try:+ endpoint = await asyncio.to_thread(self._managed_driver.start, 10.0, False)+ self._inject_cdp_endpoint(endpoint)+ if self._active_profile is not None:+ self._active_profile.cdp_url = endpoint+ self._profile_store.upsert_profile(self._active_profile, select=True)+ return+ except Exception:+ await self._stop_managed_driver()profile = self._profile_store.get_profile(self._profile_name)if (@@ -299,6 +391,24 @@ class BrowserService:self._managed_driver = Noneawait asyncio.to_thread(driver.stop)+ async def _reset_browser_runtime(self) -> None:+ try:+ if self.started:+ server_resource_id = (self.mcp_cfg.server_id or "").strip() or self.mcp_cfg.server_name+ await Runner.resource_mgr.remove_tool_server(server_resource_id, ignore_not_exist=True)+ except Exception:+ pass+ self.started = False+ self._browser_agent = None+ await self._stop_managed_driver()++ async def _restart_browser_runtime(self) -> None:+ from openjiuwen.core.common.logging import logger as _logger++ _logger.warning("BrowserService: restarting browser runtime")+ await self._reset_browser_runtime()+ await self.ensure_started()+def _ensure_screenshots_dir(self) -> None:self._screenshots_dir.mkdir(parents=True, exist_ok=True)@@ -464,6 +574,7 @@ class BrowserService:async def ensure_started(self) -> None:if self.started:+ await self._ensure_managed_driver_started()returnif shutil.which("npx") is None:@@ -498,19 +609,8 @@ class BrowserService:self.started = Trueasync def _restart(self) -> None:- """Tear down and reinitialize the browser service (e.g. after stdio subprocess dies)."""- from openjiuwen.core.common.logging import logger as _logger- _logger.warning("BrowserService: restarting due to broken MCP connection")- try:- server_resource_id = (self.mcp_cfg.server_id or "").strip() or self.mcp_cfg.server_name- await Runner.resource_mgr.remove_tool_server(- server_resource_id, ignore_not_exist=True- )- except Exception:- pass- self.started = False- self._browser_agent = None- await self.ensure_started()+ """Tear down and reinitialize the browser service (e.g. after browser/CDP/MCP failure)."""+ await self._restart_browser_runtime()async def _run_task_once(self, task: str, session_id: str, request_id: str) -> Dict[str, Any]:if self._browser_agent is None:@@ -702,6 +802,18 @@ class BrowserService:last_failure_page = parsed.get("page") if isinstance(parsed.get("page"), dict) else {}last_failure_screenshot = parsed.get("screenshot")+ if (+ not parsed_ok+ and self._is_browser_connection_error_text(parsed.get("error") or parsed.get("final"))+ and attempt_idx < attempts+ ):+ try:+ await self._restart_browser_runtime()+ next_task = base_task+ continue+ except Exception as restart_exc:+ last_error = f"browser_restart_failed: {restart_exc!r}"+if (not parsed_okand self._is_max_iteration_result(parsed)@@ -784,7 +896,11 @@ class BrowserService:if attempt_idx >= attempts:break# Restart before retry on known transport/session failures.- if (not str(exc)) or self._is_retryable_transport_error(exc):+ if (+ (not str(exc))+ or self._is_retryable_transport_error(exc)+ or self._is_browser_connection_error_text(exc)+ ):try:await self._restart()except Exception as restart_exc:@@ -819,6 +935,11 @@ class BrowserService:finally:if current_task is not None:self._unregister_inflight_task(sid, rid, current_task)+ if self._should_close_browser_after_task():+ try:+ await self._reset_browser_runtime()+ except Exception:+ passasync def shutdown(self) -> None:try:diff --git a/jiuwenclaw/agentserver/tools/browser_start_client.py b/jiuwenclaw/agentserver/tools/browser_start_client.pyindex 41f977d8e1..0628deabd9 100644--- a/jiuwenclaw/agentserver/tools/browser_start_client.py+++ b/jiuwenclaw/agentserver/tools/browser_start_client.py@@ -23,6 +23,7 @@ browser:from __future__ import annotationsimport argparse+import importlib.utilimport osimport platformimport shutil@@ -44,6 +45,58 @@ def _config_path(custom_path: str = "") -> Path:return _repo_root() / "config" / "config.yaml"+def _browser_move_root() -> Path:+ return _repo_root() / "jiuwenclaw" / "agentserver" / "tools" / "browser-move"+++def _profile_store_path() -> Path:+ configured = (os.getenv("BROWSER_PROFILE_STORE_PATH") or "").strip()+ if configured:+ return Path(configured).expanduser()+ return _browser_move_root() / ".browser" / "profiles.json"+++def _profile_name(profile_directory: str) -> str:+ configured = (os.getenv("BROWSER_PROFILE_NAME") or "").strip()+ if configured:+ return configured+ fallback = (profile_directory or "").strip()+ return fallback or "jiuwenclaw"+++def _load_profile_store_types() -> tuple[type[Any], type[Any]]:+ module_path = _browser_move_root() / "src" / "playwright_runtime" / "profiles.py"+ spec = importlib.util.spec_from_file_location("jiuwenclaw_browser_profiles", module_path)+ if spec is None or spec.loader is None:+ raise RuntimeError(f"Failed to load browser profile module: {module_path}")+ module = importlib.util.module_from_spec(spec)+ spec.loader.exec_module(module)+ return module.BrowserProfile, module.BrowserProfileStore+++def _persist_browser_profile(+ *,+ host: str,+ port: int,+ chrome_exec: str,+ user_data_dir: str,+ profile_directory: str,+) -> None:+ browser_profile_cls, browser_profile_store_cls = _load_profile_store_types()+ store = browser_profile_store_cls(_profile_store_path())+ profile = browser_profile_cls(+ name=_profile_name(profile_directory),+ driver_type="remote",+ cdp_url=f"http://{host}:{port}",+ browser_binary=chrome_exec,+ user_data_dir=user_data_dir,+ debug_port=port,+ host=host,+ extra_args=[f"--profile-directory={profile_directory}"] if profile_directory else [],+ )+ store.upsert_profile(profile, select=True)++def _load_browser_config(config_file: str = "") -> dict[str, Any]:cfg_file = _config_path(config_file)if not cfg_file.exists():@@ -207,6 +260,13 @@ def start_browser(*, dry_run: bool = False, config_file: str = "") -> int:return 0proc = subprocess.Popen(args, **kwargs)+ _persist_browser_profile(+ host=host,+ port=port,+ chrome_exec=chrome_exec,+ user_data_dir=user_data_dir,+ profile_directory=profile_directory,+ )print(f"Chrome started (pid={proc.pid}) at {host}:{port}")print(f"Executable: {chrome_exec}")print(f"Profile dir: {user_data_dir}")