Concepts
The handful of terms the rest of the docs use, in the order you meet them.
Everything in Kavach is one loop: record what a service did, keep a failure as a file, replay it, and check a fix against it.
Handler and step#
Kavach replays a handler: code that consumes one input at a time, folds it into state, and produces effects. One call of the handler on one input is a step.
A handler may touch the outside world only through the env its SDK passes it for each step. The env has five kinds of call:
| The handler | Through the env | Recorded as |
|---|---|---|
| reads the clock | env.now() |
clock |
| reads random bytes | env.random(n) |
rand |
| queries an external system (a database read, an HTTP call, a price feed) | env.query(gateway, request) |
gateway |
| reads a feature flag or limit | env.config(key) |
config |
| produces an effect (a database write, a message, an email) | env.emit(sink, data) |
output |
Names differ slightly by language (env.Now() in Go, Kavach.Env.now(env) in Elixir), but every SDK has the same five. If a handler calls the language's own clock, random generator or network directly, those reads are not recorded and replay cannot reproduce them; Kavach reports this as nondeterministic@N.
Effects are delivered only after a step succeeds. A step that panics emits nothing to the world.
Journal#
The journal is the append-only record of everything the handler did: each input, then every read and output in program order, then a marker if the step failed. The flight recorder, kavach-recorder, runs as a separate child process and keeps the journal on disk. Because it is a separate process, it sees the pipe close when your service dies mid-step and records the crash.
Each record has a sequence number (seq). Verdicts name the seq of the input whose step failed: still_failing@32.
The recorder also writes an environment record (runtime version, build revision, relevant environment variables and flags) so that drift between the recorded and replayed environment can be explained.
Fixture#
A fixture is a .kavach file cut from the journal when a step panics, returns an error, or breaks an invariant. It holds the recorded inputs and every read since the journal (or the last snapshot) began, so it replays the failure exactly. Fixtures are small, compressed and self-contained.
Fixtures are records of what happened in production. Never edit, regenerate or delete one to make a check pass.
Snapshot#
A handler that can save and restore its state (snapshot() / restore()) lets the recorder cut the journal into segments. A fixture then starts from a recent snapshot instead of from the service's first event, so it stays small for long-running services.
Host command#
Replay runs your own build. A host command starts it in replay mode: Kavach appends kavach-host to the command, the SDK sees that last argument and serves replay over standard input and output instead of consuming real inputs.
kavach replay fixture.kavach --bin ./ledger
kavach replay fixture.kavach --bin "python -m ledger"
kavach replay fixture.kavach --bin "node dist/main.js"The string is split into words like a shell would. --bin defaults to $KAVACH_BIN.
Choose old and new builds with a command-line flag or by building different code, never with an environment variable: environment facts are served from the fixture during replay.
Replay#
kavach replay folds the recorded inputs through the host's handler and answers every read from the fixture. Outputs are compared with what production produced, and captured instead of delivered. Replay touches no broker, database or network, and typically takes microseconds per step plus process start.
Invariant#
An invariant is a named property of the handler's state that must hold after every successful step: "balances are never negative", "money is conserved". Invariants are checked live, while recording, and during replay. A broken invariant fails the step with a marker of kind invariant, and a fixture is written just as for a crash.
See Write invariants.
Variants#
A fix that handles only the exact recorded input is often too narrow. To catch that, kavach diff builds variants of the incident: copies of the journal with the failing input's fields changed or removed, the input moved earlier, earlier inputs dropped or redelivered, or the clock shifted. A variant counts only if the old build fails on it the same way it failed in production. The new build must pass at least 10 of those (--variants).
diff also builds up to 48 regression variants (--regressions): the same changes applied to earlier inputs that resemble the failing one and that the old build handled without failing. The new build must produce the same outputs on them, so a fix cannot quietly change behaviour elsewhere.
Verdict#
Every check ends in one verdict, printed exactly as shown: fixed, variant_failed(7)@23, still_failing@32, and so on. A fix counts only at fixed. See Verdicts.
Contract#
For new features there is no incident to replay. A contract is the check instead: traffic recorded on the build before the feature, the steps of it that should reach the feature, and invariants written from the specification. Steps outside the feature must produce the recorded outputs; feature steps must change; invariants must hold. A person approves the contract before any code is written. See Build a feature against a contract.
Something wrong or missing on this page? Report it on GitHub, or write to hello@thekavachlabs.com.