diff --git a/migrations/0031_fix_cellular_birth_publication_record.sql b/migrations/0031_fix_cellular_birth_publication_record.sql new file mode 100644 index 0000000..72134ad --- /dev/null +++ b/migrations/0031_fix_cellular_birth_publication_record.sql @@ -0,0 +1,60 @@ +-- PL/pgSQL's local record named `b` shadowed the birth-query aliases below. +-- Renaming the loop record lets the duplicate checks resolve their query alias. +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 $$ +DECLARE + c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(p_manifest); + birth_row record; + r evolution.life_reservations%ROWTYPE; +BEGIN + IF c.profile <> 4 OR c.commit_sequence = 0 OR p_actions IS NULL THEN + RAISE EXCEPTION 'invalid cellular publication' USING ERRCODE = '22023'; + END IF; + PERFORM evolution.check_cellular_action_block(p_actions, c.begin_tick, c.end_tick); + PERFORM * FROM evolution.cellular_births(p_trace, p_manifest); + IF EXISTS (SELECT b.account_id FROM evolution.cellular_births(p_trace, p_manifest) b + GROUP BY b.account_id HAVING count(*) > 1) + OR EXISTS (SELECT b.identity FROM evolution.cellular_births(p_trace, p_manifest) b + GROUP BY b.identity HAVING count(*) > 1) THEN + RAISE EXCEPTION 'duplicate cellular birth identity' USING ERRCODE = '22023'; + END IF; + PERFORM evolution.check_region_lease(c.world_id, c.region_id, c.holder, c.epoch); + -- The lease lock fences competing commits. Exact retries are accepted only + -- when the immutable binding rows agree with every birth in the trace. + IF EXISTS (SELECT 1 FROM evolution.region_commits x WHERE x.commit_id = c.commit_id) THEN + IF NOT evolution.cellular_birth_bindings_match(c.world_id, c.commit_id, p_trace, p_manifest) THEN + RAISE EXCEPTION 'cellular birth evidence differs on retry' USING ERRCODE = 'EVL07'; + END IF; + RETURN QUERY SELECT * FROM evolution.publish_cellular_checkpoint( + p_manifest, p_decisions, p_actions); + RETURN; + END IF; + -- Lock accounts in stable order and require the exact current control generation + -- and previously allocated request identity before inserting any binding. + FOR birth_row IN SELECT * FROM evolution.cellular_births(p_trace, p_manifest) ORDER BY account_id LOOP + PERFORM evolution.check_life_control(c.world_id, c.region_id, c.holder, c.epoch, + birth_row.account_id, birth_row.generation); + SELECT * INTO r FROM evolution.life_reservations x WHERE x.world_id = c.world_id + AND x.account_id = birth_row.account_id AND x.request_id = birth_row.request_id FOR UPDATE; + IF NOT FOUND OR r.region_id <> c.region_id OR r.identity <> birth_row.identity + OR r.rejection <> 0 OR r.rules <> 1 + OR EXISTS (SELECT 1 FROM evolution.primitive_life_bindings x + WHERE x.world_id = c.world_id AND x.account_id = birth_row.account_id) THEN + RAISE EXCEPTION 'cellular birth reservation conflicts' USING ERRCODE = 'EVL16'; + END IF; + END LOOP; + FOR birth_row IN SELECT * FROM evolution.cellular_births(p_trace, p_manifest) ORDER BY account_id LOOP + PERFORM evolution.check_life_control(c.world_id, c.region_id, c.holder, c.epoch, + birth_row.account_id, birth_row.generation); + END LOOP; + INSERT INTO evolution.primitive_life_bindings(world_id, account_id, request_id, born, commit_id) + SELECT c.world_id, b.account_id, b.request_id, b.born, c.commit_id + FROM evolution.cellular_births(p_trace, p_manifest) b; + RETURN QUERY SELECT * FROM evolution.publish_cellular_checkpoint( + p_manifest, p_decisions, p_actions); +END $$; + +INSERT INTO evolution.schema_migrations(version, sha256) VALUES (31, :'migration_sha_31'); diff --git a/migrations/apply.sql b/migrations/apply.sql index c43646b..29b2e5d 100644 --- a/migrations/apply.sql +++ b/migrations/apply.sql @@ -104,7 +104,10 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') SELECT right(:'history', length(',28:' || :'migration_sha_28')) = ',28:' || :'migration_sha_28' AS at_v28 \gset SELECT right(:'history', length(',29:' || :'migration_sha_29')) = ',29:' || :'migration_sha_29' AS at_v29 \gset SELECT right(:'history', length(',30:' || :'migration_sha_30')) = ',30:' || :'migration_sha_30' AS at_v30 \gset - \if :at_v30 + SELECT right(:'history', length(',31:' || :'migration_sha_31')) = ',31:' || :'migration_sha_31' AS at_v31 \gset + \if :at_v31 + \elif :at_v30 + \ir 0031_fix_cellular_birth_publication_record.sql \elif :at_v29 \elif :at_v28 \ir 0029_cellular_mutation_reservations.sql @@ -456,7 +459,8 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24, coalesce(max(version), 0) = 27 AS at_v27, coalesce(max(version), 0) = 28 AS at_v28, coalesce(max(version), 0) = 29 AS at_v29, - coalesce(max(version), 0) = 30 AS at_v30 + coalesce(max(version), 0) = 30 AS at_v30, + coalesce(max(version), 0) = 31 AS at_v31 FROM evolution.schema_migrations \gset \if :at_v24 \ir 0025_cellular_v2_bundle.sql @@ -465,29 +469,37 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24, \ir 0028_cellular_v3_trace_bounds.sql \ir 0029_cellular_mutation_reservations.sql \ir 0030_cellular_birth_publication_binding.sql + \ir 0031_fix_cellular_birth_publication_record.sql \elif :at_v25 \ir 0026_cellular_profile_migration.sql \ir 0027_cellular_v3_migration.sql \ir 0028_cellular_v3_trace_bounds.sql \ir 0029_cellular_mutation_reservations.sql \ir 0030_cellular_birth_publication_binding.sql + \ir 0031_fix_cellular_birth_publication_record.sql \elif :at_v26 \ir 0027_cellular_v3_migration.sql \ir 0028_cellular_v3_trace_bounds.sql \ir 0029_cellular_mutation_reservations.sql \ir 0030_cellular_birth_publication_binding.sql + \ir 0031_fix_cellular_birth_publication_record.sql \elif :at_v27 \ir 0028_cellular_v3_trace_bounds.sql \ir 0029_cellular_mutation_reservations.sql \ir 0030_cellular_birth_publication_binding.sql + \ir 0031_fix_cellular_birth_publication_record.sql \elif :at_v28 \ir 0029_cellular_mutation_reservations.sql \ir 0030_cellular_birth_publication_binding.sql + \ir 0031_fix_cellular_birth_publication_record.sql \elif :at_v29 \ir 0030_cellular_birth_publication_binding.sql + \ir 0031_fix_cellular_birth_publication_record.sql \elif :at_v30 + \ir 0031_fix_cellular_birth_publication_record.sql +\elif :at_v31 \else - \echo 'Unknown or changed migration history after v30' + \echo 'Unknown or changed migration history after v31' DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$; \endif -- Effective-privilege audit on every run, including no-ops (ADR-0060). Attributes diff --git a/tools/ci/postgres-check.sh b/tools/ci/postgres-check.sh index 000c46a..585e3ed 100644 --- a/tools/ci/postgres-check.sh +++ b/tools/ci/postgres-check.sh @@ -73,7 +73,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 +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 # 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%% *}" @@ -94,7 +94,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")" = 29: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 +test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 31: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 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. @@ -148,7 +148,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 +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 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. @@ -197,9 +197,9 @@ expect_audit_failure 'GRANT SELECT ON evolution.account_controls TO evo_runtime_ expect_audit_failure 'REVOKE EXECUTE ON FUNCTION evolution.claim_account_control(evolution.u64, evolution.u64, uuid, evolution.u64, uuid, evolution.u64, evolution.u64, integer) FROM evo_runtime_test' 'GRANT EXECUTE ON FUNCTION evolution.claim_account_control(evolution.u64, evolution.u64, uuid, evolution.u64, uuid, evolution.u64, evolution.u64, integer) TO evo_runtime_test' bash "$root/tools/migrate-postgres.sh" # Restored configuration passes the audit again. # Unknown future versions and checksum drift must not be repaired silently. -psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=30 WHERE version=29' +psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=32 WHERE version=31' 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=29 WHERE version=30" +psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=31 WHERE version=32" 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 5d99f7b..9d1975d 100644 --- a/tools/migrate-postgres.sh +++ b/tools/migrate-postgres.sh @@ -72,6 +72,8 @@ digest_29="$(sha256sum "$root/migrations/0029_cellular_mutation_reservations.sql digest_29="${digest_29%% *}" digest_30="$(sha256sum "$root/migrations/0030_cellular_birth_publication_binding.sql")" digest_30="${digest_30%% *}" +digest_31="$(sha256sum "$root/migrations/0031_fix_cellular_birth_publication_record.sql")" +digest_31="${digest_31%% *}" 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" \ @@ -95,5 +97,6 @@ psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \ --set="migration_sha_28=$digest_28" \ --set="migration_sha_29=$digest_29" \ --set="migration_sha_30=$digest_30" \ + --set="migration_sha_31=$digest_31" \ --set="runtime_role=$runtime_role" \ --file="$root/migrations/apply.sql"