- TLS: replace send() with a bounded queue/flush; an interrupted record is retried with identical arguments and later frames wait behind it, so no queued frame is lost or reordered; close() flushes first. - Carriers: server-issued 'I','N' | 48-bit epoch | 64-bit ordinal ids replace the aliasing low-32-bit packing; client sequences are kept separately for replies; out-of-range epochs are refused, never truncated. - Grants: a durable, fsynced, fail-closed consumed-grant ledger (--session-grant-ledger, /objects/dev-session-grants.v1 in dev) keeps single-use grants consumed across process and pod restarts. - Liveness: protocol v1 (undeployed) gains Heartbeat and the timeout close reason; both sides heartbeat when idle and treat 5 s of silence as dead. - Client sends are bounded and unresolved tracking starts at send time. - ci-client configures with an explicit C compiler. Each finding has a regression test; ADR-0083, the schema, operations doc and VALIDATION record the corrections. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
192 lines
8.7 KiB
C++
192 lines
8.7 KiB
C++
#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 <cstdint>
|
|
#include <optional>
|
|
#include <string>
|
|
|
|
#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 <chrono>
|
|
#include <cstdio>
|
|
#include <fstream>
|
|
#include <iterator>
|
|
#include <memory>
|
|
#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<SessionBridge> 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<unsigned>(bridge.server_->port()));
|
|
return bridge;
|
|
}
|
|
[[nodiscard]] bool enabled() const noexcept {
|
|
return server_ != nullptr;
|
|
}
|
|
[[nodiscard]] std::optional<persistence::Uuid> 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 <typename State> 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<unsigned long long>(e.session),
|
|
static_cast<unsigned long long>(e.subject),
|
|
static_cast<unsigned>(e.reason));
|
|
}
|
|
|
|
private:
|
|
static std::uint64_t wall_seconds() {
|
|
return static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::seconds>(
|
|
std::chrono::system_clock::now().time_since_epoch())
|
|
.count());
|
|
}
|
|
static std::optional<SessionBridge> 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<char>{file}, std::istreambuf_iterator<char>{}};
|
|
}
|
|
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<world::VoxelAddress> 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<unsigned>(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<network::SessionServer> server_;
|
|
std::optional<std::uint64_t> published_;
|
|
std::uint64_t epoch_{};
|
|
bool warned_{};
|
|
};
|
|
#else
|
|
inline constexpr bool sessions_available = false;
|
|
class SessionBridge {
|
|
public:
|
|
static std::optional<SessionBridge> open(const SessionOptions&, persistence::RegionKey,
|
|
std::uint64_t) {
|
|
return SessionBridge{};
|
|
}
|
|
[[nodiscard]] bool enabled() const noexcept {
|
|
return false;
|
|
}
|
|
[[nodiscard]] std::optional<persistence::Uuid> 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 <typename State> void step(const State*, const server::HostStatus&) {}
|
|
};
|
|
#endif
|
|
} // namespace evo::server_app
|