#pragma once // M9.7/M9.8c remote dev session for the engineering client (ADR-0083). Without // EVO_CLIENT_NETWORK (a build without EVO_BUILD_NETWORK) remote mode is unavailable. // The client only presents copies and sends bounded intentions; it never predicts. // Grant bytes stay inside the session client: never printed, shown or persisted. #include "evo/client/environment_view.hpp" #include "evo/client/input.hpp" #include "evo/client/launcher.hpp" #include "evo/client/organism_view.hpp" #include "evo/client/outcome_log.hpp" #include "evo/client/ui_state.hpp" #include #include #include #include #include #include #ifdef EVO_CLIENT_NETWORK #include "evo/network/session_client.hpp" #include #endif namespace evo::client_app { struct RemoteStatus { std::string phase; // connecting / connected / reconnecting / out of grants std::uint64_t session{}, epoch{}, server_tick{}, commit_watermark{}; std::size_t grants_left{}; bool welcomed{}; }; #ifdef EVO_CLIENT_NETWORK inline constexpr bool remote_available = true; static_assert(std::is_same_v, "the launcher's grant record is the session's grant"); class RemoteSession { public: static std::optional open(const client::ConnectMaterial& m) { network::SessionClientConfig config; config.host = m.endpoint.host; config.port = m.endpoint.port; config.trust = {m.authority_pem, m.server_name}; config.grants = m.grants; auto made = network::SessionClient::create(std::move(config)); if (!made) return std::nullopt; RemoteSession session; session.client_ = std::move(*made); return session; } [[nodiscard]] const client::EnvironmentLayer& environment() const noexcept { return client_->environment(); } [[nodiscard]] const client::OrganismLayer& organisms() const noexcept { return client_->organisms(); } [[nodiscard]] const client::OutcomeLog& outcomes() const noexcept { return log_; } [[nodiscard]] RemoteStatus status() const { using network::SessionPhase; RemoteStatus s; const auto phase = client_->phase(); s.phase = phase == SessionPhase::established ? "connected" : phase == SessionPhase::exhausted ? "out of grants" : welcomed_ ? "reconnecting" : "connecting"; if (const auto w = client_->welcome()) { s.session = w->session; s.epoch = w->epoch; } if (const auto v = client_->latest_view()) { s.server_tick = v->server_tick; s.commit_watermark = v->commit_watermark; } s.grants_left = client_->grants_left(); s.welcomed = welcomed_; return s; } // Services the connection; sends the intention when its vector changed, at most once // per received server tick (the server also rate-limits). Session events update the // outcome log (the durable record), short notifications and the launcher. void step(std::chrono::steady_clock::time_point now, const client::Intention& intention, client::UiState& ui, client::Launcher& launcher) { client_->poll(now); const auto view = client_->latest_view(); const bool changed = intention.move_x != sent_x_ || intention.move_y != sent_y_; if (view && changed && (!sent_tick_ || view->server_tick > *sent_tick_) && client_->send(intention)) { sent_x_ = intention.move_x; sent_y_ = intention.move_y; sent_tick_ = view->server_tick; log_.record({intention.sequence, client::OutcomeState::sent}); } for (const auto& e : client_->take_events()) report(e, ui, launcher); // Refused TCP or failed TLS produce no event; each still counts toward the // launcher's bounded "server unreachable" before a first Welcome. for (; unreachable_ < client_->unreachable_attempts(); ++unreachable_) launcher.observe(client::ConnectSignal::lost); } private: static void notify(client::UiState& ui, const char* text, client::NoticeLevel level) { static_cast(ui.notify(text, level).has_value()); // Bounded; overflow evicts. } void report(const network::ClientEvent& e, client::UiState& ui, client::Launcher& launcher) { using Kind = network::ClientEvent::Kind; using network::CloseReason; switch (e.kind) { case Kind::welcome: welcomed_ = true; sent_tick_.reset(); // New session: resend the current intention. sent_x_ = sent_y_ = 0; launcher.observe(client::ConnectSignal::welcome); notify(ui, "Connected to the dev region (fresh baseline).", client::NoticeLevel::info); std::printf("session welcome session=%llu epoch=%llu tick=%llu commit=%llu\n", static_cast(e.welcome.session), static_cast(e.welcome.epoch), static_cast(e.welcome.server_tick), static_cast(e.welcome.commit_watermark)); break; case Kind::closed: { const bool retry = e.reason == CloseReason::server_shutdown || e.reason == CloseReason::capacity; const bool lost = e.reason == CloseReason::timeout || e.reason == CloseReason::stale_epoch || e.reason == CloseReason::rate_limited; launcher.observe(retry ? client::ConnectSignal::not_ready : lost ? client::ConnectSignal::lost : client::ConnectSignal::refused); notify(ui, retry ? "Server not ready; retrying." : "Session closed by the server.", retry ? client::NoticeLevel::info : client::NoticeLevel::warning); std::printf("session closed reason=%u\n", static_cast(e.reason)); break; } case Kind::lost: launcher.observe(client::ConnectSignal::lost); notify(ui, "Connection lost; views are stale.", client::NoticeLevel::warning); std::puts("session lost (reconnecting with a fresh grant)"); break; case Kind::result: log_.record({e.sequence, e.result.status == network::IntentStatus::admitted ? client::OutcomeState::admitted : client::OutcomeState::rejected, static_cast(e.result.status)}); std::printf("intent %llu status=%u\n", static_cast(e.sequence), static_cast(e.result.status)); break; case Kind::outcome: log_.record({e.sequence, e.outcome.finality == network::IntentFinality::committed_no_effect ? client::OutcomeState::durable : client::OutcomeState::not_committed, 0, e.outcome.tick, e.outcome.commit_sequence}); std::printf("intent %llu final=%u tick=%llu commit=%llu\n", static_cast(e.sequence), static_cast(e.outcome.finality), static_cast(e.outcome.tick), static_cast(e.outcome.commit_sequence)); break; case Kind::unknown: log_.record({e.sequence, client::OutcomeState::unknown}); std::printf("intent %llu outcome unknown (connection lost)\n", static_cast(e.sequence)); break; case Kind::backpressure: notify(ui, "Session event backlog; reconnecting after draining.", client::NoticeLevel::warning); std::puts("session event backlog: disconnected; draining before reconnect"); break; case Kind::exhausted: launcher.observe(client::ConnectSignal::exhausted); std::puts("session grants exhausted; issue a new bundle to reconnect"); break; } std::fflush(stdout); } std::unique_ptr client_; client::OutcomeLog log_; std::optional sent_tick_; std::uint64_t unreachable_{}; std::int16_t sent_x_{}, sent_y_{}; bool welcomed_{}; }; #else inline constexpr bool remote_available = false; class RemoteSession { public: static std::optional open(const client::ConnectMaterial&) { return std::nullopt; } [[nodiscard]] const client::EnvironmentLayer& environment() const noexcept { return environment_; } [[nodiscard]] const client::OrganismLayer& organisms() const noexcept { return organisms_; } [[nodiscard]] const client::OutcomeLog& outcomes() const noexcept { return log_; } [[nodiscard]] RemoteStatus status() const { return {"unavailable in this build"}; } void step(std::chrono::steady_clock::time_point, const client::Intention&, client::UiState&, client::Launcher&) {} private: client::EnvironmentLayer environment_; client::OrganismLayer organisms_; client::OutcomeLog log_; }; #endif } // namespace evo::client_app