add context window monitor

Writes the current context window to logs/current_context_window (uses a symlink to a session ID).

This PR was unfortunately generated by a different LLM and did a ton of superficial reformating, it's actually a fairly small and benign change, but I don't want to roll back everything. Hope that's ok.
This commit is contained in:
Jochen
2025-11-27 21:00:02 +11:00
parent 93dc4acf86
commit 52f78653b4
89 changed files with 4040 additions and 2576 deletions

View File

@@ -291,7 +291,10 @@ pub fn explore_kotlin(path: &str) -> String {
// Build files
report.push_str("--- Build Configuration ---\n");
let build = run_command("cat build.gradle.kts 2>/dev/null | head -50 || cat build.gradle 2>/dev/null | head -50", path);
let build = run_command(
"cat build.gradle.kts 2>/dev/null | head -50 || cat build.gradle 2>/dev/null | head -50",
path,
);
report.push_str(&build);
report.push('\n');

View File

@@ -9,11 +9,11 @@ pub mod prompts;
pub use code_explore::explore_codebase;
use anyhow::Result;
use g3_providers::{CompletionRequest, LLMProvider, Message, MessageRole};
use chrono::Local;
use g3_providers::{CompletionRequest, LLMProvider, Message, MessageRole};
use prompts::{DISCOVERY_REQUIREMENTS_PROMPT, DISCOVERY_SYSTEM_PROMPT};
use std::fs::{self, OpenOptions};
use std::io::Write;
use prompts::{DISCOVERY_REQUIREMENTS_PROMPT, DISCOVERY_SYSTEM_PROMPT};
/// Type alias for a status callback function
pub type StatusCallback = Box<dyn Fn(&str) + Send + Sync>;
@@ -94,7 +94,10 @@ pub async fn get_initial_discovery_messages(
// Step 5: Extract shell commands from the response
let shell_commands = extract_shell_commands(&response.content);
status(&format!("📋 Extracted {} discovery commands", shell_commands.len()));
status(&format!(
"📋 Extracted {} discovery commands",
shell_commands.len()
));
// Write the discovery commands to logs directory
write_discovery_commands(&shell_commands)?;

View File

@@ -7,38 +7,40 @@ use std::path::Path;
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";
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");
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);
@@ -48,11 +50,11 @@ fn test_log_files_created() {
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