Skip to content

[RLlib] Don't mark fully truncated MultiAgentEpisode as terminated - #67863

Merged
pseudo-rnd-thoughts merged 1 commit into
ray-project:masterfrom
pseudo-rnd-thoughts:rllib-ma-episode-truncated-not-terminated
Nov 5, 2026
Merged

pseudo-rnd-thoughts merged 1 commit into
ray-project:masterfrom
pseudo-rnd-thoughts:rllib-ma-episode-truncated-not-terminated

Conversation

@pseudo-rnd-thoughts

Copy link
Copy Markdown
Member

Why are these changes needed?

When every agent is done after MultiAgentEpisode.add_env_step(), the "Case 2" block sets self.is_terminated = True regardless 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:

e = MultiAgentEpisode(observations=[{"a": 0, "b": 0}])
e.add_env_step(..., terminateds={"__all__": False},
               truncateds={"a": True, "b": True, "__all__": True})
e.is_terminated, e.is_truncated  # (True, True) on master

Because add_env_step() sets each agent's flag as terminateds.get(agent_id) or self.is_terminated, the wrong flag also reaches every agent's SingleAgentEpisode. 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) reads sa_episode.is_terminated.
  • MultiAgentEpisodeReplayBuffer reads ma_episode.is_terminated before the truncated flag, so the last transition of every agent gets TERMINATEDS=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() and get_truncateds() are correct for fully truncated episodes.

Not a duplicate

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's SingleAgentEpisode. 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's setUpClass hangs on my local macOS machine, so I ran the TestMultiAgentEpisode cases through unittest with ray.init stubbed out (the episode tests don't use Ray; I excluded the env-runner test):

TestMultiAgentEpisode (18 tests): Ran 18 tests ... OK
pytest python/ray/rllib/utils/replay_buffers/tests/test_multi_agent_episode_buffer.py \
       python/ray/rllib/utils/replay_buffers/tests/test_multi_agent_prio_episode_buffer.py
7 passed
pre-commit run --files <changed files>: passed

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

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]>
@pseudo-rnd-thoughts pseudo-rnd-thoughts added the rllib RLlib related issues label Nov 5, 2026
@pseudo-rnd-thoughts
pseudo-rnd-thoughts marked this pull request as ready for review November 5, 2026 11:45
@pseudo-rnd-thoughts
pseudo-rnd-thoughts requested a review from a team as a code owner November 5, 2026 11:45
@pseudo-rnd-thoughts pseudo-rnd-thoughts added the go add ONLY when ready to merge, run all tests label Nov 5, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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):

@ArturNiederfahrenhorst

Copy link
Copy Markdown
Contributor

@pseudo-rnd-thoughts Can we please trigger release tests on this once?

@pseudo-rnd-thoughts
pseudo-rnd-thoughts merged commit 49720bc into ray-project:master Nov 5, 2026
6 of 7 checks passed
WiemKhlifi pushed a commit to WiemKhlifi/ray that referenced this pull request Nov 6, 2026
…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]>
WiemKhlifi pushed a commit to WiemKhlifi/ray that referenced this pull request Nov 6, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests rllib RLlib related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants