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 45s
Foundation / checks (checks) (push) Canceled after 49s
Foundation / checks (client) (push) Canceled after 49s
Foundation / checks (postgres) (push) Canceled after 48s
Foundation / checks (deploy) (push) Canceled after 54s
Foundation / checks (network) (push) Canceled after 53s
A history entry only said "Committed", so a refused engulf (membrane too thick) looked like a success. The engulf or bud result reported in the action's tick view is now attached to that entry: Recent actions shows the action, its tick and the region's result, green only when it took effect. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
342 lines
14 KiB
C++
342 lines
14 KiB
C++
#pragma once
|
|
|
|
#include "evo/network/session_protocol.hpp"
|
|
#include <algorithm>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <deque>
|
|
#include <limits>
|
|
#include <optional>
|
|
#include <utility>
|
|
|
|
namespace evo::client_app {
|
|
// Presentation and outstanding requests only. No biological prediction or saved authority.
|
|
struct MutationChoice {
|
|
std::uint64_t life{}, event{}, offer_tick{};
|
|
std::uint16_t operator_id{};
|
|
friend bool operator==(const MutationChoice&, const MutationChoice&) = default;
|
|
};
|
|
enum class CellAction : std::uint8_t { engulf, bud, mutation };
|
|
enum class CellActionState : std::uint8_t {
|
|
queued,
|
|
sent,
|
|
admitted,
|
|
unresolved,
|
|
committed,
|
|
no_effect,
|
|
refused
|
|
};
|
|
struct CellActionRecord {
|
|
CellAction kind{};
|
|
CellActionState state{CellActionState::queued};
|
|
std::uint64_t id{}, life{}, tick{};
|
|
MutationChoice choice{};
|
|
network::AdmitStatus refusal{network::AdmitStatus::admitted};
|
|
// The region's reported effect of this engulf or bud, once a view carries it.
|
|
std::optional<network::CellularEngulfResult> engulf;
|
|
std::optional<network::CellularBudOutcome> bud;
|
|
};
|
|
struct CellEnergyChange {
|
|
std::uint64_t from_tick{}, to_tick{}, amount{};
|
|
bool falling{};
|
|
};
|
|
|
|
class CellularPlay {
|
|
public:
|
|
static constexpr std::size_t history_limit = 8;
|
|
void initialize(std::uint64_t first_id) {
|
|
next_id_ = first_id == 0 ? 1 : first_id;
|
|
birth_request_ = allocate_id();
|
|
birth_pending_ = true;
|
|
}
|
|
[[nodiscard]] std::uint64_t allocate_id() {
|
|
const auto id = next_id_;
|
|
if (next_id_ != 0)
|
|
next_id_ = next_id_ == std::numeric_limits<std::uint64_t>::max() ? 0 : next_id_ + 1;
|
|
return id;
|
|
}
|
|
void connected() {
|
|
connected_ = true;
|
|
}
|
|
void disconnected() {
|
|
connected_ = false;
|
|
fresh_ = false;
|
|
energy_sample_.reset();
|
|
energy_change_.reset();
|
|
offer_.reset();
|
|
if (pending_) {
|
|
if (pending_->state == CellActionState::queued) {
|
|
pending_->state = CellActionState::refused;
|
|
finish();
|
|
} else {
|
|
pending_->state = CellActionState::unresolved;
|
|
}
|
|
}
|
|
}
|
|
void observe(const network::CellularPerception& view) {
|
|
if (view.tick < view_tick_)
|
|
return;
|
|
view_tick_ = view.tick;
|
|
fresh_ = true;
|
|
if (ended_ && view.life_identity == ended_->life_identity)
|
|
return; // A retained pre-death view must never revive this identity.
|
|
if (view.life_identity != 0 && view.diagnostics) {
|
|
const auto energy = view.diagnostics->reserve_energy_pj;
|
|
if (energy_sample_ && energy_sample_->life == view.life_identity) {
|
|
if (view.tick > energy_sample_->tick)
|
|
energy_change_ = CellEnergyChange{energy_sample_->tick, view.tick,
|
|
energy < energy_sample_->energy
|
|
? energy_sample_->energy - energy
|
|
: energy - energy_sample_->energy,
|
|
energy < energy_sample_->energy};
|
|
} else {
|
|
energy_change_.reset();
|
|
}
|
|
energy_sample_ = EnergySample{view.life_identity, view.tick, energy};
|
|
}
|
|
// Never replay an intention onto a later life. Keep its uncertain result visible.
|
|
if (life_ != view.life_identity) {
|
|
offer_.reset();
|
|
engulf_feedback_.reset();
|
|
bud_feedback_.reset();
|
|
if (pending_) {
|
|
pending_->state = pending_->state == CellActionState::queued
|
|
? CellActionState::refused
|
|
: CellActionState::unresolved;
|
|
finish();
|
|
}
|
|
}
|
|
life_ = view.life_identity;
|
|
if (view.diagnostics && view.diagnostics->has_tick_effects) {
|
|
const auto& d = *view.diagnostics;
|
|
if (d.engulf != network::CellularEngulfResult::not_reported &&
|
|
d.engulf != network::CellularEngulfResult::not_requested) {
|
|
engulf_feedback_ = std::pair{view.tick, d.engulf};
|
|
if (auto* record = awaiting_effect(CellAction::engulf, view.tick))
|
|
record->engulf = d.engulf;
|
|
}
|
|
if (d.bud != network::CellularBudOutcome::not_reported &&
|
|
d.bud != network::CellularBudOutcome::not_requested) {
|
|
bud_feedback_ = std::pair{view.tick, d.bud};
|
|
if (auto* record = awaiting_effect(CellAction::bud, view.tick))
|
|
record->bud = d.bud;
|
|
}
|
|
}
|
|
if (life_ != 0) {
|
|
birth_pending_ = false;
|
|
birth_result_ = network::PrimitiveLifeResult::born;
|
|
if (ended_ && ended_->life_identity != life_)
|
|
ended_.reset();
|
|
}
|
|
}
|
|
void observe_offer(const std::optional<network::CellularMutationOffer>& offer) {
|
|
offer_ = offer && offer->life_identity == life_ && fresh_ && !ended_ ? offer : std::nullopt;
|
|
}
|
|
void observe_end(const network::CellularLifeEnded& ended) {
|
|
if (ended.life_identity == seen_end_ || (life_ != 0 && life_ != ended.life_identity) ||
|
|
(ended_ && ended_->tick >= ended.tick) || ended.tick < birth_tick_)
|
|
return;
|
|
seen_end_ = ended.life_identity;
|
|
ended_ = ended;
|
|
life_ = 0;
|
|
offer_.reset();
|
|
if (pending_ && pending_->state == CellActionState::queued) {
|
|
pending_->state = CellActionState::refused;
|
|
finish();
|
|
}
|
|
// The discrete action may still have a final result arriving in this poll.
|
|
}
|
|
[[nodiscard]] bool can_act() const {
|
|
return connected_ && fresh_ && life_ != 0 && !ended_ && !pending_ && !birth_pending_;
|
|
}
|
|
[[nodiscard]] bool queue(CellAction kind, std::uint64_t tick, MutationChoice choice = {}) {
|
|
if (!can_act() || tick == 0)
|
|
return false;
|
|
if (kind == CellAction::mutation &&
|
|
(!offer_ || choice.life != life_ || choice.event != offer_->event_index ||
|
|
choice.offer_tick != offer_->offer_tick || tick <= choice.offer_tick ||
|
|
(choice.operator_id != 0 && // Operator 0 declines the offer (ADR-0120 amendment).
|
|
!std::ranges::any_of(offer_->candidates, [&](const auto& c) {
|
|
return c.operator_id == choice.operator_id;
|
|
}))))
|
|
return false;
|
|
const auto id = allocate_id();
|
|
if (id == 0)
|
|
return false;
|
|
pending_ = CellActionRecord{
|
|
kind, CellActionState::queued, id, life_, tick,
|
|
choice, network::AdmitStatus::admitted, std::nullopt, std::nullopt};
|
|
return true;
|
|
}
|
|
void sent() {
|
|
if (pending_)
|
|
pending_->state = CellActionState::sent;
|
|
}
|
|
void rechecking() {
|
|
if (pending_ && pending_->state != CellActionState::queued)
|
|
pending_->state = CellActionState::unresolved;
|
|
}
|
|
void receipt(const network::ActionReceipt& receipt) {
|
|
if (!pending_ || pending_->id != receipt.action_id)
|
|
return;
|
|
if (receipt.status == network::AdmitStatus::admitted ||
|
|
receipt.status == network::AdmitStatus::duplicate) {
|
|
pending_->state = CellActionState::admitted;
|
|
} else if (pending_->kind == CellAction::mutation &&
|
|
receipt.status == network::AdmitStatus::not_ready) {
|
|
// The host can report not_ready after an uncertain durable reservation.
|
|
pending_->state = CellActionState::unresolved;
|
|
} else if (pending_->state != CellActionState::unresolved &&
|
|
pending_->state != CellActionState::admitted) {
|
|
pending_->state = CellActionState::refused;
|
|
pending_->refusal = receipt.status;
|
|
finish();
|
|
}
|
|
// A refused retry cannot disprove a previously admitted or uncertain action.
|
|
}
|
|
void outcome(const network::ActionOutcome& outcome) {
|
|
if (!pending_ || pending_->id != outcome.action_id) {
|
|
// Final replies may arrive after the life ended or the user kept an unknown.
|
|
for (auto& record : history_)
|
|
if (record.id == outcome.action_id && record.state == CellActionState::unresolved) {
|
|
if (outcome.result == network::ActionResult::committed)
|
|
record.state = CellActionState::committed;
|
|
else if (outcome.result == network::ActionResult::committed_no_effect)
|
|
record.state = CellActionState::no_effect;
|
|
else if (outcome.result == network::ActionResult::not_committed)
|
|
record.state = CellActionState::refused;
|
|
}
|
|
return;
|
|
}
|
|
using enum network::ActionResult;
|
|
switch (outcome.result) {
|
|
case committed:
|
|
pending_->state = CellActionState::committed;
|
|
break;
|
|
case committed_no_effect:
|
|
pending_->state = CellActionState::no_effect;
|
|
break;
|
|
case not_committed:
|
|
pending_->state = CellActionState::refused;
|
|
break;
|
|
case pending:
|
|
pending_->state = CellActionState::admitted;
|
|
return;
|
|
case unknown:
|
|
pending_->state = CellActionState::unresolved;
|
|
return;
|
|
}
|
|
finish();
|
|
}
|
|
// User may leave an uncertain result in the bounded record, never as a success/refusal.
|
|
void keep_unknown() {
|
|
if (pending_ && pending_->state == CellActionState::unresolved)
|
|
finish();
|
|
}
|
|
[[nodiscard]] bool can_restart() const {
|
|
const bool refused = birth_result_ == network::PrimitiveLifeResult::insufficient_funding ||
|
|
birth_result_ == network::PrimitiveLifeResult::no_placement ||
|
|
birth_result_ == network::PrimitiveLifeResult::cancelled;
|
|
return connected_ && (fresh_ || refused) && life_ == 0 && !birth_pending_ &&
|
|
(birth_result_ != network::PrimitiveLifeResult::existing_life || ended_);
|
|
}
|
|
[[nodiscard]] bool restart() {
|
|
if (!can_restart())
|
|
return false;
|
|
const auto id = allocate_id();
|
|
if (id == 0)
|
|
return false;
|
|
birth_request_ = id;
|
|
birth_pending_ = true;
|
|
birth_result_ = network::PrimitiveLifeResult::pending;
|
|
return true;
|
|
}
|
|
void birth(const network::PrimitiveLifeOutcome& result) {
|
|
if (result.request_id != birth_request_)
|
|
return;
|
|
birth_result_ = result.result;
|
|
birth_pending_ = result.result == network::PrimitiveLifeResult::pending ||
|
|
result.result == network::PrimitiveLifeResult::unknown ||
|
|
result.result == network::PrimitiveLifeResult::born;
|
|
if (result.result == network::PrimitiveLifeResult::born) {
|
|
ended_.reset();
|
|
birth_tick_ = view_tick_;
|
|
}
|
|
}
|
|
[[nodiscard]] std::uint64_t birth_request() const {
|
|
return birth_request_;
|
|
}
|
|
[[nodiscard]] bool birth_pending() const {
|
|
return birth_pending_;
|
|
}
|
|
[[nodiscard]] network::PrimitiveLifeResult birth_result() const {
|
|
return birth_result_;
|
|
}
|
|
[[nodiscard]] std::uint64_t life() const {
|
|
return life_;
|
|
}
|
|
[[nodiscard]] bool fresh() const {
|
|
return connected_ && fresh_;
|
|
}
|
|
[[nodiscard]] const auto& offer() const {
|
|
return offer_;
|
|
}
|
|
[[nodiscard]] const auto& ended() const {
|
|
return ended_;
|
|
}
|
|
[[nodiscard]] const auto& pending() const {
|
|
return pending_;
|
|
}
|
|
[[nodiscard]] const auto& history() const {
|
|
return history_;
|
|
}
|
|
[[nodiscard]] const auto& engulf_feedback() const {
|
|
return engulf_feedback_;
|
|
}
|
|
[[nodiscard]] const auto& bud_feedback() const {
|
|
return bud_feedback_;
|
|
}
|
|
[[nodiscard]] const auto& energy_change() const {
|
|
return energy_change_;
|
|
}
|
|
|
|
private:
|
|
void finish() {
|
|
if (!pending_)
|
|
return;
|
|
if (history_.size() == history_limit)
|
|
history_.pop_front();
|
|
history_.push_back(*pending_);
|
|
pending_.reset();
|
|
}
|
|
// The newest engulf/bud of this life, at or before `tick`, still without a reported
|
|
// effect: the view for its tick carries what the region did with it.
|
|
CellActionRecord* awaiting_effect(CellAction kind, std::uint64_t tick) {
|
|
const auto waiting = [&](const CellActionRecord& r) {
|
|
return r.kind == kind && r.life == life_ && r.tick <= tick &&
|
|
!(kind == CellAction::engulf ? r.engulf.has_value() : r.bud.has_value());
|
|
};
|
|
if (pending_ && waiting(*pending_))
|
|
return &*pending_;
|
|
for (auto it = history_.rbegin(); it != history_.rend(); ++it)
|
|
if (waiting(*it))
|
|
return &*it;
|
|
return nullptr;
|
|
}
|
|
std::uint64_t next_id_{1}, life_{}, view_tick_{}, birth_tick_{}, birth_request_{}, seen_end_{};
|
|
bool connected_{}, fresh_{}, birth_pending_{};
|
|
network::PrimitiveLifeResult birth_result_{network::PrimitiveLifeResult::pending};
|
|
std::optional<network::CellularMutationOffer> offer_;
|
|
std::optional<network::CellularLifeEnded> ended_;
|
|
std::optional<CellActionRecord> pending_;
|
|
std::deque<CellActionRecord> history_;
|
|
std::optional<std::pair<std::uint64_t, network::CellularEngulfResult>> engulf_feedback_;
|
|
std::optional<std::pair<std::uint64_t, network::CellularBudOutcome>> bud_feedback_;
|
|
struct EnergySample {
|
|
std::uint64_t life{}, tick{}, energy{};
|
|
};
|
|
std::optional<EnergySample> energy_sample_;
|
|
std::optional<CellEnergyChange> energy_change_;
|
|
};
|
|
} // namespace evo::client_app
|