Files
aevolution/tests/environment_publication_tests.cpp
dingoandClaude Opus 5.5 b73ccbae9a
Foundation / checks (ecs) (push) Successful in 2m19s
Foundation / checks (deploy) (push) Successful in 4m47s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 16m48s
Foundation / checks (network) (push) Canceled after 16m46s
Foundation / checks (postgres) (push) Canceled after 16m47s
Foundation / checks (client) (push) Canceled after 16m53s
Foundation / checks (tsan) (push) Canceled after 14m33s
feat(ecology): make photosynthesis regions playable on DEV
ADR-0176 and schema 57 admit raw cellular bundle 13 (rules 11 on owner
format 9) through parser-local normalization to bundle 12, and a fenced
no-tick 12 -> 13 upgrade that declares the region's light; recovery reads
the light from the target and re-derives the whole target from the source.
The resumable cellular-v8 opening provisions a cellular-v7 genesis, one
empty tick and the durable upgrades to bundle 13.

Session v12 adds the owner's captured light and the producers it can see
to its perception, and lets offers name photosynthesis; older sessions keep
their layouts and never receive photosynthesis candidates. The client
draws chloroplasts by efficiency, shows sunlight in the HUD and offers
photosynthesis cards. DEV now provisions and serves region 9 with the
trial light; region 8 stays stored but unserved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 23:04:34 -04:00

501 lines
26 KiB
C++

