#pragma once #include "evo/network/intent_admission.hpp" #include "evo/persistence/life_reservation_store.hpp" #include "evo/persistence/region_lease.hpp" #include "evo/server/lifecycle/cellular_batch.hpp" #include #include #include #include #include #include #include namespace evo::server { enum class CellularSessionBatchError : std::uint8_t { tick, intents, action, life, duplicate_action, duplicate_birth, invalid_birth, mutation, duplicate_mutation, unperceived_target, head, batch_full, action_limit, biology }; struct CellularSessionTick { CellularTickIntents intents; std::vector actions; std::vector mutation_actions; }; // Overlays authenticated v7 actions and v10 durable mutation reservations onto the // scheduler's complete per-life input set. The scheduler owns non-player and no-message // behavior; this adapter binds each action to the one live account-owned life and preserves // the canonical input needed for publication and replay. [[nodiscard]] std::expected prepare_cellular_session_tick( const CellularLives& state, std::uint64_t tick, std::span baseline, std::span actions, std::span mutations = {}); // Builds one bounded publication from the last durable head. Its working copy is private; // only the host may publish, and callers must discard it after uncertainty until the host // resolves that exact prepared batch. class CellularSessionBatchBuilder { public: [[nodiscard]] static std::expected 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 births = {}, std::span mutations = {}); // Scheduler path: expected placement/funding/existing-life refusals are reported // separately, so they cannot prevent other births or the ordinary tick. Unexpected // failures retain append's all-or-nothing behavior. Persist reports only after publish. [[nodiscard]] std::expected append_with_birth_refusals( std::uint64_t tick, std::span baseline, std::span actions, std::span births = {}, std::span mutations = {}); [[nodiscard]] std::span refused_births() const noexcept { return refused_births_; } [[nodiscard]] const CellularBatch& batch() const noexcept { return batch_; } [[nodiscard]] std::span actions() const noexcept { return actions_; } [[nodiscard]] std::span mutation_actions() const noexcept { return mutation_actions_; } [[nodiscard]] const CellularLives& candidate() const noexcept { return *working_; } // Hands the working state to a commit; the builder is spent afterwards. [[nodiscard]] std::unique_ptr take_candidate() noexcept { return std::move(working_); } // A batch started on an uncommitted predecessor (a commit still in flight) takes that // predecessor's real head once it is known. The head must end where this batch begins. [[nodiscard]] bool rebase(const persistence::RegionHead& head) noexcept { if (head.frame != batch_.header.frame || head.end_tick != batch_.header.begin_tick) return false; batch_.header.predecessor = head.commit; batch_.header.predecessor_digest = head.digest; return true; } // The owner's own refusal behind the last `biology` error, for operator logs. [[nodiscard]] std::optional last_lives_error() const noexcept { return last_lives_error_; } private: [[nodiscard]] std::expected append_impl(std::uint64_t tick, std::span baseline, std::span actions, std::span births, std::span mutations, bool allow_refusals); explicit CellularSessionBatchBuilder(std::unique_ptr working) : working_{std::move(working)} {} std::unique_ptr working_; CellularBatch batch_; std::vector actions_; std::vector mutation_actions_; std::vector refused_births_; std::optional last_lives_error_; }; } // namespace evo::server