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
+85 -15
View File
@@ -42,9 +42,16 @@ class CellularBridge {
if (!options.complete())
return fail("error cellular-session-config");
CellularBridge bridge;
const auto window = server::SessionWindow::open(options.session_window, wall_seconds());
if (!window)
return fail("error cellular-session-window");
// A fixed --session-window must be open now; a database window (ADR-0087 amendment)
// may open, close and reopen while the region runs.
if (!options.window_from_database) {
const auto window = server::SessionWindow::open(options.session_window, wall_seconds());
if (!window)
return fail("error cellular-session-window");
bridge.window_ = *window;
}
bridge.window_from_database_ = options.window_from_database;
bridge.key_ = key;
const auto colon = options.listen.rfind(':');
std::uint16_t port{};
if (colon == std::string::npos ||
@@ -85,7 +92,6 @@ class CellularBridge {
std::copy(issuer.begin(), issuer.end(), public_key.begin());
config.issuer_keys.push_back({1, public_key});
bridge.window_ = *window;
bridge.slot_ = std::make_unique<server::PgSlot>(pg, std::chrono::seconds{2});
const auto lease = [&host] { return host.authority(); };
bridge.control_ =
@@ -126,15 +132,25 @@ class CellularBridge {
: "error cellular-session-config");
bridge.sessions_ = std::move(*sessions);
bridge.sessions_->set_default_interest({0, 0, 0, 7});
// A database window admits nobody until it has been read.
if (bridge.window_from_database_)
bridge.sessions_->set_admission(false);
std::printf("cellular listening %u\n", static_cast<unsigned>(bridge.sessions_->port()));
return std::optional<CellularBridge>{std::move(bridge)};
}
// Applies the session window: while it is closed every Hello is refused, and closing
// closes the open sessions.
[[nodiscard]] bool accepting(std::uint64_t now_seconds) {
const bool open = window_ && window_->accepting(now_seconds);
if (!open && !shutdown_) {
sessions_->shutdown(std::chrono::steady_clock::now());
shutdown_ = true;
if (window_from_database_)
read_window(now_seconds);
const bool open = window_from_database_ ? feed_.accepting(now_seconds)
: window_ && window_->accepting(now_seconds);
if (open != sessions_->admitting()) {
sessions_->set_admission(open);
if (!open)
sessions_->shutdown(std::chrono::steady_clock::now());
std::printf("cellular sessions %s\n", open ? "admitted" : "closed");
}
return open;
}
@@ -362,11 +378,13 @@ class CellularBridge {
const auto before = deposit_stock_.find(patch.id);
deposit.drained = before != deposit_stock_.end() &&
(patch.available.energy.value < before->second.energy.value ||
std::ranges::any_of(std::array{0, 1, 2, 3}, [&](int c) {
const auto i = static_cast<std::size_t>(c);
return patch.available.matter.at(i).value <
before->second.matter.at(i).value;
}));
std::ranges::any_of(
std::array{0, 1, 2, 3},
[&](int c) {
const auto i = static_cast<std::size_t>(c);
return patch.available.matter.at(i).value <
before->second.matter.at(i).value;
}));
seen_stock_[patch.id] = patch.available;
// Rules 12 (v13): a bubble's world velocity, current plus induced drift, so the
// client can carry it between perceptions.
@@ -614,12 +632,65 @@ class CellularBridge {
std::puts(message);
return std::nullopt;
}
// The region's newest window record, every 5 s. A failed read keeps the current window,
// which still closes at its own deadline.
void read_window(std::uint64_t now_seconds) {
constexpr std::chrono::seconds interval{5};
const auto now = std::chrono::steady_clock::now();
if (now < next_window_read_)
return;
next_window_read_ = now + interval;
auto* db = slot_->get();
if (db == nullptr)
return;
const std::array<std::string, 2> parameters{std::to_string(key_.world),
std::to_string(key_.region)};
const auto rows =
db->query("SELECT activation_id, floor(extract(epoch FROM expires_at))::bigint FROM "
"evolution.load_session_window_v1($1::evolution.u64, $2::evolution.u64)",
parameters, std::chrono::milliseconds{1000});
if (!rows) {
slot_->broken();
return;
}
std::optional<server::SessionWindowFeed::Record> record;
if (!rows->empty() && rows->front().size() == 2 && rows->front()[0]) {
record = server::SessionWindowFeed::Record{*rows->front()[0], std::nullopt};
if (const auto& expiry = rows->front()[1]) {
std::uint64_t value{};
const auto [end, ec] =
std::from_chars(expiry->data(), expiry->data() + expiry->size(), value);
if (ec == std::errc{} && end == expiry->data() + expiry->size())
record->expires_at = value;
}
}
switch (feed_.observe(record, now_seconds)) {
case server::SessionWindowFeed::Change::opened:
std::printf("cellular window opened activation=%s expires=%llu\n",
feed_.window()->activation_id().c_str(),
static_cast<unsigned long long>(feed_.window()->deadline_seconds()));
break;
case server::SessionWindowFeed::Change::closed:
std::puts("cellular window closed");
break;
case server::SessionWindowFeed::Change::refused:
std::printf("cellular window refused activation=%s\n",
record ? record->activation_id.c_str() : "");
break;
case server::SessionWindowFeed::Change::none:
break;
}
}
std::unique_ptr<server::PgSlot> slot_;
std::unique_ptr<server::PostgresControlLedger> control_;
std::unique_ptr<server::PostgresActionLedger> actions_;
std::unique_ptr<server::RegionSessions> sessions_;
std::optional<server::SessionWindow> window_;
std::optional<server::SessionWindow> window_; // A fixed --session-window.
bool window_from_database_{};
persistence::RegionKey key_{};
server::SessionWindowFeed feed_;
std::chrono::steady_clock::time_point next_window_read_{};
std::optional<std::uint64_t> replicated_tick_;
struct SentOffer {
server::LifeAccount account{};
@@ -632,6 +703,5 @@ class CellularBridge {
// ADR-0136: deposit stocks seen at the last replicated tick, and this tick's.
std::map<std::uint32_t, accounting::Inventory> deposit_stock_, seen_stock_;
std::vector<std::pair<server::LifeAccount, std::uint64_t>> owned_; // At last replication.
bool shutdown_{};
};
} // namespace evo::server_app