# tawa gate — The Merge Gate

## The Short Version

`tawa gate` (tawa 0.37.0+) is the one command that says whether a branch may merge. Run it before `git_pr_merge`. It behaves the same locally, in a tawa-runner cloud session, in Chaac and in a Forgejo Actions job.

```bash
tawa gate            # human output, exit 0 = pass, 1 = a step failed
tawa gate --json     # one JSON document on stdout, for CI and agents
```

## What it runs

1. If `package.json` has a `gate` script, `<pm> run gate`. The repo owns its gate (iec-portal's four steps can live there).
2. Otherwise, in order: **typecheck** (the `typecheck` script, else `tsc --noEmit` when a `tsconfig.json` and typescript exist), **lint**, **test**, **build**.

Rules:

- A step is skipped **only** when the repo has no script for it (npm's placeholder `test` counts as none). Every skip is printed, with the reason, and recorded in `--json`.
- The first failure stops the run and is named: `Gate failed at step "lint"`.
- `<pm>` is pnpm, yarn or npm by lockfile. `CI=true` is set for every step so watch modes and prompts behave the same everywhere.
- Exit code `0` all passed or skipped; `1` a step failed or there is no `package.json`.

> **Why it exists.** On 2026-10-14 iec-portal PR #1284 (`af96270`) merged and auto-deployed to prod having run typecheck and tests only. `npm run build` and lint were in that repo's gate by CLAUDE.md, but the gate was a self-check. One command, run the same way everywhere, is what lets CI make it required.

## `--json` schema

```jsonc
{
  "schemaVersion": 1,
  "ok": false,
  "mode": "default",              // "script" when package.json has a gate script
  "packageManager": "npm",        // npm | pnpm | yarn
  "cwd": "/work/repo",
  "durationMs": 41230,
  "failedStep": "lint",           // null when ok
  "steps": [
    { "name": "typecheck", "command": "npm run typecheck", "status": "passed", "exitCode": 0, "durationMs": 9120, "outputTail": "…" },
    { "name": "lint", "command": "npm run lint", "status": "failed", "exitCode": 1, "durationMs": 3100, "outputTail": "…" },
    { "name": "test", "command": "npm run test", "status": "not-run", "exitCode": null, "durationMs": 0, "outputTail": "" },
    { "name": "build", "command": "npm run build", "status": "skipped", "exitCode": null, "durationMs": 0,
      "skipReason": "no \"build\" script in package.json", "outputTail": "" }
  ]
}
```

| Field | Notes |
|---|---|
| `steps[].name` | `gate`, `typecheck`, `lint`, `test`, `build` |
| `steps[].status` | `passed`, `failed`, `skipped` (no script; `skipReason` set), `not-run` (an earlier step failed) |
| `steps[].exitCode` | `null` for skipped and not-run |
| `steps[].outputTail` | last 40 lines of combined output, ANSI stripped |

Step output is captured, not streamed, in `--json` mode, so stdout is always parseable.

## Where it is enforced

The agile-workflow step 8 is `tawa gate`. A required `gate` status check in Forgejo (insureco/forgejo-custom#1) runs the same command on every PR to `main`/`master`, so a failing build or lint cannot merge from any path, including `tawa-pr merge` in a cloud session.

The flow for an agent: run `tawa gate` locally, open the PR, then merge with `git_pr_merge` or `tawa-pr merge`. The required check takes about 5-10 minutes, so the merge call usually lands while it is still running. In that case it **schedules** the merge (Forgejo auto-merge, `merge_when_checks_succeed`) instead of failing, and returns success (`tawa-pr merge` prints that it is scheduled and exits 0; jci-mcp's `git_pr_merge` returns `scheduled: true`, via a parallel PR in jci-mcp). The agent moves on and does not poll. If the gate fails, the PR stays open and the failure must be fixed and pushed; the call names the failing check. `tawa-pr merge --now` keeps the old one-shot behaviour.

### The two required checks and their reasons (forgejo-custom#22)

| Check | Runs | Re-runs on | Reason status |
|---|---|---|---|
| `gate / gate (pull_request)` | `tawa gate` on the gate runner | push, reopen | `gate / why`: failed step + first concrete error (`typecheck failed: error TS2322: …`), cap exceeded + step, install error, or `passed` |
| `dod / dod (pull_request)` | the Definition-of-Done check on the PR body, ~2 s, no PR code | push, reopen, **body edit** | `dod / why`: the missing sections, or `passed` |

Agents cannot read Actions logs over the API, so the `/ why` status is where the reason is.
jci-mcp `git_pr_get` shows it as `checks[].reason` with a `next` step. Fix a DoD failure by
editing the PR body; fix a gate failure by reproducing with `tawa gate` and pushing.

## What NOT to do

- Don't hand-run three of the four commands and call it green. Run `tawa gate`.
- Don't add a `gate` script that is a subset of what the repo needs (it replaces the defaults).
- Don't treat a skip as a pass for a step you meant to have. Add the script.
