arXiv:2602.00059cs.LGcs.AI2026-02

用历史纠错经验优化代码生成,减少试错次数。

TextBFGS: A Case-Based Reasoning Approach to Code Optimization via Error-Operator Retrieval

  • 基于错误-操作轨迹构建案例库,模拟优化曲率。
  • 在HumanEval上提升通过率,调用次数减少40%以上。
  • 适合需要高效迭代的编程辅助场景。

基于大语言模型(LLM)的迭代代码生成可视为由文本反馈引导的优化过程。现有自纠错方法多为无状态、试错式的一阶搜索,未能利用过往求解经验。为此,我们提出TextBFGS,一种受拟牛顿优化启发的基于案例推理(CBR)框架。该框架不检索原始文本实例,而是维护一个动态的“错误-操作”修正轨迹案例库,以近似任务的语义曲率(逆海塞矩阵)。具体而言,当接收到文本错误反馈(目标问题)时,TextBFGS检索相似的历史修正模式(检索),并应用这些抽象操作来改进当前代码(重用/修订)。同时,成功的适应策略被持续回填至案例库中(保留),实现自我演化。在Python代码优化任务(HumanEval、MBPP)上的实证评估表明,TextBFGS显著优于无状态基线,在更少模型调用下达到更高通过率,建立了一种高效、经验驱动的LLM代码优化范式。

原文摘要 · Abstract (English)

Iterative code generation with Large Language Models (LLMs) can be viewed as an optimization process guided by textual feedback. However, existing LLM self-correction methods predominantly operate in a stateless, trial-and-error manner akin to first-order search, failing to leverage past problem-solving experiences. To bridge this gap, we introduce TextBFGS, a Case-Based Reasoning (CBR) framework inspired by the Quasi-Newton optimization method. Instead of retrieving raw, unstructured textual instances, TextBFGS maintains a dynamic Case Base of historical "Error-to-Operator" correction trajectories to approximate the semantic curvature (inverse Hessian matrix) of the task. Specifically, given a textual error feedback (the target problem), TextBFGS retrieves analogous historical correction patterns (Retrieve) and applies these abstract operators to refine the current code (Reuse/Revise). Furthermore, successful adaptations are continuously retained back into the Case Base (Retain), enabling a self-evolving system. Empirical evaluations on Python code optimization tasks (HumanEval, MBPP) demonstrate that TextBFGS significantly outperforms stateless baselines. It achieves superior pass rates with fewer model calls, establishing an efficient, experience-driven paradigm for LLM-based code optimization.

代码生成案例推理优化

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。