AI plumbing 3a: capsule, shared state, probes off the web thread
Part 3a of the AI plumbing series (part of #4419). Bottom of a GitHub stack on refactor/ai-plumbing: 3a (this PR) ← 3a-tidy (#5502) ← 3a-tests (#5503). 3b and PR 4 build on 3a-tidy.
Problem
The admin AI tab ran every health probe inside the web request: up to six model calls, each with its own timeout, holding a Puma thread while an admin waited. Nothing marked where an AI call is allowed to run, so later layers couldn't keep AI calls off the web thread. A separate "Verify worker configuration" button ran a second set of checks in a job and wrote them to Rails.cache, which web and worker don't share on every install (null store in test, a per-process file store in production without REDIS_URL).
What changes
flowchart LR
Admin["Admin opens the AI tab"] -->|GET, reads only| Page["AiHealth (stored results)"]
Admin -->|"Run checks again" POST| Q["Ai::ProbeJob<br/>background lane's AI queue"]
Q -->|probes, inside Ai::Capsule| Models[("LLM · OCR · embeddings · Jev")]
Q -->|results + WorkerAiHealth| S[("Ai::SharedState<br/>JSON in Sidekiq's Redis")]
S --> Page
Q -->|Turbo broadcast, same src| Frame["Open tabs reload the frame"]
Step 1: Ai::Capsule and Ai::SharedState
Ai::Capsuleknows where an AI call may run: in a job on an AI queue (Ai::Routes.ai_queues), which only the worker's AI capsule polls.ApplicationJobmarks such a job with anaround_performcallback rather than Sidekiq middleware, so the test adapter andperform_nowrun it too.check!raisesAi::Capsule::Violationoutside the capsule in development and test. Nothing calls it yet; 3d turns it on.Ai::Capsule.outsidelets the evals and the console run AI calls outside a job.Ai::SharedStatepasses short-lived values from a job to the web as JSON in Sidekiq's Redis, which every process shares on every install.
Step 2: probes run in Ai::ProbeJob, not the web request
Ai::ProbeJobruns every probe on the background lane's AI queue. It stores each result and this worker'sWorkerAiHealthsnapshot, then broadcasts a replacement for the tab's lazy frame with the samesrc, so open tabs reload it in a normal request. A failed probe is a result, so the job isn't retried.- The tab reads stored results only, so opening it makes no AI request. A check with nothing stored reads as "not checked", never as failing. Each result shows its age, and a row shows how long results are kept (
AI_HEALTH_PROBE_CACHE_TTL), which explains why an old result still shows (closes #3279). WorkerAiHealthCheckJob, its button and its route fold intoAi::ProbeJob, since every probe now runs in a worker. The worker card stays: it says which process ran the checks and whether that process's configuration matches the web's.shared/_ai_worker_health_warningreplaceschats/_worker_health_warningand takes its message, so the AI tab warns when no worker would run the checks.
The PDF probes' output limit (#4866) is the base's own fix (production_pdf_output_tokens). 3a's earlier version of that fix was dropped in the restack.
Plan: pr-3-off-the-web-thread.md in #4440's docs.
Verification
On refactor/ai-plumbing @ d51f2ec20, Ruby 3.4.13, 2026-11-21:
- full
bin/rails test: 18,674 runs, 203,416 assertions, 0 failures, 0 errors (32 skips); test/system/admin/system_health_test.rb: 5 runs, 0 failures;bin/rubocop(3,645 files),erb_lint --lint-all, Biome and Brakeman: clean.
🤖 Generated with Claude Code
Summary by CodeRabbit
-
New Features
- Admins can queue AI health checks to run in the background. Results are shared with the status page, which displays check times, cache retention, and worker coverage.
- Chat users with AI enabled see a warning when background jobs are unhealthy.
-
Changes
- Opening the AI status tab displays stored results without triggering new checks. Check results may be stale if workers are unavailable or backed up.
- Updated the documented AI health-check behavior and configuration options.