9. The real thing#
The miniature is 500 lines. The production version of the same six stages is in this repo, and every idea in it has appeared in the previous eight chapters. Here is the map.
| chapter | miniature | production |
|---|---|---|
| 2 | classify.mjs | packages/sieve/src/pipeline/ (eleven stages) |
| 3 | kb.json | kb/merchants/, kb/normalizers/, kb/issuers/, kb/locations/, kb/validation/ |
| 3 | check.mjs | packages/sieve vitest + scripts/classifier-eval/ |
| 4 | loop.mjs | services/siftd/lib/loop/ (signals, cluster, agent, apply, batch) |
| 4 | backends.mjs | services/siftd/lib/loop/llm.js |
| 5 | validate() | services/siftd/lib/loop/workspace.js |
| 8 | shapes() | cluster.js, second pass |
| all | design contract: specs/services/classifier-loop/spec.yaml |
Same six stages, same tier ladder, same ratchet. The knowledge base is YAML instead of JSON, and it is compiled to TypeScript by scripts/build-kb.mjs so the classifier carries no parser and can run in a browser, in Electron and on a phone.
Run it#
# classify a bank export with the shipped knowledge base: no server, no model
./services/siftd/cli/siftd classify <file.csv> --out /tmp/run.csv
# one loop pass over that output: cluster, propose, critic, apply L0, gate, report
./services/siftd/cli/siftd loop run --classified /tmp/run.csv --dry-run
# apply the L1 proposals you read and agreed with, with no model call
./services/siftd/cli/siftd loop apply scripts/classifier-loop/batches/<id> --ids p1,p2
# the per-stage scorecard and the ratchet
bun scripts/classifier-eval/index.mjs
The loop needs a model. With ANTHROPIC_API_KEY set it uses the API; otherwise it uses your local claude binary and your Claude Code login, which is how most of the batches in this repo were produced. Same seam as --backend in chapter 4.
Read a real batch report#
scripts/classifier-loop/batches/<id>/report.md, from a run over twenty-six bank exports:
generated 2026-09-08 | model claude-opus-5 | signals 536 rows (596 person-payment rows dropped) | clusters 28
proposals: 29 (L0 3, L1 26); accepted 22, rejected 7, skipped clusters 10
apply: L0 | validation PASSED
Everything from chapters 4 to 8 is in that header. Anonymised signals with person payments dropped before anything sees them. Twenty-eight clusters from 536 rows. Three auto-applied and twenty-six waiting for a human. Seven rejected by the critic, ten clusters declined.
One accepted proposal, with the critic's verdict under it:
p8 [L0] alias
Pre-Authorized Transfer to Checking-> Internal Transfer 12 real rows. The KB already has Internal Transfer and fuzzy-matches at 0.769, but the row still resolves through the unknown path and is guessed as Shopping > Online Retail. Attaching this exact rendering makes it deterministic instead of fuzzy.critic: The pattern is bank transfer language, not a merchant, and it maps to the existing Internal Transfer merchant rather than inventing one. There is no unrelated-merchant risk, and making it deterministic removes the unknown path.
And one rejected, which is the more instructive half of any report:
p12 fixture american-expr-ach-pmt-cc-payment
critic: The expectation depends on an 'AMERICAN EXPR' issuer alias that is not part of this batch, and the only Amex evidence in c10 is a 0.6 fuzzy to a KB row that maps American Express to Banking Fees > Service Fees. As things stand nothing makes this row land in Transfer > CC Payment. Propose the alias first, then pin it.
A test for a change that is not in the batch. Every rejection in that report has the same character: internally sensible, wrong in context, and caught by something that read the whole picture.
Measure every stage, not the system#
One accuracy number tells you nothing about what to fix. The harness reports:
normalize exact-match rate of normalised text against fixtures
span merchant span F1, location span F1
lookup hit@1 where a merchant is known, false-hit rate where none exists
classify category, subcategory and type accuracy
facets precision and recall per facet
top_line review rate, per bank and per account type
Then the ratchet holds every one of them. This is where per-stage measurement earns its keep: one change in this repo raised category accuracy 3.7 points and dropped a merchant-span score 2.5 points, for a reason worth knowing. Fuzzy matching started returning the canonical merchant name while the corpus truth was the raw string. The span metric was measuring the wrong thing and the aggregate would have hidden both facts.
Current state on the 25,660-row corpus, rules only, no statistical model:
| start | now | |
|---|---|---|
| category | 68.2% | 72.2% |
| subcategory | 60.7% | 65.1% |
| review rate | 32.7% | 28.1% |
Those gains are entirely knowledge-base rows and normalizer rules. The classifier code did not get smarter.
What "reproducible" means for a system like this#
The last experiment is the one that changed how the loop is built.
siftd loop regen shelves everything the loop has ever learned: its merchant rows, its normalizer rules, its validation cases, its issuer aliases. It makes that stripped state the new baseline, keeps the shelved validation cases as the target, and reruns the loop on the same inputs to see how much of its own knowledge it rebuilds.
./services/siftd/cli/siftd loop regen --classified /tmp/run.csv
One iteration recovered 11 of 13 target behaviours, and the eval returned to the pre-shelving numbers to the decimal: category 72.2, subcategory 65.1, review 28.1, coverage 65.0. Five of six merchants came back with the identical pattern and the identical name.
The two misses are more interesting than the eleven hits. One merchant came back under a duplicate label that the taxonomy should not have contained (Gym where the original said Gym/Fitness), which is a defect in the taxonomy, not in the loop. The other, a five-row local restaurant, was simply not re-proposed. And an earlier attempt with narrower inputs recovered only 5 of 13, because the merchants it missed had no evidence in those inputs. Evidence is the input; without it there is nothing to rediscover.
The conclusion is one sentence and it generalises to any system that learns:
Rule text is not the invariant. Behaviour is, and the validation cases plus the eval are its specification.
The model may phrase the same fact differently on another run, and that is fine. What must not change is what the system does. Which means your fixtures are not tests of your code. They are the definition of the artifact, and they are the only thing that makes a self-editing system something you can reason about at all.
That experiment also forced four mechanisms, every one a bug the loop had been living with. Fixtures inherit the tier of what they pin. Apply runs in two stages and screens each fixture against the rebuilt state. Reverts restore a pre-apply snapshot rather than the last commit. And near-duplicate merchant rows are refused in favour of an alias. Chapters 5 and 8 describe all four as if they were designed. They were not. They were paid for.
What to steal#
If you are building something else and want the transferable part:
- Separate procedure from fact, and put the facts in a file. This is the decision everything else rests on. Code cannot be safely edited by a machine. A file of rows can.
- Put the model on the artifact, not in the request path. Ask once per new fact, write it down, serve the answer from the file forever. Cost, latency, offline use and determinism all follow from this one choice.
- Make the model's output the smallest legible thing. One row. A human must be able to read it, argue with it, and delete it.
- Spend your engineering on the four stages around it. What you collect, how you group it, what you check, what you compare against. Chapter 8's twenty-line clustering change raised the quality ceiling more than any prompt edit could.
- Write the definition of correct before the thing that improves it. Then make every improvement add to that definition. Your checks should get stricter as the system learns.
- Give refusal a first-class path, and a place to put what it could not do.
skipwith a reason. A request for a new operator. An agent that cannot decline will invent. - Tier by what your gate can verify, not by how confident anything sounds. If a test can settle it, automate it. If it is a claim about the world, a person reads it.
- Ratchet, per slice, and snapshot before you apply. Slow drift is the failure mode of self-improving systems, and it is the one nobody notices until it is a year deep.
Back to the start.