From 0c5d36d182b6679063c64b041f15f7a24a34a761 Mon Sep 17 00:00:00 2001 From: tcpsyn Date: Sun, 5 Apr 2026 02:46:38 -0600 Subject: [PATCH] Strip markdown fences in _call_sonnet response --- backend/services/regulars_v2.py | 6 +++++- tests/test_regulars_v2.py | 22 +++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/backend/services/regulars_v2.py b/backend/services/regulars_v2.py index b84c1cf..addc98b 100644 --- a/backend/services/regulars_v2.py +++ b/backend/services/regulars_v2.py @@ -93,7 +93,11 @@ async def _call_sonnet(prompt: str) -> dict: model=PROMOTION_MODEL, usage_data=usage, ) - return json.loads(data["choices"][0]["message"]["content"]) + content = data["choices"][0]["message"]["content"].strip() + if content.startswith("```") and content.endswith("```"): + lines = content.splitlines() + content = "\n".join(lines[1:-1]) + return json.loads(content) async def evaluate_promotion(caller_name: str, call_transcript: str) -> dict: diff --git a/tests/test_regulars_v2.py b/tests/test_regulars_v2.py index 2b1891c..affb24d 100644 --- a/tests/test_regulars_v2.py +++ b/tests/test_regulars_v2.py @@ -1,5 +1,5 @@ import asyncio -from unittest.mock import AsyncMock, patch +from unittest.mock import AsyncMock, MagicMock, patch from backend.services.regulars_v2 import Regular, load_regular, REGULARS_DIR, SILAS_DIR @@ -49,3 +49,23 @@ def test_evaluate_promotion_rejects_when_no_arc(): with patch("backend.services.regulars_v2._call_sonnet", new=AsyncMock(return_value=fake_response)): result = asyncio.run(evaluate_promotion(caller_name="Carl", call_transcript="...")) assert result["promote"] is False + + +def test_call_sonnet_strips_markdown_fences(): + from backend.services.regulars_v2 import _call_sonnet + fake_resp = MagicMock() + fake_resp.raise_for_status = MagicMock() + fake_resp.json = MagicMock(return_value={ + "choices": [{"message": {"content": "```json\n{\"promote\": true, \"arc_plan\": \"ok\"}\n```"}}], + "usage": {"prompt_tokens": 10, "completion_tokens": 5}, + }) + mock_client = MagicMock() + mock_client.__aenter__ = AsyncMock(return_value=mock_client) + mock_client.__aexit__ = AsyncMock(return_value=None) + mock_client.post = AsyncMock(return_value=fake_resp) + + with patch("backend.services.regulars_v2.httpx.AsyncClient", return_value=mock_client), \ + patch("backend.services.regulars_v2.cost_tracker.record_llm_call"): + result = asyncio.run(_call_sonnet("prompt")) + assert result["promote"] is True + assert result["arc_plan"] == "ok"