Foundation / checks (ecs) (push) Successful in 2m24s
Foundation / checks (deploy) (push) Successful in 4m51s
Foundation / checks (network) (push) Successful in 20m42s
Foundation / checks (client) (push) Successful in 21m53s
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 40m19s
Foundation / checks (postgres) (push) Canceled after 40m9s
Foundation / checks (tsan) (push) Canceled after 37m53s
ADR-0174 and schema 56 add checkpoint profile 6, which stores the joint cell/environment envelope nesting population envelope 2 (bundles 10 and 11). Entry from a committed population head is explicit, one-way and no-tick, with the source commit, manifest and payload digests bound in immutable evidence. Joint intervals use schema 45's owned admission and settlement; conversions cannot overtake a pending interval. The fenced publisher validates the joint trace structurally, including the complete nested population batch, and records per-commit imported and radiated totals. Recovery replays the head's link and requires the payload exactly, refusing a forged but structurally valid trace. A trigger refuses profile-6 commits from every other publisher; profile 4/5 bytes, parsers and rules are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
411 lines
20 KiB
C++
411 lines
20 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.
|
|
void funded_source(Fixture& f) {
|
|
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, 4, 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");
|
|
}
|
|
} // namespace
|
|
|
|
void run_environment_publication_tests() {
|
|
Fixture fixture;
|
|
funded_source(fixture);
|
|
joint_profile(fixture);
|
|
forged_evidence(fixture);
|
|
std::puts("joint environment publication, intervals, conversions and recovery passed");
|
|
}
|