-
Notifications
You must be signed in to change notification settings - Fork 184
feat(openab-agent): expose MAX_TOOL_LOOPS via OPENAB_AGENT_MAX_TOOL_LOOPS env #1119
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
Merged
thepagent
merged 4 commits into
openabdev:main
from
canyugs:feat/configurable-max-tool-loops
Jun 16, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6f54d96
feat(openab-agent): expose MAX_TOOL_LOOPS via OPENAB_AGENT_MAX_TOOL_L…
canyugs f6cd0b7
fix: warn on invalid OPENAB_AGENT_MAX_TOOL_LOOPS and guard against zero
canyugs 75d13f6
test: add unit tests for max_tool_loops parsing; update ADR env example
canyugs d93cfc2
fix: downgrade max_tool_loops log to debug when default, info only wh…
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
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
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 |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| use anyhow::Result; | ||
| use serde::Deserialize; | ||
| use std::path::PathBuf; | ||
| use tracing::{debug, info}; | ||
| use tracing::{debug, info, warn}; | ||
|
|
||
| use crate::llm::{ContentBlock, LlmEvent, LlmProvider, Message, ToolDef}; | ||
| use crate::mcp::{self, McpRuntimeManager}; | ||
|
|
@@ -25,7 +25,33 @@ Be direct and concise. Execute tasks immediately rather than explaining what you | |
| // from the LLM and produced the "fs is disconnected, I give up" failure | ||
| // mode observed in the F1 PoC. | ||
|
|
||
| const MAX_TOOL_LOOPS: usize = 50; | ||
| const DEFAULT_MAX_TOOL_LOOPS: usize = 50; | ||
|
|
||
| fn max_tool_loops() -> usize { | ||
| let raw = match std::env::var("OPENAB_AGENT_MAX_TOOL_LOOPS") { | ||
| Ok(val) => match val.parse::<usize>() { | ||
| Ok(n) => n, | ||
| Err(e) => { | ||
| warn!( | ||
| "OPENAB_AGENT_MAX_TOOL_LOOPS={val:?} is not valid ({e}), \ | ||
| falling back to {DEFAULT_MAX_TOOL_LOOPS}" | ||
| ); | ||
| DEFAULT_MAX_TOOL_LOOPS | ||
| } | ||
| }, | ||
| Err(_) => DEFAULT_MAX_TOOL_LOOPS, | ||
| }; | ||
| if raw == 0 { | ||
| warn!( | ||
| "OPENAB_AGENT_MAX_TOOL_LOOPS=0 would prevent the agent from running; \ | ||
| using minimum value of 1" | ||
| ); | ||
| 1 | ||
| } else { | ||
| raw | ||
| } | ||
| } | ||
|
|
||
| /// Maximum number of messages to keep in context. When exceeded, oldest | ||
| /// messages (excluding the first user message) are dropped. | ||
| const MAX_CONTEXT_MESSAGES: usize = 100; | ||
|
|
@@ -137,8 +163,14 @@ impl Agent { | |
| }); | ||
|
|
||
| let mut final_text = String::new(); | ||
| let max_loops = max_tool_loops(); | ||
| if max_loops != DEFAULT_MAX_TOOL_LOOPS { | ||
| info!("max_tool_loops={max_loops} (overridden)"); | ||
| } else { | ||
| debug!("max_tool_loops={max_loops}"); | ||
| } | ||
|
|
||
| for iteration in 0..MAX_TOOL_LOOPS { | ||
| for iteration in 0..max_loops { | ||
|
Comment on lines
+166
to
+173
Contributor
Author
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. Addressed in f6cd0b7 — |
||
| debug!("agent loop iteration {iteration}"); | ||
|
|
||
| // Truncate context to prevent unbounded growth / token limit | ||
|
|
@@ -219,7 +251,7 @@ impl Agent { | |
|
|
||
| if final_text.is_empty() { | ||
| return Err(anyhow::anyhow!( | ||
| "agent exceeded maximum tool loop iterations ({MAX_TOOL_LOOPS})" | ||
| "agent exceeded maximum tool loop iterations ({max_loops})" | ||
| )); | ||
| } | ||
|
|
||
|
|
@@ -466,4 +498,32 @@ mod tests { | |
| let content = std::fs::read_to_string(tmp.path().join("out.txt")).unwrap(); | ||
| assert_eq!(content, "hello"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_max_tool_loops_default() { | ||
| temp_env::with_var("OPENAB_AGENT_MAX_TOOL_LOOPS", None::<&str>, || { | ||
| assert_eq!(max_tool_loops(), DEFAULT_MAX_TOOL_LOOPS); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_max_tool_loops_custom_value() { | ||
| temp_env::with_var("OPENAB_AGENT_MAX_TOOL_LOOPS", Some("200"), || { | ||
| assert_eq!(max_tool_loops(), 200); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_max_tool_loops_invalid_falls_back() { | ||
| temp_env::with_var("OPENAB_AGENT_MAX_TOOL_LOOPS", Some("abc"), || { | ||
| assert_eq!(max_tool_loops(), DEFAULT_MAX_TOOL_LOOPS); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_max_tool_loops_zero_clamps_to_one() { | ||
| temp_env::with_var("OPENAB_AGENT_MAX_TOOL_LOOPS", Some("0"), || { | ||
| assert_eq!(max_tool_loops(), 1); | ||
| }); | ||
| } | ||
| } | ||
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.
Intentionally left unbounded —
prompt_hard_timeout_secs(default 3600s) already acts as the cost/latency backstop. Adding a second hard cap would create a confusing interaction between the two knobs. Operators who set a high value are opting in.