Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Surface probe timeout separately from LLM request timeout on System Health (#3276)

jaysbeekayf7a7736e1bc3
diff --git a/app/models/ai_health.rb b/app/models/ai_health.rb
index bd39fddf20..c3e4f86ab3 100644
--- a/app/models/ai_health.rb
+++ b/app/models/ai_health.rb
@@ -15,7 +15,7 @@ class AiHealth
}.freeze
attr_reader :selected_llm_provider, :effective_llm_provider, :llm_model,
- :llm_endpoint, :llm_request_timeout, :openai_endpoint, :vector_store_adapter,
+ :llm_endpoint, :llm_request_timeout, :probe_request_timeout, :openai_endpoint, :vector_store_adapter,
:embedding_endpoint, :embedding_model, :embedding_dimensions,
:pgvector_extension_available, :pgvector_extension_enabled,
:pgvector_table_available, :qdrant_endpoint, :llm_probe,
@@ -144,6 +144,7 @@ class AiHealth
@llm_model = effective_model(provider_for_details)
@llm_endpoint = endpoint(provider_for_details)
@llm_request_timeout = request_timeout(provider_for_details)
+ @probe_request_timeout = probe_request_timeout_value
@openai_uses_responses_endpoint = @effective_llm_protocol == :openai &&
safely(false) { @llm_provider.supports_responses_endpoint? }
@pdf_processing_capable = safely(false) do
@@ -346,6 +347,15 @@ class AiHealth
end
end
+ # Timeout applied to the admin "live checks" probes. Delegates to
+ # Probe.timeout so System Health reports the exact bound the probes use,
+ # guaranteeing the two can never drift. Deliberately distinct from
+ # request_timeout, which bounds the LLM calls the app makes during
+ # normal use (chat, PDF import).
+ def probe_request_timeout_value
+ Probe.timeout
+ end
+
def openai_uri_base
ENV["OPENAI_URI_BASE"].presence || Setting.openai_uri_base
end
diff --git a/app/models/ai_health/probe.rb b/app/models/ai_health/probe.rb
index 9046e95bc0..5103529e23 100644
--- a/app/models/ai_health/probe.rb
+++ b/app/models/ai_health/probe.rb
@@ -72,6 +72,16 @@ class AiHealth
Result.new(status: :not_configured, checked_at: nil, failure_code: nil, http_status: nil)
end
+ # Seconds each probe request is allowed to run. Single source of truth:
+ # both the probes (instance methods below) and AiHealth's System Health
+ # reporting resolve the effective timeout through here so the two can
+ # never drift. Honors AI_HEALTH_PROBE_TIMEOUT; falls back to
+ # DEFAULT_TIMEOUT when unset or non-positive.
+ def self.timeout
+ seconds = ENV.fetch("AI_HEALTH_PROBE_TIMEOUT", DEFAULT_TIMEOUT).to_i
+ seconds.positive? ? seconds : DEFAULT_TIMEOUT
+ end
+
def initialize(force: false, cache: Rails.cache)
@force = force
@cache = cache
@@ -471,8 +481,7 @@ class AiHealth
end
def timeout
- seconds = ENV.fetch("AI_HEALTH_PROBE_TIMEOUT", DEFAULT_TIMEOUT).to_i
- seconds.positive? ? seconds : DEFAULT_TIMEOUT
+ self.class.timeout
end
# Map a probe error to a machine-readable code for the admin AI status
diff --git a/app/views/admin/system_health/_ai_status.html.erb b/app/views/admin/system_health/_ai_status.html.erb
index b953172629..99355c5421 100644
--- a/app/views/admin/system_health/_ai_status.html.erb
+++ b/app/views/admin/system_health/_ai_status.html.erb
@@ -153,9 +153,13 @@
</div>
<% end %>
<div>
- <dt class="text-secondary"><%= t("admin.system_health.show.ai.labels.request_timeout") %></dt>
+ <dt class="text-secondary"><%= t("admin.system_health.show.ai.labels.llm_request_timeout") %></dt>
<dd class="mt-1 font-medium text-primary"><%= t("admin.system_health.show.values.seconds", seconds: ai_health.llm_request_timeout) %></dd>
</div>
+ <div>
+ <dt class="text-secondary"><%= t("admin.system_health.show.ai.labels.probe_request_timeout") %></dt>
+ <dd class="mt-1 font-medium text-primary"><%= t("admin.system_health.show.values.seconds", seconds: ai_health.probe_request_timeout) %></dd>
+ </div>
<div>
<dt class="text-secondary">
<%= t("admin.system_health.show.ai.labels.#{ai_health.openai_compatible_endpoint? ? :openai_compatible_credentials : :openai_credentials}") %>
diff --git a/compose.example.ai.yml b/compose.example.ai.yml
index 254faef2ab..96777a54b9 100644
--- a/compose.example.ai.yml
+++ b/compose.example.ai.yml
@@ -110,6 +110,12 @@ x-rails-env: &rails_env
# an hour. The trade is that longer tool chains fail earlier, with an explicit
# tool-call limit error rather than a timeout.
OPENAI_REQUEST_TIMEOUT: ${OPENAI_REQUEST_TIMEOUT:-300}
+ # Health-check probes (System Health → AI Status → "Run checks") bound each
+ # probe call. Distinct from OPENAI_REQUEST_TIMEOUT, which bounds real LLM
+ # calls the app makes; the probe default is 5s. AI_HEALTH_PROBE_CACHE_TTL
+ # controls how long probe results are cached (default 60s).
+ AI_HEALTH_PROBE_TIMEOUT: ${AI_HEALTH_PROBE_TIMEOUT:-}
+ AI_HEALTH_PROBE_CACHE_TTL: ${AI_HEALTH_PROBE_CACHE_TTL:-}
ASSISTANT_MAX_TOOL_CALL_ITERATIONS: ${ASSISTANT_MAX_TOOL_CALL_ITERATIONS:-2}
AI_RESPONSE_TIMEOUT: ${AI_RESPONSE_TIMEOUT:-1200}
# Vector store — pgvector keeps all data local (requires pgvector/pgvector Docker image for db)
diff --git a/compose.example.yml b/compose.example.yml
index e2ebabac54..ec00a11f46 100644
--- a/compose.example.yml
+++ b/compose.example.yml
@@ -62,6 +62,12 @@ x-rails-env: &rails_env
OPENAI_URI_BASE: ${OPENAI_URI_BASE:-}
LLM_CONTEXT_WINDOW: ${LLM_CONTEXT_WINDOW:-}
OPENAI_REQUEST_TIMEOUT: ${OPENAI_REQUEST_TIMEOUT:-60}
+ # Health-check probes (System Health → AI Status → "Run checks") bound each
+ # probe call. Distinct from OPENAI_REQUEST_TIMEOUT, which bounds real LLM
+ # calls the app makes; the probe default is 5s. AI_HEALTH_PROBE_CACHE_TTL
+ # controls how long probe results are cached (default 60s).
+ AI_HEALTH_PROBE_TIMEOUT: ${AI_HEALTH_PROBE_TIMEOUT:-}
+ AI_HEALTH_PROBE_CACHE_TTL: ${AI_HEALTH_PROBE_CACHE_TTL:-}
# Seconds the chat waits for an assistant response before showing a "no response"
# error. This covers the whole turn, so its worst case is a sum:
# (1 + ASSISTANT_MAX_TOOL_CALL_ITERATIONS) * OPENAI_REQUEST_TIMEOUT
diff --git a/config/locales/views/admin/system_health/en.yml b/config/locales/views/admin/system_health/en.yml
index eab6c4ba45..2d805eba22 100644
--- a/config/locales/views/admin/system_health/en.yml
+++ b/config/locales/views/admin/system_health/en.yml
@@ -83,7 +83,8 @@ en:
function_calling: Function calling (tools)
pdf_text_extraction: PDF text-extraction path
pdf_vision_processing: PDF vision/native path
- request_timeout: Request timeout
+ llm_request_timeout: LLM request timeout
+ probe_request_timeout: Health-check probe timeout
openai_credentials: OpenAI credentials
openai_compatible_credentials: OpenAI-compatible API credentials
anthropic_credentials: Anthropic credentials
diff --git a/test/controllers/admin/system_health_controller_test.rb b/test/controllers/admin/system_health_controller_test.rb
index 2d6f81fa1b..be79a3450d 100644
--- a/test/controllers/admin/system_health_controller_test.rb
+++ b/test/controllers/admin/system_health_controller_test.rb
@@ -220,6 +220,31 @@ class Admin::SystemHealthControllerTest < ActionDispatch::IntegrationTest
assert_no_match(/sk-secret-openai/, response.body)
end
+ test "AI status surfaces LLM and probe request timeouts as distinct values" do
+ sign_in users(:sure_support_staff)
+ stub_healthy_sidekiq
+
+ # OPENAI_REQUEST_TIMEOUT bounds real LLM calls the app makes (chat, PDF
+ # import). AI_HEALTH_PROBE_TIMEOUT only bounds the admin "live checks".
+ with_ai_environment(
+ "OPENAI_ACCESS_TOKEN" => "local-token",
+ "OPENAI_REQUEST_TIMEOUT" => "300",
+ "AI_HEALTH_PROBE_TIMEOUT" => "5"
+ ) do
+ get admin_system_health_url(tab: "ai")
+ end
+
+ assert_response :success
+ llm_label = response.body.index("LLM request timeout")
+ probe_label = response.body.index("Health-check probe timeout")
+ assert llm_label, "expected an 'LLM request timeout' label on the AI status page"
+ assert probe_label, "expected a 'Health-check probe timeout' label on the AI status page"
+ assert_operator llm_label, :<, probe_label, "LLM timeout row should appear before the probe timeout row"
+ assert response.body[llm_label, 400].include?("300s"), "LLM timeout value (300s) missing near its label"
+ assert response.body[probe_label, 400].include?("5s"), "probe timeout value (5s) missing near its label"
+ assert_no_match(/local-token/, response.body)
+ end
+
test "AI status does not probe PDF processing when it is explicitly disabled" do
sign_in users(:sure_support_staff)
stub_healthy_sidekiq
diff --git a/test/models/ai_health/probe_test.rb b/test/models/ai_health/probe_test.rb
index 7b5e28a0f2..ca341e4e7f 100644
--- a/test/models/ai_health/probe_test.rb
+++ b/test/models/ai_health/probe_test.rb
@@ -27,6 +27,30 @@ class AiHealth::ProbeTest < ActiveSupport::TestCase
assert_requested request
end
+ test "timeout honors AI_HEALTH_PROBE_TIMEOUT and falls back for missing or non-positive values" do
+ ClimateControl.modify(AI_HEALTH_PROBE_TIMEOUT: "42") do
+ assert_equal 42, AiHealth::Probe.timeout
+ end
+
+ ClimateControl.modify(AI_HEALTH_PROBE_TIMEOUT: nil) do
+ assert_equal AiHealth::Probe::DEFAULT_TIMEOUT, AiHealth::Probe.timeout
+ end
+
+ ClimateControl.modify(AI_HEALTH_PROBE_TIMEOUT: "0") do
+ assert_equal AiHealth::Probe::DEFAULT_TIMEOUT, AiHealth::Probe.timeout
+ end
+
+ ClimateControl.modify(AI_HEALTH_PROBE_TIMEOUT: "-3") do
+ assert_equal AiHealth::Probe::DEFAULT_TIMEOUT, AiHealth::Probe.timeout
+ end
+
+ ClimateControl.modify(AI_HEALTH_PROBE_TIMEOUT: "not-a-number") do
+ assert_equal AiHealth::Probe::DEFAULT_TIMEOUT, AiHealth::Probe.timeout
+ end
+
+ assert_equal AiHealth::Probe.timeout, @probe.send(:timeout)
+ end
+
test "OpenAI-compatible LLM probe calls chat completions instead of the models endpoint" do
endpoint = "https://api.cloudflare.com/client/v4/accounts/account-id/ai/v1"
request = stub_request(:post, "#{endpoint}/chat/completions")