#pragma once // M9.7 dev session bridge for evo-server (ADR-0083). Without EVO_SERVER_SESSIONS (a // build without EVO_BUILD_NETWORK) every operation is a no-op and session flags are // refused at parse time. The bridge reads host state; it never mutates it. #include "evo/persistence/region_lease.hpp" #include "evo/server/ops.hpp" #include #include #include #ifdef EVO_SERVER_SESSIONS #include "evo/network/dev_credentials.hpp" #include "evo/network/session_server.hpp" #include "evo/processes/lineage_region.hpp" #include "evo/world/environment.hpp" #include #include #include #include #include #endif namespace evo::server_app { struct SessionOptions { std::string listen, certificate, key, grant_key, grant_ledger, environment; [[nodiscard]] bool any() const noexcept { return !listen.empty() || !certificate.empty() || !key.empty() || !grant_key.empty() || !grant_ledger.empty() || !environment.empty(); } [[nodiscard]] bool complete() const noexcept { return !listen.empty() && !certificate.empty() && !key.empty() && !grant_key.empty() && !grant_ledger.empty() && !environment.empty(); } }; // Intentions ride synthetic inputs, so sessions keep the driver's lookahead short. inline constexpr std::uint64_t session_lookahead_ticks = 8; #ifdef EVO_SERVER_SESSIONS inline constexpr bool sessions_available = true; class SessionBridge { public: // Empty bridge when no session options were given; nullopt on a startup failure // (the caller exits with a usage/configuration error before serving). static std::optional open(const SessionOptions& o, persistence::RegionKey key, std::uint64_t frame) { SessionBridge bridge; if (!o.any()) return bridge; const auto environment = o.environment == "dev" ? network::DeploymentEnvironment::dev : o.environment == "stage" ? network::DeploymentEnvironment::stage : network::DeploymentEnvironment::world0; const auto secret = network::parse_grant_key(read(o.grant_key)); if (!secret) return fail("error session-grant-key"); auto grant_key = network::DevGrantKey::import(environment, *secret); if (!grant_key) return fail("error session-environment"); // Dev identity exists only in dev. network::SessionServerConfig config; config.listen = o.listen; config.credentials = {read(o.certificate), read(o.key)}; config.admission.world = {key.world, frame}; config.admission.region = key.region; config.admission.max_sessions = 4; config.admission.intent_window = session_lookahead_ticks; config.admission.window_ticks = session_lookahead_ticks; config.admission.max_intents_per_window = session_lookahead_ticks; config.grant_ledger = o.grant_ledger; auto server = network::SessionServer::create(std::move(config), std::move(*grant_key), wall_seconds()); if (!server) { const auto e = server.error(); return fail(e == network::SessionServerError::listen ? "error session-listen" : e == network::SessionServerError::ledger ? "error session-grant-ledger" : "error session-credentials"); } bridge.server_ = std::move(*server); std::printf("session listening %u\n", static_cast(bridge.server_->port())); return bridge; } [[nodiscard]] bool enabled() const noexcept { return server_ != nullptr; } [[nodiscard]] std::optional carry(std::uint64_t tick) { return server_ ? server_->carry(tick) : std::nullopt; } void committed(const persistence::Uuid& request, std::uint64_t tick, std::uint64_t sequence) { if (server_) server_->committed(request, tick, sequence); } void not_committed(const persistence::Uuid& request) { if (server_) server_->not_committed(request); } // Publishes a view when the simulated tick moved, then services connections. template void step(const State* state, const server::HostStatus& s) { if (!server_) return; const network::RegionFacts facts{s.ready && s.phase == server::HostPhase::running, s.epoch, s.simulated_tick, s.committed_tick, s.committed_sequence}; if (state != nullptr && facts.ready && (!published_ || *published_ != s.simulated_tick || epoch_ != s.epoch)) publish(community(*state), facts); server_->poll(facts, wall_seconds(), std::chrono::steady_clock::now()); for (const auto& e : server_->take_events()) std::printf("session %s %llu subject=%llu reason=%u\n", e.kind == network::SessionEvent::Kind::opened ? "opened" : e.kind == network::SessionEvent::Kind::closed ? "closed" : "refused", static_cast(e.session), static_cast(e.subject), static_cast(e.reason)); } private: static std::uint64_t wall_seconds() { return static_cast(std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()) .count()); } static std::optional fail(const char* message) { std::puts(message); return std::nullopt; } static std::string read(const std::string& path) { std::ifstream file{path, std::ios::binary}; return {std::istreambuf_iterator{file}, std::istreambuf_iterator{}}; } static const processes::EnvironmentalCommunity& community(const processes::EnvironmentalCommunity& c) { return c; } static const processes::EnvironmentalCommunity& community(const processes::LineageRegion& r) { return r.community(); } // A read-only copy of the authoritative stores: every habitat in the first site's // Z slice. The region has pooled producer/consumer inventories and no placed // individuals, so the organism presentation is explicitly empty. void publish(const processes::EnvironmentalCommunity& c, const network::RegionFacts& facts) { const auto& fields = c.resources().fields(); const auto sites = fields.sites(); if (sites.empty()) return; const auto z = sites.front().voxel.z; std::vector footprint; for (const auto& site : sites) if (site.voxel.z == z && footprint.size() < world::max_environment_footprint) footprint.push_back(site.voxel); const auto view = world::query_environment(fields, c.resources().terrain(), facts.simulated_tick, footprint, world::all_environment_fields); if (!view) { if (!warned_) std::printf("session view-unavailable %u\n", static_cast(view.error())); warned_ = true; return; } auto environment = client::present_environment(*view, z); auto organisms = client::OrganismPresentation::create( {view->identity(), facts.simulated_tick, 0}, z, {}); if (!environment || !organisms) return; server_->publish(*environment, *organisms, facts); published_ = facts.simulated_tick; epoch_ = facts.epoch; } std::unique_ptr server_; std::optional published_; std::uint64_t epoch_{}; bool warned_{}; }; #else inline constexpr bool sessions_available = false; class SessionBridge { public: static std::optional open(const SessionOptions&, persistence::RegionKey, std::uint64_t) { return SessionBridge{}; } [[nodiscard]] bool enabled() const noexcept { return false; } [[nodiscard]] std::optional carry(std::uint64_t) { return std::nullopt; } void committed(const persistence::Uuid&, std::uint64_t, std::uint64_t) {} void not_committed(const persistence::Uuid&) {} template void step(const State*, const server::HostStatus&) {} }; #endif } // namespace evo::server_app