skip to content
Succeeded With Errors

Orrery 3: Three Contracts for a System-Agnostic VTT

/ 14 min read

Table of Contents

The battle map measures the distance between two squares. In Starfinder, diagonals cost five feet, then ten, then five again, so a path that goes three squares diagonally is twenty feet. In Draw Steel, the fantasy game I picked to prove the seams, every diagonal costs one square, full stop. I had a measure function on the game system contract, and the server could call it and get the right answer for whichever game was running.

The browser couldn’t. The client learns about the running game from one JSON payload at sign-in, and a function doesn’t survive the trip. The map in the browser had to draw a movement preview, and it had no way to ask the system how far anything was.

That was the moment I learned the one thing about contracts I’d want to pass on: there are rules you can send and rules you can only call, and you have to decide which side of the wire each one lives on before you write the type. The diagonal rule became a word (alternate, flat, or euclid) plus a shared measuring function that takes the word. Both ends call the same function with the same word and agree.

This is the third post in the Orrery series. The first one ended with a finding: the campaign app’s plumbing was generic in mechanism and closed in shape, with three threads tying every generic module to one game. This post is about the three contracts that cut those threads and turned a one-game campaign app into a system-agnostic virtual tabletop (VTT): what each contract owns, and the places where my first sketch of them was wrong.

What Each Contract Owns

The game system owns anything a rulebook would. A dice convention (d20 with a natural 20, or two d10 with tiered results). A vocabulary: what the currency is called, the abilities, the skills, the conditions, the creature sizes, and a hint for what a fair difficulty number looks like at a given level. The character sheet: schemas for a character’s build and state, a compute function that turns them into derived numbers, and pure functions for adding an item, removing one, or changing a resource pool. The combat model: which sides exist, how a character becomes a combatant, how initiative is rolled and turns are ordered, and what the health bands are. The map rules: the unit of measure, the diagonal rule, the kinds of move, cover, range, default vision. And the calendar.

The skin owns anything a brand guide would. The product name. What each app is called and its tagline. The device names (the phone is a Datapad in MercNet). About twenty nouns that show up in copy everywhere: currency, network, crew, job, shop, wiki. The palette, the fonts, whether there’s a narrator and who, the lines a new campaign starts with, the storage prefix.

The open registry is how a module declares itself. Instead of being hard-wired into five central lists, a module says which tables it owns, which entity kinds its writes announce, where its tables go in a snapshot, which clock hooks and new-session hooks it has, and what it contributes to the billboard and the Stream Deck. The kernel composes those at boot and refuses to start if a table has no owner or two modules claim the same one.

The rule tying them together: a skin carries no rules and a system carries no branding. A test enforces the boring half of that by grepping a denylist of setting names over every directory that will ship as a platform package.

Phase 1: Seams Without Moving a File

The first phase added the contracts as types, plus exactly one implementation: a Starfinder wrapper that delegated every function to the code that already existed. No logic moved. The character sheet’s compute on the contract called the old computeSheet. The map’s measure called the old feetBetween. Nothing at the table changed, and the proof of that was a parity test that ran a fixture character through both paths and asserted the numbers matched.

The rule I set for that test, and kept through every phase: the expected number comes from the old function, not from a constant I typed. If I write expect(pools.hp.max).toBe(42), I’ve tested that I can do arithmetic. If I write expect(pools.hp.max).toBe(computeSheet(build, state).hp.max), I’ve tested that the seam is transparent. The second one is the only one that catches a wrapper bug.

Three parts of my sketch were wrong, and the parity test is how I found two of them:

  • Changing a resource pool needed the build, not just the state. My sketch had applyPoolDelta(state, pool, delta). Starfinder’s damage order is temporary hit points, then stamina, then hit points, and the pool’s maximum bounds any change. Both of those live on the computed sheet, which needs the build. The signature grew an argument.
  • Initiative ties break on the bonus before the roll. A turn carries a value and a tiebreak. My sketch put a random number in the tiebreak. Starfinder breaks ties on the initiative bonus first, so the bonus went into the tiebreak’s whole part and the random number into its fraction.
  • A pairwise comparator can’t order turns for every game. I’d sketched sortTurns(a, b). Draw Steel doesn’t sort by initiative at all; sides alternate, starting with whoever won a toss. A comparator can’t express that. It became orderTurns(list), which takes the whole list and returns it in order, and Starfinder’s version just sorts.

