NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

ROCODE: Integrating Backtracking Mechanism and Program Analysis in Large Language Models for Code Generation

Xue Jiang, Yihong Dong, Yongding Tao, Huanyu Liu, Zhi Jin, Ge Li

TL;DR

ROCODE is a model-agnostic framework that introduces a "backspace" key to the code generation process of Large Language Models (LLMs). By integrating real-time program analysis and a backtracking mechanism, it detects errors as they occur, rolls the model back to a safe state, and regenerates code with constraints to avoid repeating past mistakes.

What Problem Does This Paper Address?

Standard LLMs generate code auto-regressively—one token at a time. Because each new token depends on the previous ones, a single "hallucinated" or incorrect token can derail the entire program. Current solutions usually involve "post-revising," where the model generates a full script and then tries to fix it after a failed test run.

The authors argue this is inefficient. Post-revising often struggles to fix deeply rooted, accumulated errors and wastes significant computational resources (tokens) on code that is fundamentally broken from the start. ROCODE aims to solve this by catching errors during the generation process, preventing error propagation before it starts.

Methods

ROCODE operates through a three-step cycle integrated into a Trie Tree structure to manage non-linear generation paths:

  1. Incremental Error Detection: Instead of waiting for the end, ROCODE uses a compiler to check the code statement-by-statement. It looks for syntax errors, type mismatches, and "repetition problems" (where the model gets stuck in a loop).
  2. Strategic Rollback: When an error is found, ROCODE must decide how far to go back. It first tries rolling back to the specific line reported by the compiler. If the error persists, it uses entropy-based analysis to find the point where the model was most "uncertain" and rolls back to that likely root cause.
  3. Constraint Regeneration: To ensure the model doesn't just make the same mistake again, ROCODE applies an exponentially decaying penalty to the tokens that led to the previous failure. This "soft" constraint discourages the bad path without completely blocking potentially valid tokens.
The Overview of ROCODE with Trie Tree illustrates the detection, rollback, and regeneration cycle.

Datasets

The researchers evaluated ROCODE across several rigorous benchmarks:

  • HumanEval & MBPP: The industry standards for Python code generation.
  • HumanEval-ET & MBPP-ET: Expanded versions of the above with 100+ additional edge-case test cases per task to ensure functional robustness.
  • CodeForces2305: A challenging dataset of competition-level problems released after the training cutoff of most models, minimizing the risk of "data contamination" (the model having seen the answers during training).
  • HumanEval-CPP: Used to test the framework's effectiveness on C++ tasks.

Key Insights and Ideas

  • Uncertainty as a Compass: One of the most clever insights is using token entropy to identify rollback points. High entropy means the model was "guessing" between multiple options. If an error occurs later, that moment of high uncertainty is often the true branching point where the model took the wrong turn.
  • Compilers are Faster than LLMs: By offloading error detection to a traditional compiler, ROCODE gains a "ground truth" check that is computationally cheap compared to running another LLM pass for debugging.
  • Model Agnosticism: Because ROCODE works at the decoding level (manipulating token probabilities), it can be "plugged in" to almost any existing LLM without needing to retrain or fine-tune the model itself.

Key Results

ROCODE demonstrated significant improvements across the board:

  • Near-Perfect Compilation: It achieved a 99.1% compilation pass rate, effectively eliminating syntax errors.
  • Functional Accuracy: The test pass rate improved by up to 23.8% compared to the best baseline methods.
  • Efficiency: Compared to post-revising methods, ROCODE reduced token costs by 19.3%, proving that fixing errors early is cheaper than fixing them late.
  • Universal Scaling: The performance gains were consistent across nine different models, including the Llama and CodeGen series. Interestingly, the "stronger" the base model, the more it benefited from ROCODE's backtracking.
Performance of ROCODE across different LLMs shows consistent gains over standard temperature sampling.

Limitations and Future Work

While highly effective, ROCODE has some acknowledged limitations:

  • Logic Errors: While excellent at catching syntax and type errors, it may still struggle with deep logical flaws that don't trigger a compiler error or a runtime crash during incremental checks.
  • Inference Latency: Calling a compiler and recalculating probabilities adds a small time overhead compared to raw, unconstrained generation.
  • API Constraints: The method requires access to token-level log-probabilities, which might not be available through some restricted commercial LLM APIs.

Future work could involve integrating more sophisticated dynamic analysis (like symbolic execution) to catch subtle logic bugs during the generation phase.

Summary updated

BibTeX
@inproceedings{jiang2025rocode,
  author = {Xue Jiang and Yihong Dong and Yongding Tao and Huanyu Liu and Zhi Jin and Ge Li},
  title = {{ROCODE: Integrating Backtracking Mechanism and Program Analysis in Large Language Models for Code Generation}},
  booktitle = {ICSE},
  year = {2025},
  url = {https://openreview.net/forum?id=axG7ff1hbh},
}
Figure