diff --git a/CMakePresets.json b/CMakePresets.json index 124dbfd..6296102 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -25,8 +25,8 @@ {"name": "client-probe", "configurePreset": "client-probe", "targets": ["evo-client-probe"]}, {"name": "network", "configurePreset": "network", "targets": ["evo-client-state-tests", "evo-network-tests", "evo-dev-session"]}, {"name": "quic", "configurePreset": "quic", "targets": ["evo-quic-smoke-tests", "evo-quic-transport-tests", "evo-quic-session-tests", "evo-quic-hol-measure", "evo-session-probe", "evo-network-tests"]}, - {"name": "client-network", "configurePreset": "client-network", "targets": ["evo-client-state-tests", "evo-client", "evo-client-lifecycle-tests", "evo-client-environment-tests", "evo-client-organism-tests", "evo-client-ui-model-tests", "evo-client-panel-tests", "evo-client-bindings-tests", "evo-network-tests", "evo-dev-session", "evo-quic-session-tests", "evo-client-remote-tests", "evo-session-probe"]}, - {"name": "client-package", "configurePreset": "client-package", "targets": ["evo-client-state-tests", "evo-client", "evo-client-lifecycle-tests", "evo-client-environment-tests", "evo-client-organism-tests", "evo-client-ui-model-tests", "evo-client-panel-tests", "evo-client-bindings-tests", "evo-network-tests", "evo-dev-session", "evo-quic-session-tests", "evo-client-remote-tests"]} + {"name": "client-network", "configurePreset": "client-network", "targets": ["evo-client-state-tests", "evo-client", "evo-client-lifecycle-tests", "evo-client-environment-tests", "evo-client-organism-tests", "evo-client-ui-model-tests", "evo-client-panel-tests", "evo-client-bindings-tests", "evo-network-tests", "evo-dev-session", "evo-quic-session-tests", "evo-client-remote-tests", "evo-motion-trace", "evo-session-probe"]}, + {"name": "client-package", "configurePreset": "client-package", "targets": ["evo-client-state-tests", "evo-client", "evo-client-lifecycle-tests", "evo-client-environment-tests", "evo-client-organism-tests", "evo-client-ui-model-tests", "evo-client-panel-tests", "evo-client-bindings-tests", "evo-network-tests", "evo-dev-session", "evo-quic-session-tests", "evo-client-remote-tests", "evo-motion-trace"]} ], "testPresets": [ {"name": "debug", "configurePreset": "debug", "output": {"outputOnFailure": true}}, diff --git a/apps/evo-client/remote_session.hpp b/apps/evo-client/remote_session.hpp index 54dcce6..020511f 100644 --- a/apps/evo-client/remote_session.hpp +++ b/apps/evo-client/remote_session.hpp @@ -61,6 +61,9 @@ struct RemoteStatus { double marker_x_mm{}, marker_y_mm{}; std::size_t inputs_pending{}, other_markers{}; std::uint64_t lead{}, corrections{}, max_error_um{}, snaps{}; + // 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{}; }; // A probe marker to draw: the own one predicted and smoothed, others as replicated. struct MarkerView { @@ -146,8 +149,17 @@ class RemoteSession { s.corrections = predictor_.stats().corrections; s.max_error_um = predictor_.stats().max_error_um; s.snaps = smoother_.snaps(); + s.rejected_inputs = predictor_.stats().rejected_inputs; + s.dropped_inputs = predictor_.stats().dropped_inputs; + s.max_pending = predictor_.stats().max_pending; + s.resends = resends_; + s.refused_inputs = refused_; return s; } + // Prediction error samples since the last call (micrometres; for measurements). + [[nodiscard]] std::vector take_prediction_errors() { + return predictor_.take_errors(); + } // Markers to draw this frame (empty without a session or snapshot). [[nodiscard]] const std::vector& markers() const noexcept { return markers_; @@ -293,6 +305,7 @@ class RemoteSession { if (!sequence) return; static_cast(predictor_.input({*sequence, tick, want})); + resends_ += want == sent_ ? 1 : 0; sent_ = want; sent_tick_ = server_tick_; const auto entry = next_entry_++; @@ -316,6 +329,7 @@ class RemoteSession { lead_.admitted(it->tick, r.server_tick); } else { predictor_.rejected(r.sequence); // It never takes effect. + ++refused_; if (r.status == network::AdmitStatus::outside_window) lead_.too_late(); // Arrived after its tick: schedule further ahead. log_.record({it->entry, client::OutcomeState::rejected, @@ -468,6 +482,7 @@ class RemoteSession { client::CorrectionSmoother smoother_; client::InputLead lead_; std::optional last_present_; + std::uint64_t resends_{}, refused_{}; std::vector others_; std::vector markers_; bool established_{}, welcomed_{}, exhausted_{}; diff --git a/cmake/ClientProbe.cmake b/cmake/ClientProbe.cmake index 1ec2bb8..7accac2 100644 --- a/cmake/ClientProbe.cmake +++ b/cmake/ClientProbe.cmake @@ -91,6 +91,17 @@ if(EVO_BUILD_CLIENT) evo_msquic_runtime(evo-client-remote-tests) add_test(NAME client-remote-session COMMAND evo-client-remote-tests) set_tests_properties(client-remote-session PROPERTIES TIMEOUT 120 LABELS "client;quic-session") + # M10.6c prediction traces through the impairment relay (development measurement); the + # smoke run is a short impaired profile plus a restart with convergence bounds. + add_executable(evo-motion-trace tools/network/motion_trace.cpp) + target_include_directories(evo-motion-trace PRIVATE "${PROJECT_SOURCE_DIR}/apps/evo-client") + target_compile_definitions(evo-motion-trace PRIVATE EVO_CLIENT_NETWORK=1) + target_link_libraries(evo-motion-trace PRIVATE evo_client_ui evo_session_client + evo_session_server evo_client_prediction evo_network_impair) + evo_owned_target(evo-motion-trace) + 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") endif() endif() if(BUILD_TESTING) diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index f577405..df35bef 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -531,8 +531,7 @@ probe) with process evidence done; 10.5c engineering client switched to M10 sess 10.6 split (ADR-0094, EXPERIMENT probe marker, not an organism movement rule): 10.6a shared 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 follow. -10.6-10.9 planned. +traces done (networking/M10_PREDICTION_TRACES.md). 10.7-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 fc44e21..a4d309d 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -57,7 +57,10 @@ protocol v3 marker records (v2, deployed, is frozen and refused), and the client (bounded replay from the authoritative marker, corrections, smoothing, adaptive input lead). M10.6b wires it: evo-server gives every session a marker in the habitat area, the probe prints it, and the engineering client predicts and draws it (WASD), following it with -camera and interest. Next: 10.6c impaired traces and the correction report. +camera and interest. M10.6c traces it through the impairment relay: exact without loss, +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. Next: M10.7 +interpolation and session loss. ## Previous gate - 2026-10-02 diff --git a/docs/adr/0094-m10-probe-motion-experiment.md b/docs/adr/0094-m10-probe-motion-experiment.md index 337ea85..e337576 100644 --- a/docs/adr/0094-m10-probe-motion-experiment.md +++ b/docs/adr/0094-m10-probe-motion-experiment.md @@ -96,3 +96,22 @@ rules. Session protocol v2 was deployed to evolution-dev on 2026-10-06 and is fr intention moves the predicted marker and the authority confirms it with zero error. - The real-process test: the probe's marker moved +x with its intention applied, and evo-server reported the motion area. + +## M10.6c traces and close (2026-10-06) +- `evo-motion-trace` drives the client's own session code through the impairment relay + against a real-time 50 ms loopback region; the figures are in + [M10_PREDICTION_TRACES](../networking/M10_PREDICTION_TRACES.md). + - Prediction is exact without loss. + - With 2-5 % loss and 60-200 ms RTT, 94-99 % of samples are exact. Errors stay within the + motion of the lost or late inputs (at most about 1.4 m) and are corrected by the resend. + - All profiles converge on the authority when stopped. + - A restart resyncs in about 2-3 s (the reconnect pause). +- The CTest `motion-trace-smoke` (labels client, quic-session) runs 6 s of rtt60-loss2 plus + a restart. It fails unless both converge, the input list stays within its bound and most + predictions are exact. +- The owned UDP relay now lets a new sender take over once the current client has been + silent for 1 s, so a reconnecting client (new source port) can be traced. Concurrent + senders are still dropped. +- The client now counts refusal receipts (`refused`) separately from inputs the predictor + removed; the unused `PredictionStats::refused_inputs` field is gone. +- M10.6 is complete. Sub-tick presentation and remote-marker interpolation belong to 10.7. diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 16aa8f9..1b00827 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -4562,3 +4562,44 @@ clang-format 19 was applied to the changed files. Not done: - impaired traces (10.6c); - a GUI run against evolution-dev, which needs a v3 server. + +## 2026-10-06 — M10.6c prediction traces (ADR-0094) + +Implemented: +- `evo-motion-trace` (the client's own session code through the impairment relay against + a real-time 50 ms loopback region); +- the CTest `motion-trace-smoke`; +- the relay takeover by a new sender after the current client has been silent for 1 s; +- a refusal count in the client status, replacing the unused predictor field. + +Figures are in docs/networking/M10_PREDICTION_TRACES.md. Linux, one run of each profile: +- clean and lan: 100 % exact; +- rtt60-loss2: 97.9 %; +- rtt120-loss2: 99.3 %; restart resync 2,946 ms; +- rtt200-loss5: 94.1 %; p99 500 mm, max 989.9 mm. + +Every profile converged after stopping. Windows was similar, with a maximum of 1,399.8 mm +at rtt200-loss5. The smoke profile passed 3 times in a row on Linux. + +Found and fixed during development: +- the relay ignored a reconnecting client (new source port), so restart resync timed out; +- refusals were undercounted when an input had already been dropped; +- a clang-tidy widening-cast finding. + +Executed: +- **Linux GCC 14** + - preset `client-network`: CTest 15/15, including motion-trace-smoke and + client-remote-session; + - preset `quic`: 4/4, including quic-transport with the changed relay. +- **Linux Clang 19:** clang-tidy 19 on the trace tool, client app, remote test, predictor + and relay. +- **Windows MSVC 19.44** (fresh binaries): + - preset `quic`: 4/4; + - preset `client-network`: 13/13, including motion-trace-smoke; + - a full trace run. + +The Postgres suite was not rerun: no server code changed since 10.6b. + +Not done: +- traces over the dev route (a v3 server must be deployed first); +- many-client load (10.8). diff --git a/docs/networking/M10_PREDICTION_TRACES.md b/docs/networking/M10_PREDICTION_TRACES.md new file mode 100644 index 0000000..9825e81 --- /dev/null +++ b/docs/networking/M10_PREDICTION_TRACES.md @@ -0,0 +1,64 @@ +# M10.6c probe-marker prediction traces (2026-10-06) + +Development reference for ADR-0094 (EXPERIMENT: a probe marker, not organism movement). +Tool: `evo-motion-trace` (tools/network/motion_trace.cpp, preset `client-network`). + +## Setup +- The engineering client's own session code (`apps/evo-client/remote_session.hpp`: the + predictor, adaptive lead, resend, smoothing). +- A loopback region simulating at 50 ms in real time, with RegionSessions and probe + markers. +- The owned UDP impairment relay with the same impairment in both directions (seed 0xE6). +- Script: a new direction every 0.4-1.1 s from 9 choices (including stop) for 20 s. Then + stop and require convergence: nothing in flight and the presented (smoothed) marker + exactly on the authoritative position. +- The `rtt120-loss2` profile then advances the epoch: the session reconnects with its next + grant after the client's 2 s pause, and resync is measured until the prediction matches + the respawned marker. +- Error: one sample per authoritative snapshot of the own marker. It is the distance + between what the client predicted for that tick and what the authority reported. + Corrections are the jumps that rebasing made to the presented position. + +## Results (Linux GCC 14 release, WSL2, one run) + +| Profile | RTT ms | Jitter ms | Loss | Samples | Exact | p95 mm | p99 mm | max mm | Corrections | Refused | Dropped | Resends | Final lead | Max pending | Converged | +|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---| +| clean | 0 | 0 | 0 % | 400 | 100 % | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2 | 1 | yes | +| lan | 20 | 2 | 0 % | 400 | 100 % | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2 | 1 | yes | +| rtt60-loss2 | 60 | 5 | 2 % | 378 | 97.9 % | 0 | 369.5 | 799.9 | 3 | 2 | 3 | 3 | 4 | 1 | yes | +| rtt120-loss2 | 120 | 10 | 2 % | 279 | 99.3 % | 0 | 0 | 200.0 | 1 | 2 | 2 | 2 | 2 | 1 | yes | +| rtt200-loss5 | 200 | 20 | 5 % | 358 | 94.1 % | 100.0 | 500.0 | 989.9 | 6 | 5 | 7 | 6 | 7 | 2 | yes | + +Windows MSVC 19.44 (same tool, one run): +- clean and lan: 100 % exact. +- rtt60-loss2: 99.5 % exact, max 200 mm. +- rtt120-loss2: 98.6 %, max 500 mm; resync 2,498 ms. +- rtt200-loss5: 94.3 %, p95 100 mm, p99 600 mm, max 1,399.8 mm, lead 7, max 2 pending. +- All profiles converged. + +Restart (rtt120-loss2): resync took 2,946 ms, almost all of it the client's reconnect pause +(2 s) and login. The smoke profile (6 s of rtt60-loss2 plus a restart) passed three runs in a +row: 95.9-98.3 % exact, resync 2,241-2,248 ms. + +## Reading +- Without loss every prediction is exact: the rule is shared and integer-exact, and inputs + are scheduled far enough ahead. +- Errors come from inputs that did not take effect: lost datagrams (dropped) or arrivals + after their tick (refused, which also raises the lead). Each costs at most the motion + until the resend lands: 100 mm per tick, so up to about 1.4 m at 200 ms RTT with 5 % loss + (two losses in a row). Smoothing hid + every correction (no snaps above 2 m). +- The input list stayed tiny (at most 2 pending), far below its bound of 32. Replay depth + equals the lead plus the snapshot age. +- The lead adapts upward on refusals (7 at 200 ms RTT) and back down when receipts show + spare ticks. At 120 ms it ended at 2 after recovering; occasional refusals stay bounded + by the resend rule. +- Figures are single runs on one machine with wall-clock pacing: indicative, not + acceptance thresholds. The CTest `motion-trace-smoke` checks only convergence, + boundedness and a majority of exact predictions. + +## Not measured +- Remote-entity interpolation (10.7). +- 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. diff --git a/include/evo/client/motion_prediction.hpp b/include/evo/client/motion_prediction.hpp index 37d33f3..fd9758c 100644 --- a/include/evo/client/motion_prediction.hpp +++ b/include/evo/client/motion_prediction.hpp @@ -35,7 +35,7 @@ struct AuthoritativeMarker { }; struct PredictionStats { std::uint64_t authoritative{}, corrections{}, replayed_ticks{}, dropped_inputs{}, - rejected_inputs{}, refused_inputs{}; + rejected_inputs{}; std::uint64_t max_error_um{}, max_pending{}; }; diff --git a/tools/network/impair/udp_relay.cpp b/tools/network/impair/udp_relay.cpp index 78f711b..c7c8db8 100644 --- a/tools/network/impair/udp_relay.cpp +++ b/tools/network/impair/udp_relay.cpp @@ -97,6 +97,7 @@ struct UdpRelay::Impl { std::uint16_t port{}; sockaddr_in target{}; std::optional client; + Clock::time_point client_seen{}; mutable std::mutex mutex; Impairment up, down; RelayCounters counters; @@ -169,11 +170,17 @@ struct UdpRelay::Impl { if (n <= 0) continue; const std::lock_guard lock{mutex}; - if (side == 0 && !client) - client = from; // The first client owns the relay. - if (side == 0 && (from.sin_port != client->sin_port || - from.sin_addr.s_addr != client->sin_addr.s_addr)) - continue; + // One client at a time: the first owns the relay; another sender takes over + // only after the owner has been silent for a second (a reconnect, M10.6c). + const bool owner = client && from.sin_port == client->sin_port && + from.sin_addr.s_addr == client->sin_addr.s_addr; + if (side == 0 && !owner) { + if (client && now - client_seen < std::chrono::seconds{1}) + continue; + client = from; + } + if (side == 0) + client_seen = now; admit(side == 0, {buffer.data(), buffer.data() + n}, now); } std::vector ready; diff --git a/tools/network/impair/udp_relay.hpp b/tools/network/impair/udp_relay.hpp index b72edb8..a20d42a 100644 --- a/tools/network/impair/udp_relay.hpp +++ b/tools/network/impair/udp_relay.hpp @@ -1,6 +1,7 @@ #pragma once // M10.2 owned UDP impairment relay (ADR-0090) for tests and measurements; never shipped. -// Listens on 127.0.0.1: for one client and forwards to 127.0.0.1:, applying +// Listens on 127.0.0.1: for one client at a time (a new sender takes over after the +// current one has been silent for 1 s: a reconnect) and forwards to 127.0.0.1:, applying // seeded loss, duplication, reordering, delay/jitter, a payload-size (MTU) limit and // corruption to each packet in each direction. Impairment decisions come from SplitMix64 // in packet order; wall-clock delivery times are not deterministic. Runs its own thread. diff --git a/tools/network/motion_trace.cpp b/tools/network/motion_trace.cpp new file mode 100644 index 0000000..84fc972 --- /dev/null +++ b/tools/network/motion_trace.cpp @@ -0,0 +1,346 @@ +// 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. +// +// evo-motion-trace [--seconds S] [--profile NAME] [--smoke] +// +// 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). +#include "evo/client/session_host.hpp" +#include "evo/network/dev_credentials.hpp" +#include "evo/network/session_grant.hpp" +#include "evo/server/region_sessions.hpp" +#include "remote_session.hpp" +#include "udp_relay.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { +using namespace evo; +using namespace evo::network; +using Clock = std::chrono::steady_clock; +using std::chrono::milliseconds; +constexpr std::string_view server_name = "dev-region.evolution.test"; +constexpr world::WorldIdentity world_id{9, 2}; +constexpr std::uint64_t region = 21; +constexpr std::chrono::milliseconds tick_period{50}; + +struct Profile { + std::string name; + 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()); +} +double percentile_mm(std::vector v, double p) { + if (v.empty()) + return 0; + std::ranges::sort(v); + const auto rank = static_cast(p / 100 * static_cast(v.size() - 1)); + return static_cast(v.at(rank)) / 1000; +} + +struct Files { + std::filesystem::path dir, ca, certificate, key; + std::string ca_pem; +}; +std::optional credentials() { + const auto made = generate_dev_credentials(DeploymentEnvironment::dev, server_name, 1, + std::chrono::system_clock::now()); + if (!made) + return std::nullopt; + Files f; + f.dir = std::filesystem::temp_directory_path() / "evo-motion-trace"; + std::filesystem::remove_all(f.dir); + std::filesystem::create_directories(f.dir); + f.ca_pem = made->authority_pem; + f.ca = f.dir / "ca.pem"; + f.certificate = f.dir / "server.pem"; + f.key = f.dir / "server.key"; + for (const auto& [path, text] : {std::pair{f.ca, made->authority_pem}, + std::pair{f.certificate, made->server_certificate_pem}, + std::pair{f.key, made->server_key_pem}}) { + std::ofstream out{path, std::ios::binary | std::ios::trunc}; + out << text; + if (!out) + return std::nullopt; + } + return f; +} +std::array scalar() { + std::array s{}; + s.back() = 0x2C; + return s; +} + +// One profile: a fresh region, relay and client session. +class Trace { + public: + Trace(const Files& f, const Profile& p) : files_{f}, profile_{p} {} + std::optional run(std::chrono::seconds moving, bool restart) { + auto key = GrantSigningKey::import(DeploymentEnvironment::dev, 1, scalar()); + if (!key) + return std::nullopt; + 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()}); + auto sessions = server::RegionSessions::create(std::move(c)); + if (!sessions) + return std::nullopt; + 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 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) { + GrantClaims g; + g.grant_id = 700 + serial; + g.account.bytes.fill(0x5A); + 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 (!signed_grant) + return std::nullopt; + grants.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)}))) + 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; + } + + 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) { + 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); + } + 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); + } + const Files& files_; + Profile profile_; + std::unique_ptr sessions_; + std::unique_ptr relay_; + std::uint64_t epoch_{1}, tick_{1}, 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()); + 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::fflush(stdout); +} +} // namespace + +int main(int argc, char** argv) { + std::chrono::seconds seconds{20}; + std::string only; + bool smoke = false; + for (int i = 1; i < argc; ++i) { + const std::string_view a = argv[i]; + if (a == "--smoke") + smoke = 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", + stderr); + return 2; + } + } + const auto files = credentials(); + if (!files) + return 2; + std::vector profiles{{"clean", milliseconds{0}, milliseconds{0}, 0.0}, + {"lan", milliseconds{10}, milliseconds{2}, 0.0}, + {"rtt60-loss2", milliseconds{30}, milliseconds{5}, 0.02}, + {"rtt120-loss2", milliseconds{60}, milliseconds{10}, 0.02}, + {"rtt200-loss5", milliseconds{100}, milliseconds{20}, 0.05}}; + if (smoke) { + profiles = {{"rtt60-loss2", milliseconds{30}, milliseconds{5}, 0.02}}; + seconds = std::chrono::seconds{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 (!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()))) + code = 1; + } + std::filesystem::remove_all(files->dir); + return code; +}