The personal finance app for everyone (by everyone)
Ruby
feat(ai-health): let admins set the health check timeout under Self-Hosting (#4019)
GFRe2661a825a8d
diff --git a/.env.example b/.env.exampleindex 6123a6c685..f6558c1812 100644--- a/.env.example+++ b/.env.example@@ -87,7 +87,7 @@ OPENAI_URI_BASE=# EMBEDDING_MODEL=mxbai-embed-large# EMBEDDING_DIMENSIONS=1024 # Must match the embedding model# EMBEDDING_ACCESS_TOKEN= # Optional; falls back to OPENAI_ACCESS_TOKEN-# AI_HEALTH_PROBE_TIMEOUT=5 # Per-request timeout for admin live checks+# AI_HEALTH_PROBE_TIMEOUT=5 # Per-request timeout for admin live checks (overrides Self-Hosting setting)# AI_HEALTH_PROBE_CACHE_TTL=60 # Cache live results and deduplicate failure logs# Optional: External AI Assistant — delegates chat to a remote AI agentdiff --git a/app/controllers/settings/hostings_controller.rb b/app/controllers/settings/hostings_controller.rbindex 53cf185fb2..fc44b57f98 100644--- a/app/controllers/settings/hostings_controller.rb+++ b/app/controllers/settings/hostings_controller.rb@@ -9,7 +9,8 @@ class Settings::HostingsController < ApplicationControllerllm_max_response_tokens: 64,llm_max_items_per_call: 1,openai_request_timeout: Provider::Openai::MIN_REQUEST_TIMEOUT,- ai_response_timeout: Chat::MIN_RESPONSE_TIMEOUT.to_i+ ai_response_timeout: Chat::MIN_RESPONSE_TIMEOUT.to_i,+ ai_health_probe_timeout: 1}.freezeguard_feature unless: -> { self_hosted? }@@ -323,7 +324,7 @@ class Settings::HostingsController < ApplicationController# Strong parameters for the self-hosting settings form.def hosting_paramsreturn ActionController::Parameters.new unless params.key?(:setting)- params.require(:setting).permit(:onboarding_state, :require_email_confirmation, :invite_only_default_family_id, :demo_family_refresh_enabled, :demo_family_refresh_family_id, :brand_fetch_client_id, :brand_fetch_high_res_logos, :twelve_data_api_key, :tiingo_api_key, :eodhd_api_key, :alpha_vantage_api_key, :tinkoff_invest_api_key, :mansa_api_key, :rentcast_api_key, :realie_api_key, :openai_access_token, :openai_uri_base, :openai_model, :openai_json_mode, :anthropic_access_token, :anthropic_base_url, :anthropic_model, :jev_api_key, :jev_endpoint, :jev_model, :llm_provider, :llm_context_window, :llm_max_response_tokens, :llm_max_items_per_call, :openai_request_timeout, :ai_response_timeout, :exchange_rate_provider, :securities_provider, :syncs_include_pending, :auto_sync_enabled, :auto_sync_time, :external_assistant_url, :external_assistant_token, :external_assistant_model, securities_providers: [])+ params.require(:setting).permit(:onboarding_state, :require_email_confirmation, :invite_only_default_family_id, :demo_family_refresh_enabled, :demo_family_refresh_family_id, :brand_fetch_client_id, :brand_fetch_high_res_logos, :twelve_data_api_key, :tiingo_api_key, :eodhd_api_key, :alpha_vantage_api_key, :tinkoff_invest_api_key, :mansa_api_key, :rentcast_api_key, :realie_api_key, :openai_access_token, :openai_uri_base, :openai_model, :openai_json_mode, :anthropic_access_token, :anthropic_base_url, :anthropic_model, :jev_api_key, :jev_endpoint, :jev_model, :llm_provider, :llm_context_window, :llm_max_response_tokens, :llm_max_items_per_call, :openai_request_timeout, :ai_response_timeout, :ai_health_probe_timeout, :exchange_rate_provider, :securities_provider, :syncs_include_pending, :auto_sync_enabled, :auto_sync_time, :external_assistant_url, :external_assistant_token, :external_assistant_model, securities_providers: [])enddef load_external_assistant_modelsdiff --git a/app/models/ai_health/probe.rb b/app/models/ai_health/probe.rbindex 5103529e23..182738fc01 100644--- a/app/models/ai_health/probe.rb+++ b/app/models/ai_health/probe.rb@@ -75,10 +75,11 @@ class AiHealth# 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.+ # never drift. Precedence: AI_HEALTH_PROBE_TIMEOUT > the Self-Hosting+ # setting > DEFAULT_TIMEOUT, with non-positive values treated as unset.def self.timeout- seconds = ENV.fetch("AI_HEALTH_PROBE_TIMEOUT", DEFAULT_TIMEOUT).to_i+ seconds = ENV["AI_HEALTH_PROBE_TIMEOUT"].to_s.strip.to_i+ seconds = Setting.ai_health_probe_timeout.to_i unless seconds.positive?seconds.positive? ? seconds : DEFAULT_TIMEOUTenddiff --git a/app/models/setting.rb b/app/models/setting.rbindex dc47539fbf..c9145d0f1b 100644--- a/app/models/setting.rb+++ b/app/models/setting.rb@@ -38,6 +38,11 @@ class Setting < RailsSettings::Base# generate would otherwise always trip the watchdog. Read via# `Chat.response_timeout`, which applies ENV > Setting > default precedence.field :ai_response_timeout, type: :integer, default: ENV["AI_RESPONSE_TIMEOUT"]&.to_i+ # Per-request bound for the admin "Run checks" AI probes on System Health.+ # The PDF probes do a full extraction round trip, which cloud models often+ # can't finish inside the 5s default. Read via `AiHealth::Probe.timeout`,+ # which applies ENV > Setting > default precedence.+ field :ai_health_probe_timeout, type: :integer, default: ENV["AI_HEALTH_PROBE_TIMEOUT"]&.to_ifield :external_assistant_url, type: :stringfield :external_assistant_token, type: :stringfield :external_assistant_model, type: :stringdiff --git a/app/views/settings/hostings/_openai_settings.html.erb b/app/views/settings/hostings/_openai_settings.html.erbindex 12284eac3a..44d835dc9a 100644--- a/app/views/settings/hostings/_openai_settings.html.erb+++ b/app/views/settings/hostings/_openai_settings.html.erb@@ -122,7 +122,18 @@min: Chat::MIN_RESPONSE_TIMEOUT.to_i,disabled: ENV["AI_RESPONSE_TIMEOUT"].present?,data: { "auto-submit-form-target": "auto" } %>- <p class="text-xs text-secondary mt-1"><%= t(".ai_response_timeout_help") %></p>+ <p class="text-xs text-secondary mt-1 mb-3"><%= t(".ai_response_timeout_help") %></p>++ <%# Probe.timeout ignores a non-positive env value, so only a positive one locks the field. %>+ <% probe_timeout_from_env = ENV["AI_HEALTH_PROBE_TIMEOUT"].to_s.strip.to_i.positive? %>+ <%= form.number_field :ai_health_probe_timeout,+ label: t(".ai_health_probe_timeout_label"),+ placeholder: AiHealth::Probe::DEFAULT_TIMEOUT.to_s,+ value: probe_timeout_from_env ? AiHealth::Probe.timeout : Setting.ai_health_probe_timeout,+ min: 1,+ disabled: probe_timeout_from_env,+ data: { "auto-submit-form-target": "auto" } %>+ <p class="text-xs text-secondary mt-1"><%= t(".ai_health_probe_timeout_help") %></p></div><% end %></div>diff --git a/config/locales/views/settings/hostings/de.yml b/config/locales/views/settings/hostings/de.ymlindex 75cbbe4269..d676ad49de 100644--- a/config/locales/views/settings/hostings/de.yml+++ b/config/locales/views/settings/hostings/de.yml@@ -104,6 +104,8 @@ de:openai_request_timeout_help: 'Standard: 60. Gilt für jede OpenAI-kompatible HTTP-Anfrage, einschließlich automatischer Kategorisierung, Händlererkennung, Chat, PDF-Verarbeitung und Vektorspeicher-Aufrufen. OPENAI_REQUEST_TIMEOUT überschreibt dieses Feld.'ai_response_timeout_label: Antwortzeitlimit in Sekunden (optional)ai_response_timeout_help: 'Standard: 90. Gilt nur für das Zeitlimit der gesamten Chat-Runde. Setz den Wert daher als Summe an: (1 + ASSISTANT_MAX_TOOL_CALL_ITERATIONS) × Anfragezeitlimit, plus Ausführungszeit der Tools und Wartezeit in der Warteschlange. AI_RESPONSE_TIMEOUT überschreibt dieses Feld.'+ ai_health_probe_timeout_label: Zeitlimit für Systemstatus-Prüfungen in Sekunden (optional)+ ai_health_probe_timeout_help: 'Standard: 5. Gilt nur für die KI-Live-Prüfungen unter Einstellungen → Systemstatus, nie für echte KI-Aufrufe. Erhöhe den Wert, wenn die PDF-Prüfungen mit einem Cloud-Modell ein Zeitlimit melden. AI_HEALTH_PROBE_TIMEOUT überschreibt dieses Feld.'yahoo_finance_settings:title: Yahoo Financedescription: Yahoo Finance bietet kostenlosen Zugriff auf Aktienkurse, Wechselkurse und Finanzdaten – ganz ohne API-Schlüssel.diff --git a/config/locales/views/settings/hostings/en.yml b/config/locales/views/settings/hostings/en.ymlindex e4ad2cc745..e9b31b7d61 100644--- a/config/locales/views/settings/hostings/en.yml+++ b/config/locales/views/settings/hostings/en.yml@@ -208,6 +208,8 @@ en:openai_request_timeout_help: "Default: 60. Applies to each OpenAI-compatible HTTP request, including auto-categorization, merchant detection, chat, PDF processing, and vector-store calls. OPENAI_REQUEST_TIMEOUT overrides this field."ai_response_timeout_label: Response Timeout in Seconds (Optional)ai_response_timeout_help: "Default: 90. Applies only to chat's whole-turn watchdog, so size it as a sum: (1 + ASSISTANT_MAX_TOOL_CALL_ITERATIONS) × request timeout, plus tool execution and queue time. AI_RESPONSE_TIMEOUT overrides this field."+ ai_health_probe_timeout_label: Health Check Timeout in Seconds (Optional)+ ai_health_probe_timeout_help: "Default: 5. Applies only to the live AI checks under Settings → System health, never to real AI calls. Raise it if the PDF checks report a timeout with a cloud model. AI_HEALTH_PROBE_TIMEOUT overrides this field."title: OpenAIyahoo_finance_settings:title: Yahoo Financediff --git a/test/controllers/settings/hostings_controller_test.rb b/test/controllers/settings/hostings_controller_test.rbindex 16a4ca8313..818c153bfa 100644--- a/test/controllers/settings/hostings_controller_test.rb+++ b/test/controllers/settings/hostings_controller_test.rb@@ -27,7 +27,7 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTestteardown do# These tests persist global Setting.* values; reset them so state can't# leak into later (order-dependent) tests.- %i[anthropic_access_token anthropic_base_url anthropic_model jev_api_key jev_endpoint jev_model llm_provider twelve_data_api_key openai_access_token openai_request_timeout ai_response_timeout external_assistant_url external_assistant_token external_assistant_model external_assistant_agent_id rentcast_api_key realie_api_key demo_family_refresh_enabled demo_family_refresh_family_id].each do |key|+ %i[anthropic_access_token anthropic_base_url anthropic_model jev_api_key jev_endpoint jev_model llm_provider twelve_data_api_key openai_access_token openai_request_timeout ai_response_timeout ai_health_probe_timeout external_assistant_url external_assistant_token external_assistant_model external_assistant_agent_id rentcast_api_key realie_api_key demo_family_refresh_enabled demo_family_refresh_family_id].each do |key|Setting.public_send("#{key}=", nil)endend@@ -79,6 +79,8 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTestopenai_request_timeout_helpai_response_timeout_labelai_response_timeout_help+ ai_health_probe_timeout_label+ ai_health_probe_timeout_help].each do |key|assert I18n.exists?("settings.hostings.openai_settings.#{key}", :de, fallback: false)end@@ -950,7 +952,8 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTestllm_max_response_tokens: "1024",llm_max_items_per_call: "40",openai_request_timeout: "180",- ai_response_timeout: "240"+ ai_response_timeout: "240",+ ai_health_probe_timeout: "30"} }assert_redirected_to settings_hosting_url@@ -959,13 +962,15 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTestassert_equal 40, Setting.llm_max_items_per_callassert_equal 180, Setting.openai_request_timeoutassert_equal 240, Setting.ai_response_timeout+ assert_equal 30, Setting.ai_health_probe_timeoutpatch settings_hosting_url, params: { setting: {llm_context_window: "",llm_max_response_tokens: "",llm_max_items_per_call: "",openai_request_timeout: "",- ai_response_timeout: ""+ ai_response_timeout: "",+ ai_health_probe_timeout: ""} }assert_nil Setting.llm_context_window@@ -973,6 +978,7 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTestassert_nil Setting.llm_max_items_per_callassert_nil Setting.openai_request_timeoutassert_nil Setting.ai_response_timeout+ assert_nil Setting.ai_health_probe_timeoutendensureSetting.llm_context_window = nil@@ -980,6 +986,7 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTestSetting.llm_max_items_per_call = nilSetting.openai_request_timeout = nilSetting.ai_response_timeout = nil+ Setting.ai_health_probe_timeout = nilendtest "rejects llm budget below field minimum" do@@ -1007,6 +1014,12 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTestassert_response :unprocessable_entityassert_match(/must be a whole number/, flash[:alert])assert_nil Setting.openai_request_timeout++ patch settings_hosting_url, params: { setting: { ai_health_probe_timeout: "0" } }++ assert_response :unprocessable_entity+ assert_match(/must be a whole number/, flash[:alert])+ assert_nil Setting.ai_health_probe_timeoutendensureSetting.llm_context_window = nil@@ -1030,6 +1043,29 @@ class Settings::HostingsControllerTest < ActionDispatch::IntegrationTestSetting.openai_request_timeout = nilend+ test "shows environment backed health check timeout when field is disabled" do+ with_self_hosting do+ Setting.ai_health_probe_timeout = 30++ with_env_overrides("AI_HEALTH_PROBE_TIMEOUT" => "45") do+ get settings_hosting_url++ assert_response :success+ assert_select "input[name='setting[ai_health_probe_timeout]'][value='45'][disabled='disabled']"+ end++ # A non-positive env value is ignored by the probes, so it must not lock the field either.+ with_env_overrides("AI_HEALTH_PROBE_TIMEOUT" => "0") do+ get settings_hosting_url++ assert_response :success+ assert_select "input[name='setting[ai_health_probe_timeout]'][value='30']:not([disabled])"+ end+ end+ ensure+ Setting.ai_health_probe_timeout = nil+ end+test "can clear data only when admin" dowith_self_hosting dosign_in users(:family_member)diff --git a/test/models/ai_health/probe_test.rb b/test/models/ai_health/probe_test.rbindex ca341e4e7f..c7e0f239b8 100644--- a/test/models/ai_health/probe_test.rb+++ b/test/models/ai_health/probe_test.rb@@ -51,6 +51,27 @@ class AiHealth::ProbeTest < ActiveSupport::TestCaseassert_equal AiHealth::Probe.timeout, @probe.send(:timeout)end+ test "timeout falls back to the self-hosting setting when AI_HEALTH_PROBE_TIMEOUT is unset" do+ Setting.stubs(:ai_health_probe_timeout).returns(30)++ ClimateControl.modify(AI_HEALTH_PROBE_TIMEOUT: nil) do+ assert_equal 30, AiHealth::Probe.timeout+ end++ ClimateControl.modify(AI_HEALTH_PROBE_TIMEOUT: "0") do+ assert_equal 30, AiHealth::Probe.timeout+ end++ ClimateControl.modify(AI_HEALTH_PROBE_TIMEOUT: "42") do+ assert_equal 42, AiHealth::Probe.timeout+ end++ Setting.stubs(:ai_health_probe_timeout).returns(0)+ ClimateControl.modify(AI_HEALTH_PROBE_TIMEOUT: nil) do+ assert_equal AiHealth::Probe::DEFAULT_TIMEOUT, AiHealth::Probe.timeout+ end+ end+test "OpenAI-compatible LLM probe calls chat completions instead of the models endpoint" doendpoint = "https://api.cloudflare.com/client/v4/accounts/account-id/ai/v1"request = stub_request(:post, "#{endpoint}/chat/completions")