Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

Fix review round 3 of #49: name a refused request instead of "try again later"

Guido Boyke44fa97a5f52c
diff --git a/app/services/lalabase/CLAUDE.md b/app/services/lalabase/CLAUDE.md
index 630b33b..51e0ad6 100644
--- a/app/services/lalabase/CLAUDE.md
+++ b/app/services/lalabase/CLAUDE.md
@@ -237,5 +237,9 @@ Wichtig:
`Meetings::BaseLlmService` meldet dann den Fehler `truncated` statt den Teiltext zu
speichern. Kam vor dem Limit gar kein Text, werfen OpenAI, Vertex und Anthropic
`Provider::TruncatedError` (eine Unterklasse von `Provider::Error`, mit dem Ergebnis samt
- Verbrauch); `BaseLlmService` bucht ihn und meldet ebenfalls `truncated`. ⚠️ Der Assistent
+ Verbrauch); `BaseLlmService` bucht ihn und meldet ebenfalls `truncated`. Lehnt der
+ Anbieter die Anfrage ab (400, etwa ein Modell mit kleinerem Ausgabe-Limit als die
+ 8.000 Tokens der Analyse oder ein zu kleines Kontextfenster), werfen OpenAI, Vertex und
+ Anthropic `Provider::RequestRefusedError`; `BaseLlmService` meldet `request_refused`
+ bzw. bei eigenem Zugang `request_refused_own_access`, nie „später erneut versuchen“. ⚠️ Der Assistent
(`Tools::Runner`) und die Teilnehmer-Erkennung werten `'length'` noch nicht aus.
diff --git a/app/services/lalabase/llm/google_vertex_provider.rb b/app/services/lalabase/llm/google_vertex_provider.rb
index f71c0c3..844c30c 100644
--- a/app/services/lalabase/llm/google_vertex_provider.rb
+++ b/app/services/lalabase/llm/google_vertex_provider.rb
@@ -390,12 +390,15 @@ module Lalabase
# HTTP status → canonical provider error: the mapped_error pattern of the
# SDK-backed providers, applied to raw REST statuses. 401/403 auth, 429
- # rate limit, anything else a generic (but diagnostic) Provider::Error.
+ # rate limit, 400 refused (the request itself was not accepted, e.g. a
+ # maxOutputTokens above the model's limit), anything else a generic (but
+ # diagnostic) Provider::Error.
def raise_mapped_http_error(status, body)
message = "Vertex AI HTTP #{status}: #{error_message_from(body)}"
case status
when 401, 403 then raise AuthError, message
when 429 then raise RateLimitError, message
+ when 400 then raise RequestRefusedError, message
else raise Error, message
end
end
diff --git a/app/services/lalabase/llm/open_ai_provider.rb b/app/services/lalabase/llm/open_ai_provider.rb
index 0416b88..6b81a7d 100644
--- a/app/services/lalabase/llm/open_ai_provider.rb
+++ b/app/services/lalabase/llm/open_ai_provider.rb
@@ -111,7 +111,7 @@ module Lalabase
end
result
- rescue Faraday::UnauthorizedError, Faraday::TooManyRequestsError => e
+ rescue Faraday::UnauthorizedError, Faraday::TooManyRequestsError, Faraday::BadRequestError => e
raise mapped_error(e)
end
@@ -168,7 +168,7 @@ module Lalabase
end
{ content: captured_content, model_used: model, token_usage: usage }
- rescue Faraday::UnauthorizedError, Faraday::TooManyRequestsError => e
+ rescue Faraday::UnauthorizedError, Faraday::TooManyRequestsError, Faraday::BadRequestError => e
raise mapped_error(e)
end
@@ -270,6 +270,9 @@ module Lalabase
case error
when Faraday::UnauthorizedError then AuthError.new(error.message)
when Faraday::TooManyRequestsError then RateLimitError.new(error.message)
+ # The endpoint refused what we asked for (max_tokens above the model's
+ # output limit, prompt + max_tokens beyond its context): see RequestRefusedError.
+ when Faraday::BadRequestError then RequestRefusedError.new(error.message)
else error
end
end
diff --git a/app/services/meetings/analysis_service.rb b/app/services/meetings/analysis_service.rb
index 4113482..324220f 100644
--- a/app/services/meetings/analysis_service.rb
+++ b/app/services/meetings/analysis_service.rb
@@ -21,6 +21,10 @@ module Meetings
# 1,500 tokens. Since an answer cut at the limit is an error (BaseLlmService),
# a small budget would fail every long analysis on a model without a thinking
# reserve. Still bounded; SOURCE_CHAR_LIMIT bounds the input.
+ # Requirement for the configured model: an output limit of at least 8,000
+ # tokens (plus the thinking reserve the provider adds) and a context window
+ # for the transcript plus 8,000. A model below that refuses with a 400, shown
+ # as services.meetings.analysis.request_refused*, never as "try again later".
MAX_TOKENS = 8_000
TEMPERATURE = 0.5
diff --git a/app/services/meetings/base_llm_service.rb b/app/services/meetings/base_llm_service.rb
index 66797f1..70a6020 100644
--- a/app/services/meetings/base_llm_service.rb
+++ b/app/services/meetings/base_llm_service.rb
@@ -86,6 +86,14 @@ module Meetings
rescue Lalabase::Llm::AiDisabledError
# ai_enabled? returns ai_disabled above first; this is defence in depth.
record_error(t('ai_disabled'))
+ rescue Lalabase::Llm::Provider::RequestRefusedError => e
+ # The endpoint refused the request (a model whose output limit is below
+ # the answer budget, a prompt beyond its context, an exhausted balance).
+ # Waiting changes nothing, so not the generic "try again later". Who can
+ # fix it decides the copy, as in ChatResponseJob#refused_copy; the
+ # provider's own words stay in the log.
+ Rails.logger.error "#{log_prefix}: provider refused the request: #{e.message}"
+ record_error(t(own_access? ? 'request_refused_own_access' : 'request_refused'))
rescue Lalabase::Llm::Provider::AuthError => e
Rails.logger.error "#{log_prefix}: invalid AI key: #{e.message}"
record_error(t('invalid_key'))
@@ -183,6 +191,13 @@ module Meetings
record.organisation
end
+ # Does this request run on the organisation's own access (on the rung of
+ # this feature)? Same question ChatResponseJob#refused_copy asks.
+ def own_access?
+ utility = Lalabase::Llm::FeatureRegistry.fetch(feature_key).utility?
+ Lalabase::Llm::Factory.own_chat_access?(organisation, utility: utility)
+ end
+
# Caps `text` at SOURCE_CHAR_LIMIT. Returns [capped_text, truncated?] so the
# subclass can phrase its own truncation notice on the prompt.
def cap_source(text)
diff --git a/config/locales/services.de.yml b/config/locales/services.de.yml
index 1b0f7d6..8de0470 100644
--- a/config/locales/services.de.yml
+++ b/config/locales/services.de.yml
@@ -110,6 +110,8 @@ de:
generic_error: "Die Zusammenfassung konnte nicht erstellt werden. Bitte versuchen Sie es später erneut."
quota_exhausted: "Das KI-Kontingent ist aufgebraucht. Die Zusammenfassung ist nach Budget-Erhöhung oder Monatsreset wieder möglich."
truncated: "Die Antwort der KI war zu lang und wurde abgeschnitten. Bitte versuchen Sie es erneut."
+ request_refused: "Der KI-Anbieter hat die Anfrage abgelehnt. Ein erneuter Versuch ändert daran nichts; bitte wenden Sie sich an den Betreiber dieser Instanz."
+ request_refused_own_access: "Der KI-Anbieter hat die Anfrage abgelehnt. Diese Organisation nutzt einen eigenen Zugang: Ein Administrator kann in den KI-Einstellungen prüfen, ob das Modell verfügbar ist und Guthaben besteht."
# Slice 5: JSON messages of the detect_participants endpoint — gate answers
# (ai_unavailable/empty/too_large) AND the service status mapping, one source.
participant_detection:
@@ -132,6 +134,8 @@ de:
generic_error: "Die Analyse konnte nicht erstellt werden. Bitte versuchen Sie es später erneut."
quota_exhausted: "Das KI-Kontingent ist aufgebraucht. Die Analyse ist nach Budget-Erhöhung oder Monatsreset wieder möglich."
truncated: "Die Antwort der KI war zu lang und wurde abgeschnitten. Bitte fordern Sie in der Arbeitsanweisung ein kürzeres Ergebnis an oder teilen Sie die Analyse auf."
+ request_refused: "Der KI-Anbieter hat die Anfrage abgelehnt. Ein erneuter Versuch ändert daran nichts; bitte wenden Sie sich an den Betreiber dieser Instanz."
+ request_refused_own_access: "Der KI-Anbieter hat die Anfrage abgelehnt. Diese Organisation nutzt einen eigenen Zugang: Ein Administrator kann in den KI-Einstellungen prüfen, ob das gewählte Modell so lange Antworten und Mitschriften dieser Länge annimmt und ob Guthaben besteht."
not_found: "Die Analyse existiert nicht mehr."
prompt_not_found: "Die gewählte Analyse-Vorlage wurde nicht gefunden."
prompt_blank: "Bitte eine Vorlage wählen oder einen Prompt eingeben."
@@ -145,6 +149,8 @@ de:
quota_exhausted: "Das KI-Kontingent ist aufgebraucht. Die Ticket-Vorschläge sind nach Budget-Erhöhung oder Monatsreset wieder möglich."
parse_error: "Die KI-Antwort war nicht auswertbar. Bitte versuche es erneut."
truncated: "Die Antwort der KI war zu lang und wurde abgeschnitten. Bitte versuche es erneut."
+ request_refused: "Der KI-Anbieter hat die Anfrage abgelehnt. Ein erneuter Versuch ändert daran nichts; bitte wende dich an den Betreiber dieser Instanz."
+ request_refused_own_access: "Der KI-Anbieter hat die Anfrage abgelehnt. Diese Organisation nutzt einen eigenen Zugang: Ein Administrator kann in den KI-Einstellungen prüfen, ob das Modell verfügbar ist und Guthaben besteht."
post_update:
unexpected_error: "Unerwarteter Fehler: %{error}"
default_smart_folder:
diff --git a/config/locales/services.en.yml b/config/locales/services.en.yml
index dfda5bf..4c73191 100644
--- a/config/locales/services.en.yml
+++ b/config/locales/services.en.yml
@@ -110,6 +110,8 @@ en:
generic_error: "The summary could not be created. Please try again later."
quota_exhausted: "The AI quota is used up. The summary is possible again after a budget increase or the monthly reset."
truncated: "The AI answer was too long and was cut off. Please try again."
+ request_refused: "The AI provider refused the request. Trying again will not change that; please contact the operator of this instance."
+ request_refused_own_access: "The AI provider refused the request. This organisation uses its own access: an administrator can check in the AI settings that the model is available and that there is credit."
# Slice 5: JSON messages of the detect_participants endpoint — gate answers
# (ai_unavailable/empty/too_large) AND the service status mapping, one source.
participant_detection:
@@ -132,6 +134,8 @@ en:
generic_error: "The analysis could not be created. Please try again later."
quota_exhausted: "The AI quota is used up. The analysis is possible again after a budget increase or the monthly reset."
truncated: "The AI answer was too long and was cut off. Please ask for a shorter result in the instruction, or split the analysis."
+ request_refused: "The AI provider refused the request. Trying again will not change that; please contact the operator of this instance."
+ request_refused_own_access: "The AI provider refused the request. This organisation uses its own access: an administrator can check in the AI settings that the chosen model accepts answers this long and transcripts of this length, and that there is credit."
not_found: "The analysis no longer exists."
prompt_not_found: "The selected analysis template was not found."
prompt_blank: "Please choose a template or enter a prompt."
@@ -145,6 +149,8 @@ en:
quota_exhausted: "The AI quota is used up. The ticket suggestions are possible again after a budget increase or the monthly reset."
parse_error: "The AI response could not be parsed. Please try creating suggestions again."
truncated: "The AI answer was too long and was cut off. Please try creating suggestions again."
+ request_refused: "The AI provider refused the request. Trying again will not change that; please contact the operator of this instance."
+ request_refused_own_access: "The AI provider refused the request. This organisation uses its own access: an administrator can check in the AI settings that the model is available and that there is credit."
post_update:
unexpected_error: "Unexpected error: %{error}"
default_smart_folder:
diff --git a/spec/services/lalabase/llm/google_vertex_provider_spec.rb b/spec/services/lalabase/llm/google_vertex_provider_spec.rb
index 868ed21..715a19f 100644
--- a/spec/services/lalabase/llm/google_vertex_provider_spec.rb
+++ b/spec/services/lalabase/llm/google_vertex_provider_spec.rb
@@ -511,10 +511,21 @@ RSpec.describe Lalabase::Llm::GoogleVertexProvider do
end
end
+ # A 400 is the endpoint refusing what we asked for (a max_tokens above the
+ # model's output limit, a prompt beyond its context): retrying cannot help,
+ # so it is the same RequestRefusedError the Anthropic provider raises.
+ it 'maps HTTP 400 → RequestRefusedError' do
+ stub_post(status: 400, body: '{"error":{"message":"max output tokens"}}')
+ expect { provider.chat(messages: messages) }
+ .to raise_error(Lalabase::Llm::Provider::RequestRefusedError, /HTTP 400.*max output tokens/)
+ end
+
it 'raises a generic Provider::Error with diagnostics for other statuses' do
stub_post(status: 500, body: '{"error":{"message":"boom"}}')
expect { provider.chat(messages: messages) }
- .to raise_error(Lalabase::Llm::Provider::Error, /HTTP 500.*boom/)
+ .to raise_error(Lalabase::Llm::Provider::Error, /HTTP 500.*boom/) { |error|
+ expect(error).not_to be_a(Lalabase::Llm::Provider::RequestRefusedError)
+ }
end
it 'copes with non-JSON error bodies' do
diff --git a/spec/services/lalabase/llm/open_ai_provider_spec.rb b/spec/services/lalabase/llm/open_ai_provider_spec.rb
index c9b9aff..2a85a8d 100644
--- a/spec/services/lalabase/llm/open_ai_provider_spec.rb
+++ b/spec/services/lalabase/llm/open_ai_provider_spec.rb
@@ -218,6 +218,14 @@ RSpec.describe Lalabase::Llm::OpenAiProvider do
allow(fake_client).to receive(:chat).and_raise(Faraday::TooManyRequestsError.new('429'))
expect { provider.chat(messages: messages) }.to raise_error(Lalabase::Llm::Provider::RateLimitError)
end
+
+ # A 400 is the endpoint refusing what we asked for (max_tokens above the
+ # model's output limit, prompt + max_tokens beyond its context): retrying
+ # cannot help. It used to escape as a raw Faraday error.
+ it 'maps Faraday::BadRequestError → RequestRefusedError' do
+ allow(fake_client).to receive(:chat).and_raise(Faraday::BadRequestError.new('400'))
+ expect { provider.chat(messages: messages) }.to raise_error(Lalabase::Llm::Provider::RequestRefusedError)
+ end
end
# -------------------------------------------------------------------------
@@ -319,6 +327,12 @@ RSpec.describe Lalabase::Llm::OpenAiProvider do
expect { provider.stream_chat(messages: messages) { |_| } }.to raise_error(Lalabase::Llm::Provider::AuthError)
end
+ it 'maps Faraday::BadRequestError → RequestRefusedError' do
+ allow(fake_client).to receive(:chat).and_raise(Faraday::BadRequestError.new('400'))
+ expect { provider.stream_chat(messages: messages) { |_| } }
+ .to raise_error(Lalabase::Llm::Provider::RequestRefusedError)
+ end
+
it 'handles a completely empty stream (no chunks at all)' do
allow(fake_client).to receive(:chat) { |_args| nil }
result = provider.stream_chat(messages: messages) { |_| }
diff --git a/spec/services/meetings/analysis_service_spec.rb b/spec/services/meetings/analysis_service_spec.rb
index c1cac53..d263e31 100644
--- a/spec/services/meetings/analysis_service_spec.rb
+++ b/spec/services/meetings/analysis_service_spec.rb
@@ -221,6 +221,43 @@ RSpec.describe Meetings::AnalysisService do
end
end
+ # The endpoint refused the request (400): a model whose output limit is below
+ # the answer budget, or a prompt + budget beyond its context. Waiting does not
+ # change that, so the message must not say "try again later".
+ describe 'request refused by the provider' do
+ let(:transcript) { 'Some transcript' }
+
+ before do
+ allow(provider).to receive(:chat).and_raise(Lalabase::Llm::Provider::RequestRefusedError, 'HTTP 400: too long')
+ end
+
+ it 'records the refused message (not the generic one) when the operator access is used' do
+ described_class.call(analysis: analysis)
+ analysis.reload
+ expect(analysis.creation_error).to eq(I18n.t('services.meetings.analysis.request_refused'))
+ expect(analysis.creation_error).not_to eq(I18n.t('services.meetings.analysis.generic_error'))
+ expect(analysis.creation_flag).to be(false)
+ end
+
+ it 'points to the AI settings when the organisation uses its own access' do
+ organisation.update!(ai_provider: AiCredential::PROVIDER_ANTHROPIC,
+ ai_access_token: 'sk-ant-test-token-that-is-long-enough')
+ described_class.call(analysis: analysis)
+ expect(analysis.reload.creation_error).to eq(I18n.t('services.meetings.analysis.request_refused_own_access'))
+ end
+
+ it 'asks whose access it was on the main rung (an analysis is not a utility feature)' do
+ allow(Lalabase::Llm::Factory).to receive(:own_chat_access?).and_call_original
+ described_class.call(analysis: analysis)
+ expect(Lalabase::Llm::Factory).to have_received(:own_chat_access?).with(organisation, utility: false)
+ end
+
+ it 'keeps the provider message out of the stored error' do
+ described_class.call(analysis: analysis)
+ expect(analysis.reload.creation_error).not_to include('too long')
+ end
+ end
+
describe 'an answer that ended normally (stop_reason stop)' do
let(:transcript) { 'Some transcript' }
let(:chat_result) { { content: 'Fertige Analyse.', stop_reason: 'stop', token_usage: {}, model_used: 'gpt-4o-mini' } }
diff --git a/spec/services/meetings/ticket_suggestion_service_spec.rb b/spec/services/meetings/ticket_suggestion_service_spec.rb
index e9e7833..d9659a4 100644
--- a/spec/services/meetings/ticket_suggestion_service_spec.rb
+++ b/spec/services/meetings/ticket_suggestion_service_spec.rb
@@ -233,6 +233,29 @@ RSpec.describe Meetings::TicketSuggestionService do
end
end
+ context 'when the provider refuses the request' do
+ before { allow(provider).to receive(:chat).and_raise(Lalabase::Llm::Provider::RequestRefusedError, 'HTTP 400') }
+
+ it 'records its own refused message and leaves the open suggestions untouched' do
+ old_open = create(:meeting_ticket_suggestion, meeting: meeting)
+
+ described_class.call(meeting: meeting)
+
+ expect(MeetingTicketSuggestion.exists?(old_open.id)).to be(true)
+ expect(meeting.reload.suggestions_creation_error)
+ .to eq(I18n.t('services.meetings.ticket_suggestions.request_refused'))
+ expect(meeting.suggestions_creation_flag).to be(false)
+ end
+
+ # Ticket suggestions route on the utility rung (FeatureRegistry), and that
+ # rung may run on a different access than the organisation's main one.
+ it 'asks whose access it was on the utility rung this request routed on' do
+ allow(Lalabase::Llm::Factory).to receive(:own_chat_access?).and_call_original
+ described_class.call(meeting: meeting)
+ expect(Lalabase::Llm::Factory).to have_received(:own_chat_access?).with(organisation, utility: true)
+ end
+ end
+
context 'when the quota is exhausted (Factory raises)' do
before { allow(Lalabase::Llm::Factory).to receive(:for_feature).with(:meeting_ticket_suggestions, organisation).and_raise(Lalabase::Llm::QuotaExceededError) }