Repository navigation
Start fresh on a failed load, write the chronicle log for loaded sessions, and show headless startup warnings - #100
Conversation
…n saves and creature files (#104) ## Why Saves and creature files are the only way to carry a world or a lineage across runs and code changes, and a creature file is documented as something a person can write by hand. Loading checked only their structure, so two kinds of bad input went straight into the running world. `genome-validation-allows-duplicate-neuron-ids`: `genome_problem` checked that every connection named a real neuron but not that neuron ids were unique. The brain reads its outputs by sorted id, so a repeated output id shifts every later output by one slot and the creature's movement, eating and attack outputs all drive the wrong actions. A repeated input id is queued twice by `topological_sort`, which then decrements a neighbour's in-degree past zero; a debug build panics with "attempt to subtract with overflow" in `clauvolution_brain`. A release build loads the file without complaint. `save-load-skips-numeric-validation`: `validate_save_state` snapped non-finite positions to zero but left every other number alone. JSON has no NaN or infinity literal, but a number too large for an `f32` (`1e39`) parses as infinity, so a hand-edited or corrupt save can carry one. An infinite organism energy makes the energy ledger's baseline infinite and its cumulative residual NaN for the rest of the run. A non-finite trait makes the ledger's net flow NaN and passes to every descendant. An infinite food energy turns the food income line into `inf`. Traits outside their bounds were kept as written, though mutation and the sim never produce them. ## What changed - `genome_problem`, shared by save loading and creature import, also refuses a genome whose neuron ids repeat, or whose scalar traits, neuron biases, connection weights, or segment sizes and angles are not finite. It now returns a `String` that names the bad id or field, and `CreatureLoadError::Invalid` carries that string. - `validate_save_state` drops an organism with an invalid genome, as before. The warning now includes the first reason. It also drops an organism whose energy is not finite, since energy is the ledger's starting balance and has no neutral value. - For the organisms it keeps, it clamps each scalar trait into its `*_BOUNDS`, which is what the next mutation of that trait would do. It resets a non-finite `health`, `signal` or `memory` to the value a founder spawns with (the value a missing field already takes), and clamps `health` to 0..1 and `signal` to -1..1, the ranges the sim keeps them in. - Food items with a non-finite position, or a non-finite or negative energy, are dropped. Regeneration refills the world. - Each kind of repair is logged once with a count, not once per organism. - `load_creature` refuses the same genome problems. A trait outside its bounds is clamped, as in a save. The file is read before the app's log exists, so `load_creature` lists each clamp in a new `CreatureFile::clamped_traits` field (`serde(skip)`, so it is never written to or read from the file). `load_seed_creatures` in the app prints them on stderr after the "Seeding with" line, for example `Warning: rounded.json: 1 trait value(s) outside their bounds were clamped: speed_factor 0.19999999 to 0.2`. That is a three-line edit, outside the `startup_system` hunks that #100 touches. - Connection weights, neuron biases and segment sizes are checked for finiteness only, not against the mutation clamps (`WEIGHT_CLAMP`, the segment size clamp). The findings name only the scalar traits, and a large finite weight does not break the brain. - The save format is unchanged. A save or export the sim wrote itself never trips the drop rules, but it can trip the trait clamp. `Genome::crossover` blends traits without clamping, so two parents at `SPEED_FACTOR_BOUNDS.min` (0.2) can give a child 0.19999999, which it keeps until that trait mutates (TODO `crossover-blend-leaves-bounds`). Clamping such a value moves it by that rounding error. An earlier revision of this branch refused it on creature import, which would have refused ordinary Inspect exports. - New DECISIONS entry: "Save and creature validation: drop what cannot be repaired, clamp what the sim would". FEATURES save/load and creature import lines updated. The review backlog entry is removed. ## Markers Resolves review backlog: save-input-validation Resolves review finding: genome-validation-allows-duplicate-neuron-ids Resolves review finding: save-load-skips-numeric-validation ## Validation Everything below is headless. Run from the repository root with Python 3 on the path. The numbers are from seed 42 on this branch and on `main` at 716d16a. Runs are deterministic per seed. 1. Make a clean save: `cargo run --release -- --headless 300 --seed 42 --save-as base`. 2. Make corrupted copies of it, plus two creature files whose genome repeats a neuron id: ``` python3 - <<'PY' import json, os, copy base = json.load(open("sessions/base/save.json")) def save(name, s): os.makedirs(f"sessions/{name}", exist_ok=True) open(f"sessions/{name}/save.json", "w").write(json.dumps(s).replace('"INF"', '1e39')) s = copy.deepcopy(base); s["organisms"][0]["energy"] = "INF"; save("inf-energy", s) s = copy.deepcopy(base); s["organisms"][0]["genome"]["body_size"] = "INF"; save("inf-trait", s) s = copy.deepcopy(base) for o in s["organisms"]: o["genome"]["body_size"] = 9.0 save("oob-trait", s) s = copy.deepcopy(base) for f in s["food"]: f["energy"] = "INF" save("inf-food", s) def dup(kind, path): g = copy.deepcopy(base["organisms"][0]["genome"]) if kind == "output": n = next(n for n in g["neurons"] if n["neuron_type"] == 2) else: # an input with an enabled outgoing connection src = next(c["from"] for c in g["connections"] if c["enabled"] and any(n["id"] == c["from"] and n["neuron_type"] == 0 for n in g["neurons"])) n = next(n for n in g["neurons"] if n["id"] == src) g["neurons"].insert(g["neurons"].index(n), copy.deepcopy(n)) json.dump({"genome": g}, open(path, "w")) dup("output", "dup-output.json"); dup("input", "dup-input.json") PY ``` 3. Load each save with `cargo run --release -- --load sessions/<name> --headless 50`. Read `Total organisms (final)`, `Body size` under "Final trait averages", and `Baseline energy`, `+ food`, `Net of flows` and `Cumulative residual` under "Energy ledger". | save | before (main) | after (this branch) | |---|---|---| | `inf-energy` | Baseline `inf`, Net `-inf`, Cumulative residual `NaN` | Baseline 38458.8 (the clean save's 38478.3 less the dropped organism's 19.5), Net -4007.6, residual +0.000446 | | `inf-trait` | Net of flows `NaN`, Cumulative residual `NaN` | Baseline 38458.8, Net -4007.6, residual +0.000446 (the organism is dropped) | | `oob-trait` (every body_size 9) | Body size 9.00, 12 organisms left | Body size 2.00 (the bound), 615 organisms | | `inf-food` (every food energy 1e39) | `+ food inf`, Net `NaN`, residual `NaN` | `+ food 8364.0`, Net -12060.7, residual +0.001072 (the saved food is dropped and regrows) | 4. Seed a fresh world with each creature file: `cargo run --release -- --seed 42 --seed-with dup-output.json --headless 50`, and the same with `dup-input.json`. Before, both print `Seeding with an unnamed creature ...` and run. The debug build `cargo run -- --seed 42 --seed-with dup-input.json --headless 5` panics with `attempt to subtract with overflow` at `crates/clauvolution_brain/src/lib.rs:239`. After, both builds exit with status 1 before the run starts, printing `Failed to load creature file: dup-output.json is not a usable creature: two neurons share id 26` (id 25 for `dup-input.json`). A creature file one rounding error past a bound now imports, clamped. Make it with `python3 -c "import json; g=json.load(open('sessions/base/save.json'))['organisms'][0]['genome']; g['speed_factor']=0.19999999; json.dump({'genome': g}, open('rounded.json','w'))"`. Then run `cargo run --release -- --seed 42 --seed-with rounded.json --headless 5`. It exits 0 and prints `Warning: rounded.json: 1 trait value(s) outside their bounds were clamped: speed_factor 0.19999999 to 0.2` under the "Seeding with" line. 5. Check that a normal save still round-trips with nothing dropped. Run `cargo run --release -- --headless 300 --seed 42 --save-as x`, then `cargo run --release -- --load sessions/x --headless 0`. Compare its `Baseline energy` with the total energy in the file: `python3 -c "import json; o=json.load(open('sessions/x/save.json'))['organisms']; print(len(o), round(sum(x['energy'] for x in o), 1))"`. Both give 38478.3 (927 organisms), so no organism was dropped. On this seed every trait in the file is also inside its bounds, every health is in 0..1 and every signal is in -1..1, so nothing was clamped either. Other seeds can carry a crossover rounding error past a bound, which is clamped and logged, never dropped. 6. The repairs are logged with `warn!`, which a GUI run shows in the terminal. Headless runs install no Bevy log, so a headless load repairs without printing anything. The ledger numbers above are the headless evidence. Supporting automated checks: there is a unit test in `crates/clauvolution_sim/src/save.rs` for each rejection, repair and clamp: duplicate input and output ids in a genome, a save and a creature file; each non-finite genome number; non-finite energy; out-of-bounds traits clamped in a save and in a creature file, with the clamp reported; a crossover-style 0.19999999 `speed_factor` that exports and imports clamped; a non-adjacent duplicate id; a trait name list checked against the serialised field names; health, signal and memory reset and clamped at both ends; unusable food dropped; trait accessor order; and a 40-organism evolved `save_world` round trip that loads unchanged. Each check was mutation-tested: 21 mutants, one per rule (removing a check, reverting creature import to refusing, an adjacent-only duplicate check, swapping trait order or trait names, dropping the health lower clamp or the clamp warning), and each failed at least one test. `scripts/check.sh` passes. ## Review Independent review by a separate Claude Opus 5.5 reviewer agent at a78df3e: changes needed. Verified: `scripts/check.sh` and the PR markers pass. A seed 9 save from this branch and a seed 13 save from origin/main both load with a baseline equal to the file's energy sum, with nothing dropped or clamped, and a 300-tick continuation is identical to main. An infinite-energy corruption gives NaN on main and the dropped-organism baseline on the branch. The clamp bounds are the genome crate's own `SCALAR_TRAIT_BOUNDS`. The health, signal and memory ranges match the sim's writers. The duplicate check covers all neuron types. Six real evolved genomes import. Findings: 2. 1. (must) False rejection of evolved organisms. `Genome::crossover`'s blend never clamps, so a child of parents at `SPEED_FACTOR_BOUNDS.min` can sit at 0.19999999 and keep it by cloning. Organisms at exactly 0.2 are common in evolved saves (35 to 173 per save). Inspect "Export creature" could therefore write a genome that `--seed-with` refused (24 of 1000 crossed founder children). The claims "a save the sim wrote itself never trips any of these checks" and "an exported genome is always inside the bounds" were false. Fixed in cfa8ad1 and 88f54f7. Creature import now clamps out-of-bounds traits as saves do and warns on stderr after the "Seeding with" line. It still refuses non-finite values and duplicate ids. The DECISIONS entry and this description now describe the crossover cause and reference TODO `crossover-blend-leaves-bounds`. The genome crate is untouched. A test exports and imports a 0.19999999 `speed_factor` genome, mutation-checked. 2. (nit) Test gaps. Fixed in cfa8ad1. Added a non-adjacent duplicate-id test, which kills an adjacent-only `windows(2)` mutant. The health lower clamp is pinned by a test, with a comment that health at or below zero dies on the next tick either way. A test sets each trait through its serialised field name and checks that the clamp names that field, which kills a swap of two `SCALAR_TRAIT_NAMES` entries. The same reviewer re-reviewed the fixes at 88f54f7: all 1000 crossover children from the probe now import, the 24 below the bound clamped and reported; the adjacent-only, name-swap, health-clamp and import-clamp mutants each fail a test; cargo test -p clauvolution_sim -p clauvolution_app passes; no new findings. Approved. Not checked: the debug-build `topological_sort` panic, GUI display of warnings, and steps 3 and 4 with the author's seed-42 numbers (reproduced on seed 9 instead).
224b7bf to
08cb12d
Compare
ReviewIndependent review by a separate Claude Opus 5.5 reviewer agent at Verified: the before and after behaviour was reproduced on the parent Findings: 3.
The same reviewer confirmed all three fixes at 224b7bf (now 1a856b3): Not checked: GUI behaviour in a window; a save.json that exists but Post-rebase reviewIndependent review by a separate Claude Opus 5.5 reviewer agent at Verified: the diff and description match the backlog entry; the Findings: 2.
Not checked: GUI behaviour in a window. |
…ronicle log at startup A save.json that failed to parse logged "starting fresh" and then returned without inserting TileMap or SimRng, so the first frame panicked. startup_system now reads the save itself and falls through to fresh_world on any failure, logging the reason to the chronicle. The chronicle's log path and its "Session started" entry move from tick 0 of tick_counter_system to startup_system. A loaded world starts at its saved tick and never ran the tick-0 branch, so it never wrote chronicle.log; fresh worlds also lost the "Seeded with" entries written before tick 0.
MinimalPlugins has no log plugin, so every warn! and error! in a headless run was dropped: a missing or unparseable save, organisms dropped by save validation, and --seed-with being ignored on a load. run_headless now adds LogPlugin at WARN, which writes to stderr alongside the summary; info! stays quiet and RUST_LOG still overrides.
…e --seed-with imports
08cb12d to
ca7a09f
Compare
Why
Three faults on the path that starts a run from a save, found in the
2026-10-09 full review and still open in the 2026-10-24 one.
(corrupt-save-load-panics).
load_saved_worldlogged "Failed to loadsave file, starting fresh" and then returned without inserting
TileMaporSimRng, andstartup_systemhad already returnedinstead of falling through to
fresh_world. The first frame thenpanicked on a missing resource. A
save.jsonof{"tick": 5}wasenough:
--headless 30 --seed 42 --load <dir>exited 101 withmass_extinction_input_system could not access system parameter ResMut<SimRng>. The GUI shares the same startup system, so itcrashed the same way. A truncated or hand-edited save should cost
the save, not the app.
chronicle.log(loaded-session-chronicle-log-unset). The chronicle's log path was
set in one place only: the
tick.0 == 0branch oftick_counter_system. A load sets the tick to the saved value beforethe first tick runs, so that branch never ran and the path stayed
unset for the whole run. The README says the chronicle log lives in
sessions/<name>/, and for a loaded world that was false. Freshworlds lost something too: the "Seeded with ..." entries that
setup_worldwrites before tick 0 never reached the file.(headless-startup-warnings-invisible). The headless app is built from
MinimalPlugins, which has no log plugin, so everywarn!wasdropped. That covered "Save file not found", save parse errors,
organisms dropped or repaired by save validation, and the
--seed-with-ignored-on-load warning thatdocs/DECISIONS.mdpromises. A headless
--loadof a missing directory printed afresh-world summary and gave no sign that the load hadn't happened.
What changed
startup_systemnow reads the save itself through a smallread_savehelper, which returns the state or a reason. On anyfailure (missing file, unreadable file, parse error) it logs
<reason>; starting a fresh worldas a warning and as a chronicleentry, then falls through to
fresh_world.load_saved_worldnowtakes an already-parsed
SaveStateand can no longer bail outhalf-way. Save validation (dropping, clamping and resetting bad
values) still runs inside
save::load_worldas before.--seed-withused to be warned about and dropped whenever asave.jsonexisted, even one that then failed to parse. The warningnow fires only when a load succeeds, and a failed load's fresh world
gets the imported creatures. Creature files are still read, and
their clamp warnings printed, before the app starts, so that path is
unchanged.
startup_systemnow sets the chronicle's log path from theSessionbefore it writes any entry, so the file gets every entryfrom the first one on, for loaded and fresh worlds alike. The
"Session '' started" entry moves there too. A fresh world logs
it at tick 0, ahead of the "Seeded with" entries. A loaded world logs
it at the saved tick, after the restored entries and just before
"World loaded from save".
tick_counter_systemno longer touches thechronicle's log path or reads
Session. Both the GUI and headlessapps run
startup_system, so the fix covers both. This is a startupsystem rather than code where the
Sessionresource is created(
CorePlugin::buildfor the GUI,run_headlessfor headless):WorldChronicledoesn't exist yet at either of those points, andstartup_systemis the one place both modes share.run_headlessnow addsLogPluginatWARN, so warnings and errorsgo to stderr next to the summary while
info!output stays quiet.RUST_LOGstill overrides the level. Bevy's formatter adds ANSIcolour codes to these lines even when stderr is redirected to a
file, unless
NO_COLORis set, the same as the GUI's log output.Runs with nothing to warn about print exactly what they printed
before.
--loadline and theSave/load entry in
docs/FEATURES.mdsay a missing, unreadable ormalformed save starts a fresh world with a warning. README's
--seed-withline and the--seed-withentry indocs/DECISIONS.mdsay that a failed load's fresh world gets the imports. The headless
entry in
docs/DECISIONS.mdrecords theWARN-level log plugin.load-failure-and-session-wiringentry is removed fromreview/BACKLOG.md.startup_systemon a small worldwith a scratch session directory: a save that fails to parse, a
missing save, a minimal valid save
(
{"tick": 300, "terrain_seed": 1, "organisms": []}), a fresh world,and a fresh world with one
--seed-withcreature. Each test failsunder the matching mutation: putting back the early return on a
failed load, removing the log-path assignment, dropping the loaded
world's "Session started" entry, or hiding the log path while the
seeding entries are written (which is what the old tick-0 wiring
did).
crates/clauvolution_sim/src/save.rsareunchanged.
Type: fix
Resolves review backlog: load-failure-and-session-wiring
Resolves review finding: corrupt-save-load-panics
Resolves review finding: loaded-session-chronicle-log-unset
Resolves review finding: headless-startup-warnings-invisible
Validation
bash scripts/check.shpasses. The five new tests are undertests::in
crates/clauvolution_app/src/main.rs.Setup: build with
cargo build --release, then run the commands belowfrom a scratch directory, so that
sessions/lands there.$BINisthe absolute path of
target/release/clauvolution. Steps 2 to 4 need agood save and a creature file:
The second command takes the first organism's genome from the save.
The Inspect tab's "Export creature" button in a GUI run writes an
equivalent file to
sessions/<name>/. Onlygenomeis required in acreature file.
A save that fails to parse starts a fresh world, with a warning
(corrupt-save-load-panics, headless-startup-warnings-invisible).
Before: no warning, a panic
clauvolution_sim::mass_extinction_input_system could not access system parameter ResMut<SimRng>, andexit=101. Now: twoWARNlines (shown without their timestamps and colour codes), then a
normal headless summary for a fresh world
(
Total organisms (final): 445on seed 42) andexit=0:To check the GUI, run
$BIN --load corrupt. Before, it crashed onthe first frame. Now it opens on a fresh world, and the Chronicle
tab's second entry reads "... could not be loaded; starting a fresh
world".
A failed load's fresh world takes the
--seed-withimports.$BIN --headless 30 --seed 42 --load corrupt --seed-with creature.json --save-as cs head -3 sessions/cs/chronicle.logBefore: the same panic as step 1. Now the summary's founder line
ends in
imported 1, there is no "ignoring 1 creature file(s)"warning, and the log opens with
Session 'cs' started, the"could not be loaded; starting a fresh world" entry and
Seeded with Test Import via creature.json.A missing save, and an ignored
--seed-with, are reported inheadless runs (headless-startup-warnings-invisible).
Before:
Loading session from nonexistentand then a fresh-worldsummary, with no warning at all; the second command printed no
warning either. Now the first prints
WARN ... Save file not found: nonexistent/save.json; starting a fresh worldbefore the summary, and the second printsWARN ... --seed-with only applies to a fresh world; ignoring 1 creature file(s) while loading a save.A loaded session writes its chronicle log
(loaded-session-chronicle-log-unset).
Before:
sessions/xheld onlysave.json, and there was nochronicle.log. Nowsessions/x/chronicle.logexists and isnon-empty (24 lines in the author's run). Its first lines are
[ 10s] Session 'x' startedand[ 10s] World loaded from save,followed by the entries from the 300 resumed ticks. A fresh
--seed-withrun(
$BIN --headless 30 --seed 7 --seed-with creature.json --save-as fresh) hasSeeded with Test Import via creature.jsonas thesecond line of
sessions/fresh/chronicle.log. Before, the filestarted with
Session 'fresh' startedand had no seeding entry.Runs without warnings are unchanged. For
--headless 300 --seed 7 --save-as s7, stdout and stderr from abuild of the parent commit and from this change are identical once
the wall-clock line is left out, with no
WARNlines, andchronicle.logis byte-identical.save.jsonholds the samevalues; only the order of
phylo_nodesdiffers, and it also differsbetween two runs of the same build (tracked as
phylo-hashmap-order-reaches-output).