Skip to content

Latest commit

 

History

History
235 lines (186 loc) · 14.6 KB

actions.md

File metadata and controls

235 lines (186 loc) · 14.6 KB

Actions: record now, apply later

An agent never mutates Bugzilla, Phabricator, TestRail or Slack, and never sends mail, while it runs. It calls a tool that records what it intends to do; hackbot-api performs it after the run has finished and is known good.

Why the indirection:

  • A run that fails halfway leaves no half-applied side effects.
  • Every intent is reviewable — visible in summary.json and in the UI, with the agent's own stated reasoning, before anything lands.
  • Applying is idempotent and retryable, which matters because the event delivery that drives it is at-least-once.
  • Auto-apply is per-agent opt-in, so a new or unproven agent can run in propose-only mode with no code change.

Recording (inside the run)

The write-action tools are declared in hackbot_runtime/actions/ — one module per domain, using the same @tool decorator as read tools. Calling one records the intent against the run's ActionsRecorder instead of performing it.

The recorded list becomes summary.json's actions array. Any attached files are published as run artifacts and referenced by key, since the local path disappears with the container.

The catalog

Each action type has a declaration the agent calls and a handler that applies it. Declarations live in the runtime; handlers live in hackbot-api. app/action_handlers/registry.py is the authoritative type → handler map.

As with read tools, nothing is exposed by default: an agent lists the dotted types it may record in its config.py and passes them to actions_server_for, which builds a server carrying only those. bug-fix, for instance, allows phabricator.submit_patch on a fresh triage run but swaps it for phabricator.update_patch on a follow-up.

Action type Records the intent to… Params
bugzilla.update_bug Change a bug's fields bug_id, changes
bugzilla.add_comment Comment on a bug bug_id, text, is_private
bugzilla.add_attachment Attach a file to a bug bug_id, + a file attachment
bugzilla.create_bug File a new bug the new bug's fields, ref
phabricator.submit_patch Deliver a fix as a new revision bug_id, title, summary, test_plan, parent_revision_id
phabricator.update_patch Add a new diff to an existing revision revision_id, title, summary, test_plan
phabricator.add_comment Reply on a revision without changing code revision_id, text
github.add_comment Comment on a pull request conversation repo, pull_number, text
github.add_inline_comments Review a pull request with inline comments repo, pull_number, comments, summary
github.submit_patch Deliver a fix as a new pull request title, body
testrail.submit_test_plan Submit a generated test plan to TestRail the validated feature + test cases
slack.post_message Post a message to Slack channel, text
email.send Email a report about the run to, subject, body_markdown
feedback.report_problem Report a problem to the Hackbot developers severity, title, message
agents.start_run Start another agent on this run's output agent

All but testrail.submit_test_plan take a reasoning argument — a free-text audit trail stored on the action and shown in the UI beside the proposed change. Each tool returns the recorded action's ID, which a later action can reference (see cross-references below).

testrail and slack also provide record_test_plan / record_message helpers that agent code calls directly rather than the model choosing to — for an action the agent always takes once it has a result, not one the model decides on. email provides record_email alongside its tool for the same reason.

slack.post_message is posted without Slack's link and media previews; they can be turned back on for a recorded message by passing unfurl=True to record_message.

bugzilla.add_comment appends a feedback-reaction footer to every recorded comment, and is_private=true marks it security-group-only.

When hackbot-api posts a Bugzilla comment, it puts a front matter block at the top that links the comment to its run:

---
hackbot:
  run: <run ID>
  action: <RunAction row ID>
  agent: <agent name>
  url: https://hackbot.moz.tools/runs/<run ID>
---

BMO's Hackbot extension hides the block from readers, bugmail, webhooks and Pulse, and shows the agent and a link to the run in the comment header instead. REST raw_text keeps it. The block is added at apply time, not recorded, so a model can't write or change it and the Hackbot UI and Slack never show it. BMO only reads run, action and agent when they are identifiers, which run IDs, row IDs and registered agent names always are. When field updates are merged with a comment, action is the ID of the row the comment came from. Agent text posted without the block (attachment comments and new-bug descriptions) is moved down a line when it starts with a rule, so BMO doesn't read a block the model wrote as the comment's metadata.

Adding a type is a declaration in the runtime's domain module plus one line in hackbot-api's handler registry. The dispatch loop never changes.

The two patch actions

submit_patch and update_patch are deliberately separate, each taking only the parameters its own case needs, so a model cannot create a revision when it meant to update one by getting an optional argument wrong. That is the general pattern for write-actions: prefer several narrow tools over one with mode flags.

Neither takes a patch file. The agent's final working-tree state is the diff — which is why the runtime builds the Phabricator payload during publish_changes, while the checkout still exists.

github.submit_patch is the GitHub counterpart of phabricator.submit_patch, for an agent whose [source].repo_url is on GitHub. hackbot-api has no checkout to apply a patch in, so publish_changes also captures the run's changes, squashed into one commit as for Phabricator, as changes/github_commit.json: each changed file with its new contents. If that fails, the run fails. The handler creates the commit through GitHub's Git Data API, with the pull request's title and body as its message, on a hackbot/<run_id> branch of the repository. It then opens a draft pull request against the default branch, with the GitHub App's credentials (so the App must be installed on that repository, with contents: write and pull_requests: write). The App is the commit's author and committer, and signs it. Retrying reuses the branch and any pull request already open from it.

