diff --git a/CMakeLists.txt b/CMakeLists.txt index f03131f..f39cc88 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,7 +95,7 @@ add_library(evo_lifecycle STATIC src/server/lifecycle/primitive_lives.cpp src/server/lifecycle/cellular_play_summary.cpp src/server/lifecycle/cellular_population.cpp src/server/lifecycle/cellular_population_codec.cpp src/server/lifecycle/cellular_population_policy.cpp src/server/lifecycle/cellular_schedule.cpp src/server/lifecycle/cellular_population_batch.cpp - src/server/lifecycle/cellular_environment.cpp) + src/server/lifecycle/cellular_environment.cpp src/server/lifecycle/cellular_environment_batch.cpp) add_library(evo::lifecycle ALIAS evo_lifecycle) target_link_libraries(evo_lifecycle PUBLIC evo::biology evo::world) evo_owned_target(evo_lifecycle) diff --git a/deploy/kubernetes/region/dev-session-route/listener.yaml b/deploy/kubernetes/region/dev-session-route/listener.yaml index 396340f..e05117c 100644 --- a/deploy/kubernetes/region/dev-session-route/listener.yaml +++ b/deploy/kubernetes/region/dev-session-route/listener.yaml @@ -12,7 +12,7 @@ spec: env: - {name: REGION_SESSION, value: "dev"} - {name: REGION_SESSION_LISTEN, value: "0.0.0.0:7400"} - - {name: REGION_SESSION_WINDOW, value: "dev-20261019T195802-5aa0ff3e:1792454282"} + - {name: REGION_SESSION_WINDOW, value: "dev-20261019T201509-a1dcbfd7:1792455309"} - {name: REGION_M10_MAX_GRANT_LIFETIME, value: "14400"} ports: # QUIC (ADR-0090): UDP only; nothing listens on TCP 7400. diff --git a/docs/STATUS.md b/docs/STATUS.md index 1b9ed38..b22a26c 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -12,6 +12,11 @@ tick or not at all. Closed and source-off regions keep their biology byte for by Raw and cohort representations agree under infall. Old formats and goldens keep their meaning; no default/live version changes. +[ADR-0173](adr/0173-cellular-environment-batch.md) adds joint batch `AEVOEBT1`: +the unchanged population batch plus each tick's recorded import and radiated heat. +Recorded unattended intervals equal the same live joint ticks; replay re-derives +every flow and refuses forged flows, boundaries and cuts. + Both full Release suites pass **94/94**; see [Validation](engineering/VALIDATION.md). Producers (ADR-0136 step C), durable admission (12.5c), worker/census/migration integration (12.5d) and target-host qualification (12.5e) remain open. diff --git a/docs/adr/0173-cellular-environment-batch.md b/docs/adr/0173-cellular-environment-batch.md new file mode 100644 index 0000000..59e1f2f --- /dev/null +++ b/docs/adr/0173-cellular-environment-batch.md @@ -0,0 +1,48 @@ +# ADR-0173: Cellular environment joint batch + +## Status and scope + +Accepted portable M12.5b evidence, 2026-10-19. Extends ADR-0172 and the population +batches of ADR-0142/0144/0171. No durable admission, SQL schema, checkpoint profile, +automatic scheduling or live/default rule change. + +## Decision + +Joint batch 1 (`AEVOEBT1`) carries one complete, unchanged population batch (format +2 for envelope 2/owner 7, format 3 for envelope 3/owner 8) plus one recorded +environmental flow per biological tick: imported energy/C/N/P/other and radiated +heat. A whole-group conversion carries no flows. The joint owner's configuration, +source progress and nested population are all in the predecessor envelope, so the +batch adds no environmental input: replay re-derives every infall and export from +configuration, tick and seed and must reproduce each recorded flow exactly. + +**Unattended intervals.** Every controlled cell must be fully suspended at the cut +and stays frozen. For each tick the recorder applies the joint exchange, then runs +the unchanged wildlife controller on the exchanged state, so new deposits are +visible to that tick's choices exactly as in a live joint tick. Recording equals the +same ticks advanced live. Replay applies the same exchange, then the recorded +intentions; the controller is not rerun. Cohort boundaries (sequence and members) +are checked after each tick, as in the population batch. A whole-group conversion +changes membership only. + +The nested population batch keeps its exact format and meaning. It is never valid +evidence for a joint result on its own: replaying it without the exchange spends +different stock. Flows are evidence, not authority: a forged flow, boundary, +missing tick, wrong cut, future format or truncated input is refused before any +result is returned, and the base is never modified. + +## Consequences + +Durable joint publication (12.5c) can bind a manifest to the predecessor joint +envelope and this complete trace, and recovery can replay it exactly without +re-deriving controller choices. Recorded flows let SQL-side evidence report +environmental crossings separately from migration flows without replay. +Representation conversion is exact: cohort and individual intervals record +identical biology and flows. No performance, compression or target-host claim. + +## Alternatives + +Recording the population batch alone and re-deriving flows was rejected: an +auditor could not distinguish environmental crossings from migration without +replay. Running the controller before the exchange was rejected: it would make +unattended intervals diverge from live joint ticks. diff --git a/docs/adr/README.md b/docs/adr/README.md index 3f41bb1..9caecec 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -1,5 +1,7 @@ # Architecture decision records +- [ADR-0173: Cellular environment joint batch](0173-cellular-environment-batch.md) - portable `AEVOEBT1` wraps the unchanged population batch with per-tick recorded environmental flows; unattended recording runs the controller after each exchange and replay re-derives every flow exactly. + - [ADR-0172: Cellular environment ownership](0172-cellular-environment-ownership.md) - one owner per patch of space for cells and their local environment; reserves stay individual; open planet boundary with recorded imports/exports; portable envelope `AEVOCEN1` with finite infall entitlements and radiative heat export. - [ADR-0171: Retained-plan exact populations](0171-retained-plan-exact-populations.md) - portable envelope/batch 3 preserve every member's expressed/heritable state, exact conversion/policy replay and projection-2 plan counts; durable admission remains separate. diff --git a/docs/engineering/M12_REMAINING_GATES.md b/docs/engineering/M12_REMAINING_GATES.md index 4dccc49..779c2bf 100644 --- a/docs/engineering/M12_REMAINING_GATES.md +++ b/docs/engineering/M12_REMAINING_GATES.md @@ -66,7 +66,7 @@ or authorize production enablement. | 12.2 | Exact cellular model and paired matrix; local samples through 256 lives; bounded four-generation rules-9 predation/twin matrix | Deployment-host tail costs including publication; larger wildlife cohorts and fuller contention coverage; useful compression remains unimplemented. | | 12.3 | Dedicated population profile/trace/store/owning host; exact recovery and private PostgreSQL fault tests | Population-specific executable/network fault drills and target-host publication/capacity qualification. | | 12.4 | Owned intervals; exact transfer/accounting; durable atomic pair/recovery; independent loopback TLS path loss and owner expiry/replacement/heal; local object sync/read-failure matrix | Multi-host/kernel-network, storage-host and orchestrator drills, linked-boundary contracts and integrated ownership/stock audits at scale. | -| 12.5 | ADR-0172 portable joint owner: one budget for cells and local environment, effective depth, tagged water/dissolved compartments, refused atmosphere, finite infall entitlements, radiative export; closed/source-off/open/exhaustion and raw/cohort comparisons | Producer process and coefficients (ADR-0136 step C), durable profile/trace/recovery (12.5c), worker/census/migration integration and escrowed cross-worker sources (12.5d), target-host qualification (12.5e). | +| 12.5 | ADR-0172 portable joint owner: one budget for cells and local environment, effective depth, tagged water/dissolved compartments, refused atmosphere, finite infall entitlements, radiative export; closed/source-off/open/exhaustion and raw/cohort comparisons; ADR-0173 joint batch with exact unattended replay | Producer process and coefficients (ADR-0136 step C), durable profile/trace/recovery (12.5c), worker/census/migration integration and escrowed cross-worker sources (12.5d), target-host qualification (12.5e). | | 12.6 | Retained archive, regional census/lineage queries, replayed history, scoped multi-region queries and consistent database head snapshots (12.6a-d); registered coverage/history domains follow in 12.7a-b | Cross-domain clone provenance, retained historical coverage/other viable stores, target-host query/index qualification and client permission/presentation; no account power or invented precise statistical ancestry. | | 12.7 | Viability/unknown policy; consistent cuts, registered coverage, history domains/restore forks and verified pending handoff evidence (12.7a-c/schema 52) | Cross-domain clone provenance, other viable-store coverage and durable accepted global evidence records/freshness. Missing/stale/unsupported owners must prevent terminal extinction. | | 12.8 | Deterministic seed/regime drivers, separate-process exact continuation and bounded unscripted generations | Reproducible integrated worlds running for actual days, real restarts/clone comparisons and retained accounting/diversity/cost audits. Accelerated tests cannot supply elapsed days. | diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 13971ca..ac2c6da 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -1,5 +1,38 @@ # Validation report +## 2026-10-19 - Cellular environment joint batch (M12.5b) + +[ADR-0173](../adr/0173-cellular-environment-batch.md) adds joint batch +`AEVOEBT1`: the unchanged population batch plus each tick's recorded import and +radiated heat. Recording runs the shared wildlife controller after each tick's +exchange; replay applies the same exchange and the recorded intentions. + +Executed locally: + +- GCC 14.2 Release full CTest **94/94, 4.97 s** and MSVC 19.44 Release full CTest + **94/94, 6.64 s**, with NDEBUG. +- Clang 19.1 ASan/UBSan cellular subset **28/28, 107.92 s** (environment + contracts 81.52 s), with no sanitizer reports. +- clang-tidy 19 (warnings as errors) and clang-format 19 on the joint owner, batch + and tests. GCC 14 -O3 reported `free-nonheap-object` for `reserve` followed by + `push_back` in the encoder, a known false positive on this toolchain; the encoder + no longer reserves. No warning was suppressed. + +Evidence in the suite: a 64-tick unattended interval (20,760 bytes, 11,200,000 pJ +imported, 832 pJ radiated) equals the same ticks advanced live, round-trips exactly +and replays to identical bytes; its flows are the interval's complete crossing. +Replay refuses a forged radiated amount, a forged cohort boundary, a missing flow, +the wrong predecessor cut, a truncated input and a future format; active controlled +cells cannot start an interval. A whole-group conversion changes membership only and +replays exactly; the subsequent cohort interval records identical biology and flows +to the individual one. A full habitat (4,096 deposits) counts infall bubbles skipped +and imports nothing for them. An initial fixture ran an every-15-tick source through +about 6,000 budding ticks, filled the habitat and correctly imported nothing in the +interval; the source now starts after a closed probe finds the first wildlife, and the +test checks that the not-yet-active source left that history unchanged. + +No durable admission, SQL schema or target-host claim. + ## 2026-10-19 - Cellular environment joint owner (M12.5a-b) [ADR-0172](../adr/0172-cellular-environment-ownership.md) records the maintainer's diff --git a/include/evo/server/lifecycle/cellular_environment.hpp b/include/evo/server/lifecycle/cellular_environment.hpp index 55a9707..0bdd431 100644 --- a/include/evo/server/lifecycle/cellular_environment.hpp +++ b/include/evo/server/lifecycle/cellular_environment.hpp @@ -1,6 +1,6 @@ #pragma once -#include "evo/server/lifecycle/cellular_population.hpp" +#include "evo/server/lifecycle/cellular_population_batch.hpp" #include "evo/world/coordinates.hpp" #include "evo/world/environment_units.hpp" @@ -87,6 +87,14 @@ enum class CellularEnvironmentError : std::uint8_t { checksum }; +// ADR-0173: one tick's recorded environmental crossing. Replay re-derives it from +// configuration, tick and seed and must reproduce it exactly; it is evidence, not input. +struct CellularEnvironmentFlow { + accounting::Inventory imported{}; + accounting::Picojoules exported{}; + bool operator==(const CellularEnvironmentFlow&) const = default; +}; +struct CellularEnvironmentBatch; class CellularEnvironment { public: // Consumes the population on success or failure. @@ -128,14 +136,54 @@ class CellularEnvironment { std::uint64_t frame, std::uint64_t tick); private: + friend std::expected, CellularEnvironmentError> + execute_environment_batch(const CellularEnvironment&, CellularEnvironmentBatch&, bool record); struct Exchange; CellularEnvironment(std::unique_ptr population, CellularEnvironmentConfig config); [[nodiscard]] std::expected plan_exchange(std::uint64_t tick) const; + // One unattended joint tick on a detached candidate: the exchange, then frozen + // controlled intentions with either the recorded wildlife intentions (replay) or + // the shared controller's choice on the exchanged state (recording, `intents` empty). + [[nodiscard]] std::expected + advance_unattended(std::vector& intents, bool record); + [[nodiscard]] CellularPopulationBoundary population_boundary() const; std::unique_ptr population_; CellularEnvironmentConfig config_; std::vector progress_; // Source order. accounting::Picojoules exported_{}; }; + +// Joint batch 1: the unchanged population batch (interval or whole-group conversion) +// plus one flow per biological tick. Conversions carry no flows. +struct CellularEnvironmentBatch { + CellularPopulationBatch population; + std::vector flows; + bool operator==(const CellularEnvironmentBatch&) const = default; +}; +inline constexpr std::uint32_t cellular_environment_batch_format = 1; +inline constexpr std::size_t environment_batch_header_bytes = 32; +inline constexpr std::size_t environment_flow_bytes = 48; +inline constexpr std::size_t max_environment_batch_bytes = + environment_batch_header_bytes + max_population_batch_bytes + + environment_flow_bytes * max_cellular_batch_ticks; +struct CellularEnvironmentExecution { + CellularEnvironmentBatch batch; + std::unique_ptr state; +}; +// An unattended interval: every controlled cell must be fully suspended. Each tick +// applies its exchange, then the unchanged wildlife controller chooses on that state; +// the recorded intentions replay without rerunning the controller. +[[nodiscard]] std::expected +prepare_environment_interval(const CellularEnvironment& base, const CellularBatchHeader& header); +[[nodiscard]] std::expected +prepare_environment_conversion(const CellularEnvironment& base, CellularBatchHeader header, + CellularPopulationConversion conversion); +[[nodiscard]] std::expected, CellularEnvironmentError> +replay_environment_batch(const CellularEnvironment& base, const CellularEnvironmentBatch& batch); +[[nodiscard]] std::expected, CellularEnvironmentError> +encode_environment_batch(const CellularEnvironmentBatch& batch); +[[nodiscard]] std::expected +decode_environment_batch(std::span bytes); } // namespace evo::server diff --git a/schemas/cellular-environment-batch-v1.md b/schemas/cellular-environment-batch-v1.md new file mode 100644 index 0000000..4b78c1b --- /dev/null +++ b/schemas/cellular-environment-batch-v1.md @@ -0,0 +1,22 @@ +# Cellular environment joint batch 1 + +Authority: ADR-0173. Little-endian integers, no padding. Size is bounded by +`max_environment_batch_bytes`. Existing population and cellular batch formats are +unchanged. + +| Offset | Bytes | Field | +|---:|---:|---| +| 0 | 8 | Magic `AEVOEBT1` | +| 8 | 4 | Format 1 | +| 12 | 4 | Kind: 0 biological interval, 1 whole-group conversion | +| 16 | 8 | Nested population batch bytes | +| 24 | 4 | Flow count: ticks for kind 0, zero for kind 1 | +| 28 | 4 | Reserved, zero | +| 32 | declared length | Canonical population batch 2 or 3 | +| after nested | 48 each | Per tick: imported energy, C, N, P, other (u64 each), then radiated heat pJ | + +Decoding requires exact length, a canonical nested batch whose kind matches, and +byte-identical re-encoding. Replay additionally requires the predecessor joint +envelope at the batch's begin tick with a matching population format, fully +suspended controlled cells for an interval, and exact equality of every re-derived +flow and cohort boundary. The nested batch alone never establishes a joint result. diff --git a/src/server/lifecycle/cellular_environment.cpp b/src/server/lifecycle/cellular_environment.cpp index 0bf5f86..62ad911 100644 --- a/src/server/lifecycle/cellular_environment.cpp +++ b/src/server/lifecycle/cellular_environment.cpp @@ -1,6 +1,7 @@ #include "evo/server/lifecycle/cellular_environment.hpp" #include "evo/core/random.hpp" +#include "evo/server/lifecycle/wildlife_controller.hpp" #include #include @@ -309,6 +310,62 @@ CellularEnvironment::advance_tick(std::span controlled return std::move(exchange->report); } +std::expected +CellularEnvironment::advance_unattended(std::vector& intents, bool record) { + if (tick() == std::numeric_limits::max()) + return std::unexpected(CellularEnvironmentError::overflow); + auto exchange = plan_exchange(tick() + 1); + if (!exchange) + return std::unexpected(exchange.error()); + auto& lives = *population_->state_; + auto savepoint = lives.exchange_boundary(exchange->deposits, exchange->heat_export); + if (!savepoint) + return std::unexpected(savepoint.error() == CellularLivesError::habitat + ? CellularEnvironmentError::habitat + : CellularEnvironmentError::conservation); + biology::CellularStepIntent frozen; + frozen.frozen = true; + if (record) { + auto chosen = wildlife_intents(lives, lives.tick() + 1); + if (!chosen) + return std::unexpected(CellularEnvironmentError::biology); + for (const auto& life : lives.lives()) + if (life.account != LifeAccount{}) + chosen->push_back({life.identity.life, frozen}); + std::ranges::sort(*chosen, {}, &CellularLifeIntent::life_id); + intents = std::move(*chosen); + } + for (const auto& life : lives.lives()) { + if (life.account == LifeAccount{}) + continue; + const auto found = + std::ranges::find(intents, life.identity.life, &CellularLifeIntent::life_id); + if (life.presence != CellularPresence::suspended || found == intents.end() || + found->intent != frozen) + return std::unexpected(CellularEnvironmentError::unsupported); + } + if (!lives.advance_tick(intents)) + return std::unexpected(CellularEnvironmentError::biology); + auto& members = population_->members_; + std::erase_if(members, [&lives](const auto& member) { + return std::ranges::find(lives.lives(), member.life, [](const auto& life) { + return life.identity.life; + }) == lives.lives().end(); + }); + // The persisted policy's exact result; no random materialization or resampling. + if (population_->policy_.automatic) { + if (population_->conversion_sequence_ == std::numeric_limits::max()) + return std::unexpected(CellularEnvironmentError::overflow); + population_->apply_policy(); + } + progress_ = std::move(exchange->progress); + exported_.value += exchange->heat_export.value; + return CellularEnvironmentFlow{exchange->report.imported, exchange->heat_export}; +} +CellularPopulationBoundary CellularEnvironment::population_boundary() const { + return {population_->conversion_sequence_, population_->members_}; +} + std::expected CellularEnvironment::collapse(std::uint64_t genome) { const auto converted = population_->collapse(genome); diff --git a/src/server/lifecycle/cellular_environment_batch.cpp b/src/server/lifecycle/cellular_environment_batch.cpp new file mode 100644 index 0000000..c4d01ec --- /dev/null +++ b/src/server/lifecycle/cellular_environment_batch.cpp @@ -0,0 +1,190 @@ +#include "evo/server/lifecycle/cellular_environment.hpp" + +#include +#include + +namespace evo::server { +namespace { +constexpr std::uint64_t magic = 0x315442454F564541ULL; // "AEVOEBT1" + +void put(std::vector& out, std::uint64_t value, unsigned width = 8) { + for (unsigned i = 0; i < width; ++i) { + out.push_back(static_cast(value & 255U)); + value >>= 8U; + } +} +std::uint64_t get(std::span 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, 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 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 +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 +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, CellularEnvironmentError> +replay_environment_batch(const CellularEnvironment& base, const CellularEnvironmentBatch& batch) { + auto copy = batch; + return execute_environment_batch(base, copy, false); +} + +std::expected, 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 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 +decode_environment_batch(std::span 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(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(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 diff --git a/tests/cellular_environment_tests.cpp b/tests/cellular_environment_tests.cpp index cc1826c..28761df 100644 --- a/tests/cellular_environment_tests.cpp +++ b/tests/cellular_environment_tests.cpp @@ -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 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 resealed(std::vector 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);