M12.4: preserve exact biology across ownership candidates
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m18s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 5m33s
Foundation / checks (postgres) (push) Canceled after 5m29s
Foundation / checks (tsan) (push) Canceled after 5m28s
Foundation / checks (client) (push) Canceled after 5m37s
Foundation / checks (network) (push) Canceled after 5m39s

This commit is contained in:
dingo
2026-10-17 18:17:59 -04:00
parent 0a8962b57e
commit decd4f8043
27 changed files with 1408 additions and 61 deletions
+175 -46
View File
@@ -18,6 +18,7 @@ constexpr std::uint64_t cellular_lives_v3_magic = 0x3330564C43564541ULL; // AEVC
constexpr std::uint64_t cellular_lives_v4_magic = 0x3430564C43564541ULL; // AEVCLV04
constexpr std::uint64_t cellular_lives_v5_magic = 0x3530564C43564541ULL; // AEVCLV05
constexpr std::uint64_t cellular_lives_v6_magic = 0x3630564C43564541ULL; // AEVCLV06
constexpr std::uint64_t cellular_lives_v7_magic = 0x3730564C43564541ULL; // AEVCLV07
constexpr std::size_t cellular_lives_header_bytes = 212;
constexpr std::size_t cellular_site_bytes = 8;
constexpr std::size_t cellular_life_v1_bytes = 168;
@@ -32,6 +33,8 @@ constexpr std::size_t cellular_life_v5_bytes = cellular_life_v2_bytes + 8;
constexpr std::size_t cellular_corpse_v5_bytes = cellular_corpse_v4_bytes + 8;
// Format 6 (ADR-0128): each life adds presence (1), 7 zero bytes and its freeze tick (8).
constexpr std::size_t cellular_life_v6_bytes = cellular_life_v5_bytes + 16;
// Format 7: two boundary-flow inventories and nonresident count/reserved.
constexpr std::size_t cellular_lives_v7_header_bytes = cellular_lives_v5_header_bytes + 88;
std::uint64_t checksum(std::span<const std::uint8_t> bytes) noexcept {
std::uint64_t value = 14695981039346656037ULL;
@@ -281,7 +284,7 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
accounting::Inventory nursery, accounting::Inventory heat,
accounting::Inventory opening_total, bool detritus_rules = false,
bool bud_rules = false, std::uint64_t next_bud_sequence = 1,
bool presence_rules = false) {
bool presence_rules = false, const CellularBoundaryLedger* boundary = nullptr) {
// A bud's identity lies in this region's namespace below the next sequence; anything
// else is an allocated identity, below the namespace (ADR-0125).
const auto bud_identity = [&](std::uint64_t identity) noexcept {
@@ -292,7 +295,11 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
const auto lineage_valid = [&](std::uint64_t life_id, std::uint64_t parent) noexcept {
if (parent == 0) // Rules 4 keep allocated identities below the bud namespace.
return !bud_rules || life_id < cellular_bud_namespace;
return bud_identity(life_id) && parent != life_id;
const auto origin = (life_id >> 32U) & cellular_bud_max_region;
const bool imported = boundary && life_id >= cellular_bud_namespace && origin != region &&
origin > 0 && origin <= cellular_bud_max_region &&
(life_id & 0xFFFF'FFFFULL) != 0;
return (bud_identity(life_id) || imported) && parent != life_id;
};
const auto& map = habitat.config();
if (world == 0 || region == 0 || frame == 0 || nursery_config.world != world ||
@@ -305,7 +312,8 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
!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())
archive.recorded() !=
lives.size() + corpses.size() + (boundary ? boundary->nonresident_organisms.size() : 0))
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};
@@ -338,7 +346,8 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
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());
life_ids.reserve(lives.size() + corpses.size() +
(boundary ? boundary->nonresident_organisms.size() : 0));
for (std::size_t i = 0; i < lives.size(); ++i) {
const auto& life = lives[i];
// Founders are owned; buds are unowned wildlife (rules 4) and never earn offers.
@@ -382,6 +391,24 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
live_organisms.push_back(life.identity.organism);
life_ids.push_back(life.identity.life);
}
if (boundary) {
if (boundary->nonresident_organisms.size() > biology::max_archived_organisms ||
!std::ranges::is_sorted(boundary->nonresident_organisms) ||
std::ranges::adjacent_find(boundary->nonresident_organisms) !=
boundary->nonresident_organisms.end())
return std::unexpected(CellularLivesError::invalid_life);
for (const auto id : boundary->nonresident_organisms) {
if (id < cellular_bud_namespace || !lineage_valid(id, 1))
return std::unexpected(CellularLivesError::invalid_life);
const auto record = archive.organism(id);
if (!record || record->born.value > config.tick ||
(!record->alive && record->died.value > config.tick))
return std::unexpected(CellularLivesError::archive);
if (record->alive)
live_organisms.push_back(id);
life_ids.push_back(id);
}
}
std::ranges::sort(live_organisms);
if (archive.living_organisms() != live_organisms)
return std::unexpected{CellularLivesError::archive};
@@ -427,7 +454,26 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
const auto current = sum_state(nursery, heat, habitat, lives, corpses);
if (!current)
return std::unexpected{current.error()};
if (*current != opening_total)
auto expected = opening_total;
if (boundary) {
accounting::Inventory credit{}, debit{};
const auto difference = [](std::uint64_t in, std::uint64_t out, std::uint64_t& plus,
std::uint64_t& minus) {
if (in >= out)
plus = in - out;
else
minus = out - in;
};
difference(boundary->incoming.energy.value, boundary->outgoing.energy.value,
credit.energy.value, debit.energy.value);
for (std::size_t channel = 0; channel < credit.matter.size(); ++channel)
difference(boundary->incoming.matter[channel].value,
boundary->outgoing.matter[channel].value, credit.matter[channel].value,
debit.matter[channel].value);
if (!add_inventory(expected, credit) || !subtract_inventory(expected, debit))
return std::unexpected(CellularLivesError::conservation);
}
if (*current != expected)
return std::unexpected{CellularLivesError::conservation};
return *current;
}
@@ -447,6 +493,7 @@ struct CellularLives::State {
accounting::Inventory opening_total{};
accounting::Inventory current_total{};
std::uint64_t next_bud_sequence{1}; // Format 5: the next bud identity's sequence.
CellularBoundaryLedger boundary{};
State(std::uint64_t world_id, std::uint64_t region_id, std::uint64_t frame_id,
CellularLivesConfig cfg, NurseryConfig nursery_definition,
@@ -454,12 +501,13 @@ struct CellularLives::State {
std::vector<CellularOwnedCorpse> dead, const biology::LineageArchive& history,
accounting::Inventory source, accounting::Inventory accumulated_heat,
accounting::Inventory initial, accounting::Inventory current,
std::uint32_t format = cellular_lives_format, std::uint32_t rules = cellular_lives_rules)
std::uint32_t format = cellular_lives_format, std::uint32_t rules = cellular_lives_rules,
CellularBoundaryLedger boundary_ledger = {})
: world{world_id}, region{region_id}, frame{frame_id}, format_version{format},
rules_version{rules}, config{cfg}, nursery_config{std::move(nursery_definition)},
habitat{std::move(map)}, lives{std::move(living)}, corpses{std::move(dead)},
archive{history}, nursery{source}, heat{accumulated_heat}, opening_total{initial},
current_total{current} {}
current_total{current}, boundary{std::move(boundary_ledger)} {}
};
CellularLives::CellularLives(std::unique_ptr<State> state) : state_{std::move(state)} {}
@@ -623,6 +671,46 @@ accounting::Inventory CellularLives::opening_total() const noexcept {
accounting::Inventory CellularLives::current_total() const noexcept {
return state_->current_total;
}
const CellularBoundaryLedger& CellularLives::boundary_ledger() const noexcept {
return state_->boundary;
}
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
CellularLives::migration_replacement(std::vector<CellularOwnedLife> living,
const biology::LineageArchive& archive,
CellularBoundaryLedger boundary) const {
if (state_->rules_version != cellular_lives_foraging_rules ||
(state_->format_version != cellular_lives_presence_format &&
state_->format_version != cellular_lives_migration_format))
return std::unexpected(CellularLivesError::version);
auto habitat = biology::CellularHabitat::restore(state_->habitat->snapshot(), state_->world,
state_->region, state_->frame);
if (!habitat)
return std::unexpected(CellularLivesError::habitat);
const auto total = validate_state(state_->world, state_->region, state_->frame, state_->config,
state_->nursery_config, **habitat, living, state_->corpses,
archive, state_->nursery, state_->heat, state_->opening_total,
true, true, state_->next_bud_sequence, true, &boundary);
if (!total)
return std::unexpected(total.error());
auto replacement = std::unique_ptr<CellularLives>{new CellularLives{std::make_unique<State>(
state_->world, state_->region, state_->frame, state_->config, state_->nursery_config,
std::move(*habitat), std::move(living), state_->corpses, archive, state_->nursery,
state_->heat, state_->opening_total, *total, cellular_lives_migration_format,
state_->rules_version, std::move(boundary))}};
replacement->state_->next_bud_sequence = state_->next_bud_sequence;
try {
[[maybe_unused]] const auto bytes = replacement->snapshot();
} catch (const std::length_error&) {
return std::unexpected(CellularLivesError::overflow);
}
return replacement;
}
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
CellularLives::migration_upgrade_candidate() const {
return migration_replacement(state_->lives, state_->archive, state_->boundary);
}
std::span<const CellularLifeTickFeedback> CellularLives::last_tick_feedback() const noexcept {
return last_tick_feedback_;
}
@@ -644,16 +732,20 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
const auto corpse_bytes = parents ? cellular_corpse_v5_bytes
: causes ? cellular_corpse_v4_bytes
: cellular_corpse_bytes;
const auto header_bytes =
parents ? cellular_lives_v5_header_bytes : cellular_lives_header_bytes;
const bool migration = state_->format_version == cellular_lives_migration_format;
const auto header_bytes = migration ? cellular_lives_v7_header_bytes
: parents ? cellular_lives_v5_header_bytes
: cellular_lives_header_bytes;
const auto external_count = migration ? state_->boundary.nonresident_organisms.size() : 0;
const auto total_bytes = header_bytes + cellular_site_bytes * site_count +
life_bytes * life_count + corpse_bytes * corpse_count +
habitat.size() + archive.size() + 8;
habitat.size() + archive.size() + external_count * 8 + 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)
corpse_count > biology::max_archived_organisms ||
external_count > biology::max_archived_organisms)
throw std::length_error{"cellular lives checkpoint exceeds its format bounds"};
std::vector<std::uint8_t> bytes;
@@ -665,6 +757,7 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
: state_->format_version == cellular_lives_engulf_format ? cellular_lives_v4_magic
: state_->format_version == cellular_lives_bud_format ? cellular_lives_v5_magic
: state_->format_version == cellular_lives_presence_format ? cellular_lives_v6_magic
: migration ? cellular_lives_v7_magic
: cellular_lives_v2_magic;
out.put(magic);
out.put(state_->format_version, 4);
@@ -688,6 +781,12 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
out.inventory(state_->heat);
if (parents)
out.put(state_->next_bud_sequence);
if (migration) {
out.inventory(state_->boundary.incoming);
out.inventory(state_->boundary.outgoing);
out.put(external_count, 4);
out.put(0, 4);
}
for (const auto site : state_->nursery_config.sites)
out.site(site);
for (const auto& life : state_->lives) {
@@ -768,6 +867,9 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
}
out.raw(habitat);
out.raw(archive);
if (migration)
for (const auto id : state_->boundary.nonresident_organisms)
out.put(id);
if (bytes.size() + 8 != total_bytes)
throw std::logic_error{"cellular lives checkpoint size calculation mismatch"};
out.put(checksum(bytes));
@@ -948,19 +1050,21 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
state_->region, state_->frame);
if (!habitat)
return std::unexpected{CellularLivesError::habitat};
const auto current =
validate_state(state_->world, state_->region, state_->frame, state_->config, nursery_config,
**habitat, lives, state_->corpses, archive, nursery, heat,
state_->opening_total, state_->rules_version >= cellular_lives_engulf_rules,
state_->rules_version >= cellular_lives_bud_rules, state_->next_bud_sequence,
state_->rules_version >= cellular_lives_presence_rules);
const auto current = validate_state(
state_->world, state_->region, state_->frame, state_->config, nursery_config, **habitat,
lives, state_->corpses, archive, nursery, heat, state_->opening_total,
state_->rules_version >= cellular_lives_engulf_rules,
state_->rules_version >= cellular_lives_bud_rules, state_->next_bud_sequence,
state_->rules_version >= cellular_lives_presence_rules,
state_->format_version == cellular_lives_migration_format ? &state_->boundary : nullptr);
if (!current)
return std::unexpected{current.error()};
auto candidate = std::unique_ptr<CellularLives>{new CellularLives{std::make_unique<State>(
state_->world, state_->region, state_->frame, state_->config, std::move(nursery_config),
std::move(*habitat), std::move(lives), state_->corpses, archive, nursery, heat,
state_->opening_total, *current, state_->format_version, state_->rules_version)}};
state_->opening_total, *current, state_->format_version, state_->rules_version,
state_->boundary)}};
candidate->last_tick_feedback_ = last_tick_feedback_;
candidate->state_->next_bud_sequence = state_->next_bud_sequence;
return candidate;
@@ -985,12 +1089,16 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
format_version == cellular_lives_format &&
rules_version == cellular_lives_rules;
// Format 6 carries rules 5, 6 (founder 2) or 7 (founder 3, ADR-0137); no new state.
const bool presence_version = magic == cellular_lives_v6_magic &&
format_version == cellular_lives_presence_format &&
(rules_version == cellular_lives_presence_rules ||
rules_version == cellular_lives_balance_rules ||
rules_version == cellular_lives_reproduction_rules ||
rules_version == cellular_lives_foraging_rules);
const bool migration_version = magic == cellular_lives_v7_magic &&
format_version == cellular_lives_migration_format &&
rules_version == cellular_lives_foraging_rules;
const bool presence_version =
migration_version ||
(magic == cellular_lives_v6_magic && format_version == cellular_lives_presence_format &&
(rules_version == cellular_lives_presence_rules ||
rules_version == cellular_lives_balance_rules ||
rules_version == cellular_lives_reproduction_rules ||
rules_version == cellular_lives_foraging_rules));
// Formats 5 and 6 carry the bud sequence and parent lives (rules 4 and 5).
const bool bud_version = presence_version || (magic == cellular_lives_v5_magic &&
format_version == cellular_lives_bud_format &&
@@ -1006,6 +1114,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
const bool mutation_version = offer_version || revision_version;
if (!legacy && !mutation_version)
return std::unexpected{CellularLivesError::version};
if (migration_version && bytes.size() < cellular_lives_v7_header_bytes + 8)
return std::unexpected(CellularLivesError::format);
const auto saved_world = in.get();
const auto saved_region = in.get();
const auto saved_frame = in.get();
@@ -1036,6 +1146,15 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
const auto nursery = in.inventory();
const auto heat = in.inventory();
const auto next_bud_sequence = bud_version ? in.get() : std::uint64_t{1};
CellularBoundaryLedger boundary;
std::uint64_t nonresident_count = 0;
if (migration_version) {
boundary.incoming = in.inventory();
boundary.outgoing = in.inventory();
nonresident_count = in.get(4);
if (in.get(4) != 0 || nonresident_count > biology::max_archived_organisms)
return std::unexpected(CellularLivesError::format);
}
const auto life_bytes = legacy ? cellular_life_v1_bytes
: presence_version ? cellular_life_v6_bytes
: bud_version ? cellular_life_v5_bytes
@@ -1043,10 +1162,12 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
const auto corpse_bytes = bud_version ? cellular_corpse_v5_bytes
: engulf_version ? cellular_corpse_v4_bytes
: cellular_corpse_bytes;
const auto expected_size =
(bud_version ? cellular_lives_v5_header_bytes : cellular_lives_header_bytes) +
cellular_site_bytes * site_count + life_bytes * life_count + corpse_bytes * corpse_count +
habitat_size + archive_size + 8;
const auto expected_size = (migration_version ? cellular_lives_v7_header_bytes
: bud_version ? cellular_lives_v5_header_bytes
: cellular_lives_header_bytes) +
cellular_site_bytes * site_count + life_bytes * life_count +
corpse_bytes * corpse_count + habitat_size + archive_size +
nonresident_count * 8 + 8;
if (source == 0 || next_bud_sequence == 0 || expected_size != bytes.size())
return std::unexpected{CellularLivesError::format};
@@ -1189,6 +1310,11 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
return std::unexpected{CellularLivesError::noncanonical};
return std::unexpected{CellularLivesError::archive};
}
if (migration_version) {
boundary.nonresident_organisms.reserve(static_cast<std::size_t>(nonresident_count));
for (std::uint64_t index = 0; index < nonresident_count; ++index)
boundary.nonresident_organisms.push_back(in.get());
}
if (archive->format_version() != (revision_version ? biology::lineage_archive_revision_format
: biology::lineage_archive_format))
return std::unexpected{CellularLivesError::version};
@@ -1214,17 +1340,17 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
return std::unexpected{CellularLivesError::noncanonical};
}
}
const auto current =
validate_state(saved_world, saved_region, saved_frame, config, nursery_config, **habitat,
lives, corpses, *archive, nursery, heat, opening_total, engulf_version,
bud_version, next_bud_sequence, presence_version);
const auto current = validate_state(
saved_world, saved_region, saved_frame, config, nursery_config, **habitat, lives, corpses,
*archive, nursery, heat, opening_total, engulf_version, bud_version, next_bud_sequence,
presence_version, migration_version ? &boundary : nullptr);
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, format_version, rules_version)}};
opening_total, *current, format_version, rules_version, std::move(boundary))}};
restored->state_->next_bud_sequence = next_bud_sequence;
if (!std::ranges::equal(bytes, restored->snapshot()))
return std::unexpected{CellularLivesError::noncanonical};
@@ -1502,8 +1628,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
}();
for (int copy = 0; copy < (twins ? 2 : 1); ++copy) {
if (copy == 1)
// Appending the first bud may have moved the parent; find it again (buds
// sort after every existing life, so the order still holds).
// Inserting the first bud may have moved the parent; find it again.
parent = std::ranges::lower_bound(
next_lives, request.life_id, {},
[](const CellularOwnedLife& value) { return value.identity.life; });
@@ -1556,9 +1681,12 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
bud.body = transition->body;
bud.reserve = transition->reserve;
bud.parent_life = parent->identity.life;
// Bud identities increase within the region and exceed every allocated identity,
// so the new life sorts after all existing ones.
next_lives.push_back(bud);
// Foreign residents can have a higher origin-region namespace. Keep
// the sequence sorted even before the next parent's lookup/twin.
const auto slot = std::ranges::lower_bound(
next_lives, identity, {},
[](const CellularOwnedLife& life) { return life.identity.life; });
next_lives.insert(slot, bud);
++next_bud_sequence;
if (!twin)
bud_results.push_back({request.life_id, identity, CellularBudResult::budded});
@@ -1640,13 +1768,14 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
auto next_habitat = biology::CellularHabitat::create(std::move(habitat_config));
if (!next_habitat)
return std::unexpected{CellularLivesError::habitat};
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,
engulf_rules, bud_rules, next_bud_sequence, presence_rules);
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, engulf_rules, bud_rules,
next_bud_sequence, presence_rules,
state_->format_version == cellular_lives_migration_format ? &state_->boundary : nullptr);
if (!total)
return std::unexpected{total.error()};
@@ -1656,7 +1785,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
state_->config.ambient_temperature_millikelvin},
state_->nursery_config, std::move(*next_habitat), std::move(next_lives),
std::move(next_corpses), next_archive, state_->nursery, next_heat, state_->opening_total,
*total, state_->format_version, state_->rules_version);
*total, state_->format_version, state_->rules_version, state_->boundary);
next->next_bud_sequence = next_bud_sequence;
std::vector<CellularLifeTickFeedback> feedback;
feedback.reserve(proposal->lives.size());