Introduction
Large Language Models (LLMs) like GPT-4 have revolutionized code generation, yet they frequently struggle with project-level tasks. In real-world software development, code is rarely isolated; it depends on a complex web of internal APIs, custom classes, and specific data structures defined across a repository. Due to limited context windows, LLMs cannot ingest an entire codebase at once, leading to hallucinations of non-existent APIs or the use of standard library functions where project-specific ones are required.
The COCOGEN Framework
To address these challenges, we introduce COCOGEN, a framework that leverages compiler feedback to iteratively refine the context provided to an LLM. Instead of relying solely on initial retrieval, COCOGEN treats compilation errors as precise signals for missing information.
The COCOGEN pipeline follows a "Generate-Verify-Retrieve-Fix" loop:
- Project Indexing: The repository is parsed into an Abstract Syntax Tree (AST) to create a structured database of cross-file relationships.
- Initial Generation: The LLM produces a first draft based on the user's prompt.
- Compiler Feedback: A static analyzer (e.g.,
pylint) identifies errors, such as "Undefined Symbol." - Hybrid Retrieval: COCOGEN uses the error message to perform a Structural Search (via SQL/CodeQL queries on the AST) and a Semantic Search (via vector embeddings) to find the exact missing definitions.
- Iterative Refinement: The model uses the new context to fix the code, repeating until success or a maximum iteration limit is reached.
Key Insights
A core finding of this work is that compiler errors are features, acting as specific "information requests" that guide retrieval more effectively than vague natural language prompts. Furthermore, the study highlights that structural search—finding exact definitions within the project hierarchy—is significantly more effective for resolving compilation failures than traditional semantic search.
Evaluation and Results
COCOGEN was evaluated on benchmarks including CoderEval and CrossCodeEval, which focus on pragmatic, project-level code generation.
- Performance Gains: COCOGEN achieved an 80% relative improvement in pass rates for GPT-3.5-Turbo on project-level tasks compared to direct generation.
- Superiority over Baselines: It consistently outperformed state-of-the-art methods like RepoCoder, particularly in scenarios with heavy cross-file dependencies.
- Efficiency: Most errors were resolved within one or two iterations, with static analysis and structural queries averaging just 1.27 seconds.
Limitations and Future Directions
While highly effective for compilation errors, COCOGEN is currently less suited for logic errors that require dynamic execution logs. Future work aims to integrate execution feedback and expand support to strictly typed languages like Java and C++, further bridging the gap between AI-generated code and production-ready software.
Summary updated