Give each AI lane a queue, and chat a thread of its own

Merged
JO
4d ago
+498-160

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_test checks that, that no job names an AI queue itself, and that schedule.yml uses the job's own queue.
  • Chat gets its own capsule: ai_chat, polling ai_interactive, with RAILS_AI_MAX_THREADS threads, default 1 (call by @josh-sanders; the plan had one thread). On main, 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. The ai capsule (RAILS_AI_MAX_THREADS) runs ai_prompt before ai_background, and drains ai_high_priority/ai_low_priority for one release. RAILS_AI_MAX_THREADS=0 leaves both AI capsules out, for all but one worker sharing a local model host.
  • Health on the new queues. SidekiqHealth reports ai_queues_not_polled until a worker polls all three lane queues, so a worker started with the old sidekiq.yml is reported. BackgroundJobHealth reads its queue names from Ai::Routes.
  • The chat reservation (#4444) holds the ai capsule's prompt and background queues, the old ai_low_priority included, while you type. 4c moves it into the gate.
  • Ai::Sequencer.lane. ApplicationJob runs 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 use with_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 on ai_interactive) tries the routes anyway. Prompt and background calls fail at once with MarkedDown, and Ai::GenerationJob waits 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's before_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_lane nests and restores.
  • sidekiq_config_test: the capsule layout, RAILS_AI_MAX_THREADS sizing of both AI capsules, and 0 leaving 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_chat capsule 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.").

RunChat waited in the queueChat jobReply saved after
1104.6 s1.5 s106.2 s
277.0 s1.6 s78.6 s
396.0 s1.5 s97.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_deferred 0, requests_processing at 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, and ai_background, named only in Ai::Routes.
  • The ai capsule's queues weigh equally, so prompt and background take turns; the AI_PROMPT_BEFORE_BACKGROUND self-hosting toggle runs prompt work first.
  • The ai capsule drains the legacy ai_high_priority/ai_low_priority queues for one release; RAILS_AI_MAX_THREADS=0 or 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.
  • SidekiqHealth reports ai_queues_not_polled until a worker polls all three lane queues, or a legacy queue that still holds jobs.
  • The chat reservation holds the ai capsule'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.yml needs the new queues and ai_chat capsule: a worker on the old file leaves chats unanswered.
  • Each worker running the AI capsules gains RAILS_AI_MAX_THREADS threads 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.