specgate/docs v1.8.0

The harness#

The harness delivers requirements unattended: it gives each pending requirement to a coding agent, runs the gate on the result, and stops when the gate says no. At the end of this page you can run it on one requirement, chain several onto a branch, run them in parallel, and read a failure.

You need: a project where specgate init has run, git, a test command (specgate config set test_cmd "npm test") and a coding agent you can run from the command line. Which agent, and how to call it, is on Choosing and wiring your agent.

The lifecycle of a requirement Three phases in order. Specify: keep the brief in the repository, draft a module as a change with specgate propose --from-brief, run the same command until the draft is Ready to review, and a person approves it at a terminal with specgate change approve; an agent cannot, and approval writes the feature files. Build: status names the next requirement, a test that names it comes first, then the code, by a person or by the harness in its own branch. Prove: specgate apply is the gate and marks the requirement Implemented only if it passes; specgate archive merges the change once every requirement in it is applied; check runs the whole gate in CI. A requirement that arrives waiting for an answer stays out of Build until the client answers; the answer changes it, so a person approves it again before it rejoins. SPECIFY BUILD PROVE 1 Brief docs/brief.md 2 Draft one module specgate propose --from-brief 3 Check: Ready to review the same command, until it passes 4 A person approves specgate change approve <id> 5 What is next specgate status 6 A test that names it // REQ-007 SCN-007a — … 7 The code by a person or the harness 8 Apply — the gate specgate apply 9 Archive — all applied specgate archive <change-id> 10 The gate, in CI specgate check Waiting for an answer apply and the harness refuse it answered → approve again (4) Nothing is built before a person approves it; nothing counts as delivered until the gate has checked it.
The trust boundary: what the agent's session holds, and what stays outside it Two areas. Inside the trust boundary is the agent's session: the coding agent (Claude Code, Codex or OpenCode), the guard hook, the working folder, the gate on stop and the refusals. Every tool call the agent makes goes to the guard hook, which judges it test first, over the whole working tree, against the step record and against a person's acts. An allowed call reaches the working folder: src, tests and features; docs/specs, the approved contract; approvals.json, which seals by hash; and .specgate, the policy and the evidence. A call that breaks a rule is refused, with the next step to take. When the agent stops, the gate on stop runs specgate check, which reads the working folder: specs and links, the sealed approval, the design scope, approved changes only. Outside the session is a person at a terminal, who approves, seals and reviews steps, and whose seals land in the working folder; and the outside judge, which reads the controls before the agent starts and compares them when it stops, and gives a verdict independent of the session. A tool with no hooks has no guard; the gate, the seals, the approval rule and the outside judge still hold. THE AGENT'S SESSION — INSIDE THE TRUST BOUNDARY OUTSIDE THE SESSION Coding agent Claude Code · Codex OpenCode Gate on stop · specgate check specs and links · sealed approval · design scope · approved changes only Guard hook test first · whole working tree · step record · a person's acts Working folder src/ · tests/ · features/ docs/specs/ — the approved contract approvals.json — seals by hash .specgate/ — policy, evidence Refused with the next step to take Person at a terminal approves · seals · reviews steps Outside judge reads the controls before the agent starts; compares when it stops Verdict independent of the session tool call allowed refused on stop reads seals before + after verdict A tool with no hooks has no guard; the gate, the seals, the approval rule and the outside judge still hold.
The guard and the gate work inside the agent's session; the approvals, the seals and the outside judge's verdict come from outside it.

One turn, in sequence#

One turn of the harness, per requirement Five steps in a row. The harness picks the next pending requirement, never one waiting for an answer; creates a git worktree on a branch named after it; builds a prompt from its Gherkin, the rules and the boundaries; your agent writes code and tests; the gate runs strict validation, checks the specification was not touched, and runs your tests. Red: the failures go into the next prompt, up to the maximum number of attempts. Out of attempts: the work is committed as a failed attempt and the requirement stays Draft. Green: the requirement is marked done and committed on its branch, and the harness moves to the next pending requirement. It never merges. 01 Pick the next pending requirement — never one waiting for an answer 02 Worktree its own branch: harness/REQ-007 03 Prompt its Gherkin, the rules, the boundaries 04 Your agent writes code and tests — any CLI, no SDK 05 The gate validate --strict spec untouched your tests ✔ Green marked done, committed on its branch ✖ Out of attempts wip(…): FAILED the gate stays Draft — done never ran ✖ red — the failures go into the next prompt · up to max_attempts next pending requirement Then it reports. It never merges — the branch is yours to merge or throw away.
  1. Each pending requirement (or the one --req names) gets a fresh git worktree on a branch called harness/REQ-NNN.
  2. The harness installs the project's dependencies in it, then gives the agent a prompt built from the requirement and its Gherkin scenario.
  3. The gate runs. First the outside judge: any change to the controls the harness read before the agent started (guard, gate policy, approvals, evidence, sealed tests) fails the attempt. Then validate --strict, with approvals, sealed tests and design scope as approved; the specification untouched; your architecture check and tests; the scenarios when bdd: required; the step record; the principles with a run= check; your quality checks.
  4. Green: specgate apply marks the requirement Implemented and the work is committed on its branch as feat(REQ-NNN): ….
  5. Red: the failures go into the next attempt, up to max_attempts (default 3). After the last one the work is committed as chore(REQ-NNN): attempt FAILED the gate and the requirement keeps its status.

