Move the typing hold into the gate, and show what a chat waits for

Merged
JO
2d ago
+1219-504

Fifth PR in stack #5748 (#5744 → #5745 outbox → #5756 4a → #5758 4b → 4c), based on feat/ai-4b-gate (#5758). Plan: pr-4-sequencer.md steps 5–9.

Problem

4b gave each local model host a gate, but chat doesn't use it yet, and #4444's typing hold still works on Sidekiq's queues: only with one AI thread, in one worker, and for a cloud chat model too. A chat that waits for its model shows "Thinking" until the watchdog gives up on it.

Change

  • Chat takes the gate. Assistant::Responder holds a place around each chat request, in the interactive lane, so chat interrupts background work on a local model. It's held per request, so a tool's own AI call to the same host never waits for its own turn. Temporary: it goes once chat is on the transports.
  • The typing hold is in the gate. Typing holds the local host of chat's route for 60 s (15-minute cap, as before): new prompt requests wait and new background ones raise Busy. A hold belongs to the last person to type on that host, and a message ends only its own user's. It works with any number of AI threads, and a cloud chat model holds nothing.
  • External assistants (call by @josh-sanders). Their own calls pass no gate, so while you type to one, and for its whole turn, no new background request starts on any of Sure's local hosts. Prompt requests still start, since the assistant's tools may need them, and a request or a local Jev batch already running finishes. The turn holds no place, so its tools never wait for it.
  • The web sees the gate. A worker publishes each gate in use to Ai::SharedState (holders, waiters, interruptions, put-off work), on every change and on its heartbeat; a dead worker's lapses in 30 s. Only Sidekiq publishes, since a web process's gate would overwrite the worker's.
  • Visible waiting. A pending reply, a second one in the same chat included, says "Waiting in line ..." while other interactive requests are ahead of it at its local host. Work it would interrupt doesn't count. Each reply has its own id at the gate, as one chat can have two at once.
  • The watchdog waits for a reply in line. One whose job is still queued with a worker polling the queue, whatever its model, or one waiting at the gate, isn't given up on. Every reply's time counts from when its job starts, cloud models and external assistants included (call by @josh-sanders), and waiting for a place isn't counted, so a reply that waited still gets its full time. One whose job was lost, or that no worker runs, is given up on as before; one that started and took too long now says so, instead of that the worker may be down. AI_RESPONSE_TIMEOUT no longer has to cover queue time, and its help text says so.
  • Deleted: ChatReservation::Fetch, the one-AI-thread check and its process label, the hold after each reply, the running-chat flag, and chats.ai_busy.

Differs from the plan:

  • The "ready" bubble also counts background work the hold refused. Background requests come back later instead of waiting, so waiters alone would hardly ever show it.
  • The label has no count: the gate can't count chats still in Sidekiq's queue.
  • The watchdog also covers a reply still in Sidekiq's queue, and starts its time when the job starts. With the default one chat thread, a chat seldom waits long at the gate itself.
  • A pending bubble is rendered with its status, and "Thinking" is broadcast as each request gets its place, instead of one "busy" broadcast that the page load after sending would replace.
  • The external assistant hold is new.

New strings are English and German. chats.ai_busy is removed from the eight locales that had it; the others show English for chats.waiting_in_line and chat.errors.timed_out until translated.

Tests

  • ai_gate_test: the hold refuses background requests, keeps prompt ones waiting until it lapses or the message ends it, interrupts nothing, never keeps an interactive request off, is per host, and fails open.
  • gate/state_test: what's published and when, the heartbeat, expiry, put-off work, web processes, a store that can't be written, and who is ahead of whom.
  • ai_nested_call_test: a chat turn whose tool embeds on chat's own one-place host completes, under the chat's correlation id; chat interrupts a background request and ends the hold; cloud models and external assistants have no gate.
  • ai_agreement_test: chat's route is on the host of the provider chat calls, in every setup.
  • ai_nested_call_test, chat_reservation_test: an external turn holds background requests off every local host, ends with the turn even when it fails, and typing to it holds only background.
  • chat_test: the bubble's status, the replies the watchdog waits for and gives up on, and the restarted time.

For the gate, the hold, chat's place and the watchdog, the tests fail with each behavior removed (nine mutations checked).

Validation: bin/rails test, full suite, 19,307 runs, 0 failures. Chat system tests: 10 runs, 0 failures. Rubocop, erb_lint and Brakeman clean.

Measured after (step 9)

Measured on 9c16cbb9c on 2026-11-25, in the sure-dev pod on venus, the same way as the baseline in #5756: RubyLLM 2.2.3, llama.cpp qwen3.6-35b-a3b-mtp on CPU, the default sidekiq.yml, the built-in assistant, 200 transactions queued for categorization, and a chat sent 20 s into a categorize call.

