RepoAgent: An LLM-Powered Open-Source Framework for Repository-level Code Documentation Generation

Qinyu Luo, Yining Ye, Shihao Liang, Zhong Zhang, Yujia Qin, Yaxi Lu, Yesai Wu, Xin Cong, Yankai Lin, Yingli Zhang, Xiaoyin Che, Zhiyuan Liu, Maosong Sun

Abstract

RepoAgent is an open-source framework designed to automate the generation and maintenance of high-quality, repository-level code documentation. By leveraging global project structures and integrating directly with Git hooks, it ensures that documentation is contextually rich and synchronized with code changes in real-time.

Introduction

Software developers spend over half their time on program comprehension, a task significantly aided by high-quality documentation. However, maintaining documentation is labor-intensive and often neglected. Existing automated tools frequently suffer from three major flaws: poor summarization of repository-wide interactions, inadequate guidance regarding practical usage and edge cases, and passive update mechanisms that result in "stale" documentation. RepoAgent addresses these issues by treating documentation as a living, repository-wide entity rather than a collection of static, isolated comments.

Methodology

The RepoAgent framework operates through a three-stage pipeline designed to capture the full context of a codebase.

  1. Global Structure Analysis: RepoAgent utilizes Abstract Syntax Tree (AST) parsing to construct a comprehensive "Project Tree." By employing the Jedi library, it maps bidirectional reference relationships—identifying both "Callers" (who uses the code) and "Callees" (what the code uses). This creates a Directed Acyclic Graph (DAG) representing the entire repository.
  2. Documentation Generation: Documentation is generated following a bottom-to-top topological order. RepoAgent prompts a Large Language Model (LLM), such as GPT-4, with the project hierarchy, specific code snippets, reference relationships, and metadata. This bottom-up approach allows the model to utilize previously generated documentation of child nodes as auxiliary context for parent nodes.
  3. Documentation Update: To prevent documentation from becoming outdated, RepoAgent integrates with Git pre-commit hooks. When a developer stages changes, the tool automatically detects modified objects, identifies their bidirectional references, and updates only the affected documentation.
The RepoAgent pipeline: Global Structure Analysis, Documentation Generation, and Documentation Update.

Key Insights and Evaluation

A core innovation of RepoAgent is the use of bidirectional context. Understanding who calls a function is often as vital for documentation as knowing what the function does internally. This "caller" context enables the LLM to explain the purpose and impact of code within the broader system. Furthermore, the topological generation strategy ensures that complex high-level functions are documented using the refined descriptions of the low-level functions they depend on.

An example of RepoAgent detecting a code modification and automatically updating the corresponding documentation.

The framework was evaluated across nine diverse Python repositories, including established projects like simdjson and LLM-based agent projects like AutoGPT. In blind human evaluations, RepoAgent-generated documentation was preferred over human-authored documentation by a significant margin: 70% for the Transformers repository and 91.33% for LlamaIndex. Benchmarking revealed that GPT-4-0125 is currently the gold standard for this task, excelling at format adherence and parameter identification compared to smaller open-source models.

Conclusion and Future Work

RepoAgent demonstrates that LLMs can significantly outperform traditional documentation methods by leveraging repository-level structure. While currently limited to Python due to its reliance on the Jedi library, future work aims to expand language support and introduce RAG-based interfaces for natural language querying of codebases. Additionally, addressing security concerns regarding proprietary code and remote LLM APIs remains a priority for enterprise adoption.

Summary updated

BibTeX
@misc{luo2024repoagent,
  author = {Qinyu Luo and Yining Ye and Shihao Liang and Zhong Zhang and Yujia Qin and Yaxi Lu and Yesai Wu and Xin Cong and Yankai Lin and Yingli Zhang and Xiaoyin Che and Zhiyuan Liu and Maosong Sun},
  title = {{RepoAgent: An LLM-Powered Open-Source Framework for Repository-level Code Documentation Generation}},
  year = {2024},
  eprint = {2402.16667},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2402.16667},
}
Figure