diff --git a/docs/engineering/M11_2_DELIVERY_PLAN.md b/docs/engineering/M11_2_DELIVERY_PLAN.md index 9f599ba..a41468b 100644 --- a/docs/engineering/M11_2_DELIVERY_PLAN.md +++ b/docs/engineering/M11_2_DELIVERY_PLAN.md @@ -131,6 +131,12 @@ Local checks do not close ADR-0097's reference-worker qualification. The Linux/PostgreSQL process suite now provisions an isolated primitive region, creates a life over v5, restarts the host and checks the same durable result; execution remains pending CI. +- 11.2d-7a implementation: migration 24 and the typed persistence adapter add + a lease-fenced world/account lookup for the unique unrejected reservation. + It finds the owner before account control can move; pending unpublished + reservations route to their original region and a refused/cancelled-only + history has no active owner. PostgreSQL and migration CI execution remain + pending. - Cross-region owner routing and reconnect-to-existing-life remain unimplemented. [ADR-0116](../adr/0116-cellular-life-owner-routing.md) resolves the contract: resolve the durable world/account owner before claiming control, then reuse diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index de9c5df..af3bf77 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -6537,3 +6537,21 @@ completed with no source-list mismatch, but did not resolve the Ninja hang. No fresh client binary or local live cellular probe is claimed. The next gate is the maintainer's short session using [the probe checklist](../playtests/M11_3_CELLULAR_PROBE.md); 11.4 remains gated on its feedback. + +## 2026-10-11 — M11.2d-7a durable life-owner lookup (PostgreSQL pending) + +Migration 24 adds a `SECURITY DEFINER` owner query that verifies the caller's +current region lease, returns only the unrejected world/account reservation's +region, and grants runtime only function execution. The C++ adapter validates +missing, malformed and multiple-row results. PostgreSQL integration coverage +checks absent accounts, cross-region lookup, a pending reservation, cancellation +after takeover, invalid account IDs, and migration/runtime privileges. + +GCC 13.2 strict-warning syntax compilation passed for +`src/persistence/region_lease_store.cpp` and +`tests/life_reservation_store_tests.cpp`; `git diff --check` passed. The +`tests/region_migration_tests.cpp` translation unit cannot compile locally +because the Windows environment lacks POSIX `poll.h`. PostgreSQL/psql, CTest, +and CI have not run for this segment. The CI migration, privilege, and +PostgreSQL integration results remain required before closing the database +gate. diff --git a/include/evo/persistence/life_reservation_store.hpp b/include/evo/persistence/life_reservation_store.hpp index efd79a5..a5cc06e 100644 --- a/include/evo/persistence/life_reservation_store.hpp +++ b/include/evo/persistence/life_reservation_store.hpp @@ -69,4 +69,9 @@ load_primitive_lives(Postgres& db, RegionKey key, std::chrono::milliseconds time query_primitive_life(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account, std::uint64_t generation, std::uint64_t request_id, std::chrono::milliseconds timeout); +// Read-only, lease-fenced lookup used before claiming durable account control. +// Returns no value when the account has no active (non-refused) life reservation. +[[nodiscard]] std::expected, LifeReservationFailure> +primitive_life_owner(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account, + std::chrono::milliseconds timeout); } // namespace evo::persistence diff --git a/migrations/0024_primitive_life_owner_lookup.sql b/migrations/0024_primitive_life_owner_lookup.sql new file mode 100644 index 0000000..da33f8e --- /dev/null +++ b/migrations/0024_primitive_life_owner_lookup.sql @@ -0,0 +1,23 @@ +-- M11.2d-7a, ADR-0116. Resolve the durable world/account owner before a +-- region claims account control during login. The lease fences the caller; +-- runtime receives no direct access to life_reservations. +CREATE FUNCTION evolution.primitive_life_owner(p_world evolution.u64, + p_region evolution.u64, p_holder uuid, p_epoch evolution.u64, p_account uuid) +RETURNS TABLE (owner_region evolution.u64) +LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ +BEGIN + IF p_world IS NULL OR p_world = 0 OR p_region IS NULL OR p_region = 0 + OR p_holder IS NULL OR p_epoch IS NULL OR p_epoch = 0 + OR p_account IS NULL OR p_account = '00000000-0000-0000-0000-000000000000' THEN + RAISE EXCEPTION 'invalid life owner lookup' USING ERRCODE = '22023'; + END IF; + PERFORM evolution.check_region_lease(p_world, p_region, p_holder, p_epoch); + RETURN QUERY SELECT r.region_id FROM evolution.life_reservations r + WHERE r.world_id = p_world AND r.account_id = p_account AND r.rejection = 0; +END $$; + +REVOKE ALL ON FUNCTION evolution.primitive_life_owner( + evolution.u64,evolution.u64,uuid,evolution.u64,uuid) FROM PUBLIC; +GRANT EXECUTE ON FUNCTION evolution.primitive_life_owner( + evolution.u64,evolution.u64,uuid,evolution.u64,uuid) TO :"runtime_role"; +INSERT INTO evolution.schema_migrations(version,sha256) VALUES (24, :'migration_sha_24'); diff --git a/migrations/apply.sql b/migrations/apply.sql index 443a7d8..508db6b 100644 --- a/migrations/apply.sql +++ b/migrations/apply.sql @@ -97,27 +97,35 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') :'history' = '1:' || :'migration_sha' || ',2:' || :'migration_sha_2' || ',3:' || :'migration_sha_3' || ',4:' || :'migration_sha_4' || ',5:' || :'migration_sha_5' || ',6:' || :'migration_sha_6' || ',7:' || :'migration_sha_7' || ',8:' || :'migration_sha_8' || ',9:' || :'migration_sha_9' || ',10:' || :'migration_sha_10' || ',11:' || :'migration_sha_11' || ',12:' || :'migration_sha_12' || ',13:' || :'migration_sha_13' || ',14:' || :'migration_sha_14' || ',15:' || :'migration_sha_15' || ',16:' || :'migration_sha_16' || ',17:' || :'migration_sha_17' || ',18:' || :'migration_sha_18' || ',19:' || :'migration_sha_19' || ',20:' || :'migration_sha_20' || ',21:' || :'migration_sha_21' AS at_v21 \gset SELECT right(:'history', length(',22:' || :'migration_sha_22')) = ',22:' || :'migration_sha_22' AS at_v22 \gset SELECT right(:'history', length(',23:' || :'migration_sha_23')) = ',23:' || :'migration_sha_23' AS at_v23 \gset - \if :at_v23 + SELECT right(:'history', length(',24:' || :'migration_sha_24')) = ',24:' || :'migration_sha_24' AS at_v24 \gset + \if :at_v24 + \elif :at_v23 + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v22 \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v21 \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v20 \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v19 \ir 0020_cellular_publication.sql \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v18 \ir 0019_cellular_action_records.sql \ir 0020_cellular_publication.sql \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v17 \ir 0018_cellular_profile_admission.sql \ir 0019_cellular_action_records.sql @@ -125,6 +133,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v16 \ir 0017_life_request_outcomes.sql \ir 0018_cellular_profile_admission.sql @@ -133,6 +142,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v15 \ir 0016_primitive_publication.sql \ir 0017_life_request_outcomes.sql @@ -142,6 +152,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v14 \ir 0015_life_reservations.sql \ir 0016_primitive_publication.sql @@ -152,6 +163,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v13 \ir 0014_probe_overdue.sql \ir 0015_life_reservations.sql @@ -163,6 +175,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v12 \ir 0013_probe_incoming.sql \ir 0014_probe_overdue.sql @@ -175,6 +188,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v11 \ir 0012_probe_handoff.sql \ir 0013_probe_incoming.sql @@ -188,6 +202,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v10 \ir 0011_region_actions.sql \ir 0012_probe_handoff.sql @@ -202,6 +217,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v9 \ir 0010_account_control.sql \ir 0011_region_actions.sql @@ -217,6 +233,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v8 \ir 0009_serialized_authority.sql \ir 0010_account_control.sql @@ -233,6 +250,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v7 \ir 0008_upgrade_barrier.sql \ir 0009_serialized_authority.sql @@ -250,6 +268,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v6 \ir 0007_region_clones.sql \ir 0008_upgrade_barrier.sql @@ -268,6 +287,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v5 \ir 0006_commit_lookup.sql \ir 0007_region_clones.sql @@ -287,6 +307,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v4 \ir pre_v5_guard.sql \ir 0005_batch_decisions.sql @@ -308,6 +329,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v3 \ir 0004_lineage_profile.sql \ir pre_v5_guard.sql @@ -330,6 +352,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v2 \ir 0003_region_checkpoints.sql \ir 0004_lineage_profile.sql @@ -353,6 +376,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \elif :at_v1 \ir 0002_region_fencing.sql \ir 0003_region_checkpoints.sql @@ -377,6 +401,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.sql \else \echo 'Unknown or changed migration history' DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$; @@ -406,6 +431,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0021_cellular_trace_and_recovery.sql \ir 0022_cellular_birth_trace_bound.sql \ir 0023_cellular_birth_publication.sql + \ir 0024_primitive_life_owner_lookup.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 @@ -429,6 +455,7 @@ BEGIN ('evolution.publish_primitive_checkpoint(bytea,bytea)'::regprocedure::oid), ('evolution.load_primitive_lives(evolution.u64,evolution.u64)'::regprocedure::oid), ('evolution.query_primitive_life(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,evolution.u64)'::regprocedure::oid), + ('evolution.primitive_life_owner(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)'::regprocedure::oid), ('evolution.reserve_primitive_life(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,evolution.u64,integer)'::regprocedure::oid), ('evolution.reject_primitive_life(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,evolution.u64,smallint)'::regprocedure::oid), ('evolution.acquire_region_lease(evolution.u64,evolution.u64,integer)'::regprocedure::oid), diff --git a/schemas/life-reservations-v1.md b/schemas/life-reservations-v1.md index 3b50d51..38fa5fb 100644 --- a/schemas/life-reservations-v1.md +++ b/schemas/life-reservations-v1.md @@ -61,6 +61,13 @@ originating gameplay. `query_primitive_life` resolves the world/account/request record for the authenticated current controller, independent of reservation region, including final refusals and published commit identity/sequence. +Migration 24 adds `primitive_life_owner`, a lease-fenced read-only lookup used +before account control is claimed during login. It returns the unique region +for an unrejected reservation, including a pending unpublished request, or no +row when the account has no active reservation. Runtime cannot select the +reservation table directly. A lookup failure prevents login; callers must not +interpret it as absence. + Validation: `tools/ci/postgres-check.sh --life-reservations-only` starts a fresh local socket-only PostgreSQL test cluster. The full postgres-check also executes these tests, alongside migration, backup, restore and existing process checks. diff --git a/src/persistence/region_lease_store.cpp b/src/persistence/region_lease_store.cpp index f8863aa..b4dd8a6 100644 --- a/src/persistence/region_lease_store.cpp +++ b/src/persistence/region_lease_store.cpp @@ -346,7 +346,8 @@ publish_cellular_checkpoint(Postgres& db, std::span manifest bytea(trace)}; auto rows = db.query( "SELECT * FROM evolution.publish_cellular_checkpoint($1::bytea, $2::bytea, $3::bytea, " - "$4::bytea)", p, timeout); + "$4::bytea)", + p, timeout); if (!rows) return std::unexpected{classify(rows.error())}; auto head = parse_head(*rows); @@ -386,19 +387,21 @@ resolve_region_commit(Postgres& db, RegionKey key, const Uuid& commit, return ResolvedCommit{Resolution::committed, *head}; } -std::expected -resolve_cellular_region_commit(Postgres& db, RegionKey key, const Uuid& commit, - std::span manifest, - std::span decisions, - std::span actions, - std::span trace, milliseconds timeout) { - const std::array p{std::to_string(key.world), std::to_string(key.region), - uuid_text(commit), bytea(manifest), - bytea(decisions), bytea(actions), bytea(trace)}; - auto rows = db.query( - "SELECT * FROM evolution.resolve_cellular_region_commit($1::evolution.u64, " - "$2::evolution.u64, $3::uuid, $4::bytea, $5::bytea, $6::bytea, $7::bytea)", - p, timeout); +std::expected resolve_cellular_region_commit( + Postgres& db, RegionKey key, const Uuid& commit, std::span manifest, + std::span decisions, std::span actions, + std::span trace, milliseconds timeout) { + const std::array p{std::to_string(key.world), + std::to_string(key.region), + uuid_text(commit), + bytea(manifest), + bytea(decisions), + bytea(actions), + bytea(trace)}; + auto rows = + db.query("SELECT * FROM evolution.resolve_cellular_region_commit($1::evolution.u64, " + "$2::evolution.u64, $3::uuid, $4::bytea, $5::bytea, $6::bytea, $7::bytea)", + p, timeout); if (!rows) return std::unexpected{classify(rows.error())}; if (rows->size() != 1 || (*rows)[0].size() != 6 || !(*rows)[0][0]) @@ -422,11 +425,10 @@ load_cellular_publication_actions(Postgres& db, RegionKey key, const LeaseGrant& const std::array p{std::to_string(key.world), std::to_string(key.region), uuid_text(lease.holder), std::to_string(lease.epoch), uuid_text(commit)}; - auto rows = db.query( - "SELECT encode(actions, 'hex') " - "FROM evolution.load_cellular_publication_actions($1::evolution.u64, " - "$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid)", - p, timeout); + auto rows = db.query("SELECT encode(actions, 'hex') " + "FROM evolution.load_cellular_publication_actions($1::evolution.u64, " + "$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid)", + p, timeout); if (!rows) return std::unexpected{classify(rows.error())}; if (rows->empty()) @@ -445,11 +447,11 @@ load_cellular_publication_births(Postgres& db, RegionKey key, const LeaseGrant& const std::array p{std::to_string(key.world), std::to_string(key.region), uuid_text(lease.holder), std::to_string(lease.epoch), uuid_text(commit)}; - auto rows = db.query( - "SELECT account_id::text, request_id::text, identity::text, born::text, " - "commit_sequence::text FROM evolution.load_cellular_publication_births(" - "$1::evolution.u64, $2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid)", - p, timeout); + auto rows = + db.query("SELECT account_id::text, request_id::text, identity::text, born::text, " + "commit_sequence::text FROM evolution.load_cellular_publication_births(" + "$1::evolution.u64, $2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid)", + p, timeout); if (!rows) return std::unexpected{classify(rows.error())}; std::vector bindings; @@ -494,8 +496,8 @@ find_region_action(Postgres& db, RegionKey key, const LeaseGrant& lease, const U } std::expected, LeaseStoreFailure> -find_region_action_record(Postgres& db, RegionKey key, const LeaseGrant& lease, - const Uuid& account, std::uint64_t action_id, milliseconds timeout) { +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)}; @@ -574,9 +576,12 @@ find_region_action_records(Postgres& db, RegionKey key, const LeaseGrant& lease, } 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}; + 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) " @@ -927,6 +932,30 @@ query_primitive_life(Postgres& db, RegionKey key, const LeaseGrant& lease, const return std::optional{out}; } +std::expected, LifeReservationFailure> +primitive_life_owner(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account, + milliseconds timeout) { + if (key.world == 0 || key.region == 0 || lease.holder == Uuid{} || lease.epoch == 0 || + account == Uuid{}) + return std::unexpected{LifeReservationFailure{LifeReservationError::invalid, {}}}; + 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)}; + auto rows = db.query("SELECT * FROM evolution.primitive_life_owner($1::evolution.u64, " + "$2::evolution.u64, $3::uuid, $4::evolution.u64, $5::uuid)", + p, timeout); + if (!rows) + return std::unexpected{life_failure(rows.error())}; + if (rows->empty()) + return std::optional{}; + if (rows->size() != 1 || (*rows)[0].size() != 1) + return std::unexpected{LifeReservationFailure{LifeReservationError::store, {}}}; + const auto owner = parse_integer((*rows)[0][0]); + if (!owner || *owner == 0) + return std::unexpected{LifeReservationFailure{LifeReservationError::store, {}}}; + return std::optional{*owner}; +} + // Schema v12 probe records and handoff (ADR-0096). Shares this file's parsing rules. namespace { ProbeStoreFailure probe_failure(const PgFailure& f) { diff --git a/tests/life_reservation_store_tests.cpp b/tests/life_reservation_store_tests.cpp index d43c203..5d0d062 100644 --- a/tests/life_reservation_store_tests.cpp +++ b/tests/life_reservation_store_tests.cpp @@ -61,6 +61,12 @@ void run_life_reservation_store_tests() { }; const auto absent = query_primitive_life(*db, a, *lease_a, alice, control->generation, 77, 5s); CHECK(absent && !*absent); + Uuid bob{}; + bob.fill(0x62); + const auto no_owner = primitive_life_owner(*db, a, *lease_a, bob, 5s); + CHECK(no_owner && !*no_owner); + CHECK( + failed(primitive_life_owner(*db, a, *lease_a, Uuid{}, 5s), LifeReservationError::invalid)); CHECK(failed(reserve(request), LifeReservationError::not_available)); CHECK(admin->query("INSERT INTO evolution.life_allocators VALUES " "(9752,9223372036854775808)", @@ -72,6 +78,10 @@ void run_life_reservation_store_tests() { const auto first = reserve(request); CHECK(first && first->identity == 9223372036854775808ULL && first->region == 1 && first->refusal == LifeRefusal::none); + const auto owner_here = primitive_life_owner(*db, a, *lease_a, alice, 5s); + const auto owner_from_other_region = primitive_life_owner(*db, b, *lease_b, alice, 5s); + CHECK(owner_here && *owner_here == 1); + CHECK(owner_from_other_region && *owner_from_other_region == 1); db = connect_as(env("EVO_PG_RUNTIME_USER")); CHECK(reserve(request) == *first); const auto conflict = reserve(2); @@ -117,6 +127,8 @@ void run_life_reservation_store_tests() { LifeRefusal::no_placement); const auto pending = reserve(4); CHECK(pending && pending->refusal == LifeRefusal::none); + const auto pending_owner = primitive_life_owner(*db, a, *lease_a, alice, 5s); + CHECK(pending_owner && *pending_owner == 1); const auto moved = claim_account_control(*db, b, *lease_b, alice, 2, 2, 20000ms, 5s); CHECK(moved); auto moved_result = query_primitive_life(*db, b, *lease_b, alice, moved->generation, 4, 5s); @@ -128,6 +140,8 @@ void run_life_reservation_store_tests() { const auto cancelled_retry = reserve_primitive_life(*db, b, *lease_b, alice, moved->generation, 4, 1, 5s); CHECK(cancelled_retry && cancelled_retry->refusal == LifeRefusal::cancelled); + const auto cancelled_owner = primitive_life_owner(*db, b, *lease_b, alice, 5s); + CHECK(cancelled_owner && !*cancelled_owner); CHECK(admin->query("UPDATE evolution.life_allocators SET next_identity=18446744073709551615 " "WHERE world_id=9752", {}, 5s)); diff --git a/tests/region_migration_tests.cpp b/tests/region_migration_tests.cpp index a2594e0..191bba8 100644 --- a/tests/region_migration_tests.cpp +++ b/tests/region_migration_tests.cpp @@ -189,6 +189,7 @@ void upgrade_across_migration_and_barrier() { " -v migration_sha_21=$(s 0021_cellular_trace_and_recovery.sql)" " -v migration_sha_22=$(s 0022_cellular_birth_trace_bound.sql)" " -v migration_sha_23=$(s 0023_cellular_birth_publication.sql)" + " -v migration_sha_24=$(s 0024_primitive_life_owner_lookup.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"); @@ -204,32 +205,41 @@ void upgrade_across_migration_and_barrier() { CHECK(shell("PGDATABASE=" + database + " EVO_PG_RUNTIME_ROLE=evo_runtime_test bash \"$EVO_SOURCE_ROOT/" "tools/migrate-postgres.sh\" > /dev/null") == 0); - CHECK(history(*owner_db) == - "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23"); + 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"); + CHECK(scalar(*owner_db, + "SELECT (to_regprocedure('evolution.primitive_life_owner(" + "evolution.u64,evolution.u64,uuid,evolution.u64,uuid)') IS NOT NULL)::text") == + "true"); + CHECK(scalar(*owner_db, "SELECT has_function_privilege('evo_runtime_login', " + "'evolution.primitive_life_owner(evolution.u64,evolution.u64,uuid," + "evolution.u64,uuid)', 'EXECUTE')::text") == "true"); + CHECK(scalar(*owner_db, "SELECT has_table_privilege('evo_runtime_login', " + "'evolution.life_reservations', 'SELECT')::text") == "false"); 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(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(scalar(*owner_db, "SELECT evolution.check_cellular_action_block(decode(repeat('01', 16) || " "'0100000000000000' || '0100000000000000' || repeat('00', 16), 'hex'), " "0, 1)::text") == "1"); - CHECK(scalar(*owner_db, - "SELECT (to_regprocedure('evolution.publish_cellular_checkpoint(bytea,bytea,bytea,bytea)') " - "IS NOT NULL)::text") == "true"); - CHECK(scalar(*owner_db, - "SELECT has_function_privilege('evo_runtime_login', " - "'evolution.publish_cellular_checkpoint(bytea,bytea,bytea,bytea)', 'EXECUTE')::text") == - "true"); + CHECK( + scalar(*owner_db, + "SELECT " + "(to_regprocedure('evolution.publish_cellular_checkpoint(bytea,bytea,bytea,bytea)') " + "IS NOT NULL)::text") == "true"); + CHECK( + scalar( + *owner_db, + "SELECT has_function_privilege('evo_runtime_login', " + "'evolution.publish_cellular_checkpoint(bytea,bytea,bytea,bytea)', 'EXECUTE')::text") == + "true"); CHECK(scalar(*owner_db, "SELECT has_function_privilege('evo_runtime_login', " "'evolution.publish_cellular_checkpoint(bytea,bytea,bytea)', 'EXECUTE')::text") == @@ -242,9 +252,8 @@ void upgrade_across_migration_and_barrier() { "SELECT has_function_privilege('evo_runtime_login', " "'evolution.resolve_cellular_region_commit(evolution.u64,evolution.u64,uuid," "bytea,bytea,bytea,bytea)', 'EXECUTE')::text") == "true"); - CHECK(scalar(*owner_db, - "SELECT has_table_privilege('evo_runtime_login', " - "'evolution.cellular_publications', 'INSERT')::text") == "false"); + CHECK(scalar(*owner_db, "SELECT has_table_privilege('evo_runtime_login', " + "'evolution.cellular_publications', 'INSERT')::text") == "false"); CHECK(scalar(*owner_db, "SELECT has_function_privilege('evo_runtime_login', " "'evolution.publish_region_checkpoint_core(bytea,bytea)', 'EXECUTE')::text") == @@ -544,16 +553,15 @@ void concurrent_authority_and_classification() { " EVO_PG_RUNTIME_ROLE=evo_runtime_test bash \"$EVO_SOURCE_ROOT/" "tools/migrate-postgres.sh\" > /dev/null") == 0); auto owner_db = connect_to(database, env("PGUSER")); - auto cellular = genesis_manifest({7, 90}, 9, filled<16>(0x70), 50000, digest_of(0xC4), - 100, filled<32>(0xA9), - CheckpointProfile::cellular_lives_v1); + auto cellular = genesis_manifest({7, 90}, 9, filled<16>(0x70), 50000, digest_of(0xC4), 100, + filled<32>(0xA9), CheckpointProfile::cellular_lives_v1); CHECK(cellular); const auto cellular_bytes = encode_manifest(*cellular); CHECK(cellular_bytes); const std::array cellular_parameter{bytea_of(*cellular_bytes)}; - const auto parsed_cellular = owner_db->query( - "SELECT (evolution.parse_checkpoint_manifest($1::bytea)).profile::text", - cellular_parameter, 5s); + const auto parsed_cellular = + owner_db->query("SELECT (evolution.parse_checkpoint_manifest($1::bytea)).profile::text", + cellular_parameter, 5s); CHECK(parsed_cellular && parsed_cellular->size() == 1 && (*parsed_cellular)[0][0].value_or("") == "4"); auto wrong_cellular_versions = *cellular; diff --git a/tools/migrate-postgres.sh b/tools/migrate-postgres.sh index 1fb617a..b1622f2 100644 --- a/tools/migrate-postgres.sh +++ b/tools/migrate-postgres.sh @@ -58,6 +58,8 @@ digest_22="$(sha256sum "$root/migrations/0022_cellular_birth_trace_bound.sql")" digest_22="${digest_22%% *}" digest_23="$(sha256sum "$root/migrations/0023_cellular_birth_publication.sql")" digest_23="${digest_23%% *}" +digest_24="$(sha256sum "$root/migrations/0024_primitive_life_owner_lookup.sql")" +digest_24="${digest_24%% *}" 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" \ @@ -74,5 +76,6 @@ psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \ --set="migration_sha_21=$digest_21" \ --set="migration_sha_22=$digest_22" \ --set="migration_sha_23=$digest_23" \ + --set="migration_sha_24=$digest_24" \ --set="runtime_role=$runtime_role" \ --file="$root/migrations/apply.sql"