Give each AI lane a queue, and chat a thread of its own
Third PR in stack #5748 (#5744 → #5745 outbox → 4a → 4b → 4c), based on feat/ai-generation-outbox (#5745). Plan: pr-4-sequencer.md steps 1–2, split per stack-sequencer-off-the-web-thread.md §1 and §3 ("4a, 4b, 4c").
Problem
All AI work shares one capsule and two queues. With one AI thread (the default), a chat waits for whatever background AI job holds that thread. The routing table already gives each feature a lane, but the lanes exist only on paper: prompt and background work share ai_low_priority.
Change
- Three lane queues, named only in
Ai::Routes:ai_interactive,ai_prompt,ai_background. Every job takes its queue from its feature's lane.ai_lane_testchecks that, that no job names an AI queue itself, and thatschedule.ymluses the job's own queue. - Chat gets its own capsule:
ai_chat, pollingai_interactive, withRAILS_AI_MAX_THREADSthreads, default 1 (call by @josh-sanders; the plan had one thread). Onmain, a raised setting answers that many chats at once, so one thread would have been a regression for cloud models, several families and external assistants. Theaicapsule (RAILS_AI_MAX_THREADS) runsai_promptbeforeai_background, and drainsai_high_priority/ai_low_priorityfor one release.RAILS_AI_MAX_THREADS=0leaves both AI capsules out, for all but one worker sharing a local model host. - Health on the new queues.
SidekiqHealthreportsai_queues_not_polleduntil a worker polls all three lane queues, so a worker started with the oldsidekiq.ymlis reported.BackgroundJobHealthreads its queue names fromAi::Routes. - The chat reservation (#4444) holds the
aicapsule's prompt and background queues, the oldai_low_priorityincluded, while you type. 4c moves it into the gate. Ai::Sequencer.lane.ApplicationJobruns each AI job in its queue's lane. The lane is in fiber storage, so Jev's parallel requests share it. Evals and the console usewith_lane. A call outside any lane counts as interactive. 4b's gate reads it too.- The lane decides who tries past down marks (call by @josh-sanders; replaces #5769's
waiting:option). When every route is marked down, an interactive call (a click on the web thread, or a job onai_interactive) tries the routes anyway. Prompt and background calls fail at once withMarkedDown, andAi::GenerationJobwaits the mark out. This PR carries #5769's commits until #5769 lands. - Merchant logos leave the AI run (call by @josh-sanders). The run no longer backfills logos, so a run without consent no longer changes them before failing. The Enhance button queues only the AI run: a website the AI saves gets its logo as it's saved (
ProviderMerchant'sbefore_save), and each family sync backfills merchants with a website but no logo (#2925).
Differs from the plan: VectorStore::RequeueIndexPollsJob's cron goes to ai_prompt, its own queue (embed is a prompt-lane feature), not ai_background. ai_lane_test requires a cron entry to use the job's own queue.
Tests
ai_lane_test: each AI job's queue is its lane's; no job names an AI queue; cron entries use their job's queue; each queue's lane, legacy queues included;with_lanenests and restores.sidekiq_config_test: the capsule layout,RAILS_AI_MAX_THREADSsizing of both AI capsules, and0leaving the AI capsules out.- Down marks: an interactive call, or one outside any job, tries marked routes; prompt and background calls don't.
- The Jev fan-out runs in the caller's lane on its own threads.
- Health and chat reservation tests on the new queues, including that the
ai_chatcapsule is never held and that legacy chats still run under a hold. - Enhancer: a click queues only the AI run, which leaves logos alone; a run without consent sends nothing and fails.
Validation: bin/rails test, full suite, 19,234 runs, 0 failures. Rubocop clean.
Baseline (step 0)
Measured on #5745's head (ab8300595, before this PR) on 2026-11-24, in the sure-dev pod on venus:
- RubyLLM 2.2.3;
- llama.cpp
qwen3.6-35b-a3b-mtp, CPU only; - default
sidekiq.yml(RAILS_AI_MAX_THREADS=1); - built-in assistant.
Each run queued 200 uncategorized transactions for categorization. It waited until a categorize batch was in flight at the model, then sent a chat 20 s later ("Reply with one short sentence saying hello.").
| Run | Chat waited in the queue | Chat job | Reply saved after |
|---|---|---|---|
| 1 | 104.6 s | 1.5 s | 106.2 s |
| 2 | 77.0 s | 1.6 s | 78.6 s |
| 3 | 96.0 s | 1.5 s | 97.6 s |
- Almost all of the wait is the AI thread. The chat ran only once the running categorize job (25 transactions, 100–135 s) finished. llama.cpp never deferred a request (
requests_deferred0,requests_processingat most 1), so the chat never reached the model while categorization ran. - The chat itself is fast. Its 14,593-token prompt was cached and it produced 10 tokens. The reply is saved whole, so the time to the first word equals the time to the reply.
- The hold after a reply works as built. It kept the next background job back for 60 s.
4c's step 9 measures the same way on 4c's head. With 4a alone, chat no longer waits for the thread but still queues at the model.
Upgrade
A custom sidekiq.yml needs the new queues and the ai_chat capsule (copy the shipped one). A worker started with the old file leaves chats unanswered, and System health says so. Each worker running the AI capsules has RAILS_AI_MAX_THREADS more threads, for chat. They aren't counted in the database pool, since each holds a connection only per query. Jobs already queued on the old two queues run once.
🤖 Generated with Claude Code
https://claude.ai/code/session_016zAWUUTovNPgC5oNfUHKhd
Summary by cubic
Gives each AI lane its own queue and chat a capsule of its own, so a chat no longer waits for a background AI job's model call when the worker has one AI thread. Each AI job now runs in its queue's lane, and merchant logos move out of the AI run. Chat's capsule takes RAILS_AI_MAX_THREADS like the rest, so chat keeps answering as many messages at once as before the lanes.
Queues and lanes
- Interactive, prompt, and background work run on
ai_interactive,ai_prompt, andai_background, named only inAi::Routes. - The
aicapsule's queues weigh equally, so prompt and background take turns; theAI_PROMPT_BEFORE_BACKGROUNDself-hosting toggle runs prompt work first. - The
aicapsule drains the legacyai_high_priority/ai_low_priorityqueues for one release;RAILS_AI_MAX_THREADS=0or less leaves both AI capsules out, a negative count counting as 0. - A call's lane decides whether it tries routes past down marks: interactive calls try anyway, prompt and background jobs fail at once and wait the mark out.
SidekiqHealthreportsai_queues_not_polleduntil a worker polls all three lane queues, or a legacy queue that still holds jobs.- The chat reservation holds the
aicapsule's prompt and background queues.
Logos and upgrade
- The Enhance button queues only the AI run; logos aren't AI, so the run never touches one — a website it saves gets its logo as it's saved, and the family sync backfills merchants with a website but no logo.
- A custom
sidekiq.ymlneeds the new queues andai_chatcapsule: a worker on the old file leaves chats unanswered. - Each worker running the AI capsules gains
RAILS_AI_MAX_THREADSthreads for chat, and jobs already queued on the legacy queues run once.
Summary by CodeRabbit
- Improvements
- Chat and background AI work now run in separate processing lanes, helping keep chat activity independent of background tasks.
- Merchant logos are refreshed through a separate background task, including when an AI enhancement is already underway or AI enhancement is unavailable.
- Documentation
- Updated hosting and AI pipeline guides with the new worker setup, queue behavior, and upgrade guidance.