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