- 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>
105 lines
4.1 KiB
C++
105 lines
4.1 KiB
C++
#pragma once
|
|
#include "evo/network/protocol.hpp"
|
|
#include "evo/world/chunks.hpp"
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <expected>
|
|
#include <span>
|
|
#include <vector>
|
|
|
|
// Server-side session admission for the M9.6 dev session (ADR-0082). Transport-
|
|
// neutral and single-owner: the host serializes calls. It admits intentions for
|
|
// scheduling only; the simulation still validates and decides every outcome.
|
|
namespace evo::network {
|
|
enum class DeploymentEnvironment : std::uint8_t { dev, stage, world0 };
|
|
// Claims a dev grant carries; meaningful only once verified.
|
|
struct DevGrantClaims {
|
|
std::uint64_t grant_id{}, subject{};
|
|
world::WorldIdentity world;
|
|
std::uint64_t region{}, issued_at{}, expires_at{}; // Server clock, seconds.
|
|
};
|
|
// A grant already used to open a session, remembered until it expires.
|
|
struct ConsumedGrant {
|
|
std::uint64_t grant_id{}, expires_at{};
|
|
};
|
|
class DevGrantKey;
|
|
// Only DevGrantKey::verify constructs this, so admission cannot be handed
|
|
// unauthenticated claims by accident.
|
|
class VerifiedGrant {
|
|
public:
|
|
[[nodiscard]] const DevGrantClaims& claims() const noexcept {
|
|
return claims_;
|
|
}
|
|
|
|
private:
|
|
friend class DevGrantKey;
|
|
explicit VerifiedGrant(DevGrantClaims claims) : claims_{claims} {}
|
|
DevGrantClaims claims_;
|
|
};
|
|
struct AdmissionConfig {
|
|
world::WorldIdentity world;
|
|
std::uint64_t region{}, epoch{};
|
|
std::size_t max_sessions{16};
|
|
std::size_t max_remembered_grants{4096};
|
|
std::uint32_t intent_window{8}; // Ticks ahead of the server tick.
|
|
std::uint32_t window_ticks{8}; // Rate window length.
|
|
std::uint32_t max_intents_per_window{16}; // Counts every attempt by a session.
|
|
std::uint64_t max_grant_lifetime{3600}; // Seconds.
|
|
std::uint64_t clock_skew{30}; // Seconds tolerated before issued_at.
|
|
};
|
|
enum class AdmissionError : std::uint8_t {
|
|
invalid_config,
|
|
wrong_world,
|
|
wrong_region,
|
|
invalid_lifetime,
|
|
not_yet_valid,
|
|
expired,
|
|
grant_reused,
|
|
duplicate_subject,
|
|
sessions_full,
|
|
grants_full,
|
|
unknown_session
|
|
};
|
|
class SessionAdmission {
|
|
public:
|
|
[[nodiscard]] static std::expected<SessionAdmission, AdmissionError>
|
|
create(AdmissionConfig config);
|
|
// Single-use grants: a grant id is remembered until it expires. A subject may hold
|
|
// one session; a second open is refused (control is never duplicated).
|
|
[[nodiscard]] std::expected<Welcome, AdmissionError> open(const VerifiedGrant& grant,
|
|
std::uint64_t now_seconds,
|
|
std::uint64_t server_tick,
|
|
std::uint64_t commit_watermark);
|
|
[[nodiscard]] IntentResult admit(const IntentMessage& intent, std::uint64_t server_tick,
|
|
std::uint64_t now_seconds);
|
|
[[nodiscard]] std::expected<void, AdmissionError> close(std::uint64_t session);
|
|
// Restores grants consumed before a restart (from a durable ledger), so a replacement
|
|
// host refuses them too. Expired entries are skipped; too many fail closed.
|
|
[[nodiscard]] std::expected<void, AdmissionError>
|
|
remember(std::span<const ConsumedGrant> consumed, std::uint64_t now_seconds);
|
|
// New ownership epoch: existing sessions become stale and must reconnect.
|
|
[[nodiscard]] std::expected<void, AdmissionError> advance_epoch(std::uint64_t epoch);
|
|
[[nodiscard]] std::size_t sessions() const noexcept {
|
|
return sessions_.size();
|
|
}
|
|
[[nodiscard]] std::uint64_t epoch() const noexcept {
|
|
return config_.epoch;
|
|
}
|
|
|
|
private:
|
|
struct Session {
|
|
std::uint64_t id{}, subject{}, epoch{}, expires_at{}, last_sequence{};
|
|
std::uint64_t window_start{};
|
|
std::uint32_t attempts{};
|
|
};
|
|
struct Remembered {
|
|
std::uint64_t grant_id{}, expires_at{};
|
|
};
|
|
explicit SessionAdmission(AdmissionConfig config) : config_{config} {}
|
|
AdmissionConfig config_;
|
|
std::uint64_t next_session_{1};
|
|
std::vector<Session> sessions_;
|
|
std::vector<Remembered> grants_;
|
|
};
|
|
} // namespace evo::network
|