inkspell
writing topics feed
MODE
THEME
← writing

The Making of an Automated Market Maker: Why So Many Tests?

Why does a 65-line Solidity contract have 670 lines of tests? Part of the answer is AI agents doing mutation testing, and one of their broken pools held a surprise: the model itself was missing a property.

November 27, 2026 · 12 min read
amm ethereum solidity foundry mutation-testing testing agents defi blockchain

The pool from the last post is 65 lines of Solidity. Its tests are 670 lines. One to one would already be a lot. Ten to one is crazy.

Some of it is setup: a tiny contract needs a lot of test code per line. The rest came from AI agents. The pool is ported from a Haskell model, and nothing yet runs the two side by side, so the tests are the only proof that they agree.

The agents’ job was to find holes in that proof. They kept writing broken versions of the pool that the tests missed, then wrote new tests to cover those blind spots. That is mutation testing done by agents.

It improved more than the tests: one broken pool showed that the model itself was missing a property.

This is the fourth post in a series about automated market makers, programs that let anyone swap one token for another. The code is tagged ch02-evm, and every bug, script and result is in the mutation folder.

Every line ran, and a hidden fee still passed

Mutation testing asks which bugs a test suite catches. A mutant is a copy of the contract with one small change. The suite runs on each mutant: if at least one test fails, the mutant is killed; if every test passes, it survives. A survivor points to a possible gap in the tests.

A cartoon zombie walks through an airport security scanner holding a crayon drawing of a smiling face over its own face. The scanner's lamp glows green. A security officer beside the gate watches, frowning, hand on chin. A cartoon zombie walks through an airport security scanner holding a crayon drawing of a smiling face over its own face. The scanner's lamp glows green. A security officer beside the gate watches, frowning, hand on chin.
Would your tests let this one through?

Code coverage is more familiar, and the two answer different questions. Line coverage only tells you that a line ran during the tests. A killed mutant says something stricter: here is a specific wrong version of the code, and here is a test that rejects it.

Here is what the tests are checking. The pool holds two tokens, gold and silver. Its owner fills it once by calling initialize, and after that anyone can swap one token for the other. The pool tracks what it holds in two reserves, one per token. Each swap pays out as much as it can without letting the two reserves multiplied together, their product, go down.

The Haskell model from the second post defines what every swap should pay. The tests check the contract against it in two ways. Worked examples are single trades whose answers come from the model. Properties are general rules the model obeys, run as fuzz tests on random inputs. A third kind of test, the invariant run, has several traders make random sequences of calls and checks a set of conditions after every call.

The first suite had 30 tests. Foundry’s coverage tool reports 100% line coverage, 97.56% statement coverage and 92.31% branch coverage. Yet all 30 tests pass this mutant:

-amountOut = reserveOut * amountIn / (reserveIn + amountIn);
+amountOut = reserveOut * amountIn * 999
+    / (reserveIn * 1000 + amountIn * 999);

This is the line that decides how much a trader gets back. The changed version takes the whole payment but prices the swap as if only 99.9% of it had arrived, so the pool keeps the other 0.1% for itself. It is the formula Uniswap V2 uses to charge its 0.3% fee, set to 0.1%. The difference is that Uniswap announces its fee, and this pool never tells its traders.

The line that prices a swap runs. The price is wrong. Nothing fails.

Rounding hides the fee from the worked examples. Token amounts are whole units, so the pool drops the fractional part of every payout. On a pool of 1,000 gold and 4,000 silver, a swap of 100 gold is owed 363.6 silver, so the honest pool pays 363. The fee pool calculates 363.3 and also pays 363. A worked example with no rounding catches it: swapping 100 gold into a pool of 100 gold and 100 silver pays exactly 50, while the fee pool pays 49. The widget below runs both examples on both pools.

The fee passed every rule in the model, too

Rounding explains the worked examples. It doesn’t explain the properties, and every one of them passed with the fee in place. They had to. The model’s rules protect the pool: its product never goes down, it never runs dry, a round trip never makes a profit. A pool that keeps a little of every payment only makes its product larger, so it breaks none of them.

The model’s formula also means something none of its rules said: the pool pays out as much as it can. The payout is the largest amount that keeps the product from going down, so one unit more would lower it. That is now a fuzz test, testFuzz_OutputIsTheLargestThatKeepsTheProduct. It catches a pool that underpays as well as one that overpays, without repeating the formula.

This is the find I’d keep if I could keep only one. A mutant usually tests the tests. This one tested the specification. The model had passed its own properties since the second post, and the fee showed that they described a pool that could quietly cheat.

Agents and tools find different bugs, and you need both

Mutation tools make mutants by fixed rules: flip a comparison, delete a statement, swap an operator. The first of five I tried, Gambit, made 83. The first suite killed 82 of them, and one more test killed the last. That 31-test suite still passes the hidden fee. Against Gambit’s mutants and the agents’ bugs together, it catches 108 of 233, where the full suite of 71 tests that grew out of the rounds catches 203. A perfect score against one tool only means the tests kill that tool’s mutants.

The agents’ wrong pools are a different kind of bug. In the later rounds, 70% of them added new code instead of editing a line, such as a pool that remembers its first caller and sends the next trader’s payout there. No rule set writes that.

What each review round found

Five AI agents ran six rounds. Across all of them, the contract changed once: it now refuses to create a pool with the same token twice. Everything else went into the tests.

  • Round 1. The agents read the code and the tests and checked each other’s findings. Nothing pinned down the exact swap formula, which is where the hidden fee and the largest-payout rule came from. The fuzz tests also threw away every refused swap, so a pool that refused valid trades looked fine.
  • Round 2. A pool that pays correctly on its first swap, then one unit less on every swap after that. All 35 tests at the time passed it, because they checked the exact payout only on a pool’s first swap.
+    uint256 swapCount;
 ...
+        if (swapCount++ > 0) amountOut -= 1;
  • Round 3. From here on, the agents wrote wrong pools aimed at getting past every test. Before a swap, the trader approves how many tokens the pool may take, and one wrong pool drained a whole 500-token approval during a 100-token swap. The tests missed it because they always approved the exact swap amount.
  • Round 4. Alice trades first, and when Bob pays later, his payout goes to Alice. The bug is serious, but it took an agent inventing a pool that remembers its first caller.
  • Round 5. By now, beating the tests mostly took mistakes nobody is likely to make, like treating two tokens with the same symbol as the same token. The realistic one was a pool that skips taking payment when the trader gives an unlimited approval, which real traders use.
  • Round 6. The agents rated how likely each new mistake was. One found no realistic mistake at all, and another found only low-severity ones, so the rounds stopped.

It works the other way too. Five tool mutants that change what the pool does passed the full suite. Here is the one that matters most:

-        if (msg.sender != owner) revert NotOwner();
+        if (msg.sender < owner) revert NotOwner();

