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:
@@ -0,0 +1,190 @@
|
||||
#include "evo/server/lifecycle/cellular_environment.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::server {
|
||||
namespace {
|
||||
constexpr std::uint64_t magic = 0x315442454F564541ULL; // "AEVOEBT1"
|
||||
|
||||
void put(std::vector<std::uint8_t>& out, std::uint64_t value, unsigned width = 8) {
|
||||
for (unsigned i = 0; i < width; ++i) {
|
||||
out.push_back(static_cast<std::uint8_t>(value & 255U));
|
||||
value >>= 8U;
|
||||
}
|
||||
}
|
||||
std::uint64_t get(std::span<const std::uint8_t> bytes, std::size_t at, unsigned width) {
|
||||
std::uint64_t value = 0;
|
||||
for (unsigned i = 0; i < width; ++i)
|
||||
value |= std::uint64_t{bytes[at + i]} << (8U * i);
|
||||
return value;
|
||||
}
|
||||
// The nested population batch must already be canonical for its own format.
|
||||
bool shape(const CellularEnvironmentBatch& batch) {
|
||||
const auto& population = batch.population;
|
||||
if (population.format_version != 2 &&
|
||||
population.format_version != cellular_population_inheritance_format)
|
||||
return false;
|
||||
const auto ticks = population.conversion ? std::size_t{0} : population.biology.ticks.size();
|
||||
return batch.flows.size() == ticks && encode_population_batch(population).has_value();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<std::unique_ptr<CellularEnvironment>, CellularEnvironmentError>
|
||||
execute_environment_batch(const CellularEnvironment& base, CellularEnvironmentBatch& batch,
|
||||
bool record) {
|
||||
auto& population = batch.population;
|
||||
const auto& state = base.state();
|
||||
if (population.format_version != base.population().format_version())
|
||||
return std::unexpected(CellularEnvironmentError::unsupported);
|
||||
const auto& h = population.header;
|
||||
if (h.world != state.world() || h.region != state.region() || h.frame != state.frame() ||
|
||||
h.begin_tick != state.tick())
|
||||
return std::unexpected(CellularEnvironmentError::identity);
|
||||
if (!record && !shape(batch))
|
||||
return std::unexpected(CellularEnvironmentError::format);
|
||||
if (!population.conversion && !population_interval_safe(state))
|
||||
return std::unexpected(CellularEnvironmentError::unsupported);
|
||||
auto candidate = CellularEnvironment::restore(base.snapshot(), state.world(), state.region(),
|
||||
state.frame(), state.tick());
|
||||
if (!candidate)
|
||||
return std::unexpected(candidate.error());
|
||||
auto& joint = **candidate;
|
||||
if (record) {
|
||||
population.boundaries.clear();
|
||||
batch.flows.clear();
|
||||
}
|
||||
const auto boundary = [&](std::size_t index) {
|
||||
auto actual = joint.population_boundary();
|
||||
if (record) {
|
||||
population.boundaries.push_back(std::move(actual));
|
||||
return true;
|
||||
}
|
||||
return population.boundaries[index] == actual;
|
||||
};
|
||||
if (population.conversion) {
|
||||
const auto converted = population.conversion->to_cohort
|
||||
? joint.collapse(population.conversion->genome)
|
||||
: joint.materialize(population.conversion->genome);
|
||||
if (!converted || !boundary(0))
|
||||
return std::unexpected(CellularEnvironmentError::biology);
|
||||
} else {
|
||||
const auto count = record ? h.end_tick - h.begin_tick : population.biology.ticks.size();
|
||||
if (record) {
|
||||
population.biology.header = h;
|
||||
population.biology.ticks.clear();
|
||||
population.biology.format_version =
|
||||
state.rules_version() == cellular_lives_inheritance_rules
|
||||
? cellular_batch_inheritance_format
|
||||
: cellular_batch_format;
|
||||
}
|
||||
for (std::size_t index = 0; index < count; ++index) {
|
||||
std::vector<CellularLifeIntent> intents;
|
||||
if (!record)
|
||||
intents = population.biology.ticks[index].lives;
|
||||
const auto flow = joint.advance_unattended(intents, record);
|
||||
if (!flow)
|
||||
return std::unexpected(flow.error());
|
||||
if (record) {
|
||||
population.biology.ticks.push_back({std::move(intents), {}, {}});
|
||||
batch.flows.push_back(*flow);
|
||||
} else if (batch.flows[index] != *flow) {
|
||||
return std::unexpected(CellularEnvironmentError::conservation);
|
||||
}
|
||||
if (!boundary(index))
|
||||
return std::unexpected(CellularEnvironmentError::biology);
|
||||
}
|
||||
}
|
||||
if (record && !shape(batch))
|
||||
return std::unexpected(CellularEnvironmentError::format);
|
||||
return candidate;
|
||||
}
|
||||
|
||||
std::expected<CellularEnvironmentExecution, CellularEnvironmentError>
|
||||
prepare_environment_interval(const CellularEnvironment& base, const CellularBatchHeader& header) {
|
||||
if (header.end_tick <= header.begin_tick ||
|
||||
header.end_tick - header.begin_tick > max_cellular_batch_ticks)
|
||||
return std::unexpected(CellularEnvironmentError::identity);
|
||||
CellularEnvironmentBatch batch;
|
||||
batch.population.header = header;
|
||||
batch.population.format_version = base.population().format_version();
|
||||
auto state = execute_environment_batch(base, batch, true);
|
||||
if (!state)
|
||||
return std::unexpected(state.error());
|
||||
return CellularEnvironmentExecution{std::move(batch), std::move(*state)};
|
||||
}
|
||||
std::expected<CellularEnvironmentExecution, CellularEnvironmentError>
|
||||
prepare_environment_conversion(const CellularEnvironment& base, CellularBatchHeader header,
|
||||
CellularPopulationConversion conversion) {
|
||||
CellularEnvironmentBatch batch;
|
||||
batch.population = {header, {}, conversion, {}};
|
||||
batch.population.format_version = base.population().format_version();
|
||||
auto state = execute_environment_batch(base, batch, true);
|
||||
if (!state)
|
||||
return std::unexpected(state.error());
|
||||
return CellularEnvironmentExecution{std::move(batch), std::move(*state)};
|
||||
}
|
||||
std::expected<std::unique_ptr<CellularEnvironment>, CellularEnvironmentError>
|
||||
replay_environment_batch(const CellularEnvironment& base, const CellularEnvironmentBatch& batch) {
|
||||
auto copy = batch;
|
||||
return execute_environment_batch(base, copy, false);
|
||||
}
|
||||
|
||||
std::expected<std::vector<std::uint8_t>, CellularEnvironmentError>
|
||||
encode_environment_batch(const CellularEnvironmentBatch& batch) {
|
||||
if (!shape(batch))
|
||||
return std::unexpected(CellularEnvironmentError::format);
|
||||
const auto nested = encode_population_batch(batch.population);
|
||||
if (!nested)
|
||||
return std::unexpected(CellularEnvironmentError::format);
|
||||
std::vector<std::uint8_t> out;
|
||||
put(out, magic);
|
||||
put(out, cellular_environment_batch_format, 4);
|
||||
put(out, batch.population.conversion ? 1 : 0, 4);
|
||||
put(out, nested->size());
|
||||
put(out, batch.flows.size(), 4);
|
||||
put(out, 0, 4);
|
||||
out.insert(out.end(), nested->begin(), nested->end());
|
||||
for (const auto& flow : batch.flows) {
|
||||
put(out, flow.imported.energy.value);
|
||||
for (const auto& channel : flow.imported.matter)
|
||||
put(out, channel.value);
|
||||
put(out, flow.exported.value);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::expected<CellularEnvironmentBatch, CellularEnvironmentError>
|
||||
decode_environment_batch(std::span<const std::uint8_t> bytes) {
|
||||
if (bytes.size() < environment_batch_header_bytes || bytes.size() > max_environment_batch_bytes)
|
||||
return std::unexpected(CellularEnvironmentError::format);
|
||||
if (get(bytes, 0, 8) != magic)
|
||||
return std::unexpected(CellularEnvironmentError::format);
|
||||
if (get(bytes, 8, 4) != cellular_environment_batch_format)
|
||||
return std::unexpected(CellularEnvironmentError::unsupported);
|
||||
const auto kind = get(bytes, 12, 4), nested = get(bytes, 16, 8), flows = get(bytes, 24, 4);
|
||||
if (kind > 1 || get(bytes, 28, 4) != 0 || nested > max_population_batch_bytes ||
|
||||
flows > max_cellular_batch_ticks ||
|
||||
bytes.size() != environment_batch_header_bytes + nested + flows * environment_flow_bytes)
|
||||
return std::unexpected(CellularEnvironmentError::format);
|
||||
auto population = decode_population_batch(
|
||||
bytes.subspan(environment_batch_header_bytes, static_cast<std::size_t>(nested)));
|
||||
if (!population || population->conversion.has_value() != (kind == 1))
|
||||
return std::unexpected(CellularEnvironmentError::format);
|
||||
CellularEnvironmentBatch batch{std::move(*population), {}};
|
||||
auto at = environment_batch_header_bytes + static_cast<std::size_t>(nested);
|
||||
for (std::uint64_t index = 0; index < flows; ++index) {
|
||||
CellularEnvironmentFlow flow;
|
||||
flow.imported.energy.value = get(bytes, at, 8);
|
||||
for (std::size_t channel = 0; channel < flow.imported.matter.size(); ++channel)
|
||||
flow.imported.matter[channel].value = get(bytes, at + 8 + 8 * channel, 8);
|
||||
flow.exported.value = get(bytes, at + 40, 8);
|
||||
at += environment_flow_bytes;
|
||||
batch.flows.push_back(flow);
|
||||
}
|
||||
const auto canonical = encode_environment_batch(batch);
|
||||
if (!canonical || !std::ranges::equal(*canonical, bytes))
|
||||
return std::unexpected(CellularEnvironmentError::format);
|
||||
return batch;
|
||||
}
|
||||
} // namespace evo::server
|
||||
Reference in New Issue
Block a user