Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m54s
Foundation / checks (client) (push) Successful in 12m21s
Foundation / checks (network) (push) Successful in 13m52s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 2m34s
Foundation / checks (postgres) (push) Canceled after 6m8s
Foundation / checks (checks) (push) Canceled after 18m34s
305 lines
17 KiB
PL/PgSQL
305 lines
17 KiB
PL/PgSQL
-- 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');
|