From 51991ac7ff24822c7ddac410a249223b2c8e4291 Mon Sep 17 00:00:00 2001 From: dingo Date: Mon, 19 Oct 2026 17:16:50 -0400 Subject: [PATCH] feat(persistence): admit durable cellular environment profile ADR-0174 and schema 56 add checkpoint profile 6, which stores the joint cell/environment envelope nesting population envelope 2 (bundles 10 and 11). Entry from a committed population head is explicit, one-way and no-tick, with the source commit, manifest and payload digests bound in immutable evidence. Joint intervals use schema 45's owned admission and settlement; conversions cannot overtake a pending interval. The fenced publisher validates the joint trace structurally, including the complete nested population batch, and records per-commit imported and radiated totals. Recovery replays the head's link and requires the payload exactly, refusing a forged but structurally valid trace. A trigger refuses profile-6 commits from every other publisher; profile 4/5 bytes, parsers and rules are unchanged. Co-Authored-By: Claude Opus 5.5 --- CMakeLists.txt | 9 +- .../region/dev-session-route/listener.yaml | 2 +- docs/STATUS.md | 12 +- ...74-durable-cellular-environment-profile.md | 57 +++ docs/adr/README.md | 2 + docs/engineering/M12_DELIVERY_PLAN.md | 2 +- docs/engineering/M12_REMAINING_GATES.md | 2 +- docs/engineering/VALIDATION.md | 45 ++ .../environment_checkpoint_store.hpp | 41 ++ include/evo/persistence/region_checkpoint.hpp | 28 ++ .../evo/persistence/region_lease_store.hpp | 15 + .../0056_cellular_environment_publication.sql | 321 ++++++++++++++ migrations/apply.sql | 19 +- schemas/region-commit.md | 8 + src/persistence/environment_checkpoint.cpp | 85 ++++ .../environment_checkpoint_store.cpp | 249 +++++++++++ src/persistence/region_checkpoint.cpp | 32 +- src/persistence/region_lease_store.cpp | 108 +++-- tests/environment_checkpoint_tests.cpp | 169 ++++++++ tests/environment_publication_tests.cpp | 410 ++++++++++++++++++ tests/postgres_tests.cpp | 6 + tests/region_migration_tests.cpp | 3 +- tools/ci/history-domain-check.sh | 16 +- tools/ci/postgres-check.sh | 17 +- tools/migrate-postgres.sh | 4 +- 25 files changed, 1599 insertions(+), 63 deletions(-) create mode 100644 docs/adr/0174-durable-cellular-environment-profile.md create mode 100644 include/evo/persistence/environment_checkpoint_store.hpp create mode 100644 migrations/0056_cellular_environment_publication.sql create mode 100644 src/persistence/environment_checkpoint.cpp create mode 100644 src/persistence/environment_checkpoint_store.cpp create mode 100644 tests/environment_checkpoint_tests.cpp create mode 100644 tests/environment_publication_tests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f39cc88..b7aa59f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -130,7 +130,7 @@ target_compile_features(evo_ops PUBLIC cxx_std_23) evo_owned_target(evo_ops) # Portable checkpoint manifest codec and environmental-v1 payload validation. add_library(evo_region_checkpoint STATIC src/persistence/region_checkpoint.cpp src/persistence/region_batch.cpp - src/persistence/population_checkpoint.cpp) + src/persistence/population_checkpoint.cpp src/persistence/environment_checkpoint.cpp) add_library(evo::region_checkpoint ALIAS evo_region_checkpoint) target_link_libraries(evo_region_checkpoint PUBLIC evo::region_lease evo::processes evo::lifecycle) evo_owned_target(evo_region_checkpoint) @@ -258,6 +258,7 @@ if(EVO_BUILD_POSTGRES) add_library(evo_region_store STATIC src/persistence/sha256.cpp src/persistence/object_store.cpp src/persistence/checkpoint_store.cpp src/persistence/primitive_checkpoint_store.cpp src/persistence/cellular_checkpoint_store.cpp src/persistence/population_checkpoint_store.cpp + src/persistence/environment_checkpoint_store.cpp src/persistence/cellular_history_store.cpp src/persistence/cellular_census_snapshot.cpp src/persistence/region_clone.cpp src/persistence/cellular_migration_store.cpp) @@ -311,7 +312,7 @@ if(EVO_BUILD_POSTGRES) # Contention tests use one thread and one connection per contender. add_executable(evo-postgres-tests tests/postgres_tests.cpp tests/region_lease_store_tests.cpp tests/cellular_publication_tests.cpp - tests/population_publication_tests.cpp + tests/population_publication_tests.cpp tests/environment_publication_tests.cpp tests/fixtures/postgres_relay.cpp tests/account_control_store_tests.cpp tests/probe_handoff_store_tests.cpp tests/life_reservation_store_tests.cpp @@ -793,6 +794,10 @@ if(BUILD_TESTING) target_link_libraries(evo-population-checkpoint-tests PRIVATE evo::region_checkpoint) evo_owned_target(evo-population-checkpoint-tests) add_test(NAME population-checkpoint-contract COMMAND evo-population-checkpoint-tests) + add_executable(evo-environment-checkpoint-tests tests/environment_checkpoint_tests.cpp) + target_link_libraries(evo-environment-checkpoint-tests PRIVATE evo::region_checkpoint) + evo_owned_target(evo-environment-checkpoint-tests) + add_test(NAME environment-checkpoint-contract COMMAND evo-environment-checkpoint-tests) set_tests_properties(population-checkpoint-contract PROPERTIES TIMEOUT 180) endif() diff --git a/deploy/kubernetes/region/dev-session-route/listener.yaml b/deploy/kubernetes/region/dev-session-route/listener.yaml index e05117c..a05761a 100644 --- a/deploy/kubernetes/region/dev-session-route/listener.yaml +++ b/deploy/kubernetes/region/dev-session-route/listener.yaml @@ -12,7 +12,7 @@ spec: env: - {name: REGION_SESSION, value: "dev"} - {name: REGION_SESSION_LISTEN, value: "0.0.0.0:7400"} - - {name: REGION_SESSION_WINDOW, value: "dev-20261019T201509-a1dcbfd7:1792455309"} + - {name: REGION_SESSION_WINDOW, value: "dev-20261019T210956-a2cdedf2:1792458596"} - {name: REGION_M10_MAX_GRANT_LIFETIME, value: "14400"} ports: # QUIC (ADR-0090): UDP only; nothing listens on TCP 7400. diff --git a/docs/STATUS.md b/docs/STATUS.md index b22a26c..752c9da 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -17,9 +17,15 @@ the unchanged population batch plus each tick's recorded import and radiated hea Recorded unattended intervals equal the same live joint ticks; replay re-derives every flow and refuses forged flows, boundaries and cuts. -Both full Release suites pass **94/94**; see [Validation](engineering/VALIDATION.md). -Producers (ADR-0136 step C), durable admission (12.5c), worker/census/migration -integration (12.5d) and target-host qualification (12.5e) remain open. +[ADR-0174](adr/0174-durable-cellular-environment-profile.md)/schema 56 makes the +joint owner durable as checkpoint profile 6: an explicit one-way, no-tick entry from +a population head, fenced joint publication with owned intervals and per-commit SQL +totals of imported and radiated stock, and recovery that replays the head's link +exactly. A forged but structurally valid trace is refused on recovery. + +See [Validation](engineering/VALIDATION.md). Producers (ADR-0136 step C), +executable/network joint fault drills, worker/census/migration integration (12.5d) +and target-host qualification (12.5e) remain open. ## Portable retained-plan exact populations - 2026-10-19 diff --git a/docs/adr/0174-durable-cellular-environment-profile.md b/docs/adr/0174-durable-cellular-environment-profile.md new file mode 100644 index 0000000..d17dba2 --- /dev/null +++ b/docs/adr/0174-durable-cellular-environment-profile.md @@ -0,0 +1,57 @@ +# ADR-0174: Durable cellular environment profile + +## Status and scope + +Accepted M12.5c durable admission, 2026-10-19. Extends ADR-0142/0143 (population +profile and owned intervals), ADR-0172 (joint owner) and ADR-0173 (joint batch). +Schema 56. No automatic selection, worker scheduling, census/migration support, +default/DEV opening, live upgrade or World0 operation. + +## Decision + +**Checkpoint profile 6** (`cellular_environment_v1`, history profile 6) stores the +complete joint envelope `AEVOCEN1`. It nests population envelope 2 only (owner 7 with +rules 8 or 9: cellular bundles 10 and 11). Bundle 12 retained-plan populations stay +outside until their own durable population admission exists. + +**Entry is explicit, no-tick and one-way.** `migrate_population_to_environment` takes +a committed profile-5 head under the current lease, restores its population, wraps +it with the declared environment and requires the nested population bytes to equal +the source exactly. SQL binds the source commit, manifest digest and payload digest +in `environment_profile_transitions`; the bundle, frame, tick quantum and tick are +unchanged and a pending player mutation or interval blocks it. There is no reverse +transition: unwrapping would discard environment state that has no other owner. +Re-entering profile 5 needs a separately versioned decision. + +**Publication** of a joint interval or whole-group conversion replays the batch from +the committed predecessor before upload and requires the candidate bytes exactly. +`publish_environment_checkpoint` validates the joint trace structurally, including +the complete nested population batch contract bound to the same identity, +predecessor and ticks, and records per-commit totals of imported energy/C/N/P/other +and radiated heat. Intervals require schema 45's owned admission: admission now +accepts profiles 5 and 6 and the same pending pointer and settlement journal apply. +A conversion cannot overtake a pending interval. Exact retries return the original +receipt; a reused identity with other bytes conflicts. + +**Recovery** loads the head, its immutable evidence and its predecessor, then either +verifies the transition's byte-identical nesting or replays the joint trace and +requires the stored payload exactly. SQL flow totals are audit evidence only; a +structurally valid but forged trace that reaches the database through the low-level +publisher is refused on recovery, not trusted. + +Every profile-6 commit requires its evidence: a trigger refuses the generic, raw and +population publishers. History/census/migration readers refuse profile 6 explicitly +until 12.5d integrates them; the region host does not select it. + +## Consequences and qualification + +Joint ownership now survives process and worker replacement with exact replay, +retries and fenced authority, without changing any profile-4/5 byte or rule. The +nested ledger's flows remain complete; recorded environmental totals separate them +from migration flows for audit. Required evidence: durable bundle 9 -> 10 -> 11, +profile 4 -> 5 -> 6, transition retry/conflict/resolution, unadmitted interval +refusal, owned interval with matching SQL totals, conversion blocked by a pending +interval, settled conversion, cohort interval, bypass-publisher refusal, forged-flow +recovery refusal, the upgrade chain and legacy history-domain reconstruction. +Executable/network fault drills, census and migration integration, producers and +target-host qualification remain open. diff --git a/docs/adr/README.md b/docs/adr/README.md index 9caecec..e1083c2 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -1,5 +1,7 @@ # Architecture decision records +- [ADR-0174: Durable cellular environment profile](0174-durable-cellular-environment-profile.md) - schema 56 checkpoint profile 6 nesting population envelope 2; explicit one-way 5 -> 6 entry, fenced joint publication with owned intervals and SQL flow totals, exact replayed recovery. + - [ADR-0173: Cellular environment joint batch](0173-cellular-environment-batch.md) - portable `AEVOEBT1` wraps the unchanged population batch with per-tick recorded environmental flows; unattended recording runs the controller after each exchange and replay re-derives every flow exactly. - [ADR-0172: Cellular environment ownership](0172-cellular-environment-ownership.md) - one owner per patch of space for cells and their local environment; reserves stay individual; open planet boundary with recorded imports/exports; portable envelope `AEVOCEN1` with finite infall entitlements and radiative heat export. diff --git a/docs/engineering/M12_DELIVERY_PLAN.md b/docs/engineering/M12_DELIVERY_PLAN.md index e54c04d..fcefc2d 100644 --- a/docs/engineering/M12_DELIVERY_PLAN.md +++ b/docs/engineering/M12_DELIVERY_PLAN.md @@ -12,7 +12,7 @@ Commit and push each validated scoped segment separately. | 12.2 | Exact local model selected and paired qualification implemented (12.2d); production cost gate open | ADR-0139, exact canonical equality across 196,608 paired ticks per platform. Regional approximation disabled; no compression claim. Deployment-host capacity and fuller interaction coverage remain open. | | 12.3 | Exact population profile/trace/store and opt-in host integration implemented (12.3b) | ADR-0142, schema 44; exact publication/recovery, mutation race guards, ambiguous replies and process kills. Executable/network population fault drills and target-host qualification remain open. | | 12.4 | Owned intervals and durable paired migration/fault qualification implemented in scoped segments through 12.4l | ADR-0143–0149/schema 49; bounded admitted work, source freeze, staged ghost, atomic pair and exact recovery, local TLS and storage faults. Multi-host/orchestrator, linked ecology and integrated stock audits remain open. | -| 12.5 | Ownership accepted; portable joint owner implemented (12.5a-b) | ADR-0172: one owner per patch, tagged water/dissolved compartments, finite infall entitlements and radiative export with exact raw/cohort, refusal and restart evidence. Producers (ADR-0136 step C), durable profile, worker integration and target-host qualification remain open. | +| 12.5 | Ownership accepted; portable joint owner, joint batch and durable profile 6 implemented (12.5a-c) | ADR-0172-0174/schema 56: one owner per patch, tagged water/dissolved compartments, finite infall entitlements and radiative export, exact unattended replay, owned durable intervals and recovery. Producers (ADR-0136 step C), worker/census/migration integration and target-host qualification remain open. | | 12.6 | Regional queries, replayed history, scoped multi-region queries and consistent database head snapshots implemented (12.6a-d) | ADR-0150–0153; short read-only snapshot capture prevents atomic pairs from straddling declared head sets, with exact immutable replay. Durable world/branch coverage, outside transfer/viability reconciliation, target-host query/index costs and client path remain required. | | 12.7 | Evidence policy, registered coverage, history domains and verified pending handoff evidence implemented (12.7a-c) | ADR-0154–0156/schema 52 captures namespace/registry/heads/pending handoffs together, with exact capsule/ghost replay and supported restore forks. Cross-domain clone provenance, other viable-store coverage and durable accepted global reconciliation/freshness remain required. | | 12.8 | Bounded unscripted generation evidence; actual-days gate not qualified | ADR-0162 measures four real wildlife generations with exact raw/cohort recovery, predation and twins. Actual multi-day integrated ecology/restart/clone evidence remains required. | diff --git a/docs/engineering/M12_REMAINING_GATES.md b/docs/engineering/M12_REMAINING_GATES.md index 779c2bf..939a797 100644 --- a/docs/engineering/M12_REMAINING_GATES.md +++ b/docs/engineering/M12_REMAINING_GATES.md @@ -66,7 +66,7 @@ or authorize production enablement. | 12.2 | Exact cellular model and paired matrix; local samples through 256 lives; bounded four-generation rules-9 predation/twin matrix | Deployment-host tail costs including publication; larger wildlife cohorts and fuller contention coverage; useful compression remains unimplemented. | | 12.3 | Dedicated population profile/trace/store/owning host; exact recovery and private PostgreSQL fault tests | Population-specific executable/network fault drills and target-host publication/capacity qualification. | | 12.4 | Owned intervals; exact transfer/accounting; durable atomic pair/recovery; independent loopback TLS path loss and owner expiry/replacement/heal; local object sync/read-failure matrix | Multi-host/kernel-network, storage-host and orchestrator drills, linked-boundary contracts and integrated ownership/stock audits at scale. | -| 12.5 | ADR-0172 portable joint owner: one budget for cells and local environment, effective depth, tagged water/dissolved compartments, refused atmosphere, finite infall entitlements, radiative export; closed/source-off/open/exhaustion and raw/cohort comparisons; ADR-0173 joint batch with exact unattended replay | Producer process and coefficients (ADR-0136 step C), durable profile/trace/recovery (12.5c), worker/census/migration integration and escrowed cross-worker sources (12.5d), target-host qualification (12.5e). | +| 12.5 | ADR-0172 portable joint owner: one budget for cells and local environment, effective depth, tagged water/dissolved compartments, refused atmosphere, finite infall entitlements, radiative export; closed/source-off/open/exhaustion and raw/cohort comparisons; ADR-0173 joint batch with exact unattended replay; ADR-0174/schema 56 durable profile 6 with owned intervals and exact recovery | Producer process and coefficients (ADR-0136 step C), executable/network joint fault drills, worker/census/migration integration and escrowed cross-worker sources (12.5d), target-host qualification (12.5e). | | 12.6 | Retained archive, regional census/lineage queries, replayed history, scoped multi-region queries and consistent database head snapshots (12.6a-d); registered coverage/history domains follow in 12.7a-b | Cross-domain clone provenance, retained historical coverage/other viable stores, target-host query/index qualification and client permission/presentation; no account power or invented precise statistical ancestry. | | 12.7 | Viability/unknown policy; consistent cuts, registered coverage, history domains/restore forks and verified pending handoff evidence (12.7a-c/schema 52) | Cross-domain clone provenance, other viable-store coverage and durable accepted global evidence records/freshness. Missing/stale/unsupported owners must prevent terminal extinction. | | 12.8 | Deterministic seed/regime drivers, separate-process exact continuation and bounded unscripted generations | Reproducible integrated worlds running for actual days, real restarts/clone comparisons and retained accounting/diversity/cost audits. Accelerated tests cannot supply elapsed days. | diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index ac2c6da..60e4f2d 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -1,5 +1,50 @@ # Validation report +## 2026-10-19 - Durable cellular environment profile (M12.5c) + +[ADR-0174](../adr/0174-durable-cellular-environment-profile.md)/schema 56 adds +checkpoint profile 6. The manifest parser normalizes only profile, history, payload +cap and nested trace length before reusing the population validator; joint traces +are checked structurally, including the complete nested population batch. Schema +45's interval admission now accepts profiles 5 and 6. Four runtime functions join +the fenced allowlist. Profile-4/5 bytes, parsers and rules are unchanged. + +Executed locally: + +- GCC 14.2 private PostgreSQL qualification `--full` passes in **622 s**: the C++ + suite, migration partitions, life reservations, backup, DR, upgrade, migration + history (through schema 56), permission audit and checksum drift. The first two runs + failed only in the region migration test, whose hard-coded digests and expected + history still ended at schema 55; both are updated. +- Joint-only PostgreSQL suite (`--environment-only`) passes in a fresh disposable + cluster: a funded rules-8 region with real wildlife and fully suspended players is + upgraded durably to bundle 10 and 11, enters profile 5 and then profile 6 (head 108, + tick 6,379; 38,700,000 pJ imported, 2,312 pJ radiated at the end). +- GCC 14.2 Release full CTest **95/95, 4.87 s** and MSVC 19.44 Release full CTest + **95/95, 6.43 s**, with NDEBUG. +- Portable `environment-checkpoint-contract`: transition/successor/conversion + manifests, round trip, payload restore, and refusals of raw or envelope-1 sources, + re-entry, another frame's lease, traceless or 65-tick successors, a population head + publishing a joint successor, history 5, bundle 12, genesis, another tick, a bare + population and a rules mismatch. +- clang-tidy 19 (warnings as errors, with libpq headers) and clang-format 19 on the + new and changed persistence sources and tests. + +Evidence in the PostgreSQL suite: exact transition retry returns the original +receipt, a reused identity with other bytes is refused with EVL07 and resolution +reports committed; recovery reproduces the nested population byte for byte with no +crossing. An interval without its owned admission is refused with EVL18; after +admission the published interval's SQL totals equal the trace's imported energy, +carbon and radiated heat, exact retry returns the same head and recovery equals the +recorded candidate. The population publisher refuses joint evidence. A conversion +cannot overtake a pending interval (EVL18); after settlement it publishes and a +cohort interval follows, each recovered exactly. A trace with one radiated picojoule +forged commits through the low-level SQL publisher, which does not replay biology, +and recovery refuses that head with `tail`. + +No executable/network fault drill, census or migration support, worker scheduling, +target-host cost, default opening, live upgrade or World0 operation. + ## 2026-10-19 - Cellular environment joint batch (M12.5b) [ADR-0173](../adr/0173-cellular-environment-batch.md) adds joint batch diff --git a/include/evo/persistence/environment_checkpoint_store.hpp b/include/evo/persistence/environment_checkpoint_store.hpp new file mode 100644 index 0000000..3ab31fd --- /dev/null +++ b/include/evo/persistence/environment_checkpoint_store.hpp @@ -0,0 +1,41 @@ +#pragma once + +#include "evo/persistence/population_checkpoint_store.hpp" + +namespace evo::persistence { +// ADR-0174 / schema 56. Prepared joint commits reuse the population shape: an empty +// trace is the no-tick profile 5 -> 6 transition, otherwise a joint batch. +struct RecoveredEnvironmentCheckpoint { + CommittedCheckpoint committed; + CheckpointManifest manifest; + std::unique_ptr state; +}; +// Explicit, one-way and no-tick: wraps the identical committed population with its +// declared environment under the same lease/head fence. No biology advances. +[[nodiscard]] std::expected +migrate_population_to_environment(Postgres&, const FilesystemObjectStore&, const LeaseGrant&, + const CommittedCheckpoint& predecessor, Uuid commit, + const Sha256Digest& build, server::CellularEnvironmentConfig, + std::chrono::milliseconds timeout, + const PublishHooks& hooks = {}); +// A joint interval (after an owned admission) or whole-group conversion. The batch is +// replayed from the committed predecessor and must reproduce `candidate` exactly. +[[nodiscard]] std::expected +publish_environment_batch(Postgres&, const FilesystemObjectStore&, const LeaseGrant&, + const CommittedCheckpoint& predecessor, + const server::CellularEnvironmentBatch& batch, + const server::CellularEnvironment& candidate, Uuid commit, + const Sha256Digest& build, std::chrono::milliseconds timeout, + const PublishHooks& hooks = {}); +[[nodiscard]] std::expected +retry_environment_prepared(Postgres&, const PopulationPreparedCommit&, + std::chrono::milliseconds timeout); +[[nodiscard]] std::expected +resolve_environment_prepared(Postgres&, const PopulationPreparedCommit&, + std::chrono::milliseconds timeout); +// Loads the head, requires its complete evidence and replays its link exactly. +[[nodiscard]] std::expected +recover_environment_checkpoint(Postgres&, const FilesystemObjectStore&, RegionKey, + const LeaseGrant&, std::span retained_builds, + std::chrono::milliseconds timeout); +} // namespace evo::persistence diff --git a/include/evo/persistence/region_checkpoint.hpp b/include/evo/persistence/region_checkpoint.hpp index a440e95..c9bb320 100644 --- a/include/evo/persistence/region_checkpoint.hpp +++ b/include/evo/persistence/region_checkpoint.hpp @@ -4,6 +4,7 @@ #include "evo/processes/environmental_community.hpp" #include "evo/processes/lineage_region.hpp" #include "evo/server/lifecycle/cellular_batch.hpp" +#include "evo/server/lifecycle/cellular_environment.hpp" #include "evo/server/lifecycle/cellular_lives.hpp" #include "evo/server/lifecycle/cellular_population_batch.hpp" #include "evo/server/lifecycle/primitive_batch.hpp" @@ -27,13 +28,22 @@ enum class CheckpointProfile : std::uint8_t { primitive_lives_v1 = 3, // Funded creation state; history_profile 2. cellular_lives_v1 = 4, // Complete cellular state; history_profile 3 or 4. cellular_population_v1 = 5, // Exact populations; cellular bundle 9, 10 or 11. + cellular_environment_v1 = 6, // ADR-0174: cells with their environment; bundle 10 or 11. }; +// Profiles whose manifests carry a CellularVersionBundle. +[[nodiscard]] constexpr bool cellular_profile(CheckpointProfile profile) noexcept { + return profile == CheckpointProfile::cellular_lives_v1 || + profile == CheckpointProfile::cellular_population_v1 || + profile == CheckpointProfile::cellular_environment_v1; +} // history_profile 1: lineage archive format 1 with genome schema 1. inline constexpr std::uint32_t lineage_history_profile = 1; // Archive format 1, genome schema 2, development 2, founder-life rules 1. inline constexpr std::uint32_t primitive_history_profile = 2; inline constexpr std::uint32_t cellular_history_profile = 3; inline constexpr std::uint32_t population_history_profile = 5; +// ADR-0174: joint envelope 1 nesting population envelope 2 and its lineage archive. +inline constexpr std::uint32_t environment_history_profile = 6; inline constexpr std::uint32_t cellular_revision_history_profile = 4; // Declared durability, verified at deployment. Development: one volume, no host-loss claim. enum class DurabilityPolicy : std::uint8_t { unknown = 0, single_volume_development = 1 }; @@ -146,6 +156,8 @@ inline constexpr std::size_t max_lineage_payload_bytes = processes::max_lineage_ : profile == CheckpointProfile::cellular_lives_v1 ? server::max_cellular_lives_bytes : profile == CheckpointProfile::cellular_population_v1 ? server::max_cellular_population_bytes + : profile == CheckpointProfile::cellular_environment_v1 + ? server::max_cellular_environment_bytes : 0; } inline constexpr std::uint64_t max_checkpoint_ticks = 64; @@ -263,6 +275,22 @@ cellular_inheritance_upgrade_manifest(const CheckpointManifest& predecessor, [[nodiscard]] std::expected, CheckpointError> cellular_inheritance_upgrade_payload(std::span payload, const CheckpointManifest& predecessor); +// ADR-0174: explicit one-way, no-tick profile 5 -> 6 transition. The payload wraps the +// identical population with its declared environment; bundle and policy are unchanged. +[[nodiscard]] std::expected +environment_profile_transition_manifest(const CheckpointManifest& predecessor, + const Sha256Digest& predecessor_digest, Uuid commit, + const LeaseGrant& lease, const Sha256Digest& build, + std::uint64_t payload_bytes, + const Sha256Digest& payload_sha256); +// Joint interval or whole-group conversion; a conversion keeps the tick with a trace. +[[nodiscard]] std::expected environment_successor_manifest( + const CheckpointManifest& predecessor, const Sha256Digest& predecessor_digest, Uuid commit, + const LeaseGrant& lease, std::uint64_t end_tick, const Sha256Digest& build, + std::uint64_t payload_bytes, const Sha256Digest& payload_sha256, const BatchDigests& batch); +[[nodiscard]] std::expected, CheckpointError> +restore_environment_payload(std::span payload, + const CheckpointManifest& manifest); // Population manual conversion may publish at the same tick with a nonempty trace. [[nodiscard]] std::expected population_successor_manifest( const CheckpointManifest& predecessor, const Sha256Digest& predecessor_digest, Uuid commit, diff --git a/include/evo/persistence/region_lease_store.hpp b/include/evo/persistence/region_lease_store.hpp index 0e9fc80..b495d89 100644 --- a/include/evo/persistence/region_lease_store.hpp +++ b/include/evo/persistence/region_lease_store.hpp @@ -238,4 +238,19 @@ resolve_population_commit(Postgres&, RegionKey, const Uuid& commit, [[nodiscard]] std::expected, LeaseStoreFailure> load_population_evidence(Postgres&, RegionKey, const LeaseGrant&, const Uuid& commit, std::chrono::milliseconds timeout); +// ADR-0174 / schema 56: the joint profile's fenced publishers and immutable evidence. +[[nodiscard]] std::expected +publish_environment_checkpoint(Postgres&, std::span manifest, + std::span trace, + std::chrono::milliseconds timeout); +[[nodiscard]] std::expected +publish_environment_profile_transition(Postgres&, std::span manifest, + std::chrono::milliseconds timeout); +[[nodiscard]] std::expected +resolve_environment_commit(Postgres&, RegionKey, const Uuid& commit, + std::span manifest, + std::span trace, std::chrono::milliseconds timeout); +[[nodiscard]] std::expected, LeaseStoreFailure> +load_environment_evidence(Postgres&, RegionKey, const LeaseGrant&, const Uuid& commit, + std::chrono::milliseconds timeout); } // namespace evo::persistence diff --git a/migrations/0056_cellular_environment_publication.sql b/migrations/0056_cellular_environment_publication.sql new file mode 100644 index 0000000..43372a3 --- /dev/null +++ b/migrations/0056_cellular_environment_publication.sql @@ -0,0 +1,321 @@ +-- ADR-0174: joint cellular/environment profile 6, immutable evidence and fenced publishers. +-- Profiles 4/5 and every stored byte keep their meaning. Normalization is parser-local. +ALTER FUNCTION evolution.parse_checkpoint_manifest(bytea) RENAME TO parse_checkpoint_manifest_v55; +REVOKE ALL ON FUNCTION evolution.parse_checkpoint_manifest_v55(bytea) FROM PUBLIC, :"runtime_role"; +CREATE FUNCTION evolution.parse_checkpoint_manifest(m bytea) +RETURNS evolution.checkpoint_manifest LANGUAGE plpgsql IMMUTABLE +SET search_path = pg_catalog, pg_temp AS $$ +DECLARE + r evolution.checkpoint_manifest; n bytea; + tick_begin numeric; tick_end numeric; payload_size numeric; trace_size numeric; nested numeric; + trace_digest bytea; + zero32 constant bytea := decode(repeat('00',32),'hex'); + b10 constant bytea := '\x080000000700000002000000010000000100000004000000030000000200000001000000'; + b11 constant bytea := '\x090000000700000002000000010000000100000004000000030000000200000001000000'; +BEGIN + IF m IS NULL OR octet_length(m) <> 360 OR evolution.manifest_uint(m,12,4) <> 6 THEN + RETURN evolution.parse_checkpoint_manifest_v55(m); + END IF; + tick_begin := evolution.manifest_uint(m,112,8); + tick_end := evolution.manifest_uint(m,120,8); + payload_size := evolution.manifest_uint(m,240,8); + trace_size := evolution.manifest_uint(m,280,8); + trace_digest := substring(m FROM 289 FOR 32); + -- Joint batch 1: 32-byte header, nested population batch, 48 bytes per tick. + nested := trace_size - 32 - 48 * greatest(tick_end - tick_begin, 0); + IF substring(m FROM 161 FOR 36) NOT IN (b10,b11) OR evolution.manifest_uint(m,232,4) <> 6 + OR evolution.manifest_uint(m,228,4) <> 0 OR evolution.manifest_uint(m,236,4) <> 1 + OR evolution.manifest_uint(m,56,8) = 0 OR tick_end < tick_begin OR tick_end - tick_begin > 64 + OR payload_size = 0 OR payload_size > 16782416 OR evolution.manifest_uint(m,320,4) <> 0 + OR substring(m FROM 329 FOR 32) <> zero32 + OR (trace_size = 0 AND (trace_digest <> zero32 OR tick_begin <> tick_end OR tick_end = 0)) + OR (trace_size > 0 AND (trace_size < 184 OR trace_size > 1971480 OR trace_size % 8 <> 0 + OR trace_digest = zero32 OR nested < 152)) THEN + RAISE EXCEPTION 'unsupported or malformed environment manifest' USING ERRCODE = '22023'; + END IF; + -- Reuse the population profile's common-field validation with only its profile, + -- history, payload cap and nested trace length normalized. Stored bytes stay profile 6. + n := overlay(m PLACING '\x05000000'::bytea FROM 13 FOR 4); + n := overlay(n PLACING '\x05000000'::bytea FROM 233 FOR 4); + n := overlay(n PLACING evolution.manifest_u64le(least(payload_size,16781400)) FROM 241 FOR 8); + IF trace_size > 0 THEN + n := overlay(n PLACING evolution.manifest_u64le(nested) FROM 281 FOR 8); + END IF; + r := evolution.parse_checkpoint_manifest_v55(n); + r.profile := 6; + r.payload_bytes := payload_size; + r.trace_bytes := trace_size; + r.trace_sha256 := trace_digest; + RETURN r; +END $$; +REVOKE ALL ON FUNCTION evolution.parse_checkpoint_manifest(bytea) FROM PUBLIC; + +-- Immutable evidence. Flow totals are derived from the trace for audit without replay; +-- they distinguish environmental crossings from migration flows in the nested ledger. +CREATE TABLE evolution.environment_publications ( + commit_id uuid PRIMARY KEY REFERENCES evolution.region_commits(commit_id) DEFERRABLE INITIALLY DEFERRED, + trace bytea NOT NULL, + imported_energy numeric NOT NULL CHECK (imported_energy >= 0), + imported_carbon numeric NOT NULL CHECK (imported_carbon >= 0), + imported_nitrogen numeric NOT NULL CHECK (imported_nitrogen >= 0), + imported_phosphorus numeric NOT NULL CHECK (imported_phosphorus >= 0), + imported_other numeric NOT NULL CHECK (imported_other >= 0), + radiated_heat numeric NOT NULL CHECK (radiated_heat >= 0) +); +CREATE TABLE evolution.environment_profile_transitions ( + target_commit uuid PRIMARY KEY REFERENCES evolution.region_commits(commit_id) DEFERRABLE INITIALLY DEFERRED, + source_commit uuid UNIQUE NOT NULL REFERENCES evolution.region_commits(commit_id), + source_manifest_sha256 bytea NOT NULL CHECK (octet_length(source_manifest_sha256) = 32), + source_payload_sha256 bytea NOT NULL CHECK (octet_length(source_payload_sha256) = 32), + CHECK (source_commit <> target_commit) +); +REVOKE ALL ON evolution.environment_publications, evolution.environment_profile_transitions FROM PUBLIC; +CREATE TRIGGER environment_publications_immutable BEFORE UPDATE OR DELETE ON evolution.environment_publications + FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite(); +CREATE TRIGGER environment_publications_no_truncate BEFORE TRUNCATE ON evolution.environment_publications + FOR EACH STATEMENT EXECUTE FUNCTION evolution.refuse_history_rewrite(); +CREATE TRIGGER environment_transitions_immutable BEFORE UPDATE OR DELETE ON evolution.environment_profile_transitions + FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite(); +CREATE TRIGGER environment_transitions_no_truncate BEFORE TRUNCATE ON evolution.environment_profile_transitions + FOR EACH STATEMENT EXECUTE FUNCTION evolution.refuse_history_rewrite(); + +-- Every profile-6 commit, including from the generic publisher, needs its evidence. +CREATE FUNCTION evolution.refuse_unpublished_environment() RETURNS trigger +LANGUAGE plpgsql SET search_path = pg_catalog, pg_temp AS $$ +BEGIN + IF evolution.manifest_uint(NEW.manifest,12,4) = 6 AND (NEW.commit_sequence = 0 OR + (NOT EXISTS (SELECT 1 FROM evolution.environment_publications x WHERE x.commit_id = NEW.commit_id) + AND NOT EXISTS (SELECT 1 FROM evolution.environment_profile_transitions x WHERE x.target_commit = NEW.commit_id))) THEN + RAISE EXCEPTION 'environment commit requires fenced publisher' USING ERRCODE = 'EVL06'; + END IF; + RETURN NEW; +END $$; +REVOKE ALL ON FUNCTION evolution.refuse_unpublished_environment() FROM PUBLIC; +CREATE TRIGGER region_environment_evidence BEFORE INSERT ON evolution.region_commits + FOR EACH ROW EXECUTE FUNCTION evolution.refuse_unpublished_environment(); + +-- Structural validation and flow totals only. C++ recovery replays the complete joint +-- biology and environment and requires exact payload equality. +CREATE FUNCTION evolution.check_environment_trace(m bytea,t bytea) +RETURNS TABLE(imported_energy numeric,imported_carbon numeric,imported_nitrogen numeric, + imported_phosphorus numeric,imported_other numeric,radiated_heat numeric) +LANGUAGE plpgsql IMMUTABLE SET search_path = pg_catalog, pg_temp AS $$ +DECLARE + c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(m); + kind numeric; nested numeric; flows numeric; n bytea; inner_trace bytea; off integer; + totals numeric[] := ARRAY[0,0,0,0,0,0]::numeric[]; +BEGIN + IF c.profile <> 6 OR t IS NULL OR octet_length(t) <> c.trace_bytes OR c.trace_bytes = 0 + OR sha256(t) <> c.trace_sha256 OR substring(t FROM 1 FOR 8) <> '\x4145564f45425431' + OR evolution.manifest_uint(t,8,4) <> 1 THEN + RAISE EXCEPTION 'invalid environment trace binding' USING ERRCODE = '22023'; + END IF; + kind := evolution.manifest_uint(t,12,4); + nested := evolution.manifest_uint(t,16,8); + flows := evolution.manifest_uint(t,24,4); + IF kind > 1 OR (kind = 1) <> (c.begin_tick = c.end_tick) OR evolution.manifest_uint(t,28,4) <> 0 + OR flows <> c.end_tick - c.begin_tick OR octet_length(t) <> 32 + nested + 48 * flows THEN + RAISE EXCEPTION 'malformed environment trace' USING ERRCODE = '22023'; + END IF; + -- The nested population batch must satisfy its own complete structural contract, + -- bound to the same identity, predecessor and ticks as this manifest. + inner_trace := substring(t FROM 33 FOR nested::integer); + n := overlay(m PLACING '\x05000000'::bytea FROM 13 FOR 4); + n := overlay(n PLACING '\x05000000'::bytea FROM 233 FOR 4); + n := overlay(n PLACING evolution.manifest_u64le(least(c.payload_bytes,16781400)) FROM 241 FOR 8); + n := overlay(n PLACING evolution.manifest_u64le(nested) FROM 281 FOR 8); + n := overlay(n PLACING sha256(inner_trace) FROM 289 FOR 32); + PERFORM evolution.check_population_trace(n,inner_trace); + off := 32 + nested::integer; + FOR i IN 0 .. flows - 1 LOOP + FOR j IN 0 .. 5 LOOP + totals[j + 1] := totals[j + 1] + evolution.manifest_uint(t, off + 48 * i + 8 * j, 8); + END LOOP; + END LOOP; + RETURN QUERY SELECT totals[1],totals[2],totals[3],totals[4],totals[5],totals[6]; +END $$; +REVOKE ALL ON FUNCTION evolution.check_environment_trace(bytea,bytea) FROM PUBLIC; + +-- Schema 45's owned interval admission also serves the joint profile: one pending +-- bounded interval per region, settled atomically with its checkpoint. +CREATE OR REPLACE FUNCTION evolution.admit_population_interval(w evolution.u64,r evolution.u64,holder uuid,epoch evolution.u64, + source uuid,source_digest bytea,target_tick evolution.u64) +RETURNS TABLE(source_commit uuid,source_manifest_sha256 bytea,begin_tick evolution.u64,end_tick evolution.u64,controller_version integer) +LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ +DECLARE a evolution.population_interval_admissions%ROWTYPE; h evolution.region_heads%ROWTYPE; + c evolution.checkpoint_manifest; m bytea; +BEGIN + IF source IS NULL OR source_digest IS NULL OR octet_length(source_digest) <> 32 OR target_tick IS NULL THEN + RAISE EXCEPTION 'invalid population interval' USING ERRCODE = '22023'; + END IF; + PERFORM evolution.check_region_lease(w,r,holder,epoch); + SELECT * INTO a FROM evolution.population_interval_admissions x WHERE x.source_commit = source; + IF FOUND THEN + IF a.world_id <> w OR a.region_id <> r OR a.source_manifest_sha256 <> source_digest OR a.end_tick <> target_tick THEN + RAISE EXCEPTION 'population interval identity reused' USING ERRCODE = 'EVL07'; + END IF; + RETURN QUERY SELECT a.source_commit,a.source_manifest_sha256,a.begin_tick,a.end_tick,a.controller_version; + RETURN; + END IF; + SELECT * INTO h FROM evolution.region_heads x WHERE x.world_id = w AND x.region_id = r FOR UPDATE; + IF NOT FOUND OR h.commit_id <> source OR h.digest <> source_digest THEN + RAISE EXCEPTION 'population interval head moved' USING ERRCODE = 'EVL03'; + END IF; + SELECT x.manifest INTO m FROM evolution.region_commits x WHERE x.commit_id = source; + c := evolution.parse_checkpoint_manifest(m); + IF c.profile NOT IN (5,6) OR target_tick <= h.end_tick OR target_tick - h.end_tick > 64 THEN + RAISE EXCEPTION 'unsupported population interval' USING ERRCODE = 'EVL06'; + END IF; + IF EXISTS (SELECT 1 FROM evolution.population_interval_pending x WHERE x.world_id = w AND x.region_id = r) THEN + RAISE EXCEPTION 'population interval already pending' USING ERRCODE = 'EVL18'; + END IF; + INSERT INTO evolution.population_interval_admissions VALUES(source,w,r,source_digest,h.end_tick,target_tick,1,holder,epoch) + RETURNING * INTO a; + INSERT INTO evolution.population_interval_pending VALUES(w,r,source); + RETURN QUERY SELECT a.source_commit,a.source_manifest_sha256,a.begin_tick,a.end_tick,a.controller_version; +END $$; + +CREATE FUNCTION evolution.publish_environment_checkpoint(m bytea,t bytea) +RETURNS TABLE(region_frame evolution.u64,head_sequence evolution.u64,head_commit uuid, + head_digest text,head_tick evolution.u64) +LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ +DECLARE + c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(m); + f record; a evolution.population_interval_admissions%ROWTYPE; existing bytea; +BEGIN + SELECT * INTO f FROM evolution.check_environment_trace(m,t); + PERFORM evolution.check_region_lease(c.world_id,c.region_id,c.holder,c.epoch); + SELECT p.trace INTO existing FROM evolution.environment_publications p WHERE p.commit_id = c.commit_id; + IF FOUND THEN + IF existing <> t THEN RAISE EXCEPTION 'environment identity reused' USING ERRCODE = 'EVL07'; END IF; + RETURN QUERY SELECT * FROM evolution.publish_region_checkpoint_core(m,''::bytea); + RETURN; + END IF; + IF EXISTS (SELECT 1 FROM evolution.region_commits x WHERE x.commit_id = c.commit_id) THEN + RAISE EXCEPTION 'environment identity reused' USING ERRCODE = 'EVL07'; + END IF; + IF c.end_tick > c.begin_tick THEN + SELECT x.* INTO a FROM evolution.population_interval_pending p JOIN evolution.population_interval_admissions x + ON x.source_commit = p.source_commit WHERE p.world_id = c.world_id AND p.region_id = c.region_id + AND p.source_commit = c.predecessor_commit; + IF NOT FOUND OR a.world_id <> c.world_id OR a.region_id <> c.region_id + OR a.source_manifest_sha256 <> c.predecessor_digest OR a.begin_tick <> c.begin_tick OR a.end_tick <> c.end_tick + OR EXISTS (SELECT 1 FROM evolution.population_interval_settlements s WHERE s.source_commit = a.source_commit) THEN + RAISE EXCEPTION 'environment publication requires exact interval' USING ERRCODE = 'EVL18'; + END IF; + INSERT INTO evolution.population_interval_settlements VALUES(a.source_commit,c.commit_id); + DELETE FROM evolution.population_interval_pending p WHERE p.world_id = c.world_id AND p.region_id = c.region_id + AND p.source_commit = a.source_commit; + ELSIF EXISTS (SELECT 1 FROM evolution.population_interval_pending x + WHERE x.world_id = c.world_id AND x.region_id = c.region_id) THEN + RAISE EXCEPTION 'environment conversion waits for interval' USING ERRCODE = 'EVL18'; + END IF; + INSERT INTO evolution.environment_publications VALUES(c.commit_id,t,f.imported_energy,f.imported_carbon, + f.imported_nitrogen,f.imported_phosphorus,f.imported_other,f.radiated_heat); + RETURN QUERY SELECT * FROM evolution.publish_region_checkpoint_core(m,''::bytea); +END $$; +REVOKE ALL ON FUNCTION evolution.publish_environment_checkpoint(bytea,bytea) FROM PUBLIC; +GRANT EXECUTE ON FUNCTION evolution.publish_environment_checkpoint(bytea,bytea) TO :"runtime_role"; + +-- One-way, no-tick profile 5 -> 6 with the identical bundle. The source payload digest +-- is bound; C++ recovery verifies the nested population is byte-identical. +CREATE FUNCTION evolution.publish_environment_profile_transition(m bytea) +RETURNS TABLE(region_frame evolution.u64,head_sequence evolution.u64,head_commit uuid, + head_digest text,head_tick evolution.u64) +LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ +DECLARE + c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(m); + s evolution.checkpoint_manifest; p evolution.region_commits%ROWTYPE; + h evolution.region_heads%ROWTYPE; old evolution.region_commits%ROWTYPE; + d bytea := sha256(m); frame evolution.u64; +BEGIN + IF c.profile <> 6 OR c.commit_sequence = 0 OR c.begin_tick <> c.end_tick OR c.end_tick = 0 + OR c.trace_bytes <> 0 THEN + RAISE EXCEPTION 'invalid environment profile transition' USING ERRCODE = '22023'; + END IF; + PERFORM evolution.check_region_lease(c.world_id,c.region_id,c.holder,c.epoch); + SELECT * INTO old FROM evolution.region_commits r WHERE r.commit_id = c.commit_id; + IF FOUND THEN + IF old.manifest <> m OR NOT EXISTS (SELECT 1 FROM evolution.environment_profile_transitions x + WHERE x.target_commit = c.commit_id AND x.source_commit = c.predecessor_commit + AND x.source_manifest_sha256 = c.predecessor_digest) THEN + RAISE EXCEPTION 'environment transition identity reused' USING ERRCODE = 'EVL07'; + END IF; + SELECT l.frame_id INTO frame FROM evolution.region_leases l WHERE l.world_id = c.world_id AND l.region_id = c.region_id; + RETURN QUERY SELECT frame,old.commit_sequence,old.commit_id,encode(old.commit_digest,'hex'),old.end_tick; + RETURN; + END IF; + IF EXISTS (SELECT 1 FROM evolution.population_interval_pending x + WHERE x.world_id = c.world_id AND x.region_id = c.region_id) THEN + RAISE EXCEPTION 'environment transition waits for interval' USING ERRCODE = 'EVL18'; + END IF; + SELECT * INTO h FROM evolution.region_heads r WHERE r.world_id = c.world_id AND r.region_id = c.region_id FOR UPDATE; + IF NOT FOUND OR h.commit_sequence = 18446744073709551615 OR c.commit_sequence <> h.commit_sequence + 1 + OR c.predecessor_commit <> h.commit_id OR c.predecessor_digest <> h.digest OR c.begin_tick <> h.end_tick THEN + RAISE EXCEPTION 'environment transition head moved' USING ERRCODE = 'EVL03'; + END IF; + SELECT * INTO p FROM evolution.region_commits r WHERE r.commit_id = h.commit_id; + IF NOT FOUND THEN RAISE EXCEPTION 'missing environment source' USING ERRCODE = 'XX000'; END IF; + s := evolution.parse_checkpoint_manifest(p.manifest); + IF EXISTS (SELECT 1 FROM evolution.cellular_mutation_reservations x + WHERE x.world_id = c.world_id AND x.region_id = c.region_id AND NOT EXISTS + (SELECT 1 FROM evolution.cellular_mutation_action_publications a + WHERE a.world_id = x.world_id AND a.account_id = x.account_id AND a.action_id = x.action_id)) THEN + RAISE EXCEPTION 'pending player mutation blocks environment transition' USING ERRCODE = 'EVL18'; + END IF; + IF s.profile <> 5 OR s.versions <> c.versions OR s.frame_id <> c.frame_id + OR s.tick_microseconds <> c.tick_microseconds OR s.end_tick <> c.end_tick + OR NOT EXISTS (SELECT 1 FROM evolution.retained_builds b WHERE b.build_identity = c.build_identity) THEN + RAISE EXCEPTION 'unsupported environment source' USING ERRCODE = 'EVL06'; + END IF; + INSERT INTO evolution.environment_profile_transitions VALUES(c.commit_id,p.commit_id,p.commit_digest,p.payload_sha256); + INSERT INTO evolution.region_commits(world_id,region_id,commit_sequence,commit_id,commit_digest,manifest, + begin_tick,end_tick,published_epoch,tick_microseconds,versions,build_identity,payload_bytes,payload_sha256) + VALUES(c.world_id,c.region_id,c.commit_sequence,c.commit_id,d,m,c.begin_tick,c.end_tick,c.epoch, + c.tick_microseconds,c.versions,c.build_identity,c.payload_bytes,c.payload_sha256); + UPDATE evolution.region_heads r SET commit_sequence = c.commit_sequence,commit_id = c.commit_id, + digest = d,end_tick = c.end_tick,published_epoch = c.epoch WHERE r.world_id = c.world_id AND r.region_id = c.region_id; + RETURN QUERY SELECT c.frame_id,c.commit_sequence,c.commit_id,encode(d,'hex'),c.end_tick; +END $$; +REVOKE ALL ON FUNCTION evolution.publish_environment_profile_transition(bytea) FROM PUBLIC; +GRANT EXECUTE ON FUNCTION evolution.publish_environment_profile_transition(bytea) TO :"runtime_role"; + +CREATE FUNCTION evolution.resolve_environment_commit(w evolution.u64,r evolution.u64,id uuid,m bytea,t bytea) +RETURNS TABLE(status text,region_frame evolution.u64,head_sequence evolution.u64,head_commit uuid, + head_digest text,head_tick evolution.u64) +LANGUAGE plpgsql STABLE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ +DECLARE c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(m); result record; +BEGIN + IF c.world_id <> w OR c.region_id <> r OR c.commit_id <> id OR t IS NULL OR c.profile <> 6 THEN + RAISE EXCEPTION 'invalid environment resolution' USING ERRCODE = '22023'; + END IF; + IF octet_length(t) > 0 THEN PERFORM * FROM evolution.check_environment_trace(m,t); + ELSIF c.trace_bytes <> 0 OR c.begin_tick <> c.end_tick THEN + RAISE EXCEPTION 'invalid transition resolution' USING ERRCODE = '22023'; + END IF; + SELECT * INTO result FROM evolution.resolve_region_commit(w,r,id,m,''::bytea); + IF result.status = 'committed' AND + ((octet_length(t) = 0 AND NOT EXISTS (SELECT 1 FROM evolution.environment_profile_transitions x WHERE x.target_commit = id)) + OR (octet_length(t) > 0 AND NOT EXISTS (SELECT 1 FROM evolution.environment_publications x WHERE x.commit_id = id AND x.trace = t))) THEN + RETURN QUERY SELECT 'conflict'::text,NULL::evolution.u64,NULL::evolution.u64,NULL::uuid,NULL::text,NULL::evolution.u64; + ELSE + RETURN QUERY SELECT result.status,result.region_frame,result.head_sequence,result.head_commit,result.head_digest,result.head_tick; + END IF; +END $$; +REVOKE ALL ON FUNCTION evolution.resolve_environment_commit(evolution.u64,evolution.u64,uuid,bytea,bytea) FROM PUBLIC; +GRANT EXECUTE ON FUNCTION evolution.resolve_environment_commit(evolution.u64,evolution.u64,uuid,bytea,bytea) TO :"runtime_role"; + +CREATE FUNCTION evolution.load_environment_evidence(w evolution.u64,r evolution.u64,holder uuid,epoch evolution.u64,id uuid) +RETURNS TABLE(trace bytea,source_commit uuid,source_manifest_sha256 bytea,source_payload_sha256 bytea) +LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ +BEGIN + PERFORM evolution.check_region_lease(w,r,holder,epoch); + RETURN QUERY SELECT p.trace,NULL::uuid,NULL::bytea,NULL::bytea FROM evolution.environment_publications p + JOIN evolution.region_commits c ON c.commit_id = p.commit_id WHERE c.world_id = w AND c.region_id = r AND c.commit_id = id + UNION ALL SELECT ''::bytea,p.source_commit,p.source_manifest_sha256,p.source_payload_sha256 FROM evolution.environment_profile_transitions p + JOIN evolution.region_commits c ON c.commit_id = p.target_commit WHERE c.world_id = w AND c.region_id = r AND c.commit_id = id; +END $$; +REVOKE ALL ON FUNCTION evolution.load_environment_evidence(evolution.u64,evolution.u64,uuid,evolution.u64,uuid) FROM PUBLIC; +GRANT EXECUTE ON FUNCTION evolution.load_environment_evidence(evolution.u64,evolution.u64,uuid,evolution.u64,uuid) TO :"runtime_role"; + +INSERT INTO evolution.schema_migrations(version,sha256) VALUES(56,:'migration_sha_56'); diff --git a/migrations/apply.sql b/migrations/apply.sql index b297e83..0db8a72 100644 --- a/migrations/apply.sql +++ b/migrations/apply.sql @@ -131,6 +131,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') SELECT :'history_v52' || ',53:' || :'migration_sha_53' AS history_v53 \gset SELECT :'history_v53' || ',54:' || :'migration_sha_54' AS history_v54 \gset SELECT :'history_v54' || ',55:' || :'migration_sha_55' AS history_v55 \gset + SELECT :'history_v55' || ',56:' || :'migration_sha_56' AS history_v56 \gset SELECT :'history' = :'history_v22' AS at_v22, :'history' = :'history_v23' AS at_v23, :'history' = :'history_v24' AS at_v24, @@ -164,8 +165,10 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') :'history' = :'history_v52' AS at_v52, :'history' = :'history_v53' AS at_v53, :'history' = :'history_v54' AS at_v54, - :'history' = :'history_v55' AS at_v55 \gset - \if :at_v55 + :'history' = :'history_v55' AS at_v55, + :'history' = :'history_v56' AS at_v56 \gset + \if :at_v56 + \elif :at_v55 \elif :at_v54 \elif :at_v53 \elif :at_v52 @@ -569,7 +572,8 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24, coalesce(max(version), 0) = 52 AS at_v52, coalesce(max(version), 0) = 53 AS at_v53, coalesce(max(version), 0) = 54 AS at_v54, - coalesce(max(version), 0) = 55 AS at_v55 + coalesce(max(version), 0) = 55 AS at_v55, + coalesce(max(version), 0) = 56 AS at_v56 FROM evolution.schema_migrations \gset \if :at_v24 \ir 0025_cellular_v2_bundle.sql @@ -793,6 +797,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24, \elif :at_v53 \elif :at_v54 \elif :at_v55 +\elif :at_v56 \else \echo 'Unknown or changed migration history after v43' DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$; @@ -845,6 +850,10 @@ SELECT coalesce(max(version), 0) = 54 AS needs_v55 FROM evolution.schema_migrati \if :needs_v55 \ir 0055_retained_cellular_plans.sql \endif +SELECT coalesce(max(version), 0) = 55 AS needs_v56 FROM evolution.schema_migrations \gset +\if :needs_v56 + \ir 0056_cellular_environment_publication.sql +\endif -- Effective-privilege audit on every run, including no-ops (ADR-0060). Attributes -- alone are insufficient: predefined roles such as pg_write_all_data, PUBLIC -- grants or SET-able memberships would bypass the fenced functions. Checks the @@ -880,6 +889,10 @@ BEGIN ('evolution.publish_population_profile_transition(bytea)'::regprocedure::oid), ('evolution.resolve_population_commit(evolution.u64,evolution.u64,uuid,bytea,bytea)'::regprocedure::oid), ('evolution.load_population_evidence(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)'::regprocedure::oid), + ('evolution.publish_environment_checkpoint(bytea,bytea)'::regprocedure::oid), + ('evolution.publish_environment_profile_transition(bytea)'::regprocedure::oid), + ('evolution.resolve_environment_commit(evolution.u64,evolution.u64,uuid,bytea,bytea)'::regprocedure::oid), + ('evolution.load_environment_evidence(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)'::regprocedure::oid), ('evolution.publish_primitive_checkpoint(bytea,bytea)'::regprocedure::oid), ('evolution.load_primitive_lives(evolution.u64,evolution.u64)'::regprocedure::oid), ('evolution.query_primitive_life(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,evolution.u64)'::regprocedure::oid), diff --git a/schemas/region-commit.md b/schemas/region-commit.md index 12b6353..83a035c 100644 --- a/schemas/region-commit.md +++ b/schemas/region-commit.md @@ -1,5 +1,13 @@ # Region commit manifest contract v1 (M7.1 design, M7.4a encoding) +M12.5c/schema 56 adds profile 6, `cellular_environment_v1`, with history profile 6. +Its payload is the [joint envelope](cellular-environment-v1.md) nesting population +envelope 2 (bundles 10 and 11 only), capped at 16,782,416 bytes. A nonempty trace is +a [joint batch](cellular-environment-batch-v1.md) of 184..1,971,480 bytes, a multiple +of 8; an empty trace with equal ticks is the explicit one-way, no-tick 5 -> 6 +transition. Genesis and cloning are refused. Ordinary continuation keeps profile and +bundle. See [ADR-0174](../docs/adr/0174-durable-cellular-environment-profile.md). + M12.4f adds the portable bundle-10 declaration `(8,7,2,1,1,4,3,2,1)` for owner-7 boundary accounting, with unchanged rules 8. Profile 4 uses raw owner 7 and history 4; profile 5 uses population envelope 2, batch 2 and history 5. diff --git a/src/persistence/environment_checkpoint.cpp b/src/persistence/environment_checkpoint.cpp new file mode 100644 index 0000000..74586bc --- /dev/null +++ b/src/persistence/environment_checkpoint.cpp @@ -0,0 +1,85 @@ +#include "evo/persistence/region_checkpoint.hpp" + +namespace evo::persistence { +namespace { +bool leased(const CheckpointManifest& previous, const Sha256Digest& previous_digest, Uuid commit, + const LeaseGrant& lease) noexcept { + return supported(previous) && encode_manifest(previous).has_value() && previous.sequence > 0 && + previous.sequence != ~std::uint64_t{0} && previous.end_tick > 0 && + population_envelope_version(previous.cellular_versions) == 2 && + previous_digest != Sha256Digest{} && lease.frame == previous.frame && lease.epoch != 0 && + lease.holder != Uuid{} && commit != Uuid{} && commit != previous.commit; +} +CheckpointManifest link(const CheckpointManifest& previous, const Sha256Digest& previous_digest, + Uuid commit, const LeaseGrant& lease, std::uint64_t end_tick, + const Sha256Digest& build, std::uint64_t bytes, const Sha256Digest& digest, + const BatchDigests& batch) { + auto next = previous; + next.commit = commit; + ++next.sequence; + next.predecessor_commit = previous.commit; + next.predecessor_digest = previous_digest; + next.begin_tick = previous.end_tick; + next.end_tick = end_tick; + next.holder = lease.holder; + next.epoch = lease.epoch; + next.build = build; + next.payload_bytes = bytes; + next.payload_sha256 = digest; + next.batch = batch; + return next; +} +} // namespace + +std::expected +environment_profile_transition_manifest(const CheckpointManifest& previous, + const Sha256Digest& previous_digest, Uuid commit, + const LeaseGrant& lease, const Sha256Digest& build, + std::uint64_t bytes, const Sha256Digest& digest) { + if (!leased(previous, previous_digest, commit, lease) || + previous.profile != CheckpointProfile::cellular_population_v1) + return std::unexpected(CheckpointError::chain); + auto next = + link(previous, previous_digest, commit, lease, previous.end_tick, build, bytes, digest, {}); + next.profile = CheckpointProfile::cellular_environment_v1; + next.history_profile = environment_history_profile; + if (!encode_manifest(next)) + return std::unexpected(CheckpointError::format); + return next; +} + +std::expected environment_successor_manifest( + const CheckpointManifest& previous, const Sha256Digest& previous_digest, Uuid commit, + const LeaseGrant& lease, std::uint64_t end_tick, const Sha256Digest& build, std::uint64_t bytes, + const Sha256Digest& digest, const BatchDigests& batch) { + if (!leased(previous, previous_digest, commit, lease) || + previous.profile != CheckpointProfile::cellular_environment_v1 || batch.trace_bytes == 0 || + end_tick < previous.end_tick || end_tick - previous.end_tick > max_checkpoint_ticks) + return std::unexpected(CheckpointError::chain); + auto next = + link(previous, previous_digest, commit, lease, end_tick, build, bytes, digest, batch); + if (!encode_manifest(next)) + return std::unexpected(CheckpointError::format); + return next; +} + +std::expected, CheckpointError> +restore_environment_payload(std::span payload, const CheckpointManifest& m) { + if (!supported(m) || m.profile != CheckpointProfile::cellular_environment_v1) + return std::unexpected(CheckpointError::unsupported); + if (!encode_manifest(m) || payload.size() != m.payload_bytes) + return std::unexpected(CheckpointError::format); + auto state = server::CellularEnvironment::restore(payload, m.key.world, m.key.region, m.frame, + m.end_tick); + if (!state) + return std::unexpected(state.error() == server::CellularEnvironmentError::unsupported + ? CheckpointError::unsupported + : CheckpointError::payload); + const auto& population = (*state)->population(); + if (population.format_version() != population_envelope_version(m.cellular_versions) || + population.state().format_version() != m.cellular_versions.envelope || + population.state().rules_version() != m.cellular_versions.rules) + return std::unexpected(CheckpointError::unsupported); + return std::move(*state); +} +} // namespace evo::persistence diff --git a/src/persistence/environment_checkpoint_store.cpp b/src/persistence/environment_checkpoint_store.cpp new file mode 100644 index 0000000..f0b1e80 --- /dev/null +++ b/src/persistence/environment_checkpoint_store.cpp @@ -0,0 +1,249 @@ +#include "evo/persistence/environment_checkpoint_store.hpp" +#include "evo/persistence/sha256.hpp" + +#include +#include + +namespace evo::persistence { +namespace { +using std::chrono::milliseconds; +PopulationPublicationFailure failure(PublicationError kind, ObjectError object = {}, + LeaseStoreFailure store = {}, + PopulationPreparedCommit prepared = {}) { + return {kind, store, object, std::move(prepared)}; +} +bool bound(const CommittedCheckpoint& checkpoint, const CheckpointManifest& m) { + const auto digest = sha256(checkpoint.manifest); + return digest && *digest == checkpoint.head.digest && m.commit == checkpoint.head.commit && + m.sequence == checkpoint.head.sequence && m.end_tick == checkpoint.head.end_tick && + m.frame == checkpoint.head.frame; +} +// The manifest, trace and their bindings agree before anything reaches the database. +bool canonical(const PopulationPreparedCommit& prepared, const CheckpointManifest& m) { + if (!supported(m) || m.profile != CheckpointProfile::cellular_environment_v1 || + m.key != prepared.key || m.commit != prepared.commit || m.sequence == 0 || + !encode_manifest(m)) + return false; + if (prepared.trace.empty()) + return m.begin_tick == m.end_tick && m.batch == BatchDigests{}; + const auto trace = server::decode_environment_batch(prepared.trace); + const auto digest = sha256(prepared.trace); + if (!trace || !digest || + trace->population.format_version != population_envelope_version(m.cellular_versions) || + m.batch != BatchDigests{prepared.trace.size(), *digest, 0, {}}) + return false; + const auto& h = trace->population.header; + return h.world == m.key.world && h.region == m.key.region && h.frame == m.frame && + h.predecessor == m.predecessor_commit && h.predecessor_digest == m.predecessor_digest && + h.begin_tick == m.begin_tick && h.end_tick == m.end_tick; +} +std::expected put(const FilesystemObjectStore& objects, + std::span bytes) { + const auto digest = sha256(bytes); + if (!digest) + return std::unexpected(failure(PublicationError::digest)); + auto object = objects.put(bytes); + if (!object) + return std::unexpected(failure(PublicationError::upload, object.error())); + auto readback = objects.get(*object); + if (!readback || object->bytes != bytes.size() || object->sha256 != *digest || + !std::ranges::equal(*readback, bytes)) + return std::unexpected( + failure(PublicationError::verify, readback ? ObjectError::corrupt : readback.error())); + return {}; +} +} // namespace + +std::expected migrate_population_to_environment( + Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease, + const CommittedCheckpoint& predecessor, Uuid commit, const Sha256Digest& build, + server::CellularEnvironmentConfig config, milliseconds timeout, const PublishHooks& hooks) { + auto previous = decode_manifest(predecessor.manifest); + const auto previous_digest = sha256(predecessor.manifest); + if (!previous || !previous_digest || !bound(predecessor, *previous) || + previous->profile != CheckpointProfile::cellular_population_v1 || + population_envelope_version(previous->cellular_versions) != 2) + return std::unexpected(failure(PublicationError::predecessor)); + auto source = objects.get({previous->payload_sha256, previous->payload_bytes}); + if (!source) + return std::unexpected(failure(PublicationError::predecessor, source.error())); + auto population = restore_population_payload(*source, *previous); + if (!population) + return std::unexpected(failure(PublicationError::predecessor)); + auto joint = server::CellularEnvironment::create(std::move(*population), std::move(config)); + if (!joint || (*joint)->population().snapshot() != *source) + return std::unexpected(failure(PublicationError::candidate)); + const auto payload = (*joint)->snapshot(); + const auto digest = sha256(payload); + if (!digest) + return std::unexpected(failure(PublicationError::digest)); + auto manifest = environment_profile_transition_manifest(*previous, *previous_digest, commit, + lease, build, payload.size(), *digest); + if (!manifest) + return std::unexpected(failure(PublicationError::candidate)); + auto encoded = encode_manifest(*manifest); + if (!encoded) + return std::unexpected(failure(PublicationError::candidate)); + if (auto uploaded = put(objects, payload); !uploaded) + return std::unexpected(uploaded.error()); + if (hooks.before_transaction) + hooks.before_transaction(); + return retry_environment_prepared(db, {previous->key, commit, std::move(*encoded), {}}, + timeout); +} + +std::expected +retry_environment_prepared(Postgres& db, const PopulationPreparedCommit& prepared, + milliseconds timeout) { + const auto m = decode_manifest(prepared.manifest); + if (!m || !canonical(prepared, *m)) + return std::unexpected(failure(PublicationError::candidate)); + auto result = + prepared.trace.empty() + ? publish_environment_profile_transition(db, prepared.manifest, timeout) + : publish_environment_checkpoint(db, prepared.manifest, prepared.trace, timeout); + if (!result) + return std::unexpected(failure(result.error().kind == LeaseStoreError::unknown + ? PublicationError::unknown + : PublicationError::rejected, + {}, result.error(), prepared)); + const auto digest = sha256(prepared.manifest); + if (!digest || result->digest != *digest || result->commit != m->commit || + result->sequence != m->sequence || result->end_tick != m->end_tick || + result->frame != m->frame) + return std::unexpected(failure(PublicationError::receipt)); + return CommittedCheckpoint{*result, prepared.manifest}; +} + +std::expected +resolve_environment_prepared(Postgres& db, const PopulationPreparedCommit& prepared, + milliseconds timeout) { + const auto manifest = decode_manifest(prepared.manifest); + if (!manifest || !canonical(prepared, *manifest)) + return std::unexpected(LeaseStoreFailure{LeaseStoreError::invalid, {}}); + return resolve_environment_commit(db, prepared.key, prepared.commit, prepared.manifest, + prepared.trace, timeout); +} + +std::expected publish_environment_batch( + Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease, + const CommittedCheckpoint& predecessor, const server::CellularEnvironmentBatch& batch, + const server::CellularEnvironment& candidate, Uuid commit, const Sha256Digest& build, + milliseconds timeout, const PublishHooks& hooks) { + const auto previous = decode_manifest(predecessor.manifest); + const auto previous_digest = sha256(predecessor.manifest); + const auto trace = server::encode_environment_batch(batch); + if (!previous || !previous_digest || !trace || !bound(predecessor, *previous) || + previous->profile != CheckpointProfile::cellular_environment_v1) + return std::unexpected(failure(PublicationError::predecessor)); + auto source = objects.get({previous->payload_sha256, previous->payload_bytes}); + if (!source) + return std::unexpected(failure(PublicationError::predecessor, source.error())); + auto base = restore_environment_payload(*source, *previous); + if (!base) + return std::unexpected(failure(PublicationError::predecessor)); + auto replay = server::replay_environment_batch(**base, batch); + const auto payload = candidate.snapshot(); + if (!replay || (*replay)->snapshot() != payload) + return std::unexpected(failure(PublicationError::candidate)); + const auto payload_digest = sha256(payload), trace_digest = sha256(*trace); + if (!payload_digest || !trace_digest) + return std::unexpected(failure(PublicationError::digest)); + auto m = environment_successor_manifest(*previous, *previous_digest, commit, lease, + batch.population.header.end_tick, build, payload.size(), + *payload_digest, {trace->size(), *trace_digest, 0, {}}); + if (!m) + return std::unexpected(failure(PublicationError::candidate)); + auto encoded = encode_manifest(*m); + if (!encoded) + return std::unexpected(failure(PublicationError::candidate)); + PopulationPreparedCommit prepared{previous->key, commit, std::move(*encoded), *trace}; + if (!canonical(prepared, *m)) + return std::unexpected(failure(PublicationError::candidate)); + if (auto uploaded = put(objects, payload); !uploaded) + return std::unexpected(uploaded.error()); + if (auto uploaded = put(objects, *trace); !uploaded) + return std::unexpected(uploaded.error()); + if (hooks.before_transaction) + hooks.before_transaction(); + return retry_environment_prepared(db, prepared, timeout); +} + +std::expected recover_environment_checkpoint( + Postgres& db, const FilesystemObjectStore& objects, RegionKey key, const LeaseGrant& lease, + std::span retained_builds, milliseconds timeout) { + const auto fail = [](RecoveryError kind) { return std::unexpected(RecoveryFailure{kind, {}}); }; + auto committed = load_region_checkpoint(db, key, timeout); + if (!committed) + return std::unexpected( + RecoveryFailure{committed.error().kind == LeaseStoreError::not_provisioned + ? RecoveryError::not_provisioned + : RecoveryError::store, + committed.error()}); + auto m = decode_manifest(committed->manifest); + if (!m || !bound(*committed, *m) || m->key != key) + return fail(RecoveryError::manifest); + if (!supported(*m) || m->profile != CheckpointProfile::cellular_environment_v1 || + lease.frame != m->frame || + std::ranges::find(retained_builds, m->build) == retained_builds.end()) + return fail(RecoveryError::unsupported); + auto bytes = objects.get({m->payload_sha256, m->payload_bytes}); + if (!bytes) + return fail(bytes.error() == ObjectError::io ? RecoveryError::store + : bytes.error() == ObjectError::missing ? RecoveryError::missing_payload + : RecoveryError::corrupt_payload); + auto state = restore_environment_payload(*bytes, *m); + if (!state) + return fail(RecoveryError::payload); + auto evidence = load_environment_evidence(db, key, lease, m->commit, timeout); + if (!evidence) + return std::unexpected(RecoveryFailure{RecoveryError::store, evidence.error()}); + const auto record_optional = std::move(*evidence); + if (!record_optional.has_value()) + return fail(RecoveryError::history); + const auto& record = *record_optional; + auto predecessor = load_region_commit(db, key, m->sequence - 1, timeout); + if (!predecessor) + return std::unexpected(RecoveryFailure{RecoveryError::tail, predecessor.error()}); + auto previous = decode_manifest(predecessor->manifest); + if (!previous || !bound(*predecessor, *previous) || !supported(*previous) || + previous->commit != m->predecessor_commit || + predecessor->head.digest != m->predecessor_digest || previous->key != key || + previous->frame != m->frame || previous->sequence + 1 != m->sequence || + previous->end_tick != m->begin_tick || previous->cellular_versions != m->cellular_versions) + return fail(RecoveryError::tail); + auto source = objects.get({previous->payload_sha256, previous->payload_bytes}); + if (!source) + return fail(RecoveryError::tail); + if (m->batch == BatchDigests{}) { + // The transition: the joint payload nests the source population byte for byte. + if (!record.trace.empty() || record.source_commit != previous->commit || + record.source_manifest_sha256 != predecessor->head.digest || + record.source_payload_sha256 != previous->payload_sha256 || + previous->profile != CheckpointProfile::cellular_population_v1 || + m->begin_tick != m->end_tick) + return fail(RecoveryError::history); + if (!restore_population_payload(*source, *previous) || + (*state)->population().snapshot() != *source) + return fail(RecoveryError::history); + } else { + if (record.source_commit.has_value() || + previous->profile != CheckpointProfile::cellular_environment_v1) + return fail(RecoveryError::history); + auto trace_bytes = objects.get({m->batch.trace_sha256, m->batch.trace_bytes}); + if (!trace_bytes || *trace_bytes != record.trace) + return fail(RecoveryError::tail); + PopulationPreparedCommit prepared{key, m->commit, committed->manifest, *trace_bytes}; + if (!canonical(prepared, *m)) + return fail(RecoveryError::tail); + auto base = restore_environment_payload(*source, *previous); + auto trace = server::decode_environment_batch(*trace_bytes); + if (!base || !trace) + return fail(RecoveryError::tail); + auto replay = server::replay_environment_batch(**base, *trace); + if (!replay || (*replay)->snapshot() != *bytes) + return fail(RecoveryError::tail); + } + return RecoveredEnvironmentCheckpoint{std::move(*committed), *m, std::move(*state)}; +} +} // namespace evo::persistence diff --git a/src/persistence/region_checkpoint.cpp b/src/persistence/region_checkpoint.cpp index 8f1db82..528f854 100644 --- a/src/persistence/region_checkpoint.cpp +++ b/src/persistence/region_checkpoint.cpp @@ -61,7 +61,8 @@ bool structural(const CheckpointManifest& m) noexcept { const auto& b = m.batch; const bool primitive = m.profile == CheckpointProfile::primitive_lives_v1; const bool population = m.profile == CheckpointProfile::cellular_population_v1; - const bool cellular = m.profile == CheckpointProfile::cellular_lives_v1 || population; + const bool joint = m.profile == CheckpointProfile::cellular_environment_v1; + const bool cellular = cellular_profile(m.profile); const bool cellular_migration = cellular && m.sequence > 0 && (m.cellular_versions == cellular_v2_versions || m.cellular_versions == cellular_v3_versions || @@ -71,12 +72,21 @@ bool structural(const CheckpointManifest& m) noexcept { m.cellular_versions == cellular_v12_versions) && m.begin_tick == m.end_tick && m.end_tick > 0 && b == BatchDigests{}; - const bool population_conversion = population && m.begin_tick == m.end_tick && m.end_tick > 0 && - b.trace_bytes >= server::population_batch_header_bytes + 16; + const auto joint_min = + server::environment_batch_header_bytes + server::population_batch_header_bytes + 16; + const bool population_conversion = + m.begin_tick == m.end_tick && m.end_tick > 0 && + ((population && b.trace_bytes >= server::population_batch_header_bytes + 16) || + (joint && b.trace_bytes >= joint_min)); if (population && (m.sequence == 0 || population_envelope_version(m.cellular_versions) == 0 || m.history_profile != population_history_profile || b.decision_count != 0 || !zero(b.decisions_sha256))) return false; + // ADR-0174: joint profile 6 nests population envelope 2 only (bundles 10 and 11). + if (joint && (m.sequence == 0 || population_envelope_version(m.cellular_versions) != 2 || + m.history_profile != environment_history_profile || b.decision_count != 0 || + !zero(b.decisions_sha256))) + return false; if ((!cellular && m.cellular_versions != cellular_v1_versions) || (cellular && m.cellular_versions != cellular_v1_versions && m.cellular_versions != cellular_v2_versions && @@ -169,6 +179,8 @@ bool structural(const CheckpointManifest& m) noexcept { : population ? b.trace_bytes >= server::population_batch_header_bytes + 16 && b.trace_bytes <= server::max_population_batch_bytes && b.trace_bytes % 8 == 0 + : joint ? b.trace_bytes >= joint_min && + b.trace_bytes <= server::max_environment_batch_bytes && b.trace_bytes % 8 == 0 : cellular ? (cellular_current_trace || cellular_v2_trace || cellular_legacy_trace) : b.trace_bytes == trace_bytes_for(ticks); return link && trace_shape && !zero(b.trace_sha256) && @@ -178,6 +190,11 @@ bool structural(const CheckpointManifest& m) noexcept { } // namespace bool supported(const CheckpointManifest& m) noexcept { + if (m.profile == CheckpointProfile::cellular_environment_v1) + return m.manifest_version == 2 && m.history_profile == environment_history_profile && + population_envelope_version(m.cellular_versions) == 2 && + m.versions == environmental_v1_versions && m.tick_microseconds == 50000 && + m.rng_streams == 0 && m.durability == DurabilityPolicy::single_volume_development; if (m.profile == CheckpointProfile::cellular_population_v1) return m.manifest_version == 2 && m.history_profile == population_history_profile && population_envelope_version(m.cellular_versions) != 0 && @@ -240,8 +257,7 @@ encode_manifest(const CheckpointManifest& m) { w.u(m.epoch, 8); w.u(m.tick_microseconds, 8); const auto versions = - (m.profile == CheckpointProfile::cellular_lives_v1 || - m.profile == CheckpointProfile::cellular_population_v1) + cellular_profile(m.profile) ? std::array{m.cellular_versions.rules, m.cellular_versions.envelope, m.cellular_versions.habitat, m.cellular_versions.coordinate, m.cellular_versions.genome, m.cellular_versions.development, @@ -298,8 +314,7 @@ decode_manifest(std::span bytes) { std::array versions{}; for (auto& v : versions) v = static_cast(r.u(4)); - if (m.profile == CheckpointProfile::cellular_lives_v1 || - m.profile == CheckpointProfile::cellular_population_v1) { + if (cellular_profile(m.profile)) { m.cellular_versions = {versions[0], versions[1], versions[2], versions[3], versions[4], versions[5], versions[6], versions[7], versions[8]}; } else { @@ -360,8 +375,7 @@ clone_manifest(const CheckpointManifest& source, const Sha256Digest& source_dige RegionKey target, Uuid commit, std::uint64_t payload_bytes, const Sha256Digest& payload_sha256, const BatchDigests& inherited) { if (!supported(source) || source.profile == CheckpointProfile::primitive_lives_v1 || - source.profile == CheckpointProfile::cellular_lives_v1 || - source.profile == CheckpointProfile::cellular_population_v1) + cellular_profile(source.profile)) return std::unexpected{CheckpointError::unsupported}; if (!structural(source) || target.world == source.key.world || commit == source.commit || inherited.trace_bytes != 0 || !zero(inherited.trace_sha256)) diff --git a/src/persistence/region_lease_store.cpp b/src/persistence/region_lease_store.cpp index f8bf3f9..c914f51 100644 --- a/src/persistence/region_lease_store.cpp +++ b/src/persistence/region_lease_store.cpp @@ -1502,26 +1502,11 @@ std::expected revoke_account_grants(Postgres& db, st return std::unexpected{AccountIdentityError::unknown}; return *revoked == "true"; } -std::expected -publish_population_checkpoint(Postgres& db, std::span manifest, - std::span trace, milliseconds timeout) { - const std::array parameters{bytea(manifest), bytea(trace)}; - auto rows = - db.query("SELECT * FROM evolution.publish_population_checkpoint($1::bytea,$2::bytea)", - parameters, timeout); - if (!rows) - return std::unexpected(classify(rows.error())); - auto head = parse_head(*rows); - if (!head) - return std::unexpected(malformed(true)); - return *head; -} -std::expected -publish_population_profile_transition(Postgres& db, std::span manifest, - milliseconds timeout) { - const std::array parameters{bytea(manifest)}; - auto rows = db.query("SELECT * FROM evolution.publish_population_profile_transition($1::bytea)", - parameters, timeout); +namespace { +std::expected publish_query(Postgres& db, const char* sql, + std::span parameters, + milliseconds timeout) { + auto rows = db.query(sql, parameters, timeout); if (!rows) return std::unexpected(classify(rows.error())); auto head = parse_head(*rows); @@ -1530,15 +1515,15 @@ publish_population_profile_transition(Postgres& db, std::span -resolve_population_commit(Postgres& db, RegionKey key, const Uuid& commit, - std::span manifest, - std::span trace, milliseconds timeout) { +resolve_query(Postgres& db, std::string_view function, RegionKey key, const Uuid& commit, + std::span manifest, std::span trace, + milliseconds timeout) { const std::array parameters{std::to_string(key.world), std::to_string(key.region), uuid_text(commit), bytea(manifest), bytea(trace)}; - auto rows = db.query("SELECT * FROM evolution.resolve_population_commit($1::evolution.u64," - "$2::evolution.u64,$3::uuid,$4::bytea,$5::bytea)", - parameters, timeout); + const auto sql = std::string{"SELECT * FROM evolution."} + std::string{function} + + "($1::evolution.u64,$2::evolution.u64,$3::uuid,$4::bytea,$5::bytea)"; + auto rows = db.query(sql, parameters, timeout); if (!rows) return std::unexpected(classify(rows.error())); if (rows->size() != 1 || (*rows)[0].size() != 6 || !(*rows)[0][0]) @@ -1558,17 +1543,17 @@ resolve_population_commit(Postgres& db, RegionKey key, const Uuid& commit, return ResolvedCommit{Resolution::committed, *head}; } std::expected, LeaseStoreFailure> -load_population_evidence(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& commit, - milliseconds timeout) { +evidence_query(Postgres& db, std::string_view function, RegionKey key, const LeaseGrant& lease, + const Uuid& commit, milliseconds timeout) { const std::array parameters{std::to_string(key.world), std::to_string(key.region), uuid_text(lease.holder), std::to_string(lease.epoch), uuid_text(commit)}; - auto rows = db.query( - "SELECT encode(trace,'hex'),source_commit::text,encode(source_manifest_sha256,'hex')," - "encode(source_payload_sha256,'hex') FROM " - "evolution.load_population_evidence($1::evolution.u64," - "$2::evolution.u64,$3::uuid,$4::evolution.u64,$5::uuid)", - parameters, timeout); + const auto sql = + std::string{"SELECT encode(trace,'hex'),source_commit::text,encode(source_manifest_sha256," + "'hex'),encode(source_payload_sha256,'hex') FROM evolution."} + + std::string{function} + + "($1::evolution.u64,$2::evolution.u64,$3::uuid,$4::evolution.u64,$5::uuid)"; + auto rows = db.query(sql, parameters, timeout); if (!rows) return std::unexpected(classify(rows.error())); if (rows->empty()) @@ -1598,4 +1583,59 @@ load_population_evidence(Postgres& db, RegionKey key, const LeaseGrant& lease, c return std::unexpected(malformed(false)); return std::optional{std::move(evidence)}; } +} // namespace +std::expected +publish_population_checkpoint(Postgres& db, std::span manifest, + std::span trace, milliseconds timeout) { + const std::array parameters{bytea(manifest), bytea(trace)}; + return publish_query( + db, "SELECT * FROM evolution.publish_population_checkpoint($1::bytea,$2::bytea)", + parameters, timeout); +} +std::expected +publish_population_profile_transition(Postgres& db, std::span manifest, + milliseconds timeout) { + const std::array parameters{bytea(manifest)}; + return publish_query(db, + "SELECT * FROM evolution.publish_population_profile_transition($1::bytea)", + parameters, timeout); +} +std::expected +resolve_population_commit(Postgres& db, RegionKey key, const Uuid& commit, + std::span manifest, + std::span trace, milliseconds timeout) { + return resolve_query(db, "resolve_population_commit", key, commit, manifest, trace, timeout); +} +std::expected, LeaseStoreFailure> +load_population_evidence(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& commit, + milliseconds timeout) { + return evidence_query(db, "load_population_evidence", key, lease, commit, timeout); +} +std::expected +publish_environment_checkpoint(Postgres& db, std::span manifest, + std::span trace, milliseconds timeout) { + const std::array parameters{bytea(manifest), bytea(trace)}; + return publish_query( + db, "SELECT * FROM evolution.publish_environment_checkpoint($1::bytea,$2::bytea)", + parameters, timeout); +} +std::expected +publish_environment_profile_transition(Postgres& db, std::span manifest, + milliseconds timeout) { + const std::array parameters{bytea(manifest)}; + return publish_query( + db, "SELECT * FROM evolution.publish_environment_profile_transition($1::bytea)", parameters, + timeout); +} +std::expected +resolve_environment_commit(Postgres& db, RegionKey key, const Uuid& commit, + std::span manifest, + std::span trace, milliseconds timeout) { + return resolve_query(db, "resolve_environment_commit", key, commit, manifest, trace, timeout); +} +std::expected, LeaseStoreFailure> +load_environment_evidence(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& commit, + milliseconds timeout) { + return evidence_query(db, "load_environment_evidence", key, lease, commit, timeout); +} } // namespace evo::persistence diff --git a/tests/environment_checkpoint_tests.cpp b/tests/environment_checkpoint_tests.cpp new file mode 100644 index 0000000..cd75c7f --- /dev/null +++ b/tests/environment_checkpoint_tests.cpp @@ -0,0 +1,169 @@ +#include "evo/persistence/region_checkpoint.hpp" +#include "fixtures/cellular_ecology.hpp" + +namespace { +using namespace evo; +using namespace evo::server; +using namespace evo::persistence; +using namespace evo::fixture; +template std::array filled(std::uint8_t byte) { + std::array out{}; + out.fill(byte); + return out; +} +std::unique_ptr joint() { + auto state = opening(7, true); + state = checked(state->migration_upgrade_candidate(), "boundary upgrade"); + state = checked(state->maturation_upgrade_candidate(), "maturation upgrade"); + auto population = + checked(CellularPopulation::create(std::move(state), {false, 900, 1'600}), "population"); + CellularEnvironmentConfig config; + config.geometry.box = {{0}, {0}, {0}, {6'000}, {6'000}, {10}}; + CellularInfallSource source; + source.id = 1; + source.width_um = 6'000; + source.height_um = 6'000; + source.start_tick = 1; + source.end_tick = ~std::uint64_t{0}; + source.mean_interval_ticks = 20; + source.per_bubble.energy.value = 1'000; + source.entitlement.energy.value = 1'000'000; + config.sources.push_back(source); + config.radiation_cap = {5}; + return checked(CellularEnvironment::create(std::move(population), std::move(config)), "joint"); +} +// A committed profile-5 head at the joint fixture's cut (bundle 11, population envelope 2). +CheckpointManifest population_head(const CellularEnvironment& state) { + CheckpointManifest m; + m.profile = CheckpointProfile::cellular_population_v1; + m.key = {state.state().world(), state.state().region()}; + m.frame = state.state().frame(); + m.commit = filled<16>(3); + m.sequence = 4; + m.predecessor_commit = filled<16>(2); + m.predecessor_digest = filled<32>(5); + m.begin_tick = state.tick(); + m.end_tick = state.tick(); + m.holder = filled<16>(6); + m.epoch = 2; + m.tick_microseconds = 50'000; + m.versions = environmental_v1_versions; + m.cellular_versions = cellular_v11_versions; + m.build = filled<32>(7); + m.history_profile = population_history_profile; + const auto bytes = state.population().snapshot(); + m.payload_bytes = bytes.size(); + m.payload_sha256 = filled<32>(8); + return m; +} +} // namespace + +int main() { + try { + auto state = joint(); + const auto head = population_head(*state); + require(encode_manifest(head) && supported(head), "profile-5 fixture head"); + const LeaseGrant lease{filled<16>(9), 3, head.frame, {}}; + const auto payload = state->snapshot(); + const auto transition = checked( + environment_profile_transition_manifest(head, filled<32>(10), filled<16>(11), lease, + filled<32>(12), payload.size(), filled<32>(13)), + "profile transition"); + require(transition.profile == CheckpointProfile::cellular_environment_v1 && + transition.history_profile == environment_history_profile && + transition.cellular_versions == cellular_v11_versions && + transition.begin_tick == transition.end_tick && + transition.batch == BatchDigests{} && + transition.sequence == head.sequence + 1 && supported(transition), + "transition changed bundle, tick or evidence"); + const auto encoded = checked(encode_manifest(transition), "encode transition"); + require(checked(decode_manifest(encoded), "decode transition") == transition, + "transition manifest round trip"); + const auto restored = + checked(restore_environment_payload(payload, transition), "restore joint payload"); + require(restored->snapshot() == payload, "joint payload restore changed bytes"); + + // Only a profile-5 owner-7 predecessor converts; there is no reverse transition. + require(!environment_profile_transition_manifest(transition, filled<32>(10), filled<16>(14), + lease, filled<32>(12), payload.size(), + filled<32>(13)), + "joint profile re-entered its own transition"); + auto raw = head; + raw.profile = CheckpointProfile::cellular_lives_v1; + raw.history_profile = cellular_revision_history_profile; + require(!environment_profile_transition_manifest(raw, filled<32>(10), filled<16>(11), lease, + filled<32>(12), payload.size(), + filled<32>(13)), + "raw profile skipped the population profile"); + auto old = head; + old.cellular_versions = cellular_v9_versions; + require(!environment_profile_transition_manifest(old, filled<32>(10), filled<16>(11), lease, + filled<32>(12), payload.size(), + filled<32>(13)), + "population envelope 1 entered the joint profile"); + auto wrong_lease = lease; + wrong_lease.frame += 1; + require(!environment_profile_transition_manifest(head, filled<32>(10), filled<16>(11), + wrong_lease, filled<32>(12), + payload.size(), filled<32>(13)), + "transition under another frame's lease"); + + const BatchDigests batch{server::environment_batch_header_bytes + + server::population_batch_header_bytes + 16 + 48, + filled<32>(15), + 0, + {}}; + const auto next = + checked(environment_successor_manifest(transition, filled<32>(16), filled<16>(17), + lease, transition.end_tick + 1, filled<32>(12), + payload.size(), filled<32>(18), batch), + "joint successor"); + require(next.profile == transition.profile && next.batch == batch && + next.begin_tick == transition.end_tick && supported(next), + "joint successor changed profile or evidence"); + const auto conversion = + checked(environment_successor_manifest(transition, filled<32>(16), filled<16>(17), + lease, transition.end_tick, filled<32>(12), + payload.size(), filled<32>(18), batch), + "joint conversion"); + require(conversion.begin_tick == conversion.end_tick, "conversion advanced its tick"); + require(!environment_successor_manifest(transition, filled<32>(16), filled<16>(17), lease, + transition.end_tick + 1, filled<32>(12), + payload.size(), filled<32>(18), {}), + "joint successor without a trace"); + require(!environment_successor_manifest(transition, filled<32>(16), filled<16>(17), lease, + transition.end_tick + 65, filled<32>(12), + payload.size(), filled<32>(18), batch), + "joint interval above 64 ticks"); + require(!environment_successor_manifest(head, filled<32>(16), filled<16>(17), lease, + head.end_tick + 1, filled<32>(12), payload.size(), + filled<32>(18), batch), + "population head published a joint successor"); + + auto history = transition; + history.history_profile = population_history_profile; + require(!encode_manifest(history) && !supported(history), "joint history 5 accepted"); + auto bundle12 = transition; + bundle12.cellular_versions = cellular_v12_versions; + require(!supported(bundle12), "joint profile admitted bundle 12"); + auto genesis = transition; + genesis.sequence = 0; + require(!encode_manifest(genesis), "joint profile provisioned without a source"); + auto later = transition; + later.end_tick += 1; + later.begin_tick = later.end_tick; + require(!restore_environment_payload(payload, later), "payload restored at another tick"); + require(!restore_environment_payload(state->population().snapshot(), transition), + "bare population restored as a joint result"); + auto unsupported = transition; + unsupported.cellular_versions = cellular_v10_versions; + require(restore_environment_payload(payload, unsupported).error() == + CheckpointError::unsupported, + "rules mismatch accepted"); + std::cout << "environment checkpoint contracts passed\n"; + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/tests/environment_publication_tests.cpp b/tests/environment_publication_tests.cpp new file mode 100644 index 0000000..70ed81f --- /dev/null +++ b/tests/environment_publication_tests.cpp @@ -0,0 +1,410 @@ +#include "evo/persistence/account_control_store.hpp" +#include "evo/persistence/environment_checkpoint_store.hpp" +#include "evo/persistence/population_interval_store.hpp" +#include "evo/persistence/sha256.hpp" +#include "fixtures/cellular_ecology.hpp" + +#include +#include + +void run_environment_publication_tests(); +namespace { +using namespace evo; +using namespace evo::server; +using namespace evo::persistence; +using namespace evo::fixture; +using namespace std::chrono_literals; +template T take(std::expected value, const char* where) { + if (!value) { + std::string message{where}; + if constexpr (requires { value.error().cause.sqlstate; }) + message += " SQLSTATE=" + std::string(value.error().cause.sqlstate.data(), 5); + if constexpr (requires { value.error().store.cause.sqlstate; }) + message += " SQLSTATE=" + std::string(value.error().store.cause.sqlstate.data(), 5); + throw std::runtime_error(message); + } + if constexpr (!std::is_void_v) + return std::move(*value); +} +std::string variable(const char* name) { + const auto* value = std::getenv(name); + require(value != nullptr, "missing private test environment"); + return value; +} +std::unique_ptr connection(bool owner = false) { + return take(Postgres::connect({variable("PGHOST"), + {}, + variable("PGPORT"), + variable("PGDATABASE"), + variable(owner ? "PGUSER" : "EVO_PG_RUNTIME_USER"), + {}, + {}}, + 5s), + "environment connection"); +} +// Mixed-byte identities, distinct from every other suite's commit namespace. +Uuid identity(unsigned number) { + Uuid id{}; + id[0] = 0xE6; + id[1] = 0x56; + id[7] = 0x3C; + id[14] = static_cast(number >> 8U); + id[15] = static_cast(number); + return id; +} +std::string hexadecimal(std::span bytes) { + std::string out = "\\x"; + constexpr char digits[] = "0123456789abcdef"; + for (const auto b : bytes) { + out.push_back(digits[b >> 4U]); + out.push_back(digits[b & 15U]); + } + return out; +} +struct Fixture { + RegionKey key{30411, 1}; + std::unique_ptr owner = connection(true), db = connection(); + std::unique_ptr objects; + LeaseGrant lease; + Sha256Digest build{}; + CommittedCheckpoint head; + std::unique_ptr raw; + std::unique_ptr joint; + unsigned next_id = 1; + Uuid id() { + return identity(next_id++); + } + CellularBatchHeader header(std::uint64_t count) const { + return {key.world, + key.region, + 1, + head.head.commit, + head.head.digest, + head.head.end_tick, + head.head.end_tick + count}; + } + [[nodiscard]] std::array builds() const { + return {build}; + } +}; + +// A real funded raw region with player-derived wildlife and fully suspended players. +void funded_source(Fixture& f) { + f.build.fill(0xE6); + const std::array build{hexadecimal(f.build)}; + take(f.owner->query("INSERT INTO evolution.retained_builds(build_identity) VALUES($1::bytea) " + "ON CONFLICT DO NOTHING", + build, 5s), + "retain test build"); + const std::array world{std::to_string(f.key.world)}; + take(f.owner->query("INSERT INTO evolution.life_allocators VALUES($1::evolution.u64,100)", + world, 5s), + "test allocator"); + f.objects = + take(FilesystemObjectStore::open(variable("PGHOST"), f.key, max_cellular_environment_bytes), + "environment object namespace"); + f.raw = opening(f.key.world - 200, true, 4, false, f.key.region); + f.head = + take(provision_cellular_region(*f.owner, *f.objects, f.key, 1, *f.raw, f.id(), f.build, 5s), + "empty cellular genesis"); + f.lease = + take(acquire_region_lease(*f.db, f.key, f.build, 60000ms, 5s), "environment test lease"); + CellularBatch birth{f.header(1), {{}}}; + for (std::uint8_t index = 1; index <= 4; ++index) { + Uuid account{}; + account[0] = 0xE6; + account.back() = index; + const auto control = + take(claim_account_control(*f.db, f.key, f.lease, account, 1, index, 60000ms, 5s), + "founder account control"); + const auto reserved = take( + reserve_primitive_life(*f.db, f.key, f.lease, account, control.generation, 1, 1, 5s), + "funded founder reservation"); + birth.ticks.front().births.push_back({account, 1, control.generation, reserved.identity}); + } + f.raw = take(replay_cellular_batch(*f.raw, birth), "funded founder replay"); + f.head = take(publish_cellular_batch(*f.db, *f.objects, f.lease, f.head, birth, *f.raw, f.id(), + f.build, 5s), + "funded founder publication") + .committed; + while (f.raw->next_bud_sequence() == 1 && f.raw->tick() < 20000) { + CellularBatch batch{f.header(1), {}}; + for (unsigned tick = 0; tick < 64; ++tick) { + auto intents = + take(wildlife_intents(*f.raw, f.raw->tick() + 1), "funded source wildlife"); + for (const auto& life : f.raw->lives()) { + if (life.account == LifeAccount{}) + continue; + CellularLifeIntent intent{life.identity.life, {}}; + intent.intent.bud = (f.raw->tick() + life.identity.life) % 32 == 0; + intents.push_back(intent); + } + std::ranges::sort(intents, {}, &CellularLifeIntent::life_id); + take(f.raw->advance_tick(intents), "funded source biology"); + batch.ticks.push_back({intents, {}, {}}); + if (f.raw->next_bud_sequence() > 1) + break; + } + batch.header.end_tick = f.raw->tick(); + f.head = take(publish_cellular_batch(*f.db, *f.objects, f.lease, f.head, batch, *f.raw, + f.id(), f.build, 5s), + "funded source batch") + .committed; + take(renew_region_lease(*f.db, f.key, f.lease, 60000ms, 5s), "source lease renewal"); + } + require(std::ranges::any_of(f.raw->lives(), + [](const auto& life) { return life.account == LifeAccount{}; }), + "durable fixture has no funded player-derived wildlife"); + const auto suspended_at = f.raw->tick() + cellular_linger_ticks + 1; + while (f.raw->tick() < suspended_at) { + auto h = f.header(std::min(std::uint64_t{64}, suspended_at - f.raw->tick())); + auto prefix = take(unattended_population_inputs(*f.raw, h), "raw linger inputs"); + f.raw = take(replay_cellular_batch(*f.raw, prefix), "raw linger biology"); + f.head = take(publish_cellular_batch(*f.db, *f.objects, f.lease, f.head, prefix, *f.raw, + f.id(), f.build, 5s), + "raw linger publication") + .committed; + std::vector endings; + for (const auto& corpse : f.raw->corpses()) + if (corpse.parent_life == 0) + endings.push_back(corpse.life_id); + if (!endings.empty()) + take(end_cellular_lives(*f.db, f.key, f.lease, f.head.head.commit, endings, 5s), + "raw linger endings"); + } + require(population_interval_safe(*f.raw), "joint source retained a vulnerable player"); +} + +CellularEnvironmentConfig declared(const CellularLives& state) { + CellularEnvironmentConfig config; + config.geometry.voxel = {4, 0, -1}; + config.geometry.box = {{0}, {0}, {0}, {state.area().width_um}, {state.area().height_um}, {12}}; + config.water = {{9'000'000}, {40'000}, {6'000}}; + CellularInfallSource source; + source.id = 3; + source.width_um = static_cast(state.area().width_um); + source.height_um = static_cast(state.area().height_um); + source.start_tick = state.tick() + 1; + source.end_tick = ~std::uint64_t{0}; + source.mean_interval_ticks = 9; + source.per_bubble.energy.value = 300'000; + source.per_bubble.matter[0].value = 30'000; + source.entitlement.energy.value = 1ULL << 40; + source.entitlement.matter[0].value = 1ULL << 40; + config.sources.push_back(source); + config.radiation_cap = {17}; + return config; +} +void recover(Fixture& f) { + auto db = connection(); + const auto builds = f.builds(); + auto recovered = + take(recover_environment_checkpoint(*db, *f.objects, f.key, f.lease, builds, 5s), + "joint durable recovery"); + f.head = std::move(recovered.committed); + f.joint = std::move(recovered.state); +} +struct Flows { + std::uint64_t energy{}, carbon{}, radiated{}; +}; +Flows recorded(Fixture& f, const Uuid& commit) { + const std::array id{[&] { + std::string out; + constexpr char digits[] = "0123456789abcdef"; + for (std::size_t i = 0; i < commit.size(); ++i) { + if (i == 4 || i == 6 || i == 8 || i == 10) + out.push_back('-'); + out.push_back(digits[commit[i] >> 4U]); + out.push_back(digits[commit[i] & 15U]); + } + return out; + }()}; + const auto rows = take(f.owner->query("SELECT imported_energy::text,imported_carbon::text," + "radiated_heat::text FROM evolution.environment_" + "publications WHERE commit_id=$1::uuid", + id, 5s), + "recorded environment flows"); + require(rows.size() == 1 && rows[0].size() == 3, "missing environment flow evidence"); + return {std::stoull(rows[0][0].value_or("x")), std::stoull(rows[0][1].value_or("x")), + std::stoull(rows[0][2].value_or("x"))}; +} +Flows sum(const CellularEnvironmentBatch& batch) { + Flows total; + for (const auto& flow : batch.flows) { + total.energy += flow.imported.energy.value; + total.carbon += flow.imported.matter[0].value; + total.radiated += flow.exported.value; + } + return total; +} +bool rejected(const auto& result, std::string_view sqlstate) { + if (result) + return false; + const auto& error = result.error(); + const std::string_view actual(error.store.cause.sqlstate.data(), 5); + if (error.kind != PublicationError::rejected || actual != sqlstate) + std::fprintf(stderr, "refusal kind %u, store %u, SQLSTATE %.5s\n", + static_cast(error.kind), static_cast(error.store.kind), + error.store.cause.sqlstate.data()); + return error.kind == PublicationError::rejected && actual == sqlstate; +} + +// The joint transition, owned intervals, conversions, retries and refusals. +void joint_profile(Fixture& f) { + f.head = take( + migrate_cellular_boundary_profile(*f.db, *f.objects, f.lease, f.head, f.id(), f.build, 5s), + "durable boundary upgrade"); + f.head = take(migrate_cellular_maturation_profile(*f.db, *f.objects, f.lease, f.head, f.id(), + f.build, 5s), + "durable maturation upgrade"); + require(!migrate_population_to_environment(*f.db, *f.objects, f.lease, f.head, f.id(), f.build, + declared(*f.raw), 5s), + "raw profile skipped the population profile"); + f.head = take(migrate_cellular_to_population(*f.db, *f.objects, f.lease, f.head, f.id(), + f.build, {false, 900, 1'600}, 5s), + "population entry"); + const auto builds = f.builds(); + auto population = + take(recover_population_checkpoint(*f.db, *f.objects, f.key, f.lease, builds, 5s), + "population recovery"); + const auto nested = population.state->snapshot(); + + const auto transition_id = f.id(); + const auto entered = + take(migrate_population_to_environment(*f.db, *f.objects, f.lease, f.head, transition_id, + f.build, declared(population.state->state()), 5s), + "joint profile transition"); + const PopulationPreparedCommit exact{f.key, transition_id, entered.manifest, {}}; + const auto retried = + take(retry_environment_prepared(*f.db, exact, 5s), "exact transition retry"); + require(retried.head == entered.head && retried.manifest == entered.manifest, + "transition retry changed its receipt"); + auto reused = decode_manifest(entered.manifest).value(); + reused.payload_sha256.fill(0x44); + const PopulationPreparedCommit conflicting{ + f.key, transition_id, encode_manifest(reused).value(), {}}; + require(rejected(retry_environment_prepared(*f.db, conflicting, 5s), "EVL07"), + "transition identity reused with other bytes"); + require(take(resolve_environment_prepared(*f.db, exact, 5s), "resolve transition").status == + Resolution::committed, + "committed transition unresolved"); + recover(f); + require(f.joint->population().snapshot() == nested && f.joint->imported() == Inventory{} && + f.joint->tick() == population.state->state().tick(), + "joint transition changed nested biology or crossed the boundary"); + + // An unattended interval needs its owned admission first. + auto interval = take(prepare_environment_interval(*f.joint, f.header(64)), "joint interval"); + require(rejected(publish_environment_batch(*f.db, *f.objects, f.lease, f.head, interval.batch, + *interval.state, f.id(), f.build, 5s), + "EVL18"), + "interval published without an owned admission"); + take(admit_population_interval(*f.db, f.key, f.lease, f.head.head, f.head.head.end_tick + 64, + 5s), + "joint interval admission"); + const auto interval_id = f.id(); + const PopulationPreparedCommit pending{ + f.key, interval_id, {}, take(encode_environment_batch(interval.batch), "encode interval")}; + f.head = take(publish_environment_batch(*f.db, *f.objects, f.lease, f.head, interval.batch, + *interval.state, interval_id, f.build, 5s), + "joint interval publication"); + const auto flows = recorded(f, interval_id), expected = sum(interval.batch); + require(flows.energy == expected.energy && flows.carbon == expected.carbon && + flows.radiated == expected.radiated && expected.energy > 0 && expected.radiated > 0, + "SQL flow evidence disagrees with the joint trace"); + const PopulationPreparedCommit published{f.key, interval_id, f.head.manifest, pending.trace}; + require(take(retry_environment_prepared(*f.db, published, 5s), "exact interval retry").head == + f.head.head, + "interval retry changed its receipt"); + recover(f); + require(f.joint->snapshot() == interval.state->snapshot(), "recovered interval diverged"); + + // Bypass publishers cannot commit or reinterpret joint evidence. + require(!publish_population_checkpoint(*f.db, f.head.manifest, pending.trace, 5s), + "population publisher accepted joint evidence"); + + // Whole-group conversion, then a cohort interval, each recovered exactly. + std::uint64_t genome = 0; + for (const auto& life : f.joint->state().lives()) + if (life.account == LifeAccount{}) + genome = life.identity.genome; + require(genome != 0, "no durable wildlife to convert"); + auto conversion = take(prepare_environment_conversion(*f.joint, f.header(0), {genome, true}), + "joint conversion"); + take( + admit_population_interval(*f.db, f.key, f.lease, f.head.head, f.head.head.end_tick + 8, 5s), + "blocking admission"); + require(rejected(publish_environment_batch(*f.db, *f.objects, f.lease, f.head, conversion.batch, + *conversion.state, f.id(), f.build, 5s), + "EVL18"), + "conversion overtook a pending interval"); + auto short_interval = + take(prepare_environment_interval(*f.joint, f.header(8)), "short interval"); + f.head = + take(publish_environment_batch(*f.db, *f.objects, f.lease, f.head, short_interval.batch, + *short_interval.state, f.id(), f.build, 5s), + "settle pending interval"); + recover(f); + conversion = take(prepare_environment_conversion(*f.joint, f.header(0), {genome, true}), + "joint conversion at the settled head"); + f.head = take(publish_environment_batch(*f.db, *f.objects, f.lease, f.head, conversion.batch, + *conversion.state, f.id(), f.build, 5s), + "joint conversion publication"); + recover(f); + require(!f.joint->population().members().empty() && + f.joint->snapshot() == conversion.state->snapshot(), + "recovered conversion diverged"); + take(admit_population_interval(*f.db, f.key, f.lease, f.head.head, f.head.head.end_tick + 64, + 5s), + "cohort interval admission"); + auto cohort = take(prepare_environment_interval(*f.joint, f.header(64)), "cohort interval"); + f.head = take(publish_environment_batch(*f.db, *f.objects, f.lease, f.head, cohort.batch, + *cohort.state, f.id(), f.build, 5s), + "cohort interval publication"); + recover(f); + require(f.joint->snapshot() == cohort.state->snapshot(), "recovered cohort interval diverged"); + std::printf("joint profile: head %llu at tick %llu, imported %llu pJ, radiated %llu pJ\n", + static_cast(f.head.head.sequence), + static_cast(f.head.head.end_tick), + static_cast(f.joint->imported().energy.value), + static_cast(f.joint->exported().value)); +} + +// Structurally valid but forged flows commit through the low-level SQL publisher, which +// does not replay biology; recovery replays the trace and refuses the head. +void forged_evidence(Fixture& f) { + take( + admit_population_interval(*f.db, f.key, f.lease, f.head.head, f.head.head.end_tick + 4, 5s), + "forged interval admission"); + auto interval = take(prepare_environment_interval(*f.joint, f.header(4)), "forged interval"); + interval.batch.flows[0].exported.value += 1; + const auto trace = take(encode_environment_batch(interval.batch), "encode forged trace"); + const auto payload = interval.state->snapshot(); + const auto previous = decode_manifest(f.head.manifest).value(); + const auto previous_digest = sha256(f.head.manifest).value(); + const auto manifest = + take(environment_successor_manifest(previous, previous_digest, f.id(), f.lease, + f.head.head.end_tick + 4, f.build, payload.size(), + sha256(payload).value(), + {trace.size(), sha256(trace).value(), 0, {}}), + "forged manifest"); + take(f.objects->put(payload), "forged payload object"); + take(f.objects->put(trace), "forged trace object"); + const auto bytes = take(encode_manifest(manifest), "encode forged manifest"); + take(publish_environment_checkpoint(*f.db, bytes, trace, 5s), "structural forged publication"); + auto db = connection(); + const auto builds = f.builds(); + const auto refused = + recover_environment_checkpoint(*db, *f.objects, f.key, f.lease, builds, 5s); + require(!refused && refused.error().kind == RecoveryError::tail, + "recovery accepted forged environmental flows"); +} +} // namespace + +void run_environment_publication_tests() { + Fixture fixture; + funded_source(fixture); + joint_profile(fixture); + forged_evidence(fixture); + std::puts("joint environment publication, intervals, conversions and recovery passed"); +} diff --git a/tests/postgres_tests.cpp b/tests/postgres_tests.cpp index 4debaf3..4aac0b3 100644 --- a/tests/postgres_tests.cpp +++ b/tests/postgres_tests.cpp @@ -10,6 +10,7 @@ void run_life_reservation_store_tests(); void run_primitive_publication_tests(); void run_cellular_publication_tests(); void run_population_publication_tests(); +void run_environment_publication_tests(); void run_probe_handoff_store_tests(); void run_region_action_tests(); void run_region_checkpoint_store_tests(); @@ -130,6 +131,10 @@ int main(int argc, char** argv) { run_population_publication_tests(); return 0; } + if (argc == 2 && std::string_view(argv[1]) == "--environment-only") { + run_environment_publication_tests(); + return 0; + } if (argc == 2 && std::string_view(argv[1]) == "--life-reservations-only") { run_life_reservation_store_tests(); run_primitive_publication_tests(); @@ -146,6 +151,7 @@ int main(int argc, char** argv) { run_primitive_publication_tests(); run_cellular_publication_tests(); run_population_publication_tests(); + run_environment_publication_tests(); run_probe_handoff_store_tests(); run_region_action_tests(); std::puts("region lease fencing integration passed"); diff --git a/tests/region_migration_tests.cpp b/tests/region_migration_tests.cpp index 4c3f36a..ebd27b3 100644 --- a/tests/region_migration_tests.cpp +++ b/tests/region_migration_tests.cpp @@ -221,6 +221,7 @@ void upgrade_across_migration_and_barrier() { " -v migration_sha_53=$(s 0053_funded_cellular_maturation.sql)" " -v migration_sha_54=$(s 0054_maturing_cellular_migration.sql)" " -v migration_sha_55=$(s 0055_retained_cellular_plans.sql)" + " -v migration_sha_56=$(s 0056_cellular_environment_publication.sql)" " -v runtime_role=evo_runtime_test -f migrations/apply.sql" " > /dev/null 2>&1") != 0); CHECK(history(*owner_db) == "1,2,3,4,5,6,7"); @@ -238,7 +239,7 @@ void upgrade_across_migration_and_barrier() { "tools/migrate-postgres.sh\" > /dev/null") == 0); CHECK(history(*owner_db) == "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24," "25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46," - "47,48,49,50,51,52,53,54,55"); + "47,48,49,50,51,52,53,54,55,56"); CHECK(scalar(*owner_db, "SELECT (c.condeferrable AND c.condeferred AND " "c.confrelid = 'evolution.region_commits'::regclass)::text " "FROM pg_catalog.pg_constraint c WHERE c.conrelid = " diff --git a/tools/ci/history-domain-check.sh b/tools/ci/history-domain-check.sh index 2a89319..4d263e5 100644 --- a/tools/ci/history-domain-check.sh +++ b/tools/ci/history-domain-check.sh @@ -58,15 +58,27 @@ for invalid in NULL "'00000000-0000-0000-0000-000000000000'"; do done # Reconstruct the exact pre-context metadata schema in this disposable database. # Restore the resulting real legacy backup through both supported workflows. -# Undo schema 55's plan validators, schema 54's three replacements and schema 53; -# backup must contain the actual pre-context function contracts and privileges. +# Undo schema 56's joint profile, schema 55's plan validators, schema 54's three +# replacements and schema 53; the backup must contain the actual pre-context +# function contracts and privileges. +awk '/^CREATE FUNCTION evolution\.admit_population_interval\(/ {capture=1; sub(/^CREATE FUNCTION/,"CREATE OR REPLACE FUNCTION")} capture {print} capture && /^END \$\$;/ {capture=0}' \ + "$root/migrations/0045_owned_population_intervals.sql" | sql > /dev/null awk '/^CREATE FUNCTION evolution\.(check_cellular_migration_trace|prepare_cellular_migration_freeze|publish_cellular_migration)\(/ {capture=1; sub(/^CREATE FUNCTION/,"CREATE OR REPLACE FUNCTION")} capture {print} capture && /^END \$\$;/ {capture=0}' \ "$root/migrations/0047_cellular_migration_freeze.sql" \ "$root/migrations/0049_cellular_migration_publication.sql" | sql > /dev/null awk '/^CREATE OR REPLACE FUNCTION evolution\.(cellular_mutation_selections|check_cellular_mutation_block)\(/ {capture=1} capture {print} capture && /^END \$\$;/ {capture=0}' \ "$root/migrations/0040_cellular_mutation_decline.sql" | sql > /dev/null sql -v runtime_role="$EVO_PG_RUNTIME_ROLE" > /dev/null <<'SQL' +DROP TRIGGER region_environment_evidence ON evolution.region_commits; +DROP FUNCTION evolution.refuse_unpublished_environment(); +DROP FUNCTION evolution.load_environment_evidence(evolution.u64,evolution.u64,uuid,evolution.u64,uuid); +DROP FUNCTION evolution.resolve_environment_commit(evolution.u64,evolution.u64,uuid,bytea,bytea); +DROP FUNCTION evolution.publish_environment_profile_transition(bytea); +DROP FUNCTION evolution.publish_environment_checkpoint(bytea,bytea); +DROP FUNCTION evolution.check_environment_trace(bytea,bytea); +DROP TABLE evolution.environment_publications, evolution.environment_profile_transitions; DROP FUNCTION evolution.parse_checkpoint_manifest(bytea); +DROP FUNCTION evolution.parse_checkpoint_manifest_v55(bytea); DROP FUNCTION evolution.parse_checkpoint_manifest_v54(bytea); ALTER FUNCTION evolution.parse_checkpoint_manifest_v52(bytea) RENAME TO parse_checkpoint_manifest; DROP FUNCTION evolution.cellular_births(bytea,bytea); diff --git a/tools/ci/postgres-check.sh b/tools/ci/postgres-check.sh index 0f499b0..945a944 100644 --- a/tools/ci/postgres-check.sh +++ b/tools/ci/postgres-check.sh @@ -3,6 +3,7 @@ set -euo pipefail # Usage: postgres-check.sh [--full|--sanitizer|--suite-only] # postgres-check.sh --life-reservations-only [] # postgres-check.sh --population-only +# postgres-check.sh --environment-only # postgres-check.sh --migration-faults (private loopback TLS) # --full (default) is the complete qualification: the C++ suite, the backup, DR and # upgrade process checks, then the SQL-only migration, permission and checksum checks. @@ -15,6 +16,7 @@ mode=full case "${1:-}" in --migration-faults) mode=migration-faults; shift ;; --population-only) mode=population; shift ;; + --environment-only) mode=environment; shift ;; --full) shift ;; --sanitizer) mode=sanitizer; shift ;; --suite-only) mode=suite-only; shift ;; @@ -82,6 +84,11 @@ if [ "$mode" = population ] || [ "$mode" = migration-faults ]; then "$1" --population-only exit 0 fi +if [ "$mode" = environment ]; then + phase environment + "$1" --environment-only + exit 0 +fi # M7.9 clone target: a separate database with its own runtime group and login, # so a clone shares neither the source's database nor its runtime credentials. psql -X -v ON_ERROR_STOP=1 -c 'CREATE ROLE evo_clone_runtime NOLOGIN' -c 'CREATE ROLE evo_clone_login LOGIN IN ROLE evo_clone_runtime' @@ -154,7 +161,7 @@ PGDATABASE=upgrade_path psql -X -v ON_ERROR_STOP=1 -1 -v "migration_sha=$v1_sha" -f "$root/migrations/0001_region_bootstrap.sql" > /dev/null test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',') FROM evolution.schema_migrations")" = 1 PGDATABASE=upgrade_path bash "$root/tools/migrate-postgres.sh" -test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55 +test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56 # v2 heads have no payload binding: v2->v3 fails closed and leaves history at v2. v2_sha="$(sha256sum "$root/migrations/0002_region_fencing.sql")" v2_sha="${v2_sha%% *}" @@ -175,7 +182,7 @@ PGDATABASE=v3_commits psql -X -v ON_ERROR_STOP=1 \ -c "INSERT INTO evolution.retained_builds VALUES (decode(repeat('b1', 32), 'hex'))" \ -c "SELECT evolution.provision_region_checkpoint(decode('4145564f52434d31' || '01000000' || '01000000' || '0100000000000000' || '0100000000000000' || '0900000000000000' || repeat('11', 16) || repeat('00', 8) || repeat('00', 16) || repeat('00', 32) || repeat('00', 16) || repeat('00', 16) || repeat('00', 8) || '50c3000000000000' || '020000000200000001000000010000000100000001000000010000000100000001000000' || repeat('b1', 32) || '00000000' || '00000000' || '01000000' || '6400000000000000' || repeat('a0', 32), 'hex'))" > /dev/null PGDATABASE=v3_commits bash "$root/tools/migrate-postgres.sh" -test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 55:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55 +test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 56:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56 test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) FROM evolution.region_commits")" = 1 # Review P1: a v4 database holding a published (possibly terminal) checkpoint has # no durable decisions for it. The v5 upgrade must refuse before changing anything. @@ -229,7 +236,7 @@ PGDATABASE=v6_populated psql -X -v ON_ERROR_STOP=1 \ -c "INSERT INTO evolution.retained_builds VALUES (decode(repeat('b1', 32), 'hex'))" \ -c "SELECT evolution.provision_region_checkpoint(decode('4145564f52434d31' || '02000000' || '01000000' || '0100000000000000' || '0100000000000000' || '0900000000000000' || repeat('11', 16) || repeat('00', 8) || repeat('00', 16) || repeat('00', 32) || repeat('00', 16) || repeat('00', 16) || repeat('00', 8) || '50c3000000000000' || '020000000200000001000000010000000100000001000000010000000100000001000000' || repeat('b1', 32) || '00000000' || '00000000' || '01000000' || '6400000000000000' || repeat('a0', 32) || repeat('00', 80), 'hex'))" > /dev/null PGDATABASE=v6_populated bash "$root/tools/migrate-postgres.sh" -test "$(PGDATABASE=v6_populated psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55 +test "$(PGDATABASE=v6_populated psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56 test "$(PGDATABASE=v6_populated psql -XAtc "SELECT count(*) FROM evolution.region_commits c, LATERAL evolution.parse_checkpoint_manifest(c.manifest) m WHERE m.commit_sequence = 0 AND m.predecessor_commit IS NULL")" = 1 test "$(PGDATABASE=v6_populated psql -XAtc "SELECT string_agg(conname, ',') FROM pg_constraint WHERE conrelid = 'evolution.region_heads'::regclass AND contype = 'c' AND pg_get_constraintdef(oid) LIKE '%end_tick%'")" = region_heads_published_tick phase permission-audit @@ -287,9 +294,9 @@ expect_audit_failure 'REVOKE EXECUTE ON FUNCTION evolution.load_world_cellular_m expect_audit_failure 'GRANT EXECUTE ON FUNCTION evolution.fork_restored_history_v1(uuid) TO evo_runtime_test' 'REVOKE EXECUTE ON FUNCTION evolution.fork_restored_history_v1(uuid) FROM evo_runtime_test' phase checksum-drift # Unknown future versions and checksum drift must not be repaired silently. -psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=56 WHERE version=55' +psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=57 WHERE version=56' if bash "$root/tools/migrate-postgres.sh"; then exit 1; fi -psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=55 WHERE version=56" +psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=56 WHERE version=57" psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET sha256=repeat('0',64) WHERE version=14" if bash "$root/tools/migrate-postgres.sh"; then exit 1; fi psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET sha256=repeat('0',64) WHERE version=13" diff --git a/tools/migrate-postgres.sh b/tools/migrate-postgres.sh index 5797942..079930a 100644 --- a/tools/migrate-postgres.sh +++ b/tools/migrate-postgres.sh @@ -122,6 +122,8 @@ digest_54="$(sha256sum "$root/migrations/0054_maturing_cellular_migration.sql")" digest_54="${digest_54%% *}" digest_55="$(sha256sum "$root/migrations/0055_retained_cellular_plans.sql")" digest_55="${digest_55%% *}" +digest_56="$(sha256sum "$root/migrations/0056_cellular_environment_publication.sql")" +digest_56="${digest_56%% *}" psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \ --set="migration_sha_2=$digest_2" --set="migration_sha_3=$digest_3" \ --set="migration_sha_4=$digest_4" --set="migration_sha_5=$digest_5" \ @@ -168,6 +170,6 @@ psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \ --set="migration_sha_51=$digest_51" \ --set="migration_sha_52=$digest_52" \ --set="migration_sha_53=$digest_53" --set="migration_sha_54=$digest_54" \ - --set="migration_sha_55=$digest_55" \ + --set="migration_sha_55=$digest_55" --set="migration_sha_56=$digest_56" \ --set="runtime_role=$runtime_role" \ --file="$root/migrations/apply.sql"