Files
aevolution/tests/cellular_inheritance_replay_tests.cpp
T
dingoandClaude Opus 5.5 9f117c9545
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m22s
Foundation / postgres-tools (push) Successful in 22s
Foundation / server-image (push) Successful in 4m21s
Foundation / image (push) Successful in 5m6s
feat(lifecycle): rules 13 mutation splits, stay-or-become and litter
Phase 4 of the cellular revamp build plan (ADR-0179), server half.

- A chosen mutation now splits the cell (Cellular_reproduction_plan.md section
  4): split_cell divides body and reserve per channel by the tuned share, each
  cell develops the largest body its own genome builds from its part, and the
  rest joins its reserve, conserving every channel. Both plans carry the
  mutation; only the offspring expresses it. A split is refused, spending
  nothing and keeping the offer, below the tuned growth, without room beside
  the cell or at the region's organism cap.
- Stay or become: a choice (trace mutation byte 34) or a bud (step intent bit 2
  of byte 13) can hand control to the offspring; the parent becomes wildlife.
  Ownership now follows the account under rules 13, so validation and plan
  history accept a founder that became wildlife (ADR-0163 succession).
- Buds under rules 13: litter replaces the 25% twin (whole hundreds plus a
  chance, up to the tuned maximum, sharing one investment), the tuned bud fee is
  charged once and reclaimed, and births stop at the organism cap.
- New results split_applied and split_refused; owners below rules 13 refuse a
  become.

The revamp tuning file gains the reproduction keys. Tests cover the split's
conservation and refusals, stay and become splits with exact restore, a refused
second split, the cap, become validation and the litter count. Refreshes the DEV
session window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 22:19:48 -04:00

388 lines
19 KiB
C++

