Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(agent/bash): always emit explicit exit marker and collapse \r progress lines
Akarshan Biswasa9e64cd13831
diff --git a/src-tauri/src/core/agent/context.rs b/src-tauri/src/core/agent/context.rsindex 26e8e7d59..87e30e97e 100644--- a/src-tauri/src/core/agent/context.rs+++ b/src-tauri/src/core/agent/context.rs@@ -14,7 +14,11 @@ running commands, editing code, and writing new files.";/// Always-on behavioral guidelines. Kept short and model-facing.const GUIDELINES: &str =- "# Guidelines\n\n- Be concise in your responses.\n- Show file paths clearly when working with files.";+ "# Guidelines\n\n- Be concise in your responses.\n- Show file paths clearly when working with files.\n\+- Tool output is complete and verbatim. Trust it. Do not re-run a command to check for hidden or \+missing output: when output is cut it always carries an explicit `[output truncated ...]` notice, so \+its absence means you have everything. A command's `[exit N]` line is the authoritative result -- \+`[exit 0]` is success even if there is text on stderr (many tools write normal status there).";/// Context-file names discovered by walking from the project root up to the/// filesystem root, most general (top ancestor) first so the nearest file wins.@@ -281,6 +285,8 @@ mod tests {assert!(out.starts_with("You are Jan, an AI coding agent."));assert!(out.contains("# Guidelines"));assert!(out.contains("Be concise"));+ assert!(out.contains("Tool output is complete and verbatim"));+ assert!(out.contains("Do not re-run a command to check"));let _ = std::fs::remove_dir_all(&root);}diff --git a/src-tauri/src/core/agent/tools/handlers.rs b/src-tauri/src/core/agent/tools/handlers.rsindex 72c40dbee..724d76fae 100644--- a/src-tauri/src/core/agent/tools/handlers.rs+++ b/src-tauri/src/core/agent/tools/handlers.rs@@ -90,6 +90,27 @@ fn cap_output(s: &str, max_lines: usize, max_bytes: usize, note: &str) -> Stringout}+/// Collapse carriage-return redraws (`git`/`curl`-style progress lines) to what+/// a terminal would actually show: text after the last `\r` on each line wins.+/// Without this, thousands of `\r`-separated progress frames read as one giant+/// line and blow past the byte cap, so tiny visible output looks truncated.+fn collapse_carriage_returns(s: &str) -> String {+ if !s.contains('\r') {+ return s.to_string();+ }+ let mut out = String::with_capacity(s.len());+ for line in s.split_inclusive('\n') {+ let (body, nl) = match line.strip_suffix('\n') {+ Some(b) => (b, "\n"),+ None => (line, ""),+ };+ let body = body.strip_suffix('\r').unwrap_or(body);+ out.push_str(body.rsplit('\r').next().unwrap_or(body));+ out.push_str(nl);+ }+ out+}+/// Execute a built-in tool. Returns the tool-result text. Errors are returned/// as a String STARTING WITH "ERROR" rather than as Err.pub async fn execute_builtin(@@ -577,11 +598,19 @@ async fn await_bash_job(job_id: &str) -> String {fn format_bash_output(output: std::io::Result<std::process::Output>) -> String {match output {Ok(out) => {- let mut combined = String::new();- combined.push_str(&String::from_utf8_lossy(&out.stdout));- combined.push_str(&String::from_utf8_lossy(&out.stderr));+ let mut combined = collapse_carriage_returns(&format!(+ "{}{}",+ String::from_utf8_lossy(&out.stdout),+ String::from_utf8_lossy(&out.stderr),+ ));+ // Always emit an explicit exit marker on its own line. A bare exit+ // code (including 0) is the only reliable success signal: commands+ // like `git push` write their normal status to stderr on success,+ // so the presence of stderr text must not be read as failure.+ if !combined.is_empty() && !combined.ends_with('\n') {+ combined.push('\n');+ }match out.status.code() {- Some(0) => {}Some(code) => combined.push_str(&format!("[exit {code}]")),None => combined.push_str("[terminated by signal]"),}@@ -1272,6 +1301,35 @@ mod tests {let _ = std::fs::remove_dir_all(&root);}+ #[tokio::test]+ async fn bash_success_emits_exit_0_marker() {+ let root = unique_root();+ let out = execute_builtin(+ lookup("bash").unwrap(),+ &json!({"command": "echo done"}),+ &root,+ )+ .await;+ assert!(!out.starts_with("ERROR"), "unexpected: {out}");+ assert!(out.contains("done"), "unexpected: {out}");+ assert!(out.contains("[exit 0]"), "unexpected: {out}");+ let _ = std::fs::remove_dir_all(&root);+ }++ #[tokio::test]+ async fn bash_exit_marker_is_on_its_own_line() {+ let root = unique_root();+ // stderr-only output with no trailing newline (mirrors `git push`).+ let out = execute_builtin(+ lookup("bash").unwrap(),+ &json!({"command": "printf 'to remote' 1>&2"}),+ &root,+ )+ .await;+ assert!(out.contains("\n[exit 0]"), "marker not on its own line: {out}");+ let _ = std::fs::remove_dir_all(&root);+ }+#[tokio::test]async fn bash_output_past_old_64kb_cap_survives_intact() {let root = unique_root();@@ -1288,6 +1346,24 @@ mod tests {let _ = std::fs::remove_dir_all(&root);}+ #[tokio::test]+ async fn bash_cr_progress_is_collapsed_not_truncated() {+ let root = unique_root();+ // Mimics git progress: one logical line redrawn thousands of times with+ // \r (no \n). Raw bytes exceed the byte cap, but only the final redraw+ // is visible, so the model must see it intact with no truncation notice.+ let out = execute_builtin(+ lookup("bash").unwrap(),+ &json!({"command": "for i in $(seq 1 30000); do printf 'Receiving objects: %d\\r' \"$i\"; done 1>&2"}),+ &root,+ )+ .await;+ assert!(!out.contains("output truncated"), "spurious truncation: {out}");+ assert!(out.contains("Receiving objects: 30000"), "final redraw lost: {out}");+ assert!(out.contains("[exit 0]"), "unexpected: {out}");+ let _ = std::fs::remove_dir_all(&root);+ }+#[tokio::test]async fn bash_output_overflow_spills_to_readable_temp_file() {let root = unique_root();diff --git a/src-tauri/src/core/agent/tools/schema.rs b/src-tauri/src/core/agent/tools/schema.rsindex 3b83341d8..b8706defc 100644--- a/src-tauri/src/core/agent/tools/schema.rs+++ b/src-tauri/src/core/agent/tools/schema.rs@@ -119,7 +119,7 @@ pub fn builtin_tool_schemas() -> Vec<Value> {"type": "function","function": {"name": "bash",- "description": "Run a shell command in the project root. Returns combined stdout and stderr. Output is truncated to 2000 lines or 64KB; full output is saved to a temp file when truncated. If the command doesn't finish within `timeout` seconds (default 30), it keeps running in the background and this call returns a job_id instead of erroring or killing it; call bash again with only job_id set to wait for and collect its output once it finishes.",+ "description": "Run a shell command in the project root. Returns combined stdout and stderr, followed by a final `[exit N]` line (or `[terminated by signal]`). Judge success by that exit code, not by whether there is text on stderr: many commands (e.g. `git push`) write normal status to stderr on success, so `[exit 0]` means it worked. The output is COMPLETE and verbatim: trust it and do not re-run a command to double-check. It is truncated only when it exceeds 10000 lines or 256KB, and only then is an explicit `[output truncated ...]` notice appended with a temp-file path; absent that notice, you have the full output. If the command doesn't finish within `timeout` seconds (default 30), it keeps running in the background and this call returns a job_id instead of erroring or killing it; call bash again with only job_id set to wait for and collect its output once it finishes.","parameters": {"type": "object","properties": {