From 71d195d4e8ca9b04689d8393cd7f5a10bd9469ae Mon Sep 17 00:00:00 2001 From: dingo Date: Thu, 22 Oct 2026 10:27:09 -0400 Subject: [PATCH] feat: durable mutation queue (schema 59) A mutation choice's reserved tick is now the earliest tick it may bind at, not the only one (ADR-0121 amendment). With write-behind commits, a choice reserved for head + 1 landed in ticks already being simulated or committed: the batch was refused, or the reservation was orphaned for a past tick and stalled the region. Schema 59: a bound record carries the tick it applied at and the rest of its input must equal the reservation's; the "omits a reserved mutation" refusals are removed. Existing rows are unchanged, so an orphaned reservation binds at the next commit that carries it. The host keeps every unbound reservation queued and the open batch binds those the commit in flight does not carry, at its next tick; the earlier hold on new choices is removed. A choice that outlives its life binds as life_died (owner, trace matching and recovery accept it when no life with that ID remains). Recovery takes a bound choice's tick from its trace. Tests: a choice skipped by one checkpoint binds a tick late and recovers; builder late binding, refusal before the earliest tick, and a departed life's life_died outcome. Full local PostgreSQL qualification passes. Co-Authored-By: Claude Opus 5.5 --- apps/evo-server/main.cpp | 31 ++-- .../region/dev-session-route/listener.yaml | 2 +- ...121-durable-cellular-mutation-selection.md | 25 +++ include/evo/server/cellular_region_host.hpp | 24 +-- migrations/0059_durable_mutation_queue.sql | 159 ++++++++++++++++++ migrations/apply.sql | 15 +- src/persistence/cellular_checkpoint_store.cpp | 54 +++++- src/server/cellular_region_host.cpp | 37 ++-- src/server/lifecycle/cellular_batch.cpp | 31 +++- src/server/lifecycle/cellular_lives.cpp | 10 +- src/server/session/cellular_session_batch.cpp | 32 ++-- tests/cellular_publication_tests.cpp | 37 ++-- tests/cellular_session_batch_tests.cpp | 44 +++++ tests/region_migration_tests.cpp | 3 +- tools/ci/history-domain-check.sh | 5 +- tools/ci/postgres-check.sh | 10 +- tools/migrate-postgres.sh | 4 +- 17 files changed, 409 insertions(+), 114 deletions(-) create mode 100644 migrations/0059_durable_mutation_queue.sql diff --git a/apps/evo-server/main.cpp b/apps/evo-server/main.cpp index 2bcf520..ec1b939 100644 --- a/apps/evo-server/main.cpp +++ b/apps/evo-server/main.cpp @@ -1063,8 +1063,6 @@ int serve_cellular(Options& o) { if (batch_builder) session_status.simulated_tick = std::max(session_status.simulated_tick, batch_builder->candidate().tick()); - // Mutation choices are reserved only while nothing is simulated past the head. - host.hold_mutation_selections(batch_builder.has_value() || host.commit_in_flight()); bridge->poll(session_status, wall); deliver_finalized(); set_ready(status.ready); @@ -1160,9 +1158,8 @@ int serve_cellular(Options& o) { } const auto flush_ticks = std::clamp( static_cast(o.host.flush_interval.count()) / 50, 1, 60); - // Births and mutation choices wait for a batch that starts on the durable head. - const bool admissions_waiting = - !held_births.empty() || !host.queued_mutations().empty() || host.selections_held(); + // Births wait for a batch that starts on the durable head. + const bool admissions_waiting = !held_births.empty(); // Write-behind commit: the open batch goes to the host's worker thread and the next // batch continues from its candidate under a provisional head, rebased when the commit // lands. One commit is in flight; meanwhile the open batch simply keeps growing. @@ -1239,15 +1236,9 @@ int serve_cellular(Options& o) { // Rules 12 regions hold thousands of deposits, so their committed payload is // large: commit every flush interval (ADR-0089: 1 s), not every tick. Players // see the batch's working state at once; outcomes become final at its commit. - // Births and mutation choices commit promptly, in their own fresh batch. + // Births commit promptly, in their own fresh batch. if (batch_builder && admissions_waiting) flush(); - // The head is current: held choices reserve its next tick and join the - // fresh batch below. - if (!batch_builder && !host.commit_in_flight() && host.selections_held()) { - host.hold_mutation_selections(false); - host.admit_held_selections(); - } const auto* current = batch_builder ? &batch_builder->candidate() : state; // A failed commit can leave the host without state until it recovers. While a // commit is in flight the open batch is the only place to simulate, and it is @@ -1310,16 +1301,24 @@ int serve_cellular(Options& o) { if (!batch_builder) { bridge->dropped(actions, true); } else { - // Births and mutation choices only ever enter a fresh batch on the - // durable head. + // Births only ever enter a fresh batch on the durable head. Queued + // mutation choices (ADR-0121 amendment, schema 59) bind at the next + // tick simulated, unless this batch or the commit in flight has them. const bool fresh = batch_builder->batch().ticks.empty() && !host.commit_in_flight(); const std::span births = fresh ? std::span{held_births} : std::span{}; + auto unbound = host.unbound_mutations(); + std::erase_if(unbound, [&](const auto& queued) { + return std::ranges::any_of( + batch_builder->mutation_actions(), [&](const auto& bound) { + return bound.account == queued.account && + bound.action_id == queued.action_id; + }); + }); const std::span mutations = - fresh ? host.queued_mutations() - : std::span{}; + unbound; auto appended = batch_builder->append_with_birth_refusals( tick, baseline, actions, births, mutations); if (!appended && !actions.empty()) { diff --git a/deploy/kubernetes/region/dev-session-route/listener.yaml b/deploy/kubernetes/region/dev-session-route/listener.yaml index a4c850e..85bc14c 100644 --- a/deploy/kubernetes/region/dev-session-route/listener.yaml +++ b/deploy/kubernetes/region/dev-session-route/listener.yaml @@ -12,7 +12,7 @@ spec: env: - {name: REGION_SESSION, value: "dev"} - {name: REGION_SESSION_LISTEN, value: "0.0.0.0:7400"} - - {name: REGION_SESSION_WINDOW, value: "dev-20261022T134205-b3f15c10:1792690925"} + - {name: REGION_SESSION_WINDOW, value: "dev-20261022T142646-799e10c6:1792693606"} - {name: REGION_M10_MAX_GRANT_LIFETIME, value: "14400"} ports: # QUIC (ADR-0090): UDP only; nothing listens on TCP 7400. diff --git a/docs/adr/0121-durable-cellular-mutation-selection.md b/docs/adr/0121-durable-cellular-mutation-selection.md index 59b8067..451d3e0 100644 --- a/docs/adr/0121-durable-cellular-mutation-selection.md +++ b/docs/adr/0121-durable-cellular-mutation-selection.md @@ -131,3 +131,28 @@ the later 11.4d client action and review UI are complete. extends v10 before any v10 server reaches DEV; v1-v9 are unchanged. - Remaining after this: the client choice and review UI (maintainer) and the 11.4d playtest notes. + +## Amendment: durable mutation queue (schema 59, 2026-10-22) + +The exact-tick rule above assumed that reserving a choice and committing a batch were +serialized on one connection, one tick at a time. Rules-12 regions commit a batch of +ticks on a background thread while the next batch is simulated (ADR-0178 follow-up), so +a choice reserved for "head + 1" could land in ticks already simulated or being +committed. The batch was then refused (losing its ticks), or, when the commit's check ran +before the reservation committed, the reservation was left for a tick that had passed +and stalled the region on DEV (tick 9752). + +- A reservation's tick is now the earliest tick the choice may bind at. The region + binds a queued choice at the first tick it simulates after admitting it; the + mutation record carries the tick it bound at, and the rest of its input must equal + the reservation's. Stored rows are unchanged, so a reservation whose tick already + passed binds at the next commit that carries it. +- The "omits a reserved mutation" refusals are removed: an unbound reservation waits in + the queue. The host keeps every unbound reservation queued (and reloads them on + recovery), and the open batch binds those the commit in flight does not carry. +- A choice can outlive its life while it waits. Its selection then names a life that is + gone; the owner records `life_died` and changes nothing, and trace matching and + recovery accept such a selection when no life with its ID remains. +- Recovery takes each bound choice's tick from its trace selection, not from the stored + input. +- Births still enter only a fresh batch on the durable head. diff --git a/include/evo/server/cellular_region_host.hpp b/include/evo/server/cellular_region_host.hpp index 8c33c0f..85eb364 100644 --- a/include/evo/server/cellular_region_host.hpp +++ b/include/evo/server/cellular_region_host.hpp @@ -139,28 +139,19 @@ class CellularRegionHost { std::uint64_t generation, std::uint64_t action_id, const CellularMutationActionInput& input); - // A choice reserves the tick after the committed head, so it must not be reserved while - // ticks past that head are simulated (an open batch, or a commit in flight): the batch - // would commit without it, or skip its tick. While held, a new choice is remembered and - // answered not_ready (the client repeats it); admit_held_selections reserves them once - // nothing is simulated past the head. - void hold_mutation_selections(bool hold) noexcept { - hold_selections_ = hold; - } - [[nodiscard]] bool selections_held() const noexcept { - return !held_selections_.empty(); - } - void admit_held_selections(); // A queued choice the builder refused (its tick passed): reload the queue from the // store, which keeps only reservations for the head's next tick. void recheck_mutations() noexcept { mutations_uncertain_ = true; } - // Reserved choices for the next tick, in reservation order; pass them to the builder. + // Reserved choices, in reservation order (ADR-0121 amendment, schema 59: a durable + // queue; each binds at a tick at or after its reservation's). [[nodiscard]] std::span queued_mutations() const noexcept { return queued_mutations_; } + // Queued choices not carried by the commit in flight: the open batch binds these. + [[nodiscard]] std::vector unbound_mutations() const; [[nodiscard]] std::vector take_finalized_mutations(); // v5 requests allocate durably, then wait for the scheduler to include the exact // reservation in a profile-4 batch. Repeating the request is idempotent. @@ -294,15 +285,8 @@ class CellularRegionHost { std::vector queued_births_; std::vector pending_birth_refusals_; std::vector queued_mutations_; - struct HeldSelection { - persistence::Uuid account{}; - std::uint64_t generation{}, action_id{}; - CellularMutationActionInput input{}; - }; - std::vector held_selections_; // A reservation may exist that the queue does not show; reload before publishing. bool mutations_uncertain_{}; - bool hold_selections_{}; std::vector finalized_mutations_; std::vector recent_mutations_; // Bounded, for exact repeats. struct Ending { diff --git a/migrations/0059_durable_mutation_queue.sql b/migrations/0059_durable_mutation_queue.sql new file mode 100644 index 0000000..a829cfe --- /dev/null +++ b/migrations/0059_durable_mutation_queue.sql @@ -0,0 +1,159 @@ +-- Durable mutation queue (ADR-0121 amendment, 2026-10-22). A reservation's tick is the +-- earliest tick its choice may apply at, not the only one: the region binds a queued choice +-- at a tick it simulates after admitting it, so a batch that was already being simulated or +-- committed when the choice arrived cannot orphan it. A bound record carries the tick it +-- applied at; the rest of its input must equal the reservation's. The exact-tick refusals of +-- schema 35 ("omits a reserved mutation") are removed: an unbound reservation simply waits. +-- Stored rows are unchanged, so a reservation whose tick has already passed is bound by the +-- next commit that carries it. + +CREATE OR REPLACE FUNCTION evolution.check_cellular_mutation_block(p_manifest bytea, p_trace bytea, + p_mutations bytea, p_results bytea) +RETURNS integer LANGUAGE plpgsql STABLE SECURITY INVOKER +SET search_path = pg_catalog, pg_temp AS $$ +DECLARE + c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(p_manifest); + n integer; + account uuid; + account_bytes bytea; + action numeric; + tick numeric; + input bytea; + previous_tick numeric := -1; + previous_account bytea := ''::bytea; + previous_action numeric := -1; + applied numeric; + r evolution.cellular_mutation_reservations%ROWTYPE; +BEGIN + IF p_mutations IS NULL OR p_results IS NULL OR octet_length(p_mutations) = 0 + OR octet_length(p_mutations) % 64 <> 0 OR octet_length(p_mutations) / 64 > 1024 + OR octet_length(p_results) <> octet_length(p_mutations) / 64 THEN + RAISE EXCEPTION 'invalid cellular mutation block' USING ERRCODE = '22023'; + END IF; + n := octet_length(p_mutations) / 64; + IF (SELECT count(*) FROM evolution.cellular_mutation_selections(p_trace, p_manifest)) <> n THEN + RAISE EXCEPTION 'cellular mutation block does not cover the trace' USING ERRCODE = '22023'; + END IF; + FOR i IN 0 .. n - 1 LOOP + account_bytes := substring(p_mutations FROM 64 * i + 1 FOR 16); + account := encode(account_bytes, 'hex')::uuid; + action := evolution.manifest_uint(p_mutations, 64 * i + 16, 8); + input := substring(p_mutations FROM 64 * i + 25 FOR 34); + tick := evolution.manifest_uint(input, 0, 8); + IF account = '00000000-0000-0000-0000-000000000000' OR action = 0 + OR NOT evolution.valid_cellular_mutation_input(input) + OR substring(p_mutations FROM 64 * i + 59 FOR 6) <> '\x000000000000' + OR (c.versions IN ('\x0a0000000800000002000000010000000100000004000000030000000200000001000000'::bytea, + '\x0b0000000900000002000000010000000100000005000000040000000200000001000000'::bytea, + '\x0c0000000a00000002000000010000000100000006000000050000000200000001000000'::bytea) + AND (get_byte(p_results,i) NOT IN (1,3,4) + OR (evolution.manifest_uint(input,32,2)=0 AND get_byte(p_results,i) NOT IN (1,3)) + OR (evolution.manifest_uint(input,32,2)<>0 AND get_byte(p_results,i) NOT IN (1,4)))) + OR (c.versions NOT IN ('\x0a0000000800000002000000010000000100000004000000030000000200000001000000'::bytea, + '\x0b0000000900000002000000010000000100000005000000040000000200000001000000'::bytea, + '\x0c0000000a00000002000000010000000100000006000000050000000200000001000000'::bytea) + AND get_byte(p_results,i)>3) + OR tick <= c.begin_tick OR tick > c.end_tick + OR (tick, account_bytes, action) <= (previous_tick, previous_account, previous_action) THEN + RAISE EXCEPTION 'invalid cellular mutation record' USING ERRCODE = '22023'; + END IF; + previous_tick := tick; + previous_account := account_bytes; + previous_action := action; + SELECT * INTO r FROM evolution.cellular_mutation_reservations x + WHERE x.world_id = c.world_id AND x.account_id = account AND x.action_id = action; + -- The reservation's tick is the earliest the choice may apply at; the record carries + -- the tick it applied at, and the rest of the input must match exactly. + applied := tick; + IF NOT FOUND OR r.region_id <> c.region_id + OR substring(r.input_bytes FROM 9) <> substring(input FROM 9) + OR r.target_tick <> evolution.manifest_uint(r.input_bytes, 0, 8) + OR applied < r.target_tick THEN + RAISE EXCEPTION 'cellular mutation reservation conflicts' USING ERRCODE = 'EVL16'; + END IF; + IF NOT EXISTS (SELECT 1 FROM evolution.cellular_mutation_selections(p_trace, p_manifest) s + WHERE s.tick = applied AND s.successor_identity = r.successor_identity + AND s.life_id = evolution.manifest_uint(input, 8, 8) + AND s.event_index = evolution.manifest_uint(input, 16, 8) + AND s.offer_tick = evolution.manifest_uint(input, 24, 8) + AND s.operator_id = evolution.manifest_uint(input, 32, 2)) THEN + RAISE EXCEPTION 'cellular mutation differs from its trace' USING ERRCODE = '22023'; + END IF; + END LOOP; + RETURN n; +END $$; + +-- Schema 35's ordinary publisher without the exact-tick refusal. +CREATE OR REPLACE FUNCTION evolution.publish_cellular_checkpoint(p_manifest bytea, p_decisions bytea, + p_actions bytea, p_trace bytea) +RETURNS TABLE(region_frame evolution.u64, head_sequence evolution.u64, head_commit uuid, + head_digest text, head_tick evolution.u64) +LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ +BEGIN + IF EXISTS (SELECT 1 FROM evolution.cellular_mutation_selections(p_trace, p_manifest)) THEN + RAISE EXCEPTION 'cellular mutation selections need their action block' USING ERRCODE = '22023'; + END IF; + RETURN QUERY SELECT * FROM evolution.publish_cellular_birth_checkpoint( + p_manifest, p_decisions, p_actions, p_trace); +END $$; + +-- Schema 35's mutation publisher without the exact-tick refusal. +CREATE OR REPLACE FUNCTION evolution.publish_cellular_mutation_checkpoint(p_manifest bytea, + p_decisions bytea, p_actions bytea, p_trace bytea, p_mutations bytea, p_results bytea) +RETURNS TABLE(region_frame evolution.u64, head_sequence evolution.u64, head_commit uuid, + head_digest text, head_tick evolution.u64) +LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ +DECLARE + c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(p_manifest); + n integer; + h record; + digest bytea; +BEGIN + IF c.profile <> 4 OR c.commit_sequence = 0 THEN + RAISE EXCEPTION 'invalid cellular publication' USING ERRCODE = '22023'; + END IF; + n := evolution.check_cellular_mutation_block(p_manifest, p_trace, p_mutations, p_results); + IF EXISTS (SELECT 1 FROM evolution.region_commits x WHERE x.commit_id = c.commit_id) THEN + IF NOT evolution.cellular_mutation_publications_match(c.world_id, c.commit_id, p_mutations, + p_results) THEN + RAISE EXCEPTION 'cellular mutation evidence differs on retry' USING ERRCODE = 'EVL07'; + END IF; + RETURN QUERY SELECT * FROM evolution.publish_cellular_birth_checkpoint( + p_manifest, p_decisions, p_actions, p_trace); + RETURN; + END IF; + -- A choice binds to one commit only; another commit's binding is a reused identity. + FOR i IN 0 .. n - 1 LOOP + IF EXISTS (SELECT 1 FROM evolution.cellular_mutation_action_publications p + WHERE p.world_id = c.world_id AND p.commit_id <> c.commit_id + AND p.account_id = encode(substring(p_mutations FROM 64 * i + 1 FOR 16), 'hex')::uuid + AND p.action_id = evolution.manifest_uint(p_mutations, 64 * i + 16, 8)) THEN + RAISE EXCEPTION 'cellular mutation already published' USING ERRCODE = 'EVL07'; + END IF; + END LOOP; + -- The birth publisher takes the lease lock and rechecks an uncertain first attempt. + SELECT * INTO h FROM evolution.publish_cellular_birth_checkpoint( + p_manifest, p_decisions, p_actions, p_trace); + IF NOT FOUND THEN + RAISE EXCEPTION 'publication returned no head' USING ERRCODE = 'XX000'; + END IF; + IF evolution.cellular_mutation_publications_match(c.world_id, c.commit_id, p_mutations, + p_results) THEN + RETURN QUERY SELECT h.region_frame, h.head_sequence, h.head_commit, h.head_digest, + h.head_tick; + RETURN; + END IF; + SELECT x.commit_digest INTO digest FROM evolution.region_commits x + WHERE x.commit_id = c.commit_id; + FOR i IN 0 .. n - 1 LOOP + INSERT INTO evolution.cellular_mutation_action_publications(world_id, region_id, + account_id, action_id, commit_id, commit_digest, commit_sequence, result) + VALUES (c.world_id, c.region_id, + encode(substring(p_mutations FROM 64 * i + 1 FOR 16), 'hex')::uuid, + evolution.manifest_uint(p_mutations, 64 * i + 16, 8), c.commit_id, digest, + c.commit_sequence, get_byte(p_results, i)); + END LOOP; + RETURN QUERY SELECT h.region_frame, h.head_sequence, h.head_commit, h.head_digest, h.head_tick; +END $$; + +INSERT INTO evolution.schema_migrations(version,sha256) VALUES(59,:'migration_sha_59'); diff --git a/migrations/apply.sql b/migrations/apply.sql index a7a9fdd..39bf1a9 100644 --- a/migrations/apply.sql +++ b/migrations/apply.sql @@ -134,6 +134,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') SELECT :'history_v55' || ',56:' || :'migration_sha_56' AS history_v56 \gset SELECT :'history_v56' || ',57:' || :'migration_sha_57' AS history_v57 \gset SELECT :'history_v57' || ',58:' || :'migration_sha_58' AS history_v58 \gset + SELECT :'history_v58' || ',59:' || :'migration_sha_59' AS history_v59 \gset SELECT :'history' = :'history_v22' AS at_v22, :'history' = :'history_v23' AS at_v23, :'history' = :'history_v24' AS at_v24, @@ -170,8 +171,10 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') :'history' = :'history_v55' AS at_v55, :'history' = :'history_v56' AS at_v56, :'history' = :'history_v57' AS at_v57, - :'history' = :'history_v58' AS at_v58 \gset - \if :at_v58 + :'history' = :'history_v58' AS at_v58, + :'history' = :'history_v59' AS at_v59 \gset + \if :at_v59 + \elif :at_v58 \elif :at_v57 \elif :at_v56 \elif :at_v55 @@ -581,7 +584,8 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24, coalesce(max(version), 0) = 55 AS at_v55, coalesce(max(version), 0) = 56 AS at_v56, coalesce(max(version), 0) = 57 AS at_v57, - coalesce(max(version), 0) = 58 AS at_v58 + coalesce(max(version), 0) = 58 AS at_v58, + coalesce(max(version), 0) = 59 AS at_v59 FROM evolution.schema_migrations \gset \if :at_v24 \ir 0025_cellular_v2_bundle.sql @@ -808,6 +812,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24, \elif :at_v56 \elif :at_v57 \elif :at_v58 +\elif :at_v59 \else \echo 'Unknown or changed migration history after v43' DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$; @@ -872,6 +877,10 @@ SELECT coalesce(max(version), 0) = 57 AS needs_v58 FROM evolution.schema_migrati \if :needs_v58 \ir 0058_liquid_bundle.sql \endif +SELECT coalesce(max(version), 0) = 58 AS needs_v59 FROM evolution.schema_migrations \gset +\if :needs_v59 + \ir 0059_durable_mutation_queue.sql +\endif -- Effective-privilege audit on every run, including no-ops (ADR-0060). Attributes -- alone are insufficient: predefined roles such as pg_write_all_data, PUBLIC -- grants or SET-able memberships would bypass the fenced functions. Checks the diff --git a/src/persistence/cellular_checkpoint_store.cpp b/src/persistence/cellular_checkpoint_store.cpp index a89c5a6..e35325f 100644 --- a/src/persistence/cellular_checkpoint_store.cpp +++ b/src/persistence/cellular_checkpoint_store.cpp @@ -6,9 +6,12 @@ #include #include #include +#include #include #include +#include #include +#include namespace evo::persistence { namespace { @@ -64,9 +67,12 @@ mutation_results(const server::CellularLives& base, std::vector results; results.reserve(actions.size()); for (const auto& action : actions) { - const auto life = - std::ranges::find(base.lives(), action.account, &server::CellularOwnedLife::account); - if (life == base.lives().end() || life->identity.life != action.input.life_id) + // A life that ended before the batch (ADR-0121 amendment, schema 59) has no owner + // entry; one still present must be the account's. + const auto life = std::ranges::find( + base.lives(), action.input.life_id, + [](const server::CellularOwnedLife& value) { return value.identity.life; }); + if (life != base.lives().end() && life->account != action.account) return std::nullopt; const auto outcome = std::ranges::find_if(outcomes, [&](const auto& candidate) { return candidate.tick == action.tick && candidate.life_id == action.input.life_id; @@ -78,6 +84,23 @@ mutation_results(const server::CellularLives& base, return results; } +// The tick of the trace selection a bound choice names (its life, offer and operator). +std::optional applied_tick(const server::CellularBatch& trace, + const server::CellularMutationActionRecord& action) { + std::optional found; + for (std::size_t i = 0; i < trace.ticks.size(); ++i) + for (const auto& selection : trace.ticks[i].mutations) + if (selection.life_id == action.input.life_id && + selection.event_index == action.input.event_index && + selection.offer_tick == action.input.offer_tick && + selection.operator_id == action.input.operator_id) { + if (found) + return std::nullopt; + found = trace.header.begin_tick + i + 1; + } + return found; +} + std::size_t selection_count(const server::CellularBatch& trace) { std::size_t count{}; for (const auto& tick : trace.ticks) @@ -844,9 +867,34 @@ recover_cellular_loaded(Postgres& db, const FilesystemObjectStore& objects, Regi server::decode_cellular_mutation_input(row.account, row.action_id, row.input); if (!action) return fail_with(RecoveryError::history); + // The stored input names the earliest tick; the choice bound at its selection's. + const auto bound = applied_tick(*trace, *action); + if (!bound || *bound < action->tick) + return fail_with(RecoveryError::history); + action->tick = *bound; mutation_actions.push_back(*action); stored_results.push_back(row.result); } + // Results follow the block's canonical (tick, account, action) order. + std::vector order(mutation_actions.size()); + for (std::size_t i = 0; i < order.size(); ++i) + order[i] = i; + std::ranges::sort(order, [&](std::size_t left, std::size_t right) { + const auto& a = mutation_actions[left]; + const auto& b = mutation_actions[right]; + return std::tie(a.tick, a.account, a.action_id) < + std::tie(b.tick, b.account, b.action_id); + }); + { + std::vector sorted_actions; + std::vector sorted_results; + for (const auto i : order) { + sorted_actions.push_back(mutation_actions[i]); + sorted_results.push_back(stored_results[i]); + } + mutation_actions = std::move(sorted_actions); + stored_results = std::move(sorted_results); + } if (mutation_actions.size() != selection_count(*trace) || (!mutation_actions.empty() && (!server::encode_cellular_mutation_action_block(mutation_actions, manifest->begin_tick, diff --git a/src/server/cellular_region_host.cpp b/src/server/cellular_region_host.cpp index e06da0b..c1fc0cf 100644 --- a/src/server/cellular_region_host.cpp +++ b/src/server/cellular_region_host.cpp @@ -774,11 +774,11 @@ void CellularRegionHost::reload_mutations() { ++status_.store_failures; return; } - // Every reservation targets the tick after the head it was made at, and SQL refuses a - // checkpoint that skips one, so an unbound choice is always for the next tick. + // Schema 59: every unbound reservation waits in the queue; its tick is the earliest it + // may bind at, which is never past the head's next tick. queued_mutations_.clear(); for (const auto& reservation : *pending) - if (reservation.target_tick == committed_->head.end_tick + 1) + if (reservation.target_tick <= committed_->head.end_tick + 1) queued_mutations_.push_back(reservation); mutations_uncertain_ = false; } @@ -827,20 +827,6 @@ CellularRegionHost::select_mutation(const persistence::Uuid& account, std::uint6 return {not_ready, 0, {}}; if (population_mode() && !set_population_mode(false)) return {not_ready, 0, {}}; - if (hold_selections_ || in_flight_) { - // Already reserved: repeats keep their answer. Otherwise remember the latest choice - // per account (bounded like a tick's lives) until the head is current. - const auto queued = std::ranges::find(queued_mutations_, account, - &persistence::CellularMutationReservation::account); - if (queued == queued_mutations_.end()) { - const auto held = std::ranges::find(held_selections_, account, &HeldSelection::account); - if (held != held_selections_.end()) - *held = {account, generation, action_id, input}; - else if (held_selections_.size() < biology::max_cellular_tick_lives) - held_selections_.push_back({account, generation, action_id, input}); - return {not_ready, 0, {}}; - } - } const auto tick = committed_->head.end_tick + 1; const CellularMutationActionRecord record{account, action_id, tick, input}; const auto bytes = encode_cellular_mutation_input(record); @@ -902,13 +888,16 @@ CellularRegionHost::select_mutation(const persistence::Uuid& account, std::uint6 return {admitted, tick, {}}; } -void CellularRegionHost::admit_held_selections() { - if (hold_selections_ || in_flight_) - return; - // The clients repeat their choices; a refusal here is answered on that repeat. - for (const auto& held : std::exchange(held_selections_, {})) - static_cast( - select_mutation(held.account, held.generation, held.action_id, held.input)); +std::vector +CellularRegionHost::unbound_mutations() const { + std::vector unbound; + for (const auto& queued : queued_mutations_) + if (!in_flight_ || + std::ranges::none_of(in_flight_->mutation_actions, [&](const auto& bound) { + return bound.account == queued.account && bound.action_id == queued.action_id; + })) + unbound.push_back(queued); + return unbound; } std::expected diff --git a/src/server/lifecycle/cellular_batch.cpp b/src/server/lifecycle/cellular_batch.cpp index b3a117e..7de5854 100644 --- a/src/server/lifecycle/cellular_batch.cpp +++ b/src/server/lifecycle/cellular_batch.cpp @@ -606,11 +606,10 @@ match_cellular_mutation_actions_to_trace(const CellularLives& predecessor, 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}; + // A life that ended before this batch: its queued choice names it directly. 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 && + return (life == predecessor.lives().end() || 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; @@ -620,13 +619,29 @@ match_cellular_mutation_actions_to_trace(const CellularLives& predecessor, } } 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)) + if (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 named = + std::ranges::find(predecessor.lives(), action.input.life_id, + [](const CellularOwnedLife& value) { return value.identity.life; }); + if (named == predecessor.lives().end()) { + // ADR-0121 amendment (schema 59): the life ended before this batch; the owner + // records life_died, so only the selection itself must match. + const auto departed = std::ranges::lower_bound(mutations, action.input.life_id, {}, + &CellularMutationSelection::life_id); + if (departed == mutations.end() || departed->life_id != action.input.life_id || + departed->event_index != action.input.event_index || + departed->offer_tick != action.input.offer_tick || + departed->operator_id != action.input.operator_id) + return std::unexpected{CellularActionBlockError::mismatch}; + continue; + } + const auto life = + std::ranges::find(predecessor.lives(), action.account, &CellularOwnedLife::account); + if (life == predecessor.lives().end()) + return std::unexpected{CellularActionBlockError::mismatch}; const auto selection = std::ranges::lower_bound(mutations, life->identity.life, {}, &CellularMutationSelection::life_id); if (!life->mutation.offer) diff --git a/src/server/lifecycle/cellular_lives.cpp b/src/server/lifecycle/cellular_lives.cpp index 899178b..3ec4080 100644 --- a/src/server/lifecycle/cellular_lives.cpp +++ b/src/server/lifecycle/cellular_lives.cpp @@ -1912,8 +1912,14 @@ CellularLives::advance_tick_with_mutations(std::span i 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.event_index != selection.event_index || + // ADR-0121 amendment (schema 59): a queued choice can outlive its life; it binds + // with the life_died result and changes nothing. + if (life == state_->lives.end() || life->identity.life != selection.life_id) { + if (state_->archive.find(selection.successor_id)) + return std::unexpected{CellularLivesError::mutation}; + continue; + } + if (life->mutation.event_index != selection.event_index || life->mutation.offer_tick != selection.offer_tick) return std::unexpected{CellularLivesError::mutation}; if (!life->mutation.offer.has_value()) diff --git a/src/server/session/cellular_session_batch.cpp b/src/server/session/cellular_session_batch.cpp index dfa6314..46c769f 100644 --- a/src/server/session/cellular_session_batch.cpp +++ b/src/server/session/cellular_session_batch.cpp @@ -102,23 +102,29 @@ prepare_cellular_session_tick(const CellularLives& state, std::uint64_t tick, const auto event_index = little_endian(bytes, 16, 8); const auto offer_tick = little_endian(bytes, 24, 8); const auto operator_id = static_cast(little_endian(bytes, 32, 2)); + // ADR-0121 amendment (schema 59): the reservation's tick is the earliest the choice + // may bind at; the queue binds it at the first tick simulated after admission. if (reservation.account == persistence::Uuid{} || reservation.action_id == 0 || reservation.region != state.region() || reservation.generation == 0 || - reservation.successor_identity == 0 || target_tick != tick || - reservation.target_tick != tick || life_id == 0 || event_index == 0 || - offer_tick == 0 || offer_tick >= tick) + reservation.successor_identity == 0 || target_tick != reservation.target_tick || + reservation.target_tick > tick || life_id == 0 || event_index == 0 || offer_tick == 0 || + offer_tick >= reservation.target_tick) return std::unexpected{CellularSessionBatchError::mutation}; + // A life that ended while its choice waited binds the choice as life_died. const auto life = - std::ranges::find(state.lives(), reservation.account, &CellularOwnedLife::account); - if (life == state.lives().end() || life->identity.life != life_id || - life->mutation.event_index != event_index || life->mutation.offer_tick != offer_tick) - return std::unexpected{CellularSessionBatchError::mutation}; - const auto& offer = life->mutation.offer; - if (!offer || (operator_id != 0 && // Operator 0 declines the offer. - !std::ranges::any_of(offer->candidates, [&](const auto& candidate) { - return candidate.operator_id == operator_id; - }))) - return std::unexpected{CellularSessionBatchError::mutation}; + std::ranges::find(state.lives(), life_id, + [](const CellularOwnedLife& value) { return value.identity.life; }); + if (life != state.lives().end()) { + if (life->account != reservation.account || life->mutation.event_index != event_index || + life->mutation.offer_tick != offer_tick) + return std::unexpected{CellularSessionBatchError::mutation}; + const auto& offer = life->mutation.offer; + if (!offer || (operator_id != 0 && // Operator 0 declines the offer. + !std::ranges::any_of(offer->candidates, [&](const auto& candidate) { + return candidate.operator_id == operator_id; + }))) + return std::unexpected{CellularSessionBatchError::mutation}; + } if (std::ranges::any_of(prepared.intents.mutations, [&](const auto& previous) { diff --git a/tests/cellular_publication_tests.cpp b/tests/cellular_publication_tests.cpp index 4862e8d..591d5cc 100644 --- a/tests/cellular_publication_tests.cpp +++ b/tests/cellular_publication_tests.cpp @@ -607,16 +607,6 @@ void cellular_host_admits_mutation_choices() { ++stale.event_index; CHECK(host->select_mutation(account, control->generation, 31, stale).status == CellularMutationAdmission::invalid); - // While ticks past the head are being simulated, a choice is held, not reserved. - host->hold_mutation_selections(true); - CHECK(host->select_mutation(account, control->generation, 31, choice).status == - CellularMutationAdmission::not_ready && - host->selections_held() && host->queued_mutations().empty()); - host->admit_held_selections(); // Still held: nothing is reserved. - CHECK(host->selections_held() && host->queued_mutations().empty()); - host->hold_mutation_selections(false); - host->admit_held_selections(); - CHECK(!host->selections_held() && host->queued_mutations().size() == 1); const auto admitted = host->select_mutation(account, control->generation, 31, choice); CHECK(admitted.status == CellularMutationAdmission::admitted && admitted.tick == next); const auto repeated = host->select_mutation(account, control->generation, 31, choice); @@ -637,12 +627,15 @@ void cellular_host_admits_mutation_choices() { CHECK(host->step().ready && host->state()->tick() + 1 == next); CHECK(host->queued_mutations().size() == 1 && host->queued_mutations().front() == reservation); - // A checkpoint for that tick without the choice is refused; the queue survives it. + // ADR-0121 amendment (schema 59): the queue is durable. A checkpoint for that tick + // without the choice publishes, and the choice still waits; it binds at a later tick. CellularBatch skipped; skipped.header = header(*host, 1); skipped.ticks.push_back({{{life_id, {}}}, {}, {}}); - CHECK(!host->publish(skipped)); - CHECK(host->step().ready && host->queued_mutations().size() == 1); + CHECK(host->publish(skipped)); + CHECK(host->step().ready && host->queued_mutations().size() == 1 && + host->unbound_mutations().size() == 1); + const auto late = next + 1; CellularBatch chosen; chosen.header = header(*host, 1); @@ -650,17 +643,17 @@ void cellular_host_admits_mutation_choices() { {}, {{life_id, choice.event_index, choice.offer_tick, reservation.successor_identity, choice.operator_id}}}); - const std::array records{CellularMutationActionRecord{account, 31, next, choice}}; + const std::array records{CellularMutationActionRecord{account, 31, late, choice}}; const auto published = host->publish(chosen, {}, records); CHECK(published && published->mutation_results.size() == 1); CHECK(host->queued_mutations().empty()); const auto finalized = host->take_finalized_mutations(); CHECK(finalized.size() == 1 && finalized.front().account == account && - finalized.front().action_id == 31 && finalized.front().tick == next && + finalized.front().action_id == 31 && finalized.front().tick == late && finalized.front().result == published->mutation_results.front() && finalized.front().commit_sequence == published->committed.head.sequence); const auto outcome = action_outcome(finalized.front()); - CHECK(outcome.action_id == 31 && outcome.tick == next && + CHECK(outcome.action_id == 31 && outcome.tick == late && outcome.result == (finalized.front().result == CellularMutationResult::applied ? evo::network::ActionResult::committed : evo::network::ActionResult::committed_no_effect)); @@ -671,6 +664,18 @@ void cellular_host_admits_mutation_choices() { *final_repeat.outcome == finalized.front()); CHECK(admit_status(final_repeat.status) == evo::network::AdmitStatus::duplicate); + // Recovery rebinds the late choice to the tick its trace applied it at. + const auto bound_snapshot = host->state()->snapshot(); + host->drain(); + host.reset(); + auto recovering = FilesystemObjectStore::open(env("PGHOST"), key, max_cellular_lives_bytes); + CHECK(recovering); + auto recovered_made = CellularRegionHost::create(config(), connect, std::move(*recovering)); + CHECK(recovered_made); + host = std::move(*recovered_made); + CHECK(host->step().ready && host->state()->snapshot() == bound_snapshot && + host->queued_mutations().empty()); + // ADR-0120 amendment (schema v40): feed until the next offer, then decline it. The // decline reserves and publishes like a choice, with result 3; the offer is consumed // and the genome is unchanged. diff --git a/tests/cellular_session_batch_tests.cpp b/tests/cellular_session_batch_tests.cpp index 1c49c33..f5e4bda 100644 --- a/tests/cellular_session_batch_tests.cpp +++ b/tests/cellular_session_batch_tests.cpp @@ -277,6 +277,50 @@ void durable_mutation_reservations_bind_to_the_exact_offer() { CHECK(stale_builder->append(action_tick, baseline, {}, {}, stale_reservations).error() == CellularSessionBatchError::mutation); CHECK(stale_builder->candidate().snapshot() == (*revision_owner)->snapshot()); + + // ADR-0121 amendment (schema 59): the queue is durable. A choice binds at a tick after + // its reservation's, recording the tick it bound at; it may not bind before it. + auto late_builder = CellularSessionBatchBuilder::create(**revision_owner, head); + CHECK(late_builder); + auto early = reservation; + early.target_tick = action_tick + 1; + early.input[0] = static_cast(early.target_tick); + for (std::size_t i = 1; i < 8; ++i) + early.input[i] = static_cast(early.target_tick >> (i * 8U)); + const std::array early_reservations{early}; + CHECK(late_builder->append(action_tick, baseline, {}, {}, early_reservations).error() == + CellularSessionBatchError::mutation); + CHECK(late_builder->append(action_tick, baseline, {}, {}, {})); + CHECK(late_builder->append(action_tick + 1, baseline, {}, {}, reservations)); + CHECK(late_builder->mutation_actions().size() == 1 && + late_builder->mutation_actions().front().tick == action_tick + 1 && + late_builder->batch().ticks.back().mutations.size() == 1 && + match_cellular_mutation_actions_to_trace(**revision_owner, late_builder->batch(), + late_builder->mutation_actions())); + const auto late_replay = replay_cellular_batch(**revision_owner, late_builder->batch()); + CHECK(late_replay && (*late_replay)->snapshot() == late_builder->candidate().snapshot()); + + // A choice whose life has ended binds as life_died and changes nothing. + auto departed_builder = CellularSessionBatchBuilder::create(**revision_owner, head); + CHECK(departed_builder); + auto departed = reservation; + const auto gone = life_id + 1'000; + for (std::size_t i = 0; i < 8; ++i) + departed.input[8 + i] = static_cast(gone >> (i * 8U)); + const std::array departed_reservations{departed}; + CHECK(departed_builder->append(action_tick, baseline, {}, {}, departed_reservations)); + CHECK(departed_builder->mutation_actions().size() == 1 && + departed_builder->mutation_actions().front().input.life_id == gone && + match_cellular_mutation_actions_to_trace(**revision_owner, departed_builder->batch(), + departed_builder->mutation_actions())); + std::vector departed_outcomes; + const auto departed_replay = + replay_cellular_batch(**revision_owner, departed_builder->batch(), departed_outcomes); + CHECK(departed_replay && + (*departed_replay)->snapshot() == departed_builder->candidate().snapshot() && + departed_outcomes.size() == 1 && departed_outcomes.front().life_id == gone && + departed_outcomes.front().result == CellularMutationResult::life_died && + (*departed_replay)->lives()[0].mutation.offer.has_value()); } void builder_batches_reserved_birth_after_empty_tick() { diff --git a/tests/region_migration_tests.cpp b/tests/region_migration_tests.cpp index b3ef0b1..d847d45 100644 --- a/tests/region_migration_tests.cpp +++ b/tests/region_migration_tests.cpp @@ -224,6 +224,7 @@ void upgrade_across_migration_and_barrier() { " -v migration_sha_56=$(s 0056_cellular_environment_publication.sql)" " -v migration_sha_57=$(s 0057_photosynthesis_bundle.sql)" " -v migration_sha_58=$(s 0058_liquid_bundle.sql)" + " -v migration_sha_59=$(s 0059_durable_mutation_queue.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"); @@ -241,7 +242,7 @@ void upgrade_across_migration_and_barrier() { "tools/migrate-postgres.sh\" > /dev/null") == 0); CHECK(history(*owner_db) == "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24," "25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46," - "47,48,49,50,51,52,53,54,55,56,57,58"); + "47,48,49,50,51,52,53,54,55,56,57,58,59"); CHECK(scalar(*owner_db, "SELECT (c.condeferrable AND c.condeferred AND " "c.confrelid = 'evolution.region_commits'::regclass)::text " "FROM pg_catalog.pg_constraint c WHERE c.conrelid = " diff --git a/tools/ci/history-domain-check.sh b/tools/ci/history-domain-check.sh index 65d4586..a93b1f4 100644 --- a/tools/ci/history-domain-check.sh +++ b/tools/ci/history-domain-check.sh @@ -58,7 +58,7 @@ for invalid in NULL "'00000000-0000-0000-0000-000000000000'"; do done # Reconstruct the exact pre-context metadata schema in this disposable database. # Restore the resulting real legacy backup through both supported workflows. -# Undo schema 58's bundle-14 wrappers, schema 57's bundle-13 wrappers, schema 56's joint profile, schema 55's plan validators, schema 54's three +# Undo schema 59's queue publishers, schema 58's bundle-14 wrappers, schema 57's bundle-13 wrappers, schema 56's joint profile, schema 55's plan validators, schema 54's three # replacements and schema 53; the backup must contain the actual pre-context # function contracts and privileges. awk '/^CREATE FUNCTION evolution\.admit_population_interval\(/ {capture=1; sub(/^CREATE FUNCTION/,"CREATE OR REPLACE FUNCTION")} capture {print} capture && /^END \$\$;/ {capture=0}' \ @@ -68,6 +68,9 @@ awk '/^CREATE FUNCTION evolution\.(check_cellular_migration_trace|prepare_cellul "$root/migrations/0049_cellular_migration_publication.sql" | sql > /dev/null awk '/^CREATE OR REPLACE FUNCTION evolution\.(cellular_mutation_selections|check_cellular_mutation_block)\(/ {capture=1} capture {print} capture && /^END \$\$;/ {capture=0}' \ "$root/migrations/0040_cellular_mutation_decline.sql" | sql > /dev/null +# Schema 59 removed the exact-tick refusals; restore schema 35's two publishers. +awk '/^CREATE FUNCTION evolution\.(publish_cellular_checkpoint|publish_cellular_mutation_checkpoint)\(/ {capture=1; sub(/^CREATE FUNCTION/,"CREATE OR REPLACE FUNCTION")} capture {print} capture && /^END \$\$;/ {capture=0}' \ + "$root/migrations/0035_cellular_mutation_publication.sql" | sql > /dev/null # Schema 58 widened the canonical action input; restore schema 36's validator. awk '/^CREATE OR REPLACE FUNCTION evolution\.valid_cellular_action_input\(/ {capture=1} capture {print} capture && /^END \$\$;/ {capture=0}' \ "$root/migrations/0036_cellular_v5_budding_bundle.sql" | sql > /dev/null diff --git a/tools/ci/postgres-check.sh b/tools/ci/postgres-check.sh index 3bd834e..1d2cb83 100644 --- a/tools/ci/postgres-check.sh +++ b/tools/ci/postgres-check.sh @@ -161,7 +161,7 @@ PGDATABASE=upgrade_path psql -X -v ON_ERROR_STOP=1 -1 -v "migration_sha=$v1_sha" -f "$root/migrations/0001_region_bootstrap.sql" > /dev/null test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',') FROM evolution.schema_migrations")" = 1 PGDATABASE=upgrade_path bash "$root/tools/migrate-postgres.sh" -test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58 +test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59 # 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%% *}" @@ -182,7 +182,7 @@ PGDATABASE=v3_commits psql -X -v ON_ERROR_STOP=1 \ -c "INSERT INTO evolution.retained_builds VALUES (decode(repeat('b1', 32), 'hex'))" \ -c "SELECT evolution.provision_region_checkpoint(decode('4145564f52434d31' || '01000000' || '01000000' || '0100000000000000' || '0100000000000000' || '0900000000000000' || repeat('11', 16) || repeat('00', 8) || repeat('00', 16) || repeat('00', 32) || repeat('00', 16) || repeat('00', 16) || repeat('00', 8) || '50c3000000000000' || '020000000200000001000000010000000100000001000000010000000100000001000000' || repeat('b1', 32) || '00000000' || '00000000' || '01000000' || '6400000000000000' || repeat('a0', 32), 'hex'))" > /dev/null PGDATABASE=v3_commits bash "$root/tools/migrate-postgres.sh" -test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 58:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58 +test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 59:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59 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. @@ -236,7 +236,7 @@ PGDATABASE=v6_populated psql -X -v ON_ERROR_STOP=1 \ -c "INSERT INTO evolution.retained_builds VALUES (decode(repeat('b1', 32), 'hex'))" \ -c "SELECT evolution.provision_region_checkpoint(decode('4145564f52434d31' || '02000000' || '01000000' || '0100000000000000' || '0100000000000000' || '0900000000000000' || repeat('11', 16) || repeat('00', 8) || repeat('00', 16) || repeat('00', 32) || repeat('00', 16) || repeat('00', 16) || repeat('00', 8) || '50c3000000000000' || '020000000200000001000000010000000100000001000000010000000100000001000000' || repeat('b1', 32) || '00000000' || '00000000' || '01000000' || '6400000000000000' || repeat('a0', 32) || repeat('00', 80), 'hex'))" > /dev/null PGDATABASE=v6_populated bash "$root/tools/migrate-postgres.sh" -test "$(PGDATABASE=v6_populated psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58 +test "$(PGDATABASE=v6_populated psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59 test "$(PGDATABASE=v6_populated psql -XAtc "SELECT count(*) FROM evolution.region_commits c, LATERAL evolution.parse_checkpoint_manifest(c.manifest) m WHERE m.commit_sequence = 0 AND m.predecessor_commit IS NULL")" = 1 test "$(PGDATABASE=v6_populated psql -XAtc "SELECT string_agg(conname, ',') FROM pg_constraint WHERE conrelid = 'evolution.region_heads'::regclass AND contype = 'c' AND pg_get_constraintdef(oid) LIKE '%end_tick%'")" = region_heads_published_tick phase permission-audit @@ -294,9 +294,9 @@ expect_audit_failure 'REVOKE EXECUTE ON FUNCTION evolution.load_world_cellular_m expect_audit_failure 'GRANT EXECUTE ON FUNCTION evolution.fork_restored_history_v1(uuid) TO evo_runtime_test' 'REVOKE EXECUTE ON FUNCTION evolution.fork_restored_history_v1(uuid) FROM evo_runtime_test' phase checksum-drift # Unknown future versions and checksum drift must not be repaired silently. -psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=59 WHERE version=58' +psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=60 WHERE version=59' 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=58 WHERE version=59" +psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=59 WHERE version=60" 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 2605812..45d74e8 100644 --- a/tools/migrate-postgres.sh +++ b/tools/migrate-postgres.sh @@ -128,6 +128,8 @@ digest_57="$(sha256sum "$root/migrations/0057_photosynthesis_bundle.sql")" digest_57="${digest_57%% *}" digest_58="$(sha256sum "$root/migrations/0058_liquid_bundle.sql")" digest_58="${digest_58%% *}" +digest_59="$(sha256sum "$root/migrations/0059_durable_mutation_queue.sql")" +digest_59="${digest_59%% *}" 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" \ @@ -175,6 +177,6 @@ psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \ --set="migration_sha_52=$digest_52" \ --set="migration_sha_53=$digest_53" --set="migration_sha_54=$digest_54" \ --set="migration_sha_55=$digest_55" --set="migration_sha_56=$digest_56" \ - --set="migration_sha_57=$digest_57" --set="migration_sha_58=$digest_58" \ + --set="migration_sha_57=$digest_57" --set="migration_sha_58=$digest_58" --set="migration_sha_59=$digest_59" \ --set="runtime_role=$runtime_role" \ --file="$root/migrations/apply.sql"