A worker is running your agent’s booking. It stops responding. Is it dead, or only slow? From the outside you cannot tell. If you hand the work to a replacement and the old worker wakes up later, you now have two workers who both believe the work is theirs.
This article is about that situation: how Robotelos makes sure the old worker cannot send anything, and why a timeout alone does not do it. It assumes you know what a database lease is. Terms from Robotelos are explained as they come: a run is the engine’s record of one piece of accepted work, and a worker is a process that advances runs.
The stale decision
The system gives every worker a time limit on its turn, a lease. When it runs out, another worker takes the run. Now replay the evening. Just before it stalled, the old worker decided to resend a table request that had timed out. At 6:08 you tell the agent you want German food instead, and a new worker records that. At 6:10 the old worker wakes up and carries out its decision, which was based on an evening that stopped being true two minutes ago. If the first request never arrived, the resend books an Italian table you no longer want.
A lease does not prevent this. The old worker does not know its lease ended, because it is stalled. Checking the clock before sending is not enough either, because it can stall again between the check and the send. The check has to be part of the write.
Epochs: a number that only goes up
Each time a worker claims a run, the run’s epoch increases by one, and every write the worker makes must carry the epoch it was given. A write with an older epoch is refused. Robotelos claims runs with one statement. The lease is judged by the database clock, so workers do not need synchronized clocks, and SKIP LOCKED lets several workers poll the same table without blocking each other:
UPDATE robotelos_engine_runs
SET epoch = epoch + 1, owner = $1, lease_until = now() + make_interval(secs => $2)
WHERE run_id = (
SELECT run_id FROM robotelos_engine_runs
WHERE status = 'RUNNING'
AND wake_at <= now()
AND (lease_until IS NULL OR lease_until < now())
ORDER BY wake_at LIMIT 1
FOR UPDATE SKIP LOCKED)
RETURNING run_id, epoch;
(The real query also claims runs that have a pending command, such as a request to stop.) Every change to a run then goes through one commit function. It locks the run row and compares three numbers with the ones the worker read when it started:
SELECT epoch, progress_version, fact_seq
FROM robotelos_engine_runs WHERE run_id = $1 FOR UPDATE
if epoch != st.Epoch { return errFenced } // this worker no longer owns the run
if decision && (progress != st.Progress || facts != st.FactSeq) {
return errStale // the world changed; decide again
}
Because the lock and the comparison happen in one transaction, there is no gap between the check and the write.
Decisions are fenced, facts are not
Two different errors come out of that function. errFenced means the worker’s turn is over, and it walks away. errStale means the worker still owns the run, but a fact arrived after it started thinking, so it reads the state again and decides again.
This is the difference between a decision and a fact. A decision, such as “resend the request”, is fenced and checked against the facts it was based on. A fact, such as “the restaurant replied”, is always recorded, against the attempt that produced it, and it increments fact_seq. That is why a genuine receipt carried by an old worker is not thrown away. Ownership says who may decide. A verified receipt says what happened outside the system.
Sending is protected too. Before any provider call, the operation moves to DISPATCHING in a commit fenced by the epoch. A stale worker’s first step at 6:10 is that write, it is refused, and nothing leaves the process.
How we test it
TestEngineFrozenPastItsLeaseIsFenced reproduces the evening. One engine sends the booking and crashes. A second takes over, asks the provider for evidence and freezes at the moment between reading and committing. A third engine takes the run and finishes it. When the frozen engine wakes with a decision computed under an old epoch, the fence refuses it. The test asserts that the restaurant was called twice (the first epoch, and the retry under the same key) and not three times, and that the finished operation stays finished. Another test freezes a real process with SIGSTOP, resumes it and checks that it changes nothing. That one needs Linux or macOS and is skipped on Windows. The tests are in fault_test.go.
What fencing does not do
- It narrows the window and does not close it. A worker can pass the check, record its attempt and stall again before the request leaves, and a request already on its way cannot be called back. Only the provider can close that gap, by recognizing the key of a duplicate. That is why fencing and idempotency keys go together.
- It does not prove the restored state is consistent. Fencing checks who writes. It does not check that what the writer read belongs together. I count that as an open question.
- It does not solve upgrades. A new release can meet a run recorded by older code. Robotelos pins the capability versions a plan was admitted against and verifies the plan’s digest when it loads it, so a run finishes under the version it was accepted with.
Takeaways
- Treat a stalled worker as alive until the store says otherwise.
- Put the ownership check inside the write, in the same transaction, not before it.
- Separate decisions, which must be fenced, from facts, which must be kept.
- Test it by freezing and killing processes. A mock cannot show you the gap.
The engine is experimental today. The execution model describes the guarantees it aims for.
