From 4fb6999210fd8f7fcecc0ccf28fa9ef87d12c636 Mon Sep 17 00:00:00 2001 From: Eliasepfl Date: Wed, 5 Aug 2026 15:06:21 -0400 Subject: [PATCH 1/3] Forward the reset seed and implement VecEnv.seed GodotEnv.reset accepted a seed and dropped it, so gymnasium style reseeding was a silent no-op, and StableBaselinesGodotEnv.seed raised NotImplementedError, which crashes any sb3 algorithm constructed with a seed (PPO(seed=0) calls env.seed()). reset now puts the seed in the reset message, and the wrapper latches one seed per parallel game using the same seed + p offsets as the constructor, applied on the next reset like sb3's own vec envs do. The wire change is additive, older plugin versions ignore the extra key, so nothing breaks. Reading it on the Godot side is a small companion change in godot_rl_agents_plugin (the reset handler in sync.gd), happy to open that too. --- godot_rl/core/godot_env.py | 6 ++ godot_rl/wrappers/stable_baselines_wrapper.py | 20 ++++- tests/test_seeding.py | 87 +++++++++++++++++++ 3 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 tests/test_seeding.py diff --git a/godot_rl/core/godot_env.py b/godot_rl/core/godot_env.py index 72ed8948..3f2f1bd8 100644 --- a/godot_rl/core/godot_env.py +++ b/godot_rl/core/godot_env.py @@ -244,12 +244,18 @@ def reset(self, seed=None): """ Reset the Godot environment. + Args: + seed (int): optional seed sent to the game along with the reset request. + Returns: dict: The initial observation data. """ message = { "type": "reset", } + if seed is not None: + # int() as numpy integers are not json serializable + message["seed"] = int(seed) self._send_as_json(message) response = self._get_json_dict() response["obs"] = self._process_obs(response["obs"]) diff --git a/godot_rl/wrappers/stable_baselines_wrapper.py b/godot_rl/wrappers/stable_baselines_wrapper.py index 1db812a9..7b5a5523 100644 --- a/godot_rl/wrappers/stable_baselines_wrapper.py +++ b/godot_rl/wrappers/stable_baselines_wrapper.py @@ -46,6 +46,9 @@ def __init__( # Initialize the results holder self.results = None + # Seeds latched by seed(), applied on the next reset + self._seeds: List[Optional[int]] = [None] * n_parallel + def _check_valid_action_space(self) -> None: # Check if the action space is a tuple space with multiple spaces action_space = self.envs[0].action_space @@ -94,12 +97,15 @@ def reset(self) -> Dict[str, np.ndarray]: all_obs = [] all_info = [] - # Reset each environment + # Reset each environment, applying any seed latched by seed() for i in range(self.n_parallel): - obs, info = self.envs[i].reset() + obs, info = self.envs[i].reset(seed=self._seeds[i]) all_obs.extend(obs) all_info.extend(info) + # A latched seed applies to one reset only, as in sb3's own vec envs + self._seeds = [None] * self.n_parallel + # Convert list of dictionaries to dictionary of lists obs = lod_to_dol(all_obs) return {k: np.array(v) for k, v in obs.items()} @@ -135,8 +141,14 @@ def get_attr(self, attr_name: str, indices=None) -> List[Any]: return [None for _ in range(self.num_envs)] raise AttributeError("get attr not fully implemented in godot-rl StableBaselinesWrapper") - def seed(self, seed=None): - raise NotImplementedError() + def seed(self, seed: Optional[int] = None) -> List[Optional[int]]: + # Each parallel game gets seed + p, matching the offsets used when they were launched. + # The seeds are latched here and sent with the next reset. + if seed is None: + self._seeds = [None] * self.n_parallel + else: + self._seeds = [seed + p for p in range(self.n_parallel)] + return list(self._seeds) def set_attr(self): raise NotImplementedError() diff --git a/tests/test_seeding.py b/tests/test_seeding.py new file mode 100644 index 00000000..f22ccd46 --- /dev/null +++ b/tests/test_seeding.py @@ -0,0 +1,87 @@ +import numpy as np + +from godot_rl.core.godot_env import GodotEnv +from godot_rl.wrappers.stable_baselines_wrapper import StableBaselinesGodotEnv + + +def make_godot_env(): + # Bypass __init__ so no game is launched, and record what would go on the socket + env = object.__new__(GodotEnv) + env.num_envs = 1 + env.sent = [] + env._send_as_json = env.sent.append + env._get_json_dict = lambda: {"type": "reset", "obs": [{"obs": [0.0]}]} + env._process_obs = lambda obs: obs + return env + + +def test_reset_without_a_seed_is_unchanged(): + env = make_godot_env() + env.reset() + assert env.sent == [{"type": "reset"}] + + +def test_reset_forwards_the_seed(): + env = make_godot_env() + env.reset(seed=42) + assert env.sent == [{"type": "reset", "seed": 42}] + + +def test_numpy_seeds_are_converted_to_int(): + # json.dumps cannot serialize numpy integers + env = make_godot_env() + env.reset(seed=np.int64(7)) + assert type(env.sent[0]["seed"]) is int + assert env.sent[0]["seed"] == 7 + + +class FakeGodotEnv: + def __init__(self, num_envs): + self.num_envs = num_envs + self.reset_seed = "not reset yet" + + def reset(self, seed=None): + self.reset_seed = seed + return [{"obs": np.zeros(2)} for _ in range(self.num_envs)], [{}] * self.num_envs + + +def make_vec_env(n_parallel=3, num_envs=1): + env = object.__new__(StableBaselinesGodotEnv) + env.envs = [FakeGodotEnv(num_envs) for _ in range(n_parallel)] + env.n_parallel = n_parallel + env._seeds = [None] * n_parallel + return env + + +def test_seed_returns_one_seed_per_parallel_game(): + env = make_vec_env(3) + assert env.seed(10) == [10, 11, 12] + + +def test_latched_seeds_reach_the_games_on_the_next_reset(): + env = make_vec_env(3) + env.seed(10) + env.reset() + assert [e.reset_seed for e in env.envs] == [10, 11, 12] + + +def test_a_seed_applies_to_a_single_reset(): + env = make_vec_env(2) + env.seed(5) + env.reset() + env.reset() + assert [e.reset_seed for e in env.envs] == [None, None] + + +def test_reset_without_seeding_sends_no_seed(): + env = make_vec_env(2) + env.reset() + assert [e.reset_seed for e in env.envs] == [None, None] + + +def test_seed_none_clears_the_latch(): + env = make_vec_env(2) + env.seed(5) + assert env.seed(None) == [None, None] + env.reset() + assert [e.reset_seed for e in env.envs] == [None, None] From c4e7599f0be3b54814a254f1725edb917f213604 Mon Sep 17 00:00:00 2001 From: Eliasepfl Date: Wed, 9 Sep 2026 06:20:42 -0400 Subject: [PATCH 2/3] Keep the seeding tests importable without stable_baselines3 The rllib CI jobs uninstall stable_baselines3 before running the suite, so a module level import of the sb3 wrapper turns into a pytest collection error and takes the whole run down. Import the wrapper inside the helper that needs it and skip the vec env tests when ray is present, as the other sb3 tests already do. The GodotEnv reset tests need no sb3 and keep running everywhere. --- tests/test_seeding.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/test_seeding.py b/tests/test_seeding.py index f22ccd46..1e3b84c9 100644 --- a/tests/test_seeding.py +++ b/tests/test_seeding.py @@ -1,7 +1,12 @@ import numpy as np +import pytest from godot_rl.core.godot_env import GodotEnv -from godot_rl.wrappers.stable_baselines_wrapper import StableBaselinesGodotEnv +from godot_rl.core.utils import can_import + +# The sb3 wrapper imports stable_baselines3, which the rllib CI job uninstalls, so it is +# imported inside the helper below and the tests that need it are skipped when ray is present. +needs_sb3 = pytest.mark.skipif(can_import("ray"), reason="rllib and sb3 are not compatable") def make_godot_env(): @@ -46,6 +51,8 @@ def reset(self, seed=None): def make_vec_env(n_parallel=3, num_envs=1): + from godot_rl.wrappers.stable_baselines_wrapper import StableBaselinesGodotEnv + env = object.__new__(StableBaselinesGodotEnv) env.envs = [FakeGodotEnv(num_envs) for _ in range(n_parallel)] env.n_parallel = n_parallel @@ -53,11 +60,13 @@ def make_vec_env(n_parallel=3, num_envs=1): return env +@needs_sb3 def test_seed_returns_one_seed_per_parallel_game(): env = make_vec_env(3) assert env.seed(10) == [10, 11, 12] +@needs_sb3 def test_latched_seeds_reach_the_games_on_the_next_reset(): env = make_vec_env(3) env.seed(10) @@ -65,6 +74,7 @@ def test_latched_seeds_reach_the_games_on_the_next_reset(): assert [e.reset_seed for e in env.envs] == [10, 11, 12] +@needs_sb3 def test_a_seed_applies_to_a_single_reset(): env = make_vec_env(2) env.seed(5) @@ -73,12 +83,14 @@ def test_a_seed_applies_to_a_single_reset(): assert [e.reset_seed for e in env.envs] == [None, None] +@needs_sb3 def test_reset_without_seeding_sends_no_seed(): env = make_vec_env(2) env.reset() assert [e.reset_seed for e in env.envs] == [None, None] +@needs_sb3 def test_seed_none_clears_the_latch(): env = make_vec_env(2) env.seed(5) From 2ef6ba7ee078a0161684f907ebf30ab09de34b1f Mon Sep 17 00:00:00 2001 From: Eliasepfl Date: Tue, 3 Nov 2026 11:12:05 -0500 Subject: [PATCH 3/3] Initialise the latched seeds next to n_parallel The seeds were initialised right after self.results, which the step_async PR removes, so the two PRs touched adjacent lines and whichever landed second would no longer merge cleanly. Initialise them where n_parallel is stored instead. No behaviour change. --- godot_rl/wrappers/stable_baselines_wrapper.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/godot_rl/wrappers/stable_baselines_wrapper.py b/godot_rl/wrappers/stable_baselines_wrapper.py index 7b5a5523..ee881368 100644 --- a/godot_rl/wrappers/stable_baselines_wrapper.py +++ b/godot_rl/wrappers/stable_baselines_wrapper.py @@ -40,15 +40,15 @@ def __init__( # Store the number of parallel environments self.n_parallel = n_parallel + # Seeds latched by seed(), applied on the next reset + self._seeds: List[Optional[int]] = [None] * n_parallel + # Check the action space for validity self._check_valid_action_space() # Initialize the results holder self.results = None - # Seeds latched by seed(), applied on the next reset - self._seeds: List[Optional[int]] = [None] * n_parallel - def _check_valid_action_space(self) -> None: # Check if the action space is a tuple space with multiple spaces action_space = self.envs[0].action_space