Files
aevolution/tests/cellular_session_batch_tests.cpp
dingoandClaude Opus 5.5 9de55e352d
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (checks) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / postgres-tools (push) Successful in 1m39s
Foundation / server-image (push) Successful in 5m19s
Foundation / image (push) Successful in 6m54s
fix(cellular): one action per account a tick, never a refused tick
Region 16 playtest: a bud showed and was then undone, and several buds were
refused. The client scheduled a discrete action at server tick + lead, but
movement runs ahead of that once the lead shrinks (each movement takes the
tick after the last one sent), so a bud could name a tick a movement already
had. A tick takes one action per account: the region refused the whole tick
and dropped every player's actions on it ("tick stalled ... error=4").

- Server: keep_one_action_per_account keeps each account's discrete action
  (engulf, bud, become, emerge), else its newest, and refuses only the rest.
- Client: a discrete action is scheduled after every tick already sent, and
  movement is held off the tick a queued discrete action owns.
- Failed background commits now log their category, publication error,
  store error and SQLSTATE (never server text), to diagnose ok=0 commits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 08:31:16 -04:00

587 lines
29 KiB
C++

#include "evo/server/cellular_session_batch.hpp"
#include <array>
#include <cstdint>
#include <cstdio>
#include <source_location>
#include <stdexcept>
#include <string>
#include <vector>
namespace {
using namespace evo;
using namespace evo::server;
void check(bool value, std::source_location at = std::source_location::current()) {
if (!value)
throw std::runtime_error("cellular session batch line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
accounting::Inventory founder_price(std::uint64_t count) {
const auto endowment = biology::primitive_endowment();
CHECK(endowment);
auto funding = endowment->body;
funding.energy.value = endowment->reserve.energy.value + endowment->construction.value;
funding.energy.value *= count;
for (auto& channel : funding.matter)
channel.value *= count;
return funding;
}
void actions_bind_to_owned_life_and_preserve_other_inputs() {
auto primitive = PrimitiveLives::create({90, 1, 7, 17, founder_price(2), {{0, 0}, {20, 0}}});
CHECK(primitive);
LifeRequest request{};
request.account.back() = 1;
request.action = 1;
CHECK((*primitive)->spawn(request, {1, 101, 201, 301, {1}}));
request.account.back() = 2;
CHECK((*primitive)->spawn(request, {2, 102, 202, 302, {1}}));
biology::CellularHabitatConfig habitat_config{
90,
1,
7,
20'000,
20'000,
{{{0, 0}, 29, {{10}, {{{10}, {10}, {10}, {10}}}}},
{{15'000, 15'000}, 30, {{10}, {{{10}, {10}, {10}, {10}}}}}}};
auto habitat = biology::CellularHabitat::create(std::move(habitat_config));
CHECK(habitat);
auto state =
CellularLives::create(**primitive, std::move(*habitat), {{20'000, 20'000}, 1, 293'150});
CHECK(state && (*state)->lives().size() == 2);
const auto& controlled = (*state)->lives()[0];
const auto& other = (*state)->lives()[1];
const auto visible = (*state)->perceive(controlled.identity.life);
CHECK(visible && visible->size() == 1 &&
visible->front().address == (biology::CellularPatchAddress{0, 0}));
const std::array baseline{CellularLifeIntent{controlled.identity.life, {{5, 6}, {}}},
CellularLifeIntent{other.identity.life, {{7, 8}, {}}}};
const auto input = network::encode_cellular_action_input({2, -3, 4, true, 0, 0});
network::CarriedAction action{network::AccountId{controlled.account}, 77, 2,
network::ActionKind::cellular, input};
const std::array actions{action};
const auto prepared = prepare_cellular_session_tick(**state, 2, baseline, actions);
CHECK(prepared && prepared->intents.lives.size() == 2 && prepared->actions.size() == 1);
CHECK(prepared->intents.lives[0] ==
CellularLifeIntent{controlled.identity.life,
{{-3, 4}, biology::CellularPatchAddress{0, 0}}});
CHECK(prepared->intents.lives[1] == baseline[1]);
CHECK(prepared->actions[0] == CellularActionRecord{controlled.account, 77, input});
const auto guessed_input =
network::encode_cellular_action_input({2, 0, 0, true, 15'000, 15'000});
const network::CarriedAction guessed{network::AccountId{controlled.account}, 78, 2,
network::ActionKind::cellular, guessed_input};
const std::array guessed_actions{guessed};
CHECK(prepare_cellular_session_tick(**state, 2, baseline, guessed_actions).error() ==
CellularSessionBatchError::unperceived_target);
// ADR-0124: an engulf action becomes the life's engulf intent, and the trace keeps it.
const auto engulf_input = network::encode_cellular_action_input({2, 1, 0, false, 0, 0, true});
const network::CarriedAction engulf_action{network::AccountId{controlled.account}, 79, 2,
network::ActionKind::cellular, engulf_input};
const std::array engulf_actions{engulf_action};
const auto engulfing = prepare_cellular_session_tick(**state, 2, baseline, engulf_actions);
CHECK(engulfing && engulfing->intents.lives[0].intent.engulf &&
!engulfing->intents.lives[0].intent.intake_target);
CellularBatch engulf_batch;
engulf_batch.header = {(*state)->world(), (*state)->region(), (*state)->frame(), {}, {}, 1, 2};
engulf_batch.header.predecessor.fill(1);
engulf_batch.header.predecessor_digest.fill(2);
engulf_batch.ticks.push_back(engulfing->intents);
CHECK(match_cellular_actions_to_trace(**state, engulf_batch, engulfing->actions));
const auto engulf_bytes = encode_cellular_batch(engulf_batch);
CHECK(engulf_bytes);
const auto engulf_decoded = decode_cellular_batch(*engulf_bytes);
CHECK(engulf_decoded && *engulf_decoded == engulf_batch);
auto stripped = engulf_batch;
stripped.ticks[0].lives[0].intent.engulf = false;
CHECK(!match_cellular_actions_to_trace(**state, stripped, engulfing->actions));
// ADR-0125: a bud action becomes the life's bud intent, kept as kind 3 in the trace.
const auto bud_input =
network::encode_cellular_action_input({2, 0, 0, false, 0, 0, false, true});
const network::CarriedAction bud_action{network::AccountId{controlled.account}, 80, 2,
network::ActionKind::cellular, bud_input};
const std::array bud_actions{bud_action};
const auto budding = prepare_cellular_session_tick(**state, 2, baseline, bud_actions);
CHECK(budding && budding->intents.lives[0].intent.bud &&
!budding->intents.lives[0].intent.engulf &&
!budding->intents.lives[0].intent.intake_target);
auto bud_batch = engulf_batch;
bud_batch.ticks[0] = budding->intents;
CHECK(match_cellular_actions_to_trace(**state, bud_batch, budding->actions));
const auto bud_bytes = encode_cellular_batch(bud_batch);
CHECK(bud_bytes);
const auto bud_decoded = decode_cellular_batch(*bud_bytes);
CHECK(bud_decoded && *bud_decoded == bud_batch);
auto unbudded = bud_batch;
unbudded.ticks[0].lives[0].intent.bud = false;
CHECK(!match_cellular_actions_to_trace(**state, unbudded, budding->actions));
CellularBatch batch;
batch.header = {(*state)->world(), (*state)->region(), (*state)->frame(), {}, {}, 1, 2};
batch.header.predecessor.fill(1);
batch.header.predecessor_digest.fill(2);
batch.ticks.push_back(prepared->intents);
CHECK(match_cellular_actions_to_trace(**state, batch, prepared->actions));
persistence::RegionHead head{};
head.frame = (*state)->frame();
head.sequence = 4;
head.end_tick = (*state)->tick();
head.commit.fill(1);
head.digest.fill(2);
auto builder = CellularSessionBatchBuilder::create(**state, head);
CHECK(builder);
CHECK(builder->append(2, baseline, actions));
std::vector<CellularLifeIntent> next_baseline;
for (const auto& life : builder->candidate().lives())
next_baseline.push_back({life.identity.life, {}});
CHECK(builder->append(3, next_baseline, {}));
CHECK(builder->batch().header.begin_tick == 1 && builder->batch().header.end_tick == 3 &&
builder->batch().ticks.size() == 2);
CHECK(builder->actions().size() == 1 && builder->actions()[0] == prepared->actions[0]);
const auto replayed = replay_cellular_batch(**state, builder->batch());
CHECK(replayed && (*replayed)->snapshot() == builder->candidate().snapshot());
const auto repeated_input = network::encode_cellular_action_input({4, 0, 0, false, 0, 0});
const network::CarriedAction repeated{network::AccountId{controlled.account}, 77, 4,
network::ActionKind::cellular, repeated_input};
const std::array repeated_actions{repeated};
CHECK(builder->append(4, next_baseline, repeated_actions).error() ==
CellularSessionBatchError::duplicate_action);
CHECK(builder->append(5, next_baseline, {}).error() == CellularSessionBatchError::tick);
auto wrong_head = head;
++wrong_head.end_tick;
CHECK(CellularSessionBatchBuilder::create(**state, wrong_head).error() ==
CellularSessionBatchError::head);
auto altered = baseline;
altered[1].intent.movement.x = 10;
const auto preserved = prepare_cellular_session_tick(**state, 2, altered, actions);
CHECK(preserved && preserved->intents.lives[1].intent.movement.x == 10);
CHECK(prepare_cellular_session_tick(**state, 3, baseline, actions).error() ==
CellularSessionBatchError::tick);
CHECK(
prepare_cellular_session_tick(**state, 2, std::span{baseline}.first(1), actions).error() ==
CellularSessionBatchError::intents);
const std::array duplicate{action, action};
CHECK(prepare_cellular_session_tick(**state, 2, baseline, duplicate).error() ==
CellularSessionBatchError::duplicate_action);
// Two actions of one account on one tick: the discrete one is kept, even when older,
// so the tick is not refused for every player; between movements the newest is kept.
{
const network::CarriedAction bud{network::AccountId{controlled.account}, 80, 2,
network::ActionKind::cellular, bud_input};
auto later = action;
later.action_id = 90;
std::vector<network::CarriedAction> both{bud, later};
const auto superseded = keep_one_action_per_account(both);
CHECK(both.size() == 1 && both[0] == bud && superseded.size() == 1 &&
superseded[0] == later);
CHECK(prepare_cellular_session_tick(**state, 2, baseline, both));
std::vector<network::CarriedAction> moves{action, later};
CHECK(keep_one_action_per_account(moves) == std::vector{action} && moves.size() == 1 &&
moves[0] == later);
std::vector<network::CarriedAction> single{action};
CHECK(keep_one_action_per_account(single).empty() && single.size() == 1);
}
auto unknown = action;
unknown.account.bytes.back() = 3;
const std::array unknown_actions{unknown};
CHECK(prepare_cellular_session_tick(**state, 2, baseline, unknown_actions).error() ==
CellularSessionBatchError::life);
auto malformed = action;
malformed.cellular_input = std::array<std::uint8_t, network::cellular_action_input_bytes>{};
const std::array malformed_actions{malformed};
CHECK(prepare_cellular_session_tick(**state, 2, baseline, malformed_actions).error() ==
CellularSessionBatchError::action);
}
void durable_mutation_reservations_bind_to_the_exact_offer() {
auto primitive = PrimitiveLives::create({93, 1, 1, 17, founder_price(1), {{0, 0}}});
CHECK(primitive);
LifeRequest request{};
request.account.back() = 3;
request.action = 1;
CHECK((*primitive)->spawn(request, {1, 101, 201, 301, {1}}));
accounting::Inventory stock{{1'000'000'000'000ULL},
{{{1'000'000'000'000ULL},
{1'000'000'000'000ULL},
{1'000'000'000'000ULL},
{1'000'000'000'000ULL}}}};
auto habitat =
biology::CellularHabitat::create({93, 1, 1, 1'000, 1'000, {{{0, 0}, 29, stock}}});
CHECK(habitat);
auto created =
CellularLives::create(**primitive, std::move(*habitat), {{1'000, 1'000}, 1, 293'150});
CHECK(created && (*created)->lives().size() == 1);
auto& owner = **created;
const auto life_id = owner.lives()[0].identity.life;
bool offered = false;
for (std::size_t tick = 0; tick < 10'000 && !offered; ++tick) {
const std::array intent{CellularLifeIntent{life_id, {{0, 0}, {{0, 0}}}}};
CHECK(owner.advance_tick(intent));
const auto current_offer = owner.mutation_offer(life_id);
CHECK(current_offer);
offered = current_offer->has_value();
}
CHECK(offered);
auto revision_owner = owner.migrate_to_v3();
CHECK(revision_owner);
const auto offer = (*revision_owner)->mutation_offer(life_id);
CHECK(offer && offer->has_value());
const auto offered_candidates = offer->value_or(biology::MutationOffer{});
CHECK(!offered_candidates.candidates.empty());
const auto& live = (*revision_owner)->lives()[0];
const auto action_tick = (*revision_owner)->tick() + 1;
const auto& candidate = offered_candidates.candidates.front();
persistence::CellularMutationReservation reservation;
reservation.account = live.account;
reservation.action_id = 42;
reservation.region = (*revision_owner)->region();
reservation.generation = 7;
reservation.target_tick = action_tick;
reservation.successor_identity = 999;
const auto put_field = [&](std::size_t offset, std::uint64_t value, std::size_t bytes) {
for (std::size_t i = 0; i < bytes; ++i)
reservation.input[offset + i] = static_cast<std::uint8_t>(value >> (i * 8U));
};
put_field(0, action_tick, 8);
put_field(8, life_id, 8);
put_field(16, live.mutation.event_index, 8);
put_field(24, live.mutation.offer_tick, 8);
put_field(32, candidate.operator_id, 2);
persistence::RegionHead head{};
head.frame = (*revision_owner)->frame();
head.sequence = 3;
head.end_tick = (*revision_owner)->tick();
head.commit.fill(0x31);
head.digest.fill(0x41);
auto builder = CellularSessionBatchBuilder::create(**revision_owner, head);
CHECK(builder);
const std::array baseline{CellularLifeIntent{life_id, {}}};
const std::array reservations{reservation};
CHECK(builder->append(action_tick, baseline, {}, {}, reservations));
const CellularMutationActionRecord expected_action{
live.account,
reservation.action_id,
action_tick,
{life_id, live.mutation.event_index, live.mutation.offer_tick, candidate.operator_id}};
const CellularMutationSelection expected_selection{
life_id, live.mutation.event_index, live.mutation.offer_tick,
reservation.successor_identity, candidate.operator_id};
CHECK(builder->batch().ticks.back().mutations.size() == 1 &&
builder->batch().ticks.back().mutations.front() == expected_selection &&
builder->mutation_actions().size() == 1 &&
builder->mutation_actions().front() == expected_action &&
match_cellular_mutation_actions_to_trace(**revision_owner, builder->batch(),
builder->mutation_actions()));
const auto replayed = replay_cellular_batch(**revision_owner, builder->batch());
CHECK(replayed && (*replayed)->snapshot() == builder->candidate().snapshot());
auto stale_builder = CellularSessionBatchBuilder::create(**revision_owner, head);
CHECK(stale_builder);
auto stale = reservation;
stale.input[24] ^= 1U;
const std::array stale_reservations{stale};
CHECK(stale_builder->append(action_tick, baseline, {}, {}, stale_reservations).error() ==
CellularSessionBatchError::mutation);
CHECK(stale_builder->candidate().snapshot() == (*revision_owner)->snapshot());
// ADR-0121 amendment (schema 59): the queue is durable. A choice binds at a tick after
// its reservation's, recording the tick it bound at; it may not bind before it.
auto late_builder = CellularSessionBatchBuilder::create(**revision_owner, head);
CHECK(late_builder);
auto early = reservation;
early.target_tick = action_tick + 1;
early.input[0] = static_cast<std::uint8_t>(early.target_tick);
for (std::size_t i = 1; i < 8; ++i)
early.input[i] = static_cast<std::uint8_t>(early.target_tick >> (i * 8U));
const std::array early_reservations{early};
CHECK(late_builder->append(action_tick, baseline, {}, {}, early_reservations).error() ==
CellularSessionBatchError::mutation);
CHECK(late_builder->append(action_tick, baseline, {}, {}, {}));
CHECK(late_builder->append(action_tick + 1, baseline, {}, {}, reservations));
CHECK(late_builder->mutation_actions().size() == 1 &&
late_builder->mutation_actions().front().tick == action_tick + 1 &&
late_builder->batch().ticks.back().mutations.size() == 1 &&
match_cellular_mutation_actions_to_trace(**revision_owner, late_builder->batch(),
late_builder->mutation_actions()));
const auto late_replay = replay_cellular_batch(**revision_owner, late_builder->batch());
CHECK(late_replay && (*late_replay)->snapshot() == late_builder->candidate().snapshot());
// A choice whose life has ended binds as life_died and changes nothing.
auto departed_builder = CellularSessionBatchBuilder::create(**revision_owner, head);
CHECK(departed_builder);
auto departed = reservation;
const auto gone = life_id + 1'000;
for (std::size_t i = 0; i < 8; ++i)
departed.input[8 + i] = static_cast<std::uint8_t>(gone >> (i * 8U));
const std::array departed_reservations{departed};
CHECK(departed_builder->append(action_tick, baseline, {}, {}, departed_reservations));
CHECK(departed_builder->mutation_actions().size() == 1 &&
departed_builder->mutation_actions().front().input.life_id == gone &&
match_cellular_mutation_actions_to_trace(**revision_owner, departed_builder->batch(),
departed_builder->mutation_actions()));
std::vector<CellularMutationOutcome> departed_outcomes;
const auto departed_replay =
replay_cellular_batch(**revision_owner, departed_builder->batch(), departed_outcomes);
CHECK(departed_replay &&
(*departed_replay)->snapshot() == departed_builder->candidate().snapshot() &&
departed_outcomes.size() == 1 && departed_outcomes.front().life_id == gone &&
departed_outcomes.front().result == CellularMutationResult::life_died &&
(*departed_replay)->lives()[0].mutation.offer.has_value());
}
// Rules 13 (session v14): emerge, become and exclusions reach the owner's intents and the trace
// check, and a catalog choice with become binds to the revamp offer through its reservation.
std::unique_ptr<CellularLives> revamp_owner() {
const auto endowment = biology::primitive_endowment(biology::primitive_founder_v3);
CHECK(endowment);
auto funding = endowment->body;
funding.energy.value = 2 * (endowment->reserve.energy.value + endowment->construction.value);
for (auto& channel : funding.matter)
channel.value *= 2;
auto primitive =
PrimitiveLives::create({94, 1, 1, 17, funding, {{1'000, 1'000}, {1'000, 4'000}}});
CHECK(primitive);
biology::CellularHabitatConfig config{94, 1, 1, 48'000, 48'000, {}};
config.format = biology::cellular_habitat_deposit_format;
auto habitat = biology::CellularHabitat::create(std::move(config));
CHECK(habitat);
auto created =
CellularLives::create(**primitive, std::move(*habitat), {{48'000, 48'000}, 0, 293'150},
CellularLivesProfile::foraging);
CHECK(created);
auto state = std::move(*created);
CHECK(state->advance_tick(std::span<const CellularLifeIntent>{}));
const auto step = [&](auto next) {
CHECK(next);
state = std::move(*next);
};
step(state->migration_upgrade_candidate());
step(state->maturation_upgrade_candidate());
step(state->inheritance_upgrade_candidate());
step(state->photosynthesis_upgrade_candidate({0, 1, {0}}));
step(state->liquid_upgrade_candidate(
{.currents = {5, 48'000, 48'000, 250, 2, 4, 3'000, 6'000, 60, 300},
.founder = {.thrust_q10 = 20'480,
.reverse_thrust_q10 = 20'480,
.forward_drag_ppm = 95'163,
.lateral_drag_ppm = 95'163,
.turn = 3'276},
.deposits = {.drift_radius_um = 200,
.max_drift_q10 = 100 * 1'024,
.drift_decay_ppm = 95'163,
.merged_cap = 8,
.bubble_radius_um = biology::cellular_mote_radius_um}}));
CellularRevampRules rules{.burn_pj_per_kfg = {2'000, 2'000, 2'000, 2'000},
.step = {.membrane_collection_per_mille_per_point = 2,
.engulf_ratio_milli = 1'500,
.engulf_reserve_percent = 100,
.kill_bubble_per_thousand = 500},
.drip_interval_ticks = 20,
.drip_bubble_pj = 20'000'000,
.drip_bubble_fg = 1'000'000,
.reserve_load_per_thousand = 1'000};
rules.offer_interval_ticks = 40;
step(state->revamp_upgrade_candidate(rules));
LifeRequest request{};
request.account.back() = 2;
request.action = 1;
step(state->birth_candidate(request, {101, 101, 101, 101, {state->tick()}}));
return state;
}
void rules_13_actions_and_choices_bind() {
auto state = revamp_owner();
for (int tick = 0; tick < 40; ++tick) {
const std::array swim{CellularLifeIntent{101, {{1'000, 0}, {}}}};
CHECK(state->advance_tick(swim));
}
const auto& life = state->lives().front();
CHECK(life.identity.life == 101 && life.revamp.offer.count == 3);
const auto tick = state->tick() + 1;
const std::array baseline{CellularLifeIntent{101, {}}};
// Emerge and exclusions travel in the canonical input and must match the trace.
const auto input = network::encode_cellular_action_input(
{tick, 300, 0, false, 0, 0, false, false, false, false, true, 0b0101});
const std::array actions{network::CarriedAction{network::AccountId{life.account}, 90, tick,
network::ActionKind::cellular, input}};
const auto prepared = prepare_cellular_session_tick(*state, tick, baseline, actions);
CHECK(prepared && prepared->intents.lives[0].intent.emerge &&
prepared->intents.lives[0].intent.exclude == 0b0101);
CellularBatch batch;
batch.format_version = cellular_batch_inheritance_format;
batch.header = {state->world(), state->region(), state->frame(), {}, {}, state->tick(), tick};
batch.header.predecessor.fill(1);
batch.header.predecessor_digest.fill(2);
batch.ticks.push_back(prepared->intents);
CHECK(match_cellular_actions_to_trace(*state, batch, prepared->actions));
auto kept = batch;
kept.ticks[0].lives[0].intent.exclude = 0;
CHECK(!match_cellular_actions_to_trace(*state, kept, prepared->actions));
// A catalog choice with become (bit 15 of the stored operator).
persistence::CellularMutationReservation reservation;
reservation.account = life.account;
reservation.action_id = 43;
reservation.region = state->region();
reservation.generation = 7;
reservation.target_tick = tick;
reservation.successor_identity = 9'500;
const auto put_field = [&](std::size_t offset, std::uint64_t value, std::size_t bytes) {
for (std::size_t i = 0; i < bytes; ++i)
reservation.input[offset + i] = static_cast<std::uint8_t>(value >> (i * 8U));
};
const auto chosen = life.revamp.offer.ids.at(0);
put_field(0, tick, 8);
put_field(8, 101, 8);
put_field(16, life.revamp.offer.event_index, 8);
put_field(24, life.revamp.offer.offer_tick, 8);
put_field(32, chosen | 0x8000U, 2);
persistence::RegionHead head{};
head.frame = state->frame();
head.sequence = 3;
head.end_tick = state->tick();
head.commit.fill(0x31);
head.digest.fill(0x41);
auto builder = CellularSessionBatchBuilder::create(*state, head);
CHECK(builder);
const std::array reservations{reservation};
CHECK(builder->append(tick, baseline, {}, {}, reservations));
const auto& selection = builder->batch().ticks.back().mutations.front();
CHECK(selection.become && selection.operator_id == chosen &&
builder->mutation_actions().front().input.become &&
encode_cellular_mutation_input(builder->mutation_actions().front())[33] >= 0x80 &&
match_cellular_mutation_actions_to_trace(*state, builder->batch(),
builder->mutation_actions()));
std::vector<CellularMutationOutcome> outcomes;
const auto replayed = replay_cellular_batch(*state, builder->batch(), outcomes);
CHECK(replayed && (*replayed)->snapshot() == builder->candidate().snapshot() &&
outcomes.size() == 1 && outcomes.front().result == CellularMutationResult::split_applied);
// The player became the offspring.
const auto& lives = builder->candidate().lives();
CHECK(std::ranges::find(lives, life.account, &CellularOwnedLife::account)->identity.life !=
101);
// Become with a decline is refused.
auto declined = reservation;
declined.input[32] = 0;
declined.input[33] = 0x80;
auto refusing = CellularSessionBatchBuilder::create(*state, head);
CHECK(refusing);
const std::array declines{declined};
CHECK(refusing->append(tick, baseline, {}, {}, declines).error() ==
CellularSessionBatchError::mutation);
}
void builder_batches_reserved_birth_after_empty_tick() {
auto primitive = PrimitiveLives::create({93, 1, 1, 17, founder_price(1), {{0, 0}}});
CHECK(primitive);
auto habitat = biology::CellularHabitat::create({93, 1, 1, 1'000, 1'000, {}});
CHECK(habitat);
auto state =
CellularLives::create(**primitive, std::move(*habitat), {{1'000, 1'000}, 0, 293'150});
CHECK(state && (*state)->lives().empty());
persistence::RegionHead head{};
head.frame = 1;
head.commit.fill(1);
head.digest.fill(2);
auto builder = CellularSessionBatchBuilder::create(**state, head);
CHECK(builder);
CellularTickIntents::Birth birth{};
birth.account.back() = 8;
birth.request = 13;
birth.generation = 4;
birth.identity = 901;
const std::array births{birth};
const std::span<const CellularLifeIntent> no_lives{};
CHECK(builder->append(1, no_lives, {}, births));
CHECK(builder->candidate().tick() == 1 && builder->candidate().lives().size() == 1 &&
builder->batch().ticks[0].births == std::vector{birth} && (*state)->lives().empty());
const auto replayed = replay_cellular_batch(**state, builder->batch());
CHECK(replayed && (*replayed)->snapshot() == builder->candidate().snapshot());
const auto snapshot = builder->candidate().snapshot();
const std::array next_lives{CellularLifeIntent{901, {}}};
CHECK(builder->append(2, next_lives, {}, births).error() ==
CellularSessionBatchError::duplicate_birth);
CHECK(builder->candidate().snapshot() == snapshot && builder->batch().ticks.size() == 1);
}
void unavailable_birth_does_not_freeze_the_tick() {
for (const bool funded : {false, true}) {
auto primitive =
PrimitiveLives::create({94, 1, 1, 17, founder_price(funded ? 2 : 1), {{0, 0}}});
CHECK(primitive);
LifeRequest request{};
request.account.back() = 1;
request.action = 1;
CHECK((*primitive)->spawn(request, {101, 101, 101, 101, {1}}));
auto habitat = biology::CellularHabitat::create({94, 1, 1, 1000, 1000, {}});
CHECK(habitat);
auto state =
CellularLives::create(**primitive, std::move(*habitat), {{1000, 1000}, 1, 293150});
CHECK(state);
persistence::RegionHead head{};
head.frame = 1;
head.end_tick = 1;
head.commit.fill(1);
head.digest.fill(2);
CellularTickIntents::Birth birth{};
birth.account.back() = 2;
birth.request = 1;
birth.generation = 1;
birth.identity = 102;
const std::array births{birth};
// Movement vacates the nursery in the underfunded case, isolating each refusal.
const std::array baseline{
CellularLifeIntent{101, {{static_cast<std::int16_t>(funded ? 0 : 1000), 0}, {}}}};
// A founder moves only a few um per tick: first move it fully away from the site.
if (!funded) {
for (int i = 0; i < 100; ++i)
CHECK((*state)->advance_tick(baseline));
head.end_tick = (*state)->tick();
}
auto strict = CellularSessionBatchBuilder::create(**state, head);
CHECK(strict);
CHECK(strict->append(head.end_tick + 1, baseline, {}, births).error() ==
CellularSessionBatchError::invalid_birth);
CHECK(strict->candidate().tick() == head.end_tick);
auto builder = CellularSessionBatchBuilder::create(**state, head);
CHECK(builder &&
builder->append_with_birth_refusals(head.end_tick + 1, baseline, {}, births));
CHECK(builder->refused_births().size() == 1 && builder->refused_births()[0].birth == birth);
CHECK(
builder->refused_births()[0].reason ==
(funded ? CellularLivesError::no_placement : CellularLivesError::insufficient_funding));
CHECK(builder->candidate().tick() == head.end_tick + 1 &&
builder->candidate().lives().size() == 1);
CHECK(builder->batch().ticks.back().births.empty());
const auto replay = replay_cellular_batch(**state, builder->batch());
CHECK(replay && (*replay)->snapshot() == builder->candidate().snapshot());
}
}
} // namespace
int main() try {
actions_bind_to_owned_life_and_preserve_other_inputs();
durable_mutation_reservations_bind_to_the_exact_offer();
rules_13_actions_and_choices_bind();
builder_batches_reserved_birth_after_empty_tick();
unavailable_birth_does_not_freeze_the_tick();
std::puts("cellular session batch: ok");
} catch (const std::exception& e) {
std::fprintf(stderr, "%s\n", e.what());
return 1;
}