M12.4: preserve exact biology across ownership candidates
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m18s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 5m33s
Foundation / checks (postgres) (push) Canceled after 5m29s
Foundation / checks (tsan) (push) Canceled after 5m28s
Foundation / checks (client) (push) Canceled after 5m37s
Foundation / checks (network) (push) Canceled after 5m39s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m18s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 5m33s
Foundation / checks (postgres) (push) Canceled after 5m29s
Foundation / checks (tsan) (push) Canceled after 5m28s
Foundation / checks (client) (push) Canceled after 5m37s
Foundation / checks (network) (push) Canceled after 5m39s
This commit is contained in:
@@ -87,6 +87,7 @@ evo_owned_target(evo_biology)
|
||||
# M11.2 portable lifecycle orchestration; no dependency from the foundation proof.
|
||||
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_migration.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_population_codec.cpp src/server/lifecycle/cellular_population_policy.cpp
|
||||
@@ -680,6 +681,11 @@ if(BUILD_TESTING)
|
||||
target_link_libraries(evo-cellular-schedule-tests PRIVATE evo::lifecycle)
|
||||
evo_owned_target(evo-cellular-schedule-tests)
|
||||
add_test(NAME cellular-unattended-schedule COMMAND evo-cellular-schedule-tests)
|
||||
add_executable(evo-cellular-migration-tests tests/cellular_migration_tests.cpp)
|
||||
target_link_libraries(evo-cellular-migration-tests PRIVATE evo::lifecycle)
|
||||
evo_owned_target(evo-cellular-migration-tests)
|
||||
add_test(NAME cellular-migration-contract COMMAND evo-cellular-migration-tests)
|
||||
set_tests_properties(cellular-migration-contract PROPERTIES TIMEOUT 180)
|
||||
add_executable(evo-population-checkpoint-tests tests/population_checkpoint_tests.cpp)
|
||||
target_link_libraries(evo-population-checkpoint-tests PRIVATE evo::region_checkpoint)
|
||||
evo_owned_target(evo-population-checkpoint-tests)
|
||||
|
||||
@@ -139,7 +139,10 @@ worker/session path. Opt-in exact population profile 5 is described in
|
||||
[owned intervals](docs/engineering/M12_OWNED_POPULATION_INTERVALS.md) survive
|
||||
replacement owners; biological migration, environmental coupling and production
|
||||
qualification remain in progress. The
|
||||
microscope client is the current M11 playtest path. `evo::simulation` remains
|
||||
portable [biological transfer candidates](docs/engineering/M12_EXACT_BIOLOGICAL_TRANSFER.md)
|
||||
preserve exact identity/history and regional boundary budgets; their durable
|
||||
escrow is still required. The microscope client is the current M11 playtest path.
|
||||
`evo::simulation` remains
|
||||
graphics-, network- and database-independent.
|
||||
|
||||
World0 is persistent state. No normal configure, build, test or CI job may create,
|
||||
|
||||
@@ -1,5 +1,21 @@
|
||||
# Project status and next steps
|
||||
|
||||
## M12.4c portable biological transfer - 2026-10-17
|
||||
|
||||
ADR-0144 adds opt-in owner format 7 and population envelope 2 with exact paired
|
||||
wildlife transfer candidates, explicit regional boundary flows, retained
|
||||
nonresident history and complete ancestry/revision merging. Cohort tags move with
|
||||
their cells; opening budgets, identity, age and biological state remain exact.
|
||||
Imported higher-namespace residents coexist with locally born lower-namespace
|
||||
buds. Old durable manifest tuples explicitly refuse the new portable envelopes.
|
||||
|
||||
Native Release passes 77/77; Clang analysis/format, ASan/UBSan and the focused
|
||||
private PostgreSQL population suite pass. [Scope and evidence](engineering/M12_EXACT_BIOLOGICAL_TRANSFER.md)
|
||||
retain the limits: paired durable escrow/activation, linked geometry/environment
|
||||
and multi-generation ecology remain open. Testing exposed rules 8's existing
|
||||
subadult-bud maturation gap; no growth or reproduction rule was changed to hide
|
||||
it. M12.4 and all of M12 remain in progress; no deployment or live entry changed.
|
||||
|
||||
## M12.4b owned population intervals - 2026-10-17
|
||||
|
||||
ADR-0143/schema 45 records one bounded interval before computation and settles it
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
# ADR-0144: Exact wildlife ownership transfer and boundary accounting
|
||||
|
||||
## Status
|
||||
|
||||
Accepted for a scoped M12.4 engineering primitive, 2026-10-17. Portable candidates
|
||||
are implemented and locally qualified; durable escrow remains open. This accepts exact ownership/accounting support,
|
||||
not automatic migration, a linked world atlas or production enablement. Existing
|
||||
rules-8 arenas and every retained format remain unchanged by default.
|
||||
|
||||
## Context and conflict
|
||||
|
||||
The current owner deliberately validates a closed regional budget, requires every
|
||||
archived organism to be a local life/corpse, and admits bud identities only below
|
||||
its own region's counter. Removing a migrant would violate those contracts;
|
||||
importing one would also violate the destination's namespace and funding closure.
|
||||
Recording departure as death, renumbering the organism, rebasing the opening stock
|
||||
or copying a probe inventory would lose biological identity or conservation.
|
||||
|
||||
Resolve this boundary before expanding transfer code. Add a separately versioned
|
||||
owner representation with explicit boundary flows and nonresident history. The
|
||||
unchanged general biological kernel continues to operate on resident cells only.
|
||||
|
||||
## Decision and scope
|
||||
|
||||
Introduce cellular owner format 7 for rules 8, and population envelope 2 containing
|
||||
it. Upgrading from format 6 is explicit and no-tick: original opening stock,
|
||||
nursery, habitat, heat, bodies, reserves, identities, archives, positions and timers
|
||||
are preserved; inbound/outbound flows are zero and nonresident history is empty.
|
||||
Retain format 6/envelope 1 readers and old biological traces. Do not replace goldens.
|
||||
|
||||
The new conservation equation is channel-wise `opening + inbound = current +
|
||||
outbound`, with checked arithmetic. `current` still counts only actual owned
|
||||
compartments, including heat. Boundary flows are immutable cumulative accounting
|
||||
evidence, never spendable resource accounts or an extra copy of biomass. Refuse
|
||||
overflow/capacity exhaustion; no clamping, debt forgiveness or automatic reset.
|
||||
|
||||
A portable transfer prepares two detached candidates from exact compatible cuts:
|
||||
same world, frame, fixed tick, rules, dimensions and ambient convention; distinct
|
||||
regions. Move a whole eligible zero-account wildlife genome group. Controlled
|
||||
cells remain excluded and fully suspended during unattended transfer work. Bodies,
|
||||
reserves, growth/damage, ancestry, parent-life IDs and coordinates are identical.
|
||||
No biological tick, movement, mutation, sampling, feeding or healing occurs.
|
||||
Destination bounds, conflicts and capacity must all validate before either result
|
||||
is usable. A rejection leaves both inputs unchanged.
|
||||
|
||||
Source history retains every departed organism as nonresident, alive at its known
|
||||
cut; departure is not death. Destination imports the selected organisms' exact
|
||||
birth/revision facts and complete genome-parent closure. Returning an already
|
||||
recorded dead organism is refused. Imported bud IDs retain their original region
|
||||
namespace; only locally created buds consume the destination's own counter.
|
||||
Nonresident archive facts confer no residency, stock or activity. Never use an
|
||||
archive's known-alive count as an owned regional census or terminal world evidence.
|
||||
|
||||
The durable follow-on must freeze preparation under current authority, stage
|
||||
read-only destination evidence, and commit both exact source/destination heads in
|
||||
one fenced transaction. A failed second publication rolls back the first. Retries
|
||||
retain the same prepared pair. Before commit, the frozen source owns the stock;
|
||||
the destination ghost owns none. After commit, only the destination owns it. Abort
|
||||
is durable and only before commit; timeout alone never releases another copy.
|
||||
Retain source cuts, capsules, receipts and both histories.
|
||||
The caller's stable workflow request UUID belongs to that durable I/O journal;
|
||||
it is not a biological identity or a kernel PRNG decision. Source/destination head
|
||||
comparison prevents a second application under a different request UUID.
|
||||
|
||||
This primitive changes ownership at an explicitly compatible cut and preserves
|
||||
coordinates. It does not choose a physical route or let current clamped movement
|
||||
cross an arena edge. Automatic linked-habitat movement, boundary contacts and
|
||||
environmental-resource mappings need their own versioned contract and fidelity
|
||||
tests; they cannot be inferred from passing escrow tests. No worker flag enables
|
||||
those capabilities as part of this primitive.
|
||||
|
||||
## Alternatives, risks and qualification
|
||||
|
||||
Changing the old envelope silently breaks retained recovery. Marking departures
|
||||
dead violates terminal death. Resetting opening inventories conceals transfers.
|
||||
Generic mutable setters or unbacked destination births allow duplication. Retain
|
||||
explicit paired candidates and boundary evidence instead.
|
||||
|
||||
Require complete per-member equality, per-channel source/destination balance,
|
||||
archive retention/parent closure, foreign-identity admission, return trips without
|
||||
resurrection, overlap/capacity/overflow rejection, encoded restore and continued
|
||||
biology after import. Durable qualification additionally needs stale owners,
|
||||
duplicate/conflicting delivery, partition/abort/restart and kills at each boundary.
|
||||
This is an engineering transfer mechanism, not proof of coupled regional ecology,
|
||||
world extinction, actual multi-day stability or useful compression.
|
||||
@@ -161,3 +161,5 @@ design. No unadopted candidate is a link dependency of the proof.
|
||||
- [ADR-0140: Exact population checkpoint and policy](0140-exact-population-checkpoint-and-policy.md)
|
||||
|
||||
- [ADR-0141: Cellular unattended demand and pacing](0141-cellular-unattended-demand-and-pacing.md)
|
||||
|
||||
- [ADR-0144: Exact wildlife transfer accounting](0144-exact-wildlife-transfer-accounting.md)
|
||||
|
||||
@@ -49,6 +49,13 @@ readiness. New work requires fully suspended controlled cells, leaving vulnerabl
|
||||
linger/death on profile 4. Earlier trace semantics remain readable. See the
|
||||
[interval contract](../../schemas/ecology/population-interval-v1.md).
|
||||
|
||||
ADR-0144 adds a portable exact transfer pair with explicit regional boundary
|
||||
flows/nonresident history (owner 7, population envelope 2). These versions have
|
||||
no admitted durable manifest bundle yet. Existing profile-4/5 publishers retain
|
||||
their tuples; population trace 1 refuses envelope 2. A future adapter must freeze
|
||||
the source and atomically commit both fenced heads before destination activation.
|
||||
See [scope and remaining proof](../engineering/M12_EXACT_BIOLOGICAL_TRANSFER.md).
|
||||
|
||||
World/region/frame are nonzero durable IDs. No ID is a Pod name. The host records
|
||||
its unique holder ID and monotonically increasing nonzero lease epoch. Exhaustion
|
||||
of IDs, epochs, commit sequences or ticks fails closed; never wrap/reuse them.
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
# M12.4c exact biological transfer candidates
|
||||
|
||||
ADR-0144 resolves the closed-owner/accounting conflict before implementing
|
||||
biological ownership transfer. The portable lifecycle layer now prepares a
|
||||
detached source/destination pair at an exact compatible cut. This segment adds
|
||||
owner format 7 and population envelope 2, without a live migration flag, database
|
||||
schema, durable manifest bundle, dependency or deployment.
|
||||
|
||||
The pair transfers actual zero-account wildlife, including every member of the
|
||||
selected genome group. Opening stocks remain immutable; explicit cumulative
|
||||
incoming/outgoing flows explain each owner's current stock. Bodies, reserves,
|
||||
age, growth/damage, ancestry, identity, coordinates and cohort tags are exact.
|
||||
Departures retain nonresident archive facts instead of recording death. Complete
|
||||
genome ancestry and organism revisions merge without erasing destination history,
|
||||
rewinding revisions or resurrecting dead organisms. Archive known-alive counts
|
||||
include nonresidents and must never be used as an owned census/extinction proof.
|
||||
|
||||
The primitive refuses incompatible cuts/worlds/frames/geometry, vulnerable
|
||||
controlled cells, identity conflicts, missing groups, placement conflicts,
|
||||
capacity exhaustion, archive conflicts and counter overflow. Both detached
|
||||
candidates must validate before success; failures leave the two originals exact.
|
||||
Local offspring use the destination's identity namespace, with sorted insertion
|
||||
even below imported IDs. Existing regional twin randomness retains its meaning;
|
||||
the migration cut does not promise identical future environments or outcomes.
|
||||
|
||||
The tests use real funded founders and real budding, not fabricated wildlife or
|
||||
the non-biological probe. They exercise repeated no-tick return trips, exact
|
||||
per-member equality and per-channel combined closure, canonical restore, imported
|
||||
death/decay, lower-namespace local reproduction among imported residents,
|
||||
whole-group capacity refusal, body
|
||||
conflicts, malformed/truncated ledgers, overflowing cumulative counters and cohort
|
||||
tag/policy/sequence preservation. Lineage tests include revision conflicts and a
|
||||
maximum-depth 4,096-genome ancestry using an iterative traversal.
|
||||
|
||||
The first reproduction fixture exposed an existing rules-8 limitation: a bud is
|
||||
created below full realization, growth is retained, and `fund_bud` requires full
|
||||
realization. Additional food does not mature that bud under the current kernel.
|
||||
This transfer segment does not invent growth or alter reproduction rules. Its
|
||||
ordering regression uses a real fully grown local founder reproducing alongside
|
||||
imported higher-namespace residents. Self-sustaining wildlife generations still
|
||||
need an explicit versioned maturation contract and ecological qualification.
|
||||
|
||||
This is not the durable escrow gate. No prepared candidate may become an active
|
||||
destination owner before a future fenced transaction commits both heads and
|
||||
immutable transfer evidence. The source remains the sole owner until that commit;
|
||||
a staged ghost owns nothing. Durable preparation/freeze, two-owner fencing,
|
||||
ambiguous outcomes, abort/restart, partition/duplicate delivery, process kills and
|
||||
stock-in-transit audits remain required. Existing profile-4/5 publishers do not
|
||||
admit this new owner tuple, and retained trace 1 explicitly refuses envelope 2.
|
||||
|
||||
Numeric coordinates are region/frame-local and current movement is clamped,
|
||||
without wrap. No atlas, route, edge crossing, cross-region contact or linked
|
||||
habitat field is inferred. Those need separately versioned contracts and fidelity
|
||||
tests. M12.4 as a whole remains open, as do dependent M12.5–M12.9 gates.
|
||||
|
||||
Executed qualification is recorded in [VALIDATION](VALIDATION.md).
|
||||
@@ -25,7 +25,7 @@ or authorize production enablement.
|
||||
|---|---|---|
|
||||
| 12.2 | Exact cellular model and paired matrix; local samples through 256 lives | Deployment-host tail costs including publication; larger wildlife cohorts, predation/twins and 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 | Current-worker demand/debt plus owned bounded population intervals and restart backlog recovery | Exact biological source/destination escrow, partition/duplicate settlement, linked-boundary contracts and stock-in-transit audits. |
|
||||
| 12.4 | Current-worker demand/debt, owned bounded intervals/restart recovery, and portable exact biological transfer candidates with boundary accounting | Durable biological source/destination escrow, partition/duplicate settlement, linked-boundary contracts and stock-in-transit audits. |
|
||||
| 12.5 | Finite cellular contact feeding/decay and separate M6 producer/consumer resource owner | One jointly owned cellular/environment budget, explicit effective depth and source entitlements, versioned water/atmosphere species mapping and producer coefficients, plus closed/open and nutrient-exhaustion comparisons. |
|
||||
| 12.6 | Retained genome/organism archive with ancestry and common-ancestor queries | Append-only population census/history events in the same durable commit, bounded world history queries/indexing and client presentation; no account power or invented precise statistical ancestry. |
|
||||
| 12.7 | ADR-0133's viability/unknown semantics | Durable region-set/watermark reconciliation covering every owned representation, suspended/viable stock and migration escrow. Missing/stale regions must prevent terminal extinction. |
|
||||
@@ -44,6 +44,14 @@ boundary and documenting the resolution. That does not prevent the independent
|
||||
worker scheduling correction or the dedicated exact population adapter above.
|
||||
Biological migration escrow is the next M12.4 boundary; none of the rows above is
|
||||
closed by adding a placeholder service or relabeling the non-biological probe.
|
||||
ADR-0144's [portable transfer candidates](M12_EXACT_BIOLOGICAL_TRANSFER.md) resolve
|
||||
the closed-owner/foreign-identity conflict with explicit flows and nonresident
|
||||
history. They do not yet supply durable paired head publication or activation.
|
||||
The transfer tests also expose the current rules-8 maturation gap: buds retain
|
||||
their subadult realization while reproduction requires a fully realized parent.
|
||||
Finite extra food alone does not close repeated wildlife generations. A versioned
|
||||
growth contract and real multi-generation evidence remain necessary; transfer
|
||||
tests must not fabricate adult wildlife or silently change the rules to pass.
|
||||
|
||||
Current expansion readiness: **no-go for M13 integration or World0 promotion**
|
||||
while these M12 integration/evidence gates remain open. This records the present
|
||||
|
||||
@@ -1,5 +1,52 @@
|
||||
# Validation report
|
||||
|
||||
## 2026-10-17 - M12.4c portable exact biological transfer
|
||||
|
||||
ADR-0144 affects portable lifecycle ownership/cohort serialization, lineage
|
||||
merging, durable payload-version validation and related tests/documentation.
|
||||
Owner format 7/population envelope 2 are explicit opt-in portable formats; old
|
||||
readers, biological rules/goldens and live worker defaults retain their meaning.
|
||||
No database schema, new dependency, deployment or World0 operation.
|
||||
|
||||
Executed locally:
|
||||
|
||||
- MSVC 19.44 Release configure/build: **77/77 CTests passed**, final 11.99 s.
|
||||
- Clang 19 analysis build with clang-tidy: ownership, migration, lineage, cohort
|
||||
transitions/restart and checkpoint subset **8/8 passed**, 27.74 s. Rebuilt
|
||||
reference/cohort drivers separately pass **2/2**, 24.08 s. Final manifest-version
|
||||
guard and its tests pass their follow-up analysis check, **1/1**, 4.59 s.
|
||||
Repository clang-format 19 check passes after the final source edits.
|
||||
- Clang 19 ASan/UBSan: the same eight-contract subset **8/8 passed**, 94.13 s,
|
||||
no reports. Rebuilt reference/cohort drivers pass **2/2**, 82.29 s. Final
|
||||
manifest/envelope admission guard passes **1/1**, 15.95 s, with no reports.
|
||||
- Actual GCC 14 QUIC/PostgreSQL server and integration-test build. Final fresh
|
||||
private `postgres-check.sh --population-only` **passed, exit 0, 6 s**. A separate
|
||||
GCC portable migration contract passes (1.60 s). No live database was accessed.
|
||||
|
||||
Real funded founders produce the transferred wildlife. Tests retain exact
|
||||
members, per-channel combined stock and opening budgets over 101 transfers and
|
||||
canonical restores, with continued biology and imported death/decay. They reject
|
||||
capacity pressure without culling, destination body conflicts, incompatible cuts,
|
||||
vulnerable controlled cells, malformed/truncated metadata, cross-channel debt and
|
||||
cumulative-flow overflow. Cohort tags/policy/sequence survive upgrade and paired
|
||||
movement. Lineage tests cover terminal death/conflicts/revision prefixes and
|
||||
iterative import of the maximum-depth 4,096-genome chain. Native tests use runtime
|
||||
checks under NDEBUG; no golden was replaced.
|
||||
|
||||
The initial reproduction fixture incorrectly assumed subadult buds mature under
|
||||
rules 8. Its failure is retained as an ecological gap, not a passing test. The
|
||||
corrected integration regression uses a real fully grown local founder budding
|
||||
beside higher-namespace imported residents; no wildlife was fabricated/healed and
|
||||
no growth rule was changed. Another audit added the explicit durable manifest/
|
||||
envelope/owner check so format 7 cannot be mislabeled as admitted bundle 9.
|
||||
|
||||
Limits: these are portable transfer candidates and compatibility checks, not
|
||||
atomic biological escrow, destination activation, migration-specific PostgreSQL
|
||||
fault drills, linked spatial/ecological coupling or actual multi-day qualification.
|
||||
The complete PostgreSQL backup/DR/upgrade suite last passed for M12.4b/schema 45;
|
||||
this segment changes no SQL and reruns the focused affected population suite.
|
||||
See [scope](M12_EXACT_BIOLOGICAL_TRANSFER.md) and [remaining gates](M12_REMAINING_GATES.md).
|
||||
|
||||
## 2026-10-17 - M12.4b owned population intervals
|
||||
|
||||
ADR-0143/schema 45 affects population admission/settlement journals, the owning
|
||||
|
||||
@@ -96,6 +96,12 @@ class LineageArchive {
|
||||
organism(std::uint64_t identity) const;
|
||||
[[nodiscard]] std::expected<std::uint64_t, HistoryError>
|
||||
current_genome(std::uint64_t organism) const;
|
||||
// Detached append-only merge for exact ownership transfer. Imports selected
|
||||
// living organisms, their immutable births/revisions and all genome parents.
|
||||
// IDs must be sorted/unique. Existing death or conflicting history refuses
|
||||
// the whole result; both input archives remain unchanged.
|
||||
[[nodiscard]] std::expected<LineageArchive, HistoryError>
|
||||
merge_living_from(const LineageArchive& source, std::span<const std::uint64_t> organisms) const;
|
||||
// Oldest first, ending at the founder. Still resolves after every carrier dies.
|
||||
[[nodiscard]] std::expected<std::vector<std::uint64_t>, HistoryError>
|
||||
ancestry(std::uint64_t genome) const;
|
||||
|
||||
@@ -42,6 +42,9 @@ inline constexpr std::uint32_t cellular_lives_reproduction_rules = 7;
|
||||
// contact; detritus becomes bubbles and emptied deposits disappear. The habitat envelope
|
||||
// carries the new state, so they keep owner format 6.
|
||||
inline constexpr std::uint32_t cellular_lives_foraging_rules = 8;
|
||||
// ADR-0144: opt-in boundary accounting and retained nonresident history. Biology
|
||||
// remains rules 8; every earlier envelope retains its original meaning.
|
||||
inline constexpr std::uint32_t cellular_lives_migration_format = 7;
|
||||
// The founder a region of these rules creates.
|
||||
[[nodiscard]] constexpr biology::FounderVersion
|
||||
cellular_founder_version(std::uint32_t rules) noexcept {
|
||||
@@ -115,6 +118,12 @@ struct CellularOwnedCorpse {
|
||||
std::uint64_t parent_life{}; // Format 5: kept after death.
|
||||
auto operator<=>(const CellularOwnedCorpse&) const = default;
|
||||
};
|
||||
struct CellularBoundaryLedger {
|
||||
accounting::Inventory incoming{}, outgoing{}; // Cumulative flows, never stock.
|
||||
std::vector<std::uint64_t> nonresident_organisms; // Retained history, never activity.
|
||||
bool operator==(const CellularBoundaryLedger&) const = default;
|
||||
};
|
||||
class CellularMigration;
|
||||
struct CellularLifeIntent {
|
||||
std::uint64_t life_id{};
|
||||
biology::CellularStepIntent intent{};
|
||||
@@ -240,6 +249,10 @@ class CellularLives {
|
||||
[[nodiscard]] accounting::Inventory heat() const noexcept;
|
||||
[[nodiscard]] accounting::Inventory opening_total() const noexcept;
|
||||
[[nodiscard]] accounting::Inventory current_total() const noexcept;
|
||||
[[nodiscard]] const CellularBoundaryLedger& boundary_ledger() const noexcept;
|
||||
// Explicit detached format-6/rules-8 -> format-7 upgrade. No biology advances.
|
||||
[[nodiscard]] std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
|
||||
migration_upgrade_candidate() const;
|
||||
// Latest successful in-memory step report; omitted from snapshots and empty after restore.
|
||||
[[nodiscard]] std::span<const CellularLifeTickFeedback> last_tick_feedback() const noexcept;
|
||||
[[nodiscard]] std::vector<std::uint8_t> snapshot() const;
|
||||
@@ -280,6 +293,10 @@ class CellularLives {
|
||||
std::span<const CellularMutationSelection> selections);
|
||||
|
||||
private:
|
||||
friend class CellularMigration;
|
||||
[[nodiscard]] std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
|
||||
migration_replacement(std::vector<CellularOwnedLife>, const biology::LineageArchive&,
|
||||
CellularBoundaryLedger) const;
|
||||
struct State;
|
||||
explicit CellularLives(std::unique_ptr<State> state);
|
||||
std::unique_ptr<State> state_;
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include "evo/server/lifecycle/cellular_population.hpp"
|
||||
|
||||
namespace evo::server {
|
||||
enum class CellularMigrationError : std::uint8_t {
|
||||
version,
|
||||
identity,
|
||||
tick,
|
||||
ineligible,
|
||||
capacity,
|
||||
placement,
|
||||
archive,
|
||||
overflow,
|
||||
conservation
|
||||
};
|
||||
struct CellularMigrationExecution {
|
||||
std::uint64_t genome{};
|
||||
std::vector<std::uint64_t> members;
|
||||
accounting::Inventory transferred;
|
||||
std::unique_ptr<CellularLives> source, destination;
|
||||
};
|
||||
struct CellularPopulationMigrationExecution {
|
||||
std::uint64_t genome{};
|
||||
std::vector<std::uint64_t> members;
|
||||
accounting::Inventory transferred;
|
||||
std::unique_ptr<CellularPopulation> source, destination;
|
||||
};
|
||||
// ADR-0144: exact no-tick ownership candidates at an explicitly compatible cut.
|
||||
// This chooses neither a route nor a movement. It preserves every coordinate and
|
||||
// biological field, imports the complete genome ancestry and moves actual stock.
|
||||
// Publication must commit the pair under both current fences before activation.
|
||||
class CellularMigration {
|
||||
public:
|
||||
[[nodiscard]] static std::expected<CellularMigrationExecution, CellularMigrationError>
|
||||
prepare(const CellularLives& source, const CellularLives& destination, std::uint64_t genome);
|
||||
[[nodiscard]] static std::expected<CellularPopulationMigrationExecution, CellularMigrationError>
|
||||
prepare(const CellularPopulation& source, const CellularPopulation& destination,
|
||||
std::uint64_t genome);
|
||||
};
|
||||
} // namespace evo::server
|
||||
@@ -49,6 +49,10 @@ class CellularPopulation {
|
||||
[[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;
|
||||
[[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,
|
||||
@@ -73,6 +77,7 @@ class CellularPopulation {
|
||||
std::span<const CellularMutationSelection> selections = {});
|
||||
|
||||
private:
|
||||
friend class CellularMigration;
|
||||
friend std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
|
||||
execute_population_batch(const CellularPopulation&, CellularPopulationBatch&, bool);
|
||||
explicit CellularPopulation(std::unique_ptr<CellularLives> state, CellularTierPolicy policy);
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
# Cellular owner format 7: exact boundary accounting
|
||||
|
||||
Authority: ADR-0144. Portable opt-in representation only; no PostgreSQL manifest
|
||||
bundle or live worker admission is introduced by this segment. Rules stay 8.
|
||||
Formats 1–6 and their canonical bytes remain unchanged. Explicit no-tick upgrade
|
||||
from rules-8 format 6 preserves biology and opening stock and adds zero flows
|
||||
and no nonresident references. No downgrade may discard a nonzero ledger.
|
||||
|
||||
Little-endian, no native padding. Magic `AEVCLV07`, format 7, rules 8. The first
|
||||
220 bytes have the format-6 layout except magic/format. The additional fields are:
|
||||
|
||||
| Offset | Bytes | Field |
|
||||
|---:|---:|---|
|
||||
| 220 | 40 | Cumulative incoming inventory: energy, C, N, P, other; five u64s |
|
||||
| 260 | 40 | Cumulative outgoing inventory in the same order |
|
||||
| 300 | 4 | Nonresident organism count, at most 65,536 |
|
||||
| 304 | 4 | Reserved zero |
|
||||
| 308 | 8 each | Existing sorted spawn-site records |
|
||||
| after sites | 504 each | Existing format-6 living records |
|
||||
| after lives | 124 each | Existing format-5/6 corpse records |
|
||||
| after corpses | declared lengths | Complete canonical habitat and lineage archive |
|
||||
| after archive | 8 each | Sorted unique nonresident organism IDs |
|
||||
| final | 8 | FNV-1a64 of all preceding bytes |
|
||||
|
||||
The exact length is `308 + 8*sites + 504*lives + 124*corpses + habitat_bytes +
|
||||
archive_bytes + 8*nonresidents + 8`, bounded by the existing 16 MiB limit. Require
|
||||
all existing nested closure, trusted identity/tick and canonical re-encoding.
|
||||
The checksum is corruption detection, not provenance or authority.
|
||||
|
||||
Per channel, `opening + incoming = current + outgoing`. Validate the net flow
|
||||
using checked addition/subtraction, without overflowing an intermediate sum of
|
||||
large equal flows. Each cumulative counter itself may never wrap. `current`
|
||||
counts nursery, habitat, bodies, reserves, corpses and heat exactly once. Flow
|
||||
metadata and nonresident history own no material and provide no funding.
|
||||
|
||||
Every archived organism must be exactly one local life, local corpse or
|
||||
nonresident reference. Nonresidents retain immutable birth/revision/death facts;
|
||||
an alive fact is only the last known cut, not a current owned census. Known facts
|
||||
cannot be in the future. Duplicate residency and terminal resurrection are refused.
|
||||
|
||||
Foreign bud IDs retain `2^63 + origin_region*2^32 + sequence`, with nonzero bounded
|
||||
origin/sequence; local-origin IDs must still lie below the local next-bud counter.
|
||||
Imported cells have zero accounts and preserve organism/event IDs, parent-life,
|
||||
source, age, genome, growth, damage, position, body and reserve. New buds consume
|
||||
the destination namespace and are inserted in identity order even when their IDs
|
||||
are smaller than imported parents. The existing twin roll remains keyed by world,
|
||||
current owner region, parent life and tick; a transfer promises an exact cut,
|
||||
not identical future ecology in a different regional environment.
|
||||
|
||||
Portable paired preparation moves a complete wildlife genome group between
|
||||
distinct compatible owners at one tick, preserving numeric local coordinates.
|
||||
Both owners' controlled cells must be fully suspended. Bounds, destination
|
||||
body conflicts, capacity, ancestry merge and both budgets must pass. Rejection
|
||||
leaves both originals unchanged. Clamped arena geometry remains ADR-0102;
|
||||
automatic spatial crossings and linked environmental fields are unimplemented.
|
||||
|
||||
Durability must bind both candidates to one fenced atomic pair publication. This
|
||||
format alone supplies no freeze, escrow, receipt, activation or retry guarantee.
|
||||
@@ -37,3 +37,17 @@ membership and sequence zero; reverse entry unwraps the identical biology.
|
||||
Conversion sequence is local to each profile-5 generation. The immutable region
|
||||
commit sequence and transition evidence distinguish generations and never reset.
|
||||
See [population batch format 1](cellular-population-batch.md).
|
||||
|
||||
## Portable envelope 2
|
||||
|
||||
ADR-0144 adds magic `AEVOPOP2`, version 2, with the identical header/member layout
|
||||
and a nested [owner format 7](cellular-lives-v7.md), rules 8. Envelope 1 requires
|
||||
owner format 6; magic, envelope version and nested tuple must agree. Explicit
|
||||
no-tick upgrade retains every membership tag, conversion sequence and policy.
|
||||
Paired population migration moves the exact existing tags with their cells,
|
||||
retaining each region's policy and conversion sequence; it performs no collapse,
|
||||
materialization or new automatic-policy evaluation. Untagged members stay untagged.
|
||||
|
||||
Envelope 2 is portable only in this segment. Existing durable trace 1 refuses it;
|
||||
schema 45/profile 5 does not admit the new nested owner tuple. No live flag or
|
||||
default entry enables migration or this new representation.
|
||||
|
||||
@@ -34,3 +34,11 @@ replays revisions against the organism's current genome. The result must
|
||||
re-encode to identical bytes. A format-2 archive with no revisions is used by
|
||||
the explicit cellular-v2-to-v3 migration. Other profiles retain their v1
|
||||
payload caps and bytes.
|
||||
|
||||
ADR-0144 adds a detached `merge_living_from` operation without changing this
|
||||
encoding. It retains the destination archive, imports selected living organisms'
|
||||
exact birth/revision facts and complete genome-parent closure, and validates a
|
||||
canonical round trip. A dead destination record, conflicting identity/birth or
|
||||
revision rewind refuses the whole candidate. Repeating the same import is exact;
|
||||
source and destination inputs remain unchanged. This operation does not confer
|
||||
residency or copy biological stock; the versioned cellular owner tracks that.
|
||||
|
||||
@@ -19,6 +19,103 @@ bool holds(const Records& records, std::uint64_t identity, Identify identify) {
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<LineageArchive, HistoryError>
|
||||
LineageArchive::merge_living_from(const LineageArchive& source,
|
||||
std::span<const std::uint64_t> organisms) const {
|
||||
if (organisms.empty() || organisms.size() > max_archived_organisms || organisms.front() == 0 ||
|
||||
!std::ranges::is_sorted(organisms) ||
|
||||
std::ranges::adjacent_find(organisms) != organisms.end())
|
||||
return std::unexpected(HistoryError::invalid_identity);
|
||||
LineageArchive result{*this};
|
||||
if (source.format_version_ == lineage_archive_revision_format)
|
||||
result.upgrade_to_revision_format();
|
||||
std::vector<std::uint64_t> roots;
|
||||
roots.reserve(organisms.size() + source.revisions_.size() * 2);
|
||||
for (const auto id : organisms) {
|
||||
const auto record = source.organism(id);
|
||||
if (!record)
|
||||
return std::unexpected(record.error());
|
||||
if (!record->alive)
|
||||
return std::unexpected(HistoryError::already_dead);
|
||||
roots.push_back(record->genome);
|
||||
}
|
||||
for (const auto& revision : source.revisions_)
|
||||
if (std::ranges::binary_search(organisms, revision.organism)) {
|
||||
roots.push_back(revision.previous_genome);
|
||||
roots.push_back(revision.successor_genome);
|
||||
}
|
||||
// Explicit postorder stack: even a maximum-depth ancestry cannot consume the
|
||||
// native call stack. Shared parents are admitted once and before their child.
|
||||
std::vector<std::pair<std::uint64_t, bool>> pending;
|
||||
pending.reserve(roots.size() + source.genomes_.size() * 2);
|
||||
for (const auto root : roots)
|
||||
pending.emplace_back(root, false);
|
||||
std::vector<std::uint64_t> complete;
|
||||
complete.reserve(source.genomes_.size());
|
||||
while (!pending.empty()) {
|
||||
const auto [id, expanded] = pending.back();
|
||||
pending.pop_back();
|
||||
if (std::ranges::find(complete, id) != complete.end())
|
||||
continue;
|
||||
const auto genome = source.find(id);
|
||||
if (!genome)
|
||||
return std::unexpected(genome.error());
|
||||
if (!expanded) {
|
||||
pending.emplace_back(id, true);
|
||||
for (const auto parent : genome->parents())
|
||||
if (std::ranges::find(complete, parent) == complete.end())
|
||||
pending.emplace_back(parent, false);
|
||||
continue;
|
||||
}
|
||||
const auto existing = result.find(id);
|
||||
if (existing) {
|
||||
if (*existing != *genome)
|
||||
return std::unexpected(HistoryError::duplicate_genome);
|
||||
} else if (const auto admitted = result.admit(*genome); !admitted)
|
||||
return std::unexpected(admitted.error());
|
||||
complete.push_back(id);
|
||||
}
|
||||
for (const auto id : organisms) {
|
||||
const auto record = source.organism(id);
|
||||
if (!record)
|
||||
return std::unexpected(record.error());
|
||||
const auto existing = result.organism(id);
|
||||
if (existing) {
|
||||
if (!existing->alive)
|
||||
return std::unexpected(HistoryError::already_dead);
|
||||
if (*existing != *record)
|
||||
return std::unexpected(HistoryError::duplicate_organism);
|
||||
} else if (const auto born = result.record_birth(id, record->genome, record->born); !born)
|
||||
return std::unexpected(born.error());
|
||||
std::vector<OrganismGenomeRevision> incoming, retained;
|
||||
for (const auto& revision : source.revisions_)
|
||||
if (revision.organism == id)
|
||||
incoming.push_back(revision);
|
||||
for (const auto& revision : result.revisions_)
|
||||
if (revision.organism == id)
|
||||
retained.push_back(revision);
|
||||
std::ranges::sort(incoming, {}, &OrganismGenomeRevision::at);
|
||||
std::ranges::sort(retained, {}, &OrganismGenomeRevision::at);
|
||||
if (retained.size() > incoming.size() ||
|
||||
!std::ranges::equal(retained, std::span{incoming}.first(retained.size())))
|
||||
return std::unexpected(HistoryError::invalid_revision);
|
||||
for (std::size_t index = retained.size(); index < incoming.size(); ++index) {
|
||||
if (result.revisions_.size() >= max_archived_genomes)
|
||||
return std::unexpected(HistoryError::archive_full);
|
||||
const auto& revision = incoming[index];
|
||||
const auto appended =
|
||||
result.append_revision(id, revision.previous_genome, revision.successor_genome,
|
||||
revision.origin_event, revision.at);
|
||||
if (!appended)
|
||||
return std::unexpected(appended.error());
|
||||
}
|
||||
}
|
||||
auto validated = decode(result.encode());
|
||||
if (!validated)
|
||||
return std::unexpected(HistoryError::invalid_revision);
|
||||
return *validated;
|
||||
}
|
||||
|
||||
std::expected<void, HistoryError> LineageArchive::admit(const Genome& genome) {
|
||||
if (genomes_.size() >= max_archived_genomes) {
|
||||
// Refusing to record is not deleting; a silent drop would be.
|
||||
|
||||
@@ -73,6 +73,10 @@ restore_population_payload(std::span<const std::uint8_t> payload, const Checkpoi
|
||||
return std::unexpected(state.error() == server::CellularPopulationError::unsupported
|
||||
? CheckpointError::unsupported
|
||||
: CheckpointError::payload);
|
||||
if ((*state)->format_version() != 1 ||
|
||||
(*state)->state().format_version() != m.cellular_versions.envelope ||
|
||||
(*state)->state().rules_version() != m.cellular_versions.rules)
|
||||
return std::unexpected(CheckpointError::unsupported);
|
||||
return std::move(*state);
|
||||
}
|
||||
} // namespace evo::persistence
|
||||
|
||||
@@ -18,6 +18,7 @@ constexpr std::uint64_t cellular_lives_v3_magic = 0x3330564C43564541ULL; // AEVC
|
||||
constexpr std::uint64_t cellular_lives_v4_magic = 0x3430564C43564541ULL; // AEVCLV04
|
||||
constexpr std::uint64_t cellular_lives_v5_magic = 0x3530564C43564541ULL; // AEVCLV05
|
||||
constexpr std::uint64_t cellular_lives_v6_magic = 0x3630564C43564541ULL; // AEVCLV06
|
||||
constexpr std::uint64_t cellular_lives_v7_magic = 0x3730564C43564541ULL; // AEVCLV07
|
||||
constexpr std::size_t cellular_lives_header_bytes = 212;
|
||||
constexpr std::size_t cellular_site_bytes = 8;
|
||||
constexpr std::size_t cellular_life_v1_bytes = 168;
|
||||
@@ -32,6 +33,8 @@ constexpr std::size_t cellular_life_v5_bytes = cellular_life_v2_bytes + 8;
|
||||
constexpr std::size_t cellular_corpse_v5_bytes = cellular_corpse_v4_bytes + 8;
|
||||
// Format 6 (ADR-0128): each life adds presence (1), 7 zero bytes and its freeze tick (8).
|
||||
constexpr std::size_t cellular_life_v6_bytes = cellular_life_v5_bytes + 16;
|
||||
// Format 7: two boundary-flow inventories and nonresident count/reserved.
|
||||
constexpr std::size_t cellular_lives_v7_header_bytes = cellular_lives_v5_header_bytes + 88;
|
||||
|
||||
std::uint64_t checksum(std::span<const std::uint8_t> bytes) noexcept {
|
||||
std::uint64_t value = 14695981039346656037ULL;
|
||||
@@ -281,7 +284,7 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
|
||||
accounting::Inventory nursery, accounting::Inventory heat,
|
||||
accounting::Inventory opening_total, bool detritus_rules = false,
|
||||
bool bud_rules = false, std::uint64_t next_bud_sequence = 1,
|
||||
bool presence_rules = false) {
|
||||
bool presence_rules = false, const CellularBoundaryLedger* boundary = nullptr) {
|
||||
// A bud's identity lies in this region's namespace below the next sequence; anything
|
||||
// else is an allocated identity, below the namespace (ADR-0125).
|
||||
const auto bud_identity = [&](std::uint64_t identity) noexcept {
|
||||
@@ -292,7 +295,11 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
|
||||
const auto lineage_valid = [&](std::uint64_t life_id, std::uint64_t parent) noexcept {
|
||||
if (parent == 0) // Rules 4 keep allocated identities below the bud namespace.
|
||||
return !bud_rules || life_id < cellular_bud_namespace;
|
||||
return bud_identity(life_id) && parent != life_id;
|
||||
const auto origin = (life_id >> 32U) & cellular_bud_max_region;
|
||||
const bool imported = boundary && life_id >= cellular_bud_namespace && origin != region &&
|
||||
origin > 0 && origin <= cellular_bud_max_region &&
|
||||
(life_id & 0xFFFF'FFFFULL) != 0;
|
||||
return (bud_identity(life_id) || imported) && parent != life_id;
|
||||
};
|
||||
const auto& map = habitat.config();
|
||||
if (world == 0 || region == 0 || frame == 0 || nursery_config.world != world ||
|
||||
@@ -305,7 +312,8 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
|
||||
!std::ranges::is_sorted(nursery_config.sites) ||
|
||||
lives.size() > biology::max_cellular_tick_lives ||
|
||||
corpses.size() > biology::max_archived_organisms ||
|
||||
archive.recorded() != lives.size() + corpses.size())
|
||||
archive.recorded() !=
|
||||
lives.size() + corpses.size() + (boundary ? boundary->nonresident_organisms.size() : 0))
|
||||
return std::unexpected{CellularLivesError::invalid_config};
|
||||
if (std::ranges::any_of(heat.matter, [](const auto& value) { return value.value != 0; }))
|
||||
return std::unexpected{CellularLivesError::conservation};
|
||||
@@ -338,7 +346,8 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
|
||||
std::vector<std::uint64_t> live_organisms;
|
||||
std::vector<std::uint64_t> life_ids;
|
||||
live_organisms.reserve(lives.size());
|
||||
life_ids.reserve(lives.size() + corpses.size());
|
||||
life_ids.reserve(lives.size() + corpses.size() +
|
||||
(boundary ? boundary->nonresident_organisms.size() : 0));
|
||||
for (std::size_t i = 0; i < lives.size(); ++i) {
|
||||
const auto& life = lives[i];
|
||||
// Founders are owned; buds are unowned wildlife (rules 4) and never earn offers.
|
||||
@@ -382,6 +391,24 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
|
||||
live_organisms.push_back(life.identity.organism);
|
||||
life_ids.push_back(life.identity.life);
|
||||
}
|
||||
if (boundary) {
|
||||
if (boundary->nonresident_organisms.size() > biology::max_archived_organisms ||
|
||||
!std::ranges::is_sorted(boundary->nonresident_organisms) ||
|
||||
std::ranges::adjacent_find(boundary->nonresident_organisms) !=
|
||||
boundary->nonresident_organisms.end())
|
||||
return std::unexpected(CellularLivesError::invalid_life);
|
||||
for (const auto id : boundary->nonresident_organisms) {
|
||||
if (id < cellular_bud_namespace || !lineage_valid(id, 1))
|
||||
return std::unexpected(CellularLivesError::invalid_life);
|
||||
const auto record = archive.organism(id);
|
||||
if (!record || record->born.value > config.tick ||
|
||||
(!record->alive && record->died.value > config.tick))
|
||||
return std::unexpected(CellularLivesError::archive);
|
||||
if (record->alive)
|
||||
live_organisms.push_back(id);
|
||||
life_ids.push_back(id);
|
||||
}
|
||||
}
|
||||
std::ranges::sort(live_organisms);
|
||||
if (archive.living_organisms() != live_organisms)
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
@@ -427,7 +454,26 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
|
||||
const auto current = sum_state(nursery, heat, habitat, lives, corpses);
|
||||
if (!current)
|
||||
return std::unexpected{current.error()};
|
||||
if (*current != opening_total)
|
||||
auto expected = opening_total;
|
||||
if (boundary) {
|
||||
accounting::Inventory credit{}, debit{};
|
||||
const auto difference = [](std::uint64_t in, std::uint64_t out, std::uint64_t& plus,
|
||||
std::uint64_t& minus) {
|
||||
if (in >= out)
|
||||
plus = in - out;
|
||||
else
|
||||
minus = out - in;
|
||||
};
|
||||
difference(boundary->incoming.energy.value, boundary->outgoing.energy.value,
|
||||
credit.energy.value, debit.energy.value);
|
||||
for (std::size_t channel = 0; channel < credit.matter.size(); ++channel)
|
||||
difference(boundary->incoming.matter[channel].value,
|
||||
boundary->outgoing.matter[channel].value, credit.matter[channel].value,
|
||||
debit.matter[channel].value);
|
||||
if (!add_inventory(expected, credit) || !subtract_inventory(expected, debit))
|
||||
return std::unexpected(CellularLivesError::conservation);
|
||||
}
|
||||
if (*current != expected)
|
||||
return std::unexpected{CellularLivesError::conservation};
|
||||
return *current;
|
||||
}
|
||||
@@ -447,6 +493,7 @@ struct CellularLives::State {
|
||||
accounting::Inventory opening_total{};
|
||||
accounting::Inventory current_total{};
|
||||
std::uint64_t next_bud_sequence{1}; // Format 5: the next bud identity's sequence.
|
||||
CellularBoundaryLedger boundary{};
|
||||
|
||||
State(std::uint64_t world_id, std::uint64_t region_id, std::uint64_t frame_id,
|
||||
CellularLivesConfig cfg, NurseryConfig nursery_definition,
|
||||
@@ -454,12 +501,13 @@ struct CellularLives::State {
|
||||
std::vector<CellularOwnedCorpse> dead, const biology::LineageArchive& history,
|
||||
accounting::Inventory source, accounting::Inventory accumulated_heat,
|
||||
accounting::Inventory initial, accounting::Inventory current,
|
||||
std::uint32_t format = cellular_lives_format, std::uint32_t rules = cellular_lives_rules)
|
||||
std::uint32_t format = cellular_lives_format, std::uint32_t rules = cellular_lives_rules,
|
||||
CellularBoundaryLedger boundary_ledger = {})
|
||||
: world{world_id}, region{region_id}, frame{frame_id}, format_version{format},
|
||||
rules_version{rules}, config{cfg}, nursery_config{std::move(nursery_definition)},
|
||||
habitat{std::move(map)}, lives{std::move(living)}, corpses{std::move(dead)},
|
||||
archive{history}, nursery{source}, heat{accumulated_heat}, opening_total{initial},
|
||||
current_total{current} {}
|
||||
current_total{current}, boundary{std::move(boundary_ledger)} {}
|
||||
};
|
||||
|
||||
CellularLives::CellularLives(std::unique_ptr<State> state) : state_{std::move(state)} {}
|
||||
@@ -623,6 +671,46 @@ accounting::Inventory CellularLives::opening_total() const noexcept {
|
||||
accounting::Inventory CellularLives::current_total() const noexcept {
|
||||
return state_->current_total;
|
||||
}
|
||||
const CellularBoundaryLedger& CellularLives::boundary_ledger() const noexcept {
|
||||
return state_->boundary;
|
||||
}
|
||||
|
||||
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
|
||||
CellularLives::migration_replacement(std::vector<CellularOwnedLife> living,
|
||||
const biology::LineageArchive& archive,
|
||||
CellularBoundaryLedger boundary) const {
|
||||
if (state_->rules_version != cellular_lives_foraging_rules ||
|
||||
(state_->format_version != cellular_lives_presence_format &&
|
||||
state_->format_version != cellular_lives_migration_format))
|
||||
return std::unexpected(CellularLivesError::version);
|
||||
auto habitat = biology::CellularHabitat::restore(state_->habitat->snapshot(), state_->world,
|
||||
state_->region, state_->frame);
|
||||
if (!habitat)
|
||||
return std::unexpected(CellularLivesError::habitat);
|
||||
const auto total = validate_state(state_->world, state_->region, state_->frame, state_->config,
|
||||
state_->nursery_config, **habitat, living, state_->corpses,
|
||||
archive, state_->nursery, state_->heat, state_->opening_total,
|
||||
true, true, state_->next_bud_sequence, true, &boundary);
|
||||
if (!total)
|
||||
return std::unexpected(total.error());
|
||||
auto replacement = std::unique_ptr<CellularLives>{new CellularLives{std::make_unique<State>(
|
||||
state_->world, state_->region, state_->frame, state_->config, state_->nursery_config,
|
||||
std::move(*habitat), std::move(living), state_->corpses, archive, state_->nursery,
|
||||
state_->heat, state_->opening_total, *total, cellular_lives_migration_format,
|
||||
state_->rules_version, std::move(boundary))}};
|
||||
replacement->state_->next_bud_sequence = state_->next_bud_sequence;
|
||||
try {
|
||||
[[maybe_unused]] const auto bytes = replacement->snapshot();
|
||||
} catch (const std::length_error&) {
|
||||
return std::unexpected(CellularLivesError::overflow);
|
||||
}
|
||||
return replacement;
|
||||
}
|
||||
|
||||
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
|
||||
CellularLives::migration_upgrade_candidate() const {
|
||||
return migration_replacement(state_->lives, state_->archive, state_->boundary);
|
||||
}
|
||||
std::span<const CellularLifeTickFeedback> CellularLives::last_tick_feedback() const noexcept {
|
||||
return last_tick_feedback_;
|
||||
}
|
||||
@@ -644,16 +732,20 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
const auto corpse_bytes = parents ? cellular_corpse_v5_bytes
|
||||
: causes ? cellular_corpse_v4_bytes
|
||||
: cellular_corpse_bytes;
|
||||
const auto header_bytes =
|
||||
parents ? cellular_lives_v5_header_bytes : cellular_lives_header_bytes;
|
||||
const bool migration = state_->format_version == cellular_lives_migration_format;
|
||||
const auto header_bytes = migration ? cellular_lives_v7_header_bytes
|
||||
: parents ? cellular_lives_v5_header_bytes
|
||||
: cellular_lives_header_bytes;
|
||||
const auto external_count = migration ? state_->boundary.nonresident_organisms.size() : 0;
|
||||
const auto total_bytes = header_bytes + cellular_site_bytes * site_count +
|
||||
life_bytes * life_count + corpse_bytes * corpse_count +
|
||||
habitat.size() + archive.size() + 8;
|
||||
habitat.size() + archive.size() + external_count * 8 + 8;
|
||||
if (total_bytes > max_cellular_lives_bytes ||
|
||||
habitat.size() > biology::max_cellular_habitat_bytes ||
|
||||
archive.size() > biology::max_archive_bytes || site_count > max_primitive_lives ||
|
||||
life_count > biology::max_cellular_tick_lives ||
|
||||
corpse_count > biology::max_archived_organisms)
|
||||
corpse_count > biology::max_archived_organisms ||
|
||||
external_count > biology::max_archived_organisms)
|
||||
throw std::length_error{"cellular lives checkpoint exceeds its format bounds"};
|
||||
|
||||
std::vector<std::uint8_t> bytes;
|
||||
@@ -665,6 +757,7 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
: state_->format_version == cellular_lives_engulf_format ? cellular_lives_v4_magic
|
||||
: state_->format_version == cellular_lives_bud_format ? cellular_lives_v5_magic
|
||||
: state_->format_version == cellular_lives_presence_format ? cellular_lives_v6_magic
|
||||
: migration ? cellular_lives_v7_magic
|
||||
: cellular_lives_v2_magic;
|
||||
out.put(magic);
|
||||
out.put(state_->format_version, 4);
|
||||
@@ -688,6 +781,12 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
out.inventory(state_->heat);
|
||||
if (parents)
|
||||
out.put(state_->next_bud_sequence);
|
||||
if (migration) {
|
||||
out.inventory(state_->boundary.incoming);
|
||||
out.inventory(state_->boundary.outgoing);
|
||||
out.put(external_count, 4);
|
||||
out.put(0, 4);
|
||||
}
|
||||
for (const auto site : state_->nursery_config.sites)
|
||||
out.site(site);
|
||||
for (const auto& life : state_->lives) {
|
||||
@@ -768,6 +867,9 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
|
||||
}
|
||||
out.raw(habitat);
|
||||
out.raw(archive);
|
||||
if (migration)
|
||||
for (const auto id : state_->boundary.nonresident_organisms)
|
||||
out.put(id);
|
||||
if (bytes.size() + 8 != total_bytes)
|
||||
throw std::logic_error{"cellular lives checkpoint size calculation mismatch"};
|
||||
out.put(checksum(bytes));
|
||||
@@ -948,19 +1050,21 @@ CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) c
|
||||
state_->region, state_->frame);
|
||||
if (!habitat)
|
||||
return std::unexpected{CellularLivesError::habitat};
|
||||
const auto current =
|
||||
validate_state(state_->world, state_->region, state_->frame, state_->config, nursery_config,
|
||||
**habitat, lives, state_->corpses, archive, nursery, heat,
|
||||
state_->opening_total, state_->rules_version >= cellular_lives_engulf_rules,
|
||||
state_->rules_version >= cellular_lives_bud_rules, state_->next_bud_sequence,
|
||||
state_->rules_version >= cellular_lives_presence_rules);
|
||||
const auto current = validate_state(
|
||||
state_->world, state_->region, state_->frame, state_->config, nursery_config, **habitat,
|
||||
lives, state_->corpses, archive, nursery, heat, state_->opening_total,
|
||||
state_->rules_version >= cellular_lives_engulf_rules,
|
||||
state_->rules_version >= cellular_lives_bud_rules, state_->next_bud_sequence,
|
||||
state_->rules_version >= cellular_lives_presence_rules,
|
||||
state_->format_version == cellular_lives_migration_format ? &state_->boundary : nullptr);
|
||||
if (!current)
|
||||
return std::unexpected{current.error()};
|
||||
|
||||
auto candidate = std::unique_ptr<CellularLives>{new CellularLives{std::make_unique<State>(
|
||||
state_->world, state_->region, state_->frame, state_->config, std::move(nursery_config),
|
||||
std::move(*habitat), std::move(lives), state_->corpses, archive, nursery, heat,
|
||||
state_->opening_total, *current, state_->format_version, state_->rules_version)}};
|
||||
state_->opening_total, *current, state_->format_version, state_->rules_version,
|
||||
state_->boundary)}};
|
||||
candidate->last_tick_feedback_ = last_tick_feedback_;
|
||||
candidate->state_->next_bud_sequence = state_->next_bud_sequence;
|
||||
return candidate;
|
||||
@@ -985,12 +1089,16 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
format_version == cellular_lives_format &&
|
||||
rules_version == cellular_lives_rules;
|
||||
// Format 6 carries rules 5, 6 (founder 2) or 7 (founder 3, ADR-0137); no new state.
|
||||
const bool presence_version = magic == cellular_lives_v6_magic &&
|
||||
format_version == cellular_lives_presence_format &&
|
||||
(rules_version == cellular_lives_presence_rules ||
|
||||
rules_version == cellular_lives_balance_rules ||
|
||||
rules_version == cellular_lives_reproduction_rules ||
|
||||
rules_version == cellular_lives_foraging_rules);
|
||||
const bool migration_version = magic == cellular_lives_v7_magic &&
|
||||
format_version == cellular_lives_migration_format &&
|
||||
rules_version == cellular_lives_foraging_rules;
|
||||
const bool presence_version =
|
||||
migration_version ||
|
||||
(magic == cellular_lives_v6_magic && format_version == cellular_lives_presence_format &&
|
||||
(rules_version == cellular_lives_presence_rules ||
|
||||
rules_version == cellular_lives_balance_rules ||
|
||||
rules_version == cellular_lives_reproduction_rules ||
|
||||
rules_version == cellular_lives_foraging_rules));
|
||||
// Formats 5 and 6 carry the bud sequence and parent lives (rules 4 and 5).
|
||||
const bool bud_version = presence_version || (magic == cellular_lives_v5_magic &&
|
||||
format_version == cellular_lives_bud_format &&
|
||||
@@ -1006,6 +1114,8 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
const bool mutation_version = offer_version || revision_version;
|
||||
if (!legacy && !mutation_version)
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
if (migration_version && bytes.size() < cellular_lives_v7_header_bytes + 8)
|
||||
return std::unexpected(CellularLivesError::format);
|
||||
const auto saved_world = in.get();
|
||||
const auto saved_region = in.get();
|
||||
const auto saved_frame = in.get();
|
||||
@@ -1036,6 +1146,15 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
const auto nursery = in.inventory();
|
||||
const auto heat = in.inventory();
|
||||
const auto next_bud_sequence = bud_version ? in.get() : std::uint64_t{1};
|
||||
CellularBoundaryLedger boundary;
|
||||
std::uint64_t nonresident_count = 0;
|
||||
if (migration_version) {
|
||||
boundary.incoming = in.inventory();
|
||||
boundary.outgoing = in.inventory();
|
||||
nonresident_count = in.get(4);
|
||||
if (in.get(4) != 0 || nonresident_count > biology::max_archived_organisms)
|
||||
return std::unexpected(CellularLivesError::format);
|
||||
}
|
||||
const auto life_bytes = legacy ? cellular_life_v1_bytes
|
||||
: presence_version ? cellular_life_v6_bytes
|
||||
: bud_version ? cellular_life_v5_bytes
|
||||
@@ -1043,10 +1162,12 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
const auto corpse_bytes = bud_version ? cellular_corpse_v5_bytes
|
||||
: engulf_version ? cellular_corpse_v4_bytes
|
||||
: cellular_corpse_bytes;
|
||||
const auto expected_size =
|
||||
(bud_version ? cellular_lives_v5_header_bytes : cellular_lives_header_bytes) +
|
||||
cellular_site_bytes * site_count + life_bytes * life_count + corpse_bytes * corpse_count +
|
||||
habitat_size + archive_size + 8;
|
||||
const auto expected_size = (migration_version ? cellular_lives_v7_header_bytes
|
||||
: bud_version ? cellular_lives_v5_header_bytes
|
||||
: cellular_lives_header_bytes) +
|
||||
cellular_site_bytes * site_count + life_bytes * life_count +
|
||||
corpse_bytes * corpse_count + habitat_size + archive_size +
|
||||
nonresident_count * 8 + 8;
|
||||
if (source == 0 || next_bud_sequence == 0 || expected_size != bytes.size())
|
||||
return std::unexpected{CellularLivesError::format};
|
||||
|
||||
@@ -1189,6 +1310,11 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
return std::unexpected{CellularLivesError::archive};
|
||||
}
|
||||
if (migration_version) {
|
||||
boundary.nonresident_organisms.reserve(static_cast<std::size_t>(nonresident_count));
|
||||
for (std::uint64_t index = 0; index < nonresident_count; ++index)
|
||||
boundary.nonresident_organisms.push_back(in.get());
|
||||
}
|
||||
if (archive->format_version() != (revision_version ? biology::lineage_archive_revision_format
|
||||
: biology::lineage_archive_format))
|
||||
return std::unexpected{CellularLivesError::version};
|
||||
@@ -1214,17 +1340,17 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
}
|
||||
}
|
||||
const auto current =
|
||||
validate_state(saved_world, saved_region, saved_frame, config, nursery_config, **habitat,
|
||||
lives, corpses, *archive, nursery, heat, opening_total, engulf_version,
|
||||
bud_version, next_bud_sequence, presence_version);
|
||||
const auto current = validate_state(
|
||||
saved_world, saved_region, saved_frame, config, nursery_config, **habitat, lives, corpses,
|
||||
*archive, nursery, heat, opening_total, engulf_version, bud_version, next_bud_sequence,
|
||||
presence_version, migration_version ? &boundary : nullptr);
|
||||
if (!current)
|
||||
return std::unexpected{current.error()};
|
||||
|
||||
auto restored = std::unique_ptr<CellularLives>{new CellularLives{std::make_unique<State>(
|
||||
saved_world, saved_region, saved_frame, config, std::move(nursery_config),
|
||||
std::move(*habitat), std::move(lives), std::move(corpses), *archive, nursery, heat,
|
||||
opening_total, *current, format_version, rules_version)}};
|
||||
opening_total, *current, format_version, rules_version, std::move(boundary))}};
|
||||
restored->state_->next_bud_sequence = next_bud_sequence;
|
||||
if (!std::ranges::equal(bytes, restored->snapshot()))
|
||||
return std::unexpected{CellularLivesError::noncanonical};
|
||||
@@ -1502,8 +1628,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
}();
|
||||
for (int copy = 0; copy < (twins ? 2 : 1); ++copy) {
|
||||
if (copy == 1)
|
||||
// Appending the first bud may have moved the parent; find it again (buds
|
||||
// sort after every existing life, so the order still holds).
|
||||
// Inserting the first bud may have moved the parent; find it again.
|
||||
parent = std::ranges::lower_bound(
|
||||
next_lives, request.life_id, {},
|
||||
[](const CellularOwnedLife& value) { return value.identity.life; });
|
||||
@@ -1556,9 +1681,12 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
bud.body = transition->body;
|
||||
bud.reserve = transition->reserve;
|
||||
bud.parent_life = parent->identity.life;
|
||||
// Bud identities increase within the region and exceed every allocated identity,
|
||||
// so the new life sorts after all existing ones.
|
||||
next_lives.push_back(bud);
|
||||
// Foreign residents can have a higher origin-region namespace. Keep
|
||||
// the sequence sorted even before the next parent's lookup/twin.
|
||||
const auto slot = std::ranges::lower_bound(
|
||||
next_lives, identity, {},
|
||||
[](const CellularOwnedLife& life) { return life.identity.life; });
|
||||
next_lives.insert(slot, bud);
|
||||
++next_bud_sequence;
|
||||
if (!twin)
|
||||
bud_results.push_back({request.life_id, identity, CellularBudResult::budded});
|
||||
@@ -1640,13 +1768,14 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
auto next_habitat = biology::CellularHabitat::create(std::move(habitat_config));
|
||||
if (!next_habitat)
|
||||
return std::unexpected{CellularLivesError::habitat};
|
||||
const auto total =
|
||||
validate_state(state_->world, state_->region, state_->frame,
|
||||
CellularLivesConfig{state_->config.area, proposal->end_tick,
|
||||
state_->config.ambient_temperature_millikelvin},
|
||||
state_->nursery_config, **next_habitat, next_lives, next_corpses,
|
||||
next_archive, state_->nursery, next_heat, state_->opening_total,
|
||||
engulf_rules, bud_rules, next_bud_sequence, presence_rules);
|
||||
const auto total = validate_state(
|
||||
state_->world, state_->region, state_->frame,
|
||||
CellularLivesConfig{state_->config.area, proposal->end_tick,
|
||||
state_->config.ambient_temperature_millikelvin},
|
||||
state_->nursery_config, **next_habitat, next_lives, next_corpses, next_archive,
|
||||
state_->nursery, next_heat, state_->opening_total, engulf_rules, bud_rules,
|
||||
next_bud_sequence, presence_rules,
|
||||
state_->format_version == cellular_lives_migration_format ? &state_->boundary : nullptr);
|
||||
if (!total)
|
||||
return std::unexpected{total.error()};
|
||||
|
||||
@@ -1656,7 +1785,7 @@ CellularLives::advance_tick_with_mutations(std::span<const CellularLifeIntent> i
|
||||
state_->config.ambient_temperature_millikelvin},
|
||||
state_->nursery_config, std::move(*next_habitat), std::move(next_lives),
|
||||
std::move(next_corpses), next_archive, state_->nursery, next_heat, state_->opening_total,
|
||||
*total, state_->format_version, state_->rules_version);
|
||||
*total, state_->format_version, state_->rules_version, state_->boundary);
|
||||
next->next_bud_sequence = next_bud_sequence;
|
||||
std::vector<CellularLifeTickFeedback> feedback;
|
||||
feedback.reserve(proposal->lives.size());
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
#include "evo/server/lifecycle/cellular_migration.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
|
||||
namespace evo::server {
|
||||
namespace {
|
||||
bool add(accounting::Inventory& total, const accounting::Inventory& value) {
|
||||
const auto maximum = std::numeric_limits<std::uint64_t>::max();
|
||||
if (value.energy.value > maximum - total.energy.value)
|
||||
return false;
|
||||
for (std::size_t c = 0; c < total.matter.size(); ++c)
|
||||
if (value.matter[c].value > maximum - total.matter[c].value)
|
||||
return false;
|
||||
total.energy.value += value.energy.value;
|
||||
for (std::size_t c = 0; c < total.matter.size(); ++c)
|
||||
total.matter[c].value += value.matter[c].value;
|
||||
return true;
|
||||
}
|
||||
bool suspended(const CellularLives& owner) {
|
||||
return std::ranges::all_of(owner.lives(), [](const auto& life) {
|
||||
return life.account == LifeAccount{} || life.presence == CellularPresence::suspended;
|
||||
});
|
||||
}
|
||||
std::expected<std::uint32_t, CellularMigrationError> extent(const CellularLives& owner,
|
||||
const CellularOwnedLife& life) {
|
||||
const auto genome = owner.archive().find(life.identity.genome);
|
||||
if (!genome)
|
||||
return std::unexpected(CellularMigrationError::archive);
|
||||
const auto budget = biology::metabolic_budget(*genome, life.growth,
|
||||
{owner.ambient_temperature_millikelvin(), {}});
|
||||
if (!budget)
|
||||
return std::unexpected(CellularMigrationError::archive);
|
||||
return budget->phenotype.extent;
|
||||
}
|
||||
bool overlaps(biology::CellularPosition a, std::uint32_t size_a, biology::CellularPosition b,
|
||||
std::uint32_t size_b) {
|
||||
return a.x_um < b.x_um + size_b && b.x_um < a.x_um + size_a && a.y_um < b.y_um + size_b &&
|
||||
b.y_um < a.y_um + size_a;
|
||||
}
|
||||
} // namespace
|
||||
std::expected<CellularMigrationExecution, CellularMigrationError>
|
||||
CellularMigration::prepare(const CellularLives& source, const CellularLives& destination,
|
||||
std::uint64_t genome) {
|
||||
if (source.format_version() != cellular_lives_migration_format ||
|
||||
destination.format_version() != cellular_lives_migration_format ||
|
||||
source.rules_version() != cellular_lives_foraging_rules ||
|
||||
destination.rules_version() != cellular_lives_foraging_rules)
|
||||
return std::unexpected(CellularMigrationError::version);
|
||||
if (genome == 0 || source.world() != destination.world() ||
|
||||
source.region() > cellular_bud_max_region ||
|
||||
destination.region() > cellular_bud_max_region || source.region() == destination.region() ||
|
||||
source.frame() != destination.frame() || source.area() != destination.area() ||
|
||||
source.ambient_temperature_millikelvin() != destination.ambient_temperature_millikelvin())
|
||||
return std::unexpected(CellularMigrationError::identity);
|
||||
if (source.tick() == 0 || source.tick() != destination.tick())
|
||||
return std::unexpected(CellularMigrationError::tick);
|
||||
if (!suspended(source) || !suspended(destination))
|
||||
return std::unexpected(CellularMigrationError::ineligible);
|
||||
for (const auto& life : source.lives())
|
||||
if (std::ranges::find(destination.lives(), life.identity.life, [](const auto& other) {
|
||||
return other.identity.life;
|
||||
}) != destination.lives().end())
|
||||
return std::unexpected(CellularMigrationError::identity);
|
||||
std::vector<CellularOwnedLife> moving, remaining;
|
||||
moving.reserve(source.lives().size());
|
||||
remaining.reserve(source.lives().size());
|
||||
std::vector<std::uint64_t> members;
|
||||
members.reserve(source.lives().size());
|
||||
accounting::Inventory transferred;
|
||||
for (const auto& life : source.lives()) {
|
||||
if (life.account == LifeAccount{} && life.identity.genome == genome) {
|
||||
moving.push_back(life);
|
||||
members.push_back(life.identity.life);
|
||||
if (!add(transferred, life.body) || !add(transferred, life.reserve))
|
||||
return std::unexpected(CellularMigrationError::overflow);
|
||||
} else
|
||||
remaining.push_back(life);
|
||||
}
|
||||
if (moving.empty())
|
||||
return std::unexpected(CellularMigrationError::ineligible);
|
||||
if (destination.lives().size() + moving.size() > biology::max_cellular_tick_lives)
|
||||
return std::unexpected(CellularMigrationError::capacity);
|
||||
const auto area = destination.area();
|
||||
for (const auto& life : moving) {
|
||||
const auto size = extent(source, life);
|
||||
if (!size)
|
||||
return std::unexpected(size.error());
|
||||
if (*size == 0 || *size > area.width_um || *size > area.height_um ||
|
||||
life.position.x_um > area.width_um - *size ||
|
||||
life.position.y_um > area.height_um - *size)
|
||||
return std::unexpected(CellularMigrationError::placement);
|
||||
for (const auto& other : destination.lives()) {
|
||||
if (other.presence == CellularPresence::suspended)
|
||||
continue;
|
||||
const auto other_size = extent(destination, other);
|
||||
if (!other_size)
|
||||
return std::unexpected(other_size.error());
|
||||
if (overlaps(life.position, *size, other.position, *other_size))
|
||||
return std::unexpected(CellularMigrationError::placement);
|
||||
}
|
||||
}
|
||||
auto merged = destination.archive().merge_living_from(source.archive(), members);
|
||||
if (!merged)
|
||||
return std::unexpected(merged.error() == biology::HistoryError::archive_full
|
||||
? CellularMigrationError::capacity
|
||||
: CellularMigrationError::archive);
|
||||
auto source_ledger = source.boundary_ledger(),
|
||||
destination_ledger = destination.boundary_ledger();
|
||||
if (!add(source_ledger.outgoing, transferred) || !add(destination_ledger.incoming, transferred))
|
||||
return std::unexpected(CellularMigrationError::overflow);
|
||||
source_ledger.nonresident_organisms.insert(source_ledger.nonresident_organisms.end(),
|
||||
members.begin(), members.end());
|
||||
std::ranges::sort(source_ledger.nonresident_organisms);
|
||||
std::erase_if(destination_ledger.nonresident_organisms,
|
||||
[&](auto id) { return std::ranges::binary_search(members, id); });
|
||||
std::vector<CellularOwnedLife> received{destination.lives().begin(), destination.lives().end()};
|
||||
received.reserve(destination.lives().size() + moving.size());
|
||||
received.insert(received.end(), moving.begin(), moving.end());
|
||||
std::ranges::sort(received, {}, [](const auto& life) { return life.identity.life; });
|
||||
auto after_source = source.migration_replacement(std::move(remaining), source.archive(),
|
||||
std::move(source_ledger));
|
||||
if (!after_source)
|
||||
return std::unexpected(after_source.error() == CellularLivesError::overflow
|
||||
? CellularMigrationError::overflow
|
||||
: CellularMigrationError::conservation);
|
||||
auto after_destination = destination.migration_replacement(std::move(received), *merged,
|
||||
std::move(destination_ledger));
|
||||
if (!after_destination)
|
||||
return std::unexpected(after_destination.error() == CellularLivesError::overflow
|
||||
? CellularMigrationError::overflow
|
||||
: CellularMigrationError::conservation);
|
||||
return CellularMigrationExecution{genome, std::move(members), transferred,
|
||||
std::move(*after_source), std::move(*after_destination)};
|
||||
}
|
||||
|
||||
std::expected<CellularPopulationMigrationExecution, CellularMigrationError>
|
||||
CellularMigration::prepare(const CellularPopulation& source, const CellularPopulation& destination,
|
||||
std::uint64_t genome) {
|
||||
auto biological = prepare(source.state(), destination.state(), genome);
|
||||
if (!biological)
|
||||
return std::unexpected(biological.error());
|
||||
auto after_source = CellularPopulation::create(std::move(biological->source), source.policy());
|
||||
auto after_destination =
|
||||
CellularPopulation::create(std::move(biological->destination), destination.policy());
|
||||
if (!after_source || !after_destination)
|
||||
return std::unexpected(CellularMigrationError::version);
|
||||
(*after_source)->members_.reserve(source.members().size());
|
||||
(*after_destination)->members_ = destination.members_;
|
||||
(*after_destination)->members_.reserve(destination.members().size() + source.members().size());
|
||||
for (const auto member : source.members()) {
|
||||
if (std::ranges::binary_search(biological->members, member.life))
|
||||
(*after_destination)->members_.push_back(member);
|
||||
else
|
||||
(*after_source)->members_.push_back(member);
|
||||
}
|
||||
std::ranges::sort((*after_destination)->members_);
|
||||
(*after_source)->conversion_sequence_ = source.conversion_sequence();
|
||||
(*after_destination)->conversion_sequence_ = destination.conversion_sequence();
|
||||
return CellularPopulationMigrationExecution{genome, std::move(biological->members),
|
||||
biological->transferred, std::move(*after_source),
|
||||
std::move(*after_destination)};
|
||||
}
|
||||
} // namespace evo::server
|
||||
@@ -15,6 +15,8 @@ CellularPopulation::CellularPopulation(std::unique_ptr<CellularLives> state,
|
||||
std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
|
||||
CellularPopulation::create(std::unique_ptr<CellularLives> state, CellularTierPolicy policy) {
|
||||
if (!state || state->rules_version() != cellular_lives_foraging_rules ||
|
||||
(state->format_version() != cellular_lives_presence_format &&
|
||||
state->format_version() != cellular_lives_migration_format) ||
|
||||
policy.materialize_radius_um == 0 ||
|
||||
policy.collapse_radius_um <= policy.materialize_radius_um ||
|
||||
policy.collapse_radius_um > biology::cellular_motion_axis_limit_um)
|
||||
@@ -36,6 +38,21 @@ CellularTierPolicy CellularPopulation::policy() const noexcept {
|
||||
std::uint64_t CellularPopulation::conversion_sequence() const noexcept {
|
||||
return conversion_sequence_;
|
||||
}
|
||||
std::uint32_t CellularPopulation::format_version() const noexcept {
|
||||
return state_->format_version() == cellular_lives_migration_format ? 2U : 1U;
|
||||
}
|
||||
std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
|
||||
CellularPopulation::migration_upgrade_candidate() const {
|
||||
auto biological = state_->migration_upgrade_candidate();
|
||||
if (!biological)
|
||||
return std::unexpected(CellularPopulationError::unsupported);
|
||||
auto candidate = create(std::move(*biological), policy_);
|
||||
if (!candidate)
|
||||
return std::unexpected(candidate.error());
|
||||
(*candidate)->members_ = members_;
|
||||
(*candidate)->conversion_sequence_ = conversion_sequence_;
|
||||
return candidate;
|
||||
}
|
||||
std::expected<std::size_t, CellularPopulationError>
|
||||
CellularPopulation::collapse(std::uint64_t genome) {
|
||||
auto candidate = members_;
|
||||
|
||||
@@ -117,6 +117,10 @@ unattended_population_inputs(const CellularLives& base, const CellularBatchHeade
|
||||
std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
|
||||
execute_population_batch(const CellularPopulation& base, CellularPopulationBatch& batch,
|
||||
bool record) {
|
||||
// Trace 1 is retained for envelope 1. A future durable migration adapter must
|
||||
// bind the new owner/envelope tuple explicitly before recording its history.
|
||||
if (base.format_version() != 1)
|
||||
return std::unexpected(CellularPopulationError::unsupported);
|
||||
if (!shape(batch, record) || batch.header.world != base.state().world() ||
|
||||
batch.header.region != base.state().region() ||
|
||||
batch.header.frame != base.state().frame() ||
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
|
||||
namespace evo::server {
|
||||
namespace {
|
||||
constexpr std::uint64_t magic = 0x31504F504F564541ULL; // "AEVOPOP1"
|
||||
constexpr std::uint64_t magic = 0x31504F504F564541ULL; // "AEVOPOP1"
|
||||
constexpr std::uint64_t migration_magic = 0x32504F504F564541ULL; // "AEVOPOP2"
|
||||
std::uint64_t checksum(std::span<const std::uint8_t> bytes) {
|
||||
std::uint64_t hash = 14695981039346656037ULL;
|
||||
for (const auto byte : bytes)
|
||||
@@ -38,8 +39,8 @@ 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, format_version() == 2 ? migration_magic : magic, 8);
|
||||
put(bytes, format_version(), 4);
|
||||
put(bytes, policy_.automatic ? 1 : 0, 4);
|
||||
put(bytes, policy_.materialize_radius_um, 4);
|
||||
put(bytes, policy_.collapse_radius_um, 4);
|
||||
@@ -66,9 +67,11 @@ CellularPopulation::restore(std::span<const std::uint8_t> bytes, std::uint64_t w
|
||||
if (tail.get(8) != checksum(bytes.first(bytes.size() - 8)))
|
||||
return std::unexpected(CellularPopulationError::checksum);
|
||||
Reader reader(bytes);
|
||||
if (reader.get(8) != magic)
|
||||
const auto saved_magic = reader.get(8);
|
||||
if (saved_magic != magic && saved_magic != migration_magic)
|
||||
return std::unexpected(CellularPopulationError::format);
|
||||
if (reader.get(4) != 1)
|
||||
const auto version = reader.get(4);
|
||||
if ((saved_magic == magic && version != 1) || (saved_magic == migration_magic && version != 2))
|
||||
return std::unexpected(CellularPopulationError::unsupported);
|
||||
const auto flags = reader.get(4);
|
||||
if (flags > 1)
|
||||
@@ -104,6 +107,8 @@ CellularPopulation::restore(std::span<const std::uint8_t> bytes, std::uint64_t w
|
||||
auto candidate = create(std::move(*biological), policy);
|
||||
if (!candidate)
|
||||
return std::unexpected(candidate.error());
|
||||
if ((*candidate)->format_version() != version)
|
||||
return std::unexpected(CellularPopulationError::unsupported);
|
||||
for (const auto& member : members) {
|
||||
const auto life = std::ranges::find((*candidate)->state().lives(), member.life,
|
||||
[](const auto& cell) { return cell.identity.life; });
|
||||
|
||||
@@ -0,0 +1,460 @@
|
||||
#include "evo/server/lifecycle/cellular_migration.hpp"
|
||||
#include "evo/server/lifecycle/cellular_population_batch.hpp"
|
||||
#include "fixtures/cellular_ecology.hpp"
|
||||
|
||||
#include <limits>
|
||||
|
||||
namespace {
|
||||
using namespace evo;
|
||||
using namespace evo::server;
|
||||
using namespace evo::fixture;
|
||||
Inventory plus(Inventory a, const Inventory& b) {
|
||||
const auto maximum = std::numeric_limits<std::uint64_t>::max();
|
||||
require(b.energy.value <= maximum - a.energy.value, "test energy overflow");
|
||||
a.energy.value += b.energy.value;
|
||||
for (std::size_t c = 0; c < a.matter.size(); ++c) {
|
||||
require(b.matter[c].value <= maximum - a.matter[c].value, "test matter overflow");
|
||||
a.matter[c].value += b.matter[c].value;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
void step(CellularLives& owner, bool freeze = true) {
|
||||
auto intents = checked(wildlife_intents(owner, owner.tick() + 1), "migration fixture wildlife");
|
||||
for (const auto& life : owner.lives()) {
|
||||
if (life.account == LifeAccount{})
|
||||
continue;
|
||||
CellularLifeIntent intent{life.identity.life, {}};
|
||||
intent.intent.frozen = freeze;
|
||||
if (!freeze)
|
||||
intent.intent.bud = (owner.tick() + 1 + life.identity.life) % 32 == 0;
|
||||
intents.push_back(intent);
|
||||
}
|
||||
std::ranges::sort(intents, {}, &CellularLifeIntent::life_id);
|
||||
checked(owner.advance_tick(intents), "migration fixture tick");
|
||||
}
|
||||
Owner funded(bool suspend = true, std::uint32_t founders = 4, std::uint64_t region = 1) {
|
||||
auto owner = opening(1, true, founders, true, region);
|
||||
const auto wildlife = [&] {
|
||||
return std::ranges::any_of(owner->lives(),
|
||||
[](const auto& life) { return life.account == LifeAccount{}; });
|
||||
};
|
||||
for (unsigned attempts = 0; attempts < 8192 && !wildlife(); ++attempts)
|
||||
step(*owner, false);
|
||||
require(wildlife(), "migration fixture has no funded wildlife");
|
||||
if (suspend)
|
||||
for (std::uint64_t i = 0; i <= cellular_linger_ticks; ++i)
|
||||
step(*owner);
|
||||
return owner;
|
||||
}
|
||||
Owner destination(const CellularLives& source, std::uint32_t founders = 0, std::uint64_t world = 0,
|
||||
bool food = false) {
|
||||
const auto endowment = checked(biology::primitive_endowment(biology::primitive_founder_v3),
|
||||
"destination endowment");
|
||||
Inventory funding;
|
||||
for (std::uint32_t i = 0; i < founders; ++i) {
|
||||
funding = plus(plus(funding, endowment.body), endowment.reserve);
|
||||
funding = plus(funding, {{endowment.construction.value}, {}});
|
||||
}
|
||||
std::vector<SpawnSite> sites;
|
||||
sites.reserve(std::max(founders, 1U));
|
||||
for (std::uint32_t i = 0; i < std::max(founders, 1U); ++i)
|
||||
sites.push_back({1'000 + static_cast<std::int32_t>(i / 16) * 3'000,
|
||||
1'000 + static_cast<std::int32_t>(i % 16) * 3'000});
|
||||
const auto id = world == 0 ? source.world() : world;
|
||||
const auto region = source.region() == 1 ? 2ULL : 1ULL;
|
||||
auto primitive =
|
||||
checked(PrimitiveLives::create({id, region, source.frame(), 27, funding, sites}),
|
||||
"destination nursery");
|
||||
const auto area = source.area();
|
||||
biology::CellularHabitatConfig habitat{id,
|
||||
region,
|
||||
source.frame(),
|
||||
static_cast<std::uint32_t>(area.width_um),
|
||||
static_cast<std::uint32_t>(area.height_um),
|
||||
{}};
|
||||
habitat.format = biology::cellular_habitat_deposit_format;
|
||||
habitat.next_deposit = 1;
|
||||
if (food) {
|
||||
// Independently funded finite destination food, placed at the incoming
|
||||
// bodies so reproduction tests do not depend on a route to old pools.
|
||||
for (const auto& life : source.lives())
|
||||
for (std::uint32_t c = 0; c < 5; ++c) {
|
||||
biology::CellularPatch mote;
|
||||
mote.address = {static_cast<std::uint32_t>(life.position.x_um) + 1 + 2 * c,
|
||||
static_cast<std::uint32_t>(life.position.y_um) + 1};
|
||||
mote.id = habitat.next_deposit++;
|
||||
mote.source = 28;
|
||||
mote.radius_um = biology::cellular_mote_radius_um;
|
||||
mote.kind = biology::CellularDepositKind::mote;
|
||||
if (c == 0)
|
||||
mote.stock.energy.value = 1'000'000'000'000ULL;
|
||||
else
|
||||
mote.stock.matter[c - 1].value = 1'000'000'000'000ULL;
|
||||
habitat.patches.push_back(mote);
|
||||
}
|
||||
std::ranges::sort(habitat.patches, {}, &biology::CellularPatch::address);
|
||||
}
|
||||
auto owner = checked(
|
||||
CellularLives::create(
|
||||
*primitive, checked(biology::CellularHabitat::create(habitat), "destination habitat"),
|
||||
{area, 0, source.ambient_temperature_millikelvin()}, CellularLivesProfile::foraging),
|
||||
"destination owner");
|
||||
const auto birth = source.tick() - (founders == 0 ? 0 : cellular_linger_ticks + 1);
|
||||
while (owner->tick() < birth)
|
||||
step(*owner);
|
||||
for (std::uint32_t i = 0; i < founders; ++i) {
|
||||
LifeRequest request{};
|
||||
request.account.front() = 0xF1;
|
||||
request.account[14] = static_cast<std::uint8_t>((i + 1) >> 8U);
|
||||
request.account.back() = static_cast<std::uint8_t>(i + 1);
|
||||
request.action = 1;
|
||||
const auto identity = 1'000'000'000'000ULL + i;
|
||||
owner = checked(owner->birth_candidate(
|
||||
request, {identity, identity, identity, identity, {owner->tick()}}),
|
||||
"destination funded birth");
|
||||
}
|
||||
while (owner->tick() < source.tick())
|
||||
step(*owner);
|
||||
require(owner->tick() == source.tick(), "destination cut mismatch");
|
||||
return owner;
|
||||
}
|
||||
std::uint64_t wildlife_genome(const CellularLives& owner) {
|
||||
const auto found = std::ranges::find_if(
|
||||
owner.lives(), [](const auto& life) { return life.account == LifeAccount{}; });
|
||||
require(found != owner.lives().end(), "no migration genome");
|
||||
return found->identity.genome;
|
||||
}
|
||||
void put(std::vector<std::uint8_t>& bytes, std::size_t offset, std::uint64_t value,
|
||||
unsigned width = 8) {
|
||||
for (unsigned i = 0; i < width; ++i)
|
||||
bytes[offset + i] = static_cast<std::uint8_t>(value >> (8U * i));
|
||||
}
|
||||
void checksum(std::vector<std::uint8_t>& bytes) {
|
||||
std::uint64_t value = 14695981039346656037ULL;
|
||||
for (const auto b : std::span{bytes}.first(bytes.size() - 8))
|
||||
value = (value ^ b) * 1099511628211ULL;
|
||||
put(bytes, bytes.size() - 8, value);
|
||||
}
|
||||
Owner decode(const CellularLives& trusted, const std::vector<std::uint8_t>& bytes) {
|
||||
return checked(CellularLives::restore(bytes, trusted.world(), trusted.region(), trusted.frame(),
|
||||
trusted.tick()),
|
||||
"migration owner restore");
|
||||
}
|
||||
void upgrade_and_move() {
|
||||
auto original = funded();
|
||||
auto other = destination(*original, 1);
|
||||
const auto original_bytes = original->snapshot();
|
||||
auto source = checked(original->migration_upgrade_candidate(), "source explicit upgrade");
|
||||
auto target = checked(other->migration_upgrade_candidate(), "destination explicit upgrade");
|
||||
require(source->format_version() == 7 && source->rules_version() == 8 &&
|
||||
source->snapshot().size() == original_bytes.size() + 88,
|
||||
"upgrade rewrote biological format");
|
||||
require(std::ranges::equal(source->lives(), original->lives()) &&
|
||||
source->archive().encode() == original->archive().encode() &&
|
||||
source->habitat().snapshot() == original->habitat().snapshot() &&
|
||||
source->opening_total() == original->opening_total() &&
|
||||
source->boundary_ledger() == CellularBoundaryLedger{} &&
|
||||
original->snapshot() == original_bytes,
|
||||
"upgrade changed biology/funding");
|
||||
const auto source_before = source->snapshot(), target_before = target->snapshot();
|
||||
const auto total = plus(source->current_total(), target->current_total());
|
||||
const auto genome = wildlife_genome(*source);
|
||||
auto moved = checked(CellularMigration::prepare(*source, *target, genome), "exact migration");
|
||||
require(!moved.members.empty() && moved.source->tick() == source->tick() &&
|
||||
moved.destination->tick() == target->tick(),
|
||||
"migration aged biology");
|
||||
require(source->snapshot() == source_before && target->snapshot() == target_before,
|
||||
"migration mutated an input");
|
||||
require(plus(moved.source->current_total(), moved.destination->current_total()) == total &&
|
||||
moved.source->opening_total() == source->opening_total() &&
|
||||
moved.destination->opening_total() == target->opening_total(),
|
||||
"migration lost stock or rebased opening");
|
||||
require(moved.source->archive().encode() == source->archive().encode() &&
|
||||
moved.source->boundary_ledger().nonresident_organisms == moved.members,
|
||||
"departure erased history or recorded death");
|
||||
for (const auto id : moved.members) {
|
||||
const auto before = std::ranges::find(source->lives(), id,
|
||||
[](const auto& life) { return life.identity.life; });
|
||||
const auto after = std::ranges::find(moved.destination->lives(), id,
|
||||
[](const auto& life) { return life.identity.life; });
|
||||
require(before != source->lives().end() && after != moved.destination->lives().end() &&
|
||||
*before == *after,
|
||||
"member was relabeled/healed/resampled");
|
||||
require(moved.destination->archive().organism(id) == source->archive().organism(id),
|
||||
"birth history changed");
|
||||
}
|
||||
require(moved.destination->next_bud_sequence() == target->next_bud_sequence(),
|
||||
"import consumed a local bud identity");
|
||||
auto source_after = decode(*moved.source, moved.source->snapshot());
|
||||
auto target_after = decode(*moved.destination, moved.destination->snapshot());
|
||||
const auto members = moved.members;
|
||||
for (unsigned turn = 0; turn < 100; ++turn) {
|
||||
const bool back = turn % 2 == 0;
|
||||
auto next =
|
||||
checked(CellularMigration::prepare(back ? *target_after : *source_after,
|
||||
back ? *source_after : *target_after, genome),
|
||||
"migration return trip");
|
||||
if (back) {
|
||||
source_after = std::move(next.destination);
|
||||
target_after = std::move(next.source);
|
||||
} else {
|
||||
source_after = std::move(next.source);
|
||||
target_after = std::move(next.destination);
|
||||
}
|
||||
source_after = decode(*source_after, source_after->snapshot());
|
||||
target_after = decode(*target_after, target_after->snapshot());
|
||||
require(plus(source_after->current_total(), target_after->current_total()) == total,
|
||||
"return trip duplicated inventory");
|
||||
}
|
||||
// 101 total moves: the group remains at the destination with every body exact.
|
||||
require(source_after->boundary_ledger().nonresident_organisms == members,
|
||||
"nonresident history changed on oscillation");
|
||||
step(*source_after);
|
||||
step(*target_after);
|
||||
require(plus(source_after->current_total(), target_after->current_total()) == total,
|
||||
"continued biology broke boundary accounting");
|
||||
bool died = false;
|
||||
for (unsigned i = 0; i < 8192 && !died; ++i) {
|
||||
step(*target_after);
|
||||
died = std::ranges::any_of(members, [&](auto id) {
|
||||
return !checked(target_after->archive().organism(id), "imported organism").alive;
|
||||
});
|
||||
}
|
||||
require(died, "imported wildlife failed to die without food");
|
||||
target_after = decode(*target_after, target_after->snapshot());
|
||||
require(target_after->current_total() == moved.destination->current_total(),
|
||||
"death/decay spent boundary history as stock");
|
||||
std::cout << "migration members=" << members.size() << " source_bytes=" << source_before.size()
|
||||
<< " target_bytes=" << target_before.size() << " oscillations=100\n";
|
||||
}
|
||||
void population_envelope() {
|
||||
auto population = checked(CellularPopulation::create(funded()), "population before migration");
|
||||
const auto genome = wildlife_genome(population->state());
|
||||
checked(population->collapse(genome), "cohort before upgrade");
|
||||
const auto old = population->snapshot();
|
||||
auto next = checked(population->migration_upgrade_candidate(), "population explicit upgrade");
|
||||
require(next->format_version() == 2 && population->format_version() == 1 &&
|
||||
population->snapshot() == old &&
|
||||
std::ranges::equal(next->members(), population->members()) &&
|
||||
next->conversion_sequence() == population->conversion_sequence() &&
|
||||
next->policy() == population->policy(),
|
||||
"population upgrade lost tags/policy or changed its input");
|
||||
const auto bytes = next->snapshot();
|
||||
next = checked(CellularPopulation::restore(bytes, next->state().world(), next->state().region(),
|
||||
next->state().frame(), next->state().tick()),
|
||||
"population-2 restore");
|
||||
require(next->snapshot() == bytes, "population-2 roundtrip changed");
|
||||
auto bad = bytes;
|
||||
bad[7] = '1';
|
||||
put(bad, 8, 1, 4);
|
||||
checksum(bad);
|
||||
require(!CellularPopulation::restore(bad, next->state().world(), next->state().region(),
|
||||
next->state().frame(), next->state().tick()),
|
||||
"new owner admitted as historical envelope 1");
|
||||
CellularPopulationBatch old_trace;
|
||||
require(!execute_population_batch(*next, old_trace, true),
|
||||
"old durable trace admitted new owner tuple");
|
||||
auto peer = checked(
|
||||
CellularPopulation::create(checked(
|
||||
destination(next->state())->migration_upgrade_candidate(), "cohort peer upgrade")),
|
||||
"cohort peer");
|
||||
const auto peer_before = peer->snapshot();
|
||||
auto migrated = checked(CellularMigration::prepare(*next, *peer, genome), "cohort transfer");
|
||||
require(migrated.source->members().empty() &&
|
||||
std::ranges::equal(migrated.destination->members(), next->members()) &&
|
||||
migrated.source->conversion_sequence() == next->conversion_sequence() &&
|
||||
migrated.destination->conversion_sequence() == peer->conversion_sequence() &&
|
||||
migrated.source->policy() == next->policy() &&
|
||||
migrated.destination->policy() == peer->policy() && next->snapshot() == bytes &&
|
||||
peer->snapshot() == peer_before,
|
||||
"migration changed representation, policy, sequence or input");
|
||||
auto restored =
|
||||
checked(CellularPopulation::restore(migrated.destination->snapshot(), peer->state().world(),
|
||||
peer->state().region(), peer->state().frame(),
|
||||
peer->state().tick()),
|
||||
"migrated cohort restore");
|
||||
require(std::ranges::equal(restored->members(), next->members()),
|
||||
"cohort tags lost on restore");
|
||||
checked(next->materialize(genome), "new envelope materialization");
|
||||
std::vector<CellularLifeIntent> controlled;
|
||||
for (const auto& life : next->state().lives())
|
||||
if (life.account != LifeAccount{}) {
|
||||
CellularLifeIntent frozen{life.identity.life, {}};
|
||||
frozen.intent.frozen = true;
|
||||
controlled.push_back(frozen);
|
||||
}
|
||||
checked(next->advance_tick(controlled), "new envelope continued biology");
|
||||
}
|
||||
void overlap_refusal() {
|
||||
auto raw = funded(false);
|
||||
for (unsigned i = 0; i < 128; ++i)
|
||||
step(*raw, false);
|
||||
for (std::uint64_t i = 0; i <= cellular_linger_ticks; ++i)
|
||||
step(*raw);
|
||||
auto source = checked(raw->migration_upgrade_candidate(), "overlap source upgrade");
|
||||
auto target = checked(destination(*source)->migration_upgrade_candidate(), "overlap peer");
|
||||
const auto first = wildlife_genome(*source);
|
||||
auto initial =
|
||||
checked(CellularMigration::prepare(*source, *target, first), "overlap first group");
|
||||
source = std::move(initial.source);
|
||||
target = std::move(initial.destination);
|
||||
const auto second = wildlife_genome(*source);
|
||||
require(second != first, "overlap fixture lacks an unrelated group");
|
||||
const auto occupied = std::ranges::find_if(
|
||||
target->lives(), [](const auto& life) { return life.account == LifeAccount{}; });
|
||||
require(occupied != target->lives().end(), "missing occupied destination");
|
||||
auto bytes = source->snapshot();
|
||||
for (std::size_t i = 0; i < source->lives().size(); ++i)
|
||||
if (source->lives()[i].account == LifeAccount{} &&
|
||||
source->lives()[i].identity.genome == second) {
|
||||
// This fixture has four founding sites; v7 header 308, life stride 504.
|
||||
const auto record = 308 + 4 * 8 + i * 504;
|
||||
put(bytes, record + 72, occupied->position.x_um);
|
||||
put(bytes, record + 80, occupied->position.y_um);
|
||||
}
|
||||
checksum(bytes);
|
||||
source = decode(*source, bytes); // Legal resident coordinates; transfer must test the peer.
|
||||
const auto other = target->snapshot();
|
||||
const auto result = CellularMigration::prepare(*source, *target, second);
|
||||
require(!result && result.error() == CellularMigrationError::placement &&
|
||||
source->snapshot() == bytes && target->snapshot() == other,
|
||||
"overlapping transfer displaced/partially moved cells");
|
||||
}
|
||||
void refusal_and_codec() {
|
||||
auto old = funded();
|
||||
auto raw_target = destination(*old);
|
||||
auto source = checked(old->migration_upgrade_candidate(), "codec source upgrade");
|
||||
auto target = checked(raw_target->migration_upgrade_candidate(), "codec target upgrade");
|
||||
const auto genome = wildlife_genome(*source);
|
||||
const auto before = source->snapshot(), target_before = target->snapshot();
|
||||
require(!CellularMigration::prepare(*old, *target, genome), "legacy owner silently migrated");
|
||||
require(!CellularMigration::prepare(*source, *source, genome), "same owner migrated to itself");
|
||||
require(!CellularMigration::prepare(*source, *target, 0) &&
|
||||
!CellularMigration::prepare(*source, *target, 999999),
|
||||
"absent group migrated");
|
||||
auto late = decode(*target, target_before);
|
||||
step(*late);
|
||||
require(!CellularMigration::prepare(*source, *late, genome),
|
||||
"different biological cuts migrated");
|
||||
auto alien =
|
||||
checked(destination(*source, 0, source->world() + 1)->migration_upgrade_candidate(),
|
||||
"alien upgrade");
|
||||
require(!CellularMigration::prepare(*source, *alien, genome), "world boundary relabeled");
|
||||
auto active = funded(false);
|
||||
auto active_target = destination(*active);
|
||||
active = checked(active->migration_upgrade_candidate(), "active representation upgrade");
|
||||
active_target = checked(active_target->migration_upgrade_candidate(), "active peer upgrade");
|
||||
require(!CellularMigration::prepare(*active, *active_target, wildlife_genome(*active)),
|
||||
"vulnerable controlled life entered transfer");
|
||||
auto bad = before;
|
||||
put(bad, 220, 1);
|
||||
checksum(bad);
|
||||
require(!CellularLives::restore(bad, source->world(), source->region(), source->frame(),
|
||||
source->tick()),
|
||||
"unfunded boundary credit admitted");
|
||||
bad = before;
|
||||
put(bad, 220 + 8, 1);
|
||||
put(bad, 260 + 16, 1);
|
||||
checksum(bad);
|
||||
require(!CellularLives::restore(bad, source->world(), source->region(), source->frame(),
|
||||
source->tick()),
|
||||
"different chemical channels canceled");
|
||||
const auto execution =
|
||||
checked(CellularMigration::prepare(*source, *target, genome), "overflow amount reference");
|
||||
std::array<std::uint64_t, 5> amounts{
|
||||
execution.transferred.energy.value, execution.transferred.matter[0].value,
|
||||
execution.transferred.matter[1].value, execution.transferred.matter[2].value,
|
||||
execution.transferred.matter[3].value};
|
||||
const auto channel = static_cast<std::size_t>(
|
||||
std::ranges::find_if(amounts, [](auto value) { return value != 0; }) - amounts.begin());
|
||||
require(channel < amounts.size(), "zero-stock organism migrated");
|
||||
bad = before;
|
||||
put(bad, 220 + channel * 8, UINT64_MAX);
|
||||
put(bad, 260 + channel * 8, UINT64_MAX);
|
||||
checksum(bad);
|
||||
auto exhausted = decode(*source, bad); // Balanced huge flows never become spendable stock.
|
||||
auto refused = CellularMigration::prepare(*exhausted, *target, genome);
|
||||
require(!refused && refused.error() == CellularMigrationError::overflow, "outflow wrapped");
|
||||
bad = target_before;
|
||||
put(bad, 220 + channel * 8, UINT64_MAX);
|
||||
put(bad, 260 + channel * 8, UINT64_MAX);
|
||||
checksum(bad);
|
||||
auto full = decode(*target, bad);
|
||||
refused = CellularMigration::prepare(*source, *full, genome);
|
||||
require(!refused && refused.error() == CellularMigrationError::overflow, "inflow wrapped");
|
||||
for (const auto offset : {std::size_t{300}, std::size_t{304}}) {
|
||||
bad = before;
|
||||
put(bad, offset, UINT32_MAX, 4);
|
||||
checksum(bad);
|
||||
require(!CellularLives::restore(bad, source->world(), source->region(), source->frame(),
|
||||
source->tick()),
|
||||
"unbounded/reserved migration header admitted");
|
||||
}
|
||||
const auto stride = std::max(std::size_t{1}, before.size() / 32);
|
||||
for (std::size_t length = 0; length < before.size(); length += stride)
|
||||
require(!CellularLives::restore(std::span{before}.first(length), source->world(),
|
||||
source->region(), source->frame(), source->tick()),
|
||||
"truncated migration owner admitted");
|
||||
require(source->snapshot() == before && target->snapshot() == target_before,
|
||||
"rejection modified a peer");
|
||||
}
|
||||
void capacity() {
|
||||
auto raw = funded(true, 1);
|
||||
auto crowded = destination(*raw, 256);
|
||||
auto source = checked(raw->migration_upgrade_candidate(), "capacity source upgrade");
|
||||
auto target = checked(crowded->migration_upgrade_candidate(), "capacity target upgrade");
|
||||
const auto before = source->snapshot(), other = target->snapshot();
|
||||
const auto result = CellularMigration::prepare(*source, *target, wildlife_genome(*source));
|
||||
require(!result && result.error() == CellularMigrationError::capacity &&
|
||||
source->snapshot() == before && target->snapshot() == other,
|
||||
"capacity pressure culled stock or partially moved a group");
|
||||
}
|
||||
void local_budding_among_imports() {
|
||||
auto raw = funded(true, 4, 2);
|
||||
auto source = checked(raw->migration_upgrade_candidate(), "higher namespace source");
|
||||
auto target = checked(destination(*source, 1, 0, true)->migration_upgrade_candidate(),
|
||||
"lower namespace funded destination");
|
||||
auto moved = checked(CellularMigration::prepare(*source, *target, wildlife_genome(*source)),
|
||||
"higher-to-lower namespace migration");
|
||||
target = std::move(moved.destination);
|
||||
const auto opening = target->current_total();
|
||||
const auto next = target->next_bud_sequence();
|
||||
const auto founder = std::ranges::find_if(target->lives(), [](const auto& life) {
|
||||
return life.account != LifeAccount{};
|
||||
})->identity.life;
|
||||
bool local_parent = false;
|
||||
for (unsigned i = 0; i < 8192 && target->next_bud_sequence() == next; ++i) {
|
||||
auto intents = checked(wildlife_intents(*target, target->tick() + 1), "imported intents");
|
||||
CellularLifeIntent controlled{founder, {}};
|
||||
controlled.intent.bud = true;
|
||||
intents.push_back(controlled);
|
||||
std::ranges::sort(intents, {}, &CellularLifeIntent::life_id);
|
||||
checked(target->advance_tick(intents), "local real budding among imports");
|
||||
for (const auto& life : target->lives())
|
||||
if (life.identity.life >= cellular_bud_identity(1, next) &&
|
||||
life.identity.life < cellular_bud_identity(2, 1) && life.parent_life == founder)
|
||||
local_parent = true;
|
||||
}
|
||||
require(local_parent && target->next_bud_sequence() > next &&
|
||||
target->current_total() == opening,
|
||||
"local reproduction among imported IDs failed or spent boundary flows");
|
||||
target = decode(*target, target->snapshot());
|
||||
for (unsigned i = 0; i < 64; ++i)
|
||||
step(*target);
|
||||
require(std::ranges::is_sorted(target->lives(), {},
|
||||
[](const auto& life) { return life.identity.life; }),
|
||||
"local children displaced foreign namespace ordering");
|
||||
}
|
||||
} // namespace
|
||||
int main() try {
|
||||
upgrade_and_move();
|
||||
refusal_and_codec();
|
||||
capacity();
|
||||
population_envelope();
|
||||
overlap_refusal();
|
||||
local_budding_among_imports();
|
||||
return 0;
|
||||
} catch (const std::exception& error) {
|
||||
std::cerr << error.what() << '\n';
|
||||
return 1;
|
||||
}
|
||||
Vendored
+7
-7
@@ -44,8 +44,8 @@ inline Owner restore(const CellularLives& state) {
|
||||
"reference restore");
|
||||
}
|
||||
|
||||
inline Owner opening(std::uint64_t seed, bool rich, std::uint32_t count = 4,
|
||||
bool populated = true) {
|
||||
inline Owner opening(std::uint64_t seed, bool rich, std::uint32_t count = 4, bool populated = true,
|
||||
std::uint64_t region = 1) {
|
||||
require(count > 0 && count <= biology::max_cellular_tick_lives, "fixture population bound");
|
||||
const std::uint32_t side = count == 4 ? 6'000 : 48'000;
|
||||
const auto world = 200 + seed;
|
||||
@@ -61,8 +61,8 @@ inline Owner opening(std::uint64_t seed, bool rich, std::uint32_t count = 4,
|
||||
for (std::int32_t index = 0; index < static_cast<std::int32_t>(count); ++index)
|
||||
sites.push_back({1'000 + (index / 16) * 3'000, 1'000 + (index % 16) * 3'000});
|
||||
}
|
||||
auto primitive =
|
||||
checked(PrimitiveLives::create({world, 1, 1, 17, funding, sites}), "reference nursery");
|
||||
auto primitive = checked(PrimitiveLives::create({world, region, 1, 17, funding, sites}),
|
||||
"reference nursery");
|
||||
std::vector<biology::CellularPatch> pools;
|
||||
for (const auto& site : sites) {
|
||||
biology::CellularPatch pool;
|
||||
@@ -78,9 +78,9 @@ inline Owner opening(std::uint64_t seed, bool rich, std::uint32_t count = 4,
|
||||
Inventory amount{{rich ? 1'000'000ULL : 10'000ULL}, {}};
|
||||
for (auto& channel : amount.matter)
|
||||
channel.value = rich ? 100'000 : 10;
|
||||
auto config = checked(
|
||||
biology::scatter_deposits({world, 1, 1, side, side, {}}, pools, {64, 19, amount, seed}),
|
||||
"reference deposits");
|
||||
auto config = checked(biology::scatter_deposits({world, region, 1, side, side, {}}, pools,
|
||||
{64, 19, amount, seed}),
|
||||
"reference deposits");
|
||||
if (rich) {
|
||||
// Explicit finite contact food on each founding site. It is not a reserve grant
|
||||
// or replenishment; the owner must acquire and spend it through real biology.
|
||||
|
||||
+79
-1
@@ -1,6 +1,7 @@
|
||||
#include "evo/biology/lineage.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <source_location>
|
||||
@@ -478,9 +479,84 @@ void a_full_archive_round_trips() {
|
||||
CHECK(bytes.size() <= max_archive_legacy_bytes);
|
||||
const auto decoded = LineageArchive::decode(bytes);
|
||||
CHECK(decoded && decoded->encode() == bytes && decoded->recorded() == max_archived_organisms);
|
||||
LineageArchive destination;
|
||||
const std::array<std::uint64_t, 1> selected{8191}; // Alive; carries the deepest genome.
|
||||
const auto imported = destination.merge_living_from(archive, selected);
|
||||
CHECK(imported && imported->recorded() == 1 && imported->archived() == max_archived_genomes);
|
||||
const auto imported_ancestry = imported->ancestry(max_archived_genomes);
|
||||
CHECK(imported_ancestry && imported_ancestry->size() == max_archived_genomes);
|
||||
CHECK(archive.encode() == bytes && destination.recorded() == 0);
|
||||
LineageArchive unrelated;
|
||||
CHECK(unrelated.admit(descend(5'000'000, 0)));
|
||||
CHECK(unrelated.record_birth(5'000'001, 5'000'000, evo::Tick{1}));
|
||||
const auto full_before = imported->encode();
|
||||
const auto overflow =
|
||||
imported->merge_living_from(unrelated, std::array<std::uint64_t, 1>{5'000'001});
|
||||
CHECK(!overflow && overflow.error() == HistoryError::archive_full &&
|
||||
imported->encode() == full_before);
|
||||
std::cout << "lineage_archive_full_bytes=" << bytes.size()
|
||||
<< " max=" << max_archive_legacy_bytes << '\n';
|
||||
}
|
||||
void ownership_import_retains_exact_history() {
|
||||
LineageArchive source;
|
||||
CHECK(source.admit(descend(90, 0)));
|
||||
CHECK(source.record_birth(500, 90, evo::Tick{10}));
|
||||
CHECK(source.record_birth(501, 90, evo::Tick{10}));
|
||||
source.upgrade_to_revision_format();
|
||||
CHECK(source.record_mutation(500, descend(20, 90, 409), evo::Tick{11}));
|
||||
CHECK(source.record_mutation(500, descend(10, 20, 418), evo::Tick{12}));
|
||||
LineageArchive target;
|
||||
CHECK(target.admit(descend(700, 0, 391)));
|
||||
CHECK(target.record_birth(1000, 700, evo::Tick{1}));
|
||||
CHECK(target.record_death(1000, evo::Tick{2}));
|
||||
const auto source_before = source.encode(), target_before = target.encode();
|
||||
const std::array<std::uint64_t, 1> selected{500};
|
||||
const auto merged = target.merge_living_from(source, selected);
|
||||
CHECK(merged && merged->recorded() == 2 && merged->archived() == 4);
|
||||
CHECK(merged->organism(500) == source.organism(500));
|
||||
CHECK(merged->current_genome(500) == source.current_genome(500));
|
||||
CHECK(merged->ancestry(10) == source.ancestry(10));
|
||||
CHECK(merged->revisions().size() == 2 && !merged->organism(501));
|
||||
const auto retained_dead = merged->organism(1000);
|
||||
CHECK(retained_dead && !retained_dead->alive && merged->find(700));
|
||||
CHECK(source.encode() == source_before && target.encode() == target_before);
|
||||
const auto repeated = merged->merge_living_from(source, selected);
|
||||
CHECK(repeated && repeated->encode() == merged->encode());
|
||||
const auto restored = LineageArchive::decode(merged->encode());
|
||||
CHECK(restored && restored->encode() == merged->encode());
|
||||
|
||||
LineageArchive prefix;
|
||||
CHECK(prefix.admit(descend(90, 0)));
|
||||
CHECK(prefix.record_birth(500, 90, evo::Tick{10}));
|
||||
prefix.upgrade_to_revision_format();
|
||||
CHECK(prefix.record_mutation(500, descend(20, 90, 409), evo::Tick{11}));
|
||||
const auto prefix_before = prefix.encode();
|
||||
const auto continued = prefix.merge_living_from(source, selected);
|
||||
CHECK(continued && continued->current_genome(500) == source.current_genome(500));
|
||||
CHECK(!source.merge_living_from(prefix, selected)); // Older evidence cannot rewind history.
|
||||
CHECK(prefix.encode() == prefix_before && source.encode() == source_before);
|
||||
|
||||
LineageArchive dead{*merged};
|
||||
CHECK(dead.record_death(500, evo::Tick{20}));
|
||||
const auto dead_before = dead.encode();
|
||||
CHECK(!dead.merge_living_from(source, selected) && dead.encode() == dead_before);
|
||||
CHECK(!target.merge_living_from(dead, selected));
|
||||
LineageArchive conflict;
|
||||
CHECK(conflict.admit(descend(90, 0, 300)));
|
||||
const auto conflict_before = conflict.encode();
|
||||
CHECK(!conflict.merge_living_from(source, selected) && conflict.encode() == conflict_before);
|
||||
LineageArchive different_birth;
|
||||
CHECK(different_birth.admit(descend(90, 0)));
|
||||
CHECK(different_birth.record_birth(500, 90, evo::Tick{9}));
|
||||
const auto birth_before = different_birth.encode();
|
||||
CHECK(!different_birth.merge_living_from(source, selected) &&
|
||||
different_birth.encode() == birth_before);
|
||||
CHECK(!target.merge_living_from(source, std::array<std::uint64_t, 2>{500, 500}));
|
||||
CHECK(!target.merge_living_from(source, std::array<std::uint64_t, 2>{501, 500}));
|
||||
CHECK(!target.merge_living_from(source, std::array<std::uint64_t, 1>{0}));
|
||||
CHECK(!target.merge_living_from(source, std::array<std::uint64_t, 1>{9999}));
|
||||
CHECK(!target.merge_living_from(source, {}));
|
||||
}
|
||||
} // namespace
|
||||
int main() try {
|
||||
a_genome_outlives_every_organism_that_carried_it();
|
||||
@@ -497,9 +573,11 @@ int main() try {
|
||||
genome_revisions_preserve_birth_and_replay_current_genome();
|
||||
mutation_history_upgrade_and_failures_are_explicit();
|
||||
a_full_archive_round_trips();
|
||||
ownership_import_retains_exact_history();
|
||||
schemas_mix_without_rewriting();
|
||||
std::cout << "PASS lineage retention, branching ancestry and provisional absence\n";
|
||||
return 0;
|
||||
} catch (...) {
|
||||
} catch (const std::exception& error) {
|
||||
std::cerr << error.what() << '\n';
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -158,6 +158,16 @@ void manifests() {
|
||||
"population payload changed");
|
||||
require(!restore_cellular_payload(population->snapshot(), next),
|
||||
"population mislabeled as cellular");
|
||||
auto future = checked(population->migration_upgrade_candidate(), "future portable envelope");
|
||||
auto mislabeled = next;
|
||||
mislabeled.payload_bytes = future->snapshot().size();
|
||||
auto refused = restore_population_payload(future->snapshot(), mislabeled);
|
||||
require(!refused && refused.error() == CheckpointError::unsupported,
|
||||
"portable envelope 2 bypassed durable bundle admission");
|
||||
auto raw_mislabeled = source;
|
||||
raw_mislabeled.payload_bytes = future->state().snapshot().size();
|
||||
require(!restore_cellular_payload(future->state().snapshot(), raw_mislabeled),
|
||||
"portable owner 7 bypassed raw durable bundle admission");
|
||||
auto back = checked(population_profile_transition_manifest(
|
||||
next, filled<32>(11), filled<16>(12), lease, source.build,
|
||||
state->snapshot().size(), filled<32>(13)),
|
||||
|
||||
Reference in New Issue
Block a user