Drive the energy cycle with measured ecosystem episodes

Close M2 with a deterministic driver that owns one community and one
seeded stream, holds no inventory and emits only the terminal, feeding
and reproduction intents the earlier increments already accept. No
simulation contract changed to make the cycle run unattended.

Two limits shaped the design. Identities are never reused and slots are
capped, so one community supports a bounded number of consumer lifetimes
and is not a population. No maintenance cost exists, so a consumer that
is never eaten never dies, and inventing a starvation rule here would
settle a question ENERGY_MODEL leaves open inside a measurement harness.
The harness therefore measures many bounded episodes, taking mortality
from predation and from a configured lifespan delivered as an ADR-0027
host terminal fact. Lifespan bounds an episode; it is not senescence.

The driver must never submit a batch the community rejects, so it probes
a discarded copy each tick to size bites against the state feeding will
actually see, claims each target for one feeder, and excludes a bitten
consumer from acting. A rejected tick is reported as a driver defect
rather than absorbed as ecology.

Across 64 seeds the suite steps tick by tick, requiring exact conserved
totals at every tick and an identical report on replay, and covers both
mortality paths, slot exhaustion, tick-limit termination, lineage
attribution, episode independence and configuration rejection.
evo-sim --ecosystem reports seeded ensembles: 3,000 episodes advanced
1,093,007 ticks, all reaching extinction, with deaths reconciling exactly
against founders plus births. That demonstrates arithmetic and turnover,
not ecological plausibility; strategy constants are chosen inputs.

