From 212ca14e83373945c584f6df7b0a91b517d780a4 Mon Sep 17 00:00:00 2001 From: dingo Date: Wed, 14 Oct 2026 06:45:48 -0400 Subject: [PATCH] Drive wildlife with a deterministic controller recorded in the trace M11.5d (ADR-0125): wildlife_intents gives each unowned cell a bud intent when it can fund one (at most once a second), else steers it to the nearest perceived food patch and feeds, else wanders. The cellular server puts these intentions into each tick's baseline, so replay and publication treat them like player input. fund_bud refuses a reserve short of the bare investment before scanning growth levels. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 2 +- apps/evo-server/main.cpp | 11 ++ docs/engineering/M11_5_DELIVERY_PLAN.md | 9 ++ docs/engineering/VALIDATION.md | 12 +++ .../server/lifecycle/wildlife_controller.hpp | 25 +++++ src/biology/budding.cpp | 4 + src/server/lifecycle/wildlife_controller.cpp | 100 ++++++++++++++++++ tests/cellular_lives_tests.cpp | 12 +++ 8 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 include/evo/server/lifecycle/wildlife_controller.hpp create mode 100644 src/server/lifecycle/wildlife_controller.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6363535..3c3e426 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,7 +81,7 @@ evo_owned_target(evo_biology) # M11.2 portable lifecycle orchestration; no dependency from the foundation proof. add_library(evo_lifecycle STATIC src/server/lifecycle/primitive_lives.cpp src/server/lifecycle/primitive_batch.cpp src/server/lifecycle/cellular_lives.cpp - src/server/lifecycle/cellular_batch.cpp) + src/server/lifecycle/cellular_batch.cpp src/server/lifecycle/wildlife_controller.cpp) add_library(evo::lifecycle ALIAS evo_lifecycle) target_link_libraries(evo_lifecycle PUBLIC evo::biology) evo_owned_target(evo_lifecycle) diff --git a/apps/evo-server/main.cpp b/apps/evo-server/main.cpp index e1f17f2..6dc3c30 100644 --- a/apps/evo-server/main.cpp +++ b/apps/evo-server/main.cpp @@ -56,6 +56,7 @@ #include "evo/processes/development_opening.hpp" #include "evo/server/cellular_session_batch.hpp" #include "evo/server/lifecycle/primitive_lives.hpp" +#include "evo/server/lifecycle/wildlife_controller.hpp" #include "evo/server/ops_http.hpp" #include "evo/server/primitive_region_host.hpp" #include "evo/server/region_host.hpp" @@ -1054,6 +1055,16 @@ int serve_cellular(Options& o) { for (const auto& life : state->lives()) baseline.push_back({life.identity.life, {}}); std::ranges::sort(baseline, {}, &server::CellularLifeIntent::life_id); + // ADR-0125: wildlife follow the deterministic controller; its intentions + // ride the trace like a player's, and the owner validates them. + if (const auto wild = server::wildlife_intents(*state, tick); wild) { + for (const auto& intent : *wild) { + const auto found = std::ranges::lower_bound( + baseline, intent.life_id, {}, &server::CellularLifeIntent::life_id); + if (found != baseline.end() && found->life_id == intent.life_id) + *found = intent; + } + } auto actions = bridge->carry(tick); auto builder = server::CellularSessionBatchBuilder::create(*state, host.committed()->head); diff --git a/docs/engineering/M11_5_DELIVERY_PLAN.md b/docs/engineering/M11_5_DELIVERY_PLAN.md index 4a1c2f6..b618ecc 100644 --- a/docs/engineering/M11_5_DELIVERY_PLAN.md +++ b/docs/engineering/M11_5_DELIVERY_PLAN.md @@ -68,3 +68,12 @@ descendant, and no special wildlife biology. offers `cellular-v3` (rules 4). A rules-4 owner refuses founder identities at or above 2^63; the allocator keeps its full range. PostgreSQL tests publish a real bud from a fed founder, restart the host and recover the wildlife cell. +- 11.5d (code): `wildlife_intents` (server/lifecycle/wildlife_controller) is the + pure controller: per unowned life, a bud intent at most once a second when + `fund_bud` would succeed; else steer to the nearest perceived patch holding + energy and feed from it (no movement once touching it); else wander, changing + direction every two seconds from the life id and tick. `serve_cellular` puts its + intentions into each tick's baseline, so they ride the trace like a player's. + `fund_bud` now refuses a reserve short of the bare investment before its growth + scan (same answer, bounded cost per wildlife tick). The rules-4 DEV region + (`cellular-v3`) follows once CI publishes images with schema v36. diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index f9ae8f0..d25d144 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -7390,3 +7390,15 @@ drift; 489 s) passes; before that the C++ suite (`postgres-check.sh publication tests; MSVC Debug 69/69, network 75/75, client-network 89/89 (the two motion-trace smokes needed a serial rerun under load); g++-14 `-Werror`, clang-tidy-19 (no new findings) and clang-format 19.1.5 on the changed sources. + +## 2026-10-14 — M11.5d wildlife controller (CI pending) + +`wildlife_intents` drives unowned lives (bud when fundable, else feed from the +nearest perceived energy patch, else wander) and `serve_cellular` records its +intentions in the trace baseline. `cellular-lives-owner` checks that only the +bud is driven, that two calls agree, that a cell touching a stocked patch feeds +without moving (or buds), and that the owner accepts the intention. + +Executed locally: MSVC Debug `cellular-lives-owner` and `budding-contracts`; +g++-14 `-Werror -Wshadow` and clang-tidy-19 on the controller, budding, owner +tests and server main; clang-format 19.1.5. diff --git a/include/evo/server/lifecycle/wildlife_controller.hpp b/include/evo/server/lifecycle/wildlife_controller.hpp new file mode 100644 index 0000000..36743a5 --- /dev/null +++ b/include/evo/server/lifecycle/wildlife_controller.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include "evo/server/lifecycle/cellular_lives.hpp" + +#include +#include +#include + +// M11.5d (ADR-0125): the bounded deterministic wildlife controller. It reads only the +// committed owner state and the tick being built, and proposes the same intentions a +// player could send. Its output is recorded in the trace, so replay never runs it, and +// the owner still validates and funds every intention. +namespace evo::server { +// A wildlife cell that could fund a bud asks for one at most once per this many ticks +// (offset by its life id), and a wandering cell keeps a direction for this many ticks. +inline constexpr std::uint64_t wildlife_bud_interval_ticks = 20; // 1 s +inline constexpr std::uint64_t wildlife_wander_interval_ticks = 40; // 2 s +inline constexpr std::int16_t wildlife_full_speed = 1'000; + +// One intention per wildlife life (zero account), sorted by life id: bud when the reserve +// funds one, else steer to and feed from the nearest perceived patch holding energy, +// else wander. Owned lives are left to their players. +[[nodiscard]] std::expected, CellularLivesError> +wildlife_intents(const CellularLives& state, std::uint64_t tick); +} // namespace evo::server diff --git a/src/biology/budding.cpp b/src/biology/budding.cpp index 19d5daf..6ee5978 100644 --- a/src/biology/budding.cpp +++ b/src/biology/budding.cpp @@ -30,6 +30,10 @@ std::expected fund_bud(const Genome& genome, Growth invested_matter += mass.value; if (invested_matter == 0) return std::unexpected{BuddingError::no_capability}; + // The cost is at least the investment, so a reserve short of it refuses before the + // growth scan (the same answer, without up to a thousand development derivations). + if (!fits(investment, parent_reserve)) + return std::unexpected{BuddingError::insufficient_funding}; // The largest realized growth whose developed body fits the invested matter on every // channel. Bodies grow with realized growth, so the scan stops at the first fit. diff --git a/src/server/lifecycle/wildlife_controller.cpp b/src/server/lifecycle/wildlife_controller.cpp new file mode 100644 index 0000000..02f25ea --- /dev/null +++ b/src/server/lifecycle/wildlife_controller.cpp @@ -0,0 +1,100 @@ +#include "evo/server/lifecycle/wildlife_controller.hpp" + +#include "evo/biology/budding.hpp" + +#include +#include +#include +#include + +namespace evo::server { +namespace { +constexpr std::int64_t half_pitch = biology::cellular_patch_pitch_um / 2; + +std::uint64_t mix(std::uint64_t life, std::uint64_t phase) noexcept { + auto value = 14695981039346656037ULL; + for (const auto part : {life, phase}) + for (unsigned index = 0; index < 8; ++index) + value = (value ^ static_cast(part >> (index * 8U))) * 1099511628211ULL; + return value; +} + +// Full speed toward (dx, dy), keeping the larger component at the limit. +biology::CellularIntent toward(std::int64_t dx, std::int64_t dy) noexcept { + const auto larger = std::max(std::llabs(dx), std::llabs(dy)); + if (larger == 0) + return {}; + return {static_cast(dx * wildlife_full_speed / larger), + static_cast(dy * wildlife_full_speed / larger)}; +} + +bool overlaps(std::uint64_t start, std::uint32_t extent, std::uint32_t patch) noexcept { + return start < std::uint64_t{patch} + biology::cellular_patch_pitch_um && + start + extent > patch; +} +} // namespace + +std::expected, CellularLivesError> +wildlife_intents(const CellularLives& state, std::uint64_t tick) { + constexpr std::array wander{{{1'000, 0}, + {707, 707}, + {0, 1'000}, + {-707, 707}, + {-1'000, 0}, + {-707, -707}, + {0, -1'000}, + {707, -707}}}; + const auto temperature = state.ambient_temperature_millikelvin(); + std::vector intents; + for (const auto& life : state.lives()) { + if (life.account != LifeAccount{}) + continue; + const auto genome = state.archive().find(life.identity.genome); + if (!genome) + return std::unexpected{CellularLivesError::archive}; + const auto budget = biology::metabolic_budget(*genome, life.growth, {temperature, {}}); + if (!budget) + return std::unexpected{CellularLivesError::biology}; + biology::CellularStepIntent intent{}; + if ((tick + life.identity.life) % wildlife_bud_interval_ticks == 0 && + biology::fund_bud(*genome, life.growth, life.reserve, temperature)) { + intent.bud = true; + intents.push_back({life.identity.life, intent}); + continue; + } + const auto perceived = state.perceive(life.identity.life); + if (!perceived) + return std::unexpected{perceived.error()}; + const auto extent = budget->phenotype.extent; + const auto centre_x = static_cast(life.position.x_um) + extent / 2; + const auto centre_y = static_cast(life.position.y_um) + extent / 2; + std::optional nearest; + std::uint64_t nearest_distance{}; + for (const auto& patch : *perceived) { + if (patch.available.energy.value == 0) + continue; + const auto dx = std::int64_t{patch.address.x_um} + half_pitch - centre_x; + const auto dy = std::int64_t{patch.address.y_um} + half_pitch - centre_y; + const auto distance = static_cast(dx * dx + dy * dy); + // Perception is ordered by address, so the first of equal distances wins. + if (!nearest || distance < nearest_distance) { + nearest = patch.address; + nearest_distance = distance; + } + } + if (nearest) { + intent.intake_target = nearest; + if (!overlaps(life.position.x_um, extent, nearest->x_um) || + !overlaps(life.position.y_um, extent, nearest->y_um)) + intent.movement = toward(std::int64_t{nearest->x_um} + half_pitch - centre_x, + std::int64_t{nearest->y_um} + half_pitch - centre_y); + } else { + intent.movement = + wander[mix(life.identity.life, tick / wildlife_wander_interval_ticks) % + wander.size()]; + } + intents.push_back({life.identity.life, intent}); + } + return intents; +} +} // namespace evo::server diff --git a/tests/cellular_lives_tests.cpp b/tests/cellular_lives_tests.cpp index 8f44480..b2493c7 100644 --- a/tests/cellular_lives_tests.cpp +++ b/tests/cellular_lives_tests.cpp @@ -1,5 +1,6 @@ #include "evo/server/lifecycle/cellular_batch.hpp" #include "evo/server/lifecycle/cellular_lives.hpp" +#include "evo/server/lifecycle/wildlife_controller.hpp" #include #include @@ -708,6 +709,17 @@ void rules_four_owner_buds_wildlife() { owner.last_tick_feedback().front().bud_life == identity); CHECK(owner.current_total() == owner.opening_total()); + // The wildlife controller drives only the unowned bud: deterministically, and here by + // feeding from the stocked patch it already touches (no movement needed). + const auto controlled = wildlife_intents(owner, owner.tick() + 1); + CHECK(controlled && controlled->size() == 1 && controlled->front().life_id == identity); + CHECK(wildlife_intents(owner, owner.tick() + 1) == controlled); + const auto& wild = controlled->front().intent; + CHECK(!wild.engulf && + (wild.bud || (wild.intake_target == evo::biology::CellularPatchAddress{0, 0} && + wild.movement == evo::biology::CellularIntent{}))); + CHECK(owner.advance_tick(std::array{CellularLifeIntent{1, feed}, controlled->front()})); + // Wildlife feeds like any cell but never earns a mutation offer. const auto child_x = child.position.x_um / 1000; const auto child_y = child.position.y_um / 1000;