AI plumbing 3a-tidy: fixes for 3b and PR 4; dead code from 2a-2f

Merged
JO
4d ago
+414-639

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::SharedState on Redis rejected a sub-second expiry (EX 0), while the test store accepted it. It now writes PX in milliseconds, rounded up, and raises ArgumentError without a positive expiry. A real-Redis round trip checks the PTTL.
  • A Redis outage returned 500 on the AI tab. WorkerAiHealth.recent now returns no worker results instead.
  • AiHealth has two modes: it reads what's stored, or probes and stores with run_probes: true. The cache path and Probe#run's outer rescue StandardError are gone. That rescue would have swallowed PR 4's Ai::Error::Busy and probed a second time. Probe#perform still rescues StandardError around the request at this head, so a failing request is stored as a failing result; the rescue Ai::Error::Busy; raise that lets Busy reach the job comes with the gate later in the stack.
  • The capsule mark is in fiber storage (Fiber[:ai_capsule]), where Ai::CallContext already lives and PR 4's lane will, so threads a job starts (Jev's pool) are inside too. Ai::Capsule reads Ai::Routes.ai_queues rather than keeping its own copy, and ApplicationJob's callback is a named method that PR 4 extends in one line.
  • sidekiq_config_test now accepts any AI capsule that polls the AI queues, so PR 4's ai_chat capsule passes unchanged.
  • Ai::ProbeJob has no lock: each "Run checks again" click queues its own run, as probe_job.rb and docs/hosting/ai.md say.
  • 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_from and #correlation_id; Judgement usage; PdfReading::Outcome#usage and #flagged?; the extras on Prompt::Rendered; Request#max_tokens; Response#raw_id.
  • Options: Generation's locale:; PdfReading's plan:; Pdf::PageImages.render's dpi: and **limits (timeout: stays as the test seam); the probes' openai_compatible: and extra_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_endpoint folds into Ai::Instrumentation::Redactor.url;
    • the RubyLLM probe dispatch.
  • Comments that named deleted code or told history.

Kept after checking against the merged base:

  • Response#finish_reason, which the .ai event 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_endpoint and #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::LegacyEnvConfig and once in Eval::ProviderFactory. Merging them would change the eval's timeout and JSON-mode fallbacks.
  • LegacyEnvConfig filters the {session_id} header a second time.
  • The Settings::HostingsHelper forwarders. Inlining them would put Ai::LegacyEnvConfig calls 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 was AiHealth::ProbeTest "PDF vision probe sends the page as an image", which renders with pdftoppm and 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.