Large Language Models of Code Fail at Completing Code with Potential Bugs

Tuan Dinh, Jinman Zhao, Samson Tan, Renato Negrinho, Leonard Lausen, Sheng Zha, George Karypis

Introduction

Large Language Models (LLMs) have transformed software development through sophisticated autocompletion tools. However, these models operate under a significant, often unspoken assumption: that the code a developer has already written is correct. In practice, software development is an iterative, error-prone process. This paper introduces the buggy-code completion (bCC) task, revealing a critical vulnerability in state-of-the-art models like CodeGen and InCoder. When faced with a single "potential bug" in the input context, model performance can plummet by over 50%, highlighting a major blind spot in current AI-assisted coding.

The "Clean Prefix" Fallacy

Most research on code completion assumes a "clean prefix" scenario, where the model is asked to finish a logically sound code snippet. Yet, statistics indicate that developers introduce approximately 70 bugs per 1,000 lines of code, and roughly half of development time is spent debugging. When a developer triggers an autocomplete suggestion in an IDE, their "draft" code often contains typos, logical slips, or unrefined implementations.

The authors identify that existing Code-LLMs are not robust to these "potential bugs"—code spans that may not be syntax errors but will cause the final program to fail if the model blindly follows the existing logic. Instead of acting as a corrective partner, the LLM often acts as a "yes-man," propagating the error into the generated completion.

Benchmarking the Buggy Context

To systematically study this phenomenon, the researchers introduced two new datasets:

  • buggy-HumanEval: Created by programmatically altering the popular HumanEval set. The authors generated 1,896 instances by flipping binary operators (e.g., changing + to - or == to !=) within reference solutions.
  • buggy-FixEval: A dataset derived from real-world user submissions on competitive programming platforms. It pairs rejected (buggy) and accepted (fixed) code snippets from the same user, capturing the nuances of realistic coding errors.

Why Models Fail: The Anchor Trap

The evaluation of CodeGen and InCoder variants revealed several key insights into why LLMs struggle with the bCC task:

  • The "Anchor" Trap: LLMs tend to treat the user's prefix as immutable ground truth. Rather than recognizing a logical inconsistency and pivoting to a correct implementation, they "double down" on the error, producing a completion that is consistent with the bug but functionally broken.
  • Worse than Nothing: Remarkably, providing a buggy prefix is often more detrimental than providing no code at all. For CodeGen-2B, the pass rate with no prefix was 9.3%, but it dropped to a staggering 3.1% when a buggy prefix was present.
  • Insensitivity to Detail: In 90% of failed cases, the model produced the exact same completion regardless of whether the prefix was clean or buggy. This suggests that models are largely insensitive to minor but semantically critical changes in the code context.
  • The Poisoning Effect: The location of the bug matters. Models are more likely to recover if the bug is near the end of the prefix. If the error is buried deep in the earlier logic, the model's internal state is effectively "poisoned."

Mitigation Strategies

The researchers tested three "post-hoc" strategies to improve model robustness:

  1. Removal-then-completion: A baseline that deletes the partial code and generates a solution from scratch using only the natural language docstring.
  2. Completion-then-rewriting: The model completes the code normally, and an external "code fixer" model (specifically RealiT) attempts to repair the entire resulting program.
  3. Rewriting-then-completion: This novel approach uses a likelihood-based measure to identify "suspicious" lines in the prefix. By treating potential bugs as outliers in the model's probability distribution, the system uses an infilling model (InCoder-6B) to rewrite the suspicious line before generating the final completion.

Results and Future Directions

The performance degradation on buggy-HumanEval was stark. CodeGen-2B-Mono saw its Pass@1 rate drop from 54.9% with a clean prefix to just 3.1% with a buggy one. While the Rewriting-then-completion method proved the most effective mitigation—boosting CodeGen-2B’s performance back up to 24.9%—a massive 30-percentage-point gap remains compared to clean-code performance.

The study concludes that current post-hoc fixes, while helpful, are computationally expensive and imperfect. Future progress may require a fundamental shift in how Code-LLMs are trained. By exposing models to "noisy" data during training, we can teach them to identify and ignore or fix errors in the prompt. The goal is to move toward AI pair programmers that act less like passive followers and more like helpful, critical editors.

Summary updated

BibTeX
@inproceedings{dinh2023large,
  author = {Tuan Dinh and Jinman Zhao and Samson Tan and Renato Negrinho and Leonard Lausen and Sheng Zha and George Karypis},
  title = {{Large Language Models of Code Fail at Completing Code with Potential Bugs}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2023},
  url = {https://openreview.net/forum?id=e1WgjvFGWp},
}
Figure