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
+10 -3
View File
@@ -53,6 +53,10 @@
namespace evo::server_app {
struct M10Options {
std::string listen, certificate, key, issuer, environment, session_window;
// ADR-0087 amendment: `--session-window-source db` reads the window from
// evolution.load_session_window_v1 every few seconds instead of --session-window
// (cellular profile only). Exactly one of the two is given.
bool window_from_database{};
// M10.5c: dev only, 60..14400 s; 0 keeps the default (300 s, ADR-0091).
std::uint64_t max_grant_lifetime{};
// M10.9 (ADR-0097, dev only): `--m10-load-governor off` keeps measuring and exposing the
@@ -68,15 +72,16 @@ struct M10Options {
std::vector<std::string> life_routes;
[[nodiscard]] bool session_any() const noexcept {
return !listen.empty() || !certificate.empty() || !key.empty() || !issuer.empty() ||
!environment.empty() || !session_window.empty() || max_grant_lifetime != 0 ||
!area.empty() || !handoff_key.empty() || !neighbours.empty() || !load_governor;
!environment.empty() || !session_window.empty() || window_from_database ||
max_grant_lifetime != 0 || !area.empty() || !handoff_key.empty() ||
!neighbours.empty() || !load_governor;
}
[[nodiscard]] bool any() const noexcept {
return session_any() || !life_routes.empty();
}
[[nodiscard]] bool complete() const noexcept {
return !listen.empty() && !certificate.empty() && !key.empty() && !issuer.empty() &&
!environment.empty() && !session_window.empty();
!environment.empty() && (session_window.empty() == window_from_database);
}
};
// Actions are handed to the host as soon as they are scheduled (at most 8 ticks ahead).
@@ -117,6 +122,8 @@ template <typename Host> class M10Bridge {
M10Bridge bridge;
if (!o.any())
return bridge;
if (o.window_from_database)
return fail("error m10-window-source"); // The cellular profile only.
const auto window = server::SessionWindow::open(o.session_window, wall_seconds());
if (!window)
return fail(window.error() == server::SessionWindow::Error::expired
+4 -1
View File
@@ -263,6 +263,8 @@ std::optional<Options> parse(int argc, char** argv) {
o.m10.environment = value;
else if (flag == "--session-window")
o.m10.session_window = value;
else if (flag == "--session-window-source" && value == "db")
o.m10.window_from_database = true;
else if (flag == "--m10-max-grant-lifetime" && n && *n >= 60 && *n <= 14400)
o.m10.max_grant_lifetime = *n;
else if (flag == "--m10-area")
@@ -1406,7 +1408,8 @@ int main(int argc, char** argv) {
"--session-cert F --session-key F --session-grant-key F "
"--session-grant-ledger F --session-environment dev]] [--m10-listen "
"ADDRESS:PORT --m10-cert F --m10-key F --m10-issuer F "
"--m10-environment dev|stage|world0 --session-window ACTIVATION:UTC_EXPIRY "
"--m10-environment dev|stage|world0 (--session-window ACTIVATION:UTC_EXPIRY | "
"--session-window-source db) "
"[--m10-max-grant-lifetime 60..14400] [--m10-area X0,Y0,X1,Y1] "
"[--m10-handoff-key F [--m10-neighbour "
"REGION=X0,Y0,X1,Y1=HOST:PORT=NAME=PUBFILE]...] "