Add cellular owner checkpoint codec
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (checks) (push) Failing after 41s
Foundation / checks (ecs) (push) Successful in 1m55s
Foundation / checks (postgres) (push) Failing after 52s
Foundation / checks (tsan) (push) Failing after 31s
Foundation / checks (network) (push) Failing after 3m3s
Foundation / checks (client) (push) Failing after 4m26s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (checks) (push) Failing after 41s
Foundation / checks (ecs) (push) Successful in 1m55s
Foundation / checks (postgres) (push) Failing after 52s
Foundation / checks (tsan) (push) Failing after 31s
Foundation / checks (network) (push) Failing after 3m3s
Foundation / checks (client) (push) Failing after 4m26s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
This commit is contained in:
@@ -3,8 +3,9 @@
|
||||
## Status
|
||||
|
||||
Accepted M11.3 engineering direction (2026-10-10). This is an additive format
|
||||
contract; it does not change gameplay constants selected by ADR-0098. Profile
|
||||
implementation, SQL validation and recovery fault tests remain incomplete.
|
||||
contract; it does not change gameplay constants selected by ADR-0098. Snapshot
|
||||
and restore implementation is in progress; SQL validation, durable tick
|
||||
publication and recovery fault tests remain incomplete.
|
||||
|
||||
## Context
|
||||
|
||||
@@ -78,7 +79,7 @@ One verified object contains every biological owner affected by a tick, so the
|
||||
existing fenced head publication can make the habitat transfer, life result,
|
||||
corpse, heat and lineage visible together. The format is bounded and reuses
|
||||
existing genome, archive and habitat validators. Each batch rewrites up to
|
||||
8 MiB, and retaining corpses and lineage grows the payload; any later compaction
|
||||
16 MiB, and retaining corpses and lineage grows the payload; any later compaction
|
||||
requires a separate proof that historical identity and conservation remain
|
||||
closed.
|
||||
|
||||
|
||||
@@ -76,9 +76,9 @@ implementation and resolve the contract first.
|
||||
|
||||
ADR-0108/0109/0110 and `schemas/cellular-lives-v1.md` define an additive profile-4
|
||||
state closure with region-wide channel conservation and a realizable 16 MiB bound while preserving profile 3.
|
||||
This is a contract only: codec/restore, SQL allowlisting, a cellular region owner
|
||||
and crash-recovery evidence are still
|
||||
required before the first hands-on cellular probe.
|
||||
The in-memory cellular owner now has a bounded canonical snapshot/restore codec.
|
||||
SQL allowlisting, durable tick publication, session execution and crash-recovery
|
||||
evidence are still required before the first hands-on cellular probe.
|
||||
|
||||
## Delivery segments
|
||||
|
||||
@@ -157,7 +157,11 @@ scoped change before starting the next segment.
|
||||
encoding, durable publication and online session execution remain open.
|
||||
- 11.3d contract: ADR-0108/0109/0110 and `schemas/cellular-lives-v1.md` define a separate
|
||||
profile-4 checkpoint closure for lives, habitat, corpses, heat and lineage.
|
||||
No profile-4 code or durable tick publication is implemented yet.
|
||||
- 11.3d-1: `CellularLives::snapshot/restore` now canonically encodes that complete
|
||||
in-memory closure, verifies nested habitat and lineage, recomputes phenotype
|
||||
and inventory invariants, and rejects malformed, wrong-identity, wrong-tick,
|
||||
corrupt, unsupported and noncanonical state. Durable profile admission,
|
||||
publication and recovery remain open.
|
||||
|
||||
## Exit boundary
|
||||
|
||||
|
||||
@@ -6047,3 +6047,20 @@ idled without starting a compiler, so the focused target was compiled directly.
|
||||
`clang-format` and `clang-tidy` are unavailable on this Windows PATH. Linux
|
||||
sanitizer/CI, profile-4 codec/restore, durable publication and session wiring
|
||||
remain pending.
|
||||
|
||||
## 2026-10-10 — M11.3d-1 cellular snapshot and restore (CI pending)
|
||||
|
||||
Added the bounded profile-4 owner snapshot/restore codec and its wire schema.
|
||||
Restore verifies checkpoint identity and tick, outer and nested checksums,
|
||||
canonical bytes, archive/live/dead closure, genome-derived phenotype/body,
|
||||
habitat bounds, corpse retention and region-wide channel conservation. Tests
|
||||
round-trip a populated state, continue original and restored owners tick for
|
||||
tick through starvation, then restore the terminal corpse/heat/lineage state;
|
||||
they also refuse identity/tick mismatch, checksum damage, unsupported versions
|
||||
and an out-of-bounds position with a valid refreshed checksum.
|
||||
|
||||
Executed locally: warning-as-error MSVC 19.44 Debug and Release/NDEBUG direct
|
||||
compile/run; Debug CTest `cellular-lives-owner` (1/1 passed); and
|
||||
`git diff --check`. `clang-format` and `clang-tidy` are unavailable on the
|
||||
Windows PATH. PostgreSQL profile admission, publication/recovery tests, Linux
|
||||
sanitizers and CI remain pending; the codec is not yet durably playable.
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
namespace evo::server {
|
||||
inline constexpr std::uint32_t cellular_lives_format = 1;
|
||||
inline constexpr std::size_t max_cellular_lives_bytes = std::size_t{8} * 1024 * 1024;
|
||||
inline constexpr std::size_t max_cellular_lives_bytes = std::size_t{16} * 1024 * 1024;
|
||||
|
||||
struct CellularOwnedLife {
|
||||
LifeAccount account{};
|
||||
|
||||
@@ -1,61 +1,73 @@
|
||||
# Cellular lives checkpoint envelope v1
|
||||
# Cellular lives checkpoint v1
|
||||
|
||||
This schema is specified by [ADR-0108](../docs/adr/0108-cellular-checkpoint-profile.md).
|
||||
It is the complete payload for region checkpoint profile 4,
|
||||
`cellular-lives-v1`. Profile 3 remains the independent creation-only
|
||||
`primitive-lives-v1` format.
|
||||
Authority: ADR-0108 through ADR-0110. `server::CellularLives` in
|
||||
`src/server/lifecycle/cellular_lives.cpp` implements the canonical owner
|
||||
snapshot and restore codec. This is checkpoint profile 4; it does not alter
|
||||
the profile-3 primitive-life format. All integers are little-endian and records
|
||||
are packed by the codec, not native C++ layout.
|
||||
|
||||
## State closure
|
||||
## Envelope
|
||||
|
||||
The canonical little-endian envelope carries explicit lengths and versions for
|
||||
the region identity, 50 ms current tick, finite cellular area, provisioned
|
||||
habitat and region-wide opening totals, ordered living-cell records, ordered corpse
|
||||
records, region heat and the complete lineage archive. Integers have fixed
|
||||
widths; records are sorted by stable identity/address. Decoders reject unknown
|
||||
versions, nonzero reserved fields, duplicate or noncanonical records,
|
||||
overflow, trailing bytes, malformed checksums and any count or length above the
|
||||
caps before allocating from untrusted lengths.
|
||||
| Offset | Width | Field | Contract |
|
||||
|---:|---:|---|---|
|
||||
| 0 | 8 | magic | ASCII `AEVCLV01` |
|
||||
| 8 | 4 | format | 1 |
|
||||
| 12 | 4 | cellular rules | 1 |
|
||||
| 16 | 8 | world | Nonzero identity, must match restore request |
|
||||
| 24 | 8 | region | Nonzero identity, must match restore request |
|
||||
| 32 | 8 | frame | Nonzero identity, must match restore request |
|
||||
| 40 | 8 | tick | Committed 50 ms simulation tick; must match restore request |
|
||||
| 48 | 4 | width_um | Cellular area width |
|
||||
| 52 | 4 | height_um | Cellular area height |
|
||||
| 56 | 4 | ambient_temperature_millikelvin | Nonzero temperature used to derive budgets |
|
||||
| 60 | 4 | spawn_site_count | 1..256 |
|
||||
| 64 | 4 | living_count | 0..256 |
|
||||
| 68 | 4 | corpse_count | 0..65,536 |
|
||||
| 72 | 4 | habitat_bytes | Nested cellular habitat v1 length |
|
||||
| 76 | 4 | archive_bytes | Nested lineage archive v1 length |
|
||||
| 80 | 4 | reserved | Zero |
|
||||
| 84 | 8 | nursery_source | Nonzero source identity |
|
||||
| 92 | 40 | opening_total | Region-wide starting inventory: energy, carbon, nitrogen, phosphorus, other matter (u64 each) |
|
||||
| 132 | 40 | nursery_inventory | Current unspent nursery inventory, same channel order |
|
||||
| 172 | 40 | heat_inventory | Accumulated energy; all matter channels must be zero |
|
||||
| 212 | 8*n | spawn sites | Signed little-endian i32 x/y micrometre pairs |
|
||||
| … | 168*n | living records | Strictly ascending life identity |
|
||||
| … | 100*n | corpse records | Strictly ascending organism identity |
|
||||
| … | habitat_bytes | habitat | Canonical cellular habitat v1 snapshot |
|
||||
| … | archive_bytes | lineage | Canonical lineage archive v1 snapshot |
|
||||
| end−8 | 8 | checksum | FNV-1a64 over all preceding bytes |
|
||||
|
||||
The state is complete at one committed tick. It contains no pending or
|
||||
client-authored intentions. Durable action requests and outcomes remain in the
|
||||
fenced database batch records that publish the matching checkpoint.
|
||||
The exact total length is `220 + 8*spawn_site_count + 168*living_count +
|
||||
100*corpse_count + habitat_bytes + archive_bytes`. The complete envelope is
|
||||
limited to 16 MiB. The checksum detects accidental corruption; it is not an
|
||||
authentication mechanism.
|
||||
|
||||
Each living record retains player-account binding when present, life and
|
||||
organism identity, current genome identity, birth tick, growth state, position,
|
||||
body matter, reserve energy/matter and every timer required by the selected
|
||||
cellular rule version. Extent, phenotype and metabolic budgets are derived from
|
||||
the archived genome and growth state. Restore recomputes them; a stored cache
|
||||
must exactly match or the payload is refused.
|
||||
## Living record (168 bytes)
|
||||
|
||||
Each corpse retains the dead organism identity, death tick, final position and
|
||||
transferable body/reserve inventory. Heat is a separate energy-only region
|
||||
account. The habitat carries its finite patch stock and immutable source
|
||||
provenance. For every ledger channel, region-wide opening holdings must
|
||||
reconcile exactly to current habitat, nursery, living bodies and reserves,
|
||||
corpses and heat according to the declared transfers. Resource amounts do not
|
||||
retain per-source tags after they leave a patch (ADR-0109).
|
||||
Fields, in order: 16-byte account identity; six u64 values (`life`, `organism`,
|
||||
`genome`, `event`, `born_tick`, `source`); two u32 values (`realized_per_thousand`,
|
||||
`damage_per_thousand`); two u64 micrometre coordinates; then body and reserve
|
||||
inventories, each five u64 channels in the envelope order. Account identities
|
||||
must be unique. IDs and source/event references must be nonzero. The body is
|
||||
the genome-derived structural matter inventory and contains no energy.
|
||||
|
||||
Restore requires exact world/region/frame and end-tick agreement with the
|
||||
manifest, complete organism/genome/archive closure, unique player bindings,
|
||||
in-bounds footprints, valid energy/matter channels, and canonical byte-identical
|
||||
re-encoding. Genesis contains no living cells or corpses. Profile-4 clone genesis
|
||||
is unsupported until an identity-remapping contract exists.
|
||||
## Corpse record (100 bytes)
|
||||
|
||||
## Bounds
|
||||
Fields, in order: four u64 values (`life`, `organism`, `source`, `died_tick`);
|
||||
two u64 micrometre coordinates; two u32 growth values; u32 phenotype extent;
|
||||
and one five-channel inventory. The archive must contain the matching dead
|
||||
organism and death tick. Inventory must retain at least the genome-derived body
|
||||
matter; heat is not part of the corpse.
|
||||
|
||||
| Component | Maximum |
|
||||
|---|---:|
|
||||
| Total payload | 16 MiB (ADR-0110) |
|
||||
| Living cells | 256 |
|
||||
| Provisioned food patches | 4,096 |
|
||||
| Retained organisms and corpses | 65,536 |
|
||||
## Restore validation
|
||||
|
||||
Every nested format/version is pinned by profile 4's manifest version bundle.
|
||||
The manifest SHA-256 verifies the immutable object; nested checksums are for
|
||||
early corruption detection and canonical decoding, not authentication.
|
||||
|
||||
## Implementation status
|
||||
|
||||
This document records the approved closure direction only. No profile-4 codec,
|
||||
SQL admission or recovery implementation is claimed until the corresponding
|
||||
code and tests land.
|
||||
The decoder checks envelope bounds and checksum, identity/tick, versions,
|
||||
reserved fields, all counts and nested lengths, and exact total length before
|
||||
reserving record vectors. It then decodes the bounded habitat/archive records,
|
||||
recomputes phenotype and body from archived genomes, verifies positions and
|
||||
footprints, verifies exact living-set/archive closure and unique life/account
|
||||
identities, and reconciles every current channel (nursery, heat, habitat, lives,
|
||||
corpses) to the explicit opening total. It accepts only byte-identical
|
||||
canonical re-encoding. The codec is an in-memory profile contract: manifest
|
||||
allowlisting, PostgreSQL publication and process recovery are separate M11.3d
|
||||
gates and remain required before this profile is durably playable.
|
||||
|
||||
@@ -1,11 +1,92 @@
|
||||
#include "evo/server/lifecycle/cellular_lives.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <bit>
|
||||
#include <limits>
|
||||
#include <ranges>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::server {
|
||||
namespace {
|
||||
constexpr std::uint64_t cellular_lives_magic = 0x3130564C43564541ULL; // AEVCLV01
|
||||
constexpr std::uint32_t cellular_lives_rules = 1;
|
||||
constexpr std::size_t cellular_lives_header_bytes = 212;
|
||||
constexpr std::size_t cellular_site_bytes = 8;
|
||||
constexpr std::size_t cellular_life_bytes = 168;
|
||||
constexpr std::size_t cellular_corpse_bytes = 100;
|
||||
|
||||
std::uint64_t checksum(std::span<const std::uint8_t> bytes) noexcept {
|
||||
std::uint64_t value = 14695981039346656037ULL;
|
||||
for (const auto byte : bytes)
|
||||
value = (value ^ byte) * 1099511628211ULL;
|
||||
return value;
|
||||
}
|
||||
|
||||
class Writer {
|
||||
public:
|
||||
explicit Writer(std::vector<std::uint8_t>& bytes) : bytes_{bytes} {}
|
||||
void put(std::uint64_t value, unsigned width = 8) {
|
||||
for (unsigned i = 0; i < width; ++i, value >>= 8U)
|
||||
bytes_.push_back(static_cast<std::uint8_t>(value & 255U));
|
||||
}
|
||||
void inventory(const accounting::Inventory& value) {
|
||||
put(value.energy.value);
|
||||
for (const auto mass : value.matter)
|
||||
put(mass.value);
|
||||
}
|
||||
void site(SpawnSite value) {
|
||||
put(std::bit_cast<std::uint32_t>(value.x_um), 4);
|
||||
put(std::bit_cast<std::uint32_t>(value.y_um), 4);
|
||||
}
|
||||
template <std::size_t N> void bytes(const std::array<std::uint8_t, N>& value) {
|
||||
bytes_.insert(bytes_.end(), value.begin(), value.end());
|
||||
}
|
||||
void raw(std::span<const std::uint8_t> value) {
|
||||
bytes_.insert(bytes_.end(), value.begin(), value.end());
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::uint8_t>& bytes_;
|
||||
};
|
||||
|
||||
class Reader {
|
||||
public:
|
||||
explicit Reader(std::span<const std::uint8_t> bytes) : bytes_{bytes} {}
|
||||
std::uint64_t get(unsigned width = 8) {
|
||||
std::uint64_t value{};
|
||||
for (unsigned i = 0; i < width; ++i)
|
||||
value |= std::uint64_t{bytes_[at_++]} << (8U * i);
|
||||
return value;
|
||||
}
|
||||
accounting::Inventory inventory() {
|
||||
accounting::Inventory value{};
|
||||
value.energy.value = get();
|
||||
for (auto& mass : value.matter)
|
||||
mass.value = get();
|
||||
return value;
|
||||
}
|
||||
SpawnSite site() {
|
||||
return {std::bit_cast<std::int32_t>(static_cast<std::uint32_t>(get(4))),
|
||||
std::bit_cast<std::int32_t>(static_cast<std::uint32_t>(get(4)))};
|
||||
}
|
||||
template <std::size_t N> std::array<std::uint8_t, N> bytes() {
|
||||
std::array<std::uint8_t, N> result{};
|
||||
std::ranges::copy(bytes_.subspan(at_, N), result.begin());
|
||||
at_ += N;
|
||||
return result;
|
||||
}
|
||||
std::span<const std::uint8_t> raw(std::size_t size) {
|
||||
const auto result = bytes_.subspan(at_, size);
|
||||
at_ += size;
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
std::span<const std::uint8_t> bytes_;
|
||||
std::size_t at_{};
|
||||
};
|
||||
|
||||
bool add_inventory(accounting::Inventory& total, const accounting::Inventory& value) noexcept {
|
||||
constexpr auto maximum = std::numeric_limits<std::uint64_t>::max();
|
||||
if (value.energy.value > maximum - total.energy.value)
|
||||
@@ -48,6 +129,128 @@ sum_state(accounting::Inventory nursery, accounting::Inventory heat,
|
||||
return std::unexpected{CellularLivesError::overflow};
|
||||
return total;
|
||||
}
|
||||
|
||||
std::expected<accounting::Inventory, CellularLivesError>
|
||||
validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
|
||||
const CellularLivesConfig& config, const NurseryConfig& nursery_config,
|
||||
const biology::CellularHabitat& habitat,
|
||||
std::span<const CellularOwnedLife> lives,
|
||||
std::span<const CellularOwnedCorpse> corpses,
|
||||
const biology::LineageArchive& archive, accounting::Inventory nursery,
|
||||
accounting::Inventory heat, accounting::Inventory opening_total) {
|
||||
const auto& map = habitat.config();
|
||||
if (world == 0 || region == 0 || frame == 0 || nursery_config.world != world ||
|
||||
nursery_config.region != region || nursery_config.frame != frame ||
|
||||
nursery_config.source == 0 || nursery_config.funding != nursery ||
|
||||
map.world != world || map.region != region || map.frame != frame ||
|
||||
config.area.width_um == 0 || config.area.height_um == 0 ||
|
||||
map.width_um != config.area.width_um || map.height_um != config.area.height_um ||
|
||||
config.ambient_temperature_millikelvin == 0 ||
|
||||
nursery_config.sites.empty() || nursery_config.sites.size() > max_primitive_lives ||
|
||||
!std::ranges::is_sorted(nursery_config.sites) ||
|
||||
lives.size() > biology::max_cellular_tick_lives ||
|
||||
corpses.size() > biology::max_archived_organisms ||
|
||||
archive.recorded() != lives.size() + corpses.size())
|
||||
return std::unexpected{CellularLivesError::invalid_config};
|
||||
if (std::ranges::any_of(heat.matter,
|
||||
[](const auto& value) { return value.value != 0; }))
|
||||
return std::unexpected{CellularLivesError::conservation};
|
||||
|
||||
const auto founder = biology::primitive_founder(1, 1);
|
||||
if (!founder)
|
||||
return std::unexpected{CellularLivesError::biology};
|
||||
const auto founder_budget =
|
||||
biology::metabolic_budget(*founder, {1000, 0}, {config.ambient_temperature_millikelvin, {}});
|
||||
if (!founder_budget)
|
||||
return std::unexpected{CellularLivesError::biology};
|
||||
const auto spawn_extent = founder_budget->phenotype.extent;
|
||||
for (std::size_t i = 0; i < nursery_config.sites.size(); ++i) {
|
||||
const auto site = nursery_config.sites[i];
|
||||
if (site.x_um < 0 || site.y_um < 0 || spawn_extent > config.area.width_um ||
|
||||
spawn_extent > config.area.height_um ||
|
||||
static_cast<std::uint64_t>(site.x_um) > config.area.width_um - spawn_extent ||
|
||||
static_cast<std::uint64_t>(site.y_um) > config.area.height_um - spawn_extent)
|
||||
return std::unexpected{CellularLivesError::invalid_config};
|
||||
for (std::size_t j = 0; j < i; ++j) {
|
||||
const auto delta_x = std::int64_t{site.x_um} - nursery_config.sites[j].x_um;
|
||||
const auto delta_y = std::int64_t{site.y_um} - nursery_config.sites[j].y_um;
|
||||
const auto dx = static_cast<std::uint64_t>(delta_x < 0 ? -delta_x : delta_x);
|
||||
const auto dy = static_cast<std::uint64_t>(delta_y < 0 ? -delta_y : delta_y);
|
||||
if (dx < spawn_extent && dy < spawn_extent)
|
||||
return std::unexpected{CellularLivesError::invalid_config};
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::uint64_t> live_organisms;
|
||||
std::vector<std::uint64_t> life_ids;
|
||||
live_organisms.reserve(lives.size());
|
||||
life_ids.reserve(lives.size() + corpses.size());
|
||||
for (std::size_t i = 0; i < lives.size(); ++i) {
|
||||
const auto& life = lives[i];
|
||||
if (life.account == LifeAccount{} || life.identity.life == 0 ||
|
||||
life.identity.organism == 0 || life.identity.genome == 0 ||
|
||||
life.identity.event == 0 || life.identity.born.value == 0 ||
|
||||
life.identity.born.value > config.tick || life.source == 0 ||
|
||||
(i != 0 && lives[i - 1].identity.life >= life.identity.life))
|
||||
return std::unexpected{CellularLivesError::invalid_life};
|
||||
for (std::size_t j = 0; j < i; ++j)
|
||||
if (lives[j].account == life.account ||
|
||||
lives[j].identity.organism == life.identity.organism)
|
||||
return std::unexpected{CellularLivesError::duplicate_life};
|
||||
const auto record = archive.organism(life.identity.organism);
|
||||
if (!record || !record->alive || record->genome != life.identity.genome ||
|
||||
record->born != life.identity.born)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto budget = budget_for(archive, life, config.ambient_temperature_millikelvin);
|
||||
if (!budget || budget->body != life.body || life.body.energy.value != 0 ||
|
||||
budget->phenotype.extent == 0 ||
|
||||
budget->phenotype.extent > config.area.width_um ||
|
||||
budget->phenotype.extent > config.area.height_um ||
|
||||
life.position.x_um > config.area.width_um - budget->phenotype.extent ||
|
||||
life.position.y_um > config.area.height_um - budget->phenotype.extent)
|
||||
return std::unexpected{CellularLivesError::invalid_life};
|
||||
live_organisms.push_back(life.identity.organism);
|
||||
life_ids.push_back(life.identity.life);
|
||||
}
|
||||
std::ranges::sort(live_organisms);
|
||||
if (archive.living_organisms() != live_organisms)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
|
||||
for (std::size_t i = 0; i < corpses.size(); ++i) {
|
||||
const auto& corpse = corpses[i];
|
||||
if (corpse.life_id == 0 || corpse.organism == 0 || corpse.source == 0 ||
|
||||
corpse.died_tick == 0 || corpse.died_tick > config.tick || corpse.extent_um == 0 ||
|
||||
(i != 0 && corpses[i - 1].organism >= corpse.organism))
|
||||
return std::unexpected{CellularLivesError::invalid_life};
|
||||
const auto record = archive.organism(corpse.organism);
|
||||
if (!record || record->alive || record->died.value != corpse.died_tick)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto genome = archive.find(record->genome);
|
||||
if (!genome)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto budget = biology::metabolic_budget(
|
||||
*genome, corpse.growth, {config.ambient_temperature_millikelvin, {}});
|
||||
if (!budget || budget->phenotype.extent != corpse.extent_um ||
|
||||
corpse.extent_um > config.area.width_um || corpse.extent_um > config.area.height_um ||
|
||||
corpse.position.x_um > config.area.width_um - corpse.extent_um ||
|
||||
corpse.position.y_um > config.area.height_um - corpse.extent_um)
|
||||
return std::unexpected{CellularLivesError::invalid_life};
|
||||
for (std::size_t channel = 0; channel < corpse.inventory.matter.size(); ++channel)
|
||||
if (corpse.inventory.matter[channel].value < budget->body.matter[channel].value)
|
||||
return std::unexpected{CellularLivesError::conservation};
|
||||
life_ids.push_back(corpse.life_id);
|
||||
}
|
||||
std::ranges::sort(life_ids);
|
||||
if (std::ranges::adjacent_find(life_ids) != life_ids.end())
|
||||
return std::unexpected{CellularLivesError::duplicate_life};
|
||||
|
||||
const auto current = sum_state(nursery, heat, habitat, lives, corpses);
|
||||
if (!current)
|
||||
return std::unexpected{current.error()};
|
||||
if (*current != opening_total)
|
||||
return std::unexpected{CellularLivesError::conservation};
|
||||
return *current;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
struct CellularLives::State {
|
||||
@@ -64,7 +267,8 @@ struct CellularLives::State {
|
||||
accounting::Inventory current_total{};
|
||||
|
||||
State(std::uint64_t world_id, std::uint64_t region_id, std::uint64_t frame_id,
|
||||
CellularLivesConfig cfg, NurseryConfig nursery, std::unique_ptr<biology::CellularHabitat> map,
|
||||
CellularLivesConfig cfg, NurseryConfig nursery,
|
||||
std::unique_ptr<biology::CellularHabitat> map,
|
||||
std::vector<CellularOwnedLife> living, std::vector<CellularOwnedCorpse> dead,
|
||||
const biology::LineageArchive& history, accounting::Inventory source,
|
||||
accounting::Inventory accumulated_heat, accounting::Inventory initial,
|
||||
@@ -134,23 +338,26 @@ CellularLives::create(const PrimitiveLives& opening,
|
||||
life.position.y_um > config.area.height_um - budget->phenotype.extent)
|
||||
return std::unexpected{CellularLivesError::invalid_life};
|
||||
}
|
||||
auto initial_total = opening.totals();
|
||||
if (!add_inventory(initial_total, habitat->totals()))
|
||||
auto opening_total = opening.totals();
|
||||
if (!add_inventory(opening_total, habitat->totals()))
|
||||
return std::unexpected{CellularLivesError::overflow};
|
||||
const auto nursery = opening.balances()[0];
|
||||
nursery_config.funding = nursery;
|
||||
const auto heat = opening.balances()[1];
|
||||
const auto current = sum_state(nursery, heat, *habitat, lives, {});
|
||||
const auto current = validate_state(nursery_config.world, nursery_config.region,
|
||||
nursery_config.frame, config, nursery_config, *habitat,
|
||||
lives, {}, opening.archive(), nursery, heat, opening_total);
|
||||
if (!current)
|
||||
return std::unexpected{current.error()};
|
||||
if (*current != initial_total)
|
||||
return std::unexpected{CellularLivesError::conservation};
|
||||
|
||||
const auto world = nursery_config.world;
|
||||
const auto region = nursery_config.region;
|
||||
const auto frame = nursery_config.frame;
|
||||
auto state = std::make_unique<State>(
|
||||
nursery_config.world, nursery_config.region, nursery_config.frame, config,
|
||||
world, region, frame, config,
|
||||
std::move(nursery_config),
|
||||
std::move(habitat), std::move(lives), std::vector<CellularOwnedCorpse>{},
|
||||
opening.archive(), nursery, heat, initial_total, *current);
|
||||
opening.archive(), nursery, heat, opening_total, *current);
|
||||
return std::unique_ptr<CellularLives>{new CellularLives{std::move(state)}};
|
||||
}
|
||||
|
||||
@@ -174,6 +381,208 @@ accounting::Inventory CellularLives::current_total() const noexcept {
|
||||
return state_->current_total;
|
||||
}
|
||||
|
||||
std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
const auto habitat = state_->habitat->snapshot();
|
||||
const auto archive = state_->archive.encode();
|
||||
const auto site_count = state_->nursery_config.sites.size();
|
||||
const auto life_count = state_->lives.size();
|
||||
const auto corpse_count = state_->corpses.size();
|
||||
const auto total_bytes = cellular_lives_header_bytes + cellular_site_bytes * site_count +
|
||||
cellular_life_bytes * life_count +
|
||||
cellular_corpse_bytes * corpse_count + habitat.size() +
|
||||
archive.size() + 8;
|
||||
if (total_bytes > max_cellular_lives_bytes || habitat.size() > biology::max_cellular_habitat_bytes ||
|
||||
archive.size() > biology::max_archive_bytes || site_count > max_primitive_lives ||
|
||||
life_count > biology::max_cellular_tick_lives ||
|
||||
corpse_count > biology::max_archived_organisms)
|
||||
throw std::length_error{"cellular lives checkpoint exceeds its format bounds"};
|
||||
|
||||
std::vector<std::uint8_t> bytes;
|
||||
bytes.reserve(total_bytes);
|
||||
Writer out{bytes};
|
||||
out.put(cellular_lives_magic);
|
||||
out.put(cellular_lives_format, 4);
|
||||
out.put(cellular_lives_rules, 4);
|
||||
out.put(state_->world);
|
||||
out.put(state_->region);
|
||||
out.put(state_->frame);
|
||||
out.put(state_->config.tick);
|
||||
out.put(state_->config.area.width_um, 4);
|
||||
out.put(state_->config.area.height_um, 4);
|
||||
out.put(state_->config.ambient_temperature_millikelvin, 4);
|
||||
out.put(site_count, 4);
|
||||
out.put(life_count, 4);
|
||||
out.put(corpse_count, 4);
|
||||
out.put(habitat.size(), 4);
|
||||
out.put(archive.size(), 4);
|
||||
out.put(0, 4);
|
||||
out.put(state_->nursery_config.source);
|
||||
out.inventory(state_->opening_total);
|
||||
out.inventory(state_->nursery);
|
||||
out.inventory(state_->heat);
|
||||
for (const auto site : state_->nursery_config.sites)
|
||||
out.site(site);
|
||||
for (const auto& life : state_->lives) {
|
||||
out.bytes(life.account);
|
||||
out.put(life.identity.life);
|
||||
out.put(life.identity.organism);
|
||||
out.put(life.identity.genome);
|
||||
out.put(life.identity.event);
|
||||
out.put(life.identity.born.value);
|
||||
out.put(life.source);
|
||||
out.put(life.growth.realized_per_thousand, 4);
|
||||
out.put(life.growth.damage_per_thousand, 4);
|
||||
out.put(life.position.x_um);
|
||||
out.put(life.position.y_um);
|
||||
out.inventory(life.body);
|
||||
out.inventory(life.reserve);
|
||||
}
|
||||
for (const auto& corpse : state_->corpses) {
|
||||
out.put(corpse.life_id);
|
||||
out.put(corpse.organism);
|
||||
out.put(corpse.source);
|
||||
out.put(corpse.died_tick);
|
||||
out.put(corpse.position.x_um);
|
||||
out.put(corpse.position.y_um);
|
||||
out.put(corpse.growth.realized_per_thousand, 4);
|
||||
out.put(corpse.growth.damage_per_thousand, 4);
|
||||
out.put(corpse.extent_um, 4);
|
||||
out.inventory(corpse.inventory);
|
||||
}
|
||||
out.raw(habitat);
|
||||
out.raw(archive);
|
||||
if (bytes.size() + 8 != total_bytes)
|
||||
throw std::logic_error{"cellular lives checkpoint size calculation mismatch"};
|
||||
out.put(checksum(bytes));
|
||||
return bytes;
|
||||
}
|
||||
|
||||
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
|
||||
CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
std::uint64_t region, std::uint64_t frame, std::uint64_t tick) {
|
||||
if (bytes.size() < cellular_lives_header_bytes + 8 || bytes.size() > max_cellular_lives_bytes)
|
||||
return std::unexpected{CellularLivesError::format};
|
||||
Reader tail{bytes.last(8)};
|
||||
if (checksum(bytes.first(bytes.size() - 8)) != tail.get())
|
||||
return std::unexpected{CellularLivesError::checksum};
|
||||
|
||||
Reader in{bytes};
|
||||
if (in.get() != cellular_lives_magic)
|
||||
return std::unexpected{CellularLivesError::format};
|
||||
if (in.get(4) != cellular_lives_format || in.get(4) != cellular_lives_rules)
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
const auto saved_world = in.get();
|
||||
const auto saved_region = in.get();
|
||||
const auto saved_frame = in.get();
|
||||
const auto saved_tick = in.get();
|
||||
if (saved_world != world || saved_region != region || saved_frame != frame)
|
||||
return std::unexpected{CellularLivesError::identity};
|
||||
if (saved_tick != tick)
|
||||
return std::unexpected{CellularLivesError::invalid_tick};
|
||||
|
||||
CellularLivesConfig config;
|
||||
config.area.width_um = static_cast<std::uint32_t>(in.get(4));
|
||||
config.area.height_um = static_cast<std::uint32_t>(in.get(4));
|
||||
config.tick = saved_tick;
|
||||
config.ambient_temperature_millikelvin = static_cast<std::uint32_t>(in.get(4));
|
||||
const auto site_count = in.get(4);
|
||||
const auto life_count = in.get(4);
|
||||
const auto corpse_count = in.get(4);
|
||||
const auto habitat_size = in.get(4);
|
||||
const auto archive_size = in.get(4);
|
||||
if (in.get(4) != 0 || site_count == 0 || site_count > max_primitive_lives ||
|
||||
life_count > biology::max_cellular_tick_lives ||
|
||||
corpse_count > biology::max_archived_organisms ||
|
||||
habitat_size > biology::max_cellular_habitat_bytes ||
|
||||
archive_size > biology::max_archive_bytes)
|
||||
return std::unexpected{CellularLivesError::format};
|
||||
const auto source = in.get();
|
||||
const auto opening_total = in.inventory();
|
||||
const auto nursery = in.inventory();
|
||||
const auto heat = in.inventory();
|
||||
const auto expected_size = cellular_lives_header_bytes + cellular_site_bytes * site_count +
|
||||
cellular_life_bytes * life_count +
|
||||
cellular_corpse_bytes * corpse_count + habitat_size + archive_size +
|
||||
8;
|
||||
if (source == 0 || expected_size != bytes.size())
|
||||
return std::unexpected{CellularLivesError::format};
|
||||
|
||||
NurseryConfig nursery_config{saved_world, saved_region, saved_frame, source, nursery, {}};
|
||||
nursery_config.sites.reserve(static_cast<std::size_t>(site_count));
|
||||
for (std::uint64_t i = 0; i < site_count; ++i)
|
||||
nursery_config.sites.push_back(in.site());
|
||||
|
||||
std::vector<CellularOwnedLife> lives;
|
||||
lives.reserve(static_cast<std::size_t>(life_count));
|
||||
for (std::uint64_t i = 0; i < life_count; ++i) {
|
||||
CellularOwnedLife life{};
|
||||
life.account = in.bytes<16>();
|
||||
life.identity.life = in.get();
|
||||
life.identity.organism = in.get();
|
||||
life.identity.genome = in.get();
|
||||
life.identity.event = in.get();
|
||||
life.identity.born.value = in.get();
|
||||
life.source = in.get();
|
||||
life.growth.realized_per_thousand = static_cast<std::uint32_t>(in.get(4));
|
||||
life.growth.damage_per_thousand = static_cast<std::uint32_t>(in.get(4));
|
||||
life.position = {in.get(), in.get()};
|
||||
life.body = in.inventory();
|
||||
life.reserve = in.inventory();
|
||||
lives.push_back(life);
|
||||
}
|
||||
|
||||
std::vector<CellularOwnedCorpse> corpses;
|
||||
corpses.reserve(static_cast<std::size_t>(corpse_count));
|
||||
for (std::uint64_t i = 0; i < corpse_count; ++i) {
|
||||
CellularOwnedCorpse corpse{};
|
||||
corpse.life_id = in.get();
|
||||
corpse.organism = in.get();
|
||||
corpse.source = in.get();
|
||||
corpse.died_tick = in.get();
|
||||
corpse.position = {in.get(), in.get()};
|
||||
corpse.growth.realized_per_thousand = static_cast<std::uint32_t>(in.get(4));
|
||||
corpse.growth.damage_per_thousand = static_cast<std::uint32_t>(in.get(4));
|
||||
corpse.extent_um = static_cast<std::uint32_t>(in.get(4));
|
||||
corpse.inventory = in.inventory();
|
||||
corpses.push_back(corpse);
|
||||
}
|
||||
const auto habitat_payload = in.raw(static_cast<std::size_t>(habitat_size));
|
||||
const auto archive_payload = in.raw(static_cast<std::size_t>(archive_size));
|
||||
|
||||
auto habitat = biology::CellularHabitat::restore(habitat_payload, saved_world, saved_region,
|
||||
saved_frame);
|
||||
if (!habitat) {
|
||||
if (habitat.error() == biology::CellularHabitatError::version)
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
if (habitat.error() == biology::CellularHabitatError::checksum)
|
||||
return std::unexpected{CellularLivesError::checksum};
|
||||
return std::unexpected{CellularLivesError::habitat};
|
||||
}
|
||||
auto archive = biology::LineageArchive::decode(archive_payload);
|
||||
if (!archive) {
|
||||
if (archive.error() == biology::ArchiveCodecError::version)
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
if (archive.error() == biology::ArchiveCodecError::checksum)
|
||||
return std::unexpected{CellularLivesError::checksum};
|
||||
if (archive.error() == biology::ArchiveCodecError::noncanonical)
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
}
|
||||
const auto current = validate_state(saved_world, saved_region, saved_frame, config,
|
||||
nursery_config, **habitat, lives, corpses, *archive,
|
||||
nursery, heat, opening_total);
|
||||
if (!current)
|
||||
return std::unexpected{current.error()};
|
||||
|
||||
auto restored = std::unique_ptr<CellularLives>{new CellularLives{std::make_unique<State>(
|
||||
saved_world, saved_region, saved_frame, config, std::move(nursery_config),
|
||||
std::move(*habitat), std::move(lives), std::move(corpses), *archive, nursery, heat,
|
||||
opening_total, *current)}};
|
||||
if (!std::ranges::equal(bytes, restored->snapshot()))
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
return restored;
|
||||
}
|
||||
|
||||
std::expected<biology::CellularRegionStepCandidate, CellularLivesError>
|
||||
CellularLives::advance_tick(std::span<const CellularLifeIntent> intents) {
|
||||
if (state_->config.tick == std::numeric_limits<std::uint64_t>::max())
|
||||
@@ -261,12 +670,14 @@ CellularLives::advance_tick(std::span<const CellularLifeIntent> intents) {
|
||||
auto next_habitat = biology::CellularHabitat::create(std::move(habitat_config));
|
||||
if (!next_habitat)
|
||||
return std::unexpected{CellularLivesError::habitat};
|
||||
const auto total = sum_state(state_->nursery, next_heat, **next_habitat, next_lives,
|
||||
next_corpses);
|
||||
const auto total = validate_state(
|
||||
state_->world, state_->region, state_->frame,
|
||||
CellularLivesConfig{state_->config.area, proposal->end_tick,
|
||||
state_->config.ambient_temperature_millikelvin},
|
||||
state_->nursery_config, **next_habitat, next_lives, next_corpses, next_archive,
|
||||
state_->nursery, next_heat, state_->opening_total);
|
||||
if (!total)
|
||||
return std::unexpected{total.error()};
|
||||
if (*total != state_->opening_total)
|
||||
return std::unexpected{CellularLivesError::conservation};
|
||||
|
||||
auto next = std::make_unique<State>(
|
||||
state_->world, state_->region, state_->frame,
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
#include "evo/server/lifecycle/cellular_lives.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <span>
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
using namespace evo::server;
|
||||
@@ -27,6 +31,19 @@ Inventory founder_price(std::uint64_t count) {
|
||||
return funding;
|
||||
}
|
||||
|
||||
std::uint64_t checksum(std::span<const std::uint8_t> bytes) {
|
||||
std::uint64_t value = 14695981039346656037ULL;
|
||||
for (const auto byte : bytes)
|
||||
value = (value ^ byte) * 1099511628211ULL;
|
||||
return value;
|
||||
}
|
||||
|
||||
void refresh_checksum(std::vector<std::uint8_t>& bytes) {
|
||||
const auto value = checksum(std::span{bytes}.first(bytes.size() - 8));
|
||||
for (unsigned i = 0; i < 8; ++i)
|
||||
bytes[bytes.size() - 8 + i] = static_cast<std::uint8_t>(value >> (i * 8U));
|
||||
}
|
||||
|
||||
void owner_advances_and_closes_death_atomically() {
|
||||
auto primitive = PrimitiveLives::create({90, 1, 7, 17, founder_price(2), {{0, 0}, {20, 0}}});
|
||||
CHECK(primitive);
|
||||
@@ -71,6 +88,27 @@ void owner_advances_and_closes_death_atomically() {
|
||||
CHECK(owner.habitat().patches()[0].stock == (Inventory{{0}, {}}));
|
||||
CHECK(owner.current_total() == owner.opening_total());
|
||||
|
||||
const auto saved = owner.snapshot();
|
||||
CHECK(saved.size() <= max_cellular_lives_bytes);
|
||||
auto recovered = CellularLives::restore(saved, 90, 1, 7, owner.tick());
|
||||
CHECK(recovered && (*recovered)->snapshot() == saved);
|
||||
CHECK(!CellularLives::restore(saved, 91, 1, 7, owner.tick()));
|
||||
CHECK(!CellularLives::restore(saved, 90, 1, 7, owner.tick() + 1));
|
||||
auto damaged = saved;
|
||||
damaged[100] ^= 1;
|
||||
const auto damaged_result = CellularLives::restore(damaged, 90, 1, 7, owner.tick());
|
||||
CHECK(!damaged_result && damaged_result.error() == CellularLivesError::checksum);
|
||||
auto unsupported = saved;
|
||||
unsupported[8] = static_cast<std::uint8_t>(cellular_lives_format + 1);
|
||||
refresh_checksum(unsupported);
|
||||
const auto unsupported_result =
|
||||
CellularLives::restore(unsupported, 90, 1, 7, owner.tick());
|
||||
CHECK(!unsupported_result && unsupported_result.error() == CellularLivesError::version);
|
||||
auto outside = saved;
|
||||
std::fill(outside.begin() + 300, outside.begin() + 308, uint8_t{0xff});
|
||||
refresh_checksum(outside);
|
||||
CHECK(!CellularLives::restore(outside, 90, 1, 7, owner.tick()));
|
||||
|
||||
std::size_t steps{};
|
||||
while (!owner.lives().empty() && steps < 2'000) {
|
||||
std::vector<CellularLifeIntent> idle;
|
||||
@@ -78,7 +116,10 @@ void owner_advances_and_closes_death_atomically() {
|
||||
for (const auto& life : owner.lives())
|
||||
idle.push_back({life.identity.life, {}});
|
||||
const auto advanced = owner.advance_tick(idle);
|
||||
CHECK(advanced && owner.current_total() == owner.opening_total());
|
||||
const auto replayed = (*recovered)->advance_tick(idle);
|
||||
CHECK(advanced && replayed && *advanced == *replayed);
|
||||
CHECK(owner.snapshot() == (*recovered)->snapshot());
|
||||
CHECK(owner.current_total() == owner.opening_total());
|
||||
++steps;
|
||||
}
|
||||
CHECK(owner.lives().empty() && owner.corpses().size() == 2 && steps < 2'000);
|
||||
@@ -94,6 +135,13 @@ void owner_advances_and_closes_death_atomically() {
|
||||
second_lineage->died.value == owner.corpses()[1].died_tick);
|
||||
CHECK(owner.heat().energy.value != 0);
|
||||
CHECK(owner.current_total() == owner.opening_total());
|
||||
const auto terminal_snapshot = owner.snapshot();
|
||||
auto terminal_recovery = CellularLives::restore(terminal_snapshot, 90, 1, 7, owner.tick());
|
||||
CHECK(terminal_recovery && (*terminal_recovery)->snapshot() == terminal_snapshot);
|
||||
CHECK((*terminal_recovery)->lives().empty() &&
|
||||
std::ranges::equal((*terminal_recovery)->corpses(), owner.corpses()));
|
||||
CHECK((*terminal_recovery)->heat() == owner.heat());
|
||||
CHECK((*terminal_recovery)->archive().organism(101) == owner.archive().organism(101));
|
||||
}
|
||||
} // namespace
|
||||
|
||||
|
||||
Reference in New Issue
Block a user