Account for the interval a model proposes; never clamp it

M4.3 advances a cohort over one declared interval and deliberately does not model
one. ECOLOGY_MODEL keeps population maths an experiment and asks for several
candidates to be compared, so choosing one here would turn a recommendation into
a locked rule. A model proposes fluxes; this checks the declared result is
exactly what they imply, for counts and for every inventory channel.

Nothing is clamped. A loss larger than what is there refuses the interval and
says whether it was the population or the biomass, so a caller can subdivide;
subtraction is checked against what is present rather than performed and
repaired, so a negative population is never constructed. That is the answer to
the named risk: a clamp is how an interval a model could not integrate becomes a
plausible result, and how a false extinction arrives. Both clamp mutations are
detected by the tests.

Counts carry whole members and owed fractions as one quantity, so a population
below one member is a number rather than a rounding decision. Biomass has no
birth term at all, so a birth cannot bring inventory in and newborns are
necessarily funded from what the cohort holds. Immigration draws from a reservoir
that already held it, so a late transfer is not new production. Conservation runs
through the ledger as one atomic transaction; an interval where nothing moves
submits none.

Two amendments to M4.1 were forced, not chosen. A cohort records the reservoir
backing it, because an advance cannot otherwise know where the inventory is and
passing it alongside would let a caller advance one cohort against another's
store, which comparing balances cannot detect between equal reserves; the
registry now refuses two cohorts sharing one. And a cohort may be vacant, through
a separate factory so vacancy stays deliberate, because a decline to exactly zero
was otherwise unrepresentable and the only way through would have been the clamp
this milestone exists to refuse. A vacant cohort is not an extinction: the
record, its genome, its habitat and its provenance remain and it stays installed.

Fourteen guards confirmed load-bearing by removing each and rerunning. Two of my
own expectations were wrong rather than the code: escrow arithmetic in the
migration test, and a dwindling case that shed all but one femtogram while
declaring a result holding none. The second surfaced a real tension now recorded
in ADR-0038 rather than worked around: a class reduced to a fraction of a member
may hold no reserve, because reserve belongs to members and a fraction is not one
yet, so that biomass has to go somewhere the same interval.

Closure is checked and stability is not, so an interval whose arithmetic balances
and whose ecology is nonsense is accepted. The declared age structure is not
verified to be what ageing would produce, because that needs the within-class
distribution the record discards.

