Hardening a billing guard in Python automation — the e-file-without-invoicing bug

jesse@cvps : ~/blog — zsh
$ git log –oneline -5 | grep efiling
// TECHNICALLY SPEAKING

Hardening a billing guard in Python automation — the e-file-without-invoicing bug

One guard that almost let a job go out the door without a fee attached — and how I closed the gap.

## THE HOOK

The Open Source AI Must Win argument is mostly about who controls the model weights. I get it. But out here running a one-man process-serving operation, the fight I care about is simpler: does my code do exactly what I told it to do, every time, with no surprises? Yesterday’s session was a reminder that the answer is only ever “yes, until it isn’t.”

## WHAT BROKE

I have a guard in the e-filing module whose whole job is simple: don’t proceed unless a $30 fee is already on the invoice. I’d written it once, tested it once, and moved on. Then a review pass surfaced a gap — there was a code path where the guard could be reached before the invoice check had actually resolved. Not “wrong answer.” Just… skipped entirely. The guard wasn’t lying. It was just getting bypassed on a timing edge.

A related issue: the confirmation email was going out on the court’s ruling event instead of on our own SUBMITTED event. So a client would hear nothing from us until the court came back. That’s a bad client experience and it’s fixable.

## THE FIX

– proceed_if_invoiced() # called once at entry; bypassed on fast re-entry path
+ auto_ensure_fee() # idempotent — runs inline, enforces before any action

The new function is idempotent. Call it ten times, same result. It doesn’t check whether the fee was attached at some earlier point — it ensures the fee is attached right now, inline, before anything downstream runs. That’s the pattern I keep relearning: a guard that can be bypassed isn’t a guard, it’s a suggestion.

# simplified shape — real version is deeper in the pipeline
def auto_ensure_fee(job):
    if job.fee_line_exists(“efile”):
        return # already good
    job.add_fee(“efile”, amount=30_00) # cents — Square format
    job.flag_for_review(“fee auto-added before efile”)

## THE LOG AFTER

Leave a comment