diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index e6888dd..939239d 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -6870,12 +6870,12 @@ not visible from this workspace, so its current value is unverified. Run #280 (commit `bfa0e1d`) exercised the workflow after adding a Harbor-gate diagnostic. Its network suite failed at `tests/network_control.cpp:375`: the -negative test still treats protocol version 10 as a future unsupported version, -although M11.4c now defines the v10 mutation-action codec. The failed checks -matrix blocked `server-image` and `image`, so this run did not attempt either an -image build or Harbor publication. The narrow proposed test correction is to -use version 11 for the future-version case; the test file remains untouched -pending resolution of its earlier review restriction. The local focused Windows -build for the next M11.4c reservation-to-tick adapter also stalled without -output and was stopped; `git diff --check` passed, but the adapter remains -unvalidated until CI can run it. +negative test still treated protocol version 10 as a future unsupported +version, although M11.4c defines the v10 mutation-action codec. The failed +checks matrix blocked `server-image` and `image`, so that run attempted neither +an image build nor Harbor publication. The future-version test now uses v11; +the M11.4c reservation-to-tick adapter is included in the next CI run so the +network, native, server-image and Harbor gates can all be evaluated together. +The local focused Windows build stalled without output and was stopped; +`git diff --check` passed, but CI must validate the adapter. Harbor's repository +setting remains unverified until that run reaches and reports the publish gate. diff --git a/include/evo/server/cellular_session_batch.hpp b/include/evo/server/cellular_session_batch.hpp index 65f6e58..dee8955 100644 --- a/include/evo/server/cellular_session_batch.hpp +++ b/include/evo/server/cellular_session_batch.hpp @@ -1,6 +1,7 @@ #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" @@ -20,6 +21,8 @@ enum class CellularSessionBatchError : std::uint8_t { duplicate_action, duplicate_birth, invalid_birth, + mutation, + duplicate_mutation, unperceived_target, head, batch_full, @@ -30,15 +33,19 @@ enum class CellularSessionBatchError : std::uint8_t { struct CellularSessionTick { CellularTickIntents intents; std::vector actions; + std::vector mutation_actions; }; -// Overlays authenticated v7 actions onto the scheduler's complete per-life input set. -// The scheduler owns all non-player and no-message behavior; this adapter only binds a -// durable action to the one live account-owned life and preserves its canonical bytes. +// 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 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 @@ -50,13 +57,17 @@ class CellularSessionBatchBuilder { [[nodiscard]] std::expected append(std::uint64_t tick, std::span baseline, std::span actions, - std::span births = {}); + std::span births = {}, + std::span mutations = {}); [[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_; } @@ -67,5 +78,6 @@ class CellularSessionBatchBuilder { std::unique_ptr working_; CellularBatch batch_; std::vector actions_; + std::vector mutation_actions_; }; } // namespace evo::server diff --git a/src/server/session/cellular_session_batch.cpp b/src/server/session/cellular_session_batch.cpp index a4372a0..c4f634e 100644 --- a/src/server/session/cellular_session_batch.cpp +++ b/src/server/session/cellular_session_batch.cpp @@ -4,15 +4,27 @@ #include namespace evo::server { +namespace { +std::uint64_t little_endian(std::span bytes, std::size_t offset, + std::size_t count) { + std::uint64_t value{}; + for (std::size_t i = 0; i < count; ++i) + value |= static_cast(bytes[offset + i]) << (i * 8U); + return value; +} +} // namespace + std::expected prepare_cellular_session_tick(const CellularLives& state, std::uint64_t tick, std::span baseline, - std::span actions) { + std::span actions, + std::span mutations) { if (tick == 0 || tick != state.tick() + 1) return std::unexpected{CellularSessionBatchError::tick}; if (baseline.size() != state.lives().size()) return std::unexpected{CellularSessionBatchError::intents}; - if (actions.size() > max_cellular_action_records) + if (actions.size() > max_cellular_action_records || + mutations.size() > max_cellular_action_records) return std::unexpected{CellularSessionBatchError::action}; CellularSessionTick prepared; @@ -72,6 +84,56 @@ prepare_cellular_session_tick(const CellularLives& state, std::uint64_t tick, return left.account < right.account; return left.action_id < right.action_id; }); + + if (!mutations.empty() && state.format_version() != cellular_lives_revision_format) + return std::unexpected{CellularSessionBatchError::mutation}; + prepared.intents.mutations.reserve(mutations.size()); + prepared.mutation_actions.reserve(mutations.size()); + for (const auto& reservation : mutations) { + const auto bytes = std::span{reservation.input}; + const auto target_tick = little_endian(bytes, 0, 8); + const auto life_id = little_endian(bytes, 8, 8); + const auto event_index = little_endian(bytes, 16, 8); + const auto offer_tick = little_endian(bytes, 24, 8); + const auto operator_id = static_cast(little_endian(bytes, 32, 2)); + if (reservation.account == persistence::Uuid{} || reservation.action_id == 0 || + reservation.region != state.region() || reservation.generation == 0 || + reservation.successor_identity == 0 || target_tick != tick || + reservation.target_tick != tick || life_id == 0 || event_index == 0 || + offer_tick == 0 || offer_tick >= tick || operator_id == 0) + return std::unexpected{CellularSessionBatchError::mutation}; + const auto life = std::ranges::find(state.lives(), reservation.account, + &CellularOwnedLife::account); + if (life == state.lives().end() || life->identity.life != life_id || + life->mutation.event_index != event_index || life->mutation.offer_tick != offer_tick || + !life->mutation.offer || + !std::ranges::any_of(life->mutation.offer->candidates, [&](const auto& candidate) { + return candidate.operator_id == operator_id; + })) + return std::unexpected{CellularSessionBatchError::mutation}; + + if (std::ranges::any_of(prepared.intents.mutations, [&](const auto& previous) { + return previous.life_id == life_id || + previous.successor_id == reservation.successor_identity; + }) || + std::ranges::any_of(prepared.mutation_actions, [&](const auto& previous) { + return previous.account == reservation.account && + previous.action_id == reservation.action_id; + })) + return std::unexpected{CellularSessionBatchError::duplicate_mutation}; + prepared.intents.mutations.push_back( + {life_id, event_index, offer_tick, reservation.successor_identity, operator_id}); + prepared.mutation_actions.push_back( + {reservation.account, reservation.action_id, tick, + {life_id, event_index, offer_tick, operator_id}}); + } + std::ranges::sort(prepared.intents.mutations, {}, &CellularMutationSelection::life_id); + std::ranges::sort(prepared.mutation_actions, + [](const auto& left, const auto& right) { + if (left.account != right.account) + return left.account < right.account; + return left.action_id < right.action_id; + }); return prepared; } @@ -101,12 +163,16 @@ std::expected CellularSessionBatchBuilder::append(std::uint64_t tick, std::span baseline, std::span actions, - std::span births) { + std::span births, + std::span + mutations) { if (batch_.ticks.size() >= max_cellular_batch_ticks) return std::unexpected{CellularSessionBatchError::batch_full}; if (actions_.size() + actions.size() > max_cellular_action_records) return std::unexpected{CellularSessionBatchError::action_limit}; - auto prepared = prepare_cellular_session_tick(*working_, tick, baseline, actions); + if (mutation_actions_.size() + mutations.size() > max_cellular_action_records) + return std::unexpected{CellularSessionBatchError::action_limit}; + auto prepared = prepare_cellular_session_tick(*working_, tick, baseline, actions, mutations); if (!prepared) return std::unexpected{prepared.error()}; for (const auto& action : prepared->actions) @@ -114,6 +180,12 @@ CellularSessionBatchBuilder::append(std::uint64_t tick, return previous.account == action.account && previous.action_id == action.action_id; }) != actions_.end()) return std::unexpected{CellularSessionBatchError::duplicate_action}; + for (const auto& action : prepared->mutation_actions) + if (std::ranges::find_if(mutation_actions_, [&](const auto& previous) { + return previous.account == action.account && + previous.action_id == action.action_id; + }) != mutation_actions_.end()) + return std::unexpected{CellularSessionBatchError::duplicate_mutation}; if (births.size() > biology::max_cellular_tick_lives) return std::unexpected{CellularSessionBatchError::invalid_birth}; prepared->intents.births.assign(births.begin(), births.end()); @@ -138,7 +210,8 @@ CellularSessionBatchBuilder::append(std::uint64_t tick, auto next = CellularLives::restore(working_->snapshot(), working_->world(), working_->region(), working_->frame(), working_->tick()); - if (!next || !(*next)->advance_tick(prepared->intents.lives)) + if (!next || !(*next)->advance_tick_with_mutations(prepared->intents.lives, + prepared->intents.mutations)) return std::unexpected{CellularSessionBatchError::biology}; for (const auto& birth : prepared->intents.births) { @@ -156,9 +229,20 @@ CellularSessionBatchBuilder::append(std::uint64_t tick, next_batch.header.end_tick = tick; if (!encode_cellular_batch(next_batch)) return std::unexpected{CellularSessionBatchError::invalid_birth}; + const auto next_mutation_actions = [&] { + auto records = mutation_actions_; + records.insert(records.end(), prepared->mutation_actions.begin(), + prepared->mutation_actions.end()); + return records; + }(); + if (!encode_cellular_mutation_action_block(next_mutation_actions, + next_batch.header.begin_tick, + next_batch.header.end_tick)) + return std::unexpected{CellularSessionBatchError::mutation}; working_ = std::move(*next); batch_ = std::move(next_batch); actions_.insert(actions_.end(), prepared->actions.begin(), prepared->actions.end()); + mutation_actions_ = next_mutation_actions; return {}; } } // namespace evo::server diff --git a/tests/cellular_session_batch_tests.cpp b/tests/cellular_session_batch_tests.cpp index 56cf3c2..ea5ed66 100644 --- a/tests/cellular_session_batch_tests.cpp +++ b/tests/cellular_session_batch_tests.cpp @@ -144,6 +144,95 @@ void actions_bind_to_owned_life_and_preserve_other_inputs() { 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 && !(*offer)->candidates.empty()); + const auto& live = (*revision_owner)->lives()[0]; + const auto action_tick = (*revision_owner)->tick() + 1; + const auto& candidate = (*offer)->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(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}}; + CHECK(builder->batch().ticks.back().mutations == + std::vector{CellularMutationSelection{life_id, live.mutation.event_index, + live.mutation.offer_tick, + reservation.successor_identity, + candidate.operator_id}} && + 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()); +} + void builder_batches_reserved_birth_after_empty_tick() { auto primitive = PrimitiveLives::create({93, 1, 1, 17, founder_price(1), {{0, 0}}}); CHECK(primitive); @@ -181,6 +270,7 @@ void builder_batches_reserved_birth_after_empty_tick() { int main() try { actions_bind_to_owned_life_and_preserve_other_inputs(); + durable_mutation_reservations_bind_to_the_exact_offer(); builder_batches_reserved_birth_after_empty_tick(); std::puts("cellular session batch: ok"); } catch (const std::exception& e) { diff --git a/tests/network_control.cpp b/tests/network_control.cpp index f088f60..07ea5e3 100644 --- a/tests/network_control.cpp +++ b/tests/network_control.cpp @@ -371,7 +371,7 @@ void session_codec() { mutated.at(0) ^= 1; CHECK(refused(decode_session(mutated), SessionCodecError::bad_magic)); mutated = bytes; - mutated.at(4) = 10; // A future version; protocol versions through 9 are supported. + mutated.at(4) = 11; // A future version; protocol versions through 10 are supported. CHECK(refused(decode_session(mutated), SessionCodecError::unsupported_version)); mutated = bytes; mutated.at(7) = 1;