The artifact is written by the agent, so it never decides where hackbot writes. The target is the agent's github_repository in AGENT_REGISTRY, an owner/repo that must match its [source].repo_url. An agent without one can't submit, and an artifact naming any other repository is refused before credentials are minted. A run that changes files under .github/workflows/ is also refused. The branch is pushed to the repository itself, not a fork, so its pull request's workflows would run with that repository's secrets.

Hooks

ActionsRecorder supports per-type hooks that run before an action is appended. A hook may mutate the action (enrichment) or raise (validation gate — nothing is recorded and no attachment is published). They live on the recorder rather than in the tool declarations so the runtime can attach cross-cutting behaviour without every handler knowing about it.

The Searchfox permalink expansion on bugzilla.add_comment is the working example: the agent writes {{searchfox.permalink}}/path/to/file.js#412, the hook expands it at record time, and the comment awaiting review already shows clickable URLs.

Applying (after the run)

Triggered by the run.completed event, on a subscription filtered to succeeded runs.

  1. Record rows. Every entry in summary.json's actions is upserted as a run_actions row (pending), keyed (run_id, idx). This happens for all succeeded runs, whether or not the agent auto-applies, so the UI can always show and apply them.
  2. Apply, if opted in. With auto_apply_actions=True on the agent's registry entry, pending rows are applied immediately, once any agent-specific consent check passes. Otherwise they wait for a human to click apply. The agent's always_apply_actions and never_apply_actions override this per action type. require_review=true on the run holds every action, including always_apply_actions.
  3. Dispatch. Each row's type selects a handler from hackbot-api's registry. The handler gets the params and an ApplyContext — which can download_artifact(key) without knowing GCS is behind it.
  4. Stamp. The row records applied or failed, its result, and its error. Only a real success sets applied_at.

Rows are committed one action at a time, and an applied row is never reapplied — so a retried event or a repeated manual apply-all is safe and resumes where it stopped.

Actions from a run that is not succeeded are never applied. The subscription filters them out and the applier checks again, because acting on a run that never reached a verified-good state is not wanted even if it recorded something before erroring.

Chaining agents

Agents stay small and single-purpose; one hands its work on to the next by recording agents.start_run. The model decides whether the work continues, and in which agent, but only among the agents its code declared with allow_agents(recorder, [...]); any other name is refused as a tool error, and a run that declared none can start nothing. Nothing starts until the run has succeeded and the action is applied, so the parent agent lists it in always_apply_actions. On a run with require_review=true, the next agent starts only once someone applies the action.

The handler starts the named agent with {"parent_run_id": <this run>} (agents_handler.py). An agent that accepts a parent declares a parent_run_id input; the API then checks the parent exists and has succeeded, and passes signed download URLs for its artifacts, which the runtime fetches before main() — see runtime.md.

Cross-action references

An action's result often isn't known until it's applied — a Phabricator revision has no URL until it exists. So a later action can reference an earlier one's result by the action ID returned when it was recorded:

submit_patch(...)  # -> "Recorded phabricator.submit_patch (ID: 1a2b3c)."
add_comment(text="Patch up for review: {{actions.1a2b3c.url}}")

A new bug works the same way, through its bug_id:

create_bug(...)  # -> "Recorded bugzilla.create_bug (ID: 4d5e6f)."
add_comment(revision_id=..., text="Filed bug {{actions.4d5e6f.bug_id}}")

{{actions.<action_id>.<field>}} is substituted at apply time, recursively through params. Resolution draws on rows already applied in earlier passes as well as this one, so a later manual apply can still reference an earlier action's result.

An unresolvable placeholder is left as-is and logged, rather than raising. The action then fails with an error a human can read, instead of silently posting mangled text.

Bugzilla coalescing

Same-bug field changes are merged with the closest comment into a single PUT /bug/{id}, so Bugzilla applies them as one transaction — one bugmail, one history entry, instead of a burst. Other comments on that bug still apply separately. A group is applied at its last member's index, once every earlier dependency has resolved, and any group with a member another pending action references is excluded (that action could run before the group is applied).

GitHub review grouping

When general and inline comments for the same GitHub PR are pending in the same apply pass, they are submitted together as one review. General comment text and existing review summaries are joined with blank lines in action order. General comments without inline comments still post to the PR conversation.

Only actions selected for that apply pass are grouped. Already-applied actions are skipped, and groups containing an action referenced by another pending action are excluded. Each grouped action retains its own status; general comments return the review URL as their comment_url.

Where to look

Concern File
Recording mechanics, hooks libs/hackbot-runtime/hackbot_runtime/actions/recorder.py
Action declarations (per domain) hackbot_runtime/actions/
Apply-side handlers services/hackbot-api/app/action_handlers/
Type → handler map app/action_handlers/registry.py
Orchestration, refs, coalescing services/hackbot-api/app/actions_applier.py

Record side and apply side live in different packages: declarations ship in the agent container with hackbot-runtime, handlers run in hackbot-api.