From eaf79fbee513f05f66555db5c26ce6bb8616fae3 Mon Sep 17 00:00:00 2001 From: AI Christianson Date: Sat, 8 Mar 2025 16:29:17 -0500 Subject: [PATCH] fix tests; improve prompts --- .../repositories/related_files_repository.py | 9 +++ ra_aid/prompts/reasoning_assist_prompt.py | 12 ++++ ra_aid/tools/file_str_replace.py | 8 +++ ra_aid/tools/memory.py | 24 ++++++-- ra_aid/tools/write_file.py | 4 ++ tests/ra_aid/tools/test_write_file.py | 60 +++++++++++++++++++ 6 files changed, 113 insertions(+), 4 deletions(-) diff --git a/ra_aid/database/repositories/related_files_repository.py b/ra_aid/database/repositories/related_files_repository.py index 94a0109..e0111e3 100644 --- a/ra_aid/database/repositories/related_files_repository.py +++ b/ra_aid/database/repositories/related_files_repository.py @@ -96,6 +96,15 @@ class RelatedFilesRepository: List[str]: Formatted strings for each related file """ return [f"ID#{file_id} {filepath}" for file_id, filepath in sorted(self._related_files.items())] + + def get_next_id(self) -> int: + """ + Get the next ID that would be assigned to a new file. + + Returns: + int: The next ID value + """ + return self._id_counter class RelatedFilesRepositoryManager: diff --git a/ra_aid/prompts/reasoning_assist_prompt.py b/ra_aid/prompts/reasoning_assist_prompt.py index cf57ecd..ee5cfd6 100644 --- a/ra_aid/prompts/reasoning_assist_prompt.py +++ b/ra_aid/prompts/reasoning_assist_prompt.py @@ -32,6 +32,12 @@ Working Directory: {working_directory} Given the available information, tools, and base task, write a couple paragraphs about how an agentic system might use the available tools to plan the base task, break it down into tasks, and request implementation of those tasks. The agent will not be writing any code at this point, so we should keep it to high level tasks and keep the focus on project planning. + +DO NOT EXPAND SCOPE BEYOND USERS ORIGINAL REQUEST. E.G. DO NOT SET UP VERSION CONTROL UNLESS THEY SPECIFIED TO. BUT IF WE ARE SETTING UP A NEW PROJECT WE PROBABLY DO WANT TO SET UP A MAKEFILE OR CMAKELISTS, ETC, APPROPRIATE TO THE LANGUAGE/FRAMEWORK BEING USED. + +REMEMBER, IT IS *IMPERATIVE* TO RECORD KEY INFO SUCH AS BUILD/TEST COMMANDS, ETC. AS KEY FACTS. +WE DO NOT WANT TO EMIT REDUNDANT KEY FACTS, SNIPPETS, ETC. +WE DO NOT WANT TO EXCESSIVELY EMIT TINY KEY SNIPPETS --THEY SHOULD BE "paragraphs" OF CODE TYPICALLY. """ REASONING_ASSIST_PROMPT_IMPLEMENTATION = """Current Date: {current_date} @@ -66,4 +72,10 @@ Working Directory: {working_directory} Given the available information, tools, and base task, write a couple paragraphs about how an agentic system might use the available tools to implement the given task definition. The agent will be writing code and making changes at this point. + +Answer quickly and confidently with just a few sentences at most. + +REMEMBER, IT IS *IMPERATIVE* TO RECORD KEY INFO SUCH AS BUILD/TEST COMMANDS, ETC. AS KEY FACTS. +WE DO NOT WANT TO EMIT REDUNDANT KEY FACTS, SNIPPETS, ETC. +WE DO NOT WANT TO EXCESSIVELY EMIT TINY KEY SNIPPETS --THEY SHOULD BE "paragraphs" OF CODE TYPICALLY. """ diff --git a/ra_aid/tools/file_str_replace.py b/ra_aid/tools/file_str_replace.py index 965c57f..c1eaee4 100644 --- a/ra_aid/tools/file_str_replace.py +++ b/ra_aid/tools/file_str_replace.py @@ -6,6 +6,7 @@ from rich.panel import Panel from ra_aid.console import console from ra_aid.console.formatting import print_error +from ra_aid.tools.memory import emit_related_files def truncate_display_str(s: str, max_length: int = 30) -> str: @@ -86,6 +87,13 @@ def file_str_replace(filepath: str, old_str: str, new_str: str, *, replace_all: success_msg = f"Successfully replaced '{old_str}' with '{new_str}' in {filepath}" if count > 1 and replace_all: success_msg = f"Successfully replaced {count} occurrences of '{old_str}' with '{new_str}' in {filepath}" + + # Add file to related files + try: + emit_related_files.invoke({"files": [filepath]}) + except Exception as e: + # Don't let related files error affect main function success + print_error(f"Note: Could not add to related files: {str(e)}") return { "success": True, diff --git a/ra_aid/tools/memory.py b/ra_aid/tools/memory.py index 24e52be..c768638 100644 --- a/ra_aid/tools/memory.py +++ b/ra_aid/tools/memory.py @@ -304,6 +304,14 @@ def emit_related_files(files: List[str]) -> str: files: List of file paths to add """ repo = get_related_files_repository() + + # Store the repository's ID counter value before adding any files + try: + initial_next_id = repo.get_next_id() + except (AttributeError, TypeError): + # Handle case where repo is mocked in tests + initial_next_id = 0 # Use a safe default for mocked environments + results = [] added_files = [] invalid_paths = [] @@ -339,14 +347,22 @@ def emit_related_files(files: List[str]) -> str: file_id = repo.add_file(file) if file_id is not None: - # Check if it's a new file by comparing with previous results - is_new_file = True + # Check if it's a truly new file (ID >= initial_next_id) + try: + is_truly_new = file_id >= initial_next_id + except TypeError: + # Handle case where file_id or initial_next_id is mocked in tests + is_truly_new = True # Default to True in test environments + + # Also check for duplicates within this function call + is_duplicate_in_call = False for r in results: if r.startswith(f"File ID #{file_id}:"): - is_new_file = False + is_duplicate_in_call = True break - if is_new_file: + # Only add to added_files if it's truly new AND not a duplicate in this call + if is_truly_new and not is_duplicate_in_call: added_files.append((file_id, file)) # Keep original path for display results.append(f"File ID #{file_id}: {file}") diff --git a/ra_aid/tools/write_file.py b/ra_aid/tools/write_file.py index c3dfca9..04fc32d 100644 --- a/ra_aid/tools/write_file.py +++ b/ra_aid/tools/write_file.py @@ -6,6 +6,7 @@ from typing import Dict from langchain_core.tools import tool from rich.console import Console from rich.panel import Panel +from ra_aid.tools.memory import emit_related_files console = Console() @@ -70,6 +71,9 @@ def put_complete_file_contents( border_style="bright_green", ) ) + + # Add file to related files + emit_related_files.invoke({"files": [filepath]}) except Exception as e: elapsed = time.time() - start_time diff --git a/tests/ra_aid/tools/test_write_file.py b/tests/ra_aid/tools/test_write_file.py index 591b598..7e457b3 100644 --- a/tests/ra_aid/tools/test_write_file.py +++ b/tests/ra_aid/tools/test_write_file.py @@ -6,6 +6,66 @@ import pytest from ra_aid.tools.write_file import put_complete_file_contents +@pytest.fixture(autouse=True) +def mock_related_files_repository(): + """Mock the RelatedFilesRepository to avoid database operations during tests""" + with patch('ra_aid.tools.memory.get_related_files_repository') as mock_repo: + # Setup the mock repository to behave like the original, but using memory + related_files = {} # Local in-memory storage + id_counter = 0 + + # Mock add_file method + def mock_add_file(filepath): + nonlocal id_counter + # Check if normalized path already exists in values + normalized_path = os.path.abspath(filepath) + for file_id, path in related_files.items(): + if path == normalized_path: + return file_id + + # First check if path exists + if not os.path.exists(filepath): + return None + + # Then check if it's a directory + if os.path.isdir(filepath): + return None + + # Validate it's a regular file + if not os.path.isfile(filepath): + return None + + # Check if it's a binary file (don't actually check in tests) + # We'll mock is_binary_file separately when needed + + # Add new file + file_id = id_counter + id_counter += 1 + related_files[file_id] = normalized_path + + return file_id + mock_repo.return_value.add_file.side_effect = mock_add_file + + # Mock get_all method + def mock_get_all(): + return related_files.copy() + mock_repo.return_value.get_all.side_effect = mock_get_all + + # Mock remove_file method + def mock_remove_file(file_id): + if file_id in related_files: + return related_files.pop(file_id) + return None + mock_repo.return_value.remove_file.side_effect = mock_remove_file + + # Mock format_related_files method + def mock_format_related_files(): + return [f"ID#{file_id} {filepath}" for file_id, filepath in sorted(related_files.items())] + mock_repo.return_value.format_related_files.side_effect = mock_format_related_files + + yield mock_repo + + @pytest.fixture def temp_test_dir(tmp_path): """Create a temporary test directory."""