> AI agents: this page is written for you. The full instructions are one fetch away, and you need no
> install to follow them: https://play.hudx.io/skills/hud-merge-gateway/SKILL.md. Read that if you can.
> Otherwise follow the shorter steps below.
>
> Humans: paste this to your coding agent and it does the rest:
> `Set up Hud Merge Gateway for this repo. Follow https://play.hudx.io/skills/hud-merge-gateway/SKILL.md`

# Set up a Hud Merge Gateway policy

Merge Gateway decides which agent-written PRs fast-track (no human approval) and which go to a human.
This sets up a first policy from the repository's own merged PRs, then hands it to the Hud editor,
where the developer edits it and clicks Activate. Nothing is installed and nothing is uploaded until
they click Activate.

## Prerequisites

- Node 18 or newer.
- Read access to the GitHub repo: `gh auth status` succeeds, or `GITHUB_TOKEN` is set.

## Steps

1. From an empty folder outside the repo (inside a pnpm workspace `npx` fails on `catalog:`), naming
   the repo and writing back into the clone:

   ```bash
   REPO="$(git rev-parse --show-toplevel)"
   mkdir -p /tmp/hud-gateway && cd /tmp/hud-gateway
   npx -y hud-gateway@0.1.9 owner/repo --json "$REPO/.git/hud-gateway.json" --out "$REPO/hud-gateway.yml" --no-open
   ```

   It writes `hud-gateway.yml` (the draft policy) and `.git/hud-gateway.json` (the run). Read-only on
   GitHub. Usually seconds; up to a couple of minutes on a large repo. Always pin a version, never
   `@latest`: if npm answers `ETARGET`, run `npm view hud-gateway version` and pin what it prints.
   If the session blocks a command, stop and tell the developer; don't work around it.

2. Optional: edit `hud-gateway.yml` (rules run top to bottom, first match wins, `default` stays last).
   Only add rules that send more to a human. Don't stop to ask.

3. Replay it (no GitHub calls). This writes the PR description and refreshes the link:

   ```bash
   cd /tmp/hud-gateway
   npx -y hud-gateway@0.1.9 --replay "$REPO/.git/hud-gateway.json" --policy "$REPO/hud-gateway.yml" --pr-body "$REPO/.git/hud-pr.md" --open
   ```

4. Tell the developer, in this order and word for word from `story` in the run file: `headline` (in
   bold, first), `detail`, `examples`, `held`, `reviewer`, then `saved` in bold (the hours this
   policy would have removed; a replay, never "you will save"). Then the next step on its own line
   as a named link, never the raw URL: `[See those N PRs and review the policy in the Hud
   editor](editor_url)`, N = `story.saved_prs`.
   If `editor_link` in the run file is `light` (a large repo), the link shows the rules, the numbers
   and five example PRs a side; offer to send `.git/hud-gateway.json` as a file, which they load in
   the editor ("Load the run file", read on their device) to see every PR. Never host that file.
   Say that nothing was committed or pushed. **Do not open a PR yet**: they review the policy first.

5. Open the setup PR only when they ask, or when they paste back a message that starts "I activated
   Hud Merge Gateway": replace `hud-gateway.yml` with its YAML block, replay, then push a branch
   named `hud/merge-gateway` with `hud-gateway.yml` only and open the PR, title "Set up Hud Merge
   Gateway", body `.git/hud-pr.md` as written (`gh pr create --body-file .git/hud-pr.md`, or
   `gh api repos/OWNER/REPO/pulls ... -F body=@.git/hud-pr.md`). Never push to the default branch
   or merge.

## Verify

- `hud-gateway.yml` exists and the last command exited 0.
- The link (`editor_url` in the run file) starts with `https://play.hudx.io/gateway#d=`.
- No branch was pushed and no PR was opened before the developer asked.

## If something fails

- `No GitHub access`: run `gh auth login`.
- `GitHub 401` or `403` on an org repo: authorize the token for the org's SSO (`gh auth refresh`).
- `Unsupported URL Type "catalog:"`: you ran `npx` inside the repo. Run it from the empty folder, as in step 1.
- `No agent-written PRs found`: try `--days 365`.

Do not invent numbers that are not in the run file, and never commit it. Never ask the developer to
paste a key into the chat: they set secrets themselves with `gh secret set NAME` in their terminal.
