From d98d71e7c0b91f3e3002866fe6e7299882372f54 Mon Sep 17 00:00:00 2001 From: dingo Date: Thu, 15 Oct 2026 19:11:28 -0400 Subject: [PATCH] Show what a bud costs (ADR-0129 amendment) The v10 cellular perception carries the bud cost after the diagnostics block: the energy (construction plus the bud's provision) and matter per channel one bud takes from a fully grown cell's reserve. biology::bud_cost reads it from fund_bud itself, so the shown cost is exactly the charge; the server caches it per genome. The client shows it and names the channels the reserve is short on. v10 clients must be rebuilt. Co-Authored-By: Claude Opus 5.5 --- apps/evo-client/cellular_controls.hpp | 11 +++++ apps/evo-client/panels.cpp | 27 ++++++++++++ apps/evo-server/cellular_bridge.hpp | 22 ++++++++++ .../region/dev-session-route/listener.yaml | 2 +- .../0129-cellular-readable-play-signals.md | 12 ++++++ docs/engineering/VALIDATION.md | 15 +++++++ include/evo/biology/budding.hpp | 7 ++++ include/evo/network/session_protocol.hpp | 5 +++ .../session-v10-cellular-mutation-action.md | 13 ++++++ src/biology/budding.cpp | 18 ++++++++ src/network/protocol/session_protocol.cpp | 42 +++++++++++++------ tests/budding_tests.cpp | 31 ++++++++++++++ tests/cellular_intent_protocol_tests.cpp | 33 +++++++++++++++ 13 files changed, 225 insertions(+), 13 deletions(-) diff --git a/apps/evo-client/cellular_controls.hpp b/apps/evo-client/cellular_controls.hpp index 7f31e9d..141c484 100644 --- a/apps/evo-client/cellular_controls.hpp +++ b/apps/evo-client/cellular_controls.hpp @@ -3,6 +3,7 @@ #include "evo/client/organism_view.hpp" #include "evo/network/session_protocol.hpp" #include +#include #include #include #include @@ -71,6 +72,16 @@ steer_cellular(const CellularControls& controls, client::Intention intention, result.intention.move_y = component(intention.move_y); return result; } +// ADR-0129 amendment: what one bud needs that the reserve lacks, per channel: energy +// first, then C, N, P and other matter. Nothing set when it can be paid; a genome that +// cannot bud reports a zero cost, which this treats as affordable (the button is the guide). +[[nodiscard]] inline std::array bud_shortfall(const network::CellularLifeDiagnostics& d) { + std::array short_on{}; + short_on[0] = d.bud_cost_energy_pj > d.reserve_energy_pj; + for (std::size_t i = 0; i < d.bud_cost_matter_fg.size(); ++i) + short_on[i + 1] = d.bud_cost_matter_fg[i] > d.reserve_matter_fg[i]; + return short_on; +} [[nodiscard]] inline bool can_fund(const network::CellularOfferedCandidate& candidate, const network::CellularLifeDiagnostics& reserve) { if (candidate.cost_energy_pj > reserve.reserve_energy_pj) diff --git a/apps/evo-client/panels.cpp b/apps/evo-client/panels.cpp index 4f9fd30..187ca4f 100644 --- a/apps/evo-client/panels.cpp +++ b/apps/evo-client/panels.cpp @@ -1,6 +1,7 @@ #include "panels.hpp" #include "evo/client/labels.hpp" #include +#include #include #include #include @@ -738,6 +739,32 @@ void cell_actions(const PanelView& v, PanelActions& a) { ImGui::EndDisabled(); disabled_wrapped("Engulf attempts nearby prey. Bud funds an independent descendant; you keep " "your cell. Automatic feeding resumes after the action tick."); + // ADR-0129 amendment: what a bud takes from the reserve once the cell is fully grown. + if (const auto* perception = v.cellular_perception; perception && perception->diagnostics) { + const auto& d = *perception->diagnostics; + const bool can_bud = d.bud_cost_energy_pj != 0 || + std::ranges::any_of(d.bud_cost_matter_fg, + [](std::uint64_t value) { return value != 0; }); + if (!can_bud) { + disabled_wrapped("This cell's genome cannot bud."); + } else { + ImGui::TextWrapped("Bud needs: %llu pJ\nC %llu / N %llu / P %llu / other %llu fg", + static_cast(d.bud_cost_energy_pj), + static_cast(d.bud_cost_matter_fg[0]), + static_cast(d.bud_cost_matter_fg[1]), + static_cast(d.bud_cost_matter_fg[2]), + static_cast(d.bud_cost_matter_fg[3])); + constexpr std::array names{"energy", "carbon", "nitrogen", "phosphorus", "other"}; + std::string missing; + const auto short_on = bud_shortfall(d); + for (std::size_t i = 0; i < short_on.size(); ++i) + if (short_on[i]) + missing += std::string{missing.empty() ? "" : ", "} + names.at(i); + if (!missing.empty()) + ImGui::TextColored(amber, "Short on %s", missing.c_str()); + disabled_wrapped("A bud also needs the cell fully grown and undamaged."); + } + } if (!play) return; if (const auto& pending = play->pending(); pending) { diff --git a/apps/evo-server/cellular_bridge.hpp b/apps/evo-server/cellular_bridge.hpp index 1062481..da00434 100644 --- a/apps/evo-server/cellular_bridge.hpp +++ b/apps/evo-server/cellular_bridge.hpp @@ -1,5 +1,6 @@ #pragma once +#include "evo/biology/budding.hpp" #include "evo/client/organism_view.hpp" #include "evo/network/session_grant.hpp" #include "evo/server/cellular_region_host.hpp" @@ -18,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -209,6 +211,11 @@ class CellularBridge { 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; + if (const auto cost = bud_cost_for(state, life.identity.genome)) { + diagnostics.bud_cost_energy_pj = cost->energy.value; + for (std::size_t i = 0; i < diagnostics.bud_cost_matter_fg.size(); ++i) + diagnostics.bud_cost_matter_fg[i] = cost->matter[i].value; + } const auto feedback = std::ranges::lower_bound(state.last_tick_feedback(), life.identity.life, {}, &server::CellularLifeTickFeedback::life_id); @@ -356,6 +363,20 @@ class CellularBridge { } private: + // ADR-0129 amendment: a genome's bud cost is pure for this region's temperature, and + // deriving it scans up to a thousand bodies, so it is computed once per genome. + std::optional bud_cost_for(const server::CellularLives& state, + std::uint64_t genome_id) { + if (const auto known = bud_costs_.find(genome_id); known != bud_costs_.end()) + return known->second; + const auto genome = state.archive().find(genome_id); + if (!genome) + return std::nullopt; + if (bud_costs_.size() >= 4096) + bud_costs_.clear(); // Bounded: recomputing is only slower. + return bud_costs_[genome_id] = + biology::bud_cost(*genome, state.ambient_temperature_millikelvin()); + } static std::uint64_t wall_seconds() { return static_cast(std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()) @@ -382,6 +403,7 @@ class CellularBridge { friend bool operator==(const SentOffer&, const SentOffer&) = default; }; std::vector offers_; // Last offer view sent to each owner. + std::map> bud_costs_; // By genome. std::vector> owned_; // At last replication. bool shutdown_{}; }; diff --git a/deploy/kubernetes/region/dev-session-route/listener.yaml b/deploy/kubernetes/region/dev-session-route/listener.yaml index 0447d93..8a8ac62 100644 --- a/deploy/kubernetes/region/dev-session-route/listener.yaml +++ b/deploy/kubernetes/region/dev-session-route/listener.yaml @@ -12,7 +12,7 @@ spec: env: - {name: REGION_SESSION, value: "dev"} - {name: REGION_SESSION_LISTEN, value: "0.0.0.0:7400"} - - {name: REGION_SESSION_WINDOW, value: "dev-20261015T213553-53423:1792110953"} + - {name: REGION_SESSION_WINDOW, value: "dev-20261015T231128-88134:1792116688"} - {name: REGION_M10_MAX_GRANT_LIFETIME, value: "14400"} ports: # QUIC (ADR-0090): UDP only; nothing listens on TCP 7400. diff --git a/docs/adr/0129-cellular-readable-play-signals.md b/docs/adr/0129-cellular-readable-play-signals.md index b286882..423613a 100644 --- a/docs/adr/0129-cellular-readable-play-signals.md +++ b/docs/adr/0129-cellular-readable-play-signals.md @@ -45,3 +45,15 @@ inferring it from a vanished view. v1-v9 are unchanged. touching cell too large even with the thinnest membrane, 7 a touching cell small enough but defended by its membrane. Value 3 (no eligible prey) is no longer sent. Who is engulfed is unchanged. + +## Amendment (2026-10-15, bud cost) + +A playtester could not tell why a bud failed or what it needed. The v10 cellular +perception now carries the bud cost after the diagnostics block: the energy +(construction plus the bud's 30 s provision) and matter per channel that one bud +takes from the reserve once the cell is fully grown and undamaged. It is computed by +the same `fund_bud` the owner uses, against an ample reserve (`biology::bud_cost`), +so a reserve covering the shown cost on every channel is exactly a reserve that can +bud. The server caches it per genome. It does not depend on tick effects; all zero +means the genome cannot bud. The client shows the cost and names the channels it is +short on. The v10 perception grows by 40 bytes, so v10 clients must be rebuilt. diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index cb42c85..08476ad 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -7675,3 +7675,18 @@ Tests: `client-auth` covers profile validation, the credential names, the refusa of an invalid profile, and `prompt=login` in the authorization URL. Executed locally: WSL clang-19 `client-probe` with clang-tidy 14/14 plus the Xvfb X11 lifecycle check; MSVC `client-package` 18/18; clang-format 19. + +## 2026-10-15 — bud cost in v10 diagnostics (CI pending) + +ADR-0129 amendment: the v10 cellular perception carries what one bud takes from a +fully grown cell's reserve (energy and matter per channel), from +`biology::bud_cost`, which reads `fund_bud`'s own charge. The client shows it and +names the channels the reserve is short on. + +Tests: `budding_tests` checks that a reserve equal to the shown cost buds and one +unit short on any channel refuses, and that a non-reproducing genome has no cost. +`cellular_intent_protocol_tests` checks the 40-byte v10 extension: offsets, round +trip with and without tick effects, v9 refusal, and refusal of a cleared view with +a cost. Executed locally: those unit tests, the WSL PostgreSQL suite, clang-tidy-19 +on the changed sources, WSL client 14/14 with clang-tidy plus the Xvfb check, MSVC +`client-package` 18/18, and clang-format 19. diff --git a/include/evo/biology/budding.hpp b/include/evo/biology/budding.hpp index bb8cdf1..1680db2 100644 --- a/include/evo/biology/budding.hpp +++ b/include/evo/biology/budding.hpp @@ -4,6 +4,7 @@ #include #include +#include // M11.5 budding (ADR-0098 decision 1, ADR-0125): a funded proposal, detached from any // owner. The parent pays its ADR-0032 reproduction investment and a starting provision @@ -33,4 +34,10 @@ enum class BuddingError : std::uint8_t { [[nodiscard]] std::expected fund_bud(const Genome& genome, GrowthState parent_growth, const accounting::Inventory& parent_reserve, std::uint32_t temperature_millikelvin); +// What one bud takes from a fully grown, undamaged parent of this genome at this temperature +// (ADR-0129 amendment): investment matter per channel, and construction energy plus the +// bud's provision; exactly what `fund_bud` charges such a parent. Nullopt when the genome +// cannot bud or cannot build a cell from its investment. +[[nodiscard]] std::optional bud_cost(const Genome& genome, + std::uint32_t temperature_millikelvin); } // namespace evo::biology diff --git a/include/evo/network/session_protocol.hpp b/include/evo/network/session_protocol.hpp index 59f3307..752164b 100644 --- a/include/evo/network/session_protocol.hpp +++ b/include/evo/network/session_protocol.hpp @@ -280,6 +280,11 @@ struct CellularLifeDiagnostics { bool has_tick_effects{}; CellularEngulfResult engulf{CellularEngulfResult::not_reported}; // v10 only. CellularBudOutcome bud{CellularBudOutcome::not_reported}; // v10 only. + // v10 only (ADR-0129 amendment): what one bud takes from the reserve once the cell is + // fully grown: construction and provision energy plus matter per channel. All zero + // when this cell's genome cannot bud. Independent of tick effects. + std::uint64_t bud_cost_energy_pj{}; + std::array bud_cost_matter_fg{}; friend bool operator==(const CellularLifeDiagnostics&, const CellularLifeDiagnostics&) = default; }; diff --git a/schemas/network/session-v10-cellular-mutation-action.md b/schemas/network/session-v10-cellular-mutation-action.md index 2dc2d01..1bfc6df 100644 --- a/schemas/network/session-v10-cellular-mutation-action.md +++ b/schemas/network/session-v10-cellular-mutation-action.md @@ -82,6 +82,19 @@ and byte 78 the bud result (0 not reported, 1 not requested, 2 budded, 3 not able, 4 insufficient funding, 5 no placement); bytes 79-81 stay zero, and both results are zero without tick effects. v9 keeps bytes 77-81 zero. +A v10 diagnostics block is followed by the bud cost (ADR-0129 amendment, 2026-10-15), +so v10 patch records start at payload offset 122: + +| Payload offset | Size | Field | +|---:|---:|---| +| 82 | 8 | bud cost energy, pJ: construction plus the bud's provision | +| 90 | 32 | bud cost C, N, P, other, each fg: the investment matter | + +It is what one bud takes from the reserve once the cell is fully grown and undamaged, +exactly as the owner funds it, and it does not depend on tick effects. All zero means +this genome cannot bud. A cleared zero-life view must carry an all-zero bud cost. v9 +has no bud cost. + Type 27 (server to owner, reliable stream, v10 only) is exactly 24 bytes: the tick (u64, nonzero), the life identity (u64, nonzero), the cause (u8: 0 not recorded, 1 starved, 2 engulfed) and seven zero bytes. diff --git a/src/biology/budding.cpp b/src/biology/budding.cpp index 6ee5978..ce18218 100644 --- a/src/biology/budding.cpp +++ b/src/biology/budding.cpp @@ -77,4 +77,22 @@ std::expected fund_bud(const Genome& genome, Growth } return std::unexpected{BuddingError::insufficient_funding}; // Too little to build a cell. } + +std::optional bud_cost(const Genome& genome, + std::uint32_t temperature_millikelvin) { + // Fund against an ample reserve and read the charge: one code path decides both. + constexpr std::uint64_t ample = std::uint64_t{1} << 60U; + const accounting::Inventory unlimited{ + {ample}, + {accounting::Femtograms{ample}, accounting::Femtograms{ample}, + accounting::Femtograms{ample}, accounting::Femtograms{ample}}}; + const auto funded = fund_bud(genome, {1000, 0}, unlimited, temperature_millikelvin); + if (!funded) + return std::nullopt; + accounting::Inventory cost; + cost.energy.value = ample - funded->parent_reserve.energy.value; + for (std::size_t channel = 0; channel < cost.matter.size(); ++channel) + cost.matter[channel].value = ample - funded->parent_reserve.matter[channel].value; + return cost; +} } // namespace evo::biology diff --git a/src/network/protocol/session_protocol.cpp b/src/network/protocol/session_protocol.cpp index b9c78d9..9ff232b 100644 --- a/src/network/protocol/session_protocol.cpp +++ b/src/network/protocol/session_protocol.cpp @@ -23,6 +23,10 @@ constexpr std::size_t cellular_perception_prefix_bytes = 8 + 8 + 2; constexpr std::size_t cellular_diagnostics_bytes = 64; constexpr std::size_t cellular_perception_v9_prefix_bytes = cellular_perception_prefix_bytes + cellular_diagnostics_bytes; +// v10 appends the bud cost (energy and four matter channels, ADR-0129 amendment). +constexpr std::size_t cellular_bud_cost_bytes = std::size_t{5} * 8; +constexpr std::size_t cellular_perception_v10_prefix_bytes = + cellular_perception_v9_prefix_bytes + cellular_bud_cost_bytes; constexpr std::size_t cellular_perceived_patch_bytes = 8 + 8 + (4 * 8); constexpr std::size_t intention_receipt_bytes = 8 + 1 + 8; constexpr std::size_t simulated_bytes = std::size_t{3} * 8; @@ -195,11 +199,15 @@ std::vector encode_session(const SessionMessage& message, std::uin ended && (version != session_protocol_v10 || ended->tick == 0 || ended->life_identity == 0 || ended->cause > CellularDeathReason::engulfed)) return {}; - // v9 has no room for engulf and bud effects; only v10 reports them (ADR-0129). + // v9 has no room for engulf and bud effects or the bud cost; only v10 reports them + // (ADR-0129). if (const auto* perception = std::get_if(&message); perception && version != session_protocol_v10 && perception->diagnostics && (perception->diagnostics->engulf != CellularEngulfResult::not_reported || - perception->diagnostics->bud != CellularBudOutcome::not_reported)) + perception->diagnostics->bud != CellularBudOutcome::not_reported || + perception->diagnostics->bud_cost_energy_pj != 0 || + std::ranges::any_of(perception->diagnostics->bud_cost_matter_fg, + [](std::uint64_t value) { return value != 0; }))) return {}; if (const auto* action = std::get_if(&message); action && (action->engulf || action->bud) && @@ -334,12 +342,13 @@ std::vector encode_session(const SessionMessage& message, std::uin return out; } std::vector operator()(const CellularPerception& m) const { - auto out = header(SessionMessageType::cellular_perception, - (version == session_protocol_v9 || version == session_protocol_v10 - ? cellular_perception_v9_prefix_bytes - : cellular_perception_prefix_bytes) + - m.patches.size() * cellular_perceived_patch_bytes, - version); + auto out = + header(SessionMessageType::cellular_perception, + (version == session_protocol_v10 ? cellular_perception_v10_prefix_bytes + : version == session_protocol_v9 ? cellular_perception_v9_prefix_bytes + : cellular_perception_prefix_bytes) + + m.patches.size() * cellular_perceived_patch_bytes, + version); put(out, m.tick, 8); put(out, m.life_identity, 8); put(out, m.patches.size(), 2); @@ -359,6 +368,10 @@ std::vector encode_session(const SessionMessage& message, std::uin out.push_back(d ? static_cast(d->engulf) : 0); out.push_back(d ? static_cast(d->bud) : 0); put(out, 0, 3); + put(out, d ? d->bud_cost_energy_pj : 0, 8); + for (const auto value : + d ? d->bud_cost_matter_fg : std::array{}) + put(out, value, 8); } else { put(out, 0, 5); } @@ -692,9 +705,9 @@ std::expected decode_session(std::span(get(payload, 16, 2)); @@ -726,6 +739,9 @@ std::expected decode_session(std::span(payload[77]); d.bud = static_cast(payload[78]); + d.bud_cost_energy_pj = get(payload, 82, 8); + for (std::size_t channel = 0; channel < d.bud_cost_matter_fg.size(); ++channel) + d.bud_cost_matter_fg[channel] = get(payload, 90 + channel * 8, 8); } d.movement = static_cast(payload[74]); d.intake = static_cast(payload[75]); @@ -739,7 +755,9 @@ std::expected decode_session(std::spanparent_reserve == Inventory{}); + for (std::size_t c = 0; c <= cost.matter.size(); ++c) { + auto short_one = cost; + auto& unit = c == 0 ? short_one.energy.value : short_one.matter[c - 1].value; + if (unit == 0) + continue; + --unit; + CHECK(fund_bud(genome, {1000, 0}, short_one, temperate).error() == + BuddingError::insufficient_funding); + } + const auto barren = Genome::create({9, + 1, + 2, + {}, + {{Capability::energy_conversion, 400, 0}, + {Capability::membrane, 1, 0}, + {Capability::target_size, 8'000'000, 0}}}); + CHECK(barren && !bud_cost(*barren, temperate)); +} } // namespace int main() { @@ -120,6 +150,7 @@ int main() { a_grown_parent_funds_the_largest_bud_its_investment_builds(); every_short_channel_refuses_and_spends_nothing(); only_a_grown_undamaged_reproducing_parent_buds(); + the_shown_bud_cost_is_exactly_what_funding_takes(); } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; diff --git a/tests/cellular_intent_protocol_tests.cpp b/tests/cellular_intent_protocol_tests.cpp index 85a2516..5aa8069 100644 --- a/tests/cellular_intent_protocol_tests.cpp +++ b/tests/cellular_intent_protocol_tests.cpp @@ -316,6 +316,39 @@ void v10_effects_and_life_end_are_owner_only() { idle.diagnostics->bud = CellularBudOutcome::budded; // Effects need tick effects. CHECK(encode_session(SessionMessage{idle}, session_protocol_v10).empty()); + // ADR-0129 amendment: v10 appends the bud cost (payload 82..121), with or without + // tick effects; v9 cannot carry it, and a cleared view carries none. + auto costed = perception; + costed.diagnostics->bud_cost_energy_pj = 0x0102030405060708; + costed.diagnostics->bud_cost_matter_fg = {11, 22, 33, 44}; + const auto costed_bytes = encode_session(SessionMessage{costed}, session_protocol_v10); + CHECK(costed_bytes.size() == bytes.size() && costed_bytes[session_header_bytes + 82] == 0x08 && + costed_bytes[session_header_bytes + 89] == 0x01 && + costed_bytes[session_header_bytes + 90] == 11 && + costed_bytes[session_header_bytes + 114] == 44); + CHECK(std::get(*decode_session(costed_bytes)) == costed); + CHECK(bytes.size() == session_header_bytes + 18 + 64 + 40); + auto resting = costed; + resting.diagnostics->has_tick_effects = false; + resting.diagnostics->maintenance_cost_pj = 0; + resting.diagnostics->movement = CellularMovementResult::not_reported; + resting.diagnostics->intake = CellularIntakeResult::not_reported; + resting.diagnostics->engulf = CellularEngulfResult::not_reported; + resting.diagnostics->bud = CellularBudOutcome::not_reported; + CHECK(std::get(*decode_session( + encode_session(SessionMessage{resting}, session_protocol_v10))) == resting); + auto v9_costed = resting; + v9_costed.diagnostics->engulf = CellularEngulfResult::not_reported; + CHECK(encode_session(SessionMessage{v9_costed}, session_protocol_v9).empty()); + v9_costed.diagnostics->bud_cost_energy_pj = 0; + v9_costed.diagnostics->bud_cost_matter_fg = {}; + CHECK(!encode_session(SessionMessage{v9_costed}, session_protocol_v9).empty()); + auto cleared = encode_session(SessionMessage{CellularPerception{40, 0, {}, std::nullopt}}, + session_protocol_v10); + CHECK(!cleared.empty() && decode_session(cleared)); + cleared[session_header_bytes + 90] = 1; // A cleared view with a bud cost. + CHECK(!decode_session(cleared)); + const CellularLifeEnded ended{41, 7, CellularDeathReason::engulfed}; const auto ended_bytes = encode_session(SessionMessage{ended}); std::uint16_t version{};