The world's first agentic reverse engineer.
feat: cap synthesis prompt at 50 most-connected functions
Amruth89a73e833e5a
diff --git a/kong/synthesis/semantic.py b/kong/synthesis/semantic.pyindex 76bb32d..6e916e6 100644--- a/kong/synthesis/semantic.py+++ b/kong/synthesis/semantic.py@@ -15,6 +15,8 @@ if TYPE_CHECKING:logger = logging.getLogger(__name__)+SYNTHESIS_FUNCTION_CAP = 50+_DAT_PATTERN = re.compile(r"DAT_[0-9a-fA-F]+")@@ -99,9 +101,17 @@ class SemanticSynthesizer:parts.append("## Functions and Decompilations")parts.append("")- for result in results:- if result.address not in decompilations:- continue+ xref_counts: dict[int, int] = {}+ for addrs in globals_map.values():+ for addr in addrs:+ xref_counts[addr] = xref_counts.get(addr, 0) + 1++ eligible = [r for r in results if r.address in decompilations]+ if len(eligible) > SYNTHESIS_FUNCTION_CAP:+ eligible.sort(key=lambda r: xref_counts.get(r.address, 0), reverse=True)+ eligible = eligible[:SYNTHESIS_FUNCTION_CAP]++ for result in eligible:parts.append(f"### {result.name} (0x{result.address:08x})")parts.append(f"Classification: {result.classification}, Confidence: {result.confidence}")parts.append("```c")diff --git a/tests/test_synthesis.py b/tests/test_synthesis.pyindex 35375e5..6cb05ba 100644--- a/tests/test_synthesis.py+++ b/tests/test_synthesis.py@@ -181,6 +181,35 @@ Trailing text'''assert result.name_refinements == {}+class TestSynthesisCap:+ def test_synthesis_caps_at_50_functions(self) -> None:+ from kong.synthesis.semantic import SYNTHESIS_FUNCTION_CAP++ results = [_make_result(i, f"func_{i}") for i in range(100)]+ decomps = {i: f"void func_{i}(void) {{ DAT_1000 = 1; }}" for i in range(100)}++ synth = SemanticSynthesizer(FakeLLMClient())+ prompt = synth._build_synthesis_prompt(results, decomps)++ func_count = prompt.count("### func_")+ assert func_count <= SYNTHESIS_FUNCTION_CAP++ def test_prioritizes_functions_with_most_xrefs(self) -> None:+ results = [_make_result(i, f"func_{i}") for i in range(60)]+ decomps = {}+ for i in range(60):+ if i < 5:+ decomps[i] = f"void func_{i}(void) {{ DAT_A = 1; DAT_B = 2; DAT_C = 3; }}"+ else:+ decomps[i] = f"void func_{i}(void) {{ return; }}"++ synth = SemanticSynthesizer(FakeLLMClient())+ prompt = synth._build_synthesis_prompt(results, decomps)++ for i in range(5):+ assert f"func_{i}" in prompt++class TestGlobalApplication:def test_apply_globals_replaces_dat_names(self) -> None:code = "int val = DAT_100008000 + DAT_100008000;"