From 34eb74342d652db99f5f7f775bf98fa53d19c71f Mon Sep 17 00:00:00 2001 From: Kevin Turcios Date: Tue, 21 Apr 2026 17:52:58 -0500 Subject: [PATCH] Improve code repair prompts and test detail formatting System prompt now focuses on repair strategy (identify pattern, compare code, minimal fix) instead of spending most tokens on SEARCH/REPLACE format spec. User prompt explicitly frames the task and asks for root cause analysis. build_test_details() reformatted for clarity: grouped by test source with clear Expected/Got lines separated by --- dividers. --- .../code_repair/CODE_REPAIR_SYSTEM_PROMPT.md | 70 ++++++------------- .../code_repair/CODE_REPAIR_USER_PROMPT.md | 12 ++-- .../python/code_repair/code_repair_context.py | 55 +++++++++------ 3 files changed, 61 insertions(+), 76 deletions(-) diff --git a/django/aiservice/core/languages/python/code_repair/CODE_REPAIR_SYSTEM_PROMPT.md b/django/aiservice/core/languages/python/code_repair/CODE_REPAIR_SYSTEM_PROMPT.md index 6087301e9..e3147fe0b 100644 --- a/django/aiservice/core/languages/python/code_repair/CODE_REPAIR_SYSTEM_PROMPT.md +++ b/django/aiservice/core/languages/python/code_repair/CODE_REPAIR_SYSTEM_PROMPT.md @@ -1,62 +1,34 @@ -You are a senior software engineer who is great at reviewing and repairing code for performance and behavior. -You have expertise in multiple programming languages including Python, JavaScript, and TypeScript. -The goal of repairing code is to ensure that the optimized code is performant and has the same behavior as the original code based on the provided test results. -You are provided the following information: +You are a code repair specialist. An optimization was generated for a function but it changed the function's behavior. Your job is to fix the optimized code so it produces identical results to the original while preserving as much of the optimization as possible. -- original_source_code -- optimized_source_code - This is the optimized implementation of the original code. -- test_details - This has the details of the behavioral differences between the original and optimized code. +You will receive: +- **original_source_code**: The correct, working implementation. +- **optimized_source_code**: The faster but behaviorally incorrect implementation. +- **test_details**: Specific test failures showing how the optimized code differs from the original. + +### Repair strategy + +1. Read the test failures carefully. Identify the *pattern* — is it an edge case (empty input, None, zero), an off-by-one error, a type mismatch, a missing branch, or a logic error? +2. Compare the original and optimized code to find where the optimization diverged from correct behavior. +3. Make the *smallest possible fix* to the optimized code. Do not rewrite the function — patch the specific lines that cause the behavioral difference. +4. If the optimization approach is fundamentally incompatible with correct behavior, fall back to the original logic for the affected code path while keeping optimizations for other paths. ### Output format -Request to replace sections of the optimized code in an existing file using SEARCH/REPLACE blocks that define exact changes to specific parts of the file. -Define your output in XML-style tags like here: +Output SEARCH/REPLACE blocks to patch the optimized code: -src/main.py +file.py <<<<<<< SEARCH -a = 2 +[exact content to find in the optimized code] ======= -a = 3 +[fixed content] >>>>>>> REPLACE -Always adhere to this format for tool use to ensure proper parsing and execution. - -## replace_in_file -Description: Request to replace sections of content in an existing file using SEARCH/REPLACE blocks that define exact changes to specific parts of the file. This tool should be used when you need to make targeted changes to specific parts of a file. -Parameters: -- path: (required) The path of the file to modify -- diff: (required) One or more SEARCH/REPLACE blocks following this exact format: - ``` - <<<<<<< SEARCH - [exact content to find] - ======= - [new content to replace with] - >>>>>>> REPLACE - ``` - Critical rules: - 1. SEARCH content must match the associated file section to find EXACTLY: - * Match character-for-character including whitespace, indentation, line endings - * Include all comments, docstrings, etc. - 2. SEARCH/REPLACE blocks will ONLY replace the first match occurrence. - * Including multiple unique SEARCH/REPLACE blocks if you need to make multiple changes. - * Include *just* enough lines in each SEARCH section to uniquely match each set of lines that need to change. - * When using multiple SEARCH/REPLACE blocks, list them in the order they appear in the file. - 3. Keep SEARCH/REPLACE blocks concise: - * Break large SEARCH/REPLACE blocks into a series of smaller blocks that each change a small portion of the file. - * Include just the changing lines, and a few surrounding lines if needed for uniqueness. - * Do not include long runs of unchanging lines in SEARCH/REPLACE blocks. - * Each line must be complete. Never truncate lines mid-way through as this can cause matching failures. - 4. Special operations: - * To move code: Use two SEARCH/REPLACE blocks (one to delete from original + one to insert at new location) - * To delete code: Use empty REPLACE section -Usage: - -File path here - -Search and replace blocks here - - +Rules: +- SEARCH content must match the optimized code exactly (whitespace, indentation, comments). +- Keep blocks small — only include lines that change plus enough context for uniqueness. +- Use multiple blocks for multiple fixes, listed in file order. +- To delete code, use an empty REPLACE section. diff --git a/django/aiservice/core/languages/python/code_repair/CODE_REPAIR_USER_PROMPT.md b/django/aiservice/core/languages/python/code_repair/CODE_REPAIR_USER_PROMPT.md index 6ce6cfd68..da14b8bef 100644 --- a/django/aiservice/core/languages/python/code_repair/CODE_REPAIR_USER_PROMPT.md +++ b/django/aiservice/core/languages/python/code_repair/CODE_REPAIR_USER_PROMPT.md @@ -1,13 +1,17 @@ -Please fix the optimized code to match the behaviour of the original code, while trying to keep the optimization logic intact. +The optimized code below fails tests that the original code passes. Fix the optimized code to match the original's behavior while keeping as much of the optimization intact as possible. -### The original source code: +### Original source code (correct behavior): {original_source_code} -### The optimized source code +### Optimized source code (to be repaired): {modified_source_code} -### The test result details +### Test failures + +The following tests show where the optimized code diverges from the original. Each entry shows the test source, what the original produced vs what the optimized code produced. {test_details} + +Analyze the pattern across these failures, identify the root cause in the optimized code, and output the minimal SEARCH/REPLACE fix. diff --git a/django/aiservice/core/languages/python/code_repair/code_repair_context.py b/django/aiservice/core/languages/python/code_repair/code_repair_context.py index dda5729d7..bf5952d97 100644 --- a/django/aiservice/core/languages/python/code_repair/code_repair_context.py +++ b/django/aiservice/core/languages/python/code_repair/code_repair_context.py @@ -76,37 +76,46 @@ class CodeRepairContext: return self.base_system_prompt def build_test_details(self, test_diffs: list[TestDiff]) -> str: - sections: defaultdict[str, str] = defaultdict(str) + sections: defaultdict[str, list[str]] = defaultdict(list) language = self.data.language test_error_label = "Pytest error" if language == "python" else "Test error" + seen_headers: set[str] = set() + for diff in test_diffs: try: - if not diff.test_src_code: - # Convert None to empty string for dict key usage - diff.test_src_code = "" - if not sections[diff.test_src_code]: - # add error strings and test def only once per test function - sections[diff.test_src_code] += f"""Test Source: - ```{language} - {diff.test_src_code or "Not Available"} - ``` - {test_error_label} (original code): {diff.original_pytest_error or ""} - {test_error_label} (optimized code): {diff.candidate_pytest_error or ""} - """ - sections[diff.test_src_code] += "\n".join( - [ - f"{SCOPE_DESCRIPTIONS.get(diff.scope, diff.scope.value)}", - f"Expected: {diff.original_value!r}.\nGot: {diff.candidate_value!r}." - if diff.scope != TestDiffScope.DID_PASS - else "", - f"Original code test status: {'Passed' if diff.original_pass else 'Failed'}. Optimized code test status: {'Passed' if diff.candidate_pass else 'Failed'}", - "---", + key = diff.test_src_code or "" + if key not in seen_headers: + seen_headers.add(key) + header_lines = [ + f"**Test source:**", + f"```{language}", + f"{diff.test_src_code or 'Not available'}", + f"```", ] - ) + if diff.original_pytest_error: + header_lines.append(f"{test_error_label} (original): {diff.original_pytest_error}") + if diff.candidate_pytest_error: + header_lines.append(f"{test_error_label} (optimized): {diff.candidate_pytest_error}") + sections[key].append("\n".join(header_lines)) + + scope_desc = SCOPE_DESCRIPTIONS.get(diff.scope, diff.scope.value) + detail_lines = [f"- {scope_desc}"] + if diff.scope != TestDiffScope.DID_PASS: + detail_lines.append(f" Expected: {diff.original_value!r}") + detail_lines.append(f" Got: {diff.candidate_value!r}") + else: + orig_status = "Passed" if diff.original_pass else "Failed" + opt_status = "Passed" if diff.candidate_pass else "Failed" + detail_lines.append(f" Original: {orig_status} → Optimized: {opt_status}") + sections[key].append("\n".join(detail_lines)) except Exception as e: logging.exception("Some issue in parsing test diffs") sentry_sdk.capture_exception(e) - return "\n".join(sections.values()) + + parts = [] + for key, lines in sections.items(): + parts.append("\n".join(lines)) + return "\n\n---\n\n".join(parts) def get_user_prompt(self) -> str: return self.base_user_prompt.format(