diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b16da0..e559075 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,7 +69,7 @@ evo_owned_target(evo_accounting) add_library(evo_biology STATIC src/biology/genome.cpp src/biology/development.cpp src/biology/metabolism.cpp src/biology/mutation.cpp src/biology/lineage.cpp src/biology/founder.cpp - src/biology/cellular_motion.cpp) + src/biology/cellular_motion.cpp src/biology/cellular_uptake.cpp) add_library(evo::biology ALIAS evo_biology) target_include_directories(evo_biology PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include") target_link_libraries(evo_biology PUBLIC evo::accounting) @@ -429,6 +429,11 @@ if(BUILD_TESTING) evo_owned_target(evo-cellular-motion-tests) add_test(NAME cellular-motion-contracts COMMAND evo-cellular-motion-tests) set_tests_properties(cellular-motion-contracts PROPERTIES TIMEOUT 60) + add_executable(evo-cellular-uptake-tests tests/cellular_uptake_tests.cpp) + target_link_libraries(evo-cellular-uptake-tests PRIVATE evo::biology) + evo_owned_target(evo-cellular-uptake-tests) + add_test(NAME cellular-uptake-contracts COMMAND evo-cellular-uptake-tests) + set_tests_properties(cellular-uptake-contracts PROPERTIES TIMEOUT 60) add_executable(evo-primitive-lives-tests tests/primitive_lives_tests.cpp) target_link_libraries(evo-primitive-lives-tests PRIVATE evo::lifecycle) evo_owned_target(evo-primitive-lives-tests) diff --git a/docs/adr/0101-cellular-microhabitat-resources.md b/docs/adr/0101-cellular-microhabitat-resources.md index dc6a14e..7179aff 100644 --- a/docs/adr/0101-cellular-microhabitat-resources.md +++ b/docs/adr/0101-cellular-microhabitat-resources.md @@ -38,6 +38,10 @@ not depend on graphics, networking or database code. server-owned atomic operation. C, N, P, other and energy remain separate conserved channels. Any conversion between tags needs an explicit biological rule; water, incident energy and heat are not edible reserve by relabeling. +- An absorption proposal moves each acquired channel into the living cell's + reserve, not directly into its fully formed body or growth state. Food matter + remains there for later funded development/reproduction; usable energy is + available for movement and maintenance. Absorption alone builds no tissue. - The habitat opening, depletion, organism balances and transfer/retry state belong to the cellular checkpoint closure. A crash cannot publish only one side of a transfer. Profile-3 creation checkpoints and M6 field snapshots diff --git a/docs/adr/0102-cellular-spatial-trial.md b/docs/adr/0102-cellular-spatial-trial.md index 86f93e0..dd0deea 100644 --- a/docs/adr/0102-cellular-spatial-trial.md +++ b/docs/adr/0102-cellular-spatial-trial.md @@ -43,8 +43,9 @@ patches without client authority over speed or position. Checked quotient/remainder arithmetic is required; overflow refuses the whole step. A zero per-tick speed or zero displacement costs zero. The movement resolver reports a proposed cost; the state owner applies the move only if the reserve - can fund that cost. This resolver does not also spend maintenance, which - remains a separate tick-ordered biological cost. + can fund that cost, then transfers the spent energy to the heat sink in the + same candidate state. This resolver does not also spend maintenance, which + remains a separate tick-ordered cost from reserve to heat. - The body footprint determines which patch squares it overlaps. Contact candidates are returned in canonical patch-address order; an exact boundary touch counts as contact. Feeding limits, simultaneous claims, predation diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index cc9bdc5..6677e75 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -5838,3 +5838,19 @@ build target idled at 0 CPU and was stopped; the test executable was compiled directly and then run by CTest. Linux GCC, ASan+UBSan and server/PostgreSQL process qualification remain pending CI. This small resolver does not make 11.3 or a playtest-ready cellular stage complete. + +## 2026-10-10 — M11.3c-1 channel-limited uptake proposal (CI pending) + +Added a pure uptake proposal that computes each 50 ms intake capacity from the +development-v2 per-second budget, then independently bounds energy and C/N/P/other +by patch stock and reserve headroom. It returns both post-transfer inventories and +the exact amount acquired; it does not mutate either side, so the cellular host can +publish them as one candidate. ADR-0101 now states that absorbed material accumulates +in reserve without changing the fully formed body. + +Executed locally: MSVC 19.44 warning-as-error Debug compile/link and focused CTest +(1/1 passed); direct Release/NDEBUG compile/run (passed); clang-format 19.1.5 +dry-run; clang-tidy 19.1.5 against the generated compile database; and +`git diff --check`. Linux GCC and sanitizer runs are unavailable locally; the full +cellular-owner conservation, contention, durable publication and PostgreSQL process +checks remain pending CI. This helper does not yet close the server-side transfer. diff --git a/include/evo/biology/cellular_uptake.hpp b/include/evo/biology/cellular_uptake.hpp new file mode 100644 index 0000000..04e31e1 --- /dev/null +++ b/include/evo/biology/cellular_uptake.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "evo/biology/metabolism.hpp" + +#include +#include + +namespace evo::biology { +struct CellularUptakeProposal { + accounting::Inventory patch_after; + accounting::Inventory reserve_after; + accounting::Inventory acquired; + auto operator<=>(const CellularUptakeProposal&) const = default; +}; +enum class CellularUptakeError : std::uint8_t { unsupported_rate_period, overflow }; + +// Pure, channel-by-channel uptake proposal for one 50 ms tick from a patch +// already confirmed in contact. The caller commits patch and reserve together. +[[nodiscard]] std::expected +propose_cellular_uptake(accounting::Inventory patch, accounting::Inventory reserve, + const MetabolicBudget& budget) noexcept; +} // namespace evo::biology diff --git a/src/biology/cellular_uptake.cpp b/src/biology/cellular_uptake.cpp new file mode 100644 index 0000000..d8bc12b --- /dev/null +++ b/src/biology/cellular_uptake.cpp @@ -0,0 +1,50 @@ +#include "evo/biology/cellular_uptake.hpp" + +#include "evo/accounting/rates.hpp" +#include "evo/biology/cellular_motion.hpp" + +#include +#include + +namespace evo::biology { +namespace { +constexpr std::uint64_t rate_period_microseconds = 1'000'000; + +std::uint64_t take(std::uint64_t stock, std::uint64_t reserve, std::uint64_t rate_limit) noexcept { + const auto room = std::numeric_limits::max() - reserve; + return std::min({stock, rate_limit, room}); +} +} // namespace + +std::expected +propose_cellular_uptake(accounting::Inventory patch, accounting::Inventory reserve, + const MetabolicBudget& budget) noexcept { + if (budget.rate_microseconds != rate_period_microseconds) + return std::unexpected{CellularUptakeError::unsupported_rate_period}; + + const auto energy_limit = + accounting::per_tick(budget.intake_limit.energy.value, cellular_motion_tick_microseconds, + accounting::Rounding::down); + if (!energy_limit) + return std::unexpected{CellularUptakeError::overflow}; + CellularUptakeProposal proposal{patch, reserve, {}}; + const auto energy = take(patch.energy.value, reserve.energy.value, *energy_limit); + proposal.acquired.energy.value = energy; + proposal.patch_after.energy.value -= energy; + proposal.reserve_after.energy.value += energy; + + for (std::size_t channel = 0; channel < proposal.acquired.matter.size(); ++channel) { + const auto limit = + accounting::per_tick(budget.intake_limit.matter[channel].value, + cellular_motion_tick_microseconds, accounting::Rounding::down); + if (!limit) + return std::unexpected{CellularUptakeError::overflow}; + const auto amount = + take(patch.matter[channel].value, reserve.matter[channel].value, *limit); + proposal.acquired.matter[channel].value = amount; + proposal.patch_after.matter[channel].value -= amount; + proposal.reserve_after.matter[channel].value += amount; + } + return proposal; +} +} // namespace evo::biology diff --git a/tests/cellular_uptake_tests.cpp b/tests/cellular_uptake_tests.cpp new file mode 100644 index 0000000..e0743a2 --- /dev/null +++ b/tests/cellular_uptake_tests.cpp @@ -0,0 +1,77 @@ +#include "evo/biology/cellular_uptake.hpp" + +#include +#include +#include +#include +#include + +namespace { +using namespace evo::biology; +using evo::accounting::Inventory; +void check(bool value, std::source_location at = std::source_location::current()) { + if (!value) + throw std::runtime_error("cellular uptake line " + std::to_string(at.line())); +} +#define CHECK(...) check(static_cast(__VA_ARGS__)) + +MetabolicBudget budget() { + MetabolicBudget result{}; + result.rate_microseconds = 1'000'000; + result.intake_limit = {{1'001}, {{{101}, {53}, {31}, {17}}}}; + return result; +} + +void channel_limited_atomic_proposal() { + constexpr auto maximum = std::numeric_limits::max(); + const Inventory patch{{101}, {{{100}, {1}, {100}, {50}}}}; + const Inventory reserve{{49}, {{{maximum - 3}, {0}, {0}, {0}}}}; + const auto result = propose_cellular_uptake(patch, reserve, budget()); + CHECK(result); + CHECK(result->acquired == Inventory{{50}, {{{3}, {1}, {1}, {0}}}}); + CHECK(result->patch_after == Inventory{{51}, {{{97}, {0}, {99}, {50}}}}); + CHECK(result->reserve_after == Inventory{{99}, {{{maximum}, {1}, {1}, {0}}}}); + CHECK(result->patch_after.energy.value + result->reserve_after.energy.value == + patch.energy.value + reserve.energy.value); + for (std::size_t channel = 0; channel < patch.matter.size(); ++channel) { + CHECK(patch.matter[channel].value - result->patch_after.matter[channel].value == + result->acquired.matter[channel].value); + CHECK(result->reserve_after.matter[channel].value - reserve.matter[channel].value == + result->acquired.matter[channel].value); + } +} + +void empty_and_full_channels() { + constexpr auto maximum = std::numeric_limits::max(); + const auto no_stock = propose_cellular_uptake({}, {}, budget()); + CHECK(no_stock && no_stock->acquired == Inventory{} && no_stock->patch_after == Inventory{}); + + Inventory patch{}; + patch.energy.value = maximum; + patch.matter.fill({maximum}); + Inventory reserve{}; + reserve.energy.value = maximum; + reserve.matter.fill({maximum}); + const auto full = propose_cellular_uptake(patch, reserve, budget()); + CHECK(full && full->acquired == Inventory{}); + CHECK(full->patch_after == patch && full->reserve_after == reserve); +} + +void unsupported_rate_period() { + auto invalid = budget(); + invalid.rate_microseconds = 50'000; + CHECK(!propose_cellular_uptake({}, {}, invalid)); +} +} // namespace + +int main() { + try { + channel_limited_atomic_proposal(); + empty_and_full_channels(); + unsupported_rate_period(); + std::puts("PASS cellular uptake: finite channel limits and atomic proposal"); + } catch (const std::exception& error) { + std::fprintf(stderr, "%s\n", error.what()); + return 1; + } +}