In Solidity, msg.sender is the caller’s address, and revert refuses the call and undoes everything it did. An Ethereum address is also a number, so with this mutant any caller whose address is higher than the owner’s can call initialize and fill the pool. Six rounds missed it because every outsider the tests tried happened to have an address lower than the owner’s, which is thoroughness in only one direction. Two of the five tools made this mutant, and test_InitializeIsOnlyForTheOwner now tries the highest possible address too.

Tools have a cost of their own. A mutant can change the bytecode without changing anything a test could see: a uint256 is never below zero, so amountIn == 0 and amountIn <= 0 are the same check. These are equivalent mutants, and 34 of the 51 tool mutants that got past the first suite were equivalent. Running a mutant takes under two seconds. Deciding whether a survivor is equivalent or a real gap took two separate agent reviews, and that is the expensive part.

When code is cheap to write, something has to say stop

Back to the ratio. Here is what the 670 lines do:

Exact counts
PartCode linesAgainst 65
Fixtures and helpers: setup, funding, the expected balances the tests track, the test token2233.4×
The model’s worked examples and properties, in Solidity1582.4×
The invariant run: the random traders, their checks, and the 8 invariants1472.3×
Contract-specific tests: initialize, the owner, decimals, donations, events, overflow1422.2×
Total67010.3×

The split between fixtures and the invariant run is approximate, because the random traders’ swap helper mixes call setup with checks.

The setup alone is 3.4 times the contract, and the copied model checks add another 2.4. Both were in the first suite, and both are fixed costs that a 65-line contract magnifies.

The problem was the growth:

Exact counts
StageTestsCode linesAgainst the contract
First version303665.8×
After round 2425358.2×
After round 46278412.1×
After round 57194014.5×
After closing the gaps6592814.3×
After the deep trim3867010.3×

Ratios use the contract’s size at each stage; the first version predates the same-token check. Test counts treat each invariant on each of the invariant run’s pools as a separate test, so Foundry reports a smaller total.

A reviewer can always invent one more wrong pool, and an agent can write it and the test that catches it for almost nothing. But code is never free. Every test has to be read, run and kept passing by every change after it. When writing is cheap, the limit has to come from somewhere else. Here it came from one question: how likely is this mistake in the real world?

The rounds stopped when the agents in round 6 found little that was realistic. The same question drove the cut. Two agents reviewed the suite and found that rounds 1 to 3 had caught slips a person could make, while the later rounds mostly chased bugs someone had to invent. So I cut deep, and checked the cut against all 502 bugs from the agents and the tools. The first draft of the trim stopped catching eleven mistakes a person could make, such as accepting a second initialize after a trade, so those tests went back in.

Before closing the gapsAfter closing the gapsAfter the deep trim
Tests716538
Code lines940928670
Caught, of 502426440427

The trimmed suite has 258 fewer code lines and catches 13 fewer bugs. It still catches every wrong price, wrong payment and wrongly allowed swap. What it lets through is mostly failing calls that report a different error, and edits nobody is likely to make.

Whether that trade is right depends on the project. This pool is a learning project, not one holding anyone’s money, so letting unlikely bugs through is fine. A pool holding real money would be different. There I’d keep inventing strange wrong pools, because an attacker will, and I’d want to know whether the properties really cover every way a payout can go wrong.

How far to go is a risk decision: how likely a mistake is, what it costs, and how much the project can afford to lose. The same goes for code. Agents tend to write a check for every case they can imagine, and whether a rare case deserves one depends on the same risk assessment.

So the 670 lines are mostly setup, plus the tests that survived that question. Mutation testing gave me two things: a record of what each test catches, which made cutting safe, and one rule the model was missing. The next chapter’s contract adds fees and liquidity shares, and it will show whether the setup stays near 223 lines while the contract grows.

← Back to all writing
Last login: Fri, Nov 27, 00:00:00 on ttys001
user@inkspell:~/blog read constant-product-tests.md
FILE: constant-product-tests.md DATE: 2026-11-27 WORDS: 2358 READ: ~12m
TAGS: [amm] [ethereum] [solidity] [foundry] [mutation-testing] [testing] [agents] [defi] [blockchain]

The Making of an Automated Market Maker: Why So Many Tests?

// Why does a 65-line Solidity contract have 670 lines of tests? Part of the answer is AI agents doing mutation testing, and one of their broken pools held a surprise: the model itself was missing a property.

» BEGIN OUTPUT

The pool from the last post is 65 lines of Solidity. Its tests are 670 lines. One to one would already be a lot. Ten to one is crazy.

Some of it is setup: a tiny contract needs a lot of test code per line. The rest came from AI agents. The pool is ported from a Haskell model, and nothing yet runs the two side by side, so the tests are the only proof that they agree.

The agents’ job was to find holes in that proof. They kept writing broken versions of the pool that the tests missed, then wrote new tests to cover those blind spots. That is mutation testing done by agents.

It improved more than the tests: one broken pool showed that the model itself was missing a property.

This is the fourth post in a series about automated market makers, programs that let anyone swap one token for another. The code is tagged ch02-evm, and every bug, script and result is in the mutation folder.

Every line ran, and a hidden fee still passed

Mutation testing asks which bugs a test suite catches. A mutant is a copy of the contract with one small change. The suite runs on each mutant: if at least one test fails, the mutant is killed; if every test passes, it survives. A survivor points to a possible gap in the tests.

A cartoon zombie walks through an airport security scanner holding a crayon drawing of a smiling face over its own face. The scanner's lamp glows green. A security officer beside the gate watches, frowning, hand on chin. A cartoon zombie walks through an airport security scanner holding a crayon drawing of a smiling face over its own face. The scanner's lamp glows green. A security officer beside the gate watches, frowning, hand on chin.
Would your tests let this one through?

Code coverage is more familiar, and the two answer different questions. Line coverage only tells you that a line ran during the tests. A killed mutant says something stricter: here is a specific wrong version of the code, and here is a test that rejects it.

Here is what the tests are checking. The pool holds two tokens, gold and silver. Its owner fills it once by calling initialize, and after that anyone can swap one token for the other. The pool tracks what it holds in two reserves, one per token. Each swap pays out as much as it can without letting the two reserves multiplied together, their product, go down.

The Haskell model from the second post defines what every swap should pay. The tests check the contract against it in two ways. Worked examples are single trades whose answers come from the model. Properties are general rules the model obeys, run as fuzz tests on random inputs. A third kind of test, the invariant run, has several traders make random sequences of calls and checks a set of conditions after every call.

The first suite had 30 tests. Foundry’s coverage tool reports 100% line coverage, 97.56% statement coverage and 92.31% branch coverage. Yet all 30 tests pass this mutant:

-amountOut = reserveOut * amountIn / (reserveIn + amountIn);
+amountOut = reserveOut * amountIn * 999
+    / (reserveIn * 1000 + amountIn * 999);

