Foundation / checks (checks) (push) Successful in 1h42m20s
Foundation / checks (client) (push) Successful in 22m8s
Foundation / checks (deploy) (push) Successful in 5m2s
Foundation / checks (ecs) (push) Successful in 2m22s
Foundation / checks (network) (push) Successful in 20m56s
Foundation / checks (tsan) (push) Successful in 46m50s
Foundation / checks (postgres) (push) Successful in 1h2m47s
Foundation / postgres-tools (push) Successful in 27s
Foundation / server-image (push) Successful in 3m42s
Foundation / image (push) Successful in 7m2s
CellularSessionBatchBuilder left every batch at cellular_batch_format, but replay accepts only the inheritance format on owners that retain offspring plans (format >= 8, rules 10 and 11). On DEV region 9 (bundle 13) every player tick, including a queued birth, was refused at publication as an invalid batch, so births stayed pending forever. The builder now declares the same format the unattended interval path already uses. The PostgreSQL suite provisions cellular-v8 and drives a birth and an ordinary tick through the host and session builder, as evo-server does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
296 lines
16 KiB
C++
296 lines
16 KiB
C++
#include "evo/server/cellular_session_batch.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <optional>
|
|
|
|
namespace evo::server {
|
|
namespace {
|
|
std::uint64_t little_endian(std::span<const std::uint8_t> bytes, std::size_t offset,
|
|
std::size_t count) {
|
|
std::uint64_t value{};
|
|
for (std::size_t i = 0; i < count; ++i)
|
|
value |= static_cast<std::uint64_t>(bytes[offset + i]) << (i * 8U);
|
|
return value;
|
|
}
|
|
} // namespace
|
|
|
|
std::expected<CellularSessionTick, CellularSessionBatchError>
|
|
prepare_cellular_session_tick(const CellularLives& state, std::uint64_t tick,
|
|
std::span<const CellularLifeIntent> baseline,
|
|
std::span<const network::CarriedAction> actions,
|
|
std::span<const persistence::CellularMutationReservation> mutations) {
|
|
if (tick == 0 || tick != state.tick() + 1)
|
|
return std::unexpected{CellularSessionBatchError::tick};
|
|
if (baseline.size() != state.lives().size())
|
|
return std::unexpected{CellularSessionBatchError::intents};
|
|
if (actions.size() > max_cellular_action_records ||
|
|
mutations.size() > max_cellular_action_records)
|
|
return std::unexpected{CellularSessionBatchError::action};
|
|
|
|
CellularSessionTick prepared;
|
|
prepared.intents.lives.assign(baseline.begin(), baseline.end());
|
|
std::ranges::sort(prepared.intents.lives, {}, &CellularLifeIntent::life_id);
|
|
for (std::size_t i = 0; i < state.lives().size(); ++i)
|
|
if (prepared.intents.lives[i].life_id != state.lives()[i].identity.life ||
|
|
(i != 0 && prepared.intents.lives[i - 1].life_id == prepared.intents.lives[i].life_id))
|
|
return std::unexpected{CellularSessionBatchError::intents};
|
|
|
|
prepared.actions.reserve(actions.size());
|
|
for (const auto& action : actions) {
|
|
if (!action.account.valid() || action.action_id == 0 || action.tick != tick ||
|
|
action.kind != network::ActionKind::cellular || !action.cellular_input)
|
|
return std::unexpected{CellularSessionBatchError::action};
|
|
const auto input = network::decode_cellular_action_input(*action.cellular_input);
|
|
if (!input || input->tick != tick)
|
|
return std::unexpected{CellularSessionBatchError::action};
|
|
|
|
const auto life =
|
|
std::ranges::find(state.lives(), action.account.bytes, &CellularOwnedLife::account);
|
|
if (life == state.lives().end())
|
|
return std::unexpected{CellularSessionBatchError::life};
|
|
if (input->has_intake_target) {
|
|
const auto view = state.perceive(life->identity.life);
|
|
const biology::CellularPatchAddress target{input->intake_x_um, input->intake_y_um};
|
|
if (!view ||
|
|
std::ranges::find(*view, target, &biology::CellularPatchPerception::address) ==
|
|
view->end())
|
|
return std::unexpected{CellularSessionBatchError::unperceived_target};
|
|
}
|
|
const auto owned_intent = std::ranges::lower_bound(
|
|
prepared.intents.lives, life->identity.life, {}, &CellularLifeIntent::life_id);
|
|
if (owned_intent == prepared.intents.lives.end() ||
|
|
owned_intent->life_id != life->identity.life)
|
|
return std::unexpected{CellularSessionBatchError::intents};
|
|
if (std::ranges::find_if(prepared.actions, [&](const CellularActionRecord& previous) {
|
|
return previous.account == action.account.bytes;
|
|
}) != prepared.actions.end())
|
|
return std::unexpected{CellularSessionBatchError::duplicate_action};
|
|
|
|
owned_intent->intent.movement = {input->move_x, input->move_y};
|
|
owned_intent->intent.intake_target =
|
|
input->has_intake_target
|
|
? std::optional<biology::CellularPatchAddress>{biology::CellularPatchAddress{
|
|
input->intake_x_um, input->intake_y_um}}
|
|
: std::nullopt;
|
|
// ADR-0124: a rules-2 region reports the intent unsupported and ignores it.
|
|
owned_intent->intent.engulf = input->engulf;
|
|
// ADR-0125: below rules 4 the owner ignores a bud intent.
|
|
owned_intent->intent.bud = input->bud;
|
|
CellularActionRecord record;
|
|
record.account = action.account.bytes;
|
|
record.action_id = action.action_id;
|
|
record.input = *action.cellular_input;
|
|
prepared.actions.push_back(record);
|
|
}
|
|
std::ranges::sort(prepared.actions,
|
|
[](const CellularActionRecord& left, const CellularActionRecord& right) {
|
|
if (left.account != right.account)
|
|
return left.account < right.account;
|
|
return left.action_id < right.action_id;
|
|
});
|
|
|
|
if (!mutations.empty() && state.format_version() < cellular_lives_revision_format)
|
|
return std::unexpected{CellularSessionBatchError::mutation};
|
|
prepared.intents.mutations.reserve(mutations.size());
|
|
prepared.mutation_actions.reserve(mutations.size());
|
|
for (const auto& reservation : mutations) {
|
|
const auto bytes = std::span{reservation.input};
|
|
const auto target_tick = little_endian(bytes, 0, 8);
|
|
const auto life_id = little_endian(bytes, 8, 8);
|
|
const auto event_index = little_endian(bytes, 16, 8);
|
|
const auto offer_tick = little_endian(bytes, 24, 8);
|
|
const auto operator_id = static_cast<std::uint16_t>(little_endian(bytes, 32, 2));
|
|
if (reservation.account == persistence::Uuid{} || reservation.action_id == 0 ||
|
|
reservation.region != state.region() || reservation.generation == 0 ||
|
|
reservation.successor_identity == 0 || target_tick != tick ||
|
|
reservation.target_tick != tick || life_id == 0 || event_index == 0 ||
|
|
offer_tick == 0 || offer_tick >= tick)
|
|
return std::unexpected{CellularSessionBatchError::mutation};
|
|
const auto life =
|
|
std::ranges::find(state.lives(), reservation.account, &CellularOwnedLife::account);
|
|
if (life == state.lives().end() || life->identity.life != life_id ||
|
|
life->mutation.event_index != event_index || life->mutation.offer_tick != offer_tick)
|
|
return std::unexpected{CellularSessionBatchError::mutation};
|
|
const auto& offer = life->mutation.offer;
|
|
if (!offer || (operator_id != 0 && // Operator 0 declines the offer.
|
|
!std::ranges::any_of(offer->candidates, [&](const auto& candidate) {
|
|
return candidate.operator_id == operator_id;
|
|
})))
|
|
return std::unexpected{CellularSessionBatchError::mutation};
|
|
|
|
if (std::ranges::any_of(prepared.intents.mutations,
|
|
[&](const auto& previous) {
|
|
return previous.life_id == life_id ||
|
|
previous.successor_id == reservation.successor_identity;
|
|
}) ||
|
|
std::ranges::any_of(prepared.mutation_actions, [&](const auto& previous) {
|
|
return previous.account == reservation.account &&
|
|
previous.action_id == reservation.action_id;
|
|
}))
|
|
return std::unexpected{CellularSessionBatchError::duplicate_mutation};
|
|
prepared.intents.mutations.push_back(
|
|
{life_id, event_index, offer_tick, reservation.successor_identity, operator_id});
|
|
prepared.mutation_actions.push_back({reservation.account,
|
|
reservation.action_id,
|
|
tick,
|
|
{life_id, event_index, offer_tick, operator_id}});
|
|
}
|
|
std::ranges::sort(prepared.intents.mutations, {}, &CellularMutationSelection::life_id);
|
|
std::ranges::sort(prepared.mutation_actions, [](const auto& left, const auto& right) {
|
|
if (left.account != right.account)
|
|
return left.account < right.account;
|
|
return left.action_id < right.action_id;
|
|
});
|
|
return prepared;
|
|
}
|
|
|
|
std::expected<CellularSessionBatchBuilder, CellularSessionBatchError>
|
|
CellularSessionBatchBuilder::create(const CellularLives& committed_state,
|
|
const persistence::RegionHead& committed_head) {
|
|
if (committed_head.frame != committed_state.frame() ||
|
|
committed_head.end_tick != committed_state.tick() ||
|
|
std::ranges::all_of(committed_head.commit, [](std::uint8_t byte) { return byte == 0; }) ||
|
|
std::ranges::all_of(committed_head.digest, [](std::uint8_t byte) { return byte == 0; }))
|
|
return std::unexpected{CellularSessionBatchError::head};
|
|
auto working = CellularLives::restore(committed_state.snapshot(), committed_state.world(),
|
|
committed_state.region(), committed_state.frame(),
|
|
committed_state.tick());
|
|
if (!working)
|
|
return std::unexpected{CellularSessionBatchError::biology};
|
|
|
|
CellularSessionBatchBuilder builder{std::move(*working)};
|
|
builder.batch_.header = {committed_state.world(), committed_state.region(),
|
|
committed_state.frame(), committed_head.commit,
|
|
committed_head.digest, committed_state.tick(),
|
|
committed_state.tick()};
|
|
// Owners that retain offspring plans (rules 10 and 11) replay only the inheritance
|
|
// trace format, exactly as their unattended intervals are written.
|
|
if (committed_state.format_version() >= cellular_lives_inheritance_format &&
|
|
cellular_retained_plans(committed_state.rules_version()))
|
|
builder.batch_.format_version = cellular_batch_inheritance_format;
|
|
return builder;
|
|
}
|
|
|
|
std::expected<void, CellularSessionBatchError> CellularSessionBatchBuilder::append(
|
|
std::uint64_t tick, std::span<const CellularLifeIntent> baseline,
|
|
std::span<const network::CarriedAction> actions,
|
|
std::span<const CellularTickIntents::Birth> births,
|
|
std::span<const persistence::CellularMutationReservation> mutations) {
|
|
return append_impl(tick, baseline, actions, births, mutations, false);
|
|
}
|
|
std::expected<void, CellularSessionBatchError>
|
|
CellularSessionBatchBuilder::append_with_birth_refusals(
|
|
std::uint64_t tick, std::span<const CellularLifeIntent> baseline,
|
|
std::span<const network::CarriedAction> actions,
|
|
std::span<const CellularTickIntents::Birth> births,
|
|
std::span<const persistence::CellularMutationReservation> mutations) {
|
|
return append_impl(tick, baseline, actions, births, mutations, true);
|
|
}
|
|
std::expected<void, CellularSessionBatchError> CellularSessionBatchBuilder::append_impl(
|
|
std::uint64_t tick, std::span<const CellularLifeIntent> baseline,
|
|
std::span<const network::CarriedAction> actions,
|
|
std::span<const CellularTickIntents::Birth> births,
|
|
std::span<const persistence::CellularMutationReservation> mutations, bool allow_refusals) {
|
|
if (batch_.ticks.size() >= max_cellular_batch_ticks)
|
|
return std::unexpected{CellularSessionBatchError::batch_full};
|
|
if (actions_.size() + actions.size() > max_cellular_action_records)
|
|
return std::unexpected{CellularSessionBatchError::action_limit};
|
|
if (mutation_actions_.size() + mutations.size() > max_cellular_action_records)
|
|
return std::unexpected{CellularSessionBatchError::action_limit};
|
|
auto prepared = prepare_cellular_session_tick(*working_, tick, baseline, actions, mutations);
|
|
if (!prepared)
|
|
return std::unexpected{prepared.error()};
|
|
for (const auto& action : prepared->actions)
|
|
if (std::ranges::find_if(actions_, [&](const CellularActionRecord& previous) {
|
|
return previous.account == action.account && previous.action_id == action.action_id;
|
|
}) != actions_.end())
|
|
return std::unexpected{CellularSessionBatchError::duplicate_action};
|
|
for (const auto& action : prepared->mutation_actions)
|
|
if (std::ranges::find_if(mutation_actions_, [&](const auto& previous) {
|
|
return previous.account == action.account && previous.action_id == action.action_id;
|
|
}) != mutation_actions_.end())
|
|
return std::unexpected{CellularSessionBatchError::duplicate_mutation};
|
|
if (births.size() > biology::max_cellular_tick_lives)
|
|
return std::unexpected{CellularSessionBatchError::invalid_birth};
|
|
prepared->intents.births.assign(births.begin(), births.end());
|
|
std::ranges::sort(prepared->intents.births, [](const auto& left, const auto& right) {
|
|
if (left.account != right.account)
|
|
return left.account < right.account;
|
|
return left.request < right.request;
|
|
});
|
|
for (std::size_t i = 0; i < prepared->intents.births.size(); ++i) {
|
|
const auto& birth = prepared->intents.births[i];
|
|
if (std::ranges::all_of(birth.account, [](std::uint8_t value) { return value == 0; }) ||
|
|
birth.request == 0 || birth.generation == 0 || birth.identity == 0)
|
|
return std::unexpected{CellularSessionBatchError::invalid_birth};
|
|
if (i > 0 && prepared->intents.births[i - 1].account == birth.account)
|
|
return std::unexpected{CellularSessionBatchError::duplicate_birth};
|
|
if (std::ranges::any_of(refused_births_, [&](const auto& previous) {
|
|
return previous.birth.account == birth.account ||
|
|
previous.birth.identity == birth.identity;
|
|
}))
|
|
return std::unexpected{CellularSessionBatchError::duplicate_birth};
|
|
for (const auto& previous_tick : batch_.ticks)
|
|
if (std::ranges::any_of(previous_tick.births, [&](const auto& previous) {
|
|
return previous.account == birth.account || previous.identity == birth.identity;
|
|
}))
|
|
return std::unexpected{CellularSessionBatchError::duplicate_birth};
|
|
}
|
|
|
|
auto next = CellularLives::restore(working_->snapshot(), working_->world(), working_->region(),
|
|
working_->frame(), working_->tick());
|
|
if (!next) {
|
|
last_lives_error_ = next.error();
|
|
return std::unexpected{CellularSessionBatchError::biology};
|
|
}
|
|
if (const auto advanced = (*next)->advance_tick_with_mutations(prepared->intents.lives,
|
|
prepared->intents.mutations);
|
|
!advanced) {
|
|
last_lives_error_ = advanced.error();
|
|
return std::unexpected{CellularSessionBatchError::biology};
|
|
}
|
|
|
|
std::vector<CellularTickIntents::Birth> accepted_births;
|
|
std::vector<CellularBirthRefusal> refused_births;
|
|
for (const auto& birth : prepared->intents.births) {
|
|
const LifeRequest request{birth.account, birth.request};
|
|
const LifeAllocation allocation{
|
|
birth.identity, birth.identity, birth.identity, birth.identity, {tick}};
|
|
auto created = (*next)->birth_candidate(request, allocation);
|
|
if (!created && allow_refusals &&
|
|
(created.error() == CellularLivesError::insufficient_funding ||
|
|
created.error() == CellularLivesError::no_placement ||
|
|
created.error() == CellularLivesError::life_exists)) {
|
|
refused_births.push_back({birth, created.error()});
|
|
continue;
|
|
}
|
|
if (!created)
|
|
return std::unexpected{CellularSessionBatchError::invalid_birth};
|
|
*next = std::move(*created);
|
|
accepted_births.push_back(birth);
|
|
}
|
|
prepared->intents.births = std::move(accepted_births);
|
|
|
|
auto next_batch = batch_;
|
|
next_batch.ticks.push_back(std::move(prepared->intents));
|
|
next_batch.header.end_tick = tick;
|
|
if (!encode_cellular_batch(next_batch))
|
|
return std::unexpected{CellularSessionBatchError::invalid_birth};
|
|
const auto next_mutation_actions = [&] {
|
|
auto records = mutation_actions_;
|
|
records.insert(records.end(), prepared->mutation_actions.begin(),
|
|
prepared->mutation_actions.end());
|
|
return records;
|
|
}();
|
|
if (!encode_cellular_mutation_action_block(next_mutation_actions, next_batch.header.begin_tick,
|
|
next_batch.header.end_tick))
|
|
return std::unexpected{CellularSessionBatchError::mutation};
|
|
working_ = std::move(*next);
|
|
batch_ = std::move(next_batch);
|
|
actions_.insert(actions_.end(), prepared->actions.begin(), prepared->actions.end());
|
|
mutation_actions_ = next_mutation_actions;
|
|
refused_births_.insert(refused_births_.end(), refused_births.begin(), refused_births.end());
|
|
return {};
|
|
}
|
|
} // namespace evo::server
|