# Backtest

> Replay the human-review score on past pull requests and measure it against where people actually commented.

Web page: https://visto-ai.com/docs/backtest/

The backtest replays the human-review score on past pull requests and measures it against where people actually commented. Use it to choose a threshold, to compare models or scorers, and to see what memory and lessons do to the score before you switch them on.

Like the [replay](https://visto-ai.com/docs/replay/), it reads from GitHub and calls the model, so it makes paid calls bounded by `--max-cases` and `--max-cost`. It posts nothing.

## Run a backtest

```bash
npm run eval:import-history -- --repo owner/name --since 2026-01-01 --until 2026-04-01 --out ../history.jsonl
npm run eval:backtest -- --history ../history.jsonl --out ../runs/score-one --label score-one --config config.local.yml
npm run eval:score-noise -- ../runs/score-one ../runs/score-two
```

1. `eval:import-history` reads a repository's merged pull requests and the comments people left on them, and writes a history file. It needs `GITHUB_TOKEN` and makes no model call.
2. `eval:backtest` scores each pull request of the history file, as the service would, and measures the score.
3. `eval:score-noise` compares two runs of the same history file, to show how much the score moves between identical runs.

The history file and the run folders must be outside the project folder: they hold paths and model-written reasons.

### Options

`eval:import-history`:

| Option | Default | What it does |
|---|---|---|
| `--repo` | required | The repository, as `owner/name`. |
| `--since`, `--until` | required | The window of merges, as `YYYY-MM-DD`. |
| `--out` | required | The history file to write. |
| `--max-prs` | `200` | The most pull requests to import. |
| `--force` | off | Overwrite a history file that is not empty. |

`eval:backtest` takes the same `--out`, `--label`, `--config`, `--max-cases` (60), `--max-cost` (10), `--concurrency` (2) and `--force` as the [replay](https://visto-ai.com/docs/replay/#options), with `--history` in place of `--cases`, plus `--memory` and `--lessons` (below). It prints `cases <n>, failed <n>, not started <n>, total_cost <x>`, and writes `results.jsonl`, `scores.jsonl`, `summary.json` and `summary.md` into the run folder.

## What it measures

The backtest reports three numbers, at the thresholds 20, 40, 70 and 90 and at each repository's own threshold:

| Number | Meaning | Better |
|---|---|---|
| **Safety** | Of the blocks and files a person had to look at, the share the score would have left below the threshold. | Lower |
| **Benefit** | The share of pull requests in which nothing reaches the threshold: no block and no file as a whole. A person need not be asked about those at all. | Higher |
| **Focus** | In the pull requests that do go to a person, the share of changed lines flagged. | Lower: the person reads less |

A block or file is labelled **loose** when a person who is not the author left an inline comment on it. It is labelled **strict** when, in addition, its lines (for a file, its diff) changed in a later commit before the merge. Safety is reported for both.

`eval:score-noise` prints how often a block, and a whole pull request, falls on a different side of each threshold between two runs. It refuses runs of different history files, contracts or scorers.

## What a backtest leaves out

- **Extension modules.** The backtest loads none: it runs without extension context and without acceptance rows. A config that names modules still needs their variables set.
- **MCP servers.** It opens none, so it runs without tools. A config that names servers still needs their variables set.

It honours `model.toolChoice`.

## The backtest with memory

Without `--memory`, a backtest runs without memory. With `--memory <history file>`, each case is reviewed with the memory a deployment would have had at the case's date, built from that file alone by the same rules the service uses (see [Memory](https://visto-ai.com/docs/memory/)). It reads no database.

It is shown no lesson unless `--lessons` is given (see [Lessons in a backtest](#lessons-in-a-backtest)): a lesson is made from a reply to one of the reviewer's findings, and a history file holds none. So without `--lessons`, a backtest is shown memory for the score alone, reverts included.

A config that names a scorer is refused (`usage: --memory cannot be used with a scorer in the config`): with a scorer, memory is not asked.

### What memory in a backtest holds

The signals of the file's other pull requests of the same repository, each dated by its merge, or by its comment when that came later, and live at the case's date. A case is never shown a record made from itself, nor one dated after its date.

The memory file may be the one `--history` names, or a longer import of the same repository. A file of a few dozen pull requests merged within weeks gives few cases any memory, and a longer window costs only reads of GitHub. It must be outside the project folder.

### How it differs from a deployment

The memory is poorer than a deployment's:

- A history file holds no score from the reviewer, so a record has no category and is never a miss.
- Only merged pull requests are in it, and no thread of the app's inline notes exists. A line there says only that a person commented on that file on that day.
- A case's date is that of the first review by a person, which comes after the reviewer would have reviewed the pull request when it was opened. Records from pull requests merged in between are shown, a little more than the service would have had.

### Reverts

The memory also holds reverts, made from the memory file alone by the rule the service runs (see [Reverts](https://visto-ai.com/docs/lessons-and-reverts/#reverts)). A pull request of the file whose description names another one the file holds, of the same repository and merged at or before it, makes records for that one's files, each dated by the merge of the revert. A case is never shown its own revert, nor the revert it made, nor one merged after its date.

A file and a deployment differ here too:

- A file knows every file of a pull request, where a deployment knows those of its last review.
- A file holds each description as it read when the file was made, so an edit after the merge counts there, where the service reads only the delivery of the merge.
- A file has no order of deliveries, where the service records nothing when the reverted pull request was not yet stored as merged.
- A file has no switch for a record.
- A pull request held twice in a file is read from its first case.
- Past the 1,000 revert records read for a case, ties are broken differently. A case's own reverts are left out before that bound, as the service leaves them out.

`import-history` keeps a description as it was sent, cut at 4,000 characters, with nothing stripped, so that a file reads a revert's reference as the service reads it. The backtest's audit counts the characters the engine strips from a description, as the service's does, and a description longer than 4,000 characters is cut before the engine strips what it shows. Seeding a deployment (below) writes no revert record.

### What it records

Each row of `results.jsonl` carries `memory`: the case's status, code and lines, or null. `summary.json` names the memory file and its sha256 and counts the cases by status. `summary.md` has a line `Memory: as of each case, from <file>. Cases shown memory: ...; with none to show: ...; not shown: ...; failed: ...; not asked: ...`. `score-noise` refuses two runs whose memory differs, including one with memory and one without (`different_memory`).

### Count first, for free

`eval:memory-count` calls no model, reads no network, no token and no config, and writes no file. Run it before any paid run:

```bash
npm run eval:memory-count -- --history ../history.jsonl --memory ../history-long.jsonl
```

It prints `cases <n>, with a line <n>, with a labelled file that got a line <n>`, then one row for each case: its key, the records live at its date, the lines it would be shown (on its files and near them), its files a person who is not the author commented on, and how many of those got a line of their own. These numbers count the signals' lines only.

Then it prints `reverts named <n>, with their pull request in the file <n>, cases shown a reverted line <n>`: the pull requests of the memory file whose description names a revert of their own repository, those whose records were made, and the cases that would be shown at least one reverted line. One line `<key>: reverted lines <n>` follows for each such case. `--max-cases <n>` counts only the first n cases.

Only the cases shown a line can show an effect of memory, and only the labelled files with a line can move Safety. With few of either, a paid run cannot show anything, whatever memory does. The same holds for the cases shown a reverted line.

### Then compare

Make two runs on the same history file and config, one without memory (the baseline) and one with it (the candidate), and ideally a second baseline for the noise:

```bash
npm run eval:score-compare -- ../runs/mem-off-1 ../runs/mem-on-1 --noise ../runs/mem-off-2
```

`eval:score-compare` refuses runs of different history files, contracts or scorers (as `score-noise` does), a candidate run without memory (`candidate_without_memory`) and a baseline run with it (`baseline_with_memory`). The `--noise` run is checked against the baseline as `score-noise` checks two runs.

It compares only the cases both runs hold with the status ok. It prints how many those are, how many are in one run only and how many are not ok in one, then a warning when the runs were answered by different models. It splits the cases by the candidate's memory status: shown memory (`ok`), none to show (`empty`), and the rest. It also counts the cases shown memory that made a different number of model calls in the two runs, since their difference may come from the packing and not from memory.

Then, for the cases shown memory, at each threshold and at each repository's own, it prints:

- each run's Safety (by block and by file, strict and loose), Benefit and Focus, each with its count and its **step**, the smallest difference that count can show (Focus is a share of lines whose count is of pull requests, so it has no step);
- how many blocks and pull requests crossed each threshold upward and downward from the baseline to the candidate;
- with `--noise`, the same between the two baselines;
- the same for the cases with none to show, whose requests were identical in both runs, so what moved there is noise.

How to read it: a difference smaller than two steps, or than what the noise moves, is no finding, and "no measurable effect" may mean "too few cases". More blocks lowered with memory than in the noise is a sign that the model read a file with no line as safer. `score-compare` compares memory as a whole: it cannot tell a case shown a reverted line from one shown a signal's.

## Lessons in a backtest

`--lessons <lessons.json>` shows each case the lessons live at its date, from the file `eval:judge-replies` writes (see [Measuring lessons](https://visto-ai.com/docs/lessons-and-reverts/#measuring-lessons)). It can be given beside `--memory`, or alone, in which case the memory holds no record and no revert, only the lessons.

```bash
npm run eval:memory-count -- --history ../history.jsonl --lessons ../lessons/lessons.json
npm run eval:backtest -- --history ../history.jsonl --lessons ../lessons/lessons.json --out ../runs/les-on-1 --label les-on-1 --config config.local.yml
```

- The file must be outside the project folder, and one that is not whole is refused (`lessons_incomplete`).
- A config that names a scorer is refused (`usage: --lessons cannot be used with a scorer in the config`): with a scorer, the score does not come from the model that is shown lessons.
- A case is never shown a lesson made from its own pull request, nor one made from a reply written on its own day or after its date, nor one that has expired.

### What it records

Each row of `results.jsonl` carries `memory.lessons`: the case's status, code and number of lessons shown. `summary.json` names the lessons file and its sha256 and counts the cases by status. `summary.md` has a line `Lessons: as of each case, from <file>. Cases shown a lesson: ...; with none to show: ...; not shown: ...; failed: ...; not asked: ...`.

### Count first

`eval:memory-count` takes `--lessons`, and then `--memory` is optional. With `--lessons` it prints, after the lines above, `lessons: cases shown a lesson <n>, with a labelled file <n>`, then one line `<key>: lessons <n>` for each case shown a lesson. It counts with the history case's final files, which approximate the files the review is handed.

Only the cases shown a lesson can show an effect, and only the labelled files among them can move Safety. As a starting point, make a paid run only with at least 20 cases shown a lesson and 10 of them with a labelled file.

### Comparing the runs

`score-noise` refuses two runs whose lessons differ, including one with lessons and one without (`different_lessons`).

`score-compare` has a second form for lessons: a candidate with lessons against a baseline without them, both run with the same memory file or none (`different_memory` otherwise). It prints `comparing lessons` first, compares only the cases both runs hold with the status ok, and splits them by the candidate's lessons status: shown a lesson (`ok`), none to show (`empty`), and the rest. Its tables are those of the memory form, for the cases shown a lesson, with the cases with none to show as the noise; its lines read "shown a lesson" where the memory form reads "shown memory". It refuses a baseline run with lessons (`baseline_with_lessons`).

Neither form can tell a lesson that changed a block's score from noise on a small count: the noise between two baselines is the bar, and a difference smaller than two steps is no finding.

### Keep in mind

The lessons are the eval's own, made by the eval's judge, which does not answer exactly as the deployment's nightly judge did. A lesson a team switched off in the deployment is shown anyway, and the history file holds merged pull requests only.

## Seeding a deployment's memory

`eval:seed-memory` writes a history file's activity of people, and the records derived from it, into a deployment's database, so that a new deployment does not start with empty memory:

```bash
npm run eval:seed-memory -- --history ../history.jsonl --config config.local.yml
```

- It reads the file, never GitHub, and needs no GitHub token. The config must have a `database` block.
- It never migrates. The service must have started once on that database, or the command ends with `database_not_migrated` having written nothing.
- For each case it stores the reviews, inline comments and general comments of people, not of bots. For a pull request the service does not store, it also adds its records, dated by its merge, and marks its comments as looked at. For one the service stores, it adds no record, even when the service's row is out of date, and leaves the comments to the learning pass.
- Every insert leaves an existing row as it is, so nothing the service captured is overwritten, and a second run adds nothing. It writes no row of `pull_requests`.

It prints `cases <n>, known <n>, activity <n>, signals <n>`: the cases read, those the service stores, the activity rows added and the records added.

It stops at a bad case, before writing it; the cases before it stay written:

| Code | Why |
|---|---|
| `bad_case_repo` | The case's repository is not `owner/name`. |
| `bad_case_number` | The pull request's number is not a whole number from 1 to 2,147,483,647, or a comment's or review's line or start line is beyond what the database's integer column takes. |
| `bad_case_time` | The merge time or an activity's time names no zone, is before the year 1, or names a day that does not exist. |

One side effect: should the service later store a seeded pull request and review it at the very commit an old comment was made on, the learning pass makes a second record, for the block, beside the seeded one for the file.

## The same measure in the UI

The UI's Repositories page counts with the backtest's own code, on labels built from the stored comments and runs instead of a history file (see [The UI](https://visto-ai.com/docs/the-ui/)). Its numbers and the backtest's differ where the stored data differs from a history file.
