Send the owner its outstanding mutation offer over v10

Session protocol v10 adds type 26: the life's earned offer with its unranked
candidates in operator order and each successor's capability value,
maintenance and total mass. The cellular bridge sends it when the offer or the
owner's session changes and clears a consumed offer; the client keeps the
latest one for the maintainer's choice UI (ADR-0121, 11.4d server half).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-13 23:07:50 -04:00
co-authored by Claude Opus 5.5
parent 187b1a7b98
commit f77956e579
16 changed files with 314 additions and 17 deletions
+14
View File
@@ -29,6 +29,7 @@ struct RegionSessionClient::Impl {
std::vector<network::PrimitiveLifeOutcome> primitive_life_outcomes;
std::optional<network::SnapshotAssembler> snapshots;
std::optional<network::CellularPerception> cellular_perception;
std::optional<network::CellularMutationOffer> mutation_offer;
std::uint64_t snapshot_chunks{}, snapshot_bytes{};
std::optional<network::HandoffOffer> handoff; // v4: continue at another region.
std::optional<network::RegionAddress> redirect; // v4: the probe is owned elsewhere.
@@ -146,6 +147,14 @@ struct RegionSessionClient::Impl {
cellular_perception = *m;
return;
}
if (const auto* m = std::get_if<network::CellularMutationOffer>(&*decoded)) {
seen_tick(m->tick);
if (m->candidates.empty())
mutation_offer.reset();
else
mutation_offer = *m;
return;
}
if (const auto* m = std::get_if<network::PrimitiveLifeReceipt>(&*decoded)) {
seen_tick(m->server_tick);
return keep(primitive_life_receipts, *m);
@@ -381,6 +390,11 @@ RegionSessionClient::cellular_perception() const noexcept {
return impl_->cellular_perception;
}
const std::optional<network::CellularMutationOffer>&
RegionSessionClient::mutation_offer() const noexcept {
return impl_->mutation_offer;
}
std::expected<client::EnvironmentPresentation, client::ViewError>
RegionSessionClient::environment() const {
const auto& snapshots = impl_->snapshots;
+70
View File
@@ -14,6 +14,8 @@ constexpr std::size_t intention_bytes = (std::size_t{4} * 8) + 4;
constexpr std::size_t cellular_intention_bytes = (std::size_t{4} * 8) + 4 + 1 + 3 + 8;
constexpr std::size_t cellular_action_bytes = (std::size_t{4} * 8) + 4 + 1 + 3 + 8;
constexpr std::size_t cellular_mutation_action_bytes = (std::size_t{7} * 8) + 2 + 6;
constexpr std::size_t cellular_offer_prefix_bytes = (std::size_t{4} * 8) + 2 + 6;
constexpr std::size_t cellular_offered_candidate_bytes = 2 + 1 + 1 + 4 + 8 + 8;
constexpr std::size_t cellular_perception_prefix_bytes = 8 + 8 + 2;
constexpr std::size_t cellular_diagnostics_bytes = 64;
constexpr std::size_t cellular_perception_v9_prefix_bytes =
@@ -37,6 +39,22 @@ constexpr std::size_t life_receipt_bytes = 17;
constexpr std::size_t life_outcome_bytes = 25;
constexpr std::size_t life_query_bytes = 8;
// Seven capabilities and four effects (biology genome/mutation enums, frozen on the wire).
bool valid_offer(const CellularMutationOffer& offer) {
if (offer.tick == 0 || offer.candidates.size() > max_cellular_offered_candidates)
return false;
if (offer.candidates.empty())
return offer.event_index == 0 && offer.offer_tick == 0;
std::uint16_t previous = 0;
for (const auto& candidate : offer.candidates) {
if (candidate.operator_id <= previous || candidate.capability > 6 || candidate.effect > 3)
return false;
previous = candidate.operator_id;
}
return offer.life_identity != 0 && offer.event_index != 0 && offer.offer_tick != 0 &&
offer.offer_tick <= offer.tick;
}
bool valid_diagnostics(const CellularLifeDiagnostics& diagnostics) {
const auto movement = static_cast<std::uint8_t>(diagnostics.movement);
const auto intake = static_cast<std::uint8_t>(diagnostics.intake);
@@ -130,6 +148,7 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message) {
: session_protocol_v8)
: std::holds_alternative<CellularAction>(message) ? session_protocol_v7
: std::holds_alternative<CellularMutationAction>(message) ? session_protocol_v10
: std::holds_alternative<CellularMutationOffer>(message) ? session_protocol_v10
: std::holds_alternative<CellularIntention>(message) ? session_protocol_v6
: is_life ? session_protocol_v5
: session_protocol_version;
@@ -161,6 +180,9 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uin
return {};
if (version != session_protocol_v10 && std::holds_alternative<CellularMutationAction>(message))
return {};
if (const auto* offer = std::get_if<CellularMutationOffer>(&message);
offer && (version != session_protocol_v10 || !valid_offer(*offer)))
return {};
if (const auto* action = std::get_if<CellularAction>(&message);
action && action->engulf &&
(version != session_protocol_v10 || action->has_intake_target || action->intake_x_um ||
@@ -262,6 +284,25 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uin
put(out, 0, 6);
return out;
}
std::vector<std::uint8_t> operator()(const CellularMutationOffer& m) const {
auto out = header(SessionMessageType::cellular_mutation_offer,
cellular_offer_prefix_bytes +
m.candidates.size() * cellular_offered_candidate_bytes,
version);
for (const auto value : {m.tick, m.life_identity, m.event_index, m.offer_tick})
put(out, value, 8);
put(out, m.candidates.size(), 2);
put(out, 0, 6);
for (const auto& candidate : m.candidates) {
put(out, candidate.operator_id, 2);
out.push_back(candidate.capability);
out.push_back(candidate.effect);
put(out, candidate.parameter, 4);
put(out, candidate.maintenance, 8);
put(out, candidate.total_mass, 8);
}
return out;
}
std::vector<std::uint8_t> operator()(const CellularPerception& m) const {
auto out = header(SessionMessageType::cellular_perception,
(version == session_protocol_v9 || version == session_protocol_v10
@@ -557,6 +598,35 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
return std::unexpected(SessionCodecError::invalid_value);
return m;
}
case static_cast<std::uint8_t>(SessionMessageType::cellular_mutation_offer): {
if (wire_version != session_protocol_v10)
return std::unexpected(SessionCodecError::unsupported_version);
if (payload.size() < cellular_offer_prefix_bytes)
return std::unexpected(SessionCodecError::wrong_length);
const auto count = static_cast<std::size_t>(get(payload, 32, 2));
if (count > max_cellular_offered_candidates ||
payload.size() !=
cellular_offer_prefix_bytes + count * cellular_offered_candidate_bytes)
return std::unexpected(SessionCodecError::wrong_length);
CellularMutationOffer m;
m.tick = get(payload, 0, 8);
m.life_identity = get(payload, 8, 8);
m.event_index = get(payload, 16, 8);
m.offer_tick = get(payload, 24, 8);
if (std::ranges::any_of(payload.subspan(34, 6),
[](std::uint8_t value) { return value != 0; }))
return std::unexpected(SessionCodecError::invalid_value);
for (std::size_t i = 0; i < count; ++i) {
const auto at = cellular_offer_prefix_bytes + i * cellular_offered_candidate_bytes;
m.candidates.push_back({static_cast<std::uint16_t>(get(payload, at, 2)),
payload[at + 2], payload[at + 3],
static_cast<std::uint32_t>(get(payload, at + 4, 4)),
get(payload, at + 8, 8), get(payload, at + 16, 8)});
}
if (!valid_offer(m))
return std::unexpected(SessionCodecError::invalid_value);
return m;
}
case static_cast<std::uint8_t>(SessionMessageType::cellular_perception): {
if (wire_version != session_protocol_v8 && wire_version != session_protocol_v9 &&
wire_version != session_protocol_v10)
+17
View File
@@ -102,6 +102,23 @@ network::ActionOutcome action_outcome(const FinalCellularMutation& mutation) noe
mutation.tick, mutation.commit_sequence};
}
network::CellularMutationOffer offer_view(const CellularOwnedLife& life, std::uint64_t tick) {
network::CellularMutationOffer view{tick, life.identity.life, 0, 0, {}};
const auto& offer = life.mutation.offer;
if (!offer)
return view;
view.event_index = life.mutation.event_index;
view.offer_tick = life.mutation.offer_tick;
for (const auto& candidate : offer->candidates)
if (view.candidates.size() < network::max_cellular_offered_candidates)
view.candidates.push_back(
{candidate.operator_id, static_cast<std::uint8_t>(candidate.target),
static_cast<std::uint8_t>(candidate.effect), candidate.parameter,
candidate.maintenance, candidate.total_mass});
std::ranges::sort(view.candidates, {}, &network::CellularOfferedCandidate::operator_id);
return view;
}
CellularRegionHost::CellularRegionHost(CellularHostConfig config, CellularConnect connect,
std::unique_ptr<persistence::FilesystemObjectStore> objects)
: config_{std::move(config)}, connect_{std::move(connect)}, objects_{std::move(objects)},
+18 -9
View File
@@ -148,6 +148,18 @@ struct RegionSessions::Impl {
(void)endpoint->send_message(c.id, network::encode_session(m, c.protocol_version))
.has_value();
}
// Reliable owner-only v10 messages, to the account's established v10 session.
bool send_to_v10_owner(const network::AccountId& account,
const network::SessionMessage& message) {
for (const auto& c : connections) {
if (c.state != State::established ||
c.protocol_version != network::session_protocol_v10 || c.account_id != account)
continue;
const auto bytes = network::encode_session(message, c.protocol_version);
return !bytes.empty() && endpoint->send_message(c.id, bytes).has_value();
}
return false;
}
// Realtime answers: a refused datagram is dropped; cumulative watermarks cover it.
void send_datagram(const Connection& c, const network::SessionMessage& m) {
(void)endpoint->send_datagram(c.id, network::encode_session(m, c.protocol_version))
@@ -937,15 +949,12 @@ bool RegionSessions::send_cellular_perception(const network::AccountId& account,
bool RegionSessions::send_mutation_outcome(const network::AccountId& account,
const network::ActionOutcome& outcome) {
auto& m = *impl_;
for (const auto& c : m.connections) {
if (c.state != Impl::State::established ||
c.protocol_version != network::session_protocol_v10 || c.account_id != account)
continue;
return m.endpoint->send_message(c.id, network::encode_session(outcome, c.protocol_version))
.has_value();
}
return false;
return impl_->send_to_v10_owner(account, outcome);
}
bool RegionSessions::send_mutation_offer(const network::AccountId& account,
const network::CellularMutationOffer& offer) {
return impl_->send_to_v10_owner(account, offer);
}
void RegionSessions::clear_cellular_perceptions(std::uint64_t tick) {