Skip to content

Fix #2051: [Feature]: LocalAgentRunner Agent Loop 应增加上下文保护与截断机制#2105

Open
JiwaniZakir wants to merge 1 commit intolangbot-app:masterfrom
JiwaniZakir:fix/2051-feature-localagentrunner-agent-loop
Open

Fix #2051: [Feature]: LocalAgentRunner Agent Loop 应增加上下文保护与截断机制#2105
JiwaniZakir wants to merge 1 commit intolangbot-app:masterfrom
JiwaniZakir:fix/2051-feature-localagentrunner-agent-loop

Conversation

@JiwaniZakir
Copy link
Copy Markdown

Closes #2051

概述 / Overview

请在此部分填写你实现/解决/优化的内容:
Summary of what you implemented/solved/optimized:

LocalAgentRunnersrc/langbot/pkg/provider/runners/localagent.py)的 Agent 循环增加了两项上下文保护机制,防止因无限工具调用或超大工具返回结果导致的 token 失控与上下文溢出。

Two context-protection mechanisms have been added to the LocalAgentRunner agent loop to prevent unbounded token consumption and context window overflow:

  1. 最大迭代次数限制 / Max iteration cap — 新增模块级常量 MAX_TOOL_ITERATIONS = 16,将 while pending_tool_calls: 改为 while pending_tool_calls and iteration_count < MAX_TOOL_ITERATIONS:,并在每轮递增计数器。LLM 持续返回 tool_calls(模型幻觉或工具重试)时循环不再无限运行。

  2. 工具返回结果截断 / Tool result truncation — 新增模块级常量 MAX_TOOL_RESULT_LENGTH = 8000。在将工具结果序列化为 JSON 后,若字符数超出上限,截断并附加 \n...[truncated] 标记,防止单条工具消息独自撑满上下文窗口。

同时在 tests/unit_tests/provider/test_localagent.py 中新增 5 个单元测试,独立验证上述两段逻辑,不依赖完整的 langbot 导入链。

更改前后对比截图 / Screenshots

修改前 / Before:

while pending_tool_calls: — 无迭代上限;工具结果原样写入消息,无长度限制。

修改后 / After:

while pending_tool_calls and iteration_count < MAX_TOOL_ITERATIONS: — 最多执行 16 轮;工具结果超过 8000 字符时自动截断并追加 ...[truncated]。单元测试全部通过(见下方检查清单)。

检查清单 / Checklist

PR 作者完成 / For PR author

请在方括号间写x以打勾 / Please tick the box with x

  • 阅读仓库贡献指引了吗? / Have you read the contribution guide?
  • 与项目所有者沟通过了吗? / Have you communicated with the project maintainer?
  • 我确定已自行测试所作的更改,确保功能符合预期。 / I have tested the changes and ensured they work as expected.

项目维护者完成 / For project maintainer

  • 相关 issues 链接了吗? / Have you linked the related issues?
  • 配置项写好了吗?迁移写好了吗?生效了吗? / Have you written the configuration items? Have you written the migration? Has it taken effect?
  • 依赖加到 pyproject.toml 和 core/bootutils/deps.py 了吗 / Have you added the dependencies to pyproject.toml and core/bootutils/deps.py?
  • 文档编写了吗? / Have you written the documentation?

This PR was created with AI assistance (Claude). The changes were reviewed by quality gates and a critic model before submission.

…ntRunner

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. eh: Improve enhance: 现有功能的改进 / improve current features m: Provider LLM 模型相关 / LLMs management labels Apr 3, 2026
@RockChinQ RockChinQ requested a review from huanghuoguoguo April 4, 2026 15:55
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 4, 2026

Codecov Report

❌ Patch coverage is 28.57143% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/langbot/pkg/provider/runners/localagent.py 28.57% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

@RockChinQ
Copy link
Copy Markdown
Member

麻烦贴一下修改前后截图证明功能正常,以便我们快速review并合入

def test_max_tool_result_length_constant():
"""MAX_TOOL_RESULT_LENGTH should be a positive integer."""
assert isinstance(MAX_TOOL_RESULT_LENGTH, int)
assert MAX_TOOL_RESULT_LENGTH > 0
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

这俩单元测试没有什么意义吧,而且变量定义在单元测试中

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

这个单元测试可以不需要。没有什么用。

@huanghuoguoguo
Copy link
Copy Markdown
Collaborator

#2057 同时参考这个pr内提出的问题。

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

Labels

eh: Improve enhance: 现有功能的改进 / improve current features m: Provider LLM 模型相关 / LLMs management size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: LocalAgentRunner Agent Loop 应增加上下文保护与截断机制

3 participants