TL;DR
This paper introduces CodePlan, a novel framework that uses Large Language Models (LLMs) and planning to automate complex, repository-wide code modifications. By intelligently planning a sequence of LLM-driven edits and considering code dependencies, CodePlan significantly outperforms existing methods in tasks like package migration and temporal code edits.
What Problem Does This Paper Address?
Modern software development often involves large-scale code changes that span entire repositories, such as migrating to new libraries, refactoring code, or fixing complex bugs. Existing LLM-based coding tools, like GitHub Copilot, excel at localized edits (e.g., completing a line or a function). However, they struggle with repository-level tasks because the entire codebase is too large to fit into an LLM's context window, and code dependencies across files are crucial for making correct, cascading changes. Manually managing these inter-dependent edits is time-consuming and error-prone. This paper addresses the challenge of automating these complex, multi-step, repository-wide coding tasks.
Methods
CodePlan tackles repository-level coding as a planning problem. Its core is a task-agnostic framework that synthesizes a multi-step "plan" of edits. Key components include:
- Incremental Dependency Analysis: Tracks syntactic and semantic relationships between code blocks within the repository.
- Change May-Impact Analysis: Identifies which parts of the codebase are likely to be affected by a given code change, going beyond simple build errors to detect subtle behavioral shifts.
- Adaptive Planning Algorithm: Dynamically builds a plan graph of code edit obligations, where each node represents an LLM call and edges represent dependencies. This plan is extended as new edits are made and their impacts are analyzed.
- LLM Integration: Each step in the plan involves calling an LLM with a carefully constructed prompt that includes task instructions, temporal context (previous edits), spatial context (related code blocks), and the code fragment to be edited.
- Oracle-Guided Iteration: After a plan is executed, an oracle (e.g., a build system or static checker) validates the repository. If errors are found, they become new seed specifications for the next iteration of planning and execution.
Datasets
The authors evaluated CodePlan on two main types of tasks using diverse datasets:
- C# Migration Task: Migrating from a legacy logging framework to a modern one. This involved internal proprietary repositories (Int-1, Int-2) and external public GitHub repositories (Ext-1).
- Temporal Edits Task (Python): Orchestrating sequences of code changes based on initial edits. This used external public GitHub repositories (T-1, T-2, T-3).
The datasets varied in size, number of files, and complexity of changes, providing a comprehensive evaluation.
Key Insights and Ideas
- Planning is Crucial for Repository-Level Tasks: Simply calling an LLM repeatedly without a plan fails to manage inter-dependencies, leading to incomplete or incorrect changes. CodePlan's planning approach is key to its success.
- Beyond Build Errors: Relying solely on build errors (like in baseline methods) is insufficient. Many changes can cause behavioral issues without breaking the build, and build systems can misattribute errors. CodePlan's "change may-impact analysis" is vital for detecting these subtle, yet critical, changes.
- Temporal and Spatial Contexts are Essential: Omitting temporal (history of changes) and spatial (code structure) contexts leads to a significant increase in missed edits and spurious blocks. LLMs need this information to understand the evolution and interconnections of the codebase.
- Incremental Analysis Preserves Relationships: CodePlan's incremental approach to updating the dependency graph ensures that relationships are maintained even as code evolves, preventing loss of context that static snapshots might suffer.
Key Results
CodePlan demonstrated superior performance compared to baseline methods like "Build-Repair" and "Pyright-Repair":
- High Accuracy: For C# migration on internal repositories, CodePlan achieved 100% "Matched Blocks" and a perfect Diff BLEU score of 1.00 after two iterations, indicating it correctly identified and modified all necessary code blocks.
- Reduced Errors: CodePlan achieved 0 errors in validity checks for 5 out of 6 repositories, while baselines failed to get any repositories to pass.
- Efficiency: CodePlan significantly reduced the number of missed and spurious blocks compared to baselines. For example, on Int-1, Build-Repair missed 69 blocks and introduced 13 spurious ones, while CodePlan missed 0 and introduced 0.
- Temporal Edits: For Python temporal edits, CodePlan successfully identified derived edit locations in 2 out of 3 repositories, outperforming Pyright-Repair which failed in 2 out of 3.
Limitations and Future Work
- Dynamic Dependencies: The current implementation primarily relies on static analysis. Handling dynamic dependencies (data flow, complex dispatching, multi-threading) is a significant area for future work.
- Language Support: While C# and Python are supported, extending to other languages requires creating dependency graphs with relevant relations.
- Artifacts Beyond Code: Future work aims to include other artifacts like configuration files, metadata, and external dependencies in the editing process.
- LLM Reliability: The system trusts LLM responses for change-may-impact analysis. Incorrect or spurious LLM responses can lead to errors, though iterative refinement helps.
- Single Block Updates: The current strategy focuses on updating one block at a time. Exploring simultaneous multi-block updates could be more efficient.
- Few-Shot Learning: Extending from zero-shot prompting to few-shot examples, Chain of Thought (CoT), and other techniques is a promising research direction.
Summary updated