Files
aevolution/migrations/0062_session_windows.sql
T
dingoandClaude Opus 5.5 46c106409e
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 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m33s
Foundation / postgres-tools (push) Successful in 2m14s
Foundation / server-image (push) Successful in 5m21s
Foundation / image (push) Successful in 6m18s
feat(admin): session windows in SQL, opened from the admin portal (schema 62)
The dev session window moves from Git to SQL (ADR-0087 amendment): the admin
portal opens, extends (30 min to 4 h from now) and closes it, and the cellular
region re-reads it every 5 s with --session-window-source db. No push, sync or
restart per window; a closed window keeps the region running instead of
crash-looping.

- Schema 62: append-only session_windows (4 h cap, no rewrites),
  load_session_window_v1 for the runtime group, admin_set_session_window_v1
  and admin_session_windows_v1 for a dedicated admin group role, granted and
  audited each migration run when EVO_PG_ADMIN_ROLE is set (aevolution_admin in
  DEV). Schema 61's views are revoked from the runtime group.
- RegionSessions admission switch: while closed every Hello is refused before
  signature work and closing closes open sessions (new sessions were still
  admitted after expiry before).
- Admin API: GET/POST /api/regions/<n>/session-window, window state in the
  regions list; the portal's session-window card with history.
- Git: REGION_SESSION_WINDOW and the pre-push refresh rule are removed; the
  policy check requires the database source and refuses a committed window.

Needs the new image and the aevolution_admin role (with the portal's login in
aevolution-admin-pg) before a sync.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 15:13:02 -04:00

98 lines
5.3 KiB
PL/PgSQL

-- Session windows in SQL (ADR-0087 amendment and ADR-0182, 2026-10-27). A region's dev
-- session window is the newest row of an append-only table instead of a Git-committed
-- environment value: the admin portal opens, extends and closes it, and the region reads it
-- every few seconds. A row with an expiry opens (or replaces) the window until then, at most
-- four hours after it was written; a row without one closes it. Rows are never changed.
--
-- The admin functions move to a dedicated group role: schema 61's read views are revoked
-- from the runtime group, and apply.sql grants every admin function to :admin_role on each
-- run when that role is configured. The runtime group may only read the current window.
CREATE TABLE evolution.session_windows (
world_id evolution.u64 NOT NULL,
region_id evolution.u64 NOT NULL,
sequence bigint GENERATED ALWAYS AS IDENTITY,
activation_id text NOT NULL UNIQUE CHECK (activation_id ~ '^[A-Za-z0-9._-]{1,64}$'),
expires_at timestamptz,
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(),
PRIMARY KEY (world_id, region_id, sequence),
FOREIGN KEY (world_id, region_id) REFERENCES evolution.region_leases,
CHECK (expires_at IS NULL OR
(expires_at > created_at AND expires_at <= created_at + interval '4 hours'))
);
CREATE TRIGGER session_windows_immutable BEFORE UPDATE OR DELETE ON evolution.session_windows
FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite();
CREATE TRIGGER session_windows_no_truncate BEFORE TRUNCATE ON evolution.session_windows
FOR EACH STATEMENT EXECUTE FUNCTION evolution.refuse_history_rewrite();
-- The region's current window: its newest row, or nothing when none was ever written.
CREATE FUNCTION evolution.load_session_window_v1(p_world evolution.u64, p_region evolution.u64)
RETURNS TABLE (activation_id text, expires_at timestamptz, author_subject text,
created_at timestamptz)
LANGUAGE sql STABLE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
SELECT w.activation_id, w.expires_at, w.author_subject, w.created_at
FROM evolution.session_windows w
WHERE w.world_id = p_world AND w.region_id = p_region
ORDER BY w.sequence DESC
LIMIT 1
$$;
REVOKE ALL ON FUNCTION evolution.load_session_window_v1(evolution.u64, evolution.u64) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION evolution.load_session_window_v1(evolution.u64, evolution.u64)
TO :"runtime_role";
-- Opens or extends (1..14400 seconds from now, a new activation) or closes (0 seconds) the
-- region's window, recording who and why. Returns the row written.
CREATE FUNCTION evolution.admin_set_session_window_v1(p_world evolution.u64,
p_region evolution.u64, p_seconds integer, p_subject text, p_reason text)
RETURNS TABLE (activation_id text, expires_at timestamptz)
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
#variable_conflict use_column
DECLARE
now_at timestamptz := clock_timestamp();
id text := 'adm-' || to_char(now_at AT TIME ZONE 'UTC', 'YYYYMMDD"T"HH24MISS') || '-'
|| substr(replace(gen_random_uuid()::text, '-', ''), 1, 8);
BEGIN
IF p_seconds IS NULL OR p_seconds < 0 OR p_seconds > 14400 THEN
RAISE EXCEPTION 'session window duration out of range' USING ERRCODE = '22023';
END IF;
IF NOT EXISTS (SELECT 1 FROM evolution.region_leases l
WHERE l.world_id = p_world AND l.region_id = p_region) THEN
RAISE EXCEPTION 'unknown region' USING ERRCODE = '22023';
END IF;
RETURN QUERY
WITH written AS (
INSERT INTO evolution.session_windows AS w(world_id, region_id, activation_id, expires_at,
author_subject, reason, created_at)
VALUES (p_world, p_region, id,
CASE WHEN p_seconds = 0 THEN NULL ELSE now_at + make_interval(secs => p_seconds) END,
p_subject, coalesce(p_reason, ''), now_at)
RETURNING w.activation_id, w.expires_at)
SELECT written.activation_id, written.expires_at FROM written;
END $$;
REVOKE ALL ON FUNCTION evolution.admin_set_session_window_v1(evolution.u64, evolution.u64,
integer, text, text) FROM PUBLIC;
-- The region's last 50 window changes, newest first.
CREATE FUNCTION evolution.admin_session_windows_v1(p_world evolution.u64, p_region evolution.u64)
RETURNS TABLE (sequence bigint, activation_id text, expires_at timestamptz,
author_subject text, reason text, created_at timestamptz)
LANGUAGE sql STABLE SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
SELECT w.sequence, w.activation_id, w.expires_at, w.author_subject, w.reason, w.created_at
FROM evolution.session_windows w
WHERE w.world_id = p_world AND w.region_id = p_region
ORDER BY w.sequence DESC
LIMIT 50
$$;
REVOKE ALL ON FUNCTION evolution.admin_session_windows_v1(evolution.u64, evolution.u64)
FROM PUBLIC;
-- Schema 61's read views belong to the admin role from now on.
REVOKE EXECUTE ON FUNCTION evolution.admin_accounts_v1(evolution.u64) FROM :"runtime_role";
REVOKE EXECUTE ON FUNCTION evolution.admin_account_lives_v1(evolution.u64, uuid)
FROM :"runtime_role";
REVOKE EXECUTE ON FUNCTION evolution.admin_regions_v1(evolution.u64) FROM :"runtime_role";
INSERT INTO evolution.schema_migrations(version,sha256) VALUES(62,:'migration_sha_62');