Share replication encoding per interest; fix dev retained builds
Foundation / checks (deploy) (push) Successful in 3m26s
Foundation / checks (ecs) (push) Successful in 1m54s
Foundation / checks (client) (push) Successful in 12m36s
Foundation / checks (network) (push) Successful in 13m35s
Foundation / checks (tsan) (push) Successful in 7m48s
Foundation / checks (checks) (push) Successful in 32m51s
Foundation / checks (postgres) (push) Successful in 46m20s
Foundation / postgres-tools (push) Successful in 1m35s
Foundation / image (push) Successful in 2m32s
Foundation / server-image (push) Successful in 4m14s

ADR-0097. The second evolution-dev load drill (image 7952185d) held 20
sessions with client budgets met (response p95 111 ms, outcome p95 about
1.2 s, 20 Hz, no starvation) but the server over its own: tick work p99
48-70 ms (25 ms budget), session layer 1.2-2.0 ms per session at p99
(0.5 ms budget), governor refusing and shedding to level 3 to little
effect. ReplicationServer::tick re-encoded every environment cell and
organism for each session. Sessions with the same requested interest
(no perception predicate) now share one encoding per tick; markers stay
per session. Locally 21 sessions: about 970 us -> 360 us per tick.
The drill now waits for two consecutive calm reports before the bots.

The dev overlay's REGION_RETAINED listed the deployed build itself
(61df6a1, f88bee2), which the deploy policy refuses (CI deploy failed on
those commits); it now lists only the other builds, keeping 6c96629e,
the build of the region head. Refreshes the dev session window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-08 15:03:45 -04:00
co-authored by Claude Opus 5.5
parent 9409d65947
commit 46b6358606
7 changed files with 187 additions and 41 deletions
+72 -35
View File
@@ -1,7 +1,9 @@
#include "evo/network/replication.hpp"
#include <algorithm>
#include <array>
#include <deque>
#include <limits>
#include <optional>
namespace evo::network {
namespace {
@@ -242,7 +244,17 @@ std::vector<std::pair<std::uint64_t, std::vector<SnapshotChunk>>> ReplicationSer
const auto overhead = session_header_bytes + snapshot_chunk_header_bytes;
if (tick == 0)
return out;
std::vector<std::pair<SessionInterest, ReplicationSource>> sources;
// Per distinct requested interest: the source, and (without a perception predicate) the
// encoded environment and organisms in its effective square, shared by every session
// with that interest (10.9d, ADR-0097: encoding them per session dominated the session
// layer on the reference worker). A deque keeps references stable as entries are added.
using Truth = std::map<Key, std::pair<std::vector<std::uint8_t>, std::uint64_t>>;
struct Shared {
SessionInterest interest;
ReplicationSource source;
std::optional<Truth> truth;
};
std::deque<Shared> sources;
for (auto& s : sessions_) {
if (!s.interest || (!s.baselines.empty() && s.baselines.back().tick >= tick))
continue;
@@ -252,36 +264,55 @@ std::vector<std::pair<std::uint64_t, std::vector<SnapshotChunk>>> ReplicationSer
const auto capacity = payload - overhead; // Record bytes per chunk.
const auto& wanted = *s.interest;
auto cached =
std::ranges::find_if(sources, [&](const auto& e) { return e.first == wanted; });
std::ranges::find_if(sources, [&](const Shared& e) { return e.interest == wanted; });
if (cached == sources.end()) {
sources.emplace_back(wanted, source(wanted));
sources.push_back({wanted, source(wanted), std::nullopt});
cached = std::prev(sources.end());
}
auto in = wanted; // Encoded within the shed cap; the stored interest is unchanged.
in.radius = std::min(in.radius, shed_radius.at(shed_));
const auto& src = cached->second;
const auto& src = cached->source;
// Spatial filter, then perception: nothing outside either is ever encoded.
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{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{organism_kind, static_cast<std::int64_t>(o.id), 0, 0}] = {
encode_record(o), distance(o.voxel.x, o.voxel.y, in)};
const auto fill = [&](Truth& into, bool filtered) {
for (const auto& c : src.environment)
if (c.voxel.z == in.z && distance(c.voxel.x, c.voxel.y, in) <= in.radius)
into[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 &&
(!filtered || perceives(s.id, o)))
into[Key{organism_kind, static_cast<std::int64_t>(o.id), 0, 0}] = {
encode_record(o), distance(o.voxel.x, o.voxel.y, in)};
};
// `own`: this session's entries (markers; everything when perception filters).
Truth own;
const Truth* shared = nullptr;
if (perceives) {
fill(own, true);
} else {
if (!cached->truth) {
cached->truth.emplace();
fill(*cached->truth, false);
}
shared = &*cached->truth;
}
// 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.ghost && 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))
const bool own_marker = !m.ghost && m.session == s.id;
if (!own_marker &&
(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};
own[Key{marker_kind, static_cast<std::int64_t>(m.id), 0, 0}] = {
encode_record(m, src.marker_area->z, own_marker ? &*src.marker_area : nullptr),
0};
}
// Marker keys never collide with environment or organism keys (the kind differs).
const auto contains = [&](const Key& key) {
return own.contains(key) || (shared != nullptr && shared->contains(key));
};
// 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();
@@ -298,7 +329,7 @@ std::vector<std::pair<std::uint64_t, std::vector<SnapshotChunk>>> ReplicationSer
std::vector<std::vector<std::uint8_t>> removals;
std::vector<Key> removed;
for (const auto& [key, entry] : base_state)
if (!truth.contains(key)) {
if (!contains(key)) {
removals.push_back(
std::get<0>(key) == environment_kind
? remove_environment({std::get<1>(key), std::get<2>(key), std::get<3>(key)})
@@ -309,21 +340,27 @@ std::vector<std::pair<std::uint64_t, std::vector<SnapshotChunk>>> ReplicationSer
}
std::vector<Candidate> candidates;
std::uint64_t deferred = 0;
for (const auto& [key, value] : truth) {
const auto known = base_state.find(key);
if (known != base_state.end() && known->second.record == value.first)
continue; // The client already holds this value.
const bool due = known == base_state.end() || value.second <= near_radius ||
tick - known->second.updated >= far_interval;
if (!due) {
++deferred;
continue;
}
const auto staleness = known == base_state.end()
? std::numeric_limits<std::uint64_t>::max()
: tick - known->second.updated;
candidates.push_back({value.second, staleness, key, &value.first});
}
const auto consider =
[&](const Key& key, const std::pair<std::vector<std::uint8_t>, std::uint64_t>& value) {
const auto known = base_state.find(key);
if (known != base_state.end() && known->second.record == value.first)
return; // The client already holds this value.
const bool due = known == base_state.end() || value.second <= near_radius ||
tick - known->second.updated >= far_interval;
if (!due) {
++deferred;
return;
}
const auto staleness = known == base_state.end()
? std::numeric_limits<std::uint64_t>::max()
: tick - known->second.updated;
candidates.push_back({value.second, staleness, key, &value.first});
};
if (shared != nullptr)
for (const auto& [key, value] : *shared)
consider(key, value);
for (const auto& [key, value] : own)
consider(key, value);
std::ranges::sort(candidates, [](const Candidate& a, const Candidate& b) {
return std::tie(a.distance, b.staleness, a.key) <
std::tie(b.distance, a.staleness, b.key);