Skip to content

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

SECURITY: Force regeneration for edit-outdated summaries and block stale fallback

Roman Rizzi5a00b47523ec
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/summarization/summary_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/summarization/summary_controller.rb
index 79c84534e1e..89bb1458c0c 100644
--- a/plugins/discourse-ai/app/controllers/discourse_ai/summarization/summary_controller.rb
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/summarization/summary_controller.rb
@@ -10,19 +10,19 @@ module DiscourseAi
def show
topic = Topic.find(params[:topic_id])
guardian.ensure_can_see!(topic)
+ summarization_service = DiscourseAi::TopicSummarization.for(topic, current_user)
+ cached_summary = summarization_service.cached_summary
- raise Discourse::NotFound if !guardian.can_see_summary?(topic)
+ if !guardian.can_see_summary?(topic, cached_summary: cached_summary)
+ raise Discourse::NotFound
+ end
RateLimiter.new(current_user, "summary", 6, 5.minutes).performed! if current_user
opts = params.permit(:skip_age_check)
skip_age_check = opts[:skip_age_check] == "true"
- summarization_service = DiscourseAi::TopicSummarization.for(topic, current_user)
-
if params[:stream] && current_user
- cached_summary = summarization_service.cached_summary
-
if cached_summary && !skip_age_check
render_serialized(cached_summary, AiTopicSummarySerializer)
return
@@ -39,6 +39,8 @@ module DiscourseAi
else
hijack do
summary = summarization_service.summarize(skip_age_check: skip_age_check)
+ raise Discourse::NotFound if summary.nil?
+
render_serialized(summary, AiTopicSummarySerializer)
end
end
diff --git a/plugins/discourse-ai/app/jobs/regular/stream_topic_ai_summary.rb b/plugins/discourse-ai/app/jobs/regular/stream_topic_ai_summary.rb
index d719294ce72..a92edcecdd1 100644
--- a/plugins/discourse-ai/app/jobs/regular/stream_topic_ai_summary.rb
+++ b/plugins/discourse-ai/app/jobs/regular/stream_topic_ai_summary.rb
@@ -9,9 +9,14 @@ module Jobs
return unless user = User.find_by(id: args[:user_id])
strategy = DiscourseAi::Summarization.topic_summary(topic)
- return if strategy.nil? || !Guardian.new(user).can_see_summary?(topic)
+ return if strategy.nil?
+
+ summarization_service = DiscourseAi::TopicSummarization.new(strategy, user)
+ cached_summary = summarization_service.cached_summary
guardian = Guardian.new(user)
+ return if !guardian.can_see_summary?(topic, cached_summary: cached_summary)
+
return unless guardian.can_see?(topic)
skip_age_check = !!args[:skip_age_check]
@@ -21,19 +26,17 @@ module Jobs
begin
summary =
- DiscourseAi::TopicSummarization
- .new(strategy, user)
- .summarize(skip_age_check: skip_age_check) do |partial_summary|
- streamed_summary << partial_summary
-
- # Throttle updates.
- if (Time.now - start > 0.3) || Rails.env.test?
- payload = { done: false, ai_topic_summary: { summarized_text: streamed_summary } }
-
- publish_update(topic, user, payload)
- start = Time.now
- end
+ summarization_service.summarize(skip_age_check: skip_age_check) do |partial_summary|
+ streamed_summary << partial_summary
+
+ # Throttle updates.
+ if (Time.now - start > 0.3) || Rails.env.test?
+ payload = { done: false, ai_topic_summary: { summarized_text: streamed_summary } }
+
+ publish_update(topic, user, payload)
+ start = Time.now
end
+ end
publish_update(
topic,
diff --git a/plugins/discourse-ai/app/services/discourse_ai/topic_summarization.rb b/plugins/discourse-ai/app/services/discourse_ai/topic_summarization.rb
index 3d33e21d2e9..7d23019611e 100644
--- a/plugins/discourse-ai/app/services/discourse_ai/topic_summarization.rb
+++ b/plugins/discourse-ai/app/services/discourse_ai/topic_summarization.rb
@@ -13,6 +13,8 @@ module DiscourseAi
end
def cached_summary
+ return if summarizer.nil?
+
summarizer.existing_summary
end
@@ -20,6 +22,9 @@ module DiscourseAi
# Existing summary shouldn't be nil in this scenario because the controller checks its existence.
return if !user && !cached_summary
+ can_summarize = Guardian.new(user).can_request_summary?
+ return if !can_summarize && cached_summary&.outdated
+
return cached_summary if use_cached?(skip_age_check)
summarizer.delete_cached_summaries! if cached_summary
@@ -32,13 +37,30 @@ module DiscourseAi
attr_reader :summarizer, :user
def use_cached?(skip_age_check)
+ return false if !cached_summary
+
can_summarize = Guardian.new(user).can_request_summary?
+ return true if !can_summarize
+
+ return true if !cached_summary.outdated
+
+ # If staleness is due to edited content, regenerate immediately.
+ return false if outdated_due_to_post_edit?
+
+ !skip_age_check && cached_summary.created_at >= 1.hour.ago
+ end
+
+ def outdated_due_to_post_edit?
+ return false if !cached_summary.outdated
+
+ fingerprint = summarizer.strategy.summary_fingerprint
+ return false if fingerprint.blank?
+
+ highest_target_unchanged =
+ cached_summary.highest_target_number == summarizer.strategy.highest_target_number
+ edited_since_summary = fingerprint[:latest_version_at]&.> cached_summary.updated_at
- cached_summary &&
- !(
- can_summarize && cached_summary.outdated &&
- (skip_age_check || cached_summary.created_at < 1.hour.ago)
- )
+ highest_target_unchanged && edited_since_summary
end
end
end
diff --git a/plugins/discourse-ai/lib/guardian_extensions.rb b/plugins/discourse-ai/lib/guardian_extensions.rb
index 8dbdca15ff7..e2aaf0303ed 100644
--- a/plugins/discourse-ai/lib/guardian_extensions.rb
+++ b/plugins/discourse-ai/lib/guardian_extensions.rb
@@ -2,7 +2,7 @@
module DiscourseAi
module GuardianExtensions
- def can_see_summary?(target)
+ def can_see_summary?(target, cached_summary: nil)
return false if !SiteSetting.ai_summarization_enabled
if target.class == Topic && target.private_message?
@@ -14,11 +14,11 @@ module DiscourseAi
return false if !allowed
end
- has_cached_summary =
- AiSummary.exists?(target: target, summary_type: AiSummary.summary_types[:complete])
- return has_cached_summary if user.nil?
+ can_summarize = can_request_summary?
+ return true if can_summarize
+ return false if cached_summary.nil?
- has_cached_summary || can_request_summary?
+ cached_summary.present? && !cached_summary.outdated
end
def can_see_gists?
diff --git a/plugins/discourse-ai/lib/summarization/entry_point.rb b/plugins/discourse-ai/lib/summarization/entry_point.rb
index 96e70b415a9..3cca57538a6 100644
--- a/plugins/discourse-ai/lib/summarization/entry_point.rb
+++ b/plugins/discourse-ai/lib/summarization/entry_point.rb
@@ -16,15 +16,15 @@ module DiscourseAi
end
plugin.add_to_serializer(:topic_view, :summarizable) do
- scope.can_see_summary?(object.topic)
+ scope.can_see_summary?(object.topic, cached_summary: ai_summary_record)
end
- plugin.add_to_serializer(:topic_view, :has_cached_summary) do
- AiSummary.exists?(target: object.topic, summary_type: AiSummary.summary_types[:complete])
- end
+ plugin.add_to_serializer(:topic_view, :has_cached_summary) { ai_summary_record.present? }
plugin.add_to_serializer(:web_hook_topic_view, :summarizable) do
- scope.can_see_summary?(object.topic)
+ cached_summary =
+ DiscourseAi::TopicSummarization.for(object.topic, scope.user).cached_summary
+ scope.can_see_summary?(object.topic, cached_summary: cached_summary)
end
plugin.add_to_serializer(
@@ -34,15 +34,18 @@ module DiscourseAi
) do
return @ai_summary_record if defined?(@ai_summary_record)
@ai_summary_record =
- object.topic.ai_summaries.find_by(summary_type: AiSummary.summary_types[:complete])
+ DiscourseAi::TopicSummarization.for(object.topic, scope.user).cached_summary
end
plugin.add_to_serializer(
:topic_view,
:ai_summary,
include_condition: -> do
- DiscoursePluginRegistry.apply_modifier(:serialize_ai_summary, false) &&
- scope.can_see_summary?(object.topic) && ai_summary_record.present?
+ next false if !DiscoursePluginRegistry.apply_modifier(:serialize_ai_summary, false)
+
+ summary_record = ai_summary_record
+ scope.can_see_summary?(object.topic, cached_summary: summary_record) &&
+ summary_record.present?
end,
) do
{
diff --git a/plugins/discourse-ai/lib/summarization/fold_content.rb b/plugins/discourse-ai/lib/summarization/fold_content.rb
index a246e37f1c7..c64d244c438 100644
--- a/plugins/discourse-ai/lib/summarization/fold_content.rb
+++ b/plugins/discourse-ai/lib/summarization/fold_content.rb
@@ -46,10 +46,7 @@ module DiscourseAi
if summary
@existing_summary = summary
- if summary.original_content_sha != latest_sha ||
- content_to_summarize.any? { |cts| cts[:last_version_at] > summary.updated_at }
- summary.mark_as_outdated
- end
+ summary.mark_as_outdated if outdated_summary?(summary)
end
end
@existing_summary
@@ -112,6 +109,18 @@ module DiscourseAi
@latest_sha ||= AiSummary.build_sha(content_to_summarize.map { |c| c[:id] }.join)
end
+ def outdated_summary?(summary)
+ if (fingerprint = strategy.summary_fingerprint)
+ return true if summary.original_content_sha != fingerprint[:original_content_sha]
+ return true if fingerprint[:latest_version_at]&.> summary.updated_at
+
+ return false
+ end
+
+ summary.original_content_sha != latest_sha ||
+ content_to_summarize.any? { |cts| cts[:last_version_at] > summary.updated_at }
+ end
+
# @param items { Array<Hash> } - Content to summarize. Structure will be: { poster: who wrote the content, id: a way to order content, text: content }
# @param user { User } - User object used for auditing usage.
# @param &on_partial_blk { Block - Optional } - The passed block will get called with the LLM partial response.
diff --git a/plugins/discourse-ai/lib/summarization/strategies/base.rb b/plugins/discourse-ai/lib/summarization/strategies/base.rb
index c56f93f3e88..7697702ef22 100644
--- a/plugins/discourse-ai/lib/summarization/strategies/base.rb
+++ b/plugins/discourse-ai/lib/summarization/strategies/base.rb
@@ -38,6 +38,15 @@ module DiscourseAi
raise NotImplementedError
end
+ # Optional lightweight fingerprint for stale cache checks.
+ # Return a hash with:
+ # - :original_content_sha (String)
+ # - :latest_version_at (Time | nil)
+ # or nil to fallback to full-content stale detection.
+ def summary_fingerprint
+ nil
+ end
+
# We'll pass this as the feature_name when doing LLM calls.
def feature
"summarize"
diff --git a/plugins/discourse-ai/lib/summarization/strategies/topic_summary.rb b/plugins/discourse-ai/lib/summarization/strategies/topic_summary.rb
index 7f6a508208d..fa8c9c10721 100644
--- a/plugins/discourse-ai/lib/summarization/strategies/topic_summary.rb
+++ b/plugins/discourse-ai/lib/summarization/strategies/topic_summary.rb
@@ -18,7 +18,7 @@ module DiscourseAi
post_attributes.push(:name)
end
- posts_data = (target.has_summary? ? best_replies : pick_selection).pluck(post_attributes)
+ posts_data = selected_posts.pluck(post_attributes)
posts_data.reduce([]) do |memo, (pn, raw, username, last_version_at, name)|
raw_text = raw
@@ -38,6 +38,15 @@ module DiscourseAi
end
end
+ def summary_fingerprint
+ posts_data = selected_posts.pluck(:post_number, :last_version_at)
+ {
+ original_content_sha:
+ AiSummary.build_sha(posts_data.map { |post_number, _| post_number }.join),
+ latest_version_at: posts_data.map { |_, last_version_at| last_version_at }.compact.max,
+ }
+ end
+
def as_llm_messages(contents)
resource_path = "#{Discourse.base_path}/t/-/#{target.id}"
content_title = target.title
@@ -66,6 +75,10 @@ module DiscourseAi
attr_reader :topic
+ def selected_posts
+ target.has_summary? ? best_replies : pick_selection
+ end
+
def best_replies
Post
.summary(target.id)
diff --git a/plugins/discourse-ai/spec/lib/guardian_extensions_spec.rb b/plugins/discourse-ai/spec/lib/guardian_extensions_spec.rb
index a1eb55d3fe3..ac274eae7eb 100644
--- a/plugins/discourse-ai/spec/lib/guardian_extensions_spec.rb
+++ b/plugins/discourse-ai/spec/lib/guardian_extensions_spec.rb
@@ -16,18 +16,30 @@ describe DiscourseAi::GuardianExtensions do
let(:anon_guardian) { Guardian.new }
let(:guardian) { Guardian.new(user) }
+ def create_cached_summary(topic)
+ strategy = DiscourseAi::Summarization::Strategies::TopicSummary.new(topic)
+ content_sha = AiSummary.build_sha(strategy.targets_data.map { |target| target[:id] }.join)
+
+ Fabricate(
+ :ai_summary,
+ target: topic,
+ original_content_sha: content_sha,
+ highest_target_number: topic.highest_post_number,
+ )
+ end
+
describe "#can_see_summary?" do
context "when the user cannot generate a summary" do
before { assign_agent_to(:ai_summarization_agent, []) }
it "returns false" do
- expect(guardian.can_see_summary?(topic)).to eq(false)
+ expect(guardian.can_see_summary?(topic, cached_summary: nil)).to eq(false)
end
it "returns true if there is a cached summary" do
- Fabricate(:ai_summary, target: topic)
+ cached_summary = create_cached_summary(topic)
- expect(guardian.can_see_summary?(topic)).to eq(true)
+ expect(guardian.can_see_summary?(topic, cached_summary: cached_summary)).to eq(true)
end
end
@@ -35,7 +47,7 @@ describe DiscourseAi::GuardianExtensions do
before { assign_agent_to(:ai_summarization_agent, [group.id]) }
it "returns true if the user group is present in the ai_custom_summarization_allowed_groups_map setting" do
- expect(guardian.can_see_summary?(topic)).to eq(true)
+ expect(guardian.can_see_summary?(topic, cached_summary: nil)).to eq(true)
end
end
@@ -44,24 +56,39 @@ describe DiscourseAi::GuardianExtensions do
let(:pm) { Fabricate(:private_message_topic) }
it "returns false" do
- expect(guardian.can_see_summary?(pm)).to eq(false)
+ expect(guardian.can_see_summary?(pm, cached_summary: nil)).to eq(false)
end
it "returns true if user is in a group that is allowed summaries" do
SiteSetting.ai_pm_summarization_allowed_groups = group.id
- expect(guardian.can_see_summary?(pm)).to eq(true)
+ expect(guardian.can_see_summary?(pm, cached_summary: nil)).to eq(true)
end
end
context "when there is no user" do
it "returns false for anons" do
- expect(anon_guardian.can_see_summary?(topic)).to eq(false)
+ expect(anon_guardian.can_see_summary?(topic, cached_summary: nil)).to eq(false)
+ end
+
+ it "returns true for anons when there is a fresh cached summary" do
+ cached_summary = create_cached_summary(topic)
+
+ expect(anon_guardian.can_see_summary?(topic, cached_summary: cached_summary)).to eq(true)
+ end
+ end
+
+ context "when summary is provided" do
+ it "returns false for non-regenerators when the provided summary is outdated" do
+ summary = Fabricate(:ai_summary, target: topic)
+ summary.mark_as_outdated
+
+ expect(anon_guardian.can_see_summary?(topic, cached_summary: summary)).to eq(false)
end
- it "returns true for anons when there is a cached summary" do
- Fabricate(:ai_summary, target: topic)
+ it "returns true for non-regenerators when the provided summary is fresh" do
+ summary = create_cached_summary(topic)
- expect(guardian.can_see_summary?(topic)).to eq(true)
+ expect(anon_guardian.can_see_summary?(topic, cached_summary: summary)).to eq(true)
end
end
end
diff --git a/plugins/discourse-ai/spec/lib/modules/summarization/strategies/topic_summary_spec.rb b/plugins/discourse-ai/spec/lib/modules/summarization/strategies/topic_summary_spec.rb
index 994592e57e8..10115c5d78a 100644
--- a/plugins/discourse-ai/spec/lib/modules/summarization/strategies/topic_summary_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/summarization/strategies/topic_summary_spec.rb
@@ -97,6 +97,25 @@ RSpec.describe DiscourseAi::Summarization::Strategies::TopicSummary do
end
end
+ describe "#summary_fingerprint" do
+ it "uses selected post numbers to compute the same sha as targets_data ids" do
+ fingerprint = topic_summary.summary_fingerprint
+ expected_sha =
+ AiSummary.build_sha(topic_summary.targets_data.map { |content| content[:id] }.join)
+
+ expect(fingerprint[:original_content_sha]).to eq(expected_sha)
+ end
+
+ it "tracks the latest post version timestamp in the selected set" do
+ post_1.update!(last_version_at: 10.minutes.ago)
+ post_2.update!(last_version_at: 2.minutes.ago)
+
+ fingerprint = topic_summary.summary_fingerprint
+
+ expect(fingerprint[:latest_version_at].to_i).to eq(post_2.last_version_at.to_i)
+ end
+ end
+
describe "#as_llm_messages" do
let(:contents) do
[{ id: 1, poster: "user1", text: "First post content", last_version_at: Time.now }]
diff --git a/plugins/discourse-ai/spec/requests/summarization/summary_controller_spec.rb b/plugins/discourse-ai/spec/requests/summarization/summary_controller_spec.rb
index d9955692200..e32bfdfb36a 100644
--- a/plugins/discourse-ai/spec/requests/summarization/summary_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/summarization/summary_controller_spec.rb
@@ -6,6 +6,18 @@ RSpec.describe DiscourseAi::Summarization::SummaryController do
fab!(:post_1) { Fabricate(:post, topic: topic, post_number: 1) }
fab!(:post_2) { Fabricate(:post, topic: topic, post_number: 2) }
+ def create_cached_summary(topic)
+ strategy = DiscourseAi::Summarization::Strategies::TopicSummary.new(topic)
+ content_sha = AiSummary.build_sha(strategy.targets_data.map { |target| target[:id] }.join)
+
+ Fabricate(
+ :ai_summary,
+ target: topic,
+ original_content_sha: content_sha,
+ highest_target_number: topic.highest_post_number,
+ )
+ end
+
before do
enable_current_plugin
assign_fake_provider_to(:ai_default_llm_model)
@@ -14,7 +26,7 @@ RSpec.describe DiscourseAi::Summarization::SummaryController do
context "when streaming" do
it "return a cached summary with json payload and does not trigger job if it exists" do
- summary = Fabricate(:ai_summary, target: topic)
+ summary = create_cached_summary(topic)
sign_in(Fabricate(:admin))
get "/discourse-ai/summarization/t/#{topic.id}.json?stream=true"
@@ -36,8 +48,8 @@ RSpec.describe DiscourseAi::Summarization::SummaryController do
expect(response.status).to eq(404)
end
- it "returns a cached summary" do
- summary = Fabricate(:ai_summary, target: topic)
+ it "returns a fresh cached summary" do
+ summary = create_cached_summary(topic)
get "/discourse-ai/summarization/t/#{topic.id}.json"
expect(response.status).to eq(200)
@@ -47,6 +59,16 @@ RSpec.describe DiscourseAi::Summarization::SummaryController do
summary.summarized_text,
)
end
+
+ it "returns a 404 for outdated cached summaries" do
+ create_cached_summary(topic)
+ Fabricate(:post, topic: topic, post_number: 3)
+ topic.update!(highest_post_number: 3)
+
+ get "/discourse-ai/summarization/t/#{topic.id}.json"
+
+ expect(response.status).to eq(404)
+ end
end
context "when the user is a member of an allowlisted group" do
@@ -118,8 +140,8 @@ RSpec.describe DiscourseAi::Summarization::SummaryController do
expect(response.status).to eq(404)
end
- it "returns a cached summary" do
- summary = Fabricate(:ai_summary, target: topic)
+ it "returns a fresh cached summary" do
+ summary = create_cached_summary(topic)
get "/discourse-ai/summarization/t/#{topic.id}.json"
@@ -130,6 +152,16 @@ RSpec.describe DiscourseAi::Summarization::SummaryController do
summary.summarized_text,
)
end
+
+ it "returns a 404 for outdated cached summaries" do
+ create_cached_summary(topic)
+ Fabricate(:post, topic: topic, post_number: 3)
+ topic.update!(highest_post_number: 3)
+
+ get "/discourse-ai/summarization/t/#{topic.id}.json"
+
+ expect(response.status).to eq(404)
+ end
end
end
diff --git a/plugins/discourse-ai/spec/serializers/topic_view_serializer_spec.rb b/plugins/discourse-ai/spec/serializers/topic_view_serializer_spec.rb
index 7485fd208ad..c960f3114b8 100644
--- a/plugins/discourse-ai/spec/serializers/topic_view_serializer_spec.rb
+++ b/plugins/discourse-ai/spec/serializers/topic_view_serializer_spec.rb
@@ -15,15 +15,46 @@ RSpec.describe TopicViewSerializer do
SiteSetting.ai_summarization_enabled = true
end
+ def create_cached_summary(topic)
+ strategy = DiscourseAi::Summarization::Strategies::TopicSummary.new(topic)
+ content_sha = AiSummary.build_sha(strategy.targets_data.map { |target| target[:id] }.join)
+
+ Fabricate(
+ :ai_summary,
+ target: topic,
+ original_content_sha: content_sha,
+ highest_target_number: topic.highest_post_number,
+ )
+ end
+
+ describe "#summarizable" do
+ let(:anon_topic_view) { TopicView.new(topic, nil) }
+ let(:anon_serializer) { described_class.new(anon_topic_view, scope: Guardian.new, root: false) }
+
+ it "is true for anons when a fresh cached summary exists" do
+ create_cached_summary(topic)
+
+ json = anon_serializer.as_json
+
+ expect(json[:summarizable]).to eq(true)
+ end
+
+ it "is false for anons when only an outdated cached summary exists" do
+ create_cached_summary(topic)
+ Fabricate(:post, topic: topic, post_number: 2)
+ topic.update!(highest_post_number: 2)
+
+ json = anon_serializer.as_json
+
+ expect(json[:summarizable]).to eq(false)
+ end
+ end
+
describe "#ai_summary" do
let!(:summary) do
- AiSummary.store!(
- DiscourseAi::Summarization::Strategies::TopicSummary.new(topic),
- build(:llm_model),
- "Test summary content",
- [{ id: post_1.id }],
- human: false,
- )
+ create_cached_summary(topic).tap do |record|
+ record.update!(summarized_text: "Test summary content")
+ end
end
context "when serialize_ai_summary modifier is not enabled" do
@@ -66,6 +97,23 @@ RSpec.describe TopicViewSerializer do
expect(json[:ai_summary][:updated_at]).to be_present
end
+ it "includes ai_summary as outdated for users who can request summaries" do
+ leader = Fabricate(:leader)
+ Group.find(Group::AUTO_GROUPS[:trust_level_3]).add(leader)
+
+ Fabricate(:post, topic: topic, post_number: 2)
+ topic.update!(highest_post_number: 2)
+
+ leader_topic_view = TopicView.new(topic, leader)
+ leader_serializer =
+ described_class.new(leader_topic_view, scope: Guardian.new(leader), root: false)
+
+ json = leader_serializer.as_json
+
+ expect(json[:ai_summary]).to be_present
+ expect(json[:ai_summary][:outdated]).to eq(true)
+ end
+
it "does not include ai_summary when summarization is disabled" do
SiteSetting.ai_summarization_enabled = false
diff --git a/plugins/discourse-ai/spec/services/discourse_ai/topic_summarization_spec.rb b/plugins/discourse-ai/spec/services/discourse_ai/topic_summarization_spec.rb
index f1434478c12..b3581015d1f 100644
--- a/plugins/discourse-ai/spec/services/discourse_ai/topic_summarization_spec.rb
+++ b/plugins/discourse-ai/spec/services/discourse_ai/topic_summarization_spec.rb
@@ -12,6 +12,18 @@ describe DiscourseAi::TopicSummarization do
SiteSetting.ai_summarization_enabled = true
end
+ def create_cached_summary(topic)
+ strategy = DiscourseAi::Summarization::Strategies::TopicSummary.new(topic)
+ content_sha = AiSummary.build_sha(strategy.targets_data.map { |target| target[:id] }.join)
+
+ Fabricate(
+ :ai_summary,
+ target: topic,
+ original_content_sha: content_sha,
+ highest_target_number: topic.highest_post_number,
+ )
+ end
+
let(:strategy) { DiscourseAi::Summarization.topic_summary(topic) }
let(:summary) { "This is the final summary" }
@@ -44,7 +56,6 @@ describe DiscourseAi::TopicSummarization do
cached_summary_text = "This is a cached summary"
AiSummary.find_by(target: topic, summary_type: AiSummary.summary_types[:complete]).update!(
summarized_text: cached_summary_text,
- updated_at: 24.hours.ago,
)
summarization = described_class.new(strategy, user)
@@ -114,6 +125,43 @@ describe DiscourseAi::TopicSummarization do
end
end
end
+
+ context "when posts were edited" do
+ before do
+ cached_summary.update!(created_at: 30.minutes.ago)
+ post_2.update_columns(last_version_at: 1.minute.from_now)
+ end
+
+ it "regenerates even when the cached summary is less than one hour old" do
+ DiscourseAi::Completions::Llm.with_prepared_responses([summary]) do
+ section = summarization.summarize
+
+ expect(section.summarized_text).to eq(summary)
+ end
+ end
+ end
+ end
+ end
+
+ context "when the user is anonymous" do
+ let(:summarization) { described_class.new(strategy, nil) }
+
+ it "returns a fresh cached summary" do
+ cached_summary = create_cached_summary(topic)
+
+ section = summarization.summarize
+
+ expect(section.id).to eq(cached_summary.id)
+ end
+
+ it "returns nil when the cached summary is outdated" do
+ create_cached_summary(topic)
+ Fabricate(:post, topic: topic, post_number: 3)
+ topic.update!(highest_post_number: 3)
+
+ section = summarization.summarize
+
+ expect(section).to be_nil
end
end