The world's first agentic reverse engineer.
feat: add batch JSON parser for multi-function LLM responses
Amruthd33cfac8e1fa
diff --git a/kong/agent/analyzer.py b/kong/agent/analyzer.pyindex e56eae5..96121f3 100644--- a/kong/agent/analyzer.py+++ b/kong/agent/analyzer.py@@ -496,3 +496,65 @@ class Analyzer:struct_proposals=struct_proposals,raw=raw,)++ @staticmethod+ def parse_llm_json_batch(raw: str) -> list[LLMResponse]:+ text = raw.strip()++ if "```json" in text:+ start = text.index("```json") + 7+ end = text.index("```", start)+ text = text[start:end].strip()+ elif "```" in text:+ start = text.index("```") + 3+ end = text.index("```", start)+ text = text[start:end].strip()++ try:+ data = json.loads(text)+ except json.JSONDecodeError:+ logger.warning("Failed to parse batch LLM response: %s", raw[:200])+ return []++ if not isinstance(data, list):+ single = Analyzer.parse_llm_json(raw)+ return [single] if single.name else []++ results: list[LLMResponse] = []+ for entry in data:+ struct_proposals = []+ for sp in entry.get("struct_proposals", []):+ if not sp.get("name") or not sp.get("total_size"):+ continue+ try:+ total_size = int(sp["total_size"])+ except (ValueError, TypeError):+ continue+ fields = [+ StructFieldProposal(+ name=f.get("name", f"field_{i}"),+ data_type=f.get("data_type", "undefined"),+ offset=int(f.get("offset", 0)),+ size=int(f.get("size", 4)),+ )+ for i, f in enumerate(sp.get("fields", []))+ ]+ struct_proposals.append(StructProposal(+ name=sp["name"],+ total_size=total_size,+ fields=fields,+ used_by_param=sp.get("used_by_param", ""),+ ))++ results.append(LLMResponse(+ name=entry.get("name", ""),+ signature=entry.get("signature", ""),+ confidence=entry.get("confidence", 0),+ classification=entry.get("classification", ""),+ comments=entry.get("comments", ""),+ reasoning=entry.get("reasoning", ""),+ struct_proposals=struct_proposals,+ raw=raw,+ ))++ return resultsdiff --git a/tests/test_agent_analyzer.py b/tests/test_agent_analyzer.pyindex f4ef4c2..644642f 100644--- a/tests/test_agent_analyzer.py+++ b/tests/test_agent_analyzer.py@@ -2,6 +2,7 @@from __future__ import annotations+import jsonfrom unittest.mock import MagicMock, patchfrom kong.agent.analyzer import (@@ -383,3 +384,28 @@ class TestBuildBatchPrompt:assert "func_0" in promptassert "func_2" in promptassert "0x00001000" in prompt+++class TestParseBatchJson:+ def test_parses_json_array(self) -> None:+ raw = json.dumps([+ {"address": "0x1000", "name": "foo", "signature": "void foo(void)",+ "confidence": 85, "classification": "utility", "comments": "", "reasoning": ""},+ {"address": "0x2000", "name": "bar", "signature": "int bar(int x)",+ "confidence": 70, "classification": "math", "comments": "", "reasoning": ""},+ ])+ results = Analyzer.parse_llm_json_batch(raw)+ assert len(results) == 2+ assert results[0].name == "foo"+ assert results[1].name == "bar"+ assert results[0].confidence == 85++ def test_handles_markdown_fences(self) -> None:+ raw = '```json\n[{"address": "0x1000", "name": "test", "confidence": 50}]\n```'+ results = Analyzer.parse_llm_json_batch(raw)+ assert len(results) == 1+ assert results[0].name == "test"++ def test_returns_empty_on_parse_failure(self) -> None:+ results = Analyzer.parse_llm_json_batch("not json at all")+ assert results == []