CoCoNUT: Structural Code Understanding does not fall out of a tree

Claas Beger, Saikat Dutta · DOI

Abstract

While Large Language Models (LLMs) have achieved near-saturation on classic code generation benchmarks, their ability to reason about the internal logic and execution flow of programs remains a critical open question. This paper introduces CoCoNUT (Code Control Flow for Navigation Understanding and Testing), a benchmark designed to evaluate the "mental execution" capabilities of state-of-the-art models. By requiring models to trace the exact line-by-line execution path of Python programs, we demonstrate a significant gap between code generation and structural understanding. Our results show that even top-tier models like Gemini 1.5 Pro and GPT-4o struggle with execution traces exceeding 25 lines and fail almost entirely on advanced concepts like recursion and concurrency.

Introduction

We are currently witnessing an era where LLMs are becoming expert code generators, frequently achieving over 90% accuracy on benchmarks like HumanEval and MBPP. However, these metrics primarily focus on the final output or docstring-to-code translation. They do not necessarily indicate whether a model understands the "plumbing" of the code it produces—the loops, conditional jumps, and nested function calls that constitute program logic.

If an LLM cannot accurately navigate a program's control flow, it likely lacks the deep reasoning required for complex debugging, formal verification, or the maintenance of large-scale software systems. The CoCoNUT benchmark was developed to test this specific hypothesis: Does the ability to generate code imply a structural understanding of how that code actually runs?

The CoCoNUT Benchmark

The CoCoNUT benchmark shifts the focus from output to execution. Given a source code snippet and a specific input, a model must output the exact sequence of line numbers executed by the program. The benchmark is divided into two primary components:

  1. HumanEval-Trace: This dataset utilizes canonical solutions to HumanEval tasks. Researchers instrumented these solutions using Python’s sys library to generate ground-truth execution traces for various test inputs, resulting in 1,083 unique traces across 161 tasks.
  2. Advanced-Trace: To test the limits of LLM reasoning, the authors curated 124 tasks (620 traces) covering complex programming paradigms often omitted from simpler benchmarks, including recursion, object-oriented programming (OOP) with inheritance/polymorphism, and parallel processing (concurrency).

To ensure the tasks remained within model context windows while maintaining difficulty, traces were filtered to stay under 1,024 tokens but varied significantly in complexity, from linear execution to deep loops.

Experimental Insights

The Generation-Understanding Gap

One of the most striking findings is the weak correlation between a model's ability to write a solution and its ability to trace it. A model might pass 80% of HumanEval tests in a generation context but correctly trace only 40% of those same programs. This suggests that LLMs often rely on pattern matching for generation rather than a robust internal model of execution.

The "Trace Length" Wall

Model performance is highly sensitive to the length of the execution path. While LLMs are relatively proficient at tracing short sequences (1–10 lines), accuracy drops precipitously once a trace exceeds 25 lines. For traces longer than 40 lines, almost all tested models failed to produce a 100% correct sequence. This "wall" indicates a fundamental difficulty in maintaining a virtual program counter over extended horizons.

Failure Modes in Mental Execution

The researchers identified several recurring "mental bugs" in LLM reasoning:

  • Statement Skip: The model correctly evaluates a condition but fails to execute the subsequent line.
  • Else Call: The model mistakenly enters an else block even when the preceding if condition was satisfied.
  • Def Call/Skip: Models struggle with the "jump" required to enter an internal function definition or a separate class method.

The Advanced Concept Ceiling

On the Advanced-Trace subset, the results were stark. For tasks involving recursion, OOP, and concurrency, almost no model achieved over 5% task accuracy. This suggests that the structural reasoning required for these paradigms is currently beyond the reach of general-purpose LLMs.

Results and Model Comparison

Gemini 1.5 Pro emerged as the strongest performer in the study, yet it only achieved a full correctness rate of 47% on the HumanEval-Trace tasks. While larger models like GPT-4o and Claude 3.5 Sonnet significantly outperformed smaller models such as Llama 3.1 8B, all models hit a performance ceiling as complexity increased.

Furthermore, the study found that Chain-of-Thought (CoT) prompting is not a universal solution for execution tracing. While CoT often helps with mathematical reasoning, it provided limited benefits for larger models in this context and occasionally degraded performance by introducing more opportunities for hallucination during the step-by-step trace.

Conclusion

The CoCoNUT benchmark highlights a critical deficiency in current AI coding assistants: they can write code they cannot "read." This lack of structural literacy limits their utility in high-stakes software engineering tasks like debugging and verification. Future work will involve expanding CoCoNUT to other languages such as C++ and Rust, and exploring whether fine-tuning models specifically on execution traces can bridge the gap between code generation and true structural understanding.

Summary updated

BibTeX
@inproceedings{beger2025coconut,
  author = {Claas Beger and Saikat Dutta},
  title = {{CoCoNUT: Structural Code Understanding does not fall out of a tree}},
  booktitle = {LLM4Code@ICSE},
  year = {2025},
  doi = {10.1109/llm4code66737.2025.00021},
}
Figure