DEV Community

Cover image for One rules engine, two languages
Amitesh
Amitesh

Posted on Edited on

One rules engine, two languages

CheX runs the rules of chess twice: once in TypeScript in your browser, once in Rust on the game's server. Both must agree on every move, every capture and every checkmate, down to the last byte. This post is about how the game keeps two engines in two languages saying exactly the same thing.

Why write the rules twice

Online chess has a trust problem. If only your browser knew the rules, anyone could open the dev tools and teleport a queen. So the server has to be the referee: it checks every move and decides what the game looks like.

But a referee that lives 200 ms away makes the game feel slow. Every tap would wait for a round trip before a piece moved. So the browser needs to know the rules too, so it can show legal moves instantly and play your move the moment you make it.

The browser runs on TypeScript and the server on Rust, for speed and safety. So the game has one set of rules in two languages, and they must never disagree. If they did, the board you see and the game the server records would quietly drift apart.

One engine, designed to be copied

The trick is not clever translation. It is designing the engine so that a faithful copy is easy to write and easy to check. Three choices did most of the work.
1. The game is a pure function. The whole engine boils down to one function: gameReducer(state, action) → state. Same state and same move in, same new state out. No clocks, no randomness, no hidden globals. A pure function is easy to port, because there is nothing around it to port.
2. Every move is a list of small effects. A move does not change the board directly. It produces effects such as relocate, remove, transform (promotion) and spawn, and one small function applies them. Castling is two relocates. En passant is a relocate plus a remove. Both engines only need to agree on six effect types, not on every special case in chess.
3. Variants plug in as rule modifiers. CheX mods, like the knight that can leap like a camel, are not forks of the engine. A mod adds movement rules to a piece kind through one hook, and the engine treats them like the piece's own rules for moves, attacks and check. Charges and cooldowns live in the game state as plain data.
The result is about 1,300 lines of TypeScript and 1,700 lines of Rust, with the same files, the same functions and the same names on both sides. Reading them side by side feels like reading a translation with the original on the facing page.

Keeping them honest: TypeScript records, Rust replays

A copy is only as good as the test that checks it. The game's test is simple: TypeScript plays a game and writes down everything, and Rust has to reproduce it exactly.


A small script plays a game with the TypeScript engine and saves three things to a JSON fixture: the starting state, every action, and the full game state after each one. Each state is everything: the board, every piece, castling rights, the event log, even the mod charges left on each knight.
On the Rust side, a test loads that file, feeds the same actions through the Rust reducer and serializes each result back to JSON. Then it demands an exact match with what TypeScript wrote. Not "same position". The same JSON: the same fields, the same order of events, the same piece ids. If anything differs, the test fails and prints both versions side by side at the exact move where they split.
The test suite keeps two of these recorded games:

  • A classic game of 29 moves, checked at every single step.
  • A knight-camel game of 90 moves, ending with a rematch. The script picks camel leaps 70% of the time when one is legal, so charges, cooldowns and "used up" knights all get exercised. It is checked every 15 moves to keep the file small.

The moves come from a seeded random generator, so the same script always records the same game. When a rule changes, both engines change with it, the game is re-recorded, and the Rust test shows whether the copy is still faithful.

Parity only proves the two engines agree, not that they are right. For that, the Rust engine also runs perft, chess programmers' standard correctness test: count every legal position a few moves deep and compare with known answers. From the starting position that is 20, 400, 8,902 and 197,281 positions at depths 1 to 4. Four more tricky test positions, full of castling, en passant and promotions, are checked too.

What two identical engines do for the gameplay

Moves that never wait for the network. When you move a piece online, your browser runs the move through its own engine and shows it at once, before the server has even heard about it. Because the server runs the same rules, its answer lands on an identical state and silently replaces the browser's. In tests, your move appeared in about 18 ms, even with 600 ms of simulated lag. If the server ever disagrees, the browser rolls back to the server's version.

One message format for both sides. The browser and the server send each other the same game state JSON that the parity test compares. There is no translation layer to go wrong, because the format is the very thing the parity test checks.

Mods ship on both sides at once. Adding the knight-camel mod meant adding one movement rule to each engine and recording a new parity game. The day the Rust test went green, the browser and the server agreed on every camel leap, check and cooldown.

The cost is real: every rule change is written twice, and the fixture has to be re-recorded. For a game where trust and speed both matter, that is a good trade.

If you are building something similar

  • Make the core a pure function of state and action. It is the easiest code in the world to port and to test.
  • Shrink the surface the two sides must agree on. Small effects beat special cases.
  • Test the copy against the original, not against your idea of the original. Record real runs from one engine and replay them in the other, comparing the exact output.
  • Keep a separate correctness test like perft, so both engines are not just agreeing on the same mistake. Next time: how your move shows up before the server even knows about it.

Top comments (0)