diff --git a/CMakeLists.txt b/CMakeLists.txt index c66b7d2..6da763c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -264,6 +264,7 @@ if(EVO_BUILD_POSTGRES) add_executable(evo-postgres-tests tests/postgres_tests.cpp tests/region_lease_store_tests.cpp tests/account_control_store_tests.cpp tests/probe_handoff_store_tests.cpp tests/life_reservation_store_tests.cpp + tests/primitive_publication_tests.cpp tests/region_action_tests.cpp tests/region_checkpoint_store_tests.cpp tests/region_host_tests.cpp tests/region_server_process_tests.cpp tests/region_recovery_tests.cpp diff --git a/docs/adr/0100-primitive-lives-and-persistent-control.md b/docs/adr/0100-primitive-lives-and-persistent-control.md index aec874d..a71a980 100644 --- a/docs/adr/0100-primitive-lives-and-persistent-control.md +++ b/docs/adr/0100-primitive-lives-and-persistent-control.md @@ -191,6 +191,26 @@ life bindings and inconsistent funding. No biological timers exist in this creation-only version; introducing progression requires a new format first. Durable publication and session integration remain 11.2c/d. +## 11.2c-2c: Guarded atomic publication + +Schema v16 admits primitive checkpoint profile 3 only through +`publish_primitive_checkpoint`. It validates the canonical batch trace and its +checkpoint scope, current lease and account-control generations, and each +reserved account/request/identity. It locks reservations in account UUID order, +then rechecks control after lock waits. A private immutable publication record, +life bindings and the existing fenced checkpoint/head publication commit in one +transaction. Generic profile-1/2 publishers cannot bypass this path; a matching +exact retry resolves to the committed receipt, while conflicting reuse, stale +control, changed bindings, malformed evidence or capacity overflow refuses. + +The owner-only genesis path admits an empty primitive-life checkpoint. Runtime +recovery can query immutable region life bindings, but object-store reconciliation +and a primitive-capable region host/session protocol are still required before a +region can serve this profile. This database gate therefore proves atomic durable +admission and retry behavior, not end-to-end recoverability or deployable gameplay. +Death, respawn, offline timers, biological tick integration and life transfer are +outside 11.2 and remain governed by later gates. + ## 11.2c-1: Durable identity reservations Schema v15 adds [life reservations](../../schemas/life-reservations-v1.md): diff --git a/docs/engineering/M11_2_DELIVERY_PLAN.md b/docs/engineering/M11_2_DELIVERY_PLAN.md index 355dcaf..038b4b3 100644 --- a/docs/engineering/M11_2_DELIVERY_PLAN.md +++ b/docs/engineering/M11_2_DELIVERY_PLAN.md @@ -77,7 +77,13 @@ Local checks do not close ADR-0097's reference-worker qualification. - 11.2c-2b: portable primitive checkpoint profile 3, history profile 2, complete payload closure and bounded birth traces. SQL admission remains disabled until guarded publication atomically binds bodies and reservations. -- 11.2c-2 and 11.2d remain: atomic funded checkpoint publication, reconciliation, - host/session integration and crash-boundary qualification. +- 11.2c-2c: schema v16 guarded primitive publication atomically validates control, + reservations and birth bindings with the checkpoint head. Exact retries + converge, conflicting evidence and stale control refuse, and runtime can read + immutable life bindings. Full PostgreSQL migrations, process, recovery, + backup/restore, host-loss, upgrade and privilege checks pass. +- Remaining 11.2c/d: C++ durable publication/object-store adapter, primitive-life + region recovery/host and explicit session protocol integration, plus crash-boundary + qualification. Schema admission alone does not make profile 3 hostable. No deployed world has been provisioned or changed by this work. diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index c87552e..82355b5 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -5601,3 +5601,22 @@ and both lifecycle/checkpoint tests passed. The revised encoder was rechecked wi ASan+UBSan (both tests), clang-tidy/format, and Windows Release 62/62. WSL temporary logs disappeared after process exit; the reproduced build log was retained under `build/m112-profile-callers.log` instead. + +## 2026-10-10 — M11.2c-2c schema v16 guarded publication + +Executed the GCC 14 Release `evo-postgres-tests` build and complete +`tools/ci/postgres-check.sh` against PostgreSQL 18.6. This includes the new +primitive publication integration (reservation/control checks, changed and exact +retry behavior, rollback and concurrent retries), every existing process and +recovery integration, schema upgrade/checksum/privilege audits, backup/restore, +host-loss and failed-upgrade rehearsals. The full command passed. A prior run +correctly reported the historical v3 fixture had 16 migration rows while the +script still expected 15; the expectation was updated and the complete run then +passed. The focused publication/concurrency lane also passed after the fixture +used commit IDs unique across this shared test database. + +No production world was provisioned or altered. This segment adds SQL profile +admission and a runtime binding read, but not the C++ object-store publication +adapter or primitive-capable server recovery/session path. Do not interpret its +green database gate as end-to-end life recovery. Full C++ unit/network/QUIC lanes +were not rerun because this segment changes no replicated network type. diff --git a/migrations/0016_primitive_publication.sql b/migrations/0016_primitive_publication.sql new file mode 100644 index 0000000..ef6d7fb --- /dev/null +++ b/migrations/0016_primitive_publication.sql @@ -0,0 +1,304 @@ +-- M11.2c-2c, ADR-0100: profile 3 admission and atomic birth bindings. +-- Existing profile 1/2 parser semantics are retained verbatim except branches +-- explicitly selecting profile 3. Runtime cannot publish that profile without +-- the private publication row checked by the commit trigger below. + +CREATE OR REPLACE FUNCTION evolution.parse_checkpoint_manifest(m bytea) +RETURNS evolution.checkpoint_manifest LANGUAGE plpgsql IMMUTABLE +SET search_path = pg_catalog, pg_temp AS $$ +DECLARE + r evolution.checkpoint_manifest; + zero16 constant bytea := '\x00000000000000000000000000000000'; + zero32 constant bytea := decode(repeat('00', 32), 'hex'); + history numeric; + cap numeric; +BEGIN + IF m IS NULL OR octet_length(m) < 12 OR substring(m FROM 1 FOR 8) <> '\x4145564f52434d31' THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + IF evolution.manifest_uint(m, 8, 4) <> 2 THEN + RAISE EXCEPTION 'unsupported checkpoint manifest' USING ERRCODE = 'EVL06'; + END IF; + IF octet_length(m) <> 360 THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + r.profile := evolution.manifest_uint(m, 12, 4); + history := evolution.manifest_uint(m, 232, 4); + IF NOT (((r.profile = 1 AND history = 0) OR (r.profile = 2 AND history = 1)) + AND substring(m FROM 161 FOR 36) = '\x020000000200000001000000010000000100000001000000010000000100000001000000' + OR (r.profile = 3 AND history = 2 + AND substring(m FROM 161 FOR 36) = '\x010000000100000001000000000000000000000001000000010000000100000001000000')) + OR evolution.manifest_uint(m, 228, 4) <> 0 OR evolution.manifest_uint(m, 236, 4) <> 1 THEN + RAISE EXCEPTION 'unsupported checkpoint manifest' USING ERRCODE = 'EVL06'; + END IF; + -- Payload caps: 1 MiB envelope; 72 + 1 MiB + lineage archive cap (3219496) container. + cap := CASE r.profile WHEN 1 THEN 1048576 WHEN 2 THEN 4268144 ELSE 3266800 END; + r.world_id := evolution.manifest_uint(m, 16, 8); + r.region_id := evolution.manifest_uint(m, 24, 8); + r.frame_id := evolution.manifest_uint(m, 32, 8); + r.commit_id := encode(substring(m FROM 41 FOR 16), 'hex')::uuid; + r.commit_sequence := evolution.manifest_uint(m, 56, 8); + r.predecessor_commit := encode(substring(m FROM 65 FOR 16), 'hex')::uuid; + r.predecessor_digest := substring(m FROM 81 FOR 32); + r.begin_tick := evolution.manifest_uint(m, 112, 8); + r.end_tick := evolution.manifest_uint(m, 120, 8); + r.holder := encode(substring(m FROM 129 FOR 16), 'hex')::uuid; + r.epoch := evolution.manifest_uint(m, 144, 8); + r.tick_microseconds := evolution.manifest_uint(m, 152, 8); + r.versions := substring(m FROM 161 FOR 36); + r.build_identity := substring(m FROM 197 FOR 32); + r.payload_bytes := evolution.manifest_uint(m, 240, 8); + r.payload_sha256 := substring(m FROM 249 FOR 32); + r.trace_bytes := evolution.manifest_uint(m, 280, 8); + r.trace_sha256 := substring(m FROM 289 FOR 32); + r.decision_count := evolution.manifest_uint(m, 320, 4); + r.decisions_sha256 := substring(m FROM 329 FOR 32); + IF r.world_id = 0 OR r.region_id = 0 OR r.frame_id = 0 + OR substring(m FROM 41 FOR 16) = zero16 OR r.tick_microseconds = 0 + OR r.build_identity = zero32 OR r.payload_bytes < 1 OR r.payload_bytes > cap + OR r.payload_sha256 = zero32 OR evolution.manifest_uint(m, 324, 4) <> 0 THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + IF r.profile = 3 AND (r.tick_microseconds <> 50000 OR r.decision_count <> 0 + OR r.decisions_sha256 <> zero32 OR (r.commit_sequence = 0 AND + (substring(m FROM 65 FOR 16) <> zero16 OR r.predecessor_digest <> zero32))) THEN + RAISE EXCEPTION 'invalid primitive checkpoint' USING ERRCODE = '22023'; + END IF; + IF r.commit_sequence = 0 THEN + IF substring(m FROM 129 FOR 16) <> zero16 OR r.epoch <> 0 OR r.begin_tick <> r.end_tick + OR r.trace_bytes <> 0 OR r.trace_sha256 <> zero32 THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + IF substring(m FROM 65 FOR 16) = zero16 AND r.predecessor_digest = zero32 THEN + -- Provisioned genesis: tick 0, no origin, no outcomes. + IF r.end_tick <> 0 OR r.decision_count <> 0 OR r.decisions_sha256 <> zero32 THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + r.predecessor_commit := NULL; + ELSIF substring(m FROM 65 FOR 16) = zero16 OR r.predecessor_digest = zero32 + OR r.predecessor_commit = r.commit_id OR r.decision_count > 8 + OR (r.decision_count = 0) <> (r.decisions_sha256 = zero32) THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + r.holder := NULL; + ELSIF substring(m FROM 65 FOR 16) = zero16 OR r.predecessor_commit = r.commit_id + OR r.predecessor_digest = zero32 OR substring(m FROM 129 FOR 16) = zero16 + OR r.epoch = 0 OR r.end_tick <= r.begin_tick OR r.end_tick - r.begin_tick > 64 + -- One 304-byte trace step per tick after a 96-byte header. + OR (r.profile <> 3 AND r.trace_bytes <> 96 + 304 * (r.end_tick - r.begin_tick)) + OR (r.profile = 3 AND (r.trace_bytes < 112 OR r.trace_bytes > 12400 + OR mod(r.trace_bytes - 112, 48) <> 0)) + OR r.trace_sha256 = zero32 + OR r.decision_count > 8 OR (r.decision_count = 0) <> (r.decisions_sha256 = zero32) THEN + RAISE EXCEPTION 'malformed checkpoint manifest' USING ERRCODE = '22023'; + END IF; + RETURN r; +END $$; + +CREATE TABLE evolution.primitive_publications ( + commit_id uuid PRIMARY KEY REFERENCES evolution.region_commits(commit_id) + DEFERRABLE INITIALLY DEFERRED, + trace bytea NOT NULL CHECK (octet_length(trace) BETWEEN 112 AND 12400) +); +CREATE TABLE evolution.primitive_life_bindings ( + world_id evolution.u64 NOT NULL, + account_id uuid NOT NULL, + request_id evolution.u64 NOT NULL, + born evolution.u64 NOT NULL CHECK (born > 0), + commit_id uuid NOT NULL REFERENCES evolution.primitive_publications, + PRIMARY KEY (world_id, account_id, request_id), + UNIQUE (world_id, account_id), + FOREIGN KEY (world_id, account_id, request_id) REFERENCES evolution.life_reservations +); +REVOKE ALL ON evolution.primitive_publications, evolution.primitive_life_bindings FROM PUBLIC; +CREATE TRIGGER primitive_publications_immutable BEFORE UPDATE OR DELETE ON evolution.primitive_publications + FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite(); +CREATE TRIGGER primitive_publications_no_truncate BEFORE TRUNCATE ON evolution.primitive_publications + FOR EACH STATEMENT EXECUTE FUNCTION evolution.refuse_history_rewrite(); +CREATE TRIGGER primitive_life_bindings_immutable BEFORE UPDATE OR DELETE ON evolution.primitive_life_bindings + FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite(); +CREATE TRIGGER primitive_life_bindings_no_truncate BEFORE TRUNCATE ON evolution.primitive_life_bindings + FOR EACH STATEMENT EXECUTE FUNCTION evolution.refuse_history_rewrite(); + +-- All runtime paths to region_commits, including old generic publishers, meet +-- this guard. Only the guarded new publisher can insert the private trace row. +CREATE FUNCTION evolution.require_primitive_publication() RETURNS trigger +LANGUAGE plpgsql SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$ +BEGIN + IF evolution.manifest_uint(NEW.manifest,12,4) = 3 AND NEW.commit_sequence > 0 + AND NOT EXISTS (SELECT 1 FROM evolution.primitive_publications p + WHERE p.commit_id = NEW.commit_id + AND octet_length(p.trace) = evolution.manifest_uint(NEW.manifest,280,8) + AND sha256(p.trace) = substring(NEW.manifest FROM 289 FOR 32)) THEN + RAISE EXCEPTION 'primitive publication evidence required' USING ERRCODE = 'EVL06'; + END IF; + RETURN NEW; +END $$; +CREATE TRIGGER region_primitive_publication BEFORE INSERT ON evolution.region_commits + FOR EACH ROW EXECUTE FUNCTION evolution.require_primitive_publication(); + +CREATE FUNCTION evolution.refuse_primitive_probe_actions() RETURNS trigger +LANGUAGE plpgsql SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$ +BEGIN + IF EXISTS (SELECT 1 FROM evolution.region_commits c WHERE c.commit_id = NEW.commit_id + AND evolution.manifest_uint(c.manifest,12,4) = 3) THEN + RAISE EXCEPTION 'primitive checkpoints cannot carry probe actions' USING ERRCODE = 'EVL06'; + END IF; + RETURN NEW; +END $$; +CREATE TRIGGER primitive_probe_actions BEFORE INSERT ON evolution.region_commit_actions + FOR EACH ROW EXECUTE FUNCTION evolution.refuse_primitive_probe_actions(); + +CREATE FUNCTION evolution.refuse_published_life_rejection() RETURNS trigger +LANGUAGE plpgsql SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$ +BEGIN + IF NEW.rejection <> 0 AND EXISTS (SELECT 1 FROM evolution.primitive_life_bindings b + WHERE b.world_id = OLD.world_id AND b.account_id = OLD.account_id + AND b.request_id = OLD.request_id) THEN + RAISE EXCEPTION 'published life is not a refusal' USING ERRCODE = 'EVL16'; + END IF; + RETURN NEW; +END $$; +CREATE TRIGGER published_life_rejection BEFORE UPDATE ON evolution.life_reservations + FOR EACH ROW EXECUTE FUNCTION evolution.refuse_published_life_rejection(); + +-- Private bounded trace parser. PostgreSQL independently checks the codec's +-- canonical ordering, identity/control fields, scope and predecessor binding. +CREATE FUNCTION evolution.primitive_births(p_trace bytea, p_manifest bytea) +RETURNS TABLE(account_id uuid, request_id evolution.u64, identity evolution.u64, + generation evolution.u64, born evolution.u64) +LANGUAGE plpgsql IMMUTABLE SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$ +DECLARE + c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(p_manifest); + n numeric; + off integer; + account bytea; + prev_account bytea := ''::bytea; + prev_tick numeric := -1; + prev_request numeric := -1; + accounts uuid[] := '{}'; + identities numeric[] := '{}'; +BEGIN + IF c.profile <> 3 OR c.commit_sequence = 0 OR p_trace IS NULL + OR octet_length(p_trace) <> c.trace_bytes OR sha256(p_trace) <> c.trace_sha256 + OR substring(p_trace FROM 1 FOR 8) <> '\x45564c4946544231' + OR evolution.manifest_uint(p_trace,8,4) <> 1 + OR evolution.manifest_uint(p_trace,104,8) <> 0 + OR evolution.manifest_uint(p_trace,16,8) <> c.world_id + OR evolution.manifest_uint(p_trace,24,8) <> c.region_id + OR evolution.manifest_uint(p_trace,32,8) <> c.frame_id + OR encode(substring(p_trace FROM 41 FOR 16),'hex')::uuid <> c.predecessor_commit + OR substring(p_trace FROM 57 FOR 32) <> c.predecessor_digest + OR evolution.manifest_uint(p_trace,88,8) <> c.begin_tick + OR evolution.manifest_uint(p_trace,96,8) <> c.end_tick THEN + RAISE EXCEPTION 'invalid primitive trace binding' USING ERRCODE = '22023'; + END IF; + n := evolution.manifest_uint(p_trace,12,4); + IF n > 256 OR 112 + n * 48 <> octet_length(p_trace) THEN + RAISE EXCEPTION 'invalid primitive trace count' USING ERRCODE = '22023'; + END IF; + FOR i IN 0 .. n - 1 LOOP + off := 112 + i * 48; + account := substring(p_trace FROM off + 1 FOR 16); + account_id := encode(account,'hex')::uuid; + request_id := evolution.manifest_uint(p_trace,off+16,8); + identity := evolution.manifest_uint(p_trace,off+24,8); + generation := evolution.manifest_uint(p_trace,off+32,8); + born := evolution.manifest_uint(p_trace,off+40,8); + IF account_id = '00000000-0000-0000-0000-000000000000' OR request_id = 0 + OR identity = 0 OR generation = 0 OR born <= c.begin_tick OR born > c.end_tick + OR (born, account, request_id) <= (prev_tick, prev_account, prev_request) + OR account_id = ANY(accounts) OR identity = ANY(identities) THEN + RAISE EXCEPTION 'invalid primitive birth' USING ERRCODE = '22023'; + END IF; + accounts := accounts || account_id; + identities := identities || identity::numeric; + prev_tick := born; + prev_account := account; + prev_request := request_id; + RETURN NEXT; + END LOOP; +END $$; + +CREATE FUNCTION evolution.publish_primitive_checkpoint(p_manifest 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); + b record; + r evolution.life_reservations%ROWTYPE; + old_trace bytea; +BEGIN + -- Validate before retry: malformed input cannot acquire authority or mutate. + PERFORM * FROM evolution.primitive_births(p_trace,p_manifest); + SELECT p.trace INTO old_trace FROM evolution.primitive_publications p WHERE p.commit_id=c.commit_id; + IF FOUND THEN + IF old_trace <> p_trace THEN + RAISE EXCEPTION 'primitive commit identity reused' USING ERRCODE='EVL07'; + END IF; + RETURN QUERY SELECT * FROM evolution.publish_region_checkpoint(p_manifest,''::bytea); + RETURN; + END IF; + PERFORM evolution.check_region_lease(c.world_id,c.region_id,c.holder,c.epoch); + -- A concurrent exact retry can have committed while this call waited for the + -- lease row. Reconcile again before treating its binding as a duplicate birth. + SELECT p.trace INTO old_trace FROM evolution.primitive_publications p WHERE p.commit_id=c.commit_id; + IF FOUND THEN + IF old_trace <> p_trace THEN + RAISE EXCEPTION 'primitive commit identity reused' USING ERRCODE='EVL07'; + END IF; + RETURN QUERY SELECT * FROM evolution.publish_region_checkpoint(p_manifest,''::bytea); + RETURN; + END IF; + IF (SELECT count(*) FROM evolution.primitive_life_bindings x + JOIN evolution.life_reservations reserved USING(world_id,account_id,request_id) + WHERE x.world_id=c.world_id AND reserved.region_id=c.region_id) + + evolution.manifest_uint(p_trace,12,4) > 256 THEN + RAISE EXCEPTION 'primitive region capacity exceeded' USING ERRCODE='EVL16'; + END IF; + -- Across regions, lock accounts by UUID, independent of their birth-tick order. + FOR b IN SELECT * FROM evolution.primitive_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,b.account_id,b.generation); + SELECT * INTO r FROM evolution.life_reservations x WHERE x.world_id=c.world_id + AND x.account_id=b.account_id AND x.request_id=b.request_id FOR UPDATE; + IF NOT FOUND OR r.region_id <> c.region_id OR r.identity <> b.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=b.account_id) THEN + RAISE EXCEPTION 'primitive reservation conflicts' USING ERRCODE='EVL16'; + END IF; + END LOOP; + -- Recheck every expiry after all lock waits, including earlier accounts. + FOR b IN SELECT * FROM evolution.primitive_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,b.account_id,b.generation); + END LOOP; + INSERT INTO evolution.primitive_publications VALUES(c.commit_id,p_trace); + INSERT INTO evolution.primitive_life_bindings(world_id,account_id,request_id,born,commit_id) + SELECT c.world_id,birth.account_id,birth.request_id,birth.born,c.commit_id + FROM evolution.primitive_births(p_trace,p_manifest) birth; + -- This statement atomically inserts the checkpoint and advances its head; + -- any refusal rolls back evidence and bindings as well. No speculative success. + RETURN QUERY SELECT * FROM evolution.publish_region_checkpoint(p_manifest,''::bytea); +END $$; + +-- Recovery uses every binding of the region, not just the current session. +CREATE FUNCTION evolution.load_primitive_lives(p_world evolution.u64,p_region evolution.u64) +RETURNS TABLE(account_id uuid,request_id evolution.u64,identity evolution.u64, + born evolution.u64,commit_id uuid,commit_sequence evolution.u64) +LANGUAGE sql STABLE SECURITY DEFINER SET search_path=pg_catalog,pg_temp AS $$ + SELECT b.account_id,b.request_id,r.identity,b.born,b.commit_id,c.commit_sequence + FROM evolution.primitive_life_bindings b + JOIN evolution.life_reservations r USING(world_id,account_id,request_id) + JOIN evolution.region_commits c ON c.commit_id=b.commit_id + WHERE b.world_id=p_world AND r.region_id=p_region + ORDER BY b.born,r.identity LIMIT 257; +$$; + +REVOKE ALL ON FUNCTION + evolution.require_primitive_publication(), evolution.refuse_primitive_probe_actions(), + evolution.refuse_published_life_rejection(), evolution.primitive_births(bytea,bytea), + evolution.publish_primitive_checkpoint(bytea,bytea), + evolution.load_primitive_lives(evolution.u64,evolution.u64) FROM PUBLIC; +GRANT EXECUTE ON FUNCTION evolution.publish_primitive_checkpoint(bytea,bytea), + evolution.load_primitive_lives(evolution.u64,evolution.u64) TO :"runtime_role"; +INSERT INTO evolution.schema_migrations(version,sha256) VALUES(16, :'migration_sha_16'); diff --git a/migrations/apply.sql b/migrations/apply.sql index eaf83a7..e17acc7 100644 --- a/migrations/apply.sql +++ b/migrations/apply.sql @@ -81,28 +81,43 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') || ',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' AS at_v15 \gset - \if :at_v15 + || ',15:' || :'migration_sha_15' AS at_v15, + :'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' AS at_v16 \gset + \if :at_v16 + \elif :at_v15 + \ir 0016_primitive_publication.sql \elif :at_v14 \ir 0015_life_reservations.sql + \ir 0016_primitive_publication.sql \elif :at_v13 \ir 0014_probe_overdue.sql \ir 0015_life_reservations.sql + \ir 0016_primitive_publication.sql \elif :at_v12 \ir 0013_probe_incoming.sql \ir 0014_probe_overdue.sql \ir 0015_life_reservations.sql + \ir 0016_primitive_publication.sql \elif :at_v11 \ir 0012_probe_handoff.sql \ir 0013_probe_incoming.sql \ir 0014_probe_overdue.sql \ir 0015_life_reservations.sql + \ir 0016_primitive_publication.sql \elif :at_v10 \ir 0011_region_actions.sql \ir 0012_probe_handoff.sql \ir 0013_probe_incoming.sql \ir 0014_probe_overdue.sql \ir 0015_life_reservations.sql + \ir 0016_primitive_publication.sql \elif :at_v9 \ir 0010_account_control.sql \ir 0011_region_actions.sql @@ -110,6 +125,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0013_probe_incoming.sql \ir 0014_probe_overdue.sql \ir 0015_life_reservations.sql + \ir 0016_primitive_publication.sql \elif :at_v8 \ir 0009_serialized_authority.sql \ir 0010_account_control.sql @@ -118,6 +134,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0013_probe_incoming.sql \ir 0014_probe_overdue.sql \ir 0015_life_reservations.sql + \ir 0016_primitive_publication.sql \elif :at_v7 \ir 0008_upgrade_barrier.sql \ir 0009_serialized_authority.sql @@ -127,6 +144,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0013_probe_incoming.sql \ir 0014_probe_overdue.sql \ir 0015_life_reservations.sql + \ir 0016_primitive_publication.sql \elif :at_v6 \ir 0007_region_clones.sql \ir 0008_upgrade_barrier.sql @@ -137,6 +155,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0013_probe_incoming.sql \ir 0014_probe_overdue.sql \ir 0015_life_reservations.sql + \ir 0016_primitive_publication.sql \elif :at_v5 \ir 0006_commit_lookup.sql \ir 0007_region_clones.sql @@ -148,6 +167,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0013_probe_incoming.sql \ir 0014_probe_overdue.sql \ir 0015_life_reservations.sql + \ir 0016_primitive_publication.sql \elif :at_v4 \ir pre_v5_guard.sql \ir 0005_batch_decisions.sql @@ -161,6 +181,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0013_probe_incoming.sql \ir 0014_probe_overdue.sql \ir 0015_life_reservations.sql + \ir 0016_primitive_publication.sql \elif :at_v3 \ir 0004_lineage_profile.sql \ir pre_v5_guard.sql @@ -175,6 +196,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0013_probe_incoming.sql \ir 0014_probe_overdue.sql \ir 0015_life_reservations.sql + \ir 0016_primitive_publication.sql \elif :at_v2 \ir 0003_region_checkpoints.sql \ir 0004_lineage_profile.sql @@ -190,6 +212,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0013_probe_incoming.sql \ir 0014_probe_overdue.sql \ir 0015_life_reservations.sql + \ir 0016_primitive_publication.sql \elif :at_v1 \ir 0002_region_fencing.sql \ir 0003_region_checkpoints.sql @@ -206,6 +229,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0013_probe_incoming.sql \ir 0014_probe_overdue.sql \ir 0015_life_reservations.sql + \ir 0016_primitive_publication.sql \else \echo 'Unknown or changed migration history' DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$; @@ -227,6 +251,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution') \ir 0013_probe_incoming.sql \ir 0014_probe_overdue.sql \ir 0015_life_reservations.sql + \ir 0016_primitive_publication.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 @@ -247,6 +272,8 @@ BEGIN UNION SELECT m.member FROM pg_catalog.pg_auth_members m JOIN closure c ON m.roleid = c.oid ), fenced(oid) AS (VALUES + ('evolution.publish_primitive_checkpoint(bytea,bytea)'::regprocedure::oid), + ('evolution.load_primitive_lives(evolution.u64,evolution.u64)'::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), @@ -300,7 +327,7 @@ BEGIN OR EXISTS (SELECT 1 FROM pg_catalog.pg_namespace n WHERE n.nspname = 'public' AND pg_catalog.has_schema_privilege(c.oid, n.oid, 'CREATE')) UNION ALL - -- Only the eighteen guarded functions are executable; provisioning/internal are not. + -- Only listed fenced functions are executable; provisioning/internal are not. SELECT c.oid, 'EXECUTE outside the fenced function set' FROM closure c CROSS JOIN pg_catalog.pg_proc p WHERE p.pronamespace = 'evolution'::regnamespace diff --git a/schemas/primitive-checkpoint-v1.md b/schemas/primitive-checkpoint-v1.md index dd74ffc..7461109 100644 --- a/schemas/primitive-checkpoint-v1.md +++ b/schemas/primitive-checkpoint-v1.md @@ -1,8 +1,8 @@ # Primitive creation checkpoint profile 3 -ADR-0100, M11.2c-2b. This is the portable profile declaration. Database schema v15 -still refuses profile 3; database admission requires atomic life publication and -must not be enabled by merely widening the SQL manifest parser. +ADR-0100, M11.2c-2b. This is the portable profile declaration. Schema v16 admits +profile 3 through the atomic publisher in [primitive-publication-v1](primitive-publication-v1.md). +Existing generic runtime publishers cannot bypass its trace/control guard. The manifest uses the existing 360-byte version 2 layout. Profile is 3; history profile is 2 (lineage archive format 1, genome schema 2, development 2, founder-life @@ -28,5 +28,5 @@ The manifest owns the checkpoint clock; an idle batch advances no biology. Clone genesis is refused until life/account bindings and world allocator ranges have a reviewed isolated-clone mapping. Existing environmental profiles, clone -contracts and goldens remain unchanged. Portable support does not yet imply -publication, backup/audit or server-host support for this new profile. +contracts and goldens remain unchanged. Portable and database publication do not +yet imply backup/audit, durable object-store orchestration or host support. diff --git a/schemas/primitive-publication-v1.md b/schemas/primitive-publication-v1.md new file mode 100644 index 0000000..eb0db33 --- /dev/null +++ b/schemas/primitive-publication-v1.md @@ -0,0 +1,42 @@ +# Primitive birth publication (schema v16) + +ADR-0100, M11.2c-2c. Migration 0016 admits manifest profile 3 and adds one +guarded publication transaction. It does not add biological ticking. The payload +must already be replayed and its immutable object-store length/digest verified; +this database stores the same commit evidence as existing profiles. + +`publish_primitive_checkpoint(manifest, trace)` parses the canonical manifest and +bounded primitive-batch-v1 trace, then checks world/region/frame, predecessor ID +and digest, begin/end ticks, trace digest, record order and bounds. For each birth +it verifies the world/account/request reservation, shared identity, founder rules, +original owner region, no prior binding and exact durable account-control +generation under the current region lease. Accounts lock in UUID order. Lease and +control are rechecked after waits. + +In one SQL transaction it appends the commit-bound trace and immutable +`primitive_life_bindings`, then calls the existing chain publisher to advance the +head. A database refusal rolls every write back. The commit trigger requires the +private trace evidence for every non-genesis profile-3 commit, so the two existing +generic runtime publishers cannot publish such a commit around the guard. Probe +action rows cannot be attached. A published life's reservation cannot later be +changed into a refusal. Bindings are append-only; schema v16 has no death or +respawn transition. + +An exact retry returns the original receipt. If it waited behind the commit, the +function checks the private evidence again before checking births. Changed bytes +under the same commit ID are a conflict; a second commit for the same life is +refused. A different commit may carry an empty birth batch and advance the owner +checkpoint tick without advancing biology. Unknown connection outcome requires +reconnect and exact-byte retry. Reservation status by itself remains insufficient +for spawn success. + +`load_primitive_lives(world, region)` returns the bounded, durable account/request, +identity, birth-tick and commit binding set for reconciliation. Session generations +are deliberately reacquired; they are not restored as authority. Recovery still +must restore the object, replay each committed trace from its manifest predecessor, +compare the canonical payload and reconcile this binding set before a host can be +ready. The generic two-profile recovery host does not yet support profile 3. + +The atomicity/fencing tests use a fresh PostgreSQL cluster, full portable payload +and trace codecs, and a real runtime role. World 9753 is test-only. No production +world is provisioned by migration or tests. diff --git a/tests/fixtures/server_process.hpp b/tests/fixtures/server_process.hpp index fe4cb7a..77e64c5 100644 --- a/tests/fixtures/server_process.hpp +++ b/tests/fixtures/server_process.hpp @@ -220,10 +220,12 @@ inline ServerRegion provision(std::uint64_t region, std::uint8_t genesis) { build.fill(0xB1); persistence::Uuid commit{}; commit.fill(genesis); - expect(persistence::provision_lineage_region(*db, **objects, r.key, region_identity.frame, - *lineage_region(), commit, build, 8000000, 5s) - .has_value(), - "provision"); + const auto provisioned = + persistence::provision_lineage_region(*db, **objects, r.key, region_identity.frame, + *lineage_region(), commit, build, 8000000, 5s); + if (!provisioned) + throw std::runtime_error("server fixture: provision SQLSTATE " + + std::string{provisioned.error().store.cause.sqlstate.data(), 5}); return r; } inline std::vector server_args(const ServerRegion& r, diff --git a/tests/postgres_tests.cpp b/tests/postgres_tests.cpp index a12e03e..2a3de86 100644 --- a/tests/postgres_tests.cpp +++ b/tests/postgres_tests.cpp @@ -7,6 +7,7 @@ void run_region_lease_store_tests(); void run_account_control_store_tests(); void run_life_reservation_store_tests(); +void run_primitive_publication_tests(); void run_probe_handoff_store_tests(); void run_region_action_tests(); void run_region_checkpoint_store_tests(); @@ -124,6 +125,7 @@ int main(int argc, char** argv) { try { if (argc == 2 && std::string_view(argv[1]) == "--life-reservations-only") { run_life_reservation_store_tests(); + run_primitive_publication_tests(); std::puts("life reservation adapter integration passed"); return 0; } @@ -134,6 +136,7 @@ int main(int argc, char** argv) { run_region_lease_store_tests(); run_account_control_store_tests(); run_life_reservation_store_tests(); + run_primitive_publication_tests(); run_probe_handoff_store_tests(); run_region_action_tests(); std::puts("region lease fencing integration passed"); diff --git a/tests/primitive_publication_tests.cpp b/tests/primitive_publication_tests.cpp new file mode 100644 index 0000000..3a146b9 --- /dev/null +++ b/tests/primitive_publication_tests.cpp @@ -0,0 +1,232 @@ +// Real database atomic-publication checks. Payloads/traces use the real portable +// codecs; object upload/recovery belongs to the subsequent adapter subsection. +#include "evo/persistence/account_control_store.hpp" +#include "evo/persistence/life_reservation_store.hpp" +#include "evo/persistence/region_checkpoint.hpp" +#include "evo/persistence/region_lease_store.hpp" +#include "evo/persistence/sha256.hpp" +#include +#include +#include +#include + +void run_primitive_publication_tests(); +namespace { +using namespace evo::persistence; +using namespace evo::server; +using namespace std::chrono_literals; +void check(bool value, std::source_location at = std::source_location::current()) { + if (!value) + throw std::runtime_error("primitive publication test line " + std::to_string(at.line())); +} +#define CHECK(...) check(static_cast(__VA_ARGS__)) +std::string env(const char* name) { + const auto* value = std::getenv(name); + if (!value) + throw std::runtime_error("missing PostgreSQL test environment"); + return value; +} +auto connect_as(const std::string& user) { + auto db = + Postgres::connect({env("PGHOST"), {}, env("PGPORT"), env("PGDATABASE"), user, {}, {}}, 5s); + CHECK(db); + return std::move(*db); +} +std::string hex(std::span bytes) { + constexpr char digits[] = "0123456789abcdef"; + std::string out = "\\x"; + for (auto b : bytes) { + out.push_back(digits[b >> 4U]); + out.push_back(digits[b & 15U]); + } + return out; +} +template auto id(std::uint8_t b) { + std::array out{}; + out.fill(b); + return out; +} +Uuid test_commit(std::uint8_t suffix) { + Uuid out{}; + out[0] = 0xEF; + out[15] = suffix; + return out; +} +bool state_is(const auto& result, std::string_view state) { + return !result && std::string_view(result.error().sqlstate.data()) == state; +} +} // namespace + +void run_primitive_publication_tests() { + static_assert(max_primitive_lives_bytes == 3266800); // SQL cap must match C++. + auto owner = connect_as(env("PGUSER")); + auto db = connect_as(env("EVO_PG_RUNTIME_USER")); + const RegionKey key{9753, 1}; + const auto build = id<32>(0xD7), payload_digest = id<32>(0xA7); + CHECK(owner->query("INSERT INTO evolution.retained_builds(build_identity) VALUES " + "(decode(repeat('d7',32),'hex'))", + {}, 5s)); + auto endowed = evo::biology::primitive_endowment(); + CHECK(endowed); + auto funding = endowed->body; + funding.energy.value = endowed->reserve.energy.value + endowed->construction.value; + auto base = PrimitiveLives::create({9753, 1, 1, 1, funding, {{0, 0}}}); + CHECK(base); + auto opening = (*base)->snapshot(); + const auto initial_digest = sha256(opening); + CHECK(initial_digest); + auto genesis = genesis_manifest(key, 1, test_commit(1), 50000, build, opening.size(), + *initial_digest, CheckpointProfile::primitive_lives_v1); + CHECK(genesis); + auto initial = encode_manifest(*genesis); + CHECK(initial); + const auto provisioned = owner->query("SELECT evolution.provision_region_checkpoint($1::bytea)", + std::array{hex(*initial)}, 5s); + if (!provisioned) + throw std::runtime_error("primitive genesis SQLSTATE " + + std::string{provisioned.error().sqlstate.data(), 5}); + CHECK(owner->query("INSERT INTO evolution.life_allocators VALUES(9753,1)", {}, 5s)); + auto lease = acquire_region_lease(*db, key, build, 20000ms, 5s); + CHECK(lease); + const auto alice = id<16>(0x71); + auto control = claim_account_control(*db, key, *lease, alice, 1, 1, 20000ms, 5s); + CHECK(control); + auto reserved = reserve_primitive_life(*db, key, *lease, alice, control->generation, 1, 1, 5s); + CHECK(reserved && reserved->identity == 1); + const auto predecessor_digest = sha256(*initial); + CHECK(predecessor_digest); + PrimitiveBatch batch{{9753, 1, 1, genesis->commit, *predecessor_digest, 0, 1}, + {{{alice, 1}, 1, control->generation, {1}}}}; + auto replayed = replay_primitive_batch(**base, batch); + CHECK(replayed); + const auto payload = (*replayed)->snapshot(); + auto trace = encode_primitive_batch(batch); + CHECK(trace); + auto trace_digest = sha256(*trace), actual_payload_digest = sha256(payload); + CHECK(trace_digest && actual_payload_digest); + auto next = successor_manifest(*genesis, *predecessor_digest, test_commit(2), *lease, 1, build, + payload.size(), *actual_payload_digest, + {trace->size(), *trace_digest, 0, {}}); + CHECK(next); + auto manifest = encode_manifest(*next); + CHECK(manifest); + const auto publish = [&](const auto& m, const auto& t) { + return db->query( + "SELECT * FROM evolution.publish_primitive_checkpoint($1::bytea,$2::bytea)", + std::array{hex(m), hex(t)}, 5s); + }; + // Old runtime publication routes cannot insert primitive commits. + auto bypass = publish_region_checkpoint(*db, *manifest, {}, 5s); + CHECK(!bypass && bypass.error().kind == LeaseStoreError::unsupported); + auto bypass_actions = publish_region_checkpoint(*db, *manifest, {}, {}, 5s); + CHECK(!bypass_actions && bypass_actions.error().kind == LeaseStoreError::unsupported); + CHECK(!db->query("SELECT * FROM evolution.primitive_publications", {}, 5s)); + CHECK(!db->query("SELECT * FROM evolution.primitive_life_bindings", {}, 5s)); + const auto refuses = [&](auto change, std::string_view sqlstate) { + auto altered = batch; + change(altered); + auto altered_trace = encode_primitive_batch(altered); + CHECK(altered_trace); + auto altered_manifest = *next; + altered_manifest.batch.trace_sha256 = *sha256(*altered_trace); + auto bytes = encode_manifest(altered_manifest); + CHECK(bytes); + CHECK(state_is(publish(*bytes, *altered_trace), sqlstate)); + const auto head = load_region_head(*db, key, 5s); + CHECK(head && head->sequence == 0); + }; + refuses([](auto& b) { b.births[0].generation = 99; }, "EVL02"); + refuses([](auto& b) { b.births[0].identity = 99; }, "EVL16"); + refuses([](auto& b) { b.births[0].request.action = 99; }, "EVL16"); + refuses([](auto& b) { b.header.frame = 99; }, "22023"); + const auto committed = publish(*manifest, *trace); + CHECK(committed); + db = connect_as(env("EVO_PG_RUNTIME_USER")); + const auto retry = publish(*manifest, *trace); + CHECK(retry && *retry == *committed); + CHECK(load_region_head(*db, key, 5s)->sequence == 1); + auto bindings = db->query("SELECT * FROM evolution.load_primitive_lives(9753,1)", {}, 5s); + CHECK(bindings && bindings->size() == 1 && (*bindings)[0][2] == "1" && + (*bindings)[0][5] == "1"); + auto reject = reject_primitive_life(*db, key, *lease, alice, control->generation, 1, + LifeRefusal::cancelled, 5s); + CHECK(!reject && reject.error().kind == LifeReservationError::existing_life); + auto changed = *next; + changed.payload_sha256 = payload_digest; + CHECK(state_is(publish(*encode_manifest(changed), *trace), "EVL07")); + // Same birth under a later commit is rejected, not first-result-wins. + auto repeated = batch; + repeated.header.predecessor = next->commit; + repeated.header.predecessor_digest = *sha256(*manifest); + repeated.header.begin_tick = 1; + repeated.header.end_tick = 2; + repeated.births[0].born.value = 2; + auto repeated_trace = encode_primitive_batch(repeated); + CHECK(repeated_trace); + auto later = successor_manifest(*next, *sha256(*manifest), test_commit(3), *lease, 2, build, + payload.size(), *actual_payload_digest, + {repeated_trace->size(), *sha256(*repeated_trace), 0, {}}); + CHECK(later && state_is(publish(*encode_manifest(*later), *repeated_trace), "EVL16")); + repeated.births.clear(); + auto idle_trace = encode_primitive_batch(repeated); + CHECK(idle_trace); + later->batch = {idle_trace->size(), *sha256(*idle_trace), 0, {}}; + auto idle_manifest = encode_manifest(*later); + CHECK(idle_manifest); + CHECK(db->query("BEGIN", {}, 5s)); + CHECK(publish(*idle_manifest, *idle_trace)); + CHECK(db->query("ROLLBACK", {}, 5s)); + CHECK(load_region_head(*db, key, 5s)->sequence == 1); + CHECK(publish(*idle_manifest, *idle_trace)); + CHECK(load_region_head(*db, key, 5s)->sequence == 2); + // Both exact retries enter before either can commit. The loser must recheck + // evidence after the lease lock wait, rather than refuse a duplicate birth. + repeated.header.predecessor = later->commit; + repeated.header.predecessor_digest = *sha256(*idle_manifest); + repeated.header.begin_tick = 2; + repeated.header.end_tick = 3; + auto concurrent_trace = encode_primitive_batch(repeated); + CHECK(concurrent_trace); + auto concurrent = successor_manifest( + *later, *sha256(*idle_manifest), test_commit(4), *lease, 3, build, payload.size(), + *actual_payload_digest, {concurrent_trace->size(), *sha256(*concurrent_trace), 0, {}}); + CHECK(concurrent); + const auto concurrent_manifest = encode_manifest(*concurrent); + CHECK(concurrent_manifest); + auto first_db = connect_as(env("EVO_PG_RUNTIME_USER")); + auto second_db = connect_as(env("EVO_PG_RUNTIME_USER")); + CHECK(owner->query("BEGIN", {}, 5s)); + CHECK(owner->query( + "SELECT * FROM evolution.region_leases WHERE world_id=9753 AND region_id=1 FOR UPDATE", {}, + 5s)); + std::optional> first_result, second_result; + const std::array parameters{hex(*concurrent_manifest), hex(*concurrent_trace)}; + const std::string sql = + "SELECT * FROM evolution.publish_primitive_checkpoint($1::bytea,$2::bytea)"; + std::jthread first([&] { first_result = first_db->query(sql, parameters, 5s); }); + std::jthread second([&] { second_result = second_db->query(sql, parameters, 5s); }); + bool waiting = false; + for (int attempt = 0; attempt < 200; ++attempt) { + CHECK(owner->query("SELECT pg_stat_clear_snapshot()", {}, 5s)); + auto active = + owner->query("SELECT count(*) FROM pg_stat_activity WHERE wait_event_type='Lock' " + "AND query LIKE 'SELECT * FROM evolution.publish_primitive_checkpoint%'", + {}, 5s); + CHECK(active); + if ((*active)[0][0] == "2") { + waiting = true; + break; + } + std::this_thread::sleep_for(10ms); + } + CHECK(owner->query("COMMIT", {}, 5s)); + first.join(); + second.join(); + if (!waiting || !first_result || !second_result || !*first_result || !*second_result) + throw std::runtime_error("concurrent primitive publication did not converge"); + CHECK(**first_result == **second_result); + CHECK(load_region_head(*db, key, 5s)->sequence == 3); + CHECK(release_region_lease(*db, key, *lease, 5s)); + // Exact committed retry survives loss of authority, but cannot originate work. + CHECK(publish(*manifest, *trace)); +} diff --git a/tests/region_migration_tests.cpp b/tests/region_migration_tests.cpp index dd14ea9..190ecf5 100644 --- a/tests/region_migration_tests.cpp +++ b/tests/region_migration_tests.cpp @@ -181,6 +181,7 @@ void upgrade_across_migration_and_barrier() { " -v migration_sha_13=$(s 0013_probe_incoming.sql)" " -v migration_sha_14=$(s 0014_probe_overdue.sql)" " -v migration_sha_15=$(s 0015_life_reservations.sql)" + " -v migration_sha_16=$(s 0016_primitive_publication.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"); @@ -196,7 +197,7 @@ 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"); + CHECK(history(*owner_db) == "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16"); 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 f91a442..6d754e5 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 +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 # 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")" = 15:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 +test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 16:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 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 +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 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. @@ -189,9 +189,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=16 WHERE version=15' +psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=17 WHERE version=16' 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=15 WHERE version=16" +psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=16 WHERE version=17" 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 2eaa47e..825ea7e 100644 --- a/tools/ci/upgrade-check.sh +++ b/tools/ci/upgrade-check.sh @@ -4,7 +4,7 @@ # exported). Real evo-server processes on world 500: # 1. Failed migration: a migration that takes the migration lock, alters a history # table, holds its locks for 3 s and then fails runs while build b1 serves. -# Nothing it did remains (schema still v15, no new column); the host keeps its +# Nothing it did remains (schema still v16, no new column); the host keeps its # authority, and every acknowledgement stays durable. # 2. Barrier (ADR-0068 operator order): build b2 is retained, and a b2 host is started # while b1 still holds the lease (it waits, never ready). upgrade_barrier(b1) fences @@ -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')" = 15 ] || fail "schema version moved" +[ "$(q -c 'SELECT max(version) FROM evolution.schema_migrations')" = 16 ] || 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 be32f00..a0e46be 100644 --- a/tools/migrate-postgres.sh +++ b/tools/migrate-postgres.sh @@ -42,6 +42,8 @@ digest_14="$(sha256sum "$root/migrations/0014_probe_overdue.sql")" digest_14="${digest_14%% *}" digest_15="$(sha256sum "$root/migrations/0015_life_reservations.sql")" digest_15="${digest_15%% *}" +digest_16="$(sha256sum "$root/migrations/0016_primitive_publication.sql")" +digest_16="${digest_16%% *}" 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" \ @@ -50,5 +52,6 @@ psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \ --set="migration_sha_10=$digest_10" --set="migration_sha_11=$digest_11" --set="migration_sha_12=$digest_12" --set="migration_sha_13=$digest_13" \ --set="migration_sha_14=$digest_14" \ --set="migration_sha_15=$digest_15" \ + --set="migration_sha_16=$digest_16" \ --set="runtime_role=$runtime_role" \ --file="$root/migrations/apply.sql"