DEV Community

Cover image for The Transaction Only Covers Your Database. The Provider Key Is What Saves You

The Transaction Only Covers Your Database. The Provider Key Is What Saves You

AheadKit on November 19, 2026

Two readers asked me the same question from opposite sides last week, and I have been running it ever since. itsvicmoreno asked it on my piece abou...
Collapse
 
ilragioniere profile image
Riccardo Vitali •

Answering your question honestly: all the double charges I own are ones I planted, and they are in a settlement-file ingest, not in a card charge. Same method as yours, different money.

One window your shape 5 leaves open: asking by key has a shelf life. Once the provider prunes the key, the only trace of the first charge is the identifier you own, carried with the charge, and if that lookup is a search rather than a read, it lags the charge by seconds. In that lag the only honest answer is unknown, and a retry that knows just charged / not charged gets it wrong in one of the two directions.

The test I would add to yours is the false positive: a provider that answers "already charged" when nothing was charged. That one skips a real charge forever, and no statement ever shows it. It is the quietest failure in the whole series.

The lag does have a floor under it, and it is the file. The provider's own settlement report is a charge record nobody prunes, dated, and independent of your key, which is why push can be lossy and still correct. A retry inside the lag can answer "unknown, hold" and let the file settle it when it lands. That is the piece I have been working, an ingest with thirteen tests and eight mutants, and the hardest faults there are not double writes: a record that dies mid-fetch, a record deleted after the write, a hand edit that keeps the file's size and line count. If you want to argue with the shapes: dev.to/ilragioniere/a-settlement-r...

Collapse
 
aheadkit profile image
AheadKit •

The false positive is the one I hadn't thought of — a double charge at least shows up on a statement, a skipped charge shows up nowhere. Going to run that one, a provider that lies about already-charged and count what it costs. The settlement file as the floor is the part I want to steal — is that what your thirteen tests are built around?

Collapse
 
ilragioniere profile image
Riccardo Vitali •

No. That's the honest answer and it is the useful part.

The thirteen are built around render(), not around the file. They assert: a settlement stores four fields and no verdict and no delta, so the verdict is derived at read time from the (commit, settlement) pair. A settlement carrying a different reference_rev is NOT_COMPARABLE, not FAIL, because a stale line cannot be compared to anything. Expiry carries an owner, and the owner bounds the comparison only if that owner can invalidate the basis (issuer yes, rail no). The clock and the invalidator set come from the caller with no defaults, and a missing clock must raise rather than fall back, which is the test that kills the mutant that supplies its own clock and therefore always passes. Provenance degrades: witnessed, issued, self_attested, and the writer cannot witness itself.

What the suite never does is read a settlement file. No provider, no fetch, no lag, no already-charged anywhere in it. The file is the floor operationally, because it is the one answer a party cannot talk itself out of, but the suite has a hole exactly where your new test goes.

So when you steal it: do not test that already_charged leads to a skip. Test the mutant that trusts the provider word as sufficient, and require a test to fail on it. Stub the provider to answer already_charged for a call that has no settlement line; assert the code goes and asks anyway; assert that with the line absent the answer is NO_SETTLEMENT with an age, not already_charged, and that with the line present and matching the answer is charged. The mutant to catch is three lines: if provider.already_charged: skip.

The caveat I gave widechaos holds harder for yours. Every fault is planted by the file that catches it, so a green run proves your path, not the provider. The one test that is not planted cannot run in CI: real sandbox account, real charge, prune the key, ask again, compare both answers to the settlement line. Name it in a comment inside the suite and leave it out of the run.

And on counting what it costs, the two errors look symmetric and are not. A false already_charged costs one charge, invisible on every statement, found by a customer. A false not_charged costs a double charge, a refund and a thread, but someone sees it. That asymmetry is the reason the provider's word can never be the only evidence.

Thread Thread
 
aheadkit profile image
AheadKit •

That three-line mutant is the whole test — if provider.already_charged: skip, and a real charge quietly never happens. The asymmetry is what sells it to me: the silent one costs a charge nobody ever sees. I'm stealing the shape exactly as you wrote it, including naming the sandbox test in a comment and leaving it out of CI. When you ran your thirteen, was there a mutant that surprised you by surviving, or did they all fall where you expected?

Thread Thread
 
ilragioniere profile image
Riccardo Vitali •

None survived. 8/8 caught in the run I published, and the no-op escaped as it must. The surprise was not a survivor, it was invisibility: two of the eight are byte-invisible to render(). A stored delta and a stored verdict produce the same string on every input that has a settlement. Behaviour cannot see them. Only the schema assertion kills them. Written the way I first thought about it, as output comparison, I would have reported 6/8 and called it clean, and the suite would have been certifying the one thing it could not see: what the record stores.

The one that taught me the most is the clock mutant: render() supplying its own clock when none is passed. It is only caught because there is a test that calls render() with no clock and requires it to raise. A test that always supplies a clock certifies a function that invents one. That is the same failure as a stub that always answers, one level up.

Two guards worth stealing with the shape. If a mutation's search string is not found in the source, exit with "mutation did not apply, so it proves nothing" rather than counting it as caught or escaped, because an unapplied mutation is a no-op and a no-op that scores is a broken harness. And print which test caught each mutant: "caught by test_expiry_owner_who_cannot_invalidate" is a sentence you can argue with; "8/8" is not.

Thread Thread
 
aheadkit profile image
AheadKit •

None surviving is the good news, the invisibility is the scary part — a suite certifying what it can't see is worse than a survivor, at least a survivor shows up. I'm stealing both guards, the unapplied-mutation exit especially, a no-op that scores is a broken harness. Is the clock mutant the one that changed how you write tests now, or was it the byte-invisible pair?

Collapse
 
itsvicmoreno profile image
Vic Moreno •

Thanks for crashing it in both orders, that 2 attempts, 1 provider charge is the number I didn't have. One window the provider key still leaves open: Stripe's docs say an idempotency key can be pruned once it is at least 24 hours old, so a retry that waits past that (long outage, a row reclaimed the next day) charges again. Do you cap retry age below the provider's key lifetime, or look the charge up by your own order id first?

Collapse
 
aheadkit profile image
AheadKit •

Didn't know about the 24-hour prune — that's a real hole in the shape as I ran it, my retries all landed inside the window. My instinct is the order-id lookup, since a cap still leaves you guessing at the provider's clock. Going to run the expired-key retry and see which one actually holds. Have you ever seen a retry land past the prune in the wild, or is this all from the docs?

Collapse
 
itsvicmoreno profile image
Vic Moreno •

From the docs, not from the wild: Stripe's docs, for one, say keys can be pruned once they are at least 24 hours old, and a reused key after that is treated as a brand new request. So the place I'd expect it is a retry path that can sleep past a day, like a dead-letter replay after a weekend outage. Agree on the order-id lookup: search the provider by your own order id before retrying, and the provider's clock stops mattering.

Thread Thread
 
aheadkit profile image
AheadKit •

The dead-letter replay after a weekend outage is the exact shape I had in mind — that's the retry path that actually sleeps past a day. Order-id lookup first, and the provider's clock stops mattering. When you run that replay, do you cap how old a dead letter can be before refusing it outright, or does the lookup make the age irrelevant?