Mistral AI Legacy Code Modernization
Mistral AI successfully migrated 40,000 lines of a physics-intensive reservoir simulator from Fortran 77 to C++ for a European energy operator. This project demonstrates that modernizing complex legacy systems requires moving beyond simple syntax translation to architectural refactoring, supported by numerical verification and structured human-in-the-loop agent workflows.
Establishing Numerical Parity for Verification
To ensure the migrated C++ codebase matched the legacy Fortran 77 system, Mistral implemented a parity harness before beginning the migration. Because structural differences between procedural Fortran and object-oriented C++ prevent line-for-line correspondence, numerical equality of outputs is the only reliable proof of correctness.
The parity harness consisted of:
- State Export Subroutines: Added to the Fortran codebase to dump state snapshots.
- C++ Test Framework: Designed to load these checkpoints and verify the output of migrated modules.
- Skill.md Files: Used to guide AI agents in utilizing the harness correctly.
By inserting lines into the Fortran code to export specific variables (e.g., RHOG), the team could use those values as reference checkpoints to validate the migrated C++ modules.
Documenting Legacy Code via Agentic Trees
Legacy codebases often suffer from fragmented documentation stored in old PDFs and inline comments. Mistral addressed this by treating the procedural Fortran program as a single caller-callee tree.
Using a custom parser to generate the tree, Mistral deployed over a hundred agents via the Vibe CLI. These agents utilized Mistral OCR and document libraries to ingest relevant PDFs and document the codebase. The process followed a bottom-up approach: agents started at the leaves of the tree and worked upward, with each node spawning a subagent to document the code and open a Pull Request (PR). A reviewer agent running on a cron schedule monitored these PRs and scheduled fix tasks as needed.
Evolution of AI Agent Workflows for Migration
Mistral tested three different levels of autonomy for the migration process, finding that a structured, human-augmented workflow provided the best balance of quality and efficiency.
Fully Autonomous Translation
In the first attempt, agents were given full autonomy to translate subroutines independently. This resulted in "Fortran retyped in C++ syntax," where COMMON blocks became global structs and GOTO-driven control flow remained intact, failing to achieve true architectural modernization.
Structured Agent Teams
The second attempt utilized a team of agents (planner, coder, tester, and code quality reviewer) per module. While code quality improved, the agents frequently stalled when encountering complex bugs without human intervention.
Human-in-the-Loop Structured Workflow
The final successful approach involved a human operating a workflow of coder, tester, and reviewer agents. The codebase was divided into independent modules (subtrees of less than 10,000 lines of Fortran). Each module followed this sequence:
- Architecture Generation: Generate the target C++ architecture.
- Engineering Review: Review the architecture with a reservoir engineer.
- Task Queuing: Break the approved architecture into a task queue.
- Implementation Loop: Execute a sub-workflow of plan $\rightarrow$ implement $>** test $\rightarrow$ repeat per task.
- Human Merge: A human reviews the resulting PRs and requests changes until they are merged.
Key Principles for Legacy System Modernization
Based on the migration of 40,000 out of 300,000 lines of the simulator, Mistral identified three core principles for large-scale legacy migrations:
- Prioritize the Parity Harness: Build the verification system before writing migration code; numerical agreement is the most convincing proof of completion.
- Order Documentation First: Use agents to reconcile and centralize documentation before migration, as code that cannot be read cannot be migrated.
- Implement Structured Workflows: At scale, structured workflows with human review gates outperform both full autonomy and purely manual sessions.