A requirement that waits for an answer (Needs Clarification) is refused. Without --chain, nothing is merged: each green requirement waits on its own branch for review.

Set it up#

bash
specgate harness init

This writes harness.config.yaml and .harness/prompt-prefix.md, detects the test command from your build files, and leaves agent: unset: which agent runs and with which credentials is your choice. Then name the agent and run one requirement:

bash
specgate harness run --req REQ-001 \
  --agent "claude -p --allowedTools Read Write Edit Glob Grep 'Bash(npm:*)' < {prompt_file}"

The harness writes the prompt to a temporary file and puts its path where {prompt_file} stands. A command without the placeholder is refused. The tool allowances, the commands for other agents and team profiles are on Choosing and wiring your agent.

With several agents in .harness/profiles.yaml, --profile <name> picks one for this run (propose --from-brief takes it too), and the studio's Build page offers the same choice.

To see what would happen without paying for an agent:

bash
specgate harness prompt REQ-001      # the exact prompt, printed
specgate harness run --dry-run       # what it would do, in order

The order. With a brief that lists its modules, requirements are taken module by module in the brief's order — list first what the rest needs — and, within a module, in the order its spec lists them; status and apply take them the same way and say which module comes next. Without a brief, the matrix's order. A requirement whose specgate:trace says depends= still waits for the ones it names.

The worktree#

Each requirement runs in its own git worktree, so the agent never touches your working copy. A fresh worktree holds only what git tracks: no node_modules, no target/, no .venv. Before the agent starts, the harness runs the setup command: setup_cmd: in harness.config.yaml, --setup-cmd, or the install your lockfile implies (npm ci, pnpm, yarn). --setup-cmd none runs nothing. See Dependencies.

The agent gets SPECGATE_REQ and SPECGATE_ATTEMPT in its environment. Worktrees are removed after each requirement; --keep-worktrees keeps them.

A chain of requirements, merged as they pass#

bash
specgate harness run --chain --req REQ-002 --req REQ-003

With --chain, requirements run one at a time, in order, each cut from the previous merge. A green one is merged into the branch you are on (a merge commit; its prompts stay on its harness/ branch), and the gate runs again on the merge. A red result is undone and the chain stops.

With trust levels declared, --chain runs only on a branch of your own (git switch -c feature/<module>); you merge that branch yourself.

On a ticket's branch#

For work that comes from a Jira ticket, run the chain on the ticket's branch:

bash
git switch feature/KAN-13-register-vehicle-entry
specgate harness run --chain

specgate propose --from-ticket KAN-13 created that branch and the change. Each requirement of the change is merged into the ticket's branch as it passes, and every commit and merge commit starts with the ticket key (KAN-13 REQ-007: …), so Jira shows them on the ticket. Then archive the change and open the pull request with specgate alm pr <change-id>. See From a Jira ticket.

Watch it work#

Run specgate studio and open /live: attempts, phases, files, tests and steps as they happen, read from .harness/events/ (ignored by git). Claude Code reports its steps too when it writes stream-json and its output goes through specgate harness events --from claude:

yaml
# harness.config.yaml
agent: "claude -p --output-format stream-json --verbose < {prompt_file} | specgate harness events --from claude"

Drive it from another tool#

A tool that starts the harness — an editor, specgate-studio — reads it as JSON: --format jsonl streams one event per line, specgate harness stop --after-current ends a run between requirements, --allow-dirty builds with uncommitted files, and a question the agent asks gets its id while the run goes on. See Drive the harness from a tool.

Parallel branches and the traceability matrix#

Each harness branch changes its own row in docs/specs/traceability.md.

A generated matrix (the default) never blocks a merge: it is declared merge=union in .gitattributes, git keeps both sides' rows, and specgate check regenerates the file (ADR-0036).

A hand-kept matrix needs a merge driver that merges by row, because git merges by line and two neighbouring rows conflict even when they belong to different requirements. specgate harness init registers that driver and writes .gitattributes:

bash
specgate harness init --project-dir .
git add .gitattributes && git commit -m "merge the matrix by row"

The driver is local git configuration, so every clone and every CI job needs specgate harness init --merge-drivers once: it registers the drivers and writes no other file. Until then git falls back to its normal merge, and specgate doctor reports the gap.

