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
- Claim sets
claimed_atandlease_until(60 s); long runs renew via a heartbeat endpoint. - The heartbeat response is also the cancel channel (
cancel: truemakes the agent kill the process), since the server cannot call the agent. - Lease expired (agent crashed or
kill -9): requeue once, then fail. - A job does not time out because the computer is off. Show "waiting for your computer"; after 24 h queued, fail with "your agent was offline". Send one push if a job sits queued more than 2 minutes with the agent offline.
online= last poll within 30 s.
Pairing (device-code flow, like gh auth login)
- The app requests a short code (
ABCD-1234, single-use, 10-minute TTL, stored hashed, attempts rate-limited per IP). - 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). - 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 ... | shthat 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
agent(id, username, name, token_hash, caps, version, created_at, last_seen, revoked_at).pair_code(code_hash, username, expires_at, used_at).job(id, post_id, username, agent_id, kind, payload, status queued|claimed|done|failed|canceled, claimed_at, lease_until, created_at). Snapshot the full payload at enqueue time.
Security
- Agent tokens authenticate only agent endpoints, see only their owner's jobs, and post results only to jobs they claimed.
- Capabilities (models, tools, folders, danger) are reported by the agent; local config is the ceiling and the server can only narrow. See Securing a shared AI agent that other users can drive.
See also
- Lifecycle of an async AI request (status machine, expiry, retries, failure)
- Memory thrash and capacity on a 1 GB VPS