This is the line that decides how much a trader gets back. The changed version takes the whole payment but prices the swap as if only 99.9% of it had arrived, so the pool keeps the other 0.1% for itself. It is the formula Uniswap V2 uses to charge its 0.3% fee, set to 0.1%. The difference is that Uniswap announces its fee, and this pool never tells its traders.

The line that prices a swap runs. The price is wrong. Nothing fails.

Rounding hides the fee from the worked examples. Token amounts are whole units, so the pool drops the fractional part of every payout. On a pool of 1,000 gold and 4,000 silver, a swap of 100 gold is owed 363.6 silver, so the honest pool pays 363. The fee pool calculates 363.3 and also pays 363. A worked example with no rounding catches it: swapping 100 gold into a pool of 100 gold and 100 silver pays exactly 50, while the fee pool pays 49. The widget below runs both examples on both pools.

The fee passed every rule in the model, too

Rounding explains the worked examples. It doesn’t explain the properties, and every one of them passed with the fee in place. They had to. The model’s rules protect the pool: its product never goes down, it never runs dry, a round trip never makes a profit. A pool that keeps a little of every payment only makes its product larger, so it breaks none of them.

The model’s formula also means something none of its rules said: the pool pays out as much as it can. The payout is the largest amount that keeps the product from going down, so one unit more would lower it. That is now a fuzz test, testFuzz_OutputIsTheLargestThatKeepsTheProduct. It catches a pool that underpays as well as one that overpays, without repeating the formula.

This is the find I’d keep if I could keep only one. A mutant usually tests the tests. This one tested the specification. The model had passed its own properties since the second post, and the fee showed that they described a pool that could quietly cheat.

Agents and tools find different bugs, and you need both

Mutation tools make mutants by fixed rules: flip a comparison, delete a statement, swap an operator. The first of five I tried, Gambit, made 83. The first suite killed 82 of them, and one more test killed the last. That 31-test suite still passes the hidden fee. Against Gambit’s mutants and the agents’ bugs together, it catches 108 of 233, where the full suite of 71 tests that grew out of the rounds catches 203. A perfect score against one tool only means the tests kill that tool’s mutants.

The agents’ wrong pools are a different kind of bug. In the later rounds, 70% of them added new code instead of editing a line, such as a pool that remembers its first caller and sends the next trader’s payout there. No rule set writes that.

What each review round found

Five AI agents ran six rounds. Across all of them, the contract changed once: it now refuses to create a pool with the same token twice. Everything else went into the tests.

  • Round 1. The agents read the code and the tests and checked each other’s findings. Nothing pinned down the exact swap formula, which is where the hidden fee and the largest-payout rule came from. The fuzz tests also threw away every refused swap, so a pool that refused valid trades looked fine.
  • Round 2. A pool that pays correctly on its first swap, then one unit less on every swap after that. All 35 tests at the time passed it, because they checked the exact payout only on a pool’s first swap.
+    uint256 swapCount;
 ...
+        if (swapCount++ > 0) amountOut -= 1;
  • Round 3. From here on, the agents wrote wrong pools aimed at getting past every test. Before a swap, the trader approves how many tokens the pool may take, and one wrong pool drained a whole 500-token approval during a 100-token swap. The tests missed it because they always approved the exact swap amount.
  • Round 4. Alice trades first, and when Bob pays later, his payout goes to Alice. The bug is serious, but it took an agent inventing a pool that remembers its first caller.
  • Round 5. By now, beating the tests mostly took mistakes nobody is likely to make, like treating two tokens with the same symbol as the same token. The realistic one was a pool that skips taking payment when the trader gives an unlimited approval, which real traders use.
  • Round 6. The agents rated how likely each new mistake was. One found no realistic mistake at all, and another found only low-severity ones, so the rounds stopped.

It works the other way too. Five tool mutants that change what the pool does passed the full suite. Here is the one that matters most:

-        if (msg.sender != owner) revert NotOwner();
+        if (msg.sender < owner) revert NotOwner();

In Solidity, msg.sender is the caller’s address, and revert refuses the call and undoes everything it did. An Ethereum address is also a number, so with this mutant any caller whose address is higher than the owner’s can call initialize and fill the pool. Six rounds missed it because every outsider the tests tried happened to have an address lower than the owner’s, which is thoroughness in only one direction. Two of the five tools made this mutant, and test_InitializeIsOnlyForTheOwner now tries the highest possible address too.

Tools have a cost of their own. A mutant can change the bytecode without changing anything a test could see: a uint256 is never below zero, so amountIn == 0 and amountIn <= 0 are the same check. These are equivalent mutants, and 34 of the 51 tool mutants that got past the first suite were equivalent. Running a mutant takes under two seconds. Deciding whether a survivor is equivalent or a real gap took two separate agent reviews, and that is the expensive part.

When code is cheap to write, something has to say stop

Back to the ratio. Here is what the 670 lines do:

Exact counts
PartCode linesAgainst 65
Fixtures and helpers: setup, funding, the expected balances the tests track, the test token2233.4×
The model’s worked examples and properties, in Solidity1582.4×
The invariant run: the random traders, their checks, and the 8 invariants1472.3×
Contract-specific tests: initialize, the owner, decimals, donations, events, overflow1422.2×
Total67010.3×

The split between fixtures and the invariant run is approximate, because the random traders’ swap helper mixes call setup with checks.

The setup alone is 3.4 times the contract, and the copied model checks add another 2.4. Both were in the first suite, and both are fixed costs that a 65-line contract magnifies.

The problem was the growth:

Exact counts
StageTestsCode linesAgainst the contract
First version303665.8×
After round 2425358.2×
After round 46278412.1×
After round 57194014.5×
After closing the gaps6592814.3×
After the deep trim3867010.3×

Ratios use the contract’s size at each stage; the first version predates the same-token check. Test counts treat each invariant on each of the invariant run’s pools as a separate test, so Foundry reports a smaller total.

A reviewer can always invent one more wrong pool, and an agent can write it and the test that catches it for almost nothing. But code is never free. Every test has to be read, run and kept passing by every change after it. When writing is cheap, the limit has to come from somewhere else. Here it came from one question: how likely is this mistake in the real world?

The rounds stopped when the agents in round 6 found little that was realistic. The same question drove the cut. Two agents reviewed the suite and found that rounds 1 to 3 had caught slips a person could make, while the later rounds mostly chased bugs someone had to invent. So I cut deep, and checked the cut against all 502 bugs from the agents and the tools. The first draft of the trim stopped catching eleven mistakes a person could make, such as accepting a second initialize after a trade, so those tests went back in.

Before closing the gapsAfter closing the gapsAfter the deep trim
Tests716538
Code lines940928670
Caught, of 502426440427

The trimmed suite has 258 fewer code lines and catches 13 fewer bugs. It still catches every wrong price, wrong payment and wrongly allowed swap. What it lets through is mostly failing calls that report a different error, and edits nobody is likely to make.

