Neurosymbolic Repo-level Code Localization: LLM과 Datalog를 결합한 코드 위치 탐색
Neurosymbolic Repo-level Code Localization
TL;DR Highlight
기존 코드 탐색 도구들이 파일명·함수명 키워드에 의존한다는 편향을 발견하고, LLM이 Datalog 쿼리를 생성해 결정론적 추론 엔진으로 실행하는 LogicLoc을 제안
Who Should Read
AI 코딩 에이전트나 자동 버그 수정 파이프라인을 개발 중인 엔지니어. 특히 SWE-bench 류 벤치마크로 코드 탐색 성능을 평가하거나, 레포 전체를 대상으로 자연어 쿼리로 코드 위치를 찾는 기능을 만들려는 개발자.
Core Mechanics
- SWE-bench Lite 기준 50% 이상의 이슈에 파일명·클래스명·함수명 같은 식별자가 그대로 포함되어 있어, 기존 도구들이 '진짜 이해' 없이 키워드 매칭만으로 높은 점수를 낼 수 있는 Keyword Shortcut 편향이 존재함을 발견.
- 이 편향을 테스트하기 위해 KA-LogicQuery(키워드 없이 순수 구조적 조건만으로 코드 위치를 찾는 벤치마크)를 만들었는데, 기존 SOTA 도구들(SweRank, Agentless, LocAgent, CoSIL)은 Hit Rate조차 함수 레벨에서 40% 이하로 폭락.
- LogicLoc은 소스코드를 정적 분석해 함수 정의·상속·호출 그래프 등을 Datalog 사실(program facts)로 추출하고, LLM이 자연어 쿼리를 Datalog 프로그램으로 변환해 Soufflé 엔진으로 실행하는 방식.
- LLM이 생성한 Datalog 코드의 문법 오류를 자동 수정하는 parser-gated validation과, 중간 결과가 비어있는 규칙을 찾아내 왜 비었는지 진단하는 mutation-based 피드백 루프를 갖춤.
- mutation 진단은 문자열 완전 일치를 부분 일치로 완화(Contains-Literal)하거나, 조건절을 하나씩 제거(Drop-Single-Atom)해 어느 제약이 결과를 비게 만드는지 찾아내 LLM에게 피드백.
- KA-LogicQuery-Neg라는 '정답이 없는' 벤치마크도 만들었는데, 기존 도구들은 정답이 없어도 top-N 추천을 강행하는 반면 LogicLoc은 70% 이상의 경우에서 '해당 없음'을 정확히 반환.
Evidence
- KA-LogicQuery 함수 레벨 PLR(예측 집합이 정답과 완전히 일치하는 비율): LogicLoc(Qwen3-Max) 38.27% vs 모든 베이스라인 0%. 파일 레벨 Precision도 LogicLoc 73.35% vs 최고 베이스라인(LocAgent Claude-3.5) 11.02%.
- KA-LCL 태스크 평균 실행 시간: LogicLoc(Claude-3.5) 39초 vs 가장 빠른 베이스라인 LocAgent(GPT-4o) 대비 2배 이상 빠름. 토큰 소비는 평균 16.2k로, LocAgent와 Agentless가 수십만 토큰 이상 사용하는 것에 비해 10배 이상 효율적.
- Ablation: validation & repair(VAL)만 추가해도 Qwen3-Max의 실행 성공률이 73.59%→84.12%, 비어있지 않은 결과 비율이 49%→75.68%로 상승, 평균 실행 시간이 427초→104초로 급감.
- SWE-bench Lite에서도 LogicLoc(Claude-3.5) 함수 레벨 Acc@5 기준 68.98%로, 평균 예측 후보 수 2개만 제시하면서도 5~100개를 제시하는 베이스라인과 경쟁력 있는 성능 유지.
How to Apply
- 레포 전체를 대상으로 '이 조건을 만족하는 함수를 찾아줘' 식의 구조적 쿼리가 필요하다면, AST 파싱으로 함수·클래스·호출 관계를 Datalog 사실로 추출하고 LLM에게 Datalog 쿼리를 생성시킨 뒤 Soufflé로 실행하는 파이프라인을 구성하면 됨.
- LLM이 Datalog/CodeQL 같은 선언형 쿼리를 생성하는 파이프라인을 만들 때, LLM 출력을 바로 실행하지 말고 파서로 문법 체크 → 결정론적 수정 시도 → 실패 시 오류 피드백 재생성 루프를 넣으면 실행 성공률이 크게 오름(논문 기준 +10~20%p).
- 코드 에이전트가 '이런 패턴의 코드가 없다'고 확실하게 답해야 하는 상황(예: 취약점 스캔 결과가 0이어야 정상)에서는 top-N 추천 방식 대신 결정론적 쿼리 엔진 기반 접근으로 false positive를 줄일 수 있음.
Code Example
# LogicLoc 스타일: Python AST → Datalog facts → LLM 쿼리 생성 → Soufflé 실행
import ast, subprocess, textwrap
# 1. Program facts 추출 (함수 정의)
def extract_function_facts(filepath: str) -> list[str]:
facts = []
with open(filepath) as f:
tree = ast.parse(f.read())
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
param_count = len(node.args.args)
containing_class = "module_level"
for parent in ast.walk(tree):
if isinstance(parent, ast.ClassDef):
if node in ast.walk(parent):
containing_class = parent.name
facts.append(
f'function_definition("{filepath}", "{node.name}", '
f'{node.lineno}, {node.end_lineno}, {param_count}, '
f'"false", "{containing_class}").'
)
return facts
# 2. LLM에게 Datalog 쿼리 생성 요청 (프롬프트 예시)
system_prompt = """
You are a Datalog query generator for code analysis.
Available EDB predicates:
function_definition(file_path, function_name, start_line, end_line, param_count, is_async, containing_class)
class_definition(file_path, class_name, start_line, end_line, base_class)
function_call(caller_file, caller_name, callee_name, call_line)
Generate a Soufflé Datalog program to answer the query.
Always declare output with .output directive.
"""
user_query = "Find all functions with more than 15 parameters that are not __init__ methods"
# 3. 생성된 Datalog 프로그램 예시
datalog_program = textwrap.dedent("""
.decl function_definition(file_path:symbol, function_name:symbol,
start_line:number, end_line:number, param_count:number,
is_async:symbol, containing_class:symbol)
.input function_definition
.decl LargeFunctions(file_path:symbol, function_name:symbol,
start_line:number, param_count:number)
LargeFunctions(fp, fn, sl, pc) :-
function_definition(fp, fn, sl, _, pc, _, _),
pc > 15,
fn != "__init__".
.output LargeFunctions
""")
# 4. Soufflé로 실행
# subprocess.run(["souffle", "-F", "facts/", "-D", "output/", "query.dl"])
print("Datalog 프로그램 생성 완료. Soufflé로 실행하세요.")
print(datalog_program)Terminology
Related Resources
Original Abstract (Expand)
Code localization is a cornerstone of autonomous software engineering. Recent advancements have achieved impressive performance on real-world issue benchmarks. However, we identify a critical yet overlooked bias: these benchmarks are saturated with keyword references (e.g. file paths, function names), encouraging models to rely on superficial lexical matching rather than genuine structural reasoning. We term this phenomenon the Keyword Shortcut. To address this, we formalize the challenge of Keyword-Agnostic Logical Code Localization (KA-LCL) and introduce KA-LogicQuery, a diagnostic benchmark requiring structural reasoning without any naming hints. Our evaluation reveals a catastrophic performance drop of state-of-the-art approaches on KA-LogicQuery, exposing their lack of deterministic reasoning capabilities. We propose LogicLoc, a novel agentic framework that combines large language models with the rigorous logical reasoning of Datalog for precise localization. LogicLoc extracts program facts from the codebase and leverages an LLM to synthesize Datalog programs, with parser-gated validation and mutation-based intermediate-rule diagnostic feedback to ensure correctness and efficiency. The validated programs are executed by a high-performance inference engine, enabling accurate and verifiable localization in a fully automated, closed-loop workflow. Experimental results demonstrate that LogicLoc significantly outperforms SOTA methods on KA-LogicQuery while maintaining competitive performance on popular issue-driven benchmarks. Notably, LogicLoc attains superior performance with significantly lower token consumption and faster execution by offloading structural traversal to a deterministic engine, reducing the overhead of iterative LLM inference.