Files
aevolution/migrations/0060_cellular_revamp_bundle.sql
dingoandClaude Opus 5.5 4bc5d5eee7
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m29s
Foundation / postgres-tools (push) Successful in 24s
Foundation / server-image (push) Successful in 4m33s
Foundation / image (push) Successful in 4m59s
feat(cellular): session v14, bundle 15 and rules-13 wildlife
Session v14 carries the rules-13 revamp: a body block in the owner's view
(members, emergence threshold, exclusions, Emerge open, payable energy,
mass and the 20 traits), become, emerge and channel exclusions on actions,
become on mutation choices, catalog offers (id, chance, cost) and the
emerged ending. Older sessions never see a v14 field. The client shows
Stay / Become offspring offers named from the shared catalog, continuing
as a bud, the organism and its Emerge button, kept channels, the body and
the emerged end screen.

Bundle 15 (rules 13 on owner format 11) is durable: schema 60 admits it
raw-only, stores become as bit 15 of the choice's operator, widens the
action input and mutation results, and publishes the 14 -> 15 revamp
profile migration. evo-world-clone opens cellular-v10 (cellular-v9 plus the
revamp upgrade from --revamp-tuning), resumable and recoverable.

Wildlife counts reserve matter as energy, waits for the bud fee, grows
organisms one short of emerging, and hunts by the region step's rules-13
engulf test. The play summary adds organisms, offers and the spent pool.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 07:31:32 -04:00

324 lines
18 KiB
PL/PgSQL

