Coding in a Bubble? Evaluating LLMs in Resolving Context Adaptation Bugs during Code Adaptation

Tanghaoran Zhang, Xinjun Mao, Shangwen Wang, Yuxin Zhao, Yao Lu, Zezhou Tang, Wenyu Xu, Longfei Sun, Changrong Xie, Kang Yang, Yue Yu

TL;DR

While Large Language Models (LLMs) excel at writing code in isolation, they suffer from a critical blind spot when adapting existing code to new environments. This paper introduces CtxBugGen, a framework that automatically generates "Context Adaptation Bugs" (CtxBugs)—errors where code is locally perfect but contextually wrong. Evaluating state-of-the-art LLMs reveals that these subtle bugs degrade model performance by up to 30%, as LLMs consistently overlook them and confidently replicate the errors.

What Problem Does This Paper Address?

Code reuse is a cornerstone of modern software engineering, but code copied from one project rarely fits perfectly into another without modification. When developers or AI assistants integrate bug-free code into a new environment without proper adjustments, they introduce Context Adaptation Bugs (CtxBugs).

Unlike standalone syntax or logic errors, CtxBugs are deviously subtle: the code compiles, runs, and looks entirely correct on its own, but it violates the broader constraints of the target environment.

An example of a CtxBug where an LLM uses an arithmetic plus (+) instead of a contextually required bitwise OR (|).

As shown in Figure 1, an LLM might reuse code that uses a standard addition operator (+) to combine status values. While numerically logical, this creates a critical failure if the target context explicitly mandates bitwise operations (|). Because LLMs tend to evaluate code "in a bubble," prioritizing local correctness over global context, the authors sought to systematically evaluate and quantify this limitation.

Methods

To rigorously test LLMs, the authors developed CtxBugGen, a novel framework designed to intentionally trick LLMs into generating context-blind code, which is then turned into an evaluation benchmark.

The four-step benchmark construction process of the CtxBugGen framework.

The framework operates via a four-step pipeline (Figure 3):

  1. Adaptation Task Selection: The authors extracted four real-world adaptation categories from existing software engineering literature: Interface Specification, Functionality Customization, Identifier Reference, and Dependency Constraint.
  2. Task-Specific Perturbation: They defined 10 rules to replace specific code elements (like constants, operators, or variable names) with placeholders (<INFILL>, <PARAMS>, etc.) using Abstract Syntax Trees (ASTs).
  3. LLM-Based Variant Generation: They prompted LLMs to fill in these placeholders without providing the target context. This forced the models to generate code that was plausible in isolation but context-free. Crucially, they used code obfuscation (renaming variables to generic tokens like var_0) to prevent the models from simply regurgitating memorized training data.
  4. CtxBug Identification: The generated variants were de-obfuscated and run against the target context's test suite. Code that failed the tests but matched the original solution everywhere except the perturbed location was flagged as a valid CtxBug.

Datasets

CtxBugGen was applied to ClassEval, a widely used benchmark for class-level Python code generation. Using four frontier models (GPT-4o, DeepSeek-V3, Qwen3-Coder-Plus, and Kimi-K2) as bug generators, they constructed a final benchmark containing 3,683 unique CtxBugs.

For a fair evaluation, they also created two baseline datasets:

  • Code Without CtxBugs: The buggy sections were replaced with blank placeholders, forcing the LLM to write code from scratch using the context.
  • Code With Isolated Bugs (IsoBugs): Traditional, locally incorrect bugs were injected at the exact same positions to compare how models handle local vs. contextual errors.

Key Insights and Ideas

  • The "Bubble" Bias: LLMs suffer from an over-reliance on local semantic harmony. If a line of code looks clean and matches the local variable names, the LLM assumes it is correct, completely ignoring conflicting constraints elsewhere in the file.
  • High-Confidence Blindness: When LLMs replicate a CtxBug, they don't do it hesitantly. Token probability analysis showed that models frequently assign a ~100% probability to the incorrect token.
A motivating example showing how an LLM is uniquely misled by a CtxBug compared to an isolated bug or a blank placeholder.
  • Self-Blindness: A fascinating finding was that every evaluated LLM performed worst on the benchmark data it generated itself. Models are uniquely susceptible to their own flavor of plausible, context-free logic.
  • CtxBugs > Isolated Bugs: LLMs are significantly better at fixing obvious, isolated bugs than resolving CtxBugs. Isolated bugs break local logic, triggering the model's internal "repair" mechanisms, whereas CtxBugs blend in perfectly.

Key Results

The authors evaluated four state-of-the-art models: GPT-4o, DeepSeek-V3, Qwen3-Coder-Plus, and Kimi-K2.

  • Disappointing Resolution Rates: The best-performing model, Kimi-K2, only achieved a 55.93% Pass@1 score and successfully resolved just 52.47% of the CtxBugs.
  • Performance Degradation: The presence of a CtxBug degraded adaptation performance by up to 30% compared to settings where the model was given a blank placeholder (<INFILL>). The buggy code acts as a powerful distractor.
  • Replication in Failure: In nearly 60% of all failure cases, the models completely overlooked the CtxBug and copied it directly into their output. The average token probability for these overlooked buggy generations was a staggering 0.983.
  • Hardest Tasks: Functionality Customization (e.g., adjusting internal logic like conditionals) was the most difficult category, with resolution rates dropping below 30% for all models.

Limitations and Future Work

The primary limitation is that the benchmark is currently restricted to Python and class-level contexts via ClassEval. It does not yet explore multi-language environments or broader system-level architectures (e.g., cross-file or cross-repository dependencies).

To bridge this gap, the authors suggest that future research should focus on:

  1. Cross-context fine-tuning strategies specifically targeting code adaptation.
  2. Advanced prompting techniques like Chain-of-Thought (CoT) or self-reflection to force models to explicitly list contextual constraints before writing code.
  3. Hybrid architectures that pair LLMs with traditional static analysis tools to flag semantic mismatches before the LLM generates its final answer.

Summary updated

BibTeX
@article{zhang2026coding,
  author = {Tanghaoran Zhang and Xinjun Mao and Shangwen Wang and Yuxin Zhao and Yao Lu and Zezhou Tang and Wenyu Xu and Longfei Sun and Changrong Xie and Kang Yang and Yue Yu},
  title = {{Coding in a Bubble? Evaluating LLMs in Resolving Context Adaptation Bugs during Code Adaptation}},
  journal = {Proc. ACM Softw. Eng.},
  year = {2026},
  doi = {10.1145/3797148},
}
Figure