AI plumbing 3a-tidy: fixes for 3b and PR 4; dead code from 2a-2f
Merged
JO
4d ago+414+414+012345678901234567890123456789-639-639-012345678901234567890123456789
Part 3a-tidy of the AI plumbing series (part of #4419). Middle of a GitHub stack on refactor/ai-plumbing: 3a (#5501) ← 3a-tidy (this PR) ← 3a-tests (#5503). This is an editor's pass over 3a and over 2a–2f as merged. 3b and PR 4 start from this branch.
3a fixes, and the shape 3b and PR 4 need
Each fix comes with a test that fails without it (test/jobs/ai/probe_job_test.rb).
- Probe results could expire before the page showed them. Each result's TTL started when it was written, and a run makes up to six probes in a row. Results are now written together when the run ends (
Probe#store!). Ai::SharedStateon Redis rejected a sub-second expiry (EX 0), while the test store accepted it. It now writesPXin milliseconds, rounded up, and raisesArgumentErrorwithout a positive expiry. A real-Redis round trip checks the PTTL.- A Redis outage returned 500 on the AI tab.
WorkerAiHealth.recentnow returns no worker results instead. AiHealthhas two modes: it reads what's stored, or probes and stores withrun_probes: true. The cache path andProbe#run's outerrescue StandardErrorare gone. That rescue would have swallowed PR 4'sAi::Error::Busyand probed a second time.Probe#performstill rescuesStandardErroraround the request at this head, so a failing request is stored as a failing result; therescue Ai::Error::Busy; raisethat letsBusyreach the job comes with the gate later in the stack.- The capsule mark is in fiber storage (
Fiber[:ai_capsule]), whereAi::CallContextalready lives and PR 4's lane will, so threads a job starts (Jev's pool) are inside too.Ai::CapsulereadsAi::Routes.ai_queuesrather than keeping its own copy, andApplicationJob's callback is a named method that PR 4 extends in one line. sidekiq_config_testnow accepts any AI capsule that polls the AI queues, so PR 4'sai_chatcapsule passes unchanged.Ai::ProbeJobhas no lock: each "Run checks again" click queues its own run, asprobe_job.rbanddocs/hosting/ai.mdsay.- 3b's cancellation code (
cancelled:,ai_cancelled?,Result#cancelled?) stays: it has no caller until 3b.
Removed from 2a–2f: nothing in production read or passed it
- Fields:
Ai::Result#usage,#failed_over_fromand#correlation_id; Judgement usage;PdfReading::Outcome#usageand#flagged?; the extras onPrompt::Rendered;Request#max_tokens;Response#raw_id. - Options:
Generation'slocale:;PdfReading'splan:;Pdf::PageImages.render'sdpi:and**limits(timeout:stays as the test seam); the probes'openai_compatible:andextra_headers:. - One copy each:
- the requesters' shared code, in
RuleRun::AiProgress; Ai::Config::Endpoint.location_for, which was three "http is local" copies;Ai::CallContext.check_budget!;- the settings-field URL redactor: Jev's
redacted_endpointfolds intoAi::Instrumentation::Redactor.url; - the RubyLLM probe dispatch.
- the requesters' shared code, in
- Comments that named deleted code or told history.
Kept after checking against the merged base:
Response#finish_reason, which the.aievent now records.- Jev's
endpoint_origin, which logs only scheme, host and port, because a gateway can carry a key in its path. Ai::Comparison#primary_endpointand#shadow_endpoint, the read API for comparison rows.
Needs a call
Each of these would change behaviour or the size of a view-layer change, so none was made here:
- The OpenAI endpoint is built twice, once in
Ai::LegacyEnvConfigand once inEval::ProviderFactory. Merging them would change the eval's timeout and JSON-mode fallbacks. LegacyEnvConfigfilters the{session_id}header a second time.- The
Settings::HostingsHelperforwarders. Inlining them would putAi::LegacyEnvConfigcalls in the views. - Jev's fallback is logged twice. Dropping one changes how many debug-log rows an operator sees.
Verification
2026-11-21:
- full
bin/rails test: 18,663 runs, 1 failure. The failure wasAiHealth::ProbeTest"PDF vision probe sends the page as an image", which renders withpdftoppmand timed out under parallel load. It passed in three isolated reruns of the file. test/system/admin/system_health_test.rb: 5 runs, 0 failures;bin/rubocop(3,643 files),erb_lint --lint-all, Biome and Brakeman: clean.
🤖 Generated with Claude Code
Summary by CodeRabbit
- New Features
- AI health checks now save probe results for display, and repeated clicks while a check is queued do not add extra work.
- AI endpoint URLs are redacted consistently in hosting settings.
- Endpoint location is now determined from URL parsing, including handling invalid URLs.
- Bug Fixes
- Worker health snapshots are retained and displayed more reliably when Redis is unavailable.
- Documentation
- Clarified AI status refresh behavior, including the cache period and requirements for automatic updates.