Skip to content

A platform for community discussion. Free, open, simple.

FIX: Handle AI completion edge cases (#44460)

Sam008b5fc4585c
diff --git a/plugins/discourse-ai/lib/completions/endpoints/base.rb b/plugins/discourse-ai/lib/completions/endpoints/base.rb
index 15cd3861b46..90a5a29bcc4 100644
--- a/plugins/discourse-ai/lib/completions/endpoints/base.rb
+++ b/plugins/discourse-ai/lib/completions/endpoints/base.rb
@@ -28,6 +28,7 @@ module DiscourseAi
Errno::ECONNREFUSED,
Errno::ECONNRESET,
Errno::ETIMEDOUT,
+ EOFError,
SocketError,
OpenSSL::SSL::SSLError,
]
diff --git a/plugins/discourse-ai/lib/completions/history_snapshot.rb b/plugins/discourse-ai/lib/completions/history_snapshot.rb
index 2756eb6d2c0..ba44f314916 100644
--- a/plugins/discourse-ai/lib/completions/history_snapshot.rb
+++ b/plugins/discourse-ai/lib/completions/history_snapshot.rb
@@ -259,6 +259,7 @@ module DiscourseAi
@source
.topic
.posts
+ .joins(:topic)
.where("post_number <= ?", source.post_number)
.where(post_type: types),
category: @source.topic.category,
diff --git a/plugins/discourse-ai/lib/completions/report.rb b/plugins/discourse-ai/lib/completions/report.rb
index 2dfb9003bff..4cc802a2057 100644
--- a/plugins/discourse-ai/lib/completions/report.rb
+++ b/plugins/discourse-ai/lib/completions/report.rb
@@ -246,7 +246,7 @@ module DiscourseAi
def token_total_columns
[
- "SUM(COALESCE(request_tokens, 0) + COALESCE(response_tokens, 0) + COALESCE(cache_read_tokens, 0) + COALESCE(cache_write_tokens, 0)) as total_tokens",
+ "SUM(COALESCE(request_tokens, 0)::bigint + COALESCE(response_tokens, 0) + COALESCE(cache_read_tokens, 0) + COALESCE(cache_write_tokens, 0))::bigint as total_tokens",
"SUM(COALESCE(cache_read_tokens, 0)) as total_cache_read_tokens",
"SUM(COALESCE(cache_write_tokens,0)) as total_cache_write_tokens",
"SUM(COALESCE(request_tokens,0)) as total_request_tokens",
diff --git a/plugins/discourse-ai/spec/lib/completions/history_snapshot_spec.rb b/plugins/discourse-ai/spec/lib/completions/history_snapshot_spec.rb
index 7d606cda2d1..3f09ca12f09 100644
--- a/plugins/discourse-ai/spec/lib/completions/history_snapshot_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/history_snapshot_spec.rb
@@ -44,6 +44,36 @@ RSpec.describe DiscourseAi::Completions::HistorySnapshot do
)
end
+ it "captures PM history when category group moderation is enabled" do
+ SiteSetting.enable_category_group_moderation = true
+
+ snapshot = post_snapshot(source)
+
+ expect(snapshot.metadata["source_id"]).to eq(source.id)
+ end
+
+ it "excludes other users' hidden PM posts from a category moderator's history" do
+ SiteSetting.enable_category_group_moderation = true
+ group = Fabricate(:group)
+ group.add(user)
+ Fabricate(:category_moderation_group, group: group)
+ source.update!(hidden: true)
+ hidden_post = Fabricate(:post, topic: source.topic, user: other_user, hidden: true)
+ latest = Fabricate(:post, topic: source.topic, user: user)
+
+ snapshot = post_snapshot(latest)
+
+ expect(snapshot.user_turn_count).to eq(2)
+ hidden_post.update!(raw: "Changed hidden evidence")
+ expect { snapshot.verify! }.not_to raise_error
+
+ source.update!(raw: "Changed reader-owned hidden evidence")
+ expect { snapshot.verify! }.to raise_error(
+ DiscourseAi::Completions::ContextPreparation::Error,
+ /history_changed/,
+ )
+ end
+
it "retains the first eligible bot answer after the initial PM user post is deleted" do
reply = Fabricate(:post, topic: source.topic, user: bot_user, raw: "Retained bot evidence")
latest = Fabricate(:post, topic: source.topic, user: user, raw: "Continue the conversation")
diff --git a/plugins/discourse-ai/spec/lib/completions/llm_spec.rb b/plugins/discourse-ai/spec/lib/completions/llm_spec.rb
index 4526f359289..ac8acc2d92f 100644
--- a/plugins/discourse-ai/spec/lib/completions/llm_spec.rb
+++ b/plugins/discourse-ai/spec/lib/completions/llm_spec.rb
@@ -883,6 +883,47 @@ RSpec.describe DiscourseAi::Completions::Llm do
expect(result.to_s).to eq('{"message":"ok"}')
end
+ it "retries an unexpected streaming EOF before any output is generated" do
+ response = instance_double(Net::HTTPResponse, code: "200")
+ reads = 0
+ allow(response).to receive(:read_body) do |&block|
+ reads += 1
+ raise EOFError, "end of file reached" if reads == 1
+ block.call(streaming_body(content: "Recovered answer"))
+ end
+ http = instance_double(Net::HTTP)
+ allow(http).to receive(:request).and_yield(response)
+ allow(FinalDestination::HTTP).to receive(:start).and_yield(http)
+ visible = []
+
+ llm.generate("Hello", user: user) { |partial| visible << partial }
+
+ expect(visible.join).to eq("Recovered answer")
+ expect(http).to have_received(:request).twice
+ end
+
+ it "reports an unexpected streaming EOF after partial output without retrying" do
+ partial = "Partial answer"
+ response = instance_double(Net::HTTPResponse, code: "200")
+ allow(response).to receive(:read_body) do |&block|
+ block.call(streaming_body(content: partial))
+ raise EOFError, "end of file reached"
+ end
+ http = instance_double(Net::HTTP)
+ allow(http).to receive(:request).and_yield(response)
+ allow(FinalDestination::HTTP).to receive(:start).and_yield(http)
+ visible = []
+
+ expect {
+ llm.generate("Hello", user: user) { |fragment| visible << fragment }
+ }.to raise_error(
+ DiscourseAi::Completions::Endpoints::Base::CompletionFailed,
+ "end of file reached",
+ )
+ expect(visible.join).to eq(partial)
+ expect(http).to have_received(:request).once
+ end
+
it "retries network errors twice" do
request =
WebMock
diff --git a/plugins/discourse-ai/spec/requests/admin/ai_usage_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_usage_controller_spec.rb
index 1d60f302359..887420a04b5 100644
--- a/plugins/discourse-ai/spec/requests/admin/ai_usage_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/admin/ai_usage_controller_spec.rb
@@ -59,6 +59,28 @@ RSpec.describe DiscourseAi::Admin::AiUsageController do
expect(json).to have_key("summary")
end
+ it "reports rolled-up token totals exceeding the 32-bit integer range" do
+ AiApiRequestStat.delete_all
+ AiApiRequestStat.create!(
+ provider_id: 1,
+ feature_name: "ai_bot",
+ language_model: llm_model.name,
+ llm_id: llm_model.id,
+ user_id: user.id,
+ request_tokens: 1_500_000_000,
+ response_tokens: 100,
+ cache_read_tokens: 800_000_000,
+ cache_write_tokens: 50,
+ rolled_up: true,
+ created_at: 1.day.ago,
+ )
+
+ get usage_report_path
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["summary"]["total_tokens"]).to eq(2_300_000_150)
+ end
+
it "respects date filters" do
get usage_report_path,
params: {