#pragma once #include "evo/network/protocol.hpp" #include "evo/world/chunks.hpp" #include #include #include #include #include // Server-side session admission for the M9.6 dev session (ADR-0082). Transport- // neutral and single-owner: the host serializes calls. It admits intentions for // scheduling only; the simulation still validates and decides every outcome. namespace evo::network { enum class DeploymentEnvironment : std::uint8_t { dev, stage, world0 }; // Claims a dev grant carries; meaningful only once verified. struct DevGrantClaims { std::uint64_t grant_id{}, subject{}; world::WorldIdentity world; std::uint64_t region{}, issued_at{}, expires_at{}; // Server clock, seconds. }; // A grant already used to open a session, remembered until it expires. struct ConsumedGrant { std::uint64_t grant_id{}, expires_at{}; }; class DevGrantKey; // Only DevGrantKey::verify constructs this, so admission cannot be handed // unauthenticated claims by accident. class VerifiedGrant { public: [[nodiscard]] const DevGrantClaims& claims() const noexcept { return claims_; } private: friend class DevGrantKey; explicit VerifiedGrant(DevGrantClaims claims) : claims_{claims} {} DevGrantClaims claims_; }; struct AdmissionConfig { world::WorldIdentity world; std::uint64_t region{}, epoch{}; std::size_t max_sessions{16}; std::size_t max_remembered_grants{4096}; std::uint32_t intent_window{8}; // Ticks ahead of the server tick. std::uint32_t window_ticks{8}; // Rate window length. std::uint32_t max_intents_per_window{16}; // Counts every attempt by a session. std::uint64_t max_grant_lifetime{14400}; // Seconds; four-hour DEV ceiling. std::uint64_t clock_skew{30}; // Seconds tolerated before issued_at. }; enum class AdmissionError : std::uint8_t { invalid_config, wrong_world, wrong_region, invalid_lifetime, not_yet_valid, expired, grant_reused, duplicate_subject, sessions_full, grants_full, unknown_session }; class SessionAdmission { public: [[nodiscard]] static std::expected create(AdmissionConfig config); // Single-use grants: a grant id is remembered until it expires. A subject may hold // one session; a second open is refused (control is never duplicated). [[nodiscard]] std::expected open(const VerifiedGrant& grant, std::uint64_t now_seconds, std::uint64_t server_tick, std::uint64_t commit_watermark); [[nodiscard]] IntentResult admit(const IntentMessage& intent, std::uint64_t server_tick, std::uint64_t now_seconds); [[nodiscard]] std::expected close(std::uint64_t session); // Restores grants consumed before a restart (from a durable ledger), so a replacement // host refuses them too. Expired entries are skipped; too many fail closed. [[nodiscard]] std::expected remember(std::span consumed, std::uint64_t now_seconds); // New ownership epoch: existing sessions become stale and must reconnect. [[nodiscard]] std::expected advance_epoch(std::uint64_t epoch); [[nodiscard]] std::size_t sessions() const noexcept { return sessions_.size(); } [[nodiscard]] std::uint64_t epoch() const noexcept { return config_.epoch; } private: struct Session { std::uint64_t id{}, subject{}, epoch{}, expires_at{}, last_sequence{}; std::uint64_t window_start{}; std::uint32_t attempts{}; }; struct Remembered { std::uint64_t grant_id{}, expires_at{}; }; explicit SessionAdmission(AdmissionConfig config) : config_{config} {} AdmissionConfig config_; std::uint64_t next_session_{1}; std::vector sessions_; std::vector grants_; }; } // namespace evo::network