Add owner-filtered cellular perception protocol
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (checks) (push) Failing after 39s
Foundation / checks (postgres) (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 0s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (ecs) (push) Canceled after 1m48s
Foundation / checks (network) (push) Canceled after 1m18s
Foundation / checks (client) (push) Canceled after 1m58s

This commit is contained in:
dingo
2026-10-11 01:06:05 -04:00
parent 10b042309e
commit 5419a579e8
7 changed files with 209 additions and 10 deletions
+78 -7
View File
@@ -13,6 +13,8 @@ constexpr std::size_t close_bytes = 1;
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_perception_prefix_bytes = 8 + 8 + 2;
constexpr std::size_t cellular_perceived_patch_bytes = 8 + 8 + (4 * 8);
constexpr std::size_t intention_receipt_bytes = 8 + 1 + 8;
constexpr std::size_t simulated_bytes = std::size_t{3} * 8;
constexpr std::size_t action_bytes = (std::size_t{3} * 8) + 2 + 8;
@@ -104,7 +106,8 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message) {
std::holds_alternative<PrimitiveLifeReceipt>(message) ||
std::holds_alternative<PrimitiveLifeOutcome>(message) ||
std::holds_alternative<PrimitiveLifeQuery>(message);
const auto version = std::holds_alternative<CellularAction>(message) ? session_protocol_v7
const auto version = std::holds_alternative<CellularPerception>(message) ? session_protocol_v8
: std::holds_alternative<CellularAction>(message) ? session_protocol_v7
: std::holds_alternative<CellularIntention>(message) ? session_protocol_v6
: is_life ? session_protocol_v5 : session_protocol_version;
return encode_session(message, version);
@@ -112,7 +115,8 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message) {
std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uint16_t version) {
if (version != session_protocol_version && version != session_protocol_v5 &&
version != session_protocol_v6 && version != session_protocol_v7)
version != session_protocol_v6 && version != session_protocol_v7 &&
version != session_protocol_v8)
return {};
if (version == session_protocol_version &&
(std::holds_alternative<PrimitiveLifeRequest>(message) ||
@@ -122,11 +126,27 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uin
return {};
if (version != session_protocol_v6 && std::holds_alternative<CellularIntention>(message))
return {};
if (version != session_protocol_v7 && std::holds_alternative<CellularAction>(message))
if (version != session_protocol_v7 && version != session_protocol_v8 &&
std::holds_alternative<CellularAction>(message))
return {};
if (version != session_protocol_v7 && std::holds_alternative<ActionOutcome>(message) &&
if (version != session_protocol_v7 && version != session_protocol_v8 &&
std::holds_alternative<ActionOutcome>(message) &&
std::get<ActionOutcome>(message).result == ActionResult::committed)
return {};
if (version != session_protocol_v8 && std::holds_alternative<CellularPerception>(message))
return {};
if (const auto* perception = std::get_if<CellularPerception>(&message)) {
if (!perception->tick || perception->patches.size() > max_cellular_perceived_patches ||
(!perception->life_identity && !perception->patches.empty()) ||
!std::ranges::is_sorted(perception->patches, [](const auto& a, const auto& b) {
return std::pair{a.x_um, a.y_um} < std::pair{b.x_um, b.y_um};
}) ||
std::adjacent_find(perception->patches.begin(), perception->patches.end(),
[](const auto& a, const auto& b) {
return a.x_um == b.x_um && a.y_um == b.y_um;
}) != perception->patches.end())
return {};
}
struct Encoder {
std::uint16_t version;
std::vector<std::uint8_t> operator()(const SessionHello& m) const {
@@ -191,6 +211,23 @@ std::vector<std::uint8_t> encode_session(const SessionMessage& message, std::uin
put(out, m.intake_y_um, 4);
return out;
}
std::vector<std::uint8_t> operator()(const CellularPerception& m) const {
auto out = header(SessionMessageType::cellular_perception,
cellular_perception_prefix_bytes +
m.patches.size() * cellular_perceived_patch_bytes,
version);
put(out, m.tick, 8);
put(out, m.life_identity, 8);
put(out, m.patches.size(), 2);
for (const auto& patch : m.patches) {
put(out, patch.x_um, 4);
put(out, patch.y_um, 4);
put(out, patch.energy_pj, 8);
for (const auto matter : patch.matter_fg)
put(out, matter, 8);
}
return out;
}
std::vector<std::uint8_t> operator()(const IntentionReceipt& m) const {
auto out =
header(SessionMessageType::intention_receipt, intention_receipt_bytes, version);
@@ -322,7 +359,8 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
return std::unexpected(SessionCodecError::bad_magic);
const auto wire_version = static_cast<std::uint16_t>(get(bytes, 4, 2));
if (wire_version != session_protocol_version && wire_version != session_protocol_v5 &&
wire_version != session_protocol_v6 && wire_version != session_protocol_v7)
wire_version != session_protocol_v6 && wire_version != session_protocol_v7 &&
wire_version != session_protocol_v8)
return std::unexpected(SessionCodecError::unsupported_version);
if (version)
*version = wire_version;
@@ -405,7 +443,7 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
return m;
}
case static_cast<std::uint8_t>(SessionMessageType::cellular_action): {
if (wire_version != session_protocol_v7)
if (wire_version != session_protocol_v7 && wire_version != session_protocol_v8)
return std::unexpected(SessionCodecError::unsupported_version);
if (!expect(cellular_action_bytes))
return std::unexpected(SessionCodecError::wrong_length);
@@ -426,6 +464,39 @@ 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_perception): {
if (wire_version != session_protocol_v8)
return std::unexpected(SessionCodecError::unsupported_version);
if (payload.size() < cellular_perception_prefix_bytes)
return std::unexpected(SessionCodecError::wrong_length);
const auto count = static_cast<std::size_t>(get(payload, 16, 2));
if (count > max_cellular_perceived_patches ||
payload.size() != cellular_perception_prefix_bytes +
count * cellular_perceived_patch_bytes)
return std::unexpected(SessionCodecError::wrong_length);
CellularPerception m;
m.tick = get(payload, 0, 8);
m.life_identity = get(payload, 8, 8);
if (!m.tick || (!m.life_identity && count))
return std::unexpected(SessionCodecError::invalid_value);
m.patches.reserve(count);
std::size_t offset = cellular_perception_prefix_bytes;
for (std::size_t i = 0; i < count; ++i) {
CellularPerceivedPatch patch;
patch.x_um = static_cast<std::uint32_t>(get(payload, offset, 4));
patch.y_um = static_cast<std::uint32_t>(get(payload, offset + 4, 4));
patch.energy_pj = get(payload, offset + 8, 8);
for (std::size_t channel = 0; channel < patch.matter_fg.size(); ++channel)
patch.matter_fg[channel] = get(payload, offset + 16 + channel * 8, 8);
if (!m.patches.empty() &&
!(std::pair{m.patches.back().x_um, m.patches.back().y_um} <
std::pair{patch.x_um, patch.y_um}))
return std::unexpected(SessionCodecError::invalid_value);
m.patches.push_back(patch);
offset += cellular_perceived_patch_bytes;
}
return m;
}
case static_cast<std::uint8_t>(SessionMessageType::intention_receipt): {
if (!expect(intention_receipt_bytes))
return std::unexpected(SessionCodecError::wrong_length);
@@ -464,7 +535,7 @@ std::expected<SessionMessage, SessionCodecError> decode_session(std::span<const
if (payload[8] < static_cast<std::uint8_t>(ActionResult::committed_no_effect) ||
payload[8] > static_cast<std::uint8_t>(ActionResult::committed) ||
(payload[8] == static_cast<std::uint8_t>(ActionResult::committed) &&
wire_version != session_protocol_v7))
wire_version != session_protocol_v7 && wire_version != session_protocol_v8))
return std::unexpected(SessionCodecError::invalid_value);
const ActionOutcome m{get(payload, 0, 8), static_cast<ActionResult>(payload[8]),
get(payload, 9, 8), get(payload, 17, 8)};