Skip to content

Commit 422ffe4

Browse files
HarishKMuraliclaudeZhentao Fan
committed
feat(recipe): add context-management agent loops
Port the agentic context-management code from verl#5636 ([algo] feat: supporting agentic rl with context management; issue #5375) into a self-contained recipe, as the verl maintainers requested. Contents: the ContextManager abstraction with sliding-window and summarizer implementations, the naive_summarizer_agent and tool_sliding_window_agent loops, the design doc, a runnable GRPO example, and the original CPU unit tests (19 passing against verl main 9c38b8bb). Changes vs #5636: relocate intra-package imports to recipe.context_management.* and fix one drifted core import (verl.tools.utils.tool_registry -> verl.tools.tool_registry) for current verl. AI assistance was used. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Co-authored-by: Zhentao Fan <zhentao.fan@mail.utoronto.ca>
1 parent e0f4dc2 commit 422ffe4

11 files changed

Lines changed: 2105 additions & 0 deletions

context_management/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Context-management agent loops
2+
3+
Plug-in **context management** for verl agent loops: keep multi-turn / long-horizon rollouts within
4+
the model's context window by compressing the trajectory on the fly, instead of truncating or
5+
failing once the window is exceeded.
6+
7+
This recipe provides two ready-to-use agent loops and the `ContextManager` abstraction they share:
8+
9+
| Agent loop (`name`) | Class | Strategy |
10+
|---|---|---|
11+
| `naive_summarizer_agent` | `SummarizerAgentLoop` | When the model emits a `<summary>...</summary>` block, replace the history with `(initial prompt + summary)` and continue. |
12+
| `tool_sliding_window_agent` | `ToolSlidingWindowAgentLoop` | Keep a sliding window over tool-calling turns, dropping the oldest turns when the window is exceeded. |
13+
14+
Both subclass `AgentLoopWithContextManagement`, which drives a generic
15+
`generate → check_and_compress → continue` loop around any `ContextManager`
16+
(`SummarizerContextManager`, `SlidingWindowContextManager`, or your own).
17+
18+
## Background
19+
20+
This code was originally proposed for verl core in
21+
[volcengine/verl#5636](https://github.com/verl-project/verl/pull/5636)
22+
("[algo] feat: supporting agentic rl with context management", see issue
23+
[#5375](https://github.com/verl-project/verl/issues/5375)). At the maintainers' request it now lives
24+
here as a self-contained recipe rather than in `verl/experimental/agent_loop/`, so it can evolve
25+
independently of the core library. The multi-trajectory / session-level GRPO training support that
26+
complements it lands separately in core (see verl#5401, #5969).
27+
28+
## Layout
29+
30+
```
31+
context_management/
32+
context_manager.py # ContextManager + Sliding-window / Summarizer implementations
33+
agent_loop_with_context_management.py # AgentLoopWithContextManagement + the two agent loops
34+
context_manager_plugin.md # design notes / how to write a custom ContextManager
35+
test_context_manager.py # CPU unit tests
36+
test_agent_loop_with_context_management.py
37+
example/ # runnable GRPO example wiring the summarizer loop
38+
```
39+
40+
## Usage
41+
42+
The loops register themselves under the `name`s above. Point verl at this recipe's agent-loop config
43+
and select a loop:
44+
45+
```bash
46+
actor_rollout_ref.rollout.agent.agent_loop_config_path=recipe/context_management/example/agent.yaml
47+
actor_rollout_ref.rollout.agent.default_agent_loop=naive_summarizer_agent
48+
```
49+
50+
See [`example/`](example/) for a full run script, and
51+
[`context_manager_plugin.md`](context_manager_plugin.md) for writing your own `ContextManager`.
52+
53+
## Required verl version
54+
55+
See [`REQUIRED_VERL.txt`](REQUIRED_VERL.txt) for the upstream repo and the pinned core-library commit.
56+
57+
## Tests
58+
59+
```bash
60+
pytest recipe/context_management/test_context_manager.py
61+
pytest recipe/context_management/test_agent_loop_with_context_management.py
62+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# context_management — rolling; refresh the commit against your verl checkout before publishing
2+
UPSTREAM=https://github.com/verl-project/verl.git
3+
MODE=rolling
4+
BRANCH=main
5+
# Core-library commit this recipe was developed/tested against. Refresh before opening the PR.
6+
VERL_COMMIT=9c38b8bb1876a81273d76de3e79328b2dd2b7b32
7+
PIP_INSTALL=pip install verl@git+https://github.com/verl-project/verl.git@9c38b8bb1876a81273d76de3e79328b2dd2b7b32
8+
GIT_SETUP=git clone https://github.com/verl-project/verl.git && cd verl && git checkout 9c38b8bb1876a81273d76de3e79328b2dd2b7b32 && git submodule update --init --recursive recipe
9+
RECIPE_FOLDER=context_management
10+
NOTES=Depends only on stable verl core APIs: verl.experimental.agent_loop.agent_loop (AgentLoopBase, register, AgentLoopOutput, AgentLoopMetrics), verl.tools, verl.utils.chat_template, verl.utils.tokenizer, verl.workers.rollout.replica.TokenOutput. No core code changes are required to use this recipe.
11+
REFRESH=Recompute VERL_COMMIT: (cd verl && git rev-parse HEAD). Re-run the tests under recipe/context_management/ after bumping.

context_management/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2024 Bytedance Ltd. and/or its affiliates
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

0 commit comments

Comments
 (0)