Skip to content

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

FIX: Preserve AI snapshots during onebox updates (#44744)

Sam1fb56e603e7c
diff --git a/plugins/discourse-ai/lib/completions/history_snapshot.rb b/plugins/discourse-ai/lib/completions/history_snapshot.rb
index ba44f314916..16de72fb210 100644
--- a/plugins/discourse-ai/lib/completions/history_snapshot.rb
+++ b/plugins/discourse-ai/lib/completions/history_snapshot.rb
@@ -171,9 +171,6 @@ module DiscourseAi
"posts.user_id",
"posts.post_type",
Arel.sql("md5(posts.raw)"),
- Arel.sql(
- "(SELECT md5(string_agg(ref.upload_id::text, ',' ORDER BY ref.upload_id)) FROM upload_references ref WHERE ref.target_type = 'Post' AND ref.target_id = posts.id)",
- ),
Arel.sql("md5(post_custom_prompts.custom_prompt::text)"),
)
else
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 3f09ca12f09..be66ea35d71 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,31 @@ RSpec.describe DiscourseAi::Completions::HistorySnapshot do
)
end
+ it "preserves a snapshot when background onebox processing localizes its favicon" do
+ url = "https://app-eu1.hubspot.com/contacts/123/record/0-1/456"
+ raw = "What are the most recent emails sent to this person?\n#{url}"
+ source.update!(raw: raw)
+ snapshot = post_snapshot(source)
+
+ source.update_columns(cooked: <<~HTML)
+ <p>What are the most recent emails sent to this person?</p>
+ <aside class="onebox">
+ <header><img class="site-icon" src="https://example.com/favicon.png"></header>
+ <a href="#{url}">HubSpot</a>
+ </aside>
+ HTML
+ source.link_post_uploads
+ expect { snapshot.verify! }.not_to raise_error
+
+ upload = Fabricate(:upload)
+ source.update_columns(cooked: source.cooked.sub("https://example.com/favicon.png", upload.url))
+ source.link_post_uploads
+
+ expect(source.reload.raw).to eq(raw)
+ expect(UploadReference.where(target: source).pluck(:upload_id)).to eq([upload.id])
+ expect { snapshot.verify! }.not_to raise_error
+ end
+
it "captures PM history when category group moderation is enabled" do
SiteSetting.enable_category_group_moderation = true