diff --git a/src/mycoder/triage_agent.py b/src/mycoder/triage_agent.py index 63e3ebc..0ae6ef8 100644 --- a/src/mycoder/triage_agent.py +++ b/src/mycoder/triage_agent.py @@ -3,6 +3,7 @@ import logging import os import sys +import re from typing import Any, Dict, List try: @@ -44,7 +45,7 @@ 1. **Strict JSON Only:** Your final output must be **only** the JSON array. No markdown, no "Here is the JSON", no fluff. 2. **Label Discipline:** Use ONLY the labels provided in `{available_labels}`. Do not hallucinate new labels. 3. **Variable Safety:** Reference variables strictly. -4. **No Command Injection:** Do not use command substitution `$()` in generated output. +4. **No Command Injection:** Do not use command substitution `$()` in generated shell commands. ## Input Data @@ -79,7 +80,7 @@ ## Output Specification -Write a JSON array to the output. Format: +Write a JSON array to the output file. Format: ```json [ @@ -94,8 +95,8 @@ "explanation": "Request to beautify logging. Low priority per Goat Principle (current logs are ugly but functional)." }} ] -``` -""" +Final Command Construction +Generate the final shell command to write the JSON to the environment variable. Ensure the JSON string is single-quoted to handle special characters correctly. """ async def triage_issues_with_llm( @@ -195,13 +196,19 @@ async def triage_issues_with_llm( # 5. Parse JSON content = response.content.strip() - # Strip Markdown code blocks if present - if content.startswith("```json"): - content = content[7:] - if content.startswith("```"): - content = content[3:] - if content.endswith("```"): - content = content[:-3] + # Attempt to extract JSON array using regex if markdown or extra text is present + # Matches [...] with DOTALL + json_match = re.search(r"\[.*\]", content, re.DOTALL) + if json_match: + content = json_match.group(0) + else: + # Fallback to simple stripping if regex fails (e.g. no brackets) + if content.startswith("```json"): + content = content[7:] + if content.startswith("```"): + content = content[3:] + if content.endswith("```"): + content = content[:-3] content = content.strip() diff --git a/tests/unit/test_triage_agent.py b/tests/unit/test_triage_agent.py index 54ccc0f..f1bf4de 100644 --- a/tests/unit/test_triage_agent.py +++ b/tests/unit/test_triage_agent.py @@ -203,9 +203,9 @@ async def mock_query(prompt, **kwargs): self.assertIn( "Functionality > Aesthetics", prompt_sent ) # Check for Goat Principle - self.assertNotIn( + self.assertIn( "Final Command Construction", prompt_sent - ) # Check for sanitized prompt + ) # Check for command construction instructions self.assertIn(github_env_val, prompt_sent)