#pragma once #include #include #include #include #include #include #include namespace evo::server { // An operator-opened, absolute UTC session window. The serialized form is // activation-id:expiry-unix-seconds. It deliberately contains no relative // duration, so a delayed rollout or restart cannot extend the window. class SessionWindow { public: enum class Error : std::uint8_t { malformed, expired, too_long }; static constexpr std::uint64_t max_duration_seconds = std::uint64_t{4} * 60 * 60; static std::expected open(std::string_view specification, std::uint64_t now_seconds) { const auto colon = specification.rfind(':'); if (colon == std::string_view::npos || colon == 0 || colon + 1 == specification.size()) return std::unexpected(Error::malformed); const auto id = specification.substr(0, colon); if (id.find_first_not_of( "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-") != std::string_view::npos) return std::unexpected(Error::malformed); std::uint64_t deadline{}; const auto text = specification.substr(colon + 1); const auto [end, ec] = std::from_chars(text.data(), text.data() + text.size(), deadline); if (ec != std::errc{} || end != text.data() + text.size()) return std::unexpected(Error::malformed); if (deadline <= now_seconds) return std::unexpected(Error::expired); if (deadline - now_seconds > max_duration_seconds) return std::unexpected(Error::too_long); return SessionWindow{std::string{id}, deadline}; } [[nodiscard]] bool accepting(std::uint64_t now_seconds) noexcept { if (closed_ || now_seconds >= deadline_seconds_) { closed_ = true; return false; } return true; } [[nodiscard]] bool closed() const noexcept { return closed_; } [[nodiscard]] std::uint64_t deadline_seconds() const noexcept { return deadline_seconds_; } [[nodiscard]] const std::string& activation_id() const noexcept { return activation_id_; } void close() noexcept { closed_ = true; } private: SessionWindow(std::string activation_id, std::uint64_t deadline_seconds) : activation_id_(std::move(activation_id)), deadline_seconds_(deadline_seconds) {} std::string activation_id_; std::uint64_t deadline_seconds_{}; bool closed_{}; }; // ADR-0087 amendment (2026-10-27): the window as the database records it, re-read every few // seconds. A newer activation replaces the current window (sessions continue); a closed, // expired, over-long or absent record closes it. Each admitted window keeps its own absolute // deadline, and a window once closed never reopens: only a newer activation opens sessions // again. While the database cannot be read the caller does not observe, so the current // window keeps running to its deadline. class SessionWindowFeed { public: struct Record { std::string activation_id; std::optional expires_at; // nullopt: the record closes the window. }; enum class Change : std::uint8_t { none, opened, closed, refused }; // `record` is the region's newest row, or nullopt when it has none. Change observe(const std::optional& record, std::uint64_t now_seconds) { if (!record) { const bool had = window_.has_value(); window_.reset(); seen_.clear(); return had ? Change::closed : Change::none; } if (record->activation_id == seen_) return Change::none; seen_ = record->activation_id; const bool had = window_.has_value(); window_.reset(); if (!record->expires_at) return had ? Change::closed : Change::none; auto opened = SessionWindow::open( record->activation_id + ":" + std::to_string(*record->expires_at), now_seconds); if (!opened) return Change::refused; window_ = std::move(*opened); return Change::opened; } [[nodiscard]] bool accepting(std::uint64_t now_seconds) noexcept { return window_ && window_->accepting(now_seconds); } [[nodiscard]] const std::optional& window() const noexcept { return window_; } private: std::optional window_; std::string seen_; // The newest activation observed, admitted or not. }; } // namespace evo::server