Skip to content

Update tool_loop_agent_runner.py#6725

Open
LIghtJUNction wants to merge 1 commit intomasterfrom
LIghtJUNction-patch-3
Open

Update tool_loop_agent_runner.py#6725
LIghtJUNction wants to merge 1 commit intomasterfrom
LIghtJUNction-patch-3

Conversation

@LIghtJUNction
Copy link
Member

@LIghtJUNction LIghtJUNction commented Mar 20, 2026

Screenshot_2026-03-21-02-44-47-88_84d3000e3f4017145260f7618db1d683

Modifications / 改动点

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果


Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Adjust tool loop agent defaults for context handling and step iteration limits.

Enhancements:

  • Set a nonzero default max_context_tokens and a fallback enforce_max_turns value in the agent context configuration.
  • Cap the maximum tool loop step count to a small upper bound to limit iterations during agent execution.

@auto-assign auto-assign bot requested review from Fridemn and Raven95676 March 20, 2026 20:26
@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Mar 20, 2026
@chatgpt-codex-connector
Copy link

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refines the ToolLoopAgentRunner's operational parameters by adjusting default values for context window management and introducing a hard limit on the number of steps an agent can execute in a single iteration. These changes aim to improve the agent's efficiency and predictability by providing more controlled defaults for how it handles conversational context and by preventing overly long processing chains.

Highlights

  • Context Configuration Defaults: Modified the default values for max_context_tokens from 0 to 4096 and adjusted enforce_max_turns to default to 15 if its value is -1, enhancing agent context management.
  • Agent Step Limiting: Introduced a new constraint within the step_until_done method to limit the maximum number of steps an agent can take, capping it at 3 steps to prevent excessive computation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • Changing max_context_tokens default from 0 (no compression) to 4096 and forcing enforce_max_turns to 15 when -1 is passed introduces new implicit behavior; consider driving these values from configuration or explicit defaults instead of hardcoding to avoid surprising callers.
  • The unconditional max_step = min(max_step, 3) silently overrides the caller’s requested max_step; if this cap is needed, consider exposing it as a configuration option or clarifying it in the function’s contract rather than hardcoding it.
  • The inline TODO about changing max_step from 30 to a smaller value is now effectively implemented with 3; either remove or rephrase the TODO to reflect the current behavior and any remaining work.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Changing `max_context_tokens` default from `0` (no compression) to `4096` and forcing `enforce_max_turns` to `15` when `-1` is passed introduces new implicit behavior; consider driving these values from configuration or explicit defaults instead of hardcoding to avoid surprising callers.
- The unconditional `max_step = min(max_step, 3)` silently overrides the caller’s requested `max_step`; if this cap is needed, consider exposing it as a configuration option or clarifying it in the function’s contract rather than hardcoding it.
- The inline TODO about changing `max_step` from 30 to a smaller value is now effectively implemented with `3`; either remove or rephrase the TODO to reflect the current behavior and any remaining work.

## Individual Comments

### Comment 1
<location path="astrbot/core/agent/runners/tool_loop_agent_runner.py" line_range="609-610" />
<code_context>
     ) -> T.AsyncGenerator[AgentResponse, None]:
         """Process steps until the agent is done."""
         step_count = 0
+        # TODO:将max_step由30改为一个较小的值
+        max_step = min(max_step, 3)
         while not self.done() and step_count < max_step:
             step_count += 1
</code_context>
<issue_to_address>
**issue (bug_risk):** Hard-capping max_step to 3 may break callers and can fail when max_step is None or non-positive.

Rebinding `max_step` to `min(max_step, 3)` silently imposes a global cap and ignores caller-provided values, which can break longer-running agents. It will also raise or change behavior if `max_step` can be `None` or `<= 0` (often used to mean “no limit”). Instead, either apply this cap only at specific call sites that need it, use a separate internal/debug variable guarded by a flag, or validate/normalize `max_step` so `min` is only applied when it’s a positive int.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +609 to +610
# TODO:将max_step由30改为一个较小的值
max_step = min(max_step, 3)
Copy link
Contributor

