ADR-0063: manifest v2 (360 bytes) binds a per-tick validation trace object and a canonical decision block by SHA-256. Publication replays the trace from the committed predecessor's verified payload and requires every report and the candidate bytes, derives deaths deterministically, uploads payload and trace with verified read-back, then sends manifest and decisions to one fenced statement. RegionBatch seals at a terminal transition or 64 ticks and refuses ticks until durable confirmation. Schema v5 records the checkpoint, its decisions and outbox rows atomically; a unique index keeps one terminal outcome per actor (EVL08), triggers make commits/decisions immutable (EVL09), an exact retry of a committed ID returns the original receipt, changed bytes conflict, and read-only resolve_region_commit answers committed/absent/ conflict. Outbox pending/acknowledge give idempotent at-least-once delivery. Recovery reconciles durable deaths with restored lifecycle (and archive deaths for the lineage profile). Validated: Windows CTest 58/58; Linux GCC Debug/Release, clang-tidy and ASan/UBSan CTest 58/58 each; PostgreSQL suites Release, analysis, ASan (lost response, committed-after-timeout, terminated orphan retry, changed bytes, repeated/partial/reordered decisions, gap, outbox redelivery, history quarantine); five mutations each caught. No Docker/Gitea CI, process kill, broker or deployment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
397 lines
21 KiB
PL/PgSQL
397 lines
21 KiB
PL/PgSQL
-- M7.5 durable batch outcomes (ADR-0063). Manifest v2 binds the validation trace
|
|
-- object and the decision block by SHA-256. Publication records the checkpoint,
|
|
-- its permanent decisions and their outbox rows in one statement; an exact retry
|
|
-- of a committed ID returns its receipt, changed bytes conflict. Committed history
|
|
-- is immutable to every caller that does not first disable these triggers.
|
|
|
|
ALTER TABLE evolution.region_commits DROP CONSTRAINT region_commits_manifest_check;
|
|
ALTER TABLE evolution.region_commits
|
|
ADD CONSTRAINT region_commits_manifest_check CHECK (octet_length(manifest) IN (280, 360)),
|
|
ADD COLUMN decisions bytea NOT NULL DEFAULT '\x'::bytea
|
|
CHECK (octet_length(decisions) % 24 = 0 AND octet_length(decisions) <= 192);
|
|
|
|
ALTER TYPE evolution.checkpoint_manifest
|
|
ADD ATTRIBUTE profile integer,
|
|
ADD ATTRIBUTE trace_bytes evolution.u64,
|
|
ADD ATTRIBUTE trace_sha256 bytea,
|
|
ADD ATTRIBUTE decision_count integer,
|
|
ADD ATTRIBUTE decisions_sha256 bytea;
|
|
|
|
-- Permanent outcomes. A death is terminal: one per actor per region, ever.
|
|
CREATE TABLE evolution.region_decisions (
|
|
world_id evolution.u64 NOT NULL,
|
|
region_id evolution.u64 NOT NULL,
|
|
outcome smallint NOT NULL CHECK (outcome = 1),
|
|
actor smallint NOT NULL CHECK (actor IN (0, 1)),
|
|
tick evolution.u64 NOT NULL CHECK (tick > 0),
|
|
organism evolution.u64 NOT NULL,
|
|
commit_sequence evolution.u64 NOT NULL,
|
|
PRIMARY KEY (world_id, region_id, outcome, actor, tick),
|
|
FOREIGN KEY (world_id, region_id, commit_sequence) REFERENCES evolution.region_commits
|
|
);
|
|
CREATE UNIQUE INDEX region_decisions_terminal
|
|
ON evolution.region_decisions (world_id, region_id, outcome, actor) WHERE outcome = 1;
|
|
-- Delivery queue derived from decisions; delivery never mutates simulation state.
|
|
CREATE TABLE evolution.region_outbox (
|
|
world_id evolution.u64 NOT NULL,
|
|
region_id evolution.u64 NOT NULL,
|
|
outcome smallint NOT NULL,
|
|
actor smallint NOT NULL,
|
|
tick evolution.u64 NOT NULL,
|
|
commit_sequence evolution.u64 NOT NULL,
|
|
delivered_at timestamptz,
|
|
PRIMARY KEY (world_id, region_id, outcome, actor, tick),
|
|
FOREIGN KEY (world_id, region_id, outcome, actor, tick) REFERENCES evolution.region_decisions
|
|
);
|
|
REVOKE ALL ON evolution.region_decisions, evolution.region_outbox FROM PUBLIC;
|
|
|
|
-- No GC in initial M7: committed history and outcomes are never rewritten or removed.
|
|
CREATE FUNCTION evolution.refuse_history_rewrite() RETURNS trigger LANGUAGE plpgsql
|
|
SET search_path = pg_catalog, pg_temp AS $$
|
|
BEGIN
|
|
RAISE EXCEPTION 'committed history is immutable' USING ERRCODE = 'EVL09';
|
|
END $$;
|
|
CREATE TRIGGER region_commits_immutable BEFORE UPDATE OR DELETE ON evolution.region_commits
|
|
FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite();
|
|
CREATE TRIGGER region_commits_no_truncate BEFORE TRUNCATE ON evolution.region_commits
|
|
FOR EACH STATEMENT EXECUTE FUNCTION evolution.refuse_history_rewrite();
|
|
CREATE TRIGGER region_decisions_immutable BEFORE UPDATE OR DELETE ON evolution.region_decisions
|
|
FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite();
|
|
CREATE TRIGGER region_decisions_no_truncate BEFORE TRUNCATE ON evolution.region_decisions
|
|
FOR EACH STATEMENT EXECUTE FUNCTION evolution.refuse_history_rewrite();
|
|
-- Outbox rows may only record delivery; their identity and existence are permanent.
|
|
CREATE FUNCTION evolution.refuse_outbox_rewrite() RETURNS trigger LANGUAGE plpgsql
|
|
SET search_path = pg_catalog, pg_temp AS $$
|
|
BEGIN
|
|
IF TG_OP = 'UPDATE' AND (NEW.world_id, NEW.region_id, NEW.outcome, NEW.actor, NEW.tick,
|
|
NEW.commit_sequence)
|
|
IS NOT DISTINCT FROM
|
|
(OLD.world_id, OLD.region_id, OLD.outcome, OLD.actor, OLD.tick,
|
|
OLD.commit_sequence)
|
|
AND OLD.delivered_at IS NULL AND NEW.delivered_at IS NOT NULL THEN
|
|
RETURN NEW;
|
|
END IF;
|
|
RAISE EXCEPTION 'outbox rows only record first delivery' USING ERRCODE = 'EVL09';
|
|
END $$;
|
|
CREATE TRIGGER region_outbox_immutable BEFORE UPDATE OR DELETE ON evolution.region_outbox
|
|
FOR EACH ROW EXECUTE FUNCTION evolution.refuse_outbox_rewrite();
|
|
CREATE TRIGGER region_outbox_no_truncate BEFORE TRUNCATE ON evolution.region_outbox
|
|
FOR EACH STATEMENT EXECUTE FUNCTION evolution.refuse_history_rewrite();
|
|
|
|
-- Manifest v2 only for new records (v1 records stay readable). Mirrors
|
|
-- decode_manifest + supported() in src/persistence/region_checkpoint.cpp.
|
|
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))
|
|
OR substring(m FROM 161 FOR 36) <> '\x020000000200000001000000010000000100000001000000010000000100000001000000'
|
|
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 ELSE 4268144 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.commit_sequence = 0 THEN
|
|
IF substring(m FROM 65 FOR 16) <> zero16 OR r.predecessor_digest <> zero32
|
|
OR r.begin_tick <> 0 OR r.end_tick <> 0 OR substring(m FROM 129 FOR 16) <> zero16
|
|
OR r.epoch <> 0 OR r.trace_bytes <> 0 OR r.trace_sha256 <> zero32
|
|
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;
|
|
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.trace_bytes <> 96 + 304 * (r.end_tick - r.begin_tick) 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 $$;
|
|
|
|
-- Owner-only genesis, now manifest v2 (no batch evidence at sequence 0).
|
|
CREATE OR REPLACE FUNCTION evolution.provision_region_checkpoint(p_manifest bytea)
|
|
RETURNS void LANGUAGE plpgsql SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$
|
|
DECLARE
|
|
g evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(p_manifest);
|
|
d bytea := sha256(p_manifest);
|
|
BEGIN
|
|
IF g.commit_sequence <> 0 THEN
|
|
RAISE EXCEPTION 'genesis manifest required' USING ERRCODE = '22023';
|
|
END IF;
|
|
INSERT INTO evolution.region_leases(world_id, region_id, frame_id)
|
|
VALUES (g.world_id, g.region_id, g.frame_id);
|
|
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 (g.world_id, g.region_id, 0, g.commit_id, d, p_manifest, 0, 0, 0,
|
|
g.tick_microseconds, g.versions, g.build_identity, g.payload_bytes,
|
|
g.payload_sha256);
|
|
INSERT INTO evolution.region_heads(world_id, region_id, commit_sequence, commit_id, digest,
|
|
end_tick, published_epoch)
|
|
VALUES (g.world_id, g.region_id, 0, g.commit_id, d, 0, 0);
|
|
END $$;
|
|
|
|
DROP FUNCTION evolution.publish_region_checkpoint(bytea);
|
|
CREATE FUNCTION evolution.publish_region_checkpoint(p_manifest bytea, p_decisions 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);
|
|
h evolution.region_heads%ROWTYPE;
|
|
p evolution.region_commits%ROWTYPE;
|
|
e evolution.region_commits%ROWTYPE;
|
|
frame evolution.u64;
|
|
d bytea := sha256(p_manifest);
|
|
zero32 constant bytea := decode(repeat('00', 32), 'hex');
|
|
n integer;
|
|
tick numeric;
|
|
actor numeric;
|
|
outcome numeric;
|
|
organism numeric;
|
|
last_tick numeric := -1;
|
|
last_actor numeric := -1;
|
|
actors integer[] := '{}';
|
|
BEGIN
|
|
IF c.commit_sequence = 0 THEN
|
|
RAISE EXCEPTION 'genesis is provisioned, not published' USING ERRCODE = '22023';
|
|
END IF;
|
|
-- The decision block is exactly what the manifest digests (ADR-0063 order/rules).
|
|
n := coalesce(octet_length(p_decisions), -1);
|
|
IF n <> 24 * c.decision_count
|
|
OR (c.decision_count = 0 AND c.decisions_sha256 <> zero32)
|
|
OR (c.decision_count > 0 AND sha256(p_decisions) <> c.decisions_sha256) THEN
|
|
RAISE EXCEPTION 'decisions do not match manifest' USING ERRCODE = '22023';
|
|
END IF;
|
|
FOR i IN 0 .. c.decision_count - 1 LOOP
|
|
tick := evolution.manifest_uint(p_decisions, 24 * i, 8);
|
|
actor := evolution.manifest_uint(p_decisions, 24 * i + 8, 4);
|
|
outcome := evolution.manifest_uint(p_decisions, 24 * i + 12, 4);
|
|
organism := evolution.manifest_uint(p_decisions, 24 * i + 16, 8);
|
|
IF outcome <> 1 OR actor NOT IN (0, 1) OR tick <= c.begin_tick OR tick > c.end_tick
|
|
OR tick < last_tick OR (tick = last_tick AND actor <= last_actor)
|
|
OR actor::integer = ANY (actors)
|
|
OR (c.profile = 1) <> (organism = 0) THEN
|
|
RAISE EXCEPTION 'invalid decision' USING ERRCODE = '22023';
|
|
END IF;
|
|
actors := actors || actor::integer;
|
|
last_tick := tick;
|
|
last_actor := actor;
|
|
END LOOP;
|
|
-- Exact retry of an already committed ID returns its immutable receipt without
|
|
-- mutation or authority; any difference conflicts.
|
|
SELECT * INTO e FROM evolution.region_commits r WHERE r.commit_id = c.commit_id;
|
|
IF FOUND THEN
|
|
IF e.world_id = c.world_id AND e.region_id = c.region_id AND e.manifest = p_manifest
|
|
AND e.decisions = p_decisions THEN
|
|
RETURN QUERY SELECT l.frame_id, e.commit_sequence, e.commit_id,
|
|
encode(e.commit_digest, 'hex'), e.end_tick
|
|
FROM evolution.region_leases l
|
|
WHERE l.world_id = e.world_id AND l.region_id = e.region_id;
|
|
RETURN;
|
|
END IF;
|
|
RAISE EXCEPTION 'commit identity reused' USING ERRCODE = 'EVL07';
|
|
END IF;
|
|
PERFORM evolution.check_region_lease(c.world_id, c.region_id, c.holder, c.epoch);
|
|
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 THEN
|
|
RAISE EXCEPTION 'region not provisioned' USING ERRCODE = 'EVL04';
|
|
END IF;
|
|
IF h.commit_sequence = 18446744073709551615 THEN
|
|
RAISE EXCEPTION 'commit sequences exhausted' USING ERRCODE = 'EVL05';
|
|
END IF;
|
|
IF 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 'region head moved' USING ERRCODE = 'EVL03';
|
|
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;
|
|
IF c.frame_id <> frame THEN
|
|
RAISE EXCEPTION 'foreign frame' USING ERRCODE = '22023';
|
|
END IF;
|
|
SELECT * INTO p FROM evolution.region_commits r
|
|
WHERE r.world_id = c.world_id AND r.region_id = c.region_id
|
|
AND r.commit_sequence = h.commit_sequence;
|
|
IF c.versions <> p.versions OR c.tick_microseconds <> p.tick_microseconds
|
|
OR substring(p_manifest FROM 13 FOR 4) <> substring(p.manifest FROM 13 FOR 4)
|
|
OR substring(p_manifest FROM 229 FOR 12) <> substring(p.manifest FROM 229 FOR 12)
|
|
OR NOT EXISTS (SELECT 1 FROM evolution.retained_builds b
|
|
WHERE b.build_identity = c.build_identity) THEN
|
|
RAISE EXCEPTION 'unsupported checkpoint' USING ERRCODE = 'EVL06';
|
|
END IF;
|
|
-- A final outcome is never repeated: the committed dead stay dead.
|
|
IF EXISTS (SELECT 1 FROM evolution.region_decisions x
|
|
WHERE x.world_id = c.world_id AND x.region_id = c.region_id AND x.outcome = 1
|
|
AND x.actor = ANY (actors)) THEN
|
|
RAISE EXCEPTION 'outcome already final' USING ERRCODE = 'EVL08';
|
|
END IF;
|
|
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, decisions)
|
|
VALUES (c.world_id, c.region_id, c.commit_sequence, c.commit_id, d, p_manifest,
|
|
c.begin_tick, c.end_tick, c.epoch, c.tick_microseconds, c.versions,
|
|
c.build_identity, c.payload_bytes, c.payload_sha256, p_decisions);
|
|
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;
|
|
FOR i IN 0 .. c.decision_count - 1 LOOP
|
|
INSERT INTO evolution.region_decisions(world_id, region_id, outcome, actor, tick, organism,
|
|
commit_sequence)
|
|
VALUES (c.world_id, c.region_id, 1, evolution.manifest_uint(p_decisions, 24 * i + 8, 4),
|
|
evolution.manifest_uint(p_decisions, 24 * i, 8),
|
|
evolution.manifest_uint(p_decisions, 24 * i + 16, 8), c.commit_sequence);
|
|
INSERT INTO evolution.region_outbox(world_id, region_id, outcome, actor, tick,
|
|
commit_sequence)
|
|
VALUES (c.world_id, c.region_id, 1, evolution.manifest_uint(p_decisions, 24 * i + 8, 4),
|
|
evolution.manifest_uint(p_decisions, 24 * i, 8), c.commit_sequence);
|
|
END LOOP;
|
|
RETURN QUERY SELECT frame, c.commit_sequence, c.commit_id, encode(d, 'hex'), c.end_tick;
|
|
END $$;
|
|
|
|
-- Read-only resolution after an ambiguous outcome. 'committed' carries the original
|
|
-- receipt (under any later epoch; it grants no authority), 'conflict' means the ID
|
|
-- holds other bytes or another region, 'absent' proves nothing about the future.
|
|
CREATE FUNCTION evolution.resolve_region_commit(p_world evolution.u64, p_region evolution.u64,
|
|
p_commit uuid, p_manifest bytea, p_decisions 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
|
|
e evolution.region_commits%ROWTYPE;
|
|
BEGIN
|
|
IF p_world IS NULL OR p_region IS NULL OR p_commit IS NULL OR p_manifest IS NULL
|
|
OR p_decisions IS NULL THEN
|
|
RAISE EXCEPTION 'invalid resolution request' USING ERRCODE = '22023';
|
|
END IF;
|
|
SELECT * INTO e FROM evolution.region_commits r WHERE r.commit_id = p_commit;
|
|
IF NOT FOUND THEN
|
|
RETURN QUERY SELECT 'absent'::text, NULL::evolution.u64, NULL::evolution.u64, NULL::uuid,
|
|
NULL::text, NULL::evolution.u64;
|
|
ELSIF e.world_id <> p_world OR e.region_id <> p_region OR e.manifest <> p_manifest
|
|
OR e.decisions <> p_decisions THEN
|
|
RETURN QUERY SELECT 'conflict'::text, NULL::evolution.u64, NULL::evolution.u64, NULL::uuid,
|
|
NULL::text, NULL::evolution.u64;
|
|
ELSE
|
|
RETURN QUERY SELECT 'committed'::text, l.frame_id, e.commit_sequence, e.commit_id,
|
|
encode(e.commit_digest, 'hex'), e.end_tick
|
|
FROM evolution.region_leases l
|
|
WHERE l.world_id = e.world_id AND l.region_id = e.region_id;
|
|
END IF;
|
|
END $$;
|
|
|
|
-- Every committed permanent outcome of a region, for recovery reconciliation.
|
|
CREATE FUNCTION evolution.load_region_decisions(p_world evolution.u64, p_region evolution.u64)
|
|
RETURNS TABLE (commit_sequence evolution.u64, tick evolution.u64, actor smallint,
|
|
outcome smallint, organism evolution.u64)
|
|
LANGUAGE sql STABLE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
|
SELECT x.commit_sequence, x.tick, x.actor, x.outcome, x.organism
|
|
FROM evolution.region_decisions x
|
|
WHERE x.world_id = p_world AND x.region_id = p_region
|
|
ORDER BY x.tick, x.actor
|
|
LIMIT 64;
|
|
$$;
|
|
|
|
CREATE FUNCTION evolution.pending_region_outbox(p_world evolution.u64, p_region evolution.u64,
|
|
p_limit integer)
|
|
RETURNS TABLE (commit_sequence evolution.u64, tick evolution.u64, actor smallint,
|
|
outcome smallint, organism evolution.u64)
|
|
LANGUAGE plpgsql STABLE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
|
BEGIN
|
|
IF p_limit IS NULL OR p_limit NOT BETWEEN 1 AND 64 THEN
|
|
RAISE EXCEPTION 'invalid outbox limit' USING ERRCODE = '22023';
|
|
END IF;
|
|
RETURN QUERY SELECT o.commit_sequence, o.tick, o.actor, o.outcome, x.organism
|
|
FROM evolution.region_outbox o
|
|
JOIN evolution.region_decisions x USING (world_id, region_id, outcome, actor, tick)
|
|
WHERE o.world_id = p_world AND o.region_id = p_region AND o.delivered_at IS NULL
|
|
ORDER BY o.commit_sequence, o.tick, o.actor
|
|
LIMIT p_limit;
|
|
END $$;
|
|
|
|
-- Idempotent: true the first time a row is marked delivered, false afterwards.
|
|
CREATE FUNCTION evolution.acknowledge_region_outbox(p_world evolution.u64,
|
|
p_region evolution.u64, p_outcome integer, p_actor integer, p_tick evolution.u64)
|
|
RETURNS boolean LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
|
|
BEGIN
|
|
UPDATE evolution.region_outbox o SET delivered_at = clock_timestamp()
|
|
WHERE o.world_id = p_world AND o.region_id = p_region AND o.outcome = p_outcome
|
|
AND o.actor = p_actor AND o.tick = p_tick AND o.delivered_at IS NULL;
|
|
IF FOUND THEN
|
|
RETURN true;
|
|
END IF;
|
|
IF EXISTS (SELECT 1 FROM evolution.region_outbox o
|
|
WHERE o.world_id = p_world AND o.region_id = p_region AND o.outcome = p_outcome
|
|
AND o.actor = p_actor AND o.tick = p_tick) THEN
|
|
RETURN false;
|
|
END IF;
|
|
RAISE EXCEPTION 'unknown outbox row' USING ERRCODE = '22023';
|
|
END $$;
|
|
|
|
REVOKE ALL ON FUNCTION
|
|
evolution.refuse_history_rewrite(),
|
|
evolution.refuse_outbox_rewrite(),
|
|
evolution.publish_region_checkpoint(bytea, bytea),
|
|
evolution.resolve_region_commit(evolution.u64, evolution.u64, uuid, bytea, bytea),
|
|
evolution.load_region_decisions(evolution.u64, evolution.u64),
|
|
evolution.pending_region_outbox(evolution.u64, evolution.u64, integer),
|
|
evolution.acknowledge_region_outbox(evolution.u64, evolution.u64, integer, integer,
|
|
evolution.u64)
|
|
FROM PUBLIC;
|
|
GRANT EXECUTE ON FUNCTION
|
|
evolution.publish_region_checkpoint(bytea, bytea),
|
|
evolution.resolve_region_commit(evolution.u64, evolution.u64, uuid, bytea, bytea),
|
|
evolution.load_region_decisions(evolution.u64, evolution.u64),
|
|
evolution.pending_region_outbox(evolution.u64, evolution.u64, integer),
|
|
evolution.acknowledge_region_outbox(evolution.u64, evolution.u64, integer, integer,
|
|
evolution.u64)
|
|
TO :"runtime_role";
|
|
INSERT INTO evolution.schema_migrations(version, sha256) VALUES (5, :'migration_sha_5');
|