#include "evo/persistence/account_control_store.hpp"
#include "evo/persistence/environment_checkpoint_store.hpp"
#include "evo/persistence/population_interval_store.hpp"
#include "evo/persistence/sha256.hpp"
#include "fixtures/cellular_ecology.hpp"
#include <cstdlib>
#include <type_traits>
void run_environment_publication_tests();
namespace {
using namespace evo;
using namespace evo::server;
using namespace evo::persistence;
using namespace evo::fixture;
using namespace std::chrono_literals;
template <typename T, typename E> T take(std::expected<T, E> value, const char* where) {
if (!value) {
std::string message{where};
if constexpr (requires { value.error().cause.sqlstate; })
message += " SQLSTATE=" + std::string(value.error().cause.sqlstate.data(), 5);
if constexpr (requires { value.error().store.cause.sqlstate; })
message += " SQLSTATE=" + std::string(value.error().store.cause.sqlstate.data(), 5);
throw std::runtime_error(message);
}
if constexpr (!std::is_void_v<T>)
return std::move(*value);
}
std::string variable(const char* name) {
const auto* value = std::getenv(name);
require(value != nullptr, "missing private test environment");
return value;
}
std::unique_ptr<Postgres> connection(bool owner = false) {
return take(Postgres::connect({variable("PGHOST"),
{},
variable("PGPORT"),
variable("PGDATABASE"),
variable(owner ? "PGUSER" : "EVO_PG_RUNTIME_USER"),
{},
{}},
5s),
"environment connection");
}
// Mixed-byte identities, distinct from every other suite's commit namespace.
Uuid identity(unsigned number) {
Uuid id{};
id[0] = 0xE6;
id[1] = 0x56;
id[7] = 0x3C;
id[14] = static_cast<std::uint8_t>(number >> 8U);
id[15] = static_cast<std::uint8_t>(number);
return id;
}
std::string hexadecimal(std::span<const std::uint8_t> bytes) {
std::string out = "\\x";
constexpr char digits[] = "0123456789abcdef";
for (const auto b : bytes) {
out.push_back(digits[b >> 4U]);
out.push_back(digits[b & 15U]);
}
return out;
}
struct Fixture {
RegionKey key{30411, 1};
std::unique_ptr<Postgres> owner = connection(true), db = connection();
std::unique_ptr<FilesystemObjectStore> objects;
LeaseGrant lease;
Sha256Digest build{};
CommittedCheckpoint head;
std::unique_ptr<CellularLives> raw;
std::unique_ptr<CellularEnvironment> joint;
unsigned next_id = 1;
Uuid id() {
return identity(next_id++);
}
CellularBatchHeader header(std::uint64_t count) const {
return {key.world,
key.region,
1,
head.head.commit,
head.head.digest,
head.head.end_tick,
head.head.end_tick + count};
}
[[nodiscard]] std::array<Sha256Digest, 1> builds() const {
return {build};
}
};
// A real funded raw region with player-derived wildlife and fully suspended players.
// `nursery` founders are funded; four are born here, so any extra stay funded for later.
void funded_source(Fixture& f, std::uint32_t nursery = 4) {
f.build.fill(0xE6);
const std::array<std::string, 1> build{hexadecimal(f.build)};
take(f.owner->query("INSERT INTO evolution.retained_builds(build_identity) VALUES($1::bytea) "
"ON CONFLICT DO NOTHING",
build, 5s),
"retain test build");
const std::array<std::string, 1> world{std::to_string(f.key.world)};
take(f.owner->query("INSERT INTO evolution.life_allocators VALUES($1::evolution.u64,100)",
world, 5s),
"test allocator");
f.objects =
take(FilesystemObjectStore::open(variable("PGHOST"), f.key, max_cellular_environment_bytes),
"environment object namespace");
f.raw = opening(f.key.world - 200, true, nursery, false, f.key.region);
f.head =
take(provision_cellular_region(*f.owner, *f.objects, f.key, 1, *f.raw, f.id(), f.build, 5s),
"empty cellular genesis");
f.lease =
take(acquire_region_lease(*f.db, f.key, f.build, 60000ms, 5s), "environment test lease");
CellularBatch birth{f.header(1), {{}}};
for (std::uint8_t index = 1; index <= 4; ++index) {
Uuid account{};
account[0] = 0xE6;
account.back() = index;
const auto control =
take(claim_account_control(*f.db, f.key, f.lease, account, 1, index, 60000ms, 5s),
"founder account control");
const auto reserved = take(
reserve_primitive_life(*f.db, f.key, f.lease, account, control.generation, 1, 1, 5s),
"funded founder reservation");
birth.ticks.front().births.push_back({account, 1, control.generation, reserved.identity});
}
f.raw = take(replay_cellular_batch(*f.raw, birth), "funded founder replay");
f.head = take(publish_cellular_batch(*f.db, *f.objects, f.lease, f.head, birth, *f.raw, f.id(),
f.build, 5s),
"funded founder publication")
.committed;
while (f.raw->next_bud_sequence() == 1 && f.raw->tick() < 20000) {
CellularBatch batch{f.header(1), {}};
for (unsigned tick = 0; tick < 64; ++tick) {
auto intents =
take(wildlife_intents(*f.raw, f.raw->tick() + 1), "funded source wildlife");
for (const auto& life : f.raw->lives()) {
if (life.account == LifeAccount{})
continue;
CellularLifeIntent intent{life.identity.life, {}};
intent.intent.bud = (f.raw->tick() + life.identity.life) % 32 == 0;
intents.push_back(intent);
}
std::ranges::sort(intents, {}, &CellularLifeIntent::life_id);
take(f.raw->advance_tick(intents), "funded source biology");
batch.ticks.push_back({intents, {}, {}});
if (f.raw->next_bud_sequence() > 1)
break;
}
batch.header.end_tick = f.raw->tick();
f.head = take(publish_cellular_batch(*f.db, *f.objects, f.lease, f.head, batch, *f.raw,
f.id(), f.build, 5s),
"funded source batch")
.committed;
take(renew_region_lease(*f.db, f.key, f.lease, 60000ms, 5s), "source lease renewal");
}
require(std::ranges::any_of(f.raw->lives(),
[](const auto& life) { return life.account == LifeAccount{}; }),
"durable fixture has no funded player-derived wildlife");
const auto suspended_at = f.raw->tick() + cellular_linger_ticks + 1;
while (f.raw->tick() < suspended_at) {
auto h = f.header(std::min(std::uint64_t{64}, suspended_at - f.raw->tick()));
auto prefix = take(unattended_population_inputs(*f.raw, h), "raw linger inputs");
f.raw = take(replay_cellular_batch(*f.raw, prefix), "raw linger biology");
f.head = take(publish_cellular_batch(*f.db, *f.objects, f.lease, f.head, prefix, *f.raw,
f.id(), f.build, 5s),
"raw linger publication")
.committed;
std::vector<std::uint64_t> endings;
for (const auto& corpse : f.raw->corpses())
if (corpse.parent_life == 0)
endings.push_back(corpse.life_id);
if (!endings.empty())
take(end_cellular_lives(*f.db, f.key, f.lease, f.head.head.commit, endings, 5s),
"raw linger endings");
}
require(population_interval_safe(*f.raw), "joint source retained a vulnerable player");
}
CellularEnvironmentConfig declared(const CellularLives& state) {
CellularEnvironmentConfig config;
config.geometry.voxel = {4, 0, -1};
config.geometry.box = {{0}, {0}, {0}, {state.area().width_um}, {state.area().height_um}, {12}};
config.water = {{9'000'000}, {40'000}, {6'000}};
CellularInfallSource source;
source.id = 3;
source.width_um = static_cast<std::uint32_t>(state.area().width_um);
source.height_um = static_cast<std::uint32_t>(state.area().height_um);
source.start_tick = state.tick() + 1;
source.end_tick = ~std::uint64_t{0};
source.mean_interval_ticks = 9;
source.per_bubble.energy.value = 300'000;
source.per_bubble.matter[0].value = 30'000;
source.entitlement.energy.value = 1ULL << 40;
source.entitlement.matter[0].value = 1ULL << 40;
config.sources.push_back(source);
config.radiation_cap = {17};
return config;
}
void recover(Fixture& f) {
auto db = connection();
const auto builds = f.builds();
auto recovered =
take(recover_environment_checkpoint(*db, *f.objects, f.key, f.lease, builds, 5s),
"joint durable recovery");
f.head = std::move(recovered.committed);
f.joint = std::move(recovered.state);
}
struct Flows {
std::uint64_t energy{}, carbon{}, radiated{};
};
Flows recorded(Fixture& f, const Uuid& commit) {
const std::array<std::string, 1> id{[&] {
std::string out;
constexpr char digits[] = "0123456789abcdef";
for (std::size_t i = 0; i < commit.size(); ++i) {
if (i == 4 || i == 6 || i == 8 || i == 10)
out.push_back('-');
out.push_back(digits[commit[i] >> 4U]);
out.push_back(digits[commit[i] & 15U]);
}
return out;
}()};
const auto rows = take(f.owner->query("SELECT imported_energy::text,imported_carbon::text,"
"radiated_heat::text FROM evolution.environment_"
"publications WHERE commit_id=$1::uuid",
id, 5s),
"recorded environment flows");
require(rows.size() == 1 && rows[0].size() == 3, "missing environment flow evidence");
return {std::stoull(rows[0][0].value_or("x")), std::stoull(rows[0][1].value_or("x")),
std::stoull(rows[0][2].value_or("x"))};
}
Flows sum(const CellularEnvironmentBatch& batch) {
Flows total;
for (const auto& flow : batch.flows) {
total.energy += flow.imported.energy.value;
total.carbon += flow.imported.matter[0].value;
total.radiated += flow.exported.value;
}
return total;
}
bool rejected(const auto& result, std::string_view sqlstate) {
if (result)
return false;
const auto& error = result.error();
const std::string_view actual(error.store.cause.sqlstate.data(), 5);
if (error.kind != PublicationError::rejected || actual != sqlstate)
std::fprintf(stderr, "refusal kind %u, store %u, SQLSTATE %.5s\n",
static_cast<unsigned>(error.kind), static_cast<unsigned>(error.store.kind),
error.store.cause.sqlstate.data());
return error.kind == PublicationError::rejected && actual == sqlstate;
}
// The joint transition, owned intervals, conversions, retries and refusals.
void joint_profile(Fixture& f) {
f.head = take(
migrate_cellular_boundary_profile(*f.db, *f.objects, f.lease, f.head, f.id(), f.build, 5s),
"durable boundary upgrade");
f.head = take(migrate_cellular_maturation_profile(*f.db, *f.objects, f.lease, f.head, f.id(),
f.build, 5s),
"durable maturation upgrade");
require(!migrate_population_to_environment(*f.db, *f.objects, f.lease, f.head, f.id(), f.build,
declared(*f.raw), 5s),
"raw profile skipped the population profile");
f.head = take(migrate_cellular_to_population(*f.db, *f.objects, f.lease, f.head, f.id(),
f.build, {false, 900, 1'600}, 5s),
"population entry");
const auto builds = f.builds();
auto population =
take(recover_population_checkpoint(*f.db, *f.objects, f.key, f.lease, builds, 5s),
"population recovery");
const auto nested = population.state->snapshot();
const auto transition_id = f.id();
const auto entered =
take(migrate_population_to_environment(*f.db, *f.objects, f.lease, f.head, transition_id,
f.build, declared(population.state->state()), 5s),
"joint profile transition");
const PopulationPreparedCommit exact{f.key, transition_id, entered.manifest, {}};
const auto retried =
take(retry_environment_prepared(*f.db, exact, 5s), "exact transition retry");
require(retried.head == entered.head && retried.manifest == entered.manifest,
"transition retry changed its receipt");
auto reused = decode_manifest(entered.manifest).value();
reused.payload_sha256.fill(0x44);
const PopulationPreparedCommit conflicting{
f.key, transition_id, encode_manifest(reused).value(), {}};
require(rejected(retry_environment_prepared(*f.db, conflicting, 5s), "EVL07"),
"transition identity reused with other bytes");
require(take(resolve_environment_prepared(*f.db, exact, 5s), "resolve transition").status ==
Resolution::committed,
"committed transition unresolved");
recover(f);
require(f.joint->population().snapshot() == nested && f.joint->imported() == Inventory{} &&
f.joint->tick() == population.state->state().tick(),
"joint transition changed nested biology or crossed the boundary");
// An unattended interval needs its owned admission first.
auto interval = take(prepare_environment_interval(*f.joint, f.header(64)), "joint interval");
require(rejected(publish_environment_batch(*f.db, *f.objects, f.lease, f.head, interval.batch,
*interval.state, f.id(), f.build, 5s),
"EVL18"),
"interval published without an owned admission");
take(admit_population_interval(*f.db, f.key, f.lease, f.head.head, f.head.head.end_tick + 64,
5s),
"joint interval admission");
const auto interval_id = f.id();
const PopulationPreparedCommit pending{
f.key, interval_id, {}, take(encode_environment_batch(interval.batch), "encode interval")};
f.head = take(publish_environment_batch(*f.db, *f.objects, f.lease, f.head, interval.batch,
*interval.state, interval_id, f.build, 5s),
"joint interval publication");
const auto flows = recorded(f, interval_id), expected = sum(interval.batch);
require(flows.energy == expected.energy && flows.carbon == expected.carbon &&
flows.radiated == expected.radiated && expected.energy > 0 && expected.radiated > 0,
"SQL flow evidence disagrees with the joint trace");
const PopulationPreparedCommit published{f.key, interval_id, f.head.manifest, pending.trace};
require(take(retry_environment_prepared(*f.db, published, 5s), "exact interval retry").head ==
f.head.head,
"interval retry changed its receipt");
recover(f);
require(f.joint->snapshot() == interval.state->snapshot(), "recovered interval diverged");
// Bypass publishers cannot commit or reinterpret joint evidence.
require(!publish_population_checkpoint(*f.db, f.head.manifest, pending.trace, 5s),
"population publisher accepted joint evidence");
// Whole-group conversion, then a cohort interval, each recovered exactly.
std::uint64_t genome = 0;
for (const auto& life : f.joint->state().lives())
if (life.account == LifeAccount{})
genome = life.identity.genome;
require(genome != 0, "no durable wildlife to convert");
auto conversion = take(prepare_environment_conversion(*f.joint, f.header(0), {genome, true}),
"joint conversion");
take(
admit_population_interval(*f.db, f.key, f.lease, f.head.head, f.head.head.end_tick + 8, 5s),
"blocking admission");
require(rejected(publish_environment_batch(*f.db, *f.objects, f.lease, f.head, conversion.batch,
*conversion.state, f.id(), f.build, 5s),
"EVL18"),
"conversion overtook a pending interval");
auto short_interval =
take(prepare_environment_interval(*f.joint, f.header(8)), "short interval");
f.head =
take(publish_environment_batch(*f.db, *f.objects, f.lease, f.head, short_interval.batch,
*short_interval.state, f.id(), f.build, 5s),
"settle pending interval");
recover(f);
conversion = take(prepare_environment_conversion(*f.joint, f.header(0), {genome, true}),
"joint conversion at the settled head");
f.head = take(publish_environment_batch(*f.db, *f.objects, f.lease, f.head, conversion.batch,
*conversion.state, f.id(), f.build, 5s),
"joint conversion publication");
recover(f);
require(!f.joint->population().members().empty() &&
f.joint->snapshot() == conversion.state->snapshot(),
"recovered conversion diverged");
take(admit_population_interval(*f.db, f.key, f.lease, f.head.head, f.head.head.end_tick + 64,
5s),
"cohort interval admission");
auto cohort = take(prepare_environment_interval(*f.joint, f.header(64)), "cohort interval");
f.head = take(publish_environment_batch(*f.db, *f.objects, f.lease, f.head, cohort.batch,
*cohort.state, f.id(), f.build, 5s),
"cohort interval publication");
recover(f);
require(f.joint->snapshot() == cohort.state->snapshot(), "recovered cohort interval diverged");
std::printf("joint profile: head %llu at tick %llu, imported %llu pJ, radiated %llu pJ\n",
static_cast<unsigned long long>(f.head.head.sequence),
static_cast<unsigned long long>(f.head.head.end_tick),
static_cast<unsigned long long>(f.joint->imported().energy.value),
static_cast<unsigned long long>(f.joint->exported().value));
}
// Structurally valid but forged flows commit through the low-level SQL publisher, which
// does not replay biology; recovery replays the trace and refuses the head.
void forged_evidence(Fixture& f) {
take(
admit_population_interval(*f.db, f.key, f.lease, f.head.head, f.head.head.end_tick + 4, 5s),
"forged interval admission");
auto interval = take(prepare_environment_interval(*f.joint, f.header(4)), "forged interval");
interval.batch.flows[0].exported.value += 1;
const auto trace = take(encode_environment_batch(interval.batch), "encode forged trace");
const auto payload = interval.state->snapshot();
const auto previous = decode_manifest(f.head.manifest).value();
const auto previous_digest = sha256(f.head.manifest).value();
const auto manifest =
take(environment_successor_manifest(previous, previous_digest, f.id(), f.lease,
f.head.head.end_tick + 4, f.build, payload.size(),
sha256(payload).value(),
{trace.size(), sha256(trace).value(), 0, {}}),
"forged manifest");
take(f.objects->put(payload), "forged payload object");
take(f.objects->put(trace), "forged trace object");
const auto bytes = take(encode_manifest(manifest), "encode forged manifest");
take(publish_environment_checkpoint(*f.db, bytes, trace, 5s), "structural forged publication");
auto db = connection();
const auto builds = f.builds();
const auto refused =
recover_environment_checkpoint(*db, *f.objects, f.key, f.lease, builds, 5s);
require(!refused && refused.error().kind == RecoveryError::tail,
"recovery accepted forged environmental flows");
}
// ADR-0176: durable bundle 9 -> 10 -> 11 -> 12 -> 13 on raw profile 4, declaring light;
// exact retry, reused-identity conflict, continued publication and recovery.
void photosynthesis_bundle(Fixture& f) {
f.head = take(
migrate_cellular_boundary_profile(*f.db, *f.objects, f.lease, f.head, f.id(), f.build, 5s),
"durable boundary upgrade");
f.head = take(migrate_cellular_maturation_profile(*f.db, *f.objects, f.lease, f.head, f.id(),
f.build, 5s),
"durable maturation upgrade");
f.head = take(migrate_cellular_inheritance_profile(*f.db, *f.objects, f.lease, f.head, f.id(),
f.build, 5s),
"durable inheritance upgrade");
const auto source = f.head;
const CellularLight light{cellular_light_trial_irradiance, 1, {1ULL << 50}};
const auto upgrade_id = f.id();
f.head = take(migrate_cellular_photosynthesis_profile(*f.db, *f.objects, f.lease, source,
upgrade_id, f.build, light, 5s),
"durable photosynthesis upgrade");
const auto upgraded = decode_manifest(f.head.manifest).value();
require(upgraded.cellular_versions == cellular_v13_versions &&
upgraded.profile == CheckpointProfile::cellular_lives_v1 &&
upgraded.begin_tick == upgraded.end_tick,
"bundle 13 upgrade changed profile or tick");
const auto retried =
take(retry_cellular_profile_migration(*f.db, {f.key, upgrade_id, f.head.manifest}, 5s),
"exact photosynthesis upgrade retry");
require(retried.head == f.head.head, "photosynthesis upgrade retry changed its receipt");
const auto conflicting = migrate_cellular_photosynthesis_profile(
*f.db, *f.objects, f.lease, source, upgrade_id, f.build,
{light.numerator * 2, 1, {1ULL << 50}}, 5s);
require(!conflicting && conflicting.error().kind == PublicationError::rejected,
"photosynthesis upgrade identity reused with other light");
const auto builds = f.builds();
auto recovered =
take(recover_cellular_checkpoint(*f.db, *f.objects, f.key, f.lease, builds, 5s),
"bundle 13 recovery");
require(recovered.state->rules_version() == cellular_lives_photosynthesis_rules &&
recovered.state->format_version() == cellular_lives_light_format &&
recovered.state->light() == light,
"recovered region lost its rules or light");
// An unattended raw interval continues on bundle 13 and recovers exactly.
auto inputs = take(unattended_population_inputs(*recovered.state, f.header(64)),
"bundle 13 interval inputs");
auto next = take(replay_cellular_batch(*recovered.state, inputs), "bundle 13 interval");
f.head = take(publish_cellular_batch(*f.db, *f.objects, f.lease, f.head, inputs, *next, f.id(),
f.build, 5s),
"bundle 13 interval publication")
.committed;
auto again = take(recover_cellular_checkpoint(*f.db, *f.objects, f.key, f.lease, builds, 5s),
"bundle 13 interval recovery");
require(again.state->snapshot() == next->snapshot(), "bundle 13 interval recovery diverged");
// A funded birth on bundle 13 is founder 4 (genome schema 5), through the same journals.
Uuid account{};
account[0] = 0xE6;
account.back() = 9;
const auto control =
take(claim_account_control(*f.db, f.key, f.lease, account, 1, 9, 60000ms, 5s),
"bundle 13 account control");
const auto reserved =
take(reserve_primitive_life(*f.db, f.key, f.lease, account, control.generation, 1, 1, 5s),
"bundle 13 funded reservation");
auto birth = take(unattended_population_inputs(*again.state, f.header(1)), "birth tick");
birth.ticks.front().births.push_back({account, 1, control.generation, reserved.identity});
auto born = take(replay_cellular_batch(*again.state, birth), "bundle 13 birth");
f.head = take(publish_cellular_batch(*f.db, *f.objects, f.lease, f.head, birth, *born, f.id(),
f.build, 5s),
"bundle 13 birth publication")
.committed;
auto with_birth =
take(recover_cellular_checkpoint(*f.db, *f.objects, f.key, f.lease, builds, 5s),
"bundle 13 birth recovery");
const auto life = std::ranges::find(with_birth.state->lives(), reserved.identity,
[](const auto& l) { return l.identity.life; });
require(life != with_birth.state->lives().end(), "bundle 13 birth missing after recovery");
const auto genome = with_birth.state->archive().find(life->identity.genome);
require(genome && genome->schema_version() == biology::genome_schema_v5 &&
with_birth.state->snapshot() == born->snapshot(),
"bundle 13 birth is not founder 4 or recovery diverged");
std::printf("photosynthesis bundle: head %llu at tick %llu\n",
static_cast<unsigned long long>(f.head.head.sequence),
static_cast<unsigned long long>(f.head.head.end_tick));
}
} // namespace
void run_environment_publication_tests() {
{
Fixture producer;
producer.key = {30412, 1};
producer.next_id = 4096; // Commit IDs are unique across every region.
funded_source(producer, 5);
photosynthesis_bundle(producer);
}
Fixture fixture;
funded_source(fixture);
joint_profile(fixture);
forged_evidence(fixture);
std::puts("joint environment publication, intervals, conversions and recovery passed");
}