Add M10.5b snapshot wiring: sessions, evo-server, client and probe
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m54s
Foundation / checks (client) (push) Successful in 12m40s
Foundation / checks (network) (push) Successful in 11m27s
Foundation / checks (tsan) (push) Successful in 8m6s
Foundation / checks (checks) (push) Successful in 31m54s
Foundation / checks (postgres) (push) Successful in 40m43s
Foundation / server-image (push) Successful in 25s
Foundation / postgres-tools (push) Successful in 1m2s
Foundation / image (push) Successful in 2m32s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m54s
Foundation / checks (client) (push) Successful in 12m40s
Foundation / checks (network) (push) Successful in 11m27s
Foundation / checks (tsan) (push) Successful in 8m6s
Foundation / checks (checks) (push) Successful in 31m54s
Foundation / checks (postgres) (push) Successful in 40m43s
Foundation / server-image (push) Successful in 25s
Foundation / postgres-tools (push) Successful in 1m2s
Foundation / image (push) Successful in 2m32s
RegionSessions owns the replication server: interest and acks per session, a host-set default interest, and per-tick snapshots sized to each connection's datagram limit. evo-server builds each interest's view from the region's own habitat sites (default: the first site, radius 7). RegionSessionClient assembles, acknowledges and presents snapshots and can change its interest; the probe prints them. Loopback and real-process evidence. ADR-0093. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -23,6 +23,8 @@ struct RegionSessionClient::Impl {
|
||||
std::vector<network::IntentionReceipt> intention_receipts;
|
||||
std::vector<network::ActionReceipt> action_receipts;
|
||||
std::vector<network::ActionOutcome> action_outcomes;
|
||||
std::optional<network::SnapshotAssembler> snapshots;
|
||||
std::uint64_t snapshot_chunks{}, snapshot_bytes{};
|
||||
static constexpr std::size_t max_kept = 1024; // Per kind, until taken; oldest dropped.
|
||||
|
||||
template <typename T> static void keep(std::vector<T>& list, const T& item) {
|
||||
@@ -44,6 +46,21 @@ struct RegionSessionClient::Impl {
|
||||
seen_tick(m->server_tick);
|
||||
return keep(intention_receipts, *m);
|
||||
}
|
||||
if (const auto* m = std::get_if<network::SnapshotChunk>(&*decoded)) {
|
||||
++snapshot_chunks;
|
||||
snapshot_bytes += bytes.size();
|
||||
seen_tick(m->tick);
|
||||
if (!snapshots)
|
||||
return; // Before Welcome (channels are not ordered): the next tick resends.
|
||||
const auto accepted = snapshots->accept(*m);
|
||||
if (!accepted)
|
||||
return end(SessionCloseReason::protocol, true); // Malformed snapshot.
|
||||
if (const auto ack = accepted.value_or(std::nullopt); ack && connection != 0)
|
||||
// A lost ack only means the next delta is against an older baseline.
|
||||
(void)endpoint->send_datagram(connection, network::encode_session(*ack))
|
||||
.has_value();
|
||||
return;
|
||||
}
|
||||
if (const auto* m = std::get_if<network::SimulatedThrough>(&*decoded)) {
|
||||
if (!welcome || m->epoch != welcome->epoch)
|
||||
return; // Another epoch's watermark says nothing about this session.
|
||||
@@ -80,6 +97,7 @@ struct RegionSessionClient::Impl {
|
||||
if (state == RegionSessionState::awaiting_welcome) {
|
||||
if (const auto* m = std::get_if<network::SessionWelcome>(&*decoded)) {
|
||||
welcome = *m;
|
||||
snapshots.emplace(world::WorldIdentity{m->world, m->frame}, m->epoch);
|
||||
seen_tick(m->server_tick);
|
||||
state = RegionSessionState::established;
|
||||
return;
|
||||
@@ -206,6 +224,40 @@ std::uint64_t RegionSessionClient::server_tick() const noexcept {
|
||||
return impl_->server_tick;
|
||||
}
|
||||
|
||||
bool RegionSessionClient::set_interest(const network::SessionInterest& interest) {
|
||||
auto& m = *impl_;
|
||||
return m.state == RegionSessionState::established && m.send(interest);
|
||||
}
|
||||
|
||||
const network::ReplicatedView* RegionSessionClient::view() const noexcept {
|
||||
const auto& snapshots = impl_->snapshots;
|
||||
return snapshots ? snapshots->latest() : nullptr;
|
||||
}
|
||||
|
||||
std::expected<client::EnvironmentPresentation, client::ViewError>
|
||||
RegionSessionClient::environment() const {
|
||||
const auto& snapshots = impl_->snapshots;
|
||||
if (!snapshots)
|
||||
return std::unexpected(client::ViewError::empty_slice);
|
||||
return snapshots->environment();
|
||||
}
|
||||
|
||||
std::expected<client::OrganismPresentation, client::ViewError>
|
||||
RegionSessionClient::organisms() const {
|
||||
const auto& snapshots = impl_->snapshots;
|
||||
if (!snapshots)
|
||||
return std::unexpected(client::ViewError::empty_slice);
|
||||
return snapshots->organisms();
|
||||
}
|
||||
|
||||
std::uint64_t RegionSessionClient::snapshot_chunks() const noexcept {
|
||||
return impl_->snapshot_chunks;
|
||||
}
|
||||
|
||||
std::uint64_t RegionSessionClient::snapshot_bytes() const noexcept {
|
||||
return impl_->snapshot_bytes;
|
||||
}
|
||||
|
||||
RegionSessionState RegionSessionClient::state() const noexcept {
|
||||
return impl_->state;
|
||||
}
|
||||
|
||||
@@ -190,16 +190,24 @@ std::vector<std::pair<std::uint64_t, std::vector<SnapshotChunk>>>
|
||||
ReplicationServer::tick(std::uint64_t tick,
|
||||
const std::function<ReplicationSource(const SessionInterest&)>& source,
|
||||
const Perceives& perceives, std::size_t payload) {
|
||||
return this->tick(tick, source, perceives, [payload](std::uint64_t) { return payload; });
|
||||
}
|
||||
|
||||
std::vector<std::pair<std::uint64_t, std::vector<SnapshotChunk>>> ReplicationServer::tick(
|
||||
std::uint64_t tick, const std::function<ReplicationSource(const SessionInterest&)>& source,
|
||||
const Perceives& perceives, const std::function<std::size_t(std::uint64_t)>& payload_of) {
|
||||
std::vector<std::pair<std::uint64_t, std::vector<SnapshotChunk>>> out;
|
||||
payload = std::min(payload, session_datagram_bytes);
|
||||
const auto overhead = session_header_bytes + snapshot_chunk_header_bytes;
|
||||
if (tick == 0 || payload <= overhead)
|
||||
if (tick == 0)
|
||||
return out;
|
||||
const auto capacity = payload - overhead; // Record bytes per chunk.
|
||||
std::vector<std::pair<SessionInterest, ReplicationSource>> sources;
|
||||
for (auto& s : sessions_) {
|
||||
if (!s.interest || (!s.baselines.empty() && s.baselines.back().tick >= tick))
|
||||
continue;
|
||||
const auto payload = std::min(payload_of(s.id), session_datagram_bytes);
|
||||
if (payload <= overhead)
|
||||
continue; // No datagram channel yet (or a path too small): nothing to send.
|
||||
const auto capacity = payload - overhead; // Record bytes per chunk.
|
||||
const auto& in = *s.interest;
|
||||
auto cached = std::ranges::find_if(sources, [&](const auto& e) { return e.first == in; });
|
||||
if (cached == sources.end()) {
|
||||
|
||||
@@ -62,15 +62,18 @@ struct RegionSessions::Impl {
|
||||
};
|
||||
|
||||
Impl(RegionSessionsConfig c, std::unique_ptr<network::quic::Endpoint> e,
|
||||
network::GrantVerifier v, network::SessionControl s, network::IntentAdmission i)
|
||||
network::GrantVerifier v, network::SessionControl s, network::IntentAdmission i,
|
||||
network::ReplicationServer r)
|
||||
: config{std::move(c)}, endpoint{std::move(e)}, verifier{std::move(v)},
|
||||
control{std::move(s)}, intents{std::move(i)} {}
|
||||
control{std::move(s)}, intents{std::move(i)}, replication{std::move(r)} {}
|
||||
|
||||
RegionSessionsConfig config;
|
||||
std::unique_ptr<network::quic::Endpoint> endpoint;
|
||||
network::GrantVerifier verifier;
|
||||
network::SessionControl control;
|
||||
network::IntentAdmission intents;
|
||||
network::ReplicationServer replication;
|
||||
std::optional<network::SessionInterest> default_interest;
|
||||
HostFacts facts;
|
||||
std::vector<Connection> connections;
|
||||
std::vector<RegionSessionEvent> events;
|
||||
@@ -110,8 +113,10 @@ struct RegionSessions::Impl {
|
||||
event.account = c.account;
|
||||
event.reason = reason;
|
||||
events.push_back(std::move(event));
|
||||
if (c.state == State::established)
|
||||
if (c.state == State::established) {
|
||||
intents.close(c.session);
|
||||
replication.close(c.session);
|
||||
}
|
||||
if (c.session != 0) // May already be released (superseded, expired, epoch).
|
||||
(void)control.close(c.session).has_value();
|
||||
// A superseded session's durable control already moved to its successor.
|
||||
@@ -183,6 +188,12 @@ struct RegionSessions::Impl {
|
||||
void welcome(Connection& c, Clock::time_point now) {
|
||||
if (!intents.open(c.session, c.account_id))
|
||||
return close(c, SessionCloseReason::capacity, now);
|
||||
if (!replication.open(c.session, c.welcome.epoch)) {
|
||||
intents.close(c.session);
|
||||
return close(c, SessionCloseReason::capacity, now);
|
||||
}
|
||||
if (default_interest)
|
||||
(void)replication.set_interest(c.session, *default_interest).has_value();
|
||||
c.state = State::established;
|
||||
c.welcome.server_tick = facts.simulated_tick;
|
||||
send(c, c.welcome);
|
||||
@@ -244,8 +255,12 @@ struct RegionSessions::Impl {
|
||||
if (c.state == State::closing)
|
||||
return;
|
||||
const auto decoded = network::decode_session(bytes);
|
||||
const auto* m = decoded ? std::get_if<network::SessionIntention>(&*decoded) : nullptr;
|
||||
if (c.state != State::established || !m)
|
||||
if (c.state != State::established || !decoded)
|
||||
return close(c, SessionCloseReason::protocol, now);
|
||||
if (const auto* ack = std::get_if<network::SnapshotAck>(&*decoded))
|
||||
return replication.acknowledge(c.session, *ack);
|
||||
const auto* m = std::get_if<network::SessionIntention>(&*decoded);
|
||||
if (!m)
|
||||
return close(c, SessionCloseReason::protocol, now);
|
||||
send_datagram(c, intents.intention(c.session, *m, admission()));
|
||||
}
|
||||
@@ -290,6 +305,10 @@ struct RegionSessions::Impl {
|
||||
}
|
||||
if (const auto* m = std::get_if<network::ActionQuery>(&*decoded))
|
||||
return send(c, intents.query(c.session, *m, admission()));
|
||||
if (const auto* m = std::get_if<network::SessionInterest>(&*decoded)) {
|
||||
(void)replication.set_interest(c.session, *m).has_value(); // Clamped by the server.
|
||||
return;
|
||||
}
|
||||
if (const auto* m = std::get_if<network::SessionClose>(&*decoded);
|
||||
m && m->reason == SessionCloseReason::client_closed) {
|
||||
close(c, SessionCloseReason::client_closed, now);
|
||||
@@ -324,13 +343,17 @@ RegionSessions::create(RegionSessionsConfig config) {
|
||||
auto intents = network::IntentAdmission::create(config.intents);
|
||||
if (!intents)
|
||||
return std::unexpected(RegionSessionsError::configuration);
|
||||
config.replication.max_sessions = config.control.max_sessions;
|
||||
auto replication = network::ReplicationServer::create(config.replication);
|
||||
if (!replication)
|
||||
return std::unexpected(RegionSessionsError::configuration);
|
||||
intents->attach(config.actions);
|
||||
auto endpoint = network::quic::Endpoint::serve(config.transport);
|
||||
if (!endpoint)
|
||||
return std::unexpected(RegionSessionsError::transport);
|
||||
return std::unique_ptr<RegionSessions>{new RegionSessions{
|
||||
std::make_unique<Impl>(std::move(config), std::move(*endpoint), std::move(*verifier),
|
||||
std::move(*control), std::move(*intents))}};
|
||||
std::move(*control), std::move(*intents), std::move(*replication))}};
|
||||
}
|
||||
|
||||
void RegionSessions::poll(const HostFacts& facts, std::uint64_t now_seconds,
|
||||
@@ -362,8 +385,10 @@ void RegionSessions::poll(const HostFacts& facts, std::uint64_t now_seconds,
|
||||
m.connections, [&](const Impl::Connection& c) { return c.id == e.connection; });
|
||||
if (it == m.connections.end())
|
||||
continue;
|
||||
if (it->state == Impl::State::established)
|
||||
if (it->state == Impl::State::established) {
|
||||
m.intents.close(it->session);
|
||||
m.replication.close(it->session);
|
||||
}
|
||||
if (it->state == Impl::State::pending) {
|
||||
(void)m.control.close(it->session).has_value();
|
||||
if (m.config.ledger && it->generation != 0)
|
||||
@@ -417,6 +442,33 @@ void RegionSessions::dropped(std::span<const network::CarriedAction> actions, bo
|
||||
impl_->deliver(impl_->intents.dropped(actions, final));
|
||||
}
|
||||
|
||||
void RegionSessions::set_default_interest(network::SessionInterest interest) {
|
||||
auto& m = *impl_;
|
||||
m.default_interest = interest;
|
||||
for (const auto& c : m.connections)
|
||||
if (c.state == Impl::State::established && !m.replication.interest(c.session))
|
||||
(void)m.replication.set_interest(c.session, interest).has_value();
|
||||
}
|
||||
|
||||
void RegionSessions::replicate(
|
||||
std::uint64_t tick,
|
||||
const std::function<network::ReplicationSource(const network::SessionInterest&)>& source,
|
||||
const network::Perceives& perceives) {
|
||||
auto& m = *impl_;
|
||||
const auto payload = [&](std::uint64_t session) -> std::size_t {
|
||||
const auto* c = m.by_session(session);
|
||||
return c && c->state == Impl::State::established ? m.endpoint->datagram_limit(c->id) : 0;
|
||||
};
|
||||
for (const auto& [session, chunks] : m.replication.tick(tick, source, perceives, payload))
|
||||
if (const auto* c = m.by_session(session); c && c->state == Impl::State::established)
|
||||
for (const auto& chunk : chunks)
|
||||
m.send_datagram(*c, chunk); // A refused chunk leaves the snapshot unacknowledged.
|
||||
}
|
||||
|
||||
network::ReplicationStats RegionSessions::replication_stats() const {
|
||||
return impl_->replication.stats();
|
||||
}
|
||||
|
||||
void RegionSessions::shutdown(Clock::time_point now) {
|
||||
for (auto& c : impl_->connections)
|
||||
impl_->close(c, SessionCloseReason::shutdown, now);
|
||||
|
||||
Reference in New Issue
Block a user