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>
145 lines
8.4 KiB
Markdown
145 lines
8.4 KiB
Markdown
# ADR-0036: Cohort state, declared resolution and exclusive ownership
|
|
|
|
## Status
|
|
|
|
Adopted for M4.1. It defines the tier 2 record and who may act on it, and nothing
|
|
else: there is no collapse transaction, no population advance, no materialization
|
|
and no habitat geometry. Those are M4.2 to M4.4 and each needs its own decision.
|
|
No M1 to M3 contract changes. ECOLOGY_MODEL's population math stays an experiment.
|
|
|
|
## Context
|
|
|
|
ECOLOGY_MODEL locks two things about tier 2 and leaves the rest recommended: no
|
|
inventory is spendable in two tiers, and distinct genomes are never averaged into
|
|
one genome. Everything M4 does afterwards moves inventory between a cohort and
|
|
individuals, so the record has to exist and has to be checkable before any of that
|
|
is written. M3 supplies what a cohort is about: a genome that validates, a
|
|
phenotype that derives from it and an archive that retains its ancestry.
|
|
|
|
The named risk for this step is that population averages hide viable outliers. A
|
|
record holding a count and a mean reserve cannot answer whether anyone in it would
|
|
survive, because a well-provisioned member and a starving one reduce to the same
|
|
average. That is a property of the record, not of the maths applied to it later,
|
|
so it has to be answered here or not at all.
|
|
|
|
## Decision
|
|
|
|
A `Cohort` is a validated value. `Cohort::create` takes a draft and either returns
|
|
a cohort meeting every invariant below or an error naming the one it broke; there
|
|
is no way to assemble one field by field and no way to mutate a field afterwards.
|
|
|
|
Age structure is a sequence of classes, each carrying its own count and its own
|
|
reserve. A cohort therefore stores a distribution and never a mean. Within one
|
|
class members are indistinguishable, which is a real loss and is the record's
|
|
declared resolution: the class bounds state it, and a reader that needs finer
|
|
structure declares narrower classes rather than dividing a total. No accessor
|
|
returns a mean, because an average that is easy to reach is an average that will
|
|
be used as the authority.
|
|
|
|
Totals are derived by summing the classes, never stored beside them. The lineage
|
|
archive already establishes this: a second tally is a thing that can disagree.
|
|
Summation rejects overflow rather than wrapping.
|
|
|
|
A cohort names the genome it carries and does not store its lineage. Ancestry is
|
|
the archive's answer, resolved from the genome, so a cohort cannot claim a
|
|
parentage the archive would contradict and cannot invent precise parentage from a
|
|
statistical membership, which ECOLOGY_MODEL forbids.
|
|
|
|
Fractional members are carried per class as a residual: an integer numerator over
|
|
a declared scale, strictly less than one member. Integer arithmetic only, as
|
|
everywhere else in this project. A residual is owed and not yet present, so it is
|
|
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`
|
|
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
|
|
is exactly what ECOLOGY_MODEL locks against.
|
|
|
|
A replacement must be a strictly later view of the same record by the same owner,
|
|
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
|
|
measures resolution equivalence; a bound this type calculated for itself would be
|
|
a claim it has no basis for.
|
|
|
|
## Alternatives Considered
|
|
|
|
Storing a count with mean age and mean reserve is smaller and is what the risk
|
|
warns about: it cannot distinguish a cohort that is fine from one where half the
|
|
members are about to starve, and no later stage can recover what it discarded.
|
|
Storing lineage on the cohort would make membership self-describing at the cost of
|
|
a second ancestry that can drift from the archive's and of inviting invented
|
|
parentage. Storing totals beside the classes would save a summation and introduce
|
|
a field that can disagree with the classes it summarises. Floating point would
|
|
make residuals natural and determinism impossible. Leaving ownership as a
|
|
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.
|
|
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
|
|
|
|
The record can answer who is viable, because it never averaged that away. Double
|
|
ownership of one genome's biomass in one habitat is unrepresentable rather than
|
|
forbidden. Every total is derived, so no two fields of a cohort can disagree.
|
|
Ancestry has exactly one authority. Residuals are explicit, bounded and integer,
|
|
so a fractional member can never be mistaken for an available one.
|
|
|
|
## Costs
|
|
|
|
A cohort is larger than a count and a mean, and every class costs an inventory.
|
|
Declaring classes is a modelling decision a caller has to make, and a caller that
|
|
declares one wide class gets the average it was trying to avoid. Totals cost a
|
|
summation on every query. The registry is a bounded in-memory container with no
|
|
persistence, no sharding and no cross-worker protocol; nothing here is serialized.
|
|
|
|
## Risks
|
|
|
|
A cohort is not evidence that aggregation is ecologically faithful. It holds a
|
|
declared approximation bound that nothing yet computes or checks, and carrying a
|
|
bound is not meeting one. The class boundaries are a chosen resolution, and
|
|
structure finer than one class is genuinely gone. Conservation between a cohort
|
|
and the rest of the world is the ledger's property and is not asserted by this
|
|
type, which holds an inventory without participating in any transfer. Ownership
|
|
here is a single-process invariant: epochs make a stale writer fail locally and
|
|
say nothing about fencing across workers, which is M7 and M10. Nothing in this
|
|
ADR says a population may advance, collapse or materialize.
|
|
|
|
## Consequences
|
|
|
|
Test that a valid cohort round-trips its declared fields, that totals equal the
|
|
summed classes, and that every invariant is rejected by name: an unsupported
|
|
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
|
|
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
|
|
under a wrong owner, a stale epoch, a repeated or older revision within an epoch,
|
|
a backward interval boundary, or a changed habitat or genome, and that it succeeds
|
|
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
|
|
a declared state that would require one. No genome schema, mutation catalog,
|
|
archive, ledger, episode driver, ensemble, renderer or deployment changes.
|