生成前规范:针对LLM生成代码的规范框架在金钱、时间、幂等性方面的预注册五模型配对评估
研究论文 · 2026/09/30
本文预注册并测试了一个规范框架:在提示中加入固定前言,说明生成代码必须满足的条件。研究使用50个来自金融、医疗和保险领域的真实后端任务,在五个前沿模型上分别进行裸提示和加入267词规范框架的配对测试,并用九个基于AST的确定性检查器和一个独立的安全扫描器评分。结果显示,规范框架在所有五个模型中都减少了缺陷(平均每任务减少0.16至0.70个发现,所有Holm校正符号检验显著,所有bootstrap置信区间排除零)。在两组结果不同的情况下,规范框架组在100次中赢了95次,且在任何领域都未使任何模型变差。安全扫描器在裸提示组发现53个中高危问题,在规范框架组发现11个,方向一致。效果在模型无提示默认最弱的地方最大。所有500个输出、提示、检查器、评分代码和预注册均已发布DOI。
这项研究通过预注册的严格实验设计,证明了在提示中加入规范框架能显著减少LLM生成代码的缺陷,且效果在多个模型和领域上一致。对于关注代码生成质量和安全性的团队,这提供了一种简单有效的改进方法,并且所有材料公开可复现。
Code generated by large language models passes security checks at a rate that has barely moved in four years. In regulated backends, the defect classes that matter most are money arithmetic, time handling, retry safety, and access control. Teams answer with instruction files, yet the largest controlled study of instruction files to date found no benefit. This paper tests a narrower idea: generated code improves when the prompt carries a specification, a fixed preamble stating what must be true of the result. We pre-registered hypotheses, refuters, analysis code, and a one-shot generation rule, then ran 50 realistic backend tasks from finance, healthcare, and insurance practice through five frontier models from five vendor lineages, each task twice: bare, and preceded by a 267-word filled specification frame. Nine deterministic AST-based checkers scored the outputs. The Bandit security scanner, which knows nothing of the frame, scored them independently. The frame reduced defects in all five models (mean reduction 0.16 to 0.70 findings per task, every Holm-adjusted sign test significant, every bootstrap confidence interval excluding zero). Where the arms differed, the frame arm won 95 of 100 times. It never made any model worse in any domain. Bandit found 53 medium-or-high issues in the bare arm and 11 in the frame arm, in the same direction for every model. The effect was largest where a model's unprompted defaults were weakest: the frame supplies the discipline a model lacks. All 500 outputs, prompts, checkers, scoring code, and the pre-registration are published with a DOI, so any team can re-derive the result without trusting the author.