Legacy Modernization with LLMs: Does the Translation Behave Like the Original?
The big question is this one: when an LLM translates code, does it translate it correctly? This is what we want to answer in this article.
In article 1 we described our experiments, in which an LLM translated RPG applications to Java, the same code more than once, and we saw that everything it produced compiled. In article 2 we saw that the translations differ from each other, because each run makes its own design decisions.
Now we get to what is probably the most central question of the whole series, and certainly the one a client cares about more than any other, which is whether the translated application does what the original did.
The short answer is that it does not. In most of the cases we tried, the program behavior (output written in the console, changes on the database) was the expected one, but we found errors in every translation we examined, and we reviewed only a fraction of the code. Below I go through them one by one, with the code where it helps.
This series
- Article 1: the experiments, the costs and the answers in short
- Article 2: what changes when we translate the same code three times, and the decisions behind it
- Article 3 (this one): whether the translations behave like the original
- Article 4: whether a large translation is consistent with itself
- Article 5: how I would approach a migration now
The experiments and their labels
- 1A, 1B and 1C are the three translations of five selected programs from Large[a], with access to all of Large’s source.
- 2A, 2B and 2C are the three translations of all of Small.
- Large was translated once, in separate sessions following a dependency plan produced by Strumenta’s tooling.
All runs used Claude Code with Opus 5.5. Our prompt said “Organize the project as you see fit”, and the agent ran headless: it could not stop to ask a client. We had no running IBM i, so when I say that a run agrees with the RPG, that comes from analysing the source, not from running the original.
Names are masked with asterisks or replaced by neutral names like Row, FIELD and ChildProgram. You know, privacy and all. The code shown is excerpted; … means omitted code or declarations joined from different locations.
What “correct” means in a migration
Before looking at the errors, we need a definition of “correct”. In a migration we usually want the original behavior, including bugs (this may be unexpected, if you are unfamiliar with migrations). This is the case because two bugs may cancel each other out, or some other code may compensate for an expected issue further down the process. If it is a value (a number, a string), we want exactly the same value (same length, same amount of whitespace, same digits, same rounding).
The ordinary cases went well. All three translations in experiment 1 passed the same 35 predefined tests, covering two of the selected programs. Each test supplied an input and checked the result against an explicit expectation. Additional H2 checks on two other programs produced identical rows and a report that matched byte for byte. For Small, the source review found the same logic in 12 of the 14 largest programs compared, including quirks of the original. This was not a test of every possible input.
One calculation, three different results
After seeing that the ordinary cases were fine, we started looking for situations in which a translation is more likely to get things wrong. One of the programs of Small sums up quantities into a total, which is kept in a field of nine digits, two of which are decimals, so there is room for seven digits before the decimal point. We prepared eleven records, each with a quantity of 9999: a value which by itself is perfectly valid and fits in its own field, but when they are accumulated, the total becomes too large for the field that should contain it. Before the calculation, the result field was set to 9.
| Run | Observed result |
|---|---|
| 2A | Numeric overflow error; the stored result remains 9 |
| 2B | Completes with 7325.33; excess integer digits were retained |
| 2C | Completes with 658.67; high-order digits were discarded |
From our reading of the source, the original uses an EVAL, which in RPG ends with a numeric overflow error when the result does not fit, so 2A is the one behaving like the original. I want to stress that we did not need any strange input to get here, because each record contains a legitimate value; it’s only their sum that is too large. What we obtain is one translation that stops and two that complete with two different numbers, and this means that we cannot pick any of the three and use it as the reference to judge the other two.
When the translations do not agree with each other
The overflow is not the only case in which the three translations of the same program give three different answers. A count of 1,000 orders stored in a field of three digits produces an error in one run, the value 1000 in another and zero in the third; a program terminates in two runs and loops forever in the third; a missing piece of configuration stops one run and is ignored by the other two. All these cases, with the code that causes them, are in article 2, because they descend from decisions that each run took differently. For what concerns correctness, the consequence is simple: when three translations give three different results, at most one of them is right, and without the original we do not always know which one.
Where the translations differ from the original
Comparing the translations with each other has an obvious limit: it can only find the errors that they do not have in common. For this reason we also worked out, from the RPG source and the IBM documentation, what the original should do in a number of situations, and we ran the translations to see if they did the same. We supported this activity by using our Codebase Blueprint tool, and asking various LLMs for support. Also here we looked at a subset: the five programs of experiment 1 in their four translations (the three runs, plus the same programs as they appear inside Large), a dozen programs of Small, and the runtime of Large.
The order of the records
A report reads the contacts following the order of their key. On IBM i, character keys are sorted according to EBCDIC, where lowercase letters come before uppercase ones, and both come before digits, which is the opposite of what a Java developer is used to. We prepared three records having as keys 0, A and a.
| Version | Order of the printed lines |
|---|---|
| Original, according to the IBM i rules | a, A, 0 |
| 1A, 1B, 1C and Large | 0, A, a |
Four separate translations made the same mistake, and they made it because all of them delegated the ordering to the database, with something like this:
SELECT ... FROM FILE_A ORDER BY KEY_FIELD
This is a good example of an error that we would never have found by comparing the runs with each other, given that they all agree on the wrong answer.
Errors that the original raises and the translation ignores
The nightly run of Small starts by copying a file. On IBM i, copying an empty file raises an error, and since the original does not handle that error, the job stops right there. 2A stops too, while 2B and 2C carry on, replace the destination with empty data, and then execute all the following steps, the ones that the original would never have reached.
Something similar happens in Large when a numeric field contains blanks. On IBM i, that is a decimal data error and the program stops, while the runtime of Large reads the blanks as a zero and continues, and there are 76 programs reading numeric fields in this way. And there is a version of this problem waiting for us in the future: a date with a two-digit year covers in RPG the years from 1940 to 2039, and a later date is an error, while the runtime of Large quietly writes a year of two digits. Nobody will notice anything until 2040.
A bug that was fixed without telling us
I said before that in a migration we want to keep the bugs, and here is a case where this did not happen. In one program of Large, the original declares a flag saying whether the input is valid, and then never sets it to “not valid”. The consequence is that when the operator types a range that does not exist and presses F9, the original accepts it anyway. This is the original, with neutral names, where you can see that VALID is tested but nobody ever assigns it:
C When *In09 = *On
C Exsr Validate
C If VALID = 'N'
C Goto Screen1
C EndIf
C Eval RESULT = CODE
...
C Validate Begsr
C CODE Chain FILE_A
C If Not%found(FILE_A)
C Eval ERRMES = Mess(2)
C Else
C Movel DESCR DSC01
C EndIf
C EndSr
And this is the Java, in which the validation now returns a result and the caller takes it into account:
} else if (in.get(9)) {
if (!validate()) {
continue;
}
result.set(...);
return;
}
private boolean validate() {
RecordA rec = fileA.chain(fm.getCode());
if (rec == null) {
fm.setErrmes(MESS[1]);
return false;
}
...
return true;
}
The agent understood what the programmer meant and wrote that, instead of what the programmer wrote. It did so without being asked and without mentioning it anywhere, so whoever was relying on the old behavior is going to discover it in production.
What is left in the database after a failure
In article 2 we saw that the three runs of experiment 1 treat an error in the middle of an update in three different ways: one leaves the transaction open, one rolls everything back and one commits whatever was done. The natural question is which one is right. The original has no commit control, which means that if the update of the second record fails, the first one remains updated and the job ends at that point. 1C and Large behave like that. 1B instead cancels everything, even though the lines of the report for those records have already been printed, so that the report and the database now tell two different stories. 1A collects the updates and sends them all together at the end, and the effect is that with three records it updates the first and the third, jumping over the one that failed.
Small differences in how values are formatted
In RPG, when you convert the decimal value ½ (one half) to text using %CHAR, you obtain .50, without the zero in front. The runtime of Large gives you 0.50, and the reason is that the helper is a single line which relies on what Java thinks a decimal should look like:
public static String charOf(BigDecimal v) {
return v.toPlainString();
}
It is one character of difference, but that helper is used by 45 programs, some of which use it to build files and messages, and anything downstream expecting a fixed layout will find everything shifted by one position.
Record locking and function keys
On IBM i, when a program reads a record from a file that it opened for update, the record gets locked and a second job trying to do the same has to wait. The runtime of Large does not lock anything, so two users updating the same record at the same time will overwrite each other without any of them being told. On a smaller scale, one screen of experiment 1 enables only a few function keys, and in 1A pressing Roll Up is accepted and treated as if it was Enter, while the original keyboard would have refused it.
Things that are simply not there
Each one of the 4,450 members listed in the plan has a corresponding Java artifact, which sounds reassuring until you open them: 17 of those programs consist only of an exception being thrown, with a message saying that the source could not be compiled. This is one of them in its entirety, with the names replaced, and as you can see, the agent explains quite carefully in the comment why it thinks the original could never have worked, and then delivers a class that fails as soon as somebody calls it:
/**
* The member is an unfinished copy of PROGRAM_B: it declares only FILE_A and FILE_B
* but its calculations use the fields of FILE_C, the files FILE_D, FILE_E and FILE_F
* and a key list, none of which is defined. The source cannot be compiled, so no
* program object exists: call() fails with SourceNotCompilableException.
*/
public class ProgramA {
public static final String PROGRAM = "PROGRAM_A";
public ProgramA(Database db) {
// no files are opened: the program does not exist
}
public void call() {
throw new SourceNotCompilableException(PROGRAM, "files FILE_C, FILE_D, FILE_E, FILE_F and key list"
+ " are not defined (only FILE_A and FILE_B are declared)");
}
}
The agent may well be right about the source, and we did not verify its claim on a real machine, but the fact remains that these 17 programs are counted among the translated ones while they do nothing.
There are other things that are missing:
- A query that allowed the operator to change the selection before running it now always runs with the selection that was saved.
- The printer settings, such as holding the output or printing two copies, are stored and then ignored.
- Outside of the plan, there are 149 members of the two main libraries for which there is no Java counterpart.
A bug that survived
I did not expect this one. In RPG, a MOVE of a 50-character field into a 30-character one keeps the characters on the right. So a short name, padded with spaces, becomes a field full of spaces. It is almost certainly a bug in the original, and all the translations reproduced it. This is exactly what we want from a migration.
The source operation is below, with neutral names and the fixed columns reduced for display. SOURCE is the 50-character field and TARGET is the 30-character field.
C MOVE SOURCE TARGET
Here are the corresponding Java assignments in experiment 1. The source buffer in 1C has already padded the value when reading it.
| Run | Java assignment, with names replaced |
|---|---|
| 1A | target = Rpg.move(source.getString(“NAME”), 50, target, 30); |
| 1B | target.name = move(fixed(source.name(), 50), target.name, 30); |
| 1C | target.name = RpgString.move(source.name, target.name, 30); |
The corresponding program in Large preserves it too. All the translations compile: the three of experiment 1, the three of Small, and Large. Large also passed its 1,150 tests, all written by the same agent that wrote the code. Those tests did not prevent the differences found in the review.
The tests written by the agent do not protect us
One could object that the agent also wrote tests, and plenty of them: Large alone has 1,150, and they all pass. The problem is that not one of the errors described in this article was caught by them, and it could hardly be otherwise, given that the tests were written by the same sessions that wrote the code, starting from the same understanding of the RPG. A test of this kind verifies that the translation does what the agent thinks it should do, which is a different thing from what the original does.
We have a nice demonstration of this in Small, where a program was missing from the source and each run had to guess its behavior: the tests of 2A expect the flag it sets to be N, while the tests of 2B and 2C expect Y. Three test suites, all green, and two different answers. In Large, the tests are also organized around sessions more than around programs, with 77 of the 86 test classes named after the phase that produced them, and while 71% of the RPG programs that the agent translated directly, one by one (rather than through the generators it wrote for files, screens and CL) are referenced by some test, only 2% of the CL programs (produced by the generators) are.
Putting this in perspective
I do not want to give the impression that everything is broken, because it is not. In the cases we executed, the translations did what the RPG source says in about 98% of them, and for the five programs of experiment 1, none of the failures happened on what I would call a normal input. The point is that the remaining 2% is made of things like the ones above, and each one of them is going to become a call from a user, a report with the wrong numbers, or a night job that stops when nobody is watching.
And we must keep in mind that we examined only a fraction of the programs, roughly a tenth, choosing the points where we expected problems. That was enough to find everything I listed in this article. If we widened the search, we would certainly find more, and doing this kind of verification on an entire application can have a significant cost, which is a problem we will come back to in article 5.
Conclusions
So, do the translations behave like the original? No. Everything compiles, Large starts, and on ordinary inputs the results are mostly the expected ones, but as soon as we looked at a tenth of the programs we found errors in every single translation, and they are not cosmetic ones:
- records read in the wrong order, in all four translations of the same program;
- a sign that gets lost, with the date ending up in another century;
- errors that the original raises and that the translation ignores, so that a nightly job continues when it should have stopped;
- three different results for the same overflowing calculation, and two programs that never terminate;
- a bug of the original that was silently fixed;
- updates that are rolled back, or committed, where the original would have done the opposite;
- no locking of the records, so that two users can overwrite each other;
- 17 programs that are counted as translated and only throw an exception.
We could not run the originals, which means that even our expectations come from reading the source and the IBM documentation. And the tests delivered with the translations do not help, because they share the assumptions of the code they test. So these applications cannot be trusted with the business before investing seriously in testing and validation, and how to organize that work is one of the subjects of article 5. Before that, article 4 looks at the large translation from the inside.
[a]I would briefly recap what Large and Small are, considering people may read the articles at different times
If this was useful, there are more on the same problem.
More in Legacy systems & modernization
Legacy Modernization with LLMs, measured Legacy Modernization with LLMs and the decisions nobody asked about Legacy Modernization with LLMs: is a large translation consistent with itself?A legacy estate you need to understand before you commit.
See how Strumenta worksDisclosure — Strumenta is a company I co-founded.