Files
aevolution/migrations/0066_tuning_revisions.sql
dingoandClaude Opus 5.5 7273b157d6
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
feat(admin): live tuning revisions adopted at recorded ticks (schema 66)
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>
2026-10-27 19:35:09 -04:00

107 lines
5.7 KiB
PL/PgSQL

-- 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');