-- Rules 13 (ADR-0179, Cellular_Revamp_Build_Plan.md): raw-only bundle 15 (rules 13 on owner
-- format 11: the cellular revamp) and exact bundle 14 -> 15 upgrades declaring the region's
-- revamp rules. Normalization is parser-local; stored evidence and retries retain real bytes.
-- Raw trace 4 is unchanged except that a bundle-15 mutation selection may set byte 34 (become).
-- The canonical cellular action input: byte 13 bit 1 become (with a bud) and bit 2 emerge (no
-- target); byte 14 the matter channels excluded from burning. Widening cannot invalidate an
-- existing row; earlier inputs keep both zero.
CREATE OR REPLACE FUNCTION evolution.valid_cellular_action_input(p_input bytea, p_tick evolution.u64)
RETURNS boolean LANGUAGE plpgsql IMMUTABLE STRICT
SET search_path = pg_catalog, pg_temp AS $$
BEGIN
IF octet_length(p_input) <> 24 THEN
RETURN false;
END IF;
IF evolution.manifest_uint(p_input, 0, 8) = 0
OR evolution.manifest_uint(p_input, 0, 8) <> p_tick OR get_byte(p_input, 12) > 3
OR get_byte(p_input, 13) > 7 OR get_byte(p_input, 14) > 15 OR get_byte(p_input, 15) <> 0
OR (get_byte(p_input, 13) & 2 <> 0 AND get_byte(p_input, 12) <> 3)
OR (get_byte(p_input, 13) & 4 <> 0 AND get_byte(p_input, 12) <> 0) THEN
RETURN false;
END IF;
IF get_byte(p_input, 12) <> 1 AND substring(p_input FROM 17 FOR 8) <> '\x0000000000000000' THEN
RETURN false;
END IF;
RETURN true;
END $$;
ALTER FUNCTION evolution.parse_checkpoint_manifest(bytea) RENAME TO parse_checkpoint_manifest_v59;
REVOKE ALL ON FUNCTION evolution.parse_checkpoint_manifest_v59(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 c evolution.checkpoint_manifest;
b14 constant bytea := '\x0c0000000a00000002000000010000000100000006000000050000000200000001000000';
b15 constant bytea := '\x0d0000000b00000002000000010000000100000007000000060000000200000001000000';
BEGIN
IF m IS NOT NULL AND octet_length(m)=360 AND evolution.manifest_uint(m,12,4)=4
AND substring(m FROM 161 FOR 36)=b15 THEN
c := evolution.parse_checkpoint_manifest_v59(overlay(m PLACING b14 FROM 161 FOR 36));
c.versions := b15;
RETURN c;
END IF;
RETURN evolution.parse_checkpoint_manifest_v59(m);
END $$;
REVOKE ALL ON FUNCTION evolution.parse_checkpoint_manifest(bytea) FROM PUBLIC;
ALTER FUNCTION evolution.cellular_births(bytea,bytea) RENAME TO cellular_births_v59;
REVOKE ALL ON FUNCTION evolution.cellular_births_v59(bytea,bytea) FROM PUBLIC, :"runtime_role";
CREATE FUNCTION evolution.cellular_births(t bytea,m bytea)
RETURNS TABLE(account_id uuid,request_id evolution.u64,generation evolution.u64,identity evolution.u64,born evolution.u64)
LANGUAGE plpgsql IMMUTABLE SET search_path=pg_catalog,pg_temp AS $$
DECLARE c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(m);
BEGIN
IF c.versions='\x0d0000000b00000002000000010000000100000007000000060000000200000001000000'::bytea THEN
m := overlay(m PLACING '\x0c0000000a00000002000000010000000100000006000000050000000200000001000000'::bytea FROM 161 FOR 36);
END IF;
RETURN QUERY SELECT * FROM evolution.cellular_births_v59(t,m);
END $$;
REVOKE ALL ON FUNCTION evolution.cellular_births(bytea,bytea) FROM PUBLIC;
-- Results 5 split applied and 6 split refused (rules 13).
ALTER TABLE evolution.cellular_mutation_action_publications
DROP CONSTRAINT cellular_mutation_action_publications_result_check;
ALTER TABLE evolution.cellular_mutation_action_publications
ADD CONSTRAINT cellular_mutation_action_publications_result_check CHECK(result BETWEEN 0 AND 6);
-- A bundle-15 selection's byte 34 is its become flag (0 or 1). The returned operator carries
-- it as bit 15, exactly as the reservation input stores it, so the block check compares them.
CREATE OR REPLACE FUNCTION evolution.cellular_mutation_selections(p_trace bytea, p_manifest bytea)
RETURNS TABLE(tick evolution.u64, life_id evolution.u64, event_index evolution.u64,
offer_tick evolution.u64, successor_identity evolution.u64, operator_id integer)
LANGUAGE plpgsql IMMUTABLE SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$
DECLARE
c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(p_manifest);
revamp boolean := c.versions =
'\x0d0000000b00000002000000010000000100000007000000060000000200000001000000'::bytea;
ticks integer;
off integer := 112;
intent_count integer;
birth_count integer;
mutation_count integer;
previous_life numeric;
become integer;
BEGIN
PERFORM * FROM evolution.cellular_births(p_trace, p_manifest); -- Binding and structure.
IF evolution.manifest_uint(p_trace, 8, 4) NOT IN (3,4) THEN
RETURN; -- Format 2 carries no selections.
END IF;
ticks := (c.end_tick - c.begin_tick)::integer;
FOR tick_index IN 0 .. ticks - 1 LOOP
intent_count := evolution.manifest_uint(p_trace, off, 4)::integer;
birth_count := evolution.manifest_uint(p_trace, off + 4, 4)::integer;
mutation_count := evolution.manifest_uint(p_trace, off + 8, 4)::integer;
off := off + 16 + intent_count * 24 + birth_count * 40;
previous_life := 0;
FOR mutation_index IN 0 .. mutation_count - 1 LOOP
tick := c.begin_tick + tick_index + 1;
life_id := evolution.manifest_uint(p_trace, off, 8);
event_index := evolution.manifest_uint(p_trace, off + 8, 8);
offer_tick := evolution.manifest_uint(p_trace, off + 16, 8);
successor_identity := evolution.manifest_uint(p_trace, off + 24, 8);
operator_id := evolution.manifest_uint(p_trace, off + 32, 2)::integer;
become := get_byte(p_trace, off + 34);
IF life_id = 0 OR life_id <= previous_life OR event_index = 0 OR offer_tick = 0
OR offer_tick >= tick OR successor_identity = 0 OR operator_id >= 32768
OR become > (CASE WHEN revamp THEN 1 ELSE 0 END) OR (become = 1 AND operator_id = 0)
OR substring(p_trace FROM off + 36 FOR 5) <> '\x0000000000' THEN
RAISE EXCEPTION 'invalid cellular mutation selection' USING ERRCODE = '22023';
END IF;
operator_id := operator_id + 32768 * become;
previous_life := life_id;
off := off + 40;
RETURN NEXT;
END LOOP;
END LOOP;
END $$;
-- Schema 59's block check, with bundle 15's results: a decline is life died or declined; a
-- choice is applied (an organism), life died, insufficient funding, split applied or split
-- refused. Only bundle 15 stores become (operator bit 15), and only with a choice.
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);
b15 constant bytea := '\x0d0000000b00000002000000010000000100000007000000060000000200000001000000';
n integer;
account uuid;
account_bytes bytea;
action numeric;
tick numeric;
input bytea;
stored integer;
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);
stored := evolution.manifest_uint(input, 32, 2)::integer;
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 = b15
AND ((stored % 32768 = 0 AND (stored >= 32768 OR get_byte(p_results,i) NOT IN (1,3)))
OR (stored % 32768 <> 0 AND get_byte(p_results,i) NOT IN (0,1,2,5,6))))
OR (c.versions <> b15 AND stored >= 32768)
OR (c.versions IN ('\x0a0000000800000002000000010000000100000004000000030000000200000001000000'::bytea,
'\x0b0000000900000002000000010000000100000005000000040000000200000001000000'::bytea,
'\x0c0000000a00000002000000010000000100000006000000050000000200000001000000'::bytea)
AND (get_byte(p_results,i) NOT IN (1,3,4)
OR (stored=0 AND get_byte(p_results,i) NOT IN (1,3))
OR (stored<>0 AND get_byte(p_results,i) NOT IN (1,4))))
OR (c.versions NOT IN ('\x0a0000000800000002000000010000000100000004000000030000000200000001000000'::bytea,
'\x0b0000000900000002000000010000000100000005000000040000000200000001000000'::bytea,
'\x0c0000000a00000002000000010000000100000006000000050000000200000001000000'::bytea,
b15)
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 = stored) THEN
RAISE EXCEPTION 'cellular mutation differs from its trace' USING ERRCODE = '22023';
END IF;
END LOOP;
RETURN n;
END $$;
ALTER FUNCTION evolution.publish_cellular_profile_migration(bytea) RENAME TO publish_cellular_profile_migration_v59;
REVOKE ALL ON FUNCTION evolution.publish_cellular_profile_migration_v59(bytea) FROM PUBLIC, :"runtime_role";
CREATE OR REPLACE FUNCTION evolution.publish_cellular_profile_migration(p_manifest 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);
s evolution.checkpoint_manifest;
h evolution.region_heads%ROWTYPE;
p evolution.region_commits%ROWTYPE;
old evolution.region_commits%ROWTYPE;
frame evolution.u64;
d bytea := sha256(p_manifest);
source_bundle constant bytea := '\x0c0000000a00000002000000010000000100000006000000050000000200000001000000';
target_bundle constant bytea := '\x0d0000000b00000002000000010000000100000007000000060000000200000001000000';
zero32 constant bytea := decode(repeat('00', 32), 'hex');
BEGIN
IF c.versions <> target_bundle THEN
RETURN QUERY SELECT * FROM evolution.publish_cellular_profile_migration_v59(p_manifest);
RETURN;
END IF;
IF c.profile <> 4 OR c.commit_sequence = 0 OR c.begin_tick <> c.end_tick
OR c.end_tick = 0 OR c.versions <> target_bundle
OR evolution.manifest_uint(p_manifest, 228, 4) <> 0
OR evolution.manifest_uint(p_manifest, 232, 4) <> (CASE c.profile WHEN 4 THEN 4 ELSE 5 END)
OR evolution.manifest_uint(p_manifest, 236, 4) <> 1
OR c.trace_bytes <> 0 OR c.trace_sha256 <> zero32
OR c.decision_count <> 0 OR c.decisions_sha256 <> zero32 THEN
RAISE EXCEPTION 'invalid revamp profile migration' 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 <> p_manifest THEN
RAISE EXCEPTION 'cellular profile migration identity reused' USING ERRCODE = 'EVL07';
END IF;
SELECT * INTO p FROM evolution.region_commits r
WHERE r.world_id = old.world_id AND r.region_id = old.region_id
AND r.commit_sequence = old.commit_sequence - 1;
IF NOT FOUND OR NOT EXISTS (
SELECT 1 FROM evolution.cellular_profile_migrations m
WHERE m.target_commit = c.commit_id AND m.world_id = c.world_id
AND m.region_id = c.region_id AND m.source_sequence = p.commit_sequence
AND m.source_commit = p.commit_id AND m.source_manifest_sha256 = p.commit_digest
AND m.source_payload_sha256 = p.payload_sha256 AND m.source_versions = p.versions
AND m.target_sequence = old.commit_sequence AND m.target_manifest_sha256 = d
AND m.target_payload_sha256 = old.payload_sha256 AND m.target_versions = old.versions
AND m.source_commit = c.predecessor_commit
AND m.source_manifest_sha256 = c.predecessor_digest) THEN
RAISE EXCEPTION 'cellular profile migration 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 'revamp upgrade waits for interval' USING ERRCODE = 'EVL18';
END IF;
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 'revamp upgrade waits for mutation choice' 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 'region head moved' USING ERRCODE = 'EVL03';
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 NOT FOUND THEN
RAISE EXCEPTION 'missing migration predecessor' USING ERRCODE = 'XX000';
END IF;
s := evolution.parse_checkpoint_manifest(p.manifest);
IF s.profile <> c.profile OR s.versions <> source_bundle OR p.versions <> source_bundle
OR c.frame_id <> s.frame_id OR c.tick_microseconds <> s.tick_microseconds
OR s.commit_id <> c.predecessor_commit OR s.end_tick <> c.end_tick
OR evolution.manifest_uint(p.manifest, 232, 4) <> (CASE c.profile WHEN 4 THEN 4 ELSE 5 END)
OR NOT EXISTS (SELECT 1 FROM evolution.retained_builds b
WHERE b.build_identity = c.build_identity) THEN
RAISE EXCEPTION 'unsupported revamp profile migration' USING ERRCODE = 'EVL06';
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;
INSERT INTO evolution.cellular_profile_migrations(target_commit, world_id, region_id,
source_sequence, source_commit, source_manifest_sha256, source_payload_sha256,
source_versions, target_sequence, target_manifest_sha256, target_payload_sha256,
target_versions)
VALUES (c.commit_id, c.world_id, c.region_id, p.commit_sequence, p.commit_id,
p.commit_digest, p.payload_sha256, p.versions, c.commit_sequence, d,
c.payload_sha256, c.versions);
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, p_manifest,
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 frame, c.commit_sequence, c.commit_id, encode(d, 'hex'), c.end_tick;
END $$;
REVOKE ALL ON FUNCTION evolution.publish_cellular_profile_migration(bytea) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION evolution.publish_cellular_profile_migration(bytea) TO :"runtime_role";
INSERT INTO evolution.schema_migrations(version,sha256) VALUES(60,:'migration_sha_60');