Add logging for discovery

This commit is contained in:
Jochen
2025-11-26 10:41:35 +11:00
parent c419833ddf
commit 1e1702001c
5 changed files with 124 additions and 3 deletions

1
Cargo.lock generated
View File

@@ -1506,6 +1506,7 @@ name = "g3-planner"
version = "0.1.0"
dependencies = [
"anyhow",
"chrono",
"const_format",
"g3-providers",
"serde",

View File

@@ -3805,8 +3805,6 @@ impl<W: UiWriter> Agent<W> {
async fn execute_tool_inner_in_dir(&mut self, tool_call: &ToolCall, working_dir: Option<&str>) -> Result<String> {
debug!("=== EXECUTING TOOL ===");
debug!("Tool name: {}", tool_call.tool);
eprintln!("[DEBUG execute_tool_inner_in_dir] tool='{}' working_dir={:?} args={}",
tool_call.tool, working_dir, serde_json::to_string(&tool_call.args).unwrap_or_default());
debug!("Working directory passed to execute_tool_inner_in_dir: {:?}", working_dir);
debug!("Tool args (raw): {:?}", tool_call.args);
debug!(

View File

@@ -10,4 +10,5 @@ serde = { workspace = true }
serde_json = { workspace = true }
const_format = "0.2"
anyhow = { workspace = true }
tokio = { workspace = true }
tokio = { workspace = true }
chrono = { version = "0.4", features = ["serde"] }

View File

@@ -10,6 +10,9 @@ pub use code_explore::explore_codebase;
use anyhow::Result;
use g3_providers::{CompletionRequest, LLMProvider, Message, MessageRole};
use chrono::Local;
use std::fs::{self, OpenOptions};
use std::io::Write;
use prompts::{DISCOVERY_REQUIREMENTS_PROMPT, DISCOVERY_SYSTEM_PROMPT};
/// Type alias for a status callback function
@@ -51,6 +54,9 @@ pub async fn get_initial_discovery_messages(
// Step 1: Run explore_codebase to get the codebase report
let codebase_report = explore_codebase(codebase_path);
// Write the codebase report to logs directory
write_code_report(&codebase_report)?;
// Step 2: Build the prompt with the codebase report appended
let user_prompt = if let Some(requirements) = requirements_text {
format!(
@@ -90,6 +96,9 @@ pub async fn get_initial_discovery_messages(
status(&format!("📋 Extracted {} discovery commands", shell_commands.len()));
// Write the discovery commands to logs directory
write_discovery_commands(&shell_commands)?;
// Step 6: Format as tool messages
let tool_messages = shell_commands
.into_iter()
@@ -169,6 +178,58 @@ pub fn extract_summary(response: &str) -> Option<String> {
}
}
/// Write the codebase report to logs directory
fn write_code_report(report: &str) -> Result<()> {
// Ensure logs directory exists
fs::create_dir_all("logs")?;
// Generate timestamp in same format as tool_calls log
let timestamp = Local::now().format("%Y%m%d_%H%M%S").to_string();
let filename = format!("logs/code_report_{}.log", timestamp);
// Write the report to file
let mut file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&filename)?;
file.write_all(report.as_bytes())?;
file.flush()?;
Ok(())
}
/// Write the discovery commands to logs directory
fn write_discovery_commands(commands: &[String]) -> Result<()> {
// Ensure logs directory exists
fs::create_dir_all("logs")?;
// Generate timestamp in same format as tool_calls log
let timestamp = Local::now().format("%Y%m%d_%H%M%S").to_string();
let filename = format!("logs/discovery_commands_{}.log", timestamp);
// Write the commands to file
let mut file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(&filename)?;
// Write header
file.write_all(b"# Discovery Commands\n")?;
file.write_all(b"# Generated by g3-planner\n\n")?;
// Write each command on a separate line
for cmd in commands {
file.write_all(cmd.as_bytes())?;
file.write_all(b"\n")?;
}
file.flush()?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;

View File

@@ -0,0 +1,60 @@
//! Integration tests for logging functionality
use std::fs;
use std::path::Path;
#[test]
fn test_log_files_created() {
// This test verifies that the logging functions work correctly
// by checking that files can be created in the logs directory
// Clean up any existing test logs
let _ = fs::remove_dir_all("logs");
// Create logs directory
fs::create_dir_all("logs").expect("Failed to create logs directory");
// Verify directory exists
assert!(Path::new("logs").exists());
assert!(Path::new("logs").is_dir());
// Test writing a code report
let test_report = "Test codebase report\nLine 2\nLine 3";
let timestamp = chrono::Local::now().format("%Y%m%d_%H%M%S").to_string();
let report_filename = format!("logs/code_report_{}.log", timestamp);
fs::write(&report_filename, test_report).expect("Failed to write code report");
assert!(Path::new(&report_filename).exists());
let content = fs::read_to_string(&report_filename).expect("Failed to read code report");
assert_eq!(content, test_report);
// Test writing discovery commands
let commands_filename = format!("logs/discovery_commands_{}.log", timestamp);
let test_commands = "# Discovery Commands\n# Generated by g3-planner\n\nls -la\ncat README.md\n";
fs::write(&commands_filename, test_commands).expect("Failed to write discovery commands");
assert!(Path::new(&commands_filename).exists());
let content = fs::read_to_string(&commands_filename).expect("Failed to read discovery commands");
assert_eq!(content, test_commands);
// Clean up
let _ = fs::remove_file(&report_filename);
let _ = fs::remove_file(&commands_filename);
}
#[test]
fn test_filename_format() {
// Verify the filename format matches the tool_calls log format
let timestamp = chrono::Local::now().format("%Y%m%d_%H%M%S").to_string();
// Check format: YYYYMMDD_HHMMSS
assert_eq!(timestamp.len(), 15); // 8 digits + underscore + 6 digits
assert!(timestamp.contains('_'));
let parts: Vec<&str> = timestamp.split('_').collect();
assert_eq!(parts.len(), 2);
assert_eq!(parts[0].len(), 8); // YYYYMMDD
assert_eq!(parts[1].len(), 6); // HHMMSS
}