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:
@@ -319,3 +319,16 @@ invariants rather than a byte layout. All nine construction and registry guards
|
|||||||
were confirmed load-bearing by removing each one and observing the suite fail.
|
were confirmed load-bearing by removing each one and observing the suite fail.
|
||||||
Native Release, analysis, ASan and formatting passed 34/34; Linux CI has not run
|
Native Release, analysis, ASan and formatting passed 34/34; Linux CI has not run
|
||||||
yet.
|
yet.
|
||||||
|
|
||||||
|
Review of `1c8b292` found two defects, both fixed with regressions confirmed to
|
||||||
|
fail without their fix. Replacement checked the ownership epoch but nothing that
|
||||||
|
ordered two writes inside one: a record installed at tick 1,000 holding 50
|
||||||
|
picojoules could be replaced by its own earlier tick-900 record holding 100,
|
||||||
|
restoring inventory the installed one had already spent. Cohorts now carry a
|
||||||
|
revision that strictly increases within an epoch, and the interval boundary is
|
||||||
|
checked independently, so neither advancing a revision nor re-acquiring ownership
|
||||||
|
rewinds simulated time. Separately, the implicit move emptied a cohort's age
|
||||||
|
classes while keeping its identities, and the registry then accepted through a
|
||||||
|
move exactly what `create` rejects by name; the type is now copy-only, as ADR-0030
|
||||||
|
already made the genome for the same reason. That precedent existed and should
|
||||||
|
have been applied when the type was written.
|
||||||
|
|||||||
@@ -53,10 +53,24 @@ not a count and is never added to one. Funding it is M4.3's problem.
|
|||||||
Ownership is exclusive and enforced rather than described. A `CohortRegistry`
|
Ownership is exclusive and enforced rather than described. A `CohortRegistry`
|
||||||
admits at most one cohort per habitat and genome, because two records of the same
|
admits at most one cohort per habitat and genome, because two records of the same
|
||||||
genome in one habitat would be two places the same biomass could be spent, which
|
genome in one habitat would be two places the same biomass could be spent, which
|
||||||
is exactly what ECOLOGY_MODEL locks against. Every cohort names an owner and an
|
is exactly what ECOLOGY_MODEL locks against.
|
||||||
epoch, and a registry replacement is refused unless it comes from the recorded
|
|
||||||
owner under the recorded epoch and leaves the habitat and genome unchanged. A late
|
A replacement must be a strictly later view of the same record by the same owner,
|
||||||
writer holding an old view cannot install it.
|
and an epoch alone cannot establish that. Two writes inside one epoch are
|
||||||
|
otherwise interchangeable, so an owner replaying its own earlier record would
|
||||||
|
restore inventory the installed one had already spent. Every cohort therefore
|
||||||
|
carries a revision that strictly increases within an epoch; a newer epoch may
|
||||||
|
restart the series, because re-acquiring ownership begins a new one. Independently
|
||||||
|
of both, the interval boundary may not run backwards, so neither advancing a
|
||||||
|
revision nor re-acquiring ownership is a way to rewind simulated time.
|
||||||
|
|
||||||
|
A cohort is copy-only. 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, and 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. ADR-0030
|
||||||
|
made the genome copy-only for the same reason: a validated value whose move leaves
|
||||||
|
it invalid is the same defect wherever it appears.
|
||||||
|
|
||||||
Approximation error is a declared field, carried and never computed here. M4.5
|
Approximation error is a declared field, carried and never computed here. M4.5
|
||||||
measures resolution equivalence; a bound this type calculated for itself would be
|
measures resolution equivalence; a bound this type calculated for itself would be
|
||||||
@@ -75,6 +89,12 @@ make residuals natural and determinism impossible. Leaving ownership as a
|
|||||||
documented rule with a field to check would repeat the mistake ADR-0034 corrected:
|
documented rule with a field to check would repeat the mistake ADR-0034 corrected:
|
||||||
an invariant nothing enforces is an invariant a later change removes quietly.
|
an invariant nothing enforces is an invariant a later change removes quietly.
|
||||||
Making the registry own conversion as well would settle M4.2 by implication.
|
Making the registry own conversion as well would settle M4.2 by implication.
|
||||||
|
Ordering replacements by epoch alone was tried and is wrong: it cannot separate
|
||||||
|
two writes an owner makes inside one epoch. Ordering by the interval boundary
|
||||||
|
alone would reject a legitimate correction at the same boundary, and ordering by
|
||||||
|
revision alone would let a newer revision rewind the boundary, so both are
|
||||||
|
checked. A validity-preserving move was considered and is not possible: leaving
|
||||||
|
the source valid would mean leaving it its classes, which is not a move.
|
||||||
|
|
||||||
## Benefits
|
## Benefits
|
||||||
|
|
||||||
@@ -112,9 +132,13 @@ representation version, a zero identity, no classes, classes that are unordered,
|
|||||||
overlapping or empty, a class holding reserve with nothing to hold it, a residual
|
overlapping or empty, a class holding reserve with nothing to hold it, a residual
|
||||||
of one member or more, and a summation that would overflow. Test that a registry
|
of one member or more, and a summation that would overflow. Test that a registry
|
||||||
refuses a second cohort for one habitat and genome, that replacement is refused
|
refuses a second cohort for one habitat and genome, that replacement is refused
|
||||||
under a wrong owner, a stale epoch or a changed habitat or genome, and that it
|
under a wrong owner, a stale epoch, a repeated or older revision within an epoch,
|
||||||
succeeds under the recorded owner. Test that a cohort names a genome the archive
|
a backward interval boundary, or a changed habitat or genome, and that it succeeds
|
||||||
can resolve ancestry for without storing that ancestry. Negative reserves are not
|
for a strictly later view. Test that a new epoch may restart the revision series
|
||||||
|
and still may not rewind the boundary, and that a moved-from cohort keeps its
|
||||||
|
classes so neither installing nor replacing with one bypasses construction. Test
|
||||||
|
that a cohort names a genome the archive can resolve ancestry for without storing
|
||||||
|
that ancestry. Negative reserves are not
|
||||||
representable, since every quantity here is unsigned; what is rejected instead is
|
representable, since every quantity here is unsigned; what is rejected instead is
|
||||||
a declared state that would require one. No genome schema, mutation catalog,
|
a declared state that would require one. No genome schema, mutation catalog,
|
||||||
archive, ledger, episode driver, ensemble, renderer or deployment changes.
|
archive, ledger, episode driver, ensemble, renderer or deployment changes.
|
||||||
|
|||||||
@@ -144,8 +144,11 @@ never mistaken for an available individual. Funding a residual against biomass,
|
|||||||
and the seeded rule that turns one into a member, are M4.3 and M4.4.
|
and the seeded rule that turns one into a member, are M4.3 and M4.4.
|
||||||
|
|
||||||
`CohortRegistry` enforces that no inventory is spendable in two tiers by admitting
|
`CohortRegistry` enforces that no inventory is spendable in two tiers by admitting
|
||||||
at most one record per habitat and genome, and by refusing a replacement from a
|
at most one record per habitat and genome, and by requiring a replacement to be a
|
||||||
different worker or an older epoch. This is a single-process invariant: epochs
|
strictly later view of the same record: same worker, no older epoch, an advancing
|
||||||
|
revision within an epoch, and an interval boundary that never runs backwards. An
|
||||||
|
epoch alone is too coarse, because an owner replaying its own earlier record
|
||||||
|
inside one epoch would restore inventory the installed record had already spent. This is a single-process invariant: epochs
|
||||||
make a stale writer fail locally and are not cross-worker fencing, which is M7 and
|
make a stale writer fail locally and are not cross-worker fencing, which is M7 and
|
||||||
M10. The registry holds no lock, lease or quorum, and nothing is serialized.
|
M10. The registry holds no lock, lease or quorum, and nothing is serialized.
|
||||||
|
|
||||||
|
|||||||
@@ -918,3 +918,46 @@ fingerprint `2cd4a15f9fbe9949`, the ensemble fingerprint `5ec59d390909b526` and
|
|||||||
the frozen genome fixture are all unchanged, and the other 33 tests pass
|
the frozen genome fixture are all unchanged, and the other 33 tests pass
|
||||||
untouched. No deployment manifest or image pin changed. Linux validation is
|
untouched. No deployment manifest or image pin changed. Linux validation is
|
||||||
pending; no Gitea result is claimed since `b01a824`.
|
pending; no Gitea result is claimed since `b01a824`.
|
||||||
|
|
||||||
|
Review of `1c8b292` found two defects (2026-09-24), both fixed with regressions
|
||||||
|
confirmed to fail against the unfixed code.
|
||||||
|
|
||||||
|
Replacement checked the ownership epoch and nothing that ordered two writes made
|
||||||
|
inside one. A record installed at tick 1,000 holding 50 picojoules could be
|
||||||
|
replaced by its own earlier tick-900 record holding 100, restoring inventory the
|
||||||
|
installed record had already spent, because within an epoch the two were
|
||||||
|
interchangeable. A cohort now carries a revision that strictly increases within an
|
||||||
|
epoch, and a repeated revision is refused as well as an older one, since a write
|
||||||
|
has to be strictly later rather than merely not earlier. A newer epoch may restart
|
||||||
|
the series, because re-acquiring ownership begins a new one. The interval boundary
|
||||||
|
is checked independently of both, so neither advancing a revision nor re-acquiring
|
||||||
|
ownership is a way to rewind 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 the invalid cohort `create` refuses to build
|
||||||
|
and the registry accepted through a move what it rejects through construction. The
|
||||||
|
type is now copy-only, which suppresses the implicit moves so a move decays to a
|
||||||
|
copy. ADR-0030 made the genome copy-only for exactly this 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 the
|
||||||
|
revision check fails the new ordering regression, as does removing the backward
|
||||||
|
boundary check. Removing the copy declarations fails to compile in Release, and
|
||||||
|
not because of the static assertion: with that assertion also removed, MSVC proves
|
||||||
|
the code after `source.classes().size() == 3` unreachable, having established that
|
||||||
|
a moved-from vector is empty and the check therefore always throws. That is a
|
||||||
|
compile-time proof of the assertion rather than an execution of it, so the runtime
|
||||||
|
path was checked separately under the ASan preset, where the same mutation exits 1.
|
||||||
|
|
||||||
|
Coverage limits are unchanged from the increment above, with one addition. The
|
||||||
|
revision is a caller-supplied counter, not one the registry issues, so nothing
|
||||||
|
prevents a caller from choosing revisions that are unrelated to its own write
|
||||||
|
order; what is enforced is only that the registry refuses a record that is not
|
||||||
|
strictly later than the one it holds. Issuing revisions, and fencing between
|
||||||
|
workers, remain M7 and M10.
|
||||||
|
|
||||||
|
No existing contract changed. The proof golden `d61ad5bf0c928c76`, the ecosystem
|
||||||
|
fingerprint `2cd4a15f9fbe9949`, the ensemble fingerprint `5ec59d390909b526` and
|
||||||
|
the frozen genome fixture are unchanged. Linux validation is pending; no Gitea
|
||||||
|
result is claimed since `b01a824`.
|
||||||
|
|||||||
@@ -64,8 +64,13 @@ struct CohortDraft {
|
|||||||
// archive's answer, resolved from this identity.
|
// archive's answer, resolved from this identity.
|
||||||
std::uint64_t genome{};
|
std::uint64_t genome{};
|
||||||
Owner owner{};
|
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{};
|
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.
|
// The conversion that produced this record, kept as provenance.
|
||||||
std::uint64_t conversion{};
|
std::uint64_t conversion{};
|
||||||
std::vector<AgeClass> classes;
|
std::vector<AgeClass> classes;
|
||||||
@@ -85,6 +90,8 @@ enum class CohortError : std::uint8_t {
|
|||||||
unknown_cohort,
|
unknown_cohort,
|
||||||
wrong_owner,
|
wrong_owner,
|
||||||
stale_epoch,
|
stale_epoch,
|
||||||
|
stale_revision,
|
||||||
|
backward_tick,
|
||||||
registry_full
|
registry_full
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -97,6 +104,14 @@ inline constexpr std::size_t max_cohorts = 4'096;
|
|||||||
class Cohort {
|
class Cohort {
|
||||||
public:
|
public:
|
||||||
[[nodiscard]] static std::expected<Cohort, CohortError> create(CohortDraft draft);
|
[[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 {
|
[[nodiscard]] std::uint32_t representation_version() const noexcept {
|
||||||
return version_;
|
return version_;
|
||||||
@@ -113,6 +128,9 @@ class Cohort {
|
|||||||
[[nodiscard]] Tick last_tick() const noexcept {
|
[[nodiscard]] Tick last_tick() const noexcept {
|
||||||
return last_tick_;
|
return last_tick_;
|
||||||
}
|
}
|
||||||
|
[[nodiscard]] std::uint64_t revision() const noexcept {
|
||||||
|
return revision_;
|
||||||
|
}
|
||||||
[[nodiscard]] std::uint64_t conversion() const noexcept {
|
[[nodiscard]] std::uint64_t conversion() const noexcept {
|
||||||
return conversion_;
|
return conversion_;
|
||||||
}
|
}
|
||||||
@@ -143,6 +161,7 @@ class Cohort {
|
|||||||
std::uint64_t genome_{};
|
std::uint64_t genome_{};
|
||||||
Owner owner_{};
|
Owner owner_{};
|
||||||
Tick last_tick_{};
|
Tick last_tick_{};
|
||||||
|
std::uint64_t revision_{};
|
||||||
std::uint64_t conversion_{};
|
std::uint64_t conversion_{};
|
||||||
std::vector<AgeClass> classes_;
|
std::vector<AgeClass> classes_;
|
||||||
ApproximationBound bound_{};
|
ApproximationBound bound_{};
|
||||||
@@ -155,9 +174,12 @@ class Cohort {
|
|||||||
class CohortRegistry {
|
class CohortRegistry {
|
||||||
public:
|
public:
|
||||||
[[nodiscard]] std::expected<void, CohortError> install(const Cohort& cohort);
|
[[nodiscard]] std::expected<void, CohortError> install(const Cohort& cohort);
|
||||||
// Refused unless it comes from the recorded owner under the recorded epoch and
|
// Refused unless it is a strictly later view of the same record by the same
|
||||||
// leaves habitat and genome unchanged, so a writer holding an older view fails
|
// owner: the habitat and genome must match, the epoch must not be older, the
|
||||||
// rather than overwriting a newer one.
|
// 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<void, CohortError> replace(const Cohort& cohort);
|
||||||
[[nodiscard]] std::expected<Cohort, CohortError> find(HabitatId habitat,
|
[[nodiscard]] std::expected<Cohort, CohortError> find(HabitatId habitat,
|
||||||
std::uint64_t genome) const;
|
std::uint64_t genome) const;
|
||||||
|
|||||||
@@ -26,7 +26,8 @@ materializes anything.
|
|||||||
| genome | u64 | The genome every member carries. Nonzero |
|
| genome | u64 | The genome every member carries. Nonzero |
|
||||||
| owner worker | u64 | Who integrates this cohort. Nonzero |
|
| owner worker | u64 | Who integrates this cohort. Nonzero |
|
||||||
| owner epoch | u64 | Which epoch that ownership is held under. Nonzero |
|
| owner epoch | u64 | Which epoch that ownership is held under. Nonzero |
|
||||||
| last tick | u64 | The last interval boundary this record was integrated to |
|
| last tick | u64 | The last interval boundary integrated to. Never runs backwards |
|
||||||
|
| revision | u64 | Nonzero. Strictly increases with every replacement within an epoch |
|
||||||
| conversion | u64 | The conversion that produced this record. Nonzero provenance |
|
| conversion | u64 | The conversion that produced this record. Nonzero provenance |
|
||||||
| age classes | 1 to 64 | Ordered, disjoint, each non-empty. See below |
|
| age classes | 1 to 64 | Ordered, disjoint, each non-empty. See below |
|
||||||
| approximation bound | count + inventory | Declared, never computed here |
|
| approximation bound | count + inventory | Declared, never computed here |
|
||||||
@@ -72,7 +73,7 @@ error instead.
|
|||||||
| Rejected | Error |
|
| Rejected | Error |
|
||||||
|---|---|
|
|---|---|
|
||||||
| Representation version other than 1 | `unsupported_version` |
|
| Representation version other than 1 | `unsupported_version` |
|
||||||
| Zero habitat, genome, owner worker, owner epoch or conversion | `invalid_identity` |
|
| Zero habitat, genome, owner worker, owner epoch, revision or conversion | `invalid_identity` |
|
||||||
| No age classes | `no_age_classes` |
|
| No age classes | `no_age_classes` |
|
||||||
| More than 64 age classes | `overflow` |
|
| More than 64 age classes | `overflow` |
|
||||||
| `age_high <= age_low`, or a class overlapping or preceding the one before it | `unordered_age_classes` |
|
| `age_high <= age_low`, or a class overlapping or preceding the one before it | `unordered_age_classes` |
|
||||||
@@ -99,9 +100,23 @@ forbidden by convention.
|
|||||||
| Replacing a record that is not installed, or naming a different habitat or genome | `unknown_cohort` |
|
| Replacing a record that is not installed, or naming a different habitat or genome | `unknown_cohort` |
|
||||||
| Replacing under a different worker | `wrong_owner` |
|
| Replacing under a different worker | `wrong_owner` |
|
||||||
| Replacing under an older epoch | `stale_epoch` |
|
| Replacing under an older epoch | `stale_epoch` |
|
||||||
|
| Replacing with a repeated or older revision inside one epoch | `stale_revision` |
|
||||||
|
| Replacing with an earlier interval boundary, under any epoch | `backward_tick` |
|
||||||
| Installing beyond `max_cohorts` (4,096) | `registry_full` |
|
| Installing beyond `max_cohorts` (4,096) | `registry_full` |
|
||||||
|
|
||||||
An owner replacing under its own current epoch is the ordinary case and is
|
A replacement has to be a strictly later view of the same record. An epoch alone
|
||||||
accepted. Epochs make a stale writer fail locally; they say nothing about fencing
|
cannot establish that, because two writes an owner makes inside one epoch are
|
||||||
between workers, which is M7 and M10. The registry is in-memory, single-process
|
otherwise interchangeable and replaying the earlier one would restore inventory
|
||||||
and holds no lock, lease or quorum.
|
the installed record had already spent. The revision orders writes within an
|
||||||
|
epoch; a newer epoch may restart the series, since re-acquiring ownership begins a
|
||||||
|
new one. 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 is legitimate and accepted: it is a correction.
|
||||||
|
|
||||||
|
Epochs make a stale writer fail locally; they say nothing about fencing between
|
||||||
|
workers, which is M7 and M10. The registry is in-memory, single-process and holds
|
||||||
|
no lock, lease or quorum.
|
||||||
|
|
||||||
|
A cohort is copy-only. A moved-from record would keep its identities and lose its
|
||||||
|
classes, which is the invalid state construction refuses, so the implicit moves
|
||||||
|
are suppressed and a move decays to a copy.
|
||||||
|
|||||||
+16
-3
@@ -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
|
// Every identity a cohort names has to name something. A zero owner or epoch
|
||||||
// would be a record nobody is responsible for integrating.
|
// would be a record nobody is responsible for integrating.
|
||||||
if (draft.habitat.value == 0 || draft.genome == 0 || draft.owner.worker == 0 ||
|
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};
|
return std::unexpected{CohortError::invalid_identity};
|
||||||
}
|
}
|
||||||
if (draft.classes.empty()) {
|
if (draft.classes.empty()) {
|
||||||
@@ -96,6 +96,7 @@ std::expected<Cohort, CohortError> Cohort::create(CohortDraft draft) {
|
|||||||
cohort.genome_ = draft.genome;
|
cohort.genome_ = draft.genome;
|
||||||
cohort.owner_ = draft.owner;
|
cohort.owner_ = draft.owner;
|
||||||
cohort.last_tick_ = draft.last_tick;
|
cohort.last_tick_ = draft.last_tick;
|
||||||
|
cohort.revision_ = draft.revision;
|
||||||
cohort.conversion_ = draft.conversion;
|
cohort.conversion_ = draft.conversion;
|
||||||
cohort.classes_ = std::move(draft.classes);
|
cohort.classes_ = std::move(draft.classes);
|
||||||
cohort.bound_ = draft.bound;
|
cohort.bound_ = draft.bound;
|
||||||
@@ -154,11 +155,23 @@ std::expected<void, CohortError> CohortRegistry::replace(const Cohort& cohort) {
|
|||||||
if (cohort.owner().worker != at->owner().worker) {
|
if (cohort.owner().worker != at->owner().worker) {
|
||||||
return std::unexpected{CohortError::wrong_owner};
|
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) {
|
if (cohort.owner().epoch < at->owner().epoch) {
|
||||||
return std::unexpected{CohortError::stale_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;
|
*at = cohort;
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|||||||
+118
-11
@@ -1,4 +1,4 @@
|
|||||||
#include "evo/biology/lineage.hpp"
|
#include "evo/biology/lineage.hpp"
|
||||||
#include "evo/ecology/cohort.hpp"
|
#include "evo/ecology/cohort.hpp"
|
||||||
|
|
||||||
#include <expected>
|
#include <expected>
|
||||||
@@ -40,6 +40,7 @@ CohortDraft draft() {
|
|||||||
source.genome = 3;
|
source.genome = 3;
|
||||||
source.owner = {11, 2};
|
source.owner = {11, 2};
|
||||||
source.last_tick = Tick{900};
|
source.last_tick = Tick{900};
|
||||||
|
source.revision = 4;
|
||||||
source.conversion = 5;
|
source.conversion = 5;
|
||||||
source.classes = {{0, 100, 40, held(400), 0},
|
source.classes = {{0, 100, 40, held(400), 0},
|
||||||
{100, 200, 25, held(900), 250'000},
|
{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.genome() == 3);
|
||||||
CHECK(cohort.owner() == Owner{11, 2});
|
CHECK(cohort.owner() == Owner{11, 2});
|
||||||
CHECK(cohort.last_tick() == Tick{900});
|
CHECK(cohort.last_tick() == Tick{900});
|
||||||
|
CHECK(cohort.revision() == 4);
|
||||||
CHECK(cohort.conversion() == 5);
|
CHECK(cohort.conversion() == 5);
|
||||||
CHECK(cohort.bound().count == 2);
|
CHECK(cohort.bound().count == 2);
|
||||||
CHECK(cohort.bound().inventory == held(30));
|
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));
|
CHECK(refused(Cohort::create(wrong_version), CohortError::unsupported_version));
|
||||||
// Each identity a cohort names has to name something, including its owner:
|
// Each identity a cohort names has to name something, including its owner:
|
||||||
// a record nobody is responsible for integrating has no owner at all.
|
// 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();
|
auto blank = draft();
|
||||||
if (missing == 0) {
|
if (missing == 0) {
|
||||||
blank.habitat = {};
|
blank.habitat = {};
|
||||||
@@ -127,6 +129,8 @@ void every_invariant_is_rejected_by_name() {
|
|||||||
blank.owner.worker = 0;
|
blank.owner.worker = 0;
|
||||||
} else if (missing == 3) {
|
} else if (missing == 3) {
|
||||||
blank.owner.epoch = 0;
|
blank.owner.epoch = 0;
|
||||||
|
} else if (missing == 4) {
|
||||||
|
blank.revision = 0;
|
||||||
} else {
|
} else {
|
||||||
blank.conversion = 0;
|
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.
|
// A different worker is refused however current its epoch.
|
||||||
auto intruder = draft();
|
auto intruder = draft();
|
||||||
intruder.owner = {12, 99};
|
intruder.owner = {12, 99};
|
||||||
|
intruder.revision = 99;
|
||||||
CHECK(refused(registry.replace(built(intruder)), CohortError::wrong_owner));
|
CHECK(refused(registry.replace(built(intruder)), CohortError::wrong_owner));
|
||||||
// A writer holding an older view of its own cohort is refused too.
|
// A writer holding an older view of its own cohort is refused too.
|
||||||
auto stale = draft();
|
auto stale = draft();
|
||||||
stale.owner = {11, 1};
|
stale.owner = {11, 1};
|
||||||
CHECK(refused(registry.replace(built(stale)), CohortError::stale_epoch));
|
CHECK(refused(registry.replace(built(stale)), CohortError::stale_epoch));
|
||||||
CHECK(registry.find(HabitatId{7}, 3)->last_tick() == Tick{900});
|
CHECK(registry.find(HabitatId{7}, 3)->last_tick() == Tick{900});
|
||||||
// The owner rewriting its own epoch, or advancing it, succeeds.
|
// Advancing the record under the same epoch requires a newer revision.
|
||||||
auto same = draft();
|
auto advanced_revision = draft();
|
||||||
same.last_tick = Tick{1'000};
|
advanced_revision.last_tick = Tick{1'000};
|
||||||
CHECK(registry.replace(built(same)).has_value());
|
advanced_revision.revision = 5;
|
||||||
|
CHECK(registry.replace(built(advanced_revision)).has_value());
|
||||||
CHECK(registry.find(HabitatId{7}, 3)->last_tick() == Tick{1'000});
|
CHECK(registry.find(HabitatId{7}, 3)->last_tick() == Tick{1'000});
|
||||||
auto advanced = draft();
|
// A newer epoch may restart the revision series, because re-acquiring
|
||||||
advanced.owner = {11, 3};
|
// ownership begins a new one.
|
||||||
advanced.last_tick = Tick{1'100};
|
auto reacquired = draft();
|
||||||
CHECK(registry.replace(built(advanced)).has_value());
|
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)->owner() == Owner{11, 3});
|
||||||
|
CHECK(registry.find(HabitatId{7}, 3)->revision() == 1);
|
||||||
// Having advanced, the previously current epoch is now the stale one.
|
// 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});
|
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
|
// 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.
|
// 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.
|
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() {
|
void lineage_stays_the_archives_answer() {
|
||||||
// A cohort names a genome and never stores its ancestry, so it cannot claim a
|
// 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.
|
// parentage the archive would contradict, and invents none from membership.
|
||||||
@@ -313,6 +418,8 @@ int main() try {
|
|||||||
every_invariant_is_rejected_by_name();
|
every_invariant_is_rejected_by_name();
|
||||||
one_habitat_and_genome_holds_one_cohort();
|
one_habitat_and_genome_holds_one_cohort();
|
||||||
only_the_recorded_owner_may_replace_a_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();
|
lineage_stays_the_archives_answer();
|
||||||
std::cout << "PASS cohort state, declared resolution and exclusive ownership\n";
|
std::cout << "PASS cohort state, declared resolution and exclusive ownership\n";
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user