Whether that trade is right depends on the project. This pool is a learning project, not one holding anyone’s money, so letting unlikely bugs through is fine. A pool holding real money would be different. There I’d keep inventing strange wrong pools, because an attacker will, and I’d want to know whether the properties really cover every way a payout can go wrong.

How far to go is a risk decision: how likely a mistake is, what it costs, and how much the project can afford to lose. The same goes for code. Agents tend to write a check for every case they can imagine, and whether a rare case deserves one depends on the same risk assessment.

So the 670 lines are mostly setup, plus the tests that survived that question. Mutation testing gave me two things: a record of what each test catches, which made cutting safe, and one rule the model was missing. The next chapter’s contract adds fees and liquidity shares, and it will show whether the setup stays near 223 lines while the contract grows.

END OF FILE
user@inkspell:~/blog $
$ cd ..
phone down  ·  breathe  ·  begin
☸︎
amm ☸︎ ethereum ☸︎ solidity ☸︎ foundry ☸︎ mutation-testing ☸︎ testing ☸︎ agents ☸︎ defi ☸︎ blockchain

The Making of an Automated Market Maker: Why So Many Tests?

☸︎

Why does a 65-line Solidity contract have 670 lines of tests? Part of the answer is AI agents doing mutation testing, and one of their broken pools held a surprise: the model itself was missing a property.

session · 12 min sitting · November 27, 2026
— enter the practice —

The pool from the last post is 65 lines of Solidity. Its tests are 670 lines. One to one would already be a lot. Ten to one is crazy.

Some of it is setup: a tiny contract needs a lot of test code per line. The rest came from AI agents. The pool is ported from a Haskell model, and nothing yet runs the two side by side, so the tests are the only proof that they agree.

The agents’ job was to find holes in that proof. They kept writing broken versions of the pool that the tests missed, then wrote new tests to cover those blind spots. That is mutation testing done by agents.

It improved more than the tests: one broken pool showed that the model itself was missing a property.

This is the fourth post in a series about automated market makers, programs that let anyone swap one token for another. The code is tagged ch02-evm, and every bug, script and result is in the mutation folder.

Every line ran, and a hidden fee still passed

Mutation testing asks which bugs a test suite catches. A mutant is a copy of the contract with one small change. The suite runs on each mutant: if at least one test fails, the mutant is killed; if every test passes, it survives. A survivor points to a possible gap in the tests.

A cartoon zombie walks through an airport security scanner holding a crayon drawing of a smiling face over its own face. The scanner's lamp glows green. A security officer beside the gate watches, frowning, hand on chin. A cartoon zombie walks through an airport security scanner holding a crayon drawing of a smiling face over its own face. The scanner's lamp glows green. A security officer beside the gate watches, frowning, hand on chin.
Would your tests let this one through?

Code coverage is more familiar, and the two answer different questions. Line coverage only tells you that a line ran during the tests. A killed mutant says something stricter: here is a specific wrong version of the code, and here is a test that rejects it.

Here is what the tests are checking. The pool holds two tokens, gold and silver. Its owner fills it once by calling initialize, and after that anyone can swap one token for the other. The pool tracks what it holds in two reserves, one per token. Each swap pays out as much as it can without letting the two reserves multiplied together, their product, go down.

The Haskell model from the second post defines what every swap should pay. The tests check the contract against it in two ways. Worked examples are single trades whose answers come from the model. Properties are general rules the model obeys, run as fuzz tests on random inputs. A third kind of test, the invariant run, has several traders make random sequences of calls and checks a set of conditions after every call.

The first suite had 30 tests. Foundry’s coverage tool reports 100% line coverage, 97.56% statement coverage and 92.31% branch coverage. Yet all 30 tests pass this mutant:

-amountOut = reserveOut * amountIn / (reserveIn + amountIn);
+amountOut = reserveOut * amountIn * 999
+    / (reserveIn * 1000 + amountIn * 999);

This is the line that decides how much a trader gets back. The changed version takes the whole payment but prices the swap as if only 99.9% of it had arrived, so the pool keeps the other 0.1% for itself. It is the formula Uniswap V2 uses to charge its 0.3% fee, set to 0.1%. The difference is that Uniswap announces its fee, and this pool never tells its traders.

The line that prices a swap runs. The price is wrong. Nothing fails.

Rounding hides the fee from the worked examples. Token amounts are whole units, so the pool drops the fractional part of every payout. On a pool of 1,000 gold and 4,000 silver, a swap of 100 gold is owed 363.6 silver, so the honest pool pays 363. The fee pool calculates 363.3 and also pays 363. A worked example with no rounding catches it: swapping 100 gold into a pool of 100 gold and 100 silver pays exactly 50, while the fee pool pays 49. The widget below runs both examples on both pools.

The fee passed every rule in the model, too

Rounding explains the worked examples. It doesn’t explain the properties, and every one of them passed with the fee in place. They had to. The model’s rules protect the pool: its product never goes down, it never runs dry, a round trip never makes a profit. A pool that keeps a little of every payment only makes its product larger, so it breaks none of them.

The model’s formula also means something none of its rules said: the pool pays out as much as it can. The payout is the largest amount that keeps the product from going down, so one unit more would lower it. That is now a fuzz test, testFuzz_OutputIsTheLargestThatKeepsTheProduct. It catches a pool that underpays as well as one that overpays, without repeating the formula.

This is the find I’d keep if I could keep only one. A mutant usually tests the tests. This one tested the specification. The model had passed its own properties since the second post, and the fee showed that they described a pool that could quietly cheat.

Agents and tools find different bugs, and you need both

Mutation tools make mutants by fixed rules: flip a comparison, delete a statement, swap an operator. The first of five I tried, Gambit, made 83. The first suite killed 82 of them, and one more test killed the last. That 31-test suite still passes the hidden fee. Against Gambit’s mutants and the agents’ bugs together, it catches 108 of 233, where the full suite of 71 tests that grew out of the rounds catches 203. A perfect score against one tool only means the tests kill that tool’s mutants.

The agents’ wrong pools are a different kind of bug. In the later rounds, 70% of them added new code instead of editing a line, such as a pool that remembers its first caller and sends the next trader’s payout there. No rule set writes that.

What each review round found

Five AI agents ran six rounds. Across all of them, the contract changed once: it now refuses to create a pool with the same token twice. Everything else went into the tests.

  • Round 1. The agents read the code and the tests and checked each other’s findings. Nothing pinned down the exact swap formula, which is where the hidden fee and the largest-payout rule came from. The fuzz tests also threw away every refused swap, so a pool that refused valid trades looked fine.
  • Round 2. A pool that pays correctly on its first swap, then one unit less on every swap after that. All 35 tests at the time passed it, because they checked the exact payout only on a pool’s first swap.
+    uint256 swapCount;
 ...
