Every system that hands work off to a background process needs a janitor. Something has to come around on a schedule and clean up the jobs that started and never finished, because if nothing does, those jobs sit there marked "running" forever and block whatever comes after them.
My setup has one. It runs on the Mac Mini that does the heavy lifting for my apps, once an hour, and it walks through a list of job tables. Theme generation, SEO analysis, domain checks, image renders. Anything that has been stuck for more than an hour gets marked failed with a plain message, so the page that was waiting on it can stop waiting and the user can try again.
Last week I added three more tables to that list. Then, for four days, the janitor failed every single hour, and nothing told me.
The change
The review I ran on two big tasks turned up a real gap. Three kinds of work in a couple of my personal apps (recommendation rounds, PDF parsing, and research runs) only got cleaned up when someone had the page open. The page itself would notice a job had been running too long and end it. But if nobody was watching, a stuck job stayed stuck, and a stuck recommendation round blocked new rounds forever.
The fix was obvious: put those three tables in the hourly sweep. It went in on November 18 alongside a pile of other review fixes. The checklist item got its checkbox. Done.
Here is roughly what the new block looked like:
const appRows = {
movie_suggestion_jobs: await sql`UPDATE movie_suggestion_jobs ...`,
ht_lab_jobs: await sql`UPDATE ht_lab_jobs ...`,
ht_research_jobs: await sql`UPDATE ht_research_jobs ...`,
};
The middle name is wrong. The PDF parsing table is called something else, and there is no table by that name anywhere in the database.
Where the wrong name came from
This is the part I find most useful. Nobody fat-fingered it. The name came from a design doc.
A few days earlier I had written up a task about making the app chats feel responsive, and in the section describing the current state of things, it says:
Lab parsing (
ht_lab_jobs): checks every 3 s, each one an HTTP call to the Mini.
That line was close enough to be true when it was written. The other tables in that app really do share a prefix. The PDF parsing one just doesn't. It's older and was named before the prefix convention existed. The doc guessed at the pattern, and the guess looked right.
Then the follow-up work was written against the doc. The agent writing the code had the doc as context, saw a table name in backticks, and used it. My review step checks the code against the doc's requirements. The doc and the code named the same table. They matched perfectly. Everything agreed with everything except the database.
That's the gap. A review that compares code to a spec can only catch the code drifting from the spec. It cannot catch the spec being wrong about the world. And raw SQL table names are exactly the kind of thing the type checker never sees, because to TypeScript that query is just a string.
Why it was quiet
The sweep is wrapped in a try/catch that logs the error and moves on, which is the right call for a background loop. One bad table shouldn't take down the worker. And it did log. The worker log has 97 lines that read:
hsw-infra stale sweep error: relation "ht_lab_jobs" does not exist
The first one is at 22:00 UTC on November 18. The last is at 18:12 UTC on November 22. Four days of the same line, roughly one an hour, in a log file I don't read unless something is already broken.
The design made it worse in a quiet way. The sweep only logs on success when it actually swept something. A healthy sweep with nothing to clean says nothing. A broken sweep says one line into a file. From the outside, both look the same: silence.
There's a second detail. Those three updates ran in order, each one awaited inside the object literal. The recommendation update ran first and worked. The PDF parsing update threw. So the research update, which came after it, never ran at all. One wrong name took out two of the three things I had just added, and the one that survived only survived because of where it sat in the list.
What it actually cost
Nothing, this time. I checked the tables afterward. Every PDF parse in the database is either completed or failed for a normal reason. Same for research jobs. Nothing got stuck during those four days, so there was nothing for the sweep to clean.
That's the uncomfortable part. The bug did no damage, and that's exactly why it could have lasted much longer. The checkbox said the problem was solved. The first time it would have mattered would have been the first time a PDF parse hung on a night nobody was looking, and the job would have sat there exactly the way it did before the fix, while I believed it couldn't.
The actual fix was a two-line diff. Rename the key, rename the table.
What I'm taking from it
A few things, none of them dramatic.
Design docs describe the world, and they can be wrong about it. When a doc names a table, a column, or an endpoint, that name is a claim. If code gets generated from the doc, the claim gets copied into code with full confidence. The fix is cheap: anything that names a real database object should get checked against the real database once, before it becomes a requirement.
A janitor needs to report in. A background job that only speaks when it finds work, or when it fails into a log file, is indistinguishable from one that isn't running. The sweep should write something somewhere I actually look, like a "last clean run" timestamp, so silence means healthy and not "unknown."
Tests that touch the database earn their keep on exactly this kind of code. A single test that ran the sweep against a real schema would have failed on the first run. My review process checks requirements against code; it doesn't execute anything. For most changes that's fine. For a raw SQL statement against a table I've never typed by hand, it's the one thing that would have caught it.
The sweep runs clean now. I know that because I went and looked. Next time I'd like the system to tell me.

