Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m1s
Foundation / checks (network) (push) Successful in 12m13s
Foundation / checks (client) (push) Successful in 14m28s
Foundation / checks (tsan) (push) Successful in 7m56s
Foundation / checks (checks) (push) Successful in 32m49s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 40m58s
EXPERIMENT (ADR-0094), not an organism movement rule: one non-biological marker per session, moved by admitted intentions with a shared integer rule (probe-motion-v1, 100 mm per tick, clamped to the area), held in region memory only. - ProbeMarkers: spawn, area, bounded per-marker schedule, catch-up bound - session protocol v3: marker records in snapshot chunks; the own marker is always sent with its applied sequence and area; v1 and v2 are refused - MotionPredictor (bounded replay from the authoritative marker, corrections, error samples), CorrectionSmoother and InputLead - tests: exact prediction with enough lead, refusal and lead adaptation, lost inputs, bounds, marker records through the codec Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
330 lines
15 KiB
C++
330 lines
15 KiB
C++
// M10.6 probe-motion experiment (ADR-0094): the shared rule, the server's markers, client
|
|
// prediction and reconciliation (exact replay, refused and lost inputs, resets, bounds),
|
|
// presentation smoothing, the input lead, and marker records in v3 snapshots.
|
|
#include "evo/client/motion_prediction.hpp"
|
|
#include "evo/network/replication.hpp"
|
|
#include "network_test.hpp"
|
|
#include <algorithm>
|
|
#include <deque>
|
|
#include <functional>
|
|
#include <set>
|
|
#include <vector>
|
|
|
|
namespace evo::network_test {
|
|
namespace {
|
|
using namespace evo::network;
|
|
using motion::Area;
|
|
using motion::ProbeMarkers;
|
|
using motion::State;
|
|
using motion::Vector;
|
|
constexpr Area area{-5'000'000, -5'000'000, 5'000'000, 5'000'000, -7};
|
|
|
|
void rule() {
|
|
CHECK(motion::valid(Vector{1000, 0}) && motion::valid(Vector{707, 707}) &&
|
|
motion::valid(Vector{0, -1000}) && !motion::valid(Vector{708, 708}) &&
|
|
!motion::valid(Vector{-32768, 0}) && !motion::valid(Vector{1001, 0}));
|
|
CHECK(motion::step({0, 0, {1000, 0}}, area) == State{100'000, 0, {1000, 0}});
|
|
CHECK(motion::step({0, 0, {707, -707}}, area) == State{70'700, -70'700, {707, -707}});
|
|
CHECK(motion::step({0, 0, {-1, 1}}, area) == State{-100, 100, {-1, 1}}); // Exact.
|
|
CHECK(motion::step({0, 0, {}}, area) == State{0, 0, {}});
|
|
// Clamped to the area, holding the vector.
|
|
CHECK(motion::step({4'950'000, -4'990'000, {1000, -1000}}, area) ==
|
|
State{5'000'000, -5'000'000, {1000, -1000}});
|
|
CHECK(!motion::valid(Area{1, 0, 0, 0, 0}) && motion::valid(Area{0, 0, 0, 0, 0}));
|
|
}
|
|
|
|
void markers() {
|
|
ProbeMarkers m{2, 3};
|
|
CHECK(refused(m.open(1), motion::MarkerError::no_area));
|
|
CHECK(!m.set_area(Area{1, 0, 0, 0, 0}, 0, 0));
|
|
CHECK(m.set_area(area, 9'000'000, 0)); // The spawn is clamped into the area.
|
|
const auto a = m.open(10), b = m.open(11);
|
|
CHECK(a && b && *a == 1 && *b == 2 && refused(m.open(12), motion::MarkerError::full) &&
|
|
refused(m.open(10), motion::MarkerError::invalid));
|
|
CHECK(m.of(10)->state == State{5'000'000, 0, {}});
|
|
m.advance(4);
|
|
// From tick 5 on the marker holds the vector; earlier ticks are past.
|
|
CHECK(m.schedule(10, 1, 5, {-1000, 0}));
|
|
CHECK(refused(m.schedule(10, 2, 4, {}), motion::MarkerError::invalid)); // Past.
|
|
CHECK(refused(m.schedule(10, 1, 6, {}), motion::MarkerError::invalid)); // Sequence.
|
|
CHECK(refused(m.schedule(10, 2, 6, {2000, 0}), motion::MarkerError::invalid)); // Vector.
|
|
CHECK(refused(m.schedule(99, 2, 6, {}), motion::MarkerError::unknown_session));
|
|
CHECK(m.schedule(10, 2, 8, {0, 1000}) && m.schedule(10, 3, 9, {}));
|
|
CHECK(refused(m.schedule(10, 4, 10, {}), motion::MarkerError::full)); // Bounded.
|
|
m.advance(4);
|
|
CHECK(m.of(10)->state.x == 5'000'000 && m.of(10)->applied_sequence == 0);
|
|
m.advance(5);
|
|
CHECK(m.of(10)->state == State{4'900'000, 0, {-1000, 0}} && m.of(10)->applied_sequence == 1);
|
|
m.advance(7);
|
|
CHECK(m.of(10)->state.x == 4'700'000);
|
|
m.advance(9); // Tick 8 moves +y; tick 9 stops.
|
|
CHECK(m.of(10)->state == State{4'700'000, 100'000, {}} && m.of(10)->applied_sequence == 3);
|
|
CHECK(m.of(11)->state == State{5'000'000, 0, {}});
|
|
// A long gap applies what was scheduled but steps at most max_catch_up ticks.
|
|
CHECK(m.schedule(10, 4, 10, {1000, 0}));
|
|
m.advance(1000);
|
|
CHECK(m.of(10)->state.x == 5'000'000 && m.tick() == 1000);
|
|
CHECK(m.of(10)->state.vector == Vector{1000, 0});
|
|
m.close(10);
|
|
CHECK(!m.of(10) && m.markers().size() == 1 && m.open(12) && m.of(12)->id == 3);
|
|
}
|
|
|
|
// One client against the server's markers with whole-tick delays. Inputs reach the server
|
|
// `up` ticks after sending, snapshots and receipts reach the client `down` ticks after.
|
|
struct Link {
|
|
std::uint64_t up{}, down{};
|
|
std::set<std::uint64_t> lost; // Input sequences lost on the way.
|
|
};
|
|
struct Run {
|
|
std::vector<std::uint64_t> errors;
|
|
std::uint64_t rejected{}, last_rejected_at{}, corrections{}, final_lead{};
|
|
std::uint64_t error_ticks_after_warmup{};
|
|
};
|
|
Run drive(const Link& link, client::InputLead lead, std::uint64_t ticks,
|
|
const std::function<Vector(std::uint64_t)>& script) {
|
|
ProbeMarkers server;
|
|
CHECK(server.set_area(area, 0, 0) && server.open(1));
|
|
client::MotionPredictor predictor;
|
|
std::deque<std::pair<std::uint64_t, client::MotionInput>> to_server;
|
|
std::deque<std::pair<std::uint64_t, client::AuthoritativeMarker>> snapshots;
|
|
std::deque<std::pair<std::uint64_t, std::uint64_t>> refusals; // Arrival, sequence.
|
|
Run run;
|
|
std::uint64_t sequence = 0;
|
|
Vector sent{};
|
|
for (std::uint64_t t = 1; t <= ticks; ++t) {
|
|
// Server: inputs that arrived during tick t-1 are admitted against server tick t-1.
|
|
while (!to_server.empty() && to_server.front().first <= t - 1) {
|
|
const auto in = to_server.front().second;
|
|
to_server.pop_front();
|
|
if (in.tick > t - 1 && in.tick - (t - 1) <= 8)
|
|
CHECK(server.schedule(1, in.sequence, in.tick, in.vector));
|
|
else
|
|
refusals.emplace_back(t - 1 + link.down, in.sequence);
|
|
}
|
|
server.advance(t);
|
|
const auto& m = *server.of(1);
|
|
snapshots.emplace_back(t + link.down,
|
|
client::AuthoritativeMarker{t, m.applied_sequence, m.state, area});
|
|
// Client at tick t: it knows the server tick t - down.
|
|
while (!snapshots.empty() && snapshots.front().first <= t) {
|
|
predictor.authoritative(snapshots.front().second);
|
|
snapshots.pop_front();
|
|
}
|
|
while (!refusals.empty() && refusals.front().first <= t) {
|
|
predictor.rejected(refusals.front().second);
|
|
lead.too_late();
|
|
++run.rejected;
|
|
run.last_rejected_at = t;
|
|
refusals.pop_front();
|
|
}
|
|
if (t <= link.down)
|
|
continue;
|
|
const auto known = t - link.down;
|
|
const auto v = script(t);
|
|
if (v != sent) {
|
|
const client::MotionInput in{++sequence, known + lead.lead(), v};
|
|
CHECK(predictor.input(in));
|
|
sent = v;
|
|
if (!link.lost.contains(in.sequence))
|
|
to_server.emplace_back(t + link.up, in);
|
|
}
|
|
CHECK(predictor.predict(known + lead.lead()));
|
|
for (const auto e : predictor.take_errors()) {
|
|
run.errors.push_back(e);
|
|
if (e != 0 && run.last_rejected_at != 0 && t > run.last_rejected_at + 16)
|
|
++run.error_ticks_after_warmup;
|
|
}
|
|
}
|
|
run.corrections = predictor.stats().corrections;
|
|
run.final_lead = lead.lead();
|
|
return run;
|
|
}
|
|
Vector zigzag(std::uint64_t t) {
|
|
const auto phase = (t / 7) % 4;
|
|
return phase == 0 ? Vector{1000, 0}
|
|
: phase == 1 ? Vector{0, 1000}
|
|
: phase == 2 ? Vector{-707, -707}
|
|
: Vector{};
|
|
}
|
|
|
|
void prediction() {
|
|
// Enough lead for the round trip: every prediction is exact (shared integer rule).
|
|
const auto exact = drive({1, 1, {}}, client::InputLead{4, 7}, 300, zigzag);
|
|
CHECK(!exact.errors.empty() && exact.rejected == 0 && exact.corrections == 0 &&
|
|
std::ranges::all_of(exact.errors, [](auto e) { return e == 0; }));
|
|
// Too little lead: inputs arrive late and are refused; the lead grows until they fit,
|
|
// and from then on predictions are exact again.
|
|
const auto late = drive({2, 2, {}}, client::InputLead{2, 7}, 400, zigzag);
|
|
CHECK(late.rejected > 0 && late.final_lead >= 5 && late.corrections > 0 &&
|
|
late.error_ticks_after_warmup == 0);
|
|
// A lost input: the authority never applies it; the client reconciles to what ran.
|
|
const auto lost = drive({1, 1, {3, 5}}, client::InputLead{4, 7}, 200, zigzag);
|
|
CHECK(lost.corrections > 0 && std::ranges::any_of(lost.errors, [](auto e) { return e != 0; }));
|
|
}
|
|
|
|
void predictor_rules() {
|
|
client::MotionPredictor p{{4, 8, 16, 16}};
|
|
CHECK(!p.predict(5)); // Nothing before the first authoritative state.
|
|
CHECK(p.input({1, 3, {1000, 0}}));
|
|
p.authoritative({2, 0, {0, 0, {}}, area});
|
|
CHECK(p.pending() == 1);
|
|
CHECK(must(p.predict(4)) == State{200'000, 0, {1000, 0}}); // Ticks 3 and 4.
|
|
// Rejection: the input never takes effect; the shown position jumps back.
|
|
p.rejected(1);
|
|
CHECK(p.pending() == 0 && must(p.predict(4)) == State{0, 0, {}});
|
|
const auto c = p.take_correction();
|
|
CHECK(c.x == 200'000 && c.y == 0 && p.take_correction().x == 0);
|
|
// Bounded: a full list refuses (the caller does not send); stale ticks are refused.
|
|
for (std::uint64_t s = 2; s <= 5; ++s)
|
|
CHECK(p.input({s, 2 + s, {0, 1000}}));
|
|
CHECK(p.full() && !p.input({6, 9, {}}));
|
|
p.authoritative({4, 3, {0, 100'000, {0, 1000}}, area}); // Applied through 3, tick 4.
|
|
CHECK(p.pending() == 2 && !p.input({6, 4, {}}) && p.input({6, 9, {}}));
|
|
// Replay is bounded in ticks beyond the authority (8 here); ticks 5-8 move, the
|
|
// tick-9 input stops the marker.
|
|
const auto replayed = p.stats().replayed_ticks;
|
|
const auto far = must(p.predict(1000));
|
|
CHECK(far.y == 100'000 + (4 * 100'000) && p.stats().replayed_ticks - replayed == 8);
|
|
p.authoritative({3, 9, {}, area}); // Older: ignored.
|
|
CHECK(must(p.authority()).tick == 4);
|
|
// An input that the authority passed without applying is dropped.
|
|
p.authoritative({7, 4, {0, 400'000, {0, 1000}}, area});
|
|
CHECK(p.pending() == 1 && p.stats().dropped_inputs == 1);
|
|
// Reset (new session or epoch) forgets everything.
|
|
p.reset();
|
|
CHECK(!p.authority() && p.pending() == 0 && !p.predict(8));
|
|
}
|
|
|
|
void smoothing_and_lead() {
|
|
client::CorrectionSmoother s{60, 2'000'000};
|
|
s.add({1'000'000, -400'000});
|
|
const auto half = s.advance(60);
|
|
CHECK(half.x == 500'000 && half.y == -200'000);
|
|
CHECK(s.advance(600).x < 1'000 && s.snaps() == 0);
|
|
s.add({3'000'000, 0}); // Beyond the snap distance: no glide.
|
|
CHECK(s.snaps() == 1 && s.advance(0).x == 0);
|
|
client::InputLead lead{2, 5};
|
|
for (int i = 0; i < 10; ++i)
|
|
lead.too_late();
|
|
CHECK(lead.lead() == 5);
|
|
for (int i = 0; i < 19; ++i)
|
|
lead.admitted(100, 90);
|
|
CHECK(lead.lead() == 5);
|
|
lead.admitted(100, 90);
|
|
CHECK(lead.lead() == 4);
|
|
lead.admitted(100, 99); // No spare: the count restarts.
|
|
for (int i = 0; i < 19; ++i)
|
|
lead.admitted(100, 90);
|
|
CHECK(lead.lead() == 4);
|
|
}
|
|
|
|
// Markers in v3 snapshots through the real codec.
|
|
std::vector<SnapshotAck> deliver(SnapshotAssembler& client,
|
|
const std::vector<SnapshotChunk>& chunks) {
|
|
std::vector<SnapshotAck> acks;
|
|
for (const auto& c : chunks) {
|
|
const auto decoded = decode_session(encode_session(c));
|
|
CHECK(decoded);
|
|
const auto accepted = client.accept(std::get<SnapshotChunk>(*decoded));
|
|
CHECK(accepted);
|
|
if (const auto ack = accepted.value_or(std::nullopt))
|
|
acks.push_back(*ack);
|
|
}
|
|
return acks;
|
|
}
|
|
const std::vector<SnapshotChunk>&
|
|
chunks_of(const std::vector<std::pair<std::uint64_t, std::vector<SnapshotChunk>>>& out,
|
|
std::uint64_t session) {
|
|
const auto it = std::ranges::find_if(out, [&](const auto& e) { return e.first == session; });
|
|
CHECK(it != out.end());
|
|
return it->second;
|
|
}
|
|
const ReplicatedMarker* marker(const SnapshotAssembler& a, std::uint64_t id) {
|
|
const auto& v = *a.latest();
|
|
const auto it = std::ranges::find(v.markers, id, &ReplicatedMarker::id);
|
|
return it == v.markers.end() ? nullptr : &*it;
|
|
}
|
|
|
|
void replicated_markers() {
|
|
constexpr world::WorldIdentity identity{11, 12};
|
|
auto made = ReplicationServer::create({});
|
|
CHECK(made);
|
|
auto server = std::move(*made);
|
|
CHECK(server.open(1, 5) && server.open(2, 5));
|
|
CHECK(server.set_interest(1, {0, 0, -7, 2}) && server.set_interest(2, {4, 0, -7, 7}));
|
|
ProbeMarkers markers;
|
|
CHECK(markers.set_area(area, 0, 0) && markers.open(1) && markers.open(2));
|
|
// Session 2's marker starts outside session 1's interest square (x cell 4 > radius 2).
|
|
CHECK(markers.schedule(2, 1, 1, {1000, 0}));
|
|
const auto source = [&](const SessionInterest&) {
|
|
ReplicationSource s;
|
|
s.identity = identity;
|
|
s.markers = markers.markers();
|
|
s.marker_area = markers.area();
|
|
return s;
|
|
};
|
|
SnapshotAssembler a{identity, 5}, b{identity, 5};
|
|
std::uint64_t tick = 0;
|
|
const auto step = [&] {
|
|
markers.advance(++tick);
|
|
const auto out = server.tick(tick, source, {});
|
|
for (const auto& ack : deliver(a, chunks_of(out, 1)))
|
|
server.acknowledge(1, ack);
|
|
for (const auto& ack : deliver(b, chunks_of(out, 2)))
|
|
server.acknowledge(2, ack);
|
|
return out;
|
|
};
|
|
for (int i = 0; i < 30; ++i)
|
|
step(); // Marker 2 moves to x = 3 m (cell 3), then 4 m and beyond.
|
|
// Session 1 holds its own marker (with sequence and area) and not marker 2 (outside).
|
|
const auto* own = marker(a, 1);
|
|
CHECK(own && own->own && own->area == area && own->state == State{0, 0, {}} && own->z == -7 &&
|
|
!marker(a, 2));
|
|
// Session 2 sees marker 1 (in its square), without sequence or area, and its own.
|
|
const auto* other = marker(b, 1);
|
|
const auto* mine = marker(b, 2);
|
|
CHECK(other && !other->own && other->applied_sequence == 0 && other->area == Area{});
|
|
CHECK(mine && mine->own && mine->applied_sequence == 1 && mine->state.x == 3'000'000);
|
|
// The own marker is sent even when it is outside the session's interest.
|
|
CHECK(server.set_interest(2, {-5, -5, -7, 1}));
|
|
step();
|
|
step();
|
|
CHECK(marker(b, 2) && marker(b, 2)->state.x == 3'200'000 && !marker(b, 1));
|
|
// An unchanged marker is not resent; a closed session's marker is removed.
|
|
const auto before = server.stats().records;
|
|
step();
|
|
CHECK(server.stats().records - before == 1); // Only marker 2 moved.
|
|
markers.close(1);
|
|
step();
|
|
CHECK(!marker(a, 1) && !marker(b, 1));
|
|
// Malformed marker records are refused.
|
|
SnapshotAssembler c{identity, 5};
|
|
motion::Marker m;
|
|
m.id = 1;
|
|
auto two_own = encode_record(m, -7, &area);
|
|
m.id = 2;
|
|
const auto second = encode_record(m, -7, &area);
|
|
two_own.insert(two_own.end(), second.begin(), second.end());
|
|
auto bad_flag = encode_record(m, -7, nullptr);
|
|
bad_flag.back() = 2;
|
|
m.state.vector = {2000, 0};
|
|
const auto bad_vector = encode_record(m, -7, nullptr);
|
|
m = {};
|
|
const auto zero_id = encode_record(m, -7, nullptr);
|
|
std::uint64_t t = 100;
|
|
for (const auto& records : {two_own, bad_flag, bad_vector, zero_id})
|
|
CHECK(refused(c.accept(SnapshotChunk{5, ++t, 0, 0, 0, 0, -7, 0, 1, records}),
|
|
AssemblyError::malformed));
|
|
}
|
|
} // namespace
|
|
|
|
void motion_tests() {
|
|
rule();
|
|
markers();
|
|
prediction();
|
|
predictor_rules();
|
|
smoothing_and_lead();
|
|
replicated_markers();
|
|
}
|
|
} // namespace evo::network_test
|