feat(ecology): record and replay joint environment intervals
ADR-0173 adds joint batch AEVOEBT1: the unchanged population batch plus each tick's recorded infall import and radiated heat. Unattended recording applies each tick's exchange before the shared wildlife controller chooses, so a recorded interval equals the same live joint ticks; replay applies the same exchange and the recorded intentions and must re-derive every flow. Forged flows or boundaries, missing ticks, wrong cuts, truncated input and future formats are refused. Whole-group conversion replays exactly and cohort intervals record identical biology and flows. A full habitat counts infall bubbles skipped and imports nothing for them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -238,6 +238,150 @@ void raw_and_cohort() {
|
||||
<< raw->exported().value << " pJ at tick " << raw->tick() << '\n';
|
||||
}
|
||||
|
||||
// A full habitat refuses new deposits: bubbles are counted skipped, never deferred.
|
||||
void capacity() {
|
||||
auto value = environment(founders(),
|
||||
config({source(1, 0, 1, energy_and_carbon(1ULL << 40, 1ULL << 40))}));
|
||||
for (unsigned tick = 0;
|
||||
tick < 1'000 && value->state().habitat().patches().size() < biology::max_cellular_patches;
|
||||
++tick)
|
||||
step(*value);
|
||||
require(value->state().habitat().patches().size() == biology::max_cellular_patches,
|
||||
"every-tick infall did not reach the deposit limit");
|
||||
const auto before = value->imported();
|
||||
const auto report = checked(value->advance_tick(controlled(value->state())), "full tick");
|
||||
require(report.events == 1 && report.skipped_bubbles > 0 &&
|
||||
report.credited_bubbles + report.skipped_bubbles >=
|
||||
cellular_infall_trial_bubbles_min &&
|
||||
value->state().habitat().patches().size() <= biology::max_cellular_patches,
|
||||
"full habitat accepted or lost infall bubbles");
|
||||
require(report.credited_bubbles > 0 || value->imported() == before,
|
||||
"skipped bubbles were imported");
|
||||
audit(*value);
|
||||
}
|
||||
|
||||
// Unattended joint intervals: recording equals live joint ticks, encoding is canonical,
|
||||
// replay re-derives every flow and refuses tampered evidence.
|
||||
CellularBatchHeader header(const CellularEnvironment& value, std::uint64_t ticks) {
|
||||
CellularBatchHeader made{
|
||||
value.state().world(), value.state().region(), value.state().frame(), {}, {},
|
||||
value.tick(), value.tick() + ticks};
|
||||
made.predecessor.fill(7);
|
||||
made.predecessor_digest.fill(9);
|
||||
return made;
|
||||
}
|
||||
std::vector<CellularLifeIntent> frozen(const CellularLives& state) {
|
||||
auto inputs = controlled(state);
|
||||
for (auto& input : inputs)
|
||||
input.intent.frozen = true;
|
||||
return inputs;
|
||||
}
|
||||
// Budding founders until real wildlife exists; returns its genome (zero if none).
|
||||
std::uint64_t first_wildlife(CellularEnvironment& value) {
|
||||
for (unsigned tick = 0; tick < 30'000; ++tick) {
|
||||
step(value, true);
|
||||
for (const auto& life : value.state().lives())
|
||||
if (life.account == LifeAccount{})
|
||||
return life.identity.genome;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
void traces() {
|
||||
// A closed probe finds when wildlife first exists; the traced source starts after it,
|
||||
// so infall neither changes that history nor fills the habitat beforehand.
|
||||
auto probe = environment(founders(), config());
|
||||
const auto genome = first_wildlife(*probe);
|
||||
require(genome != 0, "no wildlife for joint traces");
|
||||
auto value = environment(
|
||||
founders(),
|
||||
config({source(1, 0, 15, energy_and_carbon(1ULL << 40, 1ULL << 40), probe->tick() + 1)},
|
||||
13));
|
||||
require(first_wildlife(*value) == genome && value->tick() == probe->tick(),
|
||||
"a not-yet-active source changed the region's history");
|
||||
require(!prepare_environment_interval(*value, header(*value, 8)),
|
||||
"active controlled cells entered an unattended interval");
|
||||
for (unsigned tick = 0; tick < cellular_linger_ticks + 10; ++tick)
|
||||
checked(value->advance_tick(frozen(value->state())), "suspend controlled");
|
||||
require(population_interval_safe(value->state()), "controlled cells did not suspend");
|
||||
|
||||
const auto base = value->snapshot();
|
||||
auto prepared = checked(prepare_environment_interval(*value, header(*value, 64)), "record");
|
||||
require(value->snapshot() == base, "recording changed its source");
|
||||
auto live = copy(*value);
|
||||
for (std::uint64_t tick = 0; tick < 64; ++tick)
|
||||
checked(live->advance_tick(frozen(live->state())), "live joint tick");
|
||||
require(live->snapshot() == prepared.state->snapshot(),
|
||||
"recorded interval differs from live joint ticks");
|
||||
const auto bytes = checked(encode_environment_batch(prepared.batch), "encode");
|
||||
const auto decoded = checked(decode_environment_batch(bytes), "decode");
|
||||
require(decoded == prepared.batch, "batch round trip changed evidence");
|
||||
const auto replayed = checked(replay_environment_batch(*value, decoded), "replay");
|
||||
require(replayed->snapshot() == prepared.state->snapshot(), "replay diverged");
|
||||
Inventory crossed{};
|
||||
std::uint64_t radiated = 0;
|
||||
for (const auto& flow : prepared.batch.flows) {
|
||||
crossed.energy.value += flow.imported.energy.value;
|
||||
crossed.matter[0].value += flow.imported.matter[0].value;
|
||||
radiated += flow.exported.value;
|
||||
}
|
||||
require(crossed.energy.value + value->imported().energy.value ==
|
||||
prepared.state->imported().energy.value &&
|
||||
radiated + value->exported().value == prepared.state->exported().value &&
|
||||
crossed != Inventory{} && radiated > 0,
|
||||
"flows are not the interval's complete crossing");
|
||||
|
||||
auto forged = decoded;
|
||||
forged.flows[3].exported.value += 1;
|
||||
require(replay_environment_batch(*value, forged).error() ==
|
||||
CellularEnvironmentError::conservation,
|
||||
"forged radiation accepted");
|
||||
forged = decoded;
|
||||
forged.population.boundaries[5].sequence += 1;
|
||||
require(!replay_environment_batch(*value, forged), "forged population boundary accepted");
|
||||
forged = decoded;
|
||||
forged.flows.pop_back();
|
||||
require(!encode_environment_batch(forged) && !replay_environment_batch(*value, forged),
|
||||
"missing flow accepted");
|
||||
require(replay_environment_batch(*prepared.state, decoded).error() ==
|
||||
CellularEnvironmentError::identity,
|
||||
"batch replayed from the wrong cut");
|
||||
auto truncated = bytes;
|
||||
truncated.pop_back();
|
||||
require(!decode_environment_batch(truncated), "truncated batch accepted");
|
||||
auto future = bytes;
|
||||
future[8] = 2;
|
||||
require(decode_environment_batch(future).error() == CellularEnvironmentError::unsupported,
|
||||
"future batch accepted");
|
||||
|
||||
// Whole-group conversion, then a cohort interval that matches the individual one.
|
||||
auto conversion = checked(
|
||||
prepare_environment_conversion(*prepared.state, header(*prepared.state, 0), {genome, true}),
|
||||
"record conversion");
|
||||
require(conversion.batch.flows.empty() && conversion.state->population().members().size() > 0 &&
|
||||
conversion.state->state().snapshot() == prepared.state->state().snapshot(),
|
||||
"conversion changed biology or crossed the boundary");
|
||||
const auto converted =
|
||||
checked(replay_environment_batch(
|
||||
*prepared.state, checked(decode_environment_batch(checked(
|
||||
encode_environment_batch(conversion.batch), "c")),
|
||||
"decode conversion")),
|
||||
"replay conversion");
|
||||
require(converted->snapshot() == conversion.state->snapshot(), "conversion replay diverged");
|
||||
auto individual =
|
||||
checked(prepare_environment_interval(*prepared.state, header(*prepared.state, 64)),
|
||||
"individual interval");
|
||||
auto cohort =
|
||||
checked(prepare_environment_interval(*conversion.state, header(*conversion.state, 64)),
|
||||
"cohort interval");
|
||||
require(cohort.state->state().snapshot() == individual.state->state().snapshot() &&
|
||||
cohort.batch.flows == individual.batch.flows &&
|
||||
cohort.batch.population.biology == individual.batch.population.biology,
|
||||
"cohort interval changed biology or environmental flows");
|
||||
std::cout << "traces: " << bytes.size() << " bytes for 64 ticks, imported "
|
||||
<< crossed.energy.value << " pJ, radiated " << radiated << " pJ, cohort "
|
||||
<< conversion.state->population().members().size() << " members\n";
|
||||
}
|
||||
|
||||
std::vector<std::uint8_t> resealed(std::vector<std::uint8_t> bytes) {
|
||||
std::uint64_t hash = 14695981039346656037ULL;
|
||||
for (std::size_t i = 0; i + 8 < bytes.size(); ++i)
|
||||
@@ -372,6 +516,8 @@ int main(int argc, char** argv) {
|
||||
radiation();
|
||||
atomic_and_restore();
|
||||
raw_and_cohort();
|
||||
capacity();
|
||||
traces();
|
||||
refusals();
|
||||
auto value = restart_fixture();
|
||||
audit(*value);
|
||||
|
||||
Reference in New Issue
Block a user