4. The loop#

Reset first, so your numbers match the ones printed here:

cd learn/mini
git checkout kb.json baseline.json
node loop.mjs --dry-run
1 collect    34 rows -> 21 need review (61.8%)
2 cluster    21 rows -> 11 clusters
               c1   4 rows  BLUE BOTTLE COF  (+1 variant)
               c2   3 rows  PHILZ COFFEE  (+1 variant)
               c3   3 rows  SPOTIFY P02XYZ1120  (+2 variant)
               c4   2 rows  WHOLE FOOD MKT
               c5   2 rows  AMZN MKTP US 2X4YZ8901  (+1 variant)
               c6   2 rows  TARTINE BAKERY  (+1 variant)
               c7   1 rows  PAYPAL *ETSYSELLER
               c8   1 rows  PAYPAL *PATREON
               c9   1 rows  PAYPAL *STEAMGAMES
               c10  1 rows  VENMO PAYMENT
               c11  1 rows  ZELLE TO JACK CARPENTER
3 propose    15 proposals from "fake"
4 validate   12 ok, 3 rejected -> 2 sent back with their errors, 2 repaired
4 validate   10 accepted, 3 rejected, 4 clusters skipped
               REJECT propose_merchant COF: pattern "COF" has fewer than 4 alphanumeric characters; it would match unrelated merchants
               REJECT propose_merchant TARTINE BAKERY: "Dining > Coffee Shops" is not in the taxonomy
               REJECT propose_merchant STEAMGAMES: a new merchant needs at least 2 rows of evidence; this cluster has 1
5 apply      4 written to a candidate KB, 6 held for review
6 gate       fixtures 5/5   review rate 50.0% (was 61.8%)   PASS

Read it top to bottom. That is an agentic system doing a full cycle, and the model was involved in one line of it.

Stage 2 is the one to study#

Twenty-one bad rows became eleven groups. That is not a tidying step, it is the step that decides what the model is able to say.

Look at c1. Four rows, two different renderings (BLUE BOTTLE COF and BLUE BOTTLE COFFEE), collapsed into one decision. If those had arrived as four separate items, the model would have been asked four times and could have answered inconsistently four times. Grouping turns four questions into one, and makes the answer worth four rows instead of one.

Look at what grouping is done on: not the raw text, but merchantSpan, the output of stage 3 of the classifier. The reference tails were already gone, the cities were already gone. Near-duplicates are then merged by trigram similarity, which is how BLUE BOTTLE COF and BLUE BOTTLE COFFEE end up in one bucket without anyone writing a rule about truncation.

And look at c7, c8, c9. Three PayPal rows, three separate one-row clusters, because the merchant part of each is genuinely different. The model will not be able to do anything useful with them, and it is right not to. There is something to learn from those three rows, but it is invisible from this angle. That is chapter 8.

The general lesson: when an agentic system gives bad output, look at what you handed it before you look at the prompt. Most "the model is not smart enough" problems are "the model was shown one row at a time when the pattern only exists across twenty".

What the model actually sees#

Stage 3 sends the cluster list and nothing else. Here is one cluster, verbatim:

{
 "id": "c1",
 "span": "BLUE BOTTLE COF",
 "n": 4,
 "variants": ["BLUE BOTTLE COFFEE"],
 "examples": [
  "SQ *BLUE BOTTLE COF SAN FRANCISCO CA",
  "SQ *BLUE BOTTLE COF OAKLAND CA",
  "SQ *BLUE BOTTLE COFFEE BERKELEY CA"
 ],
 "facets": ["channel:pos", "city:oakland", "city:san-francisco", "processor:square", "region:ca", "country:us"],
 "kb_fuzzy": []
}

And c4, which is the interesting one:

{
 "id": "c4",
 "span": "WHOLE FOOD MKT",
 "n": 2,
 "kb_fuzzy": [{ "name": "Whole Foods", "cat": "Groceries", "sub": "Supermarket", "score": 0.72 }]
}

