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.
One turn, in sequence#
- Each pending requirement (or the one
--reqnames) gets a fresh git worktree on a branch calledharness/REQ-NNN. - The harness installs the project's dependencies in it, then gives the agent a prompt built from the requirement and its Gherkin scenario.
- 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 whenbdd: required; the step record; the principles with arun=check; your quality checks. - Green:
specgate applymarks the requirement Implemented and the work is committed on its branch asfeat(REQ-NNN): …. - Red: the failures go into the next attempt, up to
max_attempts(default 3). After the last one the work is committed aschore(REQ-NNN): attempt FAILED the gateand 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#
specgate harness initThis 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:
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:
specgate harness prompt REQ-001 # the exact prompt, printed
specgate harness run --dry-run # what it would do, in orderThe 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#
specgate harness run --chain --req REQ-002 --req REQ-003With --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:
git switch feature/KAN-13-register-vehicle-entry
specgate harness run --chainspecgate 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:
# 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:
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#
specgate harness run --concurrency 4Only 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:
❌ 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 blockedA 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:
⚠️ 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.mdkeep the base's version. Sibling branches always conflict there, because each one marks its own requirement Implemented withspecgate apply, editing the same table. The realharness/REQ-NNNbranch 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:
| Option | What it does |
|---|---|
--resume | Continues 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-ready | Skips requirements an agent cannot succeed at (no feature, unmet dependencies). |
--skip-baseline | Runs even when the base already fails validate --strict. |
--push | Pushes each green harness/REQ-NNN branch. |
--pr-cmd "<cmd>" | Runs after each push, with {branch} and {req} placeholders. |
--verbose | Streams 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.
specgate harness run --req REQ-002 --format json # the whole gate output
specgate harness run --req REQ-002 --keep-worktrees # reproduce it in placeA 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.