# Fix an incident

> The reproduce, fix, verify loop for a .kavach fixture, and what to do with each verdict on the way.

This is the loop for a person or a coding agent. It is the same loop the [`/kavach-fix`](https://www.thekavachlabs.com/docs/guides/ai-agents#the-five-skills) skill follows.

## 1. Read the incident

```bash
kavach list                       # every *.kavach under the current directory
kavach inspect incident.kavach    # record by record
kavach inspect incident.kavach --full   # with the panic stack and untruncated data
```

The last records show the failing input and the marker: `panic`, `error`, `invariant` or `crash`, with its message.

## 2. Reproduce it before changing code

Make a host command for the code as it is now.

- **Compiled service:** build it to a separate path: `go build -o /tmp/old ./cmd/service`, `cargo build --release && cp target/release/service /tmp/old`.
- **Interpreted service:** copy the checkout so later edits do not change it: `git worktree add /tmp/old HEAD`, then point the command at the copy, for example `"python -m service"` run from `/tmp/old`.

```bash
kavach replay incident.kavach --bin /tmp/old
```

Expect `still_failing@N`, where `N` is the `seq` of the failing input. **If replay does not reproduce the failure, stop.** Do not guess at a fix; check that `/tmp/old` is the build that was running, and see [Verdicts](https://www.thekavachlabs.com/docs/reference/verdicts) for `nondeterministic` and `diverged`.

### Look at state at the failing step

Time-travel replay stops at a step and lets you probe the live handler:

```bash
# stop after the last step with input seq <= 28, then send one probe input
kavach replay incident.kavach --bin /tmp/old --until 28 --probe '{"type":"balance","account":"alice"}'

# or hold the host open and type probe inputs, one per line, until EOF
kavach replay incident.kavach --bin /tmp/old --until 28 --hold
```

A probe is sent to the handler as an input from source `probe` with your text as its data, so write it in the shape your handler parses. Its outputs show what the handler would emit at that point, without touching anything real.

## 3. Fix the handler

Change code, never the fixture. Fix the class of bug, not the recorded value: if a null amount crashed a deposit, ask what else can arrive with a null amount.

## 4. Verify

Build the changed code as a second host command (`/tmp/new`, or the working tree itself):

```bash
kavach diff incident.kavach --old /tmp/old --new /tmp/new
```

The fix is accepted only at `fixed`. What to do otherwise:

| Verdict | What it means | Next |
| --- | --- | --- |
| `still_failing@N` | The recorded failure still happens. | Keep working. |
| `variant_failed(K)@N` | The recorded incident passes, but variant `K` still fails. The fix is too narrow. | Replay the saved variant and generalise. Never special-case it. |
| `regressed(K)@N` | On an earlier, similar input that used to work, the new build now behaves differently. | Replay the saved regression variant on both builds with `--json` and compare `steps[].outputs`. If the spec calls for the old output, narrow your change. If it calls for yours, the old build was wrong there too: report it. |
| `invariant_violated(X)@N` | The fix broke invariant `X`. | The fix corrupts state. |
| `diverged@N` | A step before the failure now produces different outputs from production. | The fix changed behaviour it should not have. |
| `unverified` | The incident passes, but fewer than 10 variants reproduce it on the old build. | Report it; do not claim the fix is verified. |

### Replaying a saved variant

Failing variants are saved to a temporary directory (or to `--keep DIR`), and their paths are in the JSON:

```bash
kavach diff incident.kavach --old /tmp/old --new /tmp/new --keep ./variants --json > diff.json
jq -r '.variants.checks[] | select(.file) | .file' diff.json
kavach replay ./variants/incident.variant-07.kavach --bin /tmp/new
```

For regression variants, look under `.regressions.checks[].file`. A variant records no outputs, so plain `replay` prints `ok` on both builds; compare `steps[].outputs` from `kavach replay <variant> --bin <build> --json` instead.

## 5. Keep it as a regression test

Copy the fixture into the service's `testdata/` and replay it in the test suite:

```bash
kavach replay testdata/null-amount.kavach --bin ./service   # exits 0 when ok or fixed
```

In Go, `kavachtest.Run` does the same inside `go test`:

```go
func TestFixtures(t *testing.T) {
	kavachtest.Run(t, "testdata/*.kavach", func() kavach.Handler { return NewLedger() })
}
```

Then run it on every pull request: see [Run Kavach in CI](https://www.thekavachlabs.com/docs/guides/ci).

## Report the result

Report the verdict exactly as Kavach prints it, with the variant counts from `diff`. "`fixed` · variants 51/51 · regressions 47/47" is evidence a reviewer can check by running the same command.

Source: https://www.thekavachlabs.com/docs/guides/fix-an-incident
