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.
Ruby agplai-assistantdockerdsgvogdprhotwireissue-trackerjira-alternativeknowledge-basellmmcp-servermeeting-notesopen-sourceproject-managementredmine-alternativeruby-on-railsself-hostedticketingtime-tracking
Treat an AI answer cut at the output limit as an error
Guido Boyke8fb9a8ca7d7a
diff --git a/app/services/meetings/base_llm_service.rb b/app/services/meetings/base_llm_service.rbindex 2017b65..bca5c52 100644--- a/app/services/meetings/base_llm_service.rb+++ b/app/services/meetings/base_llm_service.rb@@ -33,6 +33,11 @@ module Meetings# more than a summary/analysis needs and bounds the cost (review #235).SOURCE_CHAR_LIMIT = 100_000+ # The canonical stop_reason of an answer that hit max_tokens (Provider#chat).+ # Such an answer stops mid-sentence (or mid-JSON); it used to be stored as a+ # finished result without a hint.+ TRUNCATED_STOP_REASON = 'length'+# Template. Mirrors the original SummaryService#process one-to-one; the only# difference is that the varying parts are hook methods.def process@@ -61,6 +66,11 @@ module Meetings# Meter BEFORE handling the result — a malformed/empty answer still cost# tokens (track-before-parse, the ParticipantDetectionService rule).track_usage(provider, result)+ # A cut answer is never a finished one: checked after metering (it cost+ # tokens) and before handle_result, so every subclass gets it, including+ # one that overrides handle_result.+ return record_truncated(result) if result[:stop_reason] == TRUNCATED_STOP_REASON+handle_result(result)rescue Lalabase::Llm::Factory::MissingCredentialErrorrecord_error(t('not_configured'))@@ -195,6 +205,16 @@ module Meetingsrecord.update!(flag_attr => false, error_attr => message)end+ # Keep no part of a cut answer: readable error, flag cleared, earlier content+ # untouched. The log names the token counts, never the answer itself.+ def record_truncated(result)+ usage = result[:token_usage] || {}+ Rails.logger.warn "#{log_prefix}: answer cut at the output limit " \+ "(input #{usage[:input_tokens].inspect}, output #{usage[:output_tokens].inspect}, " \+ "total #{usage[:total_tokens].inspect})"+ record_error(t('truncated'))+ end+# Nothing to process: clear the pending flag AND any leftover error.def persist_no_sourcerecord.update!(flag_attr => false, error_attr => '')diff --git a/config/locales/services.de.yml b/config/locales/services.de.ymlindex 09f3d81..b1f88e3 100644--- a/config/locales/services.de.yml+++ b/config/locales/services.de.yml@@ -109,6 +109,7 @@ de:empty_result: "Die KI hat keine Zusammenfassung zurückgegeben. Bitte versuchen Sie es erneut."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."# 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:@@ -130,6 +131,7 @@ de:empty_result: "Die KI hat kein Ergebnis zurückgegeben. Bitte versuchen Sie es erneut."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 versuchen Sie es erneut."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."@@ -142,6 +144,7 @@ de:generic_error: "Die Ticket-Vorschläge konnten nicht erstellt werden. Bitte versuche es später erneut."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."post_update:unexpected_error: "Unerwarteter Fehler: %{error}"default_smart_folder:diff --git a/config/locales/services.en.yml b/config/locales/services.en.ymlindex 3042d9b..dd5226c 100644--- a/config/locales/services.en.yml+++ b/config/locales/services.en.yml@@ -109,6 +109,7 @@ en:empty_result: "The AI returned no summary. Please try again."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."# 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:@@ -130,6 +131,7 @@ en:empty_result: "The AI returned no result. Please try again."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 try again."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."@@ -142,6 +144,7 @@ en:generic_error: "The ticket suggestions could not be created. Please try creating suggestions again later."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."post_update:unexpected_error: "Unexpected error: %{error}"default_smart_folder:diff --git a/spec/services/meetings/analysis_service_spec.rb b/spec/services/meetings/analysis_service_spec.rbindex 6ece1e1..56321f1 100644--- a/spec/services/meetings/analysis_service_spec.rb+++ b/spec/services/meetings/analysis_service_spec.rb@@ -135,6 +135,55 @@ RSpec.describe Meetings::AnalysisService doendend+ # An answer that hit the output limit is cut mid-way. It used to be stored as a+ # finished analysis without a hint; now it is a readable error and nothing of+ # it is kept. The call still cost tokens, so it is metered all the same.+ describe 'answer cut at the output limit (stop_reason length)' do+ let(:transcript) { 'Some transcript' }+ let(:chat_result) do+ { content: 'Analyse, die mitten im', stop_reason: 'length', model_used: 'gpt-4o-mini',+ token_usage: { input_tokens: 900, output_tokens: 1500, total_tokens: 2400 } }+ end++ it 'records the truncated error, keeps no part of the answer and clears the flag' do+ described_class.call(analysis: analysis)+ analysis.reload+ expect(analysis.content).to eq('')+ expect(analysis.creation_flag).to be(false)+ expect(analysis.creation_error).to eq(I18n.t('services.meetings.analysis.truncated'))+ end++ it 'leaves an earlier content untouched' do+ analysis.update!(content: 'FRUEHERE_ANALYSE')+ described_class.call(analysis: analysis)+ expect(analysis.reload.content).to eq('FRUEHERE_ANALYSE')+ end++ it 'still meters the call' do+ described_class.call(analysis: analysis)+ expect(Lalabase::Llm::UsageTracking).to have_received(:record).with(hash_including(subject: analysis))+ end++ it 'logs the token counts but not the answer' do+ logged = []+ allow(Rails.logger).to receive(:warn) { |message| logged << message }+ described_class.call(analysis: analysis)+ expect(logged.join).to include('2400').and include('1500')+ expect(logged.join).not_to include('mitten im')+ 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' } }++ it 'is stored as before' do+ described_class.call(analysis: analysis)+ expect(analysis.reload.content).to eq('Fertige Analyse.')+ expect(analysis.creation_error).to eq('')+ end+ end+describe 'blank source' doit 'does not call the LLM and clears the pending flag' doexpect(Lalabase::Llm::Factory).not_to receive(:for_feature)diff --git a/spec/services/meetings/summary_service_spec.rb b/spec/services/meetings/summary_service_spec.rbindex 5e66877..0504d95 100644--- a/spec/services/meetings/summary_service_spec.rb+++ b/spec/services/meetings/summary_service_spec.rb@@ -169,6 +169,24 @@ RSpec.describe Meetings::SummaryService doendend+ describe 'answer cut at the output limit (stop_reason length)' do+ let(:transcript) { 'Some transcript' }+ let(:chat_result) do+ { content: 'Eine Kurzfassung, die', stop_reason: 'length', model_used: 'gemini-2.5-flash',+ token_usage: { input_tokens: 300, output_tokens: 19, total_tokens: 819 } }+ end++ it 'records the truncated error, keeps the previous summary and still meters' do+ meeting.update!(generated_summary: 'FRUEHERE_KURZFASSUNG')+ described_class.call(meeting: meeting)+ meeting.reload+ expect(meeting.generated_summary).to eq('FRUEHERE_KURZFASSUNG')+ expect(meeting.summary_creation_flag).to be(false)+ expect(meeting.summary_creation_error).to eq(I18n.t('services.meetings.summary.truncated'))+ expect(Lalabase::Llm::UsageTracking).to have_received(:record)+ end+ end+describe 'error mapping (flag always cleared, no raise)' dolet(:transcript) { 'Some transcript' }diff --git a/spec/services/meetings/ticket_suggestion_service_spec.rb b/spec/services/meetings/ticket_suggestion_service_spec.rbindex b5a8ecc..0cc34f9 100644--- a/spec/services/meetings/ticket_suggestion_service_spec.rb+++ b/spec/services/meetings/ticket_suggestion_service_spec.rb@@ -193,6 +193,24 @@ RSpec.describe Meetings::TicketSuggestionService doendend+ # Checked before the JSON is parsed: even an answer that happens to parse+ # (cut right after a closing bracket) is not the list the model meant to give.+ context 'when the answer was cut at the output limit' do+ let(:chat_result) { { content: llm_content, stop_reason: 'length', token_usage: { total_tokens: 100 }, model_used: 'gpt-4o-mini' } }++ it 'creates and deletes no suggestion, records the truncated error and still meters' do+ old_open = create(:meeting_ticket_suggestion, meeting: meeting)++ expect { described_class.call(meeting: meeting) }.not_to(change { meeting.meeting_ticket_suggestions.count })++ expect(MeetingTicketSuggestion.exists?(old_open.id)).to be(true)+ expect(meeting.reload.suggestions_creation_error)+ .to eq(I18n.t('services.meetings.ticket_suggestions.truncated'))+ expect(meeting.suggestions_creation_flag).to be(false)+ expect(Lalabase::Llm::UsageTracking).to have_received(:record)+ end+ end+context 'when the quota is exhausted (Factory raises)' dobefore { allow(Lalabase::Llm::Factory).to receive(:for_feature).with(:meeting_ticket_suggestions, organisation).and_raise(Lalabase::Llm::QuotaExceededError) }