feat(admin): live tuning revisions adopted at recorded ticks (schema 66)
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (checks) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m47s
Foundation / postgres-tools (push) Successful in 29s
Foundation / server-image (push) Successful in 4m42s
Foundation / image (push) Successful in 5m55s

ADR-0182 section 13. The admin portal's Tuning page edits the world's
rules-13 tuning (the Git revamp file format); the admin API checks it,
stores an append-only revision and queues an adopt_tuning admin command
(kind 7: revision and rules digest) for every region of the world.

- Lifecycle: adopt_tuning replaces the region's rules after births when
  a resolver names the rules for that revision and digest; the safety
  revision cannot change live. At most one adoption per batch.
- Host: fetches the revision text, rebuilds and digests the rules before
  appending the command; publication and recovery name the rules from
  the successor state, checked against the recorded digest.
- Schema 66: tuning_revisions, admin_set_tuning_v1, load_tuning_revision_v1
  (runtime), admin_tuning_revisions_v1, admin_tuning_text_v1.
- OpsServer: optional bounded request bodies (Content-Length, 413).
- Region view: rules_sha256, so the portal shows each region's revision.
- Portal: Tuning page; baseline from the mounted cellular-revamp.tuning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-27 19:35:09 -04:00
co-authored by Claude Opus 5.5
parent 7041c947a9
commit 7273b157d6
33 changed files with 1241 additions and 97 deletions
+106
View File
@@ -0,0 +1,106 @@
-- 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');
+21 -4
View File
@@ -141,6 +141,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution')
SELECT :'history_v62' || ',63:' || :'migration_sha_63' AS history_v63 \gset
SELECT :'history_v63' || ',64:' || :'migration_sha_64' AS history_v64 \gset
SELECT :'history_v64' || ',65:' || :'migration_sha_65' AS history_v65 \gset
SELECT :'history_v65' || ',66:' || :'migration_sha_66' AS history_v66 \gset
SELECT :'history' = :'history_v22' AS at_v22,
:'history' = :'history_v23' AS at_v23,
:'history' = :'history_v24' AS at_v24,
@@ -184,8 +185,10 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution')
:'history' = :'history_v62' AS at_v62,
:'history' = :'history_v63' AS at_v63,
:'history' = :'history_v64' AS at_v64,
:'history' = :'history_v65' AS at_v65 \gset
\if :at_v65
:'history' = :'history_v65' AS at_v65,
:'history' = :'history_v66' AS at_v66 \gset
\if :at_v66
\elif :at_v65
\elif :at_v64
\elif :at_v63
\elif :at_v62
@@ -609,7 +612,8 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
coalesce(max(version), 0) = 62 AS at_v62,
coalesce(max(version), 0) = 63 AS at_v63,
coalesce(max(version), 0) = 64 AS at_v64,
coalesce(max(version), 0) = 65 AS at_v65
coalesce(max(version), 0) = 65 AS at_v65,
coalesce(max(version), 0) = 66 AS at_v66
FROM evolution.schema_migrations \gset
\if :at_v24
\ir 0025_cellular_v2_bundle.sql
@@ -843,6 +847,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
\elif :at_v63
\elif :at_v64
\elif :at_v65
\elif :at_v66
\else
\echo 'Unknown or changed migration history after v43'
DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$;
@@ -935,6 +940,10 @@ SELECT coalesce(max(version), 0) = 64 AS needs_v65 FROM evolution.schema_migrati
\if :needs_v65
\ir 0065_admin_schedules.sql
\endif
SELECT coalesce(max(version), 0) = 65 AS needs_v66 FROM evolution.schema_migrations \gset
\if :needs_v66
\ir 0066_tuning_revisions.sql
\endif
-- Effective-privilege audit on every run, including no-ops (ADR-0060). Attributes
-- alone are insufficient: predefined roles such as pg_write_all_data, PUBLIC
-- grants or SET-able memberships would bypass the fenced functions. Checks the
@@ -1010,6 +1019,7 @@ BEGIN
('evolution.issue_account_grant(evolution.u64,text,text)'::regprocedure::oid),
('evolution.load_session_window_v1(evolution.u64,evolution.u64)'::regprocedure::oid),
('evolution.load_pending_admin_commands_v1(evolution.u64,evolution.u64)'::regprocedure::oid),
('evolution.load_tuning_revision_v1(evolution.u64,bigint)'::regprocedure::oid),
('evolution.revoke_account_grants(evolution.u64,text,text)'::regprocedure::oid),
('evolution.load_cellular_publication_actions(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)'::regprocedure::oid),
('evolution.load_cellular_publication_births(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)'::regprocedure::oid),
@@ -1093,6 +1103,10 @@ GRANT EXECUTE ON FUNCTION evolution.admin_set_schedule_v1(evolution.u64, evoluti
integer, boolean, text, text) TO :"admin_role";
GRANT EXECUTE ON FUNCTION evolution.admin_schedules_v1(evolution.u64, evolution.u64)
TO :"admin_role";
GRANT EXECUTE ON FUNCTION evolution.admin_set_tuning_v1(evolution.u64, text, bytea, bigint[], text,
text) TO :"admin_role";
GRANT EXECUTE ON FUNCTION evolution.admin_tuning_revisions_v1(evolution.u64) TO :"admin_role";
GRANT EXECUTE ON FUNCTION evolution.admin_tuning_text_v1(evolution.u64, bigint) TO :"admin_role";
SELECT pg_catalog.set_config('evolution.admin_role', :'admin_role', true);
DO $$
DECLARE
@@ -1123,7 +1137,10 @@ BEGIN
('evolution.admin_account_status_v1(evolution.u64)'::regprocedure::oid),
('evolution.admin_account_bans_v1(uuid)'::regprocedure::oid),
('evolution.admin_set_schedule_v1(evolution.u64,evolution.u64,bigint,integer,integer,evolution.u64,evolution.u64,evolution.u64,evolution.u64,evolution.u64,integer,boolean,text,text)'::regprocedure::oid),
('evolution.admin_schedules_v1(evolution.u64,evolution.u64)'::regprocedure::oid)
('evolution.admin_schedules_v1(evolution.u64,evolution.u64)'::regprocedure::oid),
('evolution.admin_set_tuning_v1(evolution.u64,text,bytea,bigint[],text,text)'::regprocedure::oid),
('evolution.admin_tuning_revisions_v1(evolution.u64)'::regprocedure::oid),
('evolution.admin_tuning_text_v1(evolution.u64,bigint)'::regprocedure::oid)
), violations(reason) AS (
SELECT 'admin role is the runtime role' WHERE admin = runtime
UNION ALL