Implement M9.7 dev region sessions with durable no-effect intentions
Run a non-blocking TLS 1.3 session server inside evo-server's loop: environment views come from the region's own field/terrain stores with an explicitly empty organism presentation; admitted intentions ride the next synthetic input (content unchanged) and return a durable no-effect outcome from the host's final acknowledgement, or not_committed when dropped. Sessions are bound to their connection, epochs close stale sessions, and connections have bounded output, input rate and deadlines. SessionClient and evo-client --connect consume one single-use grant per Hello (refusals before admission keep it), reset to a fresh baseline on every session, keep stale views visible and report in-flight intentions as unknown after loss. evo-dev-session generates dev credentials, grant bundles and runs a restart probe. The dev deployment gains a drill-time, loopback-only listener with an optional Secret, enforced by the policy check, and tools/ops/session-drill.sh. Protocol v1 gains IntentOutcome and the capacity close reason before any deployment. ADR-0083 records the maintainer-confirmed decisions; M8-dev/stage remains not passed and no cluster change was made. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
#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, environment;
|
||||
[[nodiscard]] bool any() const noexcept {
|
||||
return !listen.empty() || !certificate.empty() || !key.empty() || !grant_key.empty() ||
|
||||
!environment.empty();
|
||||
}
|
||||
[[nodiscard]] bool complete() const noexcept {
|
||||
return !listen.empty() && !certificate.empty() && !key.empty() && !grant_key.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;
|
||||
auto server = network::SessionServer::create(std::move(config), std::move(*grant_key));
|
||||
if (!server)
|
||||
return fail(server.error() == network::SessionServerError::listen
|
||||
? "error session-listen"
|
||||
: "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,
|
||||
static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::seconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch())
|
||||
.count()),
|
||||
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::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
|
||||
Reference in New Issue
Block a user