Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions agents/bug-fix/hackbot_agents/bug_fix/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ async def run_bug_fix(
# derives fx_ctx from the prepared source checkout and the agent's
# hackbot.toml. Provision the config before the session can call build tools.
_write_mozconfig(fx_ctx)
fx_ctx.bootstrap_before_build = True
firefox_server = build_sdk_server("firefox", fx_ctx, firefox.TOOLS)

user_prompt = select_workflow(
Expand Down
4 changes: 2 additions & 2 deletions agents/bug-fix/hackbot_agents/bug_fix/prompts/system.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Your working directory is the source repository for the product these bugs are f
When a bug needs reproducing or fixing, you have `firefox` MCP tools:

- `evaluate_testcase(content, filename, prefs?, timeout?)` — runs a testcase in Firefox under xvfb via grizzly (the build's sanitizer configuration depends on the configured mozconfig). Returns `crashed` (bool), `crashed_parent` (bool — parent-process crash), `logs` (stderr/stdout and, if crashed, `crashdata`), and the testcase bundle. When `crashed=false`, inspect `logs.stderr` for why (JS exception, gated pref, etc).
- `build_firefox()` — runs `./mach build` with the preconfigured mozconfig. Use it for every Firefox build instead of building or checking for a build from Bash.
- `bootstrap_firefox(application?)` — runs `./mach bootstrap` to install the build toolchain (rustc, cargo, clang, cbindgen). The image does not ship one, so if `build_firefox` reports a missing toolchain (e.g. no rustc or clang), call this once and then build again. Do not give up because a toolchain is missing.
- `build_firefox(target?)` — runs `./mach build` with the preconfigured mozconfig, after a one-time `mach bootstrap`. Use it for every Firefox build instead of building from Bash. Pass `target` (a directory) for a quick compile check.
- `bootstrap_firefox(application?)` — sets up the toolchain for another application, e.g. `browser_artifact_mode` for an artifact build.

For Android bugs (GeckoView, Fenix, Focus, android-components, anything under `mobile/android`), call `bootstrap_firefox(application="mobile_android_artifact_mode")` (or `"mobile_android"` if you need to change C++ or Rust code), which installs a JDK and the Android SDK and NDK; `java`, `adb`, `aapt2`, `apksigner`, `apkanalyzer` and `lint` are then on PATH. It also writes a mozconfig for Android, which `build_firefox` then builds with; its result says where, and to run mach from Bash with it, e.g. `MOZCONFIG=<that path> ./mach gradle -p mobile/android/android-components :components:support-base:testDebugUnitTest` (use `./mach gradle` instead of `gradlew`, with `-p` for a subproject). JVM unit tests (Robolectric) work; there is no Android emulator or device, so instrumented tests can't run.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging

from agent_tools.firefox import linux_build_env
from hackbot_runtime import HackbotContext, run_async
from hackbot_runtime.actions.email import record_email
from hackbot_runtime.actions.phabricator import PATCH_ACTION_TYPES
Expand All @@ -8,7 +9,7 @@
from .agent import BuildRepairResult, run_build_repair
from .config import CHECKOUT_DEPTH, NOTIFY_ONLY_WITH_PATCH
from .notify import build_email, recipients, resolve_author_email
from .resolve import PushInfo, linux_build_env, resolve_push
from .resolve import PushInfo, resolve_push

logger = logging.getLogger(__name__)

Expand Down
34 changes: 1 addition & 33 deletions agents/build-repair/hackbot_agents/build_repair/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import contextlib
import json
import os
import re
import subprocess
import sys
Expand Down Expand Up @@ -137,37 +136,6 @@ def _build_options(
)


def _setup_ci_build(fx_ctx: FirefoxContext, env: dict[str, str]) -> None:
"""Build as the failing CI task did: its toolchains and mozconfig."""
fetches = [f"{f['task']}:{f['artifact']}" for f in json.loads(env["MOZ_FETCHES"])]
fetches_dir = fx_ctx.source_dir.parent / "fetches"
fetches_dir.mkdir(exist_ok=True)
# A reused workspace keeps the in-tree mozconfig, which mach refuses outside automation.
fx_ctx.mozconfig.unlink(missing_ok=True)
subprocess.run(
[str(fx_ctx.source_dir / "mach"), "--no-interactive", "artifact"]
+ ["toolchain", "--from-task", *fetches],
cwd=fetches_dir,
check=True,
stdout=sys.stderr,
stderr=sys.stderr,
)
# mach takes in-tree mozconfigs only in automation.
os.environ.update(
{
k: env[k]
for k in ("DISABLE_CLANG_PLUGIN", "FORCE_GCC", "PERFHERDER_EXTRA_OPTIONS")
if k in env
},
MOZ_AUTOMATION="1",
MOZ_FETCHES_DIR=str(fetches_dir),
)
fx_ctx.mozconfig.write_text(
f'. "$topsrcdir/{env["MOZCONFIG"]}"\n'
f"mk_add_options MOZ_OBJDIR={fx_ctx.objdir}\n"
)


def _write_mozconfig(fx_ctx: FirefoxContext) -> None:
"""Write a mozconfig mirroring the failing CI build, unless one exists.

Expand Down Expand Up @@ -399,7 +367,7 @@ async def run_build_repair(
# Stage 2: fix (lower effort, edits the source tree and verifies it
# builds against a mozconfig that mirrors the failing CI config).
if ci_env:
_setup_ci_build(fx_ctx, ci_env)
await fx_ctx.configure_ci_build(ci_env)
else:
_write_mozconfig(fx_ctx)
fix_label = (
Expand Down
8 changes: 4 additions & 4 deletions agents/build-repair/hackbot_agents/build_repair/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@
been read, which costs a turn. To see how a commit handled comparable files, run
`git show <sha> -- <dir>` rather than guessing a sibling's name.

Logs already fetched sit under {scratch_out}/logs. A mozconfig that mirrors the failing CI configuration is already set up. Verify the fix compiles with the build_firefox tool, passing
Logs already fetched sit under {scratch_out}/logs. The mozconfig and any supported
CI toolchains are already set up. Verify the fix compiles with build_firefox, passing
the directory of the file you changed as `target` (e.g. 'docshell/base') for a
fast, focused build -- prefer this over a full tree build. If the build reports a
missing toolchain (e.g. rustc or clang), run the bootstrap_firefox tool once and
then build again. Verify via the build_firefox tool rather than a raw `./mach
fast, focused build -- prefer this over a full tree build. Do not run
bootstrap_firefox over CI toolchains. Verify via the build_firefox tool rather than a raw `./mach
build` so the build result is recorded.
{try_push}{report}"""

Expand Down
25 changes: 0 additions & 25 deletions agents/build-repair/hackbot_agents/build_repair/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
derives everything it reports from a task id.
"""

import json
import logging
import platform
import re
from dataclasses import dataclass, field

Expand Down Expand Up @@ -53,29 +51,6 @@ def _task(task_id: str) -> dict:
return _get_json(_TC_TASK_URL.format(task_id=task_id))


def linux_build_env(task_id: str) -> dict[str, str] | None:
"""The env of a build task CI ran on Linux, or None for any other task.

Its ``MOZCONFIG`` and ``MOZ_FETCHES`` say how to build exactly as CI did.
Fetches with a ``dest`` or without extraction are not supported, and CI's
toolchains only run on an x86-64 host.
"""
if platform.machine() != "x86_64":
return None
task = _task(task_id)
env = (task.get("payload") or {}).get("env") or {}
if not (
task.get("workerType", "").startswith("b-linux")
and env.get("MOZCONFIG")
and env.get("MOZ_FETCHES")
):
return None
fetches = json.loads(env["MOZ_FETCHES"])
if any("dest" in f or not f.get("extract", True) for f in fetches):
return None
return env


def _task_env(task: dict) -> dict:
return (task.get("payload") or {}).get("env") or {}

Expand Down
89 changes: 0 additions & 89 deletions agents/build-repair/tests/test_ci_build.py

This file was deleted.

27 changes: 24 additions & 3 deletions agents/build-repair/tests/test_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def _run(
push_bug=None,
blame=None,
checkout=None,
ci_env=None,
):
"""Run the agent with both sessions faked, returning their (options, prompt).

Expand Down Expand Up @@ -64,22 +65,42 @@ async def fake_session(reporter, options, prompt, captured, tracked):
agent.run_build_repair(
bugzilla_mcp_server={"type": "http", "url": "http://broker/mcp"},
source_repo=tmp_path,
fx_ctx=SimpleNamespace(
mozconfig=tmp_path / ".mozconfig", objdir=tmp_path / "objdir"
),
fx_ctx=agent.FirefoxContext.from_source_repo(tmp_path, objdir="objdir"),
bug_id=bug_id,
commit_bugs={FAILURE_COMMIT: push_bug} if push_bug else {},
git_commits=[FAILURE_COMMIT],
project="autoland",
hg_revision="abc123",
failure_tasks={"build-linux": "taskid"},
ci_env=ci_env,
actions_recorder=actions_recorder,
checkout=checkout,
)
)
return result, sessions[0], sessions[1] if len(sessions) > 1 else None


@pytest.mark.parametrize("blame", [None, ""])
def test_ci_build_is_configured_only_for_the_fix_stage(tmp_path, monkeypatch, blame):
env = {"MOZCONFIG": "browser/config/mozconfigs/linux64/nightly"}
configured = []

async def configure(self, env):
configured.append(env)

monkeypatch.setattr(agent.FirefoxContext, "configure_ci_build", configure)
_run(
tmp_path,
monkeypatch,
bug_id=1,
actions_recorder=None,
ci_env=env,
blame=blame,
)

assert configured == ([env] if blame is None else [])


# --- the fix stage works on the blamed commit itself ----------------------- #


Expand Down
9 changes: 9 additions & 0 deletions docs/hackbot/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ locally against the checkout.
full tree, much less incrementally. An agent that only needs to confirm a localized fix
compiles should pass `target` to build one directory instead of the whole tree.

### Bootstrap and CI builds

Set `ctx.firefox.bootstrap_before_build = True` to run `mach bootstrap` once before
the first build. Bug-fix does this. An explicit `bootstrap_firefox` call also counts.

To build as a Linux CI task did, call
`await ctx.firefox.configure_ci_build(linux_build_env(task_id))` before building.
It downloads the task's toolchains and uses its mozconfig.

## Adding a tool

1. Write the handler in the right domain module, `@tool`-decorated, `ctx` first, returning
Expand Down
Loading