An epoch cannot order two writes an owner makes inside it

Two defects from review of 1c8b292.

Replacement checked the ownership epoch and nothing that ordered two writes made
inside one, so an owner could replay its own earlier record: a cohort installed
at tick 1,000 holding 50 picojoules accepted its own tick-900 record holding 100,
restoring inventory the installed one had already spent. A cohort now carries a
revision that strictly increases within an epoch, and a repeated revision is
refused as well as an older one, because a write has to be strictly later rather
than merely not earlier. A newer epoch may restart the series, since re-acquiring
ownership begins a new one, but the interval boundary is checked independently of
both, so neither advancing a revision nor re-acquiring ownership rewinds
simulated time. A later revision at the same boundary stays legitimate: that is a
correction, not a rewind.

The implicit move emptied a cohort's age classes while keeping its habitat,
genome and owner, so a moved-from record was exactly the invalid cohort create
refuses to build, and the registry accepted through a move what it rejects
through construction. The type is copy-only now, so a move decays to a copy and
the source stays whole. ADR-0030 made the genome copy-only for this same reason
in M3.1; the precedent existed and was not applied when this type was written.

Both fixes were confirmed load-bearing by reverting each alone. Removing either
ordering check fails the new regression. Removing the copy declarations fails to
compile, and not only through the static assertion: with that also removed, MSVC
proves the code after the class-count check unreachable, having established that
a moved-from vector is empty and the check always throws. That is a compile-time
proof rather than an execution, so the runtime path was checked separately under
ASan, where the same mutation exits 1.

Release 34/34, analysis, ASan 34/34 and clang-format all ran and passed here.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-09-24 09:05:05 -04:00
co-authored by Claude Opus 5
parent 1c8b292147
commit 5a4c34a903
8 changed files with 273 additions and 33 deletions
+16 -3
View File
@@ -50,7 +50,7 @@ std::expected<Cohort, CohortError> Cohort::create(CohortDraft draft) {
// Every identity a cohort names has to name something. A zero owner or epoch
// would be a record nobody is responsible for integrating.
if (draft.habitat.value == 0 || draft.genome == 0 || draft.owner.worker == 0 ||
draft.owner.epoch == 0 || draft.conversion == 0) {
draft.owner.epoch == 0 || draft.revision == 0 || draft.conversion == 0) {
return std::unexpected{CohortError::invalid_identity};
}
if (draft.classes.empty()) {
@@ -96,6 +96,7 @@ std::expected<Cohort, CohortError> Cohort::create(CohortDraft draft) {
cohort.genome_ = draft.genome;
cohort.owner_ = draft.owner;
cohort.last_tick_ = draft.last_tick;
cohort.revision_ = draft.revision;
cohort.conversion_ = draft.conversion;
cohort.classes_ = std::move(draft.classes);
cohort.bound_ = draft.bound;
@@ -154,11 +155,23 @@ std::expected<void, CohortError> CohortRegistry::replace(const Cohort& cohort) {
if (cohort.owner().worker != at->owner().worker) {
return std::unexpected{CohortError::wrong_owner};
}
// Equal is accepted: an owner rewriting its own epoch is the ordinary case.
// Older is refused, so a writer holding a stale view cannot install it.
if (cohort.owner().epoch < at->owner().epoch) {
return std::unexpected{CohortError::stale_epoch};
}
// Within one epoch the revision is what orders two writes. Without it an
// owner could replay its own earlier record and restore inventory the
// installed one had already spent, because the epoch alone cannot tell the
// two apart. A newer epoch may restart the series, since re-acquiring
// ownership begins a new one.
if (cohort.owner().epoch == at->owner().epoch && cohort.revision() <= at->revision()) {
return std::unexpected{CohortError::stale_revision};
}
// Simulated time does not run backwards, whatever the epoch. This holds even
// where a new epoch restarts the revision series, so re-acquiring ownership
// is not a way to rewind the record.
if (cohort.last_tick() < at->last_tick()) {
return std::unexpected{CohortError::backward_tick};
}
*at = cohort;
return {};
}