From 01c360fe569a886cb299e72307713b0ae588637a Mon Sep 17 00:00:00 2001 From: GSSwain <12575773+GSSwain@users.noreply.github.com> Date: Sun, 28 Jun 2026 18:50:34 +1000 Subject: [PATCH] Add clean code practices to system instruction --- src/maca/agents/coder.py | 12 ++++++++++-- src/maca/agents/reviewer.py | 15 +++++++++++---- tests/test_agents.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 tests/test_agents.py diff --git a/src/maca/agents/coder.py b/src/maca/agents/coder.py index 8ed6e08..e56fdbb 100644 --- a/src/maca/agents/coder.py +++ b/src/maca/agents/coder.py @@ -12,7 +12,15 @@ def run(self, task_description, plan, repo_files_content=None, history=None): def _build_system_instruction(self): return ( - "You are a Software Coder Agent. Your job is to implement the changes outlined in the plan.\n\n" + "You are a Software Coder Agent. Your job is to implement the changes outlined in the plan " + "while strictly adhering to clean code practices.\n\n" + "CLEAN CODE GUIDELINES:\n" + "- Modularity: Break code into small, single-purpose functions/classes.\n" + "- Readability: Use clear, descriptive, and consistent variable/function names.\n" + "- Documentation: Include concise docstrings and inline comments explaining complex logic.\n" + "- Type Safety: Use type hints for function parameters and return types where applicable.\n" + "- Error Handling: Handle potential exceptions gracefully (no bare except blocks).\n" + "- Simplicity: Avoid over-engineering, code duplication, or spaghetti code.\n\n" "CRITICAL: You MUST write the file identifier line in the EXACT format: [FILE: path/to/file.ext]\n" "Do NOT use markdown headers (like '## FILE: ...' or '# FILE: ...'), bullet points, or bold text. " "The parser WILL fail if you do not use square brackets [FILE: ...].\n\n" @@ -32,7 +40,7 @@ def _build_prompt(self, task_description, plan, repo_files_content, history): f"User Task: {task_description}{history_context}\n\n" f"Implementation Plan:\n{plan}\n" f"{files_context}\n" - "Please implement the changes and output the files using the requested [FILE: path] format." + "Please implement the changes using clean code principles, and output the files using the requested [FILE: path] format." ) def _format_history(self, history): diff --git a/src/maca/agents/reviewer.py b/src/maca/agents/reviewer.py index 05a136d..1098553 100644 --- a/src/maca/agents/reviewer.py +++ b/src/maca/agents/reviewer.py @@ -12,9 +12,15 @@ def run(self, task_description, generated_files, history=None): def _build_system_instruction(self): return ( - "You are a Senior Reviewer Agent. Your job is to review the code generated for the task. " - "Look for syntax errors, logical bugs, missing imports, or edge cases. " - "If changes are needed, explain why and output the corrected files.\n\n" + "You are a Senior Reviewer Agent. Your job is to review the code generated for the task " + "with a strict focus on clean code practices and technical correctness.\n\n" + "CLEAN CODE AUDITING CRITERIA:\n" + "1. Readability & Naming: Are variables, functions, and classes descriptively and consistently named?\n" + "2. Modularity & Design: Is the code modular? Are functions doing only one thing (Single Responsibility)?\n" + "3. Documentation & Type Hints: Are public interfaces properly documented with docstrings and type hints?\n" + "4. Correctness & Quality: Are there syntax errors, logic bugs, bare excepts, or code duplication?\n\n" + "If you find issues that violate these clean code principles or correctness, explain the issues clearly, " + "and output the corrected files.\n\n" "CRITICAL: You MUST write the file identifier line in the EXACT format: [FILE: path/to/file.ext]\n" "Do NOT use markdown headers (like '## FILE: ...' or '# FILE: ...'), bullet points, or bold text. " "The parser WILL fail if you do not use square brackets [FILE: ...].\n\n" @@ -33,7 +39,8 @@ def _build_prompt(self, task_description, generated_files, history): return ( f"User Task: {task_description}{history_context}\n\n" f"Generated Files to Review:\n{files_context}" - "Please review the code, suggest improvements, and output corrected files if needed." + "Please review the code for correctness, logical bugs, and clean code practices. " + "Suggest improvements and output corrected files if needed." ) def _format_history(self, history): diff --git a/tests/test_agents.py b/tests/test_agents.py new file mode 100644 index 0000000..e6d8645 --- /dev/null +++ b/tests/test_agents.py @@ -0,0 +1,34 @@ +import unittest +from unittest.mock import MagicMock + +from maca.agents.coder import CoderAgent +from maca.agents.reviewer import ReviewerAgent + + +class TestAgentsPrompts(unittest.TestCase): + def setUp(self): + self.mock_client = MagicMock() + self.coder = CoderAgent("Coder", self.mock_client) + self.reviewer = ReviewerAgent(self.mock_client) + + def test_coder_system_instruction_clean_code(self): + sys_inst = self.coder._build_system_instruction() + self.assertIn("clean code practices", sys_inst) + self.assertIn("CLEAN CODE GUIDELINES", sys_inst) + self.assertIn("Modularity", sys_inst) + self.assertIn("Type Safety", sys_inst) + + def test_coder_prompt_clean_code(self): + prompt = self.coder._build_prompt("test task", "test plan", {}, []) + self.assertIn("clean code principles", prompt) + + def test_reviewer_system_instruction_clean_code(self): + sys_inst = self.reviewer._build_system_instruction() + self.assertIn("clean code practices", sys_inst) + self.assertIn("CLEAN CODE AUDITING CRITERIA", sys_inst) + self.assertIn("Readability & Naming", sys_inst) + self.assertIn("Single Responsibility", sys_inst) + + def test_reviewer_prompt_clean_code(self): + prompt = self.reviewer._build_prompt("test task", {}, []) + self.assertIn("clean code practices", prompt)