diff --git a/src/agent/context.rs b/src/agent/context.rs index d0e683a..ce5ff02 100644 --- a/src/agent/context.rs +++ b/src/agent/context.rs @@ -490,7 +490,10 @@ impl ResponseParser { let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); let handle = tokio::spawn(async move { let mut parser = self; - let debug = std::env::var("POC_DEBUG").is_ok(); + let agent_name = agent.state.lock().await.provenance.clone(); + let log_path = format!("/tmp/poc-{}.log", agent_name); + let mut log_file = std::fs::OpenOptions::new() + .create(true).append(true).open(&log_path).ok(); let mut full_text = String::new(); while let Some(event) = stream.recv().await { match event { @@ -498,10 +501,12 @@ impl ResponseParser { full_text.push_str(&text); let mut ctx = agent.context.lock().await; let calls = parser.feed_token(&text, id, &mut ctx); - if !calls.is_empty() && debug { - for c in &calls { - eprintln!("[parser] tool_call: {} args={}", - c.name, &c.arguments[..c.arguments.len().min(80)]); + if !calls.is_empty() { + if let Some(ref mut f) = log_file { + use std::io::Write; + for c in &calls { + let _ = writeln!(f, "tool_call: {} args={}", c.name, &c.arguments[..c.arguments.len().min(200)]); + } } } for call in calls { @@ -509,17 +514,18 @@ impl ResponseParser { } } super::api::StreamToken::Done { usage } => { - if debug { + if let Some(ref mut f) = log_file { + use std::io::Write; let tc_count = full_text.matches("").count(); - eprintln!("[parser] done: {} chars, {} tags", + let _ = writeln!(f, "done: {} chars, {} tags", full_text.len(), tc_count); - if tc_count > 0 { - // Log the raw text around tool calls for debugging - for (i, part) in full_text.split("").enumerate() { - if i > 0 { - eprintln!("[parser] tool_call text: {}...", - &part[..part.len().min(200)]); - } + if tc_count == 0 && full_text.len() > 0 { + let _ = writeln!(f, "full text:\n{}", &full_text[..full_text.len().min(2000)]); + } + for (i, part) in full_text.split("").enumerate() { + if i > 0 { + let _ = writeln!(f, "tool_call body: {}...", + &part[..part.len().min(200)]); } } }