+        if (swapCount++ > 0) amountOut -= 1;
  • Round 3. From here on, the agents wrote wrong pools aimed at getting past every test. Before a swap, the trader approves how many tokens the pool may take, and one wrong pool drained a whole 500-token approval during a 100-token swap. The tests missed it because they always approved the exact swap amount.
  • Round 4. Alice trades first, and when Bob pays later, his payout goes to Alice. The bug is serious, but it took an agent inventing a pool that remembers its first caller.
  • Round 5. By now, beating the tests mostly took mistakes nobody is likely to make, like treating two tokens with the same symbol as the same token. The realistic one was a pool that skips taking payment when the trader gives an unlimited approval, which real traders use.
  • Round 6. The agents rated how likely each new mistake was. One found no realistic mistake at all, and another found only low-severity ones, so the rounds stopped.

It works the other way too. Five tool mutants that change what the pool does passed the full suite. Here is the one that matters most:

-        if (msg.sender != owner) revert NotOwner();
+        if (msg.sender < owner) revert NotOwner();

In Solidity, msg.sender is the caller’s address, and revert refuses the call and undoes everything it did. An Ethereum address is also a number, so with this mutant any caller whose address is higher than the owner’s can call initialize and fill the pool. Six rounds missed it because every outsider the tests tried happened to have an address lower than the owner’s, which is thoroughness in only one direction. Two of the five tools made this mutant, and test_InitializeIsOnlyForTheOwner now tries the highest possible address too.

Tools have a cost of their own. A mutant can change the bytecode without changing anything a test could see: a uint256 is never below zero, so amountIn == 0 and amountIn <= 0 are the same check. These are equivalent mutants, and 34 of the 51 tool mutants that got past the first suite were equivalent. Running a mutant takes under two seconds. Deciding whether a survivor is equivalent or a real gap took two separate agent reviews, and that is the expensive part.

When code is cheap to write, something has to say stop

Back to the ratio. Here is what the 670 lines do:

Exact counts
PartCode linesAgainst 65
Fixtures and helpers: setup, funding, the expected balances the tests track, the test token2233.4×
The model’s worked examples and properties, in Solidity1582.4×
The invariant run: the random traders, their checks, and the 8 invariants1472.3×
Contract-specific tests: initialize, the owner, decimals, donations, events, overflow1422.2×
Total67010.3×

The split between fixtures and the invariant run is approximate, because the random traders’ swap helper mixes call setup with checks.

The setup alone is 3.4 times the contract, and the copied model checks add another 2.4. Both were in the first suite, and both are fixed costs that a 65-line contract magnifies.

The problem was the growth:

Exact counts
StageTestsCode linesAgainst the contract
First version303665.8×
After round 2425358.2×
After round 46278412.1×
After round 57194014.5×
After closing the gaps6592814.3×
After the deep trim3867010.3×

Ratios use the contract’s size at each stage; the first version predates the same-token check. Test counts treat each invariant on each of the invariant run’s pools as a separate test, so Foundry reports a smaller total.

A reviewer can always invent one more wrong pool, and an agent can write it and the test that catches it for almost nothing. But code is never free. Every test has to be read, run and kept passing by every change after it. When writing is cheap, the limit has to come from somewhere else. Here it came from one question: how likely is this mistake in the real world?

The rounds stopped when the agents in round 6 found little that was realistic. The same question drove the cut. Two agents reviewed the suite and found that rounds 1 to 3 had caught slips a person could make, while the later rounds mostly chased bugs someone had to invent. So I cut deep, and checked the cut against all 502 bugs from the agents and the tools. The first draft of the trim stopped catching eleven mistakes a person could make, such as accepting a second initialize after a trade, so those tests went back in.

Before closing the gapsAfter closing the gapsAfter the deep trim
Tests716538
Code lines940928670
Caught, of 502426440427

The trimmed suite has 258 fewer code lines and catches 13 fewer bugs. It still catches every wrong price, wrong payment and wrongly allowed swap. What it lets through is mostly failing calls that report a different error, and edits nobody is likely to make.

Whether that trade is right depends on the project. This pool is a learning project, not one holding anyone’s money, so letting unlikely bugs through is fine. A pool holding real money would be different. There I’d keep inventing strange wrong pools, because an attacker will, and I’d want to know whether the properties really cover every way a payout can go wrong.

How far to go is a risk decision: how likely a mistake is, what it costs, and how much the project can afford to lose. The same goes for code. Agents tend to write a check for every case they can imagine, and whether a rare case deserves one depends on the same risk assessment.

So the 670 lines are mostly setup, plus the tests that survived that question. Mutation testing gave me two things: a record of what each test catches, which made cutting safe, and one rule the model was missing. The next chapter’s contract adds fees and liquidity shares, and it will show whether the setup stays near 223 lines while the contract grows.

☸︎
the session ends
learn  ·  unlearn  ·  return
return when ready
☠︎ LARTS-SERVER BBS v2.3.1 — root console
⚠︎ Unauthorised access will be met with creative, legally ambiguous, and frankly disproportionate solutions.
Last login: 2026-11-27 00:00:00 from somewhere you'll regret  ·  session pid 1499
⚡ luser activity is being monitored. it always was. 6 LARTs administered today.
☠︎ BOFH EXCUSE #1155 :: a cat walked across the UPS keyboard
root@larts-server:/var/rants # cat constant-product-tests.txt  # and what was your username again?
SUBJECT: The Making of an Automated Market Maker: Why So Many Tests?
DATE: 2026-11-27 00:00:00 READ: ~12 min of your billable downtime WORDS: 2358
TAGS: [amm] [ethereum] [solidity] [foundry] [mutation-testing] [testing] [agents] [defi] [blockchain]
// Why does a 65-line Solidity contract have 670 lines of tests? Part of the answer is AI agents doing mutation testing, and one of their broken pools held a surprise: the model itself was missing a property.
☠︎ ::: BEGIN TRANSMISSION — touch nothing ::: ☠︎

The pool from the last post is 65 lines of Solidity. Its tests are 670 lines. One to one would already be a lot. Ten to one is crazy.

Some of it is setup: a tiny contract needs a lot of test code per line. The rest came from AI agents. The pool is ported from a Haskell model, and nothing yet runs the two side by side, so the tests are the only proof that they agree.

The agents’ job was to find holes in that proof. They kept writing broken versions of the pool that the tests missed, then wrote new tests to cover those blind spots. That is mutation testing done by agents.

It improved more than the tests: one broken pool showed that the model itself was missing a property.

This is the fourth post in a series about automated market makers, programs that let anyone swap one token for another. The code is tagged ch02-evm, and every bug, script and result is in the mutation folder.

Every line ran, and a hidden fee still passed

Mutation testing asks which bugs a test suite catches. A mutant is a copy of the contract with one small change. The suite runs on each mutant: if at least one test fails, the mutant is killed; if every test passes, it survives. A survivor points to a possible gap in the tests.

