Software maintenance, especially locating defects and repairing faulty logic, consumes a large share of development effort and remains error prone when performed manually. This paper presents SmartCode AI, an explainable bug localization and automated repair system that grounds a large language model in retrieved repair knowledge using retrieval augmented generation. The system parses submitted Python code with abstract syntax tree analysis to validate syntax, extract structure and detect code smells such as bare exception handlers, empty functions and mutable default arguments. A lightweight hashing based embedding, computed fully offline, queries a persistent ChromaDB vector store of verified repair patterns, and the retrieved patterns together with the structural facts form a grounded prompt for the Llama-3.3-70B model hosted on Groq. The model returns a bug localization report, a refactoring plan and a corrected program. Every patch is executed in an isolated subprocess with a five second timeout, so that runtime failures and infinite loops are captured before the developer sees the result, and accepted patches are written back to the vector store through a feedback loop. An interactive Streamlit dashboard exposes each stage of the pipeline. Experiments on common runtime defects show that the system localizes and repairs division by zero, unguarded indexing and unsafe dictionary access errors while keeping the host environment safe. By combining static analysis, semantic retrieval, generative repair and sandbox validation, the system reduces debugging effort and delivers transparent fixes that developers can review before adoption.
Introduction
The text presents SmartCode AI, an intelligent automated program repair system designed to identify, explain, and fix errors in Python code. Traditional debugging requires developers to manually locate defects, understand unfamiliar code, and apply corrections, making the process time-consuming and error-prone. Although large language models (LLMs) can generate code repairs, they may produce incorrect solutions, use nonexistent functions, or unintentionally alter program behavior. SmartCode AI addresses these limitations by combining static code analysis, Retrieval-Augmented Generation (RAG), AI-based patch generation, and sandbox execution.
The system is developed using Python 3.10 and Streamlit and consists of four main stages:
Static Code Analysis: Uses Python's Abstract Syntax Tree (AST) module to check syntax, identify functions and classes, and detect code smells such as empty functions, mutable default arguments, and bare exception handlers.
Semantic Retrieval: Uses hashing-based embeddings and a persistent ChromaDB knowledge base to retrieve relevant repair patterns for common errors, including index errors, missing dictionary keys, division by zero, and mutable default arguments.
AI-Based Bug Localization and Repair: Integrates the Llama-3.3-70B model through the Groq API to explain defects, identify their locations, and generate corrected code using retrieved knowledge as supporting evidence.
Sandbox Validation and Feedback: Executes generated patches in isolated subprocesses with a five-second timeout. It captures program output, reports runtime errors, and terminates suspected infinite loops. Verified fixes accepted by developers can be stored in the knowledge base for future use.
The application follows a modular workflow in which submitted code is analyzed, relevant repair examples are retrieved, a corrected program is generated, and the proposed patch is executed before its status is reported. If syntax parsing fails, the system identifies the error and its line number. If the AI service is unavailable, a built-in rule-based explanation provides a fallback.
The literature review discusses traditional search-based repair techniques such as GenProg, learning-based approaches such as SequenceR and CoCoNuT, and modern code language models. It identifies a key research gap: many existing AI repair systems generate plausible patches without adequately explaining their reasoning or verifying that the repaired program executes successfully.
Testing results indicate that the individual modules perform their intended functions. The system correctly identifies supported code smells and syntax errors, retrieves stored repair patterns, separates explanations from generated patches, captures execution results, and prevents infinite loops from running indefinitely. Integration tests also confirm that verified fixes persist in the knowledge base and can be retrieved in subsequent sessions. Parsing and embedding are fast, while remote language-model inference accounts for most of the overall processing time.
Overall, SmartCode AI provides a transparent and practical approach to automated Python debugging by integrating code analysis, knowledge retrieval, AI-generated repairs, and execution-based verification. Its primary contribution is a unified repair pipeline that explains detected defects, presents supporting repair evidence, and tests generated patches before reporting their execution status. This improves debugging efficiency, supports developer understanding, and creates opportunities for continuously improving repair recommendations through accepted fixes.
Conclusion
This paper presented SmartCode AI, a system that combines AST static analysis, vector based semantic retrieval, LLM driven repair and sandbox verification for explainable automated bug repair. The pipeline returns a structural analysis, retrieved repair patterns, a bug localization report, a corrected program and real execution evidence, and a feedback loop stores accepted patches to improve later retrievals. Testing showed that the system repairs common runtime defects and contains unsafe or non-terminating programs without affecting the host application. The use of Python, ChromaDB, a hosted Llama-3.3-70B model and Streamlit keeps the setup cost low and the design modular.
Future work includes support for several programming languages through a language agnostic parser, learned code embeddings such as CodeBERT [14] to retrieve by meaning rather than by lexical overlap, automatic test generation to confirm that the original behaviour is preserved, container based isolation for larger programs, integration with version control platforms to open pull requests, an editor extension, and ranking of several candidate patches by validation confidence.
References
[1] M. Monperrus, \"Automatic software repair: A bibliography,\" ACM Comput. Surv., vol. 51, no. 1, pp. 1–24, 2018.
[2] C. Le Goues, T. Nguyen, S. Forrest, and W. Weimer, \"GenProg: A generic method for automatic software repair,\" IEEE Trans. Softw. Eng., vol. 38, no. 1, pp. 54–72, 2012.
[3] M. Chen et al., \"Evaluating large language models trained on code,\" arXiv:2107.03374, 2021.
[4] C. S. Xia and L. Zhang, \"Less training, more repairing please: Revisiting automated program repair via zero-shot learning,\" in Proc. ACM Joint Eur. Softw. Eng. Conf. Symp. Found. Softw. Eng. (ESEC/FSE), 2022.
[5] C. E. Jimenez et al., \"SWE-bench: Can language models resolve real-world GitHub issues?\" in Proc. Int. Conf. Learn. Represent. (ICLR), 2024.
[6] A. Fan et al., \"Large language models for software engineering: Survey and open problems,\" in Proc. IEEE/ACM Int. Conf. Softw. Eng.: Future Softw. Eng., 2023, pp. 31–53.
[7] P. Lewis et al., \"Retrieval-augmented generation for knowledge-intensive NLP tasks,\" in Proc. Adv. Neural Inf. Process. Syst. (NeurIPS), vol. 33, 2020, pp. 9459–9474.
[8] G. Izacard and E. Grave, \"Leveraging passage retrieval with generative models for open domain question answering,\" in Proc. Conf. Eur. Chapter Assoc. Comput. Linguistics (EACL), 2021, pp. 874–880.
[9] Q. Zhang, C. Fang, Y. Ma, W. Sun, and Z. Chen, \"A survey of learning-based automated program repair,\" ACM Trans. Softw. Eng. Methodol., vol. 33, no. 2, 2024.
[10] Z. Chen, S. Kommrusch, M. Tufano, L.-N. Pouchet, D. Poshyvanyk, and M. Monperrus, \"SequenceR: Sequence-to-sequence learning for end-to-end program repair,\" IEEE Trans. Softw. Eng., vol. 47, no. 9, 2021.
[11] T. Lutellier, H. V. Pham, L. Pang, Y. Li, M. Wei, and L. Tan, \"CoCoNuT: Combining context-aware neural translation models using ensemble for program repair,\" in Proc. ACM SIGSOFT Int. Symp. Softw. Test. Anal. (ISSTA), 2020, pp. 101–114.
[12] H. Ye, M. Martinez, and M. Monperrus, \"Neural program repair with execution-based backpropagation,\" in Proc. Int. Conf. Softw. Eng. (ICSE), 2022.
[13] Y. Wang, W. Wang, S. Joty, and S. C. H. Hoi, \"CodeT5: Identifier-aware unified pre-trained encoder-decoder models for code understanding and generation,\" in Proc. Conf. Empirical Methods Natural Lang. Process. (EMNLP), 2021, pp. 8696–8708.
[14] Z. Feng et al., \"CodeBERT: A pre-trained model for programming and natural languages,\" in Findings Assoc. Comput. Linguistics: EMNLP, 2020, pp. 1536–1547.
[15] N. Jiang, K. Liu, T. Lutellier, and L. Tan, \"Impact of code language models on automated program repair,\" in Proc. Int. Conf. Softw. Eng. (ICSE), 2023.