Files
aevolution/docs/adr/0037-collapse-to-cohort.md
T
dingoandClaude Opus 5 14e0c1af5e An empty reservoir is not an unowned one
Two defects from review of 380d609.

Materialization treated an empty reservoir as available. A vacant cohort, or one
holding nothing, still owns its store, so a draw could credit it while the owning
record went on recording nothing: the same inventory in two places, reached from
the opposite direction to the destination defect in 1a59b63. Destinations are
checked against the registry's ownership now, not just their balance, and the
registry answers whether a reservoir is used rather than callers inferring it from
what the reservoir contains.

The same hole existed on the collapse side in a worse form, found by asking
whether the reported defect had a sibling. Nothing verified the destination was
unowned, so the shared-reservoir rule added in M4.3 would have refused the install
only after the transfer had run, leaving the members emptied and their biomass in
a reservoir nothing accounted for. That is exactly the stranding the M4.2 ordering
was chosen to prevent, defeated by a check that ran too late. Whichever registry
step follows a transfer is now proved to succeed before it, through may_install
and may_replace queries that install and replace delegate to, so there is one rule
and not a restatement that can drift.

The second finding was a functional gap the oscillation test hid by discarding its
registry each cycle. A full draw leaves a vacant record, which ADR-0036 retains
because absence is not extinction, and collapse then refused that habitat and
genome as occupied: a full materialization was a one-way door out of a habitat and
the lineage could never return. Collapse repopulates a vacant record in place now,
continuing its ownership and revision series rather than restarting them, and a
record still holding a population is refused, because merging two silently is not
a conversion. The test keeps one registry for all twelve cycles and asserts the
revision continues.

Four of the five new guards are load-bearing. The fifth is not and is recorded as
such: the may_replace pre-check in commit_materialize is unreachable given the
checks before it, since the installed record is already proved equal to the plan's
origin and the result differs only by a later revision. It is kept because it is
the right place for the guard and its collapse-side sibling is genuinely
reachable, not because it is exercised.

Release 37/37, analysis, ASan 37/37 and clang-format all ran and passed here.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-24 17:47:37 -04:00

161 lines
9.6 KiB
Markdown

