Build a feature against a contract
Record traffic before a feature exists, mark the steps it should change, write invariants from the spec, get a person to approve it, then implement until kavach contract check passes.
An incident gives you a fixture to check a fix against. A new feature has none. A contract is the check instead, and it is written before the code:
- Traffic: a journal recorded on the build before the feature.
- Feature steps: the steps of that traffic that should reach the feature, and so should change.
- Invariants: properties of state the specification promises, declared in the service's code.
A build meets the contract when every step outside the feature produces the recorded outputs, every feature step changes, and the invariants hold on the traffic and on variants of the feature steps. Every change is listed, so a reviewer reads exactly what the feature does differently on real traffic.
The /kavach-implement skill walks an agent through this loop and stops for your approval.
1. Record traffic on the current build#
Build the current code as the old build (/tmp/old, or a copy of the checkout for interpreted code). Run it under the recorder with:
- realistic inputs for what already exists, especially near the feature;
- inputs that should reach the new feature, including the edge cases the spec names.
The recorder's journal (the .kavach file in the recorder's directory, next to fixtures/) is your traffic. Write down how you generated it.
2. Declare invariants from the spec#
Write each invariant from the specification alone, as an operator would state the feature's promise: "a key with a TTL is never returned after it expires", not "the expire function sets the field". Declare them in the service's code through its SDK. They will not hold yet on the old build if the feature does not exist; that is fine.
3. Create the contract#
kavach contract init \
--spec docs/hsetex.md \
--traffic traffic.kavach \
--feature-match '$.argv[0]=="HSETEX"' \
--invariant ttl_never_served_expired \
--invariant-source internal/store/invariants.go| Flag | Meaning |
|---|---|
--spec FILE |
The specification the contract is written from. |
--traffic FIXTURE |
The journal recorded on the old build. |
--feature-match EXPR |
Selects feature steps by a value in the input's JSON data. Repeatable. |
--feature-steps 40-52,60 |
Adds feature steps by seq. Use it for steps that read what a feature step wrote. |
--invariant NAME |
An invariant the new build must declare. Repeatable. |
--invariant-source FILE |
The file that declares it, hashed into the lock. Repeatable. |
--dir DIR |
Where the contract lives. Default ./contract. |
--force |
Overwrite an existing contract. |
A step whose output depends on the feature, such as a read of a key a feature step wrote, is a feature step too. If you leave it out, the check reports it as a regression.
4. Check the contract on the old build#
kavach contract check --old /tmp/old --new /tmp/oldExpect unreached@N and nothing worse: the old build does not have the feature yet. Anything else means the traffic or the contract is wrong; fix that now.
5. A person approves it#
kavach contract show
kavach contract approveapprove locks the contract by hashing its files. It asks for confirmation at a terminal; without one it needs --yes, which the lock records. An agent must never approve its own contract. There is no MCP tool for approval on purpose.
6. Implement and check#
kavach contract check --old /tmp/old --new /tmp/new --json| Verdict | Meaning | What to do |
|---|---|---|
pass |
Approved; no regressions; every feature step changed; invariants hold on the traffic and every feature variant. | Done. Review the changed steps. |
regression@N |
Step N is not a feature step, and its outputs changed. |
Fix it, or explain why the contract should mark it. |
unreached@N |
Feature step N produced the old build's outputs. |
The feature is not wired in for that input. |
variant_failed(K)@N |
A variant of a feature step broke an invariant or failed. | Replay the saved variant and generalise. |
still_failing@N, invariant_violated(X)@N, nondeterministic@N |
As for replay. | Fix the build. |
missing_invariant(X) |
The build does not declare X. |
Declare it as the spec states it. |
baseline_mismatch(V) |
--old does not replay the traffic. |
Wrong old build, or the traffic is from another build. |
unapproved, stale_lock |
The build meets the contract, but no one approved it, or it changed since. | Ask the person. |
check exits 0 only on pass. Each check is a round in contract/rounds.jsonl; failing variants are saved to contract/variants/ and kept per round in contract/rounds/N/. --save-replays DIR writes each build's replay of the traffic as JSON, for a side-by-side read. --no-variants skips feature variants for a faster inner loop.
When to stop#
- At
pass, report the verdict and each changed step with why the spec calls for it. - When
streak(the same outcome several rounds in a row) reaches 3, or after 8 rounds, stop and report what fails. - Never edit the contract, the traffic or the invariant sources to make a check pass. Doing so makes the lock stale, and the person sees it.
Something wrong or missing on this page? Report it on GitHub, or write to hello@thekavachlabs.com.