Two more seams landed in this phase that I’d put off as “later” in the first post. The clock service that imported three modules by name became a hook bus: a module registers clockHooks with an order, the clock runs them all, and one failing hook never stops the others. And the skill union type, which reached into five generic modules, became a plain string validated at the route against whatever skills the running system lists. Old rows keep whatever id they stored.

One deviation worth noting: the palette. I’d planned to alias the brand color names (cosmo, sun, mint, coral) to neutral ones (brand, accent, positive, danger) and keep both valid for a while. The builder (the AI agent implementing that piece of the plan) removed the old names instead and renamed 945 class uses across 139 files in one codemod. It was right. An alias is a promise to come back, and I wasn’t going to.

Phase 2: The Direction Rule as a Test

Phase 2 moved files into packages: the kernel in three (platform-shared, platform-server, platform-web), the generic modules in one, the map engine in one, Starfinder in one, the MercNet skin in one, and my table’s private modules in one. The old shared package stayed as a shim that re-exports from the new ones, so every existing import kept compiling.

The rule for the packages: imports point downward only. The kernel never imports a module, the map, a system, a skin or a campaign. The map never imports a system. A system never imports a campaign. I’d planned to enforce this with a lint rule. What landed was a test that walks every source file, reads every import and export specifier, and fails with file:line: from -> to on any that points up.

The part of that test I’m fondest of is its allow-list. Every leak that couldn’t be cut yet got an entry with an until label naming the unit that would remove it. Each later unit’s definition of done included “your entries are gone.” When the map phase closed, the grep for its label returned zero. Four entries remain, all for the one schema file, all labeled for a later phase. The allow-list turned “we’ll fix that later” into a list with owners.

Two things stayed put against the plan. The database schema stayed one file, because the migration tool assumes one linear journal and splitting it would have meant merging journals at boot. And the server tests stayed in the server app rather than moving into each package, because they build the whole server anyway; per-package test roots would have bought nothing.

Phase 3: The Map

This was the risky one, and it’s the one the first post said was underway. It landed in one day, October 28, as eleven units, each a scoped pull request handed to its own builder. Here’s how it was shaped and what it decided.

The contract unit lands first, alone. Every field the ten real units would need went into one tiny PR with the Starfinder value and a test, so that ten builders working in parallel never edited the contract file at the same time. Two of them edited one shared file anyway, and the plan records it as an exception. The conflict map (the plan’s table of which unit touches which files) was mostly right.

Two lanes. The map chain ran sequentially (rules, then the extension registry, then token identity, then vitals, then the fight join) because each unit rewrote the same map files. Everything else ran alongside, two at a time.

No migration. Tokens and token views are JSON. Every new field was additive, every old name stayed readable for one release, and a back-fill runs at read time. A token saved before the change comes out of the reader with the new field on it, and the old writers still work.

Token identity: the ref is the entity; the fight link is the fight link. A token used to carry five optional ids (combatantId, characterId, mechId, petId, shipId). It now carries ref: { kind, id } saying what it stands for, and combatantId separately, because being in a fight is not the same as being a thing. The legacy ids are read first, in a fixed priority order, and the ref is written from them, so a writer that clears the old ids clears the identity, whatever ref the token carried. The one wrinkle: a companion animal’s token carried its handler’s character id, so a companion’s ref is never read as its handler.

Vitals: pools the system names. The token view’s hard-coded hp, hpMax, sp, spMax, tempHp became pools: ResourcePool[], and the health band is now a system question. The system lists its bands, best first, and exactly one of them carries a down flag. That flag turned out to reach further than the map’s ring colors: a down token is not an obstacle, and a fallen creature gives no soft cover. Both of those rules used to compare against the string 'down'. Now they ask the bands.

