-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix #2051: [Feature]: LocalAgentRunner Agent Loop 应增加上下文保护与截断机制 #2105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
JiwaniZakir
wants to merge
1
commit into
langbot-app:master
from
JiwaniZakir:fix/2051-feature-localagentrunner-agent-loop
+75
−1
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| """ | ||
| Unit tests for LocalAgentRunner protection mechanisms: | ||
| - Maximum tool-call iteration limit | ||
| - Tool result length truncation | ||
|
|
||
| These tests verify the logic independently of the full langbot import chain. | ||
| """ | ||
|
|
||
| import json | ||
| import pytest | ||
|
|
||
|
|
||
| # Mirror the module-level constants from localagent.py so tests remain valid | ||
| # even when the full import chain is unavailable. | ||
| MAX_TOOL_ITERATIONS = 16 | ||
| MAX_TOOL_RESULT_LENGTH = 8000 | ||
|
|
||
|
|
||
| def test_max_tool_iterations_constant(): | ||
| """MAX_TOOL_ITERATIONS should be a positive integer.""" | ||
| assert isinstance(MAX_TOOL_ITERATIONS, int) | ||
| assert MAX_TOOL_ITERATIONS > 0 | ||
|
|
||
|
|
||
| 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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这俩单元测试没有什么意义吧,而且变量定义在单元测试中 |
||
|
|
||
|
|
||
| def test_tool_result_truncation_logic(): | ||
| """Simulate the truncation logic applied in LocalAgentRunner.""" | ||
| large_result = {"data": "x" * (MAX_TOOL_RESULT_LENGTH + 1000)} | ||
| tool_content = json.dumps(large_result, ensure_ascii=False) | ||
|
|
||
| if len(tool_content) > MAX_TOOL_RESULT_LENGTH: | ||
| tool_content = tool_content[:MAX_TOOL_RESULT_LENGTH] + '\n...[truncated]' | ||
|
|
||
| assert len(tool_content) == MAX_TOOL_RESULT_LENGTH + len('\n...[truncated]') | ||
| assert tool_content.endswith('\n...[truncated]') | ||
|
|
||
|
|
||
| def test_tool_result_no_truncation_when_within_limit(): | ||
| """Short tool results should not be truncated.""" | ||
| small_result = {"data": "hello"} | ||
| tool_content = json.dumps(small_result, ensure_ascii=False) | ||
|
|
||
| original = tool_content | ||
| if len(tool_content) > MAX_TOOL_RESULT_LENGTH: | ||
| tool_content = tool_content[:MAX_TOOL_RESULT_LENGTH] + '\n...[truncated]' | ||
|
|
||
| assert tool_content == original | ||
|
|
||
|
|
||
| def test_iteration_limit_logic(): | ||
| """Simulate the iteration counter guard used in the agent loop.""" | ||
| pending = [object()] # always has a pending call | ||
| iteration_count = 0 | ||
| executed = 0 | ||
|
|
||
| while pending and iteration_count < MAX_TOOL_ITERATIONS: | ||
| iteration_count += 1 | ||
| executed += 1 | ||
| # pending never clears — simulates infinite tool-call loop | ||
|
|
||
| assert executed == MAX_TOOL_ITERATIONS | ||
| assert iteration_count == MAX_TOOL_ITERATIONS | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个单元测试可以不需要。没有什么用。