Files
aevolution/include/evo/network/session_server.hpp
T
dingoandClaude Opus 5.5 c588be237c Fix M9.4-M9.7 review findings in the dev session stack
- 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>
2026-10-02 05:44:44 -04:00

85 lines
4.0 KiB
C++

#pragma once
#include "evo/network/admission.hpp"
#include "evo/network/dev_grant.hpp"
#include "evo/network/grant_ledger.hpp"
#include "evo/network/tcp.hpp"
#include "evo/network/tls_channel.hpp"
#include <array>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <deque>
#include <expected>
#include <filesystem>
#include <memory>
#include <optional>
#include <string>
#include <vector>
// M9.7 dev session server (ADR-0083). Host-agnostic and non-blocking: the region
// host calls poll() from its own loop, publishes views of its state, lets admitted
// intentions ride host inputs via carry(), and reports durable/lost outcomes. It
// never touches simulation state and never decides an outcome.
namespace evo::network {
using RequestId = std::array<std::uint8_t, 16>;
struct SessionServerConfig {
std::string listen; // Numeric address:port.
ServerCredentials credentials;
AdmissionConfig admission; // `epoch` is ignored: it follows RegionFacts.
std::size_t max_connections{4};
std::chrono::milliseconds hello_deadline{5000}; // Accept to Welcome.
std::size_t max_output_bytes{std::size_t{1} << 20};
std::size_t max_input_bytes_per_second{65536};
std::size_t max_pending_intents{64}; // Admitted, not yet carried.
std::size_t max_carried_intents{1024}; // Carried, awaiting durable/lost.
// Liveness both ways: a Heartbeat is sent when nothing else was sent for an
// interval; a peer silent for the timeout is closed with `timeout`.
std::chrono::milliseconds heartbeat_interval{1000}, liveness_timeout{5000};
// Durable consumed-grant ledger (required); survives process and pod restarts.
std::filesystem::path grant_ledger;
};
// The host's current authority facts, read once per loop iteration.
struct RegionFacts {
bool ready{};
std::uint64_t epoch{}, simulated_tick{}, committed_tick{}, commit_sequence{};
};
enum class SessionServerError : std::uint8_t { configuration, listen, credentials, ledger };
struct SessionEvent {
enum class Kind : std::uint8_t { opened, closed, refused } kind{};
std::uint64_t session{}, subject{};
CloseReason reason{};
};
class SessionServer {
public:
[[nodiscard]] static std::expected<std::unique_ptr<SessionServer>, SessionServerError>
create(SessionServerConfig config, DevGrantKey key, std::uint64_t now_seconds);
SessionServer(const SessionServer&) = delete;
SessionServer& operator=(const SessionServer&) = delete;
~SessionServer();
[[nodiscard]] std::uint16_t port() const noexcept;
// Accepts, handshakes, admits and writes; never blocks. `now_seconds` is wall
// time for grant validity; `now` is monotonic time for connection deadlines.
void poll(const RegionFacts& facts, std::uint64_t now_seconds,
std::chrono::steady_clock::time_point now);
// The latest views; each session receives only the newest (older unsent views are
// replaced, never queued). New sessions receive the latest as their baseline.
void publish(const client::EnvironmentPresentation& environment,
const client::OrganismPresentation& organisms, const RegionFacts& facts);
// The oldest admitted intention whose intended tick is <= `tick`, to ride the host
// input for `tick`, under a fresh server-issued carrier id ('I','N', 48-bit epoch,
// 64-bit ordinal). The host input's content stays host-chosen.
[[nodiscard]] std::optional<RequestId> carry(std::uint64_t tick);
// Host final acknowledgement / drop for a request id; ids not carried here are ignored.
void committed(const RequestId& request, std::uint64_t tick, std::uint64_t commit_sequence);
void not_committed(const RequestId& request);
[[nodiscard]] std::vector<SessionEvent> take_events();
[[nodiscard]] std::size_t connections() const noexcept;
[[nodiscard]] std::size_t sessions() const noexcept;
private:
struct Impl;
explicit SessionServer(std::unique_ptr<Impl> impl);
std::unique_ptr<Impl> impl_;
};
} // namespace evo::network