3. Knowledge is data#

Terms introduced herefixture, pattern, ratchet, lookup, alias, span, JSON

Step 4 failed because nobody had told the system that Blue Bottle exists. So tell it. Open mini/kb.json and add one line to merchants:

{ "p": "BLUE BOTTLE", "name": "Blue Bottle Coffee", "cat": "Dining", "sub": "Coffee" }
node classify.mjs --file rows.txt
  SQ *BLUE BOTTLE COF SAN FRANCISCO CA       Blue Bottle Coffee  Dining > Coffee
  SQ *BLUE BOTTLE COF OAKLAND CA             Blue Bottle Coffee  Dining > Coffee
  SQ *BLUE BOTTLE COFFEE BERKELEY CA         Blue Bottle Coffee  Dining > Coffee
  BLUE BOTTLE COFFEE #0142 SAN FRANCISCO CA  Blue Bottle Coffee  Dining > Coffee

34 rows, 17 need review (50.0%)

Four rows fixed. Review rate 61.8% down to 50.0%. No code changed.

Stop here for a second, because this line is the whole course.

That one line of JSON is the complete output of the AI in this project. Not a category for a transaction. Not an answer to a user. A row in a text file, of the kind you just typed by hand, that a human can read, argue with, and delete.

Everything from chapter 4 onwards is about how to get a machine to write that line and how to be sure it did not write a bad one. The line itself is not clever, and that is exactly why the design works: the model's output is small, inspectable, reversible, and worth exactly the same at 3am as it is under review.

Notice also that the pattern is BLUE BOTTLE, not the full string. It caught four different renderings, including two the bank truncated differently, because the lookup takes the longest matching pattern anywhere in the span. Choosing the pattern is the skilled part of writing the row: too long and it misses variants, too short and it swallows unrelated merchants. Chapter 5 is mostly about that.

An alias is not a special thing#

Look at these two rows, already in the file:

{ "p": "WHOLE FOODS", "name": "Whole Foods", "cat": "Groceries", "sub": "Supermarket" },
{ "p": "WHOLEFDS",    "name": "Whole Foods", "cat": "Groceries", "sub": "Supermarket" }

There is no alias table and no alias record type. An alias is just another row with the same name. That is worth a design note: the cheapest possible edit to this system is "add a row that points at a merchant that already exists", and it required no schema at all. When you design the data shape, aim for the most common improvement to be the least structured thing you can write down.

Now define "correct"#

You have a knowledge base you can edit and a number that moves. You are one step away from a machine that edits it. Before you take that step, write down what must never break.

At the bottom of kb.json:

{
  "id": "wholefds-is-an-alias",
  "raw": "WHOLEFDS MKT 10234 BERKELEY CA",
  "why": "An alias is just another merchant row with the same name.",
  "expect": { "merchant": "Whole Foods", "cat": "Groceries", "sub": "Supermarket" }
}

These are fixtures: one input, and the parts of the answer that are pinned. They are ordinary tests, with two differences that matter.

They live next to the knowledge, not next to the code, because they are pinning a fact rather than a function. And they only check the keys they mention. A fixture that says nothing about tags is not claiming the tags are wrong; it is declining to have an opinion. Silence is not an assertion, which is what lets you add a hundred narrow fixtures without them fighting each other.

node check.mjs
PASS  starbucks-store-number
PASS  wholefds-is-an-alias
PASS  square-prefix-is-evidence

fixtures    3/3
review rate 61.8% (21/34)   baseline 61.8%

GATE PASSED

Two questions, both answered without a model. Do the fixtures still hold. Did the review rate get worse. baseline.json holds the number to beat, and it only ever moves down, so the second question is a ratchet: a change is allowed to improve things or leave them alone, never to make them worse.

That is check.mjs, forty lines, and it is the reason the next chapter is safe. An autonomous agent editing a system with no gate is a liability. The same agent editing a system with a gate is an employee with a code review.

The state you are in#

  • A pipeline that does four of the five steps and knows it fails the fifth.
  • A knowledge base where one line fixes four rows.
  • A definition of correct, and a number that only goes down.
  • Twenty-one rows nobody has written a line for yet.

Writing those lines by hand works, and for the first few hundred it is the right call. It stops working at the scale where new merchants appear faster than you can read them. So do not hire a model to be clever. Hire it to do the typing.


Next: 4. The loop