From e6b34e4ebcd192f0b5c02c775283909afdd8473c Mon Sep 17 00:00:00 2001 From: AI Christianson Date: Wed, 12 Feb 2025 22:12:55 -0500 Subject: [PATCH] normalize related file paths --- ra_aid/tools/memory.py | 16 +++++++++------- tests/ra_aid/tools/test_memory.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/ra_aid/tools/memory.py b/ra_aid/tools/memory.py index 5ea25f1..49a2b22 100644 --- a/ra_aid/tools/memory.py +++ b/ra_aid/tools/memory.py @@ -28,14 +28,13 @@ console = Console() _global_memory: Dict[ str, Union[ - List[Any], Dict[int, str], Dict[int, SnippetInfo], int, Set[str], bool, str, - int, + List[str], List[WorkLogEntry], ], ] = { @@ -442,10 +441,13 @@ def emit_related_files(files: List[str]) -> str: results.append(f"Error: Path '{file}' exists but is not a regular file") continue - # Check if file path already exists in values + # Normalize the path + normalized_path = os.path.abspath(file) + + # Check if normalized path already exists in values existing_id = None for fid, fpath in _global_memory["related_files"].items(): - if fpath == file: + if fpath == normalized_path: existing_id = fid break @@ -457,9 +459,9 @@ def emit_related_files(files: List[str]) -> str: file_id = _global_memory["related_file_id_counter"] _global_memory["related_file_id_counter"] += 1 - # Store file with ID - _global_memory["related_files"][file_id] = file - added_files.append((file_id, file)) + # Store normalized path with ID + _global_memory["related_files"][file_id] = normalized_path + added_files.append((file_id, file)) # Keep original path for display results.append(f"File ID #{file_id}: {file}") # Rich output - single consolidated panel diff --git a/tests/ra_aid/tools/test_memory.py b/tests/ra_aid/tools/test_memory.py index 4689f0a..11caab5 100644 --- a/tests/ra_aid/tools/test_memory.py +++ b/tests/ra_aid/tools/test_memory.py @@ -464,6 +464,34 @@ def test_related_files_formatting(reset_memory, tmp_path): assert get_memory_value("related_files") == "" +def test_emit_related_files_path_normalization(reset_memory, tmp_path): + """Test that emit_related_files fails to detect duplicates with non-normalized paths""" + # Create a test file + test_file = tmp_path / "file.txt" + test_file.write_text("test content") + + # Change to the temp directory so relative paths work + import os + original_dir = os.getcwd() + os.chdir(tmp_path) + + try: + # Add file with absolute path + result1 = emit_related_files.invoke({"files": ["file.txt"]}) + assert "File ID #0:" in result1 + + # Add same file with relative path - should get same ID due to path normalization + result2 = emit_related_files.invoke({"files": ["./file.txt"]}) + assert "File ID #0:" in result2 # Should reuse ID since it's the same file + + # Verify only one normalized path entry exists + assert len(_global_memory["related_files"]) == 1 + assert os.path.abspath("file.txt") in _global_memory["related_files"].values() + finally: + # Restore original directory + os.chdir(original_dir) + + def test_key_snippets_integration(reset_memory, tmp_path): """Integration test for key snippets functionality""" # Create test files