tools/ holds model tools. These 15 files register none: they launch and
track Chromium, hold the per-task browser sessions and run the CDP
supervisor the browser tools call. They move to a new root package:
tools/browser_session*.py -> browser/session*.py
tools/browser_launcher{,_holders} -> browser/launcher{,_holders}.py
tools/browser_lightpanda.py -> browser/lightpanda.py
tools/browser_supervisor*.py -> browser/supervisor*.py
tools/browser_real_profile_logins -> browser/real_profile_logins.py
tools/bot_desktop/browser.py -> browser/desktop_profile.py
browser/__init__.py is a docstring only. tools/browser_tool.py and every
tool file stay. 38 tests and one helper move to tests/browser/; its
conftest.py imports the autouse fixtures of tests/tools/conftest.py they
ran under.
Old-path files, each a MOVES key, a REQUIRED_NAMES row and a file
written by scripts/move/generate_shims.py:
- tools.browser_supervisor and tools.browser_supervisor_capture are on
the published plugin API (hermes_cli/data/plugin_api.json), so a
plugin's import gives the same module object.
- tools.browser_session and tools.browser_session_cloud are imported
inside functions on every turn by a process started on v0.22.7 to
v0.23.5 (the per-turn browser cleanup), which keeps running its old
code after an update swaps the checkout.
Function bodies are unchanged and every import stays where it was.
Loggers follow their modules: every moved file keeps
getLogger(__name__), and the two literal supervisor loggers name
browser.supervisor. browser joins the tools entry of
hermes_logging.COMPONENT_PREFIXES, so `hermes logs --component tools`
shows the same lines. browser also joins packages.find,
FIRST_PARTY_MODULE_ROOTS, _KNOWN_LOGGER_ROOTS, the plugin isolation
audit, six check scripts, the P33 profile-scope pattern, the gateway's
stop-path preload list and the test fixtures that list root packages.
The import and docs rewrites were made by scripts/move/rewrite_imports.py
and rewrite_docs.py; split imports, imports inside child-process code
strings, boundary-lint baseline keys, two resolution-allowlist rows and
docs globs were fixed by hand.
Pinned catalog plugins import these moved modules by the old name, so each has a key in
hermes_agent/compat/moves.py and the plugin guide lists it with the release
that may remove it (no earlier than v0.27.0):
tools.browser_session_cdp
tools.browser_session_lifecycle
160 lines
7.5 KiB
Python
160 lines
7.5 KiB
Python
"""User-supplied CDP endpoints (live ``/browser connect``, ``BROWSER_CDP_URL``, ``browser.cdp_url``),
|
|
dialog-policy config and the per-task CDP supervisor lifecycle."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import threading
|
|
from typing import Optional
|
|
|
|
from agent.proxy_bypass import loopback_request_kwargs
|
|
from agent.redact import redact_cdp_url
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Live ``/browser connect`` endpoints, one per profile home (``hermes_home_key()``): a multiplexed
|
|
# gateway serves every profile from this process, so a process-wide value would reach them all.
|
|
_live_overrides: dict[str, str] = {}
|
|
_live_overrides_lock = threading.Lock()
|
|
# Per-profile count of live-endpoint publishes. A session records the value it saw before resolving its
|
|
# endpoint; ``get_session_info`` closes one whose profile published in between.
|
|
_live_generations: dict[str, int] = {}
|
|
|
|
|
|
def resolve_cdp_override(cdp_url: str) -> str:
|
|
"""Normalize a CDP endpoint into a concrete websocket URL.
|
|
|
|
Full ``ws://.../devtools/browser/...`` endpoints pass through; HTTP discovery roots and bare
|
|
``ws://host:port`` resolve via ``/json/version`` → ``webSocketDebuggerUrl`` (falls back to the raw value).
|
|
"""
|
|
raw = (cdp_url or "").strip()
|
|
if not raw:
|
|
return ""
|
|
lowered = raw.lower()
|
|
if "/devtools/browser/" in lowered:
|
|
return raw
|
|
discovery_url = raw
|
|
if lowered.startswith(("ws://", "wss://")):
|
|
if not (raw.count(":") == 2 and raw.rstrip("/").rsplit(":", 1)[-1].isdigit() and "/" not in raw.split(":", 2)[-1]):
|
|
return raw
|
|
discovery_url = ("http://" if lowered.startswith("ws://") else "https://") + raw.split("://", 1)[1]
|
|
version_url = discovery_url if discovery_url.lower().endswith("/json/version") else discovery_url.rstrip("/") + "/json/version"
|
|
try:
|
|
import requests # lazy: shared module object, so test patches still apply
|
|
response = requests.get(version_url, timeout=10, **loopback_request_kwargs(version_url))
|
|
response.raise_for_status()
|
|
payload = response.json()
|
|
except Exception as exc: # health: allow BLE001 -- any discovery failure falls back to the raw endpoint
|
|
logger.warning("Failed to resolve CDP endpoint %s via %s: %s", redact_cdp_url(raw),
|
|
redact_cdp_url(version_url), redact_cdp_url(str(exc)))
|
|
return raw
|
|
ws_url = str(payload.get("webSocketDebuggerUrl") or "").strip()
|
|
if ws_url:
|
|
logger.info("Resolved CDP endpoint %s -> %s", redact_cdp_url(raw), redact_cdp_url(ws_url))
|
|
return ws_url
|
|
logger.warning("CDP discovery at %s did not return webSocketDebuggerUrl; using raw endpoint",
|
|
redact_cdp_url(version_url))
|
|
return raw
|
|
|
|
|
|
def live_cdp_override() -> str:
|
|
"""The current profile's live ``/browser connect`` endpoint, or "".
|
|
|
|
On a multiplexed gateway a context with no profile bound answers "" rather than the launch
|
|
profile's endpoint, the way ``get_secret`` fails closed."""
|
|
from agent.secret_scope import is_multiplex_active
|
|
from hermes_constants import get_hermes_home_override, hermes_home_key
|
|
if get_hermes_home_override() is None and is_multiplex_active():
|
|
return ""
|
|
with _live_overrides_lock:
|
|
return _live_overrides.get(hermes_home_key(), "")
|
|
|
|
|
|
def set_live_cdp_override(url: Optional[str]) -> None:
|
|
"""Set (or with a falsy ``url`` clear) the current profile's live endpoint and bump its generation."""
|
|
from hermes_constants import hermes_home_key
|
|
key = hermes_home_key()
|
|
with _live_overrides_lock:
|
|
if url:
|
|
_live_overrides[key] = url
|
|
else:
|
|
_live_overrides.pop(key, None)
|
|
_live_generations[key] = _live_generations.get(key, 0) + 1
|
|
|
|
|
|
def live_cdp_generation(home_key: Optional[str] = None) -> int:
|
|
"""The current (or ``home_key``'s) profile's live-endpoint generation."""
|
|
from hermes_constants import hermes_home_key
|
|
with _live_overrides_lock:
|
|
return _live_generations.get(home_key or hermes_home_key(), 0)
|
|
|
|
|
|
def env_cdp_url() -> str:
|
|
"""``BROWSER_CDP_URL`` from this profile's secret scope. A routed profile reads only its own ``.env``
|
|
(``os.environ`` holds the launch profile's value)."""
|
|
from agent.secret_scope import current_secret_scope, get_secret, serves_routed_profile
|
|
if serves_routed_profile():
|
|
value = (current_secret_scope() or {}).get("BROWSER_CDP_URL")
|
|
else:
|
|
value = get_secret("BROWSER_CDP_URL")
|
|
return (value or "").strip()
|
|
|
|
|
|
def get_cdp_override_raw() -> str:
|
|
"""The *configured* CDP override, no network I/O: live ``/browser connect`` > ``BROWSER_CDP_URL`` >
|
|
``browser.cdp_url``. Is-it-configured gates (check_fns, local-mode checks, doctor) MUST use this:
|
|
a 10 s discovery probe against a stale URL would stall every schema build."""
|
|
from browser.session_cloud import browser_cfg
|
|
return live_cdp_override() or env_cdp_url() or browser_cfg("cdp_url", "", lambda v: str(v or "").strip())
|
|
|
|
|
|
def get_cdp_override() -> str:
|
|
"""Resolved CDP override URL, or "". May do HTTP discovery: call only on paths about to connect."""
|
|
raw = get_cdp_override_raw()
|
|
return resolve_cdp_override(raw) if raw else ""
|
|
|
|
|
|
def get_dialog_policy_config() -> tuple[str, float]:
|
|
"""``browser.dialog_policy`` + ``browser.dialog_timeout_s``; supervisor defaults when absent/invalid."""
|
|
from browser.session_cloud import browser_cfg
|
|
from browser.supervisor_dialogs import DEFAULT_DIALOG_POLICY, DEFAULT_DIALOG_TIMEOUT_S, _VALID_POLICIES
|
|
policy = str(browser_cfg("dialog_policy", DEFAULT_DIALOG_POLICY) or DEFAULT_DIALOG_POLICY)
|
|
if policy not in _VALID_POLICIES:
|
|
logger.debug("Invalid browser.dialog_policy=%r; using default", policy)
|
|
policy = DEFAULT_DIALOG_POLICY
|
|
try:
|
|
timeout_s = float(browser_cfg("dialog_timeout_s", DEFAULT_DIALOG_TIMEOUT_S) or DEFAULT_DIALOG_TIMEOUT_S)
|
|
except (TypeError, ValueError):
|
|
timeout_s = DEFAULT_DIALOG_TIMEOUT_S
|
|
return policy, (timeout_s if timeout_s > 0 else DEFAULT_DIALOG_TIMEOUT_S)
|
|
|
|
|
|
def ensure_cdp_supervisor(task_id: str) -> None:
|
|
"""Start the CDP supervisor for ``task_id`` when an endpoint is reachable (idempotent; restarts on a
|
|
URL change). URL precedence: the CDP override, then the session's own ``cdp_url``. A failed attach
|
|
never breaks the session."""
|
|
cdp_url = get_cdp_override()
|
|
if not cdp_url:
|
|
from browser import session as _bs
|
|
with _bs._cleanup_lock:
|
|
session_cdp = str((_bs._active_sessions.get(task_id) or {}).get("cdp_url") or "")
|
|
cdp_url = resolve_cdp_override(session_cdp) if session_cdp else ""
|
|
if not cdp_url:
|
|
return
|
|
try:
|
|
from browser.supervisor import SUPERVISOR_REGISTRY
|
|
policy, timeout_s = get_dialog_policy_config()
|
|
SUPERVISOR_REGISTRY.get_or_start(task_id=task_id, cdp_url=cdp_url, dialog_policy=policy,
|
|
dialog_timeout_s=timeout_s)
|
|
except Exception as exc: # health: allow BLE001 -- a failed supervisor attach must not break the session
|
|
logger.debug("CDP supervisor attach for task=%s failed (non-fatal): %s", task_id, exc)
|
|
|
|
|
|
def stop_cdp_supervisor(task_id: str) -> None:
|
|
"""Stop ``task_id``'s CDP supervisor if one exists."""
|
|
try:
|
|
from browser.supervisor import SUPERVISOR_REGISTRY
|
|
SUPERVISOR_REGISTRY.stop(task_id)
|
|
except Exception as exc: # health: allow BLE001 -- teardown must never abort on a supervisor error
|
|
logger.debug("CDP supervisor stop for task=%s failed (non-fatal): %s", task_id, exc)
|