---
id: 18325
lang: en
title: "Mistral Ported 40,000 of 300,000 Lines of Fortran to C++ with Agents"
published_at: "2026-11-26 14:33:12"
source_name: "Habr AI"
source_url: "https://habr.com/ru/companies/otus/articles/1106686/?utm_campaign=1106686&utm_source=habrahabr&utm_medium=rss"
canonical_url: "https://news.hamidun.com/en/news/18325"
categories: ["llm", "autonomous"]
publisher: "AI News by Jemal Hamidun (news.hamidun.com)"
---

# Mistral Ported 40,000 of 300,000 Lines of Fortran to C++ with Agents

> **Краткая суть (BLUF):** Mistral set out to port an oil and gas reservoir simulator to C++: about 300,000 lines of Fortran, with no tests. In the first sprint, the team moved 40,000 lines (about 13%). When the agents were given full freedom, they simply copied the old global variables and GOTO jumps into C++ syntax. What helped were tests that compare numerical results, unified documentation, and modules of up to 10,000 lines with human review. For legacy code, the tests need to come before the agents.  - More than a hundred agents documented the code from the leaves of the call tree up to the root - First attempt: one agent per subroutine, a week for each - Check: the gas density from the Fortran code (42.71834) must match in C++

The Mistral team helped a European energy company update the development simulator for an oil and gas field. The program performs complex physical calculations and is written in Fortran: about 300,000 lines, with no proper tests and documentation scattered across old PDFs. The goal was to port it to modern C++. In the first sprint, the agents moved 40,000 lines, or about 13% of the codebase. The first result revealed an unpleasant fact, however: AI agents rewrite syntax well, but they drag the old architecture along with them. Instead of new code, they produced global variables and GOTO jumps (commands that "jump to the line with such-and-such label"), only now in C++.

## Why a "working" port turned out not to be modernization

Fortran 77 appeared in 1977, and code written in it reflects the constraints of that time. The language has no modules and no composite types. Program state is stored in COMMON blocks, that is, in global memory accessible to the whole program. A variable's type is determined by the first letter of its name, so a typo doesn't break compilation but silently creates a new variable. Names are at most six characters long, which accounts for cryptic names like RHOG and DROG.

In the example from the article, a subroutine calculates gas density using a first-order Taylor formula and writes the result into a global array. In C++, the same logic becomes a function called gasDensity: scattered arrays are merged into a GasProperties structure, the loop over grid cells moves into the calling code, and the result is returned from the function. There is no longer a line-by-line correspondence between the two versions. That makes such a port harder to verify than a simple translation.

In the first attempt, each subroutine was assigned a separate autonomous agent, which spent a week porting it on its own. The code worked, but the COMMON blocks turned into global structures almost one to one, and the GOTOs stayed where they were. The result was Fortran written in C++.

## What worked: tests, documentation, modules

The first step was an equivalence check. The agents added state dumps to the Fortran code at key points chosen by the client's engineers. A C++ test framework then loaded these snapshots as reference values. For example, in one run the gas density RHOG equaled 42.71834, and the ported module had to produce the same value. The authors advise starting any modernization project this way: long agent runs become safer, and the word "ported" acquires a measurable meaning.

The second step was assembling documentation. A program written in a procedural language can be represented as a call tree: which subroutine calls which. The team built one with its own parser and ran more than a hundred agents through Vibe CLI. They worked from the leaves to the root, read the required PDFs via Mistral OCR, and each subagent submitted its description of a section as a separate pull request. A reviewer agent ran on a schedule via cron: it checked new PRs and opened tasks for corrections. A side benefit was unified documentation next to the code, with contradictions removed.

The third step was building the process. The second attempt, with a set of roles (planner, developer, tester, reviewer), improved quality, but the agents got stuck on complex code, cycled through fix options, and had no one to help them. The final scheme added a human:

- Independent modules of roughly up to 10,000 lines of Fortran are identified from the call tree;
- The agent generates the target C++ architecture, and a simulation engineer approves it;
- The work is split into tasks in a queue, and each one goes through a "plan, implement, test, repeat" cycle;
- A human reviews the PRs and requests changes until they are ready to merge.

## Limitations and takeaways

The authors are candid that conditions were favorable: the Fortran version is self-contained and runs. For systems with external dependencies, without a working original, or with physical models that are nowhere documented, the job would be harder, and those cases are not covered in the write-up. The article gives no data on token or infrastructure costs, so the project's economics cannot be assessed from it. It also does not say how much time went into human review, which is likely the main hidden cost.

The practical takeaway for teams with legacy code comes down to three steps. First, build tests that compare the numbers of the old and new versions. Then get the documentation in order: if nobody understands the code, an agent won't understand it any better. Finally, don't let agents run free. Agree on the target architecture before work begins, cut the system into modules, and keep a human on review and at the sticking points. The cheapest approach turns out not to be the highest degree of autonomy, but the one where the result can be merged into the main branch without a rewrite.

---
- **Первоисточник:** [Habr AI](https://habr.com/ru/companies/otus/articles/1106686/?utm_campaign=1106686&utm_source=habrahabr&utm_medium=rss)
- **Каноническая ссылка:** https://news.hamidun.com/en/news/18325
- **Экосистема автора:** [Бесплатный интенсив по вайбкодингу — предзапись в Нике](https://t.me/vibecodeguidebot?start=agent_intensive_news_md) · [Nomad AI](https://nomadnet.ai) · [Telegram @jemal_hamidun](https://t.me/jemal_hamidun)
