7. Layer one: the same string means two things#
Everything so far assumed a description means one thing. Here is the line that breaks that assumption, from a real bank export:
ELECTRONIC PAYMENT RECEIVED-THANK YOU +250.00
On a credit card account that is you paying your card bill. Money leaving your checking account, arriving here, reducing what you owe. It is a transfer between two of your own accounts and it is not income.
On a checking account the identical string with the identical sign is money arriving from outside. It might be a refund, a client payment, a reimbursement. It is certainly not a credit card payment.
Same characters. Same amount. Same sign. Two different correct answers.
Try it#
Add this to merchants in kb.json:
{ "p": "ELECTRONIC PAYMENT RECEIVED", "name": "Card Payment",
"cat": "Transfer", "sub": "Internal", "when": { "account_type": ["credit"] } }
node classify.mjs "ELECTRONIC PAYMENT RECEIVED" --account credit
node classify.mjs "ELECTRONIC PAYMENT RECEIVED" --account checking
node classify.mjs "ELECTRONIC PAYMENT RECEIVED"
credit Card Payment Transfer > Internal
checking - Shopping > Online Retail NEEDS REVIEW
(none) - Shopping > Online Retail NEEDS REVIEW
Three inputs, three answers, one row of knowledge. The when clause is six lines in classify.mjs:
export function contextAllows(when, ctx) {
if (!when) return true;
return Object.entries(when).every(([k, allowed]) => {
const v = ctx[k];
return v != null && [].concat(allowed).map(String).includes(String(v));
});
}
The third case is the one to look at hardest. With no context known, the row does not apply and the system says it does not know. It does not guess the more common reading. A fact learned under a condition is only asserted when the condition is known to hold, and an unknown context value never satisfies a listed key. Getting that default wrong is how you end up labelling a stranger's income as a card payment.
Why this is the characteristic failure of a learning loop#
Write down what happened when this row was first learned. Somebody, or some model, saw twenty-four rows, all from an American Express export, all saying ELECTRONIC PAYMENT RECEIVED, all card payments. The fact was true. It got written down. And it got written down without the condition that made it true, because the condition was the same on every row it had ever seen and therefore invisible.
That is the shape to watch for, and it is not specific to transactions. A loop that learns from evidence will systematically drop the parts of the context that were constant across its evidence. Constant means invisible. Invisible means unrecorded. Unrecorded means asserted everywhere.
Three mechanisms address it, and they work together.
Context is an input to a fact, never folded into it. Every learned row either declares its conditions or claims to hold everywhere. There is no third option and no default of "probably fine". In this repo's production knowledge base, when: accepts bank_format, account_type, account_country, transaction_country and amount_sign.
A proposal whose evidence spans two contexts with different outcomes gets rejected. Not amended, rejected. That contradiction is not noise to be resolved by picking the majority. It is the discovery that the fact is context-dependent, and the correct response is to go back and find out which condition separates the two groups. The critic's rejection text says so, and it becomes the input to the next round.
Fixtures carry their context, and get replayed under every other one. A fixture says "this raw, under this context, gives this answer". The test suite then runs it under every other context in the vocabulary and expects silence: no accidental assertion outside where it was pinned. That replay is how you catch a when clause that is too loose, which no amount of reading the diff will show you.
Two kinds of context, and they disagree on purpose#
There is a distinction here that took the production system a real-data run to learn.
Account context is what the bank told you: which format the file was in, what kind of account, which country, which currency. It is stamped by the parser as a fact, it is never inferred from the description, and it is what the payment-rail rules read. A row from a credit-card CSV is a credit-card row, full stop.
Transaction context is derived from the description: which country, region and city the purchase happened in, whether it was a card tap or an ACH pull, which processor handled it. When the text says nothing, it falls back to the account's country.
A US credit card used in London is account_country: us and transaction_country: gb, and both are correct at the same time. They are not competing estimates of one value. They answer different questions, and different stages read different ones: the merchant lookup uses the transaction country to pick which country's merchant list to search, while the card-payment rules use the account type. Collapse them into one "country" field and you will spend a week finding out why London coffee shops are being matched against American brands.
And the fact that the answer can go stale#
Once knowledge is data that improves weekly, every stored answer carries a hidden timestamp. A transaction classified in March was classified by March's knowledge base, under March's understanding of the context.
So store the derivation with what produced it: the knowledge base version and the context snapshot, alongside the derived category. Then a stale answer is detectable and can be recomputed in bulk. And keep the derived answers in a different field from anything the user asserted themselves, because a recompute must be free to overwrite its own past output and must never touch a human's.
Two words worth keeping: derived outputs are recomputed, never stored as truth.