From 20d5149d02ac5f9d24308f1f159e02f4a8d1ee73 Mon Sep 17 00:00:00 2001 From: limbicnation Date: Sun, 10 May 2026 19:47:38 +0200 Subject: [PATCH] fix: gate _last_seed mutation to dynamic modes - random_seed_generator.py: only update the shared _last_seed counter for random/increment/decrement modes. fixed mode must be side-effect-free so interleaved fixed reads do not clobber the counter used by other AdvancedSeedGenerator nodes in the same workflow. - tests: add regression coverage for the new gating (fixed leaves _last_seed alone; random and increment write through). Rewrite reset_state test to set state directly instead of relying on the removed fixed side effect. - pyproject.toml: rewrite description to drop the stale "cross-library synchronization" claim (feature removed in 2.3.0). - README: replace the unrealistic "ComfyUI Python console" reset advice with three actionable options (restart, random-then-increment, and reset_state for scripts). --- README.md | 6 ++++-- pyproject.toml | 2 +- random_seed_generator.py | 5 ++++- tests/test_seed_generator.py | 27 +++++++++++++++++++++++---- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 62812e2..1a6625b 100644 --- a/README.md +++ b/README.md @@ -100,8 +100,10 @@ The `_last_seed` counter is **class-level** — shared across every instance of - Check the ComfyUI console for import errors. **Increment/decrement counter is "wrong"** -- The counter is shared across all instances of this node in the workflow. See *State scope* above. -- To reset to `0`, call `AdvancedSeedGenerator.reset_state()` from the ComfyUI Python console. +- The counter is shared across every instance of this node in the workflow. See *State scope* above. +- The simplest reset is to restart ComfyUI — `_last_seed` lives in process memory only. +- If you need a known starting point without restarting, switch to `random` mode for one execution; the next `increment` will start from that random value plus one. +- For scripts/tests, `AdvancedSeedGenerator.reset_state()` zeroes the counter directly. ## 🤝 Contributing diff --git a/pyproject.toml b/pyproject.toml index c95eb0f..5fb708c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "randomseedgenerator" -description = "Advanced seed generator for ComfyUI with multiple modes, state persistence, and cross-library synchronization" +description = "Minimal ComfyUI seed generator with fixed, random, increment, and decrement modes. Zero dependencies." version = "2.3.0" license = {file = "LICENSE"} dependencies = [] diff --git a/random_seed_generator.py b/random_seed_generator.py index 3e07b5d..ac449d7 100644 --- a/random_seed_generator.py +++ b/random_seed_generator.py @@ -76,7 +76,10 @@ class AdvancedSeedGenerator: else: raise ValueError(f"Unknown mode: {mode!r}") - self.__class__._last_seed = result + # Only dynamic modes update the shared counter; fixed must be side-effect-free + # so users can interleave fixed reads without disturbing increment/decrement state. + if mode in ("random", "increment", "decrement"): + self.__class__._last_seed = result logger.debug("Generated seed %d (mode=%s)", result, mode) return (result,) diff --git a/tests/test_seed_generator.py b/tests/test_seed_generator.py index 7774fc4..46a2c48 100644 --- a/tests/test_seed_generator.py +++ b/tests/test_seed_generator.py @@ -39,9 +39,28 @@ def test_fixed_returns_input_seed(gen): assert gen.generate_seed("fixed", 42) == (42,) -def test_fixed_updates_last_seed(gen): - gen.generate_seed("fixed", 12345) - assert AdvancedSeedGenerator._last_seed == 12345 +def test_fixed_does_not_mutate_last_seed(gen): + """Regression: fixed mode must not touch the shared increment/decrement counter. + + Before this guard, calling `fixed` clobbered `_last_seed`, which silently + altered other AdvancedSeedGenerator nodes running increment/decrement in the + same workflow. + """ + AdvancedSeedGenerator._last_seed = 500 + assert gen.generate_seed("fixed", 12345) == (12345,) + assert AdvancedSeedGenerator._last_seed == 500 + + +def test_random_updates_last_seed(gen): + AdvancedSeedGenerator._last_seed = 0 + (result,) = gen.generate_seed("random", 0) + assert AdvancedSeedGenerator._last_seed == result + + +def test_increment_updates_last_seed(gen): + AdvancedSeedGenerator._last_seed = 100 + gen.generate_seed("increment", 0) + assert AdvancedSeedGenerator._last_seed == 101 def test_random_within_bounds(gen): @@ -98,7 +117,7 @@ def test_is_changed_fixed_returns_stable_key(): def test_reset_state_clears_last_seed(gen): - gen.generate_seed("fixed", 7777) + AdvancedSeedGenerator._last_seed = 7777 assert AdvancedSeedGenerator._last_seed == 7777 AdvancedSeedGenerator.reset_state() assert AdvancedSeedGenerator._last_seed == 0