Files
aevolution/include/evo/server/cellular_session_batch.hpp
T
dingoandClaude Opus 5.5 9de55e352d
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (checks) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / postgres-tools (push) Successful in 1m39s
Foundation / server-image (push) Successful in 5m19s
Foundation / image (push) Successful in 6m54s
fix(cellular): one action per account a tick, never a refused tick
Region 16 playtest: a bud showed and was then undone, and several buds were
refused. The client scheduled a discrete action at server tick + lead, but
movement runs ahead of that once the lead shrinks (each movement takes the
tick after the last one sent), so a bud could name a tick a movement already
had. A tick takes one action per account: the region refused the whole tick
and dropped every player's actions on it ("tick stalled ... error=4").

- Server: keep_one_action_per_account keeps each account's discrete action
  (engulf, bud, become, emerge), else its newest, and refuses only the rest.
- Client: a discrete action is scheduled after every tick already sent, and
  movement is held off the tick a queued discrete action owns.
- Failed background commits now log their category, publication error,
  store error and SQLSTATE (never server text), to diagnose ok=0 commits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 08:31:16 -04:00

139 lines
6.3 KiB
C++

#pragma once
#include "evo/network/intent_admission.hpp"
#include "evo/persistence/life_reservation_store.hpp"
#include "evo/persistence/region_lease.hpp"
#include "evo/server/lifecycle/cellular_batch.hpp"
#include <cstdint>
#include <expected>
#include <memory>
#include <optional>
#include <span>
#include <utility>
#include <vector>
namespace evo::server {
enum class CellularSessionBatchError : std::uint8_t {
tick,
intents,
action,
life,
duplicate_action,
duplicate_birth,
invalid_birth,
mutation,
duplicate_mutation,
unperceived_target,
head,
batch_full,
action_limit,
biology
};
struct CellularSessionTick {
CellularTickIntents intents;
std::vector<CellularActionRecord> actions;
std::vector<CellularMutationActionRecord> mutation_actions;
};
// Overlays authenticated v7 actions and v10 durable mutation reservations onto the
// scheduler's complete per-life input set. The scheduler owns non-player and no-message
// behavior; this adapter binds each action to the one live account-owned life and preserves
// the canonical input needed for publication and replay.
[[nodiscard]] 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 = {});
// A tick takes one action per account. Keeps each account's discrete action (engulf, bud,
// become, emerge), else its newest (highest id), in `actions`, and returns the others, which
// the caller refuses. A client naming a tick it already sent a movement for then loses only
// that movement, not every player's actions on the tick.
[[nodiscard]] std::vector<network::CarriedAction>
keep_one_action_per_account(std::vector<network::CarriedAction>& actions);
// Builds one bounded publication from the last durable head. Its working copy is private;
// only the host may publish, and callers must discard it after uncertainty until the host
// resolves that exact prepared batch.
class CellularSessionBatchBuilder {
public:
[[nodiscard]] static std::expected<CellularSessionBatchBuilder, CellularSessionBatchError>
create(const CellularLives& committed_state, const persistence::RegionHead& committed_head);
[[nodiscard]] std::expected<void, CellularSessionBatchError>
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 = {},
std::span<const CellularAdminCommand> admin = {});
// Scheduler path: expected placement/funding/existing-life refusals are reported
// separately, so they cannot prevent other births or the ordinary tick. Unexpected
// failures retain append's all-or-nothing behavior. Persist reports only after publish.
[[nodiscard]] std::expected<void, CellularSessionBatchError> 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 = {},
std::span<const CellularAdminCommand> admin = {});
[[nodiscard]] std::span<const CellularBirthRefusal> refused_births() const noexcept {
return refused_births_;
}
[[nodiscard]] const CellularBatch& batch() const noexcept {
return batch_;
}
// ADR-0182: names the rules of adopt_tuning commands (the host's verified revisions).
void set_tuning_resolver(CellularTuningResolver tuning) {
tuning_ = std::move(tuning);
}
// ADR-0182: each admin command appended so far and whether it applied, in trace order.
[[nodiscard]] std::span<const CellularAdminResult> admin_results() const noexcept {
return admin_results_;
}
[[nodiscard]] std::span<const CellularActionRecord> actions() const noexcept {
return actions_;
}
[[nodiscard]] std::span<const CellularMutationActionRecord> mutation_actions() const noexcept {
return mutation_actions_;
}
[[nodiscard]] const CellularLives& candidate() const noexcept {
return *working_;
}
// Hands the working state to a commit; the builder is spent afterwards.
[[nodiscard]] std::unique_ptr<CellularLives> take_candidate() noexcept {
return std::move(working_);
}
// A batch started on an uncommitted predecessor (a commit still in flight) takes that
// predecessor's real head once it is known. The head must end where this batch begins.
[[nodiscard]] bool rebase(const persistence::RegionHead& head) noexcept {
if (head.frame != batch_.header.frame || head.end_tick != batch_.header.begin_tick)
return false;
batch_.header.predecessor = head.commit;
batch_.header.predecessor_digest = head.digest;
return true;
}
// The owner's own refusal behind the last `biology` error, for operator logs.
[[nodiscard]] std::optional<CellularLivesError> last_lives_error() const noexcept {
return last_lives_error_;
}
private:
[[nodiscard]] std::expected<void, CellularSessionBatchError>
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,
std::span<const CellularAdminCommand> admin, bool allow_refusals);
explicit CellularSessionBatchBuilder(std::unique_ptr<CellularLives> working)
: working_{std::move(working)} {}
std::unique_ptr<CellularLives> working_;
CellularBatch batch_;
std::vector<CellularActionRecord> actions_;
std::vector<CellularMutationActionRecord> mutation_actions_;
std::vector<CellularBirthRefusal> refused_births_;
std::vector<CellularAdminResult> admin_results_;
CellularTuningResolver tuning_;
std::optional<CellularLivesError> last_lives_error_;
};
} // namespace evo::server