cd /news/ai-tools/when-ai-ports-a-legacy-system-how-do… · home › topics › ai-tools › article
[ARTICLE · art-178473] src=dev.to ↗ pub= topic=ai-tools verified=true sentiment=↑ positive

When AI Ports a Legacy System, How Do You Check It Copied Correctly? Functional Core, Rule Lists and State-Machine Property Tests

A developer working on an AI-assisted rewrite of a legacy system described a testing method that pairs example tests with fast-check property tests to catch bugs in ported stateful screens. The approach models screen logic as a pure step(state, action) function returning new state and effects, then checks invariants such as 'delete only removes attachments of the record currently on the form' across random action sequences of up to 20 steps. On a fixed version each property passed 1,000 runs, while removing the fix caused all three properties to fail, catching a real bug where deleting an attachment on a new record removed the original record's attachment.

recommendation

Add property tests over random action sequences alongside example tests for ported stateful screens, drawing IDs from a tiny value set so same-record and different-record collisions surface bugs example tests miss.

more in playbook: Evals and testing for LLM apps
by read6 min views2 publishedNov 30, 2026

These are working notes on a method, not a framework recommendation. Each main step is labelled with its maturity: Executed (run on a simplified example model, with results), Built (tooling written, verified only on the example), Agreed, not yet applied (process decided, not yet used on a real module). Please read the three separately.

AI writes code fast. In a port-style rewrite the bottleneck is no longer writing, it is knowing whether what was written is right. Existing example tests only cover the path someone thought of, and what breaks is usually a sequence of operations nobody thought of. So we added property tests alongside the example tests.

The approach is four things stacked together:

We are rewriting an existing system onto a new architecture, and most of the code is written by AI. The rule is screens and behaviour are ported as-is. Even the old system's defects are copied first; whether to fix one is a human decision.

Another rewrite project hit a textbook case in its test environment. On a detail page, pressing Copy made the old code copy the form but not clear the attachment area. After a new record number was entered, the attachment area still showed the original record's attachments and delete buttons, and each delete button sent the primary key of its own row. So pressing delete on the new record deleted the original record's attachment.

It was only found after it actually happened. The fix came with example tests, but they verified only half of it. More on which half below.

Write the logic behind the screen as a pure function:

step(state, action) → { newState, effects }

The benefit is concrete: tests don't need a server. Looking at the returned effects tells you exactly which row a click would delete. The cost is just as concrete: every stateful screen gets an extra layer, and core tests only check that "deciding what to do" matches the written properties. Whether the shell actually does it still needs E2E or example tests.

Alongside the example tests, write properties, checked after every step:

# Property A violation means
1 Delete only deletes attachments of the record currently on the form Deleting another record's attachment from the new record (the bug that actually happened)
2 The attachment area only shows this record's attachments The screen shows another record's attachments
3 Every uploaded file was picked on this record's form A file picked but not yet sent on the previous record gets uploaded to the new one

The generator uses fast-check: random action sequences of up to 20 steps, with record numbers deliberately drawn from only 3 values so that "same record / different record" collisions are likely. On the fixed version, each property passed 1,000 runs (fast-check's default is 100).

The sketch below shows property 1 only, leaving out types, generators and test-framework setup. Both sides compare the record number; that detail comes back later:

fc.assert(
  fc.property(fc.array(actionArb, { maxLength: 20 }), (actions) => {
    let s = initialState;
    for (const a of actions) {
      const { state, effects } = step(s, a);
      for (const e of effects) {
        // a delete must target the record that was on the form when delete was pressed
        if (e.t === "deleteRemote") assert.equal(e.docNo, s.docNo);
      }
      s = state;
    }
  }),
  { numRuns: 1000 },
);

Results:

Version Property 1 Property 2 Property 3
Fixed (1,000 runs each) Pass Pass Pass
Fix removed (fixed seed) Caught Caught Caught

Three things worth noting:

Approach Good for Trade-off for this problem
Example tests Fixed, known paths Only cover sequences someone chose, which is exactly what was missed here; the two complement each other
E2E Confirming the shell really executes the effects Costlier to run; put large numbers of random sequences at the core layer and use E2E for key integration paths
Recording legacy output and diffing Stateless outputs such as reports and queries Needs old and new running side by side; stateful multi-step screens are hard to record completely
Formal verification (Quint and similar) Concurrent users, transaction ordering Verifies a design model; whether model and code agree must be checked separately; we plan to use it only for concurrent flows

The Copy case started from knowing what was wrong, then writing properties. Porting a whole system gives you no such head start, so the process is split into five steps, each with a checkable output:

Step Output How it is checked
① Extract rules One rule list per module, with legacy source citations Citation check, spot-check line numbers
② Check against data One verification SQL query per rule Run on existing data; chase any mismatch
③ Define the core interface State / Action / Effect types Write-point coverage table
④ Property tests At least one property per rule Must be red on base
⑤ Implementation The core implementation Acceptance gate

Three design choices I find most useful:

To make the AI write in this shape, the plan is to rely on mechanical constraints rather than just prompts: on the backend, architecture tests forbid the core package from importing framework and database layers; on the frontend, stateful screens always use a reducer, and lint rules stop the core from importing API modules. None of this is in place yet, and these constraints only guard dependency boundaries; they don't guarantee the core's logic is correct.

The division of labour follows TDD:

The gate has been run once on the example. Reviewing it ourselves turned up two holes, listed here for anyone building something similar:

test script in package.json, or a shared test helper, and still turn head green. The protected set should include these by default. The properties have a hole too. In the example, properties 1 and 2 decide "same record or not" by record number, while property 3 uses the form's internal sequence id. If the new record happens to be given the same record number as the original and then delete is pressed, property 1 cannot catch it. The fix is to use the internal sequence id throughout (both the attachment rows and the delete effect must carry it), or to state explicitly in the model that record numbers are unique, with a citation to the legacy code.

Disclosure: this article was drafted with help from Claude, an Anthropic model. Claims were checked against our internal method notes and the example code; an independent review by OpenAI Codex was done before publication. A Traditional Chinese version is also available.

── more in #ai-tools 4 stories · sorted by recency
── more on @fast-check 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/when-ai-ports-a-lega…] indexed:0 read:6min 2026-11-30 · —