perf(fast-browser): eliminate redundant browser protocol calls

This commit is contained in:
jb
2026-09-21 13:05:17 -04:00
parent 05793fe7b0
commit a79418efcb
2 changed files with 129 additions and 33 deletions
+41 -33
View File
@@ -53,6 +53,9 @@ NAVIGATION_TIMEOUT_SECONDS = 10.0
# timeout returns a structured result instead of racing the parent deadline.
POPUP_WAIT_SECONDS = 4.75
EXPECTED_BROWSER_HARNESS_VERSION = "0.1.13"
_INTERNAL_TARGET_PREFIXES = (
"chrome://", "chrome-untrusted://", "devtools://", "chrome-extension://", "about:",
)
_TEXT_INPUT_TYPES = {"", "text", "search", "email", "url", "tel", "number", "password"}
_FILL_ROLES = {"textbox", "searchbox", "spinbutton"}
@@ -493,12 +496,12 @@ class BrowserWorker:
self.backend.fill(candidate.backend_id, text)
deadline = time.monotonic() + ACTION_TIMEOUT_SECONDS
while time.monotonic() < deadline:
self._post_action_state(snapshot, same_document=True)
tab = self._post_action_state(snapshot, same_document=True)
value_verified = self.backend.value_matches(candidate.backend_id, text)
condition_verified = True
if postcondition is not None:
if postcondition["kind"] == "url":
condition_verified = self.backend.current_tab().get("url") == postcondition["value"]
condition_verified = tab.get("url") == postcondition["value"]
else:
condition_verified = self.backend.visible_text(postcondition["value"])
if value_verified and condition_verified:
@@ -560,6 +563,7 @@ class RealBrowserBackend:
self._daemon_ready = False
self._process: subprocess.Popen | None = None
self._attached_target: str | None = None
self._select_all_modifier: int | None = None
self._configure_environment(cdp_url, self._sandbox)
try:
self._load_harness()
@@ -703,45 +707,46 @@ class RealBrowserBackend:
def switch_tab(self, target_id: str) -> None:
if not isinstance(target_id, str) or not target_id:
raise WorkerError("tab identity is unavailable")
# browser-harness 0.1.13 enables Page/DOM/Runtime/Network as part of
# its switch_tab set_session path. Re-enabling them here duplicates
# four CDP calls and can race the daemon's session setup.
self._session_id = self._helpers.switch_tab(target_id, activate=False)
self._attached_target = target_id
# The pinned helper does not enable these domains on every attach. Do
# so here once per target; drain_events is then a truthful source for
# the bounded diagnostics surface.
for method in ("Page.enable", "DOM.enable", "Runtime.enable", "Network.enable"):
try:
self._helpers.cdp(method)
except Exception:
# Diagnostics are best effort and never block a browser action.
pass
def list_tabs(self) -> list[dict[str, Any]]:
tabs = self._helpers.list_tabs(include_chrome=False)
opener_by_target: dict[str, str] = {}
try:
raw = self._helpers.cdp("Target.getTargets")
for info in raw.get("targetInfos", []) if isinstance(raw, dict) else []:
if not isinstance(info, dict) or info.get("type") != "page":
continue
target = info.get("targetId")
opener = info.get("openerId")
if isinstance(target, str) and isinstance(opener, str) and opener:
opener_by_target[target] = opener
except Exception:
pass
# helpers.list_tabs() and this opener lookup used to issue two identical
# Target.getTargets requests. Keep its page/internal filtering locally
# while deriving opener metadata from the same target inventory.
raw = self._helpers.cdp("Target.getTargets")
target_infos = raw.get("targetInfos", []) if isinstance(raw, dict) else []
if not isinstance(target_infos, list):
return []
result: list[dict[str, Any]] = []
for tab in tabs:
if not isinstance(tab, dict) or not isinstance(tab.get("url"), str):
for info in target_infos:
if not isinstance(info, dict) or info.get("type") != "page":
continue
target = tab.get("targetId") or tab.get("target_id")
url = info.get("url", "")
title = info.get("title", "")
if not isinstance(url, str) or not isinstance(title, str):
continue
# Match browser-harness helpers.list_tabs(include_chrome=False),
# including its startup-placeholder suppression.
if title.startswith("Starting agent ") and (
url in ("", "about:blank") or url.startswith("about:blank#")
):
continue
if url.startswith(_INTERNAL_TARGET_PREFIXES):
continue
target = info.get("targetId")
if not isinstance(target, str) or not target:
continue
opener = info.get("openerId")
result.append({
"targetId": target,
"target_id": target,
"url": tab.get("url", ""),
"title": tab.get("title", ""),
"openerTargetId": opener_by_target.get(target),
"url": url,
"title": title,
"openerTargetId": opener if isinstance(opener, str) and opener else None,
})
return result
@@ -948,9 +953,12 @@ class RealBrowserBackend:
raise WorkerError("candidate could not be focused")
# Select all without an arbitrary page script. Input.insertText is a
# single bounded browser operation and is substantially faster than
# dispatching up to 8,000 individual key events.
user_agent = self._helpers.cdp("Browser.getVersion").get("userAgent", "")
modifiers = 4 if "Mac OS X" in user_agent or "Macintosh" in user_agent else 2
# dispatching up to 8,000 individual key events. Browser.getVersion is
# stable for this backend, so cache the platform modifier for its life.
if getattr(self, "_select_all_modifier", None) is None:
user_agent = self._helpers.cdp("Browser.getVersion").get("userAgent", "")
self._select_all_modifier = 4 if "Mac OS X" in user_agent or "Macintosh" in user_agent else 2
modifiers = self._select_all_modifier
select_all = {"key": "a", "code": "KeyA", "modifiers": modifiers,
"windowsVirtualKeyCode": 65, "nativeVirtualKeyCode": 65,
"commands": ["SelectAll"]}