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
+130
View File
@@ -0,0 +1,130 @@
#include "evo/network/probe_motion.hpp"
#include <algorithm>
namespace evo::network::motion {
namespace {
constexpr std::int32_t max_component = 1000;
// Far inside int64: a marker never moves more than one speed step per axis per tick.
constexpr std::int64_t max_coordinate = std::int64_t{1} << 60;
} // namespace
bool valid(Vector v) noexcept {
const std::int32_t x = v.x, y = v.y;
return (x * x) + (y * y) <= max_component * max_component;
}
bool valid(const Area& a) noexcept {
return a.min_x <= a.max_x && a.min_y <= a.max_y && a.min_x >= -max_coordinate &&
a.max_x <= max_coordinate && a.min_y >= -max_coordinate && a.max_y <= max_coordinate;
}
State step(State s, const Area& a) noexcept {
const auto dx = (std::int64_t{s.vector.x} * speed_um_per_tick) / max_component;
const auto dy = (std::int64_t{s.vector.y} * speed_um_per_tick) / max_component;
s.x = std::clamp(s.x + dx, a.min_x, a.max_x);
s.y = std::clamp(s.y + dy, a.min_y, a.max_y);
return s;
}
bool ProbeMarkers::set_area(const Area& area, std::int64_t spawn_x, std::int64_t spawn_y) {
if (!valid(area))
return false;
area_ = area;
spawn_x_ = std::clamp(spawn_x, area.min_x, area.max_x);
spawn_y_ = std::clamp(spawn_y, area.min_y, area.max_y);
for (auto& e : entries_) {
e.marker.state.x = std::clamp(e.marker.state.x, area.min_x, area.max_x);
e.marker.state.y = std::clamp(e.marker.state.y, area.min_y, area.max_y);
}
publish();
return true;
}
std::expected<std::uint64_t, MarkerError> ProbeMarkers::open(std::uint64_t session) {
if (!area_)
return std::unexpected(MarkerError::no_area);
if (session == 0 ||
std::ranges::any_of(entries_, [&](const Entry& e) { return e.marker.session == session; }))
return std::unexpected(MarkerError::invalid);
if (entries_.size() >= max_markers_)
return std::unexpected(MarkerError::full);
Entry e;
e.marker.id = next_id_++;
e.marker.session = session;
e.marker.state = {spawn_x_, spawn_y_, {}};
entries_.push_back(std::move(e));
publish();
return entries_.back().marker.id;
}
void ProbeMarkers::close(std::uint64_t session) {
std::erase_if(entries_, [&](const Entry& e) { return e.marker.session == session; });
publish();
}
void ProbeMarkers::clear() noexcept {
entries_.clear();
markers_.clear();
}
std::expected<void, MarkerError> ProbeMarkers::schedule(std::uint64_t session,
std::uint64_t sequence, std::uint64_t tick,
Vector vector) {
const auto it =
std::ranges::find_if(entries_, [&](const Entry& e) { return e.marker.session == session; });
if (it == entries_.end())
return std::unexpected(MarkerError::unknown_session);
if (!valid(vector) || tick <= tick_ || sequence <= it->marker.applied_sequence ||
(!it->scheduled.empty() && sequence <= it->scheduled.back().sequence))
return std::unexpected(MarkerError::invalid);
if (it->scheduled.size() >= max_scheduled_)
return std::unexpected(MarkerError::full);
// Keep ascending tick order; a later sequence for an earlier tick supersedes nothing
// already scheduled after it (each takes effect at its own tick).
const auto at = std::ranges::upper_bound(it->scheduled, tick, {}, &Scheduled::tick);
it->scheduled.insert(at, Scheduled{sequence, tick, vector});
return {};
}
void ProbeMarkers::advance(std::uint64_t tick) {
if (tick <= tick_)
return;
if (!area_) {
tick_ = tick;
return;
}
const auto first = tick - tick_ > max_catch_up ? tick - max_catch_up + 1 : tick_ + 1;
for (auto& e : entries_) {
auto& m = e.marker;
// Intentions for ticks that can no longer be stepped still set the held vector.
while (!e.scheduled.empty() && e.scheduled.front().tick < first) {
m.state.vector = e.scheduled.front().vector;
m.applied_sequence = std::max(m.applied_sequence, e.scheduled.front().sequence);
e.scheduled.pop_front();
}
for (auto t = first; t <= tick; ++t) {
while (!e.scheduled.empty() && e.scheduled.front().tick == t) {
m.state.vector = e.scheduled.front().vector;
m.applied_sequence = std::max(m.applied_sequence, e.scheduled.front().sequence);
e.scheduled.pop_front();
}
m.state = step(m.state, *area_);
}
}
tick_ = tick;
publish();
}
const Marker* ProbeMarkers::of(std::uint64_t session) const noexcept {
const auto it =
std::ranges::find_if(markers_, [&](const Marker& m) { return m.session == session; });
return it == markers_.end() ? nullptr : &*it;
}
void ProbeMarkers::publish() {
markers_.clear();
for (const auto& e : entries_)
markers_.push_back(e.marker);
std::ranges::sort(markers_, {}, &Marker::id);
}
} // namespace evo::network::motion
+78 -4
View File
@@ -70,6 +70,19 @@ std::vector<std::uint8_t> remove_organism(std::uint64_t id) {
put(out, id, 8);
return out;
}
std::vector<std::uint8_t> remove_marker(std::uint64_t id) {
std::vector<std::uint8_t> out{static_cast<std::uint8_t>(RecordKind::marker_remove)};
put(out, id, 8);
return out;
}
constexpr auto environment_kind = static_cast<std::uint8_t>(RecordKind::environment_upsert);
constexpr auto organism_kind = static_cast<std::uint8_t>(RecordKind::organism_upsert);
constexpr auto marker_kind = static_cast<std::uint8_t>(RecordKind::marker_upsert);
// The cell a marker position (micrometres) lies in.
std::int64_t cell_of(std::int64_t um) {
const auto q = um / motion::micrometres_per_cell;
return (um % motion::micrometres_per_cell != 0 && um < 0) ? q - 1 : q;
}
std::uint64_t distance(std::int64_t x, std::int64_t y, const SessionInterest& i) {
const auto dx =
x > i.x ? static_cast<std::uint64_t>(x - i.x) : static_cast<std::uint64_t>(i.x - x);
@@ -128,6 +141,24 @@ std::vector<std::uint8_t> encode_record(const client::OrganismCell& c) {
return out;
}
std::vector<std::uint8_t> encode_record(const motion::Marker& m, std::int64_t z,
const motion::Area* own_area) {
std::vector<std::uint8_t> out{static_cast<std::uint8_t>(RecordKind::marker_upsert)};
put(out, m.id, 8);
put_signed(out, m.state.x);
put_signed(out, m.state.y);
put_signed(out, z);
put(out, static_cast<std::uint16_t>(m.state.vector.x), 2);
put(out, static_cast<std::uint16_t>(m.state.vector.y), 2);
out.push_back(own_area != nullptr ? 1 : 0);
if (own_area != nullptr) {
put(out, m.applied_sequence, 8);
for (const auto v : {own_area->min_x, own_area->min_y, own_area->max_x, own_area->max_y})
put_signed(out, v);
}
return out;
}
std::expected<ReplicationServer, ReplicationError> ReplicationServer::create(ReplicationLimits l) {
if (l.max_radius == 0 || l.max_radius > 7 || l.near_radius > l.max_radius ||
l.far_interval == 0 || l.max_baselines == 0 || l.max_baselines > 16 ||
@@ -219,13 +250,24 @@ std::vector<std::pair<std::uint64_t, std::vector<SnapshotChunk>>> ReplicationSer
std::map<Key, std::pair<std::vector<std::uint8_t>, std::uint64_t>> truth;
for (const auto& c : src.environment)
if (c.voxel.z == in.z && distance(c.voxel.x, c.voxel.y, in) <= in.radius)
truth[Key{std::uint8_t{1}, c.voxel.x, c.voxel.y, c.voxel.z}] = {
truth[Key{environment_kind, c.voxel.x, c.voxel.y, c.voxel.z}] = {
encode_record(c), distance(c.voxel.x, c.voxel.y, in)};
for (const auto& o : src.organisms)
if (o.voxel.z == in.z && distance(o.voxel.x, o.voxel.y, in) <= in.radius &&
(!perceives || perceives(s.id, o)))
truth[Key{std::uint8_t{3}, static_cast<std::int64_t>(o.id), 0, 0}] = {
truth[Key{organism_kind, static_cast<std::int64_t>(o.id), 0, 0}] = {
encode_record(o), distance(o.voxel.x, o.voxel.y, in)};
// Markers: the own one always; others in the square of the slice. Distance 0 puts
// every marker first and makes it due every tick it changed.
if (src.marker_area)
for (const auto& m : src.markers) {
const bool own = m.session == s.id;
if (!own && (src.marker_area->z != in.z ||
distance(cell_of(m.state.x), cell_of(m.state.y), in) > in.radius))
continue;
truth[Key{marker_kind, static_cast<std::int64_t>(m.id), 0, 0}] = {
encode_record(m, src.marker_area->z, own ? &*src.marker_area : nullptr), 0};
}
// Baselines older than the limit are forgotten; an expired ack means a fresh one.
while (!s.baselines.empty() && tick - s.baselines.front().tick > limits_.max_baseline_age)
s.baselines.pop_front();
@@ -244,9 +286,11 @@ std::vector<std::pair<std::uint64_t, std::vector<SnapshotChunk>>> ReplicationSer
for (const auto& [key, entry] : base_state)
if (!truth.contains(key)) {
removals.push_back(
std::get<0>(key) == 1
std::get<0>(key) == environment_kind
? remove_environment({std::get<1>(key), std::get<2>(key), std::get<3>(key)})
: remove_organism(static_cast<std::uint64_t>(std::get<1>(key))));
: std::get<0>(key) == organism_kind
? remove_organism(static_cast<std::uint64_t>(std::get<1>(key)))
: remove_marker(static_cast<std::uint64_t>(std::get<1>(key))));
removed.push_back(key);
}
std::vector<Candidate> candidates;
@@ -384,11 +428,14 @@ SnapshotAssembler::accept(const SnapshotChunk& chunk) {
}
std::map<std::pair<std::int64_t, std::int64_t>, client::EnvironmentCell> environment;
std::map<std::uint64_t, client::OrganismCell> organisms;
std::map<std::uint64_t, ReplicatedMarker> markers;
if (base) {
for (const auto& c : base->environment)
environment[{c.voxel.x, c.voxel.y}] = c;
for (const auto& o : base->organisms)
organisms[o.id] = o;
for (const auto& m : base->markers)
markers[m.id] = m;
}
for (const auto& records : p.chunks) {
if (!records)
@@ -447,6 +494,29 @@ SnapshotAssembler::accept(const SnapshotChunk& chunk) {
organisms[o.id] = o;
} else if (kind == static_cast<std::uint8_t>(RecordKind::organism_remove)) {
organisms.erase(r.u(8));
} else if (kind == static_cast<std::uint8_t>(RecordKind::marker_upsert)) {
ReplicatedMarker m;
m.id = r.u(8);
m.state.x = r.i64();
m.state.y = r.i64();
m.z = r.i64();
m.state.vector = {static_cast<std::int16_t>(r.u(2)),
static_cast<std::int16_t>(r.u(2))};
const auto own = r.u(1);
if (own > 1)
return std::unexpected(AssemblyError::malformed);
m.own = own == 1;
if (m.own) {
m.applied_sequence = r.u(8);
m.area = {r.i64(), r.i64(), r.i64(), r.i64(), m.z};
if (r.ok() && !motion::valid(m.area))
return std::unexpected(AssemblyError::malformed);
}
if (r.ok() && (m.id == 0 || !motion::valid(m.state.vector)))
return std::unexpected(AssemblyError::malformed);
markers[m.id] = m;
} else if (kind == static_cast<std::uint8_t>(RecordKind::marker_remove)) {
markers.erase(r.u(8));
} else {
return std::unexpected(AssemblyError::malformed);
}
@@ -464,6 +534,10 @@ SnapshotAssembler::accept(const SnapshotChunk& chunk) {
view.environment.push_back(cell);
for (auto& [id, organism] : organisms)
view.organisms.push_back(organism);
for (auto& [id, marker] : markers)
view.markers.push_back(marker);
if (std::ranges::count_if(view.markers, &ReplicatedMarker::own) > 1)
return std::unexpected(AssemblyError::malformed);
// The same validation as any presentation: a snapshot that cannot be shown is refused.
if (!client::EnvironmentPresentation::create({identity_, view.tick, view.field_revision},
view.mask, view.slice_z, view.environment) &&