Originally published on michael-kaminski.io.

In 30 seconds

  • What: svelte-accounting-agent holds every approval for 10 seconds so the reviewer can undo it before the ledger is written.
  • The math: waiting with a 1-second poll costs 730 h × 0.25 CU = 182.5 CU-hours a month on a database that allows 100. Waiting with a timer costs nothing.
  • How: one timer per company, armed at write time for the earliest deadline. The deadline lives in the row, so a lost timer is a late commit, never a lost one.
  • The cost: the timer path has no test of its own, a deploy can stretch the window to about 40 seconds, and the cold-start number is still unmeasured.

A bookkeeping agent I pushed to GitHub on 2026-11-21 gives the reviewer ten seconds to undo any approval before it writes the ledger. The obvious way to build that is a loop that asks the database every second whether anything is due. On Neon's Free plan that loop costs 182.5 CU-hours a month against a 100 CU-hour allowance. The agent uses timers instead and the undo window costs nothing.

The conclusion first: a deferred action should be a timer armed at write time, not a poll, because the poll is paid for by the idle hours and the timer is paid for by the busy ones. On a database that bills by compute-hour and sleeps when nobody talks to it, that difference is the whole budget.

The project is svelte-accounting-agent, public, 24 commits between 2026-11-19 and 2026-11-21. It codes bank and card transactions to QuickBooks accounts for a small resale business of mine, shows its reasoning, and never writes the books without a recorded human approval. SvelteKit, Postgres on Neon through Drizzle, Claude through the API, one Fly machine. Every number below is from the repository as cloned on 2026-11-22 at f802a27, or from a test run I did the same day.

The arithmetic: 182.5 CU-hours versus 100

Neon's Free plan gives each project 100 CU-hours of compute a month and scales a compute to zero after five minutes of inactivity. The five-minute timeout cannot be disabled on Free. The smallest compute is 0.25 CU. Those are Neon's published terms as of a documentation page last updated 2026-11-18.

A month is about 730 hours. A scheduler that queries the database once a second never lets it sleep, so it burns 730 × 0.25 = 182.5 CU-hours. That is 1.8 times the entire allowance before the app has served a single request.

The same app with no polling sends zero queries while idle. Neon sleeps. The compute it uses is whatever the reviewer's own actions cost, plus any scheduled jobs. The repository's estimate for the optional hourly ledger pull is 24 × 30 wake-ups of five minutes each, 60 hours × 0.25 CU = 15 CU-hours, plus roughly 10 CU-hours of real usage, about 25 CU-hours. A quarter of the cap instead of nearly double it.

Bar chart of CU-hours per month. A one-second poll uses 182.5. Timers use about 25: 15 for the hourly pull and about 10 for real usage. The Free plan allowance is 100.

The poll is over the allowance before the first request. The timer version spends most of its budget on the optional hourly pull, not the undo window.

Nothing about the undo window changed between those two numbers. Only the way the app waits.

What the undo window is

Approve has two modes. approve now writes the ledger immediately. approve --later stamps the entry with pending_until, the current time plus UNDO_WINDOW_MS, which defaults to 10,000. The entry moves to approved and nothing external happens yet.

Undo inside the window flips it back to recommended, clears pending_until, and records an undone decision. Undo after the window returns HTTP 409 undo_window_closed, with the state in the message, because by then the write to the ledger has started and a second system now owns the entry.

The deadline lives in the database row, not in memory. That is the detail the rest of this depends on. Any process that can read pending_until can finish the job, which is what makes the timer safe to lose.

There was a cheaper-looking design: approve immediately, and on undo call the ledger's "put it back in review" endpoint. The decision log rejects it on 2026-11-19 because undoing after mark-ready races the ledger's own sync to QuickBooks. A deferred commit is the only version where undo never has to unwrite anything.

One timer per company, re-armed on every write

The scheduler is 69 lines. The core of it is armCommitTimer: select the minimum pending_until among this company's approved and coded entries, and set one setTimeout for that moment plus 50 milliseconds of slack.

If a timer already exists and fires no later than the new deadline, do nothing. If the new deadline is earlier, clear the old timer and arm the new one. If nothing is pending, clear the timer and remove it from the map.

It is called from three places: after every API write in the request handler, because an approve or undo may have opened or closed a window; after each commit run, because the commit may have left later entries behind; and once per company at boot. When the timer fires, it runs commitDue inside that company's row-level-security context, which selects everything whose pending_until has passed and commits it, then re-arms.

That is the entire mechanism. A company with no pending approvals has no timer, and the process makes no query on its behalf. Ten seconds after the last approve, the map is empty again.

The timers are unref'd, so they never keep the Node process alive during a shutdown. The deadline is in Postgres, so a shutdown loses nothing but the alarm clock.

