diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b5b313..57bd452 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,7 +80,8 @@ evo_owned_target(evo_biology) # M11.2 portable lifecycle orchestration; no dependency from the foundation proof. add_library(evo_lifecycle STATIC src/server/lifecycle/primitive_lives.cpp - src/server/lifecycle/primitive_batch.cpp src/server/lifecycle/cellular_lives.cpp) + src/server/lifecycle/primitive_batch.cpp src/server/lifecycle/cellular_lives.cpp + src/server/lifecycle/cellular_batch.cpp) add_library(evo::lifecycle ALIAS evo_lifecycle) target_link_libraries(evo_lifecycle PUBLIC evo::biology) evo_owned_target(evo_lifecycle) diff --git a/docs/adr/0113-cellular-replay-trace.md b/docs/adr/0113-cellular-replay-trace.md new file mode 100644 index 0000000..8d21941 --- /dev/null +++ b/docs/adr/0113-cellular-replay-trace.md @@ -0,0 +1,47 @@ +# ADR-0113: Cellular replay trace + +## Status + +Accepted engineering contract for M11.3d (2026-10-10). This records already +selected cellular intentions for recovery; it does not change their biological +meaning or add gameplay rules. + +## Context + +Profile 4 stores the complete post-tick cellular closure, but recovery also needs +to prove that the committed head follows from its predecessor. The existing +environmental trace cannot represent the changing set of cellular lives, +movement intentions, and optional patch targets. Durable v7 action records bind +player actions but do not by themselves describe the complete per-life inputs +needed to replay every tick in a batch. + +## Decision + +- Add a separate canonical profile-4 batch format. Its 112-byte header binds the + format, profile, world, region, frame, predecessor commit and digest, and the + batch tick range. It carries one record set for each simulated tick in + `(begin_tick, end_tick]`, in order. +- Each tick has a u32 life-intent count followed by at most 256 records, strictly + ordered by nonzero stable life ID. Each record is 24 bytes: life ID (u64 LE), + movement X/Y (signed i16 LE), target-present flag (u8), three zero reserved + bytes, and target patch X/Y (u32 LE). Absent targets encode zero coordinates. +- A batch covers 1–64 ticks. Decode validates the header, count bounds, ordering, + movement limits, flags, reserved bytes, absent-target encoding, and exact total + length before allocation. The caller binds the trace header's identity, + predecessor commit/digest, and tick range to trusted manifests before replay. + Replay clones the immutable predecessor through its canonical snapshot, + advances `CellularLives` once per tick, and refuses unless every tick's intents + cover its living set exactly. Recovery compares the resulting complete snapshot + byte for byte with the committed head. +- Maximum trace size is `112 + ticks * (4 + 256 * 24)` bytes; PostgreSQL validates + the minimum and maximum for the manifest's tick count. The exact durable v7 + action block is loaded under the current region lease and checked against the + relevant player-life intents and action result rows during recovery. + +## Consequences + +Each checkpoint batch may retain at most 393,584 bytes of replay inputs. Replay is +deterministic and bounded, while the complete post-tick checkpoint remains the +authoritative recovery state. Profile 1–3 trace formats and manifest size rules +remain unchanged. Process-level uncertain-commit and restart tests are still +required before M11.3d can close. diff --git a/docs/adr/README.md b/docs/adr/README.md index 71ca554..bd4a23d 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -13,6 +13,7 @@ - [ADR-0110: Cellular checkpoint payload bound](0110-cellular-checkpoint-payload-bound.md) - raises profile-4's maximum object size so the complete retained archive and corpse limits fit. - [ADR-0111: Durable identity for cellular player inputs](0111-cellular-durable-action-identity.md) - specifies the reliable v7 action identity needed for commit-bound cellular outcomes and exact retries. - [ADR-0112: Durable cellular action input records](0112-cellular-action-ledger-records.md) - specifies canonical retry bytes, persisted result identity and the isolated profile-4 publisher needed before v7 admission. +- [ADR-0113: Cellular replay trace](0113-cellular-replay-trace.md) - specifies bounded per-tick life intentions for deterministic profile-4 replay and durable action reconciliation. - [ADR-0058: Region durability contract](0058-region-durability-contract.md) - accepted M7.1 full checkpoint batches and explicit state closure; storage implementation follows. diff --git a/docs/engineering/M11_3_DELIVERY_PLAN.md b/docs/engineering/M11_3_DELIVERY_PLAN.md index 774e59c..4a8238c 100644 --- a/docs/engineering/M11_3_DELIVERY_PLAN.md +++ b/docs/engineering/M11_3_DELIVERY_PLAN.md @@ -222,6 +222,13 @@ scoped change before starting the next segment. existing store error contract. Focused PostgreSQL adapter tests cover malformed calls; CI execution is pending. Payload preparation, replay validation, and a cellular region host remain open. +- 11.3d-6c: ADR-0113 and the portable `CellularBatch` codec define bounded + per-life inputs for every tick. Replay clones the committed predecessor, + applies the exact living-set intentions through `CellularLives`, and returns + a state to compare byte for byte with the durable head. Migration 21 adjusts + only profile-4 trace bounds and adds lease-fenced loading of the immutable + action block. Codec, migration, and store-adapter tests are added; CI execution + is pending. Action-to-life reconciliation and process recovery remain open. ## Exit boundary diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 0505025..70252b2 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -6227,3 +6227,19 @@ rejected as invalid requests. run locally; this Windows environment lacks the PostgreSQL build/runtime needed for `EVO_BUILD_POSTGRES`, so CI is required. No cellular host calls these methods yet. +## 2026-10-10 — M11.3d-6c cellular replay evidence (CI pending) + +Added ADR-0113 and a portable profile-4 replay trace. It stores the stable life ID +and biological intention for every life at every tick, with canonical ordering, +strict bounds, and pre-allocation size checks. Replay restores an immutable +predecessor clone, applies each tick through `CellularLives`, and returns the +candidate for full canonical snapshot comparison. Migration 21 permits its +bounded variable trace size for profile 4 only and adds a lease-fenced read of the +immutable action block; the persistence adapter exposes that read. + +Cellular lifecycle tests now cover encode/decode, deterministic replay, malformed +truncation/noncanonical bytes, total conservation, and wrong-region refusal. The +PostgreSQL store test covers lease-bound absence for an unknown commit. `git diff +--check` passes. C++ compilation, PostgreSQL tests, and Linux sanitizers are +pending CI because this Windows environment has no compiler or local PostgreSQL +runtime on `PATH`. diff --git a/include/evo/persistence/region_lease_store.hpp b/include/evo/persistence/region_lease_store.hpp index 2e32aee..f378c7e 100644 --- a/include/evo/persistence/region_lease_store.hpp +++ b/include/evo/persistence/region_lease_store.hpp @@ -112,6 +112,13 @@ resolve_cellular_region_commit(Postgres& db, RegionKey key, const Uuid& commit, std::span decisions, std::span actions, std::chrono::milliseconds timeout); + +// Load the immutable action block for a committed profile-4 link under the current lease. +// Empty means this commit has no cellular publication row; a missing row for a progressing +// profile-4 commit is a database error and fails closed. +[[nodiscard]] std::expected>, LeaseStoreFailure> +load_cellular_publication_actions(Postgres& db, RegionKey key, const LeaseGrant& lease, + const Uuid& commit, std::chrono::milliseconds timeout); // The committed record of an account's action in the lease's world (schema v11), under the // caller's exact lease. Nothing when the action was never committed. struct CommittedAction { diff --git a/include/evo/server/lifecycle/cellular_batch.hpp b/include/evo/server/lifecycle/cellular_batch.hpp new file mode 100644 index 0000000..e0fe4ca --- /dev/null +++ b/include/evo/server/lifecycle/cellular_batch.hpp @@ -0,0 +1,52 @@ +#pragma once + +#include "evo/server/lifecycle/cellular_lives.hpp" + +#include +#include +#include +#include +#include +#include +#include + +namespace evo::server { +struct CellularBatchHeader { + std::uint64_t world{}, region{}, frame{}; + std::array predecessor{}; + std::array predecessor_digest{}; + std::uint64_t begin_tick{}, end_tick{}; + friend bool operator==(const CellularBatchHeader&, const CellularBatchHeader&) = default; +}; +struct CellularTickIntents { + // One intention for each life alive at the start of this tick, sorted by life_id. + std::vector lives; + friend bool operator==(const CellularTickIntents&, const CellularTickIntents&) = default; +}; +struct CellularBatch { + CellularBatchHeader header{}; + std::vector ticks; + friend bool operator==(const CellularBatch&, const CellularBatch&) = default; +}; + +inline constexpr std::uint32_t cellular_batch_format = 1; +inline constexpr std::size_t cellular_batch_header_bytes = 112; +inline constexpr std::size_t cellular_batch_tick_header_bytes = 4; +inline constexpr std::size_t cellular_batch_intent_bytes = 24; +inline constexpr std::size_t max_cellular_batch_ticks = 64; +inline constexpr std::size_t max_cellular_batch_bytes = cellular_batch_header_bytes + + max_cellular_batch_ticks * (cellular_batch_tick_header_bytes + + biology::max_cellular_tick_lives * cellular_batch_intent_bytes); +enum class CellularBatchError : std::uint8_t { format, version, scope, state }; + +// Canonical replay input: one bounded, life-ID-ordered intention set per tick. +[[nodiscard]] std::expected, CellularBatchError> +encode_cellular_batch(const CellularBatch& batch); +[[nodiscard]] std::expected +decode_cellular_batch(std::span bytes); +// Replays from an immutable predecessor snapshot. The caller must first bind the +// header identity, predecessor commit/digest and tick range to trusted manifests. +// Every tick must cover its living set; the caller compares the returned snapshot. +[[nodiscard]] std::expected, CellularBatchError> +replay_cellular_batch(const CellularLives& base, const CellularBatch& batch); +} // namespace evo::server diff --git a/migrations/0021_cellular_trace_and_recovery.sql b/migrations/0021_cellular_trace_and_recovery.sql new file mode 100644 index 0000000..6db34df --- /dev/null +++ b/migrations/0021_cellular_trace_and_recovery.sql @@ -0,0 +1,128 @@ +-- M11.3d-6c, ADR-0113: bounded cellular replay trace and durable action-block read. +CREATE OR REPLACE 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; + zero16 constant bytea := '\x00000000000000000000000000000000'; + zero32 constant bytea := decode(repeat('00', 32), 'hex'); + history numeric; + cap numeric; + ticks numeric; +BEGIN + IF m IS NULL OR octet_length(m) < 12 OR substring(m FROM 1 FOR 8) <> '\x4145564f52434d31' THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + IF evolution.manifest_uint(m, 8, 4) <> 2 THEN + RAISE EXCEPTION 'unsupported checkpoint manifest' USING ERRCODE = 'EVL06'; + END IF; + IF octet_length(m) <> 360 THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + r.profile := evolution.manifest_uint(m, 12, 4); + history := evolution.manifest_uint(m, 232, 4); + IF NOT ( + (((r.profile = 1 AND history = 0) OR (r.profile = 2 AND history = 1)) + AND substring(m FROM 161 FOR 36) = '\x020000000200000001000000010000000100000001000000010000000100000001000000') + OR (r.profile = 3 AND history = 2 + AND substring(m FROM 161 FOR 36) = '\x010000000100000001000000000000000000000001000000010000000100000001000000') + OR (r.profile = 4 AND history = 3 + AND substring(m FROM 161 FOR 36) = '\x010000000100000001000000010000000100000002000000010000000100000000000000') + ) + OR evolution.manifest_uint(m, 228, 4) <> 0 OR evolution.manifest_uint(m, 236, 4) <> 1 THEN + RAISE EXCEPTION 'unsupported checkpoint manifest' USING ERRCODE = 'EVL06'; + END IF; + cap := CASE r.profile WHEN 1 THEN 1048576 WHEN 2 THEN 4268144 + WHEN 4 THEN 16777216 ELSE 3266800 END; + r.world_id := evolution.manifest_uint(m, 16, 8); + r.region_id := evolution.manifest_uint(m, 24, 8); + r.frame_id := evolution.manifest_uint(m, 32, 8); + r.commit_id := encode(substring(m FROM 41 FOR 16), 'hex')::uuid; + r.commit_sequence := evolution.manifest_uint(m, 56, 8); + r.predecessor_commit := encode(substring(m FROM 65 FOR 16), 'hex')::uuid; + r.predecessor_digest := substring(m FROM 81 FOR 32); + r.begin_tick := evolution.manifest_uint(m, 112, 8); + r.end_tick := evolution.manifest_uint(m, 120, 8); + r.holder := encode(substring(m FROM 129 FOR 16), 'hex')::uuid; + r.epoch := evolution.manifest_uint(m, 144, 8); + r.tick_microseconds := evolution.manifest_uint(m, 152, 8); + r.versions := substring(m FROM 161 FOR 36); + r.build_identity := substring(m FROM 197 FOR 32); + r.payload_bytes := evolution.manifest_uint(m, 240, 8); + r.payload_sha256 := substring(m FROM 249 FOR 32); + r.trace_bytes := evolution.manifest_uint(m, 280, 8); + r.trace_sha256 := substring(m FROM 289 FOR 32); + r.decision_count := evolution.manifest_uint(m, 320, 4); + r.decisions_sha256 := substring(m FROM 329 FOR 32); + IF r.world_id = 0 OR r.region_id = 0 OR r.frame_id = 0 + OR substring(m FROM 41 FOR 16) = zero16 OR r.tick_microseconds = 0 + OR r.build_identity = zero32 OR r.payload_bytes < 1 OR r.payload_bytes > cap + OR r.payload_sha256 = zero32 OR evolution.manifest_uint(m, 324, 4) <> 0 THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + IF r.profile IN (3, 4) AND (r.tick_microseconds <> 50000 OR + (r.profile = 3 AND (r.decision_count <> 0 OR r.decisions_sha256 <> zero32)) OR + (r.commit_sequence = 0 AND + (substring(m FROM 65 FOR 16) <> zero16 OR r.predecessor_digest <> zero32))) THEN + RAISE EXCEPTION 'invalid lifecycle checkpoint' USING ERRCODE = '22023'; + END IF; + IF r.commit_sequence = 0 THEN + IF substring(m FROM 129 FOR 16) <> zero16 OR r.epoch <> 0 OR r.begin_tick <> r.end_tick + OR r.trace_bytes <> 0 OR r.trace_sha256 <> zero32 THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + IF substring(m FROM 65 FOR 16) = zero16 AND r.predecessor_digest = zero32 THEN + IF r.end_tick <> 0 OR r.decision_count <> 0 OR r.decisions_sha256 <> zero32 THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + r.predecessor_commit := NULL; + ELSIF substring(m FROM 65 FOR 16) = zero16 OR r.predecessor_digest = zero32 + OR r.predecessor_commit = r.commit_id OR r.profile IN (3, 4) + OR r.decision_count > 8 + OR (r.decision_count = 0) <> (r.decisions_sha256 = zero32) THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + r.holder := NULL; + ELSE + ticks := r.end_tick - r.begin_tick; + IF substring(m FROM 65 FOR 16) = zero16 OR r.predecessor_commit = r.commit_id + OR r.predecessor_digest = zero32 OR substring(m FROM 129 FOR 16) = zero16 + OR r.epoch = 0 OR r.end_tick <= r.begin_tick OR ticks > 64 + OR (r.profile IN (1, 2) AND r.trace_bytes <> 96 + 304 * ticks) + OR (r.profile = 3 AND (r.trace_bytes < 112 OR r.trace_bytes > 12400 + OR mod(r.trace_bytes - 112, 48) <> 0)) + OR (r.profile = 4 AND (r.trace_bytes < 112 + 4 * ticks + OR r.trace_bytes > 112 + 6148 * ticks)) + OR r.trace_sha256 = zero32 OR r.decision_count > 8 + OR (r.decision_count = 0) <> (r.decisions_sha256 = zero32) THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + END IF; + RETURN r; +END $$; + +CREATE FUNCTION evolution.load_cellular_publication_actions(p_world evolution.u64, + p_region evolution.u64, p_holder uuid, p_epoch evolution.u64, p_commit uuid) +RETURNS TABLE(actions bytea) +LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ +BEGIN + IF p_commit IS NULL THEN + RAISE EXCEPTION 'invalid cellular recovery request' USING ERRCODE = '22023'; + END IF; + PERFORM evolution.check_region_lease(p_world, p_region, p_holder, p_epoch); + IF EXISTS (SELECT 1 FROM evolution.region_commits c WHERE c.world_id = p_world + AND c.region_id = p_region AND c.commit_id = p_commit + AND c.commit_sequence > 0 AND evolution.manifest_uint(c.manifest, 12, 4) = 4) THEN + RETURN QUERY SELECT p.actions FROM evolution.cellular_publications p + WHERE p.commit_id = p_commit; + IF NOT FOUND THEN + RAISE EXCEPTION 'cellular publication evidence missing' USING ERRCODE = 'EVL06'; + END IF; + END IF; +END $$; + +REVOKE ALL ON FUNCTION evolution.load_cellular_publication_actions( + evolution.u64,evolution.u64,uuid,evolution.u64,uuid) FROM PUBLIC; +GRANT EXECUTE ON FUNCTION evolution.load_cellular_publication_actions( + evolution.u64,evolution.u64,uuid,evolution.u64,uuid) TO :"runtime_role"; +INSERT INTO evolution.schema_migrations(version, sha256) VALUES (21, :'migration_sha_21'); diff --git a/migrations/apply.sql b/migrations/apply.sql index 2a5c21e..12e4a99 100644 --- a/migrations/apply.sql +++ b/migrations/apply.sql @@ -93,28 +93,36 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') :'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2' || ',3:' || :'migration_sha_3' || ',4:' || :'migration_sha_4' || ',5:' || :'migration_sha_5' || ',6:' || :'migration_sha_6' || ',7:' || :'migration_sha_7' || ',8:' || :'migration_sha_8' || ',9:' || :'migration_sha_9' || ',10:' || :'migration_sha_10' || ',11:' || :'migration_sha_11' || ',12:' || :'migration_sha_12' || ',13:' || :'migration_sha_13' || ',14:' || :'migration_sha_14' || ',15:' || :'migration_sha_15' || ',16:' || :'migration_sha_16' || ',17:' || :'migration_sha_17' AS at_v17, :'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2' || ',3:' || :'migration_sha_3' || ',4:' || :'migration_sha_4' || ',5:' || :'migration_sha_5' || ',6:' || :'migration_sha_6' || ',7:' || :'migration_sha_7' || ',8:' || :'migration_sha_8' || ',9:' || :'migration_sha_9' || ',10:' || :'migration_sha_10' || ',11:' || :'migration_sha_11' || ',12:' || :'migration_sha_12' || ',13:' || :'migration_sha_13' || ',14:' || :'migration_sha_14' || ',15:' || :'migration_sha_15' || ',16:' || :'migration_sha_16' || ',17:' || :'migration_sha_17' || ',18:' || :'migration_sha_18' AS at_v18, :'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2' || ',3:' || :'migration_sha_3' || ',4:' || :'migration_sha_4' || ',5:' || :'migration_sha_5' || ',6:' || :'migration_sha_6' || ',7:' || :'migration_sha_7' || ',8:' || :'migration_sha_8' || ',9:' || :'migration_sha_9' || ',10:' || :'migration_sha_10' || ',11:' || :'migration_sha_11' || ',12:' || :'migration_sha_12' || ',13:' || :'migration_sha_13' || ',14:' || :'migration_sha_14' || ',15:' || :'migration_sha_15' || ',16:' || :'migration_sha_16' || ',17:' || :'migration_sha_17' || ',18:' || :'migration_sha_18' || ',19:' || :'migration_sha_19' AS at_v19, - :'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2' || ',3:' || :'migration_sha_3' || ',4:' || :'migration_sha_4' || ',5:' || :'migration_sha_5' || ',6:' || :'migration_sha_6' || ',7:' || :'migration_sha_7' || ',8:' || :'migration_sha_8' || ',9:' || :'migration_sha_9' || ',10:' || :'migration_sha_10' || ',11:' || :'migration_sha_11' || ',12:' || :'migration_sha_12' || ',13:' || :'migration_sha_13' || ',14:' || :'migration_sha_14' || ',15:' || :'migration_sha_15' || ',16:' || :'migration_sha_16' || ',17:' || :'migration_sha_17' || ',18:' || :'migration_sha_18' || ',19:' || :'migration_sha_19' || ',20:' || :'migration_sha_20' AS at_v20 \gset - \if :at_v20 + :'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2' || ',3:' || :'migration_sha_3' || ',4:' || :'migration_sha_4' || ',5:' || :'migration_sha_5' || ',6:' || :'migration_sha_6' || ',7:' || :'migration_sha_7' || ',8:' || :'migration_sha_8' || ',9:' || :'migration_sha_9' || ',10:' || :'migration_sha_10' || ',11:' || :'migration_sha_11' || ',12:' || :'migration_sha_12' || ',13:' || :'migration_sha_13' || ',14:' || :'migration_sha_14' || ',15:' || :'migration_sha_15' || ',16:' || :'migration_sha_16' || ',17:' || :'migration_sha_17' || ',18:' || :'migration_sha_18' || ',19:' || :'migration_sha_19' || ',20:' || :'migration_sha_20' AS at_v20, + :'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2' || ',3:' || :'migration_sha_3' || ',4:' || :'migration_sha_4' || ',5:' || :'migration_sha_5' || ',6:' || :'migration_sha_6' || ',7:' || :'migration_sha_7' || ',8:' || :'migration_sha_8' || ',9:' || :'migration_sha_9' || ',10:' || :'migration_sha_10' || ',11:' || :'migration_sha_11' || ',12:' || :'migration_sha_12' || ',13:' || :'migration_sha_13' || ',14:' || :'migration_sha_14' || ',15:' || :'migration_sha_15' || ',16:' || :'migration_sha_16' || ',17:' || :'migration_sha_17' || ',18:' || :'migration_sha_18' || ',19:' || :'migration_sha_19' || ',20:' || :'migration_sha_20' || ',21:' || :'migration_sha_21' AS at_v21 \gset + \if :at_v21 + \elif :at_v20 + \ir 0021_cellular_trace_and_recovery.sql \elif :at_v19 \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.sql \elif :at_v18 \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.sql \elif :at_v17 \ir 0018_cellular_profile_admission.sql \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.sql \elif :at_v16 \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.sql \elif :at_v15 \ir 0016_primitive_publication.sql \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.sql \elif :at_v14 \ir 0015_life_reservations.sql \ir 0016_primitive_publication.sql @@ -122,6 +130,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0018_cellular_profile_admission.sql \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.sql \elif :at_v13 \ir 0014_probe_overdue.sql \ir 0015_life_reservations.sql @@ -130,6 +139,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0018_cellular_profile_admission.sql \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.sql \elif :at_v12 \ir 0013_probe_incoming.sql \ir 0014_probe_overdue.sql @@ -139,6 +149,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0018_cellular_profile_admission.sql \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.sql \elif :at_v11 \ir 0012_probe_handoff.sql \ir 0013_probe_incoming.sql @@ -149,6 +160,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0018_cellular_profile_admission.sql \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.sql \elif :at_v10 \ir 0011_region_actions.sql \ir 0012_probe_handoff.sql @@ -160,6 +172,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0018_cellular_profile_admission.sql \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.sql \elif :at_v9 \ir 0010_account_control.sql \ir 0011_region_actions.sql @@ -172,6 +185,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0018_cellular_profile_admission.sql \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.sql \elif :at_v8 \ir 0009_serialized_authority.sql \ir 0010_account_control.sql @@ -185,6 +199,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0018_cellular_profile_admission.sql \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.sql \elif :at_v7 \ir 0008_upgrade_barrier.sql \ir 0009_serialized_authority.sql @@ -199,6 +214,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0018_cellular_profile_admission.sql \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.sql \elif :at_v6 \ir 0007_region_clones.sql \ir 0008_upgrade_barrier.sql @@ -214,6 +230,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0018_cellular_profile_admission.sql \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.sql \elif :at_v5 \ir 0006_commit_lookup.sql \ir 0007_region_clones.sql @@ -230,6 +247,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0018_cellular_profile_admission.sql \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.sql \elif :at_v4 \ir pre_v5_guard.sql \ir 0005_batch_decisions.sql @@ -248,6 +266,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0018_cellular_profile_admission.sql \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.sql \elif :at_v3 \ir 0004_lineage_profile.sql \ir pre_v5_guard.sql @@ -267,6 +286,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0018_cellular_profile_admission.sql \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.sql \elif :at_v2 \ir 0003_region_checkpoints.sql \ir 0004_lineage_profile.sql @@ -287,6 +307,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0018_cellular_profile_admission.sql \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.sql \elif :at_v1 \ir 0002_region_fencing.sql \ir 0003_region_checkpoints.sql @@ -308,6 +329,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0018_cellular_profile_admission.sql \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.sql \else \echo 'Unknown or changed migration history' DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$; @@ -334,6 +356,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0018_cellular_profile_admission.sql \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql + \ir 0021_cellular_trace_and_recovery.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 @@ -380,6 +403,7 @@ BEGIN ('evolution.find_region_action_record(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64)'::regprocedure::oid), ('evolution.publish_cellular_checkpoint(bytea,bytea,bytea)'::regprocedure::oid), ('evolution.resolve_cellular_region_commit(evolution.u64,evolution.u64,uuid,bytea,bytea,bytea)'::regprocedure::oid), + ('evolution.load_cellular_publication_actions(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)'::regprocedure::oid), ('evolution.open_probe(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,bigint,bigint,bigint,evolution.u64)'::regprocedure::oid), ('evolution.save_probe(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,bigint,bigint,bigint)'::regprocedure::oid), ('evolution.prepare_probe_transfer(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,bigint,bigint,bigint)'::regprocedure::oid), diff --git a/src/persistence/region_lease_store.cpp b/src/persistence/region_lease_store.cpp index 5093442..153273e 100644 --- a/src/persistence/region_lease_store.cpp +++ b/src/persistence/region_lease_store.cpp @@ -8,6 +8,7 @@ #include #include #include +#include namespace evo::persistence { namespace { @@ -412,6 +413,29 @@ resolve_cellular_region_commit(Postgres& db, RegionKey key, const Uuid& commit, return ResolvedCommit{Resolution::committed, *head}; } +std::expected>, LeaseStoreFailure> +load_cellular_publication_actions(Postgres& db, RegionKey key, const LeaseGrant& lease, + const Uuid& commit, milliseconds timeout) { + const std::array p{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(actions, 'hex') " + "FROM evolution.load_cellular_publication_actions($1::evolution.u64, " + "$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid)", + p, timeout); + if (!rows) + return std::unexpected{classify(rows.error())}; + if (rows->empty()) + return std::optional>{}; + if (rows->size() != 1 || (*rows)[0].size() != 1 || !(*rows)[0][0]) + return std::unexpected{malformed(false)}; + auto actions = unhex_bytes(*(*rows)[0][0]); + if (!actions) + return std::unexpected{malformed(false)}; + return std::optional>{std::move(*actions)}; +} + std::expected, LeaseStoreFailure> find_region_action(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account, std::uint64_t action_id, milliseconds timeout) { diff --git a/src/server/lifecycle/cellular_batch.cpp b/src/server/lifecycle/cellular_batch.cpp new file mode 100644 index 0000000..3727d83 --- /dev/null +++ b/src/server/lifecycle/cellular_batch.cpp @@ -0,0 +1,184 @@ +#include "evo/server/lifecycle/cellular_batch.hpp" + +#include +#include +#include +#include +#include + +namespace evo::server { +namespace { +constexpr std::array magic{'E', 'V', 'L', 'C', 'B', 'T', '0', '1'}; +constexpr std::int16_t movement_limit = 1'000; + +void put(std::vector& out, std::uint64_t value, std::size_t width) { + for (std::size_t i = 0; i < width; ++i) + out.push_back(static_cast(value >> (i * 8U))); +} +std::uint64_t get(std::span bytes, std::size_t offset, std::size_t width) { + std::uint64_t value{}; + for (std::size_t i = 0; i < width; ++i) + value |= static_cast(bytes[offset + i]) << (i * 8U); + return value; +} +bool all_zero(std::span bytes) { + return std::ranges::all_of(bytes, [](std::uint8_t byte) { return byte == 0; }); +} +bool valid_header(const CellularBatchHeader& h, std::size_t ticks) { + return h.world != 0 && h.region != 0 && h.frame != 0 && !all_zero(h.predecessor) && + !all_zero(h.predecessor_digest) && h.end_tick > h.begin_tick && + h.end_tick - h.begin_tick == ticks && ticks > 0 && + ticks <= max_cellular_batch_ticks; +} +bool valid_intent(const CellularLifeIntent& life) { + const auto& step = life.intent; + return life.life_id != 0 && step.movement.x >= -movement_limit && + step.movement.x <= movement_limit && step.movement.y >= -movement_limit && + step.movement.y <= movement_limit; +} +bool valid_ticks(std::span ticks) { + for (const auto& tick : ticks) { + if (tick.lives.size() > biology::max_cellular_tick_lives) + return false; + for (std::size_t i = 0; i < tick.lives.size(); ++i) { + const auto& life = tick.lives[i]; + if (!valid_intent(life) || + (i > 0 && tick.lives[i - 1].life_id >= life.life_id)) + return false; + } + } + return true; +} +void put_intent(std::vector& out, const CellularLifeIntent& life) { + put(out, life.life_id, 8); + put(out, static_cast(life.intent.movement.x), 2); + put(out, static_cast(life.intent.movement.y), 2); + out.push_back(life.intent.intake_target ? 1 : 0); + put(out, 0, 3); + put(out, life.intent.intake_target ? life.intent.intake_target->x_um : 0, 4); + put(out, life.intent.intake_target ? life.intent.intake_target->y_um : 0, 4); +} +} // namespace + +std::expected, CellularBatchError> +encode_cellular_batch(const CellularBatch& batch) { + if (!valid_header(batch.header, batch.ticks.size()) || !valid_ticks(batch.ticks)) + return std::unexpected{CellularBatchError::format}; + std::size_t size = cellular_batch_header_bytes; + for (const auto& tick : batch.ticks) { + const auto records = tick.lives.size() * cellular_batch_intent_bytes; + if (records > max_cellular_batch_bytes - size - cellular_batch_tick_header_bytes) + return std::unexpected{CellularBatchError::format}; + size += cellular_batch_tick_header_bytes + records; + } + std::vector out; + out.reserve(size); + out.insert(out.end(), magic.begin(), magic.end()); + put(out, cellular_batch_format, 4); + put(out, 4, 4); // Profile 4. + put(out, batch.header.world, 8); + put(out, batch.header.region, 8); + put(out, batch.header.frame, 8); + out.insert(out.end(), batch.header.predecessor.begin(), batch.header.predecessor.end()); + out.insert(out.end(), batch.header.predecessor_digest.begin(), + batch.header.predecessor_digest.end()); + put(out, batch.header.begin_tick, 8); + put(out, batch.header.end_tick, 8); + put(out, batch.ticks.size(), 4); + put(out, 0, 4); + for (const auto& tick : batch.ticks) { + put(out, tick.lives.size(), 4); + for (const auto& life : tick.lives) + put_intent(out, life); + } + if (out.size() != size) + return std::unexpected{CellularBatchError::format}; + return out; +} + +std::expected +decode_cellular_batch(std::span bytes) { + if (bytes.size() < cellular_batch_header_bytes || bytes.size() > max_cellular_batch_bytes || + !std::ranges::equal(bytes.first(magic.size()), magic)) + return std::unexpected{CellularBatchError::format}; + if (get(bytes, 8, 4) != cellular_batch_format || get(bytes, 12, 4) != 4) + return std::unexpected{CellularBatchError::version}; + if (get(bytes, 108, 4) != 0) + return std::unexpected{CellularBatchError::format}; + + CellularBatch out; + auto& h = out.header; + h.world = get(bytes, 16, 8); + h.region = get(bytes, 24, 8); + h.frame = get(bytes, 32, 8); + std::ranges::copy(bytes.subspan(40, 16), h.predecessor.begin()); + std::ranges::copy(bytes.subspan(56, 32), h.predecessor_digest.begin()); + h.begin_tick = get(bytes, 88, 8); + h.end_tick = get(bytes, 96, 8); + const auto tick_count = get(bytes, 104, 4); + if (tick_count > max_cellular_batch_ticks || + !valid_header(h, static_cast(tick_count))) + return std::unexpected{CellularBatchError::format}; + + // Bound all per-tick allocation by checking each encoded count against the + // remaining bytes before resizing the tick or intent vectors. + std::size_t offset = cellular_batch_header_bytes; + out.ticks.reserve(static_cast(tick_count)); + for (std::uint64_t tick_index = 0; tick_index < tick_count; ++tick_index) { + if (bytes.size() - offset < cellular_batch_tick_header_bytes) + return std::unexpected{CellularBatchError::format}; + const auto count = get(bytes, offset, 4); + offset += cellular_batch_tick_header_bytes; + if (count > biology::max_cellular_tick_lives || + count > (bytes.size() - offset) / cellular_batch_intent_bytes) + return std::unexpected{CellularBatchError::format}; + CellularTickIntents step; + step.lives.reserve(static_cast(count)); + for (std::uint64_t i = 0; i < count; ++i) { + const auto record = bytes.subspan(offset, cellular_batch_intent_bytes); + if (record[12] > 1 || !all_zero(record.subspan(13, 3))) + return std::unexpected{CellularBatchError::format}; + CellularLifeIntent life; + life.life_id = get(record, 0, 8); + life.intent.movement.x = std::bit_cast( + static_cast(get(record, 8, 2))); + life.intent.movement.y = std::bit_cast( + static_cast(get(record, 10, 2))); + if (record[12] == 1) { + life.intent.intake_target = biology::CellularPatchAddress{ + static_cast(get(record, 16, 4)), + static_cast(get(record, 20, 4))}; + } else if (!all_zero(record.subspan(16, 8))) { + return std::unexpected{CellularBatchError::format}; + } + if (!valid_intent(life) || + (!step.lives.empty() && step.lives.back().life_id >= life.life_id)) + return std::unexpected{CellularBatchError::format}; + step.lives.push_back(life); + offset += cellular_batch_intent_bytes; + } + out.ticks.push_back(std::move(step)); + } + if (offset != bytes.size()) + return std::unexpected{CellularBatchError::format}; + return out; +} + +std::expected, CellularBatchError> +replay_cellular_batch(const CellularLives& base, const CellularBatch& batch) { + const auto& h = batch.header; + if (!valid_header(h, batch.ticks.size()) || !valid_ticks(batch.ticks)) + return std::unexpected{CellularBatchError::format}; + if (base.world() != h.world || base.region() != h.region || base.frame() != h.frame || + base.tick() != h.begin_tick) + return std::unexpected{CellularBatchError::scope}; + auto replay = CellularLives::restore(base.snapshot(), base.world(), base.region(), base.frame(), + base.tick()); + if (!replay) + return std::unexpected{CellularBatchError::state}; + for (const auto& tick : batch.ticks) + if (!(*replay)->advance_tick(tick.lives)) + return std::unexpected{CellularBatchError::state}; + return std::move(*replay); +} +} // namespace evo::server diff --git a/tests/cellular_lives_tests.cpp b/tests/cellular_lives_tests.cpp index 97d17ba..8a6c06d 100644 --- a/tests/cellular_lives_tests.cpp +++ b/tests/cellular_lives_tests.cpp @@ -1,4 +1,5 @@ #include "evo/server/lifecycle/cellular_lives.hpp" +#include "evo/server/lifecycle/cellular_batch.hpp" #include #include @@ -109,6 +110,34 @@ void owner_advances_and_closes_death_atomically() { refresh_checksum(outside); CHECK(!CellularLives::restore(outside, 90, 1, 7, owner.tick())); + CellularBatch replay_batch; + replay_batch.header = {owner.world(), owner.region(), owner.frame(), {}, {}, owner.tick(), + owner.tick() + 2}; + replay_batch.header.predecessor.fill(0x31); + replay_batch.header.predecessor_digest.fill(0x41); + for (int i = 0; i < 2; ++i) { + CellularTickIntents tick; + for (const auto& life : owner.lives()) + tick.lives.push_back({life.identity.life, {}}); + replay_batch.ticks.push_back(std::move(tick)); + } + const auto batch_bytes = encode_cellular_batch(replay_batch); + CHECK(batch_bytes && batch_bytes->size() <= max_cellular_batch_bytes); + const auto decoded_batch = decode_cellular_batch(*batch_bytes); + CHECK(decoded_batch && *decoded_batch == replay_batch); + const auto replayed_batch = replay_cellular_batch(owner, *decoded_batch); + CHECK(replayed_batch && (*replayed_batch)->tick() == owner.tick() + 2); + CHECK((*replayed_batch)->current_total() == owner.opening_total()); + auto short_batch = *batch_bytes; + short_batch.pop_back(); + CHECK(!decode_cellular_batch(short_batch)); + auto noncanonical_batch = *batch_bytes; + noncanonical_batch[noncanonical_batch.size() - 1] = 1; + CHECK(!decode_cellular_batch(noncanonical_batch)); + auto wrong_scope = replay_batch; + wrong_scope.header.region++; + CHECK(replay_cellular_batch(owner, wrong_scope).error() == CellularBatchError::scope); + std::size_t steps{}; while (!owner.lives().empty() && steps < 2'000) { std::vector idle; diff --git a/tests/region_lease_store_tests.cpp b/tests/region_lease_store_tests.cpp index 5a5de5c..4fb361e 100644 --- a/tests/region_lease_store_tests.cpp +++ b/tests/region_lease_store_tests.cpp @@ -193,6 +193,17 @@ void cellular_publisher_adapter_rejects_malformed_requests() { CHECK(failed(resolved, LeaseStoreError::invalid)); } +void cellular_action_block_load_is_lease_fenced_and_optional() { + auto admin = owner(); + const auto key = provision(*admin, 11); + auto db = runtime(); + auto lease = acquire_region_lease(*db, key, build, 5s, 5s); + CHECK(lease); + auto absent = load_cellular_publication_actions(*db, key, *lease, id(0xF9), 5s); + CHECK(absent && !*absent); + CHECK(release_region_lease(*db, key, *lease, 5s)); +} + void acquire_renew_release_and_publish() { auto admin = owner(); const auto key = provision(*admin, 20); @@ -528,6 +539,7 @@ void measure_round_trips() { void run_region_lease_store_tests() { runtime_cannot_bypass_the_guard(); cellular_publisher_adapter_rejects_malformed_requests(); + cellular_action_block_load_is_lease_fenced_and_optional(); acquire_renew_release_and_publish(); database_time_expiry_and_paused_writer(); queued_writer_uses_post_lock_database_time(); diff --git a/tests/region_migration_tests.cpp b/tests/region_migration_tests.cpp index 501bf2b..319ceb5 100644 --- a/tests/region_migration_tests.cpp +++ b/tests/region_migration_tests.cpp @@ -186,6 +186,7 @@ void upgrade_across_migration_and_barrier() { " -v migration_sha_18=$(s 0018_cellular_profile_admission.sql)" " -v migration_sha_19=$(s 0019_cellular_action_records.sql)" " -v migration_sha_20=$(s 0020_cellular_publication.sql)" + " -v migration_sha_21=$(s 0021_cellular_trace_and_recovery.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"); @@ -201,7 +202,7 @@ void upgrade_across_migration_and_barrier() { CHECK(shell("PGDATABASE=" + database + " EVO_PG_RUNTIME_ROLE=evo_runtime_test bash \"$EVO_SOURCE_ROOT/" "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"); + 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"); CHECK(scalar(*owner_db, "SELECT count(*) FROM information_schema.columns WHERE " "table_schema = 'evolution' AND table_name = 'region_actions' AND " "column_name IN ('input_bytes', 'result')") == "2"); diff --git a/tools/ci/postgres-check.sh b/tools/ci/postgres-check.sh index fb71206..0cdd1aa 100644 --- a/tools/ci/postgres-check.sh +++ b/tools/ci/postgres-check.sh @@ -69,7 +69,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 +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 # 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%% *}" @@ -90,7 +90,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")" = 20:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 +test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 21:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21 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. @@ -144,7 +144,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 +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 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 # Inherited privileges bypass fencing, so every migrate run audits effective rights. @@ -190,9 +190,9 @@ expect_audit_failure 'GRANT SELECT ON evolution.account_controls TO evo_runtime_ expect_audit_failure 'REVOKE EXECUTE ON FUNCTION evolution.claim_account_control(evolution.u64, evolution.u64, uuid, evolution.u64, uuid, evolution.u64, evolution.u64, integer) FROM evo_runtime_test' 'GRANT EXECUTE ON FUNCTION evolution.claim_account_control(evolution.u64, evolution.u64, uuid, evolution.u64, uuid, evolution.u64, evolution.u64, integer) TO evo_runtime_test' bash "$root/tools/migrate-postgres.sh" # Restored configuration passes the audit again. # 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=21 WHERE version=20' +psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=22 WHERE version=21' 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=20 WHERE version=21" +psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=21 WHERE version=22" 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/ci/upgrade-check.sh b/tools/ci/upgrade-check.sh index 6e7cff7..be94c33 100644 --- a/tools/ci/upgrade-check.sh +++ b/tools/ci/upgrade-check.sh @@ -61,7 +61,7 @@ SQL [ "$code" != 0 ] && grep -q 'division by zero' "$work/migration.log" || fail "the rehearsal migration did not fail" [ "$(q -c "SELECT count(*) FROM information_schema.columns WHERE table_name = 'region_commits' AND column_name = 'rehearsal_column'")" = 0 ] || fail "the failed migration left a column" -[ "$(q -c 'SELECT max(version) FROM evolution.schema_migrations')" = 20 ] || fail "schema version moved" +[ "$(q -c 'SELECT max(version) FROM evolution.schema_migrations')" = 21 ] || fail "schema version moved" wait_ack "$work/b1.log" $((before + 40)) || fail "b1 did not resume after the failed migration" kill -0 "$b1_pid" 2> /dev/null || fail "b1 exited during the failed migration" bash "$root/tools/migrate-postgres.sh" > /dev/null || fail "migrate after the failure" diff --git a/tools/migrate-postgres.sh b/tools/migrate-postgres.sh index 47d70ec..d3a00f9 100644 --- a/tools/migrate-postgres.sh +++ b/tools/migrate-postgres.sh @@ -52,6 +52,8 @@ digest_19="$(sha256sum "$root/migrations/0019_cellular_action_records.sql")" digest_19="${digest_19%% *}" digest_20="$(sha256sum "$root/migrations/0020_cellular_publication.sql")" digest_20="${digest_20%% *}" +digest_21="$(sha256sum "$root/migrations/0021_cellular_trace_and_recovery.sql")" +digest_21="${digest_21%% *}" 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" \ @@ -65,5 +67,6 @@ psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \ --set="migration_sha_18=$digest_18" \ --set="migration_sha_19=$digest_19" \ --set="migration_sha_20=$digest_20" \ + --set="migration_sha_21=$digest_21" \ --set="runtime_role=$runtime_role" \ --file="$root/migrations/apply.sql"