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,415 @@
|
||||
#include "evo/network/session_server.hpp"
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::network {
|
||||
namespace {
|
||||
using Clock = std::chrono::steady_clock;
|
||||
constexpr std::size_t max_accepts_per_poll = 4;
|
||||
constexpr std::size_t max_read_per_poll = 65536;
|
||||
CloseReason close_reason(AdmissionError e) {
|
||||
switch (e) {
|
||||
case AdmissionError::duplicate_subject:
|
||||
return CloseReason::duplicate_session;
|
||||
case AdmissionError::expired:
|
||||
return CloseReason::expired;
|
||||
case AdmissionError::sessions_full:
|
||||
case AdmissionError::grants_full:
|
||||
return CloseReason::capacity;
|
||||
default:
|
||||
return CloseReason::unauthenticated;
|
||||
}
|
||||
}
|
||||
CloseReason close_reason(ProtocolError e) {
|
||||
return e == ProtocolError::unsupported_version ? CloseReason::unsupported_version
|
||||
: CloseReason::malformed;
|
||||
}
|
||||
void append(std::vector<std::uint8_t>& out, const std::vector<std::uint8_t>& bytes) {
|
||||
out.insert(out.end(), bytes.begin(), bytes.end());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
struct SessionServer::Impl {
|
||||
enum class State : std::uint8_t { handshake, hello, established, closing };
|
||||
struct Connection {
|
||||
std::uint64_t id{};
|
||||
std::unique_ptr<TcpStream> stream; // Declared first: outlives the channel.
|
||||
std::unique_ptr<TlsChannel> tls;
|
||||
State state{State::handshake};
|
||||
Clock::time_point deadline;
|
||||
FrameReader reader;
|
||||
std::vector<std::uint8_t> out, view;
|
||||
std::uint64_t session{}, subject{}, epoch{}, expires_at{};
|
||||
std::size_t window_bytes{};
|
||||
Clock::time_point window_start;
|
||||
bool dead{};
|
||||
};
|
||||
struct Pending {
|
||||
RequestId request{};
|
||||
std::uint64_t intended_tick{}, connection{}, sequence{};
|
||||
};
|
||||
struct Carried {
|
||||
RequestId request{};
|
||||
std::uint64_t connection{}, sequence{};
|
||||
};
|
||||
|
||||
Impl(SessionServerConfig c, DevGrantKey k, std::unique_ptr<TcpListener> l)
|
||||
: config{std::move(c)}, key{std::move(k)}, listener{std::move(l)} {}
|
||||
|
||||
SessionServerConfig config;
|
||||
DevGrantKey key;
|
||||
std::unique_ptr<TcpListener> listener;
|
||||
std::optional<SessionAdmission> admission;
|
||||
std::vector<Connection> connections;
|
||||
std::deque<Pending> pending;
|
||||
std::vector<Carried> carried;
|
||||
std::vector<SessionEvent> events;
|
||||
std::optional<client::EnvironmentPresentation> environment;
|
||||
std::optional<client::OrganismPresentation> organisms;
|
||||
RegionFacts published{};
|
||||
std::uint64_t baseline{}, next_connection{1};
|
||||
Clock::time_point polled_at{}; // Time of the current poll, for Close deadlines.
|
||||
|
||||
Connection* find(std::uint64_t id) {
|
||||
const auto it = std::ranges::find_if(connections, [&](const Connection& c) {
|
||||
return c.id == id && !c.dead && c.state == State::established;
|
||||
});
|
||||
return it == connections.end() ? nullptr : &*it;
|
||||
}
|
||||
void close(Connection& c, CloseReason reason) {
|
||||
if (c.state == State::closing || c.dead)
|
||||
return;
|
||||
if (c.state == State::hello || c.state == State::established)
|
||||
append(c.out, encode(reason));
|
||||
else
|
||||
c.dead = true; // No channel yet to say anything over.
|
||||
events.push_back({c.state == State::established ? SessionEvent::Kind::closed
|
||||
: SessionEvent::Kind::refused,
|
||||
c.session, c.subject, reason});
|
||||
c.state = State::closing;
|
||||
c.deadline = polled_at + std::chrono::seconds{1}; // Bounded flush of the Close frame.
|
||||
c.view.clear();
|
||||
}
|
||||
std::vector<std::uint8_t> views_for(const Connection& c) const {
|
||||
std::vector<std::uint8_t> bytes;
|
||||
if (!environment || !organisms)
|
||||
return bytes;
|
||||
const ViewHeader header{c.session, c.epoch, published.simulated_tick,
|
||||
published.committed_tick, baseline};
|
||||
append(bytes, encode(header, *environment));
|
||||
append(bytes, encode(header, *organisms));
|
||||
return bytes;
|
||||
}
|
||||
void hello(Connection& c, const OwnedFrame& f, const RegionFacts& facts,
|
||||
std::uint64_t now_seconds) {
|
||||
if (f.type != MessageType::hello) {
|
||||
close(c, CloseReason::malformed);
|
||||
return;
|
||||
}
|
||||
const auto message = decode_hello({f.type, f.payload});
|
||||
if (!message) {
|
||||
close(c, close_reason(message.error()));
|
||||
return;
|
||||
}
|
||||
if (!admission || !facts.ready || facts.epoch != admission->epoch()) {
|
||||
close(c, CloseReason::server_shutdown); // No current authority: retry later.
|
||||
return;
|
||||
}
|
||||
const auto grant = key.verify(message->grant);
|
||||
if (!grant) {
|
||||
close(c, CloseReason::unauthenticated);
|
||||
return;
|
||||
}
|
||||
const auto welcome =
|
||||
admission->open(*grant, now_seconds, facts.simulated_tick, facts.committed_tick);
|
||||
if (!welcome) {
|
||||
c.subject = grant->claims().subject;
|
||||
close(c, close_reason(welcome.error()));
|
||||
return;
|
||||
}
|
||||
c.session = welcome->session;
|
||||
c.subject = grant->claims().subject;
|
||||
c.epoch = welcome->epoch;
|
||||
c.expires_at = welcome->grant_expires_at;
|
||||
c.state = State::established;
|
||||
append(c.out, encode(*welcome));
|
||||
c.view = views_for(c); // Fresh baseline for every new session.
|
||||
events.push_back({SessionEvent::Kind::opened, c.session, c.subject, {}});
|
||||
}
|
||||
void intent(Connection& c, const OwnedFrame& f, const RegionFacts& facts,
|
||||
std::uint64_t now_seconds) {
|
||||
const auto m = decode_intent({f.type, f.payload});
|
||||
if (!m) {
|
||||
close(c, close_reason(m.error()));
|
||||
return;
|
||||
}
|
||||
IntentResult result{m->sequence, IntentStatus::unknown_session, facts.simulated_tick};
|
||||
// A connection may only speak for its own session.
|
||||
if (m->session == c.session && admission) {
|
||||
if (pending.size() >= config.max_pending_intents)
|
||||
result.status = IntentStatus::rate_limited; // Host backlog, not admission.
|
||||
else
|
||||
result = admission->admit(*m, facts.simulated_tick, now_seconds);
|
||||
}
|
||||
if (result.status == IntentStatus::admitted)
|
||||
pending.push_back({request_id(c, m->sequence), m->tick, c.id, m->sequence});
|
||||
append(c.out, encode(result));
|
||||
}
|
||||
RequestId request_id(const Connection& c, std::uint64_t sequence) const {
|
||||
RequestId id{'I', 'N', 'T', '1'};
|
||||
const std::array<std::uint64_t, 3> parts{c.epoch, c.session, sequence};
|
||||
for (std::size_t p = 0; p < parts.size(); ++p)
|
||||
for (unsigned i = 0; i < 4; ++i)
|
||||
id.at(4 + 4 * p + i) = static_cast<std::uint8_t>(parts.at(p) >> (8U * (3U - i)));
|
||||
return id;
|
||||
}
|
||||
void receive(Connection& c, const RegionFacts& facts, std::uint64_t now_seconds,
|
||||
Clock::time_point now) {
|
||||
std::array<std::uint8_t, 16384> buffer{};
|
||||
std::size_t total = 0;
|
||||
while (!c.dead && c.state != State::closing && total < max_read_per_poll) {
|
||||
const auto n = c.tls->receive(buffer);
|
||||
if (!n) {
|
||||
c.dead = true;
|
||||
return;
|
||||
}
|
||||
if (*n == 0)
|
||||
return;
|
||||
total += *n;
|
||||
if (now - c.window_start >= std::chrono::seconds{1}) {
|
||||
c.window_start = now;
|
||||
c.window_bytes = 0;
|
||||
}
|
||||
c.window_bytes += *n;
|
||||
if (c.window_bytes > config.max_input_bytes_per_second) {
|
||||
close(c, CloseReason::rate_limited);
|
||||
return;
|
||||
}
|
||||
std::span<const std::uint8_t> rest{buffer.data(), *n};
|
||||
while (!rest.empty() && c.state != State::closing) {
|
||||
const auto taken = c.reader.feed(rest);
|
||||
if (!taken) {
|
||||
close(c, close_reason(taken.error()));
|
||||
return;
|
||||
}
|
||||
rest = rest.subspan(*taken);
|
||||
while (auto f = c.reader.take()) {
|
||||
if (c.state == State::hello)
|
||||
hello(c, *f, facts, now_seconds);
|
||||
else if (f->type == MessageType::intent)
|
||||
intent(c, *f, facts, now_seconds);
|
||||
else if (f->type == MessageType::close) {
|
||||
c.state = State::closing; // Peer is leaving; nothing to say.
|
||||
events.push_back({SessionEvent::Kind::closed, c.session, c.subject,
|
||||
CloseReason::normal});
|
||||
} else
|
||||
close(c, CloseReason::malformed);
|
||||
if (c.state == State::closing)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
void transmit(Connection& c) {
|
||||
if (c.out.empty() && !c.view.empty() && c.state == State::established)
|
||||
c.out.swap(c.view); // Only the newest view waits; it is never queued twice.
|
||||
std::size_t sent = 0;
|
||||
while (sent < c.out.size()) {
|
||||
const auto n = c.tls->send(std::span{c.out}.subspan(sent));
|
||||
if (!n) {
|
||||
c.dead = true;
|
||||
return;
|
||||
}
|
||||
if (*n == 0)
|
||||
break;
|
||||
sent += *n;
|
||||
}
|
||||
c.out.erase(c.out.begin(), c.out.begin() + static_cast<std::ptrdiff_t>(sent));
|
||||
if (c.out.size() + c.view.size() > config.max_output_bytes)
|
||||
c.dead = true; // Slow consumer: drop rather than buffer without bound.
|
||||
if (c.state == State::closing && c.out.empty()) {
|
||||
// Best-effort close_notify: the connection is dropped either way.
|
||||
static_cast<void>(c.tls->close().has_value());
|
||||
c.dead = true;
|
||||
}
|
||||
}
|
||||
void step(Connection& c, const RegionFacts& facts, std::uint64_t now_seconds,
|
||||
Clock::time_point now) {
|
||||
if (c.state == State::handshake) {
|
||||
const auto p = c.tls->handshake();
|
||||
if (!p) {
|
||||
c.dead = true;
|
||||
return;
|
||||
}
|
||||
if (*p == TlsProgress::done)
|
||||
c.state = State::hello;
|
||||
}
|
||||
if (c.state == State::closing && now > c.deadline) {
|
||||
c.dead = true; // Peer would not take the Close frame.
|
||||
return;
|
||||
}
|
||||
if ((c.state == State::handshake || c.state == State::hello) && now > c.deadline)
|
||||
close(c, CloseReason::unauthenticated);
|
||||
if (c.state == State::established && now_seconds >= c.expires_at)
|
||||
close(c, CloseReason::expired);
|
||||
if (c.state == State::established && admission && c.epoch != admission->epoch())
|
||||
close(c, CloseReason::stale_epoch);
|
||||
if (c.state == State::hello || c.state == State::established)
|
||||
receive(c, facts, now_seconds, now);
|
||||
if (!c.dead && c.state != State::handshake)
|
||||
transmit(c);
|
||||
}
|
||||
void bury() {
|
||||
for (auto& c : connections) {
|
||||
if (!c.dead)
|
||||
continue;
|
||||
if (c.session != 0 && admission)
|
||||
// The session is ours; only an already-closed session could fail.
|
||||
static_cast<void>(admission->close(c.session).has_value());
|
||||
std::erase_if(pending, [&](const Pending& p) { return p.connection == c.id; });
|
||||
}
|
||||
std::erase_if(connections, [](const Connection& c) { return c.dead; });
|
||||
}
|
||||
};
|
||||
|
||||
SessionServer::SessionServer(std::unique_ptr<Impl> impl) : impl_{std::move(impl)} {}
|
||||
SessionServer::~SessionServer() = default;
|
||||
|
||||
std::expected<std::unique_ptr<SessionServer>, SessionServerError>
|
||||
SessionServer::create(SessionServerConfig config, DevGrantKey key) {
|
||||
if (config.max_connections == 0 || config.max_connections > 64 ||
|
||||
config.max_pending_intents == 0 || config.max_carried_intents == 0 ||
|
||||
config.max_output_bytes < 2 * (frame_header_bytes + max_frame_payload) ||
|
||||
config.max_input_bytes_per_second < frame_header_bytes + max_grant_bytes + 6)
|
||||
return std::unexpected(SessionServerError::configuration);
|
||||
// Validate the admission limits now, with a placeholder epoch.
|
||||
auto probe = config.admission;
|
||||
probe.epoch = 1;
|
||||
if (!SessionAdmission::create(probe))
|
||||
return std::unexpected(SessionServerError::configuration);
|
||||
// Credentials must parse before the port opens.
|
||||
struct Null final : ByteTransport {
|
||||
std::expected<std::size_t, TransportError> write(std::span<const std::uint8_t>) override {
|
||||
return 0;
|
||||
}
|
||||
std::expected<std::size_t, TransportError> read(std::span<std::uint8_t>) override {
|
||||
return 0;
|
||||
}
|
||||
} null;
|
||||
if (!TlsChannel::server(config.credentials, null))
|
||||
return std::unexpected(SessionServerError::credentials);
|
||||
auto listener = TcpListener::listen(config.listen);
|
||||
if (!listener)
|
||||
return std::unexpected(SessionServerError::listen);
|
||||
return std::unique_ptr<SessionServer>(new SessionServer(
|
||||
std::make_unique<Impl>(std::move(config), std::move(key), std::move(*listener))));
|
||||
}
|
||||
|
||||
std::uint16_t SessionServer::port() const noexcept {
|
||||
return impl_->listener->port();
|
||||
}
|
||||
|
||||
void SessionServer::poll(const RegionFacts& facts, std::uint64_t now_seconds,
|
||||
std::chrono::steady_clock::time_point now) {
|
||||
auto& m = *impl_;
|
||||
m.polled_at = now;
|
||||
if (facts.ready && facts.epoch != 0) {
|
||||
if (!m.admission) {
|
||||
auto config = m.config.admission;
|
||||
config.epoch = facts.epoch;
|
||||
if (auto created = SessionAdmission::create(config))
|
||||
m.admission.emplace(std::move(*created));
|
||||
} else if (facts.epoch > m.admission->epoch()) {
|
||||
// Strictly newer (checked above), so the advance cannot be refused.
|
||||
static_cast<void>(m.admission->advance_epoch(facts.epoch).has_value());
|
||||
// Intentions of the old epoch never ride new-epoch inputs.
|
||||
m.pending.clear();
|
||||
}
|
||||
}
|
||||
for (std::size_t i = 0; i < max_accepts_per_poll; ++i) {
|
||||
auto accepted = m.listener->accept();
|
||||
if (!accepted || !*accepted)
|
||||
break;
|
||||
if (m.connections.size() >= m.config.max_connections)
|
||||
continue; // Over capacity: the stream closes as it goes out of scope.
|
||||
Impl::Connection c;
|
||||
c.id = m.next_connection++;
|
||||
c.stream = std::move(*accepted);
|
||||
auto tls = TlsChannel::server(m.config.credentials, *c.stream);
|
||||
if (!tls)
|
||||
continue;
|
||||
c.tls = std::move(*tls);
|
||||
c.deadline = now + m.config.hello_deadline;
|
||||
c.window_start = now;
|
||||
m.connections.push_back(std::move(c));
|
||||
}
|
||||
for (auto& c : m.connections)
|
||||
if (!c.dead)
|
||||
m.step(c, facts, now_seconds, now);
|
||||
m.bury();
|
||||
}
|
||||
|
||||
void SessionServer::publish(const client::EnvironmentPresentation& environment,
|
||||
const client::OrganismPresentation& organisms,
|
||||
const RegionFacts& facts) {
|
||||
auto& m = *impl_;
|
||||
m.environment = environment;
|
||||
m.organisms = organisms;
|
||||
m.published = facts;
|
||||
++m.baseline;
|
||||
for (auto& c : m.connections)
|
||||
if (!c.dead && c.state == Impl::State::established)
|
||||
c.view = m.views_for(c);
|
||||
}
|
||||
|
||||
std::optional<RequestId> SessionServer::carry(std::uint64_t tick) {
|
||||
auto& m = *impl_;
|
||||
if (m.carried.size() >= m.config.max_carried_intents)
|
||||
return std::nullopt;
|
||||
const auto it = std::ranges::find_if(
|
||||
m.pending, [&](const Impl::Pending& p) { return p.intended_tick <= tick; });
|
||||
if (it == m.pending.end())
|
||||
return std::nullopt;
|
||||
const Impl::Carried carried{it->request, it->connection, it->sequence};
|
||||
m.pending.erase(it);
|
||||
m.carried.push_back(carried);
|
||||
return carried.request;
|
||||
}
|
||||
|
||||
void SessionServer::committed(const RequestId& request, std::uint64_t tick,
|
||||
std::uint64_t commit_sequence) {
|
||||
auto& m = *impl_;
|
||||
const auto it = std::ranges::find_if(
|
||||
m.carried, [&](const Impl::Carried& c) { return c.request == request; });
|
||||
if (it == m.carried.end())
|
||||
return;
|
||||
if (auto* c = m.find(it->connection))
|
||||
append(c->out, encode(IntentOutcome{it->sequence, IntentFinality::committed_no_effect, tick,
|
||||
commit_sequence}));
|
||||
m.carried.erase(it);
|
||||
}
|
||||
|
||||
void SessionServer::not_committed(const RequestId& request) {
|
||||
auto& m = *impl_;
|
||||
const auto it = std::ranges::find_if(
|
||||
m.carried, [&](const Impl::Carried& c) { return c.request == request; });
|
||||
if (it == m.carried.end())
|
||||
return;
|
||||
if (auto* c = m.find(it->connection))
|
||||
append(c->out, encode(IntentOutcome{it->sequence, IntentFinality::not_committed, 0, 0}));
|
||||
m.carried.erase(it);
|
||||
}
|
||||
|
||||
std::vector<SessionEvent> SessionServer::take_events() {
|
||||
return std::exchange(impl_->events, {});
|
||||
}
|
||||
|
||||
std::size_t SessionServer::connections() const noexcept {
|
||||
return impl_->connections.size();
|
||||
}
|
||||
|
||||
std::size_t SessionServer::sessions() const noexcept {
|
||||
const auto& admission = impl_->admission;
|
||||
return admission ? admission->sessions() : 0;
|
||||
}
|
||||
} // namespace evo::network
|
||||
Reference in New Issue
Block a user