Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m47s
Foundation / checks (network) (push) Successful in 9m30s
Foundation / checks (client) (push) Successful in 12m19s
Foundation / checks (tsan) (push) Successful in 7m43s
Foundation / checks (checks) (push) Successful in 31m29s
Foundation / checks (postgres) (push) Successful in 36m39s
Foundation / postgres-tools (push) Successful in 19s
Foundation / image (push) Successful in 2m15s
Foundation / server-image (push) Successful in 20m37s
Each batch's actions are recorded in the same statement as its checkpoint: one region_actions row per (world, account, action id) and the block in an append-only table, compared on exact retries and resolution. The region host attaches actions to the ticks they ride and reports acknowledgements and drops, keeping unknown-outcome batches unresolved. Outcomes come from the durable record via ActionLedger, so crashes before or after a commit, unknown publications and cross-region moves all resolve to one answer; a repeated id keeps its first record instead of stalling a region. ADR-0092. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
264 lines
9.9 KiB
C++
264 lines
9.9 KiB
C++
#include "evo/persistence/region_batch.hpp"
|
|
#include <algorithm>
|
|
#include <tuple>
|
|
#include <utility>
|
|
|
|
namespace evo::persistence {
|
|
namespace {
|
|
constexpr std::uint64_t trace_magic = 0x314352544F564541ULL; // "AEVOTRC1"
|
|
constexpr std::uint32_t trace_format = 1;
|
|
constexpr std::uint64_t max_trace_ticks = 64;
|
|
|
|
template <std::size_t N> bool zero(const std::array<std::uint8_t, N>& bytes) {
|
|
return std::ranges::all_of(bytes, [](std::uint8_t b) { return b == 0; });
|
|
}
|
|
// Fills an exactly sized buffer; callers check the final cursor.
|
|
class Writer {
|
|
public:
|
|
explicit Writer(std::vector<std::uint8_t>& out) : out_(out) {}
|
|
void u(std::uint64_t value, unsigned width) {
|
|
for (unsigned i = 0; i < width; ++i, value >>= 8U)
|
|
out_[at_++] = static_cast<std::uint8_t>(value & 0xFFU);
|
|
}
|
|
template <std::size_t N> void bytes(const std::array<std::uint8_t, N>& value) {
|
|
std::ranges::copy(value, out_.begin() + static_cast<std::ptrdiff_t>(at_));
|
|
at_ += N;
|
|
}
|
|
template <std::size_t N> void all(const std::array<std::uint64_t, N>& values) {
|
|
for (const auto v : values)
|
|
u(v, 8);
|
|
}
|
|
[[nodiscard]] std::size_t at() const noexcept {
|
|
return at_;
|
|
}
|
|
|
|
private:
|
|
std::vector<std::uint8_t>& out_;
|
|
std::size_t at_{};
|
|
};
|
|
class Reader {
|
|
public:
|
|
explicit Reader(std::span<const std::uint8_t> in) : in_(in) {}
|
|
std::uint64_t u(unsigned width) {
|
|
std::uint64_t value{};
|
|
for (unsigned i = 0; i < width; ++i)
|
|
value |= static_cast<std::uint64_t>(in_[at_ + i]) << (8U * i);
|
|
at_ += width;
|
|
return value;
|
|
}
|
|
template <std::size_t N> std::array<std::uint8_t, N> bytes() {
|
|
std::array<std::uint8_t, N> value{};
|
|
std::copy_n(in_.begin() + static_cast<std::ptrdiff_t>(at_), N, value.begin());
|
|
at_ += N;
|
|
return value;
|
|
}
|
|
template <std::size_t N> void all(std::array<std::uint64_t, N>& values) {
|
|
for (auto& v : values)
|
|
v = u(8);
|
|
}
|
|
|
|
private:
|
|
std::span<const std::uint8_t> in_;
|
|
std::size_t at_{};
|
|
};
|
|
|
|
bool valid(const TraceHeader& h) noexcept {
|
|
return h.profile != 0 && h.key.world != 0 && h.key.region != 0 && !zero(h.predecessor_commit) &&
|
|
!zero(h.predecessor_digest) && h.end_tick > h.begin_tick &&
|
|
h.end_tick - h.begin_tick <= max_trace_ticks;
|
|
}
|
|
bool contiguous(const TraceHeader& h, std::span<const TraceStep> steps) noexcept {
|
|
if (steps.size() != h.end_tick - h.begin_tick)
|
|
return false;
|
|
for (std::size_t i = 0; i < steps.size(); ++i)
|
|
if (steps[i].report.tick != h.begin_tick + i + 1)
|
|
return false;
|
|
return true;
|
|
}
|
|
} // namespace
|
|
|
|
std::expected<std::vector<std::uint8_t>, BatchError>
|
|
encode_trace(const TraceHeader& h, std::span<const TraceStep> steps) {
|
|
if (!valid(h) || !contiguous(h, steps))
|
|
return std::unexpected{BatchError::format};
|
|
std::vector<std::uint8_t> out(trace_bytes_for(steps.size()));
|
|
Writer w{out};
|
|
w.u(trace_magic, 8);
|
|
w.u(trace_format, 4);
|
|
w.u(h.profile, 4);
|
|
w.u(h.key.world, 8);
|
|
w.u(h.key.region, 8);
|
|
w.bytes(h.predecessor_commit);
|
|
w.bytes(h.predecessor_digest);
|
|
w.u(h.begin_tick, 8);
|
|
w.u(h.end_tick, 8);
|
|
for (const auto& s : steps) {
|
|
const auto& in = s.input;
|
|
const auto& r = s.report;
|
|
w.u(in.temperature_mK, 8);
|
|
w.u(in.source_enabled, 8);
|
|
w.all(in.bite);
|
|
w.all(in.terminal);
|
|
for (const auto v :
|
|
{r.tick, r.captured, r.retained, r.capture_heat, r.grown, r.unmet, r.source_status})
|
|
w.u(v, 8);
|
|
w.all(r.assimilated);
|
|
w.all(r.waste);
|
|
w.u(r.terminal_transition, 8);
|
|
w.u(r.consumer_transition, 8);
|
|
w.all(r.decomposed);
|
|
}
|
|
if (w.at() != out.size())
|
|
return std::unexpected{BatchError::format};
|
|
return out;
|
|
}
|
|
|
|
std::expected<DecodedTrace, BatchError> decode_trace(std::span<const std::uint8_t> bytes) {
|
|
if (bytes.size() < trace_header_bytes)
|
|
return std::unexpected{BatchError::format};
|
|
Reader r{bytes};
|
|
if (r.u(8) != trace_magic || r.u(4) != trace_format)
|
|
return std::unexpected{BatchError::format};
|
|
DecodedTrace t;
|
|
auto& h = t.header;
|
|
h.profile = static_cast<std::uint32_t>(r.u(4));
|
|
h.key.world = r.u(8);
|
|
h.key.region = r.u(8);
|
|
h.predecessor_commit = r.bytes<16>();
|
|
h.predecessor_digest = r.bytes<32>();
|
|
h.begin_tick = r.u(8);
|
|
h.end_tick = r.u(8);
|
|
// Size is fixed by the header before any step is allocated.
|
|
if (!valid(h) || bytes.size() != trace_bytes_for(h.end_tick - h.begin_tick))
|
|
return std::unexpected{BatchError::format};
|
|
t.steps.resize(static_cast<std::size_t>(h.end_tick - h.begin_tick));
|
|
for (auto& s : t.steps) {
|
|
auto& in = s.input;
|
|
auto& rep = s.report;
|
|
in.temperature_mK = r.u(8);
|
|
in.source_enabled = r.u(8);
|
|
r.all(in.bite);
|
|
r.all(in.terminal);
|
|
for (auto* v : {&rep.tick, &rep.captured, &rep.retained, &rep.capture_heat, &rep.grown,
|
|
&rep.unmet, &rep.source_status})
|
|
*v = r.u(8);
|
|
r.all(rep.assimilated);
|
|
r.all(rep.waste);
|
|
rep.terminal_transition = r.u(8);
|
|
rep.consumer_transition = r.u(8);
|
|
r.all(rep.decomposed);
|
|
}
|
|
if (!contiguous(h, t.steps))
|
|
return std::unexpected{BatchError::format};
|
|
return t;
|
|
}
|
|
|
|
namespace {
|
|
bool valid(std::span<const BatchDecision> d, std::uint64_t begin, std::uint64_t end) noexcept {
|
|
if (d.size() > max_batch_decisions)
|
|
return false;
|
|
for (std::size_t i = 0; i < d.size(); ++i) {
|
|
const auto& x = d[i];
|
|
if (x.outcome != Outcome::death ||
|
|
(x.actor != Actor::producer && x.actor != Actor::consumer) || x.tick <= begin ||
|
|
x.tick > end)
|
|
return false;
|
|
for (std::size_t j = 0; j < i; ++j)
|
|
if (d[j].actor == x.actor)
|
|
return false; // Terminal: at most one death per actor.
|
|
if (i > 0 &&
|
|
(d[i - 1].tick > x.tick || (d[i - 1].tick == x.tick && d[i - 1].actor >= x.actor)))
|
|
return false; // Canonical order (tick, actor).
|
|
}
|
|
return true;
|
|
}
|
|
} // namespace
|
|
|
|
std::expected<std::vector<std::uint8_t>, BatchError>
|
|
encode_decisions(std::span<const BatchDecision> decisions, std::uint64_t begin_tick,
|
|
std::uint64_t end_tick) {
|
|
if (!valid(decisions, begin_tick, end_tick))
|
|
return std::unexpected{BatchError::format};
|
|
std::vector<std::uint8_t> out(decisions.size() * decision_bytes);
|
|
Writer w{out};
|
|
for (const auto& d : decisions) {
|
|
w.u(d.tick, 8);
|
|
w.u(static_cast<std::uint32_t>(d.actor), 4);
|
|
w.u(static_cast<std::uint32_t>(d.outcome), 4);
|
|
w.u(d.organism, 8);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::expected<std::vector<BatchDecision>, BatchError>
|
|
decode_decisions(std::span<const std::uint8_t> bytes, std::uint64_t begin_tick,
|
|
std::uint64_t end_tick) {
|
|
if (bytes.size() % decision_bytes != 0 || bytes.size() > max_batch_decisions * decision_bytes)
|
|
return std::unexpected{BatchError::format};
|
|
std::vector<BatchDecision> out(bytes.size() / decision_bytes);
|
|
Reader r{bytes};
|
|
for (auto& d : out) {
|
|
d.tick = r.u(8);
|
|
const auto actor = r.u(4);
|
|
const auto outcome = r.u(4);
|
|
// Unknown values are refused, never truncated onto known ones.
|
|
if (actor > 1 || outcome != 1)
|
|
return std::unexpected{BatchError::format};
|
|
d.actor = static_cast<Actor>(actor);
|
|
d.outcome = Outcome::death;
|
|
d.organism = r.u(8);
|
|
}
|
|
if (!valid(out, begin_tick, end_tick))
|
|
return std::unexpected{BatchError::format};
|
|
return out;
|
|
}
|
|
|
|
std::vector<BatchDecision> derive_decisions(std::span<const TraceStep> steps,
|
|
std::uint64_t producer_organism,
|
|
std::uint64_t consumer_organism) {
|
|
std::vector<BatchDecision> out;
|
|
for (const auto& s : steps) {
|
|
if (s.report.terminal_transition == 1)
|
|
out.push_back({s.report.tick, Actor::producer, Outcome::death, producer_organism});
|
|
if (s.report.consumer_transition == 1)
|
|
out.push_back({s.report.tick, Actor::consumer, Outcome::death, consumer_organism});
|
|
}
|
|
return out;
|
|
}
|
|
std::expected<std::vector<std::uint8_t>, BatchError>
|
|
encode_actions(std::span<const ActionRecord> actions, std::uint64_t begin_tick,
|
|
std::uint64_t end_tick) {
|
|
if (actions.size() > max_batch_actions)
|
|
return std::unexpected{BatchError::format};
|
|
std::vector<ActionRecord> sorted(actions.begin(), actions.end());
|
|
const auto key = [](const ActionRecord& a) {
|
|
return std::tuple{a.tick, a.account, a.action_id};
|
|
};
|
|
std::ranges::sort(sorted, {}, key);
|
|
// An action id appears once, whatever its tick.
|
|
std::vector<std::pair<Uuid, std::uint64_t>> ids;
|
|
ids.reserve(sorted.size());
|
|
for (const auto& a : sorted)
|
|
ids.emplace_back(a.account, a.action_id);
|
|
std::ranges::sort(ids);
|
|
if (std::ranges::adjacent_find(ids) != ids.end())
|
|
return std::unexpected{BatchError::format};
|
|
std::vector<std::uint8_t> out;
|
|
out.reserve(sorted.size() * action_record_bytes);
|
|
for (std::size_t i = 0; i < sorted.size(); ++i) {
|
|
const auto& a = sorted[i];
|
|
if (a.account == Uuid{} || a.action_id == 0 || a.kind != 1 || a.tick <= begin_tick ||
|
|
a.tick > end_tick || (i > 0 && key(sorted[i - 1]) == key(a)))
|
|
return std::unexpected{BatchError::format};
|
|
out.insert(out.end(), a.account.begin(), a.account.end());
|
|
for (unsigned b = 0; b < 8; ++b)
|
|
out.push_back(static_cast<std::uint8_t>(a.action_id >> (8U * b)));
|
|
for (unsigned b = 0; b < 8; ++b)
|
|
out.push_back(static_cast<std::uint8_t>(a.tick >> (8U * b)));
|
|
out.push_back(static_cast<std::uint8_t>(a.kind));
|
|
out.push_back(static_cast<std::uint8_t>(a.kind >> 8U));
|
|
}
|
|
return out;
|
|
}
|
|
} // namespace evo::persistence
|