State — the shared records and the activity log
settled decided — build to this page
The state/ package holds the engine’s records: a bot’s stamina, a
constellation’s shape, a supply bin’s contents, the player’s progression, the
game data read from files, and so on. Every other package — the stations,
progression, the scheduler — reads and writes these same records. It is the
lowest package of the engine’s tree (engine).
Records keep only their own checks. A record’s methods guard its own data (a ledger never goes negative, a star never exceeds its slots) and copy it. Every decision lives in the domain that owns it: a thread chooses a plan, the arena resolves combat, the shop pays a drawn pip. The records only store the results, so any package can use one without pulling in another’s logic.
One-way imports. A module in state/ imports no other engine package, and
every other package may import from it. That is one rule of the engine’s
tree, which lint-imports checks on every run of the suite; a record shared
by two domains is not a reason to move code here, since the tree places each
piece of code in the lowest domain that owns it.
The game data. Everything read from data/ and the story pack is read once
per location into one frozen record, GameData (state/game_data.py), which
every game started from it shares. Reading it refuses a reference to a name
no file declares, for every reference the record checks (GDT-002).
Saving and loading. Every stateful object follows the same rule: what it
saves, read back, rebuilds it. A play-state record is a dataclass in state/ that saves through one module-level pydantic.TypeAdapter of its type — dump_python(record, mode="json") writes the artifact and validate_python(saved, context=...) reads it back. An object that holds live
wiring beside its play-state (a bot, a stable, the player, the Power Station,
the arena, the whole game) saves through a record of that
play-state in state/saved.py and is built back from it: by its from_saved, or for the whole game by start_game(content, saved=…) (engine/start.py). The artifact
contains only owned play-state and configuration, as plain data that
serializes on its own. Shared context — the activity log, the built mods and
catalogs, and references into them — is not written into the artifact: a
reference is saved by name and resolved at load, through the load context’s
lookups (state/saving.py) or through the ambient arguments the object is
built with.
Copying. A play-state record is a slotted dataclass with its own copy(), which copies each field a copy may change and shares the rest; no
copy goes through copy.deepcopy or through a save and load. A copy holds the
same saved state as loading a fresh save with the same context (CP-002). An
object holding other copyable objects asks each for its own copy() and
shares the context it was handed — a bot’s thread catalog, supply bin and
progression (CP-003). A thread’s search copies the arena for every plan it
plays, so how records copy is the cost arena planning runs on.
That copy opens no turn of its own, and its watch neither applies nor trips
anything. A faithful copy of the arena carries the open turn and a live
watch, so it plays the bot’s remaining thread turns as the arena does
(CP-007). The shop and the player’s hand copy the same way (CP-008, CP-009).
Stations copied with one Copies record (state/copying.py) share a single
copy of each bin, hand, clock and body they share. The thread turns a copy
plays and the pips it charges call back into nothing of the game it was
taken from.
A view gated on loaded state — the researched-mod filter, the awakened-thread filter — is rebuilt against the state that was loaded, never left watching the one that was abandoned.
Saved names. A bot’s artifact carries its specialization as the option’s name alone, and the progression artifact carries the unlocked option names as a sorted list. What the name is worth — the stamina it adds, the magnitude its ability runs on — belongs to the chassis that declared it, and the start-up builds each chassis’s options with its numbers. A bot therefore comes back through its chassis’s record, which hands back the built option and refuses a save naming one its chassis does not offer. A rebuild that skipped the record would get a body wearing a name and nothing behind it, which is why the arena’s own load takes the bot sources too (SPC-018).
The whole-game save. Engine.saved() is the contract at its largest scale:
one artifact holding everything the game owns — player, power station,
serial position, the root of chance, the pending level offer with the hauls it
promises, the offer mode and zone schedule standings that composed it, the
story’s moment and the occasions it still owes, and the step count, which numbers every step across the whole
game. start_game(content, saved=...) builds the game from that record
against the Content the start-up built, the read-only half a different
process can supply. Two things never ride the artifact: the arena, because a
game saves only at the level-select boundary and refuses mid-battle (a resumed
game reopens at the boundary before the fight), and the activity log, which
narrates the game rather than holding it. Around the artifact the store adds what the deployment
knows: a build fingerprint, so another build’s save reads as absent instead of
being migrated, and an expiry, so the store and the browser forget a game
together (docs/design/player-sessions.md §5–6).
What a body wears between battles is state. Because the two save points — minting a game and selecting a level — both sit between fights, a bot’s position at those moments is what a save has to reproduce. So everything standing on a body rides its artifact, carried by value with its own applier serial and count, needing no live reference to be stated. One rule, no per-buff exception — a player who reloads at level select resumes where the one who played straight through stands.
No combat state leaves the level, which is what leaves so little standing to save. A body carried out of a station drops everything the station put on it: the guards, debuffs and stress that belonged to the fight, and equally the stance it holds and whether it is planted. A stance outlasts the fight it was taken in — that is what makes it a stance, and what lets one level’s battles chain off it — but it dies with the level, so the next one opens from a place a replay reproduces. Every exit takes this path, a recalled bot’s forfeit as much as a victory, and a shop stint as much as a level, because the shop runs the same mods on the body that the grid does. The Power Station’s meters, benefits and armed watches go the same way at the same moment, as does the aiming hold.
What survives is what the player put there: the kit, the threads, the research, the ledger — town state, assigned outside a level and untouched by one. So a kit edit between levels takes nothing off a body, and a body in town holds nothing for it to take.
What is not state is the arena’s per-combat scratch: the answer window, the foe motion a bot has observed, its blocked-hit ledgers. None of it is saved, and none of it needs to be, because no save ever observes it — a game inside a battle resumes from the boundary before that battle. In-combat fidelity is the copy path’s concern (the bot a plan’s board plays on), not the store’s.
Ownership and allegiance. A bot has two sides. Ownership is the stable
that printed the body: whose supply bin it draws from and its shop work
fills, whose power-station hand its colors charge, which roster a battle
counts it in. It is stamped once, saved, and nothing that happens in a fight
rewrites it. Allegiance is the side it fights on this instant: whom it
targets, whom it can be targeted by, whose team benefits and field effects
reach it, whose sight its concealment hides it from. For almost every body the
two are the same string; they part when Takes control of a stressed target in sight, relieving 1 stress.
SCRAMBLER adds 2 stress first.
A single publication seam keeps them apart. The arena reads allegiance once, when it builds a bot’s published facts, and everything downstream — the targetables, the board view’s rosters, the resolvers, a plan’s board — reads only what was published. So a seized body is an ally to its captor and a foe to the team it left, all the way through targeting, support and threat, and no consumer has to know charms exist. Ownership is read in the other direction entirely, by the parts of the game that hold property, and none of them ever sees allegiance. Two consequences are deliberate and worth stating: a charm cannot win a battle, because the bodies a battle counts are its roster and not its sides, and a seized spawner buds for its owner — against itself — because inheriting allegiance at birth would leave a permanent army behind a temporary effect.
The client is the one place that needs both at once, so the arena publishes each body’s live side beside the stable that owns it (ALG-007), and a seized body reads as its captor’s while the charm holds. It does not move: it keeps its tile, its place in the initiative order and the roster it was printed into. What changes is the side it fights for, and that is the whole of what a charm does. Only the arena publishes this — the artifact a game saves carries ownership alone, and a reloaded body derives its side from its buffs rather than trusting a stored one.
Chance belongs to a context. Every roll in the game descends from one seed the game owns. A context — a battle, a shop round, a level offer — opens its own stream from that root, and a stream’s seed is derived from the root and the context’s name rather than from the order the draws happened in. Two things follow. A seed means the same thing in every process, so a battle records the number it ran on and a developer can put that battle back in front of themselves instead of waiting for the bug to recur. And because a stream is a function of saved state — the root, plus the game’s position in its own sequence, both of which ride the artifact — restoring a save and re-entering a battle re-fights that battle rather than rerolling it.
The activity log. One object in state/ is the activity log. It records
events as they happen: an ordered stream that every system appends to, where
each event has a type, a payload, an optional actor, and a pass/level tag.
Consumers group the stream by (pass_name, level).
Each event carries one log entry, and each kind of entry is a class named
after its type (ARENA_PUSH is ArenaPush), declared beside the code that
writes it. Its fields are every key its writers set, and it refuses any other.
Together the kinds form one union keyed by type; just log-types writes that
union’s JSON Schema into @bbsb/activity and generates the admin UI’s types
from it.
The log is not authoritative game state. The state lives in the data classes, and where the log and the state disagree, the data classes are correct. The log has a fixed size, so the oldest events are dropped once it fills. The admin UI renders it and the tests check it.
The log is also how the game notices what happened. The occasion service
(engine/occasions.py) listens to the live game’s log and turns each event it
knows into an occasion as the event is written, before the code that emitted
it returns; scenes are owed from those occasions
(occasions.md § Occasions). It reads the log, never the other
way round, and a speculative copy writes to a log of its own, which nothing
listens to.
Derived specs — engine/bbsb_engine/test/state/state_spec.yaml (STA representation · LOD catalog-loading · SV save/load contract
· CP copy · RNG seeded randomness · ALG allegiance · INQ initiative queue · ITM items, decided in shop.md; its OVL family, the balance parameters, is governed by docs/design/balance-lab.md) and activity_log_spec.yaml (AL · ENT entries). State pins the contract and verifies it over
the objects a saved game is built from; systems that persist their own state
— the power station — verify the same contract in their own suites,
and test/session/session_spec.yaml’s SAV family is the whole-game claim
built on top of SV.