feat: durable mutation queue (schema 59)
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m24s
Foundation / postgres-tools (push) Successful in 1m31s
Foundation / server-image (push) Successful in 4m25s
Foundation / image (push) Successful in 4m50s
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m24s
Foundation / postgres-tools (push) Successful in 1m31s
Foundation / server-image (push) Successful in 4m25s
Foundation / image (push) Successful in 4m50s
A mutation choice's reserved tick is now the earliest tick it may bind at, not the only one (ADR-0121 amendment). With write-behind commits, a choice reserved for head + 1 landed in ticks already being simulated or committed: the batch was refused, or the reservation was orphaned for a past tick and stalled the region. Schema 59: a bound record carries the tick it applied at and the rest of its input must equal the reservation's; the "omits a reserved mutation" refusals are removed. Existing rows are unchanged, so an orphaned reservation binds at the next commit that carries it. The host keeps every unbound reservation queued and the open batch binds those the commit in flight does not carry, at its next tick; the earlier hold on new choices is removed. A choice that outlives its life binds as life_died (owner, trace matching and recovery accept it when no life with that ID remains). Recovery takes a bound choice's tick from its trace. Tests: a choice skipped by one checkpoint binds a tick late and recovers; builder late binding, refusal before the earliest tick, and a departed life's life_died outcome. Full local PostgreSQL qualification passes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
-- Durable mutation queue (ADR-0121 amendment, 2026-10-22). A reservation's tick is the
|
||||
-- earliest tick its choice may apply at, not the only one: the region binds a queued choice
|
||||
-- at a tick it simulates after admitting it, so a batch that was already being simulated or
|
||||
-- committed when the choice arrived cannot orphan it. A bound record carries the tick it
|
||||
-- applied at; the rest of its input must equal the reservation's. The exact-tick refusals of
|
||||
-- schema 35 ("omits a reserved mutation") are removed: an unbound reservation simply waits.
|
||||
-- Stored rows are unchanged, so a reservation whose tick has already passed is bound by the
|
||||
-- next commit that carries it.
|
||||
|
||||
CREATE OR REPLACE FUNCTION evolution.check_cellular_mutation_block(p_manifest bytea, p_trace bytea,
|
||||
p_mutations bytea, p_results bytea)
|
||||
RETURNS integer LANGUAGE plpgsql STABLE SECURITY INVOKER
|
||||
SET search_path = pg_catalog, pg_temp AS $$
|
||||
DECLARE
|
||||
c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(p_manifest);
|
||||
n integer;
|
||||
account uuid;
|
||||
account_bytes bytea;
|
||||
action numeric;
|
||||
tick numeric;
|
||||
input bytea;
|
||||
previous_tick numeric := -1;
|
||||
previous_account bytea := ''::bytea;
|
||||
previous_action numeric := -1;
|
||||
applied numeric;
|
||||
r evolution.cellular_mutation_reservations%ROWTYPE;
|
||||
BEGIN
|
||||
IF p_mutations IS NULL OR p_results IS NULL OR octet_length(p_mutations) = 0
|
||||
OR octet_length(p_mutations) % 64 <> 0 OR octet_length(p_mutations) / 64 > 1024
|
||||
OR octet_length(p_results) <> octet_length(p_mutations) / 64 THEN
|
||||
RAISE EXCEPTION 'invalid cellular mutation block' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
n := octet_length(p_mutations) / 64;
|
||||
IF (SELECT count(*) FROM evolution.cellular_mutation_selections(p_trace, p_manifest)) <> n THEN
|
||||
RAISE EXCEPTION 'cellular mutation block does not cover the trace' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
FOR i IN 0 .. n - 1 LOOP
|
||||
account_bytes := substring(p_mutations FROM 64 * i + 1 FOR 16);
|
||||
account := encode(account_bytes, 'hex')::uuid;
|
||||
action := evolution.manifest_uint(p_mutations, 64 * i + 16, 8);
|
||||
input := substring(p_mutations FROM 64 * i + 25 FOR 34);
|
||||
tick := evolution.manifest_uint(input, 0, 8);
|
||||
IF account = '00000000-0000-0000-0000-000000000000' OR action = 0
|
||||
OR NOT evolution.valid_cellular_mutation_input(input)
|
||||
OR substring(p_mutations FROM 64 * i + 59 FOR 6) <> '\x000000000000'
|
||||
OR (c.versions IN ('\x0a0000000800000002000000010000000100000004000000030000000200000001000000'::bytea,
|
||||
'\x0b0000000900000002000000010000000100000005000000040000000200000001000000'::bytea,
|
||||
'\x0c0000000a00000002000000010000000100000006000000050000000200000001000000'::bytea)
|
||||
AND (get_byte(p_results,i) NOT IN (1,3,4)
|
||||
OR (evolution.manifest_uint(input,32,2)=0 AND get_byte(p_results,i) NOT IN (1,3))
|
||||
OR (evolution.manifest_uint(input,32,2)<>0 AND get_byte(p_results,i) NOT IN (1,4))))
|
||||
OR (c.versions NOT IN ('\x0a0000000800000002000000010000000100000004000000030000000200000001000000'::bytea,
|
||||
'\x0b0000000900000002000000010000000100000005000000040000000200000001000000'::bytea,
|
||||
'\x0c0000000a00000002000000010000000100000006000000050000000200000001000000'::bytea)
|
||||
AND get_byte(p_results,i)>3)
|
||||
OR tick <= c.begin_tick OR tick > c.end_tick
|
||||
OR (tick, account_bytes, action) <= (previous_tick, previous_account, previous_action) THEN
|
||||
RAISE EXCEPTION 'invalid cellular mutation record' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
previous_tick := tick;
|
||||
previous_account := account_bytes;
|
||||
previous_action := action;
|
||||
SELECT * INTO r FROM evolution.cellular_mutation_reservations x
|
||||
WHERE x.world_id = c.world_id AND x.account_id = account AND x.action_id = action;
|
||||
-- The reservation's tick is the earliest the choice may apply at; the record carries
|
||||
-- the tick it applied at, and the rest of the input must match exactly.
|
||||
applied := tick;
|
||||
IF NOT FOUND OR r.region_id <> c.region_id
|
||||
OR substring(r.input_bytes FROM 9) <> substring(input FROM 9)
|
||||
OR r.target_tick <> evolution.manifest_uint(r.input_bytes, 0, 8)
|
||||
OR applied < r.target_tick THEN
|
||||
RAISE EXCEPTION 'cellular mutation reservation conflicts' USING ERRCODE = 'EVL16';
|
||||
END IF;
|
||||
IF NOT EXISTS (SELECT 1 FROM evolution.cellular_mutation_selections(p_trace, p_manifest) s
|
||||
WHERE s.tick = applied AND s.successor_identity = r.successor_identity
|
||||
AND s.life_id = evolution.manifest_uint(input, 8, 8)
|
||||
AND s.event_index = evolution.manifest_uint(input, 16, 8)
|
||||
AND s.offer_tick = evolution.manifest_uint(input, 24, 8)
|
||||
AND s.operator_id = evolution.manifest_uint(input, 32, 2)) THEN
|
||||
RAISE EXCEPTION 'cellular mutation differs from its trace' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
END LOOP;
|
||||
RETURN n;
|
||||
END $$;
|
||||
|
||||
-- Schema 35's ordinary publisher without the exact-tick refusal.
|
||||
CREATE OR REPLACE 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 $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM evolution.cellular_mutation_selections(p_trace, p_manifest)) THEN
|
||||
RAISE EXCEPTION 'cellular mutation selections need their action block' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
RETURN QUERY SELECT * FROM evolution.publish_cellular_birth_checkpoint(
|
||||
p_manifest, p_decisions, p_actions, p_trace);
|
||||
END $$;
|
||||
|
||||
-- Schema 35's mutation publisher without the exact-tick refusal.
|
||||
CREATE OR REPLACE FUNCTION evolution.publish_cellular_mutation_checkpoint(p_manifest bytea,
|
||||
p_decisions bytea, p_actions bytea, p_trace bytea, p_mutations bytea, p_results 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);
|
||||
n integer;
|
||||
h record;
|
||||
digest bytea;
|
||||
BEGIN
|
||||
IF c.profile <> 4 OR c.commit_sequence = 0 THEN
|
||||
RAISE EXCEPTION 'invalid cellular publication' USING ERRCODE = '22023';
|
||||
END IF;
|
||||
n := evolution.check_cellular_mutation_block(p_manifest, p_trace, p_mutations, p_results);
|
||||
IF EXISTS (SELECT 1 FROM evolution.region_commits x WHERE x.commit_id = c.commit_id) THEN
|
||||
IF NOT evolution.cellular_mutation_publications_match(c.world_id, c.commit_id, p_mutations,
|
||||
p_results) THEN
|
||||
RAISE EXCEPTION 'cellular mutation evidence differs on retry' USING ERRCODE = 'EVL07';
|
||||
END IF;
|
||||
RETURN QUERY SELECT * FROM evolution.publish_cellular_birth_checkpoint(
|
||||
p_manifest, p_decisions, p_actions, p_trace);
|
||||
RETURN;
|
||||
END IF;
|
||||
-- A choice binds to one commit only; another commit's binding is a reused identity.
|
||||
FOR i IN 0 .. n - 1 LOOP
|
||||
IF EXISTS (SELECT 1 FROM evolution.cellular_mutation_action_publications p
|
||||
WHERE p.world_id = c.world_id AND p.commit_id <> c.commit_id
|
||||
AND p.account_id = encode(substring(p_mutations FROM 64 * i + 1 FOR 16), 'hex')::uuid
|
||||
AND p.action_id = evolution.manifest_uint(p_mutations, 64 * i + 16, 8)) THEN
|
||||
RAISE EXCEPTION 'cellular mutation already published' USING ERRCODE = 'EVL07';
|
||||
END IF;
|
||||
END LOOP;
|
||||
-- The birth publisher takes the lease lock and rechecks an uncertain first attempt.
|
||||
SELECT * INTO h FROM evolution.publish_cellular_birth_checkpoint(
|
||||
p_manifest, p_decisions, p_actions, p_trace);
|
||||
IF NOT FOUND THEN
|
||||
RAISE EXCEPTION 'publication returned no head' USING ERRCODE = 'XX000';
|
||||
END IF;
|
||||
IF evolution.cellular_mutation_publications_match(c.world_id, c.commit_id, p_mutations,
|
||||
p_results) THEN
|
||||
RETURN QUERY SELECT h.region_frame, h.head_sequence, h.head_commit, h.head_digest,
|
||||
h.head_tick;
|
||||
RETURN;
|
||||
END IF;
|
||||
SELECT x.commit_digest INTO digest FROM evolution.region_commits x
|
||||
WHERE x.commit_id = c.commit_id;
|
||||
FOR i IN 0 .. n - 1 LOOP
|
||||
INSERT INTO evolution.cellular_mutation_action_publications(world_id, region_id,
|
||||
account_id, action_id, commit_id, commit_digest, commit_sequence, result)
|
||||
VALUES (c.world_id, c.region_id,
|
||||
encode(substring(p_mutations FROM 64 * i + 1 FOR 16), 'hex')::uuid,
|
||||
evolution.manifest_uint(p_mutations, 64 * i + 16, 8), c.commit_id, digest,
|
||||
c.commit_sequence, get_byte(p_results, i));
|
||||
END LOOP;
|
||||
RETURN QUERY SELECT h.region_frame, h.head_sequence, h.head_commit, h.head_digest, h.head_tick;
|
||||
END $$;
|
||||
|
||||
INSERT INTO evolution.schema_migrations(version,sha256) VALUES(59,:'migration_sha_59');
|
||||
Reference in New Issue
Block a user