A cartoon zombie walks through an airport security scanner holding a crayon drawing of a smiling face over its own face. The scanner's lamp glows green. A security officer beside the gate watches, frowning, hand on chin. A cartoon zombie walks through an airport security scanner holding a crayon drawing of a smiling face over its own face. The scanner's lamp glows green. A security officer beside the gate watches, frowning, hand on chin.
Would your tests let this one through?

Code coverage is more familiar, and the two answer different questions. Line coverage only tells you that a line ran during the tests. A killed mutant says something stricter: here is a specific wrong version of the code, and here is a test that rejects it.

Here is what the tests are checking. The pool holds two tokens, gold and silver. Its owner fills it once by calling initialize, and after that anyone can swap one token for the other. The pool tracks what it holds in two reserves, one per token. Each swap pays out as much as it can without letting the two reserves multiplied together, their product, go down.

The Haskell model from the second post defines what every swap should pay. The tests check the contract against it in two ways. Worked examples are single trades whose answers come from the model. Properties are general rules the model obeys, run as fuzz tests on random inputs. A third kind of test, the invariant run, has several traders make random sequences of calls and checks a set of conditions after every call.

The first suite had 30 tests. Foundry’s coverage tool reports 100% line coverage, 97.56% statement coverage and 92.31% branch coverage. Yet all 30 tests pass this mutant:

-amountOut = reserveOut * amountIn / (reserveIn + amountIn);
+amountOut = reserveOut * amountIn * 999
+    / (reserveIn * 1000 + amountIn * 999);

This is the line that decides how much a trader gets back. The changed version takes the whole payment but prices the swap as if only 99.9% of it had arrived, so the pool keeps the other 0.1% for itself. It is the formula Uniswap V2 uses to charge its 0.3% fee, set to 0.1%. The difference is that Uniswap announces its fee, and this pool never tells its traders.

The line that prices a swap runs. The price is wrong. Nothing fails.

Rounding hides the fee from the worked examples. Token amounts are whole units, so the pool drops the fractional part of every payout. On a pool of 1,000 gold and 4,000 silver, a swap of 100 gold is owed 363.6 silver, so the honest pool pays 363. The fee pool calculates 363.3 and also pays 363. A worked example with no rounding catches it: swapping 100 gold into a pool of 100 gold and 100 silver pays exactly 50, while the fee pool pays 49. The widget below runs both examples on both pools.

The fee passed every rule in the model, too

Rounding explains the worked examples. It doesn’t explain the properties, and every one of them passed with the fee in place. They had to. The model’s rules protect the pool: its product never goes down, it never runs dry, a round trip never makes a profit. A pool that keeps a little of every payment only makes its product larger, so it breaks none of them.

The model’s formula also means something none of its rules said: the pool pays out as much as it can. The payout is the largest amount that keeps the product from going down, so one unit more would lower it. That is now a fuzz test, testFuzz_OutputIsTheLargestThatKeepsTheProduct. It catches a pool that underpays as well as one that overpays, without repeating the formula.

This is the find I’d keep if I could keep only one. A mutant usually tests the tests. This one tested the specification. The model had passed its own properties since the second post, and the fee showed that they described a pool that could quietly cheat.

Agents and tools find different bugs, and you need both

Mutation tools make mutants by fixed rules: flip a comparison, delete a statement, swap an operator. The first of five I tried, Gambit, made 83. The first suite killed 82 of them, and one more test killed the last. That 31-test suite still passes the hidden fee. Against Gambit’s mutants and the agents’ bugs together, it catches 108 of 233, where the full suite of 71 tests that grew out of the rounds catches 203. A perfect score against one tool only means the tests kill that tool’s mutants.

The agents’ wrong pools are a different kind of bug. In the later rounds, 70% of them added new code instead of editing a line, such as a pool that remembers its first caller and sends the next trader’s payout there. No rule set writes that.

What each review round found

Five AI agents ran six rounds. Across all of them, the contract changed once: it now refuses to create a pool with the same token twice. Everything else went into the tests.

  • Round 1. The agents read the code and the tests and checked each other’s findings. Nothing pinned down the exact swap formula, which is where the hidden fee and the largest-payout rule came from. The fuzz tests also threw away every refused swap, so a pool that refused valid trades looked fine.
  • Round 2. A pool that pays correctly on its first swap, then one unit less on every swap after that. All 35 tests at the time passed it, because they checked the exact payout only on a pool’s first swap.
+    uint256 swapCount;
 ...
+        if (swapCount++ > 0) amountOut -= 1;
  • Round 3. From here on, the agents wrote wrong pools aimed at getting past every test. Before a swap, the trader approves how many tokens the pool may take, and one wrong pool drained a whole 500-token approval during a 100-token swap. The tests missed it because they always approved the exact swap amount.
  • Round 4. Alice trades first, and when Bob pays later, his payout goes to Alice. The bug is serious, but it took an agent inventing a pool that remembers its first caller.
  • Round 5. By now, beating the tests mostly took mistakes nobody is likely to make, like treating two tokens with the same symbol as the same token. The realistic one was a pool that skips taking payment when the trader gives an unlimited approval, which real traders use.
  • Round 6. The agents rated how likely each new mistake was. One found no realistic mistake at all, and another found only low-severity ones, so the rounds stopped.

It works the other way too. Five tool mutants that change what the pool does passed the full suite. Here is the one that matters most:

-        if (msg.sender != owner) revert NotOwner();
+        if (msg.sender < owner) revert NotOwner();

In Solidity, msg.sender is the caller’s address, and revert refuses the call and undoes everything it did. An Ethereum address is also a number, so with this mutant any caller whose address is higher than the owner’s can call initialize and fill the pool. Six rounds missed it because every outsider the tests tried happened to have an address lower than the owner’s, which is thoroughness in only one direction. Two of the five tools made this mutant, and test_InitializeIsOnlyForTheOwner now tries the highest possible address too.

Tools have a cost of their own. A mutant can change the bytecode without changing anything a test could see: a uint256 is never below zero, so amountIn == 0 and amountIn <= 0 are the same check. These are equivalent mutants, and 34 of the 51 tool mutants that got past the first suite were equivalent. Running a mutant takes under two seconds. Deciding whether a survivor is equivalent or a real gap took two separate agent reviews, and that is the expensive part.

When code is cheap to write, something has to say stop

Back to the ratio. Here is what the 670 lines do:

Exact counts
PartCode linesAgainst 65
Fixtures and helpers: setup, funding, the expected balances the tests track, the test token2233.4×
The model’s worked examples and properties, in Solidity1582.4×
The invariant run: the random traders, their checks, and the 8 invariants1472.3×
Contract-specific tests: initialize, the owner, decimals, donations, events, overflow1422.2×
Total67010.3×

The split between fixtures and the invariant run is approximate, because the random traders’ swap helper mixes call setup with checks.

