feat(tier4): Add patch generation for auto-patching
- Add TIER4_PATCH_PROMPT to mma_prompts.py with unified diff format - Add run_tier4_patch_generation function to ai_client.py - Import mma_prompts in ai_client.py - Add unit tests for patch generation
This commit is contained in:
@@ -28,6 +28,7 @@ from pathlib import Path
|
||||
from src import project_manager
|
||||
from src import file_cache
|
||||
from src import mcp_client
|
||||
from src import mma_prompts
|
||||
import anthropic
|
||||
from src.gemini_cli_adapter import GeminiCliAdapter as GeminiCliAdapter
|
||||
from google import genai
|
||||
@@ -1960,6 +1961,33 @@ def run_tier4_analysis(stderr: str) -> str:
|
||||
except Exception as e:
|
||||
return f"[QA ANALYSIS FAILED] {e}"
|
||||
|
||||
|
||||
def run_tier4_patch_generation(error: str, file_context: str) -> str:
|
||||
if not error or not error.strip():
|
||||
return ""
|
||||
try:
|
||||
_ensure_gemini_client()
|
||||
if not _gemini_client:
|
||||
return ""
|
||||
prompt = (
|
||||
f"{mma_prompts.TIER4_PATCH_PROMPT}\n\n"
|
||||
f"Error:\n```\n{error}\n```\n\n"
|
||||
f"File Context:\n```\n{file_context}\n```\n"
|
||||
)
|
||||
model_name = "gemini-2.5-flash-lite"
|
||||
resp = _gemini_client.models.generate_content(
|
||||
model=model_name,
|
||||
contents=prompt,
|
||||
config=types.GenerateContentConfig(
|
||||
temperature=0.0,
|
||||
max_output_tokens=2048,
|
||||
)
|
||||
)
|
||||
patch = resp.text.strip() if resp.text else ""
|
||||
return patch
|
||||
except Exception as e:
|
||||
return f"[PATCH GENERATION FAILED] {e}"
|
||||
|
||||
def get_token_stats(md_content: str) -> dict[str, Any]:
|
||||
global _provider, _gemini_client, _model, _CHARS_PER_TOKEN
|
||||
total_tokens = 0
|
||||
|
||||
@@ -153,3 +153,28 @@ PROMPTS: Dict[str, str] = {
|
||||
"tier2_track_finalization": TIER2_TRACK_FINALIZATION,
|
||||
"tier2_contract_first": TIER2_CONTRACT_FIRST,
|
||||
}
|
||||
|
||||
TIER4_PATCH_PROMPT: str = """You are a Tier 4 QA Agent specializing in error analysis and patch generation.
|
||||
When a test or command fails, analyze the error and generate a unified diff patch to fix it.
|
||||
|
||||
OUTPUT FORMAT: Unified diff format (diff -u)
|
||||
```
|
||||
--- a/path/to/file.py
|
||||
+++ b/path/to/file.py
|
||||
@@ -start,count +start,count @@
|
||||
context line
|
||||
-removed line
|
||||
+added line
|
||||
context line
|
||||
```
|
||||
|
||||
RULES:
|
||||
1. Use "--- a/" and "+++ b/" prefixes with the actual file path
|
||||
2. Include sufficient context lines (3 above/below minimum)
|
||||
3. Use @@ -X,Y +X,Y @@ format for hunk headers
|
||||
4. Only include changes necessary to fix the error
|
||||
5. If multiple files need changes, include each in the patch
|
||||
6. Output ONLY the unified diff - no explanations, no markdown code blocks
|
||||
|
||||
Analyze this error and generate the patch:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user