# Recordings and replay

> Every revision of a Minekube game records its message log on the server. Copy a session into your project, and bun test replays it through today's rules and names the first message that now behaves differently.

A bug that a player hit once is hard to reproduce by hand. A Minekube game does not need you to reproduce it: the server recorded the session as data. You copy the recording into your project, and it becomes a test that runs in Bun.

## Recordings

Every revision on the server writes its message log to a recording:

```text
plugins/Minekube/recordings/<game>/<start>-<revision>.ndjson
```

- **One file per revision start.** `<start>` is the time the revision started, and `<revision>` is the first 8 hex digits of the SHA-256 of its bundle.
- **One line per entry.** The first line is a Wire `Hello`. Each following line is one entry of the Effect UI `MessageLog`: a message with the Model after it and the Commands it issued, a Command that started or ended, a Subscription, a failure. Messages and Models are encoded through the component's Schemas, so the file is plain JSON.
- **Each entry carries `tick`**, the world's game time when it happened. Clients see the same game time, so a client's recording lines up with the server's recording tick for tick.
- **One instance per start of the game.** The component path is `/Minekube/<Name>[0]`, and `[1]` after a [restart](https://minekube.com/games/write/game-structure#failures).
- **The server keeps the newest 50 recordings per game.** Older ones are deleted when a new revision starts.

The runtime writes entries once per tick, off the event path. A recording that cannot be written turns itself off and says why in the server log. The game keeps running.

Bundles in a [bot client](https://minekube.com/games/testing/bots) are recorded the same way, to `minekube/recordings/<bundle>/` in the client's game directory.

## Replays

To turn a session into a test:

1. Play the behavior on the dev server, or find the session where a player hit it.
2. Copy its file from `plugins/Minekube/recordings/<game>/` into your project's `recordings/` folder.
3. Run `bun test`.

From then on, `bun test` replays it through your current rules, message by message:

> Scene: You copy the session you played into recordings/ and run bun test. The replay passes through each of its 16 placed blocks with today's rules, and bun reports 3 pass, 0 fail.

The test that does it comes with every project:

```ts title="src/Round.test.ts"

// Sessions recorded on the server, copied into recordings/, replay through today's rules.
const root = `${import.meta.dir}/..`;
describe("recorded sessions", () => {
  for (const recording of new Glob("recordings/*.ndjson").scanSync(root)) {
    test(recording, () =>
      Effect.runPromise(Effect.gen(function* () {
        const log = yield* Effect.promise(() => Bun.file(`${root}/${recording}`).text());
        const frames = yield* decodeRecording(log);
        const replay = Story.fromLog(Round, frames);
        expect(replay.divergence).toBeUndefined();
      })));
  }
});
```

`Story.fromLog` starts from the recorded Model, sends each recorded message through today's `update`, and compares the Model and the Commands with what was recorded. It never runs Command bodies or Subscriptions, so a replay needs no server.

## Read a divergence

- **A divergence names the first message** whose `update` produced another Model or other Commands than the recording. If you changed the rule on purpose, record a new session and replace the file. Never edit a recording by hand.
- **A divergence on an unchanged rule** means that `update` read something outside its arguments: the clock, a random number, a global. Move that value into a Command or into the message.
- **Check that something was replayed.** A revision nobody played only has `Mounted`. `expect(replay.replayed).toBeGreaterThan(0)` makes sure the test checked real messages.
- **`Story.fromLog` replays the first instance.** Pass `{ path }` for another one, such as the instance after a restart.

## What is planned

Today a recording holds the rules' side of a session: messages, Models and Commands. The plan is to grow it into a full match record. None of the following exists yet.

| Planned | What it adds |
| --- | --- |
| A world track | The [world stream](https://minekube.com/games/local-development/live-view) of a camera next to the message log, on the same tick clock, so a replay shows what players saw. |
| Replay in the browser | A timeline that plays, pauses and scrubs a recorded match in the browser, next to the message log. |
| Checkpoints and seek | Full snapshots at intervals, so a viewer can jump to any tick without replaying from the start. |
| Clips | A shareable range of ticks from a recording, with access that its owner can revoke. |

The design learns from existing replay tools for Minecraft, such as Flashback, ReplayMod, ServerReplay and AdvancedReplay. The difference is that a Minekube recording replays the rules, not only the packets.

## Related

- [Devtools and message logs](https://minekube.com/games/testing/devtools): read the same log live, with cause chains and Model diffs.
- [Test rules with Stories](https://minekube.com/games/testing/rules): the Story API that replays use.
