From 08519b66af6588392b3d4965a279dad44c76b224 Mon Sep 17 00:00:00 2001 From: dingo Date: Sat, 10 Oct 2026 23:05:10 -0400 Subject: [PATCH] Add cellular durable action record lookup --- docs/engineering/M11_3_DELIVERY_PLAN.md | 5 + docs/engineering/VALIDATION.md | 17 ++++ .../evo/persistence/region_lease_store.hpp | 15 +++ include/evo/server/postgres_action_ledger.hpp | 6 ++ migrations/0019_cellular_action_records.sql | 55 +++++++++++ migrations/apply.sql | 26 ++++- src/persistence/region_lease_store.cpp | 99 +++++++++++++++++++ src/server/session/postgres_action_ledger.cpp | 91 +++++++++++++++++ tests/region_action_tests.cpp | 4 + tests/region_migration_tests.cpp | 16 ++- tools/ci/postgres-check.sh | 11 ++- tools/ci/upgrade-check.sh | 2 +- tools/migrate-postgres.sh | 3 + 13 files changed, 341 insertions(+), 9 deletions(-) create mode 100644 migrations/0019_cellular_action_records.sql diff --git a/docs/engineering/M11_3_DELIVERY_PLAN.md b/docs/engineering/M11_3_DELIVERY_PLAN.md index 6dcf85f..6b9a839 100644 --- a/docs/engineering/M11_3_DELIVERY_PLAN.md +++ b/docs/engineering/M11_3_DELIVERY_PLAN.md @@ -196,6 +196,11 @@ scoped change before starting the next segment. bytes, which is insufficient to admit v7 retries and therefore fails closed. PostgreSQL overrides, exact-byte admission comparison and publication remain open. +- 11.3d-5e: migration 19 extends durable action rows with input/result fields, + adds a lease-fenced input-aware lookup, and preserves legacy probe records. + The C++ persistence and PostgresActionLedger adapters parse single and batched + records, rejecting malformed cellular evidence. CI/PostgreSQL execution is + pending; no cellular writer exists yet. ## Exit boundary diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 668bf90..133c162 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -6166,3 +6166,20 @@ implementation and network regression translation units, plus `git diff --check`. The combined network executable was not rebuilt or run locally; CI must run the network suite. The PostgreSQL implementation, exact-input matching and cellular publication remain pending. + +## 2026-10-10 — M11.3d-5e durable action record reads (CI pending) + +Migration 19 adds the optional canonical input and result to the existing +account/action ledger, constrains cellular inputs to the 24-byte canonical form, +and adds a lease-fenced record lookup. The migration chain, checksum plumbing, +runtime function audit and upgrade history now include v19. C++ persistence +parses and validates legacy and cellular record shapes, and PostgresActionLedger +has a batched input-aware query; migration integration asserts that old probe +rows remain result 1 with no cellular bytes. + +Executed locally: warning-as-error MSVC 19.44 compile of the persistence +adapter, PostgresActionLedger adapter and region-action regression translation +unit, plus `git diff --check`. PostgreSQL is unavailable on this Windows +environment, and the POSIX migration integration target cannot compile with +MSVC because `poll.h` is missing; per the agreed workflow CI must run the +migration and store tests. No publisher writes cellular rows yet. diff --git a/include/evo/persistence/region_lease_store.hpp b/include/evo/persistence/region_lease_store.hpp index 3405a4b..fa4bd4c 100644 --- a/include/evo/persistence/region_lease_store.hpp +++ b/include/evo/persistence/region_lease_store.hpp @@ -105,6 +105,17 @@ struct CommittedAction { [[nodiscard]] std::expected, LeaseStoreFailure> find_region_action(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account, std::uint64_t action_id, std::chrono::milliseconds timeout); +struct CommittedActionRecord { + CommittedAction action; + std::uint16_t kind{}; + std::uint8_t result{}; + std::optional> cellular_input; + friend bool operator==(const CommittedActionRecord&, const CommittedActionRecord&) = default; +}; +[[nodiscard]] std::expected, LeaseStoreFailure> +find_region_action_record(Postgres& db, RegionKey key, const LeaseGrant& lease, + const Uuid& account, std::uint64_t action_id, + std::chrono::milliseconds timeout); // The same for several (account, action) pairs in one statement, answered in order // (M10.9d, ADR-0097). Empty asks nothing. struct ActionRef { @@ -114,6 +125,10 @@ struct ActionRef { [[nodiscard]] std::expected>, LeaseStoreFailure> find_region_actions(Postgres& db, RegionKey key, const LeaseGrant& lease, std::span actions, std::chrono::milliseconds timeout); +[[nodiscard]] std::expected>, LeaseStoreFailure> +find_region_action_records(Postgres& db, RegionKey key, const LeaseGrant& lease, + std::span actions, + std::chrono::milliseconds timeout); struct CommittedDecision { std::uint64_t commit_sequence{}; diff --git a/include/evo/server/postgres_action_ledger.hpp b/include/evo/server/postgres_action_ledger.hpp index 1ef7ed3..4f84c81 100644 --- a/include/evo/server/postgres_action_ledger.hpp +++ b/include/evo/server/postgres_action_ledger.hpp @@ -20,6 +20,12 @@ class PostgresActionLedger final : public network::ActionLedger { : db_{&db}, key_{key}, lease_{std::move(lease)}, timeout_{timeout} {} [[nodiscard]] std::expected, network::ActionLedgerError> find(const network::AccountId& account, std::uint64_t action_id) override; + [[nodiscard]] std::expected, + network::ActionLedgerError> + find_record(const network::AccountId& account, std::uint64_t action_id) override; + [[nodiscard]] std::expected>, + network::ActionLedgerError> + find_all_records(std::span keys) override; [[nodiscard]] std::expected>, network::ActionLedgerError> find_all(std::span keys) override; // One statement. diff --git a/migrations/0019_cellular_action_records.sql b/migrations/0019_cellular_action_records.sql new file mode 100644 index 0000000..82dc327 --- /dev/null +++ b/migrations/0019_cellular_action_records.sql @@ -0,0 +1,55 @@ +-- M11.3d-5e, ADR-0112: persist canonical biological input and cellular result. +-- Existing kind-1 probe rows remain byte-for-byte identifiable by NULL input. +ALTER TABLE evolution.region_actions ADD COLUMN input_bytes bytea; +ALTER TABLE evolution.region_actions ADD COLUMN result smallint NOT NULL DEFAULT 1; +ALTER TABLE evolution.region_actions DROP CONSTRAINT region_actions_kind_check; +ALTER TABLE evolution.region_actions ADD CONSTRAINT region_actions_kind_check CHECK (kind IN (1, 2)); + +CREATE FUNCTION evolution.valid_cellular_action_input(p_input bytea, p_tick evolution.u64) +RETURNS boolean LANGUAGE plpgsql IMMUTABLE STRICT +SET search_path = pg_catalog, pg_temp AS $$ +BEGIN + IF octet_length(p_input) <> 24 THEN + RETURN false; + END IF; + IF evolution.manifest_uint(p_input, 0, 8) = 0 + OR evolution.manifest_uint(p_input, 0, 8) <> p_tick OR get_byte(p_input, 12) > 1 + OR substring(p_input FROM 14 FOR 3) <> '\x000000' THEN + RETURN false; + END IF; + IF get_byte(p_input, 12) = 0 AND substring(p_input FROM 17 FOR 8) <> '\x0000000000000000' THEN + RETURN false; + END IF; + RETURN true; +END $$; + +ALTER TABLE evolution.region_actions ADD CONSTRAINT region_actions_result_input_check CHECK ( + (kind = 1 AND result = 1 AND input_bytes IS NULL) OR + (kind = 2 AND result = 5 AND input_bytes IS NOT NULL AND + evolution.valid_cellular_action_input(input_bytes, tick)) +); + +CREATE FUNCTION evolution.find_region_action_record(p_world evolution.u64, + p_region evolution.u64, p_holder uuid, p_epoch evolution.u64, p_account uuid, + p_action evolution.u64) +RETURNS TABLE(action_region evolution.u64, action_tick evolution.u64, + action_commit evolution.u64, action_kind smallint, action_result smallint, + action_input bytea) +LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ +BEGIN + IF p_account IS NULL OR p_action IS NULL THEN + RAISE EXCEPTION 'invalid action request' USING ERRCODE = '22023'; + END IF; + PERFORM evolution.check_region_lease(p_world, p_region, p_holder, p_epoch); + RETURN QUERY SELECT a.region_id, a.tick, a.commit_sequence, a.kind, a.result, a.input_bytes + FROM evolution.region_actions a + WHERE a.world_id = p_world AND a.account_id = p_account AND a.action_id = p_action; +END $$; + +REVOKE ALL ON FUNCTION evolution.valid_cellular_action_input(bytea,evolution.u64), + evolution.find_region_action_record(evolution.u64,evolution.u64,uuid,evolution.u64,uuid, + evolution.u64) FROM PUBLIC; +GRANT EXECUTE ON FUNCTION + evolution.find_region_action_record(evolution.u64,evolution.u64,uuid,evolution.u64,uuid, + evolution.u64) TO :"runtime_role"; +INSERT INTO evolution.schema_migrations(version, sha256) VALUES (19, :'migration_sha_19'); diff --git a/migrations/apply.sql b/migrations/apply.sql index 2cfd50e..4225150 100644 --- a/migrations/apply.sql +++ b/migrations/apply.sql @@ -91,28 +91,36 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') || ',13:' || :'migration_sha_13' || ',14:' || :'migration_sha_14' || ',15:' || :'migration_sha_15' || ',16:' || :'migration_sha_16' AS at_v16, :'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2' || ',3:' || :'migration_sha_3' || ',4:' || :'migration_sha_4' || ',5:' || :'migration_sha_5' || ',6:' || :'migration_sha_6' || ',7:' || :'migration_sha_7' || ',8:' || :'migration_sha_8' || ',9:' || :'migration_sha_9' || ',10:' || :'migration_sha_10' || ',11:' || :'migration_sha_11' || ',12:' || :'migration_sha_12' || ',13:' || :'migration_sha_13' || ',14:' || :'migration_sha_14' || ',15:' || :'migration_sha_15' || ',16:' || :'migration_sha_16' || ',17:' || :'migration_sha_17' AS at_v17, - :'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2' || ',3:' || :'migration_sha_3' || ',4:' || :'migration_sha_4' || ',5:' || :'migration_sha_5' || ',6:' || :'migration_sha_6' || ',7:' || :'migration_sha_7' || ',8:' || :'migration_sha_8' || ',9:' || :'migration_sha_9' || ',10:' || :'migration_sha_10' || ',11:' || :'migration_sha_11' || ',12:' || :'migration_sha_12' || ',13:' || :'migration_sha_13' || ',14:' || :'migration_sha_14' || ',15:' || :'migration_sha_15' || ',16:' || :'migration_sha_16' || ',17:' || :'migration_sha_17' || ',18:' || :'migration_sha_18' AS at_v18 \gset - \if :at_v18 + :'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2' || ',3:' || :'migration_sha_3' || ',4:' || :'migration_sha_4' || ',5:' || :'migration_sha_5' || ',6:' || :'migration_sha_6' || ',7:' || :'migration_sha_7' || ',8:' || :'migration_sha_8' || ',9:' || :'migration_sha_9' || ',10:' || :'migration_sha_10' || ',11:' || :'migration_sha_11' || ',12:' || :'migration_sha_12' || ',13:' || :'migration_sha_13' || ',14:' || :'migration_sha_14' || ',15:' || :'migration_sha_15' || ',16:' || :'migration_sha_16' || ',17:' || :'migration_sha_17' || ',18:' || :'migration_sha_18' AS at_v18, + :'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2' || ',3:' || :'migration_sha_3' || ',4:' || :'migration_sha_4' || ',5:' || :'migration_sha_5' || ',6:' || :'migration_sha_6' || ',7:' || :'migration_sha_7' || ',8:' || :'migration_sha_8' || ',9:' || :'migration_sha_9' || ',10:' || :'migration_sha_10' || ',11:' || :'migration_sha_11' || ',12:' || :'migration_sha_12' || ',13:' || :'migration_sha_13' || ',14:' || :'migration_sha_14' || ',15:' || :'migration_sha_15' || ',16:' || :'migration_sha_16' || ',17:' || :'migration_sha_17' || ',18:' || :'migration_sha_18' || ',19:' || :'migration_sha_19' AS at_v19 \gset + \if :at_v19 + \elif :at_v18 + \ir 0019_cellular_action_records.sql \elif :at_v17 \ir 0018_cellular_profile_admission.sql + \ir 0019_cellular_action_records.sql \elif :at_v16 \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql + \ir 0019_cellular_action_records.sql \elif :at_v15 \ir 0016_primitive_publication.sql \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql + \ir 0019_cellular_action_records.sql \elif :at_v14 \ir 0015_life_reservations.sql \ir 0016_primitive_publication.sql \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql + \ir 0019_cellular_action_records.sql \elif :at_v13 \ir 0014_probe_overdue.sql \ir 0015_life_reservations.sql \ir 0016_primitive_publication.sql \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql + \ir 0019_cellular_action_records.sql \elif :at_v12 \ir 0013_probe_incoming.sql \ir 0014_probe_overdue.sql @@ -120,6 +128,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0016_primitive_publication.sql \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql + \ir 0019_cellular_action_records.sql \elif :at_v11 \ir 0012_probe_handoff.sql \ir 0013_probe_incoming.sql @@ -128,6 +137,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0016_primitive_publication.sql \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql + \ir 0019_cellular_action_records.sql \elif :at_v10 \ir 0011_region_actions.sql \ir 0012_probe_handoff.sql @@ -137,6 +147,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0016_primitive_publication.sql \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql + \ir 0019_cellular_action_records.sql \elif :at_v9 \ir 0010_account_control.sql \ir 0011_region_actions.sql @@ -147,6 +158,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0016_primitive_publication.sql \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql + \ir 0019_cellular_action_records.sql \elif :at_v8 \ir 0009_serialized_authority.sql \ir 0010_account_control.sql @@ -158,6 +170,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0016_primitive_publication.sql \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql + \ir 0019_cellular_action_records.sql \elif :at_v7 \ir 0008_upgrade_barrier.sql \ir 0009_serialized_authority.sql @@ -170,6 +183,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0016_primitive_publication.sql \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql + \ir 0019_cellular_action_records.sql \elif :at_v6 \ir 0007_region_clones.sql \ir 0008_upgrade_barrier.sql @@ -183,6 +197,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0016_primitive_publication.sql \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql + \ir 0019_cellular_action_records.sql \elif :at_v5 \ir 0006_commit_lookup.sql \ir 0007_region_clones.sql @@ -197,6 +212,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0016_primitive_publication.sql \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql + \ir 0019_cellular_action_records.sql \elif :at_v4 \ir pre_v5_guard.sql \ir 0005_batch_decisions.sql @@ -213,6 +229,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0016_primitive_publication.sql \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql + \ir 0019_cellular_action_records.sql \elif :at_v3 \ir 0004_lineage_profile.sql \ir pre_v5_guard.sql @@ -230,6 +247,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0016_primitive_publication.sql \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql + \ir 0019_cellular_action_records.sql \elif :at_v2 \ir 0003_region_checkpoints.sql \ir 0004_lineage_profile.sql @@ -248,6 +266,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0016_primitive_publication.sql \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql + \ir 0019_cellular_action_records.sql \elif :at_v1 \ir 0002_region_fencing.sql \ir 0003_region_checkpoints.sql @@ -267,6 +286,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0016_primitive_publication.sql \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql + \ir 0019_cellular_action_records.sql \else \echo 'Unknown or changed migration history' DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$; @@ -291,6 +311,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0016_primitive_publication.sql \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql + \ir 0019_cellular_action_records.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 @@ -334,6 +355,7 @@ BEGIN ('evolution.publish_region_checkpoint(bytea,bytea,bytea)'::regprocedure::oid), ('evolution.resolve_region_commit(evolution.u64,evolution.u64,uuid,bytea,bytea,bytea)'::regprocedure::oid), ('evolution.find_region_action(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64)'::regprocedure::oid), + ('evolution.find_region_action_record(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64)'::regprocedure::oid), ('evolution.open_probe(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,bigint,bigint,bigint,evolution.u64)'::regprocedure::oid), ('evolution.save_probe(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,bigint,bigint,bigint)'::regprocedure::oid), ('evolution.prepare_probe_transfer(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,bigint,bigint,bigint)'::regprocedure::oid), diff --git a/src/persistence/region_lease_store.cpp b/src/persistence/region_lease_store.cpp index 6b52676..ece734e 100644 --- a/src/persistence/region_lease_store.cpp +++ b/src/persistence/region_lease_store.cpp @@ -114,6 +114,43 @@ template std::optional parse_integer(const std::optional bytes, std::size_t at, std::size_t width) { + std::uint64_t value{}; + for (std::size_t i = 0; i < width; ++i) + value |= static_cast(bytes[at + i]) << (i * 8); + return value; +} +std::optional parse_action_record(const PgRows::value_type& row, + std::size_t at) { + if (row.size() < at + 6) + return std::nullopt; + const auto region = parse_integer(row[at]); + const auto tick = parse_integer(row[at + 1]); + const auto sequence = parse_integer(row[at + 2]); + const auto kind = parse_integer(row[at + 3]); + const auto result = parse_integer(row[at + 4]); + if (!region || !tick || !sequence || !kind || !result) + return std::nullopt; + std::optional> input; + if (row[at + 5]) { + input = unhex_bytes(*row[at + 5]); + if (!input) + return std::nullopt; + } + if (*kind == 1) { + if (*result != 1 || input) + return std::nullopt; + } else if (*kind == 2) { + if (*result != 5 || !input || input->size() != 24 || little(*input, 0, 8) != *tick || + (*input)[12] > 1 || (*input)[13] != 0 || (*input)[14] != 0 || (*input)[15] != 0 || + ((*input)[12] == 0 && little(*input, 16, 8) != 0)) + return std::nullopt; + } else { + return std::nullopt; + } + return CommittedActionRecord{CommittedAction{*region, *tick, *sequence}, *kind, *result, + std::move(input)}; +} std::string bytea(std::span b) { return "\\x" + hex(b); } @@ -354,6 +391,30 @@ find_region_action(Postgres& db, RegionKey key, const LeaseGrant& lease, const U return std::optional{CommittedAction{*region, *tick, *sequence}}; } +std::expected, LeaseStoreFailure> +find_region_action_record(Postgres& db, RegionKey key, const LeaseGrant& lease, + const Uuid& account, std::uint64_t action_id, milliseconds timeout) { + const std::array p{std::to_string(key.world), std::to_string(key.region), + uuid_text(lease.holder), std::to_string(lease.epoch), + uuid_text(account), std::to_string(action_id)}; + auto rows = db.query("SELECT action_region, action_tick, action_commit, action_kind, " + "action_result, encode(action_input, 'hex') " + "FROM evolution.find_region_action_record($1::evolution.u64, " + "$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid, " + "$6::evolution.u64)", + p, timeout); + if (!rows) + return std::unexpected{classify(rows.error())}; + if (rows->empty()) + return std::optional{}; + if (rows->size() != 1 || (*rows)[0].size() != 6) + return std::unexpected{malformed(false)}; + const auto record = parse_action_record((*rows)[0], 0); + if (!record) + return std::unexpected{malformed(false)}; + return std::optional{*record}; +} + std::expected>, LeaseStoreFailure> find_region_actions(Postgres& db, RegionKey key, const LeaseGrant& lease, std::span actions, milliseconds timeout) { @@ -398,6 +459,44 @@ find_region_actions(Postgres& db, RegionKey key, const LeaseGrant& lease, return out; } +std::expected>, LeaseStoreFailure> +find_region_action_records(Postgres& db, RegionKey key, const LeaseGrant& lease, + std::span actions, milliseconds timeout) { + std::vector> out(actions.size()); + if (actions.empty()) + return out; + std::string accounts = "{", ids = "{"; + for (std::size_t i = 0; i < actions.size(); ++i) { + accounts += (i == 0 ? "" : ",") + uuid_text(actions[i].account); + ids += (i == 0 ? "" : ",") + std::to_string(actions[i].action_id); + } + accounts += "}"; + ids += "}"; + const std::array p{std::to_string(key.world), std::to_string(key.region), + uuid_text(lease.holder), std::to_string(lease.epoch), + accounts, ids}; + auto rows = db.query("SELECT x.n, f.action_region, f.action_tick, f.action_commit, " + "f.action_kind, f.action_result, encode(f.action_input, 'hex') " + "FROM unnest($5::uuid[], $6::numeric[]) WITH ORDINALITY AS x(a, i, n) " + "CROSS JOIN LATERAL evolution.find_region_action_record(" + "$1::evolution.u64, $2::evolution.u64, $3::uuid, $4::evolution.u64, " + "x.a, x.i::evolution.u64) AS f", + p, timeout); + if (!rows) + return std::unexpected{classify(rows.error())}; + for (const auto& row : *rows) { + if (row.size() != 7) + return std::unexpected{malformed(false)}; + const auto ordinal = parse_integer(row[0]); + const auto record = parse_action_record(row, 1); + if (!ordinal || *ordinal == 0 || *ordinal > actions.size() || + out[*ordinal - 1].has_value() || !record) + return std::unexpected{malformed(false)}; + out[*ordinal - 1] = *record; + } + return out; +} + namespace { std::expected, LeaseStoreFailure> decisions_from(const PgRows& rows) { diff --git a/src/server/session/postgres_action_ledger.cpp b/src/server/session/postgres_action_ledger.cpp index 53002c7..e3b5003 100644 --- a/src/server/session/postgres_action_ledger.cpp +++ b/src/server/session/postgres_action_ledger.cpp @@ -1,5 +1,7 @@ #include "evo/server/postgres_action_ledger.hpp" +#include + namespace evo::server { std::expected, network::ActionLedgerError> PostgresActionLedger::find(const network::AccountId& account, std::uint64_t action_id) { @@ -26,6 +28,95 @@ PostgresActionLedger::find(const network::AccountId& account, std::uint64_t acti record.value().tick, record.value().commit_sequence}}; } +std::expected, network::ActionLedgerError> +PostgresActionLedger::find_record(const network::AccountId& account, std::uint64_t action_id) { + const auto lease = lease_(); + if (!lease) + return std::unexpected(network::ActionLedgerError::unavailable); + auto* db = db_->get(); + if (!db) + return std::unexpected(network::ActionLedgerError::unavailable); + const auto found = persistence::find_region_action_record( + *db, key_, *lease, account.bytes, action_id, timeout_); + if (!found.has_value()) { + const auto kind = found.error().kind; + if (kind == persistence::LeaseStoreError::store || + kind == persistence::LeaseStoreError::unknown) + db_->broken(); + return std::unexpected(network::ActionLedgerError::unavailable); + } + if (!found.value()) + return std::optional{}; + const auto& row = *found.value(); + const auto result = static_cast(row.result); + network::DurableActionRecord record{ + network::ActionOutcome{action_id, result, row.action.tick, row.action.commit_sequence}, + std::nullopt}; + if (row.cellular_input) { + const auto input = network::decode_cellular_action_input(*row.cellular_input); + if (!input || input->tick != row.action.tick || + result != network::ActionResult::committed) + return std::unexpected(network::ActionLedgerError::unavailable); + std::array canonical{}; + std::ranges::copy(*row.cellular_input, canonical.begin()); + record.cellular_input = canonical; + } else if (result != network::ActionResult::committed_no_effect) { + return std::unexpected(network::ActionLedgerError::unavailable); + } + return std::optional{std::move(record)}; +} + +std::expected>, + network::ActionLedgerError> +PostgresActionLedger::find_all_records(std::span keys) { + std::vector> out(keys.size()); + if (keys.empty()) + return out; + const auto lease = lease_(); + if (!lease) + return std::unexpected(network::ActionLedgerError::unavailable); + auto* db = db_->get(); + if (!db) + return std::unexpected(network::ActionLedgerError::unavailable); + std::vector refs; + refs.reserve(keys.size()); + for (const auto& key : keys) + refs.push_back({key.account.bytes, key.action_id}); + const auto found = persistence::find_region_action_records(*db, key_, *lease, refs, timeout_); + if (!found.has_value()) { + const auto kind = found.error().kind; + if (kind == persistence::LeaseStoreError::store || + kind == persistence::LeaseStoreError::unknown) + db_->broken(); + return std::unexpected(network::ActionLedgerError::unavailable); + } + if (found->size() != keys.size()) + return std::unexpected(network::ActionLedgerError::unavailable); + for (std::size_t i = 0; i < keys.size(); ++i) { + if (!(*found)[i]) + continue; + const auto& row = *(*found)[i]; + const auto result = static_cast(row.result); + network::DurableActionRecord record{ + network::ActionOutcome{keys[i].action_id, result, row.action.tick, + row.action.commit_sequence}, + std::nullopt}; + if (row.cellular_input) { + const auto input = network::decode_cellular_action_input(*row.cellular_input); + if (!input || input->tick != row.action.tick || + result != network::ActionResult::committed) + return std::unexpected(network::ActionLedgerError::unavailable); + std::array canonical{}; + std::ranges::copy(*row.cellular_input, canonical.begin()); + record.cellular_input = canonical; + } else if (result != network::ActionResult::committed_no_effect) { + return std::unexpected(network::ActionLedgerError::unavailable); + } + out[i] = std::move(record); + } + return out; +} + std::expected>, network::ActionLedgerError> PostgresActionLedger::find_all(std::span keys) { const auto lease = lease_(); diff --git a/tests/region_action_tests.cpp b/tests/region_action_tests.cpp index 0428e5b..a1b1fe9 100644 --- a/tests/region_action_tests.cpp +++ b/tests/region_action_tests.cpp @@ -160,6 +160,10 @@ void store_rules() { CHECK(batch.confirm(10)); auto found = find_region_action(*db, r.key, *lease, alice, 1, 5s); CHECK(found && *found == CommittedAction{131, 3, 1}); + const auto found_record = find_region_action_record(*db, r.key, *lease, alice, 1, 5s); + CHECK(found_record && *found_record == + CommittedActionRecord{CommittedAction{131, 3, 1}, 1, 1, + std::nullopt}); CHECK(holds(find_region_action(*db, r.key, *lease, bob, 1, 5s), std::optional{CommittedAction{131, 3, 1}})); CHECK(holds(find_region_action(*db, r.key, *lease, alice, 2, 5s), diff --git a/tests/region_migration_tests.cpp b/tests/region_migration_tests.cpp index 15fb660..b0aa08c 100644 --- a/tests/region_migration_tests.cpp +++ b/tests/region_migration_tests.cpp @@ -184,6 +184,7 @@ void upgrade_across_migration_and_barrier() { " -v migration_sha_16=$(s 0016_primitive_publication.sql)" " -v migration_sha_17=$(s 0017_life_request_outcomes.sql)" " -v migration_sha_18=$(s 0018_cellular_profile_admission.sql)" + " -v migration_sha_19=$(s 0019_cellular_action_records.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"); @@ -199,7 +200,20 @@ void upgrade_across_migration_and_barrier() { CHECK(shell("PGDATABASE=" + database + " EVO_PG_RUNTIME_ROLE=evo_runtime_test bash \"$EVO_SOURCE_ROOT/" "tools/migrate-postgres.sh\" > /dev/null") == 0); - CHECK(history(*owner_db) == "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18"); + CHECK(history(*owner_db) == "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19"); + CHECK(scalar(*owner_db, "SELECT count(*) FROM information_schema.columns WHERE " + "table_schema = 'evolution' AND table_name = 'region_actions' AND " + "column_name IN ('input_bytes', 'result')") == "2"); + CHECK(scalar(*owner_db, + "SELECT (to_regprocedure('evolution.find_region_action_record(" + "evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64)') " + "IS NOT NULL)::text") == "true"); + CHECK(scalar(*owner_db, + "SELECT evolution.valid_cellular_action_input(decode('01' || " + "repeat('00', 23), 'hex'), 1)::text") == "true"); + CHECK(scalar(*owner_db, + "SELECT evolution.valid_cellular_action_input(decode(repeat('00', 24), " + "'hex'), 1)::text") == "false"); CHECK(renew_region_lease(*runtime, r.key, *lease, 20000ms, 5s)); confirm(publish_next(20)); // 41..60, now v8. CHECK(head.head.end_tick == 60); diff --git a/tools/ci/postgres-check.sh b/tools/ci/postgres-check.sh index cfd7ffd..7efb5bd 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 +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 # 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")" = 18:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 +test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 19:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 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 +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 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. @@ -184,14 +184,15 @@ expect_audit_failure 'GRANT UPDATE ON evolution.probe_transfers TO evo_runtime_t expect_audit_failure 'GRANT SELECT ON evolution.region_actions TO evo_runtime_test' 'REVOKE SELECT ON evolution.region_actions FROM evo_runtime_test' expect_audit_failure 'GRANT SELECT ON evolution.region_commit_actions TO evo_runtime_test' 'REVOKE SELECT ON evolution.region_commit_actions FROM evo_runtime_test' expect_audit_failure 'GRANT EXECUTE ON FUNCTION evolution.check_action_block(bytea, numeric, numeric) TO evo_runtime_test' 'REVOKE EXECUTE ON FUNCTION evolution.check_action_block(bytea, numeric, numeric) FROM evo_runtime_test' +expect_audit_failure 'GRANT EXECUTE ON FUNCTION evolution.find_region_action_record(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64) TO evo_runtime_test' 'REVOKE EXECUTE ON FUNCTION evolution.find_region_action_record(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64) FROM evo_runtime_test' # Schema v10: account controls are reachable only through the three fenced functions. expect_audit_failure 'GRANT SELECT ON evolution.account_controls TO evo_runtime_test' 'REVOKE SELECT ON evolution.account_controls FROM evo_runtime_test' 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=18 WHERE version=17' +psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=20 WHERE version=19' 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=17 WHERE version=18" +psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=19 WHERE version=20" psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET sha256=repeat('0',64) WHERE version=14" if bash "$root/tools/migrate-postgres.sh"; then exit 1; fi psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET sha256=repeat('0',64) WHERE version=13" diff --git a/tools/ci/upgrade-check.sh b/tools/ci/upgrade-check.sh index c230c2f..7d9bcb6 100644 --- a/tools/ci/upgrade-check.sh +++ b/tools/ci/upgrade-check.sh @@ -61,7 +61,7 @@ SQL [ "$code" != 0 ] && grep -q 'division by zero' "$work/migration.log" || fail "the rehearsal migration did not fail" [ "$(q -c "SELECT count(*) FROM information_schema.columns WHERE table_name = 'region_commits' AND column_name = 'rehearsal_column'")" = 0 ] || fail "the failed migration left a column" -[ "$(q -c 'SELECT max(version) FROM evolution.schema_migrations')" = 17 ] || fail "schema version moved" +[ "$(q -c 'SELECT max(version) FROM evolution.schema_migrations')" = 19 ] || fail "schema version moved" wait_ack "$work/b1.log" $((before + 40)) || fail "b1 did not resume after the failed migration" kill -0 "$b1_pid" 2> /dev/null || fail "b1 exited during the failed migration" bash "$root/tools/migrate-postgres.sh" > /dev/null || fail "migrate after the failure" diff --git a/tools/migrate-postgres.sh b/tools/migrate-postgres.sh index ec4e194..a6905e6 100644 --- a/tools/migrate-postgres.sh +++ b/tools/migrate-postgres.sh @@ -48,6 +48,8 @@ digest_17="$(sha256sum "$root/migrations/0017_life_request_outcomes.sql")" digest_17="${digest_17%% *}" digest_18="$(sha256sum "$root/migrations/0018_cellular_profile_admission.sql")" digest_18="${digest_18%% *}" +digest_19="$(sha256sum "$root/migrations/0019_cellular_action_records.sql")" +digest_19="${digest_19%% *}" 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" \ @@ -59,5 +61,6 @@ psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \ --set="migration_sha_16=$digest_16" \ --set="migration_sha_17=$digest_17" \ --set="migration_sha_18=$digest_18" \ + --set="migration_sha_19=$digest_19" \ --set="runtime_role=$runtime_role" \ --file="$root/migrations/apply.sql"