use crate::logger::Logger;
use crate::workspace::Workspace;
use std::path::Path;
pub const VAGUE_STATUS_LINE: &str = "In progress.";
pub const VAGUE_NOTES_LINE: &str = "Notes.";
pub const VAGUE_ISSUES_LINE: &str = "No issues recorded.";
pub fn delete_issues_file_for_isolation_with_workspace(
workspace: &dyn Workspace,
logger: &Logger,
) -> std::io::Result<()> {
let issues_path = Path::new(".agent/ISSUES.md");
if workspace.exists(issues_path) {
workspace.remove(issues_path)?;
logger.info("Isolation mode: deleted .agent/ISSUES.md after final fix");
}
Ok(())
}
fn overwrite_one_liner_with_workspace(
workspace: &dyn Workspace,
path: &Path,
line: &str,
) -> std::io::Result<()> {
let first_line = line.lines().next().unwrap_or_default().trim();
let content = if first_line.is_empty() {
"\n".to_string()
} else {
format!("{first_line}\n")
};
workspace.write_atomic(path, &content)
}
pub fn clean_context_for_reviewer_with_workspace(
workspace: &dyn Workspace,
logger: &Logger,
isolation_mode: bool,
) -> std::io::Result<()> {
if isolation_mode {
logger.info("Isolation mode: skipping context cleanup (files don't exist)");
return Ok(());
}
logger.info("Cleaning context for reviewer (fresh eyes)...");
let archive_dir = Path::new(".agent/archive");
let _ = workspace.remove_dir_all_if_exists(archive_dir);
overwrite_one_liner_with_workspace(
workspace,
Path::new(".agent/STATUS.md"),
VAGUE_STATUS_LINE,
)?;
overwrite_one_liner_with_workspace(workspace, Path::new(".agent/NOTES.md"), VAGUE_NOTES_LINE)?;
overwrite_one_liner_with_workspace(
workspace,
Path::new(".agent/ISSUES.md"),
VAGUE_ISSUES_LINE,
)?;
logger.success("Context cleaned for reviewer");
Ok(())
}
pub fn update_status_with_workspace(
workspace: &dyn Workspace,
_status: &str,
isolation_mode: bool,
) -> std::io::Result<()> {
if isolation_mode {
return Ok(());
}
overwrite_one_liner_with_workspace(workspace, Path::new(".agent/STATUS.md"), VAGUE_STATUS_LINE)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::logger::Colors;
use crate::workspace::{MemoryWorkspace, Workspace};
#[test]
fn test_delete_issues_file_for_isolation_with_workspace() {
let workspace = MemoryWorkspace::new_test()
.with_dir(".agent")
.with_file(".agent/ISSUES.md", "some issues");
let colors = Colors { enabled: false };
let logger = Logger::new(colors);
delete_issues_file_for_isolation_with_workspace(&workspace, &logger).unwrap();
assert!(
!workspace.exists(Path::new(".agent/ISSUES.md")),
"ISSUES.md should be deleted via workspace"
);
}
#[test]
fn test_delete_issues_file_for_isolation_with_workspace_nonexistent() {
let workspace = MemoryWorkspace::new_test().with_dir(".agent");
let colors = Colors { enabled: false };
let logger = Logger::new(colors);
let result = delete_issues_file_for_isolation_with_workspace(&workspace, &logger);
assert!(result.is_ok(), "Should succeed when file doesn't exist");
}
#[test]
fn test_clean_context_for_reviewer_with_workspace_non_isolation() {
let workspace = MemoryWorkspace::new_test()
.with_dir(".agent")
.with_file(".agent/STATUS.md", "old status")
.with_file(".agent/NOTES.md", "old notes")
.with_file(".agent/ISSUES.md", "old issues")
.with_dir(".agent/archive")
.with_file(".agent/archive/old.txt", "archived");
let colors = Colors { enabled: false };
let logger = Logger::new(colors);
clean_context_for_reviewer_with_workspace(&workspace, &logger, false).unwrap();
assert_eq!(
workspace.read(Path::new(".agent/STATUS.md")).unwrap(),
"In progress.\n"
);
assert_eq!(
workspace.read(Path::new(".agent/NOTES.md")).unwrap(),
"Notes.\n"
);
assert_eq!(
workspace.read(Path::new(".agent/ISSUES.md")).unwrap(),
"No issues recorded.\n"
);
assert!(
!workspace.exists(Path::new(".agent/archive")),
"Archive should be removed"
);
}
#[test]
fn test_clean_context_for_reviewer_with_workspace_isolation_mode() {
let workspace = MemoryWorkspace::new_test().with_dir(".agent");
let colors = Colors { enabled: false };
let logger = Logger::new(colors);
clean_context_for_reviewer_with_workspace(&workspace, &logger, true).unwrap();
assert!(
!workspace.exists(Path::new(".agent/STATUS.md")),
"STATUS.md should not be created in isolation mode"
);
}
#[test]
fn test_update_status_with_workspace_non_isolation() {
let workspace = MemoryWorkspace::new_test().with_dir(".agent");
update_status_with_workspace(&workspace, "In progress.", false).unwrap();
let content = workspace.read(Path::new(".agent/STATUS.md")).unwrap();
assert_eq!(content, "In progress.\n");
}
#[test]
fn test_update_status_with_workspace_isolation_mode() {
let workspace = MemoryWorkspace::new_test().with_dir(".agent");
update_status_with_workspace(&workspace, "In progress.", true).unwrap();
assert!(
!workspace.exists(Path::new(".agent/STATUS.md")),
"STATUS.md should not be created in isolation mode"
);
}
}