-- ADR-0174: joint cellular/environment profile 6, immutable evidence and fenced publishers. -- Profiles 4/5 and every stored byte keep their meaning. Normalization is parser-local. ALTER FUNCTION evolution.parse_checkpoint_manifest(bytea) RENAME TO parse_checkpoint_manifest_v55; REVOKE ALL ON FUNCTION evolution.parse_checkpoint_manifest_v55(bytea) FROM PUBLIC, :"runtime_role"; CREATE 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; n bytea; tick_begin numeric; tick_end numeric; payload_size numeric; trace_size numeric; nested numeric; trace_digest bytea; zero32 constant bytea := decode(repeat('00',32),'hex'); b10 constant bytea := '\x080000000700000002000000010000000100000004000000030000000200000001000000'; b11 constant bytea := '\x090000000700000002000000010000000100000004000000030000000200000001000000'; BEGIN IF m IS NULL OR octet_length(m) <> 360 OR evolution.manifest_uint(m,12,4) <> 6 THEN RETURN evolution.parse_checkpoint_manifest_v55(m); END IF; tick_begin := evolution.manifest_uint(m,112,8); tick_end := evolution.manifest_uint(m,120,8); payload_size := evolution.manifest_uint(m,240,8); trace_size := evolution.manifest_uint(m,280,8); trace_digest := substring(m FROM 289 FOR 32); -- Joint batch 1: 32-byte header, nested population batch, 48 bytes per tick. nested := trace_size - 32 - 48 * greatest(tick_end - tick_begin, 0); IF substring(m FROM 161 FOR 36) NOT IN (b10,b11) OR evolution.manifest_uint(m,232,4) <> 6 OR evolution.manifest_uint(m,228,4) <> 0 OR evolution.manifest_uint(m,236,4) <> 1 OR evolution.manifest_uint(m,56,8) = 0 OR tick_end < tick_begin OR tick_end - tick_begin > 64 OR payload_size = 0 OR payload_size > 16782416 OR evolution.manifest_uint(m,320,4) <> 0 OR substring(m FROM 329 FOR 32) <> zero32 OR (trace_size = 0 AND (trace_digest <> zero32 OR tick_begin <> tick_end OR tick_end = 0)) OR (trace_size > 0 AND (trace_size < 184 OR trace_size > 1971480 OR trace_size % 8 <> 0 OR trace_digest = zero32 OR nested < 152)) THEN RAISE EXCEPTION 'unsupported or malformed environment manifest' USING ERRCODE = '22023'; END IF; -- Reuse the population profile's common-field validation with only its profile, -- history, payload cap and nested trace length normalized. Stored bytes stay profile 6. n := overlay(m PLACING '\x05000000'::bytea FROM 13 FOR 4); n := overlay(n PLACING '\x05000000'::bytea FROM 233 FOR 4); n := overlay(n PLACING evolution.manifest_u64le(least(payload_size,16781400)) FROM 241 FOR 8); IF trace_size > 0 THEN n := overlay(n PLACING evolution.manifest_u64le(nested) FROM 281 FOR 8); END IF; r := evolution.parse_checkpoint_manifest_v55(n); r.profile := 6; r.payload_bytes := payload_size; r.trace_bytes := trace_size; r.trace_sha256 := trace_digest; RETURN r; END $$; REVOKE ALL ON FUNCTION evolution.parse_checkpoint_manifest(bytea) FROM PUBLIC; -- Immutable evidence. Flow totals are derived from the trace for audit without replay; -- they distinguish environmental crossings from migration flows in the nested ledger. CREATE TABLE evolution.environment_publications ( commit_id uuid PRIMARY KEY REFERENCES evolution.region_commits(commit_id) DEFERRABLE INITIALLY DEFERRED, trace bytea NOT NULL, imported_energy numeric NOT NULL CHECK (imported_energy >= 0), imported_carbon numeric NOT NULL CHECK (imported_carbon >= 0), imported_nitrogen numeric NOT NULL CHECK (imported_nitrogen >= 0), imported_phosphorus numeric NOT NULL CHECK (imported_phosphorus >= 0), imported_other numeric NOT NULL CHECK (imported_other >= 0), radiated_heat numeric NOT NULL CHECK (radiated_heat >= 0) ); CREATE TABLE evolution.environment_profile_transitions ( target_commit uuid PRIMARY KEY REFERENCES evolution.region_commits(commit_id) DEFERRABLE INITIALLY DEFERRED, source_commit uuid UNIQUE NOT NULL REFERENCES evolution.region_commits(commit_id), source_manifest_sha256 bytea NOT NULL CHECK (octet_length(source_manifest_sha256) = 32), source_payload_sha256 bytea NOT NULL CHECK (octet_length(source_payload_sha256) = 32), CHECK (source_commit <> target_commit) ); REVOKE ALL ON evolution.environment_publications, evolution.environment_profile_transitions FROM PUBLIC; CREATE TRIGGER environment_publications_immutable BEFORE UPDATE OR DELETE ON evolution.environment_publications FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite(); CREATE TRIGGER environment_publications_no_truncate BEFORE TRUNCATE ON evolution.environment_publications FOR EACH STATEMENT EXECUTE FUNCTION evolution.refuse_history_rewrite(); CREATE TRIGGER environment_transitions_immutable BEFORE UPDATE OR DELETE ON evolution.environment_profile_transitions FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite(); CREATE TRIGGER environment_transitions_no_truncate BEFORE TRUNCATE ON evolution.environment_profile_transitions FOR EACH STATEMENT EXECUTE FUNCTION evolution.refuse_history_rewrite(); -- Every profile-6 commit, including from the generic publisher, needs its evidence. CREATE FUNCTION evolution.refuse_unpublished_environment() RETURNS trigger LANGUAGE plpgsql SET search_path = pg_catalog, pg_temp AS $$ BEGIN IF evolution.manifest_uint(NEW.manifest,12,4) = 6 AND (NEW.commit_sequence = 0 OR (NOT EXISTS (SELECT 1 FROM evolution.environment_publications x WHERE x.commit_id = NEW.commit_id) AND NOT EXISTS (SELECT 1 FROM evolution.environment_profile_transitions x WHERE x.target_commit = NEW.commit_id))) THEN RAISE EXCEPTION 'environment commit requires fenced publisher' USING ERRCODE = 'EVL06'; END IF; RETURN NEW; END $$; REVOKE ALL ON FUNCTION evolution.refuse_unpublished_environment() FROM PUBLIC; CREATE TRIGGER region_environment_evidence BEFORE INSERT ON evolution.region_commits FOR EACH ROW EXECUTE FUNCTION evolution.refuse_unpublished_environment(); -- Structural validation and flow totals only. C++ recovery replays the complete joint -- biology and environment and requires exact payload equality. CREATE FUNCTION evolution.check_environment_trace(m bytea,t bytea) RETURNS TABLE(imported_energy numeric,imported_carbon numeric,imported_nitrogen numeric, imported_phosphorus numeric,imported_other numeric,radiated_heat numeric) LANGUAGE plpgsql IMMUTABLE SET search_path = pg_catalog, pg_temp AS $$ DECLARE c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(m); kind numeric; nested numeric; flows numeric; n bytea; inner_trace bytea; off integer; totals numeric[] := ARRAY[0,0,0,0,0,0]::numeric[]; BEGIN IF c.profile <> 6 OR t IS NULL OR octet_length(t) <> c.trace_bytes OR c.trace_bytes = 0 OR sha256(t) <> c.trace_sha256 OR substring(t FROM 1 FOR 8) <> '\x4145564f45425431' OR evolution.manifest_uint(t,8,4) <> 1 THEN RAISE EXCEPTION 'invalid environment trace binding' USING ERRCODE = '22023'; END IF; kind := evolution.manifest_uint(t,12,4); nested := evolution.manifest_uint(t,16,8); flows := evolution.manifest_uint(t,24,4); IF kind > 1 OR (kind = 1) <> (c.begin_tick = c.end_tick) OR evolution.manifest_uint(t,28,4) <> 0 OR flows <> c.end_tick - c.begin_tick OR octet_length(t) <> 32 + nested + 48 * flows THEN RAISE EXCEPTION 'malformed environment trace' USING ERRCODE = '22023'; END IF; -- The nested population batch must satisfy its own complete structural contract, -- bound to the same identity, predecessor and ticks as this manifest. inner_trace := substring(t FROM 33 FOR nested::integer); n := overlay(m PLACING '\x05000000'::bytea FROM 13 FOR 4); n := overlay(n PLACING '\x05000000'::bytea FROM 233 FOR 4); n := overlay(n PLACING evolution.manifest_u64le(least(c.payload_bytes,16781400)) FROM 241 FOR 8); n := overlay(n PLACING evolution.manifest_u64le(nested) FROM 281 FOR 8); n := overlay(n PLACING sha256(inner_trace) FROM 289 FOR 32); PERFORM evolution.check_population_trace(n,inner_trace); off := 32 + nested::integer; FOR i IN 0 .. flows - 1 LOOP FOR j IN 0 .. 5 LOOP totals[j + 1] := totals[j + 1] + evolution.manifest_uint(t, off + 48 * i + 8 * j, 8); END LOOP; END LOOP; RETURN QUERY SELECT totals[1],totals[2],totals[3],totals[4],totals[5],totals[6]; END $$; REVOKE ALL ON FUNCTION evolution.check_environment_trace(bytea,bytea) FROM PUBLIC; -- Schema 45's owned interval admission also serves the joint profile: one pending -- bounded interval per region, settled atomically with its checkpoint. CREATE OR REPLACE FUNCTION evolution.admit_population_interval(w evolution.u64,r evolution.u64,holder uuid,epoch evolution.u64, source uuid,source_digest bytea,target_tick evolution.u64) RETURNS TABLE(source_commit uuid,source_manifest_sha256 bytea,begin_tick evolution.u64,end_tick evolution.u64,controller_version integer) LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ DECLARE a evolution.population_interval_admissions%ROWTYPE; h evolution.region_heads%ROWTYPE; c evolution.checkpoint_manifest; m bytea; BEGIN IF source IS NULL OR source_digest IS NULL OR octet_length(source_digest) <> 32 OR target_tick IS NULL THEN RAISE EXCEPTION 'invalid population interval' USING ERRCODE = '22023'; END IF; PERFORM evolution.check_region_lease(w,r,holder,epoch); SELECT * INTO a FROM evolution.population_interval_admissions x WHERE x.source_commit = source; IF FOUND THEN IF a.world_id <> w OR a.region_id <> r OR a.source_manifest_sha256 <> source_digest OR a.end_tick <> target_tick THEN RAISE EXCEPTION 'population interval identity reused' USING ERRCODE = 'EVL07'; END IF; RETURN QUERY SELECT a.source_commit,a.source_manifest_sha256,a.begin_tick,a.end_tick,a.controller_version; RETURN; END IF; SELECT * INTO h FROM evolution.region_heads x WHERE x.world_id = w AND x.region_id = r FOR UPDATE; IF NOT FOUND OR h.commit_id <> source OR h.digest <> source_digest THEN RAISE EXCEPTION 'population interval head moved' USING ERRCODE = 'EVL03'; END IF; SELECT x.manifest INTO m FROM evolution.region_commits x WHERE x.commit_id = source; c := evolution.parse_checkpoint_manifest(m); IF c.profile NOT IN (5,6) OR target_tick <= h.end_tick OR target_tick - h.end_tick > 64 THEN RAISE EXCEPTION 'unsupported population interval' USING ERRCODE = 'EVL06'; END IF; IF EXISTS (SELECT 1 FROM evolution.population_interval_pending x WHERE x.world_id = w AND x.region_id = r) THEN RAISE EXCEPTION 'population interval already pending' USING ERRCODE = 'EVL18'; END IF; INSERT INTO evolution.population_interval_admissions VALUES(source,w,r,source_digest,h.end_tick,target_tick,1,holder,epoch) RETURNING * INTO a; INSERT INTO evolution.population_interval_pending VALUES(w,r,source); RETURN QUERY SELECT a.source_commit,a.source_manifest_sha256,a.begin_tick,a.end_tick,a.controller_version; END $$; CREATE FUNCTION evolution.publish_environment_checkpoint(m bytea,t 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(m); f record; a evolution.population_interval_admissions%ROWTYPE; existing bytea; BEGIN SELECT * INTO f FROM evolution.check_environment_trace(m,t); PERFORM evolution.check_region_lease(c.world_id,c.region_id,c.holder,c.epoch); SELECT p.trace INTO existing FROM evolution.environment_publications p WHERE p.commit_id = c.commit_id; IF FOUND THEN IF existing <> t THEN RAISE EXCEPTION 'environment identity reused' USING ERRCODE = 'EVL07'; END IF; RETURN QUERY SELECT * FROM evolution.publish_region_checkpoint_core(m,''::bytea); RETURN; END IF; IF EXISTS (SELECT 1 FROM evolution.region_commits x WHERE x.commit_id = c.commit_id) THEN RAISE EXCEPTION 'environment identity reused' USING ERRCODE = 'EVL07'; END IF; IF c.end_tick > c.begin_tick THEN SELECT x.* INTO a FROM evolution.population_interval_pending p JOIN evolution.population_interval_admissions x ON x.source_commit = p.source_commit WHERE p.world_id = c.world_id AND p.region_id = c.region_id AND p.source_commit = c.predecessor_commit; IF NOT FOUND OR a.world_id <> c.world_id OR a.region_id <> c.region_id OR a.source_manifest_sha256 <> c.predecessor_digest OR a.begin_tick <> c.begin_tick OR a.end_tick <> c.end_tick OR EXISTS (SELECT 1 FROM evolution.population_interval_settlements s WHERE s.source_commit = a.source_commit) THEN RAISE EXCEPTION 'environment publication requires exact interval' USING ERRCODE = 'EVL18'; END IF; INSERT INTO evolution.population_interval_settlements VALUES(a.source_commit,c.commit_id); DELETE FROM evolution.population_interval_pending p WHERE p.world_id = c.world_id AND p.region_id = c.region_id AND p.source_commit = a.source_commit; ELSIF EXISTS (SELECT 1 FROM evolution.population_interval_pending x WHERE x.world_id = c.world_id AND x.region_id = c.region_id) THEN RAISE EXCEPTION 'environment conversion waits for interval' USING ERRCODE = 'EVL18'; END IF; INSERT INTO evolution.environment_publications VALUES(c.commit_id,t,f.imported_energy,f.imported_carbon, f.imported_nitrogen,f.imported_phosphorus,f.imported_other,f.radiated_heat); RETURN QUERY SELECT * FROM evolution.publish_region_checkpoint_core(m,''::bytea); END $$; REVOKE ALL ON FUNCTION evolution.publish_environment_checkpoint(bytea,bytea) FROM PUBLIC; GRANT EXECUTE ON FUNCTION evolution.publish_environment_checkpoint(bytea,bytea) TO :"runtime_role"; -- One-way, no-tick profile 5 -> 6 with the identical bundle. The source payload digest -- is bound; C++ recovery verifies the nested population is byte-identical. CREATE FUNCTION evolution.publish_environment_profile_transition(m 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(m); s evolution.checkpoint_manifest; p evolution.region_commits%ROWTYPE; h evolution.region_heads%ROWTYPE; old evolution.region_commits%ROWTYPE; d bytea := sha256(m); frame evolution.u64; BEGIN IF c.profile <> 6 OR c.commit_sequence = 0 OR c.begin_tick <> c.end_tick OR c.end_tick = 0 OR c.trace_bytes <> 0 THEN RAISE EXCEPTION 'invalid environment profile transition' USING ERRCODE = '22023'; END IF; PERFORM evolution.check_region_lease(c.world_id,c.region_id,c.holder,c.epoch); SELECT * INTO old FROM evolution.region_commits r WHERE r.commit_id = c.commit_id; IF FOUND THEN IF old.manifest <> m OR NOT EXISTS (SELECT 1 FROM evolution.environment_profile_transitions x WHERE x.target_commit = c.commit_id AND x.source_commit = c.predecessor_commit AND x.source_manifest_sha256 = c.predecessor_digest) THEN RAISE EXCEPTION 'environment transition identity reused' USING ERRCODE = 'EVL07'; END IF; SELECT l.frame_id INTO frame FROM evolution.region_leases l WHERE l.world_id = c.world_id AND l.region_id = c.region_id; RETURN QUERY SELECT frame,old.commit_sequence,old.commit_id,encode(old.commit_digest,'hex'),old.end_tick; RETURN; END IF; IF EXISTS (SELECT 1 FROM evolution.population_interval_pending x WHERE x.world_id = c.world_id AND x.region_id = c.region_id) THEN RAISE EXCEPTION 'environment transition waits for interval' USING ERRCODE = 'EVL18'; END IF; SELECT * INTO h FROM evolution.region_heads r WHERE r.world_id = c.world_id AND r.region_id = c.region_id FOR UPDATE; IF NOT FOUND OR h.commit_sequence = 18446744073709551615 OR c.commit_sequence <> h.commit_sequence + 1 OR c.predecessor_commit <> h.commit_id OR c.predecessor_digest <> h.digest OR c.begin_tick <> h.end_tick THEN RAISE EXCEPTION 'environment transition head moved' USING ERRCODE = 'EVL03'; END IF; SELECT * INTO p FROM evolution.region_commits r WHERE r.commit_id = h.commit_id; IF NOT FOUND THEN RAISE EXCEPTION 'missing environment source' USING ERRCODE = 'XX000'; END IF; s := evolution.parse_checkpoint_manifest(p.manifest); IF EXISTS (SELECT 1 FROM evolution.cellular_mutation_reservations x WHERE x.world_id = c.world_id AND x.region_id = c.region_id AND NOT EXISTS (SELECT 1 FROM evolution.cellular_mutation_action_publications a WHERE a.world_id = x.world_id AND a.account_id = x.account_id AND a.action_id = x.action_id)) THEN RAISE EXCEPTION 'pending player mutation blocks environment transition' USING ERRCODE = 'EVL18'; END IF; IF s.profile <> 5 OR s.versions <> c.versions OR s.frame_id <> c.frame_id OR s.tick_microseconds <> c.tick_microseconds OR s.end_tick <> c.end_tick OR NOT EXISTS (SELECT 1 FROM evolution.retained_builds b WHERE b.build_identity = c.build_identity) THEN RAISE EXCEPTION 'unsupported environment source' USING ERRCODE = 'EVL06'; END IF; INSERT INTO evolution.environment_profile_transitions VALUES(c.commit_id,p.commit_id,p.commit_digest,p.payload_sha256); INSERT INTO evolution.region_commits(world_id,region_id,commit_sequence,commit_id,commit_digest,manifest, begin_tick,end_tick,published_epoch,tick_microseconds,versions,build_identity,payload_bytes,payload_sha256) VALUES(c.world_id,c.region_id,c.commit_sequence,c.commit_id,d,m,c.begin_tick,c.end_tick,c.epoch, c.tick_microseconds,c.versions,c.build_identity,c.payload_bytes,c.payload_sha256); UPDATE evolution.region_heads r SET commit_sequence = c.commit_sequence,commit_id = c.commit_id, digest = d,end_tick = c.end_tick,published_epoch = c.epoch WHERE r.world_id = c.world_id AND r.region_id = c.region_id; RETURN QUERY SELECT c.frame_id,c.commit_sequence,c.commit_id,encode(d,'hex'),c.end_tick; END $$; REVOKE ALL ON FUNCTION evolution.publish_environment_profile_transition(bytea) FROM PUBLIC; GRANT EXECUTE ON FUNCTION evolution.publish_environment_profile_transition(bytea) TO :"runtime_role"; CREATE FUNCTION evolution.resolve_environment_commit(w evolution.u64,r evolution.u64,id uuid,m bytea,t bytea) RETURNS TABLE(status text,region_frame evolution.u64,head_sequence evolution.u64,head_commit uuid, head_digest text,head_tick evolution.u64) LANGUAGE plpgsql STABLE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ DECLARE c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(m); result record; BEGIN IF c.world_id <> w OR c.region_id <> r OR c.commit_id <> id OR t IS NULL OR c.profile <> 6 THEN RAISE EXCEPTION 'invalid environment resolution' USING ERRCODE = '22023'; END IF; IF octet_length(t) > 0 THEN PERFORM * FROM evolution.check_environment_trace(m,t); ELSIF c.trace_bytes <> 0 OR c.begin_tick <> c.end_tick THEN RAISE EXCEPTION 'invalid transition resolution' USING ERRCODE = '22023'; END IF; SELECT * INTO result FROM evolution.resolve_region_commit(w,r,id,m,''::bytea); IF result.status = 'committed' AND ((octet_length(t) = 0 AND NOT EXISTS (SELECT 1 FROM evolution.environment_profile_transitions x WHERE x.target_commit = id)) OR (octet_length(t) > 0 AND NOT EXISTS (SELECT 1 FROM evolution.environment_publications x WHERE x.commit_id = id AND x.trace = t))) THEN RETURN QUERY SELECT 'conflict'::text,NULL::evolution.u64,NULL::evolution.u64,NULL::uuid,NULL::text,NULL::evolution.u64; ELSE RETURN QUERY SELECT result.status,result.region_frame,result.head_sequence,result.head_commit,result.head_digest,result.head_tick; END IF; END $$; REVOKE ALL ON FUNCTION evolution.resolve_environment_commit(evolution.u64,evolution.u64,uuid,bytea,bytea) FROM PUBLIC; GRANT EXECUTE ON FUNCTION evolution.resolve_environment_commit(evolution.u64,evolution.u64,uuid,bytea,bytea) TO :"runtime_role"; CREATE FUNCTION evolution.load_environment_evidence(w evolution.u64,r evolution.u64,holder uuid,epoch evolution.u64,id uuid) RETURNS TABLE(trace bytea,source_commit uuid,source_manifest_sha256 bytea,source_payload_sha256 bytea) LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ BEGIN PERFORM evolution.check_region_lease(w,r,holder,epoch); RETURN QUERY SELECT p.trace,NULL::uuid,NULL::bytea,NULL::bytea FROM evolution.environment_publications p JOIN evolution.region_commits c ON c.commit_id = p.commit_id WHERE c.world_id = w AND c.region_id = r AND c.commit_id = id UNION ALL SELECT ''::bytea,p.source_commit,p.source_manifest_sha256,p.source_payload_sha256 FROM evolution.environment_profile_transitions p JOIN evolution.region_commits c ON c.commit_id = p.target_commit WHERE c.world_id = w AND c.region_id = r AND c.commit_id = id; END $$; REVOKE ALL ON FUNCTION evolution.load_environment_evidence(evolution.u64,evolution.u64,uuid,evolution.u64,uuid) FROM PUBLIC; GRANT EXECUTE ON FUNCTION evolution.load_environment_evidence(evolution.u64,evolution.u64,uuid,evolution.u64,uuid) TO :"runtime_role"; INSERT INTO evolution.schema_migrations(version,sha256) VALUES(56,:'migration_sha_56');