Skip to content
Open
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
34 changes: 20 additions & 14 deletions agents/perf-triage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ a personal access token, or `gh auth token` if your `gh` login has access. It
is passed as a BuildKit secret and never lands in the image, and compose blanks
it in the agent container, so the model never sees it even if it is in `.env`.

To try a skill change before it is pinned, set `PERF_PLUGIN_LOCAL_DIR` to an
internal-aidev-plugins checkout's `plugins/perf`: compose mounts it, and runs
use it instead of the baked-in plugin, with no rebuild. The log line `perf plugin
at /plugins-local/perf` says when that is the case.

Then build and run from this directory:

```sh
Expand All @@ -57,20 +62,21 @@ whole in the container (deployed with 8Gi for that reason).
`compose.yml`, or the command line. Every `AgentInputs` field must also be listed
in `compose.yml`, or Docker drops it and the default applies silently.

| Env var | Required | Meaning |
| ------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `BUG_ID` | yes | The Bugzilla bug to triage |
| `ANTHROPIC_API_KEY` | yes | Drives the agent (billed per token, roughly $0.25-0.85 a bug) |
| `BUGZILLA_API_URL` | yes | e.g. `https://bugzilla.mozilla.org/rest`. **Broker container only** |
| `BUGZILLA_API_KEY` | yes | **Broker container only.** The agent reads Bugzilla through the broker and never sees the key |
| `BROKER_URL` | yes | Broker base URL; the agent appends `/mcp`. `compose.yml` sets it |
| `SOURCE_REF` | build | Build arg naming the commit the image is built from. Defaults to `dev` |
| `PERF_TRIAGE_PATH` | no | The calculator script. Defaults to the installed `performance-triage-calculator` package; point it at a checkout to test an unreleased calculator |
| `GITHUB_TOKEN` | build | Token that can read internal-aidev-plugins, for the build secret that fetches the `perf` plugin. Blanked in the agent container |
| `MODEL` | no | Model override; unset uses the SDK default |
| `MAX_TURNS` | no | Hard cap on loop iterations, a runaway guard |
| `EFFORT` | no | `low` \| `medium` \| `high` \| `xhigh` \| `max` |
| `BLIND_BUG_ID` | no | Evaluation only: the broker serves that bug as it stood before it was triaged (`hackbot_agents/perf_triage/blind.py`) |
| Env var | Required | Meaning |
| ----------------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `BUG_ID` | yes | The Bugzilla bug to triage |
| `ANTHROPIC_API_KEY` | yes | Drives the agent (billed per token, roughly $0.25-0.85 a bug) |
| `BUGZILLA_API_URL` | yes | e.g. `https://bugzilla.mozilla.org/rest`. **Broker container only** |
| `BUGZILLA_API_KEY` | yes | **Broker container only.** The agent reads Bugzilla through the broker and never sees the key |
| `BROKER_URL` | yes | Broker base URL; the agent appends `/mcp`. `compose.yml` sets it |
| `SOURCE_REF` | build | Build arg naming the commit the image is built from. Defaults to `dev` |
| `PERF_TRIAGE_PATH` | no | The calculator script. Defaults to the installed `performance-triage-calculator` package; point it at a checkout to test an unreleased calculator |
| `GITHUB_TOKEN` | build | Token that can read internal-aidev-plugins, for the build secret that fetches the `perf` plugin. Blanked in the agent container |
| `PERF_PLUGIN_LOCAL_DIR` | no | An internal-aidev-plugins checkout's `plugins/perf` on the host. Set, compose mounts it at `/plugins-local/perf` and runs use it instead of the baked-in plugin |
| `MODEL` | no | Model override; unset uses the SDK default |
| `MAX_TURNS` | no | Hard cap on loop iterations, a runaway guard |
| `EFFORT` | no | `low` \| `medium` \| `high` \| `xhigh` \| `max` |
| `BLIND_BUG_ID` | no | Evaluation only: the broker serves that bug as it stood before it was triaged (`hackbot_agents/perf_triage/blind.py`) |

## Output

