Complete M7.5 durable batch outcomes and commit resolution

ADR-0063: manifest v2 (360 bytes) binds a per-tick validation trace
object and a canonical decision block by SHA-256. Publication replays the
trace from the committed predecessor's verified payload and requires
every report and the candidate bytes, derives deaths deterministically,
uploads payload and trace with verified read-back, then sends manifest
and decisions to one fenced statement. RegionBatch seals at a terminal
transition or 64 ticks and refuses ticks until durable confirmation.

Schema v5 records the checkpoint, its decisions and outbox rows
atomically; a unique index keeps one terminal outcome per actor
(EVL08), triggers make commits/decisions immutable (EVL09), an exact
retry of a committed ID returns the original receipt, changed bytes
conflict, and read-only resolve_region_commit answers committed/absent/
conflict. Outbox pending/acknowledge give idempotent at-least-once
delivery. Recovery reconciles durable deaths with restored lifecycle
(and archive deaths for the lineage profile).

Validated: Windows CTest 58/58; Linux GCC Debug/Release, clang-tidy and
ASan/UBSan CTest 58/58 each; PostgreSQL suites Release, analysis, ASan
(lost response, committed-after-timeout, terminated orphan retry,
changed bytes, repeated/partial/reordered decisions, gap, outbox
redelivery, history quarantine); five mutations each caught. No
Docker/Gitea CI, process kill, broker or deployment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-09-28 17:30:30 -04:00
co-authored by Claude Opus 5.5
parent a2c4543df2
commit dd7e8028b0
27 changed files with 2075 additions and 216 deletions
+226
View File
@@ -0,0 +1,226 @@
#include "evo/persistence/region_batch.hpp"
#include <algorithm>
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;
}
} // namespace evo::persistence