2 December 2026 · 6 min read

Liveness belongs to the process, not the row

Deciding a run is dead because it looks old is a guess, and every guess is wrong on a slow day. How we replaced eight age thresholds with process leases, compare-and-set status writes and declared state machines.

Every system that runs background work eventually needs to answer one question: is this thing still running?

A job run says running. Is it? Perhaps a process is working on it right now. Perhaps the process died an hour ago in the middle of a deploy and nothing will ever finish it. The row looks the same either way. So somebody writes a sweep that closes any run that has been running for too long, and picks a number.

We had eight of those numbers. A job run's heartbeat was read as dead after two minutes of silence or fifty minutes of age. A job could be re-claimed after fifty-five minutes. A queued webhook event was given sixty. A chat reply was abandoned after sixteen seconds without a listener, or fifty minutes by the sweep. A connector sync's claim lapsed after fifteen minutes, and a maintenance pass after forty-five. Each was a reasonable guess when it was written. Each was a different answer to the same question, and each was wrong on the day a model was slow, a provider was rate limiting or a crawl had more to read than usual.

What a wrong guess costs

A threshold that is too short is the obvious failure: a sweep closes work that was still happening. The less obvious one is worse.

When the sweep decides a run is dead, it marks it failed, and something may start a replacement. Then the original process, which was only stalled, wakes up, finishes, and writes its result with something like this:

update job_runs set status = 'ok', output = $2 where id = $1

That where names only the row. It does not say "if this run is still running" or "if it is still mine". So a late writer overwrites a run that was already finished, and the record now says something that did not happen, about work the sweep had already handed on. Nothing errors. You find out when somebody asks why a job reported success and failure for the same morning.

Both failures have the same root. The row is being asked about something it cannot know. Whether work is still happening is a fact about a process, and a row has no way to know whether its process is alive.

Ask the process

The replacement has three parts.

Every process holds a lease. Each running application process has one row in a workers table with a lease_until time. A heartbeat every fifteen seconds pushes that to ninety seconds ahead, so a process is presumed dead only after missing several beats in a row. A row whose lease has passed is a process that has stopped answering, and any process may delete it.

In-flight work names its owner. When a process claims a job run, a queued event, a chat reply, a sync or a maintenance pass, it writes its own worker id into an owner column on that row. The rule for liveness is then one sentence: a row is alive while its owner's lease is. The sweep no longer asks how old a row is. It asks whether the row names an owner that holds no live lease, which is a small index range plus one primary-key lookup per owner.

The finishing write is fenced. A process finishing a run writes where id = $1 and status = 'running' and owner = $2, with its own worker id as $2. If its lease lapsed and the sweep closed the run, or another process now owns it, the update matches nothing and returns zero rows. The caller treats that as having lost: it logs that the outcome was discarded, writes nothing else, and does not report the run in its chat a second time. The run that replaced it stands.

There is one subtlety in the fence, and it is the part most easily got wrong. A process that discovers its own lease has lapsed takes a new worker id. If it re-registered under the old one, every row still naming that id would look alive again, including rows the sweep was about to hand on, and they would sit running until the process exited. So the id is read at the moment of the finishing write, never cached from the claim. A process that lost its lease since claiming the work cannot win the fence, because it is no longer the process that claimed it.

No age threshold decides liveness anywhere in this. The lease constants are the only liveness constants, and domain code uses the shared SQL fragments (ownerAlive and ownerGone) rather than spelling the rule its own way, which is how there came to be eight.

Declare the machine

The fence answers who may write. It does not answer what the legal writes are, and the late-writer bug was also a lifecycle bug: a run went from finished back to something else because nothing said it could not.

A status column is a state machine whether or not anybody wrote one down. Its transitions are whatever the updates happen to do, and its labels are whatever each page happened to choose. So now each one is declared once, as data. A job run is running, ok or error; running may move to either of the others; both of those are terminal; a row may only be created running.

Two things follow from having it written down.

A write that changes a status is a compare-and-set. Its where names the legal prior states, taken from the machine (machine.from(to)), so finishing a run is only possible from running. If the row is already somewhere else, the write matches nothing and the caller has to handle losing, which it can now see rather than silently overwriting.

Readers take their words from the machine, never from the stored string. A label and a badge colour come from the declaration. A value the machine does not declare reads "Unknown", is counted where counts are shown so somebody notices, and is offered no action. A gate that asks whether something is usable says no to anything it does not recognise. Every machine whose column has a database CHECK is tied to that CHECK by a test, so the two cannot drift apart.

One rule, two languages, one owner

A smaller discipline came out of the same work. Some rules are read in both SQL and TypeScript: whether a connection is usable, for example, is a where clause in a dozen queries and an if in application code. Ours had been written positively in the queries (status = 'active') and negatively in the webhook receiver (status !== 'disabled'). For a connection whose sign-in had failed, those disagree. It was out of every tool list and every crawl, and still had its inbound webhook deliveries stored and turned into job runs.

Now a rule like that has one owner, which exports both forms built from one constant: usableConnectionSql and isConnectionUsable read the same list of states. A test feeds both forms the same cases. Disagreement becomes a failing test rather than a production incident.

Rolling it out

None of this rewrote data. Rows written before the change, and rows written during a rolling deploy by a process still running the previous build, have no owner. The lease rule deliberately says nothing about them: ownerGone is false for a row with no owner, and each sweep keeps one narrow owner is null arm with the old age rule to cover exactly those rows. Once no running row without an owner can exist, those arms come out, one release after the change shipped.

Where Memtro stands

Scheduled jobs, event-triggered runs, chat replies, connector syncs and maintenance passes all run on process leases with fenced finishing writes, and every status is declared once as a machine. A run is reported finished by the process that finished it, or closed by the sweep when that process is gone, and never both. Jobs and their run history are described in the jobs documentation and event triggers in events. We have also written about knowing what the AI knew at the time, which is the same instinct applied to memory: record what actually happened, and do not let a later write quietly rewrite it.

Where agents remember your company

Sign in, add a provider key, create an API key and point your tools at Memtro. It takes about five minutes.

7-day free trial · card on file, nothing charged until it ends · cancel any time