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
+153
View File
@@ -0,0 +1,153 @@
#include "evo/persistence/region_batch.hpp"
#include "fixtures/environmental_region.hpp"
#include <algorithm>
#include <cstdio>
#include <source_location>
#include <stdexcept>
#include <string>
namespace {
using namespace evo::persistence;
using evo::processes::EnvironmentalCommunity;
using evo::processes::LineageRegion;
using evo::test::region_input;
void check(bool ok, std::source_location at = std::source_location::current()) {
if (!ok)
throw std::runtime_error("region batch line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
template <std::size_t N> std::array<std::uint8_t, N> filled(std::uint8_t value) {
std::array<std::uint8_t, N> out{};
out.fill(value);
return out;
}
TraceHeader header(std::uint64_t begin, std::uint64_t end) {
return {1, {7, 3}, filled<16>(1), filled<32>(2), begin, end};
}
void recorder_seals_at_outcomes_and_capacity() {
auto state = evo::test::region_community();
RegionBatch<EnvironmentalCommunity> batch{*state};
CHECK(batch.begin_tick() == 0 && !batch.sealed());
CHECK(!batch.confirm(0)); // Nothing to confirm.
for (std::uint64_t tick = 1; tick <= 19; ++tick)
CHECK(batch.advance(region_input(tick, true)));
CHECK(!batch.sealed() && batch.end_tick() == 19);
auto death = batch.advance(region_input(20, true));
CHECK(death && death->consumer_transition == 1);
// A permanent outcome pauses the region until durable confirmation.
CHECK(batch.sealed() && batch.advance(region_input(21)).error() == BatchError::sealed);
CHECK(state->progress().tick == 20);
const auto decisions = derive_decisions(batch.steps(), 0, 0);
CHECK(decisions.size() == 1 && decisions[0] == BatchDecision{20, Actor::consumer});
CHECK(!batch.confirm(19) && batch.sealed());
CHECK(batch.confirm(20) && !batch.sealed() && batch.begin_tick() == 20);
CHECK(batch.steps().empty());
// 64 ticks without an outcome also seals; an early flush is explicit.
for (std::uint64_t i = 0; i < 64; ++i)
CHECK(batch.advance(region_input(21 + i)));
CHECK(batch.sealed() && batch.steps().size() == 64);
CHECK(batch.confirm(84));
CHECK(batch.advance(region_input(85)));
batch.seal();
CHECK(batch.sealed() && batch.confirm(85));
// A refused tick records nothing.
evo::processes::EnvironmentalInput bad = region_input(86);
bad.terminal[1] = 1; // The consumer is already dead.
CHECK(batch.advance(bad).error() == BatchError::state && batch.steps().empty());
}
void lineage_recorder_uses_archived_organisms() {
auto region = evo::test::lineage_region();
RegionBatch<LineageRegion> batch{*region};
for (std::uint64_t tick = 1; tick <= 20; ++tick)
CHECK(batch.advance(region_input(tick, true)));
CHECK(batch.sealed());
const auto d =
derive_decisions(batch.steps(), region->binding().producer, region->binding().consumer);
CHECK(d.size() == 1 && d[0].organism == 11 && d[0].tick == 20);
CHECK(!region->archive().organism(11)->alive);
}
void traces_round_trip_and_bind_their_place() {
auto state = evo::test::region_community();
RegionBatch<EnvironmentalCommunity> batch{*state};
for (std::uint64_t tick = 1; tick <= 20; ++tick)
CHECK(batch.advance(region_input(tick, true)));
auto bytes = encode_trace(header(0, 20), batch.steps());
CHECK(bytes && bytes->size() == trace_bytes_for(20) && bytes->size() == 96 + (20 * 304));
auto decoded = decode_trace(*bytes);
CHECK(decoded && decoded->header == header(0, 20));
CHECK(std::ranges::equal(decoded->steps, batch.steps()));
CHECK(trace_bytes_for(64) == 19552);
const auto refused = [&](TraceHeader h, std::size_t count) {
return !encode_trace(h, batch.steps().first(count));
};
CHECK(refused(header(0, 19), 20)); // Count disagrees with the range.
CHECK(refused(header(1, 21), 20)); // Reports are for ticks 1..20.
auto h = header(0, 20);
h.predecessor_digest = {};
CHECK(refused(h, 20));
h = header(0, 20);
h.key.region = 0;
CHECK(refused(h, 20));
CHECK(!encode_trace(header(0, 0), {}));
auto damaged = *bytes;
damaged[0] ^= 1U;
CHECK(!decode_trace(damaged));
damaged = *bytes;
damaged.pop_back();
CHECK(!decode_trace(damaged));
damaged = *bytes;
damaged[96 + 72] = 7; // First report's tick: no longer contiguous.
CHECK(!decode_trace(damaged));
damaged = *bytes;
damaged[88] = 21; // End tick 21 with 20 steps: size check.
CHECK(!decode_trace(damaged));
}
void decisions_are_canonical() {
const BatchDecision producer{5, Actor::producer, Outcome::death, 10};
const BatchDecision consumer{5, Actor::consumer, Outcome::death, 11};
const std::array ok{producer, consumer};
auto bytes = encode_decisions(ok, 0, 5);
CHECK(bytes && bytes->size() == 2 * decision_bytes);
auto back = decode_decisions(*bytes, 0, 5);
CHECK(back && std::ranges::equal(*back, ok));
CHECK(encode_decisions({}, 0, 5) && encode_decisions({}, 0, 5)->empty());
const std::array reversed{consumer, producer};
CHECK(!encode_decisions(reversed, 0, 5));
const std::array twice{producer, BatchDecision{6, Actor::producer}};
CHECK(!encode_decisions(twice, 0, 6)); // An actor dies once.
CHECK(!encode_decisions(std::array{producer}, 5, 9)); // Tick not in (begin, end].
CHECK(!encode_decisions(std::array{producer}, 0, 4));
auto damaged = *bytes;
damaged[8] = 2; // Unknown actor.
CHECK(!decode_decisions(damaged, 0, 5));
damaged = *bytes;
damaged[12] = 2; // Unknown outcome.
CHECK(!decode_decisions(damaged, 0, 5));
damaged = *bytes;
damaged[9] = 1; // Actor 256: never truncated onto a known actor.
CHECK(!decode_decisions(damaged, 0, 5));
CHECK(!decode_decisions(std::span{*bytes}.first(23), 0, 5));
std::vector<std::uint8_t> nine(9 * decision_bytes);
CHECK(!decode_decisions(nine, 0, 5));
}
} // namespace
int main() {
try {
recorder_seals_at_outcomes_and_capacity();
lineage_recorder_uses_archived_organisms();
traces_round_trip_and_bind_their_place();
decisions_are_canonical();
std::puts("region batch contracts passed");
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "%s\n", e.what());
return 1;
}
}