-- Tuning revisions (ADR-0182 sections 1-2, 2026-10-27). The rules-13 tuning of a world is a -- sequence of append-only revisions: the canonical tuning text and the SHA-256 of the rules it -- produces (encode_cellular_revamp_rules). Storing a revision queues an adopt_tuning admin -- command (kind 7) for each named region; the region fetches the text, rebuilds the rules, -- checks the digest and adopts them at a tick recorded in its trace, so replay and recovery -- reproduce the change exactly. Commands of kind 7 come only from this function. ALTER TABLE evolution.admin_commands DROP CONSTRAINT admin_commands_kind_check; ALTER TABLE evolution.admin_commands ADD CONSTRAINT admin_commands_kind_check CHECK (kind BETWEEN 1 AND 7); CREATE TABLE evolution.tuning_revisions ( revision bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, world_id evolution.u64 NOT NULL, text text NOT NULL CHECK (octet_length(text) BETWEEN 1 AND 262144), rules_sha256 bytea NOT NULL CHECK (octet_length(rules_sha256) = 32), author_subject text NOT NULL CHECK (octet_length(author_subject) BETWEEN 1 AND 255), reason text NOT NULL DEFAULT '' CHECK (octet_length(reason) <= 500), created_at timestamptz NOT NULL DEFAULT clock_timestamp() ); CREATE TRIGGER tuning_revisions_immutable BEFORE UPDATE OR DELETE ON evolution.tuning_revisions FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite(); -- Admin: stores a revision (validated by the admin API, which also computed the rules digest; -- the text is kept as written, comments included) and queues its adoption in each of -- `p_regions`. CREATE FUNCTION evolution.admin_set_tuning_v1(p_world evolution.u64, p_text text, p_rules_sha256 bytea, p_regions bigint[], p_subject text, p_reason text) RETURNS bigint LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ DECLARE made bigint; region bigint; BEGIN IF p_world IS NULL OR p_world = 0 OR p_regions IS NULL OR cardinality(p_regions) = 0 OR cardinality(p_regions) > 64 OR p_rules_sha256 IS NULL OR octet_length(p_rules_sha256) <> 32 THEN RAISE EXCEPTION 'invalid tuning revision' USING ERRCODE = '22023'; END IF; BEGIN INSERT INTO evolution.tuning_revisions(world_id, text, rules_sha256, author_subject, reason) VALUES (p_world, p_text, p_rules_sha256, p_subject, coalesce(p_reason, '')) RETURNING revision INTO made; EXCEPTION WHEN check_violation OR not_null_violation THEN RAISE EXCEPTION 'invalid tuning revision' USING ERRCODE = '22023'; END; FOREACH region IN ARRAY p_regions LOOP IF NOT EXISTS (SELECT 1 FROM evolution.region_leases l WHERE l.world_id = p_world AND l.region_id = region) THEN RAISE EXCEPTION 'unknown region' USING ERRCODE = '22023'; END IF; INSERT INTO evolution.admin_commands(world_id, region_id, kind, cell_count, target, energy, carbon, nitrogen, phosphorus, other, author_subject, reason) VALUES (p_world, region, 7, 0, made, evolution.manifest_uint(p_rules_sha256, 0, 8), evolution.manifest_uint(p_rules_sha256, 8, 8), evolution.manifest_uint(p_rules_sha256, 16, 8), evolution.manifest_uint(p_rules_sha256, 24, 8), 0, p_subject, coalesce(p_reason, '')); END LOOP; RETURN made; END $$; REVOKE ALL ON FUNCTION evolution.admin_set_tuning_v1(evolution.u64, text, bytea, bigint[], text, text) FROM PUBLIC; -- The region: a revision's text and rules digest. CREATE FUNCTION evolution.load_tuning_revision_v1(p_world evolution.u64, p_revision bigint) RETURNS TABLE (text text, rules_sha256 bytea) LANGUAGE sql STABLE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ SELECT t.text, t.rules_sha256 FROM evolution.tuning_revisions t WHERE t.world_id = p_world AND t.revision = p_revision $$; REVOKE ALL ON FUNCTION evolution.load_tuning_revision_v1(evolution.u64, bigint) FROM PUBLIC; GRANT EXECUTE ON FUNCTION evolution.load_tuning_revision_v1(evolution.u64, bigint) TO :"runtime_role"; -- Admin: the world's revisions, newest first, with how many regions have adopted each. CREATE FUNCTION evolution.admin_tuning_revisions_v1(p_world evolution.u64) RETURNS TABLE (revision bigint, rules_sha256 text, author_subject text, reason text, created_at timestamptz, regions bigint, adopted bigint, last_tick evolution.u64) LANGUAGE sql STABLE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ SELECT t.revision, encode(t.rules_sha256, 'hex'), t.author_subject, t.reason, t.created_at, (SELECT count(*) FROM evolution.admin_commands q WHERE q.kind = 7 AND q.world_id = p_world AND q.target = t.revision), (SELECT count(*) FROM evolution.admin_commands q JOIN evolution.admin_command_applications a ON a.command_id = q.command_id WHERE q.kind = 7 AND q.world_id = p_world AND q.target = t.revision), (SELECT max(a.tick) FROM evolution.admin_commands q JOIN evolution.admin_command_applications a ON a.command_id = q.command_id WHERE q.kind = 7 AND q.world_id = p_world AND q.target = t.revision) FROM evolution.tuning_revisions t WHERE t.world_id = p_world ORDER BY t.revision DESC LIMIT 100 $$; REVOKE ALL ON FUNCTION evolution.admin_tuning_revisions_v1(evolution.u64) FROM PUBLIC; -- Admin: a revision's text (0: the newest). CREATE FUNCTION evolution.admin_tuning_text_v1(p_world evolution.u64, p_revision bigint) RETURNS TABLE (revision bigint, text text) LANGUAGE sql STABLE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ SELECT t.revision, t.text FROM evolution.tuning_revisions t WHERE t.world_id = p_world AND (p_revision = 0 OR t.revision = p_revision) ORDER BY t.revision DESC LIMIT 1 $$; REVOKE ALL ON FUNCTION evolution.admin_tuning_text_v1(evolution.u64, bigint) FROM PUBLIC; INSERT INTO evolution.schema_migrations(version,sha256) VALUES(66,:'migration_sha_66');