Release 36/36, analysis, ASan 36/36 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 10:23:09 -04:00
co-authored by Claude Opus 5
parent 0697895e25
commit cabada70c4
16 changed files with 1331 additions and 8 deletions
+79 -2
View File
@@ -42,6 +42,7 @@ CohortDraft draft() {
source.last_tick = Tick{900};
source.revision = 4;
source.conversion = 5;
source.reservoir = {1};
source.classes = {{0, 100, 40, held(400), 0},
{100, 200, 25, held(900), 250'000},
{200, 300, 10, held(1'000), 999'999}};
@@ -68,6 +69,9 @@ void a_cohort_keeps_exactly_what_it_was_given() {
CHECK(cohort.last_tick() == Tick{900});
CHECK(cohort.revision() == 4);
CHECK(cohort.conversion() == 5);
CHECK(cohort.reservoir() == ReservoirId{1});
CHECK(!cohort.is_vacant());
CHECK(cohort.population() == cohort.count() * residual_scale + cohort.residual());
CHECK(cohort.bound().count == 2);
CHECK(cohort.bound().inventory == held(30));
CHECK(cohort.classes().size() == 3);
@@ -119,7 +123,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, 5U}) {
for (auto missing : {0U, 1U, 2U, 3U, 4U, 5U, 6U}) {
auto blank = draft();
if (missing == 0) {
blank.habitat = {};
@@ -131,8 +135,12 @@ void every_invariant_is_rejected_by_name() {
blank.owner.epoch = 0;
} else if (missing == 4) {
blank.revision = 0;
} else {
} else if (missing == 5) {
blank.conversion = 0;
} else {
// A cohort that does not say where its reserve is could be advanced
// against another cohort's reservoir.
blank.reservoir = {};
}
CHECK(refused(Cohort::create(blank), CohortError::invalid_identity));
}
@@ -220,8 +228,10 @@ void one_habitat_and_genome_holds_one_cohort() {
// Another genome, or the same genome elsewhere, is a different record.
auto other_genome = draft();
other_genome.genome = 4;
other_genome.reservoir = {2};
auto other_habitat = draft();
other_habitat.habitat = {8};
other_habitat.reservoir = {3};
CHECK(registry.install(built(other_genome)).has_value());
CHECK(registry.install(built(other_habitat)).has_value());
CHECK(registry.size() == 3);
@@ -370,6 +380,70 @@ void a_moved_from_cohort_is_still_a_valid_cohort() {
CHECK(refused(Cohort::create(hollow), CohortError::no_age_classes));
}
void one_reservoir_backs_one_cohort() {
// Two records sharing a reservoir would be the same inventory in two places,
// which is the whole point of the registry. Habitat and genome differing does
// not make it two stores.
CohortRegistry registry;
CHECK(registry.install(valid()).has_value());
auto sharing = draft();
sharing.genome = 4;
CHECK(refused(registry.install(built(sharing)), CohortError::duplicate_cohort));
auto elsewhere_sharing = draft();
elsewhere_sharing.habitat = {8};
CHECK(refused(registry.install(built(elsewhere_sharing)), CohortError::duplicate_cohort));
CHECK(registry.size() == 1);
auto separate = draft();
separate.genome = 4;
separate.reservoir = {2};
CHECK(registry.install(built(separate)).has_value());
CHECK(registry.size() == 2);
// A replacement cannot move a record to a different store either: that would
// be other inventory holding this cohort, not a later view of this one.
auto rehomed = draft();
rehomed.revision = 9;
rehomed.reservoir = {3};
CHECK(refused(registry.replace(built(rehomed)), CohortError::unknown_cohort));
CHECK(registry.find(HabitatId{7}, 3)->reservoir() == ReservoirId{1});
}
void vacancy_is_deliberate_and_keeps_the_record() {
// A population that reaches exactly zero has to be representable, or a caller
// would have to clamp, which ECOLOGY_MODEL forbids. It is a separate factory
// so it can never happen by leaving the classes out of a draft by accident.
auto empty_draft = draft();
empty_draft.classes.clear();
CHECK(refused(Cohort::create(empty_draft), CohortError::no_age_classes));
CHECK(refused(Cohort::vacant(draft()), CohortError::not_vacant));
const auto vacated = Cohort::vacant(empty_draft);
CHECK(vacated.has_value());
CHECK(vacated->is_vacant());
CHECK(vacated->count() == 0);
CHECK(vacated->residual() == 0);
CHECK(vacated->population() == 0);
CHECK(vacated->reserve() == Inventory{});
// Everything that identifies the record survives being empty. A vacant cohort
// is not an extinction: the genome and its habitat are both still here.
CHECK(vacated->genome() == 3);
CHECK(vacated->habitat() == HabitatId{7});
CHECK(vacated->owner() == Owner{11, 2});
CHECK(vacated->conversion() == 5);
CHECK(vacated->reservoir() == ReservoirId{1});
CHECK(vacated->last_tick() == Tick{900});
// The identities are still required, borrowed from the same checks.
auto anonymous = empty_draft;
anonymous.genome = 0;
CHECK(refused(Cohort::vacant(anonymous), CohortError::invalid_identity));
auto homeless = empty_draft;
homeless.reservoir = {};
CHECK(refused(Cohort::vacant(homeless), CohortError::invalid_identity));
// It installs and is kept like any other record.
CohortRegistry registry;
CHECK(registry.install(*vacated).has_value());
CHECK(registry.find(HabitatId{7}, 3)->is_vacant());
CHECK(registry.size() == 1);
}
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.
@@ -407,6 +481,7 @@ void lineage_stays_the_archives_answer() {
CHECK(registry.install(cohort).has_value());
auto sibling = draft();
sibling.genome = 3;
sibling.reservoir = {2};
CHECK(registry.install(built(sibling)).has_value());
CHECK(registry.size() == 2);
CHECK(registry.find(HabitatId{7}, 3)->genome() != registry.find(HabitatId{7}, 9)->genome());
@@ -418,6 +493,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();
one_reservoir_backs_one_cohort();
vacancy_is_deliberate_and_keeps_the_record();
an_epoch_alone_cannot_order_two_writes();
a_moved_from_cohort_is_still_a_valid_cohort();
lineage_stays_the_archives_answer();