The world's first agentic reverse engineer.
feat: batch cleanup re-analysis for low-confidence functions
Amruth2956b6945e3c
diff --git a/kong/agent/supervisor.py b/kong/agent/supervisor.pyindex 30f4531..098fb15 100644--- a/kong/agent/supervisor.py+++ b/kong/agent/supervisor.py@@ -604,6 +604,7 @@ class Supervisor:known_types = self.client.list_custom_types()analyzer = Analyzer(self.client, self.llm_client)+ reanalyze_items: list[tuple[WorkItem, str, str]] = []for addr in reanalyze_addrs:if self._paused:continue@@ -628,42 +629,75 @@ class Supervisor:callees=self.triage_result.call_graph.callees.get(addr, []) if self.triage_result else [],)- try:- new_result = analyzer.analyze(- item,- binary_info=self.binary_info,- known_results=self.results,- strings=self.strings,- known_types=known_types,- )- except Exception as e:- self._emit(Event(- type=EventType.FUNCTION_ERROR,- phase=Phase.CLEANUP,- message=f"Error re-analyzing {func_name}: {e}",- data={"address": addr, "error": str(e)},- ))- continue+ decompilation = self._get_decompilation(addr)+ model = self._pick_model(item, decompilation)+ reanalyze_items.append((item, decompilation, model))- if new_result.confidence > (old_result.confidence if old_result else 0):- self.results[addr] = new_result- reanalyzed += 1+ if reanalyze_items:+ batches = self._assemble_batches(reanalyze_items)- self._emit(Event(- type=EventType.FUNCTION_COMPLETE,- phase=Phase.CLEANUP,- message=(- f"0x{addr:08x} → {new_result.name} "- f"(confidence: {new_result.confidence}%, "- f"was {old_result.confidence if old_result else 0}%)"- ),- data={- "address": addr,- "name": new_result.name,- "confidence": new_result.confidence,- "previous_confidence": old_result.confidence if old_result else 0,- },- ))+ for batch_items, model in batches:+ contexts = []+ for work_item, _, _ in batch_items:+ ctx = analyzer._build_context(+ work_item, self.binary_info, self.results,+ self.strings, known_types,+ )+ contexts.append(ctx)++ batch_prompt = analyzer.build_batch_prompt(contexts)+ try:+ responses = self.llm_client.analyze_function_batch(+ batch_prompt, model=model,+ )+ except Exception as e:+ logger.warning("Batch cleanup call failed: %s", e)+ continue++ if len(responses) != len(batch_items):+ continue++ for (work_item, _, _), response in zip(batch_items, responses):+ addr = work_item.function.address+ old_result = self.results.get(addr)++ if not response.name:+ continue++ old_confidence = old_result.confidence if old_result else 0++ if response.confidence > old_confidence:+ self._write_back_response(addr, response)+ new_result = FunctionResult(+ address=addr,+ original_name=work_item.function.name,+ name=response.name,+ signature=response.signature,+ confidence=response.confidence,+ classification=response.classification,+ comments=response.comments,+ reasoning=response.reasoning,+ llm_calls=1,+ struct_proposals=response.struct_proposals,+ )+ self.results[addr] = new_result+ reanalyzed += 1++ self._emit(Event(+ type=EventType.FUNCTION_COMPLETE,+ phase=Phase.CLEANUP,+ message=(+ f"0x{addr:08x} → {new_result.name} "+ f"(confidence: {new_result.confidence}%, "+ f"was {old_confidence}%)"+ ),+ data={+ "address": addr,+ "name": new_result.name,+ "confidence": new_result.confidence,+ "previous_confidence": old_confidence,+ },+ ))self._emit(Event(type=EventType.PHASE_COMPLETE,diff --git a/tests/test_agent_supervisor.py b/tests/test_agent_supervisor.pyindex b19dfc1..f707e33 100644--- a/tests/test_agent_supervisor.py+++ b/tests/test_agent_supervisor.py@@ -424,3 +424,36 @@ class TestBatchAssembly:assert sup._pick_model(small_item, "line\n" * 30) == HAIKU_MODELassert sup._pick_model(big_item, "line\n" * 30) == SONNET_MODEL+++class TestBatchedCleanup:+ def test_cleanup_uses_batch_call(self, tmp_path):+ from kong.agent.analyzer import LLMResponse++ funcs = [_func(0x1000, "FUN_1000", size=64)]+ client = _make_client(functions=funcs)+ client.get_decompilation.return_value = "void f(void) { return; }"+ client.list_custom_types.return_value = []+ config = KongConfig(output=OutputConfig(directory=tmp_path / "out"))++ mock_llm = MagicMock()+ mock_llm.analyze_function_batch.return_value = [+ LLMResponse(name="init_module", confidence=80, classification="init"),+ ]++ sup = Supervisor(client, config, llm_client=mock_llm)+ sup.results[0x1000] = FunctionResult(+ address=0x1000, original_name="FUN_1000",+ name="maybe_init", confidence=30, llm_calls=1,+ )+ sup.binary_info = client.get_binary_info()+ sup.functions = funcs+ sup.triage_result = MagicMock()+ sup.triage_result.call_graph.callers = {}+ sup.triage_result.call_graph.callees = {}++ sup._run_cleanup()++ assert mock_llm.analyze_function_batch.called+ assert sup.results[0x1000].name == "init_module"+ assert sup.results[0x1000].confidence == 80diff --git a/tests/test_type_recovery.py b/tests/test_type_recovery.pyindex 47796c3..bff8941 100644--- a/tests/test_type_recovery.py+++ b/tests/test_type_recovery.py@@ -640,17 +640,17 @@ class TestSupervisorCleanupIntegration:client.get_xrefs_from.return_value = []client.list_custom_types.return_value = []- call_count = 0+ batch_call_count = 0- def varying_response(prompt: str, **kwargs: object) -> LLMResponse:- nonlocal call_count- call_count += 1- if call_count == 1:- return LLMResponse(name="unknown_func", confidence=30)- return LLMResponse(name="decode_buffer", confidence=70)+ def varying_batch_response(prompt: str, **kwargs: object) -> list[LLMResponse]:+ nonlocal batch_call_count+ batch_call_count += 1+ if batch_call_count == 1:+ return [LLMResponse(name="unknown_func", confidence=30)]+ return [LLMResponse(name="decode_buffer", confidence=70)]llm = MagicMock()- llm.analyze_function.side_effect = varying_response+ llm.analyze_function_batch.side_effect = varying_batch_responseconfig = KongConfig(output=OutputConfig(directory=tmp_path / "out"))sup = Supervisor(client, config, llm_client=llm)