From 5728e7682e8cdffb88af3d4b946fc4ce09e03140 Mon Sep 17 00:00:00 2001 From: dingo Date: Tue, 6 Oct 2026 17:50:41 -0400 Subject: [PATCH] Wire M10.7b interpolation and the stale loss view into the client ADR-0095. The engineering client times presentation with the tick clock: its own marker between ticks, other markers interpolated a few ticks behind (grey when stale). Silence beyond 3 ticks and long-gap holds are stale. A lost session leaves its last view frozen and labelled stale until the reconnect's fresh baseline; nothing is predicted or sent meanwhile. evo-motion-trace --remote measures remote-marker error between two clients through the relay, with a 1.5 s outage (motion-trace-remote-smoke CTest). Prediction errors are recorded only for ticks inputs are scheduled for. Co-Authored-By: Claude Opus 5.5 --- apps/evo-client/main.cpp | 16 +- apps/evo-client/panels.cpp | 8 +- apps/evo-client/remote_session.hpp | 85 ++- cmake/ClientProbe.cmake | 3 + docs/ROADMAP.md | 4 +- docs/STATUS.md | 7 +- ...0095-m10-interpolation-and-session-loss.md | 31 ++ docs/engineering/VALIDATION.md | 44 ++ docs/networking/M10_CLIENT_SESSIONS.md | 5 +- docs/networking/M10_PREDICTION_TRACES.md | 42 ++ include/evo/client/interpolation.hpp | 11 +- include/evo/client/motion_prediction.hpp | 8 +- src/client/prediction/interpolation.cpp | 11 +- src/client/prediction/motion_prediction.cpp | 12 +- tests/client_remote_session.cpp | 46 ++ tests/network_interpolation.cpp | 7 +- tools/network/motion_trace.cpp | 521 ++++++++++++------ 17 files changed, 651 insertions(+), 210 deletions(-) diff --git a/apps/evo-client/main.cpp b/apps/evo-client/main.cpp index 8492b17..96cde64 100644 --- a/apps/evo-client/main.cpp +++ b/apps/evo-client/main.cpp @@ -184,17 +184,19 @@ std::optional parse_point(std::string_view value) { return std::nullopt; return evo::client::ScreenPoint{static_cast(x), static_cast(y)}; } -// A probe marker (ADR-0094): the own one larger and amber, others smaller and blue; clipped. -void marker_box(std::vector& out, double x, double y, bool own, +// A probe marker (ADR-0094): the own one larger and amber, others smaller and blue; stale +// ones (no current authority, ADR-0095) grey. Clipped to the viewport. +void marker_box(std::vector& out, double x, double y, bool own, bool stale, double width, double height) { const double half = own ? 7 : 4; const double x0 = std::max(x - half, 0.0), y0 = std::max(y - half, 0.0); const double x1 = std::min(x + half, width), y1 = std::min(y + half, height); if (x1 > x0 && y1 > y0) - out.push_back( - {static_cast(x0), static_cast(y0), static_cast(x1 - x0), - static_cast(y1 - y0), - own ? evo::render::Color{250, 190, 40, 255} : evo::render::Color{90, 170, 250, 255}}); + out.push_back({static_cast(x0), static_cast(y0), static_cast(x1 - x0), + static_cast(y1 - y0), + stale ? evo::render::Color{120, 120, 120, 255} + : own ? evo::render::Color{250, 190, 40, 255} + : evo::render::Color{90, 170, 250, 255}}); } void outline(std::vector& out, double l, double t, double r, double b, double width, double height) { @@ -544,7 +546,7 @@ int main(int argc, char** argv) try { if (const auto* remote = host.session(); remote && mode() == Mode::remote) for (const auto& m : remote->markers()) if (const auto at = camera->world_to_screen({m.x_mm, m.y_mm})) - marker_box(boxes, at->x, at->y, m.own, width, height); + marker_box(boxes, at->x, at->y, m.own, m.stale, width, height); if (const auto* o = selection.organism(orgs)) { const auto b = evo::render::organism_bounds(*o, *camera); outline(boxes, b.left - 3, b.top - 3, b.right + 3, b.bottom + 3, width, height); diff --git a/apps/evo-client/panels.cpp b/apps/evo-client/panels.cpp index 84a3cb7..6630bbb 100644 --- a/apps/evo-client/panels.cpp +++ b/apps/evo-client/panels.cpp @@ -153,7 +153,8 @@ void banner(const PanelView& v, PanelActions& a) { a.open_controls = true; } else { ImGui::TextColored(v.remote && v.remote->phase == "connected" ? green : amber, - "DEV REGION - %s", v.remote ? v.remote->phase.c_str() : "?"); + "DEV REGION - %s%s", v.remote ? v.remote->phase.c_str() : "?", + v.remote && v.remote->stale_view ? " - STALE (last view, frozen)" : ""); ImGui::SameLine(); if (ImGui::SmallButton("Disconnect")) a.ask_disconnect = true; @@ -366,7 +367,10 @@ void session(const PanelView& v) { row("Corrections", std::to_string(s.corrections) + ", max error " + std::to_string(s.max_error_um / 1000) + " mm, " + std::to_string(s.snaps) + " snaps"); - row("Other markers", std::to_string(s.other_markers)); + char delay[48]; + std::snprintf(delay, sizeof delay, "%zu, %.1f ticks behind, %zu stale", s.other_markers, + s.interpolation_delay, s.stale_markers); + row("Other markers", delay); } ImGui::EndTable(); } diff --git a/apps/evo-client/remote_session.hpp b/apps/evo-client/remote_session.hpp index 020511f..9e1dd2e 100644 --- a/apps/evo-client/remote_session.hpp +++ b/apps/evo-client/remote_session.hpp @@ -17,6 +17,12 @@ // intention that was lost or refused is sent again once nothing is in flight and the // authority holds another vector. While following, the camera and the interest follow the // predicted marker (or, before a marker exists, the server's default view). +// +// M10.7b (ADR-0095): a drift-corrected tick clock (observed server ticks) times the +// presentation. The own marker is drawn between ticks; other markers are interpolated +// `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. #include "evo/client/camera.hpp" #include "evo/client/environment_view.hpp" #include "evo/client/input.hpp" @@ -33,6 +39,7 @@ #include #ifdef EVO_CLIENT_NETWORK +#include "evo/client/interpolation.hpp" #include "evo/client/motion_prediction.hpp" #include "evo/client/region_session.hpp" #include "evo/network/quic_transport.hpp" @@ -64,11 +71,18 @@ struct RemoteStatus { // Refusal receipts (each, whether or not the input was still pending), inputs the // predictor removed as refused or dropped as never applied, and repeated vectors. std::uint64_t refused_inputs{}, rejected_inputs{}, dropped_inputs{}, resends{}, max_pending{}; + // 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::size_t stale_markers{}; + bool stale_view{}; }; // A probe marker to draw: the own one predicted and smoothed, others as replicated. struct MarkerView { double x_mm{}, y_mm{}; bool own{}; + bool stale{}; // No current authority behind it (draw dimmed). + bool extrapolated{}; // Continued beyond the newest snapshot (at most two ticks). }; #ifdef EVO_CLIENT_NETWORK @@ -78,6 +92,7 @@ static_assert(std::is_same_v, class RemoteSession { public: static constexpr std::uint16_t interest_radius = 7; + static constexpr double silent_ticks = 3; // Extrapolation limit (2) plus the clock's lead. // Looks the endpoint up once (blocking); nothing connects until the first step(). static std::optional open(const client::ConnectMaterial& m) { const auto address = network::quic::resolve_ipv4(m.endpoint.host); @@ -136,7 +151,7 @@ class RemoteSession { s.focus_y = sent_interest_->y; s.focus_z = sent_interest_->z; } - for (const auto& m : markers_) + for (const auto& m : markers_) { if (m.own) { s.marker = true; s.marker_x_mm = m.x_mm; @@ -144,6 +159,11 @@ class RemoteSession { } else { ++s.other_markers; } + s.stale_markers += m.stale ? 1 : 0; + } + s.interpolation_delay = interpolator_.delay(); + s.remote_render_tick = render_tick_; + s.stale_view = stale_view_; s.inputs_pending = predictor_.pending(); s.lead = lead_.lead(); s.corrections = predictor_.stats().corrections; @@ -208,6 +228,10 @@ class RemoteSession { if (!established_) welcome(ui, launcher); server_tick_ = c.server_tick(); + if (server_tick_ != observed_tick_) { // Every watermark or snapshot tick times the clock. + observed_tick_ = server_tick_; + clock_.observe(server_tick_, now); + } send(intention); receipts(); snapshot(); @@ -281,6 +305,11 @@ class RemoteSession { predictor_.reset(); // A new session has a new marker. smoother_.reset(); lead_.reset(); + clock_.reset(); // Fresh baseline: the frozen view of a lost session is dropped. + interpolator_.reset(); + markers_.clear(); + observed_tick_ = 0; + stale_view_ = false; launcher.observe(client::ConnectSignal::welcome); notify(ui, "Connected to the dev region (fresh baseline).", client::NoticeLevel::info); std::printf("session welcome session=%llu epoch=%llu tick=%llu\n", @@ -367,13 +396,14 @@ class RemoteSession { static_cast(organisms_.accept(std::move(*o)).has_value()); } } - others_.clear(); + std::vector remote; for (const auto& m : v->markers) { if (m.own) predictor_.authoritative({v->tick, m.applied_sequence, m.state, m.area}); else - others_.push_back(m); + remote.push_back({m.id, m.state}); } + interpolator_.push(v->tick, remote); if (!following_ || v->environment.empty() || predictor_.authority()) return; // With a marker, present() follows it instead. const auto [lx, hx] = @@ -397,23 +427,36 @@ class RemoteSession { : 0; last_present_ = now; markers_.clear(); - const auto predicted = predictor_.predict(server_tick_ + lead_.lead()); + interpolator_.advance(elapsed); + const auto clock = clock_.at(now).value_or(static_cast(server_tick_)); + // The own marker in the predicted present, between whole ticks. + const auto presented = clock + static_cast(lead_.lead()); + const auto whole = static_cast(std::floor(presented)); + // Errors are measured only for ticks inputs are already being scheduled for. + const auto predicted = predictor_.predict_between( + whole, presented - static_cast(whole), whole <= server_tick_ + lead_.lead()); const auto& authority = predictor_.authority(); if (predicted && authority) { smoother_.add(predictor_.take_correction()); const auto offset = smoother_.advance(elapsed); - const MarkerView own{static_cast(predicted->x + offset.x) / 1000, - static_cast(predicted->y + offset.y) / 1000, true}; + const MarkerView own{(predicted->x + static_cast(offset.x)) / 1000, + (predicted->y + static_cast(offset.y)) / 1000, true}; markers_.push_back(own); if (following_) { recentre_ = offered_ = client::WorldPoint{own.x_mm, own.y_mm}; - wanted_ = network::SessionInterest{cell(predicted->x), cell(predicted->y), - authority->area.z, interest_radius}; + const auto x = static_cast(std::floor(predicted->x)); + const auto y = static_cast(std::floor(predicted->y)); + wanted_ = + network::SessionInterest{cell(x), cell(y), authority->area.z, interest_radius}; } } - for (const auto& m : others_) - markers_.push_back({static_cast(m.state.x) / 1000, - static_cast(m.state.y) / 1000, false}); + // Other markers in the recent past, interpolated. + render_tick_ = clock - interpolator_.delay(); + // A silent server freezes the clock; beyond the extrapolation limit that silence is + // what makes the shown markers stale. + const bool silent = clock_.silence(now).value_or(0) > silent_ticks; + for (const auto& m : interpolator_.at(render_tick_)) + markers_.push_back({m.x / 1000, m.y / 1000, false, m.stale || silent, m.extrapolated}); } static std::int64_t cell(std::int64_t um) { constexpr auto size = network::motion::micrometres_per_cell; @@ -430,9 +473,19 @@ class RemoteSession { pending_.clear(); environment_.mark_stale(); organisms_.mark_stale(); + // Freeze the view as it was: the own marker at its last authoritative position, + // everything stale. Nothing is predicted or sent until a new session starts afresh. + const auto& authority = predictor_.authority(); + std::erase_if(markers_, [](const MarkerView& m) { return m.own; }); + if (authority) + markers_.push_back({static_cast(authority->state.x) / 1000, + static_cast(authority->state.y) / 1000, true}); + for (auto& m : markers_) { + m.stale = true; + m.extrapolated = false; + } + stale_view_ = was_established || stale_view_; predictor_.reset(); // The marker ended with its session. - others_.clear(); - markers_.clear(); client_.reset(); established_ = false; const bool not_ready = @@ -483,8 +536,12 @@ class RemoteSession { client::InputLead lead_; std::optional last_present_; std::uint64_t resends_{}, refused_{}; - std::vector others_; + client::TickClock clock_; + client::MarkerInterpolator interpolator_; + std::uint64_t observed_tick_{}; + double render_tick_{}; std::vector markers_; + bool stale_view_{}; bool established_{}, welcomed_{}, exhausted_{}; bool following_{true}; // The camera follows the server's (default) view. }; diff --git a/cmake/ClientProbe.cmake b/cmake/ClientProbe.cmake index 7accac2..dcdb3a0 100644 --- a/cmake/ClientProbe.cmake +++ b/cmake/ClientProbe.cmake @@ -102,6 +102,9 @@ if(EVO_BUILD_CLIENT) evo_msquic_runtime(evo-motion-trace) add_test(NAME motion-trace-smoke COMMAND evo-motion-trace --smoke) set_tests_properties(motion-trace-smoke PROPERTIES TIMEOUT 120 LABELS "client;quic-session") + # M10.7b: two clients, remote-marker interpolation error and a 1.5 s outage (ADR-0095). + add_test(NAME motion-trace-remote-smoke COMMAND evo-motion-trace --remote --smoke) + set_tests_properties(motion-trace-remote-smoke PROPERTIES TIMEOUT 120 LABELS "client;quic-session") endif() endif() if(BUILD_TESTING) diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index ef115ef..8e41262 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -532,8 +532,8 @@ probe) with process evidence done; 10.5c engineering client switched to M10 sess probe-motion-v1 rule, server markers, session protocol v3 marker records and the client predictor done; 10.6b wiring (sessions, evo-server, probe, GUI marker) done; 10.6c impaired traces done (networking/M10_PREDICTION_TRACES.md). 10.7 split (ADR-0095): 10.7a tick clock, -remote-marker interpolation and fractional prediction done; 10.7b client wiring, loss view -and two-client trace follow. 10.8-10.9 planned. +remote-marker interpolation and fractional prediction done; 10.7b client wiring, stale loss +view and two-client interpolation traces done. 10.8-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 6710ff1..a2d0047 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -61,8 +61,11 @@ camera and interest. M10.6c traces it through the impairment relay: exact withou 94-99 % exact at 60-200 ms RTT with 2-5 % loss, convergence after stopping and a restart resync of about 2-3 s (networking/M10_PREDICTION_TRACES.md). M10.6 is complete. M10.7a (ADR-0095) adds a drift-corrected tick clock, adaptive interpolation of remote markers (2-6 ticks behind, at most 2 ticks extrapolated, then -stale; organisms never) and sub-tick own-marker presentation. Next: 10.7b client wiring, -the stale loss view and a two-client trace. +stale; organisms never) and sub-tick own-marker presentation. M10.7b wires it into the +engineering client. A lost session's view stays frozen and labelled stale until the +reconnect's fresh baseline. In two-client traces, remote markers shown as current stay +within about 0.1 m of the true path, and outages show stale. M10.7 is complete. Next: +M10.8 authority transfer between two regions (needs a ratified handoff ADR). ## Previous gate - 2026-10-02 diff --git a/docs/adr/0095-m10-interpolation-and-session-loss.md b/docs/adr/0095-m10-interpolation-and-session-loss.md index c69ddf7..02e4bf1 100644 --- a/docs/adr/0095-m10-interpolation-and-session-loss.md +++ b/docs/adr/0095-m10-interpolation-and-session-loss.md @@ -55,3 +55,34 @@ when a snapshot is lost. No controllable organism moves yet. - Remote markers are shown 100-300 ms in the past, the own marker in the predicted present. That is the usual split; interactions that need both are later work. - Extrapolated or stale markers are flagged so the client can draw them differently. + +## M10.7b client wiring, loss view and traces (2026-10-06) +- **Engineering client** (`apps/evo-client/remote_session.hpp`): + - every newly observed server tick (watermark or snapshot) feeds the clock; + - each snapshot's other markers go to the interpolator; + - the own marker is presented at clock + lead, between whole ticks; + - other markers are presented at clock - delay; + - the Session panel shows the delay and the stale count; + - stale markers are drawn grey. +- **Silence is stale.** The clock freezes one tick past the last observation, so on a + silent link the render tick never passes the newest snapshot. Markers are therefore also + stale once 3 ticks (150 ms) pass without any observation. A marker held across a gap of + more than 8 ticks is stale too. +- **Session loss.** The last view stays frozen: the own marker at its last authoritative + position, all markers stale. The banner reads "STALE (last view, frozen)" and the + environment is already stale-encoded. Nothing is predicted or sent. Welcome resets the + clock, interpolator and markers (fresh baseline). The server already stops admission and + removes the marker when a session closes: no offline progression. +- **Error measurement** records a prediction only for ticks at or before the newest server + tick plus the lead. The clock may run one tick past the newest observation, and inputs + for such a tick may still come. Recording it gave false errors, seen as a Windows-only + test failure. +- **Evidence:** + - `client-remote-session`: the frozen stale view and its reset on reconnect; two + sessions each seeing the other's marker behind its own predicted view; removal when + one leaves. + - `evo-motion-trace --remote`, with the CTest `motion-trace-remote-smoke`: current + markers' 95th percentile within 1 m, the outage mostly shown stale, recovery after it. + - Figures in [M10_PREDICTION_TRACES](../networking/M10_PREDICTION_TRACES.md). +- Not covered: interpolating organisms (none move; they are shown as replicated and never + extrapolated). M10.7 is complete. diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 4071ee4..1cce469 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -4635,3 +4635,47 @@ Executed: - `client-network`: 13/13. Not done: client wiring, the loss view and the two-client trace (10.7b). + +## 2026-10-06 — M10.7b client interpolation, loss view and traces (ADR-0095) + +Implemented: +- the engineering client's tick clock, interpolated remote markers (grey when stale), the + own marker between ticks, and the frozen stale view on loss (banner, all markers stale, + own marker at its last authoritative position), reset on Welcome; +- `TickClock::silence` and silence-based staleness; long-gap holds marked stale; +- error recording limited to scheduled ticks (`predict_between(..., record)`); +- `evo-motion-trace --remote` and the CTest `motion-trace-remote-smoke`; +- a shared region bench in the trace tool. + +Found and fixed during development: +- an outage never showed stale (the frozen clock kept the render tick inside the buffer); +- post-outage holds were shown as current; +- false prediction errors from presenting one tick past the scheduled ticks (a Windows-only + `client-remote-session` failure); +- MSVC rejecting `std::erase_if` with a member-pointer predicate; +- two clang-tidy optional-access findings. + +Figures are in docs/networking/M10_PREDICTION_TRACES.md: +- remote markers shown as current: p99 at most 60.7 mm, max 100 mm; +- outages: 84-90 % of samples shown stale, recovery 150-950 ms; +- smoke variance: one of three runs had 32 % extrapolated and 36 % stale samples, with + current errors still 0. + +Executed: +- **Linux GCC 14:** + - preset `client-network`: CTest 16/16 (including both trace smokes and + client-remote-session); + - preset `quic`: 4/4; + - full remote and prediction trace runs. +- **Linux Clang 19:** ASan/UBSan `evo-network-tests`, and clang-tidy 19 on the trace tool, + client app, panels, remote test, interpolation sources and tests. +- **Windows MSVC 19.44** (fresh binaries): + - preset `client-network`: 14/14; + - `client-remote-session` passed 5 times in a row after the fix; + - preset `quic`: 4/4. + +The Postgres suite was not rerun: no server code changed in M10.7. + +Not done: +- interpolating organisms (none move); +- a GUI run against evolution-dev (needs a v3 server). diff --git a/docs/networking/M10_CLIENT_SESSIONS.md b/docs/networking/M10_CLIENT_SESSIONS.md index d83696a..50954d8 100644 --- a/docs/networking/M10_CLIENT_SESSIONS.md +++ b/docs/networking/M10_CLIENT_SESSIONS.md @@ -35,7 +35,10 @@ evo-client --connect void.aeternalisgames.com:7400 --server-name NAME --ca dev-c are blue), an engineering experiment, not an organism (ADR-0094). It is predicted locally and corrected by the server; the camera follows it until you pan or zoom. The Session panel lists each intention as sent, admitted, rejected, simulated (final; - intentions are never durable) or unknown, plus the prediction figures. + intentions are never durable) or unknown, plus the prediction figures. Other players' + markers are shown a few ticks in the past, interpolated (ADR-0095); grey means stale. +- If the connection is lost, the last view stays on screen, frozen and labelled STALE, + 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 diff --git a/docs/networking/M10_PREDICTION_TRACES.md b/docs/networking/M10_PREDICTION_TRACES.md index 9825e81..c722f93 100644 --- a/docs/networking/M10_PREDICTION_TRACES.md +++ b/docs/networking/M10_PREDICTION_TRACES.md @@ -62,3 +62,45 @@ row: 95.9-98.3 % exact, resync 2,241-2,248 ms. - Many clients at once (10.8). - Real links (dev route); deferred until a v3 server is deployed. - Sub-tick presentation: the marker steps at 20 Hz. + +## M10.7b remote interpolation (2026-10-06, ADR-0095) +`evo-motion-trace --remote`. Two clients, each behind its own relay with the profile's +impairment. A moves with the script; B shows A's marker interpolated. Every 10 ms B's +presented position is compared with A's authoritative path, interpolated linearly at B's +render tick (clock minus delay). Midway through the 20 s, B's link is cut for 1.5 s +(outage). "Current" excludes markers shown as stale. + +| Profile | Samples | p99 mm (all) | max mm (all) | Current p99 mm | Current max mm | Extrapolated | Stale | Mean delay (ticks) | Outage shown stale | Recovered ms | +|---|---|---|---|---|---|---|---|---|---|---| +| clean | 1705 | 0 | 2,514.0 | 0 | 0 | 0 % | 0.8 % | 2.00 | 89.9 % | 156 | +| lan | 1706 | 0 | 2,518.7 | 0 | 0 | 0 % | 0.8 % | 2.00 | 89.3 % | 160 | +| rtt60-loss2 | 1704 | 0 | 2,598.8 | 0 | 0 | 0 % | 0.8 % | 2.00 | 89.9 % | 154 | +| rtt120-loss2 | 1700 | 87.6 | 2,977.8 | 60.7 | 100.0 | 0 % | 2.8 % | 2.98 | 86.3 % | 510 | +| rtt200-loss5 | 1696 | 2,811.5 | 2,862.4 | 22.0 | 83.0 | 2.5 % | 5.3 % | 2.11 | 84.2 % | 950 | + +- Markers shown as current are within about 0.1 m of the true path at their render tick. + Linear motion between snapshots is exact; errors come from direction changes within a + gap. +- The large all-sample maxima come from the long-gap rule after the outage: the + pre-outage position is held until the first post-outage snapshot's tick. Those frames + are flagged stale and drawn grey, never presented as current. +- During the outage the marker is shown stale after the 150 ms grace (the clock freezes + one tick past the last observation; silence beyond 3 ticks is stale). The ~10 % of + outage samples that are not yet stale are that grace. +- Recovery (the first non-stale frame after the link returns) takes 150-950 ms. It grows + with RTT and loss: the next snapshot must arrive, and the delay must cover it. +- Variance: in one of three smoke runs (rtt60-loss2, 8 s), 32 % of samples were + extrapolated and 36 % stale, while current errors stayed at 0 mm. That is consistent + with snapshot datagrams being dropped under congestion while watermarks still arrived; + the adaptive delay rose only to 2.23 ticks on average. The other two runs matched the + table. + +Prediction rerun after the M10.7 changes (own marker presented between ticks; errors +recorded only for ticks inputs are scheduled for): +- clean: 100 % exact. +- lan: 99.8 %. +- rtt60-loss2: 99.5 %. +- rtt120-loss2: 97.1 %; resync 2,500 ms. +- rtt200-loss5: 94.2 %; max 1,293.3 mm. + +All profiles converged. diff --git a/include/evo/client/interpolation.hpp b/include/evo/client/interpolation.hpp index 7be4230..0b735c4 100644 --- a/include/evo/client/interpolation.hpp +++ b/include/evo/client/interpolation.hpp @@ -35,12 +35,15 @@ class TickClock { [[nodiscard]] std::uint64_t snaps() const noexcept { return snaps_; } + // Ticks of local time since the last observation (nothing before the first): a silent + // server freezes the clock, so silence itself is what marks views stale. + [[nodiscard]] std::optional silence(Clock::time_point now) const; private: [[nodiscard]] double raw(double base, Clock::time_point now) const; TickClockLimits limits_; std::optional base_tick_; - Clock::time_point base_time_{}; + Clock::time_point base_time_{}, seen_{}; std::uint64_t newest_{}; double returned_{}; std::uint64_t snaps_{}; @@ -51,9 +54,9 @@ class TickClock { // snapshot a marker continues its replicated held vector for at most `max_extrapolation` // ticks, then freezes and is marked stale. A marker appears at the tick of the snapshot that // added it and disappears at the tick of the one that removed it (never interpolated toward a -// removal). Snapshots further apart than `max_gap` are not interpolated across (the newer -// one is shown from its tick). The delay adapts to the observed gaps between received -// snapshots (lost or late ones), within [min_delay, max_delay]. +// removal). Snapshots further apart than `max_gap` are not interpolated across: the older is +// held, marked stale, until the newer one's tick. The delay adapts to the observed gaps between +// received snapshots (lost or late ones), within [min_delay, max_delay]. struct InterpolationLimits { double min_delay{2}, max_delay{6}; // Ticks (ADR-0095). double max_extrapolation{2}; // Ticks beyond the newest snapshot. diff --git a/include/evo/client/motion_prediction.hpp b/include/evo/client/motion_prediction.hpp index 4ae0573..8be669a 100644 --- a/include/evo/client/motion_prediction.hpp +++ b/include/evo/client/motion_prediction.hpp @@ -55,11 +55,14 @@ class MotionPredictor { // nothing before the first authoritative state. [[nodiscard]] std::optional predict(std::uint64_t tick); // M10.7 (ADR-0095): the position `fraction` (0..1) of the way from tick to tick + 1, for - // presentation between ticks; records the prediction for `tick` like predict(). + // presentation between ticks. With `record`, the prediction for `tick` is kept to measure + // errors like predict(); a caller presenting beyond the tick it schedules inputs for passes + // false (inputs for that tick may still come). struct Point { double x{}, y{}; // Micrometres. }; - [[nodiscard]] std::optional predict_between(std::uint64_t tick, double fraction); + [[nodiscard]] std::optional predict_between(std::uint64_t tick, double fraction, + bool record = true); // The jump rebasing made to the newest prediction (old minus new), since the last call. struct Offset { std::int64_t x{}, y{}; @@ -82,6 +85,7 @@ class MotionPredictor { } private: + [[nodiscard]] std::optional predict(std::uint64_t tick, bool record); [[nodiscard]] std::optional replay(std::uint64_t tick); // Re-predicts the newest presented tick after a change and accumulates the jump. void rebase(const std::optional& before); diff --git a/src/client/prediction/interpolation.cpp b/src/client/prediction/interpolation.cpp index 7fb5b16..5921a97 100644 --- a/src/client/prediction/interpolation.cpp +++ b/src/client/prediction/interpolation.cpp @@ -26,6 +26,7 @@ double TickClock::raw(double base, Clock::time_point now) const { void TickClock::observe(std::uint64_t tick, Clock::time_point now) { newest_ = std::max(newest_, tick); + seen_ = now; const auto sample = static_cast(tick); if (!base_tick_) { base_tick_ = sample; @@ -43,6 +44,12 @@ void TickClock::observe(std::uint64_t tick, Clock::time_point now) { base_time_ = now; } +std::optional TickClock::silence(Clock::time_point now) const { + if (!base_tick_) + return std::nullopt; + return ms(now - seen_) / limits_.tick_ms; +} + std::optional TickClock::at(Clock::time_point now) { if (!base_tick_) return std::nullopt; @@ -123,8 +130,10 @@ std::vector MarkerInterpolator::at(double render_tick) { snapshots_, [&](const Snapshot& s) { return static_cast(s.tick) > render_tick; }); const auto& a = *std::prev(b); if (b->tick - a.tick > limits_.max_gap) { - ++stats_.gap_snaps; // Too far apart to interpolate: hold the older until the newer. + ++stats_.gap_snaps; // Too far apart to interpolate: hold the older (stale) until b. show(a); + for (auto& m : out) + m.stale = true; return out; } const auto t = diff --git a/src/client/prediction/motion_prediction.cpp b/src/client/prediction/motion_prediction.cpp index 398a5dd..1cea5c3 100644 --- a/src/client/prediction/motion_prediction.cpp +++ b/src/client/prediction/motion_prediction.cpp @@ -66,6 +66,10 @@ void MotionPredictor::authoritative(const AuthoritativeMarker& a) { } std::optional MotionPredictor::predict(std::uint64_t tick) { + return predict(tick, true); +} + +std::optional MotionPredictor::predict(std::uint64_t tick, bool record) { if (!authority_) return std::nullopt; const auto authority_tick = authority_->tick; @@ -73,7 +77,7 @@ std::optional MotionPredictor::predict(std::uint64_t tic if (!state) return std::nullopt; presented_tick_ = tick; - if (tick > authority_tick) { + if (record && tick > authority_tick) { history_[tick] = *state; while (history_.size() > limits_.max_history) history_.erase(history_.begin()); @@ -81,9 +85,9 @@ std::optional MotionPredictor::predict(std::uint64_t tic return state; } -std::optional MotionPredictor::predict_between(std::uint64_t tick, - double fraction) { - const auto from = predict(tick); +std::optional +MotionPredictor::predict_between(std::uint64_t tick, double fraction, bool record) { + const auto from = predict(tick, record); if (!from) return std::nullopt; const auto to = replay(tick + 1); diff --git a/tests/client_remote_session.cpp b/tests/client_remote_session.cpp index 544cf7e..7cc2947 100644 --- a/tests/client_remote_session.cpp +++ b/tests/client_remote_session.cpp @@ -243,7 +243,13 @@ void views_interest_and_intentions(Region& r) { CHECK(s.environment().freshness() == client::Freshness::stale); CHECK(s.outcomes().entries().size() == 2 && s.outcomes().entries()[1].sequence == 2 && s.outcomes().entries()[1].state == OutcomeState::unknown); + // The lost session's view stays frozen and stale (ADR-0095): the own marker at its last + // authoritative position, nothing moving while reconnecting. + const auto frozen = s.status(); + CHECK(frozen.stale_view && frozen.marker && frozen.stale_markers == s.markers().size() && + std::ranges::all_of(s.markers(), [](const auto& m) { return m.stale; })); CHECK(c.until(r, [&] { return s.status().phase == "connected"; }, 20)); + CHECK(!s.status().stale_view); CHECK(s.status().epoch == 4 && s.status().session != old_session && s.status().grants_left == 0 && c.phase() == LaunchPhase::connected); // Views restart for the new epoch, at the camera's interest (sent again). @@ -263,6 +269,45 @@ void views_interest_and_intentions(Region& r) { CHECK(c.host.launcher().error() == LaunchError::grant_refused && !c.host.session()); } +// Two sessions: each sees the other's marker interpolated a few ticks behind its +// authoritative path; a session that leaves takes its marker with it (ADR-0095). +void remote_markers(Region& r) { + Client a, b; + CHECK(a.host.connect(RemoteSession::open(r.material({r.grant(account(7), 1)})))); + CHECK(b.host.connect(RemoteSession::open(r.material({r.grant(account(8), 1)})))); + const auto both = [&] { + a.host.step(Clock::now(), a.intention, a.ui); + return a.phase() == LaunchPhase::connected && b.phase() == LaunchPhase::connected && + a.host.session()->status().marker && b.host.session()->status().other_markers == 1; + }; + CHECK(b.until(r, both, 20)); + auto& sa = *a.host.session(); + auto& sb = *b.host.session(); + a.intention.move_y = 1000; // A walks +y at 100 mm per tick. + const auto drive = [&](auto done) { + return b.until( + r, + [&] { + a.host.step(Clock::now(), a.intention, a.ui); + return done(); + }, + 20); + }; + const auto remote = [&] { + const auto it = std::ranges::find_if(sb.markers(), [](const auto& m) { return !m.own; }); + CHECK(it != sb.markers().end()); + return *it; + }; + CHECK(drive([&] { return remote().y_mm > 4500; })); + // B shows A in the recent past: behind A's own (predicted) view, never ahead of it. + const auto seen = remote(); + CHECK(!seen.stale && seen.x_mm == 3500 && seen.y_mm < sa.status().marker_y_mm && + sb.status().interpolation_delay >= 2 && sb.status().interpolation_delay <= 6); + // A leaves: its marker disappears from B's view. + a.host.cancel(); + CHECK(drive([&] { return sb.status().other_markers == 0; })); +} + void refusals(Region& r) { // A grant for another region: refused before any Welcome; the launcher stops. Client wrong; @@ -289,6 +334,7 @@ int main() try { { Region r{files}; views_interest_and_intentions(r); + remote_markers(r); refusals(r); } std::filesystem::remove_all(files.dir); diff --git a/tests/network_interpolation.cpp b/tests/network_interpolation.cpp index 45f4428..af85a71 100644 --- a/tests/network_interpolation.cpp +++ b/tests/network_interpolation.cpp @@ -38,8 +38,10 @@ void clock_basics() { // A resume far ahead (pause, reconnect) snaps forward. clock.observe(200, t0 + milliseconds{5000}); CHECK(clock.snaps() == 1 && near(must(clock.at(t0 + milliseconds{5000})), 200)); + // Silence is measured in local ticks since the last observation. + CHECK(near(must(clock.silence(t0 + milliseconds{5100})), 2)); clock.reset(); - CHECK(!clock.at(t0)); + CHECK(!clock.at(t0) && !clock.silence(t0)); clock.observe(3, t0); CHECK(near(must(clock.at(t0)), 3)); // After a reset (new epoch) it may start lower. } @@ -108,7 +110,8 @@ void interpolation() { const std::vector later{marker(1, 9'000'000)}; m.push(20, later); at = m.at(12); - CHECK(at.size() == 2 && near(at[0].x, 400'000) && m.stats().gap_snaps == 1); + CHECK(at.size() == 2 && near(at[0].x, 400'000) && at[0].stale && at[1].stale && + m.stats().gap_snaps == 1); CHECK(near(m.at(20)[0].x, 9'000'000)); m.reset(); CHECK(m.at(20).empty() && near(m.delay(), 2)); diff --git a/tools/network/motion_trace.cpp b/tools/network/motion_trace.cpp index 84fc972..43c069a 100644 --- a/tools/network/motion_trace.cpp +++ b/tools/network/motion_trace.cpp @@ -1,17 +1,23 @@ -// M10.6c probe-marker prediction traces (ADR-0094). Development measurement, not a test of -// game behaviour: the engineering client's own session code (apps/evo-client -// remote_session.hpp) against a loopback region that simulates at 50 ms in real time, through -// the owned UDP impairment relay. Each profile scripts WASD-like movement, then stops and -// requires the prediction to converge on the authority. +// M10.6c/M10.7b probe-marker traces (ADR-0094, ADR-0095). Development measurement, not a +// test of game behaviour: the engineering client's own session code (apps/evo-client +// remote_session.hpp) against a loopback region that simulates at 50 ms in real time, +// through the owned UDP impairment relay (one relay per client). // -// evo-motion-trace [--seconds S] [--profile NAME] [--smoke] +// evo-motion-trace [--seconds S] [--profile NAME] [--remote] [--smoke] // -// One line per profile: +// Prediction (default): one client scripts WASD-like movement, then stops and requires its +// prediction to converge on the authority. One line per profile: // motion PROFILE rtt=MS jitter=MS loss=% samples=N exact=% p50/p95/p99/max=MM corrections=N // snaps=N refused=N dropped=N resends=N lead=TICKS max_pending=N converged=0|1 // and for the restart profile: restart resync_ms=MS converged=0|1. -// --smoke runs one short impaired profile plus the restart and exits 1 unless both converge, -// the input list stayed bounded and most predictions were exact (the CTest). +// Remote (--remote): client A moves, client B shows A's marker interpolated; every 10 ms +// B's presented position is compared with A's authoritative path at B's render tick. Midway, +// B's link is cut for 1.5 s (outage). One line per profile: +// remote PROFILE rtt=MS jitter=MS loss=% samples=N p50/p95/p99/max=MM +// current_p99/max=MM (markers not shown stale) extrapolated=% stale=% delay=TICKS +// outage_stale=% recovered_ms=MS +// --smoke runs one short impaired profile (prediction: plus a restart) and exits 1 unless +// its bounds hold (the CTests). #include "evo/client/session_host.hpp" #include "evo/network/dev_credentials.hpp" #include "evo/network/session_grant.hpp" @@ -27,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -38,6 +45,7 @@ using namespace evo; using namespace evo::network; using Clock = std::chrono::steady_clock; using std::chrono::milliseconds; +using Host = client::SessionHost; constexpr std::string_view server_name = "dev-region.evolution.test"; constexpr world::WorldIdentity world_id{9, 2}; constexpr std::uint64_t region = 21; @@ -48,17 +56,12 @@ struct Profile { milliseconds one_way{}, jitter{}; double loss{}; }; -struct Result { - std::vector errors; - client_app::RemoteStatus status; - bool converged{}; - double resync_ms{}; -}; std::uint64_t now_seconds() { return static_cast(std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()) .count()); } +// Nearest-rank percentile of micrometre samples, in millimetres. double percentile_mm(std::vector v, double p) { if (v.empty()) return 0; @@ -66,6 +69,9 @@ double percentile_mm(std::vector v, double p) { const auto rank = static_cast(p / 100 * static_cast(v.size() - 1)); return static_cast(v.at(rank)) / 1000; } +double share(std::uint64_t part, std::uint64_t whole) { + return whole == 0 ? 0.0 : 100.0 * static_cast(part) / static_cast(whole); +} struct Files { std::filesystem::path dir, ca, certificate, key; @@ -99,194 +105,357 @@ std::array scalar() { s.back() = 0x2C; return s; } +impair::Impairment impairment(const Profile& p) { + impair::Impairment way; + way.delay = p.one_way; + way.jitter = p.jitter; + way.loss = p.loss; + return way; +} -// One profile: a fresh region, relay and client session. -class Trace { +// WASD-like movement: a new direction every 0.4-1.1 s (seeded), held in between. +class Script { public: - Trace(const Files& f, const Profile& p) : files_{f}, profile_{p} {} - std::optional run(std::chrono::seconds moving, bool restart) { + void step(client::Intention& intention) { + if (Clock::now() < change_) + return; + static constexpr std::array, 9> directions{ + {{1000, 0}, + {-1000, 0}, + {0, 1000}, + {0, -1000}, + {707, 707}, + {-707, 707}, + {707, -707}, + {-707, -707}, + {0, 0}}}; + const auto d = directions.at(next() % directions.size()); + intention.move_x = d.first; + intention.move_y = d.second; + change_ = Clock::now() + milliseconds{400 + static_cast(next() % 700)}; + } + + private: + std::uint64_t next() { + seed_ = ((seed_ ^ (seed_ >> 31)) * 0xBF58'476D'1CE4'E5B9ULL) + 1; + return seed_; + } + std::uint64_t seed_{0x9E37'79B9'7F4A'7C15ULL}; + Clock::time_point change_{}; +}; + +// A loopback region on real 50 ms ticks with probe markers, and clients behind relays. +class Bench { + public: + explicit Bench(const Files& f) : files_{f} {} + bool start() { auto key = GrantSigningKey::import(DeploymentEnvironment::dev, 1, scalar()); if (!key) - return std::nullopt; + return false; + key_.emplace(std::move(*key)); server::RegionSessionsConfig c; c.transport.certificate_pem_file = files_.certificate.string(); c.transport.key_pem_file = files_.key.string(); c.control.environment = DeploymentEnvironment::dev; c.control.world = world_id; c.control.region = region; - c.control.epoch = epoch_; - c.issuer_keys.push_back({1, *key->public_key()}); + c.control.epoch = epoch; + c.issuer_keys.push_back({1, *key_->public_key()}); auto sessions = server::RegionSessions::create(std::move(c)); if (!sessions) - return std::nullopt; + return false; sessions_ = std::move(*sessions); sessions_->set_default_interest({0, 0, 0, 7}); - if (!sessions_->set_motion_area({-20'000'000, -20'000'000, 20'999'999, 20'999'999, 0}, - 500'000, 500'000)) + return sessions_->set_motion_area({-20'000'000, -20'000'000, 20'999'999, 20'999'999, 0}, + 500'000, 500'000); + } + // A client session behind its own relay, with `grants` single-use grants. + std::optional, std::unique_ptr>> + client(std::uint8_t account, const Profile& p, std::uint64_t grants) { + auto relay = impair::UdpRelay::start(sessions_->port(), impairment(p), impairment(p), + 0xE6 + account); + if (!relay) return std::nullopt; - impair::Impairment way; - way.delay = profile_.one_way; - way.jitter = profile_.jitter; - way.loss = profile_.loss; - relay_ = impair::UdpRelay::start(sessions_->port(), way, way, 0xE6); - if (!relay_) - return std::nullopt; - std::vector grants; - for (std::uint64_t serial = 1; serial <= 4; ++serial) { + std::vector bundle; + for (std::uint64_t serial = 1; serial <= grants; ++serial) { GrantClaims g; - g.grant_id = 700 + serial; - g.account.bytes.fill(0x5A); + g.grant_id = (std::uint64_t{account} << 16) + serial; + g.account.bytes.fill(account); g.serial = serial; g.world = world_id; g.region = region; g.issued_at = now_seconds(); g.expires_at = g.issued_at + 300; - const auto signed_grant = key->sign(g); + if (!key_) + return std::nullopt; + const auto signed_grant = key_->sign(g); if (!signed_grant) return std::nullopt; - grants.push_back(*signed_grant); + bundle.push_back(*signed_grant); } - client::SessionHost host; - if (!host.connect(client_app::RemoteSession::open({{"127.0.0.1", relay_->port()}, - std::string{server_name}, - files_.ca_pem, - files_.ca.string(), - std::move(grants)}))) + auto host = std::make_unique(); + if (!host->connect(client_app::RemoteSession::open({{"127.0.0.1", relay->port()}, + std::string{server_name}, + files_.ca_pem, + files_.ca.string(), + std::move(bundle)}))) return std::nullopt; - client::UiState ui; - client::Intention intention; - auto* session = host.session(); - const auto connected = [&] { return session && session->status().phase == "connected"; }; - if (!pump(host, ui, intention, connected, std::chrono::seconds{10})) - return std::nullopt; - Result r; - // Movement: a new direction every 0.4-1.1 s (seeded), held in between. - std::uint64_t seed = 0x9E37'79B9'7F4A'7C15ULL; - const auto next = [&] { - seed = (seed ^ (seed >> 31)) * 0xBF58'476D'1CE4'E5B9ULL + 1; - return seed; - }; - const std::array, 9> directions{{{1000, 0}, - {-1000, 0}, - {0, 1000}, - {0, -1000}, - {707, 707}, - {-707, 707}, - {707, -707}, - {-707, -707}, - {0, 0}}}; - const auto end = Clock::now() + moving; - auto change = Clock::now(); - while (Clock::now() < end) { - if (Clock::now() >= change) { - const auto d = directions.at(next() % directions.size()); - intention.move_x = d.first; - intention.move_y = d.second; - change = Clock::now() + milliseconds{400 + static_cast(next() % 700)}; - } - if (!pump( - host, ui, intention, [] { return false; }, milliseconds{10}) && - !host.session()) - return std::nullopt; - collect(r, *host.session()); - } - // Stop and require convergence: no input in flight, prediction equal to the authority. - intention.move_x = intention.move_y = 0; - r.converged = pump( - host, ui, intention, [&] { return settled(*host.session()); }, std::chrono::seconds{5}); - collect(r, *host.session()); - if (restart) { - // A restart: the epoch advances, the session reconnects with its next grant and the - // marker is respawned; resync is the time until the prediction matches again. - const auto before = host.session()->status().session; - ++epoch_; - const auto start = Clock::now(); - const bool back = pump( - host, ui, intention, - [&] { - const auto s = host.session()->status(); - return s.phase == "connected" && s.session != before && s.marker && - settled(*host.session()); - }, - std::chrono::seconds{15}); - r.resync_ms = std::chrono::duration(Clock::now() - start).count(); - r.converged = r.converged && back; - collect(r, *host.session()); - } - r.status = host.session()->status(); - return r; + return std::pair{std::move(relay), std::move(host)}; } - - private: - // Region ticks every 50 ms of real time; the client steps every millisecond. - bool pump(client::SessionHost& host, client::UiState& ui, - const client::Intention& intention, const std::function& done, - std::chrono::milliseconds limit) { + // One server iteration: a new tick every 50 ms of real time, then poll and replicate. + void serve() { + const auto now = Clock::now(); + if (now >= next_tick_) { + ++tick; + next_tick_ = std::max(next_tick_ + tick_period, now - tick_period); + } + sessions_->poll({true, epoch, tick}, now_seconds(), now); + if (tick != replicated_) { + replicated_ = tick; + sessions_->replicate(tick, [](const SessionInterest&) { + ReplicationSource s; + s.identity = world_id; + return s; + }); + } + static_cast(sessions_->take_events()); + } + // Steps the region and the clients (each with its intention) until `done` or `limit`. + bool pump(const std::vector>& clients, + const std::function& done, std::chrono::milliseconds limit) { + client::UiState ui; const auto end = Clock::now() + limit; while (Clock::now() < end) { - const auto now = Clock::now(); - if (now >= next_tick_) { - ++tick_; - next_tick_ = std::max(next_tick_ + tick_period, now - tick_period); + serve(); + for (const auto& [host, intention] : clients) { + host->step(Clock::now(), *intention, ui); + if (!host->session()) + return false; } - sessions_->poll({true, epoch_, tick_}, now_seconds(), now); - if (tick_ != replicated_) { - replicated_ = tick_; - sessions_->replicate(tick_, [](const SessionInterest&) { - ReplicationSource s; - s.identity = world_id; - return s; - }); - } - static_cast(sessions_->take_events()); - host.step(Clock::now(), intention, ui); - if (!host.session()) - return false; if (done()) return true; std::this_thread::sleep_for(milliseconds{1}); } return done(); } - // Nothing in flight and the presented (smoothed) marker on the authoritative position. - bool settled(client_app::RemoteSession& s) { - const auto status = s.status(); - const auto* m = sessions_->markers().of(status.session); - return status.marker && m != nullptr && status.inputs_pending == 0 && - m->state.vector == motion::Vector{} && - std::llround(status.marker_x_mm * 1000) == m->state.x && - std::llround(status.marker_y_mm * 1000) == m->state.y; - } - static void collect(Result& r, client_app::RemoteSession& s) { - for (const auto e : s.take_prediction_errors()) - r.errors.push_back(e); + [[nodiscard]] const motion::Marker* marker(std::uint64_t session) const { + return sessions_->markers().of(session); } + std::uint64_t epoch{1}, tick{1}; + + private: const Files& files_; - Profile profile_; + std::optional key_; std::unique_ptr sessions_; - std::unique_ptr relay_; - std::uint64_t epoch_{1}, tick_{1}, replicated_{}; + std::uint64_t replicated_{}; Clock::time_point next_tick_{Clock::now()}; }; -void report(const Profile& p, const Result& r) { - const auto exact = std::ranges::count(r.errors, std::uint64_t{0}); - const auto share = r.errors.empty() ? 0.0 - : 100.0 * static_cast(exact) / - static_cast(r.errors.size()); +bool connected(Host& h) { + return h.session() && h.session()->status().phase == "connected"; +} +// Nothing in flight and the presented (smoothed) own marker on the authoritative position. +bool settled(const Bench& bench, client_app::RemoteSession& s) { + const auto status = s.status(); + const auto* m = bench.marker(status.session); + return status.marker && m != nullptr && status.inputs_pending == 0 && + m->state.vector == motion::Vector{} && + std::llround(status.marker_x_mm * 1000) == m->state.x && + std::llround(status.marker_y_mm * 1000) == m->state.y; +} + +struct PredictionResult { + std::vector errors; + client_app::RemoteStatus status; + bool converged{}; + double resync_ms{}; +}; +std::optional prediction(const Files& files, const Profile& p, + std::chrono::seconds moving, bool restart) { + Bench bench{files}; + if (!bench.start()) + return std::nullopt; + auto made = bench.client(0x5A, p, 4); + if (!made) + return std::nullopt; + auto& host = *made->second; + client::Intention intention; + const std::vector> one{{&host, &intention}}; + if (!bench.pump(one, [&] { return connected(host); }, std::chrono::seconds{10})) + return std::nullopt; + PredictionResult r; + const auto collect = [&] { + for (const auto e : host.session()->take_prediction_errors()) + r.errors.push_back(e); + }; + Script script; + const auto end = Clock::now() + moving; + while (Clock::now() < end) { + script.step(intention); + if (!bench.pump(one, [] { return false; }, milliseconds{10}) && !host.session()) + return std::nullopt; + collect(); + } + // Stop and require convergence on the authority. + intention = {}; + r.converged = + bench.pump(one, [&] { return settled(bench, *host.session()); }, std::chrono::seconds{5}); + collect(); + if (restart) { + // The epoch advances: the session reconnects with its next grant and the marker is + // respawned; resync is the time until the prediction matches again. + const auto before = host.session()->status().session; + ++bench.epoch; + const auto start = Clock::now(); + const bool back = bench.pump( + one, + [&] { + const auto s = host.session()->status(); + return s.phase == "connected" && s.session != before && s.marker && + settled(bench, *host.session()); + }, + std::chrono::seconds{15}); + r.resync_ms = std::chrono::duration(Clock::now() - start).count(); + r.converged = r.converged && back; + collect(); + } + r.status = host.session()->status(); + return r; +} + +struct RemoteResult { + std::vector errors; // Outside the outage. + std::vector current; // The same, for markers not shown as stale. + std::uint64_t samples{}, extrapolated{}, stale{}, outage_samples{}, outage_stale{}; + double delay_sum{}, recovered_ms{-1}; +}; +std::optional remote(const Files& files, const Profile& p, + std::chrono::seconds moving) { + Bench bench{files}; + if (!bench.start()) + return std::nullopt; + auto a = bench.client(0x61, p, 2); + auto b = bench.client(0x62, p, 2); + if (!a || !b) + return std::nullopt; + auto& ha = *a->second; + auto& hb = *b->second; + client::Intention ia, ib; + const std::vector> both{{&ha, &ia}, {&hb, &ib}}; + if (!bench.pump( + both, + [&] { + return connected(ha) && connected(hb) && hb.session()->status().other_markers == 1; + }, + std::chrono::seconds{10})) + return std::nullopt; + const auto session_a = ha.session()->status().session; + // A's authoritative path by tick (state after the tick). + std::map> path; + const auto record = [&] { + if (const auto* m = bench.marker(session_a)) + path[bench.tick] = {static_cast(m->state.x), static_cast(m->state.y)}; + }; + const auto truth = [&](double t) -> std::optional> { + const auto lo = static_cast(std::floor(t)); + const auto from = path.find(lo), to = path.find(lo + 1); + if (from == path.end() || to == path.end()) + return std::nullopt; + const auto f = t - static_cast(lo); + return std::pair{from->second.first + ((to->second.first - from->second.first) * f), + from->second.second + ((to->second.second - from->second.second) * f)}; + }; + RemoteResult r; + Script script; + const auto start = Clock::now(); + const auto outage_from = start + (moving / 2), outage_to = outage_from + milliseconds{1500}; + bool cut = false; + const auto end = start + moving; + while (Clock::now() < end) { + const auto now = Clock::now(); + if (!cut && now >= outage_from && now < outage_to) { + impair::Impairment dead = impairment(p); + dead.loss = 1.0; + b->first->set(dead, dead); + cut = true; + } else if (cut && now >= outage_to) { + b->first->set(impairment(p), impairment(p)); + cut = false; + } + script.step(ia); + if (!bench.pump( + both, + [&] { + record(); + return false; + }, + milliseconds{10}) && + (!ha.session() || !hb.session())) + return std::nullopt; + const auto s = hb.session()->status(); + const auto& markers = hb.session()->markers(); + const auto it = std::ranges::find_if(markers, [](const auto& m) { return !m.own; }); + if (it == markers.end()) + continue; + const bool in_outage = Clock::now() >= outage_from && Clock::now() < outage_to; + if (in_outage) { + ++r.outage_samples; + r.outage_stale += it->stale ? 1 : 0; + continue; + } + if (Clock::now() >= outage_to && r.recovered_ms < 0 && !it->stale) + r.recovered_ms = + std::chrono::duration(Clock::now() - outage_to).count(); + const auto t = truth(s.remote_render_tick); + if (!t) + continue; + ++r.samples; + r.extrapolated += it->extrapolated ? 1 : 0; + r.stale += it->stale ? 1 : 0; + r.delay_sum += s.interpolation_delay; + const auto dx = (it->x_mm * 1000) - t->first, dy = (it->y_mm * 1000) - t->second; + const auto error = static_cast(std::llround(std::hypot(dx, dy))); + r.errors.push_back(error); + if (!it->stale) + r.current.push_back(error); + } + return r; +} + +void report(const Profile& p, const PredictionResult& r) { + const auto exact = static_cast(std::ranges::count(r.errors, std::uint64_t{0})); const auto& s = r.status; - std::printf( - "motion %s rtt=%lld jitter=%lld loss=%.1f samples=%zu exact=%.1f " - "p50/p95/p99/max=%.1f/%.1f/%.1f/%.1f corrections=%llu snaps=%llu refused=%llu " - "dropped=%llu resends=%llu lead=%llu max_pending=%llu converged=%d\n", - p.name.c_str(), 2 * static_cast(p.one_way.count()), - static_cast(p.jitter.count()), p.loss * 100, r.errors.size(), share, - percentile_mm(r.errors, 50), percentile_mm(r.errors, 95), percentile_mm(r.errors, 99), - percentile_mm(r.errors, 100), static_cast(s.corrections), - static_cast(s.snaps), static_cast(s.refused_inputs), - static_cast(s.dropped_inputs), - static_cast(s.resends), static_cast(s.lead), - static_cast(s.max_pending), r.converged ? 1 : 0); + std::printf("motion %s rtt=%lld jitter=%lld loss=%.1f samples=%zu exact=%.1f " + "p50/p95/p99/max=%.1f/%.1f/%.1f/%.1f corrections=%llu snaps=%llu refused=%llu " + "dropped=%llu resends=%llu lead=%llu max_pending=%llu converged=%d\n", + p.name.c_str(), 2 * static_cast(p.one_way.count()), + static_cast(p.jitter.count()), p.loss * 100, r.errors.size(), + share(exact, r.errors.size()), percentile_mm(r.errors, 50), + percentile_mm(r.errors, 95), percentile_mm(r.errors, 99), + percentile_mm(r.errors, 100), static_cast(s.corrections), + static_cast(s.snaps), + static_cast(s.refused_inputs), + static_cast(s.dropped_inputs), + static_cast(s.resends), static_cast(s.lead), + static_cast(s.max_pending), r.converged ? 1 : 0); + if (r.resync_ms > 0) + std::printf("restart %s resync_ms=%.0f converged=%d\n", p.name.c_str(), r.resync_ms, + r.converged ? 1 : 0); + std::fflush(stdout); +} +void report(const Profile& p, const RemoteResult& r) { + std::printf("remote %s rtt=%lld jitter=%lld loss=%.1f samples=%llu " + "p50/p95/p99/max=%.1f/%.1f/%.1f/%.1f current_p99/max=%.1f/%.1f extrapolated=%.1f " + "stale=%.1f delay=%.2f outage_stale=%.1f recovered_ms=%.0f\n", + p.name.c_str(), 2 * static_cast(p.one_way.count()), + static_cast(p.jitter.count()), p.loss * 100, + static_cast(r.samples), percentile_mm(r.errors, 50), + percentile_mm(r.errors, 95), percentile_mm(r.errors, 99), + percentile_mm(r.errors, 100), percentile_mm(r.current, 99), + percentile_mm(r.current, 100), share(r.extrapolated, r.samples), + share(r.stale, r.samples), + r.samples == 0 ? 0.0 : r.delay_sum / static_cast(r.samples), + share(r.outage_stale, r.outage_samples), r.recovered_ms); std::fflush(stdout); } } // namespace @@ -294,17 +463,20 @@ void report(const Profile& p, const Result& r) { int main(int argc, char** argv) { std::chrono::seconds seconds{20}; std::string only; - bool smoke = false; + bool smoke = false, remote_mode = false; for (int i = 1; i < argc; ++i) { const std::string_view a = argv[i]; if (a == "--smoke") smoke = true; + else if (a == "--remote") + remote_mode = true; else if (a == "--seconds" && i + 1 < argc) seconds = std::chrono::seconds{std::max(1, std::atoi(argv[++i]))}; else if (a == "--profile" && i + 1 < argc) only = argv[++i]; else { - std::fputs("usage: evo-motion-trace [--seconds S] [--profile NAME] [--smoke]\n", + std::fputs("usage: evo-motion-trace [--seconds S] [--profile NAME] [--remote] " + "[--smoke]\n", stderr); return 2; } @@ -319,23 +491,34 @@ int main(int argc, char** argv) { {"rtt200-loss5", milliseconds{100}, milliseconds{20}, 0.05}}; if (smoke) { profiles = {{"rtt60-loss2", milliseconds{30}, milliseconds{5}, 0.02}}; - seconds = std::chrono::seconds{6}; + seconds = std::chrono::seconds{remote_mode ? 8 : 6}; } int code = 0; for (const auto& p : profiles) { if (!only.empty() && p.name != only) continue; - Trace t{*files, p}; - const auto r = t.run(seconds, smoke || p.name == "rtt120-loss2"); + if (remote_mode) { + const auto r = remote(*files, p, seconds); + if (!r) { + std::printf("remote %s setup-failed\n", p.name.c_str()); + code = 1; + continue; + } + report(p, *r); + // Bounds: errors within a metre at the 95th percentile, the outage shown stale, + // recovery after it. + if (smoke && (r->samples == 0 || percentile_mm(r->current, 95) > 1000 || + share(r->outage_stale, r->outage_samples) < 50 || r->recovered_ms < 0)) + code = 1; + continue; + } + const auto r = prediction(*files, p, seconds, smoke || p.name == "rtt120-loss2"); if (!r) { std::printf("motion %s setup-failed\n", p.name.c_str()); code = 1; continue; } report(p, *r); - if (r->resync_ms > 0) - std::printf("restart %s resync_ms=%.0f converged=%d\n", p.name.c_str(), r->resync_ms, - r->converged ? 1 : 0); const auto exact = std::ranges::count(r->errors, std::uint64_t{0}); if (smoke && (!r->converged || r->status.max_pending > 32 || r->errors.empty() || exact * 2 < static_cast(r->errors.size())))