From 704ce0f2c8daca06a51c5b82b0f0cf4505497d6a Mon Sep 17 00:00:00 2001 From: dingo Date: Sun, 11 Oct 2026 00:51:05 -0400 Subject: [PATCH] Queue births in cellular tick batches --- docs/engineering/M11_3_DELIVERY_PLAN.md | 6 +++ docs/engineering/VALIDATION.md | 15 ++++++ include/evo/server/cellular_session_batch.hpp | 5 +- src/server/session/cellular_session_batch.cpp | 50 +++++++++++++++++-- tests/cellular_session_batch_tests.cpp | 41 +++++++++++++-- 5 files changed, 109 insertions(+), 8 deletions(-) diff --git a/docs/engineering/M11_3_DELIVERY_PLAN.md b/docs/engineering/M11_3_DELIVERY_PLAN.md index 608458b..fd90d4c 100644 --- a/docs/engineering/M11_3_DELIVERY_PLAN.md +++ b/docs/engineering/M11_3_DELIVERY_PLAN.md @@ -327,6 +327,12 @@ scoped change before starting the next segment. integration test covers empty-genesis birth, durable request outcome and restart recovery. CI/PostgreSQL execution remains pending, and the live v5 request scheduler still has to feed these records before 11.3e is playable. +- 11.3d-7j: `CellularSessionBatchBuilder::append` now accepts reserved birth + records, sorts and validates them, applies the tick's existing-life inputs, + then builds each birth on a detached candidate at the end tick. A failed birth + leaves the builder unchanged. Tests cover empty-region birth replay and + duplicate request refusal. The server still needs to route v5 requests into + this queue and run/publish the scheduler tick. ## Exit boundary diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 495cac5..0fa0c6c 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -6437,3 +6437,18 @@ environment lacks `psql`. The Windows `bash.exe` shim is access-denied, so the migration scripts could not be syntax-checked here. CTest, sanitizer checks, and CI have not run for this segment. The migration-test source itself cannot compile locally because its fixture requires POSIX `poll.h`. + +## 2026-10-11 — M11.3d-7j queued birth batch building + +`CellularSessionBatchBuilder` accepts reserved birth records for a scheduled +tick. It canonicalizes their order, advances existing lives first, and applies +each birth only to a detached candidate. Failed funding, placement, or duplicate +identity validation does not partially advance the builder. The focused session +batch test covers successful empty-region birth replay, duplicate birth refusal, +and unchanged state after refusal. + +The focused session-batch executable was manually compiled and run with GCC +13.2 in debug and `-O2 -DNDEBUG`; both pass. `git diff --check` passes. CTest, +sanitizers and CI are pending. The existing unrelated initializer warning in +`cellular_region_step.cpp:29` is demoted for these manual builds. This builder +is not yet wired into an `evo-server` cellular tick loop. diff --git a/include/evo/server/cellular_session_batch.hpp b/include/evo/server/cellular_session_batch.hpp index 5f7529a..6c9a3b7 100644 --- a/include/evo/server/cellular_session_batch.hpp +++ b/include/evo/server/cellular_session_batch.hpp @@ -18,6 +18,8 @@ enum class CellularSessionBatchError : std::uint8_t { action, life, duplicate_action, + duplicate_birth, + invalid_birth, unperceived_target, head, batch_full, @@ -47,7 +49,8 @@ class CellularSessionBatchBuilder { create(const CellularLives& committed_state, const persistence::RegionHead& committed_head); [[nodiscard]] std::expected append(std::uint64_t tick, std::span baseline, - std::span actions); + std::span actions, + std::span births = {}); [[nodiscard]] const CellularBatch& batch() const noexcept { return batch_; } [[nodiscard]] std::span actions() const noexcept { return actions_; diff --git a/src/server/session/cellular_session_batch.cpp b/src/server/session/cellular_session_batch.cpp index afba405..8e6ecb6 100644 --- a/src/server/session/cellular_session_batch.cpp +++ b/src/server/session/cellular_session_batch.cpp @@ -101,7 +101,8 @@ CellularSessionBatchBuilder::create(const CellularLives& committed_state, std::expected CellularSessionBatchBuilder::append(std::uint64_t tick, std::span baseline, - std::span actions) { + std::span actions, + std::span births) { if (batch_.ticks.size() >= max_cellular_batch_ticks) return std::unexpected{CellularSessionBatchError::batch_full}; if (actions_.size() + actions.size() > max_cellular_action_records) @@ -115,10 +116,51 @@ CellularSessionBatchBuilder::append(std::uint64_t tick, previous.action_id == action.action_id; }) != actions_.end()) return std::unexpected{CellularSessionBatchError::duplicate_action}; - if (!working_->advance_tick(prepared->intents.lives)) + if (births.size() > biology::max_cellular_tick_lives) + return std::unexpected{CellularSessionBatchError::invalid_birth}; + prepared->intents.births.assign(births.begin(), births.end()); + std::ranges::sort(prepared->intents.births, [](const auto& left, const auto& right) { + if (left.account != right.account) + return left.account < right.account; + return left.request < right.request; + }); + for (std::size_t i = 0; i < prepared->intents.births.size(); ++i) { + const auto& birth = prepared->intents.births[i]; + if (std::ranges::all_of(birth.account, [](std::uint8_t value) { return value == 0; }) || + birth.request == 0 || birth.generation == 0 || birth.identity == 0) + return std::unexpected{CellularSessionBatchError::invalid_birth}; + if (i > 0 && prepared->intents.births[i - 1].account == birth.account) + return std::unexpected{CellularSessionBatchError::duplicate_birth}; + for (const auto& previous_tick : batch_.ticks) + if (std::ranges::any_of(previous_tick.births, [&](const auto& previous) { + return previous.account == birth.account || + previous.identity == birth.identity; + })) + return std::unexpected{CellularSessionBatchError::duplicate_birth}; + } + + auto next = CellularLives::restore(working_->snapshot(), working_->world(), + working_->region(), working_->frame(), working_->tick()); + if (!next || !(*next)->advance_tick(prepared->intents.lives)) return std::unexpected{CellularSessionBatchError::biology}; - batch_.ticks.push_back(std::move(prepared->intents)); - batch_.header.end_tick = tick; + + for (const auto& birth : prepared->intents.births) { + const LifeRequest request{birth.account, birth.request}; + const LifeAllocation allocation{birth.identity, birth.identity, birth.identity, + birth.identity, {tick}}; + auto created = (*next)->birth_candidate(request, allocation); + if (!created) + return std::unexpected{CellularSessionBatchError::invalid_birth}; + *next = std::move(*created); + } + + auto next_batch = batch_; + next_batch.ticks.push_back(std::move(prepared->intents)); + next_batch.header.end_tick = tick; + if (!encode_cellular_batch(next_batch)) + return std::unexpected{CellularSessionBatchError::invalid_birth}; + working_ = std::move(*next); + batch_ = std::move(next_batch); actions_.insert(actions_.end(), prepared->actions.begin(), prepared->actions.end()); return {}; } diff --git a/tests/cellular_session_batch_tests.cpp b/tests/cellular_session_batch_tests.cpp index 09406a7..7783370 100644 --- a/tests/cellular_session_batch_tests.cpp +++ b/tests/cellular_session_batch_tests.cpp @@ -107,11 +107,11 @@ void actions_bind_to_owned_life_and_preserve_other_inputs() { 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({3, 0, 0, false, 0, 0}); - const network::CarriedAction repeated{network::AccountId{controlled.account}, 77, 3, + 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(3, next_baseline, repeated_actions).error() == + CHECK(builder->append(4, next_baseline, repeated_actions).error() == CellularSessionBatchError::duplicate_action); CHECK(builder->append(5, next_baseline, {}).error() == CellularSessionBatchError::tick); @@ -144,10 +144,45 @@ void actions_bind_to_owned_life_and_preserve_other_inputs() { CHECK(prepare_cellular_session_tick(**state, 2, baseline, malformed_actions).error() == CellularSessionBatchError::action); } + +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 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); +} } // namespace int main() try { actions_bind_to_owned_life_and_preserve_other_inputs(); + builder_batches_reserved_birth_after_empty_tick(); std::puts("cellular session batch: ok"); } catch (const std::exception& e) { std::fprintf(stderr, "%s\n", e.what());