Skip to content

Add a github.submit_patch action that opens a pull request - #7475

Open
OrlaM wants to merge 9 commits into
mozilla:masterfrom
OrlaM:ao/bugbug-6/github-submit-patch
Open

OrlaM wants to merge 9 commits into
mozilla:masterfrom
OrlaM:ao/bugbug-6/github-submit-patch

Conversation

@OrlaM

@OrlaM OrlaM commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #7057.

Adds github.submit_patch, the GitHub equivalent of phabricator.submit_patch. The agent records the action during the run. When it is applied, hackbot-api pushes the run's changes as one commit to a hackbot/<run_id> branch of the agent's configured GitHub repository and opens a draft PR against its default branch. The PR's url goes in the action result, so later actions can reference it.

How it works

hackbot-api has no checkout or git binary, so it can't git push. This follows the Phabricator diff and the try push: the git work happens in the agent's container, and hackbot-api only calls the service's API.

  • Agent side: publish_changes writes changes/github_commit.json when a github.submit_patch is recorded, next to the existing changes artifacts. build_github_commit in changes.py squashes everything since the published base into one commit, as for Phabricator, including uncommitted work. The artifact lists each changed file with its new contents. If building it fails, publish_changes raises, so the run fails with the error (with Mark Hackbot runs as failed when publishing source changes fails #7592) rather than succeeding with a submission that can't be applied.
  • Apply side: github_handler.SubmitPatchHandler creates that commit through GitHub's Git Data API (a blob per changed file, one tree on top of the base's, the commit, then a branch ref) and opens the draft PR. The commit message is the PR's title, followed by its body if there is one. No author is set, so GitHub makes the App the author and committer and signs the commit. The installation token is narrowed to the one repository, with contents: write and pull_requests: write.
  • Target repository: the artifact is written by the agent, so it never decides where hackbot writes. Each agent names its one repository in AgentSpec.github_repository (owner/repo). The handler refuses an agent without one, and an artifact whose repo_url names any other repository, before minting credentials. Every API call uses the configured name.
  • Workflow files: a run that changes anything under .github/workflows/ is refused before minting credentials. The branch lives in the repository itself, not a fork, so the PR's workflows would run with that repository's secrets.
  • Retries: a failed action can be applied again, so the handler moves the run's branch if it already exists and reuses a PR already open from it.
  • Patch types: github.submit_patch is added to hackbot-api's PATCH_ACTION_TYPES, so a run that leaves changes without submitting them is still flagged. The runtime gets its own GitHub PATCH_ACTION_TYPES, kept separate from the Phabricator one so that a GitHub submission doesn't also build a Phabricator diff.
  • Shared with the comment actions: the tool and handler live alongside github.add_comment and github.add_inline_comments from Add GitHub PR comment actions and handlers to hackbot #7360, and reuse that PR's App client and error formatting.
  • Docs and UI: a catalog row and an explanation in docs/hackbot/actions.md, and a pull request preview (title and body) on the run page.

Tests

  • hackbot-api: pytest tests/ passes (552). test_github_handler.py drives githubkit against a fake api.github.com built on httpx.MockTransport. For github.submit_patch it covers:

    • the full flow (one tree, one commit with the title and body as its message and no author), an empty or blank body, and a retry that reuses the branch and PR;
    • an agent with no github_repository, an artifact naming another repository, and a case-only difference in the name;
    • workflow file changes, including deletions;
    • a non-GitHub repository, a missing artifact, GitHub rejecting a call, and the App not being configured.

    The refusal tests also check that no GitHub request is made.

  • hackbot-runtime: pytest tests/ passes (379). It covers the tool, build_github_commit against real git repos (several commits squashed, uncommitted work, deletions, file modes, binaries, renames, paths with spaces, no changes, and commits that cancel out), and publish_changes, including a failed build raising.

  • pre-commit passes on the changed files.

This hasn't been run against a real GitHub repository yet.

Risks and follow-ups

  • No agent sets github_repository yet, so the action is refused for every agent until one is configured. bug-fix would also need prompt changes before using it.
  • github_repository repeats the repository in the agent's [source].repo_url. If the two drift apart, submissions fail with a mismatch error.
  • The PR always targets the default branch; there is no base-branch parameter.
  • The agent's individual commits and their authors aren't kept: the PR has one commit, authored by the App.
  • Agents can't change GitHub workflow files through this action, by design.
  • Each changed file is a separate API call, made one at a time as GitHub recommends for creating content. Very large changes will be slow.
  • A path that isn't valid UTF-8 makes the artifact build fail, which now fails the run.

