Foundation / checks (client) (push) Successful in 5s
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 5s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m32s
Foundation / postgres-tools (push) Successful in 28s
Foundation / server-image (push) Successful in 4m21s
Foundation / image (push) Successful in 5m1s
Playtest: after one mutation every later choice came back "split refused"
because a split needs a grown cell, and the client gave no reason. The v14
body block now carries the body's growth per thousand (the reserved bytes)
and a split-ready flag; the client disables Stay / Become until the cell can
split ("Grow to split (now 62%)") and shows growth in the Body panel.
Adhesion and organism choices do not split and stay open.
The DEV revamp tuning splits once half grown (split_min_growth 500, was full
growth); it takes effect in the next region opened.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
747 lines
37 KiB
C++
747 lines
37 KiB
C++
#include "evo/network/session_protocol.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <limits>
|
|
#include <source_location>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
namespace {
|
|
using namespace evo::network;
|
|
void check(bool value, std::source_location at = std::source_location::current()) {
|
|
if (!value)
|
|
throw std::runtime_error("cellular intent protocol line " + std::to_string(at.line()));
|
|
}
|
|
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
|
|
|
void round_trip_and_version_gate() {
|
|
const CellularIntention targeted{17, 23, 29, 31, -707, 707, true, 4'000, 5'000};
|
|
const SessionMessage message{targeted};
|
|
const auto bytes = encode_session(message);
|
|
CHECK(!bytes.empty() && bytes.size() == session_header_bytes + 48);
|
|
std::uint16_t version{};
|
|
const auto decoded = decode_session(bytes, &version);
|
|
CHECK(decoded && version == session_protocol_v6);
|
|
CHECK(std::get<CellularIntention>(*decoded) == targeted);
|
|
CHECK(encode_session(*decoded) == bytes);
|
|
CHECK(encode_session(message, session_protocol_v5).empty());
|
|
|
|
const CellularIntention no_target{1, 2, 3, 4, 0, -1, false, 0, 0};
|
|
const auto no_target_bytes = encode_session(SessionMessage{no_target});
|
|
CHECK(std::get<CellularIntention>(*decode_session(no_target_bytes)) == no_target);
|
|
}
|
|
|
|
void malformed_frames_are_refused() {
|
|
const auto bytes =
|
|
encode_session(SessionMessage{CellularIntention{1, 2, 3, 4, 0, 0, true, 1'000, 2'000}});
|
|
for (std::size_t size = 0; size < bytes.size(); ++size) {
|
|
const auto truncated = decode_session(std::span{bytes}.first(size));
|
|
CHECK(!truncated);
|
|
CHECK(truncated.error() == (size < session_header_bytes ? SessionCodecError::truncated
|
|
: SessionCodecError::wrong_length));
|
|
}
|
|
auto bad = bytes;
|
|
bad.at(6) = 255; // Unknown message type.
|
|
const auto wrong_type = decode_session(bad);
|
|
CHECK(!wrong_type && wrong_type.error() == SessionCodecError::unknown_type);
|
|
|
|
bad = bytes;
|
|
bad.at(session_header_bytes + 36) = 2;
|
|
const auto bad_flag = decode_session(bad);
|
|
CHECK(!bad_flag && bad_flag.error() == SessionCodecError::invalid_value);
|
|
|
|
bad = bytes;
|
|
bad.at(session_header_bytes + 38) = 1;
|
|
const auto bad_reserved = decode_session(bad);
|
|
CHECK(!bad_reserved && bad_reserved.error() == SessionCodecError::invalid_value);
|
|
|
|
bad = encode_session(SessionMessage{CellularIntention{1, 2, 3, 4, 0, 0, false, 0, 0}});
|
|
bad.at(session_header_bytes + 40) = 1; // Coordinates must be zero when target is absent.
|
|
const auto noncanonical = decode_session(bad);
|
|
CHECK(!noncanonical && noncanonical.error() == SessionCodecError::invalid_value);
|
|
|
|
bad = bytes;
|
|
bad.at(4) = static_cast<std::uint8_t>(session_protocol_v5);
|
|
const auto old_version = decode_session(bad);
|
|
CHECK(!old_version && old_version.error() == SessionCodecError::unsupported_version);
|
|
CHECK(encode_session(SessionMessage{CellularIntention{1, 2, 3, 4}}, 7).empty());
|
|
}
|
|
|
|
void v7_reliable_action_is_stable_and_strict() {
|
|
const CellularAction action{17, 23, 29, 31, -707, 707, true, 4'000, 5'000};
|
|
const auto bytes = encode_session(SessionMessage{action});
|
|
CHECK(bytes.size() == session_header_bytes + 48);
|
|
std::uint16_t version{};
|
|
const auto decoded = decode_session(bytes, &version);
|
|
CHECK(decoded && version == session_protocol_v7);
|
|
CHECK(std::get<CellularAction>(*decoded) == action);
|
|
CHECK(encode_session(*decoded) == bytes);
|
|
CHECK(encode_session(SessionMessage{action}, session_protocol_v6).empty());
|
|
|
|
const CellularActionInput input{31, -707, 707, true, 4'000, 5'000};
|
|
const auto canonical = encode_cellular_action_input(input);
|
|
CHECK(canonical.size() == cellular_action_input_bytes);
|
|
CHECK(decode_cellular_action_input(canonical) == input);
|
|
const CellularAction retry{99, 101, 29, 31, -707, 707, true, 4'000, 5'000};
|
|
CHECK(encode_cellular_action_input({retry.tick, retry.move_x, retry.move_y,
|
|
retry.has_intake_target, retry.intake_x_um,
|
|
retry.intake_y_um}) == canonical);
|
|
|
|
const auto outcome = ActionOutcome{29, ActionResult::committed, 31, 41};
|
|
const auto outcome_bytes = encode_session(SessionMessage{outcome}, session_protocol_v7);
|
|
CHECK(std::get<ActionOutcome>(*decode_session(outcome_bytes)) == outcome);
|
|
CHECK(encode_session(SessionMessage{outcome}, session_protocol_v6).empty());
|
|
|
|
auto bad = bytes;
|
|
std::fill(bad.begin() + static_cast<std::ptrdiff_t>(session_header_bytes + 16),
|
|
bad.begin() + static_cast<std::ptrdiff_t>(session_header_bytes + 24),
|
|
std::uint8_t{0});
|
|
CHECK(!decode_session(bad)); // Stable action id cannot be zero.
|
|
bad = bytes;
|
|
bad[session_header_bytes + 37] = 1;
|
|
CHECK(!decode_session(bad)); // Reserved bytes are canonical.
|
|
bad = encode_session(SessionMessage{CellularAction{1, 2, 3, 4, 0, 0, false, 0, 0}});
|
|
bad[session_header_bytes + 44] = 1;
|
|
CHECK(!decode_session(bad)); // An absent patch has no coordinates.
|
|
|
|
auto malformed_input = canonical;
|
|
malformed_input[12] = 2;
|
|
CHECK(!decode_cellular_action_input(malformed_input));
|
|
// Byte 13 is a flags byte (schema 58): bit 0 brake, every other bit refused.
|
|
malformed_input = canonical;
|
|
malformed_input[13] = 2;
|
|
CHECK(!decode_cellular_action_input(malformed_input));
|
|
auto braking_input = canonical;
|
|
braking_input[13] = 1;
|
|
const auto braking = decode_cellular_action_input(braking_input);
|
|
CHECK(braking && braking->brake && encode_cellular_action_input(*braking) == braking_input);
|
|
malformed_input = canonical;
|
|
// The canonical biological input itself requires a nonzero tick.
|
|
std::fill(malformed_input.begin(), malformed_input.begin() + 8, std::uint8_t{0});
|
|
CHECK(!decode_cellular_action_input(malformed_input));
|
|
}
|
|
|
|
// ADR-0125: target kind 3 (bud) exists only on v10, without coordinates, exclusive with
|
|
// a patch target and with engulfment.
|
|
void v10_bud_action_is_versioned_and_exclusive() {
|
|
CellularAction bud{17, 23, 29, 31, 0, 0, false, 0, 0};
|
|
bud.bud = true;
|
|
const auto bytes = encode_session(SessionMessage{bud}, session_protocol_v10);
|
|
CHECK(bytes.size() == session_header_bytes + 48 && bytes[session_header_bytes + 36] == 3);
|
|
const auto decoded = decode_session(bytes);
|
|
CHECK(decoded && std::get<CellularAction>(*decoded) == bud);
|
|
CHECK(encode_session(SessionMessage{bud}, session_protocol_v9).empty());
|
|
CHECK(encode_session(SessionMessage{bud}).empty());
|
|
auto feeding = bud;
|
|
feeding.has_intake_target = true;
|
|
CHECK(encode_session(SessionMessage{feeding}, session_protocol_v10).empty());
|
|
auto engulfing = bud;
|
|
engulfing.engulf = true;
|
|
CHECK(encode_session(SessionMessage{engulfing}, session_protocol_v10).empty());
|
|
auto as_v9 = bytes;
|
|
as_v9[4] = static_cast<std::uint8_t>(session_protocol_v9);
|
|
CHECK(!decode_session(as_v9));
|
|
auto kind_four = bytes;
|
|
kind_four[session_header_bytes + 36] = 4;
|
|
CHECK(!decode_session(kind_four));
|
|
|
|
CellularActionInput input{31, 0, 0, false, 0, 0, false, true};
|
|
const auto canonical = encode_cellular_action_input(input);
|
|
CHECK(canonical[12] == 3 && decode_cellular_action_input(canonical) == input);
|
|
auto placed = canonical;
|
|
placed[16] = 1;
|
|
CHECK(!decode_cellular_action_input(placed));
|
|
}
|
|
|
|
// ADR-0124: target kind 2 (engulf) exists only on v10, without coordinates or a patch.
|
|
void v10_engulf_action_is_versioned_and_exclusive() {
|
|
CellularAction engulf{17, 23, 29, 31, 5, -5, false, 0, 0};
|
|
engulf.engulf = true;
|
|
const auto bytes = encode_session(SessionMessage{engulf}, session_protocol_v10);
|
|
CHECK(bytes.size() == session_header_bytes + 48 && bytes[session_header_bytes + 36] == 2);
|
|
std::uint16_t version{};
|
|
const auto decoded = decode_session(bytes, &version);
|
|
CHECK(decoded && version == session_protocol_v10 &&
|
|
std::get<CellularAction>(*decoded) == engulf);
|
|
CHECK(encode_session(SessionMessage{engulf}, session_protocol_v9).empty());
|
|
CHECK(encode_session(SessionMessage{engulf}).empty()); // The default v7 has no engulf.
|
|
auto with_patch = engulf;
|
|
with_patch.has_intake_target = true;
|
|
CHECK(encode_session(SessionMessage{with_patch}, session_protocol_v10).empty());
|
|
// A v9 decoder refuses kind 2 even with a valid layout.
|
|
auto as_v9 = bytes;
|
|
as_v9[4] = static_cast<std::uint8_t>(session_protocol_v9);
|
|
CHECK(!decode_session(as_v9));
|
|
// Coordinates are reserved for a patch target.
|
|
auto placed = bytes;
|
|
placed[session_header_bytes + 40] = 1;
|
|
CHECK(!decode_session(placed));
|
|
// A patch action on v10 is unchanged.
|
|
const CellularAction feed{17, 23, 30, 31, 0, 0, true, 1'000, 2'000};
|
|
const auto feed_bytes = encode_session(SessionMessage{feed}, session_protocol_v10);
|
|
CHECK(std::get<CellularAction>(*decode_session(feed_bytes)) == feed);
|
|
|
|
CellularActionInput input{31, 5, -5, false, 0, 0, true};
|
|
const auto canonical = encode_cellular_action_input(input);
|
|
CHECK(canonical[12] == 2 && decode_cellular_action_input(canonical) == input);
|
|
auto coordinated = canonical;
|
|
coordinated[16] = 1;
|
|
CHECK(!decode_cellular_action_input(coordinated));
|
|
auto unknown_kind = canonical;
|
|
unknown_kind[12] = 4; // Kind 3 is a bud since ADR-0125.
|
|
CHECK(!decode_cellular_action_input(unknown_kind));
|
|
}
|
|
|
|
void v8_perception_is_bounded_and_canonical() {
|
|
CellularPerception perception{19, 42, {}, std::nullopt};
|
|
perception.patches = {
|
|
{1'000, 2'000, 3, {4, 5, 6, 7}},
|
|
{1'000, 3'000, 8, {9, 10, 11, 12}},
|
|
};
|
|
const auto bytes = encode_session(SessionMessage{perception});
|
|
CHECK(!bytes.empty() && bytes.size() == session_header_bytes + 18 + std::size_t{2} * 48);
|
|
std::uint16_t version{};
|
|
const auto decoded = decode_session(bytes, &version);
|
|
CHECK(decoded && version == session_protocol_v8);
|
|
CHECK(std::get<CellularPerception>(*decoded) == perception);
|
|
CHECK(encode_session(*decoded) == bytes);
|
|
CHECK(encode_session(SessionMessage{perception}, session_protocol_v7).empty());
|
|
CHECK(!decode_session(std::span{bytes}.first(bytes.size() - 1)));
|
|
|
|
auto reversed = perception;
|
|
std::ranges::reverse(reversed.patches);
|
|
CHECK(encode_session(SessionMessage{reversed}).empty());
|
|
auto duplicate = perception;
|
|
duplicate.patches[1] = duplicate.patches[0];
|
|
CHECK(encode_session(SessionMessage{duplicate}).empty());
|
|
const CellularPerception invalid{19, 0, {perception.patches[0]}, std::nullopt};
|
|
CHECK(encode_session(SessionMessage{invalid}).empty());
|
|
const CellularPerception cleared{19, 0, {}, std::nullopt};
|
|
const auto clear_bytes = encode_session(SessionMessage{cleared});
|
|
CHECK(std::get<CellularPerception>(*decode_session(clear_bytes)) == cleared);
|
|
auto oversized = perception;
|
|
oversized.patches.clear();
|
|
for (std::size_t i = 0; i <= max_cellular_perceived_patches; ++i)
|
|
oversized.patches.push_back({static_cast<std::uint32_t>(i), 0, 0, {0, 0, 0, 0}});
|
|
CHECK(encode_session(SessionMessage{oversized}).empty());
|
|
}
|
|
|
|
void v9_perception_carries_only_valid_owner_diagnostics() {
|
|
CellularPerception perception{27, 82, {{2'000, 3'000, 17, {4, 5, 6, 7}}}, std::nullopt};
|
|
perception.diagnostics = CellularLifeDiagnostics{
|
|
900, {11, 12, 13, 14}, 21, 8, CellularMovementResult::moved, CellularIntakeResult::acquired,
|
|
true};
|
|
const auto bytes = encode_session(SessionMessage{perception});
|
|
CHECK(!bytes.empty() && bytes.size() == session_header_bytes + 82 + 48);
|
|
std::uint16_t version{};
|
|
const auto decoded = decode_session(bytes, &version);
|
|
CHECK(decoded && version == session_protocol_v9);
|
|
CHECK(std::get<CellularPerception>(*decoded) == perception);
|
|
CHECK(encode_session(*decoded) == bytes);
|
|
|
|
auto malformed = bytes;
|
|
malformed[session_header_bytes + 77] = 1; // Reserved byte.
|
|
CHECK(!decode_session(malformed));
|
|
malformed = bytes;
|
|
malformed[session_header_bytes + 74] = 0xFF; // Unknown movement result.
|
|
CHECK(!decode_session(malformed));
|
|
malformed = bytes;
|
|
malformed[session_header_bytes + 76] = 0; // Costs/status require a tick result.
|
|
CHECK(!decode_session(malformed));
|
|
|
|
auto missing = perception;
|
|
missing.diagnostics.reset();
|
|
CHECK(encode_session(SessionMessage{missing}, session_protocol_v9).empty());
|
|
CHECK(encode_session(SessionMessage{perception}, session_protocol_v8).empty());
|
|
auto cleared = CellularPerception{27, 0, {}, std::nullopt};
|
|
const auto clear_bytes = encode_session(SessionMessage{cleared}, session_protocol_v9);
|
|
CHECK(!clear_bytes.empty());
|
|
CHECK(std::get<CellularPerception>(*decode_session(clear_bytes)) == cleared);
|
|
}
|
|
|
|
void v10_mutation_choice_is_a_strict_reliable_action() {
|
|
const CellularMutationAction action{17, 23, 29, 53, 37, 41, 43, 47};
|
|
const auto bytes = encode_session(SessionMessage{action});
|
|
CHECK(!bytes.empty() && bytes.size() == session_header_bytes + 64);
|
|
std::uint16_t version{};
|
|
const auto decoded = decode_session(bytes, &version);
|
|
CHECK(decoded && version == session_protocol_v10);
|
|
CHECK(std::get<CellularMutationAction>(*decoded) == action);
|
|
CHECK(encode_session(*decoded) == bytes);
|
|
CHECK(encode_session(SessionMessage{action}, session_protocol_v9).empty());
|
|
|
|
auto malformed = bytes;
|
|
std::fill(malformed.begin() + static_cast<std::ptrdiff_t>(session_header_bytes + 16),
|
|
malformed.begin() + static_cast<std::ptrdiff_t>(session_header_bytes + 24),
|
|
std::uint8_t{0});
|
|
CHECK(!decode_session(malformed)); // The action id is the retry identity.
|
|
malformed = bytes;
|
|
malformed[session_header_bytes + 58] = 1;
|
|
CHECK(!decode_session(malformed)); // Reserved bytes stay canonical.
|
|
malformed = bytes;
|
|
std::fill(malformed.begin() + static_cast<std::ptrdiff_t>(session_header_bytes + 48),
|
|
malformed.begin() + static_cast<std::ptrdiff_t>(session_header_bytes + 56),
|
|
std::uint8_t{0xFF});
|
|
CHECK(!decode_session(malformed)); // Offer must precede the scheduled tick.
|
|
malformed = bytes;
|
|
malformed[4] = static_cast<std::uint8_t>(session_protocol_v9);
|
|
CHECK(!decode_session(malformed)); // v1-v9 do not know this message.
|
|
// ADR-0120 amendment: operator 0 declines the named offer; it is a valid choice.
|
|
const CellularMutationAction decline{17, 23, 30, 53, 37, 41, 43, 0};
|
|
const auto decline_bytes = encode_session(SessionMessage{decline});
|
|
CHECK(!decline_bytes.empty() && decline_bytes[session_header_bytes + 56] == 0 &&
|
|
decline_bytes[session_header_bytes + 57] == 0);
|
|
CHECK(std::get<CellularMutationAction>(*decode_session(decline_bytes)) == decline);
|
|
|
|
const ActionOutcome outcome{29, ActionResult::committed, 31, 41};
|
|
const auto outcome_bytes = encode_session(SessionMessage{outcome}, session_protocol_v10);
|
|
CHECK(!outcome_bytes.empty());
|
|
CHECK(std::get<ActionOutcome>(*decode_session(outcome_bytes)) == outcome);
|
|
}
|
|
|
|
// 11.8 (ADR-0129): v10 reports engulf and bud effects and announces a life's end.
|
|
void v10_effects_and_life_end_are_owner_only() {
|
|
CellularLifeDiagnostics d;
|
|
d.reserve_energy_pj = 9;
|
|
d.maintenance_cost_pj = 2;
|
|
d.movement = CellularMovementResult::no_movement;
|
|
d.intake = CellularIntakeResult::not_requested;
|
|
d.has_tick_effects = true;
|
|
d.engulf = CellularEngulfResult::no_eligible_prey;
|
|
d.bud = CellularBudOutcome::insufficient_funding;
|
|
const CellularPerception perception{40, 7, {}, d};
|
|
const auto bytes = encode_session(SessionMessage{perception}, session_protocol_v10);
|
|
CHECK(!bytes.empty() && bytes[session_header_bytes + 77] == 3 &&
|
|
bytes[session_header_bytes + 78] == 4);
|
|
CHECK(std::get<CellularPerception>(*decode_session(bytes)) == perception);
|
|
// v9 has no room for them: an encoder refuses, a decoder refuses non-zero bytes.
|
|
CHECK(encode_session(SessionMessage{perception}, session_protocol_v9).empty());
|
|
auto as_v9 = bytes;
|
|
as_v9[4] = static_cast<std::uint8_t>(session_protocol_v9);
|
|
CHECK(!decode_session(as_v9));
|
|
auto unknown_bud = bytes;
|
|
unknown_bud[session_header_bytes + 78] = 6;
|
|
CHECK(!decode_session(unknown_bud));
|
|
auto idle = perception;
|
|
idle.diagnostics = CellularLifeDiagnostics{};
|
|
idle.diagnostics->bud = CellularBudOutcome::budded; // Effects need tick effects.
|
|
CHECK(encode_session(SessionMessage{idle}, session_protocol_v10).empty());
|
|
|
|
// ADR-0129 amendment: v10 appends the bud cost (payload 82..121), with or without
|
|
// tick effects; v9 cannot carry it, and a cleared view carries none.
|
|
auto costed = perception;
|
|
costed.diagnostics->bud_cost_energy_pj = 0x0102030405060708;
|
|
costed.diagnostics->bud_cost_matter_fg = {11, 22, 33, 44};
|
|
const auto costed_bytes = encode_session(SessionMessage{costed}, session_protocol_v10);
|
|
CHECK(costed_bytes.size() == bytes.size() && costed_bytes[session_header_bytes + 82] == 0x08 &&
|
|
costed_bytes[session_header_bytes + 89] == 0x01 &&
|
|
costed_bytes[session_header_bytes + 90] == 11 &&
|
|
costed_bytes[session_header_bytes + 114] == 44);
|
|
CHECK(std::get<CellularPerception>(*decode_session(costed_bytes)) == costed);
|
|
CHECK(bytes.size() == session_header_bytes + 18 + 64 + 40);
|
|
auto resting = costed;
|
|
resting.diagnostics->has_tick_effects = false;
|
|
resting.diagnostics->maintenance_cost_pj = 0;
|
|
resting.diagnostics->movement = CellularMovementResult::not_reported;
|
|
resting.diagnostics->intake = CellularIntakeResult::not_reported;
|
|
resting.diagnostics->engulf = CellularEngulfResult::not_reported;
|
|
resting.diagnostics->bud = CellularBudOutcome::not_reported;
|
|
CHECK(std::get<CellularPerception>(*decode_session(
|
|
encode_session(SessionMessage{resting}, session_protocol_v10))) == resting);
|
|
auto v9_costed = resting;
|
|
v9_costed.diagnostics->engulf = CellularEngulfResult::not_reported;
|
|
CHECK(encode_session(SessionMessage{v9_costed}, session_protocol_v9).empty());
|
|
v9_costed.diagnostics->bud_cost_energy_pj = 0;
|
|
v9_costed.diagnostics->bud_cost_matter_fg = {};
|
|
CHECK(!encode_session(SessionMessage{v9_costed}, session_protocol_v9).empty());
|
|
auto cleared = encode_session(SessionMessage{CellularPerception{40, 0, {}, std::nullopt}},
|
|
session_protocol_v10);
|
|
CHECK(!cleared.empty() && decode_session(cleared));
|
|
cleared[session_header_bytes + 90] = 1; // A cleared view with a bud cost.
|
|
CHECK(!decode_session(cleared));
|
|
// M11-PT-001: a v10 server built before the bud cost sends the 82-byte prefix. A
|
|
// rebuilt client still reads those views (no bud cost) instead of closing on them.
|
|
auto patched = costed;
|
|
patched.patches.push_back({5, 6, 70, {1, 2, 3, 4}});
|
|
auto legacy = encode_session(SessionMessage{patched}, session_protocol_v10);
|
|
legacy.erase(legacy.begin() + session_header_bytes + 82,
|
|
legacy.begin() + session_header_bytes + 122);
|
|
auto expected = patched;
|
|
expected.diagnostics->bud_cost_energy_pj = 0;
|
|
expected.diagnostics->bud_cost_matter_fg = {};
|
|
const auto legacy_decoded = decode_session(legacy);
|
|
CHECK(legacy_decoded && std::get<CellularPerception>(*legacy_decoded) == expected);
|
|
auto legacy_cleared = encode_session(
|
|
SessionMessage{CellularPerception{40, 0, {}, std::nullopt}}, session_protocol_v10);
|
|
legacy_cleared.resize(session_header_bytes + 82);
|
|
CHECK(decode_session(legacy_cleared));
|
|
legacy.pop_back(); // Neither layout's length.
|
|
CHECK(!decode_session(legacy));
|
|
|
|
const CellularLifeEnded ended{41, 7, CellularDeathReason::engulfed};
|
|
const auto ended_bytes = encode_session(SessionMessage{ended});
|
|
std::uint16_t version{};
|
|
const auto decoded = decode_session(ended_bytes, &version);
|
|
CHECK(ended_bytes.size() == session_header_bytes + 24 && decoded &&
|
|
version == session_protocol_v10 && std::get<CellularLifeEnded>(*decoded) == ended);
|
|
CHECK(encode_session(SessionMessage{ended}, session_protocol_v9).empty());
|
|
auto bad_cause = ended_bytes;
|
|
bad_cause[session_header_bytes + 16] = 3;
|
|
CHECK(!decode_session(bad_cause));
|
|
CHECK(encode_session(SessionMessage{CellularLifeEnded{41, 0, CellularDeathReason::starved}})
|
|
.empty());
|
|
}
|
|
|
|
// 11.4d: the owner's offer lists unranked candidates in operator order with their tradeoffs.
|
|
void v10_mutation_offer_is_owner_only_and_canonical() {
|
|
const CellularMutationOffer offer{120,
|
|
9,
|
|
2,
|
|
100,
|
|
{{3, 1, 0, 40, 150'000, 9'000, 1'200, {300, 70, 35, 140}},
|
|
{7, 2, 2, 1, 160'000, 9'500, 2'400, {600, 140, 70, 280}}}};
|
|
const auto bytes = encode_session(SessionMessage{offer});
|
|
CHECK(bytes.size() == session_header_bytes + 40 + (std::size_t{2} * 64));
|
|
std::uint16_t version{};
|
|
const auto decoded = decode_session(bytes, &version);
|
|
CHECK(decoded && version == session_protocol_v10);
|
|
CHECK(std::get<CellularMutationOffer>(*decoded) == offer);
|
|
CHECK(encode_session(SessionMessage{offer}, session_protocol_v9).empty());
|
|
|
|
const CellularMutationOffer cleared{121, 9, 0, 0, {}};
|
|
const auto clear_bytes = encode_session(SessionMessage{cleared});
|
|
CHECK(!clear_bytes.empty() &&
|
|
std::get<CellularMutationOffer>(*decode_session(clear_bytes)) == cleared);
|
|
|
|
auto unordered = offer;
|
|
std::swap(unordered.candidates[0], unordered.candidates[1]);
|
|
CHECK(encode_session(SessionMessage{unordered}).empty()); // Stable order, never a rank.
|
|
auto future = offer;
|
|
future.offer_tick = 121;
|
|
CHECK(encode_session(SessionMessage{future}).empty());
|
|
auto stale_clear = cleared;
|
|
stale_clear.event_index = 2;
|
|
CHECK(encode_session(SessionMessage{stale_clear}).empty());
|
|
auto malformed = bytes;
|
|
malformed[session_header_bytes + 40 + 2] = 7; // No eighth capability.
|
|
CHECK(!decode_session(malformed));
|
|
malformed = bytes;
|
|
malformed[session_header_bytes + 34] = 1; // Reserved bytes stay canonical.
|
|
CHECK(!decode_session(malformed));
|
|
malformed = bytes;
|
|
malformed.pop_back();
|
|
CHECK(!decode_session(malformed));
|
|
}
|
|
} // namespace
|
|
|
|
// ADR-0136 (v11): the owner's view carries pools and bubbles by ascending id, with 16-bit
|
|
// amount codes; v10 never carries deposits and v11 never carries square patches.
|
|
void v11_perception_carries_deposits() {
|
|
CHECK(deposit_amount_code(0) == 0 && deposit_amount_value(0) == 0);
|
|
CHECK(deposit_amount_value(deposit_amount_code(1)) == 1);
|
|
for (const std::uint64_t value :
|
|
{std::uint64_t{7}, std::uint64_t{1'000}, std::uint64_t{123'456'789},
|
|
std::uint64_t{5'000'000'000}, std::numeric_limits<std::uint64_t>::max()}) {
|
|
const auto back = deposit_amount_value(deposit_amount_code(value));
|
|
CHECK(back <= value && value - back <= value / 256); // Within 0.4 %.
|
|
CHECK(deposit_amount_code(back) == deposit_amount_code(value));
|
|
}
|
|
CellularLifeDiagnostics d;
|
|
d.reserve_energy_pj = 9;
|
|
CellularPerception view{77, 7, {}, d};
|
|
view.deposits.push_back({3,
|
|
1'500,
|
|
2'500,
|
|
30,
|
|
CellularDepositShape::bubble,
|
|
true,
|
|
{deposit_amount_code(4'000'000), 0, 0, 0, 0}});
|
|
view.deposits.push_back(
|
|
{9, 9'000, 400, 1'200, CellularDepositShape::pool, false, {1, 2, 3, 4, 5}});
|
|
const auto bytes = encode_session(SessionMessage{view}, session_protocol_v11);
|
|
CHECK(bytes.size() == session_header_bytes + 18 + 64 + 40 + std::size_t{2} * 26);
|
|
std::uint16_t version{};
|
|
const auto decoded = decode_session(bytes, &version);
|
|
CHECK(decoded && version == session_protocol_v11 &&
|
|
std::get<CellularPerception>(*decoded) == view);
|
|
// v10 cannot carry deposits; v11 cannot carry patches.
|
|
CHECK(encode_session(SessionMessage{view}, session_protocol_v10).empty());
|
|
auto patched = view;
|
|
patched.patches.push_back({0, 0, 1, {}});
|
|
CHECK(encode_session(SessionMessage{patched}, session_protocol_v11).empty());
|
|
// Ids must ascend; a zero id or unknown shape is refused on both sides.
|
|
auto unordered = view;
|
|
std::swap(unordered.deposits[0], unordered.deposits[1]);
|
|
CHECK(encode_session(SessionMessage{unordered}, session_protocol_v11).empty());
|
|
auto bad = bytes;
|
|
bad[session_header_bytes + 122 + 14] = 7; // First entry's shape.
|
|
CHECK(!decode_session(bad));
|
|
bad = bytes;
|
|
bad[session_header_bytes + 122 + 15] = 2; // First entry's drained flag.
|
|
CHECK(!decode_session(bad));
|
|
// A cleared view and the other v10 messages are unchanged in v11.
|
|
CHECK(decode_session(encode_session(SessionMessage{CellularPerception{78, 0, {}, std::nullopt}},
|
|
session_protocol_v11)));
|
|
const CellularLifeEnded ended{41, 7, CellularDeathReason::starved};
|
|
const auto ended_bytes = encode_session(SessionMessage{ended}, session_protocol_v11);
|
|
CHECK(!ended_bytes.empty() &&
|
|
std::get<CellularLifeEnded>(*decode_session(ended_bytes)) == ended);
|
|
}
|
|
|
|
// ADR-0176: v12 is v11 plus captured light and visible producers in the owner's view, and
|
|
// capability 7 (photosynthesis) in offers. Earlier versions refuse both.
|
|
void v12_perception_carries_light_and_producers() {
|
|
CellularLifeDiagnostics d;
|
|
d.reserve_energy_pj = 9;
|
|
d.light_pj = 1'600;
|
|
CellularPerception view{77, 7, {}, d};
|
|
view.deposits.push_back(
|
|
{9, 9'000, 400, 1'200, CellularDepositShape::pool, false, {1, 2, 3, 4, 5}});
|
|
view.producers.push_back({5, 100});
|
|
view.producers.push_back({41, 1'000});
|
|
const auto bytes = encode_session(SessionMessage{view}, session_protocol_v12);
|
|
CHECK(bytes.size() == session_header_bytes + 18 + 64 + 40 + 16 + 26 + std::size_t{2} * 16);
|
|
std::uint16_t version{};
|
|
const auto decoded = decode_session(bytes, &version);
|
|
CHECK(decoded && version == session_protocol_v12 &&
|
|
std::get<CellularPerception>(*decoded) == view);
|
|
// v11 has room for neither light nor producers.
|
|
CHECK(encode_session(SessionMessage{view}, session_protocol_v11).empty());
|
|
auto plain = view;
|
|
plain.producers.clear();
|
|
plain.diagnostics->light_pj = 0;
|
|
CHECK(!encode_session(SessionMessage{plain}, session_protocol_v11).empty());
|
|
CHECK(std::get<CellularPerception>(*decode_session(
|
|
encode_session(SessionMessage{plain}, session_protocol_v12))) == plain);
|
|
// Producers ascend by organism with an efficiency in 1..1000; reserved bytes are zero.
|
|
auto unordered = view;
|
|
std::swap(unordered.producers[0], unordered.producers[1]);
|
|
CHECK(encode_session(SessionMessage{unordered}, session_protocol_v12).empty());
|
|
auto excessive = view;
|
|
excessive.producers[1].photosynthesis_per_thousand = 1'001;
|
|
CHECK(encode_session(SessionMessage{excessive}, session_protocol_v12).empty());
|
|
auto bad = bytes;
|
|
bad[bytes.size() - 1] = 1; // Last producer's reserved byte.
|
|
CHECK(!decode_session(bad));
|
|
bad = bytes;
|
|
bad[bytes.size() - 7] = 0xFF; // Last producer's efficiency high byte.
|
|
CHECK(!decode_session(bad));
|
|
// A cleared view carries no producers.
|
|
CHECK(decode_session(encode_session(SessionMessage{CellularPerception{78, 0, {}, std::nullopt}},
|
|
session_protocol_v12)));
|
|
// Offers name photosynthesis only from v12.
|
|
CellularMutationOffer offer{90, 7, 3, 80, {}};
|
|
CellularOfferedCandidate candidate{};
|
|
candidate.operator_id = 31;
|
|
candidate.capability = 7;
|
|
candidate.effect = 2;
|
|
candidate.parameter = 100;
|
|
offer.candidates.push_back(candidate);
|
|
const auto offered = encode_session(SessionMessage{offer}, session_protocol_v12);
|
|
CHECK(!offered.empty() && std::get<CellularMutationOffer>(*decode_session(offered)) == offer);
|
|
CHECK(encode_session(SessionMessage{offer}, session_protocol_v11).empty());
|
|
offer.candidates[0].capability = 8;
|
|
CHECK(encode_session(SessionMessage{offer}, session_protocol_v12).empty());
|
|
}
|
|
|
|
// Rules 12: v13 is v12 plus liquid motion: the owner's velocity and heading and the region's
|
|
// current configuration in its view, each deposit's world velocity, and a brake on actions.
|
|
// Earlier versions refuse every liquid field.
|
|
void v13_perception_carries_liquid_motion() {
|
|
CellularLifeDiagnostics d;
|
|
d.reserve_energy_pj = 9;
|
|
d.light_pj = 3;
|
|
d.velocity_x_q10 = -190 * 1'024;
|
|
d.velocity_y_q10 = 12;
|
|
d.heading = 49'152;
|
|
CellularPerception view{77, 7, {}, d};
|
|
CellularPerceivedDeposit bubble{9, 9'000, 400, 30, CellularDepositShape::bubble,
|
|
false, {1, 0, 0, 4, 0}};
|
|
bubble.velocity_x_um_s = -250;
|
|
bubble.velocity_y_um_s = 60;
|
|
view.deposits.push_back(bubble);
|
|
view.currents = CellularCurrentsView{5, 60'000, 60'000, 250, 2, 4, 3'000, 6'000, 60, 300};
|
|
const auto bytes = encode_session(SessionMessage{view}, session_protocol_v13);
|
|
CHECK(bytes.size() == session_header_bytes + 18 + 64 + 40 + 16 + 56 + 30);
|
|
std::uint16_t version{};
|
|
const auto decoded = decode_session(bytes, &version);
|
|
CHECK(decoded && version == session_protocol_v13 &&
|
|
std::get<CellularPerception>(*decoded) == view);
|
|
// v12 has room for none of it.
|
|
CHECK(encode_session(SessionMessage{view}, session_protocol_v12).empty());
|
|
auto still = view;
|
|
still.currents.reset();
|
|
still.diagnostics->velocity_x_q10 = still.diagnostics->velocity_y_q10 = 0;
|
|
still.diagnostics->heading = 0;
|
|
still.deposits[0].velocity_x_um_s = still.deposits[0].velocity_y_um_s = 0;
|
|
CHECK(!encode_session(SessionMessage{still}, session_protocol_v12).empty());
|
|
// A region without currents sends a v13 view with the liquid flag clear.
|
|
CHECK(std::get<CellularPerception>(*decode_session(
|
|
encode_session(SessionMessage{still}, session_protocol_v13))) == still);
|
|
// The flag is 0 or 1, the reserved byte zero, and a cleared view carries no currents.
|
|
constexpr std::size_t flags = session_header_bytes + 18 + 64 + 40 + 16 + 10;
|
|
auto bad = bytes;
|
|
bad[flags] = 2;
|
|
CHECK(!decode_session(bad));
|
|
bad = bytes;
|
|
bad[flags + 1] = 1;
|
|
CHECK(!decode_session(bad));
|
|
auto cleared = still;
|
|
cleared.life_identity = 0;
|
|
cleared.diagnostics.reset();
|
|
cleared.deposits.clear();
|
|
cleared.currents = view.currents;
|
|
CHECK(encode_session(SessionMessage{cleared}, session_protocol_v13).empty());
|
|
|
|
// A brake travels only on v13 actions.
|
|
CellularAction braking{1, 2, 3, 4, 500, 0, false, 0, 0};
|
|
braking.brake = true;
|
|
const auto action = encode_session(SessionMessage{braking}, session_protocol_v13);
|
|
CHECK(std::get<CellularAction>(*decode_session(action)) == braking);
|
|
auto old_brake = encode_session(SessionMessage{CellularAction{1, 2, 3, 4, 500, 0, false, 0, 0}},
|
|
session_protocol_v12);
|
|
old_brake[session_header_bytes + 37] = 1;
|
|
CHECK(!decode_session(old_brake));
|
|
}
|
|
|
|
// Rules 13: v14 is v13 plus the revamp: the body block in the view, become, emerge and
|
|
// exclusions on actions, become on choices, catalog offers and the emerged ending. Earlier
|
|
// versions refuse every revamp field.
|
|
void v14_carries_the_revamp() {
|
|
CellularLifeDiagnostics d;
|
|
d.reserve_energy_pj = 9;
|
|
d.members = 3;
|
|
d.emerge_members = 8;
|
|
d.exclude = 0b0101;
|
|
d.can_emerge = false;
|
|
d.split_ready = true;
|
|
d.growth_per_thousand = 620;
|
|
d.payable_energy_pj = 12'000;
|
|
d.total_mass_fg = 24'000'000;
|
|
for (std::size_t i = 0; i < d.traits.size(); ++i)
|
|
d.traits.at(i) = static_cast<std::uint16_t>(100 + i);
|
|
CellularPerception view{77, 7, {}, d};
|
|
const auto bytes = encode_session(SessionMessage{view}, session_protocol_v14);
|
|
CHECK(bytes.size() == session_header_bytes + 18 + 64 + 40 + 16 + 56 + 64);
|
|
std::uint16_t version{};
|
|
const auto decoded = decode_session(bytes, &version);
|
|
CHECK(decoded && version == session_protocol_v14 &&
|
|
std::get<CellularPerception>(*decoded) == view);
|
|
CHECK(encode_session(SessionMessage{view}, session_protocol_v13).empty());
|
|
// A life has at least one member; exclusions are four channels; the flag is 0 or 1.
|
|
auto none = view;
|
|
none.diagnostics->members = 0;
|
|
CHECK(encode_session(SessionMessage{none}, session_protocol_v14).empty());
|
|
constexpr std::size_t block = session_header_bytes + 18 + 64 + 40 + 16 + 56;
|
|
auto bad = bytes;
|
|
bad[block + 4] = 0x10;
|
|
CHECK(!decode_session(bad));
|
|
bad = bytes;
|
|
bad[block + 5] = 4; // Flags: bit 0 Emerge open, bit 1 split ready.
|
|
CHECK(!decode_session(bad));
|
|
bad = bytes;
|
|
bad[block + 6] = 0xE9; // Growth 1,001 per thousand.
|
|
bad[block + 7] = 0x03;
|
|
CHECK(!decode_session(bad));
|
|
// A cleared view carries an empty block.
|
|
CellularPerception cleared{78, 0, {}, std::nullopt};
|
|
const auto empty = encode_session(SessionMessage{cleared}, session_protocol_v14);
|
|
CHECK(std::get<CellularPerception>(*decode_session(empty)) == cleared);
|
|
auto stray = empty;
|
|
stray[block] = 1;
|
|
CHECK(!decode_session(stray));
|
|
|
|
// Actions: become with a bud, emerge without a target, exclusions; v14 only.
|
|
CellularAction become{1, 2, 3, 4, 0, 0, false, 0, 0};
|
|
become.bud = true;
|
|
become.become = true;
|
|
become.exclude = 0x0F;
|
|
CHECK(std::get<CellularAction>(*decode_session(
|
|
encode_session(SessionMessage{become}, session_protocol_v14))) == become);
|
|
CHECK(encode_session(SessionMessage{become}, session_protocol_v13).empty());
|
|
auto lone = become;
|
|
lone.bud = false;
|
|
CHECK(encode_session(SessionMessage{lone}, session_protocol_v14).empty());
|
|
CellularAction emerge{1, 2, 3, 4, 300, 0, false, 0, 0};
|
|
emerge.emerge = true;
|
|
CHECK(std::get<CellularAction>(*decode_session(
|
|
encode_session(SessionMessage{emerge}, session_protocol_v14))) == emerge);
|
|
auto feeding = emerge;
|
|
feeding.engulf = true;
|
|
CHECK(encode_session(SessionMessage{feeding}, session_protocol_v14).empty());
|
|
auto wide = become;
|
|
wide.exclude = 0x10;
|
|
CHECK(encode_session(SessionMessage{wide}, session_protocol_v14).empty());
|
|
auto forged = encode_session(SessionMessage{emerge}, session_protocol_v14);
|
|
forged[session_header_bytes + 36] = 3; // Emerge with a bud.
|
|
CHECK(!decode_session(forged));
|
|
|
|
// The canonical action input keeps them (schema 60).
|
|
CellularActionInput input{9, 0, 0, false, 0, 0, false, true, false, true, false, 0b0011};
|
|
const auto stored = encode_cellular_action_input(input);
|
|
CHECK(stored[13] == 2 && stored[14] == 3 && decode_cellular_action_input(stored) == input);
|
|
auto loose = stored;
|
|
loose[12] = 0; // Become without the bud.
|
|
CHECK(!decode_cellular_action_input(loose));
|
|
|
|
// Choices: become travels only on v14.
|
|
CellularMutationAction choice{1, 2, 3, 40, 7, 1, 30, 12};
|
|
choice.become = true;
|
|
CHECK(std::get<CellularMutationAction>(*decode_session(
|
|
encode_session(SessionMessage{choice}, session_protocol_v14))) == choice);
|
|
CHECK(encode_session(SessionMessage{choice}, session_protocol_v13).empty());
|
|
|
|
// Offers name catalog mutations with a chance and a cost, nothing operator-shaped.
|
|
CellularMutationOffer offer{40, 7, 1, 30, {}};
|
|
CellularOfferedCandidate named{};
|
|
named.operator_id = 12;
|
|
named.parameter = 700'000;
|
|
named.cost_energy_pj = 5;
|
|
named.cost_matter_fg = {1, 2, 3, 4};
|
|
offer.candidates.push_back(named);
|
|
named.operator_id = 49;
|
|
named.parameter = 10;
|
|
offer.candidates.push_back(named);
|
|
CHECK(std::get<CellularMutationOffer>(*decode_session(
|
|
encode_session(SessionMessage{offer}, session_protocol_v14))) == offer);
|
|
auto shaped = offer;
|
|
shaped.candidates[0].capability = 1;
|
|
CHECK(encode_session(SessionMessage{shaped}, session_protocol_v14).empty());
|
|
auto certain = offer;
|
|
certain.candidates[0].parameter = 1'000'001;
|
|
CHECK(encode_session(SessionMessage{certain}, session_protocol_v14).empty());
|
|
|
|
// The emerged ending is v14's.
|
|
CellularLifeEnded emerged{50, 7, CellularDeathReason::emerged};
|
|
CHECK(std::get<CellularLifeEnded>(*decode_session(
|
|
encode_session(SessionMessage{emerged}, session_protocol_v14))) == emerged);
|
|
CHECK(encode_session(SessionMessage{emerged}, session_protocol_v13).empty());
|
|
}
|
|
|
|
int main() {
|
|
try {
|
|
round_trip_and_version_gate();
|
|
malformed_frames_are_refused();
|
|
v7_reliable_action_is_stable_and_strict();
|
|
v10_engulf_action_is_versioned_and_exclusive();
|
|
v10_bud_action_is_versioned_and_exclusive();
|
|
v8_perception_is_bounded_and_canonical();
|
|
v9_perception_carries_only_valid_owner_diagnostics();
|
|
v10_mutation_choice_is_a_strict_reliable_action();
|
|
v10_mutation_offer_is_owner_only_and_canonical();
|
|
v10_effects_and_life_end_are_owner_only();
|
|
v11_perception_carries_deposits();
|
|
v12_perception_carries_light_and_producers();
|
|
v13_perception_carries_liquid_motion();
|
|
v14_carries_the_revamp();
|
|
std::puts(
|
|
"PASS cellular protocol: v6 intention, v7 action, v8-v10 perception/action framing");
|
|
} catch (const std::exception& error) {
|
|
std::fprintf(stderr, "%s\n", error.what());
|
|
return 1;
|
|
}
|
|
}
|