T-017: Metabolism and the matter cycle
Goal
The economy of the world: where energy and matter come from, what everything costs, and
what happens at death. See docs/DESIGN.md sections 3 and 4.
Scope
- Energy and matter accounting for every organism. Building a cell takes both. Keeping a cell alive takes energy every step.
- Photo cells: light plus dissolved nutrients become energy and matter. Chemo cells: the same from vent chemicals.
- Costs for muscle work, signals, neurons, glow, pigment and growth.
- Starvation and death. Dead bodies become detritus that sinks, can be eaten later, and decays back into dissolved nutrients.
- Decide and record the ageing rule (the design proposes maintenance cost rising with cell age) and whether energy is shared body-wide or flows along bonds.
- A conservation audit: total matter in the world, counted across fields, bodies, eggs and detritus, checked every N steps in debug runs.
Done when
- ☐ The matter audit holds exactly over a long run with births and deaths.
- ☐ A hand-built rooted body with Photo cells sustains itself in light and starves in the dark. A shaded one does worse than an unshaded one.
- ☐ Every cost is in
Paramswith a comment.
Added 2026-12-02: size has to matter through the economy
Read "Scale is free" in docs/DESIGN.md section 5. No cost or income may depend on body
size by rule. Size effects have to come from this: **exchange happens at surfaces, costs are
paid by volume.** Light, dissolved food and vent chemicals reach a cell according to how
exposed it is to the water. Upkeep is paid for all matter.
- Design how exposure is worked out, cheaply enough for the hot path, and record it.
- Extra done-when check: the same cells arranged as a compact ball, a sheet and a branching shape, at several sizes. Report income and upkeep per cell for each. A large compact ball should earn less per cell than a sheet of the same cells.
- Report how long bodies of different sizes take to grow, and how long they last without food.
Added 2026-12-02: what the fields give you (from T-011)
Read docs/ARCHITECTURE.md section 16 first. In short:
- The field grid has 4-unit voxels, so it cannot see inside one voxel. Two cells in the
same voxel get the same light value from
light(p). Light income that is fair between neighbours is your job: compute each cell's exposure and share outlight_budget(voxel)among the cells in it, so the column's light is spent once and only once.light_per_cross_section(p)andlight_shade(voxel)are there to help. - Dissolved nutrients, vent chemicals and vent heat are whole quanta (
u64), 2^24 per unit of mass. Usetake(it gives fair shares when a voxel is overdrawn) andgive. Never convert through floats in a way that loses a quantum: conservation is exact today and must stay exact. FieldsStateis at version 2. If you change what it stores, raise the version.
Added 2026-12-02: what the genome leaves for you to price (from T-010)
See docs/GENOME.md section 7 and the end of the T-010 ticket.
- The energy cost of building a cell and the densities are placeholders. So are the costs of the genes for density, reach and stiffness: today they are free, and "everything costs" says they cannot stay free. A long reach, a stiff bond and a light or heavy cell each need a price that comes from what they are, not from a table of penalties.
- An egg's yolk has no weight yet. Matter must weigh something wherever it is.
Egg::default()has an endless budget, for tests. A genome that never stops growing then reaches 65,535 cells. Once matter and energy are real, an egg can only spend what its parent gave it. Check that no path into the world uses the endless one.develop.max_bondslimits bonds only when a cell is born.- Regrowing lost cells costs only what growing them cost the first time. Decide whether that is right once wounds and feeding exist.
Added 2026-12-02, after you started: energy by source (from T-019)
The lineage log records, per organism and per region, how much energy came from each
source. It is always zero today. Credit energy as it is taken in with
body.org.lineage.earn(source, amount) (docs/ARCHITECTURE.md section 18). If this note
reached you too late, say so in your report and the orchestrator will make it a small
follow-up.
Result
Branch t-017-metabolism. The full write-up is docs/METABOLISM.md. Charts and tables are
in docs/media/T-017/.
Done-when
- The matter audit holds exactly over a long run with births and deaths: yes.
metabolism_lab audit --n 200000: 55.6 days, 87,969 births, 82,130 deaths, 668,263 cells built, 601,198 dead cells dissolved, 3,125 audits, largest drift 0 quanta, for matter and for energy. Matter at the start and the end: 261,715,628,448 quanta. Reproduction is not written, so the births come from a stand-in in the lab (a body pays for an egg from its stores). Seedocs/media/T-017/audit.png. - **A rooted body with Photo cells lives in light, starves in the dark, does worse in
shade: yes.** Test
a_rooted_body_lives_in_the_light_starves_in_the_dark_and_does_worse_in_shade. - Every cost is in
Paramswith a comment: yes.MetabolismParams, 32 entries, each with meaning, unit and range. - Size matters only through surface and volume: yes. Light per cell per second,
cells of radius 0.5: lone cell 0.51, sheet of 1,000 0.38, ball of 125 0.115, ball of
1,000 0.053. Upkeep per cell is 0.013 to 0.018 for all of them. 60 random body plans
fall between or below the two lines. See
docs/media/T-017/shape.png. - Grow times and fasting times: reported.
docs/media/T-017/life.pngandlife.md.
What exists
crates/sim/src/metabolism/: exact integer books for matter and energy (ledger), exposure from geometry (exposure), prices (price), the stage (tick), and the calls other stages use (mod):withdraw,deposit,take_cell,kill,share,enroll,seed_egg,set_stores,audit.- Development pays for growth out of the body's stores. Its own matter and energy columns and its two placeholder prices are gone.
- 14 tests in
crates/sim/tests/metabolism.rs. `cargo run --release -p sim --example metabolism_lab -- shapes|life|auditmakes the tables.scripts/t017_charts.py` draws the charts.runner bench --livingmeasures the stage.
The leftovers from T-010 and T-011
- Light:
light_budget(voxel)is shared by lit cross-section and capped at the beam, so a column's light is spent at most once (testa_column_of_light_is_spent_at_most_once). - Fields: all exchange goes through
takeandgivein whole quanta. - Density, reach, stiffness, radius, colour, genome length: each priced by the matter,
lift, bond or pigment it makes. Table in
docs/METABOLISM.mdsection 5. - Egg yolk has weight from the first step.
Egg::default()is still endless, and still what tests use. A body with endless energy is outside the economy: the audit does not count it and it earns and pays nothing. The only way into the economy from there ismetabolism::enroll, which is written down by hand in the ledger. No rule of the world makes an endless egg.develop.max_bondsstill only limits at birth. Not changed. Bonds have upkeep now.- Regrowth costs what growth costs. Decided, with the reason in section 5.
Energy by source and death causes (the note added after I started)
It reached me in time. The branch has main as of c793f66 merged in (three merges, as
main moved while the checks ran).
- Light and vent chemistry are credited with
earnas they come in. A bite given tometabolism::depositis credited as organic. An egg's store is credited as inherited. All are at book value, which counts the energy held in matter. - Every death by metabolism's rules calls
lineage::deathwith a cause frommetabolism::cause: 16 starved, 17 killed with no cause given, 18 lost its last cell. metabolism::seed_eggreports the founder's birth.- Test:
the_lineage_log_is_told_where_energy_came_from_and_why_a_life_ended. lineage::breed(T-019's stand-in) now passes its egg matter in quanta. Its bodies have finite matter and endless energy, so they are outside the economy, as before.
Worlds grown from genomes (T-065, merged into main while this ran)
T-065's testbed used the egg and store calls this ticket replaced, so the merge needed code, not only conflict fixes:
develop::spawn_grownnow sets the metabolism columns, gives stored matter weight, and writes a body with a finite energy store down as made by hand. Tested both ways.- Bodies made by
testbed::genomes(runner run --genomes) are outside the economy: finite matter, endless energy, like the soup. They grow exactly as before, T-065's own tests pass, andenergy_createdis now 0. I chose this because most random genomes cannot feed themselves: inside the economy those worlds would be worlds of the dying, which is not what that testbed is for. Putting them in is one call per body (metabolism::enroll) and should be a deliberate choice. Files:testbed/genomes.rs,runner/src/genomes.rs(help text),runner/examples/grew.rs,docs/RUNS.md.
Shared files touched
crates/sim/src/develop/{mod,grow,embryo}.rs, crates/sim/src/index.rs (new
for_each_in_box), crates/sim/src/fields/mod.rs (noise made public, for its portable
exp), crates/sim/src/testbed.rs, crates/sim/src/lineage/breed.rs (one line),
crates/sim/src/store/body.rs and crates/sim/src/store/columns.rs (#[inline(always)]
on the view builders, nothing else),
crates/runner/src/perf/world.rs (living = true for soup worlds),
docs/perf/worlds/soup-100k-living.toml (new), docs/perf/README.md,
crates/sim/tests/develop.rs,
crates/sim/examples/wasm_probe_native.rs, crates/sim/Cargo.toml,
crates/runner/src/bench.rs, docs/ARCHITECTURE.md, docs/DESIGN.md, docs/GENOME.md,
docs/perf/LOG.md, docs/perf/IDEAS.md. Nothing under physics/.
Checkpoint format: orgs.develop is version 2, the metabolism state is version 2, and
there are new columns orgs.metabolism.* and cells.metabolism.*. Older checkpoints are
refused with a message that names orgs.develop.
Not done, or not as good as hoped
- Detritus does not sink and muscle work is not charged. Both wait for physics. The
hooks are
orgs.metabolism.deadandorgs.metabolism.debt. - Nothing eats.
take_cellanddepositare the hooks for T-024, used by the tests. - Signals, neurons and glow pay upkeep for the cell. What they do has no cost yet because
they do nothing yet. Their tickets add to
debt. - A flat sheet catches 0.38 of the light where geometry says about 0.5: the light field spreads a voxel's cells evenly and under-lights a sheet that tiles it.
- The sea floor and terrain do not shade or cover cells.
- Exposure is up to 32 steps old.
- Whole bodies die. Nothing sheds old cells or shrinks when hungry.
- The reserve floor is one global number, not a gene.
- The default prices are a first set that works, chosen so that a lone light-catching cell
earns about 30 times its upkeep and a 1,000-cell ball about 4 times. They are not tuned
for evolution. Bodies live about 20 to 26 days (about 80,000 steps) in the light, which
may be long for fast generations.
ageingis the knob. - 15 of 40 founder body plans could not live on light alone. That is the rule working, not a fault, but it means random founders mostly die.
Speed
scripts/perf.sh --check against main at c793f66, on the final code, load average 2.8.
Exit 0, every case "same":
| world | threads | base steps/s | new steps/s | time new vs base | cycles | verdict |
|---|---|---|---|---|---|---|
| genomes-grown | 1 | 37.4 | 37.0 | +0.9% (+0.4 to +1.8) | +1.4% (+0.9 to +2.3) | same |
| genomes-grown | 4 | 108.7 | 108.8 | +0.4% (-0.7 to +1.0) | +0.2% (-0.4 to +0.5) | same |
| soup-100k-churn | 4 | 288.2 | 289.0 | -0.3% (-0.6 to -0.0) | -0.2% (-0.4 to +0.0) | same |
| soup-100k-living | 1 and 4 | new world, the base build cannot make it | ||||
| soup-100k | 1 | 96.4 | 95.8 | +0.5% (-0.2 to +1.4) | +0.5% (-0.0 to +0.8) | same |
| soup-100k | 4 | 310.5 | 312.0 | -0.5% (-0.7 to -0.1) | +0.0% (-0.3 to +0.1) | same |
| soup-10k | 1 | 926.0 | 925.2 | -0.0% (-2.1 to +0.7) | +0.3% (-0.6 to +0.6) | same |
| soup-10k | 4 | 2917.5 | 2876.8 | +1.4% (+1.3 to +1.6) | +0.8% (+0.7 to +1.0) | same |
| soup-1m | 4 | 33.2 | 34.0 | -2.3% (-3.9 to +0.1) | -0.5% (-0.8 to +0.4) | same |
By stage: physics within 1% everywhere, the fields stage 11 to 14% faster, the index 3 to 9% faster in all cases but one (soup-10k on 4 threads, +9.5% of 0.04 ms).
- An earlier run of the check, on a busier machine, flagged one line: the metabolism stage
in the million-cell world, 0.0000 to 0.0099 ms per step. That is the idle stage looking
to see whether anybody is in the economy, 0.03% of that step. This run could not tell it
from noise. An idea for removing it is in
docs/perf/IDEAS.md. - The first run of this check found a real slowdown, and it was mine. Physics was 3%
slower and the fields' shading loop 33% slower, with no change to either. Profiling
found it: every loop builds a view per organism with one reference per column, and my 15
columns pushed those builders past the size at which the compiler honours
#[inline]. The fix is#[inline(always)]on them (store/body.rs,store/columns.rs, attributes only). Fields is now 12 to 14% faster than onmain, which was near the same cliff. The whole story with the numbers is indocs/perf/LOG.md.
The new world is docs/perf/worlds/soup-100k-living.toml: a soup with every organism in
the economy. runner perf suite --only soup-100k,soup-100k-living --threads 1,4, load 3:
| world | threads | steps/s | ns per cell-step | metabolism ms/step |
|---|---|---|---|---|
| soup-100k | 1 | 96.5 | 103.6 | 0.001 |
| soup-100k | 4 | 315.1 | 31.7 | 0.001 |
| soup-100k-living | 1 | 87.2 | 114.6 | 1.179 |
| soup-100k-living | 4 | 281.5 | 35.5 | 0.399 |
The stage costs 11.8 ns per cell-step on one thread and 4.0 on four. A world with every cell in the economy runs at 0.90 of the speed of one with none. Two thirds of the stage on one thread is the exposure refresh. Memory: 18 to 19 MB per 100,000 cells in `runner bench` (10 bytes per cell, 57 per organism).
Memory safety
No unsafe code was added or changed. The stage adds columns and two parallel loops, and
the inlining fix touches attributes in store/, so scripts/check-unsafe.sh was run in
full on the final code (b381331):
| step | result |
|---|---|
| lint | ok |
| miri-sb (Stacked Borrows) | ok, 620 s |
| miri-tb (Tree Borrows, data races, three threads) | ok, 1204 s |
| tsan (threaded integration tests) | ok, 32 s |
tsan on tests/metabolism.rs, which the script does not list | ok, 13 passed |
The script took 31 minutes, not the 8 it promises. An earlier full run also passed.
After the last merge (no change under store/ or to exec.rs since that run) the quick
form, lint and ThreadSanitizer, passed again on the final code.
Merged (orchestrator, 2026-12-02)
Merged into main at 511efcb. Format check, lint and all tests pass on the merged tree.
- Speed check: exit 1, not a slowdown. Every whole step is the same. One flag,
soup-1mon 4 threads, "metabolism x1189 SLOWER in a stage": the stage did nothing before and now costs about 0.01 ms of a 29 ms step. Written up indocs/perf/LOG.md; the check's floor is the thing to fix (docs/perf/IDEAS.md). - Worlds from genomes stay outside the economy for now, as you left them. Founding a world inside the economy is part of T-018, which started from this merge.
- Your change in
store/(forcing the column views inline) is accepted. It is the same fix T-063 made and the full memory-safety check covers it. - Hand-over notes went to T-013 (work is charged through
debt), T-018, T-023, T-024 and T-056, and the long-run questions todocs/tuning/long-runs.md.