5. The gates#

Stage 3 is the one people find interesting. Stages 4 and 6 are the ones that decide whether you can leave the thing running overnight.

They are different checks and it is worth being precise about the difference.

  • Stage 4, the validator, checks the proposal. Is this a well-formed thing to write down? It runs before anything is written, it knows nothing about outcomes, and its rejection is a sentence the proposer can act on.
  • Stage 6, the gate, checks the world after the change. Given that we wrote it down, does the system still do what we already agreed it should do? It runs the whole pipeline over everything and compares.

You need both, because each one catches things the other cannot see.

What the validator catches#

Read validate() in loop.mjs. Nine checks, and each exists because of a specific way this can go wrong.

if (alnum(P).length < 4)                    // "COF" would match unrelated merchants
if (!span.toUpperCase().includes(P))        // the pattern must appear in its own evidence
if (m.p.toUpperCase() === P)                // this pattern already exists
if (m.p.toUpperCase().includes(P))          // it sits inside another merchant's pattern
if (!(kb.taxonomy[p.cat] || []).includes(p.sub))   // invented label
if (nameTokensSubsetOfExisting)             // "Lazy Dog Restaurant" vs "Lazy Dog Restaurant & Bar"
if ((p._n ?? 0) < MIN_EVIDENCE_NEW_MERCHANT)       // one row is not evidence
if (!cluster_id)                            // a proposal with no evidence is not a proposal

Two of those deserve attention because they are not obvious and both come from real production failures.

The shadowing check. Lookup takes the longest matching pattern. So adding a short pattern can silently steal rows from a longer one that is already there, and nothing errors: the answers just quietly change. This is a class of bug where the new thing works perfectly and an old thing breaks in the dark.

The near-duplicate check. A model looking at LAZY DOG RESTAURANT when the base already contains Lazy Dog Restaurant & Bar will happily create a second merchant. Now you have two rows, two categories that can drift apart, and two icons. The right edit was an alias on the one that exists. In this repo's production loop the rule is: if the proposed name shares all its meaningful tokens with an existing merchant, refuse and say "use an alias".

Evidence is a check, not a vibe. Every proposal must cite the cluster it came from, and a new merchant needs a minimum number of rows behind it. This is the single cheapest guard against confident invention, and it does not depend on the model cooperating.

What the validator cannot catch#

node loop.mjs --backend file:examples/shadow-hijack.json --apply-all --dry-run
4 validate   1 accepted, 0 rejected, 0 clusters skipped
5 apply      1 written to a candidate KB
6 gate       fixtures 2/3   review rate 61.8% (was 61.8%)   FAIL
               FAIL wholefds-is-an-alias: merchant: want "Whole Foods", got "Wholefds Market"

Open examples/shadow-hijack.json. It proposes a new merchant called Wholefds Market with the pattern WHOLEFDS MKT. Every stage-4 check passes: the pattern is long enough, it is in the evidence, the category exists, the name shares no tokens with Whole Foods.

And it is wrong. WHOLEFDS MKT 10234 BERKELEY CA is a Whole Foods, and the longer new pattern now beats the WHOLEFDS alias that was correctly handling it. A fixture written months ago catches it in stage 6, and the batch is thrown away.

Nothing about the proposal was detectably wrong. Only its effect was. That is the whole argument for having a gate that runs the real system on real inputs rather than just inspecting the diff.

Apply in two stages#

let candidate = applyTo(kb, chosen.filter(d => d.proposal.tool !== 'propose_fixture'));
for (const d of fixtures) {
  const probe = applyTo(candidate, [d.proposal]);
  if (gateFails(probe, d.proposal.id)) dropped.push(d);   // drop the test, keep the batch
  else candidate = probe;
}

Knowledge rows go in first. Then each proposed fixture is screened against the state those rows produced, and one that does not hold is dropped on its own.

The reason is a failure mode you will hit: a model proposes nine good rows and one test with an expectation slightly off, and a naive gate fails the whole batch on the tenth item. You lose nine good edits to one bad assertion, every night. Dropping the unsatisfied fixture and reporting it keeps the batch alive and keeps the fixture set honest, because a fixture that does not hold was never a fact.

This is not hypothetical. The production loop in this repo grew exactly this mechanism, along with three others, in one day, after a reproducibility experiment kept sinking whole batches on single fixtures.

The ratchet#

baseline.json holds the review rate to beat. Any change may improve it or leave it alone. Nothing may make it worse.

That one rule is what makes unattended improvement safe over months. Individual edits can be wrong and be caught, but the danger with a self-editing system is slow drift: a hundred changes each costing a tenth of a point, and a year later it is worse than when you started and no single commit is to blame. A ratchet makes that impossible by construction, not by vigilance.

Two things to get right. Ratchet per slice, not just overall, or a big improvement in one area will hide a collapse in another. And when the batch fails, restore from a snapshot you took just before applying, not from the last commit, or you will also throw away whatever a human had in flight.

The autonomy ladder#

Not every change deserves the same treatment. Sort them by what could go wrong:

tierwhatwho decides
L0aliases, fixtures, noise rules that only strip bank formattingthe gate; merged automatically
L1new merchants, category changes, anything touching the merchant namea human, from the batch report
L2anything needing a code change: a new stage, a new kind of rulea human writes the code

The line between L0 and L1 is not "how confident is the model". It is can the gate check this. An alias either keeps the fixtures green or it does not, and the eval will say so. "This company sells coffee" is not a claim any test can settle, so it goes to a person no matter how sure everyone sounds.

L2 is the interesting one. When the loop finds something it cannot express with the tools it has, the right output is not a clever workaround. It is a written request for a new tool, with the rows that motivated it. In this repo that request becomes a ticket, a human implements the new operator with tests, and from then on it is available as data for every future batch. The system's vocabulary grows deliberately, through review, and the model never writes code.

One more gate: a second model#

The production loop has a stage the miniature does not: after proposing and before validating, every proposal goes to the same model again, in a fresh context, with one job, to argue against it.

It matters more than it sounds, because the second pass has no memory of wanting the proposal to be true. Here is a real rejection from a batch in this repo:

rejected: 'HOTEL SU' is a truncated two-token prefix, not a merchant identity. It will match any descriptor starting that way (Hotel Suites, Hotel Sunset, Hotel Super 8, Hotel Supply Co) and stamp all of them with the display name 'Hotel Su'. The span itself looks bank-truncated and the cluster carries no location, variant or KB corroboration to pin which property it is. Vacation > Lodging may well be right, but the identity claim is not supported.

No code check would have caught that. The pattern is long enough, the category is real, the evidence is there. It is wrong for a reason that requires reading. Put the critic where judgement is needed and code everywhere else, and be honest about which is which.


Next: 6. The same loop with no AI in it