TL;DR
This paper introduces SEMANTICFORGE, a novel framework that tackles systematic errors in AI-generated code by leveraging semantic knowledge graphs and constraint satisfaction. It significantly reduces logical and schematic hallucinations, leading to more reliable and accurate code generation for entire repositories.
What Problem Does This Paper Address?
Modern AI code generation tools, like GitHub Copilot and Code-Llama, are powerful but often produce code with subtle, systematic errors. These errors fall into two main categories: logical hallucination (incorrect control/data flow reasoning) and schematic hallucination (type mismatches, signature violations, architectural inconsistencies). These issues arise because current models lack a deep understanding of the entire repository's semantics, relying instead on local context or pattern matching. This limits their practical deployment, especially for complex, multi-file tasks.
Methods
SEMANTICFORGE tackles this problem with a four-stage pipeline:
- Dual Static-Dynamic Knowledge Graphs: It constructs a comprehensive knowledge graph by merging information from static code analysis (like ASTs) and dynamic execution traces (from tests). This provides a richer, more accurate representation of repository semantics than either method alone.
- Neural Query Planner: Instead of fixed retrieval strategies, it uses a learned neural network to translate natural language instructions into graph queries. This allows the system to intelligently select the most relevant parts of the knowledge graph for a given task.
- SMT-Integrated Beam Search: This is a core innovation. It integrates a Satisfiability Modulo Theories (SMT) solver directly into the code generation beam search process. This allows for real-time verification of generated code against semantic constraints, pruning invalid paths during generation, rather than as a post-hoc check.
- Continual Maintenance Agent: To keep the knowledge graph up-to-date with evolving codebases, an incremental update algorithm is used, ensuring semantic equivalence with full reconstruction at a much lower computational cost.
Datasets
The paper introduces REPOKG-50, a new benchmark dataset specifically designed for repository-level code generation. It comprises 4,250 tasks across 50 Python projects, with detailed annotations including natural language instructions, ground truth implementations, semantic annotations, and difficulty ratings. This dataset aims to address limitations of existing benchmarks that focus on isolated function synthesis.
Key Insights and Ideas
- Explicit Semantics are Crucial: The paper strongly argues that relying solely on implicit patterns learned by LLMs is insufficient. Explicit, queryable representations of repository-wide semantics (like knowledge graphs) are essential for robust code generation.
- Constraint Satisfaction is Key to Reliability: Integrating constraint checking during generation (SMT-guided beam search) is far more effective than post-hoc validation for eliminating schematic errors and improving overall code quality.
- Static + Dynamic is Better Than Either Alone: Combining static analysis with dynamic execution traces provides a more complete picture of program behavior, especially for resolving ambiguities in polymorphic code.
- Learned Context Selection Outperforms Fixed Retrieval: A neural planner that learns to query the knowledge graph is more effective at identifying relevant context than traditional keyword or embedding-based methods.
Key Results
- Improved Functional Correctness: SEMANTICFORGE achieved 49.8% Pass@1, an 18.1% absolute improvement over the best baseline (CodePlan at 42.3%).
- Dramatic Hallucination Reduction:
- Schematic Hallucination: Reduced by 52% compared to GPT-4-Code.
- Logical Hallucination: Reduced by 31% compared to RAG-Code.
- Efficiency: Achieved a 2.4s average latency for generation, comparable to baseline LLMs, while providing formal guarantees.
- Scalability: The system scales effectively to repositories up to 500K lines of code.
- Cross-Repository Generalization: Showed strong transfer capabilities, with only a 4.3% average performance degradation across different architectural patterns.
Limitations and Future Work
- Algorithmic Reasoning: The system still struggles with complex logical problems requiring multi-step reasoning or novel algorithmic insights, as it inherits limitations from the underlying LLMs.
- Creative Design: The emphasis on consistency might limit its ability to propose truly innovative architectural designs.
- Multi-Language Support: The current work focuses on Python; extending to other languages (Java, JavaScript, etc.) is a significant future direction.
- Resource Requirements: The system requires substantial computational resources, which might be a barrier for some users.
- Test Suite Dependency: Effectiveness relies heavily on the quality and coverage of existing test suites.
- Ethical Considerations: Concerns around bias amplification, IP protection, and developer skill evolution are acknowledged.
Future work includes extending to multi-language support, improving algorithmic reasoning, enabling more creative design exploration, and integrating security and performance optimization into the generation process.
Summary updated