From ab9fbdec8f7a1b2f1926b1c9336b1c13a3a5352b Mon Sep 17 00:00:00 2001 From: dingo Date: Sun, 11 Oct 2026 02:04:55 -0400 Subject: [PATCH] Deliver cellular tick diagnostics to owners --- apps/evo-client/panels.cpp | 58 +++- apps/evo-client/remote_session.hpp | 4 +- apps/evo-server/cellular_bridge.hpp | 46 +++- docs/engineering/M11_3_DELIVERY_PLAN.md | 17 +- docs/engineering/VALIDATION.md | 17 ++ docs/playtests/M11_3_CELLULAR_PROBE.md | 4 +- include/evo/biology/cellular_step.hpp | 7 + .../evo/server/lifecycle/cellular_lives.hpp | 14 +- include/evo/server/region_sessions.hpp | 4 +- src/biology/cellular_step.cpp | 28 +- src/client/session/region_session.cpp | 18 +- src/server/lifecycle/cellular_lives.cpp | 259 ++++++++++-------- src/server/session/region_sessions.cpp | 23 +- tests/cellular_lives_tests.cpp | 60 ++-- tests/cellular_step_tests.cpp | 2 + tests/quic_sessions.cpp | 35 ++- 16 files changed, 408 insertions(+), 188 deletions(-) diff --git a/apps/evo-client/panels.cpp b/apps/evo-client/panels.cpp index 0103967..19bd9b3 100644 --- a/apps/evo-client/panels.cpp +++ b/apps/evo-client/panels.cpp @@ -125,7 +125,7 @@ void launcher(const PanelView& v, FormBuffers& form, PanelActions& a) { ImGui::InputText("Dev CA file", form.authority.data(), form.authority.size()); ImGui::InputText("Grant bundle file", form.grants.data(), form.grants.size()); bool cellular_trial = v.cellular_trial; - if (ImGui::Checkbox("Cellular trial (v8)", &cellular_trial)) + if (ImGui::Checkbox("Cellular trial (v9)", &cellular_trial)) a.cellular_trial = cellular_trial; const bool start = phase == LaunchPhase::choosing && ImGui::Button("Connect", {px(160), 0}); ImGui::EndDisabled(); @@ -411,6 +411,52 @@ void session(const PanelView& v, PanelActions& a) { ImGui::Text("Life %llu, tick %llu", static_cast(v.cellular_perception->life_identity), static_cast(v.cellular_perception->tick)); + if (const auto& diagnostics = v.cellular_perception->diagnostics) { + const auto& matter = diagnostics->reserve_matter_fg; + ImGui::TextWrapped("Reserve %llu pJ | C %llu N %llu P %llu O %llu fg", + static_cast(diagnostics->reserve_energy_pj), + static_cast(matter[0]), + static_cast(matter[1]), + static_cast(matter[2]), + static_cast(matter[3])); + const auto movement_name = [&] { + switch (diagnostics->movement) { + case network::CellularMovementResult::not_reported: + return "unavailable"; + case network::CellularMovementResult::no_movement: + return "no movement"; + case network::CellularMovementResult::moved: + return "moved"; + case network::CellularMovementResult::blocked: + return "blocked"; + case network::CellularMovementResult::insufficient_energy: + return "insufficient reserve"; + } + return "unknown"; + }; + const auto intake_name = [&] { + switch (diagnostics->intake) { + case network::CellularIntakeResult::not_reported: + return "unavailable"; + case network::CellularIntakeResult::not_requested: + return "not feeding"; + case network::CellularIntakeResult::acquired: + return "acquired"; + case network::CellularIntakeResult::missing_patch: + return "patch missing"; + case network::CellularIntakeResult::out_of_contact: + return "out of contact"; + } + return "unknown"; + }; + ImGui::TextWrapped( + "Last tick: maintenance %llu pJ, movement %llu pJ; %s; feeding %s", + static_cast(diagnostics->maintenance_cost_pj), + static_cast(diagnostics->movement_cost_pj), movement_name(), + intake_name()); + if (!diagnostics->has_tick_effects) + ImGui::TextDisabled("Tick effects unavailable after recovery."); + } ImGui::TextDisabled("Steer with movement keys. Feeding continues until stopped."); if (v.cellular_feed_target && ImGui::SmallButton("Stop feeding")) a.stop_feeding = true; @@ -423,11 +469,11 @@ void session(const PanelView& v, PanelActions& a) { ImGui::PushID(static_cast(i)); ImGui::Text("Patch (%u, %u) um", patch.x_um, patch.y_um); ImGui::TextWrapped("Energy %llu pJ | C %llu N %llu P %llu O %llu fg", - static_cast(patch.energy_pj), - static_cast(patch.matter_fg[0]), - static_cast(patch.matter_fg[1]), - static_cast(patch.matter_fg[2]), - static_cast(patch.matter_fg[3])); + static_cast(patch.energy_pj), + static_cast(patch.matter_fg[0]), + static_cast(patch.matter_fg[1]), + static_cast(patch.matter_fg[2]), + static_cast(patch.matter_fg[3])); const bool active = v.cellular_feed_target && v.cellular_feed_target->first == patch.x_um && v.cellular_feed_target->second == patch.y_um; diff --git a/apps/evo-client/remote_session.hpp b/apps/evo-client/remote_session.hpp index 320586e..5b40882 100644 --- a/apps/evo-client/remote_session.hpp +++ b/apps/evo-client/remote_session.hpp @@ -2,7 +2,7 @@ // M10.5c remote dev session for the engineering client (ADR-0093, over ADR-0091/0092 // sessions). Without EVO_CLIENT_NETWORK (a build without EVO_BUILD_QUIC) remote mode is // unavailable. The client only presents copies of replicated snapshots and sends bounded -// bounded intentions; the explicit cellular trial uses reliable v8 actions and its +// bounded intentions; the explicit cellular trial uses reliable v9 actions and its // owner-filtered perception view. Grant bytes stay inside the session: each is handed to one // connection and wiped here; never printed, shown or persisted. // @@ -413,7 +413,7 @@ class RemoteSession { return false; client::RegionSessionClientConfig config; if (cellular_trial_) - config.protocol_version = network::session_protocol_v8; + config.protocol_version = network::session_protocol_v9; const auto route = routes_.find(target_region_); config.transport = route != routes_.end() ? route->second : transport_; if (ticket_) { // A handoff: the ticket replaces a grant, once. diff --git a/apps/evo-server/cellular_bridge.hpp b/apps/evo-server/cellular_bridge.hpp index ed67fa5..890026e 100644 --- a/apps/evo-server/cellular_bridge.hpp +++ b/apps/evo-server/cellular_bridge.hpp @@ -25,8 +25,8 @@ namespace evo::server_app { // Profile-4 network boundary. Biology and persistence remain owned by the cellular host; -// this bridge handles admission, v7/v8 action receipts, public cell replication and -// owner-filtered v8 perception. +// this bridge handles admission, v7-v9 action receipts, public cell replication and +// owner-filtered v8/v9 perception. class CellularBridge { public: static std::optional open(const M10Options& options, @@ -177,6 +177,48 @@ class CellularBridge { network::CellularPerception view; view.tick = tick; view.life_identity = life.identity.life; + network::CellularLifeDiagnostics diagnostics; + diagnostics.reserve_energy_pj = life.reserve.energy.value; + for (std::size_t i = 0; i < diagnostics.reserve_matter_fg.size(); ++i) + diagnostics.reserve_matter_fg[i] = life.reserve.matter[i].value; + const auto feedback = + std::ranges::lower_bound(state.last_tick_feedback(), life.identity.life, {}, + &server::CellularLifeTickFeedback::life_id); + if (feedback != state.last_tick_feedback().end() && + feedback->life_id == life.identity.life && feedback->tick == tick) { + diagnostics.has_tick_effects = true; + diagnostics.maintenance_cost_pj = feedback->maintenance_cost.value; + diagnostics.movement_cost_pj = feedback->movement_cost.value; + switch (feedback->movement) { + case biology::CellularMovementStatus::not_requested: + diagnostics.movement = network::CellularMovementResult::no_movement; + break; + case biology::CellularMovementStatus::moved: + diagnostics.movement = network::CellularMovementResult::moved; + break; + case biology::CellularMovementStatus::blocked: + diagnostics.movement = network::CellularMovementResult::blocked; + break; + case biology::CellularMovementStatus::insufficient_reserve: + diagnostics.movement = network::CellularMovementResult::insufficient_energy; + break; + } + switch (feedback->intake) { + case biology::CellularIntakeStatus::not_requested: + diagnostics.intake = network::CellularIntakeResult::not_requested; + break; + case biology::CellularIntakeStatus::acquired: + diagnostics.intake = network::CellularIntakeResult::acquired; + break; + case biology::CellularIntakeStatus::missing: + diagnostics.intake = network::CellularIntakeResult::missing_patch; + break; + case biology::CellularIntakeStatus::out_of_contact: + diagnostics.intake = network::CellularIntakeResult::out_of_contact; + break; + } + } + view.diagnostics = diagnostics; view.patches.reserve(perceived->size()); for (const auto& patch : *perceived) { network::CellularPerceivedPatch visible; diff --git a/docs/engineering/M11_3_DELIVERY_PLAN.md b/docs/engineering/M11_3_DELIVERY_PLAN.md index d20b787..6895c02 100644 --- a/docs/engineering/M11_3_DELIVERY_PLAN.md +++ b/docs/engineering/M11_3_DELIVERY_PLAN.md @@ -377,7 +377,7 @@ scoped change before starting the next segment. after CI confirms the v8 server/client build. - Readiness update (2026-10-11): the maintainer reports that the Git checks passed, `evo-region` is running, and the client connects. This is operator - confirmation of checks and connectivity; it does not establish that the v8 + confirmation of checks and connectivity; it does not establish that the cellular path was selected or that life creation, movement, feeding and the on-screen perception work in a hands-on session. Those outcomes and the exploratory notes remain the 11.3e exit evidence. The 11.4 mutation expansion @@ -391,12 +391,21 @@ scoped change before starting the next segment. - 11.3e-6a: ADR-0117 defines owner-only protocol v9 diagnostics for current reserve and the latest committed tick's maintenance, movement and intake result. The v9 codec keeps v8 bytes frozen and rejects malformed status combinations; - standalone MSVC `NDEBUG` protocol tests pass. Server report projection and the - client display remain the next parts of this subsection, with CI still pending. + standalone MSVC `NDEBUG` protocol tests pass. +- 11.3e-6b: tick results now expose transient authoritative life feedback from + the committed cellular candidate. The profile bridge includes it only in the + authenticated owner's v9 perception; v8 owner sessions continue to receive + their frozen v8 payload. Recovery clears this non-persistent presentation + feedback. Focused biology, lifecycle and QUIC tests cover these paths; CI is + still pending. +- 11.3e-6c: the cellular trial selects v9 and shows reserve inventories, + maintenance and movement energy costs, movement outcome and feeding outcome. + Recovery is identified when tick effects are unavailable. The playtest remains + exploratory; the probe checklist needs a human run before 11.3 closes. ## Exit boundary -11.3 closes only when all five segments have evidence and the player-facing +11.3 closes only when the documented segments have evidence and the player-facing single-region probe works against the authoritative server. 11.2 owner routing must also be closed before M11 claims persistent control across regions. Mutation, funded budding, complete permadeath, logout/resume and repeated-life playtesting diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 81d4466..11d3bc6 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -6629,3 +6629,20 @@ libraries predate v8, first missing the new client methods and then failing during old server-session setup. The full remote-session test, fresh client link, and CI run remain pending; no cellular runtime or playtest success is claimed. + +## 2026-10-11 — M11.3e-6b/c v9 diagnostics delivery (CI pending) + +The committed cellular tick now exposes transient per-life feedback, the +cellular server bridge sends it only to the authenticated life owner over v9, +and the client displays reserve inventory, the last tick's maintenance and +movement costs, movement outcome and feeding outcome. v8 owner sessions still +receive the byte-compatible v8 perception; restart clears transient tick +effects. The QUIC test covers v9 owner filtering and v8 compatibility. + +MSVC 19.44 compiled the changed biology, lifecycle, client-session and +server-session translation units, the cellular step/lifecycle tests, the QUIC +session test, the client panel and remote-session test with `/W4 /WX /DNDEBUG`. +Visual Studio LLVM `clang-format --dry-run --Werror` and `git diff --check` +passed. The configured CMake/Ninja build remained at `Re-checking globbed +directories...` for 60 seconds and was stopped; the QUIC test executable, +cellular runtime test executables, CI run and live-region probe have not run. diff --git a/docs/playtests/M11_3_CELLULAR_PROBE.md b/docs/playtests/M11_3_CELLULAR_PROBE.md index 8ac27f4..349a498 100644 --- a/docs/playtests/M11_3_CELLULAR_PROBE.md +++ b/docs/playtests/M11_3_CELLULAR_PROBE.md @@ -2,7 +2,7 @@ This is the first hands-on check of the cellular stage. It is a one-player, single-region engineering trial, not a balance sign-off or a claim that M11 is -complete. Run it after CI is green for the v8 server and client changes. +complete. Run it after CI is green for the v9 server and client changes. ## Start @@ -20,7 +20,7 @@ development CA: Replace the endpoint and certificate name with the values for the running dev region. The launcher can do the same: fill in endpoint, server name, CA and -grant bundle, check **Cellular trial (v8)**, then connect. Do not select the +grant bundle, check **Cellular trial (v9)**, then connect. Do not select the load-test grant bundle for an interactive session. On first successful login, the client submits a stable first-life request. The diff --git a/include/evo/biology/cellular_step.hpp b/include/evo/biology/cellular_step.hpp index 74b6414..8c92c70 100644 --- a/include/evo/biology/cellular_step.hpp +++ b/include/evo/biology/cellular_step.hpp @@ -20,6 +20,12 @@ enum class CellularStepError : std::uint8_t { motion }; enum class CellularIntakeStatus : std::uint8_t { not_requested, acquired, missing, out_of_contact }; +enum class CellularMovementStatus : std::uint8_t { + not_requested, + moved, + blocked, + insufficient_reserve +}; // Complete detached candidate for one 50 ms life tick. Maintenance is charged // first; unaffordable motion leaves the cell stationary, then one targeted patch @@ -30,6 +36,7 @@ struct CellularStepCandidate { accounting::Inventory reserve_after; accounting::Inventory heat_after; accounting::Picojoules maintenance_cost{}, movement_cost{}; + CellularMovementStatus movement_status{CellularMovementStatus::not_requested}; CellularIntakeStatus intake_status{CellularIntakeStatus::not_requested}; std::optional intake; auto operator<=>(const CellularStepCandidate&) const = default; diff --git a/include/evo/server/lifecycle/cellular_lives.hpp b/include/evo/server/lifecycle/cellular_lives.hpp index c757f01..047bc5f 100644 --- a/include/evo/server/lifecycle/cellular_lives.hpp +++ b/include/evo/server/lifecycle/cellular_lives.hpp @@ -41,6 +41,14 @@ struct CellularLifeIntent { biology::CellularStepIntent intent{}; auto operator<=>(const CellularLifeIntent&) const = default; }; +struct CellularLifeTickFeedback { + std::uint64_t life_id{}, tick{}; + accounting::Inventory reserve; + accounting::Picojoules maintenance_cost{}, movement_cost{}; + biology::CellularMovementStatus movement{biology::CellularMovementStatus::not_requested}; + biology::CellularIntakeStatus intake{biology::CellularIntakeStatus::not_requested}; + auto operator<=>(const CellularLifeTickFeedback&) const = default; +}; struct CellularLivesConfig { biology::CellularArea area{}; std::uint64_t tick{}; @@ -95,11 +103,12 @@ class CellularLives { [[nodiscard]] accounting::Inventory heat() const noexcept; [[nodiscard]] accounting::Inventory opening_total() const noexcept; [[nodiscard]] accounting::Inventory current_total() const noexcept; + // Latest successful in-memory step report; omitted from snapshots and empty after restore. + [[nodiscard]] std::span last_tick_feedback() const noexcept; [[nodiscard]] std::vector snapshot() const; // Exact stock view for one live cell, filtered by its authoritative footprint and the // M11.3 perception radius. Patch provenance and out-of-range stock are never returned. - [[nodiscard]] std::expected, - CellularLivesError> + [[nodiscard]] std::expected, CellularLivesError> perceive(std::uint64_t life_id) const; // Returns a detached post-birth candidate for an allocator-reserved request. @@ -123,5 +132,6 @@ class CellularLives { struct State; explicit CellularLives(std::unique_ptr state); std::unique_ptr state_; + std::vector last_tick_feedback_; }; } // namespace evo::server diff --git a/include/evo/server/region_sessions.hpp b/include/evo/server/region_sessions.hpp index 16fed34..6016894 100644 --- a/include/evo/server/region_sessions.hpp +++ b/include/evo/server/region_sessions.hpp @@ -100,7 +100,7 @@ struct RegionSessionsConfig { // M10.8d recovery: this often, open outgoing transfers prepared more than ticket_lifetime // + 10 s ago (by the store's clock, e.g. by a process that died) are aborted. std::chrono::milliseconds overdue_poll{5000}; - // v5/v8 are accepted only when both primitive-life handlers are supplied. Callbacks + // v5/v8/v9 are accepted only when both primitive-life handlers are supplied. Callbacks // receive the authenticated account and durable control generation for this session. // A cellular host may enable v5 together with the v7 cellular action path; each // connection remains fixed to its negotiated protocol. @@ -112,7 +112,7 @@ struct RegionSessionsConfig { query_primitive_life; // M11.3 v6 cellular intentions. Opt-in until a cellular state owner consumes them. bool accept_cellular_intentions{}; - // M11.3 v7 durable cellular actions and v8 owner-filtered perception. Requires durable + // M11.3 v7 durable cellular actions and v8/v9 owner-filtered perception. Requires durable // control and action ledgers; the caller forwards carried inputs to profile 4. bool accept_cellular_actions{}; }; diff --git a/src/biology/cellular_step.cpp b/src/biology/cellular_step.cpp index 68e955b..9d8c367 100644 --- a/src/biology/cellular_step.cpp +++ b/src/biology/cellular_step.cpp @@ -39,6 +39,8 @@ propose_cellular_step(std::uint64_t begin_tick, CellularArea area, CellularPosit return std::unexpected{CellularStepError::overflow}; reserve.energy.value -= *maintenance; + const auto original_position = position; + const bool movement_requested = intent.movement.x != 0 || intent.movement.y != 0; const auto movement = propose_funded_cellular_motion(area, position, extent_um, intent.movement, budget, reserve, heat); if (!movement && movement.error() != CellularMotionError::insufficient_reserve) @@ -49,12 +51,28 @@ propose_cellular_step(std::uint64_t begin_tick, CellularArea area, CellularPosit heat = movement->heat_after; } - CellularStepCandidate candidate{ - begin_tick, begin_tick + 1, position, reserve, - heat, {*maintenance}, {}, CellularIntakeStatus::not_requested, - {}}; - if (movement) + CellularStepCandidate candidate{begin_tick, + begin_tick + 1, + position, + reserve, + heat, + {*maintenance}, + {}, + CellularMovementStatus::not_requested, + CellularIntakeStatus::not_requested, + {}}; + if (!movement_requested) { + candidate.movement_status = CellularMovementStatus::not_requested; + } else if (!movement) { + candidate.movement_status = CellularMovementStatus::insufficient_reserve; + } else if (movement->position == original_position) { + candidate.movement_status = CellularMovementStatus::blocked; + } else { + candidate.movement_status = CellularMovementStatus::moved; + } + if (movement) { candidate.movement_cost = movement->movement_cost; + } if (!intent.intake_target) return candidate; diff --git a/src/client/session/region_session.cpp b/src/client/session/region_session.cpp index 33a6369..03d5511 100644 --- a/src/client/session/region_session.cpp +++ b/src/client/session/region_session.cpp @@ -159,7 +159,8 @@ RegionSessionClient::connect(RegionSessionClientConfig config, Clock::time_point config.protocol_version != network::session_protocol_v5 && config.protocol_version != network::session_protocol_v6 && config.protocol_version != network::session_protocol_v7 && - config.protocol_version != network::session_protocol_v8) + config.protocol_version != network::session_protocol_v8 && + config.protocol_version != network::session_protocol_v9) return std::unexpected(network::quic::EndpointError::configuration); auto endpoint = network::quic::Endpoint::connect(config.transport); if (!endpoint) @@ -265,7 +266,8 @@ bool RegionSessionClient::send_cellular_action( std::optional> intake_target) { auto& m = *impl_; return (m.protocol_version == network::session_protocol_v7 || - m.protocol_version == network::session_protocol_v8) && + m.protocol_version == network::session_protocol_v8 || + m.protocol_version == network::session_protocol_v9) && m.state == RegionSessionState::established && m.welcome && action_id != 0 && tick != 0 && m.send(network::CellularAction{m.welcome->session, m.welcome->epoch, action_id, tick, move_x, move_y, intake_target.has_value(), @@ -277,14 +279,16 @@ bool RegionSessionClient::query_action(std::uint64_t action_id) { auto& m = *impl_; return (m.protocol_version == network::session_protocol_version || m.protocol_version == network::session_protocol_v7 || - m.protocol_version == network::session_protocol_v8) && + m.protocol_version == network::session_protocol_v8 || + m.protocol_version == network::session_protocol_v9) && m.state == RegionSessionState::established && m.send(network::ActionQuery{action_id}); } bool RegionSessionClient::request_primitive_life(std::uint64_t request_id) { auto& m = *impl_; return (m.protocol_version == network::session_protocol_v5 || - m.protocol_version == network::session_protocol_v8) && + m.protocol_version == network::session_protocol_v8 || + m.protocol_version == network::session_protocol_v9) && m.state == RegionSessionState::established && m.welcome && request_id != 0 && m.send(network::PrimitiveLifeRequest{m.welcome->session, m.welcome->epoch, request_id}); } @@ -292,7 +296,8 @@ bool RegionSessionClient::request_primitive_life(std::uint64_t request_id) { bool RegionSessionClient::query_primitive_life(std::uint64_t request_id) { auto& m = *impl_; return (m.protocol_version == network::session_protocol_v5 || - m.protocol_version == network::session_protocol_v8) && + m.protocol_version == network::session_protocol_v8 || + m.protocol_version == network::session_protocol_v9) && m.state == RegionSessionState::established && request_id != 0 && m.send(network::PrimitiveLifeQuery{request_id}); } @@ -329,7 +334,8 @@ bool RegionSessionClient::set_interest(const network::SessionInterest& interest) auto& m = *impl_; return (m.protocol_version == network::session_protocol_version || m.protocol_version == network::session_protocol_v7 || - m.protocol_version == network::session_protocol_v8) && + m.protocol_version == network::session_protocol_v8 || + m.protocol_version == network::session_protocol_v9) && m.state == RegionSessionState::established && m.send(interest); } diff --git a/src/server/lifecycle/cellular_lives.cpp b/src/server/lifecycle/cellular_lives.cpp index 900bc91..9d1d8d2 100644 --- a/src/server/lifecycle/cellular_lives.cpp +++ b/src/server/lifecycle/cellular_lives.cpp @@ -101,8 +101,7 @@ bool add_inventory(accounting::Inventory& total, const accounting::Inventory& va return true; } -bool subtract_inventory(accounting::Inventory& total, - const accounting::Inventory& value) noexcept { +bool subtract_inventory(accounting::Inventory& total, const accounting::Inventory& value) noexcept { if (value.energy.value > total.energy.value) return false; for (std::size_t channel = 0; channel < total.matter.size(); ++channel) @@ -147,34 +146,31 @@ sum_state(accounting::Inventory nursery, accounting::Inventory heat, std::expected validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame, const CellularLivesConfig& config, const NurseryConfig& nursery_config, - const biology::CellularHabitat& habitat, - std::span lives, - std::span corpses, - const biology::LineageArchive& archive, accounting::Inventory nursery, - accounting::Inventory heat, accounting::Inventory opening_total) { + const biology::CellularHabitat& habitat, std::span lives, + std::span corpses, const biology::LineageArchive& archive, + accounting::Inventory nursery, accounting::Inventory heat, + accounting::Inventory opening_total) { const auto& map = habitat.config(); if (world == 0 || region == 0 || frame == 0 || nursery_config.world != world || nursery_config.region != region || nursery_config.frame != frame || - nursery_config.source == 0 || nursery_config.funding != nursery || - map.world != world || map.region != region || map.frame != frame || - config.area.width_um == 0 || config.area.height_um == 0 || - map.width_um != config.area.width_um || map.height_um != config.area.height_um || - config.ambient_temperature_millikelvin == 0 || + nursery_config.source == 0 || nursery_config.funding != nursery || map.world != world || + map.region != region || map.frame != frame || config.area.width_um == 0 || + config.area.height_um == 0 || map.width_um != config.area.width_um || + map.height_um != config.area.height_um || config.ambient_temperature_millikelvin == 0 || nursery_config.sites.empty() || nursery_config.sites.size() > max_primitive_lives || !std::ranges::is_sorted(nursery_config.sites) || lives.size() > biology::max_cellular_tick_lives || corpses.size() > biology::max_archived_organisms || archive.recorded() != lives.size() + corpses.size()) return std::unexpected{CellularLivesError::invalid_config}; - if (std::ranges::any_of(heat.matter, - [](const auto& value) { return value.value != 0; })) + if (std::ranges::any_of(heat.matter, [](const auto& value) { return value.value != 0; })) return std::unexpected{CellularLivesError::conservation}; const auto founder = biology::primitive_founder(1, 1); if (!founder) return std::unexpected{CellularLivesError::biology}; - const auto founder_budget = - biology::metabolic_budget(*founder, {1000, 0}, {config.ambient_temperature_millikelvin, {}}); + const auto founder_budget = biology::metabolic_budget( + *founder, {1000, 0}, {config.ambient_temperature_millikelvin, {}}); if (!founder_budget) return std::unexpected{CellularLivesError::biology}; const auto spawn_extent = founder_budget->phenotype.extent; @@ -202,10 +198,9 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame, for (std::size_t i = 0; i < lives.size(); ++i) { const auto& life = lives[i]; if (life.account == LifeAccount{} || life.identity.life == 0 || - life.identity.organism == 0 || life.identity.genome == 0 || - life.identity.event == 0 || life.identity.born.value == 0 || - life.identity.born.value > config.tick || life.source == 0 || - (i != 0 && lives[i - 1].identity.life >= life.identity.life)) + life.identity.organism == 0 || life.identity.genome == 0 || life.identity.event == 0 || + life.identity.born.value == 0 || life.identity.born.value > config.tick || + life.source == 0 || (i != 0 && lives[i - 1].identity.life >= life.identity.life)) return std::unexpected{CellularLivesError::invalid_life}; for (std::size_t j = 0; j < i; ++j) if (lives[j].account == life.account || @@ -217,8 +212,7 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame, return std::unexpected{CellularLivesError::archive}; const auto budget = budget_for(archive, life, config.ambient_temperature_millikelvin); if (!budget || budget->body != life.body || life.body.energy.value != 0 || - budget->phenotype.extent == 0 || - budget->phenotype.extent > config.area.width_um || + budget->phenotype.extent == 0 || budget->phenotype.extent > config.area.width_um || budget->phenotype.extent > config.area.height_um || life.position.x_um > config.area.width_um - budget->phenotype.extent || life.position.y_um > config.area.height_um - budget->phenotype.extent) @@ -242,8 +236,8 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame, const auto genome = archive.find(record->genome); if (!genome) return std::unexpected{CellularLivesError::archive}; - const auto budget = biology::metabolic_budget( - *genome, corpse.growth, {config.ambient_temperature_millikelvin, {}}); + const auto budget = biology::metabolic_budget(*genome, corpse.growth, + {config.ambient_temperature_millikelvin, {}}); if (!budget || budget->phenotype.extent != corpse.extent_um || corpse.extent_um > config.area.width_um || corpse.extent_um > config.area.height_um || corpse.position.x_um > config.area.width_um - corpse.extent_um || @@ -282,16 +276,14 @@ struct CellularLives::State { State(std::uint64_t world_id, std::uint64_t region_id, std::uint64_t frame_id, CellularLivesConfig cfg, NurseryConfig nursery_definition, - std::unique_ptr map, - std::vector living, std::vector dead, - const biology::LineageArchive& history, accounting::Inventory source, - accounting::Inventory accumulated_heat, accounting::Inventory initial, - accounting::Inventory current) + std::unique_ptr map, std::vector living, + std::vector dead, const biology::LineageArchive& history, + accounting::Inventory source, accounting::Inventory accumulated_heat, + accounting::Inventory initial, accounting::Inventory current) : world{world_id}, region{region_id}, frame{frame_id}, config{cfg}, - nursery_config{std::move(nursery_definition)}, - habitat{std::move(map)}, lives{std::move(living)}, corpses{std::move(dead)}, - archive{history}, nursery{source}, heat{accumulated_heat}, opening_total{initial}, - current_total{current} {} + nursery_config{std::move(nursery_definition)}, habitat{std::move(map)}, + lives{std::move(living)}, corpses{std::move(dead)}, archive{history}, nursery{source}, + heat{accumulated_heat}, opening_total{initial}, current_total{current} {} }; CellularLives::CellularLives(std::unique_ptr state) : state_{std::move(state)} {} @@ -319,8 +311,8 @@ CellularLives::create(const PrimitiveLives& opening, for (const auto& primitive : opening.lives()) { if (primitive.identity.born.value == 0 || primitive.identity.born.value > config.tick || primitive.identity.life == 0 || primitive.identity.organism == 0 || - primitive.identity.genome == 0 || primitive.source == 0 || - primitive.site.x_um < 0 || primitive.site.y_um < 0) + primitive.identity.genome == 0 || primitive.source == 0 || primitive.site.x_um < 0 || + primitive.site.y_um < 0) return std::unexpected{CellularLivesError::invalid_life}; if (primitive.body.value == 0 || primitive.reserve.value == 0) return std::unexpected{CellularLivesError::invalid_life}; @@ -342,8 +334,8 @@ CellularLives::create(const PrimitiveLives& opening, const auto& life = lives[i]; if (i != 0 && lives[i - 1].identity.life == life.identity.life) return std::unexpected{CellularLivesError::duplicate_life}; - const auto budget = budget_for(opening.archive(), life, - config.ambient_temperature_millikelvin); + const auto budget = + budget_for(opening.archive(), life, config.ambient_temperature_millikelvin); if (!budget || budget->phenotype.extent == 0 || budget->phenotype.extent > config.area.width_um || budget->phenotype.extent > config.area.height_um || life.body.energy.value != 0 || @@ -367,36 +359,58 @@ CellularLives::create(const PrimitiveLives& opening, const auto world = nursery_config.world; const auto region = nursery_config.region; const auto frame = nursery_config.frame; - auto state = std::make_unique( - world, region, frame, config, - std::move(nursery_config), - std::move(habitat), std::move(lives), std::vector{}, - opening.archive(), nursery, heat, opening_total, *current); + auto state = std::make_unique(world, region, frame, config, std::move(nursery_config), + std::move(habitat), std::move(lives), + std::vector{}, opening.archive(), + nursery, heat, opening_total, *current); return std::unique_ptr{new CellularLives{std::move(state)}}; } -std::uint64_t CellularLives::world() const noexcept { return state_->world; } -std::uint64_t CellularLives::region() const noexcept { return state_->region; } -std::uint64_t CellularLives::frame() const noexcept { return state_->frame; } -std::uint64_t CellularLives::tick() const noexcept { return state_->config.tick; } +std::uint64_t CellularLives::world() const noexcept { + return state_->world; +} +std::uint64_t CellularLives::region() const noexcept { + return state_->region; +} +std::uint64_t CellularLives::frame() const noexcept { + return state_->frame; +} +std::uint64_t CellularLives::tick() const noexcept { + return state_->config.tick; +} std::uint32_t CellularLives::ambient_temperature_millikelvin() const noexcept { return state_->config.ambient_temperature_millikelvin; } -biology::CellularArea CellularLives::area() const noexcept { return state_->config.area; } -std::span CellularLives::lives() const noexcept { return state_->lives; } +biology::CellularArea CellularLives::area() const noexcept { + return state_->config.area; +} +std::span CellularLives::lives() const noexcept { + return state_->lives; +} std::span CellularLives::corpses() const noexcept { return state_->corpses; } -const biology::CellularHabitat& CellularLives::habitat() const noexcept { return *state_->habitat; } -const biology::LineageArchive& CellularLives::archive() const noexcept { return state_->archive; } -accounting::Inventory CellularLives::nursery() const noexcept { return state_->nursery; } -accounting::Inventory CellularLives::heat() const noexcept { return state_->heat; } +const biology::CellularHabitat& CellularLives::habitat() const noexcept { + return *state_->habitat; +} +const biology::LineageArchive& CellularLives::archive() const noexcept { + return state_->archive; +} +accounting::Inventory CellularLives::nursery() const noexcept { + return state_->nursery; +} +accounting::Inventory CellularLives::heat() const noexcept { + return state_->heat; +} accounting::Inventory CellularLives::opening_total() const noexcept { return state_->opening_total; } accounting::Inventory CellularLives::current_total() const noexcept { return state_->current_total; } +std::span CellularLives::last_tick_feedback() const noexcept { + return last_tick_feedback_; +} std::vector CellularLives::snapshot() const { const auto habitat = state_->habitat->snapshot(); @@ -408,7 +422,8 @@ std::vector CellularLives::snapshot() const { cellular_life_bytes * life_count + cellular_corpse_bytes * corpse_count + habitat.size() + archive.size() + 8; - if (total_bytes > max_cellular_lives_bytes || habitat.size() > biology::max_cellular_habitat_bytes || + if (total_bytes > max_cellular_lives_bytes || + habitat.size() > biology::max_cellular_habitat_bytes || archive.size() > biology::max_archive_bytes || site_count > max_primitive_lives || life_count > biology::max_cellular_tick_lives || corpse_count > biology::max_archived_organisms) @@ -476,13 +491,12 @@ std::vector CellularLives::snapshot() const { std::expected, CellularLivesError> CellularLives::perceive(std::uint64_t life_id) const { - const auto life = std::ranges::find(state_->lives, life_id, [](const auto& owned) { - return owned.identity.life; - }); + const auto life = std::ranges::find(state_->lives, life_id, + [](const auto& owned) { return owned.identity.life; }); if (life == state_->lives.end()) return std::unexpected{CellularLivesError::invalid_life}; - const auto budget = budget_for(state_->archive, *life, - state_->config.ambient_temperature_millikelvin); + const auto budget = + budget_for(state_->archive, *life, state_->config.ambient_temperature_millikelvin); if (!budget) return std::unexpected{budget.error()}; auto view = state_->habitat->perceive(life->position, budget->phenotype.extent); @@ -507,15 +521,19 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c return life.account == request.account; })) return std::unexpected{CellularLivesError::life_exists}; - if (std::ranges::any_of(state_->lives, [&](const CellularOwnedLife& life) { - return life.identity.life == allocation.life || - life.identity.organism == allocation.organism || - life.identity.genome == allocation.genome || - life.identity.event == allocation.event; - }) || - std::ranges::any_of(state_->corpses, [&](const CellularOwnedCorpse& corpse) { - return corpse.life_id == allocation.life || corpse.organism == allocation.organism; - }) || state_->archive.find(allocation.genome) || state_->archive.organism(allocation.organism)) + if (std::ranges::any_of(state_->lives, + [&](const CellularOwnedLife& life) { + return life.identity.life == allocation.life || + life.identity.organism == allocation.organism || + life.identity.genome == allocation.genome || + life.identity.event == allocation.event; + }) || + std::ranges::any_of(state_->corpses, + [&](const CellularOwnedCorpse& corpse) { + return corpse.life_id == allocation.life || + corpse.organism == allocation.organism; + }) || + state_->archive.find(allocation.genome) || state_->archive.organism(allocation.organism)) return std::unexpected{CellularLivesError::duplicate_life}; const auto endowed = biology::primitive_endowment(); @@ -523,8 +541,7 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c if (!endowed || !genome) return std::unexpected{CellularLivesError::biology}; const auto budget = biology::metabolic_budget( - *genome, endowed->growth, - {state_->config.ambient_temperature_millikelvin, {}}); + *genome, endowed->growth, {state_->config.ambient_temperature_millikelvin, {}}); if (!budget || budget->body != endowed->body) return std::unexpected{CellularLivesError::biology}; @@ -533,11 +550,11 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c if (site.x_um < 0 || site.y_um < 0) return std::unexpected{CellularLivesError::invalid_config}; const biology::CellularPosition candidate{static_cast(site.x_um), - static_cast(site.y_um)}; + static_cast(site.y_um)}; bool occupied = false; for (const auto& living : state_->lives) { - const auto living_budget = budget_for( - state_->archive, living, state_->config.ambient_temperature_millikelvin); + const auto living_budget = + budget_for(state_->archive, living, state_->config.ambient_temperature_millikelvin); if (!living_budget) return std::unexpected{living_budget.error()}; const auto separated = [](std::uint64_t left, std::uint64_t right, @@ -568,8 +585,7 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c return std::unexpected{CellularLivesError::insufficient_funding}; nursery.energy.value -= endowed->construction.value; auto heat = state_->heat; - if (endowed->construction.value > - std::numeric_limits::max() - heat.energy.value) + if (endowed->construction.value > std::numeric_limits::max() - heat.energy.value) return std::unexpected{CellularLivesError::overflow}; heat.energy.value += endowed->construction.value; @@ -580,27 +596,25 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c auto lives = state_->lives; lives.push_back({request.account, allocation, state_->nursery_config.source, endowed->growth, *position, endowed->body, endowed->reserve}); - std::ranges::sort(lives, {}, [](const CellularOwnedLife& life) { - return life.identity.life; - }); + std::ranges::sort(lives, {}, [](const CellularOwnedLife& life) { return life.identity.life; }); auto nursery_config = state_->nursery_config; nursery_config.funding = nursery; auto habitat = biology::CellularHabitat::restore(state_->habitat->snapshot(), state_->world, - state_->region, state_->frame); + state_->region, state_->frame); if (!habitat) return std::unexpected{CellularLivesError::habitat}; - const auto current = validate_state(state_->world, state_->region, state_->frame, - state_->config, nursery_config, **habitat, lives, - state_->corpses, archive, nursery, heat, - state_->opening_total); + const auto current = validate_state( + state_->world, state_->region, state_->frame, state_->config, nursery_config, **habitat, + lives, state_->corpses, archive, nursery, heat, state_->opening_total); if (!current) return std::unexpected{current.error()}; auto candidate = std::unique_ptr{new CellularLives{std::make_unique( - state_->world, state_->region, state_->frame, state_->config, - std::move(nursery_config), std::move(*habitat), std::move(lives), state_->corpses, - archive, nursery, heat, state_->opening_total, *current)}}; + state_->world, state_->region, state_->frame, state_->config, std::move(nursery_config), + std::move(*habitat), std::move(lives), state_->corpses, archive, nursery, heat, + state_->opening_total, *current)}}; + candidate->last_tick_feedback_ = last_tick_feedback_; return candidate; } @@ -696,8 +710,8 @@ CellularLives::restore(std::span bytes, std::uint64_t world, const auto habitat_payload = in.raw(static_cast(habitat_size)); const auto archive_payload = in.raw(static_cast(archive_size)); - auto habitat = biology::CellularHabitat::restore(habitat_payload, saved_world, saved_region, - saved_frame); + auto habitat = + biology::CellularHabitat::restore(habitat_payload, saved_world, saved_region, saved_frame); if (!habitat) { if (habitat.error() == biology::CellularHabitatError::version) return std::unexpected{CellularLivesError::version}; @@ -715,9 +729,9 @@ CellularLives::restore(std::span bytes, std::uint64_t world, return std::unexpected{CellularLivesError::noncanonical}; return std::unexpected{CellularLivesError::archive}; } - const auto current = validate_state(saved_world, saved_region, saved_frame, config, - nursery_config, **habitat, lives, corpses, *archive, - nursery, heat, opening_total); + const auto current = + validate_state(saved_world, saved_region, saved_frame, config, nursery_config, **habitat, + lives, corpses, *archive, nursery, heat, opening_total); if (!current) return std::unexpected{current.error()}; @@ -747,8 +761,8 @@ CellularLives::advance_tick(std::span intents) { inputs.reserve(state_->lives.size()); for (std::size_t i = 0; i < state_->lives.size(); ++i) { const auto& life = state_->lives[i]; - const auto budget = budget_for(state_->archive, life, - state_->config.ambient_temperature_millikelvin); + const auto budget = + budget_for(state_->archive, life, state_->config.ambient_temperature_millikelvin); if (!budget) return std::unexpected{budget.error()}; inputs.push_back({life.identity.life, @@ -786,18 +800,13 @@ CellularLives::advance_tick(std::span intents) { if (found == next_lives.end() || found->identity.life != dead.life_id || !next_archive.record_death(found->identity.organism, {proposal->end_tick})) return std::unexpected{CellularLivesError::archive}; - const auto corpse_budget = budget_for( - next_archive, *found, state_->config.ambient_temperature_millikelvin); + const auto corpse_budget = + budget_for(next_archive, *found, state_->config.ambient_temperature_millikelvin); if (!corpse_budget) return std::unexpected{corpse_budget.error()}; - next_corpses.push_back({found->identity.life, - found->identity.organism, - found->source, - proposal->end_tick, - dead.position, - found->growth, - corpse_budget->phenotype.extent, - dead.inventory}); + next_corpses.push_back({found->identity.life, found->identity.organism, found->source, + proposal->end_tick, dead.position, found->growth, + corpse_budget->phenotype.extent, dead.inventory}); next_lives.erase(found); } std::ranges::sort(next_corpses, {}, &CellularOwnedCorpse::organism); @@ -806,10 +815,9 @@ CellularLives::advance_tick(std::span intents) { return std::unexpected{CellularLivesError::overflow}; next_heat.energy.value += proposal->heat_released.value; for (const auto& update : proposal->patches) { - const auto found = std::ranges::lower_bound( - habitat_config.patches, update.address, {}, [](const auto& patch) { - return patch.address; - }); + const auto found = + std::ranges::lower_bound(habitat_config.patches, update.address, {}, + [](const auto& patch) { return patch.address; }); if (found == habitat_config.patches.end() || found->address != update.address) return std::unexpected{CellularLivesError::habitat}; found->stock = update.stock; @@ -817,23 +825,36 @@ CellularLives::advance_tick(std::span intents) { auto next_habitat = biology::CellularHabitat::create(std::move(habitat_config)); if (!next_habitat) return std::unexpected{CellularLivesError::habitat}; - const auto total = validate_state( - state_->world, state_->region, state_->frame, - CellularLivesConfig{state_->config.area, proposal->end_tick, - state_->config.ambient_temperature_millikelvin}, - state_->nursery_config, **next_habitat, next_lives, next_corpses, next_archive, - state_->nursery, next_heat, state_->opening_total); + const auto total = + validate_state(state_->world, state_->region, state_->frame, + CellularLivesConfig{state_->config.area, proposal->end_tick, + state_->config.ambient_temperature_millikelvin}, + state_->nursery_config, **next_habitat, next_lives, next_corpses, + next_archive, state_->nursery, next_heat, state_->opening_total); if (!total) return std::unexpected{total.error()}; - auto next = std::make_unique( - state_->world, state_->region, state_->frame, - CellularLivesConfig{state_->config.area, proposal->end_tick, - state_->config.ambient_temperature_millikelvin}, - state_->nursery_config, - std::move(*next_habitat), std::move(next_lives), std::move(next_corpses), next_archive, - state_->nursery, next_heat, state_->opening_total, *total); + auto next = + std::make_unique(state_->world, state_->region, state_->frame, + CellularLivesConfig{state_->config.area, proposal->end_tick, + state_->config.ambient_temperature_millikelvin}, + state_->nursery_config, std::move(*next_habitat), + std::move(next_lives), std::move(next_corpses), next_archive, + state_->nursery, next_heat, state_->opening_total, *total); + std::vector feedback; + feedback.reserve(proposal->lives.size()); + for (const auto& result : proposal->lives) { + const auto input = + std::ranges::lower_bound(ordered, result.life_id, {}, + [](const CellularLifeIntent& value) { return value.life_id; }); + feedback.push_back({result.life_id, proposal->end_tick, result.candidate.reserve_after, + result.candidate.maintenance_cost, result.candidate.movement_cost, + result.candidate.movement_status, result.candidate.intake_status}); + if (input == ordered.end() || input->life_id != result.life_id) + return std::unexpected{CellularLivesError::invalid_intents}; + } state_.swap(next); + last_tick_feedback_ = std::move(feedback); return std::move(*proposal); } } // namespace evo::server diff --git a/src/server/session/region_sessions.cpp b/src/server/session/region_sessions.cpp index 7e70a7b..b71cc42 100644 --- a/src/server/session/region_sessions.cpp +++ b/src/server/session/region_sessions.cpp @@ -422,7 +422,8 @@ struct RegionSessions::Impl { c.protocol_version == network::session_protocol_v6) || (config.accept_cellular_actions && (c.protocol_version == network::session_protocol_v7 || - c.protocol_version == network::session_protocol_v8))))) + c.protocol_version == network::session_protocol_v8 || + c.protocol_version == network::session_protocol_v9))))) return close(c, SessionCloseReason::protocol, now); if (const auto* ack = std::get_if(&*decoded)) return replication.acknowledge(c.session, *ack); @@ -589,9 +590,10 @@ struct RegionSessions::Impl { (wire_version == network::session_protocol_v5 && !primitive_v5) || (wire_version == network::session_protocol_v6 && !cellular_v6) || ((wire_version == network::session_protocol_v7 || - wire_version == network::session_protocol_v8) && + wire_version == network::session_protocol_v8 || + wire_version == network::session_protocol_v9) && !cellular_actions) || - wire_version > network::session_protocol_v8) + wire_version > network::session_protocol_v9) return close(c, SessionCloseReason::unsupported_version, now); } else if (wire_version != c.protocol_version) { return close(c, SessionCloseReason::unsupported_version, now); @@ -615,7 +617,8 @@ struct RegionSessions::Impl { } if (const auto* m = std::get_if(&*decoded)) { if (c.protocol_version != network::session_protocol_v7 && - c.protocol_version != network::session_protocol_v8) + c.protocol_version != network::session_protocol_v8 && + c.protocol_version != network::session_protocol_v9) return close(c, SessionCloseReason::protocol, now); if (const auto receipt = intents.admit(c.session, *m, admission())) answer(c, *receipt); @@ -881,10 +884,15 @@ bool RegionSessions::send_cellular_perception(const network::AccountId& account, auto& m = *impl_; for (const auto& c : m.connections) { if (c.state != Impl::State::established || - c.protocol_version != network::session_protocol_v8 || c.account_id != account) + (c.protocol_version != network::session_protocol_v8 && + c.protocol_version != network::session_protocol_v9) || + c.account_id != account) continue; + auto delivered = perception; + if (c.protocol_version == network::session_protocol_v8) + delivered.diagnostics.reset(); return m.endpoint - ->send_message(c.id, network::encode_session(perception, c.protocol_version)) + ->send_message(c.id, network::encode_session(delivered, c.protocol_version)) .has_value(); } return false; @@ -897,7 +905,8 @@ void RegionSessions::clear_cellular_perceptions(std::uint64_t tick) { const network::CellularPerception cleared{tick, 0, {}}; for (const auto& c : m.connections) if (c.state == Impl::State::established && - c.protocol_version == network::session_protocol_v8) + (c.protocol_version == network::session_protocol_v8 || + c.protocol_version == network::session_protocol_v9)) m.send(c, cleared); } diff --git a/tests/cellular_lives_tests.cpp b/tests/cellular_lives_tests.cpp index 84769e4..7ff5938 100644 --- a/tests/cellular_lives_tests.cpp +++ b/tests/cellular_lives_tests.cpp @@ -1,12 +1,12 @@ -#include "evo/server/lifecycle/cellular_lives.hpp" #include "evo/server/lifecycle/cellular_batch.hpp" +#include "evo/server/lifecycle/cellular_lives.hpp" #include #include #include #include -#include #include +#include #include #include #include @@ -46,12 +46,13 @@ void refresh_checksum(std::vector& bytes) { } std::unique_ptr empty_cellular_owner(Inventory funding, - std::vector sites) { + std::vector sites) { auto primitive = PrimitiveLives::create({91, 1, 1, 17, funding, std::move(sites)}); CHECK(primitive); auto habitat = evo::biology::CellularHabitat::create({91, 1, 1, 1'000, 1'000, {}}); CHECK(habitat); - auto owner = CellularLives::create(**primitive, std::move(*habitat), {{1'000, 1'000}, 0, 293'150}); + auto owner = + CellularLives::create(**primitive, std::move(*habitat), {{1'000, 1'000}, 0, 293'150}); CHECK(owner); CHECK((*owner)->ambient_temperature_millikelvin() == 293'150); return std::move(*owner); @@ -75,8 +76,8 @@ void reserved_birth_is_funded_and_detached() { const auto endowment = evo::biology::primitive_endowment(); CHECK(endowment); CHECK((*born)->nursery().energy.value == opening->nursery().energy.value - - endowment->reserve.energy.value - - endowment->construction.value); + endowment->reserve.energy.value - + endowment->construction.value); CHECK((*born)->heat().energy.value == endowment->construction.value && (*born)->current_total() == (*born)->opening_total() && (*born)->archive().recorded() == 1); @@ -92,13 +93,12 @@ void reserved_birth_is_funded_and_detached() { CHECK(opening->birth_candidate(request, {502, 502, 502, 502, {2}}).error() == CellularLivesError::invalid_tick); const std::array next_tick{CellularLifeIntent{allocation.life, {}}}; - CHECK((*born)->advance_tick(next_tick) && (*born)->tick() == 2 && - (*born)->lives().size() == 1); + CHECK((*born)->advance_tick(next_tick) && (*born)->tick() == 2 && (*born)->lives().size() == 1); auto trace_base = empty_cellular_owner(founder_price(1), {{0, 0}}); CellularBatch birth_batch; - birth_batch.header = {trace_base->world(), trace_base->region(), trace_base->frame(), {}, {}, - trace_base->tick(), trace_base->tick() + 1}; + birth_batch.header = {trace_base->world(), trace_base->region(), trace_base->frame(), {}, {}, + trace_base->tick(), trace_base->tick() + 1}; birth_batch.header.predecessor.fill(3); birth_batch.header.predecessor_digest.fill(4); CellularTickIntents birth_tick; @@ -134,18 +134,16 @@ void reserved_birth_is_funded_and_detached() { CellularLivesError::insufficient_funding); CHECK(unfunded->snapshot() == unfunded_snapshot && unfunded->lives().empty()); - auto occupied_primitive = - PrimitiveLives::create({92, 1, 1, 17, founder_price(2), {{0, 0}}}); + auto occupied_primitive = PrimitiveLives::create({92, 1, 1, 17, founder_price(2), {{0, 0}}}); CHECK(occupied_primitive); LifeRequest existing{}; existing.account.back() = 11; existing.action = 1; CHECK((*occupied_primitive)->spawn(existing, {701, 701, 701, 701, {1}})); - auto occupied_habitat = - evo::biology::CellularHabitat::create({92, 1, 1, 1'000, 1'000, {}}); + auto occupied_habitat = evo::biology::CellularHabitat::create({92, 1, 1, 1'000, 1'000, {}}); CHECK(occupied_habitat); auto occupied = CellularLives::create(**occupied_primitive, std::move(*occupied_habitat), - {{1'000, 1'000}, 1, 293'150}); + {{1'000, 1'000}, 1, 293'150}); CHECK(occupied); LifeRequest arriving{}; arriving.account.back() = 12; @@ -168,8 +166,8 @@ void owner_advances_and_closes_death_atomically() { 90, 1, 7, 3'000, 2'000, {{{0, 0}, 29, {{10}, {{{10}, {10}, {10}, {10}}}}}}}; auto habitat = evo::biology::CellularHabitat::create(std::move(habitat_config)); CHECK(habitat); - auto created = CellularLives::create( - **primitive, std::move(*habitat), {{3'000, 2'000}, 1, 293'150}); + auto created = + CellularLives::create(**primitive, std::move(*habitat), {{3'000, 2'000}, 1, 293'150}); CHECK(created); auto& owner = **created; CHECK(owner.world() == 90 && owner.region() == 1 && owner.frame() == 7); @@ -187,9 +185,8 @@ void owner_advances_and_closes_death_atomically() { visible_patches->front().address == (evo::biology::CellularPatchAddress{0, 0}) && visible_patches->front().available == owner.habitat().patches()[0].stock); CHECK(owner.perceive(0).error() == CellularLivesError::invalid_life); - const std::array invalid_motion{ - CellularLifeIntent{first_life_id, {}}, - CellularLifeIntent{second_life_id, {{1'001, 0}, std::nullopt}}}; + const std::array invalid_motion{CellularLifeIntent{first_life_id, {}}, + CellularLifeIntent{second_life_id, {{1'001, 0}, std::nullopt}}}; const auto partial = owner.advance_tick(invalid_motion); CHECK(!partial && partial.error() == CellularLivesError::region_step && owner.tick() == 1 && owner.lives().size() == 2); @@ -200,6 +197,15 @@ void owner_advances_and_closes_death_atomically() { CHECK(fed && owner.tick() == 2 && owner.lives().size() == 2); CHECK(fed->lives.size() == 2 && fed->lives[0].candidate.intake_status == evo::biology::CellularIntakeStatus::acquired); + CHECK(owner.last_tick_feedback().size() == 2 && + owner.last_tick_feedback()[0].tick == owner.tick() && + owner.last_tick_feedback()[0].life_id == owner.lives()[0].identity.life && + owner.last_tick_feedback()[0].reserve == fed->lives[0].candidate.reserve_after && + owner.last_tick_feedback()[0].maintenance_cost == + fed->lives[0].candidate.maintenance_cost && + owner.last_tick_feedback()[0].movement == + evo::biology::CellularMovementStatus::not_requested && + owner.last_tick_feedback()[0].intake == evo::biology::CellularIntakeStatus::acquired); CHECK(owner.habitat().patches()[0].stock == (Inventory{{0}, {}})); CHECK(owner.current_total() == owner.opening_total()); @@ -207,6 +213,7 @@ void owner_advances_and_closes_death_atomically() { CHECK(saved.size() <= max_cellular_lives_bytes); auto recovered = CellularLives::restore(saved, 90, 1, 7, owner.tick()); CHECK(recovered && (*recovered)->snapshot() == saved); + CHECK((*recovered)->last_tick_feedback().empty()); // Transient presentation is not restored. CHECK(!CellularLives::restore(saved, 91, 1, 7, owner.tick())); CHECK(!CellularLives::restore(saved, 90, 1, 7, owner.tick() + 1)); auto damaged = saved; @@ -216,8 +223,7 @@ void owner_advances_and_closes_death_atomically() { auto unsupported = saved; unsupported[8] = static_cast(cellular_lives_format + 1); refresh_checksum(unsupported); - const auto unsupported_result = - CellularLives::restore(unsupported, 90, 1, 7, owner.tick()); + const auto unsupported_result = CellularLives::restore(unsupported, 90, 1, 7, owner.tick()); CHECK(!unsupported_result && unsupported_result.error() == CellularLivesError::version); auto outside = saved; std::fill(outside.begin() + 300, outside.begin() + 308, uint8_t{0xff}); @@ -225,8 +231,8 @@ void owner_advances_and_closes_death_atomically() { CHECK(!CellularLives::restore(outside, 90, 1, 7, owner.tick())); CellularBatch replay_batch; - replay_batch.header = {owner.world(), owner.region(), owner.frame(), {}, {}, owner.tick(), - owner.tick() + 2}; + replay_batch.header = {owner.world(), owner.region(), owner.frame(), {}, {}, + owner.tick(), owner.tick() + 2}; replay_batch.header.predecessor.fill(0x31); replay_batch.header.predecessor_digest.fill(0x41); for (int i = 0; i < 2; ++i) { @@ -246,8 +252,8 @@ void owner_advances_and_closes_death_atomically() { const std::array actions{action}; const auto action_bytes = encode_cellular_action_block(actions, owner.tick(), owner.tick() + 2); CHECK(action_bytes && action_bytes->size() == cellular_action_record_bytes); - const auto decoded_actions = decode_cellular_action_block( - *action_bytes, owner.tick(), owner.tick() + 2); + const auto decoded_actions = + decode_cellular_action_block(*action_bytes, owner.tick(), owner.tick() + 2); CHECK(decoded_actions && *decoded_actions == std::vector{action}); CHECK(match_cellular_actions_to_trace(owner, *decoded_batch, *decoded_actions)); auto changed_action = action; @@ -257,6 +263,8 @@ void owner_advances_and_closes_death_atomically() { CellularActionBlockError::mismatch); const auto replayed_batch = replay_cellular_batch(owner, *decoded_batch); CHECK(replayed_batch && (*replayed_batch)->tick() == owner.tick() + 2); + CHECK((*replayed_batch)->last_tick_feedback().size() == owner.lives().size() && + (*replayed_batch)->last_tick_feedback()[0].tick == (*replayed_batch)->tick()); CHECK((*replayed_batch)->current_total() == owner.opening_total()); auto short_batch = *batch_bytes; short_batch.pop_back(); diff --git a/tests/cellular_step_tests.cpp b/tests/cellular_step_tests.cpp index 6c2443e..3758876 100644 --- a/tests/cellular_step_tests.cpp +++ b/tests/cellular_step_tests.cpp @@ -45,6 +45,7 @@ void maintenance_motion_and_intake_conserve_one_tick() { CHECK(step->begin_tick == 12 && step->end_tick == 13); CHECK(step->position == CellularPosition{10, 0}); CHECK(step->maintenance_cost.value == 5 && step->movement_cost.value == 50); + CHECK(step->movement_status == CellularMovementStatus::moved); CHECK(step->heat_after.energy.value == 55); CHECK(step->reserve_after == Inventory{{195}, {{{5}, {2}, {1}, {0}}}}); if (step->intake_status != CellularIntakeStatus::acquired || !step->intake) @@ -73,6 +74,7 @@ void maintenance_precedes_motion_and_intake() { CHECK(step); CHECK(step->position == CellularPosition{0, 0}); CHECK(step->movement_cost.value == 0); + CHECK(step->movement_status == CellularMovementStatus::insufficient_reserve); CHECK(step->maintenance_cost.value == 5 && step->heat_after.energy.value == 5); CHECK(step->intake_status == CellularIntakeStatus::acquired); CHECK(step->reserve_after.energy.value == 99); // Intake cannot fund this tick's move. diff --git a/tests/quic_sessions.cpp b/tests/quic_sessions.cpp index 938a763..aa350b7 100644 --- a/tests/quic_sessions.cpp +++ b/tests/quic_sessions.cpp @@ -611,7 +611,7 @@ void cellular_v5_spawn_and_v7_action_share_region_sessions(const Files& f) { } actions; FakeLedger control; Harness h{ - f, 4, 64, &control, [&](server::RegionSessionsConfig& c) { + f, 5, 64, &control, [&](server::RegionSessionsConfig& c) { c.actions = &actions; c.accept_cellular_actions = true; c.create_primitive_life = [](const AccountId&, std::uint64_t, std::uint64_t request) { @@ -624,13 +624,16 @@ void cellular_v5_spawn_and_v7_action_share_region_sessions(const Files& f) { auto spawn_client = h.connect(h.issuer.grant(account(0xB5), 1), session_protocol_v5); auto action_client = h.connect(h.issuer.grant(account(0xB7), 1), session_protocol_v7); const auto owner_account = account(0xB8); - auto perception_owner = h.connect(h.issuer.grant(owner_account, 1), session_protocol_v8); - auto other_owner = h.connect(h.issuer.grant(account(0xB9), 1), session_protocol_v8); + auto perception_owner = h.connect(h.issuer.grant(owner_account, 1), session_protocol_v9); + auto other_owner = h.connect(h.issuer.grant(account(0xB9), 1), session_protocol_v9); + const auto legacy_account = account(0xBA); + auto legacy_owner = h.connect(h.issuer.grant(legacy_account, 1), session_protocol_v8); CHECK(h.settled(*spawn_client) && spawn_client->state() == RegionSessionState::established); CHECK(h.settled(*action_client) && action_client->state() == RegionSessionState::established); CHECK(h.settled(*perception_owner) && perception_owner->state() == RegionSessionState::established); CHECK(h.settled(*other_owner) && other_owner->state() == RegionSessionState::established); + CHECK(h.settled(*legacy_owner) && legacy_owner->state() == RegionSessionState::established); CHECK(spawn_client->request_primitive_life(44)); std::vector birth_outcomes; @@ -665,19 +668,41 @@ void cellular_v5_spawn_and_v7_action_share_region_sessions(const Files& f) { action_client->view()->organisms.front().id == 901); CHECK(action_client->set_interest({1, 0, 0, 2})); - const CellularPerception perception{101, 901, {{1'000, 2'000, 3, {4, 5, 6, 7}}}}; + CellularPerception perception{101, 901, {{1'000, 2'000, 3, {4, 5, 6, 7}}}}; + CellularLifeDiagnostics diagnostics; + diagnostics.reserve_energy_pj = 88; + diagnostics.reserve_matter_fg = {9, 10, 11, 12}; + diagnostics.maintenance_cost_pj = 3; + diagnostics.movement_cost_pj = 2; + diagnostics.movement = CellularMovementResult::moved; + diagnostics.intake = CellularIntakeResult::acquired; + diagnostics.has_tick_effects = true; + perception.diagnostics = diagnostics; CHECK(h.server->send_cellular_perception(owner_account, perception)); CHECK(h.until([&] { return perception_owner->cellular_perception() && perception_owner->cellular_perception()->tick == perception.tick; })); CHECK(*perception_owner->cellular_perception() == perception); - CHECK(!other_owner->cellular_perception() && !action_client->cellular_perception()); + CHECK(!other_owner->cellular_perception() && !legacy_owner->cellular_perception() && + !action_client->cellular_perception()); + CHECK(h.server->send_cellular_perception(legacy_account, perception)); + CHECK(h.until([&] { + return legacy_owner->cellular_perception() && + legacy_owner->cellular_perception()->tick == perception.tick; + })); + auto legacy_perception = perception; + legacy_perception.diagnostics.reset(); + CHECK(*legacy_owner->cellular_perception() == legacy_perception); h.server->clear_cellular_perceptions(102); CHECK(h.until([&] { return perception_owner->cellular_perception() && perception_owner->cellular_perception()->tick == 102; })); + CHECK(h.until([&] { + return legacy_owner->cellular_perception() && + legacy_owner->cellular_perception()->tick == 102; + })); CHECK(perception_owner->cellular_perception()->life_identity == 0 && perception_owner->cellular_perception()->patches.empty()); }