Expand Down
8 changes: 8 additions & 0 deletions agents/perf-triage/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,19 @@ services:
- BROKER_URL=http://perf-triage-broker:8765
- ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY:?error}
- ARTIFACTS_DIR=/artifacts
# Only whether it is set matters in here: it opts in to the
# /plugins-local/perf mount below.
- PERF_PLUGIN_LOCAL_DIR
# The build's token, never the run's: blanked here in case it is in
# ../../.env, which env_file would otherwise pass in for the model to see.
- GITHUB_TOKEN=
volumes:
- ${HOME}/hackbot/artifacts:/artifacts
# Set PERF_PLUGIN_LOCAL_DIR to a local internal-aidev-plugins checkout's
# plugins/perf to run against it instead of the plugin baked into the
# image (plugin.py). Unset, an empty placeholder is mounted, which
# plugin.py never reads.
- ${PERF_PLUGIN_LOCAL_DIR:-${HOME}/hackbot/no-local-plugin}:/plugins-local/perf:ro
depends_on:
perf-triage-broker:
condition: service_started
Expand Down
60 changes: 53 additions & 7 deletions agents/perf-triage/hackbot_agents/perf_triage/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,86 @@
table pins (the Dockerfile's `repo-fetch` stage) and keeps the plugin in
/opt/perf-plugin, so the job itself needs no GitHub credential.

Which plugin a run uses:

- /plugins-local/perf, when $PERF_PLUGIN_LOCAL_DIR is set: compose.yml mounts
that host checkout's plugins/perf there, e.g. to try an unmerged skill
change. An explicit choice, so it must be the plugin.
- /opt/perf-plugin otherwise: the one baked into the image. Compose then mounts
an empty placeholder at /plugins-local/perf, which is never read.

The choice is made once per run (selected()).

Plugin skills are enabled by their plugin-qualified names.
"""

import functools
import os
from pathlib import Path

from claude_agent_sdk.types import SdkPluginConfig
from perf_triage_calculator.perf_triage import TOP_SITES_ENV

# Where the Dockerfile puts the plugin it bakes in.
# The host path of a local checkout's plugins/perf. Only compose reads the
# value; in here, being set opts in to MOUNTED_PERF_PLUGIN_DIR.
PERF_PLUGIN_LOCAL_DIR_ENV = "PERF_PLUGIN_LOCAL_DIR"

# In the container: where compose.yml mounts that checkout, and where the
# Dockerfile puts the plugin it bakes in.
MOUNTED_PERF_PLUGIN_DIR = Path("/plugins-local/perf")
BAKED_PERF_PLUGIN_DIR = Path("/opt/perf-plugin")

TRIAGE_SKILL = "perf:triage-perf-bug"
PROFILER_ANALYSIS_SKILL = "perf:profiler-analysis"


def _skill(plugin: Path) -> Path:
return plugin / "skills" / "triage-perf-bug" / "SKILL.md"


def _top_sites(plugin: Path) -> Path:
return plugin / "data" / "top_sites.json"


def _is_plugin(plugin: Path) -> bool:
return _skill(plugin).is_file() and _top_sites(plugin).is_file()


def _select() -> Path:
if os.environ.get(PERF_PLUGIN_LOCAL_DIR_ENV):
plugin = MOUNTED_PERF_PLUGIN_DIR
if not _is_plugin(plugin):
raise RuntimeError(
f"{PERF_PLUGIN_LOCAL_DIR_ENV}={os.environ[PERF_PLUGIN_LOCAL_DIR_ENV]} "
f"is not the perf plugin (no {_skill(plugin)} or "
f"{_top_sites(plugin)} in the mount); point it at a checkout's "
f"plugins/perf"
)
return plugin
if not _is_plugin(BAKED_PERF_PLUGIN_DIR):
raise RuntimeError(
f"no perf plugin at {BAKED_PERF_PLUGIN_DIR}, where the image build puts it"
)
return BAKED_PERF_PLUGIN_DIR


@functools.cache
def selected() -> Path:
"""The plugin this run uses; it stops the run if there is none."""
return _select()


def ensure_perf_plugin() -> Path:
"""Check the plugin is there, export its rankings, and return its directory.
"""Pick the plugin, export its rankings, and return its directory.

Sets $PERF_TRIAGE_TOP_SITES, unless already set, to the plugin's rankings,
which the calculator reads. The run stops without them: triaging without
the rankings changes ratings.
"""
plugin = BAKED_PERF_PLUGIN_DIR
skill = plugin / "skills" / "triage-perf-bug" / "SKILL.md"
if not skill.is_file() or not _top_sites(plugin).is_file():
raise RuntimeError(f"no perf plugin at {plugin}, where the image build puts it")
plugin = selected()
os.environ.setdefault(TOP_SITES_ENV, str(_top_sites(plugin)))
return plugin


def perf_plugin() -> list[SdkPluginConfig]:
return [{"type": "local", "path": str(BAKED_PERF_PLUGIN_DIR)}]
return [{"type": "local", "path": str(selected())}]
12 changes: 7 additions & 5 deletions agents/perf-triage/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
def _perf_plugin_env(monkeypatch):
"""Run every test as the agent runs, after ensure_perf_plugin().

The plugin is in place, with no rankings override. Going through
monkeypatch also undoes what ensure_perf_plugin() writes to os.environ.
The plugin is chosen, with no rankings override and no local plugin. Going
through monkeypatch also undoes what ensure_perf_plugin() writes to
os.environ.
"""
monkeypatch.setattr(plugin, "BAKED_PERF_PLUGIN_DIR", Path("/plugins/perf"))
monkeypatch.setenv("PERF_TRIAGE_TOP_SITES", "")
monkeypatch.delenv("PERF_TRIAGE_TOP_SITES")
monkeypatch.setattr(plugin, "selected", lambda: Path("/plugins/perf"))
for v in ("PERF_TRIAGE_TOP_SITES", "PERF_PLUGIN_LOCAL_DIR"):
monkeypatch.setenv(v, "")
monkeypatch.delenv(v)
13 changes: 13 additions & 0 deletions agents/perf-triage/tests/test_compose_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ def test_the_dockerfile_copies_from_a_repository_the_build_table_names():
assert (agent.parents[1] / "tools/hackbot-deploy/scripts/fetch_repos.py").is_file()


def test_a_local_checkout_is_mounted_where_plugin_py_looks():
from hackbot_agents.perf_triage import plugin

spec = yaml.safe_load(COMPOSE.read_text())
volumes = spec["services"]["perf-triage-agent"]["volumes"]
assert (
"${PERF_PLUGIN_LOCAL_DIR:-${HOME}/hackbot/no-local-plugin}"
f":{plugin.MOUNTED_PERF_PLUGIN_DIR}:ro"
) in volumes
# Passed through so plugin.py knows it was asked for.
assert _env()["PERF_PLUGIN_LOCAL_DIR"] is None


def test_every_module_the_dockerfile_runs_exists():
# Left out of the first split from perf-repro-agent: every run then failed
# closed as "bug lookup failed", which no other test noticed.
Expand Down
55 changes: 55 additions & 0 deletions agents/perf-triage/tests/test_plugin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import os

import pytest
Expand All @@ -13,19 +14,66 @@ def _make_plugin(root):
return p


@pytest.fixture(autouse=True)
def _real_selection(monkeypatch):
# conftest pins the choice for the other tests; these test it, each with a
# fresh cache.
monkeypatch.setattr(plugin, "selected", functools.cache(plugin._select))


@pytest.fixture
def baked(tmp_path, monkeypatch):
p = _make_plugin(tmp_path / "baked")
monkeypatch.setattr(plugin, "BAKED_PERF_PLUGIN_DIR", p)
return p


@pytest.fixture
def local(tmp_path, monkeypatch):
p = _make_plugin(tmp_path / "local")
monkeypatch.setattr(plugin, "MOUNTED_PERF_PLUGIN_DIR", p)
return p


def test_the_baked_in_plugin_is_used_and_supplies_the_rankings(baked):
assert plugin.ensure_perf_plugin() == baked
assert os.environ["PERF_TRIAGE_TOP_SITES"] == str(baked / "data" / "top_sites.json")
assert plugin.perf_plugin() == [{"type": "local", "path": str(baked)}]


def test_a_local_checkout_is_ignored_unless_asked_for(baked, local):
# Compose always mounts something there; only the variable opts in.
assert plugin.ensure_perf_plugin() == baked


def test_asking_for_the_local_checkout_uses_it(baked, local, monkeypatch):
monkeypatch.setenv(
"PERF_PLUGIN_LOCAL_DIR", "/Users/me/internal-aidev-plugins/plugins/perf"
)
assert plugin.ensure_perf_plugin() == local
assert plugin.perf_plugin() == [{"type": "local", "path": str(local)}]


def test_asking_for_a_local_checkout_that_is_not_there_stops_the_run(
baked, tmp_path, monkeypatch
):
# The empty placeholder compose mounts, or a wrong path: never a silent
# fallback to the baked-in plugin.
empty = tmp_path / "empty"
empty.mkdir()
monkeypatch.setattr(plugin, "MOUNTED_PERF_PLUGIN_DIR", empty)
Comment thread
suhaibmujahid marked this conversation as resolved.
Resolved
monkeypatch.setenv("PERF_PLUGIN_LOCAL_DIR", "/Users/me/wrong")
with pytest.raises(RuntimeError, match="PERF_PLUGIN_LOCAL_DIR=/Users/me/wrong"):
plugin.ensure_perf_plugin()


def test_the_choice_is_made_once_per_run(baked, tmp_path, monkeypatch):
assert plugin.ensure_perf_plugin() == baked
# Even if the baked-in plugin went away, the run keeps what it chose.
monkeypatch.setattr(plugin, "BAKED_PERF_PLUGIN_DIR", tmp_path / "gone")
assert plugin.perf_plugin() == [{"type": "local", "path": str(baked)}]


def test_an_explicit_rankings_path_is_kept(baked, monkeypatch):
monkeypatch.setenv("PERF_TRIAGE_TOP_SITES", "/elsewhere.json")
plugin.ensure_perf_plugin()
Expand All @@ -38,6 +86,13 @@ def test_a_plugin_without_the_rankings_stops_the_run(baked):
plugin.ensure_perf_plugin()


def test_a_local_checkout_without_the_rankings_stops_the_run(baked, local, monkeypatch):
(local / "data" / "top_sites.json").unlink()
monkeypatch.setenv("PERF_PLUGIN_LOCAL_DIR", "/Users/me/checkout/plugins/perf")
with pytest.raises(RuntimeError, match="is not the perf plugin"):
plugin.ensure_perf_plugin()


def test_no_plugin_stops_the_run(tmp_path, monkeypatch):
monkeypatch.setattr(plugin, "BAKED_PERF_PLUGIN_DIR", tmp_path / "missing")
with pytest.raises(RuntimeError, match="no perf plugin"):
Expand Down