Native Release, clang-tidy analysis, ASan and format checks pass 11/11;
golden d61ad5bf0c928c76 is unchanged. Linux CI has not run yet.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-09-22 17:38:34 -04:00
co-authored by Claude Opus 5
parent 67637ac14c
commit eaf58f2b85
13 changed files with 1009 additions and 11 deletions
+114 -2
View File
@@ -1,3 +1,4 @@
#include "evo/processes/ecosystem.hpp"
#include "evo/simulation/world.hpp"
#include "evo/telemetry/sinks.hpp"
@@ -10,6 +11,8 @@
#include <iostream>
#include <limits>
#include <optional>
#include <sstream>
#include <string>
#include <string_view>
#include <system_error>
#include <utility>
@@ -19,8 +22,11 @@ struct Options {
std::uint64_t seed{123456};
std::uint64_t ticks{1000};
std::uint64_t entities{8};
std::uint64_t episodes{1};
std::uint64_t tick_limit{2000};
std::filesystem::path load, save, output, metrics;
bool verify_replay{};
bool ecosystem{};
bool help{};
};
@@ -78,6 +84,7 @@ std::expected<void, evo::Error> validate_paths(const Options& options) {
std::expected<Options, evo::Error> parse(int argc, char** argv) {
Options options;
bool initial_state_option = false;
bool entities_option = false;
for (int index = 1; index < argc; ++index) {
const std::string_view key{argv[index]};
if (key == "--help") {
@@ -88,11 +95,16 @@ std::expected<Options, evo::Error> parse(int argc, char** argv) {
options.verify_replay = true;
continue;
}
if (key == "--ecosystem") {
options.ecosystem = true;
continue;
}
if (++index >= argc) {
return std::unexpected{evo::Error::invalid_arguments};
}
const std::string_view value{argv[index]};
if (key == "--seed" || key == "--ticks" || key == "--entities") {
if (key == "--seed" || key == "--ticks" || key == "--entities" || key == "--episodes" ||
key == "--tick-limit") {
const auto parsed = number(value);
if (!parsed) {
return std::unexpected{parsed.error()};
@@ -103,6 +115,11 @@ std::expected<Options, evo::Error> parse(int argc, char** argv) {
} else if (key == "--entities") {
options.entities = *parsed;
initial_state_option = true;
entities_option = true;
} else if (key == "--episodes") {
options.episodes = *parsed;
} else if (key == "--tick-limit") {
options.tick_limit = *parsed;
} else {
options.ticks = *parsed;
}
@@ -121,6 +138,12 @@ std::expected<Options, evo::Error> parse(int argc, char** argv) {
if (options.entities > evo::max_entities || (!options.load.empty() && initial_state_option)) {
return std::unexpected{evo::Error::invalid_arguments};
}
// Episode measurement shares no state, snapshot or particle metric with the proof run.
if (options.ecosystem && (entities_option || options.verify_replay || !options.load.empty() ||
!options.save.empty() || !options.metrics.empty() ||
options.episodes == 0 || options.tick_limit == 0)) {
return std::unexpected{evo::Error::invalid_arguments};
}
if (const auto valid = validate_paths(options); !valid) {
return std::unexpected{valid.error()};
}
@@ -167,7 +190,92 @@ std::expected<void, evo::Error> advance(evo::SimulationWorld& world, std::uint64
return {};
}
std::expected<void, evo::Error> write_report(const Options& options, const std::string& text) {
std::ofstream output_file;
if (!options.output.empty()) {
if (const auto valid = validate_paths(options); !valid) {
return valid;
}
output_file.open(options.output);
if (!output_file) {
return std::unexpected{evo::Error::io_failure};
}
}
auto& output = options.output.empty() ? std::cout : output_file;
output << text;
output.flush();
if (!options.output.empty()) {
output_file.close();
}
if (!output) {
return std::unexpected{evo::Error::io_failure};
}
return {};
}
// CLI-local fixture constants. They are measurement inputs, not simulation rules,
// and are deliberately not exported by the library. See ADR-0029.
evo::accounting::Inventory bundle(std::uint64_t value) {
using namespace evo::accounting;
return {{value}, {Femtograms{value}, Femtograms{value}, Femtograms{value}, Femtograms{value}}};
}
evo::processes::StrategyConfig grazer() {
return {bundle(20), bundle(3), 900, bundle(30), bundle(20), 50};
}
evo::processes::StrategyConfig forager() {
return {bundle(20), bundle(2), 1000, bundle(24), bundle(16), 70};
}
std::expected<void, evo::Error> run_ecosystem(const Options& options) {
using namespace evo::processes;
const ProducerConfig plants{{{10}, 1}, {2, 1}, 1000, {1}, bundle(1).matter};
const EcosystemConfig config{plants, {100'000}, bundle(200).matter, {bundle(4)},
800, 14, options.tick_limit};
const std::array founders{grazer(), forager()};
std::uint64_t ticks{}, births{}, predation{}, lifespan{}, extinctions{}, survivors{};
std::uint64_t shortest = std::numeric_limits<std::uint64_t>::max(), longest{}, fingerprint{};
for (std::uint64_t index = 0; index < options.episodes; ++index) {
if (options.seed > std::numeric_limits<std::uint64_t>::max() - index) {
return std::unexpected{evo::Error::invalid_arguments};
}
auto episode = EcosystemEpisode::create(config, founders, options.seed + index);
if (!episode) {
return std::unexpected{evo::Error::invalid_arguments};
}
const auto summary = episode->run();
if (!summary) {
return std::unexpected{evo::Error::replay_mismatch};
}
if (summary->ticks > std::numeric_limits<std::uint64_t>::max() - ticks) {
return std::unexpected{evo::Error::tick_exhausted};
}
ticks += summary->ticks;
births += summary->births;
predation += summary->predation_deaths;
lifespan += summary->lifespan_deaths;
extinctions += summary->extinction_tick != 0 ? 1 : 0;
survivors += summary->surviving_lineages;
shortest = std::min(shortest, summary->ticks);
longest = std::max(longest, summary->ticks);
fingerprint ^= summary->fingerprint + 0x9e3779b97f4a7c15ULL + (fingerprint << 6U) +
(fingerprint >> 2U);
}
std::ostringstream report;
report << "{\"rules_version\":" << evo::rules_version
<< ",\"mode\":\"ecosystem\",\"seed\":" << options.seed
<< ",\"episodes\":" << options.episodes << ",\"tick_limit\":" << options.tick_limit
<< ",\"ticks\":" << ticks << ",\"births\":" << births
<< ",\"predation_deaths\":" << predation << ",\"lifespan_deaths\":" << lifespan
<< ",\"extinctions\":" << extinctions << ",\"surviving_lineages\":" << survivors
<< ",\"shortest\":" << shortest << ",\"longest\":" << longest << ",\"fingerprint\":\""
<< std::hex << std::setfill('0') << std::setw(16) << fingerprint << std::dec << "\"}\n";
return write_report(options, report.str());
}
std::expected<void, evo::Error> run(const Options& options) {
if (options.ecosystem) {
return run_ecosystem(options);
}
auto loaded = initial_world(options);
if (!loaded) {
return std::unexpected{loaded.error()};
@@ -257,7 +365,11 @@ int main(int argc, char** argv) try {
std::cout
<< "evo-sim [--seed N --entities N | --load FILE] --ticks N\n"
" [--verify-replay] [--save FILE] [--output JSON] [--metrics FILE]\n"
"Synthetic proof. When loading, ticks are additional.\n";
"evo-sim --ecosystem [--seed N] [--episodes N] [--tick-limit N]\n"
" [--output JSON]\n"
"Synthetic proof. When loading, ticks are additional.\n"
"Ecosystem mode measures seeded bounded episodes; it shares no state,\n"
"snapshot or particle metric with the proof run.\n";
return 0;
}
const auto result = run(*options);