_Published: 2026-12-05_
**tl;dr: I rewrote Nexus Rift's server and client in Gleam in about a month, with a lot of help from AI agents. The Gleam community is pretty sceptical that AI can write good Gleam. I think they're half right, and the other half is fixable.**
> [!info] [Nexus Rift](https://nexusrift.gg) is a persistent mech war that runs in your browser.
> Citizens build, repair and trade mechs, deploy them into a live three-faction arena that never stops, and spend NXR to swing battles while they watch.
Back in April 2024 I wrote [Squash bug families, not bugs with Gleam](https://md.jtmn.dev/blog/%F0%9F%92%BB+Programming/PR-008+-+Squash+bug+families%2C+not+bugs+with+Gleam). I ended it by saying I wouldn't be forcing Gleam on a real codebase anytime soon. Side projects only.
Well. Nexus Rift is my side project, and it's not small anymore.
## How we got here
If you've read [[NXR-001 - The Engine to No Engine Journey]], you know I've bounced between more engines than I care to admit. The short version of the last three months:
1. A React client that had become AI slop: features bolted on everywhere, no thought about layout.
2. An Evangelion restyle. Deleted in a week.
3. A Go + HTMX "linked world" where every entity has its own page. It worked, and it looked like a bank.
4. Ebitengine. Again. Rejected the same day.
5. Godot via graphics.gd. Gorgeous, but 45-minute UI test runs, huge downloads, and no real web support for Clerk.
6. A second React client.
7. Gleam, server and client.
The full story for players is in the [Nexus Rift status report](https://nexusrift.gg/dispatches/status-report-2026-12-05-seven-rewrites-and-a-cutover). This post is about the Gleam part.
![[nxr-0c-bench.jpg]]
*The old crafting bench. Acres of whitespace, tiny text, nothing lines up.*
![[nxr-5a-godot-operations-menu-2026-10.jpg]]
*The Godot version. Gorgeous, and miserable to iterate on.*
## Why Gleam, for real this time
Nothing has changed about _why_ I like it:
- It's basically Go (to me anyway), but functional. Pipelines, immutable data, no magic.
- `case` expressions are great, and exhaustiveness checking means the compiler finds the branch you forgot.
- Static types, no nulls, no exceptions.
- The BEAM for concurrency and supervision.
- It compiles to JavaScript as well, so one language covers the server and the browser.
What changed is the ecosystem. When I first looked, there was no proper language server, Postgres support was thin and S3 was a "write it yourself". Now there's a real LSP, [Squirrel](https://github.com/giacomocavalieri/squirrel) for typed SQL, and S3 clients that work. Every box I needed is ticked.
There's this also this big fight online (when is there not?) about the best language for agentic coding.
- I'm told its TypeScript, because of all the training data
- But its actually Go, because its design for large scale, maintainable software that's easy to understand
- Nah its Rust because its totally safe and precisely what you asked for.
But I had a theory I wanted to test out. That **functional, immutable code is good for AI**. No hidden state, no spooky action at a distance, a compiler that is strict and fast. A language that is simple, in which an agent can read a function and know everything it can touch. The compiler tells it when it's wrong, quickly and precisely.
## What I built
About 34 days, from 2 November to 5 December:
- **~1,400 commits and ~1,350 merged pull requests**
- **4 packages:** `sim`, `shared`, `server` (Erlang target) and `client` (JavaScript target)
- **~800 modules and ~5,000 tests**
- **~400 Postgres migrations**, carried over from the Go server
Yes you heard me right. I decided to swap the engines out of my passenger jet (Go server) and put some nice immutable ones in (Gleam server), while it was flying in the air.
About 410k lines of Go became about 270k lines of Gleam on the server, roughly a third smaller. The client shrank far more, from about 463k lines of React to about 70k lines of Gleam plus JS!
- 21 November 2026: Go server competely replaced with the new Gleam server
- 5 December 2026: React app completely (ish) replaced with the new Gleam client
![[nxr-7-gleam.jpg]]
*The new Gleam client: Expeditions, rebuilt. It doesn't look amazing... yet!*
### One simulation, two runtimes
The battle simulation is a pure Gleam package. It doesn't touch a clock, the network or the database. It takes a seed and a list of commands, and produces a battle deterministically.
The same code compiles to Erlang and to JavaScript, and on every change CI runs a nine-mech battle with seed 42 on _both_ and fails if the final digests differ by a single bit.
That's harder than it sounds. JavaScript numbers are not Erlang numbers. `int.parse("9007199254740993")` gives you a different answer on each. Floats format differently. So the sim uses fixed-point maths, its own PRNG and committed trig tables.
Today the server runs the sim and the browser plays back a recording. The point of all this is what it unlocks _later_: send commands instead of frames, run battles client-side, run your own practice solo arenas, and replay anything exactly.
### The store pattern
The client uses [Lustre](https://lustre.build), and the architecture leans heavily on the "store pattern" [Hayleigh Thompson](https://github.com/hayleigh-dot-dev) presented at Code BEAM '26.
Every signed-in citizen gets a store: a process on the server that owns their state and talks to the browser over one socket. The client dispatches events, the store decides what happens, and the client renders what the store provides.
Because it's the BEAM, each store is supervised. If one crashes, it restarts on its own and nobody else notices (yay the thing Kubernetes invented apparently). Background loops (arena, economy, repairs) live under one supervisor, too.
It's weird, so here's a picture. If you've used Redux: imagine the Redux store lives **on the server**, one per signed-in player, and the browser is just a view that sends actions down a websocket.
![[remote_store.png]]
The browser never owns game state. It dispatches an event, the store runs `update` on the server, and the new slices of state stream back over the socket. If a store crashes, the supervisor starts a fresh one and the page carries on.
### Islands for the heavy stuff
Not everything belongs in Gleam. The 3D arena replay, the main menu's turning mech and the repair mini-game are three.js, written as plain JavaScript "islands": custom elements the Gleam client mounts and talks to. Gleam owns the data and the page; the island owns the pixels.
### Boring but important
- **Typed SQL.** Squirrel generates Gleam functions from `.sql` files. CI fails if the generated code is stale.
- **Warnings fail the build.** Every package, both targets.
- **One release artifact.** An OTP release with each tier's settings embedded, so a server boots without a checkout.
## The AI part
Here's where it gets awkward.
The Gleam community is, broadly, not a fan of AI coding. The general view I've run into is that LLMs don't write good Gleam: they write Elixir or Rust or Haskell with Gleam syntax, invent functions that don't exist, and produce something that compiles but isn't idiomatic.
And honestly? Out of the box, that's correct.
Gleam is young. There isn't much Gleam on the internet compared to Go or TypeScript, so models haven't seen much of it. The standard library has also changed a lot. My favourite example: agents kept writing `dict.update`. It doesn't exist. The function is `dict.upsert`.
### The skill
So I built [gleam-development](https://github.com/jinchuugames/gleam-development), a toolkit for agents writing Gleam:
- **A skill** describing how good Gleam is written: custom types over strings, `Result` over panics, no catch-all `case` arms on your own types, small pure modules, a store boundary instead of actors everywhere.
- **An API lookup** that reads the _installed_ package sources, at the pinned versions, so an agent checks a function exists before it uses it.
- **A doc-comment checker**, because agents love to write essays in comments.
- **A Go-to-Gleam guide** with the same service written in both, side by side.
- **A benchmark harness** with fixture tasks, so I could measure whether a change to the skill made agents better or worse instead of guessing.
- **An honest "where Gleam is weaker" doc.** It covers deployment, the ecosystem, and how the JavaScript target behaves differently from Erlang.
The core idea is a loop: look up the real API, compile, test, repair. Gleam's compiler is fast and its errors are good, so that loop is cheap. That's the part I think the sceptics underestimate. The first attempt is often wrong. The fifth attempt, with the compiler and the tests in the loop, is usually fine.
### Did it work?
Mostly, yes. I won't pretend every module is beautiful. Some of it is verbose, and some of it is Go wearing a Gleam costume. But it's _consistent_, it's tested, and the compiler holds a line that no amount of code review would.
I have to admit that the majority of my code these days is not my code at all. Ol' mate Claude does most of the typing. At the very least I'm insistent on controlling what files go into what folders which live where, because that is what bit me in the past. And of course the use of specific services, and optimisation shortcuts that an AI would balk at.
Anyway the theory held up. Immutable data and exhaustive matching make code that's easy for an agent to change without breaking something three files away. When it did break something, the compiler or a test usually said so within seconds.
### Where it hurt
- **Generated code drift.** If an agent hand-edits Squirrel's output instead of regenerating it, CI rightly fails. Same for the embedded tier settings.
- **Two targets, two sets of surprises.** Code that's fine on Erlang can be subtly wrong on JavaScript. The determinism tests caught most of it.
- **The frontend ecosystem is thin.** No drag-and-drop helpers, no animation helpers. That's partly why the islands exist.
- **Debugging is print-and-reason.** There's no step debugger.
## Would I do it again?
Yes. I was genuinely excited to finally use Gleam on a big project with a real architecture and about 700,000 lines of Gleam, not another toy. It held up.
To the Gleam folks who think AI and Gleam don't mix: I get it, and the default experience backs you up. But a strict language with a fast compiler is exactly what an agent needs. It just needs to be told what good looks like, and checked every step of the way.