browser_supervisor_dialogs.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. """Dialog capture + response half of the CDP supervisor.
  2. Two capture paths feed one ``PendingDialog`` queue: native ``Page.javascriptDialogOpening``
  3. events (answered with ``Page.handleJavaScriptDialog``), and the injected *dialog bridge* —
  4. a page script rewriting alert/confirm/prompt into a sync XHR to a magic host we intercept
  5. via the CDP ``Fetch`` domain and answer with ``Fetch.fulfillRequest``. The bridge works on
  6. Browserbase (whose CDP proxy auto-dismisses native dialogs) because the native dialog never fires.
  7. ``DialogSupervisionMixin`` relies on state ``CDPSupervisor.__init__`` sets.
  8. """
  9. from __future__ import annotations
  10. import asyncio
  11. import base64
  12. import json
  13. import logging
  14. import time
  15. from dataclasses import dataclass
  16. from typing import Any, Callable, Dict, Optional
  17. from urllib.parse import parse_qs, urlparse
  18. # Logger-name parity with the origin module (records must look unchanged).
  19. logger = logging.getLogger("tools.browser_supervisor")
  20. def _redact_supervisor_text(value: str) -> str:
  21. """Redact page-originated text before exposing supervisor snapshots."""
  22. from agent.redact import redact_sensitive_text
  23. return redact_sensitive_text(value, force=True)
  24. def _trim_ring(events: list, keep: int) -> list:
  25. """Cap a ring buffer at ``keep`` entries once it overflows to 2x (slack reduces churn)."""
  26. return events[-keep:] if len(events) > keep * 2 else events
  27. _REDACTED_FIELDS = frozenset({"message", "default_prompt"})
  28. def _dialog_dict(obj: Any, keys: tuple) -> dict[str, Any]:
  29. """Snapshot dict of ``keys`` with page-originated text fields redacted."""
  30. return {k: _redact_supervisor_text(getattr(obj, k)) if k in _REDACTED_FIELDS else getattr(obj, k) for k in keys}
  31. DIALOG_POLICY_MUST_RESPOND = "must_respond"
  32. DIALOG_POLICY_AUTO_DISMISS = "auto_dismiss"
  33. DIALOG_POLICY_AUTO_ACCEPT = "auto_accept"
  34. _VALID_POLICIES = frozenset(
  35. {DIALOG_POLICY_MUST_RESPOND, DIALOG_POLICY_AUTO_DISMISS, DIALOG_POLICY_AUTO_ACCEPT}
  36. )
  37. DEFAULT_DIALOG_POLICY = DIALOG_POLICY_MUST_RESPOND
  38. DEFAULT_DIALOG_TIMEOUT_S = 300.0
  39. # Last N closed dialogs kept so agents on backends that auto-dismiss server-side
  40. # (Browserbase) can still observe that a dialog fired.
  41. RECENT_DIALOGS_MAX = 20
  42. # Magic host the bridge XHRs to; intercepted via CDP Fetch before any network
  43. # resolution, so it never has to exist. Keep ASCII + URL-safe (Fetch patterns gate on it).
  44. DIALOG_BRIDGE_HOST = "hermes-dialog-bridge.invalid"
  45. DIALOG_BRIDGE_URL_PATTERN = f"http://{DIALOG_BRIDGE_HOST}/*"
  46. # Injected into every frame via Page.addScriptToEvaluateOnNewDocument. Sync GET with
  47. # query params so the Fetch interceptor never parses a body; unreachable bridge → null
  48. # so the page still sees *some* behavior. onbeforeunload is left native (can't be
  49. # prompted synchronously without racing navigation); the native path still records it.
  50. _DIALOG_BRIDGE_SCRIPT = r"""
  51. (() => {
  52. if (window.__hermesDialogBridgeInstalled) return;
  53. window.__hermesDialogBridgeInstalled = true;
  54. const ENDPOINT = "http://hermes-dialog-bridge.invalid/";
  55. function ask(kind, message, defaultPrompt) {
  56. try {
  57. const xhr = new XMLHttpRequest();
  58. const params = new URLSearchParams({
  59. kind: String(kind || ""),
  60. message: String(message == null ? "" : message),
  61. default_prompt: String(defaultPrompt == null ? "" : defaultPrompt),
  62. });
  63. xhr.open("GET", ENDPOINT + "?" + params.toString(), false); // sync
  64. xhr.send(null);
  65. if (xhr.status !== 200) return null;
  66. let parsed;
  67. try { parsed = JSON.parse(xhr.responseText || ""); } catch (e) { return null; }
  68. if (kind === "alert") return undefined;
  69. if (kind === "confirm") return Boolean(parsed && parsed.accept);
  70. if (kind === "prompt") {
  71. if (!parsed || !parsed.accept) return null;
  72. return parsed.prompt_text == null ? "" : String(parsed.prompt_text);
  73. }
  74. return null;
  75. } catch (e) {
  76. return null;
  77. }
  78. }
  79. window.alert = function(message) { ask("alert", message, ""); };
  80. window.confirm = function(message) {
  81. const r = ask("confirm", message, "");
  82. return r === null ? false : Boolean(r);
  83. };
  84. window.prompt = function(message, def) {
  85. const r = ask("prompt", message, def == null ? "" : def);
  86. return r === null ? null : String(r);
  87. };
  88. })();
  89. """
  90. @dataclass
  91. class PendingDialog:
  92. """A JS dialog currently open on some frame's session."""
  93. id: str
  94. type: str # "alert" | "confirm" | "prompt" | "beforeunload"
  95. message: str
  96. default_prompt: str
  97. opened_at: float
  98. cdp_session_id: str # which attached CDP session the dialog fired in
  99. frame_id: Optional[str] = None
  100. # Bridge XHR path: respond via Fetch.fulfillRequest, NOT Page.handleJavaScriptDialog.
  101. bridge_request_id: Optional[str] = None
  102. def to_dict(self) -> dict[str, Any]:
  103. return _dialog_dict(self, ("id", "type", "message", "default_prompt", "opened_at", "frame_id"))
  104. @dataclass
  105. class DialogRecord:
  106. """A dialog that was opened and then handled (kept briefly in ``recent_dialogs``)."""
  107. id: str
  108. type: str
  109. message: str
  110. opened_at: float
  111. closed_at: float
  112. closed_by: str # "agent" | "auto_policy" | "remote" | "watchdog"
  113. frame_id: Optional[str] = None
  114. def to_dict(self) -> dict[str, Any]:
  115. return _dialog_dict(self, ("id", "type", "message", "opened_at", "closed_at", "closed_by", "frame_id"))
  116. class DialogSupervisionMixin:
  117. """Dialog event handling for ``CDPSupervisor`` (all methods run on its loop)."""
  118. async def _cdp_quiet(self, method: str, params: dict[str, Any], *, session_id: Optional[str],
  119. timeout: float, what: str) -> None:
  120. """Best-effort CDP call: failures are logged at debug and swallowed."""
  121. try:
  122. await self._cdp(method, params, session_id=session_id, timeout=timeout)
  123. except Exception as e:
  124. logger.debug("%s failed (%s): %s", method, what, e)
  125. async def _install_dialog_bridge(self, session_id: str) -> None:
  126. """Install the dialog-bridge init script + Fetch interceptor on a session. Idempotent at
  127. the CDP level (Chromium de-dupes identical add-script calls; Fetch.enable replaces prior
  128. patterns); the final Runtime.evaluate injects into the already-loaded document so
  129. existing pages pick up the override on reconnect."""
  130. sid = (session_id or "")[:16]
  131. steps = (
  132. ("Page.addScriptToEvaluateOnNewDocument", {"source": _DIALOG_BRIDGE_SCRIPT, "runImmediately": True},
  133. 5.0, f"dialog bridge sid={sid}"),
  134. ("Fetch.enable", {"patterns": [{"urlPattern": DIALOG_BRIDGE_URL_PATTERN, "requestStage": "Request"}],
  135. "handleAuthRequests": False}, 5.0, f"dialog bridge sid={sid}"),
  136. ("Runtime.evaluate", {"expression": _DIALOG_BRIDGE_SCRIPT, "returnByValue": True},
  137. 3.0, f"dialog bridge inject sid={sid}"),
  138. )
  139. for method, params, timeout, what in steps:
  140. await self._cdp_quiet(method, params, session_id=session_id, timeout=timeout, what=what)
  141. # ── Capture ──────────────────────────────────────────────────────────────
  142. async def _on_dialog_opening(self, params: dict[str, Any], session_id: Optional[str]) -> None:
  143. self._admit_dialog(
  144. type=str(params.get("type") or ""), message=str(params.get("message") or ""),
  145. default_prompt=str(params.get("defaultPrompt") or ""), session_id=session_id, frame_id=params.get("frameId"),
  146. )
  147. async def _on_fetch_paused(self, params: dict[str, Any], session_id: Optional[str]) -> None:
  148. """Bridge XHR captured mid-flight — materialize as a pending dialog. The page's JS
  149. thread is blocked on the XHR until we Fetch.fulfillRequest (agent or watchdog);
  150. requests for other hosts are forwarded unchanged."""
  151. url = str(params.get("request", {}).get("url") or "")
  152. request_id = params.get("requestId")
  153. if not request_id:
  154. return
  155. if DIALOG_BRIDGE_HOST not in url:
  156. await self._cdp_quiet("Fetch.continueRequest", {"requestId": request_id},
  157. session_id=session_id, timeout=3.0, what="passthrough")
  158. return
  159. q = {k: v[0] for k, v in parse_qs(urlparse(url).query).items()}
  160. self._admit_dialog(
  161. type=q.get("kind") or "alert", message=q.get("message", ""), default_prompt=q.get("default_prompt", ""),
  162. session_id=session_id, frame_id=params.get("frameId"), bridge_request_id=str(request_id),
  163. )
  164. def _admit_dialog(self, *, type: str, message: str, default_prompt: str, session_id: Optional[str],
  165. frame_id: Optional[str], bridge_request_id: Optional[str] = None) -> None:
  166. """Create the dialog and apply the policy: auto-respond, or queue + arm the watchdog.
  167. Auto policies archive FIRST (tagged ``auto_policy``) so the ``closed`` event that
  168. follows our own response isn't re-archived as ``remote``."""
  169. self._dialog_seq += 1
  170. dialog = PendingDialog(
  171. id=f"d-{self._dialog_seq}", type=type, message=message, default_prompt=default_prompt,
  172. opened_at=time.time(), cdp_session_id=session_id or self._page_session_id or "",
  173. frame_id=frame_id, bridge_request_id=bridge_request_id,
  174. )
  175. auto = {DIALOG_POLICY_AUTO_DISMISS: (False, ""), DIALOG_POLICY_AUTO_ACCEPT: (True, default_prompt)}.get(
  176. self.dialog_policy
  177. )
  178. if auto is not None:
  179. with self._state_lock:
  180. self._archive_dialog_locked(dialog, "auto_policy")
  181. asyncio.create_task(self._respond_quiet(dialog, accept=auto[0], prompt_text=auto[1]))
  182. return
  183. with self._state_lock:
  184. self._pending_dialogs[dialog.id] = dialog
  185. self._dialog_watchdogs[dialog.id] = asyncio.get_running_loop().call_later(
  186. self.dialog_timeout_s,
  187. lambda: asyncio.create_task(self._dialog_timeout_expired(dialog.id)),
  188. )
  189. # ── Responding ───────────────────────────────────────────────────────────
  190. async def _respond(self, dialog: PendingDialog, *, accept: bool, prompt_text: Optional[str]) -> None:
  191. """Bridge-fulfill for XHR-captured dialogs (swallows failures so the page
  192. unblocks), else native CDP — ``promptText`` only for prompt dialogs when
  193. given; raises on CDP failure."""
  194. session_id = dialog.cdp_session_id or None
  195. if dialog.bridge_request_id:
  196. body = json.dumps({"accept": bool(accept), "dialog_id": dialog.id,
  197. "prompt_text": (prompt_text or "") if dialog.type == "prompt" else ""}).encode()
  198. await self._cdp_quiet(
  199. "Fetch.fulfillRequest",
  200. {"requestId": dialog.bridge_request_id, "responseCode": 200,
  201. "responseHeaders": [{"name": "Content-Type", "value": "application/json"},
  202. {"name": "Access-Control-Allow-Origin", "value": "*"}],
  203. "body": base64.b64encode(body).decode()},
  204. session_id=session_id, timeout=5.0, what=f"bridge fulfill {dialog.id}",
  205. )
  206. return
  207. params: dict[str, Any] = {"accept": accept}
  208. if prompt_text is not None and dialog.type == "prompt":
  209. params["promptText"] = prompt_text
  210. await self._cdp("Page.handleJavaScriptDialog", params, session_id=session_id, timeout=5.0)
  211. async def _respond_quiet(self, dialog: PendingDialog, *, accept: bool, prompt_text: Optional[str]) -> None:
  212. """Auto-policy / watchdog response (already archived by the caller); failures logged only."""
  213. try:
  214. await self._respond(dialog, accept=accept, prompt_text=prompt_text)
  215. except Exception as e:
  216. logger.debug("auto response failed for %s: %s", dialog.id, e)
  217. async def _handle_dialog_cdp(self, dialog: PendingDialog, *, accept: bool, prompt_text: str) -> None:
  218. """Agent response path. The dialog is retired regardless of outcome — a CDP
  219. error usually means it already closed (browser auto-dismissed after navigation)."""
  220. try:
  221. await self._respond(dialog, accept=accept, prompt_text=prompt_text)
  222. finally:
  223. self._retire_dialog(dialog.id, "agent")
  224. async def _dialog_timeout_expired(self, dialog_id: str) -> None:
  225. with self._state_lock:
  226. dialog = self._pending_dialogs.get(dialog_id)
  227. if dialog is None:
  228. return
  229. logger.warning("CDP supervisor %s: dialog %s (%s) auto-dismissed after %ss timeout",
  230. self.task_id, dialog_id, dialog.type, self.dialog_timeout_s)
  231. # Archive with watchdog tag BEFORE unblocking the page.
  232. self._retire_dialog(dialog_id, "watchdog")
  233. await self._respond_quiet(dialog, accept=False, prompt_text=None)
  234. # ── Bookkeeping ──────────────────────────────────────────────────────────
  235. def _retire_dialog(self, dialog_id: str, closed_by: str) -> None:
  236. """Remove a pending dialog (archiving it with ``closed_by``) and cancel its watchdog."""
  237. with self._state_lock:
  238. dialog = self._pending_dialogs.pop(dialog_id, None)
  239. if dialog is not None:
  240. self._archive_dialog_locked(dialog, closed_by)
  241. handle = self._dialog_watchdogs.pop(dialog_id, None)
  242. if handle is not None:
  243. handle.cancel()
  244. def _archive_dialog_locked(self, dialog: PendingDialog, closed_by: str) -> None:
  245. """Move a pending dialog to the recent_dialogs ring buffer. Must hold state_lock."""
  246. record = DialogRecord(id=dialog.id, type=dialog.type, message=dialog.message, opened_at=dialog.opened_at,
  247. closed_at=time.time(), closed_by=closed_by, frame_id=dialog.frame_id)
  248. self._recent_dialogs = _trim_ring([*self._recent_dialogs, record], RECENT_DIALOGS_MAX)
  249. async def _on_dialog_closed(self, params: dict[str, Any], session_id: Optional[str]) -> None:
  250. # ``Page.javascriptDialogClosed`` carries only ``result``/``userInput``: match by
  251. # session id and clear the oldest native dialog on it (the JS thread blocks while
  252. # a dialog is up, so at most one is in flight). Bridge dialogs resolve via Fetch.
  253. with self._state_lock:
  254. candidate = next((d.id for d in self._pending_dialogs.values()
  255. if d.cdp_session_id == session_id and d.bridge_request_id is None), None)
  256. if candidate:
  257. self._retire_dialog(candidate, "remote")
  258. # CDP event → handler(self, params, session_id); merged into CDPSupervisor._EVENT_HANDLERS.
  259. EVENT_HANDLERS: dict[str, Callable[..., Any]] = {
  260. "Page.javascriptDialogOpening": _on_dialog_opening,
  261. "Page.javascriptDialogClosed": _on_dialog_closed,
  262. "Fetch.requestPaused": _on_fetch_paused,
  263. }