📚 Encyclopedia

Web Development · updated 2026-10-13

Pull-based job queues for agents behind NAT (design)

architecturejob-queuepollingnatleasespairingdevice-codephp-fpm

When the worker runs on a user's own computer (behind NAT, often asleep), the server cannot push work to it. The worker must dial out and pull. This design was specified for TLDR's "bring your own AI" agents in October 2026 and fits any small server that hands jobs to user-owned machines: build runners, sync clients, home automation bridges.

Flow

user action -> API enqueues job for the owner's agent (status queued, UI "waiting for your computer")
agent: GET /agent/jobs every 3-5 s while online   (cheap indexed query, doubles as heartbeat)
agent claims a job with a lease -> runs it -> POST /agent/jobs/{id}/result
API turns the result into the normal response + push notification

Short polling, not long-polling

On a small PHP server (php-fpm with 15 workers on a 1 GB box) a long-poll pins a worker per connected agent; ten idle agents could starve the site. Short polls are a few milliseconds each. Back off when idle (3 s, then 15 s after 10 idle minutes) and return to fast polling right after a job. Lower latency later, if needed: an FCM data message or a tiny SSE sidecar, not PHP.

Leases and liveness

Pairing (device-code flow, like gh auth login)

  1. The app requests a short code (ABCD-1234, single-use, 10-minute TTL, stored hashed, attempts rate-limited per IP).
  2. The user runs the agent's pair <code> command; the server exchanges it for a long random agent token (stored as a SHA-256 hash; the agent keeps it in a 0600 config file).
  3. The app's setup screen polls "is my agent connected" and flips to "Connected" with the agent's reported capabilities. Installation as a one-liner (curl ... | sh that verifies a published sha256), with the pair step also installing a user service (systemd user unit, launchd agent, or a logon scheduled task).

Data model

Security

See also

Linked from

Sources: tldr memory (plan-byo-ai-agent, server-web-performance), post #1703