browser_session_cdp.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. """User-supplied CDP endpoints (live ``/browser connect``, ``BROWSER_CDP_URL``, ``browser.cdp_url``),
  2. dialog-policy config and the per-task CDP supervisor lifecycle."""
  3. from __future__ import annotations
  4. import logging
  5. import threading
  6. from typing import Optional
  7. from agent.proxy_bypass import loopback_request_kwargs
  8. from agent.redact import redact_cdp_url
  9. logger = logging.getLogger(__name__)
  10. # Live ``/browser connect`` endpoints, one per profile home (``hermes_home_key()``): a multiplexed
  11. # gateway serves every profile from this process, so a process-wide value would reach them all.
  12. _live_overrides: dict[str, str] = {}
  13. _live_overrides_lock = threading.Lock()
  14. # Per-profile count of live-endpoint publishes. A session records the value it saw before resolving its
  15. # endpoint; ``get_session_info`` closes one whose profile published in between.
  16. _live_generations: dict[str, int] = {}
  17. def resolve_cdp_override(cdp_url: str) -> str:
  18. """Normalize a CDP endpoint into a concrete websocket URL.
  19. Full ``ws://.../devtools/browser/...`` endpoints pass through; HTTP discovery roots and bare
  20. ``ws://host:port`` resolve via ``/json/version`` → ``webSocketDebuggerUrl`` (falls back to the raw value).
  21. """
  22. raw = (cdp_url or "").strip()
  23. if not raw:
  24. return ""
  25. lowered = raw.lower()
  26. if "/devtools/browser/" in lowered:
  27. return raw
  28. discovery_url = raw
  29. if lowered.startswith(("ws://", "wss://")):
  30. if not (raw.count(":") == 2 and raw.rstrip("/").rsplit(":", 1)[-1].isdigit() and "/" not in raw.split(":", 2)[-1]):
  31. return raw
  32. discovery_url = ("http://" if lowered.startswith("ws://") else "https://") + raw.split("://", 1)[1]
  33. version_url = discovery_url if discovery_url.lower().endswith("/json/version") else discovery_url.rstrip("/") + "/json/version"
  34. try:
  35. import requests # lazy: shared module object, so test patches still apply
  36. response = requests.get(version_url, timeout=10, **loopback_request_kwargs(version_url))
  37. response.raise_for_status()
  38. payload = response.json()
  39. except Exception as exc: # health: allow BLE001 -- any discovery failure falls back to the raw endpoint
  40. logger.warning("Failed to resolve CDP endpoint %s via %s: %s", redact_cdp_url(raw),
  41. redact_cdp_url(version_url), redact_cdp_url(str(exc)))
  42. return raw
  43. ws_url = str(payload.get("webSocketDebuggerUrl") or "").strip()
  44. if ws_url:
  45. logger.info("Resolved CDP endpoint %s -> %s", redact_cdp_url(raw), redact_cdp_url(ws_url))
  46. return ws_url
  47. logger.warning("CDP discovery at %s did not return webSocketDebuggerUrl; using raw endpoint",
  48. redact_cdp_url(version_url))
  49. return raw
  50. def live_cdp_override() -> str:
  51. """The current profile's live ``/browser connect`` endpoint, or "".
  52. On a multiplexed gateway a context with no profile bound answers "" rather than the launch
  53. profile's endpoint, the way ``get_secret`` fails closed."""
  54. from agent.secret_scope import is_multiplex_active
  55. from hermes_constants import get_hermes_home_override, hermes_home_key
  56. if get_hermes_home_override() is None and is_multiplex_active():
  57. return ""
  58. with _live_overrides_lock:
  59. return _live_overrides.get(hermes_home_key(), "")
  60. def set_live_cdp_override(url: Optional[str]) -> None:
  61. """Set (or with a falsy ``url`` clear) the current profile's live endpoint and bump its generation."""
  62. from hermes_constants import hermes_home_key
  63. key = hermes_home_key()
  64. with _live_overrides_lock:
  65. if url:
  66. _live_overrides[key] = url
  67. else:
  68. _live_overrides.pop(key, None)
  69. _live_generations[key] = _live_generations.get(key, 0) + 1
  70. def live_cdp_generation(home_key: Optional[str] = None) -> int:
  71. """The current (or ``home_key``'s) profile's live-endpoint generation."""
  72. from hermes_constants import hermes_home_key
  73. with _live_overrides_lock:
  74. return _live_generations.get(home_key or hermes_home_key(), 0)
  75. def env_cdp_url() -> str:
  76. """``BROWSER_CDP_URL`` from this profile's secret scope. A routed profile reads only its own ``.env``
  77. (``os.environ`` holds the launch profile's value)."""
  78. from agent.secret_scope import current_secret_scope, get_secret, serves_routed_profile
  79. if serves_routed_profile():
  80. value = (current_secret_scope() or {}).get("BROWSER_CDP_URL")
  81. else:
  82. value = get_secret("BROWSER_CDP_URL")
  83. return (value or "").strip()
  84. def get_cdp_override_raw() -> str:
  85. """The *configured* CDP override, no network I/O: live ``/browser connect`` > ``BROWSER_CDP_URL`` >
  86. ``browser.cdp_url``. Is-it-configured gates (check_fns, local-mode checks, doctor) MUST use this:
  87. a 10 s discovery probe against a stale URL would stall every schema build."""
  88. from tools.browser_session_cloud import browser_cfg
  89. return live_cdp_override() or env_cdp_url() or browser_cfg("cdp_url", "", lambda v: str(v or "").strip())
  90. def get_cdp_override() -> str:
  91. """Resolved CDP override URL, or "". May do HTTP discovery: call only on paths about to connect."""
  92. raw = get_cdp_override_raw()
  93. return resolve_cdp_override(raw) if raw else ""
  94. def get_dialog_policy_config() -> tuple[str, float]:
  95. """``browser.dialog_policy`` + ``browser.dialog_timeout_s``; supervisor defaults when absent/invalid."""
  96. from tools.browser_session_cloud import browser_cfg
  97. from tools.browser_supervisor_dialogs import DEFAULT_DIALOG_POLICY, DEFAULT_DIALOG_TIMEOUT_S, _VALID_POLICIES
  98. policy = str(browser_cfg("dialog_policy", DEFAULT_DIALOG_POLICY) or DEFAULT_DIALOG_POLICY)
  99. if policy not in _VALID_POLICIES:
  100. logger.debug("Invalid browser.dialog_policy=%r; using default", policy)
  101. policy = DEFAULT_DIALOG_POLICY
  102. try:
  103. timeout_s = float(browser_cfg("dialog_timeout_s", DEFAULT_DIALOG_TIMEOUT_S) or DEFAULT_DIALOG_TIMEOUT_S)
  104. except (TypeError, ValueError):
  105. timeout_s = DEFAULT_DIALOG_TIMEOUT_S
  106. return policy, (timeout_s if timeout_s > 0 else DEFAULT_DIALOG_TIMEOUT_S)
  107. def ensure_cdp_supervisor(task_id: str) -> None:
  108. """Start the CDP supervisor for ``task_id`` when an endpoint is reachable (idempotent; restarts on a
  109. URL change). URL precedence: the CDP override, then the session's own ``cdp_url``. A failed attach
  110. never breaks the session."""
  111. cdp_url = get_cdp_override()
  112. if not cdp_url:
  113. from tools import browser_session as _bs
  114. with _bs._cleanup_lock:
  115. session_cdp = str((_bs._active_sessions.get(task_id) or {}).get("cdp_url") or "")
  116. cdp_url = resolve_cdp_override(session_cdp) if session_cdp else ""
  117. if not cdp_url:
  118. return
  119. try:
  120. from tools.browser_supervisor import SUPERVISOR_REGISTRY
  121. policy, timeout_s = get_dialog_policy_config()
  122. SUPERVISOR_REGISTRY.get_or_start(task_id=task_id, cdp_url=cdp_url, dialog_policy=policy,
  123. dialog_timeout_s=timeout_s)
  124. except Exception as exc: # health: allow BLE001 -- a failed supervisor attach must not break the session
  125. logger.debug("CDP supervisor attach for task=%s failed (non-fatal): %s", task_id, exc)
  126. def stop_cdp_supervisor(task_id: str) -> None:
  127. """Stop ``task_id``'s CDP supervisor if one exists."""
  128. try:
  129. from tools.browser_supervisor import SUPERVISOR_REGISTRY
  130. SUPERVISOR_REGISTRY.stop(task_id)
  131. except Exception as exc: # health: allow BLE001 -- teardown must never abort on a supervisor error
  132. logger.debug("CDP supervisor stop for task=%s failed (non-fatal): %s", task_id, exc)