kb_fuzzy is the knowledge base saying "I nearly know this one". It is a trigram comparison against every merchant name it already has. That field exists to push the model towards the cheapest correct edit: attach a new spelling to a merchant that already exists, rather than invent a second Whole Foods.

The instructions are in backends.mjs, in systemPrompt(). They are twelve lines, and they are mostly not about the task:

- Prefer propose_alias (a new pattern on an EXISTING merchant) over propose_merchant.
- A pattern must be a substring of the cluster span, at least 4 alphanumeric
  characters, and specific to that merchant.
- cat and sub must be exact strings from the taxonomy below. Do not invent labels.
- Pin every merchant or alias with a propose_fixture using one of the cluster examples.
- If the evidence is ambiguous, or the payee is a person, payroll or a transfer,
  call skip with the reason. Never guess.

Every one of those is also enforced in code in stage 4. Saying it in the prompt makes compliance likely; checking it in code makes violation harmless. You need both, and if you can only have one, take the code.

The four things it can write#

Not free text. Four shapes, and it must pick one:

propose_alias      a new pattern on a merchant that already exists    (the cheapest edit)
propose_merchant   a new merchant: pattern, name, category            (a claim about the world)
propose_fixture    a test that pins what was just decided
skip               this cluster needs no change, and here is why

skip is a first-class answer, and four of eleven clusters got it. An agent that cannot decline will invent something for every input you show it. Make refusal cheap and legible, and much of your bad-output problem disappears.

propose_fixture is the shape worth copying into your own designs. The model does not only make the change, it writes the test that pins the change. From then on, whether that decision still holds is a question code can answer forever, with no model and no judgement. Chapter 9 shows why this matters more than anything else in the loop.

The repair round#

4 validate   12 ok, 3 rejected -> 2 sent back with their errors, 2 repaired

Three proposals failed. Two were sent back with the rejection text and returned fixed: COF became PHILZ COFFEE, and Dining > Coffee Shops became Dining > Bakery. That is not the model being clever; it is a validator writing an error message specific enough to act on.

Compare the two messages you would have written:

invalid pattern
pattern "COF" has fewer than 4 alphanumeric characters; it would match unrelated merchants

The second one is repairable and the first one is not. When code checks a model's output, the error message is a prompt. Write it for a reader who will try again.

The third rejection stayed rejected. PAYPAL *STEAMGAMES is one row, and one row is not enough evidence for a new merchant no matter how confident anyone is. There is no repair for "not enough evidence", and the loop is right to leave it.

Two tiers, and the run that actually changed something#

5 apply      4 written to a candidate KB, 6 held for review

Ten proposals were accepted and only four were applied, because they are not the same kind of thing:

  • Applied automatically. Aliases and fixtures. An alias attaches a spelling to a merchant a human already approved; a fixture is a test. Both are additive, both are reversible, both are checked by the gate.
  • Held for a human. New merchants. "This string is a company called Blue Bottle Coffee and it sells coffee" is a claim about the world that the gate cannot check. Fixtures pinning a held merchant are held with it, since shipping a test for a row that is still waiting would just break the build.

Now run it for real:

node loop.mjs

kb.json gains four rows and the baseline moves to 50.0%. Read the report it wrote in runs/, which has the held proposals with their reasoning. Then, playing the maintainer who read them and agreed:

node loop.mjs --apply-all
5 apply      10 written to a candidate KB
6 gate       fixtures 7/7   review rate 14.7% (was 61.8%)   PASS

61.8% to 14.7%. Six merchants, one alias, four fixtures, no code.

Use a real model#

Everything above ran with canned answers, so it is reproducible and free. Swap in a real one and the five other stages do not change at all:

node loop.mjs --dry-run --apply-all --backend claude-cli     # uses your Claude Code login
node loop.mjs --dry-run --apply-all --backend anthropic      # needs ANTHROPIC_API_KEY

The live run on claude-cli reaches the same 14.7%, by a slightly different route: it proposed a merchant for PAYPAL *PATREON too and got the same evidence rejection, and it wrote two fixtures that turned out not to hold and were dropped at apply time. That variation is normal and it is the reason stages 4 to 6 exist.


Next: 5. The gates