Add M10.6a probe-motion experiment core and session protocol v3
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>
This commit is contained in:
dingo
2026-10-06 15:12:13 -04:00
co-authored by Claude Opus 5.5
parent d781492957
commit ab7e26c02e
20 changed files with 1135 additions and 18 deletions
+161
View File
@@ -0,0 +1,161 @@
#include "evo/client/motion_prediction.hpp"
#include <algorithm>
#include <cmath>
#include <utility>
namespace evo::client {
namespace {
std::uint64_t distance_um(const network::motion::State& a, const network::motion::State& b) {
const auto dx = static_cast<double>(a.x - b.x), dy = static_cast<double>(a.y - b.y);
return static_cast<std::uint64_t>(std::llround(std::sqrt((dx * dx) + (dy * dy))));
}
} // namespace
void MotionPredictor::reset() {
authority_.reset();
pending_.clear();
history_.clear();
presented_tick_.reset();
correction_ = {};
}
bool MotionPredictor::input(const MotionInput& in) {
if (full() || !network::motion::valid(in.vector) ||
(authority_ && in.tick <= authority_->tick) ||
(!pending_.empty() && in.sequence <= pending_.back().sequence))
return false;
// A new input changes the future, not what is shown now: it is not a correction.
const auto at = std::ranges::upper_bound(pending_, in.tick, {}, &MotionInput::tick);
pending_.insert(at, in);
stats_.max_pending = std::max<std::uint64_t>(stats_.max_pending, pending_.size());
return true;
}
void MotionPredictor::rejected(std::uint64_t sequence) {
const auto it = std::ranges::find(pending_, sequence, &MotionInput::sequence);
if (it == pending_.end())
return;
const auto before = presented_tick_ ? replay(*presented_tick_) : std::nullopt;
pending_.erase(it);
++stats_.rejected_inputs;
rebase(before);
}
void MotionPredictor::authoritative(const AuthoritativeMarker& a) {
if (authority_ && a.tick <= authority_->tick)
return;
++stats_.authoritative;
if (const auto predicted = history_.find(a.tick); predicted != history_.end()) {
const auto error = distance_um(predicted->second, a.state);
stats_.max_error_um = std::max(stats_.max_error_um, error);
if (errors_.size() < limits_.max_errors)
errors_.push_back(error);
}
std::erase_if(history_, [&](const auto& e) { return e.first <= a.tick; });
const auto before = presented_tick_ ? replay(*presented_tick_) : std::nullopt;
// Applied inputs are in the state; unapplied ones whose tick has run never will be.
stats_.dropped_inputs +=
static_cast<std::uint64_t>(std::ranges::count_if(pending_, [&](const MotionInput& in) {
return in.sequence > a.applied_sequence && in.tick <= a.tick;
}));
std::erase_if(pending_, [&](const MotionInput& in) {
return in.sequence <= a.applied_sequence || in.tick <= a.tick;
});
authority_ = a;
rebase(before);
}
std::optional<network::motion::State> MotionPredictor::predict(std::uint64_t tick) {
if (!authority_)
return std::nullopt;
const auto authority_tick = authority_->tick;
auto state = replay(tick);
if (!state)
return std::nullopt;
presented_tick_ = tick;
if (tick > authority_tick) {
history_[tick] = *state;
while (history_.size() > limits_.max_history)
history_.erase(history_.begin());
}
return state;
}
std::optional<network::motion::State> MotionPredictor::replay(std::uint64_t tick) {
if (!authority_)
return std::nullopt;
auto state = authority_->state;
if (tick <= authority_->tick)
return state;
const auto last =
authority_->tick + std::min(tick - authority_->tick, limits_.max_replay_ticks);
auto next = pending_.begin();
for (auto t = authority_->tick + 1; t <= last; ++t) {
for (; next != pending_.end() && next->tick <= t; ++next)
state.vector = next->vector;
state = network::motion::step(state, authority_->area);
++stats_.replayed_ticks;
}
return state;
}
void MotionPredictor::rebase(const std::optional<network::motion::State>& before) {
if (!before || !presented_tick_)
return;
const auto after = replay(*presented_tick_);
if (!after)
return;
const auto dx = before->x - after->x, dy = before->y - after->y;
if (dx != 0 || dy != 0) {
++stats_.corrections;
correction_.x += dx;
correction_.y += dy;
}
}
MotionPredictor::Offset MotionPredictor::take_correction() noexcept {
return std::exchange(correction_, Offset{});
}
std::vector<std::uint64_t> MotionPredictor::take_errors() {
return std::exchange(errors_, {});
}
void CorrectionSmoother::add(MotionPredictor::Offset c) {
x_ += static_cast<double>(c.x);
y_ += static_cast<double>(c.y);
if (std::hypot(x_, y_) > static_cast<double>(snap_um_)) {
x_ = y_ = 0; // Too far to glide: show the corrected position at once.
++snaps_;
}
}
MotionPredictor::Offset CorrectionSmoother::advance(double elapsed_ms) {
if (elapsed_ms > 0 && half_life_ms_ > 0) {
const double keep = std::exp2(-elapsed_ms / half_life_ms_);
x_ *= keep;
y_ *= keep;
}
if (std::abs(x_) < 1 && std::abs(y_) < 1)
x_ = y_ = 0;
return {static_cast<std::int64_t>(std::llround(x_)),
static_cast<std::int64_t>(std::llround(y_))};
}
void InputLead::too_late() noexcept {
lead_ = std::min(lead_ + 1, max_);
spare_ = 0;
}
void InputLead::admitted(std::uint64_t tick, std::uint64_t server_tick) noexcept {
// More than two ticks to spare twenty times in a row: one tick less lead.
if (tick > server_tick + 2) {
if (++spare_ >= 20 && lead_ > min_) {
--lead_;
spare_ = 0;
}
} else {
spare_ = 0;
}
}
} // namespace evo::client