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:
+118
-11
@@ -1,4 +1,4 @@
|
||||
#include "evo/biology/lineage.hpp"
|
||||
#include "evo/biology/lineage.hpp"
|
||||
#include "evo/ecology/cohort.hpp"
|
||||
|
||||
#include <expected>
|
||||
@@ -40,6 +40,7 @@ CohortDraft draft() {
|
||||
source.genome = 3;
|
||||
source.owner = {11, 2};
|
||||
source.last_tick = Tick{900};
|
||||
source.revision = 4;
|
||||
source.conversion = 5;
|
||||
source.classes = {{0, 100, 40, held(400), 0},
|
||||
{100, 200, 25, held(900), 250'000},
|
||||
@@ -65,6 +66,7 @@ void a_cohort_keeps_exactly_what_it_was_given() {
|
||||
CHECK(cohort.genome() == 3);
|
||||
CHECK(cohort.owner() == Owner{11, 2});
|
||||
CHECK(cohort.last_tick() == Tick{900});
|
||||
CHECK(cohort.revision() == 4);
|
||||
CHECK(cohort.conversion() == 5);
|
||||
CHECK(cohort.bound().count == 2);
|
||||
CHECK(cohort.bound().inventory == held(30));
|
||||
@@ -117,7 +119,7 @@ void every_invariant_is_rejected_by_name() {
|
||||
CHECK(refused(Cohort::create(wrong_version), CohortError::unsupported_version));
|
||||
// Each identity a cohort names has to name something, including its owner:
|
||||
// a record nobody is responsible for integrating has no owner at all.
|
||||
for (auto missing : {0U, 1U, 2U, 3U, 4U}) {
|
||||
for (auto missing : {0U, 1U, 2U, 3U, 4U, 5U}) {
|
||||
auto blank = draft();
|
||||
if (missing == 0) {
|
||||
blank.habitat = {};
|
||||
@@ -127,6 +129,8 @@ void every_invariant_is_rejected_by_name() {
|
||||
blank.owner.worker = 0;
|
||||
} else if (missing == 3) {
|
||||
blank.owner.epoch = 0;
|
||||
} else if (missing == 4) {
|
||||
blank.revision = 0;
|
||||
} else {
|
||||
blank.conversion = 0;
|
||||
}
|
||||
@@ -235,24 +239,30 @@ void only_the_recorded_owner_may_replace_a_cohort() {
|
||||
// A different worker is refused however current its epoch.
|
||||
auto intruder = draft();
|
||||
intruder.owner = {12, 99};
|
||||
intruder.revision = 99;
|
||||
CHECK(refused(registry.replace(built(intruder)), CohortError::wrong_owner));
|
||||
// A writer holding an older view of its own cohort is refused too.
|
||||
auto stale = draft();
|
||||
stale.owner = {11, 1};
|
||||
CHECK(refused(registry.replace(built(stale)), CohortError::stale_epoch));
|
||||
CHECK(registry.find(HabitatId{7}, 3)->last_tick() == Tick{900});
|
||||
// The owner rewriting its own epoch, or advancing it, succeeds.
|
||||
auto same = draft();
|
||||
same.last_tick = Tick{1'000};
|
||||
CHECK(registry.replace(built(same)).has_value());
|
||||
// Advancing the record under the same epoch requires a newer revision.
|
||||
auto advanced_revision = draft();
|
||||
advanced_revision.last_tick = Tick{1'000};
|
||||
advanced_revision.revision = 5;
|
||||
CHECK(registry.replace(built(advanced_revision)).has_value());
|
||||
CHECK(registry.find(HabitatId{7}, 3)->last_tick() == Tick{1'000});
|
||||
auto advanced = draft();
|
||||
advanced.owner = {11, 3};
|
||||
advanced.last_tick = Tick{1'100};
|
||||
CHECK(registry.replace(built(advanced)).has_value());
|
||||
// A newer epoch may restart the revision series, because re-acquiring
|
||||
// ownership begins a new one.
|
||||
auto reacquired = draft();
|
||||
reacquired.owner = {11, 3};
|
||||
reacquired.last_tick = Tick{1'100};
|
||||
reacquired.revision = 1;
|
||||
CHECK(registry.replace(built(reacquired)).has_value());
|
||||
CHECK(registry.find(HabitatId{7}, 3)->owner() == Owner{11, 3});
|
||||
CHECK(registry.find(HabitatId{7}, 3)->revision() == 1);
|
||||
// Having advanced, the previously current epoch is now the stale one.
|
||||
CHECK(refused(registry.replace(built(same)), CohortError::stale_epoch));
|
||||
CHECK(refused(registry.replace(built(advanced_revision)), CohortError::stale_epoch));
|
||||
CHECK(registry.find(HabitatId{7}, 3)->last_tick() == Tick{1'100});
|
||||
// A record naming a different habitat or genome is not a newer view of this
|
||||
// one, and replacing a cohort never installed is refused outright.
|
||||
@@ -265,6 +275,101 @@ void only_the_recorded_owner_may_replace_a_cohort() {
|
||||
CHECK(registry.size() == 1); // A refused replacement installs nothing.
|
||||
}
|
||||
|
||||
void an_epoch_alone_cannot_order_two_writes() {
|
||||
// An owner replaying its own earlier record inside one epoch would restore
|
||||
// inventory the installed record had already spent. The epoch is equal in
|
||||
// both, so only the revision and the interval boundary can tell them apart.
|
||||
CohortRegistry registry;
|
||||
auto spent = draft();
|
||||
spent.last_tick = Tick{1'000};
|
||||
spent.revision = 5;
|
||||
spent.classes = {{0, 100, 40, held(50), 0}};
|
||||
CHECK(registry.install(built(spent)).has_value());
|
||||
auto replayed = draft(); // tick 900, revision 4, the richer earlier state.
|
||||
replayed.classes = {{0, 100, 40, held(100), 0}};
|
||||
CHECK(refused(registry.replace(built(replayed)), CohortError::stale_revision));
|
||||
// The installed record still holds what it held; nothing was restored.
|
||||
CHECK(registry.find(HabitatId{7}, 3)->reserve() == held(50));
|
||||
CHECK(registry.find(HabitatId{7}, 3)->last_tick() == Tick{1'000});
|
||||
// Re-sending the installed revision is refused as well: a write has to be
|
||||
// strictly later, not merely not-earlier.
|
||||
auto same_revision = spent;
|
||||
same_revision.classes = {{0, 100, 40, held(100), 0}};
|
||||
CHECK(refused(registry.replace(built(same_revision)), CohortError::stale_revision));
|
||||
CHECK(registry.find(HabitatId{7}, 3)->reserve() == held(50));
|
||||
// A newer revision that rewinds the boundary is refused too, so advancing a
|
||||
// counter is not a way to rewind simulated time.
|
||||
auto rewound = draft();
|
||||
rewound.last_tick = Tick{999};
|
||||
rewound.revision = 6;
|
||||
CHECK(refused(registry.replace(built(rewound)), CohortError::backward_tick));
|
||||
// Nor can re-acquiring ownership rewind it, though a new epoch may restart
|
||||
// the revision series.
|
||||
auto reacquired = draft();
|
||||
reacquired.owner = {11, 9};
|
||||
reacquired.revision = 1;
|
||||
reacquired.last_tick = Tick{999};
|
||||
CHECK(refused(registry.replace(built(reacquired)), CohortError::backward_tick));
|
||||
CHECK(registry.find(HabitatId{7}, 3)->last_tick() == Tick{1'000});
|
||||
// A strictly later write at the same boundary is the ordinary case.
|
||||
auto corrected = draft();
|
||||
corrected.last_tick = Tick{1'000};
|
||||
corrected.revision = 6;
|
||||
corrected.classes = {{0, 100, 39, held(40), 0}};
|
||||
CHECK(registry.replace(built(corrected)).has_value());
|
||||
CHECK(registry.find(HabitatId{7}, 3)->count() == 39);
|
||||
}
|
||||
|
||||
void a_moved_from_cohort_is_still_a_valid_cohort() {
|
||||
// A moved-from record would keep its identities while losing its classes,
|
||||
// which is the invalid cohort create refuses to build. The registry would
|
||||
// then accept through a move what it rejects through construction, so the
|
||||
// move is suppressed and decays to a copy.
|
||||
static_assert(std::is_copy_constructible_v<Cohort>);
|
||||
static_assert(std::is_copy_assignable_v<Cohort>);
|
||||
// Move-constructing is still well formed; it selects the copy.
|
||||
auto source = valid();
|
||||
const auto before = source;
|
||||
// Both diagnostics are the point of the check: clang-tidy is right that no
|
||||
// move happens here and wrong that it is a mistake.
|
||||
// NOLINTNEXTLINE(performance-move-const-arg)
|
||||
auto moved = std::move(source);
|
||||
// The source is untouched, because nothing was moved out of it.
|
||||
// NOLINTNEXTLINE(bugprone-use-after-move): the point of the check.
|
||||
CHECK(source == before);
|
||||
// NOLINTNEXTLINE(bugprone-use-after-move)
|
||||
CHECK(source.classes().size() == 3);
|
||||
// NOLINTNEXTLINE(bugprone-use-after-move)
|
||||
CHECK(source.count() == 75);
|
||||
CHECK(moved == before);
|
||||
// Both remain installable, and the source is not a record that bypasses
|
||||
// create's rejection of a cohort with no classes.
|
||||
CohortRegistry registry;
|
||||
CHECK(registry.install(source).has_value());
|
||||
CHECK(registry.find(HabitatId{7}, 3)->classes().size() == 3);
|
||||
CHECK(registry.find(HabitatId{7}, 3)->count() == 75);
|
||||
// The same holds for move assignment and for replacement.
|
||||
auto target = valid();
|
||||
// NOLINTNEXTLINE(performance-move-const-arg)
|
||||
target = std::move(moved);
|
||||
// NOLINTNEXTLINE(bugprone-use-after-move): the point of the check.
|
||||
CHECK(moved.classes().size() == 3);
|
||||
CHECK(target.classes().size() == 3);
|
||||
auto later = draft();
|
||||
later.revision = 5;
|
||||
auto replacement = built(later);
|
||||
// NOLINTNEXTLINE(performance-move-const-arg)
|
||||
CHECK(registry.replace(std::move(replacement)).has_value());
|
||||
// NOLINTNEXTLINE(bugprone-use-after-move): the point of the check.
|
||||
CHECK(replacement.classes().size() == 3);
|
||||
CHECK(registry.find(HabitatId{7}, 3)->count() == 75);
|
||||
// No cohort anywhere reports an empty class list, which is what a genuine
|
||||
// move would have produced and what create rejects by name.
|
||||
auto hollow = draft();
|
||||
hollow.classes.clear();
|
||||
CHECK(refused(Cohort::create(hollow), CohortError::no_age_classes));
|
||||
}
|
||||
|
||||
void lineage_stays_the_archives_answer() {
|
||||
// A cohort names a genome and never stores its ancestry, so it cannot claim a
|
||||
// parentage the archive would contradict, and invents none from membership.
|
||||
@@ -313,6 +418,8 @@ int main() try {
|
||||
every_invariant_is_rejected_by_name();
|
||||
one_habitat_and_genome_holds_one_cohort();
|
||||
only_the_recorded_owner_may_replace_a_cohort();
|
||||
an_epoch_alone_cannot_order_two_writes();
|
||||
a_moved_from_cohort_is_still_a_valid_cohort();
|
||||
lineage_stays_the_archives_answer();
|
||||
std::cout << "PASS cohort state, declared resolution and exclusive ownership\n";
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user