normalize related file paths

This commit is contained in:
AI Christianson 2025-02-12 22:12:55 -05:00
parent f91237ee02
commit e6b34e4ebc
2 changed files with 37 additions and 7 deletions

View File

@ -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

View File

@ -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