diff --git a/apps/evo-server/m10_bridge.hpp b/apps/evo-server/m10_bridge.hpp index 1feb34c..eeaa826 100644 --- a/apps/evo-server/m10_bridge.hpp +++ b/apps/evo-server/m10_bridge.hpp @@ -7,6 +7,8 @@ // scheduled for upcoming ticks are submitted to the host, which commits them with the batch // of the tick they ride; the host's action acknowledgements and drops become outcomes. // Session control and action records use their own connection under the host's lease. +// M10.5 (ADR-0093): once per simulated tick every session's snapshot is built from the +// region's own stores for its interest (default: the region's first habitat site, radius 7). #include "evo/persistence/postgres.hpp" #include "evo/persistence/region_lease.hpp" #include @@ -14,6 +16,8 @@ #include #ifdef EVO_SERVER_M10 +#include "evo/client/environment_view.hpp" +#include "evo/processes/lineage_region.hpp" #include "evo/server/pg_slot.hpp" #include "evo/server/postgres_action_ledger.hpp" #include "evo/server/postgres_control_ledger.hpp" @@ -61,9 +65,9 @@ template class M10Bridge { if (!window) return fail(window.error() == server::SessionWindow::Error::expired ? "error m10-window-expired" - : window.error() == server::SessionWindow::Error::too_long - ? "error m10-window-too-long" - : "error m10-window"); + : window.error() == server::SessionWindow::Error::too_long + ? "error m10-window-too-long" + : "error m10-window"); const auto colon = o.listen.rfind(':'); std::uint16_t port = 0; if (colon == std::string::npos || @@ -129,6 +133,19 @@ template class M10Bridge { const server::HostFacts facts{s.ready && s.phase == server::HostPhase::running, s.epoch, s.simulated_tick}; sessions_->poll(facts, wall_seconds(), std::chrono::steady_clock::now()); + if (facts.ready && host.state() != nullptr && s.simulated_tick > replicated_) { + replicated_ = s.simulated_tick; + const auto& c = community(*host.state()); + if (!default_interest_) + if (const auto& sites = c.resources().fields().sites(); !sites.empty()) { + const auto v = sites.front().voxel; + default_interest_ = network::SessionInterest{v.x, v.y, v.z, 7}; + sessions_->set_default_interest(*default_interest_); + } + sessions_->replicate(s.simulated_tick, [&](const network::SessionInterest& in) { + return source(c, in, s.simulated_tick); + }); + } if (facts.ready) for (const auto& a : sessions_->carry(s.simulated_tick + m10_carry_ahead_ticks)) { const persistence::ActionRecord record{a.account.bytes, a.action_id, a.tick, @@ -164,6 +181,44 @@ template class M10Bridge { } private: + 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 region's stores within one interest: the habitat sites of its + // square in its slice. The region has no placed individuals: organisms stay empty. + network::ReplicationSource source(const processes::EnvironmentalCommunity& c, + const network::SessionInterest& in, std::uint64_t tick) { + network::ReplicationSource src; + src.mask = world::all_environment_fields; + const auto& fields = c.resources().fields(); + std::vector footprint; + for (const auto& site : fields.sites()) { + const auto v = site.voxel; + if (v.z == in.z && v.x >= in.x - in.radius && v.x <= in.x + in.radius && + v.y >= in.y - in.radius && v.y <= in.y + in.radius && + footprint.size() < world::max_environment_footprint) + footprint.push_back(v); + } + if (footprint.empty()) + return src; + const auto view = world::query_environment(fields, c.resources().terrain(), tick, footprint, + world::all_environment_fields); + if (!view) { + if (!warned_) + std::printf("m10 view-unavailable %u\n", static_cast(view.error())); + warned_ = true; + return src; + } + src.identity = view->identity(); + src.field_revision = view->field_revision(); + if (const auto cells = client::present_environment(*view, in.z)) + src.environment.assign(cells->cells().begin(), cells->cells().end()); + return src; + } static network::CarriedAction carried(const persistence::ActionRecord& r) { network::CarriedAction a; a.account.bytes = r.account; @@ -193,6 +248,9 @@ template class M10Bridge { std::unique_ptr sessions_; // Destroyed first: uses the ledgers. std::optional window_; bool window_closed_{}; + std::uint64_t replicated_{}; + std::optional default_interest_; + bool warned_{}; }; #else inline constexpr bool m10_available = false; @@ -205,7 +263,9 @@ template class M10Bridge { [[nodiscard]] bool enabled() const noexcept { return false; } - [[nodiscard]] bool accepting(std::uint64_t) const noexcept { return false; } + [[nodiscard]] bool accepting(std::uint64_t) const noexcept { + return false; + } void after_step() {} void shutdown() {} }; diff --git a/apps/evo-session-probe/main.cpp b/apps/evo-session-probe/main.cpp index 6136bc7..5534069 100644 --- a/apps/evo-session-probe/main.cpp +++ b/apps/evo-session-probe/main.cpp @@ -6,12 +6,15 @@ // evo-session-probe run --connect HOST:PORT --server-name NAME --ca FILE --issuer-key FILE // --world N --frame N --region N --account HEX32 --serial N [--lifetime S] // [--intents N] [--actions N --action-base ID] [--query ID]... [--timeout-ms N] +// [--interest X,Y,Z,R] [--snapshots N] // Signs a dev grant for the account and serial, logs in over QUIC, sends N intentions // (one per tick, two ticks ahead) and N probe actions (three ticks ahead), queries the // given action ids, and prints one line per answer: // welcome SESSION EPOCH TICK | intention SEQ STATUS | simulated SEQ TICK // receipt ID STATUS TICK | outcome ID RESULT TICK COMMIT | done // resolved HOST ADDRESS (a host name was looked up; IPv4) +// snapshot TICK CELLS ORGANISMS MINX MINY MAXX MAXY Z (each newly applied snapshot; +// --snapshots N waits for N; --interest asks for another focus than the default) // closed REASON TRANSPORT (TRANSPORT names why QUIC ended: untrusted means the server // answered but failed the pinned CA/name check; idle means nothing answered) // Statuses, results and reasons are the session-v1 wire values. Exits 0 once every @@ -160,7 +163,24 @@ struct RunOptions { action_base{1}, timeout_ms{20000}; std::optional account; std::vector queries; + std::optional interest; + std::uint64_t snapshots{}; }; +std::optional interest_of(std::string_view v) { + std::array parts{}; + for (std::size_t i = 0; i < parts.size(); ++i) { + const auto comma = v.find(','); + const auto part = v.substr(0, comma); + const auto [end, ec] = std::from_chars(part.data(), part.data() + part.size(), parts.at(i)); + if (ec != std::errc{} || end != part.data() + part.size() || + (comma == std::string_view::npos) != (i + 1 == parts.size())) + return std::nullopt; + v.remove_prefix(comma == std::string_view::npos ? v.size() : comma + 1); + } + if (parts[3] < 0 || parts[3] > 65535) + return std::nullopt; + return SessionInterest{parts[0], parts[1], parts[2], static_cast(parts[3])}; +} int run(const RunOptions& o) { const auto scalar = read(o.issuer_key); @@ -219,6 +239,7 @@ int run(const RunOptions& o) { const auto deadline = Clock::now() + std::chrono::milliseconds{o.timeout_ms}; bool welcomed = false, queried = false; std::uint64_t sent_intents = 0, sent_actions = 0, last_tick = 0, simulated = 0; + std::uint64_t seen_snapshot = 0, snapshots = 0; std::set pending_actions, pending_queries(o.queries.begin(), o.queries.end()); for (std::uint64_t i = 0; i < o.actions; ++i) pending_actions.insert(o.action_base + i); @@ -253,6 +274,8 @@ int run(const RunOptions& o) { } if (!queried) { queried = true; + if (o.interest && !c.set_interest(*o.interest)) + std::puts("error interest"); for (const auto query : o.queries) (void)c.query_action(query); } @@ -264,6 +287,23 @@ int run(const RunOptions& o) { std::printf("simulated %llu %llu\n", static_cast(s->sequence), static_cast(s->tick)); } + if (const auto* v = c.view(); v && v->tick != seen_snapshot) { + seen_snapshot = v->tick; + ++snapshots; + std::int64_t min_x = 0, min_y = 0, max_x = 0, max_y = 0; + for (std::size_t i = 0; i < v->environment.size(); ++i) { + const auto& voxel = v->environment[i].voxel; + min_x = i == 0 ? voxel.x : std::min(min_x, voxel.x); + min_y = i == 0 ? voxel.y : std::min(min_y, voxel.y); + max_x = i == 0 ? voxel.x : std::max(max_x, voxel.x); + max_y = i == 0 ? voxel.y : std::max(max_y, voxel.y); + } + std::printf("snapshot %llu %zu %zu %lld %lld %lld %lld %lld\n", + static_cast(v->tick), v->environment.size(), + v->organisms.size(), static_cast(min_x), + static_cast(min_y), static_cast(max_x), + static_cast(max_y), static_cast(v->slice_z)); + } for (const auto& r : c.take_action_receipts()) std::printf("receipt %llu %u %llu\n", static_cast(r.action_id), static_cast(r.status), static_cast(r.tick)); @@ -279,7 +319,11 @@ int run(const RunOptions& o) { } std::fflush(stdout); if (sent_actions == o.actions && pending_actions.empty() && pending_queries.empty() && - sent_intents == o.intents && simulated >= std::min(o.intents, 1)) { + sent_intents == o.intents && simulated >= std::min(o.intents, 1) && + snapshots >= o.snapshots) { + std::printf("snapshot-bytes %llu chunks %llu\n", + static_cast(c.snapshot_bytes()), + static_cast(c.snapshot_chunks())); std::puts("done"); c.close(); return 0; @@ -338,6 +382,10 @@ int main(int argc, char** argv) { o.action_base = *n; else if (n && flag == "--timeout-ms" && *n > 0) o.timeout_ms = *n; + else if (n && flag == "--snapshots" && *n <= 100000) + o.snapshots = *n; + else if (flag == "--interest") + ok = (o.interest = interest_of(value)).has_value(); else ok = false; } @@ -346,7 +394,8 @@ int main(int argc, char** argv) { std::fputs("usage: evo-session-probe keys --out DIR | run --connect HOST:PORT " "--server-name NAME --ca FILE --issuer-key FILE --world N --frame N " "--region N --account HEX32 --serial N [--lifetime S] [--intents N] " - "[--actions N --action-base ID] [--query ID]... [--timeout-ms N]\n", + "[--actions N --action-base ID] [--query ID]... [--timeout-ms N] " + "[--interest X,Y,Z,R] [--snapshots N]\n", stderr); return 2; } diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 1564d7e..ecdd2f4 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -526,7 +526,8 @@ session control done; 10.3b QUIC session login (schemas/network/session-v1.md) d (schema v11, same transaction as the checkpoint) done; 10.4c evo-server QUIC listener and headless client with process kill tests done. The engineering GUI client still uses the M9 dev path. 10.5 split (ADR-0093): 10.5a session protocol v2 and the replication core -done; 10.5b wiring and process evidence and 10.5c the GUI client switch follow. +done; 10.5b wiring (sessions, evo-server, client, probe) with process evidence done; 10.5c +the GUI client switch follows. 10.6-10.9 planned. **Entry:** M9 client/session evidence and M7/M8 dev/stage durability. Read diff --git a/docs/STATUS.md b/docs/STATUS.md index c9a047f..2d131c6 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -45,8 +45,10 @@ route is not yet verified end to end: the edge firewall rule (UDP 7400 from M10.5a (ADR-0093) adds session protocol v2 (interest, snapshot chunks, snapshot acks; v1 is refused) and the replication core: server-capped interest with a perception boundary, entity deltas against the acknowledged baseline, nearest first within 16 KiB per tick with -a slower distant tier, datagram chunks and client assembly. Next: 10.5b wiring, then the -GUI client switch (10.5c). +a slower distant tier, datagram chunks and client assembly. M10.5b wires it end to end: +evo-server replicates the region's environment for each session's interest (default: the +first habitat site), the client assembles and acknowledges snapshots, and the probe prints +them. Next: the GUI client switch (10.5c). ## Previous gate - 2026-10-02 diff --git a/docs/adr/0093-m10-snapshots-and-interest.md b/docs/adr/0093-m10-snapshots-and-interest.md index 35c1e0c..5ba5df8 100644 --- a/docs/adr/0093-m10-snapshots-and-interest.md +++ b/docs/adr/0093-m10-snapshots-and-interest.md @@ -58,3 +58,26 @@ are ignored; malformed records, a slice mismatch or disagreeing chunk headers ar are not needed at this size and are left for measurement (10.9). - Wiring into RegionSessions, evo-server and the clients is 10.5b; until then v2 messages exist but no server sends snapshots. + +## M10.5b wiring (2026-10-06) +- **RegionSessions** owns the `ReplicationServer`: replication opens with Welcome and + closes with the session; `Interest` arrives on the stream, `SnapshotAck` as a datagram. + The host calls `replicate(tick, source)` once per simulated tick; each session's chunks + are sized to its connection's current datagram limit (no snapshot while the datagram + extension is not negotiated). A refused chunk leaves the snapshot unacknowledged. +- **Default interest:** until a client sends its own, a session has the interest the host + set: evo-server uses the region's first habitat site with radius 7, so a new session + sees the region at once. Clients learn the slice and square from the snapshot itself. +- **evo-server source:** for each distinct interest, the habitat sites of the square in + the slice are queried from the region's own stores (`query_environment`, all fields) and + copied through `present_environment`, the same read-only path as the M9 view; organisms + stay empty (no placed individuals). A failed query sends an empty source and logs once. +- **Client:** `RegionSessionClient` assembles and acknowledges snapshots, exposes the view + and validated presentations, sends `Interest`, and closes the session (protocol) on a + malformed or unpresentable snapshot. Chunks arriving before Welcome are dropped (the + channels are not ordered); the next tick resends. +- **Probe:** `--interest X,Y,Z,R` and `--snapshots N`; one `snapshot` line per applied + snapshot (tick, cells, organisms, bounding square, slice) and `snapshot-bytes`. +- Evidence: the test region's default interest holds one habitat site, so the process test + shows a one-cell view refreshed every tick (40 chunks, 8,888 bytes in 40 ticks); the + 225-cell and 32-session figures come from synthetic sources. diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index dbd0841..530e9c2 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -4380,3 +4380,29 @@ tests; format check (only unrelated uncommitted files of the maintainer fail it) MSVC 19.44 preset `quic` build and CTest 4/4 (binaries rebuilt, timestamps checked). Not done: wiring into RegionSessions, evo-server and the clients (10.5b); the GUI client (10.5c). + +## 2026-10-06 — M10.5b snapshot wiring (ADR-0093) + +Implemented replication in `RegionSessions` (open/close with the session, `Interest`, +`SnapshotAck`, default interest, `replicate` with per-connection datagram limits), +snapshot assembly and acknowledgement in `RegionSessionClient`, the evo-server source (the +region's habitat sites in the interest square, `query_environment` and +`present_environment`), and probe options `--interest` and `--snapshots`. +Loopback QUIC (`evo-quic-session-tests`): a 21 x 21 synthetic slice; the default interest +(clamped to radius 7) arriving as 225 cells, none outside the square; a change reaching the +client; acknowledgements keeping deltas (fresh baselines fewer than snapshots); a new +interest replacing the view with the 25-cell square; the session staying open. +Real processes (`tools/ci/postgres-check.sh`, evo-server with QUIC): snapshots of the test +region's default interest (one habitat site), every cell in one slice and one 15 x 15 +square, a snapshot every tick (40 chunks, 8,888 bytes in 40 ticks); the probe printing +snapshots and `snapshot-bytes` with three snapshots. +Found and fixed during development: test cells requesting water without moisture (the +client correctly refused the snapshot and closed); a local name hiding the fixture's +`env()`; clang-tidy optional and return-value findings. +Executed: Linux GCC 14 `postgres-check.sh` with EVO_BUILD_QUIC (20 suites); GCC preset +`quic` CTest 4/4; session tests 10/10 repeated; Clang 19 ASan/UBSan session and network +tests; clang-tidy 19 on sessions, client, replication, bridge, probe and tests; Windows MSVC +19.44 preset `quic` build (including the probe) and CTest 4/4 (binaries rebuilt, timestamps +checked). The repository format check fails only on `include/evo/server/session_window.hpp` +(already on main, not part of this change). +Not done: the GUI client (10.5c); deployment of v2 (the dev server still runs v1). diff --git a/include/evo/client/region_session.hpp b/include/evo/client/region_session.hpp index c06be8b..59369f7 100644 --- a/include/evo/client/region_session.hpp +++ b/include/evo/client/region_session.hpp @@ -1,5 +1,6 @@ #pragma once #include "evo/network/quic_transport.hpp" +#include "evo/network/replication.hpp" #include "evo/network/session_protocol.hpp" #include #include @@ -15,7 +16,9 @@ // the Welcome deadline or a lost transport ends the session; reconnecting needs a new // client and a fresh grant (ADR-0089). M10.4 (ADR-0092): intentions as datagrams with // receipts and a simulated watermark; actions with stable ids, receipts and final outcomes. -// The client never treats a receipt or the watermark as a final outcome. Views are 10.5. +// The client never treats a receipt or the watermark as a final outcome. +// M10.5 (ADR-0093): snapshot chunks are assembled and acknowledged here; the newest complete +// snapshot is available as a view and as validated presentations. namespace evo::client { struct RegionSessionClientConfig { network::quic::ClientConfig transport; @@ -57,6 +60,17 @@ class RegionSessionClient { [[nodiscard]] std::optional simulated() const noexcept; // Latest server tick seen (Welcome, receipts, watermarks); 0 before Welcome. [[nodiscard]] std::uint64_t server_tick() const noexcept; + // Asks for another spatial focus (the server clamps it and restarts from a fresh + // baseline). False unless established and sent. + [[nodiscard]] bool set_interest(const network::SessionInterest& interest); + // The newest complete snapshot, or null before the first. + [[nodiscard]] const network::ReplicatedView* view() const noexcept; + [[nodiscard]] std::expected + environment() const; + [[nodiscard]] std::expected organisms() const; + // Snapshot datagrams and bytes received (whole messages), for reporting. + [[nodiscard]] std::uint64_t snapshot_chunks() const noexcept; + [[nodiscard]] std::uint64_t snapshot_bytes() const noexcept; [[nodiscard]] RegionSessionState state() const noexcept; [[nodiscard]] const std::optional& welcome() const noexcept; [[nodiscard]] std::optional close_reason() const noexcept; diff --git a/include/evo/network/replication.hpp b/include/evo/network/replication.hpp index ce8ba00..5aac151 100644 --- a/include/evo/network/replication.hpp +++ b/include/evo/network/replication.hpp @@ -75,6 +75,10 @@ class ReplicationServer { [[nodiscard]] std::vector>> tick(std::uint64_t tick, const std::function& source, const Perceives& perceives, std::size_t payload = session_datagram_bytes); + // The same with each session's own largest datagram (its path's current limit). + [[nodiscard]] std::vector>> + tick(std::uint64_t tick, const std::function& source, + const Perceives& perceives, const std::function& payload); [[nodiscard]] ReplicationStats stats() const; private: diff --git a/include/evo/server/region_sessions.hpp b/include/evo/server/region_sessions.hpp index ca19e31..fb522c8 100644 --- a/include/evo/server/region_sessions.hpp +++ b/include/evo/server/region_sessions.hpp @@ -1,6 +1,7 @@ #pragma once #include "evo/network/intent_admission.hpp" #include "evo/network/quic_transport.hpp" +#include "evo/network/replication.hpp" #include "evo/network/session_control.hpp" #include "evo/network/session_grant.hpp" #include "evo/network/session_protocol.hpp" @@ -25,6 +26,9 @@ // 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. +// M10.5 (ADR-0093): every established session has a spatial interest (the default set by +// the host until the client sends its own); the host calls replicate() once per simulated +// tick and each session receives its snapshot chunks as datagrams sized to its path. // 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`. @@ -48,6 +52,7 @@ struct RegionSessionsConfig { network::IntentLimits intents{}; // max_sessions follows control.max_sessions. // Durable action records (10.4b), owned by the host; null keeps outcomes in memory only. network::ActionLedger* actions{}; + network::ReplicationLimits replication{}; // max_sessions follows control.max_sessions. }; // The region host's authority, read once per poll. struct HostFacts { @@ -83,6 +88,16 @@ class RegionSessions { // The host dropped these carried actions: final ones are reported not_committed; the // others await their durable record (their batch outcome was unknown). void dropped(std::span actions, bool final); + // Interest given to each new session until it sends its own (none: no snapshots until it + // does). Sessions already open without an interest receive it too. + void set_default_interest(network::SessionInterest interest); + // Sends every interested session its snapshot for `tick` (call once per simulated tick). + // `source` reads the host's state for one interest; `perceives` hides organisms. + void replicate( + std::uint64_t tick, + const std::function& source, + const network::Perceives& perceives = {}); + [[nodiscard]] network::ReplicationStats replication_stats() const; // Closes every connection with `shutdown` (flushed by later polls). void shutdown(std::chrono::steady_clock::time_point now); [[nodiscard]] std::vector take_events(); diff --git a/schemas/network/session-v2.md b/schemas/network/session-v2.md index fe16954..5c8ef05 100644 --- a/schemas/network/session-v2.md +++ b/schemas/network/session-v2.md @@ -77,7 +77,8 @@ Close reasons (wire 1..13): 1 client_closed, 2 superseded, 3 expired, 4 stale_ep after a duplicate of a final action, and in answer to `ActionQuery`. - Interest (ADR-0093): the server clamps the radius to 7 and sends only entities in that square of the slice that the session may perceive. A new interest restarts from a fresh - baseline. + baseline. Until a client sends one, the server may apply a default interest (evo-server: + the region's first habitat site, radius 7); snapshots then start right after Welcome. - SnapshotChunk: epoch and tick nonzero; baseline 0 (fresh) or an earlier tick; 1 <= count <= 16; index < count. Records are whole entities, concatenated: diff --git a/src/client/session/region_session.cpp b/src/client/session/region_session.cpp index 0d654e1..e6cadb1 100644 --- a/src/client/session/region_session.cpp +++ b/src/client/session/region_session.cpp @@ -23,6 +23,8 @@ struct RegionSessionClient::Impl { std::vector intention_receipts; std::vector action_receipts; std::vector action_outcomes; + std::optional snapshots; + std::uint64_t snapshot_chunks{}, snapshot_bytes{}; static constexpr std::size_t max_kept = 1024; // Per kind, until taken; oldest dropped. template static void keep(std::vector& list, const T& item) { @@ -44,6 +46,21 @@ struct RegionSessionClient::Impl { seen_tick(m->server_tick); return keep(intention_receipts, *m); } + if (const auto* m = std::get_if(&*decoded)) { + ++snapshot_chunks; + snapshot_bytes += bytes.size(); + seen_tick(m->tick); + if (!snapshots) + return; // Before Welcome (channels are not ordered): the next tick resends. + const auto accepted = snapshots->accept(*m); + if (!accepted) + return end(SessionCloseReason::protocol, true); // Malformed snapshot. + if (const auto ack = accepted.value_or(std::nullopt); ack && connection != 0) + // A lost ack only means the next delta is against an older baseline. + (void)endpoint->send_datagram(connection, network::encode_session(*ack)) + .has_value(); + return; + } if (const auto* m = std::get_if(&*decoded)) { if (!welcome || m->epoch != welcome->epoch) return; // Another epoch's watermark says nothing about this session. @@ -80,6 +97,7 @@ struct RegionSessionClient::Impl { if (state == RegionSessionState::awaiting_welcome) { if (const auto* m = std::get_if(&*decoded)) { welcome = *m; + snapshots.emplace(world::WorldIdentity{m->world, m->frame}, m->epoch); seen_tick(m->server_tick); state = RegionSessionState::established; return; @@ -206,6 +224,40 @@ std::uint64_t RegionSessionClient::server_tick() const noexcept { return impl_->server_tick; } +bool RegionSessionClient::set_interest(const network::SessionInterest& interest) { + auto& m = *impl_; + return m.state == RegionSessionState::established && m.send(interest); +} + +const network::ReplicatedView* RegionSessionClient::view() const noexcept { + const auto& snapshots = impl_->snapshots; + return snapshots ? snapshots->latest() : nullptr; +} + +std::expected +RegionSessionClient::environment() const { + const auto& snapshots = impl_->snapshots; + if (!snapshots) + return std::unexpected(client::ViewError::empty_slice); + return snapshots->environment(); +} + +std::expected +RegionSessionClient::organisms() const { + const auto& snapshots = impl_->snapshots; + if (!snapshots) + return std::unexpected(client::ViewError::empty_slice); + return snapshots->organisms(); +} + +std::uint64_t RegionSessionClient::snapshot_chunks() const noexcept { + return impl_->snapshot_chunks; +} + +std::uint64_t RegionSessionClient::snapshot_bytes() const noexcept { + return impl_->snapshot_bytes; +} + RegionSessionState RegionSessionClient::state() const noexcept { return impl_->state; } diff --git a/src/network/protocol/replication.cpp b/src/network/protocol/replication.cpp index ab4d770..37d939f 100644 --- a/src/network/protocol/replication.cpp +++ b/src/network/protocol/replication.cpp @@ -190,16 +190,24 @@ std::vector>> ReplicationServer::tick(std::uint64_t tick, const std::function& source, const Perceives& perceives, std::size_t payload) { + return this->tick(tick, source, perceives, [payload](std::uint64_t) { return payload; }); +} + +std::vector>> ReplicationServer::tick( + std::uint64_t tick, const std::function& source, + const Perceives& perceives, const std::function& payload_of) { std::vector>> out; - payload = std::min(payload, session_datagram_bytes); const auto overhead = session_header_bytes + snapshot_chunk_header_bytes; - if (tick == 0 || payload <= overhead) + if (tick == 0) return out; - const auto capacity = payload - overhead; // Record bytes per chunk. std::vector> sources; for (auto& s : sessions_) { if (!s.interest || (!s.baselines.empty() && s.baselines.back().tick >= tick)) continue; + const auto payload = std::min(payload_of(s.id), session_datagram_bytes); + if (payload <= overhead) + continue; // No datagram channel yet (or a path too small): nothing to send. + const auto capacity = payload - overhead; // Record bytes per chunk. const auto& in = *s.interest; auto cached = std::ranges::find_if(sources, [&](const auto& e) { return e.first == in; }); if (cached == sources.end()) { diff --git a/src/server/session/region_sessions.cpp b/src/server/session/region_sessions.cpp index 261170d..8be7134 100644 --- a/src/server/session/region_sessions.cpp +++ b/src/server/session/region_sessions.cpp @@ -62,15 +62,18 @@ struct RegionSessions::Impl { }; Impl(RegionSessionsConfig c, std::unique_ptr e, - network::GrantVerifier v, network::SessionControl s, network::IntentAdmission i) + network::GrantVerifier v, network::SessionControl s, network::IntentAdmission i, + network::ReplicationServer r) : config{std::move(c)}, endpoint{std::move(e)}, verifier{std::move(v)}, - control{std::move(s)}, intents{std::move(i)} {} + control{std::move(s)}, intents{std::move(i)}, replication{std::move(r)} {} RegionSessionsConfig config; std::unique_ptr endpoint; network::GrantVerifier verifier; network::SessionControl control; network::IntentAdmission intents; + network::ReplicationServer replication; + std::optional default_interest; HostFacts facts; std::vector connections; std::vector events; @@ -110,8 +113,10 @@ struct RegionSessions::Impl { event.account = c.account; event.reason = reason; events.push_back(std::move(event)); - if (c.state == State::established) + if (c.state == State::established) { intents.close(c.session); + replication.close(c.session); + } if (c.session != 0) // May already be released (superseded, expired, epoch). (void)control.close(c.session).has_value(); // A superseded session's durable control already moved to its successor. @@ -183,6 +188,12 @@ struct RegionSessions::Impl { void welcome(Connection& c, Clock::time_point now) { if (!intents.open(c.session, c.account_id)) return close(c, SessionCloseReason::capacity, now); + if (!replication.open(c.session, c.welcome.epoch)) { + intents.close(c.session); + return close(c, SessionCloseReason::capacity, now); + } + if (default_interest) + (void)replication.set_interest(c.session, *default_interest).has_value(); c.state = State::established; c.welcome.server_tick = facts.simulated_tick; send(c, c.welcome); @@ -244,8 +255,12 @@ struct RegionSessions::Impl { if (c.state == State::closing) return; const auto decoded = network::decode_session(bytes); - const auto* m = decoded ? std::get_if(&*decoded) : nullptr; - if (c.state != State::established || !m) + if (c.state != State::established || !decoded) + return close(c, SessionCloseReason::protocol, now); + if (const auto* ack = std::get_if(&*decoded)) + return replication.acknowledge(c.session, *ack); + const auto* m = std::get_if(&*decoded); + if (!m) return close(c, SessionCloseReason::protocol, now); send_datagram(c, intents.intention(c.session, *m, admission())); } @@ -290,6 +305,10 @@ struct RegionSessions::Impl { } if (const auto* m = std::get_if(&*decoded)) return send(c, intents.query(c.session, *m, admission())); + if (const auto* m = std::get_if(&*decoded)) { + (void)replication.set_interest(c.session, *m).has_value(); // Clamped by the server. + return; + } if (const auto* m = std::get_if(&*decoded); m && m->reason == SessionCloseReason::client_closed) { close(c, SessionCloseReason::client_closed, now); @@ -324,13 +343,17 @@ RegionSessions::create(RegionSessionsConfig config) { auto intents = network::IntentAdmission::create(config.intents); if (!intents) return std::unexpected(RegionSessionsError::configuration); + config.replication.max_sessions = config.control.max_sessions; + auto replication = network::ReplicationServer::create(config.replication); + if (!replication) + return std::unexpected(RegionSessionsError::configuration); intents->attach(config.actions); auto endpoint = network::quic::Endpoint::serve(config.transport); if (!endpoint) return std::unexpected(RegionSessionsError::transport); return std::unique_ptr{new RegionSessions{ std::make_unique(std::move(config), std::move(*endpoint), std::move(*verifier), - std::move(*control), std::move(*intents))}}; + std::move(*control), std::move(*intents), std::move(*replication))}}; } void RegionSessions::poll(const HostFacts& facts, std::uint64_t now_seconds, @@ -362,8 +385,10 @@ void RegionSessions::poll(const HostFacts& facts, std::uint64_t now_seconds, m.connections, [&](const Impl::Connection& c) { return c.id == e.connection; }); if (it == m.connections.end()) continue; - if (it->state == Impl::State::established) + if (it->state == Impl::State::established) { m.intents.close(it->session); + m.replication.close(it->session); + } if (it->state == Impl::State::pending) { (void)m.control.close(it->session).has_value(); if (m.config.ledger && it->generation != 0) @@ -417,6 +442,33 @@ void RegionSessions::dropped(std::span actions, bo impl_->deliver(impl_->intents.dropped(actions, final)); } +void RegionSessions::set_default_interest(network::SessionInterest interest) { + auto& m = *impl_; + m.default_interest = interest; + for (const auto& c : m.connections) + if (c.state == Impl::State::established && !m.replication.interest(c.session)) + (void)m.replication.set_interest(c.session, interest).has_value(); +} + +void RegionSessions::replicate( + std::uint64_t tick, + const std::function& source, + const network::Perceives& perceives) { + auto& m = *impl_; + const auto payload = [&](std::uint64_t session) -> std::size_t { + const auto* c = m.by_session(session); + return c && c->state == Impl::State::established ? m.endpoint->datagram_limit(c->id) : 0; + }; + for (const auto& [session, chunks] : m.replication.tick(tick, source, perceives, payload)) + if (const auto* c = m.by_session(session); c && c->state == Impl::State::established) + for (const auto& chunk : chunks) + m.send_datagram(*c, chunk); // A refused chunk leaves the snapshot unacknowledged. +} + +network::ReplicationStats RegionSessions::replication_stats() const { + return impl_->replication.stats(); +} + void RegionSessions::shutdown(Clock::time_point now) { for (auto& c : impl_->connections) impl_->close(c, SessionCloseReason::shutdown, now); diff --git a/tests/quic_sessions.cpp b/tests/quic_sessions.cpp index a31e395..c117c4b 100644 --- a/tests/quic_sessions.cpp +++ b/tests/quic_sessions.cpp @@ -151,6 +151,10 @@ struct Harness { } void pump() { server->poll({ready, epoch, tick}, now_seconds, Clock::now()); + if (source && tick != replicated) { // One snapshot per simulated tick (ADR-0093). + replicated = tick; + server->replicate(tick, source); + } for (auto& e : server->take_events()) events.push_back(std::move(e)); for (auto* c : clients) @@ -191,6 +195,8 @@ struct Harness { std::vector events; std::uint64_t epoch{5}, now_seconds{start}, tick{100}; bool ready{true}; + std::function source; + std::uint64_t replicated{}; }; bool refused_with(Harness& h, const WireGrant& grant, Reason reason) { @@ -471,6 +477,75 @@ void intentions_and_actions(const Files& f) { CHECK(welcome && reason == Reason::protocol); } +// M10.5: snapshots over QUIC from a simulated host's 21 x 21 slice. +client::EnvironmentCell grid_cell(std::int64_t x, std::int64_t y, std::uint64_t quantity) { + client::EnvironmentCell c; + c.voxel = {x, y, -7}; + c.chunk_revision = 40; + c.habitat = {{100}, {200}, {0}, {1000}, {500}, {10}}; + c.volume = {1000ULL * 500 * 10}; + c.substrate = {1}; + c.terrain_material = {2}; + c.terrain_fill = 1000; + c.presence[0] = client::FieldPresence::present; + c.quantity[0] = quantity; + c.presence[1] = client::FieldPresence::missing; + c.presence[7] = client::FieldPresence::present; + c.quantity[7] = 5; + c.moisture_status = world::MoistureStatus::available; + c.moisture = {3, 50}; + return c; +} + +void snapshots_over_quic(const Files& f) { + Harness h{f}; + std::uint64_t quantity = 30; + h.source = [&](const SessionInterest&) { + ReplicationSource s; + s.identity = world_id; + s.field_revision = h.tick; + s.mask = (1U << 0) | (1U << 1) | (1U << 7); // Water requested: moisture is present. + for (std::int64_t x = -10; x <= 10; ++x) + for (std::int64_t y = -10; y <= 10; ++y) + s.environment.push_back(grid_cell(x, y, quantity)); + return s; + }; + h.server->set_default_interest({0, 0, -7, 30}); // Clamped to radius 7. + auto a = h.connect(h.issuer.grant(account(1), 1)); + CHECK(h.settled(*a) && a->state() == RegionSessionState::established); + const auto ticks_until = [&](const std::function& done) { + for (int i = 0; i < 400 && !done(); ++i) { + ++h.tick; + h.until(done, std::chrono::milliseconds{20}); + } + return done(); + }; + // The default interest's 225 cells arrive within the per-tick budget. + CHECK(ticks_until([&] { return a->view() && a->view()->environment.size() == 225; })); + const auto env = a->environment(); + CHECK(env && env->cells().size() == 225 && env->revision().identity == world_id); + CHECK(std::ranges::all_of(env->cells(), [](const auto& c) { + return c.voxel.x >= -7 && c.voxel.x <= 7 && c.voxel.y >= -7 && c.voxel.y <= 7; + })); + CHECK(a->snapshot_chunks() > 0 && a->snapshot_bytes() > std::uint64_t{225} * 50); + // A change reaches the client; acknowledgements keep deltas small. + quantity = 31; + CHECK(ticks_until([&] { + return a->view() && std::ranges::all_of(a->view()->environment, + [](const auto& c) { return c.quantity[0] == 31; }); + })); + const auto stats = h.server->replication_stats(); + CHECK(stats.snapshots > 2 && stats.fresh_baselines < stats.snapshots); + // Another interest: only the new square. + CHECK(a->set_interest({5, 5, -7, 2})); + CHECK(ticks_until([&] { return a->view() && a->view()->environment.size() == 25; })); + CHECK(std::ranges::all_of(a->view()->environment, [](const auto& c) { + return c.voxel.x >= 3 && c.voxel.x <= 7 && c.voxel.y >= 3 && c.voxel.y <= 7; + })); + // A snapshot ack datagram is accepted (not a protocol error); the session stays open. + CHECK(a->state() == RegionSessionState::established); +} + void durable_control(const Files& f) { FakeLedger ledger; Harness h{f, 4, 64, &ledger}; @@ -546,6 +621,7 @@ int main() try { limits(files); durable_control(files); intentions_and_actions(files); + snapshots_over_quic(files); std::filesystem::remove_all(files.dir); std::puts("quic session tests passed"); return 0; diff --git a/tests/region_m10_session_tests.cpp b/tests/region_m10_session_tests.cpp index c7c2877..b917423 100644 --- a/tests/region_m10_session_tests.cpp +++ b/tests/region_m10_session_tests.cpp @@ -8,6 +8,7 @@ #include "evo/network/dev_credentials.hpp" #include "evo/network/session_grant.hpp" #include "fixtures/server_process.hpp" +#include #include #include #include @@ -219,6 +220,25 @@ void end_to_end() { auto server = std::make_unique(s); auto a = connect(s, *server, s.grant(alice, 1)); CHECK(established(*server, *a)); + // Snapshots of the real region (ADR-0093): the default interest is the first habitat + // site with radius 7; every cell lies in one slice and one 15 x 15 square. + const auto first_tick = a->server_tick(); + CHECK(until(*server, *a, [&] { return a->view() && !a->view()->environment.empty(); })); + const auto shown = a->environment(); + CHECK(shown && !shown->cells().empty() && shown->cells().size() <= 225); + const auto [min_x, max_x] = + std::ranges::minmax(shown->cells(), {}, [](const auto& c) { return c.voxel.x; }); + const auto [min_y, max_y] = + std::ranges::minmax(shown->cells(), {}, [](const auto& c) { return c.voxel.y; }); + CHECK(max_x.voxel.x - min_x.voxel.x <= 14 && max_y.voxel.y - min_y.voxel.y <= 14); + CHECK(std::ranges::all_of(shown->cells(), + [&](const auto& c) { return c.voxel.z == shown->slice_z(); })); + CHECK(until(*server, *a, [&] { return a->server_tick() >= first_tick + 40; })); + std::printf("m10 snapshot report cells=%zu ticks=%llu chunks=%llu bytes=%llu\n", + shown->cells().size(), + static_cast(a->server_tick() - first_tick), + static_cast(a->snapshot_chunks()), + static_cast(a->snapshot_bytes())); // Intentions: receipts, then the watermark. // Datagrams may be refused until the QUIC datagram extension is negotiated: retry. std::optional sequence; @@ -298,7 +318,8 @@ void end_to_end() { "--intents", "3", "--actions", "2", "--action-base", "10", - "--query", "10"}}; + "--query", "10", + "--snapshots", "3"}}; int code = -1; for (const auto end = Clock::now() + 30s; Clock::now() < end && code < 0;) { server->pump(); @@ -307,7 +328,8 @@ void end_to_end() { code = p.wait_exit(10s); } CHECK(code == 0 && p.printed("welcome ") && p.printed("intention 1 0") && - p.printed("outcome 10 1 ") && p.printed("outcome 11 1 ")); + p.printed("outcome 10 1 ") && p.printed("outcome 11 1 ") && p.printed("snapshot ") && + p.printed("snapshot-bytes ")); CHECK(!record(bob, 10).empty() && !record(bob, 11).empty()); server->child->signal(SIGTERM); CHECK(server->child->wait_exit(30s) == 0);