From 463b091056c7af40cc5557940d3a7ac489e2e37e Mon Sep 17 00:00:00 2001 From: dingo Date: Mon, 19 Oct 2026 06:23:51 -0400 Subject: [PATCH] feat(persistence): admit durable funded maturation upgrades --- README.md | 6 +- docs/STATUS.md | 18 ++ docs/adr/0157-funded-cellular-maturation.md | 4 + docs/adr/0158-durable-cellular-maturation.md | 52 +++++ docs/architecture/PERSISTENCE_ARCHITECTURE.md | 8 + docs/engineering/M12_REMAINING_GATES.md | 6 + docs/engineering/VALIDATION.md | 81 +++++++ include/evo/persistence/checkpoint_store.hpp | 6 + include/evo/persistence/region_checkpoint.hpp | 25 +- .../0053_funded_cellular_maturation.sql | 221 ++++++++++++++++++ migrations/apply.sql | 15 +- schemas/cellular-lives-v7.md | 6 + schemas/cellular-maturation-upgrade.md | 44 ++++ schemas/cellular-population-batch.md | 4 + schemas/cellular-population.md | 5 + src/persistence/cellular_checkpoint_store.cpp | 54 +++-- src/persistence/population_checkpoint.cpp | 53 ++++- src/persistence/region_checkpoint.cpp | 12 +- src/server/cellular_region_host.cpp | 3 +- tests/population_checkpoint_tests.cpp | 79 ++++++- tests/population_publication_tests.cpp | 142 +++++++++-- tests/region_migration_tests.cpp | 3 +- tools/ci/history-domain-check.sh | 13 +- tools/ci/postgres-check.sh | 10 +- tools/migrate-postgres.sh | 3 + 25 files changed, 799 insertions(+), 74 deletions(-) create mode 100644 docs/adr/0158-durable-cellular-maturation.md create mode 100644 migrations/0053_funded_cellular_maturation.sql create mode 100644 schemas/cellular-maturation-upgrade.md diff --git a/README.md b/README.md index 64d90c4..285d9b5 100644 --- a/README.md +++ b/README.md @@ -167,8 +167,10 @@ implemented; [TLS transport fault checks](docs/engineering/M12_MIGRATION_TRANSPO cover independent local path loss and owner replacement. Linked ecology and deployment qualification remain open. Portable opt-in [rules-9 maturation](docs/adr/0157-funded-cellular-maturation.md) now funds real -juveniles through adulthood and budding in the same raw/cohort kernel; durable -admission and autonomous ecological qualification remain separate gates. +juveniles through adulthood and budding in the same raw/cohort kernel. +[Schema 53's explicit durable upgrade](schemas/cellular-maturation-upgrade.md) +retains both representations and their full recovery evidence. Rules-9 migration, +census and autonomous ecological qualification remain separate gates. The microscope client is the M11 playtest path. `evo::simulation` remains graphics-, network- and database-independent. diff --git a/docs/STATUS.md b/docs/STATUS.md index 0617085..ea3efeb 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -1,5 +1,23 @@ # Project status and next steps +## Durable funded cellular maturation - 2026-10-19 + +[ADR-0158](adr/0158-durable-cellular-maturation.md)/schema 53 admits explicit +bundle-10 -> 11 maturation upgrades in either raw or population profile, using +the existing immutable version journal and current head/lease fence. Recovery +rederives full state, and upgraded hosts retain exact growth through publication, +replacement, interval settlement and representation changes. No automatic upgrade +or default/DEV provisioning change. + +Native Release passes **81/81**. Private Release and ASan/UBSan upgrade/fault +matrices, including prepared-handoff refusal with no partial journal evidence, +and final analysis/formatting pass. Detailed evidence and limits are recorded in +[validation](engineering/VALIDATION.md). The full private persistence/process, +backup/DR, migration-history and permission qualification reports **exit 0, 563 s**. +Rules-9 migration/census, autonomous +generations, shared environmental budgets, target-host cost, actual days and +product gates remain open; this is not completion of M12. + ## Portable M12 funded maturation prerequisite - 2026-10-18 [ADR-0157](adr/0157-funded-cellular-maturation.md) adds opt-in rules-9 funded diff --git a/docs/adr/0157-funded-cellular-maturation.md b/docs/adr/0157-funded-cellular-maturation.md index 128bf1b..3c109db 100644 --- a/docs/adr/0157-funded-cellular-maturation.md +++ b/docs/adr/0157-funded-cellular-maturation.md @@ -63,3 +63,7 @@ Rules-9 durable checkpoint admission, migration, registered census, environmenta coupling and client presentation require their own contract/qualification. Current durable bundle allowlists continue to reject this rule. No actual-days soak, deployment, World0 operation, extinction or M12 completion is inferred. + +ADR-0158/schema 53 subsequently admits explicit durable bundle-10 -> 11 upgrades. +The maturation rule above is unchanged; migration/census and ecological/product +qualification remain separate gates. diff --git a/docs/adr/0158-durable-cellular-maturation.md b/docs/adr/0158-durable-cellular-maturation.md new file mode 100644 index 0000000..b5ddadc --- /dev/null +++ b/docs/adr/0158-durable-cellular-maturation.md @@ -0,0 +1,52 @@ +# ADR-0158: Durable admission of funded cellular maturation + +## Status + +Accepted for the scoped M12 maturation durability gate, 2026-10-19. Extends +ADR-0146/0157. No deployment, automatic upgrade, balance acceptance or World0 change. + +## Decision + +Admit cellular bundle 11 `{9,7,2,1,1,4,3,2,1}` through schema 53. Reuse the +immutable `cellular_profile_migrations` journal and current lease/head fence. +Only explicit bundle 10 -> 11, same-profile (4 or 5), same-tick upgrades are +allowed. Preserve all stocks, organisms, genomes, growth, presence, boundary +flows, archive, policy, cohort membership and conversion sequence. No new genesis, +skipped prerequisite, downgrade or independently published upgrade record. + +The adapter verifies both predecessor bindings, derives and reads back the full +candidate object, then atomically records evidence/result/head. Pending intervals +and biological handoffs prevent a new upgrade. Exact retries retain original +bytes/receipt under current authority; changed bytes conflict. An uncertain +transaction retains the exact prepared request. Recovery verifies both complete +manifests/objects and the journal and rederives the recorded operation, selected +from the exact source/target tuple. No fallback to older biology or fresh genesis. + +Both raw trace 3 and population envelope/batch 2 keep their existing layouts. +The bound predecessor's rules select funded maturation. Representation changes +preserve the tuple. Parser-local SQL normalization can reuse the existing shape +checks, after checking real trace binding; stored bytes, strong digests and retry +identity always retain bundle 11. Legacy parser/publisher helpers remain private. + +Keep all original version meanings and migration checksums. Existing freeze, +interval, player-choice, permanent-death and dedicated-publication guards remain. +No worker upgrades automatically, and DEV/default provisioning stays rules 8. +An explicitly upgraded host can recover and continue either representation. +Biological migration still accepts bundle 10 only. Rules-9 census queries return +unsupported until separately qualified; a missing projection is never zero life +or evidence of extinction. + +## Alternatives and qualification + +A fresh opening loses funded ancestry and stock history. Reusing profile-change +evidence obscures a biological-rules change. A separate journal duplicates the +existing atomic authority. Extend its exact pair and replay dispatch instead. + +Require both profiles, real juvenile growth after upgrade, exact continuation and +profile conversion, stale/current leases, retries/conflicts, pending-work refusal, +ambiguous outcomes/process loss, missing/corrupt/false source or result objects, +rollback, retained history and runtime privilege audits on private PostgreSQL. +Run portable Release, sanitizers, analysis and the full persistence/backup/DR +matrix. This qualifies the retained biological rule and its storage path, not +autonomous multi-generation ecology, linked environmental budgets, actual days, +deployment-host costs, product readiness or M12 completion. diff --git a/docs/architecture/PERSISTENCE_ARCHITECTURE.md b/docs/architecture/PERSISTENCE_ARCHITECTURE.md index 6974d44..5bb24e3 100644 --- a/docs/architecture/PERSISTENCE_ARCHITECTURE.md +++ b/docs/architecture/PERSISTENCE_ARCHITECTURE.md @@ -1,5 +1,13 @@ # Persistence and region authority +ADR-0158/schema 53 admits the explicit same-profile bundle-10 -> 11 maturation +upgrade through the existing immutable version journal and lease/head fence. +Recovery rederives the complete retained transformation; raw trace 3 and population +batch 2 keep their layouts. Profile changes preserve the tuple. No new genesis, +automatic upgrade or default change. Biological migration and registered census +for rules 9 remain unsupported; an unavailable owner projection cannot become +zero life. See [the upgrade contract](../../schemas/cellular-maturation-upgrade.md). + M12.7c/schema 52 adds [pending migration evidence](../../schemas/ecology/census-pending-migrations-v1.md) to registered snapshots. Pending source/destination pointers, namespace, registry and heads share one metadata view. Exact captured capsules/ghosts are fully diff --git a/docs/engineering/M12_REMAINING_GATES.md b/docs/engineering/M12_REMAINING_GATES.md index 7fa9c75..2b39ca3 100644 --- a/docs/engineering/M12_REMAINING_GATES.md +++ b/docs/engineering/M12_REMAINING_GATES.md @@ -111,6 +111,12 @@ because the existing quarter-yield rounds down; finite nutrient motes/contact an space still matter. Durable rules-9 publication/migration/census, linked resources, automatic wildlife robustness, balance and actual-days qualification remain open. +ADR-0158/schema 53 now adds explicit durable bundle-10 -> 11 maturation admission, +using the existing immutable version journal and exact same-profile recovery. +Raw/cohort publication and representation changes preserve the retained rule. +This does not enable rules-9 biological migration/census, autonomous sustainability, +joint environmental budgets or actual-days qualification. + M12.6a's [regional census projection](../../schemas/ecology/regional-cellular-census-v1.md) independently implements local current-genome and descendant-closure queries. It keeps suspended stock and retained zero-carrier ancestry, separates exact cohort diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 67b3bce..43b54a7 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -1,5 +1,86 @@ # Validation report +## 2026-10-19 - Durable funded cellular maturation + +[ADR-0158](../adr/0158-durable-cellular-maturation.md)/schema 53 admits bundle 11 +through an explicit same-profile, same-tick bundle-10 upgrade. The existing +immutable version journal, fenced head transaction and exact source/target +recovery are reused. Raw trace 3 and population envelope/batch 2 keep their +layouts; no default, DEV provisioning, old golden or numbered migration changes. +The host can continue an explicitly upgraded owner in either representation. + +Executed locally: + +- MSVC default Release full CTest **81/81 passed, 17.72 s**, with NDEBUG. +- GCC 14 Release maturation/process/manifest contracts **3/3 passed, 1.59 s**. +- Private PostgreSQL raw/population upgrade, publication/history/migration/census + regression and host fault matrix **exit 0, 19 s** (17 s population). The final + raw/cohort pending-handoff upgrade refusal and journal-rollback assertions pass + in a separate complete matrix rerun, also **exit 0, 19 s**. +- Clang 19 ASan/UBSan manifest contracts and complete affected private matrix + passed **exit 0, 55 s** (53 s population), without sanitizer reports. The final + pending-handoff guard rerun passed **exit 0, 56 s** (54 s population), without + reports. These runs use private Unix-socket databases; the full qualification + separately runs the existing loopback TLS migration/storage matrix. +- The full frozen-executable private persistence/process/backup/DR and SQL suite + reported **exit 0, 563 s**: C++ integration/process/load/history **435 s**, TLS + migration/storage faults **44 s**, life reservations **3 s**, backup/restore + **31 s**, host-loss DR **8 s**, upgrade **10 s**, migration history **5 s**, + effective runtime permissions **14 s**, checksum/future refusal **8 s**, plus + setup/migration. It includes schema-53 admission and legacy pre-context restore. + The later test-only pending-handoff assertions passed the separate final + Release/ASan matrices above; no implementation changed after freezing. +- clang-tidy 19 passed on the three changed persistence sources, cellular host, + both checkpoint/publication tests and schema-migration integration tests. The + final pending-handoff assertions passed a separate recheck. Repository + clang-format 19, changed-script `bash -n` and `git diff --check` pass. + +The inline outer WSL launcher mishandled its status variable after the full suite +finished and left an empty completion file. The original log contains both the +all-phase success marker and the suite's own `timing (exit 0)` trap. A separate +successful verifier checked that complete final summary, every expected phase +and the exact 563-second total before recording the zero completion result. The +outer runner is now a saved shell file for future use; the original log was +preserved and unchanged tests were not repeated solely for that capture error. + +Both profile-4 and profile-5 candidates preserve complete state, policy, cohort +tags and conversion history. Ordinary raw continuation, batch-2 growth and +representation transitions retain bundle 11. A real, player-origin juvenile +increases its realized growth in a funded 64-tick interval, publishes under its +durable admission and recovers byte-identically. Host replacement restores the +upgraded population, settles retained admitted work with no fresh tick debt, +and materializes before returning-player lookup. No biological rule was changed +to manufacture this evidence. + +The reused fault matrix covers source-object absence/corruption, a valid but +unequal conserved target, pre-transaction publisher kill, uncertain transaction +retaining exact prepared bytes, duplicate/conflicting requests, ordinary publisher +process loss and exact owner replacement. The maturation cases additionally +reject stale upgrade authority, SQL-level skipped prerequisites and downgrades. +Pending intervals block upgrades. Both raw and cohort prepared handoffs also +block them: the head stays unchanged and no partial upgrade journal row remains. +Pure manifests reject repeated/skipped operations, incompatible tuples and genesis; +rules-8 manifests still reject rules-9 payloads, and unknown rules remain unsupported. + +The first private run exposed best-effort shutdown in a deliberately uncertain +host fixture: the next test could not immediately acquire its lease. The fixture +now confirms release through an independent connection before handing authority +to the next test. Another new positive growth test initially omitted its required +durable interval admission and correctly failed EVL18; adding the real admission +made the complete path pass. Neither fix relaxes runtime ownership/admission. + +Schema 53 adds no state table and preserves all previous migration bytes. Runtime +can execute only the existing fenced entry points; newly retained parser/publisher +helpers remain private. The legacy history-domain backup fixture restores the +exact pre-53 function definitions/grants before making its pre-context backup. + +Biological migration remains bundle 10 only. Rules-9 census is unsupported until +separately qualified, preventing a complete census rather than producing zero +life. Autonomous generations, linked environmental stocks, deployment-host cost, +actual days and product readiness remain open. No live database, deployment or +World0 operation occurred. TSan was not rerun: no shared-memory ownership or +scheduler concurrency changed. + ## 2026-10-18 - Portable M12 funded maturation prerequisite [ADR-0157](../adr/0157-funded-cellular-maturation.md) adds an explicit detached diff --git a/include/evo/persistence/checkpoint_store.hpp b/include/evo/persistence/checkpoint_store.hpp index 414403b..793fc25 100644 --- a/include/evo/persistence/checkpoint_store.hpp +++ b/include/evo/persistence/checkpoint_store.hpp @@ -267,6 +267,12 @@ migrate_cellular_boundary_profile(Postgres&, const FilesystemObjectStore&, const const CommittedCheckpoint&, Uuid commit, const Sha256Digest& build, std::chrono::milliseconds timeout, const PublishHooks& hooks = {}); +// ADR-0158: explicit bundle 10 -> 11 maturation, same journal and exact retry contract. +[[nodiscard]] std::expected +migrate_cellular_maturation_profile(Postgres&, const FilesystemObjectStore&, const LeaseGrant&, + const CommittedCheckpoint&, Uuid commit, + const Sha256Digest& build, std::chrono::milliseconds timeout, + const PublishHooks& hooks = {}); // Verify the recorded no-tick operation under current authority, replaying its // entire source/target payload. Version upgrades and profile transitions retain // separate evidence; neither is inferred solely from the target tuple. diff --git a/include/evo/persistence/region_checkpoint.hpp b/include/evo/persistence/region_checkpoint.hpp index 428ec0a..d8be0b3 100644 --- a/include/evo/persistence/region_checkpoint.hpp +++ b/include/evo/persistence/region_checkpoint.hpp @@ -25,8 +25,8 @@ enum class CheckpointProfile : std::uint8_t { environmental_v1 = 1, // Environmental envelope only; history_profile 0. environmental_lineage_v1 = 2, // LineageRegion container; history_profile 1. primitive_lives_v1 = 3, // Funded creation state; history_profile 2. - cellular_lives_v1 = 4, // Complete cellular state; history_profile 3. - cellular_population_v1 = 5, // Exact population envelope; nested cellular bundle 9 or 10. + cellular_lives_v1 = 4, // Complete cellular state; history_profile 3 or 4. + cellular_population_v1 = 5, // Exact populations; cellular bundle 9, 10 or 11. }; // history_profile 1: lineage archive format 1 with genome schema 1. inline constexpr std::uint32_t lineage_history_profile = 1; @@ -71,16 +71,20 @@ inline constexpr CellularVersionBundle cellular_v9_versions{8, 6, 2, 1, 1, 4, 3, // ADR-0145: portable boundary accounting, unchanged rules 8 on owner envelope 7. // Database publication requires its own explicit schema admission. inline constexpr CellularVersionBundle cellular_v10_versions{8, 7, 2, 1, 1, 4, 3, 2, 1}; +// ADR-0158: explicit funded maturation, retaining owner 7 and population envelope 2. +inline constexpr CellularVersionBundle cellular_v11_versions{9, 7, 2, 1, 1, 4, 3, 2, 1}; // Zero means this bundle has no population representation. [[nodiscard]] constexpr std::uint32_t population_envelope_version(const CellularVersionBundle& b) noexcept { - return b == cellular_v9_versions ? 1 : b == cellular_v10_versions ? 2 : 0; + return b == cellular_v9_versions ? 1 + : (b == cellular_v10_versions || b == cellular_v11_versions) ? 2 + : 0; } // Bundles with genome revisions: the revision history profile and format-3 traces. [[nodiscard]] constexpr bool cellular_revision_bundle(const CellularVersionBundle& b) noexcept { return b == cellular_v3_versions || b == cellular_v4_versions || b == cellular_v5_versions || b == cellular_v6_versions || b == cellular_v7_versions || b == cellular_v8_versions || - b == cellular_v9_versions || b == cellular_v10_versions; + b == cellular_v9_versions || b == cellular_v10_versions || b == cellular_v11_versions; } // Batch evidence bound by manifest v2 (ADR-0063); all zero at genesis. @@ -206,7 +210,7 @@ restore_cellular_payload(std::span payload, const Checkpoint [[nodiscard]] std::expected, CheckpointError> restore_population_payload(std::span payload, const CheckpointManifest& manifest); -// Dedicated profile 4 <-> profile 5 transition, preserving bundle 9 or 10 exactly. +// Dedicated profile 4 <-> profile 5 transition, preserving bundle 9, 10 or 11 exactly. [[nodiscard]] std::expected population_profile_transition_manifest(const CheckpointManifest& predecessor, const Sha256Digest& predecessor_digest, Uuid commit, @@ -227,6 +231,17 @@ cellular_boundary_upgrade_manifest(const CheckpointManifest& predecessor, [[nodiscard]] std::expected, CheckpointError> cellular_boundary_upgrade_payload(std::span payload, const CheckpointManifest& predecessor); +// ADR-0158: exact same-profile, no-tick bundle 10 -> 11 maturation upgrade. +// The caller independently binds source SHA-256 and fenced publication authority. +[[nodiscard]] std::expected +cellular_maturation_upgrade_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); +[[nodiscard]] std::expected, CheckpointError> +cellular_maturation_upgrade_payload(std::span payload, + const CheckpointManifest& predecessor); // 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/migrations/0053_funded_cellular_maturation.sql b/migrations/0053_funded_cellular_maturation.sql new file mode 100644 index 0000000..834a6d7 --- /dev/null +++ b/migrations/0053_funded_cellular_maturation.sql @@ -0,0 +1,221 @@ +-- ADR-0158: exact same-profile bundle 10 -> 11 funded-maturation upgrades. +ALTER FUNCTION evolution.parse_checkpoint_manifest(bytea) RENAME TO parse_checkpoint_manifest_v52; +REVOKE ALL ON FUNCTION evolution.parse_checkpoint_manifest_v52(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 c evolution.checkpoint_manifest; + b10 constant bytea := '\x080000000700000002000000010000000100000004000000030000000200000001000000'; + b11 constant bytea := '\x090000000700000002000000010000000100000004000000030000000200000001000000'; +BEGIN + IF m IS NOT NULL AND octet_length(m)=360 AND evolution.manifest_uint(m,12,4) IN (4,5) + AND substring(m FROM 161 FOR 36)=b11 THEN + c := evolution.parse_checkpoint_manifest_v52(overlay(m PLACING b10 FROM 161 FOR 36)); + c.versions := b11; + RETURN c; + END IF; + RETURN evolution.parse_checkpoint_manifest_v52(m); +END $$; +REVOKE ALL ON FUNCTION evolution.parse_checkpoint_manifest(bytea) FROM PUBLIC; + +ALTER FUNCTION evolution.cellular_births(bytea,bytea) RENAME TO cellular_births_v52; +REVOKE ALL ON FUNCTION evolution.cellular_births_v52(bytea,bytea) FROM PUBLIC, :"runtime_role"; +CREATE FUNCTION evolution.cellular_births(t bytea,m bytea) +RETURNS TABLE(account_id uuid,request_id evolution.u64,generation evolution.u64,identity evolution.u64,born evolution.u64) +LANGUAGE plpgsql IMMUTABLE SET search_path=pg_catalog,pg_temp AS $$ +DECLARE c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(m); +BEGIN + IF c.versions='\x090000000700000002000000010000000100000004000000030000000200000001000000'::bytea THEN + m := overlay(m PLACING '\x080000000700000002000000010000000100000004000000030000000200000001000000'::bytea FROM 161 FOR 36); + END IF; + RETURN QUERY SELECT * FROM evolution.cellular_births_v52(t,m); +END $$; +REVOKE ALL ON FUNCTION evolution.cellular_births(bytea,bytea) FROM PUBLIC; + +ALTER FUNCTION evolution.check_population_trace(bytea,bytea) RENAME TO check_population_trace_v52; +REVOKE ALL ON FUNCTION evolution.check_population_trace_v52(bytea,bytea) FROM PUBLIC, :"runtime_role"; +CREATE FUNCTION evolution.check_population_trace(m bytea,t bytea) RETURNS void +LANGUAGE plpgsql IMMUTABLE SET search_path=pg_catalog,pg_temp AS $$ +DECLARE c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(m); +BEGIN + -- Only a parser-local version tuple changes. The real trace/digest binding is + -- checked by the existing batch-2 validator; no committed bytes are normalized. + IF c.versions='\x090000000700000002000000010000000100000004000000030000000200000001000000'::bytea THEN + m := overlay(m PLACING '\x080000000700000002000000010000000100000004000000030000000200000001000000'::bytea FROM 161 FOR 36); + END IF; + PERFORM evolution.check_population_trace_v52(m,t); +END $$; +REVOKE ALL ON FUNCTION evolution.check_population_trace(bytea,bytea) FROM PUBLIC; + +ALTER FUNCTION evolution.publish_cellular_profile_migration(bytea) RENAME TO publish_cellular_profile_migration_v52; +REVOKE ALL ON FUNCTION evolution.publish_cellular_profile_migration_v52(bytea) FROM PUBLIC, :"runtime_role"; +CREATE OR REPLACE FUNCTION evolution.publish_cellular_profile_migration(p_manifest 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(p_manifest); + s evolution.checkpoint_manifest; + h evolution.region_heads%ROWTYPE; + p evolution.region_commits%ROWTYPE; + old evolution.region_commits%ROWTYPE; + frame evolution.u64; + d bytea := sha256(p_manifest); + source_bundle constant bytea := '\x080000000700000002000000010000000100000004000000030000000200000001000000'; + target_bundle constant bytea := '\x090000000700000002000000010000000100000004000000030000000200000001000000'; + zero32 constant bytea := decode(repeat('00', 32), 'hex'); +BEGIN + IF c.versions <> target_bundle THEN + RETURN QUERY SELECT * FROM evolution.publish_cellular_profile_migration_v52(p_manifest); + RETURN; + END IF; + IF c.profile NOT IN (4,5) OR c.commit_sequence = 0 OR c.begin_tick <> c.end_tick + OR c.end_tick = 0 OR c.versions <> target_bundle + OR evolution.manifest_uint(p_manifest, 228, 4) <> 0 + OR evolution.manifest_uint(p_manifest, 232, 4) <> (CASE c.profile WHEN 4 THEN 4 ELSE 5 END) + OR evolution.manifest_uint(p_manifest, 236, 4) <> 1 + OR c.trace_bytes <> 0 OR c.trace_sha256 <> zero32 + OR c.decision_count <> 0 OR c.decisions_sha256 <> zero32 THEN + RAISE EXCEPTION 'invalid funded-maturation profile migration' 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 <> p_manifest THEN + RAISE EXCEPTION 'cellular profile migration identity reused' USING ERRCODE = 'EVL07'; + END IF; + SELECT * INTO p FROM evolution.region_commits r + WHERE r.world_id = old.world_id AND r.region_id = old.region_id + AND r.commit_sequence = old.commit_sequence - 1; + IF NOT FOUND OR NOT EXISTS ( + SELECT 1 FROM evolution.cellular_profile_migrations m + WHERE m.target_commit = c.commit_id AND m.world_id = c.world_id + AND m.region_id = c.region_id AND m.source_sequence = p.commit_sequence + AND m.source_commit = p.commit_id AND m.source_manifest_sha256 = p.commit_digest + AND m.source_payload_sha256 = p.payload_sha256 AND m.source_versions = p.versions + AND m.target_sequence = old.commit_sequence AND m.target_manifest_sha256 = d + AND m.target_payload_sha256 = old.payload_sha256 AND m.target_versions = old.versions + AND m.source_commit = c.predecessor_commit + AND m.source_manifest_sha256 = c.predecessor_digest) THEN + RAISE EXCEPTION 'cellular profile migration 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 'maturation upgrade 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 'region head moved' USING ERRCODE = 'EVL03'; + END IF; + SELECT * INTO p FROM evolution.region_commits r + WHERE r.world_id = c.world_id AND r.region_id = c.region_id + AND r.commit_sequence = h.commit_sequence; + IF NOT FOUND THEN + RAISE EXCEPTION 'missing migration predecessor' USING ERRCODE = 'XX000'; + END IF; + s := evolution.parse_checkpoint_manifest(p.manifest); + IF s.profile <> c.profile OR s.versions <> source_bundle OR p.versions <> source_bundle + OR c.frame_id <> s.frame_id OR c.tick_microseconds <> s.tick_microseconds + OR s.commit_id <> c.predecessor_commit OR s.end_tick <> c.end_tick + OR evolution.manifest_uint(p.manifest, 232, 4) <> (CASE c.profile WHEN 4 THEN 4 ELSE 5 END) + OR NOT EXISTS (SELECT 1 FROM evolution.retained_builds b + WHERE b.build_identity = c.build_identity) THEN + RAISE EXCEPTION 'unsupported funded-maturation profile migration' USING ERRCODE = 'EVL06'; + 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; + IF c.frame_id <> frame THEN + RAISE EXCEPTION 'foreign frame' USING ERRCODE = '22023'; + END IF; + INSERT INTO evolution.cellular_profile_migrations(target_commit, world_id, region_id, + source_sequence, source_commit, source_manifest_sha256, source_payload_sha256, + source_versions, target_sequence, target_manifest_sha256, target_payload_sha256, + target_versions) + VALUES (c.commit_id, c.world_id, c.region_id, p.commit_sequence, p.commit_id, + p.commit_digest, p.payload_sha256, p.versions, c.commit_sequence, d, + c.payload_sha256, c.versions); + 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, p_manifest, + 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 frame, c.commit_sequence, c.commit_id, encode(d, 'hex'), c.end_tick; +END $$; +REVOKE ALL ON FUNCTION evolution.publish_cellular_profile_migration(bytea) FROM PUBLIC; +GRANT EXECUTE ON FUNCTION evolution.publish_cellular_profile_migration(bytea) TO :"runtime_role"; + + +-- Preserve the old private implementation for exact legacy-schema qualification. +ALTER FUNCTION evolution.publish_population_profile_transition_v44(bytea) RENAME TO publish_population_profile_transition_v52; +REVOKE ALL ON FUNCTION evolution.publish_population_profile_transition_v52(bytea) FROM PUBLIC, :"runtime_role"; +CREATE OR REPLACE FUNCTION evolution.publish_population_profile_transition_v44(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; + bundle9 constant bytea := '\x080000000600000002000000010000000100000004000000030000000200000001000000'; +BEGIN + IF c.profile NOT IN (4,5) OR c.versions NOT IN (bundle9,'\x080000000700000002000000010000000100000004000000030000000200000001000000'::bytea,'\x090000000700000002000000010000000100000004000000030000000200000001000000'::bytea) 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 population 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.population_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 AND x.target_profile = c.profile) THEN + RAISE EXCEPTION 'population 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; + 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 'population 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 population source' USING ERRCODE = 'XX000'; END IF; + s := evolution.parse_checkpoint_manifest(p.manifest); + IF c.profile = 5 AND 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 population transition' USING ERRCODE = 'EVL18'; + END IF; + IF s.profile NOT IN (4,5) OR s.profile = c.profile 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 population source' USING ERRCODE = 'EVL06'; + END IF; + INSERT INTO evolution.population_profile_transitions VALUES(c.commit_id,p.commit_id,p.commit_digest,p.payload_sha256,s.profile,c.profile); + 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_population_profile_transition_v44(bytea) FROM PUBLIC, :"runtime_role"; +INSERT INTO evolution.schema_migrations(version,sha256) VALUES(53,:'migration_sha_53'); diff --git a/migrations/apply.sql b/migrations/apply.sql index df14f3d..fdbf357 100644 --- a/migrations/apply.sql +++ b/migrations/apply.sql @@ -128,6 +128,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') SELECT :'history_v49' || ',50:' || :'migration_sha_50' AS history_v50 \gset SELECT :'history_v50' || ',51:' || :'migration_sha_51' AS history_v51 \gset SELECT :'history_v51' || ',52:' || :'migration_sha_52' AS history_v52 \gset + SELECT :'history_v52' || ',53:' || :'migration_sha_53' AS history_v53 \gset SELECT :'history' = :'history_v22' AS at_v22, :'history' = :'history_v23' AS at_v23, :'history' = :'history_v24' AS at_v24, @@ -158,8 +159,10 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') :'history' = :'history_v49' AS at_v49, :'history' = :'history_v50' AS at_v50, :'history' = :'history_v51' AS at_v51, - :'history' = :'history_v52' AS at_v52 \gset - \if :at_v52 + :'history' = :'history_v52' AS at_v52, + :'history' = :'history_v53' AS at_v53 \gset + \if :at_v53 + \elif :at_v52 \elif :at_v51 \elif :at_v50 \elif :at_v49 @@ -557,7 +560,8 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24, coalesce(max(version), 0) = 49 AS at_v49, coalesce(max(version), 0) = 50 AS at_v50, coalesce(max(version), 0) = 51 AS at_v51, - coalesce(max(version), 0) = 52 AS at_v52 + coalesce(max(version), 0) = 52 AS at_v52, + coalesce(max(version), 0) = 53 AS at_v53 FROM evolution.schema_migrations \gset \if :at_v24 \ir 0025_cellular_v2_bundle.sql @@ -778,6 +782,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24, \elif :at_v50 \elif :at_v51 \elif :at_v52 +\elif :at_v53 \else \echo 'Unknown or changed migration history after v43' DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$; @@ -818,6 +823,10 @@ SELECT coalesce(max(version), 0) = 51 AS needs_v52 FROM evolution.schema_migrati \if :needs_v52 \ir 0052_census_pending_migrations.sql \endif +SELECT coalesce(max(version), 0) = 52 AS needs_v53 FROM evolution.schema_migrations \gset +\if :needs_v53 + \ir 0053_funded_cellular_maturation.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 diff --git a/schemas/cellular-lives-v7.md b/schemas/cellular-lives-v7.md index 932768b..af219be 100644 --- a/schemas/cellular-lives-v7.md +++ b/schemas/cellular-lives-v7.md @@ -73,3 +73,9 @@ Saved mutation offers retain their original draw/context; rules 9 fund the chose successor at current growth. No context/body reset is encoded. Unknown rules fail closed; rules 9 in owner format 6 are refused. Biological migration and durable rules-9 admission remain unsupported pending their separate qualification. + +ADR-0158/schema 53 subsequently admits rules 9 as bundle 11, via an explicit +retained-source bundle-10 upgrade in the same raw/population profile. Full +publication/recovery evidence is specified in +[the maturation upgrade contract](cellular-maturation-upgrade.md). Biological +migration and census for this rule remain separate gates. diff --git a/schemas/cellular-maturation-upgrade.md b/schemas/cellular-maturation-upgrade.md new file mode 100644 index 0000000..0767aa6 --- /dev/null +++ b/schemas/cellular-maturation-upgrade.md @@ -0,0 +1,44 @@ +# Durable cellular maturation upgrade + +Authority: ADR-0158/schema 53. Bundle 11 is exactly +`{rules=9,envelope=7,habitat=2,coordinate=1,genome=1,development=4,founder=3,archive=2,mutation_state=1}`. +Raw profile 4 retains history 4/trace 3. Population profile 5 retains history 5, +envelope 2/batch 2. No wire field or payload limit changes. + +Only bundle 10 -> 11 may enter these rules. Require an existing non-genesis, +same-profile predecessor, current lease/frame/head/digest, nonzero retained tick, +new commit/next sequence, unchanged tick and empty batch evidence. The sole +biological change at that boundary is the explicit rules selector. Derive the +entire raw or nested population candidate from its verified retained object; +preserve all identities, stocks, flows, history, presence and cohort state. +Store and verify the candidate before invoking fenced publication. + +The existing immutable `cellular_profile_migrations` journal binds both complete +manifest/payload digests, sequences, commits and exact version tuples. Evidence, +result and head move share one transaction. An admitted interval or biological +handoff blocks new work. Ordinary publication and profile transitions cannot +change the tuple; profile transitions only wrap/unwrap identical biology. +Skipped prerequisites, repeats under new identities, downgrades and new genesis +are refused. Exact retries under their current writer's lease retain the original +request and receipt. A stale writer remains fenced; replacement recovery uses its +new authority to verify the original committed evidence. + +Recovery distinguishes version upgrades from representation changes and paired +biological handoffs by their retained evidence. For a maturation journal entry, +reconstruct the recorded bundle-10 -> 11 operation, verify both manifests and +objects, and compare the full derived target bytes. Equivalent total stock alone +is insufficient. Missing/corrupt/false evidence fails without older-head fallback. + +SQL checks may normalize parser-local version tuples to reuse unchanged trace +shape validation. Population trace 2 must first retain its actual length/SHA-256 +binding. Stored manifests, traces, digests and retry bytes remain exact bundle 11. +Old helper functions are private, and the effective runtime privilege audit still +allows only the existing fenced entry points. Earlier migration bytes are intact. + +No live/default auto-upgrade or new provisioning mode. An explicitly upgraded +owner can recover and continue raw/cohort biology, including interval admission, +settlement and materialization before returning-player operations. Biological +migration remains bundle 10 only. Rules-9 census is unsupported pending separate +qualification; unavailable evidence prevents a complete global result and cannot +prove zero life or terminal extinction. Ecology/environment/actual-days/product +qualification remains separate. diff --git a/schemas/cellular-population-batch.md b/schemas/cellular-population-batch.md index d3f6d32..e66fa78 100644 --- a/schemas/cellular-population-batch.md +++ b/schemas/cellular-population-batch.md @@ -70,6 +70,10 @@ selects maturation. Both raw and cohort replay perform the same funded growth and preserve exact member tags and stocks. This is not durable rules-9 admission; the rules-8 bundle allowlist remains unchanged. +ADR-0158/schema 53 subsequently admits this same batch-2 layout under bundle 11, +after an explicit retained bundle-10 maturation upgrade. Full recorded growth +replay and exact cohort boundaries remain required. No trace format is reinterpreted. + M12.4g/schema 46 adds the dedicated bundle-9 to 10 upgrade and full recovery contract. Bundle 10 admits batch 2 for ordinary intervals and manual conversion; bundle 9 still refuses it and remains bound to batch 1. Actual SHA-256 and wire diff --git a/schemas/cellular-population.md b/schemas/cellular-population.md index 389b3f0..289b7ca 100644 --- a/schemas/cellular-population.md +++ b/schemas/cellular-population.md @@ -64,3 +64,8 @@ sequence or wire-layout change is needed. An explicit no-tick maturation candida preserves all tags and upgrades only the nested rules. Both representations use the same funded-growth kernel. Envelope 1 remains rules 8. Durable bundle 10 remains rules 8 and does not admit this portable maturation upgrade. + +ADR-0158/schema 53 subsequently admits nested rules 9 as bundle 11. Entry requires +the retained bundle-10 same-profile upgrade; ordinary intervals use batch 2 and +profile transitions preserve the exact tuple. See +[durable maturation](cellular-maturation-upgrade.md). Default entry is unchanged. diff --git a/src/persistence/cellular_checkpoint_store.cpp b/src/persistence/cellular_checkpoint_store.cpp index 11401db..1d388e7 100644 --- a/src/persistence/cellular_checkpoint_store.cpp +++ b/src/persistence/cellular_checkpoint_store.cpp @@ -243,7 +243,8 @@ retry_cellular_profile_migration(Postgres& db, const CellularProfileMigrationPre manifest->cellular_versions == cellular_v3_versions)) || ((manifest->profile == CheckpointProfile::cellular_lives_v1 || manifest->profile == CheckpointProfile::cellular_population_v1) && - manifest->cellular_versions == cellular_v10_versions)) || + (manifest->cellular_versions == cellular_v10_versions || + manifest->cellular_versions == cellular_v11_versions))) || manifest->begin_tick != manifest->end_tick || manifest->batch != BatchDigests{}) return std::unexpected{migration_failure(PublicationError::candidate, {}, {}, prepared)}; @@ -276,11 +277,12 @@ retry_cellular_profile_migration(Postgres& db, const CellularProfileMigrationPre migration_failure(PublicationError::rejected, {}, published.error(), prepared)}; } +namespace { std::expected -migrate_cellular_boundary_profile(Postgres& db, const FilesystemObjectStore& objects, - const LeaseGrant& lease, const CommittedCheckpoint& predecessor, - Uuid commit, const Sha256Digest& build, milliseconds timeout, - const PublishHooks& hooks) { +migrate_rules_profile(Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease, + const CommittedCheckpoint& predecessor, Uuid commit, + const Sha256Digest& build, milliseconds timeout, const PublishHooks& hooks, + bool maturation) { const auto previous = decode_manifest(predecessor.manifest); const auto previous_digest = sha256(predecessor.manifest); if (!previous || !previous_digest || predecessor.head.digest != *previous_digest || @@ -292,14 +294,17 @@ migrate_cellular_boundary_profile(Postgres& db, const FilesystemObjectStore& obj auto source = objects.get({previous->payload_sha256, previous->payload_bytes}); if (!source) return std::unexpected(migration_failure(PublicationError::predecessor, source.error())); - auto payload = cellular_boundary_upgrade_payload(*source, *previous); + auto payload = maturation ? cellular_maturation_upgrade_payload(*source, *previous) + : cellular_boundary_upgrade_payload(*source, *previous); if (!payload) return std::unexpected(migration_failure(PublicationError::candidate)); const auto digest = sha256(*payload); if (!digest) return std::unexpected(migration_failure(PublicationError::digest)); - auto manifest = cellular_boundary_upgrade_manifest(*previous, *previous_digest, commit, lease, - build, payload->size(), *digest); + const auto derive = + maturation ? cellular_maturation_upgrade_manifest : cellular_boundary_upgrade_manifest; + auto manifest = + derive(*previous, *previous_digest, commit, lease, build, payload->size(), *digest); if (!manifest) return std::unexpected(migration_failure(PublicationError::candidate)); auto encoded = encode_manifest(*manifest); @@ -312,6 +317,23 @@ migrate_cellular_boundary_profile(Postgres& db, const FilesystemObjectStore& obj return retry_cellular_profile_migration(db, {previous->key, commit, std::move(*encoded)}, timeout); } +} // namespace +std::expected +migrate_cellular_boundary_profile(Postgres& db, const FilesystemObjectStore& objects, + const LeaseGrant& lease, const CommittedCheckpoint& predecessor, + Uuid commit, const Sha256Digest& build, milliseconds timeout, + const PublishHooks& hooks) { + return migrate_rules_profile(db, objects, lease, predecessor, commit, build, timeout, hooks, + false); +} +std::expected +migrate_cellular_maturation_profile(Postgres& db, const FilesystemObjectStore& objects, + const LeaseGrant& lease, const CommittedCheckpoint& predecessor, + Uuid commit, const Sha256Digest& build, milliseconds timeout, + const PublishHooks& hooks) { + return migrate_rules_profile(db, objects, lease, predecessor, commit, build, timeout, hooks, + true); +} std::expected migrate_cellular_profile_to_v2(Postgres& db, const FilesystemObjectStore& objects, @@ -597,18 +619,20 @@ verify_cellular_no_tick_checkpoint(Postgres& db, const FilesystemObjectStore& ob // evidence above is fenced by the CURRENT lease, which may be a newer epoch. const LeaseGrant recorded{m.holder, m.epoch, m.frame, milliseconds{0}}; const bool boundary = m.cellular_versions == cellular_v10_versions; - auto expected = - boundary ? cellular_boundary_upgrade_manifest(*source, *source_digest, m.commit, recorded, - m.build, m.payload_bytes, m.payload_sha256) - : cellular_profile_migration_manifest(*source, *source_digest, m.commit, recorded, - m.build, m.payload_bytes, m.payload_sha256); + const bool maturation = m.cellular_versions == cellular_v11_versions; + const auto derive = maturation ? cellular_maturation_upgrade_manifest + : boundary ? cellular_boundary_upgrade_manifest + : cellular_profile_migration_manifest; + auto expected = derive(*source, *source_digest, m.commit, recorded, m.build, m.payload_bytes, + m.payload_sha256); if (!expected || *expected != m) return fail(RecoveryError::history); auto bytes = objects.get({source->payload_sha256, source->payload_bytes}); if (!bytes) return fail(RecoveryError::tail); - if (boundary) { - auto upgraded = cellular_boundary_upgrade_payload(*bytes, *source); + if (boundary || maturation) { + auto upgraded = maturation ? cellular_maturation_upgrade_payload(*bytes, *source) + : cellular_boundary_upgrade_payload(*bytes, *source); if (!upgraded || !std::ranges::equal(*upgraded, payload)) return fail(RecoveryError::history); } else { diff --git a/src/persistence/population_checkpoint.cpp b/src/persistence/population_checkpoint.cpp index 9c1e722..3603271 100644 --- a/src/persistence/population_checkpoint.cpp +++ b/src/persistence/population_checkpoint.cpp @@ -80,23 +80,24 @@ restore_population_payload(std::span payload, const Checkpoi return std::move(*state); } +namespace { std::expected -cellular_boundary_upgrade_manifest(const CheckpointManifest& previous, - const Sha256Digest& previous_digest, Uuid commit, - const LeaseGrant& lease, const Sha256Digest& build, - std::uint64_t bytes, const Sha256Digest& digest) { +upgrade_manifest(const CheckpointManifest& previous, const Sha256Digest& previous_digest, + Uuid commit, const LeaseGrant& lease, const Sha256Digest& build, + std::uint64_t bytes, const Sha256Digest& digest, const CellularVersionBundle& from, + const CellularVersionBundle& to) { if (!supported(previous)) return std::unexpected(CheckpointError::unsupported); if (!encode_manifest(previous) || previous.sequence == 0 || previous.sequence == ~std::uint64_t{0} || previous.end_tick == 0 || - previous.cellular_versions != cellular_v9_versions || + previous.cellular_versions != from || (previous.profile != CheckpointProfile::cellular_lives_v1 && previous.profile != CheckpointProfile::cellular_population_v1) || previous_digest == Sha256Digest{} || commit == Uuid{} || commit == previous.commit || lease.frame != previous.frame || lease.epoch == 0 || lease.holder == Uuid{}) return std::unexpected(CheckpointError::chain); auto next = previous; - next.cellular_versions = cellular_v10_versions; + next.cellular_versions = to; next.commit = commit; ++next.sequence; next.predecessor_commit = previous.commit; @@ -114,9 +115,10 @@ cellular_boundary_upgrade_manifest(const CheckpointManifest& previous, } std::expected, CheckpointError> -cellular_boundary_upgrade_payload(std::span payload, - const CheckpointManifest& previous) { - if (!supported(previous) || previous.cellular_versions != cellular_v9_versions) +upgrade_payload(std::span payload, const CheckpointManifest& previous, + bool maturation) { + if (!supported(previous) || + previous.cellular_versions != (maturation ? cellular_v10_versions : cellular_v9_versions)) return std::unexpected(CheckpointError::unsupported); if (!encode_manifest(previous) || previous.sequence == 0 || previous.end_tick == 0) return std::unexpected(CheckpointError::chain); @@ -124,7 +126,8 @@ cellular_boundary_upgrade_payload(std::span payload, auto state = restore_cellular_payload(payload, previous); if (!state) return std::unexpected(state.error()); - auto upgraded = (*state)->migration_upgrade_candidate(); + auto upgraded = maturation ? (*state)->maturation_upgrade_candidate() + : (*state)->migration_upgrade_candidate(); if (!upgraded) return std::unexpected(CheckpointError::payload); return (*upgraded)->snapshot(); @@ -133,11 +136,39 @@ cellular_boundary_upgrade_payload(std::span payload, auto state = restore_population_payload(payload, previous); if (!state) return std::unexpected(state.error()); - auto upgraded = (*state)->migration_upgrade_candidate(); + auto upgraded = maturation ? (*state)->maturation_upgrade_candidate() + : (*state)->migration_upgrade_candidate(); if (!upgraded) return std::unexpected(CheckpointError::payload); return (*upgraded)->snapshot(); } return std::unexpected(CheckpointError::unsupported); } +} // namespace +std::expected +cellular_boundary_upgrade_manifest(const CheckpointManifest& previous, + const Sha256Digest& previous_digest, Uuid commit, + const LeaseGrant& lease, const Sha256Digest& build, + std::uint64_t bytes, const Sha256Digest& digest) { + return upgrade_manifest(previous, previous_digest, commit, lease, build, bytes, digest, + cellular_v9_versions, cellular_v10_versions); +} +std::expected +cellular_maturation_upgrade_manifest(const CheckpointManifest& previous, + const Sha256Digest& previous_digest, Uuid commit, + const LeaseGrant& lease, const Sha256Digest& build, + std::uint64_t bytes, const Sha256Digest& digest) { + return upgrade_manifest(previous, previous_digest, commit, lease, build, bytes, digest, + cellular_v10_versions, cellular_v11_versions); +} +std::expected, CheckpointError> +cellular_boundary_upgrade_payload(std::span payload, + const CheckpointManifest& previous) { + return upgrade_payload(payload, previous, false); +} +std::expected, CheckpointError> +cellular_maturation_upgrade_payload(std::span payload, + const CheckpointManifest& previous) { + return upgrade_payload(payload, previous, true); +} } // namespace evo::persistence diff --git a/src/persistence/region_checkpoint.cpp b/src/persistence/region_checkpoint.cpp index 0b970d3..73eb91f 100644 --- a/src/persistence/region_checkpoint.cpp +++ b/src/persistence/region_checkpoint.cpp @@ -66,7 +66,8 @@ bool structural(const CheckpointManifest& m) noexcept { (m.cellular_versions == cellular_v2_versions || m.cellular_versions == cellular_v3_versions || m.cellular_versions == cellular_v9_versions || - m.cellular_versions == cellular_v10_versions) && + m.cellular_versions == cellular_v10_versions || + m.cellular_versions == cellular_v11_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 && @@ -85,7 +86,8 @@ bool structural(const CheckpointManifest& m) noexcept { m.cellular_versions != cellular_v7_versions && m.cellular_versions != cellular_v8_versions && m.cellular_versions != cellular_v9_versions && - m.cellular_versions != cellular_v10_versions) || + m.cellular_versions != cellular_v10_versions && + m.cellular_versions != cellular_v11_versions) || (cellular && m.versions != environmental_v1_versions)) return false; if (primitive && (m.manifest_version != 2 || m.tick_microseconds != 50000 || @@ -99,7 +101,8 @@ bool structural(const CheckpointManifest& m) noexcept { if (m.sequence == 0) { // Boundary accounting is admitted only by an explicit retained-source // upgrade. Provisioning cannot skip its original opening-stock history. - if (cellular && m.cellular_versions == cellular_v10_versions) + if (cellular && (m.cellular_versions == cellular_v10_versions || + m.cellular_versions == cellular_v11_versions)) return false; const bool genesis = zero(m.holder) && m.epoch == 0 && m.begin_tick == m.end_tick && b.trace_bytes == 0 && zero(b.trace_sha256); @@ -196,7 +199,8 @@ bool supported(const CheckpointManifest& m) noexcept { m.cellular_versions == cellular_v7_versions || m.cellular_versions == cellular_v8_versions || m.cellular_versions == cellular_v9_versions || - m.cellular_versions == cellular_v10_versions) && + m.cellular_versions == cellular_v10_versions || + m.cellular_versions == cellular_v11_versions) && m.versions == environmental_v1_versions && m.tick_microseconds == 50000 && m.rng_streams == 0 && m.durability == DurabilityPolicy::single_volume_development; } diff --git a/src/server/cellular_region_host.cpp b/src/server/cellular_region_host.cpp index 845bc4a..3f4c01e 100644 --- a/src/server/cellular_region_host.cpp +++ b/src/server/cellular_region_host.cpp @@ -1173,7 +1173,8 @@ std::expected CellularRegionHost::set_population_m return {}; if (enabled && (!config_.population_policy || !queued_mutations_.empty() || !pending_endings_.empty() || - state_->rules_version() != cellular_lives_foraging_rules)) + (state_->rules_version() != cellular_lives_foraging_rules && + state_->rules_version() != cellular_lives_maturation_rules))) return std::unexpected(CellularPublishFailure{CellularHostError::not_ready, {}, {}, {}}); if (enabled && !population_interval_safe(*state_)) return std::unexpected(CellularPublishFailure{CellularHostError::not_ready, {}, {}, {}}); diff --git a/tests/population_checkpoint_tests.cpp b/tests/population_checkpoint_tests.cpp index a8aca0e..2a1f27b 100644 --- a/tests/population_checkpoint_tests.cpp +++ b/tests/population_checkpoint_tests.cpp @@ -194,10 +194,10 @@ void manifests() { !restore_cellular_payload(maturation->state().snapshot(), rules8_raw), "rules 9 bypassed durable raw admission"); auto unsupported_rules = rules8_population; - unsupported_rules.cellular_versions.rules = cellular_lives_maturation_rules; + unsupported_rules.cellular_versions.rules = cellular_lives_maturation_rules + 1; require(!supported(unsupported_rules) && !encode_manifest(unsupported_rules) && population_envelope_version(unsupported_rules.cellular_versions) == 0, - "portable rules 9 silently entered a durable bundle"); + "unknown rules silently entered a durable bundle"); auto back = checked(population_profile_transition_manifest( next, filled<32>(11), filled<16>(12), lease, source.build, state->snapshot().size(), filled<32>(13)), @@ -271,6 +271,78 @@ void manifests() { checked(decode_manifest(checked(encode_manifest(upgraded), "upgrade encode")), "upgrade decode") == upgraded, "boundary upgrade changed profile, clock, history or identity"); + auto matured_payload = + checked(cellular_maturation_upgrade_payload(upgraded_payload, upgraded), + "maturation payload upgrade"); + auto matured = checked(cellular_maturation_upgrade_manifest( + upgraded, filled<32>(19), filled<16>(20), lease, upgraded.build, + matured_payload.size(), filled<32>(21)), + "maturation manifest upgrade"); + auto expected_matured = upgraded; + expected_matured.cellular_versions = cellular_v11_versions; + expected_matured.commit = filled<16>(20); + ++expected_matured.sequence; + expected_matured.predecessor_commit = upgraded.commit; + expected_matured.predecessor_digest = filled<32>(19); + expected_matured.payload_bytes = matured_payload.size(); + expected_matured.payload_sha256 = filled<32>(21); + require(matured == expected_matured && supported(matured) && + checked(decode_manifest(checked(encode_manifest(matured), "maturation encode")), + "maturation decode") == matured, + "maturation upgrade changed profile, identity, stock metadata or time"); + require(!cellular_maturation_upgrade_payload(payload, base) && + !cellular_maturation_upgrade_payload(matured_payload, matured) && + !cellular_boundary_upgrade_payload(matured_payload, matured), + "maturation skipped prerequisite, repeated or downgraded"); + if (index == 0) { + auto old = checked(restore_cellular_payload(upgraded_payload, upgraded), + "maturation raw source"); + auto target = checked(restore_cellular_payload(matured_payload, matured), + "maturation raw target"); + require(target->snapshot() == + checked(old->maturation_upgrade_candidate(), "raw maturation oracle") + ->snapshot() && + target->current_total() == old->current_total(), + "raw maturation changed stocks"); + } else { + auto old = checked(restore_population_payload(upgraded_payload, upgraded), + "maturation cohort source"); + auto target = checked(restore_population_payload(matured_payload, matured), + "maturation cohort target"); + require(target->snapshot() == + checked(old->maturation_upgrade_candidate(), "cohort maturation oracle") + ->snapshot() && + target->conversion_sequence() == old->conversion_sequence() && + std::ranges::equal(target->members(), old->members()), + "maturation lost cohort history"); + auto biology = + checked(unattended_population_inputs(target->state(), header(*target, 64)), + "matured intentions"); + auto advanced = + checked(prepare_population_batch(*target, std::move(biology)), "matured interval"); + const auto matured_trace = + checked(encode_population_batch(advanced.batch), "matured trace"); + auto matured_next = + checked(population_successor_manifest( + matured, filled<32>(22), filled<16>(23), lease, matured.end_tick + 64, + matured.build, advanced.state->snapshot().size(), filled<32>(24), + {matured_trace.size(), filled<32>(25), 0, {}}), + "matured successor"); + require(matured_next.cellular_versions == cellular_v11_versions && + restore_population_payload(advanced.state->snapshot(), matured_next) + .has_value(), + "matured population cannot continue"); + } + auto switched = checked(population_profile_transition_manifest( + matured, filled<32>(22), filled<16>(23), lease, matured.build, + matured.payload_bytes, matured.payload_sha256), + "matured profile switch"); + require(switched.cellular_versions == cellular_v11_versions && + switched.profile != matured.profile, + "representation switch changed maturation rules"); + auto genesis_matured = matured; + genesis_matured.sequence = 0; + require(!encode_manifest(genesis_matured), "maturation bypassed retained opening history"); if (index == 0) { auto restored = checked(restore_cellular_payload(upgraded_payload, upgraded), "upgraded raw restore"); @@ -392,7 +464,8 @@ void manifests() { } require(population_envelope_version(cellular_v8_versions) == 0 && population_envelope_version(cellular_v9_versions) == 1 && - population_envelope_version(cellular_v10_versions) == 2, + population_envelope_version(cellular_v10_versions) == 2 && + population_envelope_version(cellular_v11_versions) == 2, "population bundle to envelope mapping differs"); } } // namespace diff --git a/tests/population_publication_tests.cpp b/tests/population_publication_tests.cpp index 8b76a67..f882ebd 100644 --- a/tests/population_publication_tests.cpp +++ b/tests/population_publication_tests.cpp @@ -1133,21 +1133,57 @@ void snapshot_guards(Fixture& f) { std::puts("consistent census snapshot guards and connection cleanup passed"); snapshot_transport_faults(f); } -void boundary_upgrades(Fixture& f) { +void boundary_upgrades(Fixture& f, bool maturation = false) { + const auto migrate = + [maturation](Postgres& db, const FilesystemObjectStore& objects, const LeaseGrant& lease, + const CommittedCheckpoint& previous, Uuid commit, const Sha256Digest& build, + std::chrono::milliseconds timeout, const PublishHooks& hooks = {}) { + return (maturation ? migrate_cellular_maturation_profile + : migrate_cellular_boundary_profile)(db, objects, lease, previous, + commit, build, timeout, hooks); + }; + const auto derive_manifest = + maturation ? cellular_maturation_upgrade_manifest : cellular_boundary_upgrade_manifest; + const auto target_bundle = maturation ? cellular_v11_versions : cellular_v10_versions; const std::array builds{f.build}; const auto original = f.head; - const auto expected_raw = - take(f.raw->migration_upgrade_candidate(), "raw upgrade oracle")->snapshot(); + if (maturation) { + const auto stale = f.lease; + take(release_region_lease(*f.db, f.key, f.lease, 5s), "release pre-maturation authority"); + f.lease = take(acquire_region_lease(*f.db, f.key, f.build, 60000ms, 5s), + "new maturation authority"); + const auto refused = migrate(*f.db, *f.objects, stale, f.head, f.id(), f.build, 5s); + require(!refused && refused.error().store.kind == LeaseStoreError::fenced && + take(load_region_checkpoint(*f.db, f.key, 5s), "stale upgrade head").head == + original.head, + "stale maturation writer changed head"); + } else { + // A validly encoded target cannot skip the retained accounting predecessor. + const auto base = take(decode_manifest(f.head.manifest), "skipped upgrade source"); + auto skipped = take(cellular_boundary_upgrade_manifest(base, f.head.head.digest, f.id(), + f.lease, f.build, base.payload_bytes, + base.payload_sha256), + "skipped target fixture"); + skipped.cellular_versions = cellular_v11_versions; + require(!publish_cellular_profile_migration( + *f.db, take(encode_manifest(skipped), "skipped encode"), 5s) && + take(load_region_checkpoint(*f.db, f.key, 5s), "skipped upgrade head").head == + original.head, + "SQL maturation skipped bundle 10"); + } + const auto expected_raw = take(maturation ? f.raw->maturation_upgrade_candidate() + : f.raw->migration_upgrade_candidate(), + "raw upgrade oracle") + ->snapshot(); // Rehearse the raw upgrade, recovery and ordinary trace-3 publication in one // transaction, then roll it back to retain a real bundle-9 source for profile 5. take(f.db->query("BEGIN", {}, 5s), "raw upgrade rollback rehearsal"); - auto raw_head = take( - migrate_cellular_boundary_profile(*f.db, *f.objects, f.lease, f.head, f.id(), f.build, 5s), - "raw accounting upgrade"); + auto raw_head = take(migrate(*f.db, *f.objects, f.lease, f.head, f.id(), f.build, 5s), + "raw accounting upgrade"); auto raw = take(recover_cellular_checkpoint(*f.db, *f.objects, f.key, f.lease, builds, 5s), "raw accounting recovery"); require(raw.state->snapshot() == expected_raw && - raw.manifest.cellular_versions == cellular_v10_versions && + raw.manifest.cellular_versions == target_bundle && raw_head.head.end_tick == original.head.end_tick, "raw accounting upgrade changed biology or time"); const CellularBatchHeader h{f.key.world, @@ -1192,8 +1228,7 @@ void boundary_upgrades(Fixture& f) { take(admit_population_interval(*f.db, f.key, f.lease, f.head.head, interval.batch.header.end_tick, 5s), "upgrade pending interval"); - auto pending = - migrate_cellular_boundary_profile(*f.db, *f.objects, f.lease, f.head, f.id(), f.build, 5s); + auto pending = migrate(*f.db, *f.objects, f.lease, f.head, f.id(), f.build, 5s); require(!pending && std::string(pending.error().store.cause.sqlstate.data(), 5) == "EVL18", "accounting upgrade discarded admitted work"); f.head = take(publish_population_batch(*f.db, *f.objects, f.lease, f.head, interval.batch, @@ -1202,8 +1237,9 @@ void boundary_upgrades(Fixture& f) { recover(f); const auto before = f.head; const auto source = take(decode_manifest(before.manifest), "accounting source manifest"); - const auto upgraded_population = - take(f.population->migration_upgrade_candidate(), "cohort upgrade oracle"); + const auto upgraded_population = take(maturation ? f.population->maturation_upgrade_candidate() + : f.population->migration_upgrade_candidate(), + "cohort upgrade oracle"); const auto expected = upgraded_population->snapshot(); const auto altered = take(CellularPopulation::create(restore(upgraded_population->state()), {false, 1200, 1800}), @@ -1212,10 +1248,10 @@ void boundary_upgrades(Fixture& f) { altered->snapshot() != expected, "unequal upgrade fixture must retain biological totals"); const auto altered_object = take(f.objects->put(altered->snapshot()), "unequal upgrade object"); - const auto altered_manifest = take( - cellular_boundary_upgrade_manifest(source, before.head.digest, f.id(), f.lease, f.build, - altered_object.bytes, altered_object.sha256), - "unequal upgrade manifest"); + const auto altered_manifest = + take(derive_manifest(source, before.head.digest, f.id(), f.lease, f.build, + altered_object.bytes, altered_object.sha256), + "unequal upgrade manifest"); take(f.db->query("BEGIN", {}, 5s), "unequal upgrade recovery rehearsal"); take(publish_cellular_profile_migration( *f.db, take(encode_manifest(altered_manifest), "unequal upgrade encoding"), 5s), @@ -1234,8 +1270,8 @@ void boundary_upgrades(Fixture& f) { auto db = connection(); PublishHooks hooks; hooks.before_transaction = [] { ::kill(::getpid(), SIGKILL); }; - [[maybe_unused]] const auto result = migrate_cellular_boundary_profile( - *db, *f.objects, f.lease, before, identity(60000), f.build, 5s, hooks); + [[maybe_unused]] const auto result = + migrate(*db, *f.objects, f.lease, before, identity(60000), f.build, 5s, hooks); } catch (...) { ::_exit(31); } @@ -1255,8 +1291,7 @@ void boundary_upgrades(Fixture& f) { {}, 5s), "hold accounting reply"); auto timed = connection(); - auto unknown = migrate_cellular_boundary_profile(*timed, *f.objects, f.lease, before, f.id(), - f.build, 300ms); + auto unknown = migrate(*timed, *f.objects, f.lease, before, f.id(), f.build, 300ms); require(!unknown && unknown.error().kind == PublicationError::unknown && !unknown.error().prepared.manifest.empty(), "uncertain accounting upgrade lost its exact request"); @@ -1271,8 +1306,21 @@ void boundary_upgrades(Fixture& f) { recover(f); require(f.population->snapshot() == expected && f.head.head.end_tick == before.head.end_tick && take(decode_manifest(f.head.manifest), "upgraded population manifest") - .cellular_versions == cellular_v10_versions, + .cellular_versions == target_bundle, "accounting upgrade changed biology, cohort history or time"); + if (maturation) { + auto downgrade = take(decode_manifest(f.head.manifest), "downgrade fixture"); + downgrade.predecessor_commit = downgrade.commit; + downgrade.predecessor_digest = f.head.head.digest; + downgrade.commit = f.id(); + ++downgrade.sequence; + downgrade.cellular_versions = cellular_v10_versions; + require(!publish_cellular_profile_migration( + *f.db, take(encode_manifest(downgrade), "downgrade encode"), 5s) && + take(load_region_checkpoint(*f.db, f.key, 5s), "downgrade head").head == + f.head.head, + "SQL downgraded maturation biology"); + } auto changed = unknown.error().prepared; auto changed_manifest = take(decode_manifest(changed.manifest), "changed accounting manifest"); changed_manifest.payload_sha256[0] ^= 1; @@ -1280,9 +1328,8 @@ void boundary_upgrades(Fixture& f) { auto conflict = retry_cellular_profile_migration(*resolver, changed, 5s); require(!conflict && conflict.error().store.kind == LeaseStoreError::commit_conflict, "accounting retry accepted changed bytes"); - require( - !migrate_cellular_boundary_profile(*f.db, *f.objects, f.lease, f.head, f.id(), f.build, 5s), - "accounting upgrade applied twice"); + require(!migrate(*f.db, *f.objects, f.lease, f.head, f.id(), f.build, 5s), + "accounting upgrade applied twice"); const auto path = f.objects->directory() / hexadecimal(source.payload_sha256, false); const auto backup = path.string() + ".upgrade-test"; std::filesystem::rename(path, backup); @@ -1325,6 +1372,26 @@ void boundary_upgrades(Fixture& f) { take(load_region_checkpoint(*f.db, f.key, 5s), "head after old trace").head == f.head.head, "bundle 10 reinterpreted retained population trace 1"); + if (maturation) { + auto growing = unattended(f); + bool grew = false; + for (const auto& life : growing.state->state().lives()) { + const auto old = std::ranges::find(f.population->state().lives(), life.identity.life, + [](const auto& cell) { return cell.identity.life; }); + grew |= old != f.population->state().lives().end() && + life.growth.realized_per_thousand > old->growth.realized_per_thousand; + } + require(grew, "durable maturation fixture never grew a real juvenile"); + take(admit_population_interval(*f.db, f.key, f.lease, f.head.head, + growing.batch.header.end_tick, 5s), + "admit funded growth interval"); + f.head = take(publish_population_batch(*f.db, *f.objects, f.lease, f.head, growing.batch, + *growing.state, f.id(), f.build, 5s), + "funded maturation publication"); + recover(f); + require(f.population->snapshot() == growing.state->snapshot(), + "funded growth recovery differs"); + } // Exercise the existing publication/process-loss matrix with batch 2 as well. process_loss(f, 0); process_loss(f, 1); @@ -2667,6 +2734,18 @@ void migration_freezes(Fixture& f) { } const auto capsule_manifest = take(decode_manifest(destination_head.manifest), "capsule manifest"); + const auto denied_upgrade_id = f.id(); + const auto denied_upgrade = migrate_cellular_maturation_profile( + *f.db, *f.objects, f.lease, f.head, denied_upgrade_id, f.build, 5s); + require( + !denied_upgrade && + std::string(denied_upgrade.error().store.cause.sqlstate.data(), 5) == "EVL18" && + !take(load_cellular_profile_migration(*f.db, f.key, f.lease, denied_upgrade_id, 5s), + "frozen maturation journal") + .has_value() && + take(load_region_checkpoint(*f.db, f.key, 5s), "frozen maturation head").head == + f.head.head, + "maturation escaped a frozen handoff or retained partial upgrade evidence"); const auto capsule_path = f.objects->directory() / hexadecimal(capsule_manifest.payload_sha256, false); const auto backup = capsule_path.string() + ".freeze-test"; @@ -3176,6 +3255,7 @@ void host_ownership_and_admission(Fixture& f) { host->state()->snapshot() == unknown_expected.state->state().snapshot(), "admission retry chose different work"); const auto final_bytes = host->state()->snapshot(); + const auto final_authority = take(host->authority(), "final host authority"); Uuid account{}; account.back() = 1; // A returning player's operation completes the durable reverse transition before lookup. @@ -3184,6 +3264,11 @@ void host_ownership_and_admission(Fixture& f) { host->state()->tick() == tick + 192, "player operation did not materialize first"); host->drain(); + // drain is best effort after the deliberate unknown-admission connection + // loss. Confirm the fixture handoff before another test acquires authority. + const auto released = release_region_lease(*f.db, f.key, final_authority, 5s); + require(released.has_value() || released.error().kind == LeaseStoreError::fenced, + "final host authority release failed"); } } // namespace void run_population_publication_tests() { @@ -3204,5 +3289,16 @@ void run_population_publication_tests() { boundary_upgrades(fixture); migration_freezes(fixture); host_ownership_and_admission(fixture); + fixture.lease = take(acquire_region_lease(*fixture.db, fixture.key, fixture.build, 60000ms, 5s), + "maturation replacement authority"); + const std::array retained{fixture.build}; + auto maturation_source = + take(recover_cellular_checkpoint(*fixture.db, *fixture.objects, fixture.key, fixture.lease, + retained, 5s), + "maturation source recovery"); + fixture.head = std::move(maturation_source.committed); + fixture.raw = std::move(maturation_source.state); + boundary_upgrades(fixture, true); + host_ownership_and_admission(fixture); std::puts("population publication, exact recovery, process loss and fencing passed"); } diff --git a/tests/region_migration_tests.cpp b/tests/region_migration_tests.cpp index c45394e..8bc8834 100644 --- a/tests/region_migration_tests.cpp +++ b/tests/region_migration_tests.cpp @@ -218,6 +218,7 @@ void upgrade_across_migration_and_barrier() { " -v migration_sha_50=$(s 0050_world_region_catalog.sql)" " -v migration_sha_51=$(s 0051_database_history_domain.sql)" " -v migration_sha_52=$(s 0052_census_pending_migrations.sql)" + " -v migration_sha_53=$(s 0053_funded_cellular_maturation.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"); @@ -235,7 +236,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"); + "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"); 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 f1b8c89..c14b414 100644 --- a/tools/ci/history-domain-check.sh +++ b/tools/ci/history-domain-check.sh @@ -58,7 +58,18 @@ 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. -sql > /dev/null <<'SQL' +sql -v runtime_role="$EVO_PG_RUNTIME_ROLE" > /dev/null <<'SQL' +DROP FUNCTION evolution.parse_checkpoint_manifest(bytea); +ALTER FUNCTION evolution.parse_checkpoint_manifest_v52(bytea) RENAME TO parse_checkpoint_manifest; +DROP FUNCTION evolution.cellular_births(bytea,bytea); +ALTER FUNCTION evolution.cellular_births_v52(bytea,bytea) RENAME TO cellular_births; +DROP FUNCTION evolution.check_population_trace(bytea,bytea); +ALTER FUNCTION evolution.check_population_trace_v52(bytea,bytea) RENAME TO check_population_trace; +DROP FUNCTION evolution.publish_cellular_profile_migration(bytea); +ALTER FUNCTION evolution.publish_cellular_profile_migration_v52(bytea) RENAME TO publish_cellular_profile_migration; +GRANT EXECUTE ON FUNCTION evolution.publish_cellular_profile_migration(bytea) TO :"runtime_role"; +DROP FUNCTION evolution.publish_population_profile_transition_v44(bytea); +ALTER FUNCTION evolution.publish_population_profile_transition_v52(bytea) RENAME TO publish_population_profile_transition_v44; DROP FUNCTION evolution.fork_restored_history_v1(uuid); DROP FUNCTION evolution.load_world_cellular_migrations_v1(evolution.u64); DROP FUNCTION evolution.load_history_domain_v1(); diff --git a/tools/ci/postgres-check.sh b/tools/ci/postgres-check.sh index cc9096e..6c463a8 100644 --- a/tools/ci/postgres-check.sh +++ b/tools/ci/postgres-check.sh @@ -154,7 +154,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 +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 # 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 +175,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")" = 52: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 +test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 53: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 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 +229,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 +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 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 +287,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=53 WHERE version=52' +psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=54 WHERE version=53' 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=52 WHERE version=53" +psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=53 WHERE version=54" 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 fb93e13..a732f5f 100644 --- a/tools/migrate-postgres.sh +++ b/tools/migrate-postgres.sh @@ -116,6 +116,8 @@ digest_51="$(sha256sum "$root/migrations/0051_database_history_domain.sql")" digest_51="${digest_51%% *}" digest_52="$(sha256sum "$root/migrations/0052_census_pending_migrations.sql")" digest_52="${digest_52%% *}" +digest_53="$(sha256sum "$root/migrations/0053_funded_cellular_maturation.sql")" +digest_53="${digest_53%% *}" 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" \ @@ -161,5 +163,6 @@ psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \ --set="migration_sha_50=$digest_50" \ --set="migration_sha_51=$digest_51" \ --set="migration_sha_52=$digest_52" \ + --set="migration_sha_53=$digest_53" \ --set="runtime_role=$runtime_role" \ --file="$root/migrations/apply.sql"