M12.3: persist exact population state and automatic conversion policy
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 3m16s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (client) (push) Canceled after 13m8s
Foundation / checks (checks) (push) Canceled after 13m12s
Foundation / checks (postgres) (push) Canceled after 13m2s
Foundation / checks (network) (push) Canceled after 13m4s
Foundation / checks (tsan) (push) Canceled after 13m9s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 3m16s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (client) (push) Canceled after 13m8s
Foundation / checks (checks) (push) Canceled after 13m12s
Foundation / checks (postgres) (push) Canceled after 13m2s
Foundation / checks (network) (push) Canceled after 13m4s
Foundation / checks (tsan) (push) Canceled after 13m9s
This commit is contained in:
+15
-1
@@ -88,7 +88,8 @@ evo_owned_target(evo_biology)
|
||||
add_library(evo_lifecycle STATIC src/server/lifecycle/primitive_lives.cpp
|
||||
src/server/lifecycle/primitive_batch.cpp src/server/lifecycle/cellular_lives.cpp
|
||||
src/server/lifecycle/cellular_batch.cpp src/server/lifecycle/wildlife_controller.cpp
|
||||
src/server/lifecycle/cellular_play_summary.cpp src/server/lifecycle/cellular_population.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)
|
||||
add_library(evo::lifecycle ALIAS evo_lifecycle)
|
||||
target_link_libraries(evo_lifecycle PUBLIC evo::biology)
|
||||
evo_owned_target(evo_lifecycle)
|
||||
@@ -660,6 +661,19 @@ if(BUILD_TESTING)
|
||||
set_tests_properties(cellular-cohort-qualification PROPERTIES TIMEOUT 180)
|
||||
endif()
|
||||
|
||||
if(BUILD_TESTING)
|
||||
add_executable(evo-cellular-population-tests tests/cellular_population_tests.cpp)
|
||||
target_link_libraries(evo-cellular-population-tests PRIVATE evo::lifecycle)
|
||||
evo_owned_target(evo-cellular-population-tests)
|
||||
add_test(NAME cellular-population-transitions COMMAND evo-cellular-population-tests)
|
||||
set_tests_properties(cellular-population-transitions PROPERTIES TIMEOUT 180)
|
||||
add_test(NAME cellular-population-process-restart COMMAND "${CMAKE_COMMAND}"
|
||||
"-DPOPULATION_TEST=$<TARGET_FILE:evo-cellular-population-tests>"
|
||||
"-DTEST_DIR=${CMAKE_CURRENT_BINARY_DIR}/population-restart"
|
||||
-P "${CMAKE_CURRENT_SOURCE_DIR}/tests/cellular_population_restart.cmake")
|
||||
set_tests_properties(cellular-population-process-restart PROPERTIES TIMEOUT 180)
|
||||
endif()
|
||||
|
||||
add_executable(evo-chunk-layout experiments/world/chunk_layout.cpp)
|
||||
target_link_libraries(evo-chunk-layout PRIVATE evo::world)
|
||||
evo_owned_target(evo-chunk-layout)
|
||||
|
||||
@@ -1,5 +1,16 @@
|
||||
# Project status and next steps
|
||||
|
||||
## M12.3a exact conversion recovery - 2026-10-17
|
||||
|
||||
Population envelope 1 retains biology, cohort membership, policy and conversion
|
||||
sequence together. Detached candidates preserve their predecessor; opt-in
|
||||
automatic hysteresis is deterministic and disabled by default. Separate-process
|
||||
continuation matches uninterrupted execution, with malformed input, oscillation
|
||||
and sequence exhaustion checks. Release tests pass 74/74, plus clang-format/tidy
|
||||
19 and ASan/UBSan. [ADR-0140](adr/0140-exact-population-checkpoint-and-policy.md)
|
||||
records the remaining production profile/trace/publication adapter; local file
|
||||
recovery does not close that gate. No deployment or live format change.
|
||||
|
||||
## M12.2d exact cellular cohorts - 2026-10-17
|
||||
|
||||
Under the maintainer's delegated M12.2 authority, ADR-0139 selects exact local
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
# ADR-0140: Exact population checkpoint and automatic local policy
|
||||
|
||||
## Status
|
||||
|
||||
Accepted for the portable M12.3 implementation, 2026-10-17. Production publication
|
||||
and enabling remain separate gates. No live region changes or deployment.
|
||||
|
||||
## Decision
|
||||
|
||||
Persist exact biology and exclusive cohort membership together in population
|
||||
envelope 1, never in a sidecar that can get ahead of its biological checkpoint.
|
||||
Retain the existing nested rules-8 owner bytes without reinterpretation. Store
|
||||
policy configuration and a monotonic conversion sequence. Recovery restores
|
||||
the exact committed membership sample; it does not repeat a random allocation.
|
||||
Exact members have no fractional demographic residual or sampling stream.
|
||||
|
||||
Provide detached conversion candidates: the predecessor remains untouched until
|
||||
the caller publishes the entire candidate under the current durable ownership
|
||||
fence. Pre-publication loss recovers the predecessor; confirmed publication must
|
||||
recover the complete successor. In-memory success is not a durable receipt.
|
||||
Sequence exhaustion refuses conversion before mutation. Unknown versions,
|
||||
malformed bounds, noncanonical member IDs, wrong identity/tick, ineligible
|
||||
members and corrupt checksums are refused.
|
||||
|
||||
An opt-in automatic policy runs only after a successful biological tick. Whole
|
||||
wildlife genome groups materialize if any member is within 1200 micrometres of
|
||||
an active/lingering controlled observer. They collapse if all members are beyond
|
||||
1800 micrometres of every such observer, including when none exist. The band
|
||||
retains current membership. Distances use lower-left positions in the existing
|
||||
local coordinate frame. Suspended controlled lives are not observers and never
|
||||
become cohort members. Unsupported wildlife presence keeps the group individual.
|
||||
Radii are configurable, bounded, persisted trial policy rather than locked
|
||||
biological constants. No state, phenotype, resource or ancestry changes at a
|
||||
conversion. Defaults keep automation disabled.
|
||||
|
||||
## Boundary
|
||||
|
||||
The portable owner, codec and separate-process continuation checks implement
|
||||
state closure. They do not supply a new PostgreSQL publication profile, durable
|
||||
conversion trace, database fencing, lost-reply reconciliation or host integration.
|
||||
Do not place this envelope under existing profile 4: that profile contains raw
|
||||
CellularLives bytes. The new payload needs a dedicated profile/schema migration
|
||||
and recovery/publication adapter before production use. Existing M7/M11 stores
|
||||
and live sessions remain on their existing accepted formats.
|
||||
|
||||
This explicit boundary permits engineering work on the exact representation
|
||||
without mistaking a local file test for production crash-consistent durability.
|
||||
@@ -157,3 +157,5 @@ design. No unadopted candidate is a link dependency of the proof.
|
||||
- [ADR-0085: Rebindable engineering controls](0085-rebindable-engineering-controls.md) - SDL-free action/HID-key bindings, conflict validation, versioned all-or-nothing controls file, binding capture before toolkit/world.
|
||||
|
||||
- [ADR-0139: Exact cellular local cohorts](0139-exact-cellular-local-cohorts.md)
|
||||
|
||||
- [ADR-0140: Exact population checkpoint and policy](0140-exact-population-checkpoint-and-policy.md)
|
||||
|
||||
@@ -20,9 +20,23 @@ removes stale ownership tags; funded births begin as individuals. Suspension
|
||||
continues to freeze controlled biology regardless of representation elsewhere.
|
||||
|
||||
The model retains exact members and makes no aggregation speedup claim. There is
|
||||
no regional population approximation, process migration, production selector or
|
||||
new persisted format in this segment. The existing 256-life ceiling remains;
|
||||
target-host service capacity is a separate qualification.
|
||||
no regional population approximation, process migration or production selector.
|
||||
The existing 256-life ceiling remains; target-host service capacity is separate.
|
||||
|
||||
M12.3 adds [population envelope 1](../../schemas/cellular-population.md), detached
|
||||
conversion candidates and an opt-in automatic policy (ADR-0140). The default
|
||||
remains disabled. The policy materializes whole genome groups within 1200 um of
|
||||
any nonsuspended controlled observer, collapses beyond 1800 um of all observers,
|
||||
and retains membership in the intervening band. Biology always advances through
|
||||
the same 50 ms kernel. Policy configuration, sequence, membership and complete
|
||||
biology restore together; loading a checkpoint does not reselect members.
|
||||
|
||||
`conversion_candidate` leaves the predecessor intact and returns the complete
|
||||
exact successor for fenced publication. Successful candidate creation and local
|
||||
checkpoint file writes are not durable production acknowledgements. A dedicated
|
||||
PostgreSQL profile/trace/publication adapter is still required; existing raw
|
||||
cellular profile 4 cannot contain this envelope. The separate-process test
|
||||
demonstrates identical continuation, not power-loss or database-failover safety.
|
||||
|
||||
Run `evo-cellular-cohorts --verify` for the CTest matrix or omit the option for
|
||||
8192 ticks per case. The executable compares complete canonical state every tick,
|
||||
|
||||
@@ -10,7 +10,7 @@ Commit and push each validated scoped segment separately.
|
||||
|---|---|---|
|
||||
| 12.1 | Engineering evidence policy and explicit deferrals documented | ADR-0133, identity/time contract and worked histories; no new game rules or runtime versions. |
|
||||
| 12.2 | Exact local model selected and paired qualification implemented (12.2d); production cost gate open | ADR-0139, exact canonical equality across 196,608 paired ticks per platform. Regional approximation disabled; no compression claim. Deployment-host capacity and fuller interaction coverage remain open. |
|
||||
| 12.3 | Exact-owner durable implementation authorized; production enabling depends on qualification | ADR-0139 resolves the averaging conflict for retained members. Durable conversion closure/publication remains to implement. |
|
||||
| 12.3 | Portable exact conversion closure, automatic policy and process recovery implemented | ADR-0140, population envelope 1. Dedicated PostgreSQL profile/trace/publication adapter and fault injection remain required before enabling the live selector. |
|
||||
| 12.4 | Blocked on 12.2-12.3 | Existing cellular worker pauses without sessions/lingering players; bounded unattended recovery and biological migration escrow remain unimplemented. |
|
||||
| 12.5 | Blocked on 12.2-12.4 | Finite M6 process fixtures do not qualify coupled persistent cellular/cohort atmosphere/water budgets. |
|
||||
|
||||
|
||||
@@ -1,5 +1,29 @@
|
||||
# Validation report
|
||||
|
||||
## 2026-10-17 - M12.3a exact population checkpoint closure
|
||||
|
||||
ADR-0140 adds population envelope 1, detached conversion candidates and opt-in
|
||||
automatic hysteresis to portable lifecycle orchestration. Existing raw cellular
|
||||
profile 4, database schemas, biology, goldens and live configuration are unchanged.
|
||||
|
||||
Executed: MSVC 19.44 Release configure/build and **74/74 CTests**; GCC 14.2
|
||||
Release build and cohort/transition/process-restart CTests; clang-tidy 19 analysis
|
||||
build, repository clang-format 19 and the two new CTests (3.29 s); ASan/UBSan
|
||||
transition/restart CTests (12.10 s), no reports. Explicit checks work under
|
||||
NDEBUG. Codec tests include sampled truncations, appended bytes, corrupt checksum,
|
||||
invalid version/flags/policy/identity/tick/length/count/member, controlled-member
|
||||
forgery and sequence exhaustion. One hundred conversion/restore oscillations
|
||||
leave all biological bytes unchanged. Automatic policy matches the active
|
||||
biological control across 513 offline ticks and 32 checkpoint restores.
|
||||
|
||||
The fresh-process test writes a cohort-bearing checkpoint after 250 offline
|
||||
ticks, exits, restores in a new executable process and advances 128 more ticks;
|
||||
the entire resulting population envelope equals uninterrupted continuation.
|
||||
It tests state closure, not fsync/power loss or a database publication fence.
|
||||
There is no new PostgreSQL profile/trace/publication adapter yet. Production
|
||||
crash-boundary, stale-writer and lost-reply acceptance remain open M12.3 work.
|
||||
Contract: [population envelope](../../schemas/cellular-population.md).
|
||||
|
||||
## 2026-10-17 - M12.2d exact cellular cohorts
|
||||
|
||||
ADR-0139 selects exact retained-member local cohorts under delegated M12.2
|
||||
|
||||
@@ -16,12 +16,29 @@ struct CellularCohortMember {
|
||||
std::uint64_t life{}, genome{};
|
||||
auto operator<=>(const CellularCohortMember&) const = default;
|
||||
};
|
||||
enum class CellularPopulationError : std::uint8_t { unsupported, absent, ineligible };
|
||||
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::size_t max_cellular_population_bytes = cellular_population_header_bytes +
|
||||
16 * biology::max_cellular_tick_lives +
|
||||
max_cellular_lives_bytes + 8;
|
||||
|
||||
class CellularPopulation {
|
||||
public:
|
||||
[[nodiscard]] static std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
|
||||
create(std::unique_ptr<CellularLives> state);
|
||||
create(std::unique_ptr<CellularLives> state, CellularTierPolicy policy = {});
|
||||
CellularPopulation(const CellularPopulation&) = delete;
|
||||
CellularPopulation& operator=(const CellularPopulation&) = delete;
|
||||
|
||||
@@ -29,6 +46,16 @@ class CellularPopulation {
|
||||
[[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::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.
|
||||
@@ -45,8 +72,11 @@ class CellularPopulation {
|
||||
std::span<const CellularMutationSelection> selections = {});
|
||||
|
||||
private:
|
||||
explicit CellularPopulation(std::unique_ptr<CellularLives> state);
|
||||
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
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
# Exact cellular population envelope 1
|
||||
|
||||
Authority: ADR-0139/0140. Little-endian integers, no padding. Size is bounded by
|
||||
`max_cellular_population_bytes`. No changes to existing cellular owner envelopes.
|
||||
|
||||
| Offset | Bytes | Field |
|
||||
|---:|---:|---|
|
||||
| 0 | 8 | Magic `AEVOPOP1` |
|
||||
| 8 | 4 | Version 1 |
|
||||
| 12 | 4 | Automatic policy flag, exactly 0 or 1 |
|
||||
| 16 | 4 | Materialization radius, micrometres, positive |
|
||||
| 20 | 4 | Collapse radius, strictly larger, at most 1,000,000 micrometres |
|
||||
| 24 | 8 | Conversion sequence |
|
||||
| 32 | 8 | World |
|
||||
| 40 | 8 | Region |
|
||||
| 48 | 8 | Frame |
|
||||
| 56 | 8 | Completed tick |
|
||||
| 64 | 8 | Nested biological payload bytes |
|
||||
| 72 | 8 | Cohort member count, at most 256 |
|
||||
| 80 | 16 each | Sorted unique member life ID and exact genome ID |
|
||||
| after members | declared length | Complete canonical rules-8 CellularLives snapshot |
|
||||
| final | 8 | FNV-1a64 over all preceding bytes |
|
||||
|
||||
Require exact total length, matching trusted identity/tick, valid policy, canonical
|
||||
nested restoration and complete byte-identical re-encoding. Every tagged member
|
||||
must exist, have zero account, active presence and exactly the recorded genome.
|
||||
The membership table contains no additional inventory. Untagged living cells are
|
||||
individuals. New offspring may be individual until the next applicable group
|
||||
conversion. No random seed or residual is missing: the representation is exact.
|
||||
|
||||
The checksum detects corruption, not malicious alteration. Production object
|
||||
publication must also bind SHA-256, predecessor, current fence and immutable
|
||||
manifest. This format is not yet an accepted PostgreSQL checkpoint profile; it
|
||||
must never be labeled as the existing cellular profile-4 payload.
|
||||
@@ -3,17 +3,23 @@
|
||||
#include "evo/server/lifecycle/wildlife_controller.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <set>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::server {
|
||||
CellularPopulation::CellularPopulation(std::unique_ptr<CellularLives> state)
|
||||
: state_(std::move(state)) {}
|
||||
CellularPopulation::CellularPopulation(std::unique_ptr<CellularLives> state,
|
||||
CellularTierPolicy policy)
|
||||
: state_(std::move(state)), policy_(policy) {}
|
||||
|
||||
std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
|
||||
CellularPopulation::create(std::unique_ptr<CellularLives> state) {
|
||||
if (!state || state->rules_version() != cellular_lives_foraging_rules)
|
||||
CellularPopulation::create(std::unique_ptr<CellularLives> state, CellularTierPolicy policy) {
|
||||
if (!state || state->rules_version() != cellular_lives_foraging_rules ||
|
||||
policy.materialize_radius_um == 0 ||
|
||||
policy.collapse_radius_um <= policy.materialize_radius_um ||
|
||||
policy.collapse_radius_um > biology::cellular_motion_axis_limit_um)
|
||||
return std::unexpected(CellularPopulationError::unsupported);
|
||||
return std::unique_ptr<CellularPopulation>(new CellularPopulation(std::move(state)));
|
||||
return std::unique_ptr<CellularPopulation>(new CellularPopulation(std::move(state), policy));
|
||||
}
|
||||
const CellularLives& CellularPopulation::state() const noexcept {
|
||||
return *state_;
|
||||
@@ -24,6 +30,12 @@ std::span<const CellularCohortMember> CellularPopulation::members() const noexce
|
||||
bool CellularPopulation::in_cohort(std::uint64_t life) const noexcept {
|
||||
return std::ranges::binary_search(members_, life, {}, &CellularCohortMember::life);
|
||||
}
|
||||
CellularTierPolicy CellularPopulation::policy() const noexcept {
|
||||
return policy_;
|
||||
}
|
||||
std::uint64_t CellularPopulation::conversion_sequence() const noexcept {
|
||||
return conversion_sequence_;
|
||||
}
|
||||
std::expected<std::size_t, CellularPopulationError>
|
||||
CellularPopulation::collapse(std::uint64_t genome) {
|
||||
auto candidate = members_;
|
||||
@@ -42,21 +54,30 @@ CellularPopulation::collapse(std::uint64_t genome) {
|
||||
}
|
||||
if (!found)
|
||||
return std::unexpected(CellularPopulationError::absent);
|
||||
if (converted > 0 && conversion_sequence_ == std::numeric_limits<std::uint64_t>::max())
|
||||
return std::unexpected(CellularPopulationError::overflow);
|
||||
std::ranges::sort(candidate);
|
||||
members_.swap(candidate);
|
||||
if (converted > 0)
|
||||
++conversion_sequence_;
|
||||
return converted;
|
||||
}
|
||||
std::expected<std::size_t, CellularPopulationError>
|
||||
CellularPopulation::materialize(std::uint64_t genome) {
|
||||
if (std::ranges::none_of(members_, [genome](const auto& m) { return m.genome == genome; }))
|
||||
return std::unexpected(CellularPopulationError::absent);
|
||||
if (conversion_sequence_ == std::numeric_limits<std::uint64_t>::max())
|
||||
return std::unexpected(CellularPopulationError::overflow);
|
||||
const auto removed =
|
||||
std::erase_if(members_, [genome](const auto& m) { return m.genome == genome; });
|
||||
if (removed == 0)
|
||||
return std::unexpected(CellularPopulationError::absent);
|
||||
++conversion_sequence_;
|
||||
return removed;
|
||||
}
|
||||
std::expected<CellularTickExecution, CellularLivesError>
|
||||
CellularPopulation::advance_tick(std::span<const CellularLifeIntent> controlled,
|
||||
std::span<const CellularMutationSelection> selections) {
|
||||
if (policy_.automatic && conversion_sequence_ == std::numeric_limits<std::uint64_t>::max())
|
||||
return std::unexpected(CellularLivesError::overflow);
|
||||
auto wildlife = wildlife_intents(*state_, state_->tick() + 1);
|
||||
if (!wildlife)
|
||||
return std::unexpected(wildlife.error());
|
||||
@@ -87,6 +108,8 @@ CellularPopulation::advance_tick(std::span<const CellularLifeIntent> controlled,
|
||||
return life.identity.life;
|
||||
}) == state_->lives().end();
|
||||
});
|
||||
if (policy_.automatic)
|
||||
apply_policy();
|
||||
return executed;
|
||||
}
|
||||
} // namespace evo::server
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
#include "evo/server/lifecycle/cellular_population.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::server {
|
||||
namespace {
|
||||
constexpr std::uint64_t magic = 0x31504F504F564541ULL; // "AEVOPOP1"
|
||||
std::uint64_t checksum(std::span<const std::uint8_t> bytes) {
|
||||
std::uint64_t hash = 14695981039346656037ULL;
|
||||
for (const auto byte : bytes)
|
||||
hash = (hash ^ byte) * 1099511628211ULL;
|
||||
return hash; // Corruption detection; trusted publication must also bind SHA-256.
|
||||
}
|
||||
void put(std::vector<std::uint8_t>& bytes, std::uint64_t value, unsigned width) {
|
||||
for (unsigned i = 0; i < width; ++i) {
|
||||
bytes.push_back(static_cast<std::uint8_t>(value & 255U));
|
||||
value >>= 8U;
|
||||
}
|
||||
}
|
||||
class Reader {
|
||||
public:
|
||||
explicit Reader(std::span<const std::uint8_t> bytes) : bytes_(bytes) {}
|
||||
std::uint64_t get(unsigned width) {
|
||||
std::uint64_t value = 0;
|
||||
for (unsigned i = 0; i < width; ++i)
|
||||
value |= std::uint64_t{bytes_[at_++]} << (8U * i);
|
||||
return value;
|
||||
}
|
||||
|
||||
private:
|
||||
std::span<const std::uint8_t> bytes_;
|
||||
std::size_t at_{};
|
||||
};
|
||||
} // namespace
|
||||
|
||||
std::vector<std::uint8_t> CellularPopulation::snapshot() const {
|
||||
const auto biology = state_->snapshot();
|
||||
std::vector<std::uint8_t> bytes;
|
||||
bytes.reserve(cellular_population_header_bytes + members_.size() * 16 + biology.size() + 8);
|
||||
put(bytes, magic, 8);
|
||||
put(bytes, 1, 4);
|
||||
put(bytes, policy_.automatic ? 1 : 0, 4);
|
||||
put(bytes, policy_.materialize_radius_um, 4);
|
||||
put(bytes, policy_.collapse_radius_um, 4);
|
||||
for (const auto value :
|
||||
{conversion_sequence_, state_->world(), state_->region(), state_->frame(), state_->tick(),
|
||||
static_cast<std::uint64_t>(biology.size()), static_cast<std::uint64_t>(members_.size())})
|
||||
put(bytes, value, 8);
|
||||
for (const auto& member : members_) {
|
||||
put(bytes, member.life, 8);
|
||||
put(bytes, member.genome, 8);
|
||||
}
|
||||
bytes.insert(bytes.end(), biology.begin(), biology.end());
|
||||
put(bytes, checksum(bytes), 8);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
|
||||
CellularPopulation::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
std::uint64_t region, std::uint64_t frame, std::uint64_t tick) {
|
||||
if (bytes.size() < cellular_population_header_bytes + 8 ||
|
||||
bytes.size() > max_cellular_population_bytes)
|
||||
return std::unexpected(CellularPopulationError::format);
|
||||
Reader tail(bytes.last(8));
|
||||
if (tail.get(8) != checksum(bytes.first(bytes.size() - 8)))
|
||||
return std::unexpected(CellularPopulationError::checksum);
|
||||
Reader reader(bytes);
|
||||
if (reader.get(8) != magic)
|
||||
return std::unexpected(CellularPopulationError::format);
|
||||
if (reader.get(4) != 1)
|
||||
return std::unexpected(CellularPopulationError::unsupported);
|
||||
const auto flags = reader.get(4);
|
||||
if (flags > 1)
|
||||
return std::unexpected(CellularPopulationError::format);
|
||||
CellularTierPolicy policy;
|
||||
policy.automatic = flags == 1;
|
||||
policy.materialize_radius_um = static_cast<std::uint32_t>(reader.get(4));
|
||||
policy.collapse_radius_um = static_cast<std::uint32_t>(reader.get(4));
|
||||
const auto sequence = reader.get(8);
|
||||
if (reader.get(8) != world || reader.get(8) != region || reader.get(8) != frame ||
|
||||
reader.get(8) != tick)
|
||||
return std::unexpected(CellularPopulationError::identity);
|
||||
const auto biological_bytes = reader.get(8), count = reader.get(8);
|
||||
if (count > biology::max_cellular_tick_lives || biological_bytes > max_cellular_lives_bytes ||
|
||||
biological_bytes == 0 ||
|
||||
bytes.size() != cellular_population_header_bytes + count * 16 + biological_bytes + 8)
|
||||
return std::unexpected(CellularPopulationError::format);
|
||||
std::vector<CellularCohortMember> members;
|
||||
members.reserve(static_cast<std::size_t>(count));
|
||||
for (std::uint64_t index = 0; index < count; ++index) {
|
||||
CellularCohortMember member{reader.get(8), reader.get(8)};
|
||||
if (member.life == 0 || member.genome == 0 ||
|
||||
(!members.empty() && members.back().life >= member.life))
|
||||
return std::unexpected(CellularPopulationError::format);
|
||||
members.push_back(member);
|
||||
}
|
||||
const auto offset = cellular_population_header_bytes + static_cast<std::size_t>(count) * 16;
|
||||
auto biological =
|
||||
CellularLives::restore(bytes.subspan(offset, static_cast<std::size_t>(biological_bytes)),
|
||||
world, region, frame, tick);
|
||||
if (!biological)
|
||||
return std::unexpected(CellularPopulationError::format);
|
||||
auto candidate = create(std::move(*biological), policy);
|
||||
if (!candidate)
|
||||
return std::unexpected(candidate.error());
|
||||
for (const auto& member : members) {
|
||||
const auto life = std::ranges::find((*candidate)->state().lives(), member.life,
|
||||
[](const auto& cell) { return cell.identity.life; });
|
||||
if (life == (*candidate)->state().lives().end() || life->account != LifeAccount{} ||
|
||||
life->presence != CellularPresence::active || life->identity.genome != member.genome)
|
||||
return std::unexpected(CellularPopulationError::ineligible);
|
||||
}
|
||||
(*candidate)->members_ = std::move(members);
|
||||
(*candidate)->conversion_sequence_ = sequence;
|
||||
if (!std::ranges::equal((*candidate)->snapshot(), bytes))
|
||||
return std::unexpected(CellularPopulationError::format);
|
||||
return candidate;
|
||||
}
|
||||
|
||||
std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
|
||||
CellularPopulation::conversion_candidate(std::uint64_t genome, bool to_cohort) const {
|
||||
auto candidate =
|
||||
restore(snapshot(), state_->world(), state_->region(), state_->frame(), state_->tick());
|
||||
if (!candidate)
|
||||
return std::unexpected(candidate.error());
|
||||
const auto converted =
|
||||
to_cohort ? (*candidate)->collapse(genome) : (*candidate)->materialize(genome);
|
||||
if (!converted)
|
||||
return std::unexpected(converted.error());
|
||||
return candidate;
|
||||
}
|
||||
} // namespace evo::server
|
||||
@@ -0,0 +1,51 @@
|
||||
#include "evo/server/lifecycle/cellular_population.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <set>
|
||||
|
||||
namespace evo::server {
|
||||
void CellularPopulation::apply_policy() {
|
||||
auto candidate = members_;
|
||||
std::set<std::uint64_t> genomes;
|
||||
for (const auto& life : state_->lives())
|
||||
if (life.account == LifeAccount{})
|
||||
genomes.insert(life.identity.genome);
|
||||
const auto inner = std::uint64_t{policy_.materialize_radius_um} * policy_.materialize_radius_um;
|
||||
const auto outer = std::uint64_t{policy_.collapse_radius_um} * policy_.collapse_radius_um;
|
||||
for (const auto genome : genomes) {
|
||||
bool near = false, far = true, eligible = true;
|
||||
for (const auto& life : state_->lives()) {
|
||||
if (life.account != LifeAccount{} || life.identity.genome != genome)
|
||||
continue;
|
||||
eligible = eligible && life.presence == CellularPresence::active;
|
||||
for (const auto& observer : state_->lives()) {
|
||||
if (observer.account == LifeAccount{} ||
|
||||
observer.presence == CellularPresence::suspended)
|
||||
continue;
|
||||
const auto dx = life.position.x_um > observer.position.x_um
|
||||
? life.position.x_um - observer.position.x_um
|
||||
: observer.position.x_um - life.position.x_um;
|
||||
const auto dy = life.position.y_um > observer.position.y_um
|
||||
? life.position.y_um - observer.position.y_um
|
||||
: observer.position.y_um - life.position.y_um;
|
||||
const auto distance = dx * dx + dy * dy; // Coordinates bounded to 1e6 um.
|
||||
near = near || distance <= inner;
|
||||
far = far && distance > outer;
|
||||
}
|
||||
}
|
||||
if (near || !eligible) {
|
||||
std::erase_if(candidate, [genome](const auto& m) { return m.genome == genome; });
|
||||
} else if (far) {
|
||||
std::erase_if(candidate, [genome](const auto& m) { return m.genome == genome; });
|
||||
for (const auto& life : state_->lives())
|
||||
if (life.account == LifeAccount{} && life.identity.genome == genome)
|
||||
candidate.push_back({life.identity.life, genome});
|
||||
}
|
||||
}
|
||||
std::ranges::sort(candidate);
|
||||
if (candidate != members_) {
|
||||
members_.swap(candidate);
|
||||
++conversion_sequence_;
|
||||
}
|
||||
}
|
||||
} // namespace evo::server
|
||||
@@ -0,0 +1,16 @@
|
||||
file(MAKE_DIRECTORY "${TEST_DIR}")
|
||||
execute_process(COMMAND "${POPULATION_TEST}" --checkpoint "${TEST_DIR}/checkpoint.pop"
|
||||
"${TEST_DIR}/expected.pop" RESULT_VARIABLE prepared)
|
||||
if(NOT prepared EQUAL 0)
|
||||
message(FATAL_ERROR "Population checkpoint preparation failed: ${prepared}")
|
||||
endif()
|
||||
execute_process(COMMAND "${POPULATION_TEST}" --resume "${TEST_DIR}/checkpoint.pop"
|
||||
"${TEST_DIR}/resumed.pop" RESULT_VARIABLE resumed)
|
||||
if(NOT resumed EQUAL 0)
|
||||
message(FATAL_ERROR "Fresh-process population recovery failed: ${resumed}")
|
||||
endif()
|
||||
execute_process(COMMAND "${CMAKE_COMMAND}" -E compare_files "${TEST_DIR}/expected.pop"
|
||||
"${TEST_DIR}/resumed.pop" RESULT_VARIABLE compared)
|
||||
if(NOT compared EQUAL 0)
|
||||
message(FATAL_ERROR "Population state diverged across process recovery")
|
||||
endif()
|
||||
@@ -0,0 +1,210 @@
|
||||
#include "evo/server/lifecycle/cellular_population.hpp"
|
||||
#include "fixtures/cellular_ecology.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
|
||||
namespace {
|
||||
using namespace evo;
|
||||
using namespace evo::server;
|
||||
using namespace evo::fixture;
|
||||
using Population = std::unique_ptr<CellularPopulation>;
|
||||
|
||||
Owner descendants() {
|
||||
auto state = opening(1, true);
|
||||
std::set<std::uint64_t> mutated;
|
||||
for (unsigned step = 0; step < 8192; ++step) {
|
||||
const auto input = inputs(*state, 1, mutated);
|
||||
const auto executed =
|
||||
checked(state->advance_tick_with_mutations(input.lives, input.mutations),
|
||||
"descendant fixture tick");
|
||||
for (const auto& mutation : executed.mutations)
|
||||
if (mutation.result == CellularMutationResult::applied)
|
||||
mutated.insert(mutation.life_id);
|
||||
}
|
||||
require(std::ranges::any_of(state->lives(),
|
||||
[](const auto& life) { return life.account == LifeAccount{}; }),
|
||||
"fixture has no funded descendant");
|
||||
return state;
|
||||
}
|
||||
Population restored(std::span<const std::uint8_t> bytes, const CellularLives& state) {
|
||||
return checked(CellularPopulation::restore(bytes, state.world(), state.region(), state.frame(),
|
||||
state.tick()),
|
||||
"population restore");
|
||||
}
|
||||
std::uint64_t wildlife_genome(const CellularPopulation& population) {
|
||||
for (const auto& life : population.state().lives())
|
||||
if (life.account == LifeAccount{})
|
||||
return life.identity.genome;
|
||||
throw std::runtime_error("missing wildlife genome");
|
||||
}
|
||||
void frozen_tick(CellularPopulation& population) {
|
||||
std::vector<CellularLifeIntent> controlled;
|
||||
for (const auto& life : population.state().lives())
|
||||
if (life.account != LifeAccount{}) {
|
||||
CellularLifeIntent input{life.identity.life, {}};
|
||||
input.intent.frozen = true;
|
||||
controlled.push_back(input);
|
||||
}
|
||||
checked(population.advance_tick(controlled), "offline population tick");
|
||||
}
|
||||
void put(std::vector<std::uint8_t>& bytes, std::size_t at, std::uint64_t value,
|
||||
unsigned width = 8) {
|
||||
for (unsigned i = 0; i < width; ++i) {
|
||||
bytes[at++] = static_cast<std::uint8_t>(value & 255U);
|
||||
value >>= 8U;
|
||||
}
|
||||
}
|
||||
void refresh(std::vector<std::uint8_t>& bytes) {
|
||||
std::uint64_t hash = 14695981039346656037ULL;
|
||||
for (const auto byte : std::span{bytes}.first(bytes.size() - 8))
|
||||
hash = (hash ^ byte) * 1099511628211ULL;
|
||||
put(bytes, bytes.size() - 8, hash);
|
||||
}
|
||||
void conversions_and_codec() {
|
||||
auto population = checked(CellularPopulation::create(descendants()), "population create");
|
||||
const auto genome = wildlife_genome(*population);
|
||||
const auto before = population->snapshot();
|
||||
const auto biology = population->state().snapshot();
|
||||
auto pending = checked(population->conversion_candidate(genome, true), "collapse candidate");
|
||||
require(population->snapshot() == before && population->members().empty(),
|
||||
"planning consumed predecessor");
|
||||
require(pending->state().snapshot() == biology && !pending->members().empty() &&
|
||||
pending->conversion_sequence() == 1,
|
||||
"candidate changed biology");
|
||||
// Recovery selects a trusted old/new complete checkpoint; it never rerolls members.
|
||||
require(restored(before, population->state())->snapshot() == before, "old checkpoint recovery");
|
||||
auto committed = restored(pending->snapshot(), pending->state());
|
||||
require(committed->snapshot() == pending->snapshot(), "committed sample recovery");
|
||||
auto active = checked(committed->conversion_candidate(genome, false), "materialize candidate");
|
||||
require(active->state().snapshot() == biology && active->members().empty() &&
|
||||
active->conversion_sequence() == 2,
|
||||
"materialization reset biology");
|
||||
require(checked(committed->collapse(genome), "idempotent collapse") == 0 &&
|
||||
committed->conversion_sequence() == 1,
|
||||
"duplicate conversion advanced sequence");
|
||||
for (unsigned repeat = 0; repeat < 100; ++repeat) {
|
||||
checked(committed->materialize(genome), "oscillation materialize");
|
||||
checked(committed->collapse(genome), "oscillation collapse");
|
||||
committed = restored(committed->snapshot(), committed->state());
|
||||
require(committed->state().snapshot() == biology,
|
||||
"conversion healed or duplicated biology");
|
||||
}
|
||||
const auto valid = committed->snapshot();
|
||||
const auto& state = committed->state();
|
||||
const auto refuses = [&](const auto& bytes) {
|
||||
require(!CellularPopulation::restore(bytes, state.world(), state.region(), state.frame(),
|
||||
state.tick()),
|
||||
"malformed population admitted");
|
||||
};
|
||||
for (std::size_t size = 0; size < valid.size(); size += 97)
|
||||
refuses(std::span{valid}.first(size));
|
||||
auto changed = valid;
|
||||
changed.back() ^= 1;
|
||||
refuses(changed);
|
||||
changed = valid;
|
||||
changed.push_back(0);
|
||||
refuses(changed);
|
||||
for (const auto offset : {8U, 12U, 16U, 20U, 32U, 40U, 48U, 56U, 64U, 72U, 80U, 88U}) {
|
||||
changed = valid;
|
||||
put(changed, offset, offset == 12 ? 2 : 0, offset < 24 ? 4 : 8);
|
||||
refresh(changed);
|
||||
refuses(changed);
|
||||
}
|
||||
changed = valid;
|
||||
put(changed, 80, 100); // An actual controlled life cannot be reclassified as wildlife.
|
||||
refresh(changed);
|
||||
refuses(changed);
|
||||
changed = valid;
|
||||
put(changed, 24, std::numeric_limits<std::uint64_t>::max());
|
||||
refresh(changed);
|
||||
auto exhausted = restored(changed, state);
|
||||
const auto exhausted_bytes = exhausted->snapshot();
|
||||
require(!exhausted->materialize(genome) && exhausted->snapshot() == exhausted_bytes,
|
||||
"exhausted conversion sequence mutated state");
|
||||
require(!CellularPopulation::create(restore(state), {true, 1800, 1200}),
|
||||
"invalid hysteresis admitted");
|
||||
}
|
||||
|
||||
void automatic_and_restart() {
|
||||
auto initial = descendants();
|
||||
auto policy = checked(CellularPopulation::create(restore(*initial), {true, 1200, 1800}),
|
||||
"automatic owner");
|
||||
auto exact = checked(CellularPopulation::create(restore(*initial)), "individual control");
|
||||
const auto genome = wildlife_genome(*policy);
|
||||
checked(policy->collapse(genome), "force near cohort");
|
||||
frozen_tick(*policy);
|
||||
frozen_tick(*exact);
|
||||
require(policy->members().empty(), "near controlled observer failed to materialize");
|
||||
std::uint64_t cohort_ticks = 0;
|
||||
for (unsigned tick = 0; tick < 512; ++tick) {
|
||||
frozen_tick(*policy);
|
||||
frozen_tick(*exact);
|
||||
require(policy->state().snapshot() == exact->state().snapshot(),
|
||||
"automatic policy changed biology");
|
||||
cohort_ticks += policy->members().size();
|
||||
if (tick % 16 == 0) {
|
||||
const auto bytes = policy->snapshot();
|
||||
policy = restored(bytes, policy->state());
|
||||
require(policy->snapshot() == bytes && policy->policy().automatic,
|
||||
"restart lost automatic policy");
|
||||
}
|
||||
}
|
||||
require(cohort_ticks > 0, "suspended observers prevented unattended cohort collapse");
|
||||
}
|
||||
void write(const char* path, const CellularPopulation& population) {
|
||||
const auto bytes = population.snapshot();
|
||||
std::ofstream stream(path, std::ios::binary | std::ios::trunc);
|
||||
stream.write(reinterpret_cast<const char*>(bytes.data()),
|
||||
static_cast<std::streamsize>(bytes.size()));
|
||||
stream.close();
|
||||
require(!stream.fail(), "test checkpoint write failed");
|
||||
}
|
||||
void checkpoint(const char* path, const char* expected) {
|
||||
auto population = checked(CellularPopulation::create(descendants(), {true, 1200, 1800}),
|
||||
"process checkpoint owner");
|
||||
for (unsigned tick = 0; tick < 250; ++tick)
|
||||
frozen_tick(*population);
|
||||
require(!population->members().empty(), "process checkpoint has no cohort");
|
||||
write(path, *population);
|
||||
for (unsigned tick = 0; tick < 128; ++tick)
|
||||
frozen_tick(*population);
|
||||
write(expected, *population);
|
||||
}
|
||||
void resume(const char* path, const char* output) {
|
||||
std::ifstream stream(path, std::ios::binary | std::ios::ate);
|
||||
require(stream.good() && stream.tellg() > 0 &&
|
||||
stream.tellg() <= static_cast<std::streamoff>(max_cellular_population_bytes),
|
||||
"test checkpoint read bound");
|
||||
const auto size = static_cast<std::size_t>(stream.tellg());
|
||||
stream.seekg(0);
|
||||
std::vector<std::uint8_t> bytes(size);
|
||||
stream.read(reinterpret_cast<char*>(bytes.data()), static_cast<std::streamsize>(size));
|
||||
require(stream.good(), "test checkpoint read failed");
|
||||
auto population =
|
||||
checked(CellularPopulation::restore(bytes, 201, 1, 1, 8443), "fresh process restore");
|
||||
for (unsigned tick = 0; tick < 128; ++tick)
|
||||
frozen_tick(*population);
|
||||
write(output, *population);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) try {
|
||||
if (argc == 4 && std::string_view{argv[1]} == "--checkpoint") {
|
||||
checkpoint(argv[2], argv[3]);
|
||||
return 0;
|
||||
}
|
||||
if (argc == 4 && std::string_view{argv[1]} == "--resume") {
|
||||
resume(argv[2], argv[3]);
|
||||
return 0;
|
||||
}
|
||||
if (argc != 1)
|
||||
return 2;
|
||||
conversions_and_codec();
|
||||
automatic_and_restart();
|
||||
return 0;
|
||||
} catch (const std::exception& error) {
|
||||
std::fprintf(stderr, "%s\n", error.what());
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user