Files
aevolution/schemas/population-v1.md
T
dingoandClaude Opus 5 5a4c34a903 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>
2026-09-24 09:05:05 -04:00

123 lines
6.1 KiB
Markdown

# Cohort record, representation version 1
Scope: the tier 2 record defined by
[ADR-0036](../docs/adr/0036-cohort-state-and-ownership.md) and shaped by
[ECOLOGY_MODEL.md](../docs/biology/ECOLOGY_MODEL.md). Its validator is
`src/ecology/cohort.cpp`.
There is no byte layout here and no encoder. Unlike the genome record, a cohort
is not hashed, not a mutation context and not exchanged between processes yet, so
a wire format would be a format nothing writes or reads. Durable storage and
cross-version migration are M7 and M8, and the version field exists so a later
schema can fail closed on records it does not understand rather than guess. What
this document pins is the meaning of each field and the invariants a record must
satisfy to exist at all.
Not a network protocol, a durable format, a habitat geometry, a conversion
transaction or a population model. Nothing here advances, collapses or
materializes anything.
## Record
| Field | Type | Meaning |
|---|---|---|
| representation version | u32 | 1. Unknown versions fail closed |
| habitat | u64 | Where the cohort is. Nonzero. Carries no geometry in version 1 |
| genome | u64 | The genome every member carries. Nonzero |
| owner worker | u64 | Who integrates this cohort. Nonzero |
| owner epoch | u64 | Which epoch that ownership is held under. Nonzero |
| 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 |
| age classes | 1 to 64 | Ordered, disjoint, each non-empty. See below |
| approximation bound | count + inventory | Declared, never computed here |
Lineage is deliberately absent. Ancestry is resolved from the genome through
`LineageArchive`, so a cohort cannot hold a parentage that contradicts the
archive's and cannot invent precise parentage from a statistical membership,
which ECOLOGY_MODEL forbids.
Totals are absent for the same reason. Count, reserve and residual are summed
from the classes on demand; a stored total is a second tally that can disagree
with what it summarises.
## Age classes
| Field | Type | Meaning |
|---|---|---|
| age low | u64 | Ticks, inclusive |
| age high | u64 | Ticks, exclusive. Must exceed age low |
| count | u64 | Whole members in this class |
| reserve | inventory | Energy and four matter channels held by the class in total |
| residual | u64 | Fraction of a member owed, over `residual_scale` (1,000,000) |
Each class holds its own count and its own reserve, so the record stores a
distribution and not a mean. This is the point of the record: a count with a mean
reserve cannot distinguish a cohort where every member is provisioned from one
where half hold nothing, and no later stage can recover what that discards.
Within one class members are indistinguishable. That is the record's declared
resolution and a real loss. A caller needing finer structure declares narrower
classes; it does not divide a total afterwards. No accessor returns a mean,
because an average that is easy to reach becomes the authority.
A residual is owed and not yet present. It is not a count, is never added to one,
and is strictly less than one member, because at one member it is a member.
Funding a residual against biomass is M4.3.
## Invariants
A record that breaks any of these does not exist; construction returns the named
error instead.
| Rejected | Error |
|---|---|
| Representation version other than 1 | `unsupported_version` |
| Zero habitat, genome, owner worker, owner epoch, revision or conversion | `invalid_identity` |
| No age classes | `no_age_classes` |
| More than 64 age classes | `overflow` |
| `age_high <= age_low`, or a class overlapping or preceding the one before it | `unordered_age_classes` |
| A class with no members and nothing owed | `empty_age_class` |
| A class holding reserve with no members to own it | `unowned_reserve` |
| A residual of one member or more | `residual_out_of_range` |
| Counts, residuals or any inventory channel that would overflow when summed | `overflow` |
Every quantity is unsigned, so a negative reserve is not representable. What is
rejected is a declared state that would require one: reserve held by a class with
no members. Arithmetic that could produce one does not exist in this version,
because this version performs none.
## Ownership
`CohortRegistry` holds at most one record per habitat and genome. Two records of
one genome in one habitat would be two places the same biomass could be spent,
which is what ECOLOGY_MODEL locks against; here it is unrepresentable rather than
forbidden by convention.
| Rejected | Error |
|---|---|
| Installing a second record for one habitat and genome | `duplicate_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 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` |
A replacement has to be a strictly later view of the same record. An epoch alone
cannot establish that, because two writes an owner makes inside one epoch are
otherwise interchangeable and replaying the earlier one would restore inventory
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.