‹ 返回 2026-06-30

代码智能体需要多少静态结构?确定性锚定研究

How Much Static Structure Do Code Agents Need? A Study of Deterministic Anchoring

▲ 0 💬 1 2026-06-30

Zhihao Lin, Mingyi Zhou, Yizhuo Yang, Li Li

摘要

基于大语言模型的代码代理通过关键词搜索来在代码仓库中导航,但无法理解那些定义软件实际运行方式的结构关系,比如调用图、继承层次结构以及配置依赖关系。这就使得代理的导航行为具有随机性,难以在不同运行环境中重复实现。我们研究的是:是否可以通过轻量级的静态分析为这些代理提供确定性的指导依据——即通过将稳定的结构信息以纯文本形式注入到代码中,从而约束其概率性探索行为,使导航过程更加可预测。以OpenAI的Codex作为基础,我们系统地注入不同粒度的结构注释,并测量这些注释对代码定位、执行路径以及运行稳定性的影响。我们的研究发现了所谓的“确定性锚定效应”:静态结构虽然能提升代理的“智能程度”,但也能使其导航行为更加有规律且可重复。有三个现象支持这一发现:(1) 锚定效应有效:轻量级的调用/继承拓扑结构能够改善函数的定位效果(Func@5评分提升2.2个百分点),同时缩短执行路径(交互轮数减少1.6轮);(2) 锚定效应与规模相关:最佳的注释粒度和方向取决于代码仓库的特性——在语义密度较高的仓库中,这种效果有限;而在以中心节点为主的项目中,仅使用反向链接就能带来更好的效果;(3) 锚定效应能稳定执行过程:标签能够将链接跟随率从0.15-0.18提升至0.21-0.24,从而将运行间的差异降低约一半,同时还能提高中等规模仓库中的单次运行可靠性(Pass@1评分提升3.4个百分点)。不过,这样做会增加大约10%的输入token量。这些发现为实际应用提供了指导:对于中等规模的项目,应采用轻量级的拓扑结构;在大型仓库中则应去除正向边;而对于那些存在隐式依赖关系的场景,则应使用密集的标签。

English Abstract

LLM-based code agents navigate repositories through keyword search but miss the structural relationships, such as call graphs, inheritance hierarchies, and configuration dependencies, that define how software actually works. This makes agent navigation stochastic and difficult to reproduce across runs. We investigate whether lightweight static analysis can provide deterministic anchors for these agents: stable structural facts injected as plain-text comments that constrain probabilistic exploration and make navigation more predictable. Starting from a strong baseline, Codex from OpenAI, we systematically inject varying granularities of structural annotations and measure their effects on localization, trajectory behavior, and run-to-run stability. Our study identifies what we call the deterministic anchoring effect: static structure helps less by making agents "smarter" and more by making their navigation disciplined and reproducible. Three observations support this finding: (1) Anchoring works: lightweight call/inheritance topology improves function-level localization (+2.2pp Func@5) and shortens trajectories (-1.6 interaction rounds); (2) Anchoring is scale-sensitive: the optimal granularity and directionality depend on repository characteristics, where denser semantics show diminishing returns and hub-heavy projects benefit from inverse-only links that expose "who-calls-me" without forward edges; (3) Anchoring stabilizes: tags raise link-following rate from 0.15-0.18 to 0.21-0.24, roughly halve run-to-run variance, and improve single-run reliability (Pass@1 +3.4 pp) on medium-scale repositories, at the cost of roughly 10% more input tokens. These observations suggest practical guidelines: default to lightweight topology on medium projects, prune forward edges in large repositories, and reserve dense tags for implicit-dependency cases.