From 6bb5448d3ffe5ad60d567a369459bdc50e79005b Mon Sep 17 00:00:00 2001 From: "Dhanji R. Prasanna" Date: Sat, 17 Jan 2026 15:20:01 +0530 Subject: [PATCH] feat(project_files): add read_include_prompt() and update combine_project_content() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add read_include_prompt() function to read prompt content from a file - Update combine_project_content() to accept include_prompt parameter - Change prompt order: cwd → agents → readme → language → include_prompt → memory - Add section markers around Project Memory for clearer boundaries - Add comprehensive tests for include prompt functionality and ordering --- crates/g3-cli/src/project_files.rs | 135 ++++++++++++++++++++++++++++- 1 file changed, 131 insertions(+), 4 deletions(-) diff --git a/crates/g3-cli/src/project_files.rs b/crates/g3-cli/src/project_files.rs index c0f0fd5..65975ee 100644 --- a/crates/g3-cli/src/project_files.rs +++ b/crates/g3-cli/src/project_files.rs @@ -76,27 +76,57 @@ pub fn read_project_memory(workspace_dir: &Path) -> Option { match std::fs::read_to_string(&memory_path) { Ok(content) => { let size = format_size(content.len()); - Some(format!("🧠 Project Memory ({}):{}\n{}", size, "\n", content)) + Some(format!( + "=== Project Memory (read from analysis/memory.md, {}) ===\n{}\n=== End Project Memory ===", + size, + content + )) } Err(_) => None, } } +/// Read include prompt content from a specified file path. +/// Returns formatted content with emoji prefix, or None if path is None or file doesn't exist. +pub fn read_include_prompt(path: Option<&std::path::Path>) -> Option { + let path = path?; + + if !path.exists() { + tracing::error!("Include prompt file not found: {}", path.display()); + return None; + } + + match std::fs::read_to_string(path) { + Ok(content) => Some(format!("📎 Included Prompt (from {}):\n{}", path.display(), content)), + Err(e) => { + tracing::error!("Failed to read include prompt file {}: {}", path.display(), e); + None + } + } +} + /// Combine AGENTS.md, README, and memory content into a single string. /// /// Returns None if all inputs are None, otherwise joins non-None parts with double newlines. /// Prepends the current working directory to help the LLM avoid path hallucinations. +/// +/// Order: Working Directory → AGENTS.md → README → Language prompts → Include prompt → Memory pub fn combine_project_content( agents_content: Option, readme_content: Option, memory_content: Option, language_content: Option, + include_prompt: Option, workspace_dir: &Path, ) -> Option { // Always include working directory to prevent LLM from hallucinating paths let cwd_info = format!("📂 Working Directory: {}", workspace_dir.display()); - let parts: Vec = [Some(cwd_info), agents_content, readme_content, memory_content, language_content] + // Order: cwd → agents → readme → language → include_prompt → memory + // Include prompt comes BEFORE memory so memory is always last (most recent context) + let parts: Vec = [ + Some(cwd_info), agents_content, readme_content, language_content, include_prompt, memory_content + ] .into_iter() .flatten() .collect(); @@ -224,6 +254,7 @@ mod tests { Some("readme".to_string()), Some("memory".to_string()), Some("language".to_string()), + None, // include_prompt &workspace, ); assert!(result.is_some()); @@ -238,7 +269,7 @@ mod tests { #[test] fn test_combine_project_content_partial() { let workspace = std::path::PathBuf::from("/test/workspace"); - let result = combine_project_content(None, Some("readme".to_string()), None, None, &workspace); + let result = combine_project_content(None, Some("readme".to_string()), None, None, None, &workspace); assert!(result.is_some()); let content = result.unwrap(); assert!(content.contains("📂 Working Directory: /test/workspace")); @@ -248,9 +279,105 @@ mod tests { #[test] fn test_combine_project_content_all_none() { let workspace = std::path::PathBuf::from("/test/workspace"); - let result = combine_project_content(None, None, None, None, &workspace); + let result = combine_project_content(None, None, None, None, None, &workspace); // Now always returns Some because we always include the working directory assert!(result.is_some()); assert!(result.unwrap().contains("📂 Working Directory: /test/workspace")); } + + #[test] + fn test_combine_project_content_with_include_prompt() { + let workspace = std::path::PathBuf::from("/test/workspace"); + let result = combine_project_content( + Some("agents".to_string()), + Some("readme".to_string()), + Some("memory".to_string()), + Some("language".to_string()), + Some("include_prompt".to_string()), + &workspace, + ); + assert!(result.is_some()); + let content = result.unwrap(); + assert!(content.contains("include_prompt")); + } + + #[test] + fn test_combine_project_content_order_include_before_memory() { + // Verify that include_prompt appears BEFORE memory in the combined content + let workspace = std::path::PathBuf::from("/test/workspace"); + let result = combine_project_content( + Some("AGENTS_CONTENT".to_string()), + Some("README_CONTENT".to_string()), + Some("MEMORY_CONTENT".to_string()), + Some("LANGUAGE_CONTENT".to_string()), + Some("INCLUDE_PROMPT_CONTENT".to_string()), + &workspace, + ); + let content = result.unwrap(); + + // Find positions of each section + let agents_pos = content.find("AGENTS_CONTENT").expect("agents not found"); + let readme_pos = content.find("README_CONTENT").expect("readme not found"); + let language_pos = content.find("LANGUAGE_CONTENT").expect("language not found"); + let include_pos = content.find("INCLUDE_PROMPT_CONTENT").expect("include_prompt not found"); + let memory_pos = content.find("MEMORY_CONTENT").expect("memory not found"); + + // Verify order: agents < readme < language < include_prompt < memory + assert!(agents_pos < readme_pos, "agents should come before readme"); + assert!(readme_pos < language_pos, "readme should come before language"); + assert!(language_pos < include_pos, "language should come before include_prompt"); + assert!(include_pos < memory_pos, "include_prompt should come before memory"); + } + + #[test] + fn test_combine_project_content_order_memory_last() { + // Verify memory is always last even when include_prompt is None + let workspace = std::path::PathBuf::from("/test/workspace"); + let result = combine_project_content( + Some("AGENTS".to_string()), + Some("README".to_string()), + Some("MEMORY".to_string()), + Some("LANGUAGE".to_string()), + None, // no include_prompt + &workspace, + ); + let content = result.unwrap(); + + // Memory should still be last + let language_pos = content.find("LANGUAGE").expect("language not found"); + let memory_pos = content.find("MEMORY").expect("memory not found"); + assert!(language_pos < memory_pos, "memory should come after language"); + } + + #[test] + fn test_read_include_prompt_none_path() { + // None path should return None + let result = read_include_prompt(None); + assert!(result.is_none()); + } + + #[test] + fn test_read_include_prompt_nonexistent_file() { + // Non-existent file should return None + let path = std::path::Path::new("/nonexistent/path/to/file.md"); + let result = read_include_prompt(Some(path)); + assert!(result.is_none()); + } + + #[test] + fn test_read_include_prompt_valid_file() { + // Create a temp file and read it + let temp_dir = std::env::temp_dir(); + let temp_file = temp_dir.join("test_include_prompt.md"); + std::fs::write(&temp_file, "Test prompt content").unwrap(); + + let result = read_include_prompt(Some(&temp_file)); + assert!(result.is_some()); + let content = result.unwrap(); + assert!(content.contains("📎 Included Prompt")); + assert!(content.contains("Test prompt content")); + + // Cleanup + let _ = std::fs::remove_file(&temp_file); + } }