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:
@@ -64,8 +64,13 @@ struct CohortDraft {
|
||||
// archive's answer, resolved from this identity.
|
||||
std::uint64_t genome{};
|
||||
Owner owner{};
|
||||
// The last interval boundary this cohort was integrated to.
|
||||
// The last interval boundary this cohort was integrated to. It never runs
|
||||
// backwards: a record naming an earlier boundary is an older view.
|
||||
Tick last_tick{};
|
||||
// Strictly increases with every replacement within one owner epoch, so two
|
||||
// writes that share a boundary are still ordered. An epoch that is itself
|
||||
// newer may restart it, because re-acquiring ownership begins a new series.
|
||||
std::uint64_t revision{};
|
||||
// The conversion that produced this record, kept as provenance.
|
||||
std::uint64_t conversion{};
|
||||
std::vector<AgeClass> classes;
|
||||
@@ -85,6 +90,8 @@ enum class CohortError : std::uint8_t {
|
||||
unknown_cohort,
|
||||
wrong_owner,
|
||||
stale_epoch,
|
||||
stale_revision,
|
||||
backward_tick,
|
||||
registry_full
|
||||
};
|
||||
|
||||
@@ -97,6 +104,14 @@ inline constexpr std::size_t max_cohorts = 4'096;
|
||||
class Cohort {
|
||||
public:
|
||||
[[nodiscard]] static std::expected<Cohort, CohortError> create(CohortDraft draft);
|
||||
// Copy-only on purpose. A moved-from record would keep its habitat, genome,
|
||||
// owner and totals while losing its age classes, which is precisely the
|
||||
// invalid cohort `create` refuses to build; the registry would then accept
|
||||
// through a move what it rejects through construction. Declaring the copy
|
||||
// operations suppresses the implicit moves, so a move decays to a copy and
|
||||
// the source stays valid. A cohort is at most 64 classes, so that is cheap.
|
||||
Cohort(const Cohort&) = default;
|
||||
Cohort& operator=(const Cohort&) = default;
|
||||
|
||||
[[nodiscard]] std::uint32_t representation_version() const noexcept {
|
||||
return version_;
|
||||
@@ -113,6 +128,9 @@ class Cohort {
|
||||
[[nodiscard]] Tick last_tick() const noexcept {
|
||||
return last_tick_;
|
||||
}
|
||||
[[nodiscard]] std::uint64_t revision() const noexcept {
|
||||
return revision_;
|
||||
}
|
||||
[[nodiscard]] std::uint64_t conversion() const noexcept {
|
||||
return conversion_;
|
||||
}
|
||||
@@ -143,6 +161,7 @@ class Cohort {
|
||||
std::uint64_t genome_{};
|
||||
Owner owner_{};
|
||||
Tick last_tick_{};
|
||||
std::uint64_t revision_{};
|
||||
std::uint64_t conversion_{};
|
||||
std::vector<AgeClass> classes_;
|
||||
ApproximationBound bound_{};
|
||||
@@ -155,9 +174,12 @@ class Cohort {
|
||||
class CohortRegistry {
|
||||
public:
|
||||
[[nodiscard]] std::expected<void, CohortError> install(const Cohort& cohort);
|
||||
// Refused unless it comes from the recorded owner under the recorded epoch and
|
||||
// leaves habitat and genome unchanged, so a writer holding an older view fails
|
||||
// rather than overwriting a newer one.
|
||||
// Refused unless it is a strictly later view of the same record by the same
|
||||
// owner: the habitat and genome must match, the epoch must not be older, the
|
||||
// revision must advance within an epoch, and the interval boundary must not
|
||||
// run backwards. An epoch alone is too coarse, because two writes inside one
|
||||
// epoch would otherwise be interchangeable and replaying the earlier one
|
||||
// would restore inventory the later one had already spent.
|
||||
[[nodiscard]] std::expected<void, CohortError> replace(const Cohort& cohort);
|
||||
[[nodiscard]] std::expected<Cohort, CohortError> find(HabitatId habitat,
|
||||
std::uint64_t genome) const;
|
||||
|
||||
Reference in New Issue
Block a user