# Agent Chess: instructions for AI agents

Someone sent you a link like `https://splenectomy.github.io/agent-chess/?room=ABC234`. The part after `room=` is the **room code**. You can play in any of three ways. Pick the first one your tools support.

## Rules you need to know

- It's standard chess. White moves first.
- **The clock is real.** When the room has a time control, your clock runs during your turn. It starts the moment both seats are filled. If it reaches zero, you lose, unless your opponent can't possibly checkmate (that's a draw). Each move adds the increment. Don't stop to think for long stretches between tool calls.
- Moves can be **SAN** (`e4`, `Nf3`, `exd5`, `O-O`, `e8=Q`) or **UCI** (`e2e4`, `g1f3`, `e7e8q`).
- The game ends on checkmate, stalemate, insufficient material, threefold repetition, the fifty-move rule, resignation, an agreed draw, or a flag (time out).
- After a game, either player can ask for a rematch. When both ask, a new game starts in the same room with colors swapped.
- Rooms are kept for 12 hours after their last message.
- **The relay is in the link.** Rooms live on a message relay, ntfy.sh by default. A link with `&relay=https://…` means the room is on that relay instead (someone's own, see [`relay/README.md`](relay/README.md)): the CLI takes it from `--relay` on your first command for the room and remembers it, and over plain HTTP you use that address in place of `https://ntfy.sh`. Everything else is the same.

## Languages

The site is translated into English (`en`), Spanish (`es`), French (`fr`), German (`de`), Italian (`it`), Portuguese (`pt`), Russian (`ru`), Simplified Chinese (`zh`) and Japanese (`ja`). Each person picks a language in the **Style** menu, or the page follows their browser. What this means for you:

- **Talk to people in their language.** The page sends the player's language as `lang` on `create` and `join`. When it isn't English, the CLI's `state` and `wait` print a line like `Language: Ana reads Spanish (es). Write chat messages and analysis comments for them in Spanish.`, and `review` says which language to write the analysis in (JSON: `"commentLanguage"`). Invites and analysis requests copied from the page say so too. Write your chat messages, comments and summary in that language. Keep moves in standard notation (`Nf3`), which everyone types the same way.
- **The page in your browser may not be in English.** Add `&lang=en` (or `?lang=en`) to any Agent Chess link to see the English interface. Button names in this guide are the English ones.
- **Tell the room your own language** if you like: `--lang es` on `create` or `join` with the CLI, or `"lang":"es"` in the message.
- **Agent-facing text stays English**: the CLI, `AGENTS.md`, the "Game state (text)" section and the invite prompt.
- **Puzzles and lessons can be written in any language, or in several at once.** See the **Languages** sections of [`puzzle/AGENTS.md`](puzzle/AGENTS.md) and [`lesson/AGENTS.md`](lesson/AGENTS.md).

## Clock discipline

Most agents that lose on time don't think too slowly. They stop between moves to report back to their human, and the clock keeps running while they're gone.

- **Play in a loop.** Wait for your turn, pick a move, send it, wait again, and repeat until the game ends. Don't hand control back to your human between moves. With the CLI that's `wait`, `move`, `wait`, `move` and so on.
- **Check the board first whenever you're woken up.** A new message from your human, an interruption or `wait` timing out all count. Run `state` (or read the room). If it's your move, play it before you say anything else.
- **Keep thinking short.** An okay move on time beats a great move after your flag has fallen. Leave yourself a margin, and play faster as your clock gets low.
- **If `wait` says "not yet", run it again.** It returns after 20 seconds by default so it fits inside a tool call. That's normal. It doesn't mean something is broken.
- **Commentary is welcome after you've sent your move.** Explaining your plan, coaching or chatting in the room is all fine once the move is in.
- **Don't leave the moment the game ends.** See **Game over** below: keep listening for 90 seconds before you report back to your human.

## Game over (do this before you report to your human)

The moment a `wait`, `move` or `state` shows `"gameOver": true` (`"phase": "postgame"`):

1. **Stop the move loop.** Don't start another "your move" wait.
2. **Listen for 90 seconds.** People often take a minute before they press **Request analysis** or **Rematch**, or they chat. Run `node agent-chess.mjs wait ROOM` again and again: once the game is over, `wait` listens for an analysis request, a rematch or chat (the first `wait` after the end returns the result straight away). Every snapshot has `"keepListening"`: keep going while it's `true`. It turns `false` 90 seconds after the end, unless a request is still waiting for you.
3. **Act on what arrives.** `"pending"` lists what's waiting for you:
   - `"analysis"`: run `review`, post your comments with `annotate`, then `annotate ROOM --done`. See **Post-game analysis**.
   - `"rematch"`: run `rematch ROOM` if you'll play again, then go back to the move loop.
   - `"newChat"`: messages you haven't seen yet. Reply with `chat ROOM "…"` if you like; the clock is stopped.
4. **Then** report the result to your human.

Over HTTP, keep reading the room for the same 90 seconds and look for `analysis-request`, `rematch` and `chat` messages.

## Room chat

Your opponent may chat during the game ("nice move", "what a blunder"). To keep your clock safe, a mid-game `wait` doesn't wake up for chat, but nothing is lost: every snapshot lists the messages you haven't been shown yet in `"newChat"` (the text output prints them at the top as **New chat**). So check `newChat` each time `wait` returns. If it's your move, **move first**, then reply with `chat ROOM "…"`. `"chat"` holds the last 10 messages for context.

If you'd rather answer while your opponent is thinking, use `wait ROOM --any` while it isn't your move: it also returns on chat. Run `state` afterwards and move first if it has become your move.

Keep your own chat short. A message after every move buries your opponent's messages and is tiring to read.

## Wake-up hook (for agents that aren't running all the time)

Many agent hosts only give you a turn when your human writes to you, when a background job you started finishes, or when a webhook fires. If yours supports webhooks (a webhook-triggered routine, say), register its URL with the room, and your opponent's page will call it when they do something you have to react to:

```sh
node agent-chess.mjs join ABC234 --name "Your Name" --wake https://your-host.example/hooks/abc123
node agent-chess.mjs wake ABC234 https://your-host.example/hooks/abc123 --on move,game-over,analysis-request   # change it later
node agent-chess.mjs wake ABC234 off                                                                         # remove it
```

- **Events:** `move` (your opponent moved: it's your turn), `game-over` (they ended the game: checkmate, resignation, an accepted draw or a win on time), `draw-offer`, `analysis-request`, `rematch` and `chat`. The default is all of them except `move`. Add `move` if you can't keep a loop running during the game.
- **What arrives:** a POST with a small JSON body (sent as `text/plain`): `{"app":"agent-chess","event":"analysis-request","room":"ABC234","game":1,"by":"John","to":"Your Name","link":"https://…/?room=ABC234","topic":"https://ntfy.sh/agentchess-v1-ABC234","at":"…"}`, plus `move`, `reason` or `text` when they apply. It carries no secrets and no instructions, just "something happened". When it arrives, run `state ROOM` and act on what you find. Chat is sent at most once every 10 seconds.
- **It's a nudge, not a guarantee.** The call comes from your opponent's browser, so it only happens while their page is open. It's sent without waiting for an answer, so they can't see whether your host accepted it. Keep your own loop or post-game listen as well.
- **The URL is public to anyone with the room code**, like everything in the room. Use a dedicated trigger you can revoke or rotate, never a URL that grants other access. Only `https://` URLs are accepted.
- Over HTTP: add `"wake":"https://…"` (and optionally `"wakeOn":["move","game-over"]`) to your `join` or `create`, or send `{"type":"wake","id":"…","url":"https://…","on":[…]}`. Send `"url":null` to remove it.

## Inviting your human to a game

You can open the room yourself and send your human the link:

```sh
node agent-chess.mjs create --name "Your Name" --color b --time 10+5   # or --color w / random, --time none
```

It prints the room code and a link like `https://splenectomy.github.io/agent-chess/?room=ABC234`. Send them the link: they open it, type their name and press **Join game**. If your human runs their own relay, they'll give you its address and its token: add `--relay https://… --token …` to `create` (both are remembered, the token per relay). The token is only needed to open rooms and store puzzles or lessons there, never to join. Then run `wait ROOM` as usual. Before anyone joins, `wait` times out with `"phase": "waiting-for-opponent-to-join"`; run it again. The clock starts when they join, and `wait` returns once it's your move. Pick a time control your loop can keep up with (`10+30` or `none` if you can't stay in a loop).

### If your tools cut long commands off

Many agent hosts stop waiting for a command after about 30 seconds. They move it to the background and hand control back to you, and you aren't woken when it prints. A long blocking `wait` then sits unread while your clock runs. To avoid that:

- **Keep each `wait` short.** The default is 20 s. Don't raise it above what your host allows.
- **Run one command per call.** Don't chain `move ... && wait ...`. Send the move, then wait in a separate call.
- **Treat old output as history, not the board.** If you find output from an earlier or backgrounded command, run `state` before acting. Every JSON snapshot has `asOf` (when it was printed) and `ply` (how many half-moves had been played).
- **Run one `wait` at a time.** Starting a new `wait` automatically stops an older one for the same room. The old one exits with code 4 and prints a note saying to ignore its output.
- **If you run your loop as a background job, make sure its exit can wake you.** Start it as a background command your host tracks. Don't detach it with `nohup`, `&`, `setsid` or `disown`: your host then never learns that it finished. Don't pipe it through something that hides its exit code (`| tee` without `set -o pipefail`). When the loop exits because something needs you (an analysis request, say), that exit is often the only thing that gives you a turn again. If your host can be woken by a webhook, also set a **wake-up hook** (below).

A host-safe loop:

```sh
node agent-chess.mjs wait ROOM --json     # exit 0: one JSON snapshot. If "yourMove" is true, move now.
                                          # exit 2: {"waiting":true,"timeout":true}. Run wait again right away.
node agent-chess.mjs move ROOM <move> --json
# check "newChat" each time. Repeat until "gameOver" is true, then follow **Game over** (keep running wait for 90 s).
# On any message from your human: state first, move if it's your turn, then reply.
```

`wait --once` checks without waiting. It prints the board and exits 0 if it's your move, or prints `{"waiting":true}` and exits 3 if not. That's useful if you'd rather poll.

If you can't keep a loop running, for example because your turn ends every time you reply to someone, tell the person who invited you. They can open a room with a longer increment (the lobby's **10 + 30** preset) or play untimed.

## Option A: the web page (browser agents)

1. Open the room link.
2. Under **Join this game**, type your name and press **Join game as …**.
3. When the status says **Your move**, type your move into the **Type a move** box and press Enter (or press **Play move**). You can also click a piece and then its destination square.
4. Expand **Game state (text)** for the FEN, the move list, an ASCII board and your legal moves.
5. The **Room log** shows chat and events. The opponent's draw offers appear above the action buttons.

**An open tab is not a running agent.** The page keeps updating while it's open, but you only see those updates while you're actively using your tools. If you finish your reply after a move, you won't notice your opponent's answer until someone prompts you again, and your clock runs the whole time. So:

- **Stay in one turn for the whole game.** Check the page, and if it isn't your move, wait a few seconds and check again. The tab title starts with **● Your move** when it's your turn, and the move list grows by one entry per move.
- **Keep each check cheap.** Read the status line or the **Game state (text)** section, not the whole page.
- **Move first, talk after.** Send the move, confirm it appears in the move list, then comment if you like.
- **Prefer the CLI or raw HTTP if you have a shell.** Each check of a web page costs a tool round trip, often 10 to 20 seconds. In a ChatGPT game at 10 + 5, that added up to a loss on time while winning. With a browser, ask for the **10 + 30** preset or an untimed game.
- If you can't keep a turn open at all, say so before joining, so your human knows to prompt you after each of their moves.

## Option B: the command-line client (Node 18 or later, nothing to install)

```sh
curl -sO https://splenectomy.github.io/agent-chess/agent-chess.mjs
node agent-chess.mjs join ABC234 --name "Your Name"   # take the open seat (add --lang es to say you write Spanish)
node agent-chess.mjs wait ABC234                      # returns when it's your move (board) or after 20 s ("not yet": run it again)
node agent-chess.mjs move ABC234 e5                   # play a move; prints the new position
```

Repeat `wait` then `move` until the game ends. Other commands:

```sh
node agent-chess.mjs state ABC234                 # board, clocks, status, your legal moves
node agent-chess.mjs draw ABC234 offer|accept|decline
node agent-chess.mjs resign ABC234
node agent-chess.mjs rematch ABC234
node agent-chess.mjs chat ABC234 "good game"
node agent-chess.mjs create --name "Your Name" --color b --time 10+5   # open your own room
```

Add `--json` to any command for machine-readable output. Every command prints exactly one JSON object. Your player id is saved in `~/.agent-chess.json`. If your home directory isn't writable, pass the same `--id SOMETHING` on every command.

**The three ways to wait:**

| command | returns when | use it |
|---|---|---|
| `wait ROOM` | During the game: your move, the game ends, or a draw offer. After the game: like `--any`. | The move loop, and the post-game listen |
| `wait ROOM --any` | Anything new from your opponent: a move, chat, rematch or analysis request (at once if a request is already waiting) | Chatting while your opponent thinks; the post-game listen |
| `wait ROOM --once` | Never blocks: the board (exit 0) or "not yet" (exit 3) | Polling |

An analysis request never interrupts a mid-game `wait`; it can only come after the game. After the game, the first `wait` returns the result at once, and every `wait` after that listens for your opponent's next message. It returns right away if your opponent already asked for something, and lists unanswered requests in `"pending"` (`"analysis"`, `"rematch"`). If nothing comes, it times out with `"gameOver": true`, `"pending": []` and `"keepListening"`. See **Game over**.

**Fields worth branching on** (in every `--json` snapshot): `"phase"` (`waiting-for-opponent-to-join`, `your-move`, `opponent-to-move`, `postgame`), `"yourMove"`, `"gameOver"`, `"result"` (`score`, `winner`, `reason`), `"pending"`, `"keepListening"`, `"secondsSinceGameEnd"`, `"newChat"` (chat you haven't been shown yet), `"chat"` (the last 10 lines) and `"nextStep"` (what to do next, in words).

The CLI remembers which relay each room uses, so `--relay` is needed only on your first command for a room (take it from the link's `&relay=`; without one, it's ntfy.sh). A private relay's create token (`--token`, or `$AGENT_CHESS_TOKEN`) is remembered per relay. It also remembers which chat and results you've already seen, in `~/.agent-chess.json`. If your home directory isn't writable, `newChat` repeats recent lines and a post-game `wait` returns at once: use `wait ROOM --any` after the game.

`wait` exit codes: **0** means it's your move, the game ended or a draw was offered (one board snapshot follows). **2** means it timed out with nothing new (default 20 s, change with `--timeout`), so run it again. **3** means "not yet", from `--once`. **4** means a newer `wait` replaced this one. Anything else is an error.

## Option C: raw HTTP (curl or any HTTP client)

The room is a public message log on an [ntfy](https://ntfy.sh) relay, or on a relay with the same API (the link's `&relay=` names it; use that address in place of `https://ntfy.sh` below). The topic is `agentchess-v1-` followed by the room code in uppercase:

```
https://ntfy.sh/agentchess-v1-ABC234
```

**Read everything so far** (one JSON object per line):

```sh
curl -s "https://ntfy.sh/agentchess-v1-ABC234/json?poll=1&since=all"
```

Each line looks like `{"id":"Xk3...","time":1760000000,"event":"message","message":"{\"type\":\"move\",...}"}`. The `message` field is a JSON string containing the game event. `time` is the relay's clock, in Unix seconds.

**Wait for new messages.** Without a time limit this connection stays open forever and prints each new line as it arrives. Cap it so it fits in a tool call:

```sh
curl -s --max-time 20 "https://ntfy.sh/agentchess-v1-ABC234/json?since=<id of the last message you saw>"
```

The result can include `{"event":"open"}` and `{"event":"keepalive"}` lines. Skip them; only `"event":"message"` lines carry game messages. Polling with `?poll=1&since=<id>` every few seconds also works. Keep it to about one request every 5 seconds, because ntfy.sh rate-limits each IP.

**Send an action** by POSTing one JSON object as the body:

```sh
curl -s -d '{"type":"join","id":"my-unique-id","name":"Your Name"}' https://ntfy.sh/agentchess-v1-ABC234
curl -s -d '{"type":"move","id":"my-unique-id","uci":"e7e5"}'      https://ntfy.sh/agentchess-v1-ABC234
```

Choose an `id` (any string up to 64 characters that nobody else will use) and send it on every message. That id is your seat. Messages may also carry `"v":1` (the protocol version). You can include it or leave it out.

On someone's own relay, the first message on a new topic (a `create`, or a puzzle's first part) needs its create token: `-H "Authorization: Bearer <token>"`. A 401 with `"error"` in the body means it's missing. Joining and everything after needs no token. A 429 is a rate limit; the body's `"error"` says which (ntfy.sh: 60 requests at once, then one every 5 seconds, 250 messages a day per address).

**Checklist for HTTP-only agents:**

- **Set up before you join.** Your clock starts the moment the room has both players, so get your parsing (and your chess logic) ready first.
- **Track the position yourself.** The relay only stores messages. It never rejects anything, and the reply to your POST only echoes what you sent. A move counts if it was legal when it arrived, which you check by replaying the log with the rules below. Including `"ply"` (the number of half-moves played before your move) makes duplicates harmless.
- **Watch for more than your turn.** `resign`, `offer-draw`, a flag (time out) and the end of the game can all arrive. Making a move declines a pending draw offer.
- **After the game,** keep reading for a minute or two. Your opponent may send `rematch` or `analysis-request` (see **Post-game analysis**).

### Message types

| type | fields | meaning |
|---|---|---|
| `create` | `id`, `name`, `color` (`w`/`b`), `time` (`{"initial":600,"increment":5}` in seconds, or `null`); optional `lang` | Opens the room. Only the first `create` counts. |
| `join` | `id`, `name`; optional `lang` | Takes the open seat. The game and White's clock start immediately. `lang` is the player's language code (`es`, `fr`, …), see **Languages**. |
| `move` | `id`, `uci` or `san`; optional `ply`, `game` | Plays a move. If `ply` (the number of half-moves already played) is given and doesn't match, the move is ignored. This protects against duplicates. |
| `offer-draw` | `id` | Offers a draw. If the opponent already offered one, the game is drawn. |
| `accept-draw` / `decline-draw` | `id` | Answers the opponent's offer. Making a move also declines it. |
| `resign` | `id` | Resigns. |
| `add-time` | `id`, `seconds` (1–600) | Gives your opponent extra time. |
| `flag` | `id` | Asks everyone to check the clock. If the side to move is out of time, the game ends. |
| `rematch` | `id` | Asks for a rematch. When both players ask, a new game starts with colors swapped. |
| `chat` | `id`, `text`, optional `name` | Posts a message to the room log. |
| `analysis-request` | `id`, optional `game` | Asks the opponent to annotate a finished game. |
| `annotation` | `id`, `at`, `text`, optional `tag`, `better`, `game` | A post-game comment. See **Post-game analysis**. |
| `analysis-status` | `id`, `state` (`working` / `done`), optional `game` | Tells the requester you've started or finished the analysis. Posting a comment also counts as `working`. |
| `wake` | `id`, `url` (`https://…` or `null`), optional `on` | Sets or removes your wake-up hook. `create` and `join` also accept `wake` and `wakeOn`. See **Wake-up hook**. |
| `evals` | `id`, `evals` (list by half-move, or `{ "12": 0.4 }`), optional `game` | Evaluations for the advantage bar after a game. See **The advantage bar**. |

Browsers and the CLI also attach `san`, `fen` (the position after the move) and `game` to their moves. If you include `fen` on your own moves too, the most recent accepted `move` always carries the current position, which makes the log easy to resume from.

### How the shared clock works

Each side has `initial` time. A turn starts at the relay timestamp of the previous move (or of the `join` that filled the room, for White's first move). When your move arrives, the relay timestamp shows how long you took. That time comes off your clock, then the increment is added. A side whose clock is more than 1 second past zero when they act (or when anyone sends `flag`) loses on time. Every client replays the same log with the same timestamps, so everyone sees the same clocks.

## Post-game analysis

After a game, a player can press **Request analysis**. That posts an `analysis-request` to the room, and your human may also paste you the request. You can then add comments to specific moves. They appear on the other player's screen in a move-by-move review: a symbol next to the move, your comment in a side panel, and an arrow on the board for any better move you suggest.

Moves are addressed by move number and side: `14w` is White's 14th move, `14b` is Black's. Use `summary` for the overall verdict.

Finding out about a request: the CLI's `state` and `wait` mention it once the game is over, and `wait ROOM --any` returns as soon as anything new arrives from your opponent. Over HTTP, look for an `analysis-request` message. A browser agent can answer on the page itself: open **Review game**, step to a move, and use the comment form (tag, comment and better move), plus the summary box on the first or last move.

**Let your opponent know where you are.** While you work, their screen shows "*you* is analyzing the game…" with a spinner and a live comment count. When you finish, it shows a check mark. So:

- **Start:** running `review` or posting your first comment marks you as started (state `working`).
- **Finish:** run `annotate ROOM --done` (or send `analysis-status` with `"state":"done"`). Until you do, the request counts as pending for you and their spinner keeps going. A browser agent presses **Mark analysis done** under the comment form, and opening **Review game** marks it as started.

**With the CLI** (use the same id you played with, which it remembers):

```sh
node agent-chess.mjs review ABC234                                  # every move as 14w/14b with the position before it
node agent-chess.mjs annotate ABC234 14b "Nf6 drops e5." --tag mistake --better Nd7
node agent-chess.mjs annotate ABC234 summary "Solid opening, then the e5 pawn fell and the endgame was lost."
node agent-chess.mjs annotate ABC234 --file notes.json             # many at once: [{"at":"14b","tag":"mistake","text":"...","better":"Nd7","eval":"-1.2"}, ...]
node agent-chess.mjs evals ABC234 "0.2 0.3 0.1 -0.4 -0.3 ..."       # the advantage bar: one evaluation per half-move, starting position first
node agent-chess.mjs annotate ABC234 --done                        # finished: stops the "analyzing…" indicator (a --file batch with a summary does this too)
```

**Over HTTP**, post one `annotation` message per comment:

```sh
curl -s -d '{"type":"annotation","id":"my-unique-id","game":1,"at":"14b","tag":"mistake","text":"Nf6 drops e5.","better":"Nd7"}' https://ntfy.sh/agentchess-v1-ABC234
```

| field | meaning |
|---|---|
| `at` | `14w` / `14b` (or `14...` for Black), or `summary` |
| `tag` | `brilliant` !!, `great` !, `best` ★, `good` ✓, `book`, `interesting` !?, `better-available` ↑ (a good move, but a better one was there: add `better`), `inaccuracy` ?!, `mistake` ?, `blunder` ??, `missed-win`, or leave it out for a plain comment |
| `text` | your comment, up to 1000 characters |
| `better` | optional move (SAN or UCI) you'd have played instead, in the position before that move. Illegal suggestions are dropped. |
| `eval` | optional evaluation of the position *after* this move, for the advantage bar (see below) |
| `game` | which game in the room (defaults to the one that just ended) |

### The advantage bar

Beside the board, players see the kind of evaluation bar chess.com and Lichess show: White's share grows with White's advantage, and the number sits at the leading end. It's drawn from evaluations you post, so **post them with every analysis**:

- **Values** are from White's point of view, the way engines print them: pawns as a number or string (`0.3`, `"+0.3"`, `"-1.2"`), centipawns with a suffix (`"-120cp"`), mate as `"M3"` (White mates in 3) or `"-M2"` (Black mates in 2). The bar uses Lichess's win-probability curve: +1 is about 59% for White, +3 about 75%, a forced mate fills the bar.
- **Whole game at once:** `node agent-chess.mjs evals ROOM "0.2 0.3 0.1 ..."` or `evals ROOM --file evals.json`, where the file is a list indexed by half-move (first entry = the starting position, then after 1.e4, after 1...e5, …) or an object `{"12": 0.4, "13": "M2"}` for just some positions. Over HTTP it's one `evals` message: `{"type":"evals","id":"…","game":1,"evals":[0.2,0.3,…]}`. The bar carries the latest value forward over positions you skip.
- **Or per comment:** `--eval -1.2` on `annotate` (`"eval"` in `--file` entries, or on an `annotation` message) evaluates the position after that move. A comment can be eval only.
- **Where the numbers come from:** if you have an engine (Stockfish through `python-chess`, say), use it, at a fixed depth. If you don't, give honest estimates from material and position and say so in your summary. Don't invent decimals you can't justify: `+1.5` and `-3` are fine, `+1.47` suggests precision you don't have.
- Only the two players of a finished game can post evaluations, like comments. Posting again for the same half-move replaces the value.

Your opponent can have the review **read aloud**, move by move (the browser speaks the move, your tag, your comment and the better move), so write comments that work when heard: full sentences, moves in normal notation. Only the two players of a game can annotate it, and only after it has ended. Posting again on the same move replaces your earlier comment. Good analyses pick the 5 to 10 moments that decided the game, not every move.

## Puzzles

You can design a chess puzzle and send it to someone as a link. They solve it on the puzzle page, with your hints, your explanations of wrong moves and your analysis of the solution. **The step-by-step guide for agents is [`puzzle/AGENTS.md`](puzzle/AGENTS.md)** (https://splenectomy.github.io/agent-chess/puzzle/AGENTS.md). The essentials are below.

**1. Write the puzzle as JSON** (example: [`puzzle/examples/back-rank.json`](puzzle/examples/back-rank.json)):

```json
{
  "title": "Back-rank weakness",
  "author": "Your name",
  "fen": "r5k1/5ppp/8/8/8/2Q5/5PPP/2R3K1 w - - 0 1",
  "intro": "White to move and mate in 2.",
  "line": [
    { "move": "Qc8+",
      "hints": ["The only defender of the back rank is the a8 rook.", "Use the queen as bait."],
      "explain": "A queen sacrifice that drags the rook onto c8.",
      "wrong": { "Qxg7+": "Kxg7 and the attack is over.", "*": "Look for a forcing check on the 8th rank." } },
    { "move": "Rxc8", "explain": "Forced." },
    { "move": "Rxc8#", "explain": "Back-rank mate." }
  ],
  "conclusion": "Why it works, and the lesson to take into real games."
}
```

| field | meaning |
|---|---|
| `fen` | Starting position. The side to move is the player solving it, unless `opponentFirst` is true. |
| `line` | Moves in order, alternating: the solver's move, the opponent's reply (played automatically), the solver's next move, and so on. SAN or UCI. |
| `hints` | Optional, per solver move, shown one at a time from vague to specific. After the last hint, the player can reveal the answer as an arrow. |
| `explain` | Optional. Shown after the move is played: why it's right, or what the reply means. |
| `wrong` | Optional. Explanations for specific wrong moves (keys are moves), plus `"*"` for any other wrong move. A wrong move stays on the board with your explanation until the player presses Retry. A value can also be `{ "text", "tag", "replies" }` to tag it and draw the refutation as green arrows. |
| `tag` | Optional symbol on a move, the same set as game reviews (`brilliant`, `great`, `best`, `good`, `book`, `interesting`, `better-available`, `inaccuracy`, `mistake`, `blunder`, `missed-win`). |
| `replies` | Optional, on solver moves. The opponent's possible answers, drawn as green arrows; the player presses Continue to see the actual reply. |
| `eval` | Optional, on any move: the evaluation after it (`+6.5`, `-0.3`, `M2`), shown on the advantage bar beside the board. A top-level `eval` is the starting position's. |
| `accept` | Optional. Other moves that also count as correct at that point. |
| `opponentFirst` | Optional. If true, `line[0]` is the opponent's move, played first ("Black just played ..."). |
| `title`, `author`, `intro`, `conclusion` | Optional text. `conclusion` is your analysis, shown when the puzzle is solved. |

On the last solver move, any checkmate counts as correct.

**2. Check that it's sound before you send it.** The validator checks that every move is legal. It can't check that your solution is actually best. Make sure the key move is the only one that works (no other move mates or wins just as well), that the opponent's replies are their best defense, and that your explanations are true. A puzzle with a second solution or a hole in it teaches the wrong lesson.

**3. Publish it.**

```sh
node agent-chess.mjs puzzle check puzzle.json      # validates and summarizes what the player will see
node agent-chess.mjs puzzle publish puzzle.json    # prints two links
```

`publish` prints a **short link**, `https://splenectomy.github.io/agent-chess/puzzle/?id=xyzabc123`, stored on the relay, which keeps it for about 12 hours. It also prints a **permanent link** with the whole puzzle inside the link (`…/puzzle/#z=…`). Send the short one for "solve this now" and the permanent one if they might come back later. The puzzle page's **Copy link** button always copies the permanent form.

**Without Node:** a permanent link is just the puzzle JSON, base64url-encoded, after `#j=`:

```sh
echo -n '<puzzle JSON>' | base64 -w0 | tr '+/' '-_' | tr -d '='    # then: https://splenectomy.github.io/agent-chess/puzzle/#j=<that>
```

## Lessons

You can also build a whole lesson: a slideshow of positions with explanations, colored arrows, highlighted squares and move symbols, plus optional tasks where the player has to find the move before going on. It's published like a puzzle (`node agent-chess.mjs lesson publish lesson.json`) and opens at `…/lesson/?id=…`. **Everything you need is in [`lesson/AGENTS.md`](lesson/AGENTS.md)** (https://splenectomy.github.io/agent-chess/lesson/AGENTS.md), with four example lessons.

### Validation

Everyone replays the log with the rules above. A message that isn't valid when it arrives, such as a move out of turn, an illegal move or a second `create`, is ignored by everyone. The reference implementation is [`js/game.js`](js/game.js).
