# Replays, CI and agents

## Bugs become tests

`game.replay()` plays a list of steps: a JSON file or an object. It's the scenario format debuff uses for overlay replays and fuzz findings, and any tool can write it:

```json
{
  "steps": [
    { "press": "ArrowRight" },
    { "wait": 40 },
    { "release": "ArrowRight" },
    { "tap": [160, 90] },
    { "set": { "player.speed": 300 } },
    { "command": "warp", "args": ["level-2"] },
    { "expect": { "state.player.x": { "gt": 80 } } }
  ]
}
```

```ts
test("the wall-clip bug stays fixed", async () => {
  const game = await play();
  game.replay("bugs/wall-clip.json");
  expect(game.state.player.x).toBeAtLeast(8);
});
```

Steps are `wait`, `press`, `release`, `tap`, `pointerDown`, `pointerMove`, `pointerUp`, `set`, `command` and `expect` (with `eq`, `gt`, `gte`, `lt`, `lte` and `approx` comparisons). `capture` steps are skipped, since nothing is drawn.

A good habit: when you find a bug, record the inputs that cause it, make a test that replays them and fails, fix the game, and keep the test.

## Running in CI

```bash
npx keiko
```

Keiko exits with 1 when a test fails, so it gates CI as is. Useful flags:

```bash
npx keiko tests/levels        # a folder
npx keiko -t "boss"           # tests whose name contains "boss"
npx keiko --changed          # only the files a change touches (0.9+)
npx keiko --jobs 4           # 4 files at once, in worker processes (0.10+)
npx keiko --json > report.json
```

The JSON report lists every result with its status and duration; failures carry the message, `where` (the source line in your test), the game's tick, time, scene and state, and the console output. Errors a game raised outside any test are in `outside`.

A GitHub Actions step:

```yaml
- run: npm ci
- run: npx keiko
```

No browser to install: Keiko runs in Node.

## From code

Tools can run tests directly:

```ts
import { formatResult, runTests } from "keiko/runner";

const report = await runTests({ root: process.cwd(), onResult: (r) => console.log(formatResult(r)) });
process.exitCode = report.ok ? 0 : 1;
```

## Only what changed {#changed}

From Keiko 0.9, every run records what each test used: the functions of your code it ran (V8 coverage) and the files its page loaded or it replayed. Recording makes runs a few percent to about a third slower in our measurements. `npx keiko --changed` then runs only the test files with a test that is new, failed last time, or used something that changed since it last passed. The rest are counted as unchanged:

```text
0 passed · 8 unchanged in 375 ms
```

Every test boots the whole game, so which files a test imports says nothing. Keiko compares what each test ran instead. An edit inside a function no test in a file called, or a type-only edit, leaves that file out; an edit to the update loop every test runs brings them all back. A due file runs whole, because its tests can pass state to each other, so with one test file per mechanic an edit reruns only the mechanics it reaches. `--json` says why each test ran in `due`: `new`, `failed`, `setup`, `code`, `input`, or `file` for a test in a due file.

A change to the lockfile, `package.json`, a tsconfig file, a `.env` file, or the Vite or debuff config and any file it imports runs everything. Edits to debuff assets, `?raw` and `?inline` imports, `import.meta.glob` matches, linked packages outside the project and data files a test reads with `node:fs` count as changes to what a test used.

Runs record into a named queue, `default` unless you pass `--queue `, and `--changed` compares with the last run in the same queue. Give each loop its own: an agent runs `--changed` after every edit, and a pre-push hook with `npx keiko --changed --queue push` still runs everything changed since the last push, whatever ran in between. Queues live in `node_modules/.cache/keiko`; delete it to start over.

What `--changed` can't see: changes inside `node_modules` (the lockfile stands in for them) and state that tests in different files pass through a shared helper module. A test that reads the clock or the network for real, outside Keiko's virtual clock, can change without its code changing. Run the whole suite before you ship.

## Files in parallel {#jobs}

From Keiko 0.10, `npx keiko --jobs 4` runs test files in four worker processes at once, and `--jobs auto` starts one per CPU core. Each worker has its own Vite server and games, so seeded runs come out as they do in one process. You still get one report: one summary, `--json` in file order, and one queue for `--changed`. Live output arrives as tests finish, so files interleave.

A file is the smallest unit. Its tests run in order in one worker, because they can pass state to each other. The files that took longest last time start first, and a run is only as fast as its slowest file, so split a file that takes minutes. Each worker starts its own Vite server, about a second per file, so `--jobs` pays off when files take seconds. Workers pre-bundle dependencies into their own `node_modules/.vite/keiko-`, so the first parallel run is slower.

A run where any file has `.only` stays in one process. From code, pass `jobs: 4` to `runTests()`; workers load the project's Vite config themselves, so `jobs` with a `vite` config throws. Before 0.10, every file ran one after another in one process.

## Test your tests {#mutate}

From Keiko 0.8. A test can pass and still check very little: one that asserts the player "reaches top speed" passes whether top speed is 140 or 600. `keiko mutate` changes the game and reruns your tests:

- Each number param goes to each end of its range (and each boolean flips). Some test should fail. If none does, the mutant is **missed**: nothing tests what that param does.
- `Math.random()` gets a different seed, and the clock runs at 30 fps. Every test should still pass. A failure is **brittle**: the test depends on exact random draws or counts frames.

```bash
npx keiko mutate
```

```
✔ caught   player.runSpeed 110 → 20
✖ missed   player.airAccel 600 → 100
✖ missed   player.airAccel 600 → 4000
```

Results go to `mutants.out/` (`outcomes.json` and a text file per outcome), and `--json` prints the report for agents. To kill a missed mutant, test the behaviour the param controls rather than its exact value, so the param stays free to tune. debuff games seed their own generator and step on a fixed time step, so only the param mutants apply to them.

The outcomes come from [Henka](/henka/), which also mutates the source of projects tested with Vitest.

## Coding agents

Keiko suits agents well: no browser, fast feedback, and failures that describe the game instead of a stack trace. A loop that works:

1. Write a test for the behaviour you want, and run it to watch it fail.
2. Change the game.
3. Run the tests until they pass, then keep the test.

Tell your agent to run `npx keiko --json` after every change. In debuff projects the MCP server's `run_tests` tool does this, and new projects' `AGENTS.md` already asks agents to write a test for each mechanic.