RunBaseline: reply saved after4c: reply saved after4c: chat job
1106.2 s6.1 s5.5 s
278.6 s2.7 s2.0 s
397.6 s1.9 s1.2 s
  • Target: about 2 s of extra wait, never more than 5 s. A chat alone took 0.9–2.0 s on the same warm cache, so the extra wait was at most about 4–5 s in run 1 and about 2 s or less in runs 2 and 3. llama.cpp was restarted before these runs, so one chat warmed its cache first (303.9 s cold), as the baseline's was warm.
  • What happened: the gate interrupted the categorize call each time, its job went back to the queue and ran later. llama.cpp never had more than one request and deferred none. The interrupted calls' usage rows have unknown tokens and no cost.
  • Typing hold: held for 40 s with categorization queued, the host got no request and the bubble said "ready". The chat was answered in 0.9 s, its message ended the hold, and categorization started within 13 s.
  • A chat 10 s into a PDF import's call (prompt lane): answered in 2.9 s. The import was interrupted once, finished, and extracted 13 rows.

The run also showed each Busy in the debug log as an error (and, from the code, in Langfuse as a failed trace). Fixed in 4b (#5758).

Used in the browser

In the sure-dev pod on venus, 2026-11-25, through the sidebar chat (headless Chrome), with Redis cable so live updates reach the page. These ran before the later commits: labels then said "1 task ahead", and shadow runs came back when their host was busy. The later commits are covered by tests, not run in the browser again.

  • Built-in chat on llama.cpp, one thread, 90 s limit: the "ready" and "busy" bubbles, the interrupt (a chat during categorization answered in 0.7 s), three chats in line (the third waited past 90 s, seven watchdog reports declined, answered after 123 s), and a second message in the same chat.
  • Two chat threads: both jobs run, the gate serves one at a time, and the labels change from "1 task ahead" to "Thinking".
  • Jev (OpenCode Zen, free) categorizing: chat and typing don't hold it up. With llama.cpp as its shadow, a chat interrupted the shadow run, which came back and recorded all 50 comparisons. That chat took 17.4 s, over the target, all of it inside its llama.cpp request.
  • OpenClaw: three short chats at once were answered. With three long ones and a 60 s limit, one was given up on with one thread, two with two threads. OpenClaw seems to answer them one by one on this server, so a second thread only starts the next reply's time sooner.
  • OpenClaw sharing llama.cpp with categorization: before the external hold, two chats took 102 and 307 s, and one waited behind two batches. With it, two took 209 and 220 s, each behind only the batch already running. In the second, under 80 s of that was the wait, and the rest OpenClaw's own, with its prompt no longer cached. Not covered at first: OpenClaw calling Sure's tools. OpenClaw's default Tool Search hid them from the model. With profile: "minimal" and bundle-mcp, it called them correctly, directly and through Pipelock 3.6.0 and 3.8.0.

Upgrade

Nothing to do. RAILS_AI_MAX_THREADS sets how many chats a worker answers at once (4a); leave it at 1 for one local model. With several workers against one local host, run the AI capsules on one (RAILS_AI_MAX_THREADS=0 on the others), as 4a's notes say: two would overwrite each other's gate state.

🤖 Generated with Claude Code

https://claude.ai/code/session_016zAWUUTovNPgC5oNfUHKhd


Summary by cubic

Moves chat's typing hold from Sidekiq queues into the per-host model gate, so it works across thread counts, and pending replies now show what they're waiting for. The watchdog no longer counts queue or gate wait against the reply timeout, and a reply whose job is still queued is now waited on regardless of model instead of being given up on as a likely worker failure.

  • Chat holds a place at the gate around each model request; interrupted work isn't counted as a task ahead, and nested tool calls don't wait on their own chat's place.
  • Typing holds the chat's local model host: prompt requests wait, new background requests are deferred. This applies only with Setting.ai_local_chat_first on; without it, chat takes no gate and holds nothing.
  • Each reply has its own gate id, and a reply is kept marked as waiting until its wait is credited, so the watchdog can't give up on it in between.
  • A hold is its typist's: a message ends only its own user's hold, and releasing deletes the hold only while it's still the user's, in one Redis step.
  • Workers publish gate status for the web to show pending replies and typing-hold status; a reply whose job is queued says "Waiting in line" without a count.
  • The old queue-fetch hold, the single-thread check, the post-reply hold, the running-chat flag, and chats.ai_busy are removed; no upgrade steps are needed.

Summary by CodeRabbit

  • New Features
    • Chat replies now show when they’re thinking or waiting, including how many tasks are ahead.
    • Interactive chat requests can take priority over background work on local AI hosts. Typing and external-assistant activity can temporarily hold background work.
    • Chat status indicators better reflect activity on the relevant AI host.
  • Bug Fixes
    • Reply timeouts now measure from when response processing starts, rather than when the message was created.
  • Documentation
    • Updated AI hosting guidance with revised queueing, reservation, and timeout details.