#include "evo/biology/budding.hpp"
#include "fixtures/cellular_ecology.hpp"
#include <fstream>
#include <iterator>
namespace {
using namespace evo;
using namespace evo::server;
using namespace evo::fixture;
std::vector<CellularLifeIntent> idle(const CellularLives& state, bool bud = false,
bool frozen = false) {
std::vector<CellularLifeIntent> result;
for (const auto& life : state.lives()) {
CellularLifeIntent input{life.identity.life, {}};
if (life.account != LifeAccount{}) {
input.intent.bud = bud && !frozen;
input.intent.frozen = frozen;
}
result.push_back(input);
}
return result;
}
const CellularOwnedLife& parent(const CellularLives& state) {
const auto found =
std::ranges::find(state.lives(), 100, [](const auto& life) { return life.identity.life; });
require(found != state.lives().end(), "missing replay parent");
return *found;
}
Owner old_owner(bool populated = true, std::uint32_t count = 1) {
auto state = opening(7, true, count, populated);
state = checked(state->migration_upgrade_candidate(), "replay boundary upgrade");
return checked(state->maturation_upgrade_candidate(), "replay maturation upgrade");
}
Owner inherited_owner() {
return checked(old_owner()->inheritance_upgrade_candidate(), "replay inheritance upgrade");
}
CellularMutationSelection await_structure(Owner& state, std::uint64_t successor) {
for (unsigned tick = 0; tick < 60'000; ++tick) {
const auto cell = parent(*state);
if (cell.mutation.offer) {
const auto& offer = *cell.mutation.offer;
const auto candidate = std::ranges::find_if(offer.candidates, [](const auto& value) {
return value.target == biology::Capability::membrane ||
value.target == biology::Capability::target_size;
});
const CellularMutationSelection choice{
100, cell.mutation.event_index, cell.mutation.offer_tick, successor,
candidate == offer.candidates.end() ? std::uint16_t{0} : candidate->operator_id};
if (choice.operator_id != 0)
return choice;
checked(state->advance_tick_with_mutations(idle(*state), std::span{&choice, 1}),
"replay fixture decline");
} else
checked(state->advance_tick_with_mutations(idle(*state), {}), "earn replay offer");
}
throw std::runtime_error("finite fixture failed to earn structural offer");
}
CellularBatch trace(const CellularLives& base, std::uint64_t count = 1) {
CellularBatch batch;
batch.format_version = cellular_batch_inheritance_format;
batch.header = {base.world(), base.region(), base.frame(), {},
{}, base.tick(), base.tick() + count};
batch.header.predecessor.fill(1);
batch.header.predecessor_digest.fill(2);
return batch;
}
struct Recorded {
CellularBatch batch;
Owner candidate;
std::vector<CellularMutationOutcome> outcomes;
std::vector<CellularBudExecution> buds{};
};
Recorded record(const CellularLives& base, CellularMutationSelection choice, bool bud,
std::uint64_t count = 1) {
Recorded result{trace(base, count), restore(base), {}};
for (std::uint64_t tick = 0; tick < count; ++tick) {
CellularTickIntents input{idle(*result.candidate, tick == 0 && bud, tick != 0), {}, {}};
if (tick == 0)
input.mutations.push_back(choice);
const auto executed =
checked(result.candidate->advance_tick_with_mutations(input.lives, input.mutations),
"direct recorded tick");
for (const auto& mutation : executed.mutations)
result.outcomes.push_back({result.candidate->tick(), mutation.life_id,
mutation.successor_id, mutation.result});
result.buds.insert(result.buds.end(), executed.buds.begin(), executed.buds.end());
require(result.candidate->current_total() == result.candidate->opening_total(),
"direct replay fixture did not close");
result.batch.ticks.push_back(std::move(input));
}
return result;
}
void prepare_birth(Owner& state, const CellularMutationSelection& choice) {
const auto expressed =
checked(state->archive().find(parent(*state).identity.genome), "birth expressed genome");
const auto plan =
checked(state->archive().find(parent(*state).offspring_genome), "birth inherited genome");
const auto offer = parent(*state).mutation.offer;
if (!offer)
throw std::runtime_error("birth offer missing");
const auto selected =
checked(biology::select_mutation(plan, offer->context, *offer, choice.operator_id,
choice.successor_id, choice.successor_id),
"birth selected genome");
for (unsigned tick = 0; tick < 30'000; ++tick) {
const auto& cell = parent(*state);
if (biology::fund_offspring(expressed, selected, cell.growth, cell.reserve,
state->ambient_temperature_millikelvin())) {
const auto probe = record(*state, choice, true);
if (std::ranges::any_of(probe.buds, [](const auto& bud) {
return bud.result == CellularBudResult::budded;
}))
return;
}
checked(state->advance_tick_with_mutations(idle(*state), {}), "fund recorded birth");
}
throw std::runtime_error("finite food never funded same-tick inherited birth");
}
void compare(const CellularLives& base, const Recorded& expected) {
const auto before = base.snapshot();
const auto encoded = checked(encode_cellular_batch(expected.batch), "encode inherited trace");
const auto decoded = checked(decode_cellular_batch(encoded), "decode inherited trace");
require(decoded == expected.batch &&
checked(encode_cellular_batch(decoded), "reencode trace") == encoded,
"trace canonical roundtrip");
std::vector<CellularMutationOutcome> outcomes{{999, 999, 999, CellularMutationResult::applied}};
const auto replayed = checked(replay_cellular_batch(base, decoded, outcomes), "plan replay");
require(replayed->snapshot() == expected.candidate->snapshot() &&
outcomes == expected.outcomes && base.snapshot() == before,
"recorded plans/outcomes differ from direct execution");
}
void put(std::span<std::uint8_t> bytes, std::size_t at, std::uint64_t value, unsigned width = 8) {
require(at <= bytes.size() && width <= bytes.size() - at, "test byte offset");
for (unsigned index = 0; index < width; ++index)
bytes[at + index] = static_cast<std::uint8_t>(value >> (index * 8U));
}
void action_checks(const CellularLives& base, const CellularBatch& batch) {
const auto& choice = batch.ticks.front().mutations.front();
const CellularMutationActionRecord action{
parent(base).account,
41,
base.tick() + 1,
{100, choice.event_index, choice.offer_tick, choice.operator_id}};
const auto block = checked(encode_cellular_mutation_action_block(
std::span{&action, 1}, base.tick(), batch.header.end_tick),
"encode inherited action");
const auto decoded =
checked(decode_cellular_mutation_action_block(block, base.tick(), batch.header.end_tick),
"decode inherited action");
require(match_cellular_mutation_actions_to_trace(base, batch, decoded).has_value(),
"inherited action did not match standing plan offer");
const auto rejects = [&](CellularMutationActionRecord wrong) {
require(!match_cellular_mutation_actions_to_trace(base, batch, std::span{&wrong, 1}),
"mismatched inherited action accepted");
};
auto wrong = action;
++wrong.account[0];
rejects(wrong);
wrong = action;
++wrong.input.life_id;
rejects(wrong);
wrong = action;
++wrong.input.event_index;
rejects(wrong);
wrong = action;
++wrong.input.offer_tick;
rejects(wrong);
wrong = action;
wrong.input.operator_id = 0xFFFF;
rejects(wrong);
wrong = action;
++wrong.tick;
rejects(wrong);
require(!match_cellular_mutation_actions_to_trace(base, batch, {}), "missing action accepted");
CellularActionRecord movement{parent(base).account, 42, {}};
put(movement.input, 0, base.tick() + 1);
movement.input[12] = batch.ticks.front().lives.front().intent.bud ? 3 : 0;
require(match_cellular_actions_to_trace(base, batch, std::span{&movement, 1}).has_value(),
"controlled intent action mismatch");
put(movement.input, 8, 1000, 2);
require(!match_cellular_actions_to_trace(base, batch, std::span{&movement, 1}),
"changed movement action matched");
}
void refuse(const CellularLives& base, const CellularBatch& batch, CellularBatchError error) {
const auto before = base.snapshot();
std::vector<CellularMutationOutcome> outcomes{{999, 999, 999, CellularMutationResult::applied}};
const auto replayed = replay_cellular_batch(base, batch, outcomes);
if (replayed)
throw std::runtime_error("invalid inherited replay accepted");
require(replayed.error() == error && outcomes.empty() && base.snapshot() == before,
"failed replay changed predecessor or exposed partial outcomes");
}
void version_checks(const CellularLives& base, const CellularBatch& batch) {
for (std::uint32_t version = 1; version <= 3; ++version) {
auto old_trace = batch;
old_trace.format_version = version;
refuse(base, old_trace, CellularBatchError::version);
require(!match_cellular_actions_to_trace(base, old_trace, {}) &&
!match_cellular_mutation_actions_to_trace(base, old_trace, {}),
"action matcher admitted old trace on new owner");
}
auto old = old_owner();
auto empty = trace(*old);
empty.ticks.push_back({idle(*old), {}, {}});
refuse(*old, empty, CellularBatchError::version);
require(!match_cellular_actions_to_trace(*old, empty, {}) &&
!match_cellular_mutation_actions_to_trace(*old, empty, {}),
"action matcher admitted new trace on old owner");
empty.format_version = cellular_batch_format;
require(replay_cellular_batch(*old, empty).has_value(), "old replay control failed");
empty.format_version = 5;
refuse(*old, empty, CellularBatchError::version);
require(!encode_cellular_batch(empty), "unknown trace encoded");
auto scope = batch;
++scope.header.region;
refuse(base, scope, CellularBatchError::scope);
auto malformed = batch;
malformed.header.end_tick = malformed.header.begin_tick;
refuse(base, malformed, CellularBatchError::format);
}
void codec_checks(const CellularBatch& batch) {
const auto bytes = checked(encode_cellular_batch(batch), "hostile trace opening");
const auto corrupt = [&](std::size_t at, std::uint64_t value, unsigned width = 4) {
auto altered = bytes;
put(altered, at, value, width);
require(!decode_cellular_batch(altered), "malformed trace accepted");
};
corrupt(8, 5);
corrupt(12, 5);
corrupt(104, 65);
corrupt(108, 1);
corrupt(112, 0xFFFF'FFFFULL);
corrupt(124, 1);
const auto selection = 112 + 16 + batch.ticks.front().lives.size() * 24;
corrupt(selection + 34, 2, 1); // Byte 34 is the rules-13 become flag: 0 or 1.
for (const auto length : {std::size_t{0}, std::size_t{111}, std::size_t{127}, bytes.size() - 1})
require(!decode_cellular_batch(std::span{bytes}.first(length)), "truncated trace accepted");
auto trailing = bytes;
trailing.push_back(0);
require(!decode_cellular_batch(trailing), "trailing trace byte accepted");
}
void selected_replay() {
auto base = inherited_owner();
const auto choice = await_structure(base, 1'000'000);
// Retain the earned offer while acquiring actual finite food until birth is affordable.
prepare_birth(base, choice);
const auto proof = record(*base, choice, true, 64);
compare(*base, proof);
require(proof.outcomes.size() == 1 &&
proof.outcomes.front().result == CellularMutationResult::offspring_plan_applied &&
parent(*proof.candidate).identity.genome == parent(*base).identity.genome &&
parent(*proof.candidate).offspring_genome == choice.successor_id,
"recorded selection rebuilt parent or lost plan");
bool born = false;
for (const auto& life : proof.candidate->lives())
if (life.parent_life == 100) {
require(life.identity.genome == choice.successor_id &&
life.offspring_genome == choice.successor_id &&
life.identity.born.value == base->tick() + 1,
"same-tick offspring used predecessor plan");
born = true;
}
if (!born)
throw std::runtime_error("same-tick birth result: " +
(proof.buds.empty() ? std::string{"missing"}
: std::to_string(static_cast<unsigned>(
proof.buds.front().result))));
action_checks(*base, proof.batch);
version_checks(*base, proof.batch);
codec_checks(proof.batch);
auto broken = proof.batch;
broken.ticks[1].lives.front().life_id = 9999;
const auto canonical =
checked(decode_cellular_batch(checked(encode_cellular_batch(broken), "encode bad set")),
"decode bad set");
refuse(*base, canonical, CellularBatchError::state);
auto stale = proof.batch;
++stale.ticks.front().mutations.front().event_index;
refuse(*base, stale, CellularBatchError::state);
auto later = restore(*proof.candidate);
const auto next = await_structure(later, 1'000'001);
const auto offer = parent(*later).mutation.offer;
if (!offer)
throw std::runtime_error("second inherited offer missing");
require(offer->parent != parent(*later).identity.genome,
"second offer did not differ from expressed body");
const auto changed = record(*later, next, false);
compare(*later, changed);
action_checks(*later, changed.batch);
auto decline = next;
decline.operator_id = 0;
const auto declined = record(*later, decline, false);
compare(*later, declined);
action_checks(*later, declined.batch);
require(declined.outcomes.front().result == CellularMutationResult::declined &&
parent(*declined.candidate).offspring_genome == parent(*later).offspring_genome &&
!declined.candidate->archive().find(decline.successor_id),
"decline edited inherited plan");
}
void allocator_births() {
auto base = checked(old_owner(false, 2)->inheritance_upgrade_candidate(), "empty upgrade");
auto direct = restore(*base);
auto batch = trace(*base, 2);
for (std::uint64_t index = 0; index < 2; ++index) {
CellularTickIntents tick{idle(*direct), {}, {}};
LifeAccount account{};
account.back() = static_cast<std::uint8_t>(index + 1);
tick.births.push_back({account, 1, 1, 100 + index});
checked(direct->advance_tick_with_mutations(tick.lives, {}), "allocated birth tick");
const auto id = 100 + index;
direct = checked(direct->birth_candidate({account, 1}, {id, id, id, id, {direct->tick()}}),
"direct allocator birth");
batch.ticks.push_back(std::move(tick));
}
compare(*base, Recorded{batch, std::move(direct), {}});
auto bytes = checked(encode_cellular_batch(batch), "allocated birth trace");
put(bytes, bytes.size() - 8, 100); // Duplicate the first tick's reserved identity.
require(!decode_cellular_batch(bytes), "cross-tick identity reuse decoded");
auto old = old_owner(false, 2);
batch.format_version = cellular_batch_legacy_format;
require(!encode_cellular_batch(batch), "legacy trace encoded births");
refuse(*old, batch, CellularBatchError::format);
}
void save(const std::string& path, std::span<const std::uint8_t> bytes) {
std::ofstream file(path, std::ios::binary | std::ios::trunc);
file.write(reinterpret_cast<const char*>(bytes.data()),
static_cast<std::streamsize>(bytes.size()));
require(file.good(), "write replay fixture");
}
std::vector<std::uint8_t> read(const std::string& path, std::size_t bound) {
std::ifstream file(path, std::ios::binary);
require(file.good(), "read replay fixture");
std::vector<std::uint8_t> bytes{std::istreambuf_iterator<char>{file}, {}};
require(bytes.size() <= bound, "replay fixture bound");
return bytes;
}
void checkpoint(const char* path, const char* expected) {
auto base = inherited_owner();
const auto initial = await_structure(base, 1'000'000);
auto first = record(*base, initial, false);
base = std::move(first.candidate);
const auto choice = await_structure(base, 1'000'001);
prepare_birth(base, choice);
const auto proof = record(*base, choice, true, 64);
compare(*base, proof);
action_checks(*base, proof.batch);
save(path, base->snapshot());
save(std::string{path} + ".trace", checked(encode_cellular_batch(proof.batch), "save trace"));
save(expected, proof.candidate->snapshot());
}
void resume(const char* path, const char* output) {
const auto bytes = read(path, max_cellular_lives_bytes);
require(bytes.size() >= 324, "replay predecessor length");
std::uint64_t tick = 0;
for (unsigned offset = 0; offset < 8; ++offset)
tick |= std::uint64_t{bytes[40 + offset]} << (offset * 8U);
const auto base = checked(CellularLives::restore(bytes, 207, 1, 1, tick), "fresh replay base");
const auto batch =
checked(decode_cellular_batch(read(std::string{path} + ".trace", max_cellular_batch_bytes)),
"fresh trace decode");
action_checks(*base, batch);
std::vector<CellularMutationOutcome> outcomes;
const auto candidate = checked(replay_cellular_batch(*base, batch, outcomes), "fresh replay");
require(outcomes.size() == 1 &&
outcomes.front().result == CellularMutationResult::offspring_plan_applied,
"fresh replay lost selected outcome");
save(output, candidate->snapshot());
}
} // namespace
int main(int argc, char** argv) try {
if (argc == 4 && std::string_view{argv[1]} == "--checkpoint") {
checkpoint(argv[2], argv[3]);
return 0;
}
if (argc == 4 && std::string_view{argv[1]} == "--resume") {
resume(argv[2], argv[3]);
return 0;
}
require(argc == 1, "unknown inherited replay arguments");
selected_replay();
allocator_births();
return 0;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}