diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index c55febc..3112e40 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -6773,9 +6773,15 @@ not be verified in this environment. No full-CI pass is claimed for the 11.4b migration/recovery work; rerun or inspect this gate when the host is available. For the M11.4c archive-revision and v3 checkpoint-migration changes, clang-format -and `git diff --check` pass. The targeted Windows Debug build for lineage, cellular -lives and checkpoint tests again produced no output for 60 seconds and was -stopped. Earlier direct compiler setup lacked the Visual C++ standard-library -environment, and WSL is unavailable in this session. These tests and the -PostgreSQL migration/publication path therefore remain unverified here; rely on -the Linux CI run after the repository host is reachable. +and `git diff --check` passed at that commit. The targeted Windows Debug build for +lineage, cellular lives and checkpoint tests again produced no output for 60 +seconds and was stopped. Earlier direct compiler setup lacked the Visual C++ +standard-library environment, and WSL is unavailable in this session. + +The next M11.4c segment adds owner-side application and deterministic replay of +mutation selections, v3 trace decoding, a separate canonical mutation-action +codec, and SQL v3 trace bounds. No local compile or PostgreSQL test completed: +both targeted Ninja invocations stalled without output and were stopped. The +publisher deliberately refuses batches containing mutation selections until +durable action reservation, publication and recovery are implemented. Formatting +and Linux CI remain pending for this segment. diff --git a/include/evo/server/lifecycle/cellular_batch.hpp b/include/evo/server/lifecycle/cellular_batch.hpp index e4530a0..0c535eb 100644 --- a/include/evo/server/lifecycle/cellular_batch.hpp +++ b/include/evo/server/lifecycle/cellular_batch.hpp @@ -18,6 +18,9 @@ struct CellularBatchHeader { std::uint64_t begin_tick{}, end_tick{}; friend bool operator==(const CellularBatchHeader&, const CellularBatchHeader&) = default; }; +inline constexpr std::uint32_t cellular_batch_format = 3; +inline constexpr std::uint32_t cellular_batch_v2_format = 2; +inline constexpr std::uint32_t cellular_batch_legacy_format = 1; struct CellularTickIntents { // One intention for each life alive at the start of this tick, sorted by life_id. std::vector lives; @@ -29,11 +32,15 @@ struct CellularTickIntents { friend bool operator==(const Birth&, const Birth&) = default; }; std::vector births; + // Choices accepted against the preceding checkpoint and scheduled for this tick. + // The successor identity is durably reserved before the batch is replayed. + std::vector mutations; friend bool operator==(const CellularTickIntents&, const CellularTickIntents&) = default; }; struct CellularBatch { CellularBatchHeader header{}; std::vector ticks; + std::uint32_t format_version{cellular_batch_format}; friend bool operator==(const CellularBatch&, const CellularBatch&) = default; }; struct CellularActionRecord { @@ -42,22 +49,38 @@ struct CellularActionRecord { std::array input{}; friend bool operator==(const CellularActionRecord&, const CellularActionRecord&) = default; }; +struct CellularMutationActionInput { + std::uint64_t life_id{}, event_index{}, offer_tick{}; + std::uint16_t operator_id{}; + friend bool operator==(const CellularMutationActionInput&, + const CellularMutationActionInput&) = default; +}; +struct CellularMutationActionRecord { + std::array account{}; + std::uint64_t action_id{}, tick{}; + CellularMutationActionInput input{}; + friend bool operator==(const CellularMutationActionRecord&, + const CellularMutationActionRecord&) = default; +}; -inline constexpr std::uint32_t cellular_batch_format = 2; -inline constexpr std::uint32_t cellular_batch_legacy_format = 1; inline constexpr std::size_t cellular_batch_header_bytes = 112; -inline constexpr std::size_t cellular_batch_tick_header_bytes = 8; +inline constexpr std::size_t cellular_batch_v1_tick_header_bytes = 4; +inline constexpr std::size_t cellular_batch_v2_tick_header_bytes = 8; +inline constexpr std::size_t cellular_batch_tick_header_bytes = 16; inline constexpr std::size_t cellular_batch_intent_bytes = 24; inline constexpr std::size_t cellular_batch_birth_bytes = 40; +inline constexpr std::size_t cellular_batch_mutation_bytes = 40; 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 + cellular_batch_birth_bytes)); + (cellular_batch_intent_bytes + cellular_batch_birth_bytes + + cellular_batch_mutation_bytes)); enum class CellularBatchError : std::uint8_t { format, version, scope, state }; inline constexpr std::size_t cellular_action_record_bytes = 48; inline constexpr std::size_t max_cellular_action_records = 1'024; +inline constexpr std::size_t cellular_mutation_action_record_bytes = 64; enum class CellularActionBlockError : std::uint8_t { format, mismatch }; // Canonical replay input: one bounded, life-ID-ordered intention set per tick. @@ -76,9 +99,19 @@ encode_cellular_action_block(std::span actions, [[nodiscard]] std::expected, CellularActionBlockError> decode_cellular_action_block(std::span bytes, std::uint64_t begin_tick, std::uint64_t end_tick); +[[nodiscard]] std::expected, CellularActionBlockError> +encode_cellular_mutation_action_block(std::span actions, + std::uint64_t begin_tick, std::uint64_t end_tick); +[[nodiscard]] std::expected, CellularActionBlockError> +decode_cellular_mutation_action_block(std::span bytes, std::uint64_t begin_tick, + std::uint64_t end_tick); // Every durable action must match the canonical input for that account's life // in the corresponding replay tick. An action cannot create a life or intent. [[nodiscard]] std::expected match_cellular_actions_to_trace(const CellularLives& predecessor, const CellularBatch& batch, std::span actions); +[[nodiscard]] std::expected +match_cellular_mutation_actions_to_trace(const CellularLives& predecessor, + const CellularBatch& batch, + std::span actions); } // namespace evo::server diff --git a/include/evo/server/lifecycle/cellular_lives.hpp b/include/evo/server/lifecycle/cellular_lives.hpp index 888db97..ade98a7 100644 --- a/include/evo/server/lifecycle/cellular_lives.hpp +++ b/include/evo/server/lifecycle/cellular_lives.hpp @@ -58,6 +58,21 @@ struct CellularLifeIntent { biology::CellularStepIntent intent{}; auto operator<=>(const CellularLifeIntent&) const = default; }; +struct CellularMutationSelection { + std::uint64_t life_id{}, event_index{}, offer_tick{}, successor_id{}; + std::uint16_t operator_id{}; + auto operator<=>(const CellularMutationSelection&) const = default; +}; +enum class CellularMutationResult : std::uint8_t { applied, life_died, insufficient_funding }; +struct CellularMutationExecution { + std::uint64_t life_id{}, successor_id{}; + CellularMutationResult result{}; + auto operator<=>(const CellularMutationExecution&) const = default; +}; +struct CellularTickExecution { + biology::CellularRegionStepCandidate step; + std::vector mutations; +}; struct CellularLifeTickFeedback { std::uint64_t life_id{}, tick{}; accounting::Inventory reserve; @@ -156,6 +171,11 @@ class CellularLives { // this owner byte-for-byte unchanged. [[nodiscard]] std::expected advance_tick(std::span intents); + // Mutation selections are validated against the predecessor offer, then applied + // after the normal life step and before births on the same advancing tick. + [[nodiscard]] std::expected + advance_tick_with_mutations(std::span intents, + std::span selections); private: struct State; diff --git a/migrations/0028_cellular_v3_trace_bounds.sql b/migrations/0028_cellular_v3_trace_bounds.sql new file mode 100644 index 0000000..c6bf6cf --- /dev/null +++ b/migrations/0028_cellular_v3_trace_bounds.sql @@ -0,0 +1,48 @@ +-- M11.4c, ADR-0121: accept the canonical mutation-selection records in v3 traces. +-- Keep the prior v3 profile migration parser as the authority for all other fields. + +ALTER FUNCTION evolution.parse_checkpoint_manifest(bytea) + RENAME TO parse_checkpoint_manifest_v27; + +CREATE FUNCTION evolution.parse_checkpoint_manifest(m bytea) +RETURNS evolution.checkpoint_manifest LANGUAGE plpgsql IMMUTABLE +SET search_path = pg_catalog, pg_temp AS $$ +DECLARE + r evolution.checkpoint_manifest; + normalized bytea; + ticks numeric; + trace_bytes numeric; + min_trace numeric; + max_trace numeric; + v2_bundle constant bytea := '\x020000000200000001000000010000000100000002000000010000000100000001000000'; + v3_bundle constant bytea := '\x020000000300000001000000010000000100000002000000010000000200000001000000'; +BEGIN + IF m IS NOT NULL AND octet_length(m) = 360 + AND evolution.manifest_uint(m, 12, 4) = 4 + AND substring(m FROM 161 FOR 36) = v3_bundle + AND evolution.manifest_uint(m, 228, 4) = 0 + AND evolution.manifest_uint(m, 232, 4) = 4 + AND evolution.manifest_uint(m, 236, 4) = 1 + AND evolution.manifest_uint(m, 120, 8) > evolution.manifest_uint(m, 112, 8) THEN + ticks := evolution.manifest_uint(m, 120, 8) - evolution.manifest_uint(m, 112, 8); + trace_bytes := evolution.manifest_uint(m, 280, 8); + min_trace := 112 + 16 * ticks; + max_trace := min_trace + ticks * 256 * (24 + 40 + 40); + IF ticks <= 0 OR ticks > 64 OR trace_bytes < min_trace OR trace_bytes > max_trace + OR mod(trace_bytes - min_trace, 8) <> 0 THEN + RAISE EXCEPTION 'malformed cellular-v3 trace bounds' USING ERRCODE = '22023'; + END IF; + normalized := overlay(m PLACING v2_bundle FROM 161 FOR 36); + normalized := overlay(normalized PLACING evolution.manifest_u64le(3) FROM 233 FOR 4); + normalized := overlay(normalized PLACING evolution.manifest_u64le(112 + 8 * ticks) + FROM 281 FOR 8); + r := evolution.parse_checkpoint_manifest_v27(normalized); + r.versions := v3_bundle; + r.trace_bytes := trace_bytes; + RETURN r; + END IF; + RETURN evolution.parse_checkpoint_manifest_v27(m); +END $$; +REVOKE ALL ON FUNCTION evolution.parse_checkpoint_manifest(bytea) FROM PUBLIC; + +INSERT INTO evolution.schema_migrations(version, sha256) VALUES (28, :'migration_sha_28'); diff --git a/migrations/apply.sql b/migrations/apply.sql index d6854dd..8edeb47 100644 --- a/migrations/apply.sql +++ b/migrations/apply.sql @@ -101,7 +101,10 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') SELECT right(:'history', length(',25:' || :'migration_sha_25')) = ',25:' || :'migration_sha_25' AS at_v25 \gset SELECT right(:'history', length(',26:' || :'migration_sha_26')) = ',26:' || :'migration_sha_26' AS at_v26 \gset SELECT right(:'history', length(',27:' || :'migration_sha_27')) = ',27:' || :'migration_sha_27' AS at_v27 \gset - \if :at_v27 + SELECT right(:'history', length(',28:' || :'migration_sha_28')) = ',28:' || :'migration_sha_28' AS at_v28 \gset + \if :at_v28 + \elif :at_v27 + \ir 0028_cellular_v3_trace_bounds.sql \elif :at_v26 \ir 0027_cellular_v3_migration.sql \elif :at_v25 @@ -445,20 +448,26 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') SELECT coalesce(max(version), 0) = 24 AS at_v24, coalesce(max(version), 0) = 25 AS at_v25, coalesce(max(version), 0) = 26 AS at_v26, - coalesce(max(version), 0) = 27 AS at_v27 + coalesce(max(version), 0) = 27 AS at_v27, + coalesce(max(version), 0) = 28 AS at_v28 FROM evolution.schema_migrations \gset \if :at_v24 \ir 0025_cellular_v2_bundle.sql \ir 0026_cellular_profile_migration.sql \ir 0027_cellular_v3_migration.sql + \ir 0028_cellular_v3_trace_bounds.sql \elif :at_v25 \ir 0026_cellular_profile_migration.sql \ir 0027_cellular_v3_migration.sql + \ir 0028_cellular_v3_trace_bounds.sql \elif :at_v26 \ir 0027_cellular_v3_migration.sql + \ir 0028_cellular_v3_trace_bounds.sql \elif :at_v27 + \ir 0028_cellular_v3_trace_bounds.sql +\elif :at_v28 \else - \echo 'Unknown or changed migration history after v27' + \echo 'Unknown or changed migration history after v28' DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$; \endif -- Effective-privilege audit on every run, including no-ops (ADR-0060). Attributes diff --git a/schemas/cellular-lives-v3.md b/schemas/cellular-lives-v3.md index ba29b5c..3ffdd45 100644 --- a/schemas/cellular-lives-v3.md +++ b/schemas/cellular-lives-v3.md @@ -24,3 +24,8 @@ tick, lives, outstanding offers, corpses, habitat and conserved inventory. SQL records the source and target as one immutable migration link. Ordinary v3 successors retain the exact v3 bundle; a mutation is published with an advancing v3 tick. + +The advancing v3 batch trace is specified in +[cellular-region-batch-v3](cellular-region-batch-v3.md). Its mutation selection +records are decoded and replayed by the lifecycle layer; durable action +reservation and checkpoint publication are not yet enabled. diff --git a/schemas/cellular-region-batch-v3.md b/schemas/cellular-region-batch-v3.md new file mode 100644 index 0000000..fcb613b --- /dev/null +++ b/schemas/cellular-region-batch-v3.md @@ -0,0 +1,25 @@ +# Cellular region batch trace v3 (M11.4c) + +Authority: ADR-0121 and the cellular batch codec in +`src/server/lifecycle/cellular_batch.cpp`. All integers are little-endian. +Formats 1 and 2 remain readable; they contain no mutation selections. + +The 112-byte common header is `EVLCBT01`, format u32, profile u32, world, +region and frame u64, predecessor commit (16 bytes), predecessor manifest +SHA-256 (32 bytes), begin and end ticks u64, tick count u32 and zero reserved +u32. The count is `end_tick - begin_tick`, from 1 through 64. + +Each v3 tick starts with four u32 values: life-intent count, birth count, +mutation-selection count and zero reserved. The arrays follow in that order. +Counts are bounded by the cellular per-tick life limit. Life intents are the +24-byte format-2 records. Births are the 40-byte format-2 records. A 40-byte +mutation selection contains life ID, event index, offer tick and reserved +successor genome ID as u64, operator ID as u16, then six zero bytes. Each array +is strictly ordered by its canonical key; selections are ordered by life ID. + +The separate 64-byte mutation-action codec binds an authenticated account, +action ID, scheduled tick and the exact `(life_id, event_index, offer_tick, +operator_id)` input. It is an in-memory codec only at this stage. Durable +reservation, publication and recovery are still required before mutation +selections may be published; the checkpoint publisher currently rejects such +selections. diff --git a/src/persistence/cellular_checkpoint_store.cpp b/src/persistence/cellular_checkpoint_store.cpp index 75064ec..20e952a 100644 --- a/src/persistence/cellular_checkpoint_store.cpp +++ b/src/persistence/cellular_checkpoint_store.cpp @@ -62,7 +62,11 @@ bool prepared_is_canonical(const CellularPreparedCommit& prepared, std::unexpected{server::CellularActionBlockError::format}}; const auto encoded_trace = server::encode_cellular_batch(trace); const auto digest = sha256(prepared.trace); - return manifest.profile == CheckpointProfile::cellular_lives_v1 && + const bool trace_version_matches = (manifest.cellular_versions == cellular_v3_versions && + trace.format_version == server::cellular_batch_format) || + (manifest.cellular_versions != cellular_v3_versions && + trace.format_version == server::cellular_batch_v2_format); + return manifest.profile == CheckpointProfile::cellular_lives_v1 && trace_version_matches && manifest.key == prepared.key && manifest.commit == prepared.commit && manifest.sequence > 0 && manifest.batch.trace_bytes == prepared.trace.size() && digest && *digest == manifest.batch.trace_sha256 && manifest.batch.decision_count == 0 && @@ -320,7 +324,12 @@ publish_cellular_batch(Postgres& db, const FilesystemObjectStore& objects, const std::span actions) { const auto previous = decode_manifest(predecessor.manifest); const auto predecessor_digest = sha256(predecessor.manifest); - const auto trace = server::encode_cellular_batch(batch); + auto committed_batch = batch; + if (previous) + committed_batch.format_version = previous->cellular_versions == cellular_v3_versions + ? server::cellular_batch_format + : server::cellular_batch_v2_format; + const auto trace = server::encode_cellular_batch(committed_batch); const auto action_block = server::encode_cellular_action_block(actions, batch.header.begin_tick, batch.header.end_tick); if (!previous || !predecessor_digest || !trace || !action_block || @@ -336,7 +345,9 @@ publish_cellular_batch(Postgres& db, const FilesystemObjectStore& objects, const candidate.region() != previous->key.region || candidate.frame() != previous->frame || candidate.format_version() != previous->cellular_versions.envelope || candidate.rules_version() != previous->cellular_versions.rules || - candidate.tick() != batch.header.end_tick) + candidate.tick() != batch.header.end_tick || + std::ranges::any_of(committed_batch.ticks, + [](const auto& tick) { return !tick.mutations.empty(); })) return std::unexpected{failure(PublicationError::candidate)}; auto predecessor_bytes = objects.get({previous->payload_sha256, previous->payload_bytes}); @@ -345,10 +356,10 @@ publish_cellular_batch(Postgres& db, const FilesystemObjectStore& objects, const auto base = restore_cellular_payload(*predecessor_bytes, *previous); if (!base) return std::unexpected{failure(PublicationError::predecessor)}; - auto replay = server::replay_cellular_batch(**base, batch); + auto replay = server::replay_cellular_batch(**base, committed_batch); const auto payload = candidate.snapshot(); if (!replay || (*replay)->snapshot() != payload || - !server::match_cellular_actions_to_trace(**base, batch, actions)) + !server::match_cellular_actions_to_trace(**base, committed_batch, actions)) return std::unexpected{failure(PublicationError::candidate)}; const auto payload_digest = sha256(payload); @@ -488,6 +499,11 @@ recover_cellular_checkpoint(Postgres& db, const FilesystemObjectStore& objects, auto trace = server::decode_cellular_batch(*trace_bytes); if (!trace) return fail_with(RecoveryError::tail); + if ((manifest->cellular_versions == cellular_v3_versions && + trace->format_version != server::cellular_batch_format) || + (manifest->cellular_versions != cellular_v3_versions && + trace->format_version != server::cellular_batch_v2_format)) + return fail_with(RecoveryError::tail); if (trace->header.world != key.world || trace->header.region != key.region || trace->header.frame != manifest->frame || trace->header.predecessor != manifest->predecessor_commit || diff --git a/src/persistence/region_checkpoint.cpp b/src/persistence/region_checkpoint.cpp index 67a5165..6537caf 100644 --- a/src/persistence/region_checkpoint.cpp +++ b/src/persistence/region_checkpoint.cpp @@ -105,17 +105,25 @@ bool structural(const CheckpointManifest& m) noexcept { return link; const auto ticks = m.end_tick - m.begin_tick; const auto cellular_legacy_min = - server::cellular_batch_header_bytes + ticks * sizeof(std::uint32_t); + server::cellular_batch_header_bytes + ticks * server::cellular_batch_v1_tick_header_bytes; + const auto cellular_v2_min = + server::cellular_batch_header_bytes + ticks * server::cellular_batch_v2_tick_header_bytes; const auto cellular_current_min = server::cellular_batch_header_bytes + ticks * server::cellular_batch_tick_header_bytes; const auto cellular_max = server::cellular_batch_header_bytes + ticks * (server::cellular_batch_tick_header_bytes + + biology::max_cellular_tick_lives * + (server::cellular_batch_intent_bytes + server::cellular_batch_birth_bytes + + server::cellular_batch_mutation_bytes)); + const auto cellular_v2_max = + server::cellular_batch_header_bytes + + ticks * (server::cellular_batch_v2_tick_header_bytes + biology::max_cellular_tick_lives * (server::cellular_batch_intent_bytes + server::cellular_batch_birth_bytes)); const auto cellular_legacy_max = server::cellular_batch_header_bytes + - ticks * (sizeof(std::uint32_t) + + ticks * (server::cellular_batch_v1_tick_header_bytes + biology::max_cellular_tick_lives * server::cellular_batch_intent_bytes); const bool cellular_current_trace = b.trace_bytes >= cellular_current_min && b.trace_bytes <= cellular_max && @@ -123,14 +131,17 @@ bool structural(const CheckpointManifest& m) noexcept { const bool cellular_legacy_trace = b.trace_bytes >= cellular_legacy_min && b.trace_bytes <= cellular_legacy_max && (b.trace_bytes - cellular_legacy_min) % server::cellular_batch_intent_bytes == 0; - const bool trace_shape = primitive - ? b.trace_bytes >= server::primitive_batch_header_bytes && - b.trace_bytes <= server::max_primitive_batch_bytes && - (b.trace_bytes - server::primitive_batch_header_bytes) % - server::primitive_birth_bytes == - 0 - : cellular ? (cellular_current_trace || cellular_legacy_trace) - : b.trace_bytes == trace_bytes_for(ticks); + const bool cellular_v2_trace = b.trace_bytes >= cellular_v2_min && + b.trace_bytes <= cellular_v2_max && + (b.trace_bytes - cellular_v2_min) % sizeof(std::uint64_t) == 0; + const bool trace_shape = + primitive ? b.trace_bytes >= server::primitive_batch_header_bytes && + b.trace_bytes <= server::max_primitive_batch_bytes && + (b.trace_bytes - server::primitive_batch_header_bytes) % + server::primitive_birth_bytes == + 0 + : cellular ? (cellular_current_trace || cellular_v2_trace || cellular_legacy_trace) + : b.trace_bytes == trace_bytes_for(ticks); return link && trace_shape && !zero(b.trace_sha256) && b.decision_count <= max_batch_decisions && (b.decision_count == 0) == zero(b.decisions_sha256); diff --git a/src/server/lifecycle/cellular_batch.cpp b/src/server/lifecycle/cellular_batch.cpp index b42c0e2..f5302cd 100644 --- a/src/server/lifecycle/cellular_batch.cpp +++ b/src/server/lifecycle/cellular_batch.cpp @@ -43,7 +43,8 @@ bool valid_ticks(std::span ticks) { std::set accounts; for (const auto& tick : ticks) { if (tick.lives.size() > biology::max_cellular_tick_lives || - tick.births.size() > biology::max_cellular_tick_lives) + tick.births.size() > biology::max_cellular_tick_lives || + tick.mutations.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]; @@ -60,6 +61,14 @@ bool valid_ticks(std::span ticks) { if (!identities.insert(birth.identity).second || !accounts.insert(birth.account).second) return false; } + for (std::size_t i = 0; i < tick.mutations.size(); ++i) { + const auto& mutation = tick.mutations[i]; + if (mutation.life_id == 0 || mutation.event_index == 0 || mutation.offer_tick == 0 || + mutation.successor_id == 0 || mutation.operator_id == 0 || + (i > 0 && tick.mutations[i - 1].life_id >= mutation.life_id) || + !identities.insert(mutation.successor_id).second) + return false; + } } return true; } @@ -99,6 +108,31 @@ bool valid_actions(std::span actions, std::uint64_t } return true; } +bool mutation_action_less(const CellularMutationActionRecord& left, + const CellularMutationActionRecord& right) { + return left.tick < right.tick || + (left.tick == right.tick && + (left.account < right.account || + (left.account == right.account && left.action_id < right.action_id))); +} +bool valid_mutation_actions(std::span actions, + std::uint64_t begin_tick, std::uint64_t end_tick) { + if (end_tick <= begin_tick || actions.size() > max_cellular_action_records) + return false; + for (std::size_t i = 0; i < actions.size(); ++i) { + const auto& action = actions[i]; + const auto& input = action.input; + if (all_zero(action.account) || action.action_id == 0 || action.tick <= begin_tick || + action.tick > end_tick || input.life_id == 0 || input.event_index == 0 || + input.offer_tick == 0 || input.offer_tick > begin_tick || input.operator_id == 0 || + (i != 0 && !mutation_action_less(actions[i - 1], action))) + return false; + for (std::size_t j = 0; j < i; ++j) + if (actions[j].account == action.account && actions[j].action_id == action.action_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); @@ -112,20 +146,40 @@ void put_intent(std::vector& out, const CellularLifeIntent& life) std::expected, CellularBatchError> encode_cellular_batch(const CellularBatch& batch) { - if (!valid_header(batch.header, batch.ticks.size()) || !valid_ticks(batch.ticks)) + if ((batch.format_version != cellular_batch_format && + batch.format_version != cellular_batch_v2_format && + batch.format_version != cellular_batch_legacy_format) || + !valid_header(batch.header, batch.ticks.size()) || !valid_ticks(batch.ticks)) return std::unexpected{CellularBatchError::format}; + if (batch.format_version == cellular_batch_legacy_format && + std::ranges::any_of(batch.ticks, [](const auto& tick) { + return !tick.births.empty() || !tick.mutations.empty(); + })) + return std::unexpected{CellularBatchError::format}; + if (batch.format_version == cellular_batch_v2_format && + std::ranges::any_of(batch.ticks, [](const auto& tick) { return !tick.mutations.empty(); })) + return std::unexpected{CellularBatchError::format}; + const auto tick_header_bytes = + batch.format_version == cellular_batch_format ? cellular_batch_tick_header_bytes + : batch.format_version == cellular_batch_v2_format ? cellular_batch_v2_tick_header_bytes + : cellular_batch_v1_tick_header_bytes; std::size_t size = cellular_batch_header_bytes; for (const auto& tick : batch.ticks) { const auto records = tick.lives.size() * cellular_batch_intent_bytes + - tick.births.size() * cellular_batch_birth_bytes; - if (records > max_cellular_batch_bytes - size - cellular_batch_tick_header_bytes) + (batch.format_version >= cellular_batch_v2_format + ? tick.births.size() * cellular_batch_birth_bytes + : 0) + + (batch.format_version == cellular_batch_format + ? tick.mutations.size() * cellular_batch_mutation_bytes + : 0); + if (records > max_cellular_batch_bytes - size - tick_header_bytes) return std::unexpected{CellularBatchError::format}; - size += cellular_batch_tick_header_bytes + records; + size += 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, batch.format_version, 4); put(out, 4, 4); // Profile 4. put(out, batch.header.world, 8); put(out, batch.header.region, 8); @@ -139,15 +193,32 @@ encode_cellular_batch(const CellularBatch& batch) { put(out, 0, 4); for (const auto& tick : batch.ticks) { put(out, tick.lives.size(), 4); - put(out, tick.births.size(), 4); + if (batch.format_version >= cellular_batch_v2_format) + put(out, tick.births.size(), 4); + if (batch.format_version == cellular_batch_format) { + put(out, tick.mutations.size(), 4); + put(out, 0, 4); + } for (const auto& life : tick.lives) put_intent(out, life); for (const auto& birth : tick.births) { + if (batch.format_version < cellular_batch_v2_format) + break; out.insert(out.end(), birth.account.begin(), birth.account.end()); put(out, birth.request, 8); put(out, birth.generation, 8); put(out, birth.identity, 8); } + for (const auto& mutation : tick.mutations) { + if (batch.format_version != cellular_batch_format) + break; + put(out, mutation.life_id, 8); + put(out, mutation.event_index, 8); + put(out, mutation.offer_tick, 8); + put(out, mutation.successor_id, 8); + put(out, mutation.operator_id, 2); + put(out, 0, 6); + } } if (out.size() != size) return std::unexpected{CellularBatchError::format}; @@ -160,13 +231,15 @@ decode_cellular_batch(std::span bytes) { !std::ranges::equal(bytes.first(magic.size()), magic)) return std::unexpected{CellularBatchError::format}; const auto format = get(bytes, 8, 4); - if ((format != cellular_batch_format && format != cellular_batch_legacy_format) || + if ((format != cellular_batch_format && format != cellular_batch_v2_format && + format != cellular_batch_legacy_format) || get(bytes, 12, 4) != 4) return std::unexpected{CellularBatchError::version}; if (get(bytes, 108, 4) != 0) return std::unexpected{CellularBatchError::format}; CellularBatch out; + out.format_version = static_cast(format); auto& h = out.header; h.world = get(bytes, 16, 8); h.region = get(bytes, 24, 8); @@ -185,21 +258,30 @@ decode_cellular_batch(std::span bytes) { 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) { - const auto tick_header_bytes = format == cellular_batch_format - ? cellular_batch_tick_header_bytes - : sizeof(std::uint32_t); + const auto tick_header_bytes = + format == cellular_batch_format ? cellular_batch_tick_header_bytes + : format == cellular_batch_v2_format ? cellular_batch_v2_tick_header_bytes + : cellular_batch_v1_tick_header_bytes; if (bytes.size() - offset < tick_header_bytes) return std::unexpected{CellularBatchError::format}; const auto count = get(bytes, offset, 4); offset += 4; std::uint64_t birth_count{}; - if (format == cellular_batch_format) { + if (format >= cellular_batch_v2_format) { birth_count = get(bytes, offset, 4); offset += 4; } + std::uint64_t mutation_count{}; + if (format == cellular_batch_format) { + mutation_count = get(bytes, offset, 4); + if (get(bytes, offset + 4, 4) != 0) + return std::unexpected{CellularBatchError::format}; + offset += 8; + } if (count > biology::max_cellular_tick_lives || count > (bytes.size() - offset) / cellular_batch_intent_bytes || - birth_count > biology::max_cellular_tick_lives) + birth_count > biology::max_cellular_tick_lives || + mutation_count > biology::max_cellular_tick_lives) return std::unexpected{CellularBatchError::format}; CellularTickIntents step; step.lives.reserve(static_cast(count)); @@ -245,6 +327,25 @@ decode_cellular_batch(std::span bytes) { step.births.push_back(birth); offset += cellular_batch_birth_bytes; } + if (mutation_count > (bytes.size() - offset) / cellular_batch_mutation_bytes) + return std::unexpected{CellularBatchError::format}; + step.mutations.reserve(static_cast(mutation_count)); + for (std::uint64_t i = 0; i < mutation_count; ++i) { + const auto record = bytes.subspan(offset, cellular_batch_mutation_bytes); + CellularMutationSelection mutation; + mutation.life_id = get(record, 0, 8); + mutation.event_index = get(record, 8, 8); + mutation.offer_tick = get(record, 16, 8); + mutation.successor_id = get(record, 24, 8); + mutation.operator_id = static_cast(get(record, 32, 2)); + if (!all_zero(record.subspan(34, 6)) || mutation.life_id == 0 || + mutation.event_index == 0 || mutation.offer_tick == 0 || + mutation.successor_id == 0 || mutation.operator_id == 0 || + (!step.mutations.empty() && step.mutations.back().life_id >= mutation.life_id)) + return std::unexpected{CellularBatchError::format}; + step.mutations.push_back(mutation); + offset += cellular_batch_mutation_bytes; + } out.ticks.push_back(std::move(step)); } if (offset != bytes.size()) @@ -265,7 +366,7 @@ replay_cellular_batch(const CellularLives& base, const CellularBatch& batch) { if (!replay) return std::unexpected{CellularBatchError::state}; for (const auto& tick : batch.ticks) { - if (!(*replay)->advance_tick(tick.lives)) + if (!(*replay)->advance_tick_with_mutations(tick.lives, tick.mutations)) return std::unexpected{CellularBatchError::state}; for (const auto& birth : tick.births) { const LifeRequest request{birth.account, birth.request}; @@ -318,6 +419,55 @@ decode_cellular_action_block(std::span bytes, std::uint64_t return out; } +std::expected, CellularActionBlockError> +encode_cellular_mutation_action_block(std::span actions, + std::uint64_t begin_tick, std::uint64_t end_tick) { + if (!valid_mutation_actions(actions, begin_tick, end_tick)) + return std::unexpected{CellularActionBlockError::format}; + std::vector out; + out.reserve(actions.size() * cellular_mutation_action_record_bytes); + for (const auto& action : actions) { + out.insert(out.end(), action.account.begin(), action.account.end()); + put(out, action.action_id, 8); + put(out, action.tick, 8); + put(out, action.input.life_id, 8); + put(out, action.input.event_index, 8); + put(out, action.input.offer_tick, 8); + put(out, action.input.operator_id, 2); + put(out, 0, 6); + } + return out; +} + +std::expected, CellularActionBlockError> +decode_cellular_mutation_action_block(std::span bytes, std::uint64_t begin_tick, + std::uint64_t end_tick) { + if (end_tick <= begin_tick || bytes.size() % cellular_mutation_action_record_bytes != 0 || + bytes.size() / cellular_mutation_action_record_bytes > max_cellular_action_records) + return std::unexpected{CellularActionBlockError::format}; + std::vector out; + out.reserve(bytes.size() / cellular_mutation_action_record_bytes); + std::size_t offset{}; + for (std::size_t i = 0; i < bytes.size() / cellular_mutation_action_record_bytes; ++i) { + const auto record = bytes.subspan(offset, cellular_mutation_action_record_bytes); + CellularMutationActionRecord action; + std::ranges::copy(record.first(16), action.account.begin()); + action.action_id = get(record, 16, 8); + action.tick = get(record, 24, 8); + action.input.life_id = get(record, 32, 8); + action.input.event_index = get(record, 40, 8); + action.input.offer_tick = get(record, 48, 8); + action.input.operator_id = static_cast(get(record, 56, 2)); + if (!all_zero(record.subspan(58, 6))) + return std::unexpected{CellularActionBlockError::format}; + out.push_back(action); + offset += cellular_mutation_action_record_bytes; + } + if (!valid_mutation_actions(out, begin_tick, end_tick)) + return std::unexpected{CellularActionBlockError::format}; + return out; +} + std::expected match_cellular_actions_to_trace(const CellularLives& predecessor, const CellularBatch& batch, std::span actions) { @@ -354,4 +504,64 @@ match_cellular_actions_to_trace(const CellularLives& predecessor, const Cellular } return {}; } + +std::expected +match_cellular_mutation_actions_to_trace(const CellularLives& predecessor, + const CellularBatch& batch, + std::span actions) { + if (!valid_header(batch.header, batch.ticks.size()) || !valid_ticks(batch.ticks) || + !valid_mutation_actions(actions, batch.header.begin_tick, batch.header.end_tick)) + return std::unexpected{CellularActionBlockError::format}; + if (predecessor.world() != batch.header.world || predecessor.region() != batch.header.region || + predecessor.frame() != batch.header.frame || predecessor.tick() != batch.header.begin_tick) + return std::unexpected{CellularActionBlockError::mismatch}; + std::size_t trace_selection_count{}; + for (const auto& tick : batch.ticks) + trace_selection_count += tick.mutations.size(); + if (trace_selection_count != actions.size()) + return std::unexpected{CellularActionBlockError::mismatch}; + for (std::size_t tick_index = 0; tick_index < batch.ticks.size(); ++tick_index) { + const auto tick = batch.header.begin_tick + tick_index + 1; + for (const auto& selection : batch.ticks[tick_index].mutations) { + const auto life = std::ranges::find( + predecessor.lives(), selection.life_id, + [](const CellularOwnedLife& value) { return value.identity.life; }); + if (life == predecessor.lives().end()) + return std::unexpected{CellularActionBlockError::mismatch}; + const auto matches = std::ranges::count_if(actions, [&](const auto& action) { + return action.account == life->account && action.tick == tick && + action.input.life_id == selection.life_id && + action.input.event_index == selection.event_index && + action.input.offer_tick == selection.offer_tick && + action.input.operator_id == selection.operator_id; + }); + if (matches != 1) + return std::unexpected{CellularActionBlockError::mismatch}; + } + } + for (const auto& action : actions) { + const auto life = + std::ranges::find(predecessor.lives(), action.account, &CellularOwnedLife::account); + if (life == predecessor.lives().end() || + (predecessor.format_version() != cellular_lives_revision_format)) + return std::unexpected{CellularActionBlockError::mismatch}; + const auto tick_index = static_cast(action.tick - batch.header.begin_tick - 1); + const auto& mutations = batch.ticks[tick_index].mutations; + const auto selection = std::ranges::lower_bound(mutations, life->identity.life, {}, + &CellularMutationSelection::life_id); + if (selection == mutations.end() || selection->life_id != life->identity.life || + selection->event_index != action.input.event_index || + selection->offer_tick != action.input.offer_tick || + selection->operator_id != action.input.operator_id || !life->mutation.offer || + life->mutation.event_index != action.input.event_index || + life->mutation.offer_tick != action.input.offer_tick || + life->identity.life != action.input.life_id || + life->mutation.offer->parent != life->identity.genome || + !std::ranges::any_of(life->mutation.offer->candidates, [&](const auto& candidate) { + return candidate.operator_id == action.input.operator_id; + })) + return std::unexpected{CellularActionBlockError::mismatch}; + } + return {}; +} } // namespace evo::server diff --git a/src/server/lifecycle/cellular_lives.cpp b/src/server/lifecycle/cellular_lives.cpp index 1f3d4e6..2f5bb03 100644 --- a/src/server/lifecycle/cellular_lives.cpp +++ b/src/server/lifecycle/cellular_lives.cpp @@ -953,6 +953,16 @@ CellularLives::restore(std::span bytes, std::uint64_t world, std::expected CellularLives::advance_tick(std::span intents) { + auto execution = + advance_tick_with_mutations(intents, std::span{}); + if (!execution) + return std::unexpected{execution.error()}; + return std::move(execution->step); +} + +std::expected +CellularLives::advance_tick_with_mutations(std::span intents, + std::span selections) { if (state_->config.tick == std::numeric_limits::max()) return std::unexpected{CellularLivesError::invalid_tick}; if (intents.size() != state_->lives.size()) @@ -964,6 +974,33 @@ CellularLives::advance_tick(std::span intents) { ordered[i].life_id != state_->lives[i].identity.life) return std::unexpected{CellularLivesError::invalid_intents}; + if (!selections.empty() && state_->format_version != cellular_lives_revision_format) + return std::unexpected{CellularLivesError::version}; + for (std::size_t i = 0; i < selections.size(); ++i) { + const auto& selection = selections[i]; + if (selection.life_id == 0 || selection.event_index == 0 || selection.offer_tick == 0 || + selection.operator_id == 0 || selection.successor_id == 0 || + (i != 0 && selections[i - 1].life_id >= selection.life_id)) + return std::unexpected{CellularLivesError::invalid_intents}; + const auto life = std::ranges::lower_bound( + state_->lives, selection.life_id, {}, + [](const CellularOwnedLife& value) { return value.identity.life; }); + if (life == state_->lives.end() || life->identity.life != selection.life_id || + !life->mutation.offer || life->mutation.event_index != selection.event_index || + life->mutation.offer_tick != selection.offer_tick || + life->mutation.offer->parent != life->identity.genome || + life->mutation.offer->context.event_index != selection.event_index || + !std::ranges::any_of(life->mutation.offer->candidates, + [&](const auto& candidate) { + return candidate.operator_id == selection.operator_id; + }) || + state_->archive.find(selection.successor_id)) + return std::unexpected{CellularLivesError::mutation}; + for (std::size_t j = 0; j < i; ++j) + if (selections[j].successor_id == selection.successor_id) + return std::unexpected{CellularLivesError::identity}; + } + std::vector inputs; inputs.reserve(state_->lives.size()); for (std::size_t i = 0; i < state_->lives.size(); ++i) { @@ -1060,6 +1097,46 @@ CellularLives::advance_tick(std::span intents) { corpse_budget->phenotype.extent, dead.inventory}); next_lives.erase(found); } + std::vector mutation_results; + mutation_results.reserve(selections.size()); + for (const auto& selection : selections) { + const auto found = std::ranges::lower_bound( + next_lives, selection.life_id, {}, + [](const CellularOwnedLife& value) { return value.identity.life; }); + if (found == next_lives.end() || found->identity.life != selection.life_id) { + mutation_results.push_back( + {selection.life_id, selection.successor_id, CellularMutationResult::life_died}); + continue; + } + const auto parent = next_archive.find(found->identity.genome); + if (!parent || !found->mutation.offer) + return std::unexpected{CellularLivesError::mutation}; + auto transition = biology::fund_mutation( + *parent, found->mutation.offer->context, *found->mutation.offer, selection.operator_id, + selection.successor_id, selection.successor_id, found->reserve); + if (!transition) { + if (transition.error() == biology::MutationError::insufficient_funding) { + mutation_results.push_back({selection.life_id, selection.successor_id, + CellularMutationResult::insufficient_funding}); + continue; + } + return std::unexpected{CellularLivesError::mutation}; + } + if (!next_archive.record_mutation(found->identity.organism, transition->successor, + {proposal->end_tick})) + return std::unexpected{CellularLivesError::archive}; + if (transition->intake.energy.value > + std::numeric_limits::max() - next_heat.energy.value) + return std::unexpected{CellularLivesError::overflow}; + next_heat.energy.value += transition->intake.energy.value; + found->identity.genome = selection.successor_id; + found->body = transition->body; + found->reserve = transition->reserve; + found->mutation.offer.reset(); + found->mutation.offer_tick = 0; + mutation_results.push_back( + {selection.life_id, selection.successor_id, CellularMutationResult::applied}); + } std::ranges::sort(next_corpses, {}, &CellularOwnedCorpse::organism); if (proposal->heat_released.value > std::numeric_limits::max() - next_heat.energy.value) @@ -1106,6 +1183,6 @@ CellularLives::advance_tick(std::span intents) { } state_.swap(next); last_tick_feedback_ = std::move(feedback); - return std::move(*proposal); + return CellularTickExecution{std::move(*proposal), std::move(mutation_results)}; } } // namespace evo::server diff --git a/tests/cellular_lives_tests.cpp b/tests/cellular_lives_tests.cpp index a6c34c1..f634bb9 100644 --- a/tests/cellular_lives_tests.cpp +++ b/tests/cellular_lives_tests.cpp @@ -98,6 +98,7 @@ void reserved_birth_is_funded_and_detached() { auto trace_base = empty_cellular_owner(founder_price(1), {{0, 0}}); CellularBatch birth_batch; + birth_batch.format_version = cellular_batch_v2_format; birth_batch.header = {trace_base->world(), trace_base->region(), trace_base->frame(), {}, {}, trace_base->tick(), trace_base->tick() + 1}; birth_batch.header.predecessor.fill(3); @@ -123,7 +124,14 @@ void reserved_birth_is_funded_and_detached() { (*legacy_bytes)[8] = static_cast(cellular_batch_legacy_format); legacy_bytes->erase(legacy_bytes->begin() + 116, legacy_bytes->begin() + 120); const auto decoded_legacy = decode_cellular_batch(*legacy_bytes); - CHECK(decoded_legacy && decoded_legacy->ticks[0].births.empty()); + CHECK(decoded_legacy && decoded_legacy->format_version == cellular_batch_legacy_format && + decoded_legacy->ticks[0].births.empty()); + auto v2_bytes = encode_cellular_batch(legacy_batch); + CHECK(v2_bytes); + const auto decoded_v2 = decode_cellular_batch(*v2_bytes); + CHECK(decoded_v2 && decoded_v2->format_version == cellular_batch_v2_format && + decoded_v2->ticks[0].births.empty() && + decoded_v2->ticks[0].lives == legacy_batch.ticks[0].lives); auto invalid_birth_batch = birth_batch; invalid_birth_batch.ticks[0].births[0].generation = 0; CHECK(encode_cellular_batch(invalid_birth_batch).error() == CellularBatchError::format); @@ -297,6 +305,82 @@ void retained_feeding_earns_a_persistent_mutation_offer() { CHECK(owner.advance_tick(idle)); const auto after_tick = owner.mutation_offer(life_id); CHECK(after_tick && after_tick == earned); + + auto revision_owner = owner.migrate_to_v3(); + CHECK(revision_owner && (*revision_owner)->format_version() == cellular_lives_revision_format); + const auto current_offer = (*revision_owner)->mutation_offer(life_id); + CHECK(current_offer && *current_offer); + const CellularMutationSelection selection{life_id, + (*revision_owner)->lives()[0].mutation.event_index, + (*revision_owner)->lives()[0].mutation.offer_tick, + 999, (*current_offer)->candidates[0].operator_id}; + CellularBatch mutation_batch; + mutation_batch.header = {(*revision_owner)->world(), + (*revision_owner)->region(), + (*revision_owner)->frame(), + {}, + {}, + (*revision_owner)->tick(), + (*revision_owner)->tick() + 1}; + mutation_batch.header.predecessor.fill(0x31); + mutation_batch.header.predecessor_digest.fill(0x41); + CellularTickIntents mutation_tick; + mutation_tick.lives.push_back({life_id, {}}); + mutation_tick.mutations.push_back(selection); + mutation_batch.ticks.push_back(mutation_tick); + const auto mutation_bytes = encode_cellular_batch(mutation_batch); + CHECK(mutation_bytes); + const auto decoded_mutation = decode_cellular_batch(*mutation_bytes); + CHECK(decoded_mutation && *decoded_mutation == mutation_batch); + const std::array mutation_actions{CellularMutationActionRecord{ + (*revision_owner)->lives()[0].account, + 44, + mutation_batch.header.end_tick, + {life_id, selection.event_index, selection.offer_tick, selection.operator_id}}}; + const auto action_block = encode_cellular_mutation_action_block( + mutation_actions, mutation_batch.header.begin_tick, mutation_batch.header.end_tick); + CHECK(action_block); + const auto decoded_action_block = decode_cellular_mutation_action_block( + *action_block, mutation_batch.header.begin_tick, mutation_batch.header.end_tick); + CHECK(decoded_action_block && + *decoded_action_block == + std::vector{mutation_actions.front()} && + match_cellular_mutation_actions_to_trace(**revision_owner, *decoded_mutation, + *decoded_action_block)); + const auto replayed_mutation = replay_cellular_batch(**revision_owner, *decoded_mutation); + CHECK(replayed_mutation && (*replayed_mutation)->tick() == mutation_batch.header.end_tick); + const std::array idle_next{CellularLifeIntent{life_id, {}}}; + const std::array selections{selection}; + const auto direct_mutation = + (*revision_owner)->advance_tick_with_mutations(idle_next, selections); + CHECK(direct_mutation && direct_mutation->mutations.size() == 1 && + (*replayed_mutation)->snapshot() == (*revision_owner)->snapshot() && + (*revision_owner)->current_total() == (*revision_owner)->opening_total()); + if (direct_mutation->mutations[0].result == CellularMutationResult::applied) { + CHECK(direct_mutation->mutations[0].successor_id == selection.successor_id && + (*revision_owner)->lives()[0].identity.genome == selection.successor_id && + (*revision_owner) + ->archive() + .current_genome((*revision_owner)->lives()[0].identity.organism) == + selection.successor_id && + (*revision_owner)->archive().revisions().size() == 1 && + !(*revision_owner)->lives()[0].mutation.offer); + } else if (direct_mutation->mutations[0].result == + CellularMutationResult::insufficient_funding) { + CHECK(direct_mutation->mutations[0].result == + CellularMutationResult::insufficient_funding && + (*revision_owner)->lives()[0].identity.genome == + (*revision_owner) + ->archive() + .organism((*revision_owner)->lives()[0].identity.organism) + ->genome && + (*revision_owner)->archive().revisions().empty() && + (*revision_owner)->lives()[0].mutation.offer.has_value()); + } else { + CHECK(direct_mutation->mutations[0].result == CellularMutationResult::life_died && + (*revision_owner)->lives().empty() && (*revision_owner)->corpses().size() == 1 && + (*revision_owner)->archive().revisions().empty()); + } } void owner_advances_and_closes_death_atomically() { diff --git a/tools/ci/postgres-check.sh b/tools/ci/postgres-check.sh index b613938..01b3b11 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,21,22,23,24,25,26,27 +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 # 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")" = 27: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 +test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 28: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 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,21,22,23,24,25,26,27 +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 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=27 WHERE version=26' +psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=29 WHERE version=28' 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=26 WHERE version=27" +psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=28 WHERE version=29" 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 d663a51..3714d88 100644 --- a/tools/migrate-postgres.sh +++ b/tools/migrate-postgres.sh @@ -66,6 +66,8 @@ digest_26="$(sha256sum "$root/migrations/0026_cellular_profile_migration.sql")" digest_26="${digest_26%% *}" digest_27="$(sha256sum "$root/migrations/0027_cellular_v3_migration.sql")" digest_27="${digest_27%% *}" +digest_28="$(sha256sum "$root/migrations/0028_cellular_v3_trace_bounds.sql")" +digest_28="${digest_28%% *}" 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" \ @@ -86,5 +88,6 @@ psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \ --set="migration_sha_25=$digest_25" \ --set="migration_sha_26=$digest_26" \ --set="migration_sha_27=$digest_27" \ + --set="migration_sha_28=$digest_28" \ --set="runtime_role=$runtime_role" \ --file="$root/migrations/apply.sql"