Add in-memory cellular life owner
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 41s
Foundation / checks (ecs) (push) Successful in 1m50s
Foundation / checks (postgres) (push) Failing after 50s
Foundation / checks (tsan) (push) Failing after 33s
Foundation / checks (network) (push) Failing after 3m6s
Foundation / checks (client) (push) Failing after 4m24s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 41s
Foundation / checks (ecs) (push) Successful in 1m50s
Foundation / checks (postgres) (push) Failing after 50s
Foundation / checks (tsan) (push) Failing after 33s
Foundation / checks (network) (push) Failing after 3m6s
Foundation / checks (client) (push) Failing after 4m24s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
This commit is contained in:
+6
-1
@@ -80,7 +80,7 @@ evo_owned_target(evo_biology)
|
|||||||
|
|
||||||
# M11.2 portable lifecycle orchestration; no dependency from the foundation proof.
|
# M11.2 portable lifecycle orchestration; no dependency from the foundation proof.
|
||||||
add_library(evo_lifecycle STATIC src/server/lifecycle/primitive_lives.cpp
|
add_library(evo_lifecycle STATIC src/server/lifecycle/primitive_lives.cpp
|
||||||
src/server/lifecycle/primitive_batch.cpp)
|
src/server/lifecycle/primitive_batch.cpp src/server/lifecycle/cellular_lives.cpp)
|
||||||
add_library(evo::lifecycle ALIAS evo_lifecycle)
|
add_library(evo::lifecycle ALIAS evo_lifecycle)
|
||||||
target_link_libraries(evo_lifecycle PUBLIC evo::biology)
|
target_link_libraries(evo_lifecycle PUBLIC evo::biology)
|
||||||
evo_owned_target(evo_lifecycle)
|
evo_owned_target(evo_lifecycle)
|
||||||
@@ -455,6 +455,11 @@ if(BUILD_TESTING)
|
|||||||
target_link_libraries(evo-primitive-lives-tests PRIVATE evo::lifecycle)
|
target_link_libraries(evo-primitive-lives-tests PRIVATE evo::lifecycle)
|
||||||
evo_owned_target(evo-primitive-lives-tests)
|
evo_owned_target(evo-primitive-lives-tests)
|
||||||
add_test(NAME primitive-lives COMMAND evo-primitive-lives-tests)
|
add_test(NAME primitive-lives COMMAND evo-primitive-lives-tests)
|
||||||
|
add_executable(evo-cellular-lives-tests tests/cellular_lives_tests.cpp)
|
||||||
|
target_link_libraries(evo-cellular-lives-tests PRIVATE evo::lifecycle)
|
||||||
|
evo_owned_target(evo-cellular-lives-tests)
|
||||||
|
add_test(NAME cellular-lives-owner COMMAND evo-cellular-lives-tests)
|
||||||
|
set_tests_properties(cellular-lives-owner PROPERTIES TIMEOUT 60)
|
||||||
target_link_libraries(evo-metabolism-tests PRIVATE evo::processes)
|
target_link_libraries(evo-metabolism-tests PRIVATE evo::processes)
|
||||||
evo_owned_target(evo-metabolism-tests)
|
evo_owned_target(evo-metabolism-tests)
|
||||||
add_test(NAME metabolism-contracts COMMAND evo-metabolism-tests)
|
add_test(NAME metabolism-contracts COMMAND evo-metabolism-tests)
|
||||||
|
|||||||
@@ -124,12 +124,15 @@ producer, feeding, reproduction and episode fixtures built on that ledger; they
|
|||||||
are bounded experiments, not organism biology. `src/biology` holds the immutable
|
are bounded experiments, not organism biology. `src/biology` holds the immutable
|
||||||
genome record, the deterministic development that derives a phenotype from it and
|
genome record, the deterministic development that derives a phenotype from it and
|
||||||
the metabolic budgets that give capability a cost, and the seeded mutation
|
the metabolic budgets that give capability a cost, and the seeded mutation
|
||||||
catalog that proposes successors and the archive that keeps them. No fixture reads
|
catalog that proposes successors and the archive that keeps them. Synthetic
|
||||||
a genome yet.
|
process fixtures remain separate from organism biology.
|
||||||
`apps/evo-sim` owns CLI/file I/O
|
`apps/evo-sim` owns CLI/file I/O
|
||||||
and reporting. `tests` holds contract,
|
and reporting. `tests` holds contract,
|
||||||
fuzz and measurement programs. Future biology, world, networking and persistence
|
fuzz and measurement programs. `src/server/lifecycle` owns funded primitive
|
||||||
modules are documented boundaries; they are added when they have real behavior.
|
founder creation and an in-memory cellular tick owner over the finite habitat.
|
||||||
|
The cellular owner is not yet wired to durable profile-4 recovery or session
|
||||||
|
intentions, so it is not a playable client path. `evo::simulation` remains
|
||||||
|
graphics-, network- and database-independent.
|
||||||
|
|
||||||
World0 is persistent state. No normal configure, build, test or CI job may create,
|
World0 is persistent state. No normal configure, build, test or CI job may create,
|
||||||
reset, deploy to or delete it. Gitea builds the proof image for Harbor; a bounded
|
reset, deploy to or delete it. Gitea builds the proof image for Harbor; a bounded
|
||||||
|
|||||||
@@ -151,6 +151,10 @@ scoped change before starting the next segment.
|
|||||||
- 11.3c-5: ADR-0107 closes unaffordable maintenance in the same tick, moving body
|
- 11.3c-5: ADR-0107 closes unaffordable maintenance in the same tick, moving body
|
||||||
and reserve into a corpse candidate and accumulated heat to an explicit output.
|
and reserve into a corpse candidate and accumulated heat to an explicit output.
|
||||||
The durable heat owner and lineage/checkpoint publication remain open.
|
The durable heat owner and lineage/checkpoint publication remain open.
|
||||||
|
- 11.3c-6: `server::CellularLives` adopts funded profile-3 founders into one
|
||||||
|
in-memory cellular owner, applies shared-habitat ticks atomically, records
|
||||||
|
starvation corpses/deaths and enforces region-wide conservation. Checkpoint
|
||||||
|
encoding, durable publication and online session execution remain open.
|
||||||
- 11.3d contract: ADR-0108/0109 and `schemas/cellular-lives-v1.md` define a separate
|
- 11.3d contract: ADR-0108/0109 and `schemas/cellular-lives-v1.md` define a separate
|
||||||
profile-4 checkpoint closure for lives, habitat, corpses, heat and lineage.
|
profile-4 checkpoint closure for lives, habitat, corpses, heat and lineage.
|
||||||
No profile-4 code or durable tick publication is implemented yet.
|
No profile-4 code or durable tick publication is implemented yet.
|
||||||
|
|||||||
@@ -6028,3 +6028,21 @@ profile 3. This is documentation only;
|
|||||||
no codec, SQL admission or recovery behavior is implemented or validated by
|
no codec, SQL admission or recovery behavior is implemented or validated by
|
||||||
this entry. `git diff --check` is the applicable local check. Profile-4 code,
|
this entry. `git diff --check` is the applicable local check. Profile-4 code,
|
||||||
PostgreSQL CI and crash/restart tests remain required.
|
PostgreSQL CI and crash/restart tests remain required.
|
||||||
|
|
||||||
|
## 2026-10-10 — M11.3c-6 in-memory cellular owner (CI pending)
|
||||||
|
|
||||||
|
Added `server::CellularLives` as a portable lifecycle owner over funded
|
||||||
|
profile-3 opening state. It derives each life budget from its archived genome,
|
||||||
|
advances a complete stable-ID intention set against one shared habitat, and
|
||||||
|
atomically applies living results, death records, corpses, heat and patch stock
|
||||||
|
only when the region-wide totals still reconcile. The focused test covers
|
||||||
|
contested intake, a late invalid intention with no partial advance, and both
|
||||||
|
founders reaching terminal death while retaining complete lineage and inventory.
|
||||||
|
|
||||||
|
Executed locally: direct warning-as-error MSVC 19.44 Debug and Release/NDEBUG
|
||||||
|
compile/run; Debug CTest `cellular-lives-owner` (1/1 passed); CMake configure and
|
||||||
|
generation registered the target; and `git diff --check`. The CMake/Ninja build
|
||||||
|
idled without starting a compiler, so the focused target was compiled directly.
|
||||||
|
`clang-format` and `clang-tidy` are unavailable on this Windows PATH. Linux
|
||||||
|
sanitizer/CI, profile-4 codec/restore, durable publication and session wiring
|
||||||
|
remain pending.
|
||||||
|
|||||||
@@ -0,0 +1,94 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "evo/biology/cellular_region_step.hpp"
|
||||||
|
#include "evo/server/lifecycle/primitive_lives.hpp"
|
||||||
|
|
||||||
|
#include <compare>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <expected>
|
||||||
|
#include <memory>
|
||||||
|
#include <span>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace evo::server {
|
||||||
|
struct CellularOwnedLife {
|
||||||
|
LifeAccount account{};
|
||||||
|
LifeAllocation identity{};
|
||||||
|
std::uint64_t source{};
|
||||||
|
biology::GrowthState growth{};
|
||||||
|
biology::CellularPosition position{};
|
||||||
|
accounting::Inventory body{};
|
||||||
|
accounting::Inventory reserve{};
|
||||||
|
auto operator<=>(const CellularOwnedLife&) const = default;
|
||||||
|
};
|
||||||
|
struct CellularOwnedCorpse {
|
||||||
|
std::uint64_t organism{};
|
||||||
|
std::uint64_t died_tick{};
|
||||||
|
biology::CellularPosition position{};
|
||||||
|
accounting::Inventory inventory{};
|
||||||
|
auto operator<=>(const CellularOwnedCorpse&) const = default;
|
||||||
|
};
|
||||||
|
struct CellularLifeIntent {
|
||||||
|
std::uint64_t life_id{};
|
||||||
|
biology::CellularStepIntent intent{};
|
||||||
|
auto operator<=>(const CellularLifeIntent&) const = default;
|
||||||
|
};
|
||||||
|
struct CellularLivesConfig {
|
||||||
|
biology::CellularArea area{};
|
||||||
|
std::uint64_t tick{};
|
||||||
|
std::uint32_t ambient_temperature_millikelvin{};
|
||||||
|
};
|
||||||
|
enum class CellularLivesError : std::uint8_t {
|
||||||
|
identity,
|
||||||
|
invalid_config,
|
||||||
|
invalid_life,
|
||||||
|
duplicate_life,
|
||||||
|
invalid_tick,
|
||||||
|
invalid_intents,
|
||||||
|
biology,
|
||||||
|
region_step,
|
||||||
|
archive,
|
||||||
|
habitat,
|
||||||
|
overflow,
|
||||||
|
conservation
|
||||||
|
};
|
||||||
|
|
||||||
|
// In-memory authoritative cellular owner. It consumes funded profile-3 opening
|
||||||
|
// state but never mutates or reinterprets that checkpoint. Each tick is applied
|
||||||
|
// to a detached candidate and swapped only after life, corpse, heat, habitat and
|
||||||
|
// archive closure all validate. Durable profile-4 publication remains external.
|
||||||
|
class CellularLives {
|
||||||
|
public:
|
||||||
|
[[nodiscard]] static std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
|
||||||
|
create(const PrimitiveLives& opening, std::unique_ptr<biology::CellularHabitat> habitat,
|
||||||
|
CellularLivesConfig config);
|
||||||
|
CellularLives(const CellularLives&) = delete;
|
||||||
|
CellularLives& operator=(const CellularLives&) = delete;
|
||||||
|
~CellularLives();
|
||||||
|
|
||||||
|
[[nodiscard]] std::uint64_t world() const noexcept;
|
||||||
|
[[nodiscard]] std::uint64_t region() const noexcept;
|
||||||
|
[[nodiscard]] std::uint64_t frame() const noexcept;
|
||||||
|
[[nodiscard]] std::uint64_t tick() const noexcept;
|
||||||
|
[[nodiscard]] biology::CellularArea area() const noexcept;
|
||||||
|
[[nodiscard]] std::span<const CellularOwnedLife> lives() const noexcept;
|
||||||
|
[[nodiscard]] std::span<const CellularOwnedCorpse> corpses() const noexcept;
|
||||||
|
[[nodiscard]] const biology::CellularHabitat& habitat() const noexcept;
|
||||||
|
[[nodiscard]] const biology::LineageArchive& archive() const noexcept;
|
||||||
|
[[nodiscard]] accounting::Inventory nursery() const noexcept;
|
||||||
|
[[nodiscard]] accounting::Inventory heat() const noexcept;
|
||||||
|
[[nodiscard]] accounting::Inventory opening_total() const noexcept;
|
||||||
|
[[nodiscard]] accounting::Inventory current_total() const noexcept;
|
||||||
|
|
||||||
|
// Advances every living cell exactly one 50 ms tick. Intentions are keyed by
|
||||||
|
// stable life ID and must cover the living set exactly. Any refusal leaves
|
||||||
|
// this owner byte-for-byte unchanged.
|
||||||
|
[[nodiscard]] std::expected<biology::CellularRegionStepCandidate, CellularLivesError>
|
||||||
|
advance_tick(std::span<const CellularLifeIntent> intents);
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct State;
|
||||||
|
explicit CellularLives(std::unique_ptr<State> state);
|
||||||
|
std::unique_ptr<State> state_;
|
||||||
|
};
|
||||||
|
} // namespace evo::server
|
||||||
@@ -0,0 +1,266 @@
|
|||||||
|
#include "evo/server/lifecycle/cellular_lives.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <limits>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace evo::server {
|
||||||
|
namespace {
|
||||||
|
bool add_inventory(accounting::Inventory& total, const accounting::Inventory& value) noexcept {
|
||||||
|
constexpr auto maximum = std::numeric_limits<std::uint64_t>::max();
|
||||||
|
if (value.energy.value > maximum - total.energy.value)
|
||||||
|
return false;
|
||||||
|
for (std::size_t channel = 0; channel < total.matter.size(); ++channel)
|
||||||
|
if (value.matter[channel].value > maximum - total.matter[channel].value)
|
||||||
|
return false;
|
||||||
|
total.energy.value += value.energy.value;
|
||||||
|
for (std::size_t channel = 0; channel < total.matter.size(); ++channel)
|
||||||
|
total.matter[channel].value += value.matter[channel].value;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::expected<biology::MetabolicBudget, CellularLivesError>
|
||||||
|
budget_for(const biology::LineageArchive& archive, const CellularOwnedLife& life,
|
||||||
|
std::uint32_t temperature) {
|
||||||
|
const auto genome = 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};
|
||||||
|
return *budget;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::expected<accounting::Inventory, CellularLivesError>
|
||||||
|
sum_state(accounting::Inventory nursery, accounting::Inventory heat,
|
||||||
|
const biology::CellularHabitat& habitat, std::span<const CellularOwnedLife> lives,
|
||||||
|
std::span<const CellularOwnedCorpse> corpses) noexcept {
|
||||||
|
auto total = nursery;
|
||||||
|
if (!add_inventory(total, heat))
|
||||||
|
return std::unexpected{CellularLivesError::overflow};
|
||||||
|
if (!add_inventory(total, habitat.totals()))
|
||||||
|
return std::unexpected{CellularLivesError::overflow};
|
||||||
|
for (const auto& life : lives)
|
||||||
|
if (!add_inventory(total, life.body) || !add_inventory(total, life.reserve))
|
||||||
|
return std::unexpected{CellularLivesError::overflow};
|
||||||
|
for (const auto& corpse : corpses)
|
||||||
|
if (!add_inventory(total, corpse.inventory))
|
||||||
|
return std::unexpected{CellularLivesError::overflow};
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
struct CellularLives::State {
|
||||||
|
std::uint64_t world{}, region{}, frame{};
|
||||||
|
CellularLivesConfig config{};
|
||||||
|
std::unique_ptr<biology::CellularHabitat> habitat;
|
||||||
|
std::vector<CellularOwnedLife> lives;
|
||||||
|
std::vector<CellularOwnedCorpse> corpses;
|
||||||
|
biology::LineageArchive archive;
|
||||||
|
accounting::Inventory nursery{};
|
||||||
|
accounting::Inventory heat{};
|
||||||
|
accounting::Inventory opening_total{};
|
||||||
|
accounting::Inventory current_total{};
|
||||||
|
|
||||||
|
State(std::uint64_t world_id, std::uint64_t region_id, std::uint64_t frame_id,
|
||||||
|
CellularLivesConfig cfg, std::unique_ptr<biology::CellularHabitat> map,
|
||||||
|
std::vector<CellularOwnedLife> living, std::vector<CellularOwnedCorpse> 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},
|
||||||
|
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) : state_{std::move(state)} {}
|
||||||
|
CellularLives::~CellularLives() = default;
|
||||||
|
|
||||||
|
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
|
||||||
|
CellularLives::create(const PrimitiveLives& opening,
|
||||||
|
std::unique_ptr<biology::CellularHabitat> habitat,
|
||||||
|
CellularLivesConfig config) {
|
||||||
|
if (!habitat)
|
||||||
|
return std::unexpected{CellularLivesError::invalid_config};
|
||||||
|
const auto& nursery_config = opening.config();
|
||||||
|
const auto& map = habitat->config();
|
||||||
|
if (nursery_config.world == 0 || nursery_config.region == 0 || nursery_config.frame == 0 ||
|
||||||
|
map.world != nursery_config.world || map.region != nursery_config.region ||
|
||||||
|
map.frame != nursery_config.frame || config.area.width_um == 0 ||
|
||||||
|
config.area.height_um == 0 || config.area.width_um != map.width_um ||
|
||||||
|
config.area.height_um != map.height_um || config.ambient_temperature_millikelvin == 0 ||
|
||||||
|
opening.lives().size() > biology::max_cellular_tick_lives ||
|
||||||
|
opening.balances().size() != 2 + 2 * nursery_config.sites.size())
|
||||||
|
return std::unexpected{CellularLivesError::invalid_config};
|
||||||
|
|
||||||
|
std::vector<CellularOwnedLife> lives;
|
||||||
|
lives.reserve(opening.lives().size());
|
||||||
|
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)
|
||||||
|
return std::unexpected{CellularLivesError::invalid_life};
|
||||||
|
if (primitive.body.value == 0 || primitive.reserve.value == 0)
|
||||||
|
return std::unexpected{CellularLivesError::invalid_life};
|
||||||
|
const auto body_index = static_cast<std::size_t>(primitive.body.value - 1);
|
||||||
|
const auto reserve_index = static_cast<std::size_t>(primitive.reserve.value - 1);
|
||||||
|
if (body_index >= opening.balances().size() || reserve_index >= opening.balances().size())
|
||||||
|
return std::unexpected{CellularLivesError::invalid_life};
|
||||||
|
lives.push_back({primitive.request.account,
|
||||||
|
primitive.identity,
|
||||||
|
primitive.source,
|
||||||
|
primitive.growth,
|
||||||
|
{static_cast<std::uint64_t>(primitive.site.x_um),
|
||||||
|
static_cast<std::uint64_t>(primitive.site.y_um)},
|
||||||
|
opening.balances()[body_index],
|
||||||
|
opening.balances()[reserve_index]});
|
||||||
|
}
|
||||||
|
std::ranges::sort(lives, {}, [](const auto& life) { return life.identity.life; });
|
||||||
|
for (std::size_t i = 0; i < lives.size(); ++i) {
|
||||||
|
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);
|
||||||
|
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 ||
|
||||||
|
budget->body != life.body ||
|
||||||
|
life.position.x_um > config.area.width_um - budget->phenotype.extent ||
|
||||||
|
life.position.y_um > config.area.height_um - budget->phenotype.extent)
|
||||||
|
return std::unexpected{CellularLivesError::invalid_life};
|
||||||
|
}
|
||||||
|
auto initial_total = opening.totals();
|
||||||
|
if (!add_inventory(initial_total, habitat->totals()))
|
||||||
|
return std::unexpected{CellularLivesError::overflow};
|
||||||
|
const auto nursery = opening.balances()[0];
|
||||||
|
const auto heat = opening.balances()[1];
|
||||||
|
const auto current = sum_state(nursery, heat, *habitat, lives, {});
|
||||||
|
if (!current)
|
||||||
|
return std::unexpected{current.error()};
|
||||||
|
if (*current != initial_total)
|
||||||
|
return std::unexpected{CellularLivesError::conservation};
|
||||||
|
|
||||||
|
auto state = std::make_unique<State>(
|
||||||
|
nursery_config.world, nursery_config.region, nursery_config.frame, config,
|
||||||
|
std::move(habitat), std::move(lives), std::vector<CellularOwnedCorpse>{},
|
||||||
|
opening.archive(), nursery, heat, initial_total, *current);
|
||||||
|
return std::unique_ptr<CellularLives>{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; }
|
||||||
|
biology::CellularArea CellularLives::area() const noexcept { return state_->config.area; }
|
||||||
|
std::span<const CellularOwnedLife> CellularLives::lives() const noexcept { return state_->lives; }
|
||||||
|
std::span<const CellularOwnedCorpse> 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; }
|
||||||
|
accounting::Inventory CellularLives::opening_total() const noexcept {
|
||||||
|
return state_->opening_total;
|
||||||
|
}
|
||||||
|
accounting::Inventory CellularLives::current_total() const noexcept {
|
||||||
|
return state_->current_total;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::expected<biology::CellularRegionStepCandidate, CellularLivesError>
|
||||||
|
CellularLives::advance_tick(std::span<const CellularLifeIntent> intents) {
|
||||||
|
if (state_->config.tick == std::numeric_limits<std::uint64_t>::max())
|
||||||
|
return std::unexpected{CellularLivesError::invalid_tick};
|
||||||
|
if (intents.size() != state_->lives.size())
|
||||||
|
return std::unexpected{CellularLivesError::invalid_intents};
|
||||||
|
std::vector<CellularLifeIntent> ordered{intents.begin(), intents.end()};
|
||||||
|
std::ranges::sort(ordered, {}, &CellularLifeIntent::life_id);
|
||||||
|
for (std::size_t i = 0; i < ordered.size(); ++i)
|
||||||
|
if ((i != 0 && ordered[i - 1].life_id == ordered[i].life_id) ||
|
||||||
|
ordered[i].life_id != state_->lives[i].identity.life)
|
||||||
|
return std::unexpected{CellularLivesError::invalid_intents};
|
||||||
|
|
||||||
|
std::vector<biology::CellularLifeStepInput> inputs;
|
||||||
|
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);
|
||||||
|
if (!budget)
|
||||||
|
return std::unexpected{budget.error()};
|
||||||
|
inputs.push_back({life.identity.life,
|
||||||
|
life.position,
|
||||||
|
budget->phenotype.extent,
|
||||||
|
ordered[i].intent,
|
||||||
|
life.body,
|
||||||
|
life.reserve,
|
||||||
|
{},
|
||||||
|
*budget});
|
||||||
|
}
|
||||||
|
auto proposal = biology::propose_cellular_region_step(state_->config.tick, state_->config.area,
|
||||||
|
inputs, *state_->habitat);
|
||||||
|
if (!proposal)
|
||||||
|
return std::unexpected{CellularLivesError::region_step};
|
||||||
|
|
||||||
|
auto next_lives = state_->lives;
|
||||||
|
auto next_corpses = state_->corpses;
|
||||||
|
auto next_archive = state_->archive;
|
||||||
|
auto next_heat = state_->heat;
|
||||||
|
auto habitat_config = state_->habitat->config();
|
||||||
|
for (const auto& result : proposal->lives) {
|
||||||
|
const auto found = std::ranges::lower_bound(
|
||||||
|
next_lives, result.life_id, {}, [](const auto& life) { return life.identity.life; });
|
||||||
|
if (found == next_lives.end() || found->identity.life != result.life_id)
|
||||||
|
return std::unexpected{CellularLivesError::invalid_life};
|
||||||
|
found->position = result.candidate.position;
|
||||||
|
found->reserve = result.candidate.reserve_after;
|
||||||
|
if (!add_inventory(next_heat, result.candidate.heat_after))
|
||||||
|
return std::unexpected{CellularLivesError::overflow};
|
||||||
|
}
|
||||||
|
for (const auto& dead : proposal->starved) {
|
||||||
|
const auto found = std::ranges::lower_bound(
|
||||||
|
next_lives, dead.life_id, {}, [](const auto& life) { return life.identity.life; });
|
||||||
|
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};
|
||||||
|
next_corpses.push_back(
|
||||||
|
{found->identity.organism, proposal->end_tick, dead.position, dead.inventory});
|
||||||
|
next_lives.erase(found);
|
||||||
|
}
|
||||||
|
std::ranges::sort(next_corpses, {}, &CellularOwnedCorpse::organism);
|
||||||
|
if (proposal->heat_released.value >
|
||||||
|
std::numeric_limits<std::uint64_t>::max() - next_heat.energy.value)
|
||||||
|
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;
|
||||||
|
});
|
||||||
|
if (found == habitat_config.patches.end() || found->address != update.address)
|
||||||
|
return std::unexpected{CellularLivesError::habitat};
|
||||||
|
found->stock = update.stock;
|
||||||
|
}
|
||||||
|
auto next_habitat = biology::CellularHabitat::create(std::move(habitat_config));
|
||||||
|
if (!next_habitat)
|
||||||
|
return std::unexpected{CellularLivesError::habitat};
|
||||||
|
const auto total = sum_state(state_->nursery, next_heat, **next_habitat, next_lives,
|
||||||
|
next_corpses);
|
||||||
|
if (!total)
|
||||||
|
return std::unexpected{total.error()};
|
||||||
|
if (*total != state_->opening_total)
|
||||||
|
return std::unexpected{CellularLivesError::conservation};
|
||||||
|
|
||||||
|
auto next = std::make_unique<State>(
|
||||||
|
state_->world, state_->region, state_->frame,
|
||||||
|
CellularLivesConfig{state_->config.area, proposal->end_tick,
|
||||||
|
state_->config.ambient_temperature_millikelvin},
|
||||||
|
std::move(*next_habitat), std::move(next_lives), std::move(next_corpses), next_archive,
|
||||||
|
state_->nursery, next_heat, state_->opening_total, *total);
|
||||||
|
state_.swap(next);
|
||||||
|
return std::move(*proposal);
|
||||||
|
}
|
||||||
|
} // namespace evo::server
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
#include "evo/server/lifecycle/cellular_lives.hpp"
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <source_location>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
using namespace evo::server;
|
||||||
|
using evo::accounting::Inventory;
|
||||||
|
|
||||||
|
void check(bool value, std::source_location at = std::source_location::current()) {
|
||||||
|
if (!value)
|
||||||
|
throw std::runtime_error("cellular lives line " + std::to_string(at.line()));
|
||||||
|
}
|
||||||
|
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
||||||
|
|
||||||
|
Inventory founder_price(std::uint64_t count) {
|
||||||
|
const auto endowment = evo::biology::primitive_endowment();
|
||||||
|
CHECK(endowment);
|
||||||
|
auto funding = endowment->body;
|
||||||
|
funding.energy.value = endowment->reserve.energy.value + endowment->construction.value;
|
||||||
|
funding.energy.value *= count;
|
||||||
|
for (auto& channel : funding.matter)
|
||||||
|
channel.value *= count;
|
||||||
|
return funding;
|
||||||
|
}
|
||||||
|
|
||||||
|
void owner_advances_and_closes_death_atomically() {
|
||||||
|
auto primitive = PrimitiveLives::create({90, 1, 7, 17, founder_price(2), {{0, 0}, {20, 0}}});
|
||||||
|
CHECK(primitive);
|
||||||
|
LifeRequest request{};
|
||||||
|
request.account.back() = 1;
|
||||||
|
request.action = 1;
|
||||||
|
CHECK((*primitive)->spawn(request, {1, 101, 201, 301, {1}}));
|
||||||
|
request.account.back() = 2;
|
||||||
|
CHECK((*primitive)->spawn(request, {2, 102, 202, 302, {1}}));
|
||||||
|
|
||||||
|
evo::biology::CellularHabitatConfig habitat_config{
|
||||||
|
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});
|
||||||
|
CHECK(created);
|
||||||
|
auto& owner = **created;
|
||||||
|
CHECK(owner.world() == 90 && owner.region() == 1 && owner.frame() == 7);
|
||||||
|
CHECK(owner.lives().size() == 2 && owner.tick() == 1);
|
||||||
|
CHECK(owner.current_total() == owner.opening_total());
|
||||||
|
|
||||||
|
const std::array invalid{CellularLifeIntent{1, {}}, CellularLifeIntent{1, {}}};
|
||||||
|
const auto refused = owner.advance_tick(invalid);
|
||||||
|
CHECK(!refused && owner.tick() == 1 && owner.lives().size() == 2);
|
||||||
|
|
||||||
|
const auto first_life_id = owner.lives()[0].identity.life;
|
||||||
|
const auto second_life_id = owner.lives()[1].identity.life;
|
||||||
|
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);
|
||||||
|
|
||||||
|
const std::array feed{CellularLifeIntent{first_life_id, {{0, 0}, {{0, 0}}}},
|
||||||
|
CellularLifeIntent{second_life_id, {{0, 0}, {{0, 0}}}}};
|
||||||
|
const auto fed = owner.advance_tick(feed);
|
||||||
|
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.habitat().patches()[0].stock == (Inventory{{0}, {}}));
|
||||||
|
CHECK(owner.current_total() == owner.opening_total());
|
||||||
|
|
||||||
|
std::size_t steps{};
|
||||||
|
while (!owner.lives().empty() && steps < 2'000) {
|
||||||
|
std::vector<CellularLifeIntent> idle;
|
||||||
|
idle.reserve(owner.lives().size());
|
||||||
|
for (const auto& life : owner.lives())
|
||||||
|
idle.push_back({life.identity.life, {}});
|
||||||
|
const auto advanced = owner.advance_tick(idle);
|
||||||
|
CHECK(advanced && owner.current_total() == owner.opening_total());
|
||||||
|
++steps;
|
||||||
|
}
|
||||||
|
CHECK(owner.lives().empty() && owner.corpses().size() == 2 && steps < 2'000);
|
||||||
|
CHECK(owner.corpses()[0].organism == 101 && owner.corpses()[0].died_tick <= owner.tick());
|
||||||
|
CHECK(owner.corpses()[1].organism == 102 && owner.corpses()[1].died_tick <= owner.tick());
|
||||||
|
const auto lineage = owner.archive().organism(101);
|
||||||
|
CHECK(lineage && !lineage->alive && lineage->died.value == owner.corpses()[0].died_tick);
|
||||||
|
const auto second_lineage = owner.archive().organism(102);
|
||||||
|
CHECK(second_lineage && !second_lineage->alive &&
|
||||||
|
second_lineage->died.value == owner.corpses()[1].died_tick);
|
||||||
|
CHECK(owner.heat().energy.value != 0);
|
||||||
|
CHECK(owner.current_total() == owner.opening_total());
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
try {
|
||||||
|
owner_advances_and_closes_death_atomically();
|
||||||
|
std::puts("PASS cellular lives: feeding, tick ownership and terminal closure");
|
||||||
|
} catch (const std::exception& error) {
|
||||||
|
std::fprintf(stderr, "%s\n", error.what());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user