Foundation / checks (ecs) (push) Successful in 2m20s
Foundation / checks (deploy) (push) Successful in 4m1s
Foundation / checks (tsan) (push) Successful in 9m49s
Foundation / checks (client) (push) Successful in 19m23s
Foundation / checks (network) (push) Successful in 19m31s
Foundation / checks (checks) (push) Successful in 42m41s
Foundation / checks (postgres) (push) Successful in 45m36s
Foundation / postgres-tools (push) Successful in 28s
Foundation / server-image (push) Successful in 3m15s
Foundation / image (push) Successful in 3m18s
An offer the cell cannot afford stayed open, and only one offer is issued at a time, so banked feeding credit never became a new offer. A v10 choice naming operator 0 now declines the exact outstanding offer. It is reserved and published like any choice: schema v40 accepts operator 0 in the reserved input and trace selections and stores result 3, declined. The owner consumes the offer and changes nothing else, and the client reports it as committed. Old traces never carry operator 0, so their replay is unchanged. The Evolution panel gets a "Decline this offer" button. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
317 lines
12 KiB
C++
317 lines
12 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};
|
|
};
|
|
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 (d.bud != network::CellularBudOutcome::not_reported &&
|
|
d.bud != network::CellularBudOutcome::not_requested)
|
|
bud_feedback_ = std::pair{view.tick, 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};
|
|
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();
|
|
}
|
|
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
|