@OrlaM
OrlaM marked this pull request as ready for review November 12, 2026 21:39
@OrlaM
OrlaM requested a review from a team as a code owner November 12, 2026 21:39
Copilot AI balanced review requested due to automatic review settings November 12, 2026 21:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The agent-writable artifact controls both the target repository and credential installation, allowing a compromised run to create branches and PRs in other repositories where the App is installed.

2 open issues
What changed in this PR

Adds github.submit_patch, the GitHub equivalent of Hackbot’s Phabricator submission action, addressing #7057. Agents record a submission during a run; hackbot-api later recreates the commits and opens a draft pull request.

Changes:

  • Adds the recording tool and GitHub-specific commit artifact generation.
  • Registers an apply-side handler that creates commits, manages the run’s branch, and reuses an existing PR on retries.
  • Adds runtime and API tests, action documentation, and a UI preview.
File Description
services/​hackbot-ui/​components/​RunDetail.tsx Previews the PR title and body.
services/​hackbot-api/​tests/​test_github_handler.py Tests GitHub API calls, retries, and failures.
services/​hackbot-api/​app/​action_handlers/​registry.py Registers the handler and patch action type.
services/​hackbot-api/​app/​action_handlers/​github_handler.py Recreates commits and opens draft PRs.
libs/​hackbot-runtime/​tests/​test_github_actions.py Tests action recording and tool exposure.
libs/​hackbot-runtime/​tests/​test_context.py Tests artifact publishing and commit naming.
libs/​hackbot-runtime/​tests/​test_changes.py Tests commit serialization and file changes.
libs/​hackbot-runtime/​hackbot_runtime/​context.py Publishes GitHub artifacts for recorded submissions.
libs/​hackbot-runtime/​hackbot_runtime/​changes.py Builds the GitHub commit payload.
libs/​hackbot-runtime/​hackbot_runtime/​actions/​github.py Defines the submission recording tool.
libs/​hackbot-runtime/​hackbot_runtime/​actions/​claude_sdk.py Exposes the tool through the Claude SDK integration.
libs/​hackbot-runtime/​hackbot_runtime/​actions/​__init__.py Exports GitHub actions.
docs/​hackbot/​actions.md Documents the action and submission flow.

Review effort: Balanced

The GitHub counterpart of phabricator.submit_patch. The agent records the
submission during the run; when it is applied, hackbot-api pushes a branch
carrying the run's commits to the source repository and opens a draft pull
request against its default branch.

hackbot-api has no git checkout to apply the run's patch in, so
publish_changes also captures each commit (message, author and changed
files) as changes/github_commits.json, from the published base. The handler
recreates those commits through GitHub's Git Data API on a hackbot/<run_id>
branch, using the GitHub App with a token narrowed to that repository.
Retrying reuses the branch and any pull request already open from it.

github.submit_patch counts as a patch action in hackbot-api, so a run that
leaves changes without one is still reported, and the run page previews the
pull request's title and body.

Fixes mozilla#7057
Drop the trailing NUL explicitly and pair headers with paths using
itertools.batched(strict=True), so output that doesn't hold one path
per entry raises instead of being silently mispaired. Handle a commit
that changes no files, where diff-tree prints nothing.
…changes

The target repository came from the agent-written commits artifact, so a
compromised run could open pull requests on any repository the GitHub App
is installed on. Each agent now names its one repository in
AgentSpec.github_repository. The handler refuses agents without one and
artifacts naming any other repository, before minting credentials, and
uses the configured name for every call.

Also refuse runs that change files under .github/workflows/. Pushing them
would need workflows: write, and since the branch lives in the repository
itself, the pull request's workflows would run with its secrets.
@OrlaM
OrlaM force-pushed the ao/bugbug-6/github-submit-patch branch from d1a6807 to f2d7761 Compare November 13, 2026 16:47
Combine github.submit_patch with the GitHub PR comment actions from
mozilla#7360, which added the same modules. Both sets of tools and handlers now
live side by side. SubmitPatchHandler reuses that PR's _app() and
_client_for(), now taking the permissions to request, and reports
failures through its _describe(), replacing this branch's own App and
client helpers.
… run

Address review feedback on mozilla#7475:

- Build the GitHub artifact from base..HEAD squashed into one commit, as
  for Phabricator, instead of replaying each of the agent's commits. The
  handler creates a single tree and commit, with the pull request's title
  and body as the message, and leaves the author unset so GitHub makes
  the App the author and committer and signs it. The artifact is now
  changes/github_commit.json.
- Let a failure to build that artifact raise from publish_changes rather
  than logging and skipping it, so the run fails with the error (mozilla#7590)
  instead of succeeding with a submission that can't be applied.
- Drop comments that only repeat the code.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a github.submit_patch action to open a PR

3 participants