It still conflicts when two branches set the same row to different values. Keep exactly one of the two lines and delete the three marker lines; never keep both, because a duplicated requirement row is corruption. Then run specgate validate ., which checks the statuses and that no scenario id repeats.

Parallel branches and capability specs#

Two branches often archive into the same capability spec. specgate harness init registers a merge driver for those files that merges requirement by requirement: different ids are united and sorted, and a requirement changed on one side only takes that side. It conflicts only when both rewrote the same requirement (change_overlap warned before): write it as it should read, delete the markers, run specgate check.

Without a local node_modules (Maven, say), the drivers run this machine's specgate. If that copy goes away, git falls back to line merges and specgate doctor warns; run specgate harness init --merge-drivers again.

Run more than one requirement at a time#

bash
specgate harness run --concurrency 4

Only requirements that do not depend on each other run together. The harness reads the depends= keys in each requirement's specgate:trace comment and works through the queue in levels: everything in a level is independent and goes out at once; the next level waits.

A failure blocks what depends on it; it does not fail it. The agent is not paid for work that cannot succeed:

text
  ❌ REQ-001  fail (1 attempt)     → harness/REQ-001
  ⛔ REQ-002  blocked (0 attempts) → harness/REQ-002
       Not attempted: REQ-001 did not pass.

  0 passed · 1 failed · 0 skipped · 1 blocked

A requirement in a dependency cycle is reported the same way and never attempted; specgate validate explains the cycle.

The base is derived. A requirement is cut from the branch of the requirement it depends on, because that is where its code exists during a run. The harness warns when that base is behind your main branch:

text
⚠️  REQ-002: base harness/REQ-001 is 3 commit(s) behind main.
    A fix that landed on main is not in this worktree —
    a gate failure may not be about REQ-002.

A requirement with several dependencies gets them merged into a throwaway harness/base/REQ-NNN branch first:

  • Conflicts in docs/specs/traceability.md keep the base's version. Sibling branches always conflict there, because each one marks its own requirement Implemented with specgate apply, editing the same table. The real harness/REQ-NNN branch keeps its own row.
  • A conflict in a source file blocks the requirement and names the file: two dependencies changed the same code in incompatible ways.

The default is 1. Above 1, each requirement runs in a worker process, a newer path than the serial one. And four green branches are four reviews. concurrency: 4 in harness.config.yaml sets it for the project.

Unattended runs in CI#

For a nightly job that leaves branches to review:

OptionWhat it does
--resumeContinues an interrupted run — Ctrl-C, a cancelled job — from the attempt it stopped on.
--budget-seconds <n>Stops cleanly before starting a requirement once the time is spent.
--max-requirements <n>Attempts at most n requirements.
--skip-not-readySkips requirements an agent cannot succeed at (no feature, unmet dependencies).
--skip-baselineRuns even when the base already fails validate --strict.
--pushPushes each green harness/REQ-NNN branch.
--pr-cmd "<cmd>"Runs after each push, with {branch} and {req} placeholders.
--verboseStreams the gate's output while it runs.

Stopping a run. Ctrl-C, a signal or a cancelled CI job ends the agent and every command the run started, also when the run itself is killed outright; the requirement's worktree stays, and --resume continues from it. The run exits 130.

When a run fails#

Before any agent runs, the harness validates the base. The gate judges the whole project, so a requirement outside the run that is past Draft with no test (TDD-1, TDD-2), such as one marked Approved before its test, would fail every attempt whatever the agent did, and the agent may not change its status. The run stops there and lists them: write their tests or move them back to Draft, or pass --skip-baseline to run anyway. What fails on the requirements being run is theirs to fix, and does not stop it.

When the session touched the controls, the attempt fails before anything else runs (controls_changed_by_session): the agent approved, switched the guard, reviewed steps or edited the gate policy, the evidence or the sealed tests. Those are a person's. Put the controls back and look at what the agent was asked to do.

The report prints the tail of the gate output and the command that failed. Some failures end the run after one attempt, because retrying cannot help: see Troubleshooting.

The failed attempt is committed on its branch as chore(REQ-NNN): attempt FAILED the gate, so you can read what the agent did. The requirement's status does not change, because specgate apply never ran.

bash
specgate harness run --req REQ-002 --format json      # the whole gate output
specgate harness run --req REQ-002 --keep-worktrees   # reproduce it in place

A requirement that builds on another runs with --chain, or with --base-branch harness/REQ-001. That base branch also supplies the project configuration, so a fix committed to main reaches the run only once the base has it. Check the gate command the report prints.

To see what the harness has cost over time (first-attempt rate, time per delivered requirement), see What the harness has cost.

Agents per module, checked first; a swarm in sandboxes#

A module can have its own agent (match: { module: … }); harness run checks every agent answers before it starts (specgate harness doctor); with sandbox: { engine: sbx } each module works in its own sandbox, removed at archive. In detail.

Next#