The world's first agentic reverse engineer.
feat: add batch prompt builder to Analyzer
Amruth5540c726a6a1
diff --git a/kong/agent/analyzer.py b/kong/agent/analyzer.pyindex a96433f..e56eae5 100644--- a/kong/agent/analyzer.py+++ b/kong/agent/analyzer.py@@ -352,6 +352,54 @@ class Analyzer:return "\n".join(parts)+ def build_batch_prompt(self, contexts: list[AnalysisContext]) -> str:+ """Build a single LLM prompt combining multiple function contexts for batch analysis."""+ if not contexts:+ return ""++ first = contexts[0]+ parts = []++ parts.append(+ f"Binary: {first.binary_info.arch} {first.binary_info.format} "+ f"({first.binary_info.compiler})"+ )+ parts.append("")+ parts.append(f"Analyze the following {len(contexts)} functions.")++ for i, context in enumerate(contexts, start=1):+ parts.append("")+ parts.append(+ f"=== Function {i}: {context.function.name} "+ f"(0x{context.function.address:08x}) ==="+ )+ parts.append(f"Size: {context.function.size} bytes")+ parts.append("")+ parts.append("### Decompilation")+ parts.append("```c")+ parts.append(context.decompilation)+ parts.append("```")++ if context.referenced_strings:+ parts.append("")+ parts.append("### Referenced Strings")+ for s in context.referenced_strings:+ parts.append(f'- "{s}"')++ if context.callee_snippets:+ parts.append("")+ parts.append("### Known Callees")+ for cs in context.callee_snippets:+ parts.append(f"- 0x{cs.address:08x}: {cs.name}")++ if first.known_functions:+ parts.append("")+ parts.append("### Already Identified Functions")+ for addr, name in sorted(first.known_functions.items()):+ parts.append(f"- 0x{addr:08x}: {name}")++ return "\n".join(parts)+def _write_back(self, addr: int, response: LLMResponse) -> bool:"""Write analysis results back to Ghidra.diff --git a/tests/test_agent_analyzer.py b/tests/test_agent_analyzer.pyindex bdb8c05..f4ef4c2 100644--- a/tests/test_agent_analyzer.py+++ b/tests/test_agent_analyzer.py@@ -349,3 +349,37 @@ class TestNormalizerIntegration:assert len(ctx.callee_snippets) == 1assert "undefined4" not in ctx.callee_snippets[0].snippetassert "- 3" in ctx.callee_snippets[0].snippet+++class TestBuildBatchPrompt:+ def test_batch_prompt_contains_all_functions(self) -> None:+ """Batch prompt should contain separator and decompilation for each context."""+ client = MagicMock()+ llm = MagicMock()+ analyzer = Analyzer(client, llm)++ binary_info = BinaryInfo(+ arch="x86", format="ELF", endianness="little",+ word_size=8, compiler="gcc", name="test",+ )++ contexts = []+ for i in range(3):+ ctx = AnalysisContext(+ function=FunctionInfo(+ address=0x1000 + i * 0x100,+ name=f"FUN_{0x1000 + i * 0x100:08x}",+ size=64,+ ),+ decompilation=f"void func_{i}(void) {{ return; }}",+ binary_info=binary_info,+ )+ contexts.append(ctx)++ prompt = analyzer.build_batch_prompt(contexts)+ assert "=== Function 1" in prompt+ assert "=== Function 2" in prompt+ assert "=== Function 3" in prompt+ assert "func_0" in prompt+ assert "func_2" in prompt+ assert "0x00001000" in prompt