feat(lifecycle): retain inherited offspring plans separately from bodies
Foundation / checks (ecs) (push) Successful in 2m26s
Foundation / checks (deploy) (push) Successful in 4m6s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (client) (push) Canceled after 9m29s
Foundation / checks (tsan) (push) Canceled after 7m4s
Foundation / checks (network) (push) Canceled after 9m28s
Foundation / checks (postgres) (push) Canceled after 9m28s
Foundation / checks (checks) (push) Canceled after 9m34s
Foundation / checks (ecs) (push) Successful in 2m26s
Foundation / checks (deploy) (push) Successful in 4m6s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (client) (push) Canceled after 9m29s
Foundation / checks (tsan) (push) Canceled after 7m4s
Foundation / checks (network) (push) Canceled after 9m28s
Foundation / checks (postgres) (push) Canceled after 9m28s
Foundation / checks (checks) (push) Canceled after 9m34s
This commit is contained in:
@@ -6,7 +6,9 @@
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <ranges>
|
||||
#include <set>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
@@ -19,6 +21,7 @@ constexpr std::uint64_t cellular_lives_v4_magic = 0x3430564C43564541ULL; // AEVC
|
||||
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::uint64_t cellular_lives_v8_magic = 0x3830564C43564541ULL; // AEVCLV08
|
||||
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;
|
||||
@@ -35,6 +38,8 @@ constexpr std::size_t cellular_corpse_v5_bytes = cellular_corpse_v4_bytes + 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;
|
||||
constexpr std::size_t cellular_lives_v8_header_bytes = cellular_lives_v7_header_bytes + 8;
|
||||
constexpr std::size_t cellular_offspring_revision_bytes = 32;
|
||||
|
||||
std::uint64_t checksum(std::span<const std::uint8_t> bytes) noexcept {
|
||||
std::uint64_t value = 14695981039346656037ULL;
|
||||
@@ -276,6 +281,84 @@ sum_state(accounting::Inventory nursery, accounting::Inventory heat,
|
||||
return total;
|
||||
}
|
||||
|
||||
bool validate_plans(std::span<const CellularOwnedLife> lives,
|
||||
std::span<const CellularOwnedCorpse> corpses,
|
||||
const biology::LineageArchive& archive,
|
||||
std::span<const CellularOffspringRevision> history, std::uint64_t tick) {
|
||||
if (history.size() > biology::max_archived_genomes)
|
||||
return false;
|
||||
struct Plan {
|
||||
std::uint64_t life, expressed, current, final, editable_after, died;
|
||||
bool selectable;
|
||||
};
|
||||
std::vector<Plan> plans;
|
||||
std::map<std::uint64_t, std::uint64_t> earliest_birth;
|
||||
std::map<std::uint64_t, std::uint64_t> last_expression;
|
||||
for (const auto& revision : archive.revisions())
|
||||
last_expression[revision.organism] =
|
||||
std::max(last_expression[revision.organism], revision.at.value);
|
||||
const auto observe = [&](std::uint64_t genome, std::uint64_t born) {
|
||||
const auto [position, inserted] = earliest_birth.emplace(genome, born);
|
||||
if (!inserted)
|
||||
position->second = std::min(position->second, born);
|
||||
};
|
||||
plans.reserve(lives.size() + corpses.size());
|
||||
for (const auto& life : lives) {
|
||||
plans.push_back(
|
||||
{life.identity.life, life.identity.genome, life.identity.genome, life.offspring_genome,
|
||||
std::max(life.identity.born.value, last_expression[life.identity.organism]), 0,
|
||||
life.account != LifeAccount{}});
|
||||
observe(life.identity.genome, life.identity.born.value);
|
||||
}
|
||||
for (const auto& corpse : corpses) {
|
||||
const auto record = archive.organism(corpse.organism);
|
||||
const auto expressed = archive.current_genome(corpse.organism);
|
||||
if (!record || !expressed)
|
||||
return false;
|
||||
plans.push_back({corpse.life_id, *expressed, *expressed, corpse.offspring_genome,
|
||||
std::max(record->born.value, last_expression[corpse.organism]),
|
||||
corpse.died_tick, corpse.parent_life == 0});
|
||||
observe(record->genome, record->born.value);
|
||||
observe(*expressed, record->born.value);
|
||||
}
|
||||
std::ranges::sort(plans, {}, &Plan::life);
|
||||
std::set<std::uint64_t> successors;
|
||||
for (std::size_t index = 0; index < history.size(); ++index) {
|
||||
const auto& revision = history[index];
|
||||
if (revision.tick == 0 || revision.tick > tick || revision.successor == revision.previous ||
|
||||
(index != 0 && (history[index - 1].tick > revision.tick ||
|
||||
(history[index - 1].tick == revision.tick &&
|
||||
history[index - 1].life >= revision.life))) ||
|
||||
!successors.insert(revision.successor).second)
|
||||
return false;
|
||||
const auto actor = std::ranges::lower_bound(plans, revision.life, {}, &Plan::life);
|
||||
const auto parent = archive.find(revision.previous);
|
||||
const auto child = archive.find(revision.successor);
|
||||
if (actor == plans.end() || actor->life != revision.life || !actor->selectable ||
|
||||
actor->current != revision.previous || revision.tick <= actor->editable_after ||
|
||||
(actor->died != 0 && revision.tick >= actor->died) || !parent || !child ||
|
||||
child->parents().size() != 1 || child->parents().front() != revision.previous ||
|
||||
child->origin_event() != revision.successor ||
|
||||
child->schema_version() != parent->schema_version() ||
|
||||
child->development_version() != parent->development_version())
|
||||
return false;
|
||||
actor->current = revision.successor;
|
||||
// A plan selected here cannot have been expressed at an earlier birth.
|
||||
const auto born = earliest_birth.find(revision.successor);
|
||||
if (born != earliest_birth.end() && born->second < revision.tick)
|
||||
return false;
|
||||
}
|
||||
for (const auto& plan : plans) {
|
||||
const auto expressed = archive.find(plan.expressed);
|
||||
const auto inherited = archive.find(plan.final);
|
||||
if (plan.final == 0 || plan.current != plan.final || !expressed || !inherited ||
|
||||
expressed->schema_version() != inherited->schema_version() ||
|
||||
expressed->development_version() != inherited->development_version())
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
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,
|
||||
@@ -284,7 +367,8 @@ 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, const CellularBoundaryLedger* boundary = nullptr) {
|
||||
bool presence_rules = false, const CellularBoundaryLedger* boundary = nullptr,
|
||||
const std::vector<CellularOffspringRevision>* plans = 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 {
|
||||
@@ -350,6 +434,8 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
|
||||
(boundary ? boundary->nonresident_organisms.size() : 0));
|
||||
for (std::size_t i = 0; i < lives.size(); ++i) {
|
||||
const auto& life = lives[i];
|
||||
if (!plans && life.offspring_genome != 0)
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
// Founders are owned; buds are unowned wildlife (rules 4) and never earn offers.
|
||||
const bool wildlife = life.parent_life != 0;
|
||||
// ADR-0128: only owned lives leave; a lingering life is within its linger time.
|
||||
@@ -415,6 +501,8 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
|
||||
|
||||
for (std::size_t i = 0; i < corpses.size(); ++i) {
|
||||
const auto& corpse = corpses[i];
|
||||
if (!plans && corpse.offspring_genome != 0)
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
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) ||
|
||||
@@ -451,6 +539,8 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
|
||||
if (std::ranges::adjacent_find(life_ids) != life_ids.end())
|
||||
return std::unexpected{CellularLivesError::duplicate_life};
|
||||
|
||||
if (plans && !validate_plans(lives, corpses, archive, *plans, config.tick))
|
||||
return std::unexpected{CellularLivesError::invalid_life};
|
||||
const auto current = sum_state(nursery, heat, habitat, lives, corpses);
|
||||
if (!current)
|
||||
return std::unexpected{current.error()};
|
||||
@@ -494,6 +584,7 @@ struct CellularLives::State {
|
||||
accounting::Inventory current_total{};
|
||||
std::uint64_t next_bud_sequence{1}; // Format 5: the next bud identity's sequence.
|
||||
CellularBoundaryLedger boundary{};
|
||||
std::vector<CellularOffspringRevision> offspring_revisions;
|
||||
|
||||
State(std::uint64_t world_id, std::uint64_t region_id, std::uint64_t frame_id,
|
||||
CellularLivesConfig cfg, NurseryConfig nursery_definition,
|
||||
@@ -730,6 +821,31 @@ std::span<const CellularLifeTickFeedback> CellularLives::last_tick_feedback() co
|
||||
return last_tick_feedback_;
|
||||
}
|
||||
|
||||
std::span<const CellularOffspringRevision> CellularLives::offspring_revisions() const noexcept {
|
||||
return state_->offspring_revisions;
|
||||
}
|
||||
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
|
||||
CellularLives::inheritance_upgrade_candidate() const {
|
||||
if (state_->format_version != cellular_lives_migration_format ||
|
||||
state_->rules_version != cellular_lives_maturation_rules)
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
auto candidate = restore(snapshot(), world(), region(), frame(), tick());
|
||||
if (!candidate)
|
||||
return std::unexpected(candidate.error());
|
||||
auto& next = *(*candidate)->state_;
|
||||
next.format_version = cellular_lives_inheritance_format;
|
||||
next.rules_version = cellular_lives_inheritance_rules;
|
||||
for (auto& life : next.lives)
|
||||
life.offspring_genome = life.identity.genome;
|
||||
for (auto& corpse : next.corpses) {
|
||||
const auto genome = next.archive.current_genome(corpse.organism);
|
||||
if (!genome)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
corpse.offspring_genome = *genome;
|
||||
}
|
||||
return restore((*candidate)->snapshot(), world(), region(), frame(), tick());
|
||||
}
|
||||
|
||||
std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
const auto habitat = state_->habitat->snapshot();
|
||||
const auto archive = state_->archive.encode();
|
||||
@@ -738,29 +854,36 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
const auto corpse_count = state_->corpses.size();
|
||||
const bool parents = state_->format_version >= cellular_lives_bud_format;
|
||||
const bool presence = state_->format_version >= cellular_lives_presence_format;
|
||||
const auto life_bytes = state_->format_version == cellular_lives_legacy_format
|
||||
const bool inheritance = state_->format_version == cellular_lives_inheritance_format;
|
||||
const auto life_bytes = inheritance ? cellular_life_v6_bytes + 8
|
||||
: state_->format_version == cellular_lives_legacy_format
|
||||
? cellular_life_v1_bytes
|
||||
: presence ? cellular_life_v6_bytes
|
||||
: parents ? cellular_life_v5_bytes
|
||||
: cellular_life_v2_bytes;
|
||||
const bool causes = state_->format_version >= cellular_lives_engulf_format;
|
||||
const auto corpse_bytes = parents ? cellular_corpse_v5_bytes
|
||||
: causes ? cellular_corpse_v4_bytes
|
||||
: cellular_corpse_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 corpse_bytes = inheritance ? cellular_corpse_v5_bytes + 8
|
||||
: parents ? cellular_corpse_v5_bytes
|
||||
: causes ? cellular_corpse_v4_bytes
|
||||
: cellular_corpse_bytes;
|
||||
const bool migration = state_->format_version >= cellular_lives_migration_format;
|
||||
const auto header_bytes = inheritance ? cellular_lives_v8_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() + external_count * 8 + 8;
|
||||
const auto total_bytes =
|
||||
header_bytes + cellular_site_bytes * site_count + life_bytes * life_count +
|
||||
corpse_bytes * corpse_count + habitat.size() + archive.size() + external_count * 8 +
|
||||
state_->offspring_revisions.size() * cellular_offspring_revision_bytes + 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 ||
|
||||
external_count > biology::max_archived_organisms)
|
||||
external_count > biology::max_archived_organisms ||
|
||||
state_->offspring_revisions.size() > biology::max_archived_genomes ||
|
||||
(!inheritance && !state_->offspring_revisions.empty()))
|
||||
throw std::length_error{"cellular lives checkpoint exceeds its format bounds"};
|
||||
|
||||
std::vector<std::uint8_t> bytes;
|
||||
@@ -772,6 +895,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
|
||||
: inheritance ? cellular_lives_v8_magic
|
||||
: migration ? cellular_lives_v7_magic
|
||||
: cellular_lives_v2_magic;
|
||||
out.put(magic);
|
||||
@@ -802,6 +926,10 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
out.put(external_count, 4);
|
||||
out.put(0, 4);
|
||||
}
|
||||
if (inheritance) {
|
||||
out.put(state_->offspring_revisions.size(), 4);
|
||||
out.put(0, 4);
|
||||
}
|
||||
for (const auto site : state_->nursery_config.sites)
|
||||
out.site(site);
|
||||
for (const auto& life : state_->lives) {
|
||||
@@ -860,6 +988,8 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
out.zeros(7);
|
||||
out.put(life.frozen_since);
|
||||
}
|
||||
if (inheritance)
|
||||
out.put(life.offspring_genome);
|
||||
}
|
||||
for (const auto& corpse : state_->corpses) {
|
||||
out.put(corpse.life_id);
|
||||
@@ -879,12 +1009,21 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
}
|
||||
if (parents)
|
||||
out.put(corpse.parent_life);
|
||||
if (inheritance)
|
||||
out.put(corpse.offspring_genome);
|
||||
}
|
||||
out.raw(habitat);
|
||||
out.raw(archive);
|
||||
if (migration)
|
||||
for (const auto id : state_->boundary.nonresident_organisms)
|
||||
out.put(id);
|
||||
if (inheritance)
|
||||
for (const auto& revision : state_->offspring_revisions) {
|
||||
out.put(revision.life);
|
||||
out.put(revision.previous);
|
||||
out.put(revision.successor);
|
||||
out.put(revision.tick);
|
||||
}
|
||||
if (bytes.size() + 8 != total_bytes)
|
||||
throw std::logic_error{"cellular lives checkpoint size calculation mismatch"};
|
||||
out.put(checksum(bytes));
|
||||
@@ -1057,6 +1196,8 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
|
||||
auto lives = state_->lives;
|
||||
lives.push_back({request.account, allocation, state_->nursery_config.source, endowed->growth,
|
||||
*position, endowed->body, endowed->reserve});
|
||||
if (state_->format_version == cellular_lives_inheritance_format)
|
||||
lives.back().offspring_genome = allocation.genome;
|
||||
std::ranges::sort(lives, {}, [](const CellularOwnedLife& life) { return life.identity.life; });
|
||||
|
||||
auto nursery_config = state_->nursery_config;
|
||||
@@ -1071,7 +1212,9 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
|
||||
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);
|
||||
state_->format_version >= cellular_lives_migration_format ? &state_->boundary : nullptr,
|
||||
state_->format_version == cellular_lives_inheritance_format ? &state_->offspring_revisions
|
||||
: nullptr);
|
||||
if (!current)
|
||||
return std::unexpected{current.error()};
|
||||
|
||||
@@ -1082,6 +1225,7 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
|
||||
state_->boundary)}};
|
||||
candidate->last_tick_feedback_ = last_tick_feedback_;
|
||||
candidate->state_->next_bud_sequence = state_->next_bud_sequence;
|
||||
candidate->state_->offspring_revisions = state_->offspring_revisions;
|
||||
return candidate;
|
||||
}
|
||||
|
||||
@@ -1104,10 +1248,14 @@ 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 migration_version = magic == cellular_lives_v7_magic &&
|
||||
format_version == cellular_lives_migration_format &&
|
||||
(rules_version == cellular_lives_foraging_rules ||
|
||||
rules_version == cellular_lives_maturation_rules);
|
||||
const bool inheritance_version = magic == cellular_lives_v8_magic &&
|
||||
format_version == cellular_lives_inheritance_format &&
|
||||
rules_version == cellular_lives_inheritance_rules;
|
||||
const bool migration_version =
|
||||
inheritance_version ||
|
||||
(magic == cellular_lives_v7_magic && format_version == cellular_lives_migration_format &&
|
||||
(rules_version == cellular_lives_foraging_rules ||
|
||||
rules_version == cellular_lives_maturation_rules));
|
||||
const bool presence_version =
|
||||
migration_version ||
|
||||
(magic == cellular_lives_v6_magic && format_version == cellular_lives_presence_format &&
|
||||
@@ -1132,6 +1280,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
if (migration_version && bytes.size() < cellular_lives_v7_header_bytes + 8)
|
||||
return std::unexpected(CellularLivesError::format);
|
||||
if (inheritance_version && bytes.size() < cellular_lives_v8_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();
|
||||
@@ -1171,19 +1321,29 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
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
|
||||
: cellular_life_v2_bytes;
|
||||
const auto corpse_bytes = bud_version ? cellular_corpse_v5_bytes
|
||||
: engulf_version ? cellular_corpse_v4_bytes
|
||||
: cellular_corpse_bytes;
|
||||
const auto expected_size = (migration_version ? cellular_lives_v7_header_bytes
|
||||
: bud_version ? cellular_lives_v5_header_bytes
|
||||
: cellular_lives_header_bytes) +
|
||||
std::uint64_t plan_count = 0;
|
||||
if (inheritance_version) {
|
||||
plan_count = in.get(4);
|
||||
if (in.get(4) != 0 || plan_count > biology::max_archived_genomes)
|
||||
return std::unexpected{CellularLivesError::format};
|
||||
}
|
||||
const auto life_bytes = inheritance_version ? cellular_life_v6_bytes + 8
|
||||
: legacy ? cellular_life_v1_bytes
|
||||
: presence_version ? cellular_life_v6_bytes
|
||||
: bud_version ? cellular_life_v5_bytes
|
||||
: cellular_life_v2_bytes;
|
||||
const auto corpse_bytes = inheritance_version ? cellular_corpse_v5_bytes + 8
|
||||
: bud_version ? cellular_corpse_v5_bytes
|
||||
: engulf_version ? cellular_corpse_v4_bytes
|
||||
: cellular_corpse_bytes;
|
||||
const auto expected_size = (inheritance_version ? cellular_lives_v8_header_bytes
|
||||
: 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;
|
||||
nonresident_count * 8 +
|
||||
plan_count * cellular_offspring_revision_bytes + 8;
|
||||
if (source == 0 || next_bud_sequence == 0 || expected_size != bytes.size())
|
||||
return std::unexpected{CellularLivesError::format};
|
||||
|
||||
@@ -1266,6 +1426,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
life.presence = static_cast<CellularPresence>(presence);
|
||||
}
|
||||
if (inheritance_version)
|
||||
life.offspring_genome = in.get();
|
||||
lives.push_back(life);
|
||||
}
|
||||
|
||||
@@ -1298,6 +1460,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
}
|
||||
if (bud_version)
|
||||
corpse.parent_life = in.get();
|
||||
if (inheritance_version)
|
||||
corpse.offspring_genome = in.get();
|
||||
corpses.push_back(corpse);
|
||||
}
|
||||
const auto habitat_payload = in.raw(static_cast<std::size_t>(habitat_size));
|
||||
@@ -1331,6 +1495,10 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
for (std::uint64_t index = 0; index < nonresident_count; ++index)
|
||||
boundary.nonresident_organisms.push_back(in.get());
|
||||
}
|
||||
std::vector<CellularOffspringRevision> plan_history;
|
||||
plan_history.reserve(static_cast<std::size_t>(plan_count));
|
||||
for (std::uint64_t index = 0; index < plan_count; ++index)
|
||||
plan_history.push_back({in.get(), in.get(), in.get(), in.get()});
|
||||
if (archive->format_version() != (revision_version ? biology::lineage_archive_revision_format
|
||||
: biology::lineage_archive_format))
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
@@ -1338,12 +1506,13 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
for (const auto& life : lives) {
|
||||
if (!life.mutation.offer)
|
||||
continue;
|
||||
const auto genome = archive->find(life.identity.genome);
|
||||
const auto genome = archive->find(cellular_offspring_genome(life));
|
||||
if (!genome)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto& offer = *life.mutation.offer;
|
||||
if (life.mutation.event_index == 0 || life.mutation.offer_tick == 0 ||
|
||||
life.mutation.offer_tick > saved_tick || offer.parent != life.identity.genome ||
|
||||
life.mutation.offer_tick > saved_tick ||
|
||||
offer.parent != cellular_offspring_genome(life) ||
|
||||
offer.context.seed != mutation_seed(saved_world, saved_region, life.identity.life,
|
||||
life.mutation.event_index) ||
|
||||
offer.context.stream != cellular_mutation_stream ||
|
||||
@@ -1359,7 +1528,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
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);
|
||||
presence_version, migration_version ? &boundary : nullptr,
|
||||
inheritance_version ? &plan_history : nullptr);
|
||||
if (!current)
|
||||
return std::unexpected{current.error()};
|
||||
|
||||
@@ -1368,6 +1538,7 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
std::move(*habitat), std::move(lives), std::move(corpses), *archive, nursery, heat,
|
||||
opening_total, *current, format_version, rules_version, std::move(boundary))}};
|
||||
restored->state_->next_bud_sequence = next_bud_sequence;
|
||||
restored->state_->offspring_revisions = std::move(plan_history);
|
||||
if (!std::ranges::equal(bytes, restored->snapshot()))
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
return restored;
|
||||
@@ -1403,6 +1574,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
const bool contact_rules = state_->rules_version >= cellular_lives_foraging_rules;
|
||||
const bool bud_rules = state_->rules_version >= cellular_lives_bud_rules;
|
||||
const bool presence_rules = state_->rules_version >= cellular_lives_presence_rules;
|
||||
const bool inheritance_rules = state_->rules_version == cellular_lives_inheritance_rules;
|
||||
for (std::size_t i = 0; i < ordered.size(); ++i) {
|
||||
const auto& intent = ordered[i].intent;
|
||||
// Below rules 4 a bud is ignored, as engulfment is below rules 3 (ADR-0124/0125).
|
||||
@@ -1431,7 +1603,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
if (!life->mutation.offer.has_value())
|
||||
return std::unexpected{CellularLivesError::mutation};
|
||||
const auto offer = life->mutation.offer.value_or(biology::MutationOffer{});
|
||||
if (offer.parent != life->identity.genome ||
|
||||
if (offer.parent != cellular_offspring_genome(*life) ||
|
||||
offer.context.event_index != selection.event_index ||
|
||||
(selection.operator_id != 0 && !std::ranges::any_of(offer.candidates,
|
||||
[&](const auto& candidate) {
|
||||
@@ -1481,6 +1653,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
auto next_lives = state_->lives;
|
||||
auto next_corpses = state_->corpses;
|
||||
auto next_archive = state_->archive;
|
||||
auto next_offspring_revisions = state_->offspring_revisions;
|
||||
auto next_heat = state_->heat;
|
||||
auto habitat_config = state_->habitat->config();
|
||||
for (const auto& result : proposal->lives) {
|
||||
@@ -1514,7 +1687,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
if (found->mutation.fed_energy_credit_pj >= threshold) {
|
||||
if (found->mutation.event_index == std::numeric_limits<std::uint64_t>::max())
|
||||
return std::unexpected{CellularLivesError::overflow};
|
||||
const auto genome = next_archive.find(found->identity.genome);
|
||||
const auto genome = next_archive.find(cellular_offspring_genome(*found));
|
||||
if (!genome)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto next_event = found->mutation.event_index + 1;
|
||||
@@ -1549,11 +1722,11 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
budget_for(next_archive, *found, state_->config.ambient_temperature_millikelvin);
|
||||
if (!corpse_budget)
|
||||
return std::unexpected{corpse_budget.error()};
|
||||
next_corpses.push_back({found->identity.life, found->identity.organism, found->source,
|
||||
proposal->end_tick, dead.position, found->growth,
|
||||
corpse_budget->phenotype.extent, dead.inventory,
|
||||
engulf_rules ? dead.cause : biology::CellularDeathCause::unrecorded,
|
||||
engulf_rules ? dead.predator_id : 0, found->parent_life});
|
||||
next_corpses.push_back(
|
||||
{found->identity.life, found->identity.organism, found->source, proposal->end_tick,
|
||||
dead.position, found->growth, corpse_budget->phenotype.extent, dead.inventory,
|
||||
engulf_rules ? dead.cause : biology::CellularDeathCause::unrecorded,
|
||||
engulf_rules ? dead.predator_id : 0, found->parent_life, found->offspring_genome});
|
||||
next_lives.erase(found);
|
||||
}
|
||||
std::vector<CellularMutationExecution> mutation_results;
|
||||
@@ -1567,7 +1740,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
{selection.life_id, selection.successor_id, CellularMutationResult::life_died});
|
||||
continue;
|
||||
}
|
||||
const auto parent = next_archive.find(found->identity.genome);
|
||||
const auto parent = next_archive.find(cellular_offspring_genome(*found));
|
||||
if (!parent || !found->mutation.offer.has_value())
|
||||
return std::unexpected{CellularLivesError::mutation};
|
||||
if (selection.operator_id == 0) {
|
||||
@@ -1580,6 +1753,24 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
continue;
|
||||
}
|
||||
const auto offer = found->mutation.offer.value_or(biology::MutationOffer{});
|
||||
if (inheritance_rules) {
|
||||
const auto selected =
|
||||
biology::select_mutation(*parent, offer.context, offer, selection.operator_id,
|
||||
selection.successor_id, selection.successor_id);
|
||||
if (!selected)
|
||||
return std::unexpected{CellularLivesError::mutation};
|
||||
if (next_offspring_revisions.size() >= biology::max_archived_genomes ||
|
||||
!next_archive.admit(*selected))
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
next_offspring_revisions.push_back({selection.life_id, found->offspring_genome,
|
||||
selection.successor_id, proposal->end_tick});
|
||||
found->offspring_genome = selection.successor_id;
|
||||
found->mutation.offer.reset();
|
||||
found->mutation.offer_tick = 0;
|
||||
mutation_results.push_back({selection.life_id, selection.successor_id,
|
||||
CellularMutationResult::offspring_plan_applied});
|
||||
continue;
|
||||
}
|
||||
auto transition = biology::fund_mutation_at_growth(
|
||||
*parent, offer.context, offer, selection.operator_id, selection.successor_id,
|
||||
selection.successor_id, found->reserve,
|
||||
@@ -1633,15 +1824,17 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
if (state_->rules_version < cellular_lives_reproduction_rules)
|
||||
return false;
|
||||
const auto parent_genome = next_archive.find(parent->identity.genome);
|
||||
if (!parent_genome)
|
||||
const auto offspring_genome = next_archive.find(cellular_offspring_genome(*parent));
|
||||
if (!parent_genome || !offspring_genome)
|
||||
return false;
|
||||
const auto skill = parent_genome->parameter(biology::Capability::reproduction);
|
||||
// Twins split one whole bud: the parent must afford that whole bud first.
|
||||
return skill &&
|
||||
cellular_twin_bud(state_->world, state_->region, request.life_id,
|
||||
proposal->end_tick, *skill) &&
|
||||
biology::fund_bud(*parent_genome, parent->growth, parent->reserve,
|
||||
state_->config.ambient_temperature_millikelvin)
|
||||
biology::fund_offspring(*parent_genome, *offspring_genome, parent->growth,
|
||||
parent->reserve,
|
||||
state_->config.ambient_temperature_millikelvin)
|
||||
.has_value();
|
||||
}();
|
||||
for (int copy = 0; copy < (twins ? 2 : 1); ++copy) {
|
||||
@@ -1652,12 +1845,13 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
[](const CellularOwnedLife& value) { return value.identity.life; });
|
||||
const bool twin = copy == 1;
|
||||
const auto temperature = state_->config.ambient_temperature_millikelvin;
|
||||
const auto genome = next_archive.find(parent->identity.genome);
|
||||
const auto genome = next_archive.find(cellular_offspring_genome(*parent));
|
||||
const auto expressed = next_archive.find(parent->identity.genome);
|
||||
const auto parent_budget = budget_for(next_archive, *parent, temperature);
|
||||
if (!genome || !parent_budget)
|
||||
if (!genome || !expressed || !parent_budget)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
const auto transition = biology::fund_bud(*genome, parent->growth, parent->reserve,
|
||||
temperature, twins ? 2U : 1U);
|
||||
const auto transition = biology::fund_offspring(
|
||||
*expressed, *genome, parent->growth, parent->reserve, temperature, twins ? 2U : 1U);
|
||||
if (!transition) {
|
||||
if (transition.error() == biology::BuddingError::development)
|
||||
return std::unexpected{CellularLivesError::biology};
|
||||
@@ -1687,18 +1881,19 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
break;
|
||||
}
|
||||
if (!add_inventory(next_heat, accounting::Inventory{transition->heat, {}}) ||
|
||||
!next_archive.record_birth(identity, parent->identity.genome, {proposal->end_tick}))
|
||||
!next_archive.record_birth(identity, genome->id(), {proposal->end_tick}))
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
parent->reserve = transition->parent_reserve;
|
||||
CellularOwnedLife bud{};
|
||||
bud.identity = {
|
||||
identity, identity, parent->identity.genome, identity, {proposal->end_tick}};
|
||||
bud.identity = {identity, identity, genome->id(), identity, {proposal->end_tick}};
|
||||
bud.source = parent->source;
|
||||
bud.growth = transition->growth;
|
||||
bud.position = **position;
|
||||
bud.body = transition->body;
|
||||
bud.reserve = transition->reserve;
|
||||
bud.parent_life = parent->identity.life;
|
||||
if (inheritance_rules)
|
||||
bud.offspring_genome = genome->id();
|
||||
// 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(
|
||||
@@ -1754,7 +1949,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
// resuming from suspension did not take an active step and cannot grow here.
|
||||
// Expansion holds the existing lower corner fixed; earlier IDs claim space first.
|
||||
std::vector<CellularGrowthExecution> growth_results;
|
||||
if (state_->rules_version == cellular_lives_maturation_rules) {
|
||||
if (state_->rules_version >= cellular_lives_maturation_rules) {
|
||||
for (auto& life : next_lives) {
|
||||
const auto input = std::ranges::lower_bound(ordered, life.identity.life, {},
|
||||
&CellularLifeIntent::life_id);
|
||||
@@ -1856,7 +2051,8 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
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);
|
||||
state_->format_version >= cellular_lives_migration_format ? &state_->boundary : nullptr,
|
||||
inheritance_rules ? &next_offspring_revisions : nullptr);
|
||||
if (!total)
|
||||
return std::unexpected{total.error()};
|
||||
|
||||
@@ -1868,6 +2064,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
std::move(next_corpses), next_archive, state_->nursery, next_heat, state_->opening_total,
|
||||
*total, state_->format_version, state_->rules_version, state_->boundary);
|
||||
next->next_bud_sequence = next_bud_sequence;
|
||||
next->offspring_revisions = std::move(next_offspring_revisions);
|
||||
std::vector<CellularLifeTickFeedback> feedback;
|
||||
feedback.reserve(proposal->lives.size());
|
||||
for (const auto& result : proposal->lives) {
|
||||
@@ -1910,7 +2107,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
entry = std::prev(feedback.end());
|
||||
}
|
||||
entry->presence = life.presence;
|
||||
if (state_->rules_version == cellular_lives_maturation_rules)
|
||||
if (state_->rules_version >= cellular_lives_maturation_rules)
|
||||
entry->reserve = life.reserve; // Includes funded growth/mutation/budding.
|
||||
entry->resume_blocked =
|
||||
std::ranges::find(resume_blocked, life.identity.life) != resume_blocked.end();
|
||||
|
||||
Reference in New Issue
Block a user