Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 4980a4b

Browse files
authored
FIX: Multiple concurrent summaries could result in pg index errors (#973)
1 parent 54ebbbe commit 4980a4b

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

app/models/ai_summary.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@ class AiSummary < ActiveRecord::Base
99
def self.store!(strategy, llm_model, summary, og_content, human:)
1010
content_ids = og_content.map { |c| c[:id] }
1111

12-
AiSummary.create!(
13-
target: strategy.target,
14-
algorithm: llm_model.name,
15-
content_range: (content_ids.first..content_ids.last),
16-
summarized_text: summary,
17-
original_content_sha: build_sha(content_ids.join),
18-
summary_type: strategy.type,
19-
origin: !!human ? origins[:human] : origins[:system],
20-
)
12+
AiSummary
13+
.upsert(
14+
{
15+
target_id: strategy.target.id,
16+
target_type: strategy.target.class.name,
17+
algorithm: llm_model.name,
18+
content_range: (content_ids.first..content_ids.last),
19+
summarized_text: summary,
20+
original_content_sha: build_sha(content_ids.join),
21+
summary_type: strategy.type,
22+
origin: !!human ? origins[:human] : origins[:system],
23+
},
24+
unique_by: %i[target_id target_type summary_type],
25+
update_only: %i[summarized_text original_content_sha algorithm origin content_range],
26+
)
27+
.first
28+
.then { AiSummary.find_by(id: _1["id"]) }
2129
end
2230

2331
def self.build_sha(joined_ids)

lib/summarization/fold_content.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def delete_cached_summaries!
6969
end
7070

7171
def force_summarize(user, &on_partial_blk)
72-
delete_cached_summaries!
72+
@persist_summaries = true
7373
summarize(user, &on_partial_blk)
7474
end
7575

0 commit comments

Comments
 (0)