Files
aevolution/include/evo/server/lifecycle/cellular_population.hpp
T
dingoandClaude Opus 5.5 3c753970c8
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / postgres-tools (push) Successful in 31s
Foundation / server-image (push) Successful in 4m29s
Foundation / image (push) Successful in 5m43s
feat(cellular): region ticks of up to 4,096 lives for testing (schema 68)
ADR-0182 section 17. The plan's 2,000-organism cap became the rules-12
key region.organism_cap, which was parsed but never enforced; the real
bound was max_cellular_tick_lives = 256 (ADR-0106), which rules 13's
organism cap could not exceed. The bound is now 4,096, for testing limits
only; the DEV tuning file keeps 256.

- max_cellular_tick_lives (4,096) bounds the raw region path: step, owner
  state, batch trace (cellular_batch_bytes_for) and current checkpoint
  traces. max_cellular_frozen_lives (256) keeps cohort populations,
  population batches, migration traces and pre-v3 cellular traces, whose
  database checks are fixed.
- Tuning: reproduction.organism_cap and wildlife_cap accept up to 4,096.
- Schema 68: the trace checker under the current publishers
  (cellular_births_v45) accepts 4,096 intents, births and mutations per
  tick; spawn_wildlife takes up to 256 cells a command.
- Region view and portal: the lineage archive's fill (births of 65,536,
  genomes of 4,096), at which a region stops ticking.
- PostgreSQL suite: 256 wildlife spawned in one command, then a 289-life
  tick published and recovered exactly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 17:30:22 -04:00

99 lines
4.8 KiB
C++

#pragma once
#include "evo/server/lifecycle/cellular_lives.hpp"
#include <cstddef>
#include <cstdint>
#include <expected>
#include <memory>
#include <span>
#include <vector>
namespace evo::server {
struct CellularPopulationBatch;
// ADR-0139: exact local cohorts retain individuals, never mean reserves or samples.
// The single owned CellularLives store backs BOTH representations, exactly once.
struct CellularCohortMember {
std::uint64_t life{}, genome{};
auto operator<=>(const CellularCohortMember&) const = default;
};
enum class CellularPopulationError : std::uint8_t {
unsupported,
absent,
ineligible,
format,
checksum,
identity,
overflow
};
struct CellularTierPolicy {
bool automatic{false};
std::uint32_t materialize_radius_um{1'200}, collapse_radius_um{1'800};
auto operator<=>(const CellularTierPolicy&) const = default;
};
inline constexpr std::size_t cellular_population_header_bytes = 80;
inline constexpr std::uint32_t cellular_population_inheritance_format = 3;
inline constexpr std::size_t max_cellular_population_bytes =
cellular_population_header_bytes + 16 * biology::max_cellular_frozen_lives +
max_cellular_lives_bytes + 8;
class CellularPopulation {
public:
[[nodiscard]] static std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
create(std::unique_ptr<CellularLives> state, CellularTierPolicy policy = {});
CellularPopulation(const CellularPopulation&) = delete;
CellularPopulation& operator=(const CellularPopulation&) = delete;
// Read-only biology view: it confers no separate spending rights.
[[nodiscard]] const CellularLives& state() const noexcept;
[[nodiscard]] std::span<const CellularCohortMember> members() const noexcept;
[[nodiscard]] bool in_cohort(std::uint64_t life) const noexcept;
[[nodiscard]] CellularTierPolicy policy() const noexcept;
[[nodiscard]] std::uint64_t conversion_sequence() const noexcept;
[[nodiscard]] std::uint32_t format_version() const noexcept;
// Explicit no-tick envelope-1 -> envelope-2 upgrade, retaining all cohort tags.
[[nodiscard]] std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
migration_upgrade_candidate() const;
// ADR-0157: preserve exact membership while upgrading nested biology to rules 9.
[[nodiscard]] std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
maturation_upgrade_candidate() const;
// ADR-0171: envelope 3 retains all tags and nested expressed/heritable state.
[[nodiscard]] std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
inheritance_upgrade_candidate() const;
[[nodiscard]] std::vector<std::uint8_t> snapshot() const;
[[nodiscard]] static std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
restore(std::span<const std::uint8_t> bytes, std::uint64_t world, std::uint64_t region,
std::uint64_t frame, std::uint64_t tick);
// Detached exact sample for publication. Never acknowledge it before a fenced
// durable head commits its complete snapshot; recovery restores that sample.
[[nodiscard]] std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
conversion_candidate(std::uint64_t genome, bool to_cohort) const;
// Convert the WHOLE eligible wildlife genome group. No selectable healthy subset,
// resampling, rounding, new identity, healing, age reset or inventory transfer.
// Controlled lives are never members. Refusal leaves biology and ownership unchanged.
[[nodiscard]] std::expected<std::size_t, CellularPopulationError>
collapse(std::uint64_t genome);
[[nodiscard]] std::expected<std::size_t, CellularPopulationError>
materialize(std::uint64_t genome);
// Only controlled-life intentions/selections are supplied by the host, sorted by ID.
// All wildlife, including cohorts, uses the unchanged shared controller and kernel.
[[nodiscard]] std::expected<CellularTickExecution, CellularLivesError>
advance_tick(std::span<const CellularLifeIntent> controlled,
std::span<const CellularMutationSelection> selections = {});
private:
friend class CellularMigration;
friend class CellularEnvironment; // ADR-0172: exclusive joint owner of this population.
friend std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
execute_population_batch(const CellularPopulation&, CellularPopulationBatch&, bool);
explicit CellularPopulation(std::unique_ptr<CellularLives> state, CellularTierPolicy policy);
void apply_policy();
std::unique_ptr<CellularLives> state_;
std::vector<CellularCohortMember> members_; // Sorted life IDs, unique ownership tags.
CellularTierPolicy policy_;
std::uint64_t conversion_sequence_{};
};
} // namespace evo::server