feat(admin): session windows in SQL, opened from the admin portal (schema 62)
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

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>
This commit is contained in:
dingo
2026-10-27 15:13:02 -04:00
co-authored by Claude Opus 5.5
parent 510ecaee80
commit 46c106409e
31 changed files with 807 additions and 93 deletions
+113 -9
View File
@@ -144,6 +144,40 @@ std::string json_string(std::string_view text) {
return out + "\"";
}
std::optional<std::string> query_value(std::string_view query, std::string_view name,
std::size_t limit) {
while (!query.empty()) {
const auto amp = query.find('&');
const auto pair = query.substr(0, amp);
query = amp == std::string_view::npos ? std::string_view{} : query.substr(amp + 1);
const auto equals = pair.find('=');
if (pair.substr(0, equals) != name)
continue;
const auto raw =
equals == std::string_view::npos ? std::string_view{} : pair.substr(equals + 1);
std::string out;
for (std::size_t i = 0; i < raw.size(); ++i) {
char ch = raw[i];
if (ch == '+') {
ch = ' ';
} else if (ch == '%') {
unsigned value{};
if (i + 2 >= raw.size() ||
std::from_chars(raw.data() + i + 1, raw.data() + i + 3, value, 16).ptr !=
raw.data() + i + 3)
return std::nullopt;
ch = static_cast<char>(value);
i += 2;
}
if (static_cast<unsigned char>(ch) < 0x20 || ch == 0x7F || out.size() >= limit)
return std::nullopt;
out.push_back(ch);
}
return out;
}
return std::nullopt;
}
std::optional<std::string> http_get(std::string_view address, std::string_view path,
std::chrono::milliseconds timeout, std::size_t limit) {
const auto colon = address.rfind(':');
@@ -225,8 +259,14 @@ AdminApi::AdminApi(AdminConfig config, broker::TokenVerifier verifier, AdminConn
OpsResponse AdminApi::handle(const ApiRequest& request, std::uint64_t now_seconds,
std::chrono::steady_clock::time_point now) {
auto path = request.path.substr(0, request.path.find('?'));
if (request.method != "GET")
const auto mark = request.path.find('?');
auto path = request.path.substr(0, mark);
const auto query =
mark == std::string_view::npos ? std::string_view{} : request.path.substr(mark + 1);
// Only the session window accepts POST; everything else is read-only.
if (request.method != "GET" &&
!(request.method == "POST" && path.starts_with("/api/regions/") &&
path.ends_with("/session-window")))
return {405, "method not allowed\n"};
if (path == "/healthz")
return {200, "ok\n"};
@@ -255,10 +295,11 @@ OpsResponse AdminApi::handle(const ApiRequest& request, std::uint64_t now_second
if (!claims)
return error(claims.error() == TokenError::missing_role ? 403 : 401,
token_error(claims.error()));
return api(path, *claims, now);
return api(request.method, path, query, *claims, now);
}
OpsResponse AdminApi::api(std::string_view path, const broker::AccessTokenClaims& admin,
OpsResponse AdminApi::api(std::string_view method, std::string_view path, std::string_view query,
const broker::AccessTokenClaims& admin,
std::chrono::steady_clock::time_point now) {
if (path == "/api/me")
return json(200, "{\"subject\":" + json_string(admin.subject) +
@@ -280,6 +321,15 @@ OpsResponse AdminApi::api(std::string_view path, const broker::AccessTokenClaims
region_prefix.size(), path.size() - region_prefix.size() - view_suffix.size()));
return region ? view(*region, now) : error(404, "not_found");
}
constexpr std::string_view window_suffix = "/session-window";
if (path.starts_with(region_prefix) && path.ends_with(window_suffix) &&
path.size() > region_prefix.size() + window_suffix.size()) {
const auto region = number(path.substr(
region_prefix.size(), path.size() - region_prefix.size() - window_suffix.size()));
if (!region)
return error(404, "not_found");
return method == "POST" ? set_window(*region, query, admin) : window(*region);
}
return error(404, "not_found");
}
@@ -335,11 +385,15 @@ OpsResponse AdminApi::accounts(std::optional<std::string_view> account) {
OpsResponse AdminApi::regions() {
const auto world = std::to_string(config_.world);
const auto rows = query("SELECT region_id, frame_id, lease_epoch, lease_held, "
"floor(extract(epoch FROM lease_expires))::bigint, commit_sequence, "
"end_tick, floor(extract(epoch FROM committed_at))::bigint, "
"build_identity FROM evolution.admin_regions_v1($1)",
{world});
const auto rows =
query("SELECT r.region_id, r.frame_id, r.lease_epoch, r.lease_held, "
"floor(extract(epoch FROM r.lease_expires))::bigint, r.commit_sequence, r.end_tick, "
"floor(extract(epoch FROM r.committed_at))::bigint, r.build_identity, "
"floor(extract(epoch FROM w.expires_at))::bigint, "
"coalesce(w.expires_at > clock_timestamp(), false) "
"FROM evolution.admin_regions_v1($1) r "
"LEFT JOIN LATERAL evolution.load_session_window_v1($1, r.region_id) w ON true",
{world});
if (!rows)
return error(503, "unavailable");
std::string out = "{\"world\":" + world + ",\"regions\":[";
@@ -355,11 +409,61 @@ OpsResponse AdminApi::regions() {
",\"sequence\":" + text_value(at(row, 5)) + ",\"tick\":" + text_value(at(row, 6)) +
",\"committed_at\":" + seconds_value(at(row, 7)) +
",\"build\":" + text_value(at(row, 8)) +
",\"window_expires\":" + seconds_value(at(row, 9)) +
",\"window_open\":" + (at(row, 10) == "t" ? "true" : "false") +
",\"viewable\":" + (viewable ? "true" : "false") + "}";
}
return json(200, out + "]}\n");
}
OpsResponse AdminApi::window(std::uint64_t region) {
const std::vector<std::string> key{std::to_string(config_.world), std::to_string(region)};
const auto rows = query("SELECT sequence, activation_id, "
"floor(extract(epoch FROM expires_at))::bigint, author_subject, "
"reason, floor(extract(epoch FROM created_at))::bigint, "
"coalesce(expires_at > clock_timestamp(), false), "
"floor(extract(epoch FROM clock_timestamp()))::bigint "
"FROM evolution.admin_session_windows_v1($1, $2)",
key);
if (!rows)
return error(503, "unavailable");
std::string out = "{\"region\":" + json_string(key[1]) + ",\"open\":" +
(!rows->empty() && at(rows->front(), 6) == "t" ? "true" : "false") +
",\"history\":[";
for (std::size_t i = 0; i < rows->size(); ++i) {
const auto& row = (*rows)[i];
out += std::string(i == 0 ? "" : ",") + "{\"sequence\":" + count_value(at(row, 0)) +
",\"activation\":" + text_value(at(row, 1)) +
",\"expires_at\":" + seconds_value(at(row, 2)) +
",\"author\":" + text_value(at(row, 3)) + ",\"reason\":" + text_value(at(row, 4)) +
",\"created_at\":" + seconds_value(at(row, 5)) + "}";
}
return json(200, out + "]}\n");
}
OpsResponse AdminApi::set_window(std::uint64_t region, std::string_view query_text,
const broker::AccessTokenClaims& admin) {
const auto minutes_text = query_value(query_text, "minutes", 3);
const auto minutes = minutes_text ? number(*minutes_text) : std::nullopt;
if (!minutes || *minutes > 240)
return error(400, "bad_minutes");
const auto reason = query_value(query_text, "reason");
if (!reason && query_text.find("reason=") != std::string_view::npos)
return error(400, "bad_reason");
// Who: the email for people reading the history, and the subject that identifies them.
auto author = admin.email.empty() ? admin.subject : admin.email + " (" + admin.subject + ")";
if (author.size() > 255)
author = admin.subject;
const auto rows = query("SELECT activation_id, floor(extract(epoch FROM expires_at))::bigint "
"FROM evolution.admin_set_session_window_v1($1, $2, $3, $4, $5)",
{std::to_string(config_.world), std::to_string(region),
std::to_string(*minutes * 60), author, reason.value_or("")});
if (!rows || rows->empty())
return error(503, "unavailable");
return json(200, "{\"activation\":" + text_value(at(rows->front(), 0)) +
",\"expires_at\":" + seconds_value(at(rows->front(), 1)) + "}\n");
}
OpsResponse AdminApi::view(std::uint64_t region, std::chrono::steady_clock::time_point now) {
if (!std::ranges::contains(config_.view_regions, region))
return error(404, "not_viewable");
+11
View File
@@ -128,6 +128,7 @@ struct RegionSessions::Impl {
Clock::time_point next_ghosts{}, next_overdue{};
std::optional<network::SessionInterest> default_interest;
bool refuse_logins{}; // Load governor (ADR-0097).
bool admission_closed{}; // Session window closed (ADR-0087).
std::uint64_t refused_load{}; // Grant logins refused for load.
HostFacts facts;
std::vector<Connection> connections;
@@ -264,6 +265,8 @@ struct RegionSessions::Impl {
Clock::time_point now) {
if (m.required_features != 0)
return close(c, SessionCloseReason::protocol, now);
if (admission_closed) // No session window: before any signature work.
return close(c, SessionCloseReason::shutdown, now);
if (!control.attempt(now_seconds)) // Region-wide, before signature work.
return close(c, SessionCloseReason::rate_limited, now);
if (network::looks_like_handoff_ticket(m.grant))
@@ -1079,6 +1082,14 @@ std::optional<std::uint64_t> RegionSessions::holder(const network::AccountId& ac
return impl_->control.holder(account);
}
void RegionSessions::set_admission(bool open) noexcept {
impl_->admission_closed = !open;
}
bool RegionSessions::admitting() const noexcept {
return !impl_->admission_closed;
}
void RegionSessions::set_load(bool refuse_logins, std::uint8_t shed) {
impl_->refuse_logins = refuse_logins;
impl_->replication.set_shed(shed);