verl icon indicating copy to clipboard operation
verl copied to clipboard

[ContinuousToken] Agent Loop Integration

Open gxlvera opened this issue 5 months ago • 1 comments

What does this PR do?

This PR wires Continuous Token into the existing AgentLoop execution paths. When CT is enabled and the rollout is text-only, AgentLoop builds the initial prompt with the CT builder, appends generated assistant tokens directly, and merges tool/user/system messages through CT while keeping response_mask and rollout logprobs aligned.

Multimodal processor paths continue to use the legacy behavior.

Checklist Before Starting

  • [x] Search for similar PRs. Paste at least one query link here: ...
  • [x] Format the PR title as [{modules}] {type}: {description} (This will be checked by the CI)
    • {modules} include fsdp, megatron, veomni, sglang, vllm, rollout, trainer, ci, training_utils, recipe, hardware, deployment, ray, worker, single_controller, misc, perf, model, algo, env, tool, ckpt, doc, data, cfg, reward, fully_async, one_step_off
    • If this PR involves multiple modules, separate them with , like [megatron, fsdp, doc]
    • {type} is in feat, fix, refactor, chore, test
    • If this PR breaks any API (CLI arguments, config, function signature, etc.), add [BREAKING] to the beginning of the title.
    • Example: [BREAKING][fsdp, megatron] feat: dynamic batching

Test

Test will be in PR 03

API and Usage Example

Demonstrate how the API changes if any, and provide usage example(s) if possible.

# Add code snippet or script demonstrating how to use this

Design & Code Changes

Main Changes

  • Creates a CT builder in AgentLoopBase from multi_turn.continuous_token config.
  • Adds helper methods for CT initial prompt creation, assistant-token merge, non-assistant message merge, prompt-length capping, and response metadata alignment.
  • Integrates CT into SingleTurnAgentLoop for text-only prompts.
  • Integrates CT into ToolAgentLoop for tool-call rollouts:
    • Keeps assistant generated tokens in the runtime token stream.
    • Appends tool responses using CT rather than isolated per-turn template rendering.
    • Preserves assistant tool_calls and tool tool_call_id/name metadata in message history.
  • Adds additional model-specific tool parsers needed (GLM, Seed, MiniMax, Kimi). (This is open to discussion. Inference backend like SGLang has already supported returning both token id and OpenAI compatible message that contained the parsed tool call. Maybe we don't have to implement and use tool parser in Verl if we can get parsed tool calls from inference backend directly?)

Changed Files

  • verl/experimental/agent_loop/agent_loop.py

    • Instantiates continuous_token_builder when CT is enabled and no multimodal processor is active.
    • Adds build_ct_initial_prompt, merge_ct_non_assistant_msg, and merge_ct_assistant_token.
    • Adds _align_continuous_token_response_metadata so inserted boundary tokens get mask/logprob 0, assistant tokens get mask 1, and removed prefix tokens are reflected in metadata.
    • Reuses _cap_text_prompt_length for CT and legacy text prompts.
  • verl/experimental/agent_loop/single_turn_agent_loop.py

    • Uses CT for text-only single-turn prompts.
    • Appends generated assistant tokens with CT and then splits the merged stream back into prompt/response for AgentLoopOutput.
    • Keeps masks and logprobs aligned with the CT-merged response.
  • verl/experimental/agent_loop/tool_agent_loop.py

    • Uses CT for the initial prompt and assistant/tool-message merges.
    • Appends assistant messages with structured tool_calls to the message history after parsing model output.
    • Adds tool-call IDs and tool names to generated tool messages.
    • Uses parser-provided stop_token_ids.
    • Keeps legacy special formatting paths for gpt-oss, gemma4, and multimodal tool responses.
  • verl/experimental/agent_loop/tool_parser.py

    • Adds GLMToolParser.
    • Adds SeedToolParser.
    • Adds MiniMaxToolParser.
    • Adds KimiToolParser, including last_tool_call_ids.
    • Adds stop-token behavior for GLM/Kimi-style tool-call boundaries.

Notes

CT remains opt-in. The integration is conservative: if CT is enabled but a multimodal processor is present, the code logs a warning and falls back to the legacy multimodal path.

This PR also contains the diff from PR01. After PR01 is merged, this branch will be rebased and will only show the diff broughput about by this PR.

Checklist Before Submitting

[!IMPORTANT] Please check all the following items before requesting a review, otherwise the reviewer might deprioritize this PR for review.

gxlvera avatar Jun 13 '26 01:06 gxlvera

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar Jun 13 '26 01:06 CLAassistant