Files
aevolution/include/evo/server/region_sessions.hpp
T
dingoandClaude Opus 5.5 e2f6208433
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m45s
Foundation / checks (network) (push) Successful in 9m37s
Foundation / checks (client) (push) Successful in 12m14s
Foundation / checks (tsan) (push) Successful in 7m39s
Foundation / checks (checks) (push) Successful in 31m15s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 32m59s
Add M10.4a intentions, actions and admission
Session protocol v1 (not yet deployed, amended in place) gains realtime intentions on
datagrams with receipts and a cumulative simulated watermark, and account-scoped
actions with stable ids, receipts, final outcomes and queries. IntentAdmission
enforces session binding, rate, authority, epoch, vector, sequence, tick window,
duplicates and the unresolved bound; RegionSessions and the client carry it over
QUIC. Intentions and actions have no biological effect yet. ADR-0092.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 22:09:06 -04:00

94 lines
4.7 KiB
C++

#pragma once
#include "evo/network/intent_admission.hpp"
#include "evo/network/quic_transport.hpp"
#include "evo/network/session_control.hpp"
#include "evo/network/session_grant.hpp"
#include "evo/network/session_protocol.hpp"
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <expected>
#include <memory>
#include <optional>
#include <string>
#include <vector>
// M10.3b region-side session login over QUIC (ADR-0091, schemas/network/session-v1.md).
// Owned by the region host's thread, which calls poll(). A connection must send Hello
// with a grant before the deadline; the grant is verified with the region's issuer
// public keys and admitted by SessionControl. A newer grant for an account closes the
// account's previous connection with `superseded`. Refusals and closes send Close with a
// reason, then the connection is closed after a short flush. Intentions and views are
// later segments (10.5). Events carry account log labels only, never grants.
// M10.4 (ADR-0092): established sessions send intentions (datagrams) and actions (stream);
// IntentAdmission answers with receipts, simulated watermarks and final action outcomes.
// The host reports its authority each poll, takes carried actions for its batches and
// reports durable commits.
// With a ControlLedger, control is durable and unique per world: a session that loses its
// durable control (another region accepted a newer grant) closes with `superseded`, and
// one whose control cannot be renewed in time closes with `shutdown`.
namespace evo::server {
struct IssuerKey {
std::uint32_t key_id{};
network::GrantPublicKey public_key{};
};
struct RegionSessionsConfig {
network::quic::ServerConfig transport;
network::ControlConfig control; // `epoch` is the initial ownership epoch.
std::vector<IssuerKey> issuer_keys;
std::chrono::milliseconds hello_deadline{5000}; // Connected to Hello (ADR-0089).
std::chrono::milliseconds close_flush{1000}; // Close sent to connection closed.
// Durable per-world control (10.3c), owned by the host; null keeps control in memory
// only (tests, tools). With a ledger, a session is admitted only after its durable
// claim, waits out another region's control before Welcome, renews every
// `renew_interval` and closes before its control can lapse (`safety_margin`).
network::ControlLedger* ledger{};
std::chrono::milliseconds renew_interval{5000}, safety_margin{2000};
network::IntentLimits intents{}; // max_sessions follows control.max_sessions.
};
// The region host's authority, read once per poll.
struct HostFacts {
bool ready{}; // Current authority, recovered, not draining.
std::uint64_t epoch{}; // Ownership epoch; a newer one closes every session.
std::uint64_t simulated_tick{};
};
enum class RegionSessionsError : std::uint8_t { configuration, keys, transport };
struct RegionSessionEvent {
enum class Kind : std::uint8_t { opened, closed, refused } kind{};
std::uint64_t session{}; // 0 for refusals.
std::string account; // log_label, empty when unauthenticated.
network::SessionCloseReason reason{};
};
class RegionSessions {
public:
[[nodiscard]] static std::expected<std::unique_ptr<RegionSessions>, RegionSessionsError>
create(RegionSessionsConfig config);
RegionSessions(const RegionSessions&) = delete;
RegionSessions& operator=(const RegionSessions&) = delete;
~RegionSessions();
// A newer epoch closes every session (stale_epoch) and abandons unscheduled actions.
// `now_seconds` is wall time for grants, `now` monotonic for deadlines.
void poll(const HostFacts& facts, std::uint64_t now_seconds,
std::chrono::steady_clock::time_point now);
// Actions scheduled for ticks <= `tick`, to be recorded durably with the batch that
// simulates that tick (10.4b). Each action is returned once.
[[nodiscard]] std::vector<network::CarriedAction> carry(std::uint64_t tick);
// A commit covering ticks up to `end_tick` is durable: carried actions become final and
// their outcomes are sent to each account's current session.
void committed(std::uint64_t end_tick, std::uint64_t commit_sequence);
// Closes every connection with `shutdown` (flushed by later polls).
void shutdown(std::chrono::steady_clock::time_point now);
[[nodiscard]] std::vector<RegionSessionEvent> take_events();
[[nodiscard]] std::optional<std::uint64_t> holder(const network::AccountId& account) const;
[[nodiscard]] std::size_t sessions() const noexcept;
[[nodiscard]] std::size_t connections() const noexcept;
[[nodiscard]] std::uint16_t port() const noexcept;
private:
struct Impl;
explicit RegionSessions(std::unique_ptr<Impl> impl);
std::unique_ptr<Impl> impl_;
};
} // namespace evo::server