Skip to content

Reject duplicate neuron ids and non-finite or out-of-bounds numbers in saves and creature files - #104

Merged
vhata merged 5 commits into
mainfrom
review/save-input-validation
Oct 11, 2026
Merged

vhata merged 5 commits into
mainfrom
review/save-input-validation

Conversation

@vhata

@vhata vhata commented Oct 9, 2026

Copy link
Copy Markdown
Owner

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 Start fresh on a failed load, write the chronicle log for loaded sessions, and show headless startup warnings #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).

vhata added 5 commits October 9, 2026 08:18
…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.
@vhata
vhata merged commit 051f5e8 into main Oct 11, 2026
4 checks passed
@vhata
vhata deleted the review/save-input-validation branch October 11, 2026 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant