Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(agent): remove hard turn cap; 0 = unbounded

Akarshan Biswas89edb8846906
diff --git a/src-tauri/src/core/agent/loop.rs b/src-tauri/src/core/agent/loop.rs
index 4689d840e..afdbe5ab5 100644
--- a/src-tauri/src/core/agent/loop.rs
+++ b/src-tauri/src/core/agent/loop.rs
@@ -528,11 +528,13 @@ async fn orchestrate_inner(
)
.await?;
+ // Explicit values pass through unclamped; `0` means unbounded (guarded by
+ // the session token budget and cancellation). Absent falls back to 8 for
+ // the proxy path, which has no interactive cancel.
let max_turns = json_body
.get("max_turns")
.and_then(|v| v.as_u64())
- .unwrap_or(8)
- .clamp(1, 400) as usize;
+ .unwrap_or(8) as usize;
let http_model = HttpModelInvoker {
client: client.clone(),
@@ -610,7 +612,12 @@ async fn run_turn_cycle(
) -> Result<serde_json::Value, String> {
let mut last_response: Option<serde_json::Value> = None;
- for turn in 0..max_turns {
+ // `max_turns == 0` means unbounded: the session token budget and user
+ // cancellation are the real guards, so an interactive run isn't cut off
+ // mid-task by a fixed turn cap.
+ let unlimited = max_turns == 0;
+ let mut turn: usize = 0;
+ while unlimited || turn < max_turns {
let _ = events.send(StreamEvent::Step {
index: (turn as u32) + 1,
max: max_turns as u32,
@@ -708,6 +715,7 @@ async fn run_turn_cycle(
"content": content
}));
}
+ turn += 1;
}
Err(format!(
@@ -871,6 +879,26 @@ mod tests {
);
}
+ #[tokio::test]
+ async fn turn_cycle_unbounded_runs_until_final_answer() {
+ let (tx, _rx) = mpsc::unbounded_channel();
+ let model = MockModel::new(vec![
+ tool_call_completion(),
+ json!({ "choices": [{ "message": { "content": "done" }, "finish_reason": "stop" }] }),
+ ]);
+ let tool = MockTool::default();
+ let mut budget = SessionBudget::new(None);
+ let convo = vec![json!({ "role": "user", "content": "hi" })];
+
+ // max_turns = 0 means unbounded: it must not error out and must keep
+ // going past the tool-call turn to return the final answer.
+ let result = run_turn_cycle(&tx, &json!({}), "m", &[], convo, 0, &mut budget, &model, &tool)
+ .await
+ .unwrap();
+
+ assert_eq!(result["choices"][0]["message"]["content"], "done");
+ }
+
#[test]
fn stop_reason_reads_first_choice() {
let completion = json!({ "choices": [{ "finish_reason": "tool_calls" }] });
diff --git a/src-tauri/src/core/cli/mod.rs b/src-tauri/src/core/cli/mod.rs
index efbf9d719..4cfa7987f 100644
--- a/src-tauri/src/core/cli/mod.rs
+++ b/src-tauri/src/core/cli/mod.rs
@@ -664,8 +664,10 @@ use std::io::Write as _;
use tokio::sync::{mpsc, Mutex};
/// Default turn cap when neither `--max-turns` nor `agent.toml [agent].max_turns`
-/// is set. The loop separately clamps the effective value to 1..=400.
-const DEFAULT_MAX_TURNS: u32 = 400;
+/// is set. `0` means unbounded: the session token budget and user cancellation
+/// guard the loop instead of a fixed step count, so runs aren't cut off
+/// mid-task. Set an explicit cap to bound it.
+const DEFAULT_MAX_TURNS: u32 = 0;
/// Resolved-config + provider snapshot for `jan agent status`.
pub fn cli_agent_status(
diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rs
index 2b8cbd0ce..502b1772a 100644
--- a/src-tauri/src/core/cli/tui.rs
+++ b/src-tauri/src/core/cli/tui.rs
@@ -1642,10 +1642,10 @@ fn header(app: &App) -> Paragraph<'static> {
Status::Idle => ("ready", Style::new().green()),
Status::Running => ("working", Style::new().yellow().bold()),
};
- let turn = if app.turn.1 > 0 {
- format!("turn {}/{} ", app.turn.0, app.turn.1)
- } else {
- String::new()
+ let turn = match app.turn {
+ (0, _) => String::new(),
+ (n, 0) => format!("turn {n} "),
+ (n, m) => format!("turn {n}/{m} "),
};
Paragraph::new(Line::from(vec![
Span::styled(" jan agent ", Style::new().on_blue().white().bold()),