Strip markdown fences in _call_sonnet response

This commit is contained in:
2026-04-05 02:46:38 -06:00
parent d7e475331d
commit 0c5d36d182
2 changed files with 26 additions and 2 deletions
+5 -1
View File
@@ -93,7 +93,11 @@ async def _call_sonnet(prompt: str) -> dict:
model=PROMOTION_MODEL, model=PROMOTION_MODEL,
usage_data=usage, 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: async def evaluate_promotion(caller_name: str, call_transcript: str) -> dict:
+21 -1
View File
@@ -1,5 +1,5 @@
import asyncio 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 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)): with patch("backend.services.regulars_v2._call_sonnet", new=AsyncMock(return_value=fake_response)):
result = asyncio.run(evaluate_promotion(caller_name="Carl", call_transcript="...")) result = asyncio.run(evaluate_promotion(caller_name="Carl", call_transcript="..."))
assert result["promote"] is False 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"