specgate/docs v1.8.0

Specify a system that already runs#

At the end of this page your existing code has its first requirements, each one Draft and citing the lines it was read from, and the gate is on in CI without failing on the gaps the project already had.

You need: a repository adopted with specgate init (or specgate adopt), git, and an agent you can run from the command line (Choosing your agent).

Adopting a repository that already runs, then specifying it module by module Six steps. One: your repository, code that already runs, with a build file and a test suite that passes. Two: specgate init adopts it in place, the same as specgate adopt: it writes spec.md, AGENTS.md, docs/specs, the baseline scenario features/adoption/baseline.feature, the gate policy .specgate/gate.yaml and the CI job — only files that do not exist yet. Your src and tests folders stay as they are: git diff shows new specification files only. Three: specgate check is green on day one, with your own tests in it; it certifies the skeleton, not the code yet. Four: pick one module, by either of two roads. By hand, open a change for it with specgate propose describe-orders --capability orders. Or let specgate reverse --modules find the candidate modules — folders with their own build manifest, Go packages, code under src or apps — each with its stack; no agent runs. Five: your agent reads that module. On the reverse road, specgate reverse with the module and --agent writes the change reverse-orders; on the other, it writes the describe-orders delta. Every requirement is Draft; reverse also cites in source= the lines each was read from. Behaviour that looks like a bug becomes a question in questions.md, never a requirement, because written as one the bug would become the contract. A person reads each requirement against its lines before moving it past Draft. Six: from then on, one behaviour per bug fix — open a change for the behaviour that broke, write the scenario that reproduces it, name it in a test, and the fix applies it. 1 Your repository code that already runs, a build file and a test suite that passes 2 specgate init adopts it in place — the same as adopt: + spec.md + AGENTS.md + docs/specs/ + features/adoption/baseline.feature + .specgate/gate.yaml + the CI job only files that do not exist yet 3 Green on day one specgate check Gate passed, with your own tests in it — so far it certifies the skeleton, not the code src/ and tests/ stay as they are — git diff shows new specification files only 4 Pick one module by hand, a change for it: propose describe-orders --capability orders or let Specgate find them, no agent: specgate reverse --modules 5 Your agent reads one reverse orders --agent "…" writes reverse-orders — or, by hand, the describe-orders delta. All Draft; reverse cites the lines it read: source=…/Cancel.java:12 Looks like a bug? a question in questions.md, never a requirement: written as one, the bug would become the contract 6 From then on, one behaviour per bug fix Before a fix, open a change for the behaviour that broke and write the scenario that reproduces it; a test names it, and the fix applies it. The specification grows where the risk actually was. then, one module at a time suspect a person reads each against its lines, then moves it past Draft
Adopting writes specification files only and is green on day one; a change per module — by hand with propose --capability, or found by reverse — then turns each into Draft requirements, and a suspected bug stays a question.

1. See the modules#

bash
specgate reverse --modules
text
  billing                          java     41 file(s)
  orders                           java     63 file(s)

The candidates are the folders with their own build manifest (pom.xml, build.gradle, package.json, …) at the first or second level of the repository, the packages of a Go module, and the folders under src/ or apps/ that hold code; folders named like tests, examples or samples are skipped. Each is listed by its path. Each comes with its stack and its number of code files, tests included. No agent runs. With --json, the list is reverse.modules.

When nothing is found, name the folder yourself: specgate reverse src/billing.

2. Reverse one module#

bash
specgate reverse orders --agent "claude -p < {prompt_file}"

Without --agent, the agent: of harness.config.yaml runs. The agent gets the module's source, test and schema files, and answers one JSON file: requirements, their scenarios, and the source each was read from. Specgate writes the change docs/specs/changes/reverse-orders/ from that answer:

markdown
### Requirement: REQ-041 — A shipped order cannot be cancelled

The system SHALL refuse to cancel an order whose status is SHIPPED.

#### Scenario: SCN-041a — A shipped order

- GIVEN an order with status SHIPPED
- WHEN its owner cancels it
- THEN the cancellation is refused

<!-- specgate:trace status=Draft actor=Developer kind=functional source=orders/src/main/java/x/Cancel.java:12 -->

What holds whatever the agent does:

  • Every requirement is Draft. Code says what the system does, not what it should do. A person moves a requirement past Draft once they have read it against its source.
  • Every requirement cites its code in source=.
  • A suspected defect is a question, not a requirement. Behaviour the agent marks as suspect goes to the change's questions.md. Writing it as a requirement would make the bug the contract.
  • Nothing else changes. spec.md and the capability specs are untouched, and every write the agent makes outside its answer file is undone and named.
  • A change that does not validate is not kept. Neither is an answer without a source, without a scenario, or that is not JSON.

One module per run, reviewed before the next: a whole repository reversed at once is more than anyone reads.

3. Review it#

Read each requirement against the lines its source= cites, answer questions.md, then continue as with any change: specgate change validate reverse-orders, a test that names each requirement, specgate apply, specgate archive.

When a cited file is deleted or moved, specgate validate warns source_missing and names it. It does not fail the gate: moving code is not a broken contract. Read the requirement against where the code went, and update its source=.

4. Turn the gate on without a red build#

An existing project usually has gaps the gate finds on day one, such as rows marked Implemented with no test. Record them once:

bash
specgate check --baseline
git add docs/specs/gate-baseline.json

From then on, specgate check and specgate validate fail only on a finding that is not in docs/specs/gate-baseline.json. Each run says how many it let through (baselined), and names each baselined finding that no longer occurs (baseline_fixed), so that you can run check --baseline again and the file shrinks.

A finding is matched by its code, its file and its requirement (or target), not by its wording — except a finding that names neither, which is matched by its message. The baseline holds the traceability findings of validate --strict (the TDD rules and the matrix checks). The other checks still fail as before: when one does, check --baseline says so and records what it could.

A large system: one area first#

specgate adopt --scope src/billing adopts the repository but lists only the areas under that path in docs/specs/adoption.md, and spec.md and the baseline scenario name the scope (REQ-337). Adoption lists eight areas at most; when it finds more it warns adopt_too_big, naming the ones left out, and suggests --scope (REQ-338).

Next#