From 65f877f311724819311122fde69da7a4c1d90e4c Mon Sep 17 00:00:00 2001 From: Priyanshu Kumar Date: Tue, 7 Apr 2026 17:20:40 +0530 Subject: [PATCH 1/7] feat: infer intent and include in test generation --- agent/main.py | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/agent/main.py b/agent/main.py index 33fb385..fe53391 100644 --- a/agent/main.py +++ b/agent/main.py @@ -6,6 +6,7 @@ from agent.github.committer import commit_tests import subprocess , os from agent.llm.groq_client import generate_review +from agent.llm.groq_client import infer_intent from agent.github.commenter import post_comment def get_pr_diff(): try: @@ -65,29 +66,49 @@ def main(): print("Retrieving relevant context...") relevant_chunks = query_embeddings(query_embedding) - # 7. Generate review + # 7. Infer intent + print("Inferring intent...") + intent = infer_intent(diff) + + print("Intent extracted:") + print(intent) + + # 8. Generate review print("Generating review...") review = generate_review(diff, context=relevant_chunks) - # 8. Generate tests + # 9. Generate tests print("Generating tests...") - tests = generate_tests(diff, context=relevant_chunks) + tests = generate_tests(diff, context=relevant_chunks, intent=intent) + print("Generated tests with intent:") + print(tests) os.makedirs("tests", exist_ok=True) with open("tests/test_generated.py", "w", encoding="utf-8") as f: f.write(tests) # Save generated tests to a file for potential commit print("Generated tests saved to tests/test_generated.py") - # 9. Combine output - final_output = f"{review}\n\n---\n\n### Suggested Tests\n{tests}" + # 10. Combine output + formatted_tests = f"""```bash + pytest tests/test_generated.py + {tests} + ```""" + + final_output = f"""{review} + + --- + + ### Suggested Tests + {formatted_tests} + """ print("\n FINAL OUTPUT:\n") print(final_output) - # 10. Post comment + # 11. Post comment print("Posting comment...") post_comment(final_output) - # 11. Commit tests + # 12. Commit tests print("Committing tests...") commit_tests() From 78c6c6ea10528e8243956f004ea1add96f3406d1 Mon Sep 17 00:00:00 2001 From: Priyanshu Kumar Date: Tue, 7 Apr 2026 17:21:36 +0530 Subject: [PATCH 2/7] fix: handle commit failure with error message --- agent/main.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/agent/main.py b/agent/main.py index fe53391..0eac420 100644 --- a/agent/main.py +++ b/agent/main.py @@ -110,7 +110,10 @@ def main(): # 12. Commit tests print("Committing tests...") - commit_tests() + try: + commit_tests() + except Exception as e: + print(f"Commit failed: {e}") print("Done") From 8968fe2acc4c4d73dda3ba8e1266f70c71afcb42 Mon Sep 17 00:00:00 2001 From: Priyanshu Kumar Date: Tue, 7 Apr 2026 17:30:24 +0530 Subject: [PATCH 3/7] fix: handle intent extraction failure with fallback --- agent/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/agent/main.py b/agent/main.py index 0eac420..470bf93 100644 --- a/agent/main.py +++ b/agent/main.py @@ -68,7 +68,9 @@ def main(): # 7. Infer intent print("Inferring intent...") - intent = infer_intent(diff) + if not intent or "error" in intent: + print("Intent extraction failed, using fallback") + intent = {"purpose": "", "properties": [], "edge_cases": []} print("Intent extracted:") print(intent) From edab2cf16c7be335c5fc052d6fd5bd5d3b49234f Mon Sep 17 00:00:00 2001 From: Priyanshu Kumar Date: Tue, 7 Apr 2026 17:30:56 +0530 Subject: [PATCH 4/7] feat: add logging for generated review in main function --- agent/main.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/agent/main.py b/agent/main.py index 470bf93..ebbc868 100644 --- a/agent/main.py +++ b/agent/main.py @@ -78,6 +78,8 @@ def main(): # 8. Generate review print("Generating review...") review = generate_review(diff, context=relevant_chunks) + print("Review generated:") + print(review) # 9. Generate tests print("Generating tests...") From 1a8563c1d3e95d3c76115b4066ac68de8d442221 Mon Sep 17 00:00:00 2001 From: Priyanshu Kumar Date: Wed, 8 Apr 2026 21:11:09 +0530 Subject: [PATCH 5/7] fix: handle intent extraction failure with fallback --- agent/main.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/agent/main.py b/agent/main.py index ebbc868..bde1d3b 100644 --- a/agent/main.py +++ b/agent/main.py @@ -68,6 +68,8 @@ def main(): # 7. Infer intent print("Inferring intent...") + intent = infer_intent(diff) + if not intent or "error" in intent: print("Intent extraction failed, using fallback") intent = {"purpose": "", "properties": [], "edge_cases": []} From ecacb681b99b0cc3cba01d5e2eb7422df4ff70c3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 9 Apr 2026 05:20:41 +0000 Subject: [PATCH 6/7] fix: add infer_intent with .replace(), add INTENT_PROMPT, update test_generator with intent param Agent-Logs-Url: https://github.com/Neuropole/apricot/sessions/f9f90f4a-4747-41aa-98ca-761f97030019 Co-authored-by: haddybhaiya <88364904+haddybhaiya@users.noreply.github.com> --- agent/llm/groq_client.py | 32 +++++++++++++++++++++++++++++++- agent/llm/prompts.py | 23 +++++++++++++++++++++++ agent/llm/test_generator.py | 21 ++++++++++++++++++++- 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/agent/llm/groq_client.py b/agent/llm/groq_client.py index 9868b8f..444037a 100644 --- a/agent/llm/groq_client.py +++ b/agent/llm/groq_client.py @@ -62,4 +62,34 @@ def generate_review(diff: str,context : list = None) -> str: ] result = generate_review(test_diff, context=test_context) #added conetxt parameter (kindly see @pleasingsunlight) - print(result) \ No newline at end of file + print(result) + +def infer_intent(diff: str) -> dict: + from agent.llm.prompts import INTENT_PROMPT + import json + + prompt = INTENT_PROMPT.replace("{diff}", diff[:8000]) + try: + response = client.chat.completions.create( + model=model, + messages=[ + { + "role": "user", + "content": prompt + } + ] + ) + content = response.choices[0].message.content.strip() + + if content.startswith("```"): + content = content.split("```")[1] + if content.startswith("json"): + content = content[len("json"):] + return json.loads(content) + except Exception as e: + return { + "purpose": "", + "properties": [], + "edge_cases": [], + "error": str(e) + } \ No newline at end of file diff --git a/agent/llm/prompts.py b/agent/llm/prompts.py index 1365eff..c5b08ce 100644 --- a/agent/llm/prompts.py +++ b/agent/llm/prompts.py @@ -17,4 +17,27 @@ - Do NOT repeat the diff - Use context if helpful - If everything looks good, say: "Code looks good ✅" +""" +INTENT_PROMPT = """ +Your are a senior software engineer. + +Analyze the given code diff and extract: + +1. Purpose of the code +2. Key properties (invariants) +3. Edge cases + +STRICT RULES: +- Output only in JSON +- No explanations text +- Keep it concise + +FORMAT: +{"purpose": "...", + "properties": ["...", "..."], + "edge_cases": ["...", "..."] +} + +---DIFF--- +{diff} """ \ No newline at end of file diff --git a/agent/llm/test_generator.py b/agent/llm/test_generator.py index 733bba7..0e277b2 100644 --- a/agent/llm/test_generator.py +++ b/agent/llm/test_generator.py @@ -1,8 +1,24 @@ from agent.llm.groq_client import client, model -def generate_tests(diff:str,context:list =None)->str: +def generate_tests(diff:str,context:list =None, intent:dict = None)->str: context_text = "" if context: context_text = "\n\n".join(context[:5]) # Include only the first 5 chunks for context + + intent_text = "" + if intent: + properties = ", ".join(intent.get("properties", [])) + edge_cases = ", ".join(intent.get("edge_cases", [])) + intent_text = f""" +Purpose: +{intent.get("purpose", "")} + +Properties: +{properties} + +Edge Cases: +{edge_cases} +""" + prompt = f""" You are a senior software engineer. Generate concise pytest test cases for the given code diff. @@ -15,6 +31,9 @@ def generate_tests(diff:str,context:list =None)->str: - NO explanations, NO extra text + ---INTENT--- + {intent_text} + ---CONTEXT--- {context_text} From 79bc30d32ec1d6c1241d3f06a8777e7b2faf7457 Mon Sep 17 00:00:00 2001 From: github-actions Date: Thu, 9 Apr 2026 11:18:56 +0000 Subject: [PATCH 7/7] Add AI-generated tests --- tests/test_generated.py | 47 +++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/tests/test_generated.py b/tests/test_generated.py index 48db62f..a7c68e4 100644 --- a/tests/test_generated.py +++ b/tests/test_generated.py @@ -1,34 +1,21 @@ -import pytest -from your_module import store_embeddings, query_embeddings, client +def test_empty_diff(): + assert generate_tests(diff="") == "" -def test_store_embeddings(): - chunks = ["chunk1", "chunk2"] - embeddings = [[1, 2], [3, 4]] - store_embeddings(chunks, embeddings) +def test_empty_context(): + assert generate_tests(diff="diff --git a/app.py b/app.py", context=[]) == "" -def test_query_embeddings(): - query_embedding = [1, 2] - results = query_embeddings(query_embedding) - assert isinstance(results, list) +def test_intent_extraction_failure(): + assert generate_tests(diff="diff --git a/app.py b/app.py", intent={"error": "intent extraction failed"}) == "" -def test_query_embeddings_empty(): - collection = client.get_or_create_collection(name="empty") - query_embedding = [1, 2] - results = collection.query(query_embeddings=[query_embedding], n_results=5) - assert results.get("documents", []) == [[]] +def test_commit_tests_failure(): + try: + commit_tests() + assert False + except Exception as e: + assert str(e) != "" -def test_query_embeddings_min_length(): - query_embedding = [1, 2] - chunks = ["a" * 29, "b" * 31] - embeddings = [[1, 2], [3, 4]] - store_embeddings(chunks, embeddings) - results = query_embeddings(query_embedding) - assert len(results) == 1 - -def test_query_embeddings_k(): - query_embedding = [1, 2] - chunks = ["a" * 31, "b" * 31, "c" * 31] - embeddings = [[1, 2], [3, 4], [5, 6]] - store_embeddings(chunks, embeddings) - results = query_embeddings(query_embedding, k=2) - assert len(results) == 2 \ No newline at end of file +def test_generate_review_with_intent(): + diff = "diff --git a/app.py b/app.py" + context = ["def safe_divide(a, b):"] + intent = {"purpose": "generate tests", "properties": ["a", "b"], "edge_cases": ["a=0", "b=0"]} + assert generate_tests(diff, context, intent) != "" \ No newline at end of file