Why the undo window costs zero compute

Neon stays awake for five minutes after the last query. A reviewer approving an entry is a query, so the compute is awake when the window opens. The commit fires ten seconds later, well inside the five minutes the compute would have stayed up anyway. The window adds no wake-ups and extends no sessions.

Timeline. The approve at zero seconds wakes the compute. The commit fires at ten seconds. The compute stays awake until five minutes after the last query either way, then sleeps.

The ten seconds sit inside five minutes the compute was going to stay awake for anyway.

A one-second poll inverts that. The poll keeps the compute awake during the 23 hours a day when nobody is reviewing anything, to detect deadlines that only ever exist in the ten seconds after someone clicks. The cost is proportional to idleness. The timer's cost is proportional to use. For a tool a small business touches for a few minutes a day, that is the difference between 182.5 and about 25.

Recovery and the second machine

Two things can make a timer disappear: the process restarts, or a second copy of the process is running during a deploy. Both are handled, and one of them has a cost I want to name.

At boot, the scheduler runs commitDue for every company and re-arms. Anything whose window expired while the process was down is committed on the first sweep. A deadline is never lost, only delayed by however long the process was off.

Only one machine may run jobs. Each process tries pg_try_advisory_lock with a fixed key on a dedicated reserved connection and holds it for its lifetime. The holder is the leader; a follower retries every 30 seconds. When the old machine exits during a deploy, Postgres frees the lock and the new one takes it on its next attempt, then runs the boot sweep.

This is also one reason the app uses Neon's direct endpoint instead of the pooled one: a session-level lock and the per-request session settings that row-level security depends on both die under transaction pooling.

The cost: armCommitTimer is a no-op on a follower. During a deploy overlap, an approval handled by the follower arms nothing. It is caught by the new leader's boot sweep, which can be up to 30 seconds after the old leader exits. So in the worst case a ten-second undo window becomes a forty-second one, during deploys only. I accepted that over a second table of distributed timers.

What the tests prove, and what they do not

I ran the write-path and learning suites on 2026-11-22 from a fresh clone: 16 tests, 16 passed, 20.9 seconds under Vitest on Node 22.23.5 against an in-process Postgres (PGlite). Two of them are the window.

"Approve later plus undo inside the window: no ledger write" approves with --later, runs commitDue, asserts zero writes to the ledger fixture, undoes, and asserts zero writes again. "Approve later: commits once the window passes" sets the window to 0 milliseconds, waits 5, runs commitDue, and asserts exactly two writes in order: update the entry, mark it ready.

Both tests call commitDue directly. The scheduler short-circuits under the test runner, so the setTimeout path, the re-arming, and the advisory-lock election are not covered by a test. They are covered by the boot sweep, which makes any timer bug a delay rather than a loss. That is the kind of safety net you should have before you trust an alarm clock, and it is also an admission that the alarm clock itself is untested.

The numbers in the repository's operating envelope were measured on 2026-11-21 on a local production build: 3 milliseconds from click to the row leaving the queue, GET /api/entries at 13.0 / 19.7 / 23.5 milliseconds p50 / p95 / p99, 551 milliseconds from process start to a healthy response.

The hosted rows, including the one that matters most for this essay, the first query after Neon has scaled to zero, are still marked TBD. Neon's own documentation does not give a cold-start figure. The repository estimates 300 to 500 milliseconds and has not measured it.

The cost of letting the database sleep

Scale-to-zero is a trade, and the reviewer pays the other side of it. The first action after five idle minutes waits for the compute to wake. If the estimate holds, the queue page that renders in 180 milliseconds locally will take half a second on the first load of the morning. If the estimate is wrong, the first number I will see from production is how wrong.

The second cost is that the whole design rests on one process holding one lock. There is no queue, no second worker, no retry service. For one machine and a handful of companies that is correct. At the point where commits must survive a region outage, the timer map becomes a job table and this essay's arithmetic changes, because a job table has to be polled by someone.

The third is that the hourly pull, when it is on, is now the bill. Fifteen of the estimated 25 CU-hours are the ledger sync waking the database 720 times a month. The undo window is free; the thing that keeps the queue fresh is not.

The rule I would carry to the next one

When a system bills by the hour it is awake, write down what wakes it. A deferred action has exactly one moment it needs to run, and that moment is known at write time. Arm a timer for it, keep the deadline in the database so the timer is disposable, and sweep on boot so a lost timer is a late commit and never a lost one.

Then measure the one number the design is betting on. Mine is the wake-up after idle, and it is still a TBD in the table.

If you have run a deferred-commit window on a scale-to-zero database and have the cold-start number, I would trade notes.

Related: Human approval gates for irreversible agent actions is the principle this window implements, and Where to cut an agent in half is the same rules-around-judgment split in an MCP server.