Repository navigation
Reject duplicate neuron ids and non-finite or out-of-bounds numbers in saves and creature files - #104
Merged
Merged
Conversation
…ure files genome_problem now refuses a genome whose neuron ids repeat or whose traits, biases, weights or segment sizes and angles are not finite. validate_save_state also drops organisms with non-finite energy, clamps out-of-bounds traits into their bounds, resets non-finite health, signal and memory to founder values, clamps health and signal to the ranges the sim keeps them in, and drops food with a non-finite position or a non-finite or negative energy. load_creature refuses a trait outside its bounds instead of clamping it.
…fusing Crossover blends traits without clamping, so an ordinary evolved organism can sit a rounding error past a bound (speed_factor 0.19999999), and an Inspect export of it was refused by --seed-with. load_creature now clamps like save loading does and lists each clamp in CreatureFile::clamped_traits; the --seed-with startup prints them as a warning. Duplicate ids and non-finite numbers are still refused. Adds tests for a non-adjacent duplicate id, the health lower clamp and the trait name order.
…over can leave bounds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_problemchecked 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 bytopological_sort, which then decrements a neighbour's in-degree past zero; a debug build panics with "attempt to subtract with overflow" inclauvolution_brain. A release build loads the file without complaint.save-load-skips-numeric-validation:validate_save_statesnapped non-finite positions to zero but left every other number alone. JSON has no NaN or infinity literal, but a number too large for anf32(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 intoinf. 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 aStringthat names the bad id or field, andCreatureLoadError::Invalidcarries that string.validate_save_statedrops 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.*_BOUNDS, which is what the next mutation of that trait would do. It resets a non-finitehealth,signalormemoryto the value a founder spawns with (the value a missing field already takes), and clampshealthto 0..1 andsignalto -1..1, the ranges the sim keeps them in.load_creaturerefuses 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, soload_creaturelists each clamp in a newCreatureFile::clamped_traitsfield (serde(skip), so it is never written to or read from the file).load_seed_creaturesin the app prints them on stderr after the "Seeding with" line, for exampleWarning: 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 thestartup_systemhunks that Start fresh on a failed load, write the chronicle log for loaded sessions, and show headless startup warnings #100 touches.WEIGHT_CLAMP, the segment size clamp). The findings name only the scalar traits, and a large finite weight does not break the brain.Genome::crossoverblends traits without clamping, so two parents atSPEED_FACTOR_BOUNDS.min(0.2) can give a child 0.19999999, which it keeps until that trait mutates (TODOcrossover-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.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
mainat 716d16a. Runs are deterministic per seed.Make a clean save:
cargo run --release -- --headless 300 --seed 42 --save-as base.Make corrupted copies of it, plus two creature files whose genome repeats a neuron id:
Load each save with
cargo run --release -- --load sessions/<name> --headless 50. ReadTotal organisms (final),Body sizeunder "Final trait averages", andBaseline energy,+ food,Net of flowsandCumulative residualunder "Energy ledger".inf-energyinf, Net-inf, Cumulative residualNaNinf-traitNaN, Cumulative residualNaNoob-trait(every body_size 9)inf-food(every food energy 1e39)+ food inf, NetNaN, residualNaN+ food 8364.0, Net -12060.7, residual +0.001072 (the saved food is dropped and regrows)Seed a fresh world with each creature file:
cargo run --release -- --seed 42 --seed-with dup-output.json --headless 50, and the same withdup-input.json. Before, both printSeeding with an unnamed creature ...and run. The debug buildcargo run -- --seed 42 --seed-with dup-input.json --headless 5panics withattempt to subtract with overflowatcrates/clauvolution_brain/src/lib.rs:239. After, both builds exit with status 1 before the run starts, printingFailed to load creature file: dup-output.json is not a usable creature: two neurons share id 26(id 25 fordup-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 runcargo run --release -- --seed 42 --seed-with rounded.json --headless 5. It exits 0 and printsWarning: rounded.json: 1 trait value(s) outside their bounds were clamped: speed_factor 0.19999999 to 0.2under the "Seeding with" line.Check that a normal save still round-trips with nothing dropped. Run
cargo run --release -- --headless 300 --seed 42 --save-as x, thencargo run --release -- --load sessions/x --headless 0. Compare itsBaseline energywith 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.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.rsfor 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.19999999speed_factorthat 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 evolvedsave_worldround 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.shpasses.Review
Independent review by a separate Claude Opus 5.5 reviewer agent at a78df3e: changes needed.
Verified:
scripts/check.shand 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 ownSCALAR_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.
Genome::crossover's blend never clamps, so a child of parents atSPEED_FACTOR_BOUNDS.mincan 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-withrefused (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 TODOcrossover-blend-leaves-bounds. The genome crate is untouched. A test exports and imports a 0.19999999speed_factorgenome, mutation-checked.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 twoSCALAR_TRAIT_NAMESentries.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_sortpanic, GUI display of warnings, and steps 3 and 4 with the author's seed-42 numbers (reproduced on seed 9 instead).