The setup alone is 3.4 times the contract, and the copied model checks add another 2.4. Both were in the first suite, and both are fixed costs that a 65-line contract magnifies.

The problem was the growth:

Exact counts
StageTestsCode linesAgainst the contract
First version303665.8×
After round 2425358.2×
After round 46278412.1×
After round 57194014.5×
After closing the gaps6592814.3×
After the deep trim3867010.3×

Ratios use the contract’s size at each stage; the first version predates the same-token check. Test counts treat each invariant on each of the invariant run’s pools as a separate test, so Foundry reports a smaller total.

A reviewer can always invent one more wrong pool, and an agent can write it and the test that catches it for almost nothing. But code is never free. Every test has to be read, run and kept passing by every change after it. When writing is cheap, the limit has to come from somewhere else. Here it came from one question: how likely is this mistake in the real world?

The rounds stopped when the agents in round 6 found little that was realistic. The same question drove the cut. Two agents reviewed the suite and found that rounds 1 to 3 had caught slips a person could make, while the later rounds mostly chased bugs someone had to invent. So I cut deep, and checked the cut against all 502 bugs from the agents and the tools. The first draft of the trim stopped catching eleven mistakes a person could make, such as accepting a second initialize after a trade, so those tests went back in.

Before closing the gapsAfter closing the gapsAfter the deep trim
Tests716538
Code lines940928670
Caught, of 502426440427

The trimmed suite has 258 fewer code lines and catches 13 fewer bugs. It still catches every wrong price, wrong payment and wrongly allowed swap. What it lets through is mostly failing calls that report a different error, and edits nobody is likely to make.

Whether that trade is right depends on the project. This pool is a learning project, not one holding anyone’s money, so letting unlikely bugs through is fine. A pool holding real money would be different. There I’d keep inventing strange wrong pools, because an attacker will, and I’d want to know whether the properties really cover every way a payout can go wrong.

How far to go is a risk decision: how likely a mistake is, what it costs, and how much the project can afford to lose. The same goes for code. Agents tend to write a check for every case they can imagine, and whether a rare case deserves one depends on the same risk assessment.

So the 670 lines are mostly setup, plus the tests that survived that question. Mutation testing gave me two things: a record of what each test catches, which made cutting safe, and one rule the model was missing. The next chapter’s contract adds fees and liquidity shares, and it will show whether the setup stays near 223 lines while the contract grows.

☠︎ ::: END OF FILE — this never happened ::: ☠︎
[2026-11-27 00:00:00] SYSTEM: users who found the any-key: 0
[2026-11-27 00:00:00] SYSTEM: your read has been logged against your permanent record.
root@larts-server:/var/rants # logout  # don't let the airlock hit you
⛤︎
the circle is opened
☾ return to the archive ☽
☾ amm ☽ ☾ ethereum ☽ ☾ solidity ☽ ☾ foundry ☽ ☾ mutation-testing ☽ ☾ testing ☽ ☾ agents ☽ ☾ defi ☽ ☾ blockchain ☽
⛤︎   NEXUS   ⛤︎

The Making of an Automated Market Maker: Why So Many Tests?

☾   ⛤︎   ☽

Why does a 65-line Solidity contract have 670 lines of tests? Part of the answer is AI agents doing mutation testing, and one of their broken pools held a surprise: the model itself was missing a property.

November 27, 2026 ⛤︎ 12 min rite ⛤︎ 2358 words

The pool from the last post is 65 lines of Solidity. Its tests are 670 lines. One to one would already be a lot. Ten to one is crazy.

Some of it is setup: a tiny contract needs a lot of test code per line. The rest came from AI agents. The pool is ported from a Haskell model, and nothing yet runs the two side by side, so the tests are the only proof that they agree.

The agents’ job was to find holes in that proof. They kept writing broken versions of the pool that the tests missed, then wrote new tests to cover those blind spots. That is mutation testing done by agents.

It improved more than the tests: one broken pool showed that the model itself was missing a property.

This is the fourth post in a series about automated market makers, programs that let anyone swap one token for another. The code is tagged ch02-evm, and every bug, script and result is in the mutation folder.

Every line ran, and a hidden fee still passed

Mutation testing asks which bugs a test suite catches. A mutant is a copy of the contract with one small change. The suite runs on each mutant: if at least one test fails, the mutant is killed; if every test passes, it survives. A survivor points to a possible gap in the tests.

A cartoon zombie walks through an airport security scanner holding a crayon drawing of a smiling face over its own face. The scanner's lamp glows green. A security officer beside the gate watches, frowning, hand on chin. A cartoon zombie walks through an airport security scanner holding a crayon drawing of a smiling face over its own face. The scanner's lamp glows green. A security officer beside the gate watches, frowning, hand on chin.
Would your tests let this one through?

Code coverage is more familiar, and the two answer different questions. Line coverage only tells you that a line ran during the tests. A killed mutant says something stricter: here is a specific wrong version of the code, and here is a test that rejects it.

Here is what the tests are checking. The pool holds two tokens, gold and silver. Its owner fills it once by calling initialize, and after that anyone can swap one token for the other. The pool tracks what it holds in two reserves, one per token. Each swap pays out as much as it can without letting the two reserves multiplied together, their product, go down.

The Haskell model from the second post defines what every swap should pay. The tests check the contract against it in two ways. Worked examples are single trades whose answers come from the model. Properties are general rules the model obeys, run as fuzz tests on random inputs. A third kind of test, the invariant run, has several traders make random sequences of calls and checks a set of conditions after every call.

The first suite had 30 tests. Foundry’s coverage tool reports 100% line coverage, 97.56% statement coverage and 92.31% branch coverage. Yet all 30 tests pass this mutant:

-amountOut = reserveOut * amountIn / (reserveIn + amountIn);
+amountOut = reserveOut * amountIn * 999
+    / (reserveIn * 1000 + amountIn * 999);

This is the line that decides how much a trader gets back. The changed version takes the whole payment but prices the swap as if only 99.9% of it had arrived, so the pool keeps the other 0.1% for itself. It is the formula Uniswap V2 uses to charge its 0.3% fee, set to 0.1%. The difference is that Uniswap announces its fee, and this pool never tells its traders.

The line that prices a swap runs. The price is wrong. Nothing fails.

Rounding hides the fee from the worked examples. Token amounts are whole units, so the pool drops the fractional part of every payout. On a pool of 1,000 gold and 4,000 silver, a swap of 100 gold is owed 363.6 silver, so the honest pool pays 363. The fee pool calculates 363.3 and also pays 363. A worked example with no rounding catches it: swapping 100 gold into a pool of 100 gold and 100 silver pays exactly 50, while the fee pool pays 49. The widget below runs both examples on both pools.

The fee passed every rule in the model, too

