Introduction
Large Language Models (LLMs) have demonstrated impressive capabilities in code generation and translation. However, translating code at the repository level remains a significant challenge. Unlike isolated snippets, repository-level code is deeply embedded in a web of dependencies and project-specific conventions. Standard translation approaches often fail to maintain functional correctness due to a lack of contextual awareness.
The Challenge: Context Blindness and Static Knowledge
Existing translation tools frequently encounter two primary obstacles:
- Dependency Misuse: Models often fail to correctly invoke libraries or variables because they lack visibility into how these dependencies are used elsewhere in the target project.
- Static Knowledge Bases: Traditional Retrieval-Augmented Generation (RAG) relies on fixed databases. In incremental translation, the optimal way to translate a module often depends on how previous, related modules were handled.
The K³Trans Framework
To address these issues, we introduce K³Trans, a framework that augments LLMs with "triple knowledge": dependency usage examples, target-language code samples, and prior successful translation pairs.
1. Triple Knowledge Augmentation
K³Trans retrieves three specific types of context for each translation task:
- Dependency Usage Examples: Demonstrates how specific libraries or variables are invoked within the target repository.
- Target-Language Code Samples: Provides idiomatic examples from large-scale open-source projects (e.g., Rust code from GitHub).
- Successful Translation Function Pairs: Leverages historical translation data to ensure consistency.
2. Evolving Knowledge Base
The core innovation of K³Trans is its evolving knowledge base. As the system successfully translates functions, it dynamically updates its internal repository. This "self-evolution" allows the model to learn from its own successful outputs, ensuring that subsequent translations are consistent with the established project structure.
3. Retrieval and Self-Debugging
The framework employs a two-stage retrieval process using BM25 for initial keyword matching and UniXcoder for semantic re-ranking. Furthermore, it incorporates a self-debugging mechanism where compilation errors are fed back into the LLM for one-shot repair, significantly increasing the repairable ratio of the generated code.
Key Results and Insights
K³Trans was evaluated using the RustRepoTrans benchmark, which includes 375 tasks translating C, Java, and Python into Rust.
- Functional Correctness: K³Trans achieved a Pass@1 improvement of up to 135.9% over baseline models like Claude-3.5-Sonnet and DeepSeek-V3.
- Consistency: Performance improved linearly as the knowledge base grew, validating the importance of the evolving knowledge component.
- Superiority over Rule-Based Tools: When compared to C2Rust, a traditional rule-based transpiler, K³Trans successfully handled architectural divergences (e.g., C pointers to Rust memory safety) that rule-based systems could not map.
Conclusion
K³Trans demonstrates that providing specific, evolving context is crucial for repository-level code translation. By bridging the gap between isolated code generation and project-wide integration, K³Trans sets a new standard for automated software migration. Future work will focus on expanding language support and handling complex dependency refactoring.
Summary updated