The world's first agentic reverse engineer.
feat: wire semantic synthesis into supervisor pipeline
Amruth4e908d7175b4
diff --git a/kong/agent/supervisor.py b/kong/agent/supervisor.pyindex 4872ce1..146ea28 100644--- a/kong/agent/supervisor.py+++ b/kong/agent/supervisor.py@@ -1,6 +1,6 @@"""Supervisor agent — orchestrates the full analysis pipeline.-Drives: triage → analysis → cleanup → export.+Drives: triage → analysis → cleanup → synthesis → export.Emits structured events for TUI/CLI consumption."""@@ -24,6 +24,8 @@ from kong.export.structured import export_jsonfrom kong.ghidra.client import GhidraClientfrom kong.ghidra.types import BinaryInfo, FunctionInfo, StringEntryfrom kong.llm.client import TokenUsage+from kong.normalizer.syntactic import normalize+from kong.synthesis.semantic import SemanticSynthesizer, SynthesisResultlogger = logging.getLogger(__name__)@@ -62,6 +64,7 @@ class Supervisor:self.functions: list[FunctionInfo] = []self.strings: list[StringEntry] = []self.struct_accumulator = StructAccumulator()+ self._synthesis_result: SynthesisResult | None = Noneself._listeners: list[EventCallback] = []self._paused: bool = False@@ -99,6 +102,7 @@ class Supervisor:self._run_triage()self._run_analysis()self._run_cleanup()+ self._run_synthesis()self._run_export()except Exception as e:self._emit(Event(@@ -611,6 +615,96 @@ class Supervisor:},))+ def _run_synthesis(self) -> None:+ """Cross-function synthesis: unify globals, synthesize structs, refine names."""+ self._emit(Event(+ type=EventType.PHASE_START,+ phase=Phase.SYNTHESIS,+ message="Starting synthesis...",+ ))++ if self.llm_client is None:+ self._emit(Event(+ type=EventType.PHASE_COMPLETE,+ phase=Phase.SYNTHESIS,+ message="Synthesis skipped (no LLM client).",+ ))+ return++ decompilations: dict[int, str] = {}+ for addr, result in self.results.items():+ if result.skipped or result.error:+ continue+ decomp = self.client.get_decompilation(addr)+ if decomp:+ decompilations[addr] = normalize(decomp)++ if not decompilations:+ self._emit(Event(+ type=EventType.PHASE_COMPLETE,+ phase=Phase.SYNTHESIS,+ message="Synthesis skipped (no decompilations).",+ ))+ return++ synthesizer = SemanticSynthesizer(self.llm_client)+ try:+ synthesis_result = synthesizer.synthesize(+ list(self.results.values()), decompilations, model=SONNET_MODEL,+ )+ except Exception as e:+ logger.warning("Synthesis failed: %s", e)+ self._emit(Event(+ type=EventType.PHASE_COMPLETE,+ phase=Phase.SYNTHESIS,+ message=f"Synthesis failed: {e}",+ data={"error": str(e)},+ ))+ return++ if synthesis_result.globals:+ self._emit(Event(+ type=EventType.SYNTHESIS_GLOBALS_UNIFIED,+ phase=Phase.SYNTHESIS,+ message=f"Unified {len(synthesis_result.globals)} global variables.",+ data={"count": len(synthesis_result.globals), "globals": synthesis_result.globals},+ ))++ if synthesis_result.structs:+ self._emit(Event(+ type=EventType.SYNTHESIS_STRUCTS_SYNTHESIZED,+ phase=Phase.SYNTHESIS,+ message=f"Synthesized {len(synthesis_result.structs)} structs.",+ data={"count": len(synthesis_result.structs)},+ ))++ if synthesis_result.name_refinements:+ for addr_str, new_name in synthesis_result.name_refinements.items():+ addr = int(addr_str, 16) if addr_str.startswith("0x") else int(addr_str)+ if addr in self.results:+ self.results[addr].name = new_name+ try:+ self.client.rename_function(addr, new_name)+ except Exception as e:+ logger.warning(+ "Failed to rename 0x%08x to %s during synthesis: %s",+ addr, new_name, e,+ )+ self._emit(Event(+ type=EventType.SYNTHESIS_NAMES_REFINED,+ phase=Phase.SYNTHESIS,+ message=f"Refined {len(synthesis_result.name_refinements)} function names.",+ data={"count": len(synthesis_result.name_refinements)},+ ))++ self._synthesis_result = synthesis_result++ self._emit(Event(+ type=EventType.PHASE_COMPLETE,+ phase=Phase.SYNTHESIS,+ message="Synthesis complete.",+ ))+def _run_export(self) -> None:"""Generate output files."""self._emit(Event(@@ -630,7 +724,13 @@ class Supervisor:for addr in exportable_addrs:decomp = self.client.get_decompilation(addr)if decomp:- decompilations[addr] = decomp+ decompilations[addr] = normalize(decomp)++ if self._synthesis_result:+ synthesizer = SemanticSynthesizer(self.llm_client)+ decompilations = synthesizer.apply_to_decompilations(+ self._synthesis_result, decompilations,+ )raw_usage = getattr(self.llm_client, "usage", None) if self.llm_client else Nonetoken_usage = raw_usage if isinstance(raw_usage, TokenUsage) else TokenUsage()diff --git a/tests/test_agent_supervisor.py b/tests/test_agent_supervisor.pyindex 9948b5a..dfdb981 100644--- a/tests/test_agent_supervisor.py+++ b/tests/test_agent_supervisor.py@@ -45,7 +45,7 @@ class TestSupervisorLifecycle:assert types[0] == EventType.RUN_STARTassert types[-1] == EventType.RUN_COMPLETE- def test_all_four_phases_run(self, tmp_path):+ def test_all_five_phases_run(self, tmp_path):client = _make_client()config = KongConfig(output=OutputConfig(directory=tmp_path / "out"))sup = Supervisor(client, config)@@ -55,10 +55,8 @@ class TestSupervisorLifecycle:sup.run()phase_starts = [e.phase for e in events if e.type == EventType.PHASE_START]- assert Phase.TRIAGE in phase_starts- assert Phase.ANALYSIS in phase_starts- assert Phase.CLEANUP in phase_starts- assert Phase.EXPORT in phase_starts+ expected = [Phase.TRIAGE, Phase.ANALYSIS, Phase.CLEANUP, Phase.SYNTHESIS, Phase.EXPORT]+ assert phase_starts == expecteddef test_run_with_no_functions(self, tmp_path):client = _make_client(functions=[])@@ -298,3 +296,65 @@ class TestSupervisorExport:))sup = Supervisor(client, config)sup.run()+++class TestSupervisorSynthesis:+ def test_synthesis_phase_runs(self, tmp_path):+ funcs = [_func(0x1000, "a")]+ client = _make_client(functions=funcs)+ client.get_decompilation.return_value = "void a(void) { return; }"+ config = KongConfig(output=OutputConfig(directory=tmp_path / "out"))++ from kong.agent.analyzer import LLMResponse++ mock_llm = MagicMock()+ mock_llm.analyze_function.return_value = LLMResponse(+ name="init", confidence=80, raw='{"globals":{},"structs":[],"name_refinements":{}}',+ )++ sup = Supervisor(client, config, llm_client=mock_llm)++ events = []+ sup.on_event(events.append)+ sup.run()++ phase_starts = [e.phase for e in events if e.type == EventType.PHASE_START]+ assert Phase.SYNTHESIS in phase_starts++ phase_completes = [e.phase for e in events if e.type == EventType.PHASE_COMPLETE]+ assert Phase.SYNTHESIS in phase_completes++ def test_synthesis_skipped_without_llm_client(self, tmp_path):+ funcs = [_func(0x1000, "a")]+ client = _make_client(functions=funcs)+ config = KongConfig(output=OutputConfig(directory=tmp_path / "out"))+ sup = Supervisor(client, config)++ events = []+ sup.on_event(events.append)+ sup.run()++ synthesis_completes = [+ e for e in events+ if e.type == EventType.PHASE_COMPLETE and e.phase == Phase.SYNTHESIS+ ]+ assert len(synthesis_completes) == 1+ assert "skipped" in synthesis_completes[0].message.lower()++ def test_synthesis_skipped_with_no_results(self, tmp_path):+ client = _make_client(functions=[])+ config = KongConfig(output=OutputConfig(directory=tmp_path / "out"))++ mock_llm = MagicMock()+ sup = Supervisor(client, config, llm_client=mock_llm)++ events = []+ sup.on_event(events.append)+ sup.run()++ synthesis_completes = [+ e for e in events+ if e.type == EventType.PHASE_COMPLETE and e.phase == Phase.SYNTHESIS+ ]+ assert len(synthesis_completes) == 1+ assert "skipped" in synthesis_completes[0].message.lower()