feat(biology): evolve photosynthetic producers that capture light
Foundation / checks (ecs) (push) Successful in 2m25s
Foundation / checks (deploy) (push) Successful in 5m15s
Foundation / checks (network) (push) Successful in 21m56s
Foundation / checks (client) (push) Successful in 22m54s
Foundation / checks (tsan) (push) Successful in 47m4s
Foundation / checks (postgres) (push) Successful in 1h15m49s
Foundation / checks (checks) (push) Failing after 1h30m4s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped

ADR-0175 implements ADR-0136 step C with trial costs to be balanced after
playtesting. Genome schema 5 adds a photosynthesis efficiency (empty in
schemas 1-4, so earlier offers and draws are unchanged; mutation operators
29-32 are appended). Development version 5 builds the organelles as
internal membrane (p/4 of structure, upkept twice) and slows movement by
p/2 per thousand. Founder 4 and cellular rules 11 let lineages earn the
capability as an offspring plan that children express.

Owner format 9 declares the region's light. Inside each tick's detached
candidate, active unfrozen producers capture cross-section x irradiance x
efficiency (a quarter while moving), credited to the reserve and boundary
inflow together and replayed from recorded intentions. Well-lit wildlife
producers hold still. The joint owner keeps infall, water and radiation and
nests a rules-11 owner unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-19 18:06:02 -04:00
co-authored by Claude Opus 5.5
parent 51991ac7ff
commit 108f29eda5
34 changed files with 856 additions and 128 deletions
+149 -2
View File
@@ -85,8 +85,9 @@ void audit(const CellularEnvironment& value) {
expected.matter[channel].value +=
ledger.incoming.matter[channel].value - ledger.outgoing.matter[channel].value;
require(state.current_total() == expected, "joint owner broke open-boundary closure");
require(ledger.incoming == value.imported() &&
ledger.outgoing == Inventory{value.exported(), {}},
auto environmental = value.imported();
environmental.energy.value += state.captured_light().value; // A rules-11 owner's light.
require(ledger.incoming == environmental && ledger.outgoing == Inventory{value.exported(), {}},
"environment flows differ from the nested boundary ledger");
for (std::size_t index = 0; index < value.sources().size(); ++index) {
const auto& made = value.sources()[index];
@@ -238,6 +239,151 @@ void raw_and_cohort() {
<< raw->exported().value << " pJ at tick " << raw->tick() << '\n';
}
// ADR-0175: a rules-11 region whose founder is born after the upgrade (founder 4).
Owner producer_region(std::uint64_t irradiance) {
auto state = opening(7, true, 1, false);
checked(state->advance_tick({}), "producer genesis tick");
state = checked(state->migration_upgrade_candidate(), "boundary upgrade");
state = checked(state->maturation_upgrade_candidate(), "maturation upgrade");
state = checked(state->inheritance_upgrade_candidate(), "inheritance upgrade");
const CellularLight light{irradiance, 1, {irradiance == 0 ? 0 : 1ULL << 50}};
require(!state->photosynthesis_upgrade_candidate({1, 0, {1}}) &&
!state->photosynthesis_upgrade_candidate({1, 1, {0}}),
"invalid light declared");
state = checked(state->photosynthesis_upgrade_candidate(light), "photosynthesis upgrade");
require(state->rules_version() == cellular_lives_photosynthesis_rules &&
state->format_version() == cellular_lives_light_format && state->light() == light &&
!state->photosynthesis_upgrade_candidate(light),
"photosynthesis upgrade repeated, wrong versions or lost light");
LifeAccount account{};
account.back() = 1;
state = checked(state->birth_candidate({account, 1}, {100, 100, 100, 100, {state->tick()}}),
"founder 4 birth");
const auto genome = state->archive().find(100);
require(genome && genome->schema_version() == biology::genome_schema_v5 &&
!genome->carries(biology::Capability::photosynthesis),
"founder 4 is not schema 5 without photosynthesis");
return state;
}
bool photosynthetic(const CellularLives& state, std::uint64_t genome) {
const auto found = state.archive().find(genome);
return found && found->carries(biology::Capability::photosynthesis);
}
void closed(const CellularLives& state) {
const auto& ledger = state.boundary_ledger();
auto expected = state.opening_total();
expected.energy.value += ledger.incoming.energy.value - ledger.outgoing.energy.value;
require(state.current_total() == expected && ledger.incoming.energy == state.captured_light(),
"lit region broke closure or booked light outside the ledger");
}
struct Producers {
std::unique_ptr<CellularPopulation> population;
std::uint64_t plan{}, child{}, captured{}, lit_ticks{}, still{};
};
// The founder earns photosynthesis as its offspring plan (rules 10+), buds a child that
// expresses it, and that child captures light inside the owner's own tick.
Producers grow_producers(std::uint64_t irradiance) {
Producers out;
out.population = checked(
CellularPopulation::create(producer_region(irradiance), {false, 900, 1'600}), "producers");
auto& population = *out.population;
for (unsigned tick = 0; tick < 80'000 && out.plan == 0; ++tick) {
const auto& life = population.state().lives().front();
if (!life.mutation.offer) {
checked(population.advance_tick(controlled(population.state())), "feed");
continue;
}
const auto& offer = *life.mutation.offer;
const auto found = std::ranges::find_if(offer.candidates, [](const auto& candidate) {
return candidate.target == biology::Capability::photosynthesis &&
candidate.effect == biology::MutationEffect::acquire;
});
const auto successor = 2'000'000 + tick;
// Decided before the tick: the tick replaces the state the offer lives in.
const bool acquire = found != offer.candidates.end();
const CellularMutationSelection selection{100, life.mutation.event_index,
life.mutation.offer_tick, successor,
acquire ? found->operator_id : std::uint16_t{0}};
checked(population.advance_tick(controlled(population.state()), std::span{&selection, 1}),
"selection tick");
if (acquire)
out.plan = successor;
}
require(
out.plan != 0 && photosynthetic(population.state(), out.plan) &&
!photosynthetic(population.state(), population.state().lives().front().identity.genome),
"founder never planned photosynthesis or expressed it itself");
for (unsigned tick = 0; tick < 40'000 && out.child == 0; ++tick) {
checked(population.advance_tick(controlled(population.state(), true)), "bud");
for (const auto& life : population.state().lives())
if (life.parent_life == 100 && photosynthetic(population.state(), life.identity.genome))
out.child = life.identity.life;
}
require(out.child != 0, "no child expressed the photosynthesis plan");
for (unsigned tick = 0; tick < 400; ++tick) {
const auto before = population.state().captured_light().value;
const auto intents =
checked(wildlife_intents(population.state(), population.state().tick() + 1), "intents");
for (const auto& intent : intents) {
const auto life = std::ranges::find(population.state().lives(), intent.life_id,
[](const auto& l) { return l.identity.life; });
if (photosynthetic(population.state(), life->identity.genome) &&
intent.intent == biology::CellularStepIntent{})
++out.still;
}
checked(population.advance_tick(controlled(population.state())), "lit tick");
const auto gained = population.state().captured_light().value - before;
out.captured += gained;
out.lit_ticks += gained > 0 ? 1 : 0;
closed(population.state());
}
return out;
}
void producers() {
auto trial = grow_producers(cellular_light_trial_irradiance);
require(trial.captured > 0 && trial.lit_ticks > 0, "a photosynthetic child captured no light");
const auto& state = trial.population->state();
const auto bytes = state.snapshot();
require(bytes.size() > 8 && bytes[7] == '9', "lit region not owner format 9");
auto restored =
checked(CellularPopulation::restore(trial.population->snapshot(), state.world(),
state.region(), state.frame(), state.tick()),
"lit restore");
for (unsigned tick = 0; tick < 200; ++tick) {
checked(trial.population->advance_tick(controlled(trial.population->state())), "a");
checked(restored->advance_tick(controlled(restored->state())), "b");
require(trial.population->snapshot() == restored->snapshot(),
"lit region diverged after restore");
}
// A joint owner nests the lit owner: its own infall stays separate from the light.
const auto& lit_state = trial.population->state();
auto joint_config = config({}, 9);
joint_config.geometry.box.width = {lit_state.area().width_um};
joint_config.geometry.box.height = {lit_state.area().height_um};
auto joint = checked(CellularEnvironment::create(std::move(restored), std::move(joint_config)),
"lit joint");
for (unsigned tick = 0; tick < 200; ++tick) {
step(*joint);
audit(*joint);
}
require(joint->state().captured_light().value > 0, "nested owner stopped capturing");
// Without light nothing is captured, and a producer forages as before.
auto dark = grow_producers(0);
require(dark.captured == 0 && dark.population->state().captured_light().value == 0,
"a dark region captured light");
// Same history until the plan, then capture scales exactly with irradiance. (These
// producers always touch food, so they hold still in any light; the light-driven
// holding-still branch is not observable in this rich fixture.)
auto bright = grow_producers(cellular_light_trial_irradiance * 20);
require(bright.child == trial.child && bright.captured == 20 * trial.captured &&
bright.still == trial.still && dark.still == trial.still,
"capture did not scale with irradiance or light changed the controller");
std::cout << "producers: plan " << trial.plan << ", child " << trial.child << ", captured "
<< trial.captured << " pJ over " << trial.lit_ticks << " lit ticks (bright "
<< bright.captured << ", still intents " << bright.still << " vs dark " << dark.still
<< ")\n";
}
// A full habitat refuses new deposits: bubbles are counted skipped, never deferred.
void capacity() {
auto value = environment(founders(),
@@ -517,6 +663,7 @@ int main(int argc, char** argv) {
atomic_and_restore();
raw_and_cohort();
capacity();
producers();
traces();
refusals();
auto value = restart_fixture();
+2 -2
View File
@@ -174,12 +174,12 @@ void rule_versions_and_invalid_growth() {
v3->structure_density == v2->structure_density &&
v3->maintenance_per_kilogram == v2->maintenance_per_kilogram &&
development_rate_microseconds(3) == development_rate_microseconds(2));
for (const auto version : {0U, 5U, 99U}) {
for (const auto version : {0U, 6U, 99U}) {
const auto missing = rules_for(version);
CHECK(!missing && missing.error() == DevelopmentError::unsupported_rules);
}
auto source = draft();
source.development_version = 5;
source.development_version = 6; // Version 5 exists since rules 11 (ADR-0175).
const auto future = build(source);
const auto derived = derive_phenotype(future, {1000, 0}, temperate);
CHECK(!derived && derived.error() == DevelopmentError::unsupported_rules);
+14 -3
View File
@@ -116,7 +116,8 @@ void capability_and_range_rules() {
draft.capabilities.push_back({Capability::thermal_tolerance, 1, 1U << capability_count});
reject(draft, GenomeError::unknown_capability);
// Every boundary of every declared range, from both sides, in each schema's own table.
for (const auto schema : {genome_schema_v1, genome_schema_v2, genome_schema_v3}) {
for (const auto schema :
{genome_schema_v1, genome_schema_v2, genome_schema_v3, genome_schema_v5}) {
const auto& table = *capability_schema_for(schema);
const auto fresh = [&] {
auto d = founder();
@@ -126,6 +127,16 @@ void capability_and_range_rules() {
for (std::size_t index = 0; index < capability_count; ++index) {
const auto kind = static_cast<Capability>(index);
const auto& limits = table[index];
// An empty range (photosynthesis before schema 5) admits no value at all.
if (limits.maximum < limits.minimum) {
CHECK(kind == Capability::photosynthesis && schema != genome_schema_v5);
for (const auto value : {0U, 1U, 100U, 1000U}) {
draft = fresh();
draft.capabilities.push_back({kind, value, 0});
reject(draft, GenomeError::parameter_out_of_range);
}
continue;
}
for (const auto value : {limits.minimum, limits.maximum}) {
draft = fresh();
std::erase_if(draft.capabilities,
@@ -351,10 +362,10 @@ void schema_v2_rates() {
v1.capabilities[4].parameter = 1'000'001;
reject(v1, GenomeError::parameter_out_of_range);
auto unknown = founder();
unknown.schema_version = 5; // Schema 4 exists since rules 7 (ADR-0137).
unknown.schema_version = 6; // Schema 5 exists since rules 11 (ADR-0175).
reject(unknown, GenomeError::unsupported_version);
auto bytes = Genome::create(founder())->encode();
bytes[8] = 5; // Schema field; decode refuses before the checksum matters.
bytes[8] = 6; // Schema field; decode refuses before the checksum matters.
CHECK(!Genome::decode(bytes) &&
Genome::decode(bytes).error() == GenomeError::unsupported_version);
}
+2 -2
View File
@@ -385,12 +385,12 @@ void rules_fail_closed() {
const auto m3 = metabolic_rules_for(3);
CHECK(m2 && m3 && m3->movement_per_kilogram == m2->movement_per_kilogram &&
m3->intake_energy_per_kilogram == m2->intake_energy_per_kilogram);
for (const auto version : {0U, 5U, 99U}) {
for (const auto version : {0U, 6U, 99U}) {
const auto missing = metabolic_rules_for(version);
CHECK(!missing && missing.error() == DevelopmentError::unsupported_rules);
}
auto source = draft();
source.development_version = 5;
source.development_version = 6;
const auto future = build(source);
const auto refused = metabolic_budget(future, {1000, 0}, temperate);
CHECK(!refused && refused.error() == DevelopmentError::unsupported_rules);
+1 -1
View File
@@ -315,7 +315,7 @@ void invalid_contexts_are_refused() {
// An unsupported development version leaves no eligible operator at all,
// because every successor fails to derive a budget.
auto source = draft();
source.development_version = 5;
source.development_version = 6;
const auto future = build(source);
const auto empty = mutation_candidates(future, context(1));
CHECK(!empty && empty.error() == MutationError::no_candidates);
+137
View File
@@ -0,0 +1,137 @@
#include "evo/biology/founder.hpp"
#include "evo/biology/mutation.hpp"
#include "evo/server/lifecycle/cellular_light.hpp"
#include <algorithm>
#include <iostream>
#include <stdexcept>
#include <string>
namespace {
using namespace evo;
using namespace evo::biology;
using namespace evo::server;
void require(bool condition, const char* message) {
if (!condition)
throw std::runtime_error(message);
}
template <typename T, typename E> T checked(std::expected<T, E> value, const char* message) {
if (!value)
throw std::runtime_error(std::string{message} + ": " +
std::to_string(static_cast<unsigned>(value.error())));
return std::move(*value);
}
Genome with(const Genome& base, std::uint32_t efficiency) {
GenomeDraft draft{base.id() + 1, 2, base.development_version(), {base.id()}, {}, {}};
draft.schema_version = base.schema_version();
draft.capabilities.assign(base.capabilities().begin(), base.capabilities().end());
draft.capabilities.push_back({Capability::photosynthesis, efficiency, 0});
return checked(Genome::create(draft), "photosynthetic genome");
}
constexpr EnvironmentState reference{293'150, {}};
// Founder 4 is founder 3 with a schema that can express photosynthesis; it starts without.
void founders() {
const auto three = checked(primitive_founder(5, 5, primitive_founder_v3), "founder 3");
const auto four = checked(primitive_founder(5, 5, primitive_founder_v4), "founder 4");
require(!four.carries(Capability::photosynthesis) && four.schema_version() == 5 &&
four.development_version() == 5,
"founder 4 carries photosynthesis or wrong versions");
const auto a = checked(metabolic_budget(three, {1000, 0}, reference), "founder 3 budget");
const auto b = checked(metabolic_budget(four, {1000, 0}, reference), "founder 4 budget");
require(a.phenotype == b.phenotype && a.body == b.body && a.maintenance == b.maintenance &&
a.reproduction_investment == b.reproduction_investment,
"founder 4 differs from founder 3 without photosynthesis");
require(checked(primitive_endowment(primitive_founder_v4), "endowment 4") ==
checked(primitive_endowment(primitive_founder_v3), "endowment 3"),
"founder 4 endowment changed");
// Earlier schemas cannot carry it at any efficiency.
GenomeDraft old{9, 9, 4, {}, {}, genome_schema_v4};
old.capabilities.assign(three.capabilities().begin(), three.capabilities().end());
old.capabilities.push_back({Capability::photosynthesis, 100, 0});
require(Genome::create(old).error() == GenomeError::parameter_out_of_range,
"schema 4 accepted photosynthesis");
}
// Organelles: p/4 of structure as membrane mass (upkept twice), speed down by p/2.
void costs() {
const auto four = checked(primitive_founder(5, 5, primitive_founder_v4), "founder 4");
const auto base = checked(metabolic_budget(four, {1000, 0}, reference), "base budget");
for (const auto efficiency : {100U, 500U, 1000U}) {
const auto producer = with(four, efficiency);
const auto budget = checked(metabolic_budget(producer, {1000, 0}, reference), "producer");
const auto organelles = base.phenotype.structural_mass / 4 * efficiency / 1000;
require(budget.phenotype.membrane_mass == base.phenotype.membrane_mass + organelles &&
budget.phenotype.structural_mass == base.phenotype.structural_mass &&
budget.phenotype.extent == base.phenotype.extent &&
budget.maintenance.value > base.maintenance.value &&
budget.phenotype.movement_speed ==
base.phenotype.movement_speed * (1000 - efficiency / 2) / 1000,
"photosynthesis costs differ from the declared trial rule");
const CellularLight light{cellular_light_trial_irradiance, 1, {1ULL << 50}};
const auto captured_still =
cellular_light_capture(producer, budget.phenotype.extent, light, false);
const auto captured_moving =
cellular_light_capture(producer, budget.phenotype.extent, light, true);
require(captured_still.has_value() && captured_moving.has_value(), "capture overflowed");
const auto still = captured_still.value_or(0), moving = captured_moving.value_or(0);
const auto section = std::uint64_t{budget.phenotype.extent} * budget.phenotype.extent;
require(still == section * cellular_light_trial_irradiance * efficiency / 1000 &&
moving == still / 4,
"light capture differs from cross-section x irradiance x efficiency");
std::cout << "photosynthesis " << efficiency << ": maintenance "
<< budget.maintenance.value / 20 << " pJ/tick (founder "
<< base.maintenance.value / 20 << "), capture still " << still << ", moving "
<< moving << ", speed " << budget.phenotype.movement_speed << " um/s\n";
}
require(cellular_light_capture(four, 20, {cellular_light_trial_irradiance, 1, {1}}, false) == 0,
"a genome without the capability captured light");
}
// Schema 5 offers photosynthesis through the appended operators; earlier schemas never do.
void offers() {
const auto catalog = mutation_catalog();
require(catalog.size() == 32, "catalog size");
for (std::uint16_t id = 29; id <= 32; ++id)
require(catalog[id - 1].target == Capability::photosynthesis,
"photosynthesis operators are not IDs 29-32");
const auto four = checked(primitive_founder(5, 5, primitive_founder_v4), "founder 4");
const auto three = checked(primitive_founder(5, 5, primitive_founder_v3), "founder 3");
bool offered = false;
for (std::uint64_t event = 1; event <= 400 && !offered; ++event) {
const MutationContext context{0x5EED, 1, event, 8, {1000, 0}, reference};
const auto old = checked(mutation_candidates(three, context), "schema 4 offer");
require(std::ranges::none_of(
old.candidates,
[](const auto& c) { return c.target == Capability::photosynthesis; }),
"schema 4 lineage offered photosynthesis");
const auto offer = checked(mutation_candidates(four, context), "schema 5 offer");
const auto found = std::ranges::find_if(offer.candidates, [](const auto& c) {
return c.target == Capability::photosynthesis && c.effect == MutationEffect::acquire;
});
if (found == offer.candidates.end())
continue;
offered = true;
require(found->parameter == photosynthesis_acquired_start,
"acquired photosynthesis does not start at the trial value");
const auto successor =
checked(select_mutation(four, context, offer, found->operator_id, 77, 77), "select");
require(*successor.parameter(Capability::photosynthesis) == photosynthesis_acquired_start,
"selected successor lost photosynthesis");
}
require(offered, "schema 5 never offered photosynthesis");
}
} // namespace
int main() {
try {
founders();
costs();
offers();
std::cout << "photosynthesis contracts passed\n";
return 0;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}
}