Integrate without an SDK
Kavach is two small protocols and a file format. Any program that speaks them records and replays exactly as an SDK does. Here is what to implement and how you know you are done.
The SDKs are thin wrappers. Underneath, a service takes part in Kavach by speaking two protocols:
| Side | Protocol | You write | Kavach does, once for every language |
|---|---|---|---|
| Live | Recorder protocol | Binary frames into kavach-recorder's standard input |
Sequence numbers, blocks, compression, segments, fixtures, the environment record, crash markers |
| Replay | Host protocol | JSON Lines answers on stdout when started with kavach-host as the last argument |
Serving reads, comparing outputs, verdicts, variants |
Never write .kavach files yourself. Before 1.0 every minor version of the format is a different format, and a process that writes its own journal cannot record its own death: only the separate recorder sees the pipe close mid-step and writes the crash marker.
The handler model#
Your integration provides five calls, and the handler must use nothing else:
| Handler does | Live: you | Replay: you |
|---|---|---|
| reads the clock | read it, send a clock record |
send {"t":"clock"}, return the answer |
| reads random bytes | read them, send a rand record |
send {"t":"rand","n":N}, return the answer |
| queries an external system | call it, send a gateway record with the raw request and response |
send {"t":"gateway",...}, return the answer; never call it |
| reads a flag or limit | read it, send a config record |
send {"t":"config","key":K}, return the answer |
| produces an effect | send an output record; deliver it after the step succeeds |
send {"t":"emit",...}; never deliver it |
Reads happen in a deterministic order, one at a time. Gateway records hold the bytes at the connection, below any parsing, so a parsing bug replays.
Recording#
Start kavach-recorder as a child process (from KAVACH_RECORDER, else PATH) with the environment unchanged. Write frames to its stdin. Read JSON Lines from its stdout on a separate thread, and never make a step wait on them.
Every frame is uvarint len, u8 kind, then len − 1 bytes of payload. Integers are little-endian; strings and byte strings are a uvarint length then the bytes.
open 0x01 JSON: {"protocol":1,"service":"ledger","start":"genesis", ...}
facts 0x04 host.runtime and flag.* facts, right after open
per step:
record 0x02 input — write it before calling the handler
record 0x02 clock | rand | gateway | config | output, in program order
record 0x02 marker — only if the step failed
step_end 0x03
close 0x07 at shutdown; wait (bounded) for {"t":"closed"}A record frame's payload is u8 type, u8 flags, then the type's fields; the recorder assigns seq. Set flags to 0x01 (critical) on gateway, config and snapshot records, 0x00 on the rest. An input from source q, position 0, data {}:
0a len = 10
02 kind: record
01 00 type: input, flags: 0
01 71 source "q"
01 30 position "0"
02 7b 7d data "{}"Rules that, if broken, make journals replay to the wrong answer:
- Never drop a record silently. When the pipe is full, wait; or drop and send a
markerof kinddropped. - Never send
environmentrecords. The recorder collects them. - Failure messages are deterministic. No file paths, line numbers, addresses or object identities in a
panicorerrormessage; put those in the marker's data. Replay compares the message. - Recording must not fail the service. If the recorder cannot start or reports a fatal error, log it and stop recording.
Later, optionally: answer snapshot_request so the recorder can segment long runs, and use the shared-memory ring instead of the pipe when a step costs microseconds.
Replaying#
When the program's last argument is kavach-host, it is a host and does nothing else: no consuming, no serving, no live effects. Keep stdout for protocol messages only and point the language's own stdout at stderr.
One JSON object per line; bytes are padded base64; seq and unix_nanos are decimal strings.
driver → host {"t":"hello","protocol":1,"service":"ledger","start":"genesis"}
host → driver {"t":"ready","protocol":1,"sdk":"kavach-julia/0.1.0","invariants":[],"environment":{...}}
driver → host {"t":"step","seq":"0","source":"q","position":"0","data":"e30="}
host → driver {"t":"clock"}
driver → host {"t":"clock","unix_nanos":"1759752000000000000"}
host → driver {"t":"emit","sink":"postgres:balances","data":"...","scope":"remote"}
host → driver {"t":"done","outcome":"ok"}
driver → host {"t":"end"}Get ready.environment by running kavach-recorder facts and adding host.runtime. On abort, unwind the step in a way handler code is unlikely to catch, and reply done with outcome "aborted".
Done means both conformance suites pass#
Implement the conformance handler in your language: a counter driven by JSON operations, about a hundred lines. Then, from a checkout of the Kavach repository:
-
Replay:
python3 spec/host/run.py --host "<command that starts your conformance host>"Every transcript in
spec/hostmust pass. -
Recording: with
spec/recorder/sdk/fake_recorder.pyas the recorder command, every case inspec/recorder/sdkmust write"pass": true. Your integration therefore needs to accept a recorder command as an option, besidesKAVACH_RECORDER.
Do not edit the transcripts or cases to make them pass; they are generated, and they are the contract every SDK meets.
Finally, check the full path once by hand: record a step that fails, find the fixture the recorder reports ({"t":"fixture",...} on its stdout), and run kavach replay <fixture> --bin "<your host command>". Expect still_failing@N.
The Kavach repository includes a worked example: the ledger demo in Julia, speaking both protocols directly with no SDK, under examples/ledger/protocol. The full wire format is summarised in Journal format.
Something wrong or missing on this page? Report it on GitHub, or write to hello@thekavachlabs.com.