The fight join becomes a TokenSource. Half of the map store was neutral (fog, clamps, undo, who sees what) and half of it joined tokens to Starfinder’s combat tracker. The second half became an interface the system provides. The part I care about is the null implementation: a NullTokenSource that has no fight, ever. With it, a server with no fight tracker still has a map that lists, places, moves and fogs tokens. That’s what let the Draw Steel build ship a map without shipping a combat tracker.

The extension registry, and the bundle. The map’s tools, panels, layers and phone sheets used to be a static list of thirty-seven. They’re now registered by whoever owns them: the map package registers its thirty-three neutral ones and the Starfinder package its four. The first cut registered them as plain objects from the shell files, and the main JavaScript chunk went from 855 KB to 1,553 KB, an 81 percent jump, because every extension and everything it imported came along. The fix was to register loaders (() => import(...)) instead, and each screen shows a spinner until its chunk arrives. Main chunk afterwards: 859 KB.

Setting geography belongs to the skin. The list of regions (the union, near space, the vast) that the wiki’s place picker and the travel card use looked like a rule. It’s a list of names. Names are the skin’s. The travel rules per region stay with the system, and the two are joined by id.

Things that went wrong on the day: the billboard unit’s first CI run was red because the rendered image was cached for two seconds, and that cache outlived map changes that never announced themselves. The route became stateless. My PC crashed before the last builder could file its report; the branch was complete and pushed, so a reviewer picked it up from the pushed head. And every unit pinned fixtures from the trunk before it changed a line of code, which is the only reason I can say with a straight face that the map behaves identically.

The Proof

In the first post I wrote that Draw Steel didn’t exist yet and that the post would be wrong about some detail by the time it went out. It was wrong about that one by a day.

On October 29 a Draw Steel system package and a fantasy skin booted on the same kernel, the same modules and the same map. The system is mechanics only: five characteristics, the two-d10 power roll with its three tiers, Stamina with winded and dying bands, Recoveries, a square grid with flat diagonals, a made-up calendar. No ability text, no class features, no monsters, and a test that refuses any string literal over 120 characters in the package’s data files so that rules text can’t creep in. I want the package to be mechanics, not a copy of the book. The skin calls the messaging app Sending, the billboard a Scrying Glass, and the GM console a Chronicle. Gold instead of credits. No narrator: the skin plugs in a NullBrain that never speaks, and nothing upstream notices.

A deployment picks them with two environment variables. A Draw Steel boot on a fresh database creates no Starfinder rows. The power roll was checked against the shared function for every one of 12,600 edge-and-bane combinations. MercNet’s screens are pixel-identical to before. The end-to-end suite now runs 83 specs across both games.

What’s not done: there is no Draw Steel fight tracker, so its map is a map without fights, by design for now. The four schema leaks remain. And the Starfinder sheet still owns inventory in its own build JSON, exposed to the platform through compute; that was a deliberate call (Starfinder’s numbers depend on gear, so a platform-side inventory table couldn’t be the source of truth), and I’ll find out whether it holds when a third system shows up.

What I’d Take From This

Decide which side of the wire each rule lives on. Functions are for the server. Data is for both. If the browser needs the answer, the rule has to be expressible as data, and the sooner you know that the less you rewrite.

Parity numbers come from the old code. Not from your head, not from a constant. The seam is proven when the new path equals the old path on the same fixture, and that test is deleted the day the old path goes.

Keep both names for one release and back-fill at read. A migration is a cliff. A reader that understands both shapes is a ramp, and the ramp let eleven units land in a day without a single schema change.

The null implementation is the proof the seam is real. A NullBrain for a skin with no narrator. A NullTokenSource for a system with no fights. If you can’t write the null version, the thing you’re abstracting is still load-bearing in a way you haven’t named.

Land the contract first, alone. One small PR with the fields, the current values and a test, before anyone consumes a field. Parallel builders then never fight over the type file. Mostly.

Two games run on one map. Four leaks are labeled and waiting. The third system will tell me what I got wrong. Succeeded with errors.


Next in the series: zero-downtime deploys on a six-dollar droplet, and the day I built them for the wrong machine.