Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 43s
Foundation / checks (ecs) (push) Successful in 1m52s
Foundation / checks (postgres) (push) Failing after 52s
Foundation / checks (tsan) (push) Failing after 32s
Foundation / checks (network) (push) Failing after 3m4s
Foundation / checks (client) (push) Failing after 4m24s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
208 lines
12 KiB
PL/PgSQL
208 lines
12 KiB
PL/PgSQL
-- M11.3d-7i, ADR-0114: bind cellular birth trace rows and life outcomes atomically.
|
|
CREATE FUNCTION evolution.cellular_births(p_trace bytea, p_manifest bytea)
|
|
RETURNS TABLE(account_id uuid, request_id evolution.u64, generation evolution.u64,
|
|
identity 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);
|
|
ticks numeric;
|
|
tick_index integer;
|
|
intent_count numeric;
|
|
birth_count numeric;
|
|
off integer := 112;
|
|
account bytea;
|
|
previous_account bytea;
|
|
previous_request numeric;
|
|
BEGIN
|
|
IF c.profile <> 4 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) <> '\x45564c4342543031'
|
|
OR evolution.manifest_uint(p_trace, 8, 4) <> 2
|
|
OR evolution.manifest_uint(p_trace, 12, 4) <> 4
|
|
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 cellular birth trace binding' USING ERRCODE = '22023';
|
|
END IF;
|
|
ticks := c.end_tick - c.begin_tick;
|
|
IF ticks < 1 OR ticks > 64 OR evolution.manifest_uint(p_trace, 104, 4) <> ticks
|
|
OR evolution.manifest_uint(p_trace, 108, 4) <> 0 THEN
|
|
RAISE EXCEPTION 'invalid cellular birth trace range' USING ERRCODE = '22023';
|
|
END IF;
|
|
FOR tick_index IN 0 .. ticks::integer - 1 LOOP
|
|
IF off + 8 > octet_length(p_trace) THEN
|
|
RAISE EXCEPTION 'truncated cellular tick header' USING ERRCODE = '22023';
|
|
END IF;
|
|
intent_count := evolution.manifest_uint(p_trace, off, 4);
|
|
birth_count := evolution.manifest_uint(p_trace, off + 4, 4);
|
|
IF intent_count > 256 OR birth_count > 256
|
|
OR off + 8 + intent_count * 24 + birth_count * 40 > octet_length(p_trace) THEN
|
|
RAISE EXCEPTION 'invalid cellular tick counts' USING ERRCODE = '22023';
|
|
END IF;
|
|
off := off + 8 + intent_count::integer * 24;
|
|
previous_account := ''::bytea;
|
|
previous_request := -1;
|
|
FOR birth_index IN 0 .. birth_count::integer - 1 LOOP
|
|
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);
|
|
generation := evolution.manifest_uint(p_trace, off + 24, 8);
|
|
identity := evolution.manifest_uint(p_trace, off + 32, 8);
|
|
born := c.begin_tick + tick_index + 1;
|
|
IF account_id = '00000000-0000-0000-0000-000000000000' OR request_id = 0
|
|
OR generation = 0 OR identity = 0
|
|
OR (account, request_id) <= (previous_account, previous_request) THEN
|
|
RAISE EXCEPTION 'invalid cellular birth record' USING ERRCODE = '22023';
|
|
END IF;
|
|
previous_account := account;
|
|
previous_request := request_id;
|
|
off := off + 40;
|
|
RETURN NEXT;
|
|
END LOOP;
|
|
END LOOP;
|
|
IF off <> octet_length(p_trace) THEN
|
|
RAISE EXCEPTION 'trailing cellular trace bytes' USING ERRCODE = '22023';
|
|
END IF;
|
|
END $$;
|
|
|
|
CREATE FUNCTION evolution.cellular_birth_bindings_match(p_world evolution.u64,
|
|
p_commit uuid, p_trace bytea, p_manifest bytea)
|
|
RETURNS boolean LANGUAGE sql STABLE SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$
|
|
SELECT NOT EXISTS (
|
|
SELECT 1 FROM evolution.cellular_births(p_trace, p_manifest) b
|
|
LEFT JOIN evolution.life_reservations r ON r.world_id = p_world
|
|
AND r.account_id = b.account_id AND r.request_id = b.request_id
|
|
LEFT JOIN evolution.primitive_life_bindings x ON x.world_id = p_world
|
|
AND x.account_id = b.account_id AND x.request_id = b.request_id
|
|
WHERE r.identity IS DISTINCT FROM b.identity OR r.region_id IS DISTINCT FROM
|
|
evolution.manifest_uint(p_manifest, 24, 8)
|
|
OR x.commit_id IS DISTINCT FROM p_commit OR x.born IS DISTINCT FROM b.born)
|
|
AND (SELECT count(*) FROM evolution.primitive_life_bindings x
|
|
WHERE x.world_id = p_world AND x.commit_id = p_commit) =
|
|
(SELECT count(*) FROM evolution.cellular_births(p_trace, p_manifest));
|
|
$$;
|
|
|
|
-- The original three-argument function remains an internal action-only core.
|
|
-- Runtime callers must provide the exact format-2 trace as birth evidence.
|
|
REVOKE ALL ON FUNCTION evolution.publish_cellular_checkpoint(bytea,bytea,bytea),
|
|
evolution.resolve_cellular_region_commit(evolution.u64,evolution.u64,uuid,bytea,bytea,bytea)
|
|
FROM PUBLIC, :"runtime_role";
|
|
|
|
CREATE FUNCTION evolution.publish_cellular_checkpoint(p_manifest bytea,
|
|
p_decisions bytea, p_actions bytea, p_trace bytea)
|
|
RETURNS TABLE(region_frame evolution.u64, head_sequence evolution.u64, head_commit uuid,
|
|
head_digest text, head_tick evolution.u64)
|
|
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
|
DECLARE
|
|
c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(p_manifest);
|
|
b record;
|
|
r evolution.life_reservations%ROWTYPE;
|
|
BEGIN
|
|
IF c.profile <> 4 OR c.commit_sequence = 0 OR p_actions IS NULL THEN
|
|
RAISE EXCEPTION 'invalid cellular publication' USING ERRCODE = '22023';
|
|
END IF;
|
|
PERFORM evolution.check_cellular_action_block(p_actions, c.begin_tick, c.end_tick);
|
|
PERFORM * FROM evolution.cellular_births(p_trace, p_manifest);
|
|
IF EXISTS (SELECT b.account_id FROM evolution.cellular_births(p_trace, p_manifest) b
|
|
GROUP BY b.account_id HAVING count(*) > 1)
|
|
OR EXISTS (SELECT b.identity FROM evolution.cellular_births(p_trace, p_manifest) b
|
|
GROUP BY b.identity HAVING count(*) > 1) THEN
|
|
RAISE EXCEPTION 'duplicate cellular birth identity' USING ERRCODE = '22023';
|
|
END IF;
|
|
PERFORM evolution.check_region_lease(c.world_id, c.region_id, c.holder, c.epoch);
|
|
-- The lease lock fences competing commits. Exact retries are accepted only
|
|
-- when the immutable binding rows agree with every birth in the trace.
|
|
IF EXISTS (SELECT 1 FROM evolution.region_commits x WHERE x.commit_id = c.commit_id) THEN
|
|
IF NOT evolution.cellular_birth_bindings_match(c.world_id, c.commit_id, p_trace, p_manifest) THEN
|
|
RAISE EXCEPTION 'cellular birth evidence differs on retry' USING ERRCODE = 'EVL07';
|
|
END IF;
|
|
RETURN QUERY SELECT * FROM evolution.publish_cellular_checkpoint(
|
|
p_manifest, p_decisions, p_actions);
|
|
RETURN;
|
|
END IF;
|
|
-- Lock accounts in stable order and require the exact current control generation
|
|
-- and previously allocated request identity before inserting any binding.
|
|
FOR b IN SELECT * FROM evolution.cellular_births(p_trace, p_manifest) ORDER BY account_id LOOP
|
|
PERFORM evolution.check_life_control(c.world_id, c.region_id, c.holder, c.epoch,
|
|
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 'cellular birth reservation conflicts' USING ERRCODE = 'EVL16';
|
|
END IF;
|
|
END LOOP;
|
|
FOR b IN SELECT * FROM evolution.cellular_births(p_trace, p_manifest) ORDER BY account_id LOOP
|
|
PERFORM evolution.check_life_control(c.world_id, c.region_id, c.holder, c.epoch,
|
|
b.account_id, b.generation);
|
|
END LOOP;
|
|
INSERT INTO evolution.primitive_life_bindings(world_id, account_id, request_id, born, commit_id)
|
|
SELECT c.world_id, b.account_id, b.request_id, b.born, c.commit_id
|
|
FROM evolution.cellular_births(p_trace, p_manifest) b;
|
|
RETURN QUERY SELECT * FROM evolution.publish_cellular_checkpoint(
|
|
p_manifest, p_decisions, p_actions);
|
|
END $$;
|
|
|
|
CREATE FUNCTION evolution.resolve_cellular_region_commit(p_world evolution.u64,
|
|
p_region evolution.u64, p_commit uuid, p_manifest bytea, p_decisions bytea,
|
|
p_actions bytea, p_trace 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(p_manifest);
|
|
BEGIN
|
|
IF c.world_id <> p_world OR c.region_id <> p_region OR c.commit_id <> p_commit
|
|
OR p_actions IS NULL THEN
|
|
RAISE EXCEPTION 'invalid cellular resolution request' USING ERRCODE = '22023';
|
|
END IF;
|
|
PERFORM evolution.check_cellular_action_block(p_actions, c.begin_tick, c.end_tick);
|
|
PERFORM * FROM evolution.cellular_births(p_trace, p_manifest);
|
|
IF EXISTS (SELECT 1 FROM evolution.region_commits x WHERE x.commit_id = p_commit)
|
|
AND NOT evolution.cellular_birth_bindings_match(p_world, p_commit, p_trace, p_manifest) THEN
|
|
RETURN QUERY SELECT 'conflict'::text, NULL::evolution.u64, NULL::evolution.u64,
|
|
NULL::uuid, NULL::text, NULL::evolution.u64;
|
|
RETURN;
|
|
END IF;
|
|
RETURN QUERY SELECT * FROM evolution.resolve_cellular_region_commit(
|
|
p_world, p_region, p_commit, p_manifest, p_decisions, p_actions);
|
|
END $$;
|
|
|
|
CREATE FUNCTION evolution.load_cellular_publication_births(p_world evolution.u64,
|
|
p_region evolution.u64, p_holder uuid, p_epoch evolution.u64, p_commit uuid)
|
|
RETURNS TABLE(account_id uuid, request_id evolution.u64, identity evolution.u64,
|
|
born evolution.u64, commit_sequence evolution.u64)
|
|
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
|
BEGIN
|
|
PERFORM evolution.check_region_lease(p_world, p_region, p_holder, p_epoch);
|
|
IF NOT EXISTS (SELECT 1 FROM evolution.region_commits c
|
|
JOIN evolution.cellular_publications p USING (commit_id)
|
|
WHERE c.world_id = p_world AND c.region_id = p_region AND c.commit_id = p_commit
|
|
AND c.commit_sequence > 0 AND evolution.manifest_uint(c.manifest, 12, 4) = 4) THEN
|
|
RAISE EXCEPTION 'cellular publication evidence missing' USING ERRCODE = 'EVL06';
|
|
END IF;
|
|
RETURN QUERY SELECT b.account_id, b.request_id, r.identity, b.born, 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 AND b.commit_id = p_commit
|
|
ORDER BY b.born, b.account_id, b.request_id;
|
|
END $$;
|
|
|
|
REVOKE ALL ON FUNCTION evolution.cellular_births(bytea,bytea),
|
|
evolution.cellular_birth_bindings_match(evolution.u64,uuid,bytea,bytea),
|
|
evolution.publish_cellular_checkpoint(bytea,bytea,bytea,bytea),
|
|
evolution.resolve_cellular_region_commit(evolution.u64,evolution.u64,uuid,bytea,bytea,bytea,bytea),
|
|
evolution.load_cellular_publication_births(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)
|
|
FROM PUBLIC;
|
|
GRANT EXECUTE ON FUNCTION evolution.publish_cellular_checkpoint(bytea,bytea,bytea,bytea),
|
|
evolution.resolve_cellular_region_commit(evolution.u64,evolution.u64,uuid,bytea,bytea,bytea,bytea),
|
|
evolution.load_cellular_publication_births(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)
|
|
TO :"runtime_role";
|
|
INSERT INTO evolution.schema_migrations(version, sha256) VALUES (23, :'migration_sha_23');
|