Part of the Determesh series.

What

Every conversation in Determesh, and every piece of work behind it, is a thread. A thread is a row in Postgres with a status. Workers claim runnable rows with SELECT ... FOR UPDATE SKIP LOCKED, run them, and write the result back. There is no message broker and no workflow engine. Postgres is the queue and the record at once.

Why

The design was already a queue of records

The design said threads are loosely tied. A thread waiting on a manager’s approval holds nothing: no process, no timer, no connection. If the manager replies in five years, the thread picks up. A new message from the user reopens a thread that finished. A parent waits for its children, and children can wait for each other.

That describes rows with a status column far better than it describes long-running workflows. A workflow engine like Temporal would replay history to rebuild state that is already sitting in the database.

One source of truth

With RabbitMQ next to Postgres, every message lives in two places, and the interesting bugs are the ones where they disagree: the row says pending, the queue already delivered it, a worker crashed in between. With Postgres alone, the status is the queue. A thread is runnable exactly when its row says so, and the transaction that changes the row is the only event that matters.

The cost is that scheduling, retries and crash recovery are ours to write. That turned out to be a few hundred lines, and it is the part of the system we understand best.

How

Statuses

StatusMeaning
PENDINGHas work to do. Any worker may claim it.
RUNNINGClaimed by a worker holding a lease.
WAITINGA parent waiting for its children.
BLOCKEDA child waiting for siblings it depends on.
IDLEDone for now. A new message makes it PENDING again.
FAILEDGave up after retries. Counts as finished for a waiting parent.

There is no CLOSED. Telegram and email conversations never end, they go idle.

Claiming

Workers poll for pending threads and claim them in one statement:

SELECT id FROM threads
WHERE status = 'PENDING' AND run_after <= now()
ORDER BY last_activity_at
LIMIT :n
FOR UPDATE SKIP LOCKED

SKIP LOCKED is what makes this work with many workers and many instances. A row another worker has locked is skipped, not waited on, so two workers never take the same thread and never block each other. Each run happens on a virtual thread, so an instance can run many at once without a thread pool to tune.

Leases and the reaper

A claim is a lease with an expiry, and the worker renews it with a heartbeat while the run continues. If the worker dies, the heartbeat stops, the lease runs out, and a reaper puts the thread back to PENDING. A run can only be ended by the worker that still holds the lease, so a worker that was presumed dead and comes back cannot overwrite the result of the one that took over.

A newer message during a run

A user who writes “sorry, I meant February” while the assistant is still working on March should not get the March answer.

Each run remembers the newest message it started with. Before the reply is written, the thread is checked. If a newer message has arrived, the reply is dropped. The work the run did stays in the thread, so the next run sees it: if the new message needs no new work, such as “please hurry”, the results are reused; if it changes the request, the orchestrator works again, and can undo what the first run did.

Parents and children

When the orchestrator splits work into tasks, each task becomes a child thread. A child can depend on earlier siblings and stays BLOCKED until they finish. The parent goes to WAITING.

When the last child finishes, the parent gets a system message, “all child threads finished: 11 done, 1 failed”, and becomes PENDING again. A failed child does not fail the parent. The parent decides what a partial result means, usually by reporting it.

Children started together share a creation time, so the engine counts and reports only the latest round. That matters for long jobs that run in several rounds, where an old round’s children must not be counted again.

Updates after completion

Complete does not mean silent. A thread that acknowledged a long job is IDLE, and the job’s coordinator thread keeps appending updates to it: “sending now”, “all sent”, and later “3 bounced”. Every update goes out on the user’s channel like any reply. Completion and messaging are deliberately not tied together.

Fair claiming

One company starting a job with a thousand tasks must not hold back every other company’s chat. So every other runnable thread is claimed before a task, oldest activity first, and one company runs only a few tasks at once. A big job runs in waves. A small question from another company is picked up between them.

Compaction

A Telegram thread that never ends cannot be sent to a model whole. After a set number of messages since the last summary, everything but the last few is summarised, and the model’s context becomes the summary, the recent messages and the new one. The summary is a record of what happened, never instructions. That rule came from a bug, and it has its own post later.

Two instances

Because every claim is a row lock, two instances of the backend can run side by side with no coordination beyond the database. Jobs over whole sets, like the embedding sweep or the billing round, run under a Postgres advisory lock so only one instance does each round. That is also what makes releases without downtime possible, later in the series.

If workers ever need to wake instantly instead of polling, Postgres LISTEN/NOTIFY is there. So far, polling has been fast enough.


Previous: Submit Only: Why Replies Come Back by Webhook

Next: Running the Tool Loop Ourselves