Journal format
What a .kavach file contains, how records are ordered, how replay and fix verification are defined, and how the format is versioned.
This page summarises the journal format (version 0) for readers who want to know what is in a fixture. You do not need it to use Kavach: the SDKs and the recorder write the format, and the CLI reads it. If you are integrating a language without an SDK, read Integrate without an SDK first.
Model#
A journal is the ordered record of everything a single-writer handler read and wrote. Replaying it through the same handler reproduces the same state and outputs. Everything nondeterministic the handler touched (time, randomness, external answers, config) is in the journal, so replay needs nothing else.
File layout#
A .kavach file is a header, then a sequence of compressed blocks of records.
- The header carries the format version, the service name, whether the journal starts at
genesisor from asnapshot, the run and segment it belongs to, and, for a fixture,cut_from: the journal it was cut from. - Blocks are closed and written when they reach a target size, when their oldest record has waited a flush interval (default one second), immediately after a failure marker, and when the pipe closes. A truncated last block is detected and ignored, so a journal written by a process that died is still readable up to its last complete block.
- Records each carry a
seq, a type and flags.seqvalues are kept when a fixture is cut, so records are numbered as in production.
Record types#
| Type | Code | Holds |
|---|---|---|
input |
0x01 |
Source, position and the exact bytes of one input. Starts a step. |
clock |
0x02 |
A clock read, in Unix nanoseconds. |
rand |
0x03 |
Random bytes the handler read. |
output |
0x04 |
Sink, data and scope (local or remote) of one effect. |
marker |
0x05 |
How a step failed (panic, error, invariant, crash) or that records were dropped. A deterministic message, plus data such as a stack trace. |
snapshot |
0x06 |
The handler's saved state. Starts a segment. |
gateway |
0x07 |
A query to an external system: the request and response (or error) bytes at the connection, and its scope. |
environment |
0x08 |
Facts about the process: runtime, build revision, hashed environment variables, feature flags. Collected by the recorder. |
config |
0x09 |
A config value the handler read, and where it came from. |
Ordering#
Within a step, records appear in program order: input, then every read and output as the handler made them, then a marker if the step failed. A segment starts with a snapshot and the full environment.
Replay#
Replay feeds each input to a fresh handler and answers every read from the next matching record. A read of the wrong kind, or in the wrong order, is nondeterministic. Outputs are compared with the recorded ones; a difference before the recorded failure is diverged. Invariants are checked after each step that ends ok.
Environment drift (a different runtime, a different flag value) is reported alongside the verdict, so a replay that differs because the environment changed can be told apart from one that differs because the code did.
Verifying a fix#
A fix is accepted (fixed) only when the new build:
- replays the recorded journal with the failure gone, invariants holding, and every earlier step producing the recorded outputs;
- passes at least 10 variants: 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, each of which makes the old build fail the same way it did in production;
- leaves regression variants unchanged: the same mutations applied to earlier, similar inputs that the old build handled without failing.
If fewer than 10 variants reproduce the incident, the verdict is unverified, not fixed.
Feature steps and contracts#
A replay can be told that some inputs are feature steps: places where a new build is meant to behave differently. Their reads are served leniently and a change in their outputs is reported as changed rather than stopping the replay. A contract combines traffic, feature steps and invariant names; see Build a feature.
Versioning#
Before 1.0, every minor version of the format is a different format, and the CLI reads only the versions it knows. Do not write .kavach files yourself; use an SDK or the recorder protocol, and the recorder writes the current format.
Protocols#
Two protocols connect a service to Kavach:
- The recorder protocol: binary frames from the service to
kavach-recorder's stdin, and JSON Lines control messages back. Optionally carried over a shared-memory ring instead of a pipe, so a recorded step costs memory copies and no system call. - The host protocol: JSON Lines between the
kavachCLI and a service started withkavach-host, one step at a time.
Both are shown in Integrate without an SDK.
Something wrong or missing on this page? Report it on GitHub, or write to hello@thekavachlabs.com.