Rounding explains the worked examples. It doesn’t explain the properties, and every one of them passed with the fee in place. They had to. The model’s rules protect the pool: its product never goes down, it never runs dry, a round trip never makes a profit. A pool that keeps a little of every payment only makes its product larger, so it breaks none of them.

The model’s formula also means something none of its rules said: the pool pays out as much as it can. The payout is the largest amount that keeps the product from going down, so one unit more would lower it. That is now a fuzz test, testFuzz_OutputIsTheLargestThatKeepsTheProduct. It catches a pool that underpays as well as one that overpays, without repeating the formula.

This is the find I’d keep if I could keep only one. A mutant usually tests the tests. This one tested the specification. The model had passed its own properties since the second post, and the fee showed that they described a pool that could quietly cheat.

Agents and tools find different bugs, and you need both

Mutation tools make mutants by fixed rules: flip a comparison, delete a statement, swap an operator. The first of five I tried, Gambit, made 83. The first suite killed 82 of them, and one more test killed the last. That 31-test suite still passes the hidden fee. Against Gambit’s mutants and the agents’ bugs together, it catches 108 of 233, where the full suite of 71 tests that grew out of the rounds catches 203. A perfect score against one tool only means the tests kill that tool’s mutants.

The agents’ wrong pools are a different kind of bug. In the later rounds, 70% of them added new code instead of editing a line, such as a pool that remembers its first caller and sends the next trader’s payout there. No rule set writes that.

What each review round found

Five AI agents ran six rounds. Across all of them, the contract changed once: it now refuses to create a pool with the same token twice. Everything else went into the tests.

  • Round 1. The agents read the code and the tests and checked each other’s findings. Nothing pinned down the exact swap formula, which is where the hidden fee and the largest-payout rule came from. The fuzz tests also threw away every refused swap, so a pool that refused valid trades looked fine.
  • Round 2. A pool that pays correctly on its first swap, then one unit less on every swap after that. All 35 tests at the time passed it, because they checked the exact payout only on a pool’s first swap.
+    uint256 swapCount;
 ...
+        if (swapCount++ > 0) amountOut -= 1;
  • Round 3. From here on, the agents wrote wrong pools aimed at getting past every test. Before a swap, the trader approves how many tokens the pool may take, and one wrong pool drained a whole 500-token approval during a 100-token swap. The tests missed it because they always approved the exact swap amount.
  • Round 4. Alice trades first, and when Bob pays later, his payout goes to Alice. The bug is serious, but it took an agent inventing a pool that remembers its first caller.
  • Round 5. By now, beating the tests mostly took mistakes nobody is likely to make, like treating two tokens with the same symbol as the same token. The realistic one was a pool that skips taking payment when the trader gives an unlimited approval, which real traders use.
  • Round 6. The agents rated how likely each new mistake was. One found no realistic mistake at all, and another found only low-severity ones, so the rounds stopped.

It works the other way too. Five tool mutants that change what the pool does passed the full suite. Here is the one that matters most:

-        if (msg.sender != owner) revert NotOwner();
+        if (msg.sender < owner) revert NotOwner();

In Solidity, msg.sender is the caller’s address, and revert refuses the call and undoes everything it did. An Ethereum address is also a number, so with this mutant any caller whose address is higher than the owner’s can call initialize and fill the pool. Six rounds missed it because every outsider the tests tried happened to have an address lower than the owner’s, which is thoroughness in only one direction. Two of the five tools made this mutant, and test_InitializeIsOnlyForTheOwner now tries the highest possible address too.

Tools have a cost of their own. A mutant can change the bytecode without changing anything a test could see: a uint256 is never below zero, so amountIn == 0 and amountIn <= 0 are the same check. These are equivalent mutants, and 34 of the 51 tool mutants that got past the first suite were equivalent. Running a mutant takes under two seconds. Deciding whether a survivor is equivalent or a real gap took two separate agent reviews, and that is the expensive part.

When code is cheap to write, something has to say stop

Back to the ratio. Here is what the 670 lines do:

Exact counts
PartCode linesAgainst 65
Fixtures and helpers: setup, funding, the expected balances the tests track, the test token2233.4×
The model’s worked examples and properties, in Solidity1582.4×
The invariant run: the random traders, their checks, and the 8 invariants1472.3×
Contract-specific tests: initialize, the owner, decimals, donations, events, overflow1422.2×
Total67010.3×

The split between fixtures and the invariant run is approximate, because the random traders’ swap helper mixes call setup with checks.

The setup alone is 3.4 times the contract, and the copied model checks add another 2.4. Both were in the first suite, and both are fixed costs that a 65-line contract magnifies.

The problem was the growth:

Exact counts
StageTestsCode linesAgainst the contract
First version303665.8×
After round 2425358.2×
After round 46278412.1×
After round 57194014.5×
After closing the gaps6592814.3×
After the deep trim3867010.3×

Ratios use the contract’s size at each stage; the first version predates the same-token check. Test counts treat each invariant on each of the invariant run’s pools as a separate test, so Foundry reports a smaller total.

A reviewer can always invent one more wrong pool, and an agent can write it and the test that catches it for almost nothing. But code is never free. Every test has to be read, run and kept passing by every change after it. When writing is cheap, the limit has to come from somewhere else. Here it came from one question: how likely is this mistake in the real world?

The rounds stopped when the agents in round 6 found little that was realistic. The same question drove the cut. Two agents reviewed the suite and found that rounds 1 to 3 had caught slips a person could make, while the later rounds mostly chased bugs someone had to invent. So I cut deep, and checked the cut against all 502 bugs from the agents and the tools. The first draft of the trim stopped catching eleven mistakes a person could make, such as accepting a second initialize after a trade, so those tests went back in.

Before closing the gapsAfter closing the gapsAfter the deep trim
Tests716538
Code lines940928670
Caught, of 502426440427

The trimmed suite has 258 fewer code lines and catches 13 fewer bugs. It still catches every wrong price, wrong payment and wrongly allowed swap. What it lets through is mostly failing calls that report a different error, and edits nobody is likely to make.

Whether that trade is right depends on the project. This pool is a learning project, not one holding anyone’s money, so letting unlikely bugs through is fine. A pool holding real money would be different. There I’d keep inventing strange wrong pools, because an attacker will, and I’d want to know whether the properties really cover every way a payout can go wrong.

How far to go is a risk decision: how likely a mistake is, what it costs, and how much the project can afford to lose. The same goes for code. Agents tend to write a check for every case they can imagine, and whether a rare case deserves one depends on the same risk assessment.

So the 670 lines are mostly setup, plus the tests that survived that question. Mutation testing gave me two things: a record of what each test catches, which made cutting safe, and one rule the model was missing. The next chapter’s contract adds fees and liquidity shares, and it will show whether the setup stays near 223 lines while the contract grows.

☾  ⛤︎  ────────  ⛤︎  ☽
so it is written · so it is bound
☾ return to the archive ☽
inkspell
© 2026 inkspell RSS