Choose a reason for hiding this comment

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

issue (bug_risk): Hard-capping max_step to 3 may break callers and can fail when max_step is None or non-positive.

Rebinding max_step to min(max_step, 3) silently imposes a global cap and ignores caller-provided values, which can break longer-running agents. It will also raise or change behavior if max_step can be None or <= 0 (often used to mean “no limit”). Instead, either apply this cap only at specific call sites that need it, use a separate internal/debug variable guarded by a flag, or validate/normalize max_step so min is only applied when it’s a positive int.

@dosubot dosubot bot added the area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. label Mar 20, 2026
@dosubot
Copy link

dosubot bot commented Mar 20, 2026

Related Documentation

1 document(s) may need updating based on files changed in this PR:

AstrBotTeam's Space

pr4697的改动
View Suggested Changes
@@ -273,6 +273,8 @@
 - 子代理执行时,`max_steps` 参数从系统配置 `provider_settings.max_agent_step` 读取(默认值为 30)
 - 用户可通过桌面应用 → 配置文件 → 普通配置 → AI 配置页面,统一设置主代理和子代理的工具调用轮数上限
 - 子代理在执行长自动化任务时不再因硬编码上限而提前终止,而是遵循用户配置的上限值
+
+> **重要变更**:`max_agent_step` 配置现已被限制为最大 3 步,无论用户配置的值为多少。该限制通过 `step_until_done()` 方法中的 `max_step = min(max_step, 3)` 实现。此限制是临时措施,未来可能调整为其他较小值。
 
 ##### 参考图片自动传递(PR #5579)
 
@@ -494,6 +496,12 @@
 
 #### 逻辑改进
 工具注册和配置加载逻辑已优化,确保子代理配置的正确性和工具的动态注册。FunctionTool 新增 `is_background_task` 属性,支持异步后台任务。
+
+**上下文压缩配置**:工具循环 Agent Runner 的上下文管理配置已更新:
+
+- **`max_context_tokens`**:默认值从 0(禁用压缩)更改为 4096,意味着当上下文超过 4096 个 token 时,默认启用上下文压缩
+- **`enforce_max_turns`**:当配置为 -1 时,默认值为 15 个对话轮次,超过此轮数后强制执行压缩
+- 这些默认值通过 `ContextConfig` 在 `tool_loop_agent_runner.py` 的 `reset()` 方法中设置
 
 #### MCP 客户端初始化(PR #5993)
 

[Accept] [Decline]

Note: You must be authenticated to accept/decline updates.

How did I do? Any feedback?  Join Discord

Copy link
Contributor

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

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 introduces sensible defaults to improve agent behavior by preventing excessively long-running or resource-intensive executions. It sets default values for max_context_tokens and enforce_max_turns to enable context management by default, and caps the maximum number of tool loop steps. My feedback focuses on improving maintainability by replacing magic numbers with named constants. The provided rule regarding asyncio event loops does not apply to these comments, so they remain unchanged.

Comment on lines +142 to +144
max_context_tokens=provider.provider_config.get("max_context_tokens", 4096),
# enforce max turns before compression
enforce_max_turns=self.enforce_max_turns,
enforce_max_turns=self.enforce_max_turns if self.enforce_max_turns != -1 else 15,
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The values 4096 and 15 are used as magic numbers for default configuration values. To improve code clarity and maintainability, it's recommended to define these as named constants at the module level. For example:

DEFAULT_MAX_CONTEXT_TOKENS = 4096
DEFAULT_ENFORCE_MAX_TURNS = 15

These constants can then be used here, making the code easier to understand and modify in the future.

"""Process steps until the agent is done."""
step_count = 0
# TODO:将max_step由30改为一个较小的值
max_step = min(max_step, 3)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The value 3 is hardcoded here to cap the maximum number of agent steps. Using a magic number like this can make the code harder to understand and maintain. It would be better to define it as a named constant at the module level, for instance:

MAX_TOOL_STEPS_CAP = 3

Then, you could use max_step = min(max_step, MAX_TOOL_STEPS_CAP). This makes the purpose of the value clear and centralizes its definition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant