M11.6a (ADR-0127, schema v37): a published life is marked ended once, by a commit of its region, through the lease-fenced end_cellular_lives. One active life per account replaces one life per account, so after a death the account reserves and publishes a new primitive founder while the corpse, archive and descendants stay. The host records endings after the death's publication and again for every founder corpse on recovery. The cellular server now refuses only an unusable carried action, not the whole tick. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
110 lines
5.7 KiB
C++
110 lines
5.7 KiB
C++
#pragma once
|
|
#include "evo/persistence/postgres.hpp"
|
|
#include "evo/persistence/region_lease.hpp"
|
|
#include <array>
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <expected>
|
|
#include <span>
|
|
#include <vector>
|
|
|
|
namespace evo::persistence {
|
|
// Schema v15, ADR-0100. These operations allocate identity, never a biological
|
|
// body. Each call is one statement on the caller's connection, without retries.
|
|
enum class LifeReservationError : std::uint8_t {
|
|
existing_life,
|
|
fenced,
|
|
not_available, // Missing allocator/reservation, or reservation belongs elsewhere.
|
|
exhausted,
|
|
invalid,
|
|
store,
|
|
unknown, // May have committed. Reconnect and retry the SAME request to reconcile.
|
|
pending_mutation,
|
|
identity_conflict
|
|
};
|
|
struct LifeReservationFailure {
|
|
LifeReservationError kind{};
|
|
PgFailure cause{};
|
|
};
|
|
enum class LifeRefusal : std::uint8_t {
|
|
none = 0, // Reserved only; NOT successful spawn.
|
|
insufficient_funding = 1,
|
|
no_placement = 2,
|
|
cancelled = 3,
|
|
existing_life = 4
|
|
};
|
|
struct LifeReservation {
|
|
std::uint64_t identity{}, region{};
|
|
LifeRefusal refusal{};
|
|
friend bool operator==(const LifeReservation&, const LifeReservation&) = default;
|
|
};
|
|
struct PrimitiveLifeBinding {
|
|
Uuid account{};
|
|
std::uint64_t request{}, identity{}, born{};
|
|
Uuid commit{};
|
|
std::uint64_t commit_sequence{};
|
|
friend bool operator==(const PrimitiveLifeBinding&, const PrimitiveLifeBinding&) = default;
|
|
};
|
|
struct PrimitiveLifeOutcome {
|
|
LifeReservation reservation;
|
|
std::optional<PrimitiveLifeBinding> published;
|
|
friend bool operator==(const PrimitiveLifeOutcome&, const PrimitiveLifeOutcome&) = default;
|
|
};
|
|
inline constexpr std::size_t cellular_mutation_reservation_input_bytes = 34;
|
|
struct CellularMutationReservation {
|
|
Uuid account{};
|
|
std::uint64_t action_id{}, region{}, generation{}, target_tick{}, successor_identity{};
|
|
std::array<std::uint8_t, cellular_mutation_reservation_input_bytes> input{};
|
|
friend bool operator==(const CellularMutationReservation&,
|
|
const CellularMutationReservation&) = default;
|
|
};
|
|
// Exact retry returns the original identity/owning region/refusal, even after
|
|
// account control moves. The result grants no authority in the returned region.
|
|
// request_id is in the new-life namespace, separate from M10 probe action IDs.
|
|
[[nodiscard]] std::expected<LifeReservation, LifeReservationFailure>
|
|
reserve_primitive_life(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account,
|
|
std::uint64_t generation, std::uint64_t request_id, std::uint32_t rules,
|
|
std::chrono::milliseconds timeout);
|
|
// Requires control in the reservation's owning region. First refusal is final;
|
|
// repeats return it. Passing none or an unknown enum value is invalid.
|
|
[[nodiscard]] std::expected<LifeRefusal, LifeReservationFailure>
|
|
reject_primitive_life(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account,
|
|
std::uint64_t generation, std::uint64_t request_id, LifeRefusal reason,
|
|
std::chrono::milliseconds timeout);
|
|
// Bounded durable binding set for checkpoint reconciliation; session generations
|
|
// are intentionally absent and must be reacquired after recovery.
|
|
[[nodiscard]] std::expected<std::vector<PrimitiveLifeBinding>, LifeReservationFailure>
|
|
load_primitive_lives(Postgres& db, RegionKey key, std::chrono::milliseconds timeout);
|
|
// Authenticated world/account/request lookup. A missing value is unknown; a
|
|
// present pending reservation is still not a published life.
|
|
[[nodiscard]] std::expected<std::optional<PrimitiveLifeOutcome>, LifeReservationFailure>
|
|
query_primitive_life(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account,
|
|
std::uint64_t generation, std::uint64_t request_id,
|
|
std::chrono::milliseconds timeout);
|
|
// Read-only, lease-fenced lookup used before claiming durable account control.
|
|
// Returns no value when the account has no active (non-refused) life reservation.
|
|
[[nodiscard]] std::expected<std::optional<std::uint64_t>, LifeReservationFailure>
|
|
primitive_life_owner(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account,
|
|
std::chrono::milliseconds timeout);
|
|
// Schema v29, ADR-0121. The canonical bytes are target tick (u64 LE), life ID
|
|
// (u64 LE), offer event index (u64 LE), offer tick (u64 LE), and operator ID
|
|
// (u16 LE). Exact retries return the same world-allocated successor identity.
|
|
[[nodiscard]] std::expected<CellularMutationReservation, LifeReservationFailure>
|
|
reserve_cellular_mutation(
|
|
Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& account,
|
|
std::uint64_t generation, std::uint64_t action_id,
|
|
const std::array<std::uint8_t, cellular_mutation_reservation_input_bytes>& input,
|
|
std::chrono::milliseconds timeout);
|
|
// Schema v37, ADR-0127: records that these published lives of this region had ended by
|
|
// `commit` (a commit of this region), so their accounts may start new lives. At most
|
|
// 256 identities; repeats are no-ops. Returns how many were newly ended.
|
|
inline constexpr std::size_t max_life_endings_per_call = 256;
|
|
[[nodiscard]] std::expected<std::uint32_t, LifeReservationFailure>
|
|
end_cellular_lives(Postgres& db, RegionKey key, const LeaseGrant& lease, const Uuid& commit,
|
|
std::span<const std::uint64_t> identities, std::chrono::milliseconds timeout);
|
|
// Pending selections are reloaded under the recovered region lease after restart.
|
|
[[nodiscard]] std::expected<std::vector<CellularMutationReservation>, LifeReservationFailure>
|
|
load_pending_cellular_mutations(Postgres& db, RegionKey key, const LeaseGrant& lease,
|
|
std::chrono::milliseconds timeout);
|
|
} // namespace evo::persistence
|