A Timer Is Not a Queue: Timely and Recoverable Agent Jobs
Suppose a report takes two minutes to generate while a scheduler scans a jobs table every five minutes. Normal queue waiting can approach five minutes; longer model timeouts cannot fix it. These numbers are teaching assumptions. Frequent scans can also increase idle database activity.
Two trigger paths, one durable task
Commit task ID, authorized input revision and state in one transaction. Send a short wakeup after commit. If that fails, a scheduled scan discovers the same task rather than creating another. HTTP 202 only acknowledges a trigger.
| Stage | Record | Recovery boundary |
|---|---|---|
| Enqueue | ID, revision, authorization | Rollback creates no task |
| Claim | Attempt, lease, execution identity | Reconcile external effects on expiry |
| Execute | Provider request and known/unknown outcome | Do not blindly repeat paid calls |
| Publish | Immutable report revision | Read the original ID after restart |
Claim in a short transaction
Assume a jobs table with primary key id and due_at, state, run_id and lease_until fields. This is an unexecuted standalone design fragment:
WITH candidate AS (
SELECT id FROM jobs
WHERE state = 'pending' AND due_at <= now()
ORDER BY due_at, id
LIMIT 1 FOR UPDATE SKIP LOCKED
)
UPDATE jobs j
SET state = 'running', run_id = $1,
lease_until = now() + interval '2 minutes'
FROM candidate c WHERE j.id = c.id
RETURNING j.*;Commit before calling the model. Completion must check task ID, execution identity, state and unexpired lease. Renew long-running leases; late executions cannot commit. PostgreSQL SELECT explains that SKIP LOCKED suits queue-like contention rather than general consistent reads.
Leases cannot cancel an external request. Connection loss after dispatch may hide a charge or a delivered notification. Preserve unknown and attempt identity; reconcile before retrying, and expose an explicit recovery choice when reconciliation is impossible.
Acceptance
Simulate competing workers, a failed post-commit wakeup, a worker crash, a late expired execution and an external simulator that loses the response after executing. Verify tasks persist, stale workers cannot publish and unknown effects are not unconditionally replayed. Qualify real providers separately.
Open ETF Agent has its own scheduled research and delivery contract; this SQL is not its implementation. Future event and messaging integrations are in the application roadmap.