|
| 1 | +"""Firefox web-compatibility agent. |
| 2 | +
|
| 3 | +Drives an agent that reproduces a broken-site report in Firefox |
| 4 | +using the Firefox DevTools MCP. The bug is passed either inline as ``bug_data`` |
| 5 | +text or a Bugzilla ``bug_id`` (read via Bugzilla broker). |
| 6 | +Bugzilla "writes" are recorded as actions into summary.json, never posted. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import sys |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +from claude_agent_sdk import ( |
| 15 | + ClaudeAgentOptions, |
| 16 | + ClaudeSDKClient, |
| 17 | + McpServerConfig, |
| 18 | + ResultMessage, |
| 19 | +) |
| 20 | +from hackbot_runtime import ActionsRecorder, AgentError, HackbotAgentResult |
| 21 | +from hackbot_runtime.actions import ACTIONS_SERVER_NAME |
| 22 | +from hackbot_runtime.actions.claude_sdk import actions_server_for, actions_to_tool_names |
| 23 | +from hackbot_runtime.claude import Reporter |
| 24 | + |
| 25 | +from .config import BUGZILLA_READ_TOOLS, DEVTOOLS_TOOLS, ENABLED_ACTION_TYPES |
| 26 | +from .devtools_mcp import build_devtools_server |
| 27 | + |
| 28 | +HERE = Path(__file__).resolve().parent |
| 29 | + |
| 30 | +# mode-specific prompt filename under prompts/. Every prompt is |
| 31 | +# system.md (shared rules) + the selected mode file, concatenated. |
| 32 | +PROMPTS = { |
| 33 | + "triage": "triage.md", |
| 34 | + "chrome-mask": "chrome_mask.md", |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +class AutoWebcompatResult(HackbotAgentResult): |
| 39 | + mode: str |
| 40 | + result: str | None = None |
| 41 | + |
| 42 | + |
| 43 | +def load_system_prompt(mode: str) -> str: |
| 44 | + try: |
| 45 | + filename = PROMPTS[mode] |
| 46 | + except KeyError as exc: |
| 47 | + raise AgentError( |
| 48 | + f"unknown mode {mode!r}; expected one of {sorted(PROMPTS)}" |
| 49 | + ) from exc |
| 50 | + base = (HERE / "prompts" / "system.md").read_text() |
| 51 | + mode_specific = (HERE / "prompts" / filename).read_text() |
| 52 | + return f"{base.rstrip()}\n\n{mode_specific.lstrip()}" |
| 53 | + |
| 54 | + |
| 55 | +def build_user_prompt(bug_data: str | None, bug_id: int | None) -> str: |
| 56 | + if bug_data: |
| 57 | + return f"We've received this report:\n\n{bug_data}\n\n" |
| 58 | + if bug_id is not None: |
| 59 | + return ( |
| 60 | + f"Fetch bug {bug_id} using Bugzilla MCP and identify the affected URL " |
| 61 | + "and broken behavior." |
| 62 | + ) |
| 63 | + raise AgentError("neither bug_data nor bug_id was provided") |
| 64 | + |
| 65 | + |
| 66 | +async def run_autowebcompat( |
| 67 | + *, |
| 68 | + bugzilla_mcp_server: McpServerConfig, |
| 69 | + mode: str = "triage", |
| 70 | + bug_data: str | None = None, |
| 71 | + bug_id: int | None = None, |
| 72 | + model: str | None = None, |
| 73 | + max_turns: int | None = None, |
| 74 | + effort: str | None = None, |
| 75 | + firefox_path: str | None = None, |
| 76 | + verbose: bool = False, |
| 77 | + log: Path | None = None, |
| 78 | + actions_recorder: ActionsRecorder | None = None, |
| 79 | +) -> AutoWebcompatResult: |
| 80 | + """Reproduce a web-compat issue and return the agent's findings. |
| 81 | +
|
| 82 | + Returns an :class:`AutoWebcompatResult` on success; raises |
| 83 | + :class:`AgentError` if the agent ends in an error. |
| 84 | + """ |
| 85 | + subject = bug_data if bug_data else f"bug {bug_id}" |
| 86 | + print(f"[autowebcompat] investigating {subject} (mode={mode})", file=sys.stderr) |
| 87 | + |
| 88 | + devtools_server = build_devtools_server( |
| 89 | + firefox_path=Path(firefox_path) if firefox_path else None, |
| 90 | + headless=True, |
| 91 | + enable_script=True, |
| 92 | + ) |
| 93 | + |
| 94 | + # Action-recording MCP server (in-process). Standalone/script runs pass |
| 95 | + # actions_recorder=None and get a local recorder that copies attachments |
| 96 | + # under ./artifacts (no uploader). |
| 97 | + actions_recorder, actions_server = actions_server_for( |
| 98 | + actions_recorder, types=ENABLED_ACTION_TYPES |
| 99 | + ) |
| 100 | + enabled_action_tools = actions_to_tool_names(ENABLED_ACTION_TYPES) |
| 101 | + |
| 102 | + system_prompt = load_system_prompt(mode) |
| 103 | + |
| 104 | + options = ClaudeAgentOptions( |
| 105 | + system_prompt=system_prompt, |
| 106 | + mcp_servers={ |
| 107 | + "bugzilla": bugzilla_mcp_server, |
| 108 | + "firefox-devtools": devtools_server, |
| 109 | + ACTIONS_SERVER_NAME: actions_server, |
| 110 | + }, |
| 111 | + permission_mode="bypassPermissions", |
| 112 | + allowed_tools=[ |
| 113 | + "Read", |
| 114 | + "Grep", |
| 115 | + "Glob", |
| 116 | + "Bash", |
| 117 | + *BUGZILLA_READ_TOOLS, |
| 118 | + *DEVTOOLS_TOOLS, |
| 119 | + *enabled_action_tools, |
| 120 | + ], |
| 121 | + disallowed_tools=["WebFetch", "WebSearch"], |
| 122 | + model=model, |
| 123 | + max_turns=max_turns, |
| 124 | + **({"effort": effort} if effort else {}), |
| 125 | + setting_sources=[], |
| 126 | + # DevTools snapshots/screenshots of complex pages serialize to JSON that |
| 127 | + # can exceed the SDK's default 1 MiB message buffer (the reader dies |
| 128 | + # fatally if it does). Raise it well above that ceiling. |
| 129 | + max_buffer_size=10 * 1024 * 1024, |
| 130 | + ) |
| 131 | + |
| 132 | + user_prompt = build_user_prompt(bug_data, bug_id) |
| 133 | + |
| 134 | + result_msg: ResultMessage | None = None |
| 135 | + with Reporter(verbose=verbose, log_path=log) as reporter: |
| 136 | + reporter.header(subject) |
| 137 | + async with ClaudeSDKClient(options=options) as client: |
| 138 | + await client.query(user_prompt) |
| 139 | + async for msg in client.receive_response(): |
| 140 | + reporter.message(msg) |
| 141 | + if isinstance(msg, ResultMessage): |
| 142 | + result_msg = msg |
| 143 | + |
| 144 | + if result_msg is None: |
| 145 | + raise AgentError(f"{subject}: agent produced no result message") |
| 146 | + if result_msg.is_error: |
| 147 | + raise AgentError( |
| 148 | + f"{subject} investigation failed: {result_msg.result or result_msg.subtype}" |
| 149 | + ) |
| 150 | + |
| 151 | + return AutoWebcompatResult( |
| 152 | + mode=mode, |
| 153 | + result=result_msg.result, |
| 154 | + num_turns=result_msg.num_turns, |
| 155 | + total_cost_usd=result_msg.total_cost_usd, |
| 156 | + ) |
0 commit comments