From 989c784acabd7a4d0cdfe5c81cd691fe5adac640 Mon Sep 17 00:00:00 2001 From: Reginald Alfret Date: Sat, 14 Nov 2026 16:14:42 +0530 Subject: [PATCH] fix(pettingzoo): fix action space resolution, error message formatting and agent cleanup - Fix discrete action space resolution so single-branch discrete space retains spaces.Discrete instead of being overwritten by MultiDiscrete in mixed action spaces - Fix invalid action error message formatting to display current_action_space rather than bound method representation - Guard _cleanup_agents against ValueError when terminated agent is already removed from _live_agents - Add regression unit tests in test_pettingzoo_wrapper.py --- .../envs/unity_pettingzoo_base_env.py | 7 +++--- .../tests/test_pettingzoo_wrapper.py | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/ml-agents-envs/mlagents_envs/envs/unity_pettingzoo_base_env.py b/ml-agents-envs/mlagents_envs/envs/unity_pettingzoo_base_env.py index 36d75b7ba8..bebf375e4b 100644 --- a/ml-agents-envs/mlagents_envs/envs/unity_pettingzoo_base_env.py +++ b/ml-agents-envs/mlagents_envs/envs/unity_pettingzoo_base_env.py @@ -119,6 +119,7 @@ def _update_action_spaces(self) -> None: and len(act_spec.discrete_branches) == 0 ): raise error.Error("No actions found") + d_space = None if act_spec.discrete_size == 1: d_space = spaces.Discrete(act_spec.discrete_branches[0]) if self._seed is not None: @@ -126,7 +127,7 @@ def _update_action_spaces(self) -> None: if act_spec.continuous_size == 0: self._action_spaces[behavior_name] = d_space continue - if act_spec.discrete_size > 0: + elif act_spec.discrete_size > 1: d_space = spaces.MultiDiscrete(act_spec.discrete_branches) if self._seed is not None: d_space.seed(self._seed) @@ -154,7 +155,7 @@ def _process_action(self, current_agent, action): action = self._action_to_np(current_action_space, action) if not current_action_space.contains(action): # type: ignore raise error.Error( - f"Invalid action, got {action} but was expecting action from {self.action_space}" + f"Invalid action, got {action} but was expecting action from {current_action_space}" ) if isinstance(current_action_space, spaces.Tuple): action = ActionTuple(action[0], action[1]) @@ -202,7 +203,7 @@ def _step(self): def _cleanup_agents(self): for current_agent in self._agents: - if self._terminations[current_agent] or self._truncations[current_agent]: + if (self._terminations.get(current_agent, False) or self._truncations.get(current_agent, False)) and current_agent in self._live_agents: self._live_agents.remove(current_agent) @property diff --git a/ml-agents-envs/tests/test_pettingzoo_wrapper.py b/ml-agents-envs/tests/test_pettingzoo_wrapper.py index 71b55042c0..f16af91eac 100644 --- a/ml-agents-envs/tests/test_pettingzoo_wrapper.py +++ b/ml-agents-envs/tests/test_pettingzoo_wrapper.py @@ -84,3 +84,26 @@ def test_reset_seed_reseeds_action_spaces(): second = env.action_space(agent).sample() assert np.array_equal(first, second) + + +def test_invalid_action_error_message(): + unity_env = SimpleEnvironment(["test_single"]) + env = UnityAECEnv(unity_env) + env.reset() + agent = env.possible_agents[0] + with pytest.raises(Exception) as exc_info: + env.step(9999) + assert "bound method" not in str(exc_info.value) + assert "Discrete" in str(exc_info.value) or "Box" in str(exc_info.value) or "MultiDiscrete" in str(exc_info.value) + + +def test_cleanup_agents_safe_when_already_removed(): + unity_env = SimpleEnvironment(["test_single"]) + env = UnityParallelEnv(unity_env) + env.reset() + agent = env.possible_agents[0] + env._terminations[agent] = True + env._live_agents.clear() # simulate already removed + # Must not raise ValueError + env._cleanup_agents() + assert len(env._live_agents) == 0