Files
aevolution/tests/client_remote_session.cpp
T
dingoandClaude Opus 5.5 5728e7682e
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m55s
Foundation / checks (client) (push) Successful in 12m27s
Foundation / checks (network) (push) Successful in 12m46s
Foundation / checks (tsan) (push) Successful in 7m59s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 25m13s
Foundation / checks (postgres) (push) Canceled after 12m48s
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 <noreply@anthropic.com>
2026-10-06 17:50:41 -04:00

347 lines
15 KiB
C++

// M10.5c: the engineering client's remote session (apps/evo-client/remote_session.hpp)
// against a loopback region (ADR-0093): the launcher's connect flow, snapshot-fed views,
// the camera recentre and interest, intention outcomes (simulated, unknown), reconnects
// with the next grant of the bundle, and refusals (scope, superseded, exhausted).
#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 <algorithm>
#include <chrono>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <functional>
#include <source_location>
#include <stdexcept>
#include <string>
#include <thread>
#include <vector>
namespace {
using namespace evo;
using namespace evo::network;
using client::LaunchError;
using client::LaunchPhase;
using client::OutcomeState;
using client_app::RemoteSession;
using Clock = std::chrono::steady_clock;
void check(bool ok, std::source_location at = std::source_location::current()) {
if (!ok)
throw std::runtime_error("client remote session line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
constexpr std::string_view name = "dev-region.evolution.test";
constexpr world::WorldIdentity world_id{7, 3};
constexpr std::uint64_t region = 12;
constexpr std::uint64_t start = 1'800'000'000;
struct Files {
std::filesystem::path dir, ca, certificate, key;
std::string ca_pem;
};
Files credentials() {
const auto made = generate_dev_credentials(DeploymentEnvironment::dev, name, 1,
std::chrono::system_clock::now());
CHECK(made.has_value());
Files f;
f.dir = std::filesystem::temp_directory_path() / "evo-client-remote-session";
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;
CHECK(out.good());
}
return f;
}
AccountId account(std::uint8_t tag) {
AccountId a;
a.bytes.fill(tag);
return a;
}
std::array<std::uint8_t, grant_private_key_bytes> scalar() {
std::array<std::uint8_t, grant_private_key_bytes> s{};
s.back() = 0x2B;
return s;
}
client::EnvironmentCell cell(std::int64_t x, std::int64_t y, std::uint64_t quantity) {
client::EnvironmentCell c;
c.voxel = {x, y, -7};
c.chunk_revision = 40;
c.habitat = {{100}, {200}, {0}, {1000}, {500}, {10}};
c.volume = {1000ULL * 500 * 10};
c.substrate = {1};
c.terrain_material = {2};
c.terrain_fill = 1000;
c.presence[0] = client::FieldPresence::present;
c.quantity[0] = quantity;
c.presence[1] = client::FieldPresence::missing;
c.presence[7] = client::FieldPresence::present;
c.quantity[7] = 5;
c.moisture_status = world::MoistureStatus::available; // Water is requested.
c.moisture = {3, 50};
return c;
}
// A region on loopback with a 21 x 21 slice (x, y in -10..10 at z -7), one snapshot per tick.
struct Region {
explicit Region(const Files& f) : files{f} {
server::RegionSessionsConfig c;
c.transport.certificate_pem_file = f.certificate.string();
c.transport.key_pem_file = f.key.string();
c.control.environment = DeploymentEnvironment::dev;
c.control.world = world_id;
c.control.region = region;
c.control.epoch = epoch;
c.control.max_account_attempts = 16;
c.issuer_keys.push_back({1, *key.public_key()});
auto made = server::RegionSessions::create(std::move(c));
CHECK(made.has_value());
sessions = std::move(*made);
sessions->set_default_interest({3, 3, -7, 7});
// Probe markers (ADR-0094) over the slice's cells, spawning in the middle of (3, 3).
CHECK(sessions->set_motion_area({-10'000'000, -10'000'000, 10'999'999, 10'999'999, -7},
3'500'000, 3'500'000));
}
WireGrant grant(const AccountId& who, std::uint64_t serial,
std::uint64_t in_region = region) const {
GrantClaims c;
c.grant_id = 900 + serial;
c.account = who;
c.serial = serial;
c.world = world_id;
c.region = in_region;
c.issued_at = start;
c.expires_at = start + 120;
const auto g = key.sign(c);
CHECK(g.has_value());
return *g;
}
// The launcher's material: the endpoint by host name, as a tester types it.
client::ConnectMaterial material(std::vector<WireGrant> grants) const {
return {{"localhost", sessions->port()},
std::string{name},
files.ca_pem,
files.ca.string(),
std::move(grants)};
}
void pump() {
sessions->poll({true, epoch, tick}, start + 1, Clock::now());
if (tick != replicated) {
replicated = tick;
sessions->replicate(tick, [&](const SessionInterest&) {
ReplicationSource s;
s.identity = world_id;
s.field_revision = tick;
s.mask = (1U << 0) | (1U << 1) | (1U << 7);
for (std::int64_t x = -10; x <= 10; ++x)
for (std::int64_t y = -10; y <= 10; ++y)
s.environment.push_back(cell(x, y, 30));
return s;
});
}
static_cast<void>(sessions->take_events());
}
const Files& files;
GrantSigningKey key = *GrantSigningKey::import(DeploymentEnvironment::dev, 1, scalar());
std::unique_ptr<server::RegionSessions> sessions;
std::uint64_t epoch{3}, tick{100}, replicated{};
};
struct Client {
client::SessionHost<RemoteSession> host;
client::UiState ui;
client::Intention intention;
// Steps the region and the client together (one region tick every `tick_every`
// iterations, 0: none) until `done` or the limit.
bool until(Region& r, const std::function<bool()>& done, int tick_every = 0,
std::chrono::milliseconds limit = std::chrono::milliseconds{15000}) {
const auto end = Clock::now() + limit;
for (int i = 1; Clock::now() < end; ++i) {
r.pump();
if (tick_every != 0 && i % tick_every == 0)
++r.tick;
host.step(Clock::now(), intention, ui);
if (done())
return true;
std::this_thread::sleep_for(std::chrono::milliseconds{1});
}
return done();
}
[[nodiscard]] LaunchPhase phase() const {
return host.launcher().phase();
}
[[nodiscard]] std::size_t cells() const {
const auto* s = host.session();
const auto* e = s ? s->environment().current() : nullptr;
return e ? e->cells().size() : 0;
}
[[nodiscard]] bool has(OutcomeState state) const {
const auto* s = host.session();
return s && std::ranges::any_of(s->outcomes().entries(),
[&](const auto& e) { return e.state == state; });
}
};
void views_interest_and_intentions(Region& r) {
Client c;
CHECK(c.host.connect(
RemoteSession::open(r.material({r.grant(account(1), 1), r.grant(account(1), 2)}))));
CHECK(c.until(r, [&] { return c.phase() == LaunchPhase::connected; }));
// The default interest (3, 3, radius 7) arrives as the copied view.
CHECK(c.until(r, [&] { return c.cells() == 225; }, 20));
auto& s = *c.host.session();
const auto* view = s.environment().current();
CHECK(view != nullptr && view->slice_z() == -7 &&
std::ranges::all_of(view->cells(), [](const auto& e) {
return e.voxel.x >= -4 && e.voxel.x <= 10 && e.voxel.y >= -4 && e.voxel.y <= 10;
}));
CHECK(s.environment().freshness() == client::Freshness::current);
// The session's probe marker, predicted at its spawn; the camera follows it, and so does
// the interest (its cell is the default's centre: the same view).
CHECK(c.until(r, [&] { return s.status().marker; }, 20));
CHECK(s.markers().size() == 1 && s.markers()[0].own && s.status().marker_x_mm == 3500);
const auto centre = s.take_recentre().value_or(client::WorldPoint{-1, -1});
CHECK(centre.x_mm == 3500 && centre.y_mm == 3500 && !s.take_recentre());
s.focus(centre); // Where the session put it: still following.
static_cast<void>(c.until(r, [] { return false; }, 20, std::chrono::milliseconds{300}));
CHECK(s.status().focused && s.status().focus_x == 3 && s.status().focus_y == 3 &&
c.cells() == 225); // 300 ms of ticks: the same view.
// The user moves the camera: its centre's voxel becomes the interest (clipped).
s.focus({-5500, -5200});
CHECK(c.until(r, [&] { return c.cells() == 144; }, 20)); // x, y in -10..1.
const auto status = s.status();
CHECK(status.focused && status.focus_x == -6 && status.focus_y == -6 && status.focus_z == -7 &&
status.phase == "connected" && status.grants_left == 1 && status.snapshot_bytes > 0);
// An intention: sent once per change, admitted, then simulated (never durable).
c.intention.move_x = 700;
CHECK(c.until(r, [&] { return c.has(OutcomeState::admitted); }));
CHECK(c.until(r, [&] { return c.has(OutcomeState::simulated); }, 20));
const auto& first = s.outcomes().entries().front();
CHECK(first.sequence == 1 && first.tick != 0 && s.status().simulated_tick >= first.tick);
// The marker moves +x at 70 mm per tick, predicted ahead of the authority, and the
// authority confirms the prediction (loopback: no correction needed).
CHECK(c.until(r, [&] { return s.status().marker_x_mm > 4000; }, 20));
CHECK(s.status().marker_y_mm == 3500 && s.status().max_error_um == 0 &&
s.status().inputs_pending == 0);
// A new epoch closes the session: the open intention is unknown, the views are stale,
// and the session reconnects with the bundle's next grant after its pause.
const auto old_session = s.status().session;
c.intention.move_x = -700;
CHECK(c.until(r, [&] { return c.has(OutcomeState::admitted) || c.has(OutcomeState::sent); }));
r.epoch = 4;
CHECK(c.until(r, [&] { return c.has(OutcomeState::unknown); }));
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).
CHECK(c.until(r, [&] { return c.cells() == 144 && s.status().snapshot_tick > 0; }, 20));
// The current intention is sent again in the new session; the record numbers it on
// (session sequences restart) and keeps the earlier entries.
CHECK(c.until(r, [&] { return s.outcomes().entries().size() == 3; }));
CHECK(s.outcomes().entries()[0].state == OutcomeState::simulated &&
s.outcomes().entries()[2].sequence == 3);
// Another client with a newer grant for the account supersedes this one: no fight back.
Client other;
CHECK(other.host.connect(RemoteSession::open(r.material({r.grant(account(1), 9)}))));
CHECK(other.until(r, [&] {
c.host.step(Clock::now(), c.intention, c.ui);
return c.phase() == LaunchPhase::failed && other.phase() == LaunchPhase::connected;
}));
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;
CHECK(wrong.host.connect(RemoteSession::open(r.material({r.grant(account(2), 1, 99)}))));
CHECK(wrong.until(r, [&] { return wrong.phase() == LaunchPhase::failed; }));
CHECK(wrong.host.launcher().error() == LaunchError::grant_refused);
// One grant: once its session ends there is nothing to reconnect with.
Client one;
CHECK(one.host.connect(RemoteSession::open(r.material({r.grant(account(3), 1)}))));
CHECK(one.until(r, [&] { return one.phase() == LaunchPhase::connected; }));
r.epoch = 5;
CHECK(one.until(r, [&] { return one.phase() == LaunchPhase::failed; }));
CHECK(one.host.launcher().error() == LaunchError::grants_exhausted);
// Nothing to open: an unresolvable endpoint or an empty bundle.
auto bad = r.material({r.grant(account(4), 1)});
bad.endpoint.host = "no-such-host.invalid";
CHECK(!RemoteSession::open(bad));
CHECK(!RemoteSession::open(r.material({})));
}
} // namespace
int main() try {
const auto files = credentials();
{
Region r{files};
views_interest_and_intentions(r);
remote_markers(r);
refusals(r);
}
std::filesystem::remove_all(files.dir);
std::puts("client remote session tests passed");
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "%s\n", e.what());
return 1;
}