# ADR-0037: Collapsing individuals into a cohort as one sampled transaction
## Status
Adopted for M4.2. It converts identified individuals into the ADR-0036 record and
nothing else: there is no population advance, no materialization back into
individuals, no transfer between habitats and no persistence. Those are M4.3,
M4.4 and M7. ADR-0036 is unchanged. The only change outside this milestone is
additive: `ReservoirId` and `Transfer` gain the defaulted comparisons every other
value type in the project already has, so a plan's legs can be compared. No
accounting behaviour changes.
## Context
ECOLOGY_MODEL's headline risk for this step is dual representation: the same
biomass existing as individuals and as a cohort at once, or existing as neither
because a conversion stopped halfway. It also forbids an adversarial conversion
that selects only healthy or high-energy members, or that resets age, injury,
fertility or incubation on the way in.
M4.1 gave the destination and proved it cannot be double-owned. What is missing is
the move itself, and the move is where conservation is either exact or quietly
wrong. The project already has the instrument for that: `accounting::Ledger`
applies ordered legs to a candidate copy and commits only if every leg holds, so
an atomic transfer is available rather than something this ADR has to invent.
## Decision
Collapse is two phases over one sample. `plan_collapse` reads the members once and
returns a `CollapsePlan`; `commit_collapse` applies exactly that plan and derives
nothing new. ECOLOGY_MODEL requires that recovery replays the committed sample and
never samples again, so the plan is the sample: it is immutable, it carries the
already-validated cohort, and committing it twice cannot resample.
A member that is controlled, suspended, dead or unsettled refuses the whole plan
rather than being skipped. This is the part that answers adversarial selection.
Filtering would mean a caller could always obtain a plan by quietly omitting
whoever was inconvenient, and the result would look identical to an honest one.
Refusing loudly means an excluded member is a plan that failed and named who.
What this cannot establish is that the caller offered every member it had;
completeness of the input set is outside what this function can see, and is stated
as a limit rather than implied to be covered.
Age is carried, never reset. The request declares class bounds, each member enters
the class containing its age, and a member whose age falls outside every declared
bound refuses the plan. A conversion therefore cannot quietly move anyone to a
different age class, and cannot discard one by having nowhere to put it.
Conservation runs through the ledger rather than beside it. The plan holds one leg
per member, moving that member's entire reserve to the cohort's reservoir, and
commit submits them as a single transaction. The ledger's conserved total is
unchanged by construction, so conservation is a property of the instrument rather
than an assertion this code makes about itself.
Dual representation is closed from both ends. A member must be emptied exactly: at
commit each member's reservoir has to hold precisely what the plan sampled, and
anything else means the world moved since the sample and the plan is stale.
Leaving a remainder behind would be the same biomass in two tiers, which
ECOLOGY_MODEL locks against.
Ordering inside commit is chosen so no failure can strand inventory. The
destination is checked before anything moves: the registry must not already hold
this habitat and genome and must have room, and the destination reservoir itself
must be empty. An occupied reservoir would mix inventory the conversion never
sampled into the cohort's backing store, so the cohort's recorded reserve would
understate what backs it and the surplus would belong to nobody. A cohort's
reserve is therefore the whole of its reservoir, not a part of it. Only then does the ledger transaction
run, and only then is the cohort installed, which cannot fail because the plan
carries a cohort already validated and the destination was already proved
available. Every rejection therefore leaves the ledger and the registry exactly as
they were.
Two things were added here after review, and both are corrections rather than
refinements. A destination reservoir that another cohort owns is refused, because
emptiness is not availability: a vacant cohort still owns its store, and the
shared-reservoir rule would otherwise have refused the install only after the
transfer had run, leaving the members emptied and their biomass in a reservoir
nothing accounts for. Whichever registry step will follow the transfer is now
checked before it, through the registry rather than by restating its rules.
And a habitat and genome already holding a vacant record is repopulated rather than
refused. ADR-0036 keeps a record whose population reached zero because absence is
not extinction, so without this a lineage could never return to a habitat it had
once emptied, and a full materialization would be a one-way door. The retained
record's ownership and revision series continue: the plan has to be a strictly
later view of it, which is the rule every other replacement obeys. A record that is
not vacant is a population, and merging two silently is not a conversion.
The reservoir's emptiness is checked after the sample rather than beside the other
destination checks. Both run before anything moves, so this is a reporting choice:
when a committed plan is replayed into a fresh registry both would refuse it, and
the informative answer is that the members no longer hold what was sampled rather
than that this reservoir is occupied.
## Alternatives Considered
Skipping ineligible members instead of refusing would make adversarial selection
indistinguishable from ordinary use, which is the risk this step is named after.
Moving inventory first and installing afterwards would allow a transfer whose
destination then refuses, stranding biomass in a reservoir nothing owns. Building
the cohort during commit rather than during planning would let commit fail on
validity after the ledger had already moved. Summing member reserves into a single
total and redistributing it across classes would conserve mass while destroying
the distribution ADR-0036 exists to keep. Letting the collapse write directly into
a cohort's inventory without a ledger transaction would make conservation
something this code claims rather than something the instrument enforces. Removing
each member's reservoir from the ledger would be closer to "remove active spending
rights", but the ledger deliberately has no removal; emptying is the strongest
form available and is what the tests assert.
## Benefits
Conservation is exact because the ledger enforces it, not because a test checks a
sum. Every failure path leaves the world unchanged, so there is no partial
conversion to recover from. A conversion that drops a member fails and says which
one. Ages and the reserve distribution survive the move intact. The plan is a
value, so the sample can be inspected before anything is committed.
## Costs
A plan is bounded to 256 members, because that is the ledger's transaction limit
and one leg is needed per member; larger groups need several conversions, which
this ADR does not coordinate. The plan holds a copy of the cohort and of every
leg. Members are supplied by the caller as an explicit record rather than read
from any live representation, so something has to map a world onto them, and that
mapping is not written here.
## Risks
This is not persistence. The before-and-after property holds across a rejection
inside one process; a crash between the ledger transaction and the registry
install is not something an in-memory registry can be recovered from, and durable
conversion sequence numbers, escrow and replay are M7 and M8. The function cannot
tell whether the members it was given are all the members there were, so
completeness is a caller obligation and an honest gap. Emptying a reservoir is not
the same as withdrawing the right to use it: nothing prevents a later transfer
crediting that reservoir again, and only the cohort registry's single-ownership
rule limits what that could mean. Nothing here says a cohort may then advance, and
a collapsed cohort cannot yet be turned back into individuals, so this direction
is one-way until M4.4.
## Consequences
Test that a plan totals its members exactly, that classes carry the ages and
reserves the members had, and that a member's reserve is never redistributed. Test
that every ineligible state refuses the whole plan and names the member, that a
foreign habitat or genome, a duplicate identity, an unclassifiable age, an empty
group and a group beyond the transaction limit are each refused. Test that a
committed collapse leaves the ledger's conserved total unchanged, empties every
member reservoir, credits the destination with exactly the planned total, leaves
the cohort's reserve equal to the whole of that reservoir, and installs the
cohort. Test that a destination reservoir already holding anything is refused, and
that emptying it first makes the same plan committable. Test that a plan which is never committed changes nothing,
that a commit refused at the destination or on a stale balance changes nothing in
either the ledger or the registry, and that committing one plan twice is refused
without moving inventory a second time. Test that the plan is copy-only, since a
moved-from plan would keep its identities and lose its legs. No genome schema,
mutation catalog, archive, episode driver, ensemble, cohort record, renderer or
deployment changes. The ledger changes only by gaining defaulted comparisons on
two of its value types, which no behaviour depends on.