Repository navigation
[RLlib] Don't mark fully truncated MultiAgentEpisode as terminated - #67863
Conversation
When every agent is done after `MultiAgentEpisode.add_env_step()`, the
"Case 2" block set `is_terminated = True` regardless of why the agents
were done. A fully truncated episode (e.g. a time limit with
`truncateds={"__all__": True}`) was therefore reported as both terminated
and truncated, and the flag was propagated to every agent's
`SingleAgentEpisode`. Learner connectors and the multi-agent episode
replay buffer then treated the last observations as terminal and did not
bootstrap their values.
Case 2 now only declares the episode terminated if at least one agent
terminated (at this or a prior timestep), and declares it truncated if
all agents were only truncated.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request updates the logic in MultiAgentEpisode.add_env_step to correctly distinguish between terminated and truncated episodes when all agents are done, ensuring proper bootstrapping. A comprehensive unit test is added to verify these scenarios. The reviewer suggested optimizing the check for completed agents by iterating over self.agent_episodes directly instead of calling self.agent_ids, which creates a new set on each invocation.
| # not yet terminated. If at least one agent terminated, declare the episode | ||
| # terminated. If all agents were only truncated, declare it truncated, so the | ||
| # last observations are still bootstrapped and not treated as terminal. | ||
| if not self.is_terminated and all(aid in agents_done for aid in self.agent_ids): |
There was a problem hiding this comment.
Using self.agent_ids here is inefficient because self.agent_ids is a property that constructs and returns a new set on every call. Since self.agent_episodes is a dictionary containing the agent IDs as keys, you can iterate over it directly. This avoids the overhead of creating a temporary set.
| if not self.is_terminated and all(aid in agents_done for aid in self.agent_ids): | |
| if not self.is_terminated and all(aid in agents_done for aid in self.agent_episodes): |
|
@pseudo-rnd-thoughts Can we please trigger release tests on this once? |
…inates When every agent is done and at least one of them terminated, Case 2 in `add_env_step()` declares the episode terminated (ray-project#67863). That flag was then copied onto every agent with data on that step, so agents that were only truncated (e.g. by a time limit, also when the env sends `truncateds["__all__"]=True`) ended up both terminated and truncated. PPO/GAE, DQN/SAC replay and v-trace then dropped their value bootstrap. An agent is now marked terminated only by its own flag, by the env's `terminateds["__all__"]`, or by the episode's terminated flag if it is not truncated. This is the per-agent propagation ray-project#67863 left as a known limitation. Also drop this branch's `get_truncateds()` change (covered by ray-project#67627) and move the tests into `test_add_env_step_all_agents_done_terminated_vs_truncated`. Signed-off-by: Wiem Khlifi <[email protected]>
…inates When every agent is done and at least one of them terminated, Case 2 in `add_env_step()` declares the episode terminated (ray-project#67863). That flag was then copied onto every agent with data on that step, so agents that were only truncated (e.g. by a time limit, also when the env sends `truncateds["__all__"]=True`) ended up both terminated and truncated. PPO/GAE, DQN/SAC replay and v-trace then dropped their value bootstrap. An agent is now marked terminated only by its own flag, by the env's `terminateds["__all__"]`, or by the episode's terminated flag if it is not truncated. This is the per-agent propagation ray-project#67863 left as a known limitation. The tests extend `test_add_env_step_all_agents_done_terminated_vs_truncated` with per-agent flag checks. Signed-off-by: Wiem Khlifi <[email protected]>
Why are these changes needed?
When every agent is done after
MultiAgentEpisode.add_env_step(), the "Case 2" block setsself.is_terminated = Trueregardless of why the agents are done. A fully truncated episode (the common time-limit case,truncateds={"__all__": True}) is therefore reported as both terminated and truncated:Because
add_env_step()sets each agent's flag asterminateds.get(agent_id) or self.is_terminated, the wrong flag also reaches every agent'sSingleAgentEpisode. Training code that reads it then treats time-limit truncations as true terminal states and does not bootstrap the value of the last observation:AddOneTsToEpisodesAndTruncate(PPO/GAE value targets) readssa_episode.is_terminated.MultiAgentEpisodeReplayBufferreadsma_episode.is_terminatedbefore the truncated flag, so the last transition of every agent getsTERMINATEDS=True, TRUNCATEDS=False.This change makes Case 2 declare the episode terminated only if at least one agent terminated (at this or a prior timestep), and truncated if all agents were only truncated. Episodes that mix terminated and truncated agents still count as terminated, as the existing comment describes, and the
__all__handling (Case 1) is unchanged.This was flagged in #67627, which fixes
get_truncateds()["__all__"]but leaves this block alone. With both PRs merged,get_terminateds()andget_truncateds()are correct for fully truncated episodes.Not a duplicate
get_truncateds()returningis_terminatedfor"__all__". It points out this bug but doesn't change it.Known limitation (out of scope)
In the mixed case (one agent terminated and another truncated at the same step, without
__all__), the episode is terminated and that flag is still propagated to the truncated agent'sSingleAgentEpisode. That's existing behavior, and changing the propagation could interact with #66913's newcomer handling, so I left it for a follow-up.Testing
Added
test_add_env_step_all_agents_done_terminated_vs_truncated. It covers: all agents truncated with and without__all__; agents truncated at different timesteps; mixed terminated/truncated at the same step and across steps; and all agents terminated. It fails on master (AssertionError: True is not false) and passes with this change.ray.init()in the test class'ssetUpClasshangs on my local macOS machine, so I ran theTestMultiAgentEpisodecases throughunittestwithray.initstubbed out (the episode tests don't use Ray; I excluded the env-runner test):CI runs the full test file, including the env-runner test.
AI assistance
AI assistance (Claude Code) was used to find the issue, write the fix and the test, and draft this description.
🤖 Generated with Claude Code