diff --git a/apps/evo-client/panels.cpp b/apps/evo-client/panels.cpp index 6630bbb..786aa5e 100644 --- a/apps/evo-client/panels.cpp +++ b/apps/evo-client/panels.cpp @@ -346,6 +346,7 @@ void session(const PanelView& v) { if (ImGui::BeginTable("status", 3, ImGuiTableFlags_SizingFixedFit)) { row("Connection", s.phase); row("Session / epoch", std::to_string(s.session) + " / " + std::to_string(s.epoch)); + row("Region", std::to_string(s.region) + " (" + std::to_string(s.handoffs) + " crossings)"); row("Server tick", std::to_string(s.server_tick)); row("Simulated through", std::to_string(s.simulated_tick)); row("Grants left", std::to_string(s.grants_left)); diff --git a/apps/evo-client/remote_session.hpp b/apps/evo-client/remote_session.hpp index 4ebc622..6854b67 100644 --- a/apps/evo-client/remote_session.hpp +++ b/apps/evo-client/remote_session.hpp @@ -23,6 +23,13 @@ // `delay` ticks behind (adaptive), extrapolated at most two ticks, then marked stale. When the // session is lost the last view stays frozen and stale: the own marker at its last // authoritative position, nothing predicted or sent. A new session starts afresh. +// +// M10.8c-2 (ADR-0096): a HandoffOffer (the probe crossed into another region) is followed at +// once: the frozen view stays on screen, no grant is used, and the next connection goes to +// the destination with the offer's ticket. A Redirect at login (the probe lives elsewhere) +// is followed with a grant for that region. Grants are picked by their region claim (the +// session-grant-v1 layout is public; the claim only routes, the region still verifies), so a +// bundle may hold grants for several regions. #include "evo/client/camera.hpp" #include "evo/client/environment_view.hpp" #include "evo/client/input.hpp" @@ -45,8 +52,11 @@ #include "evo/network/quic_transport.hpp" #include "evo/world/coordinates.hpp" #include +#include +#include #include #include +#include #include #include #include @@ -74,6 +84,7 @@ struct RemoteStatus { // Interpolation (ADR-0095): the current delay in ticks, markers shown stale, and whether // the whole view is a frozen copy from a lost session. double interpolation_delay{}, remote_render_tick{}; // Ticks: delay, clock - delay. + std::uint64_t region{}, handoffs{}; // Region of the current session; crossings followed. std::size_t stale_markers{}; bool stale_view{}; }; @@ -106,6 +117,10 @@ class RemoteSession { session.transport_.authority_pem = m.authority_pem; session.transport_.authority_pem_file = m.authority_path; session.grants_ = m.grants; + session.used_.assign(m.grants.size(), false); + // The endpoint typed in the launcher is the first grant's region. + session.target_region_ = grant_region(m.grants.front()); + session.routes_[session.target_region_] = session.transport_; return session; } RemoteSession(RemoteSession&&) noexcept = default; @@ -117,6 +132,8 @@ class RemoteSession { client_->close(); // Close{client_closed}; unresolved intentions stay unknown. for (auto& g : grants_) g.fill(0); + if (ticket_) + ticket_->fill(0); } [[nodiscard]] const client::EnvironmentLayer& environment() const noexcept { return environment_; @@ -138,7 +155,11 @@ class RemoteSession { s.epoch = epoch_; s.server_tick = server_tick_; s.simulated_tick = simulated_tick_; - s.grants_left = grants_.size() - next_; + s.grants_left = static_cast(std::ranges::count(used_, false)); + s.region = region_; + s.handoffs = handoffs_; + if (ticket_ || (client_ && handing_off_ && !live)) + s.phase = "handing off"; s.welcomed = welcomed_; s.snapshot_tick = seen_snapshot_; s.snapshot_bytes = snapshot_bytes_ + (client_ ? client_->snapshot_bytes() : 0); @@ -258,22 +279,85 @@ class RemoteSession { static_cast(ui.notify(text, level).has_value()); // Bounded; overflow evicts. } // Opens the next connection with the next grant; false while pausing or out of grants. + // The region claim of a session-grant-v1 record (marker "EVO-GRANT", region at offset + // 72), read only to route; anything else has no region (0). + static std::uint64_t grant_field(const network::WireGrant& g, std::size_t at) { + static constexpr std::array marker{'E', 'V', 'O', '-', 'G', + 'R', 'A', 'N', 'T'}; + if (!std::equal(marker.begin(), marker.end(), g.begin())) + return 0; + std::uint64_t v = 0; + for (std::size_t i = 0; i < 8; ++i) + v |= static_cast(g.at(at + i)) << (8 * i); + return v; + } + static std::uint64_t grant_region(const network::WireGrant& g) { + return grant_field(g, 72); + } + // Serials must rise world-wide (durable account control): never use a lower one after a + // higher one, whichever region it is for. + static std::uint64_t grant_serial(const network::WireGrant& g) { + return grant_field(g, 48); + } + // How to dial a region named by an offer or redirect ("host:port" and its certificate + // name), resolved once; nothing when the address cannot be used. + [[nodiscard]] bool learn_route(const network::RegionAddress& a) { + const auto colon = a.endpoint.rfind(':'); + if (colon == std::string::npos) + return false; + std::uint16_t port = 0; + const auto* begin = a.endpoint.data() + colon + 1; + const auto* end = a.endpoint.data() + a.endpoint.size(); + if (const auto [at, ec] = std::from_chars(begin, end, port); + ec != std::errc{} || at != end || port == 0) + return false; + const auto address = network::quic::resolve_ipv4(a.endpoint.substr(0, colon)); + if (!address) + return false; + auto route = transport_; // Same pinned CA: one world, one dev CA. + route.server_address = *address; + route.port = port; + route.server_name = a.server_name; + routes_[a.region] = std::move(route); + return true; + } bool start(Clock::time_point now, client::Launcher& launcher) { if (now < retry_at_) return false; - if (next_ >= grants_.size()) { - if (!exhausted_) { - exhausted_ = true; - launcher.observe(client::ConnectSignal::exhausted); - std::puts("session grants exhausted; issue a new bundle to reconnect"); - } - return false; - } client::RegionSessionClientConfig config; - config.transport = transport_; - config.grant = grants_[next_]; - grants_[next_].fill(0); - ++next_; + const auto route = routes_.find(target_region_); + config.transport = route != routes_.end() ? route->second : transport_; + if (ticket_) { // A handoff: the ticket replaces a grant, once. + config.grant = *ticket_; + ticket_->fill(0); + ticket_.reset(); + } else { + std::size_t pick = grants_.size(); + for (std::size_t i = 0; i < grants_.size(); ++i) + if (!used_[i] && grant_serial(grants_[i]) < newest_serial_) { + grants_[i].fill(0); // Outdated by a grant already used: unusable. + used_[i] = true; + } + for (std::size_t i = 0; i < grants_.size(); ++i) + if (!used_[i] && + (target_region_ == 0 || grant_region(grants_[i]) == target_region_) && + (pick == grants_.size() || + grant_serial(grants_[i]) < grant_serial(grants_[pick]))) + pick = i; // The lowest usable serial for the region. + if (pick == grants_.size()) { + if (!exhausted_) { + exhausted_ = true; + launcher.observe(client::ConnectSignal::exhausted); + std::printf("session grants for region %llu exhausted; issue a new bundle\n", + static_cast(target_region_)); + } + return false; + } + newest_serial_ = std::max(newest_serial_, grant_serial(grants_[pick]) + 1); + config.grant = grants_[pick]; + grants_[pick].fill(0); + used_[pick] = true; + } auto made = client::RegionSessionClient::connect(std::move(config), now); if (!made) { launcher.observe(client::ConnectSignal::lost); @@ -293,13 +377,15 @@ class RemoteSession { established_ = true; welcomed_ = true; backoff_ = std::chrono::seconds{2}; - if (w.epoch != epoch_) { // Another authority lineage: start the views afresh. + handing_off_ = false; + if (w.epoch != epoch_ || w.region != region_) { // Another lineage or region: afresh. environment_.reset(); organisms_.reset(); seen_snapshot_ = 0; } session_ = w.session; epoch_ = w.epoch; + region_ = w.region; sent_tick_.reset(); // New session: resend the current intention. sent_ = {}; sent_interest_.reset(); // The server starts from its default interest. @@ -468,6 +554,8 @@ class RemoteSession { void end(Clock::time_point now, client::UiState& ui, client::Launcher& launcher) { using network::SessionCloseReason; const auto reason = client_->close_reason().value_or(SessionCloseReason::connection_lost); + const auto offer = client_->take_handoff(); + const auto redirect = client_->redirect(); const bool was_established = established_; snapshot_bytes_ += client_->snapshot_bytes(); for (const auto& p : pending_) @@ -490,6 +578,30 @@ class RemoteSession { predictor_.reset(); // The marker ended with its session. client_.reset(); established_ = false; + // A crossing or a redirect is followed at once: no grant for a handoff, no pause. + if (reason == SessionCloseReason::handed_off && offer && learn_route(offer->destination)) { + ticket_ = offer->ticket; + target_region_ = offer->destination.region; + handing_off_ = true; + ++handoffs_; + stale_view_ = false; // A switch, not a loss: the view stays as it was. + launcher.observe(client::ConnectSignal::not_ready); + notify(ui, "Crossing into a neighbouring region.", client::NoticeLevel::info); + std::printf("session handoff region=%llu\n", + static_cast(target_region_)); + retry_at_ = now; + return; + } + if (reason == SessionCloseReason::moved && redirect && learn_route(*redirect)) { + target_region_ = redirect->region; + launcher.observe(client::ConnectSignal::not_ready); + notify(ui, "Your probe is in another region; connecting there.", + client::NoticeLevel::info); + std::printf("session redirect region=%llu\n", + static_cast(target_region_)); + retry_at_ = now; + return; + } const bool not_ready = reason == SessionCloseReason::shutdown || reason == SessionCloseReason::capacity; const bool lost = reason == SessionCloseReason::connection_lost || @@ -517,7 +629,11 @@ class RemoteSession { } network::quic::ClientConfig transport_; std::vector grants_; - std::size_t next_{}; + std::vector used_; + std::map routes_; // Region -> how to dial it. + std::uint64_t target_region_{}, region_{}, handoffs_{}, newest_serial_{}; + std::optional ticket_; // A handoff ticket for the next connection. + bool handing_off_{}; std::unique_ptr client_; client::EnvironmentLayer environment_; client::OrganismLayer organisms_; diff --git a/apps/evo-server/m10_bridge.hpp b/apps/evo-server/m10_bridge.hpp index f7a7449..0c9cc11 100644 --- a/apps/evo-server/m10_bridge.hpp +++ b/apps/evo-server/m10_bridge.hpp @@ -9,23 +9,29 @@ // 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). -// M10.6 (ADR-0094): sessions' probe markers move within the habitat sites' bounding cells. +// M10.6 (ADR-0094): sessions' probe markers move within the habitat sites' bounding cells, +// or within --m10-area. M10.8c (ADR-0096): with --m10-handoff-key, markers are durable probe +// records and a marker crossing into a --m10-neighbour's area is handed over with a ticket. #include "evo/persistence/postgres.hpp" #include "evo/persistence/region_lease.hpp" #include #include #include +#include #ifdef EVO_SERVER_M10 #include "evo/client/environment_view.hpp" +#include "evo/network/handoff_ticket.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" +#include "evo/server/postgres_handoff_ledger.hpp" #include "evo/server/region_host.hpp" #include "evo/server/region_sessions.hpp" #include "evo/server/session_window.hpp" #include +#include #include #include #include @@ -33,6 +39,9 @@ #include #include #include +#include +#include +#include #include #endif @@ -41,9 +50,15 @@ struct M10Options { std::string listen, certificate, key, issuer, environment, session_window; // M10.5c: dev only, 60..3600 s; 0 keeps the default (300 s, ADR-0091). std::uint64_t max_grant_lifetime{}; + // M10.8c (ADR-0096, dev only): this region's marker area in cells "X0,Y0,X1,Y1" + // (inclusive), its handoff key (32-byte P-256 scalar; key id = region id) and up to 8 + // neighbours "REGION=X0,Y0,X1,Y1=HOST:PORT=SERVER_NAME=PUBLIC_KEY_FILE". + std::string area, handoff_key; + std::vector neighbours; [[nodiscard]] bool any() const noexcept { return !listen.empty() || !certificate.empty() || !key.empty() || !issuer.empty() || - !environment.empty() || !session_window.empty() || max_grant_lifetime != 0; + !environment.empty() || !session_window.empty() || max_grant_lifetime != 0 || + !area.empty() || !handoff_key.empty() || !neighbours.empty(); } [[nodiscard]] bool complete() const noexcept { return !listen.empty() && !certificate.empty() && !key.empty() && !issuer.empty() && @@ -95,6 +110,13 @@ template class M10Bridge { return fail("error m10-grant-lifetime"); config.control.max_grant_lifetime = o.max_grant_lifetime; } + std::optional> area_cells; + if (!o.area.empty() && !(area_cells = cells_of(o.area))) + return fail("error m10-area"); + if ((!o.handoff_key.empty() || !o.neighbours.empty()) && + (config.control.environment != network::DeploymentEnvironment::dev || + o.handoff_key.empty() || o.neighbours.empty() || !area_cells)) + return fail("error m10-handoff"); // Dev only; key, neighbours and area together. config.control.world = {key.world, frame}; config.control.region = key.region; config.control.epoch = 1; // Follows the host's epoch from the first poll. @@ -111,6 +133,33 @@ template class M10Bridge { bridge.actions_ = std::make_unique(*bridge.slot_, key, lease); config.ledger = bridge.control_.get(); config.actions = bridge.actions_.get(); + bridge.area_cells_ = area_cells; + if (!o.handoff_key.empty()) { + const auto scalar = read(o.handoff_key); + auto signer = network::HandoffSigningKey::import( + config.control.environment, static_cast(key.region), + std::span{reinterpret_cast(scalar.data()), scalar.size()}); + if (!signer) + return fail("error m10-handoff-key"); + config.handoff_key = + std::make_shared(std::move(*signer)); + for (const auto& spec : o.neighbours) { + auto n = neighbour_of(spec); + if (!n || n->first.address.region == key.region) + return fail("error m10-neighbour"); + config.handoff_keys.push_back({n->first.address.region, + static_cast(n->first.address.region), + n->second}); + config.neighbours.push_back(std::move(n->first)); + } + bridge.probes_ = + std::make_unique(*bridge.slot_, key, lease); + bridge.handoff_ = + std::make_unique(*bridge.slot_, key, lease); + config.probes = bridge.probes_.get(); + config.handoff = bridge.handoff_.get(); + std::printf("m10 handoff neighbours=%zu\n", config.neighbours.size()); + } auto sessions = server::RegionSessions::create(std::move(config)); if (!sessions) return fail(sessions.error() == server::RegionSessionsError::keys ? "error m10-issuer" @@ -204,6 +253,18 @@ template class M10Bridge { constexpr auto cell = network::motion::micrometres_per_cell; network::motion::Area area{first.x * cell, first.y * cell, (first.x * cell) + cell - 1, (first.y * cell) + cell - 1, first.z}; + if (area_cells_) { // --m10-area: this region's cells, inclusive. + const auto& c = *area_cells_; + area = {c[0] * cell, c[1] * cell, (c[2] * cell) + cell - 1, (c[3] * cell) + cell - 1, + first.z}; + if (sessions_->set_motion_area(area, ((c[0] + c[2] + 1) * cell) / 2, + ((c[1] + c[3] + 1) * cell) / 2)) + std::printf("m10 motion area %lld %lld %lld %lld z=%lld\n", + static_cast(area.min_x), static_cast(area.min_y), + static_cast(area.max_x), static_cast(area.max_y), + static_cast(area.z)); + return; + } for (const auto& site : sites) if (site.voxel.z == first.z) { area.min_x = std::min(area.min_x, site.voxel.x * cell); @@ -249,6 +310,53 @@ template class M10Bridge { src.environment.assign(cells->cells().begin(), cells->cells().end()); return src; } + // "X0,Y0,X1,Y1" in cells, X0 <= X1 and Y0 <= Y1, each within +-2^40. + static std::optional> cells_of(std::string_view text) { + std::array v{}; + for (std::size_t i = 0; i < v.size(); ++i) { + const auto comma = text.find(','); + const auto part = text.substr(0, comma); + const auto [end, ec] = std::from_chars(part.data(), part.data() + part.size(), v.at(i)); + if (ec != std::errc{} || end != part.data() + part.size() || + (comma == std::string_view::npos) != (i + 1 == v.size()) || + v.at(i) < -(std::int64_t{1} << 40) || v.at(i) > (std::int64_t{1} << 40)) + return std::nullopt; + text.remove_prefix(comma == std::string_view::npos ? text.size() : comma + 1); + } + if (v[0] > v[2] || v[1] > v[3]) + return std::nullopt; + return v; + } + // "REGION=X0,Y0,X1,Y1=HOST:PORT=SERVER_NAME=PUBLIC_KEY_FILE". + static std::optional> + neighbour_of(std::string_view spec) { + std::array part{}; + for (std::size_t i = 0; i < part.size(); ++i) { + const auto eq = spec.find('='); + if ((eq == std::string_view::npos) != (i + 1 == part.size())) + return std::nullopt; + part.at(i) = spec.substr(0, eq); + spec.remove_prefix(eq == std::string_view::npos ? spec.size() : eq + 1); + } + std::uint64_t region = 0; + if (const auto [end, ec] = + std::from_chars(part[0].data(), part[0].data() + part[0].size(), region); + ec != std::errc{} || end != part[0].data() + part[0].size() || region == 0) + return std::nullopt; + const auto cells = cells_of(part[1]); + const auto key = read(std::string{part[4]}); + if (!cells || part[2].empty() || part[3].empty() || part[2].size() > 64 || + part[3].size() > 64 || key.size() != network::grant_public_key_bytes) + return std::nullopt; + constexpr auto cell = network::motion::micrometres_per_cell; + const auto& c = *cells; + server::Neighbour n{ + {region, std::string{part[2]}, std::string{part[3]}}, + {c[0] * cell, c[1] * cell, (c[2] * cell) + cell - 1, (c[3] * cell) + cell - 1, 0}}; + network::GrantPublicKey pub{}; + std::copy(key.begin(), key.end(), pub.begin()); + return std::pair{std::move(n), pub}; + } static network::CarriedAction carried(const persistence::ActionRecord& r) { network::CarriedAction a; a.account.bytes = r.account; @@ -275,6 +383,9 @@ template class M10Bridge { std::unique_ptr slot_; std::unique_ptr control_; std::unique_ptr actions_; + std::unique_ptr probes_; // Handoff mode only. + std::unique_ptr handoff_; // Handoff mode only. + std::optional> area_cells_; std::unique_ptr sessions_; // Destroyed first: uses the ledgers. std::optional window_; bool window_closed_{}; diff --git a/apps/evo-server/main.cpp b/apps/evo-server/main.cpp index 0b062f9..4b73f09 100644 --- a/apps/evo-server/main.cpp +++ b/apps/evo-server/main.cpp @@ -231,6 +231,12 @@ std::optional parse(int argc, char** argv) { o.m10.session_window = value; else if (flag == "--m10-max-grant-lifetime" && n && *n >= 60 && *n <= 3600) o.m10.max_grant_lifetime = *n; + else if (flag == "--m10-area") + o.m10.area = value; + else if (flag == "--m10-handoff-key") + o.m10.handoff_key = value; + else if (flag == "--m10-neighbour" && o.m10.neighbours.size() < 8) + o.m10.neighbours.emplace_back(value); else if (flag == "--crash-at") { const auto colon = value.find(':'); const auto ordinal = @@ -596,7 +602,9 @@ int main(int argc, char** argv) { "--session-grant-ledger F --session-environment dev]] [--m10-listen " "ADDRESS:PORT --m10-cert F --m10-key F --m10-issuer F " "--m10-environment dev|stage|world0 --session-window ACTIVATION:UTC_EXPIRY " - "[--m10-max-grant-lifetime 60..3600]]\n", + "[--m10-max-grant-lifetime 60..3600] [--m10-area X0,Y0,X1,Y1] " + "[--m10-handoff-key F [--m10-neighbour " + "REGION=X0,Y0,X1,Y1=HOST:PORT=NAME=PUBFILE]...]]\n", stderr); return 2; } diff --git a/apps/evo-session-probe/main.cpp b/apps/evo-session-probe/main.cpp index c46b01d..e58eba3 100644 --- a/apps/evo-session-probe/main.cpp +++ b/apps/evo-session-probe/main.cpp @@ -11,6 +11,10 @@ // account supersedes an earlier one without bookkeeping. The file is created new and // owner-only (a credential: never commit or share it); the issuer key stays here. // The region refuses grants longer than its --m10-max-grant-lifetime (default 300). +// M10.8c (ADR-0096): --region may repeat; COUNT grants are written per region, +// interleaved in serial order, so a client crossing between regions always has a newer +// serial for the next one (serials must rise world-wide). A handoff key is a key pair +// from `keys` (issuer.key for --m10-handoff-key, issuer.pub for --m10-neighbour). // 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] @@ -25,6 +29,7 @@ // --snapshots N waits for N; --interest asks for another focus than the default) // marker TICK X Y APPLIED (the session's own probe marker in each new snapshot: um // position after TICK and the newest applied intention sequence; ADR-0094) +// handoff REGION ENDPOINT | redirect REGION ENDPOINT (v4, ADR-0096; then closed) // 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 @@ -162,6 +167,7 @@ struct RunOptions { action_base{1}, timeout_ms{20000}; std::optional account; std::vector queries; + std::vector more_regions; // grants: further --region values. std::optional interest; std::uint64_t snapshots{}; }; @@ -202,20 +208,29 @@ int grants(const RunOptions& o, const std::string& out, std::uint64_t count) { GrantClaims claims; claims.account = *o.account; claims.world = {o.world, o.frame}; - claims.region = o.region; claims.issued_at = now_seconds(); claims.expires_at = claims.issued_at + o.lifetime; const auto first = o.serial != 0 ? o.serial : claims.issued_at * 64; - std::vector bundle; - for (std::uint64_t i = 0; i < count; ++i) { - claims.serial = first + i; - const auto grant = sign(*key, claims); - if (!grant) { - std::puts("error grant"); - return 2; - } - bundle.insert(bundle.end(), grant->begin(), grant->end()); + std::vector regions{o.region}; + regions.insert(regions.end(), o.more_regions.begin(), o.more_regions.end()); + if (count * regions.size() > 64) { + std::puts("error count (at most 64 grants in all)"); + return 2; } + std::vector bundle; + std::uint64_t serial = first; + for (std::uint64_t i = 0; i < count; ++i) + for (const auto region : regions) { + claims.region = region; + claims.serial = serial++; + const auto grant = sign(*key, claims); + if (!grant) { + std::puts("error grant"); + return 2; + } + bundle.insert(bundle.end(), grant->begin(), grant->end()); + } + count *= regions.size(); if (!write_new(out, bundle)) { std::puts("error out (exists or unwritable)"); return 1; @@ -277,6 +292,13 @@ int run(const RunOptions& o) { while (Clock::now() < deadline) { c.poll(Clock::now()); if (c.state() == client::RegionSessionState::closed) { + if (const auto offer = c.take_handoff()) + std::printf("handoff %llu %s\n", + static_cast(offer->destination.region), + offer->destination.endpoint.c_str()); + if (const auto& to = c.redirect()) + std::printf("redirect %llu %s\n", static_cast(to->region), + to->endpoint.c_str()); std::printf("closed %u %s\n", static_cast( c.close_reason().value_or(SessionCloseReason::connection_lost)), @@ -410,6 +432,8 @@ int main(int argc, char** argv) { o.world = *n; else if (n && flag == "--frame") o.frame = *n; + else if (n && *n != 0 && flag == "--region" && bundle && o.region != 0) + o.more_regions.push_back(*n); else if (n && flag == "--region") o.region = *n; else if (n && flag == "--serial") diff --git a/deploy/kubernetes/region/dev-session-route/listener.yaml b/deploy/kubernetes/region/dev-session-route/listener.yaml index f0e686b..73ed370 100644 --- a/deploy/kubernetes/region/dev-session-route/listener.yaml +++ b/deploy/kubernetes/region/dev-session-route/listener.yaml @@ -12,7 +12,7 @@ spec: env: - {name: REGION_SESSION, value: "dev"} - {name: REGION_SESSION_LISTEN, value: "0.0.0.0:7400"} - - {name: REGION_SESSION_WINDOW, value: "dev-20261007T011838-10712:1791337118"} + - {name: REGION_SESSION_WINDOW, value: "dev-20261007T015045-2140:1791339045"} ports: # QUIC (ADR-0090): UDP only; nothing listens on TCP 7400. - {name: session, containerPort: 7400, protocol: UDP} diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index b0e6f75..23fdbc9 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -538,7 +538,8 @@ handoff ADR; EXPERIMENT durable probe records): 10.8a schema v12 probe records a transfer state machine with a PostgreSQL fault matrix done; 10.8b signed handoff tickets and ticket admission (durable stage, commit and control move) done; 10.8c-1 session protocol v4, durable probes in sessions, edge crossing, ghosts (schema v13) and a two-region loopback test -done; 10.8c-2 client switch and server configuration, 10.8d two-process kill tests follow. 10.9 planned. +done; 10.8c-2 client switch (offers, redirects, multi-region grants) and evo-server/probe +configuration done; 10.8d two-process kill tests follow. 10.9 planned. **Entry:** M9 client/session evidence and M7/M8 dev/stage durability. Read NETWORK_ARCHITECTURE, PERSISTENCE_ARCHITECTURE and ADR-0017. The M9 transport is diff --git a/docs/STATUS.md b/docs/STATUS.md index adf3972..977e50c 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -84,7 +84,10 @@ the session. M10.8c-1 wires the regions: - the destination shows arriving probes as ghosts (schema v13) and opens the marker where it crossed. -Next: 10.8c-2, the client switch and server configuration. +M10.8c-2 has the engineering client follow crossings (ticket, no loading screen) and +redirects, with grants picked by region in rising serial order. evo-server gains +`--m10-area`, `--m10-handoff-key` and `--m10-neighbour`; the probe tool writes multi-region +bundles. Next: 10.8d, two evo-server processes on PostgreSQL with kills at each step. ## Previous gate - 2026-10-02 diff --git a/docs/adr/0096-m10-probe-handoff.md b/docs/adr/0096-m10-probe-handoff.md index 8ac4434..d0f7bd2 100644 --- a/docs/adr/0096-m10-probe-handoff.md +++ b/docs/adr/0096-m10-probe-handoff.md @@ -187,3 +187,36 @@ controllable before M11. It passed 3 times in a row on Linux and also passes on Windows. - Not yet: the engineering client's switch (offer, redirect, multi-region grants), evo-server and probe configuration (10.8c-2), and real processes on PostgreSQL (10.8d). + +## M10.8c-2 client switch and configuration (2026-10-06) +- **Engineering client** (`apps/evo-client/remote_session.hpp`): + - On `Close(handed_off)` after a HandoffOffer it learns the destination's address (one + lookup), keeps the frozen view, uses no grant and connects at once with the ticket. The + panel shows "handing off", then the region and the count of crossings. + - On `Close(moved)` after a Redirect it connects to the owner with a grant for that + region. + - Grants are picked by their region claim. They are used in rising serial order + world-wide (durable control refuses a serial not above the newest), and a grant + outdated by one already used is discarded. + - A new region or epoch resets the views. +- **evo-server** (dev only, with the M10 listener): + - `--m10-area X0,Y0,X1,Y1`: the region's cells. + - `--m10-handoff-key FILE`: this region's P-256 scalar; the key id is the region id. + - `--m10-neighbour REGION=X0,Y0,X1,Y1=HOST:PORT=SERVER_NAME=PUBLIC_KEY_FILE`: up to 8; + the neighbour's key id is its region id. + + A handoff key needs neighbours and an area, and runs with the PostgreSQL probe and + handoff ledgers on the host's connection slot. Without these flags behaviour is as + before (in-memory markers). +- **Probe tool:** + - `grants` accepts `--region` several times (COUNT per region, serials interleaved in + order); + - `keys` makes handoff key pairs too; + - `run` prints `handoff` and `redirect` lines. +- **Evidence:** + - `client-remote-session`: the GUI session crosses into region 2 with the ticket (no + grant used, connected, marker past the border, probe owned there); a second client + with a newer bundle logging in at region 1 is redirected and takes over at region 2. + It passed 3 times in a row on Linux and also passes on Windows. + - The test fixture `tests/fixtures/fake_handoff_world.hpp` (the v12 state machine) is + shared by both handoff tests. diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 634fffd..5d9fcb0 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -4823,3 +4823,38 @@ Not done: Deployed v3 servers refuse v4 clients until redeployed; v12 and v13 need a rebuilt migrations image. + +## 2026-10-06 — M10.8c-2 client switch and handoff configuration (ADR-0096) + +Implemented: +- the engineering client follows handoff offers (ticket, no grant, no pause, view kept) and + redirects; +- grants are picked by region claim in rising serial order, and outdated grants are + discarded; +- views reset on a new region; +- evo-server `--m10-area`, `--m10-handoff-key` and `--m10-neighbour` (dev only), wiring the + PostgreSQL probe and handoff ledgers; +- the probe tool's multi-region `grants` and its `handoff`/`redirect` lines; +- the shared fixture `tests/fixtures/fake_handoff_world.hpp`. + +Tests: +- `client-remote-session` `crossing_regions`: + - the GUI session walks into region 2 and continues there with the ticket (no grant used, + connected, marker past the border, probe owned by region 2); + - a second client's login at region 1 is redirected to region 2 and takes over. + + It passed 3 times in a row on Linux, plus once more after a tidy fix. + +Executed: +- **Linux GCC 14:** + - `postgres-check.sh` with QUIC: 21 suites; + - presets `quic` 4/4 and `client-network` 16/16. +- **Linux Clang 19:** clang-tidy 19 on the client app, panels, remote test, probe, QUIC + session test and evo-server `main.cpp` with the bridge (one finding fixed); clang-format + 19. +- **Windows MSVC 19.44:** presets `quic` 4/4 and `client-network` 14/14. + +Not done: +- real two-process handoffs on PostgreSQL with kills (10.8d); +- a GUI run across regions on evolution-dev (one dev region only; needs v4 and v13 + deployed). diff --git a/docs/networking/M10_CLIENT_SESSIONS.md b/docs/networking/M10_CLIENT_SESSIONS.md index 50954d8..f3b911e 100644 --- a/docs/networking/M10_CLIENT_SESSIONS.md +++ b/docs/networking/M10_CLIENT_SESSIONS.md @@ -41,5 +41,23 @@ evo-client --connect void.aeternalisgames.com:7400 --server-name NAME --ca dev-c until the reconnect brings a fresh view. Nothing you press is sent meanwhile. - Each reconnect uses the next grant. A second client with a bundle for the same account takes the session over; the first stops (do not share bundles). -- A server on an older session protocol (v1, v2) refuses the client - (`unsupported_version`): deploy a v3 server first. +- A server on an older session protocol (v1-v3) refuses the client + (`unsupported_version`): deploy a v4 server first. + +## Two regions (M10.8, ADR-0096, EXPERIMENT) + +Regions with adjacent areas hand a probe over when it walks across the edge. The client +follows on its own: the view stays on screen, no grant is used, and the Session panel +counts the crossings. Each region needs: +- `--m10-area X0,Y0,X1,Y1` (its cells); +- `--m10-handoff-key issuer.key` (a pair from `evo-session-probe keys`); +- one `--m10-neighbour REGION=X0,Y0,X1,Y1=HOST:PORT=SERVER_NAME=neighbour.pub` per + neighbour. + +All of them need schema v13. Issue bundles with one `--region` per region: + +```sh +evo-session-probe grants --issuer-key issuer.key --world 100 --frame 9 --region 1 --region 2 --account --count 8 --lifetime 3600 --out tester.grants +``` + +A login at a region that does not own the probe is redirected to the one that does. diff --git a/tests/client_remote_session.cpp b/tests/client_remote_session.cpp index 7cc2947..fff1936 100644 --- a/tests/client_remote_session.cpp +++ b/tests/client_remote_session.cpp @@ -6,6 +6,7 @@ #include "evo/network/dev_credentials.hpp" #include "evo/network/session_grant.hpp" #include "evo/server/region_sessions.hpp" +#include "fixtures/fake_handoff_world.hpp" #include "remote_session.hpp" #include #include @@ -13,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -327,6 +329,131 @@ void refusals(Region& r) { CHECK(!RemoteSession::open(bad)); CHECK(!RemoteSession::open(r.material({}))); } +// M10.8c-2 (ADR-0096): the client follows a crossing into the next region with the offer's +// ticket (no grant, no pause, the view kept) and follows a redirect at login, picking grants +// by region from a two-region bundle in rising serial order. +void crossing_regions(const Files& f) { + using evo::test::RegionDb; + using evo::test::WorldDb; + constexpr std::int64_t m = 1'000'000; + const auto area = [](std::int64_t x0) { + return network::motion::Area{x0, 0, x0 + (10 * m) - 1, (10 * m) - 1, 0}; + }; + WorldDb world; + RegionDb db1{world, 1}, db2{world, 2}; + auto key1 = std::make_shared( + *HandoffSigningKey::generate(DeploymentEnvironment::dev, 41)); + auto key2 = std::make_shared( + *HandoffSigningKey::generate(DeploymentEnvironment::dev, 42)); + auto imported = GrantSigningKey::import(DeploymentEnvironment::dev, 1, scalar()); + CHECK(imported.has_value()); + const auto& issuer = *imported; + const auto make = [&](std::uint64_t id, RegionDb& db, + std::shared_ptr own, + const HandoffSigningKey& neighbour_key, std::uint64_t neighbour, + std::uint16_t neighbour_port, std::uint16_t port) { + server::RegionSessionsConfig c; + c.transport.certificate_pem_file = f.certificate.string(); + c.transport.key_pem_file = f.key.string(); + c.transport.port = port; + c.control.environment = DeploymentEnvironment::dev; + c.control.world = world_id; + c.control.region = id; + c.control.epoch = 3; + c.control.max_account_attempts = 16; + c.issuer_keys.push_back({1, *issuer.public_key()}); + c.ledger = &db; + c.handoff = &db; + c.probes = &db; + c.handoff_key = std::move(own); + c.handoff_keys.push_back( + {neighbour, static_cast(neighbour + 40), *neighbour_key.public_key()}); + c.neighbours.push_back( + {{neighbour, "localhost:" + std::to_string(neighbour_port), std::string{name}}, + area(neighbour == 1 ? 0 : 10 * m)}); + c.ghost_poll = std::chrono::milliseconds{20}; + auto made = server::RegionSessions::create(std::move(c)); + CHECK(made.has_value()); + const auto x0 = id == 1 ? 0 : 10 * m; + CHECK((*made)->set_motion_area(area(x0), x0 + (5 * m), 5 * m)); + (*made)->set_default_interest({(x0 / m) + 5, 5, 0, 7}); + return std::move(*made); + }; + // Region 2 listens on a port found by binding a throwaway region first. + std::uint16_t port2 = 0; + { + auto probe = make(2, db2, key2, *key1, 1, 1, 0); + port2 = probe->port(); + } + auto r1 = make(1, db1, key1, *key2, 2, port2, 0); + auto r2 = make(2, db2, key2, *key1, 1, r1->port(), port2); + std::uint64_t tick = 100; + const auto grants = [&](const AccountId& who, std::uint64_t first) { + std::vector out; + for (std::uint64_t i = 0; i < 4; ++i) { + GrantClaims c; + c.grant_id = 5000 + first + i; + c.account = who; + c.serial = first + i; + c.world = world_id; + c.region = (i % 2) + 1; // Interleaved: region 1, 2, 1, 2. + c.issued_at = start; + c.expires_at = start + 120; + out.push_back(*issuer.sign(c)); + } + return out; + }; + const auto material = [&](std::vector g) { + return client::ConnectMaterial{ + {"localhost", r1->port()}, std::string{name}, f.ca_pem, f.ca.string(), std::move(g)}; + }; + const auto pump = [&](const std::vector& clients, const std::function& done) { + const auto end = Clock::now() + std::chrono::seconds{20}; + for (int i = 1; Clock::now() < end; ++i) { + if (i % 5 == 0) + ++tick; + for (auto* r : {r1.get(), r2.get()}) { + r->poll({true, 3, tick}, start + 1, Clock::now()); + r->replicate(tick, [](const SessionInterest&) { + ReplicationSource s; + s.identity = world_id; + return s; + }); + static_cast(r->take_events()); + } + for (auto* c : clients) + c->host.step(Clock::now(), c->intention, c->ui); + if (done()) + return true; + std::this_thread::sleep_for(std::chrono::milliseconds{2}); + } + return done(); + }; + const auto dana = account(31); + Client a; + CHECK(a.host.connect(RemoteSession::open(material(grants(dana, 10))))); + auto& s = *a.host.session(); + CHECK(pump({&a}, [&] { return s.status().phase == "connected" && s.status().marker; })); + CHECK(s.status().region == 1 && s.status().marker_x_mm == 5000); + // Walking east: the crossing is followed into region 2 with the ticket, no grant used. + a.intention.move_x = 1000; + const auto left = s.status().grants_left; + CHECK(pump({&a}, [&] { + const auto st = s.status(); + return st.region == 2 && st.phase == "connected" && st.handoffs == 1 && st.marker; + })); + CHECK(s.status().grants_left == left && a.phase() == LaunchPhase::connected && + world.probes.at(dana).owner == 2 && s.status().marker_x_mm >= 10'000); + a.intention.move_x = 0; + // A second client with a newer bundle logs in at region 1 and is redirected to region 2, + // where it takes over (the first is superseded). + Client b; + CHECK(b.host.connect(RemoteSession::open(material(grants(dana, 20))))); + auto& t = *b.host.session(); + CHECK( + pump({&a, &b}, [&] { return t.status().region == 2 && t.status().phase == "connected"; })); + CHECK(a.phase() == LaunchPhase::failed && world.probes.at(dana).owner == 2); +} } // namespace int main() try { @@ -337,6 +464,7 @@ int main() try { remote_markers(r); refusals(r); } + crossing_regions(files); std::filesystem::remove_all(files.dir); std::puts("client remote session tests passed"); return 0; diff --git a/tests/fixtures/fake_handoff_world.hpp b/tests/fixtures/fake_handoff_world.hpp new file mode 100644 index 0000000..1889a55 --- /dev/null +++ b/tests/fixtures/fake_handoff_world.hpp @@ -0,0 +1,119 @@ +#pragma once +// M10.8c test fixture (ADR-0096): a fake world database with the same probe transfer state +// machine as schema v12 (prepared -> committed or aborted; stage and commit happen together +// at the ticket login), serving one region's ProbeLedger, HandoffLedger and ControlLedger. +#include "evo/network/handoff_ticket.hpp" +#include "evo/network/session_control.hpp" +#include "evo/server/probe_ledger.hpp" +#include +#include +#include +#include +#include +#include + +namespace evo::test { +using network::AccountId; +using network::ControlLease; +using network::ControlLedger; +using network::ControlLedgerError; +using network::HandoffAdmission; +using network::HandoffLedger; +using network::HandoffLedgerError; +using network::TicketClaims; +using network::TransferId; +struct WorldDb { + struct Probe { + std::uint64_t owner{}, generation{}; + std::int64_t x{}, y{}; + std::optional transfer; + }; + enum class State : std::uint8_t { prepared, committed, aborted }; + struct Transfer { + AccountId account; + std::uint64_t source{}, destination{}, generation{}; + std::int64_t x{}, y{}; + State state{}; + }; + std::map probes; + std::map transfers; +}; +struct RegionDb final : server::ProbeLedger, HandoffLedger, ControlLedger { + RegionDb(WorldDb& w, std::uint64_t r) : world{&w}, region{r} {} + std::expected + open(const AccountId& account, std::int64_t x, std::int64_t y, std::int64_t) override { + auto [it, created] = + world->probes.try_emplace(account, WorldDb::Probe{region, 1, x, y, std::nullopt}); + const auto& p = it->second; + return server::ProbeHome{p.owner, p.generation, p.x, p.y, p.transfer.has_value()}; + } + void save(const AccountId& account, std::uint64_t generation, std::int64_t x, std::int64_t y, + std::int64_t) override { + auto& p = world->probes.at(account); + if (p.owner == region && p.generation == generation && !p.transfer) { + p.x = x; + p.y = y; + ++saves; + } + } + std::expected + prepare(const AccountId& account, std::uint64_t generation, const TransferId& transfer, + std::uint64_t destination, std::int64_t x, std::int64_t y, std::int64_t) override { + auto& p = world->probes.at(account); + if (p.owner != region || p.generation != generation || p.transfer) + return std::unexpected(server::ProbeLedgerError::unavailable); + p.transfer = transfer; + p.x = x; + p.y = y; + world->transfers[transfer] = { + account, region, destination, generation, x, y, WorldDb::State::prepared}; + return {}; + } + std::expected abort(const TransferId& transfer) override { + auto& t = world->transfers.at(transfer); + if (t.state == WorldDb::State::prepared) { + t.state = WorldDb::State::aborted; + world->probes.at(t.account).transfer.reset(); + } + return t.state == WorldDb::State::aborted; + } + std::expected, server::ProbeLedgerError> + incoming() override { + std::vector out; + for (const auto& [id, t] : world->transfers) + if (t.destination == region && t.state == WorldDb::State::prepared) + out.push_back({id, t.x, t.y}); + return out; + } + std::expected admit(const TicketClaims& ticket, + std::uint64_t) override { + const auto found = world->transfers.find(ticket.transfer); + if (found == world->transfers.end() || found->second.destination != region || + found->second.account != ticket.account || + found->second.state == WorldDb::State::aborted) + return std::unexpected(HandoffLedgerError::refused); + auto& t = found->second; + auto& p = world->probes.at(t.account); + if (t.state == WorldDb::State::prepared) { // Stage and commit: the switch. + t.state = WorldDb::State::committed; + p = {region, t.generation + 1, t.x, t.y, std::nullopt}; + } + return HandoffAdmission{ControlLease{++control, {}, std::chrono::milliseconds{20000}}, + p.generation, p.x, p.y}; + } + std::expected claim(const AccountId&, std::uint64_t, + std::uint64_t) override { + return ControlLease{++control, {}, std::chrono::milliseconds{20000}}; + } + std::expected renew(const AccountId&, + std::uint64_t) override { + return std::chrono::milliseconds{20000}; + } + void release(const AccountId&, std::uint64_t) override { + ++releases; + } + WorldDb* world; + std::uint64_t region; + std::uint64_t control{}, saves{}, releases{}; +}; +} // namespace evo::test diff --git a/tests/quic_sessions.cpp b/tests/quic_sessions.cpp index 6667e59..e9de284 100644 --- a/tests/quic_sessions.cpp +++ b/tests/quic_sessions.cpp @@ -8,6 +8,7 @@ #include "evo/network/session_grant.hpp" #include "evo/server/probe_ledger.hpp" #include "evo/server/region_sessions.hpp" +#include "fixtures/fake_handoff_world.hpp" #include #include #include @@ -628,102 +629,10 @@ void ticket_login(const Files& f) { CHECK(a->state() == RegionSessionState::established && handoff.admitted.size() == 1); } -// M10.8c (ADR-0096): two regions with adjacent areas in one process, over a fake world -// database with the same transfer state machine as schema v12. -struct WorldDb { - struct Probe { - std::uint64_t owner{}, generation{}; - std::int64_t x{}, y{}; - std::optional transfer; - }; - enum class State : std::uint8_t { prepared, committed, aborted }; - struct Transfer { - AccountId account; - std::uint64_t source{}, destination{}, generation{}; - std::int64_t x{}, y{}; - State state{}; - }; - std::map probes; - std::map transfers; -}; -struct RegionDb final : server::ProbeLedger, HandoffLedger, ControlLedger { - RegionDb(WorldDb& w, std::uint64_t r) : world{&w}, region{r} {} - std::expected - open(const AccountId& account, std::int64_t x, std::int64_t y, std::int64_t) override { - auto [it, created] = - world->probes.try_emplace(account, WorldDb::Probe{region, 1, x, y, std::nullopt}); - const auto& p = it->second; - return server::ProbeHome{p.owner, p.generation, p.x, p.y, p.transfer.has_value()}; - } - void save(const AccountId& account, std::uint64_t generation, std::int64_t x, std::int64_t y, - std::int64_t) override { - auto& p = world->probes.at(account); - if (p.owner == region && p.generation == generation && !p.transfer) { - p.x = x; - p.y = y; - ++saves; - } - } - std::expected - prepare(const AccountId& account, std::uint64_t generation, const TransferId& transfer, - std::uint64_t destination, std::int64_t x, std::int64_t y, std::int64_t) override { - auto& p = world->probes.at(account); - if (p.owner != region || p.generation != generation || p.transfer) - return std::unexpected(server::ProbeLedgerError::unavailable); - p.transfer = transfer; - p.x = x; - p.y = y; - world->transfers[transfer] = { - account, region, destination, generation, x, y, WorldDb::State::prepared}; - return {}; - } - std::expected abort(const TransferId& transfer) override { - auto& t = world->transfers.at(transfer); - if (t.state == WorldDb::State::prepared) { - t.state = WorldDb::State::aborted; - world->probes.at(t.account).transfer.reset(); - } - return t.state == WorldDb::State::aborted; - } - std::expected, server::ProbeLedgerError> - incoming() override { - std::vector out; - for (const auto& [id, t] : world->transfers) - if (t.destination == region && t.state == WorldDb::State::prepared) - out.push_back({id, t.x, t.y}); - return out; - } - std::expected admit(const TicketClaims& ticket, - std::uint64_t) override { - const auto found = world->transfers.find(ticket.transfer); - if (found == world->transfers.end() || found->second.destination != region || - found->second.account != ticket.account || - found->second.state == WorldDb::State::aborted) - return std::unexpected(HandoffLedgerError::refused); - auto& t = found->second; - auto& p = world->probes.at(t.account); - if (t.state == WorldDb::State::prepared) { // Stage and commit: the switch. - t.state = WorldDb::State::committed; - p = {region, t.generation + 1, t.x, t.y, std::nullopt}; - } - return HandoffAdmission{ControlLease{++control, {}, std::chrono::milliseconds{20000}}, - p.generation, p.x, p.y}; - } - std::expected claim(const AccountId&, std::uint64_t, - std::uint64_t) override { - return ControlLease{++control, {}, std::chrono::milliseconds{20000}}; - } - std::expected renew(const AccountId&, - std::uint64_t) override { - return std::chrono::milliseconds{20000}; - } - void release(const AccountId&, std::uint64_t) override { - ++releases; - } - WorldDb* world; - std::uint64_t region; - std::uint64_t control{}, saves{}, releases{}; -}; +// M10.8c (ADR-0096): two regions with adjacent areas in one process, over the fake world +// database of tests/fixtures/fake_handoff_world.hpp. +using evo::test::RegionDb; +using evo::test::WorldDb; constexpr std::int64_t m_um = 1'000'000; // One metre (one cell) in micrometres. // Region `id` owns x in [x0, x0 + 10 m), y in [0, 10 m) of slice 0. network::motion::Area area_of(std::int64_t x0) {