From f566552ac2f974431228f7cd24d36d1c29806ffa Mon Sep 17 00:00:00 2001 From: Rafael Silva Date: Thu, 28 Nov 2024 10:41:08 -0300 Subject: [PATCH 1/4] FIX: Multiple concurrent summaries could result in pg index errors --- app/models/ai_summary.rb | 11 ++++++++++- lib/summarization/fold_content.rb | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/models/ai_summary.rb b/app/models/ai_summary.rb index 6a880a5f2..cc5320e0e 100644 --- a/app/models/ai_summary.rb +++ b/app/models/ai_summary.rb @@ -9,7 +9,7 @@ class AiSummary < ActiveRecord::Base def self.store!(strategy, llm_model, summary, og_content, human:) content_ids = og_content.map { |c| c[:id] } - AiSummary.create!( + AiSummary.upsert( target: strategy.target, algorithm: llm_model.name, content_range: (content_ids.first..content_ids.last), @@ -17,6 +17,15 @@ def self.store!(strategy, llm_model, summary, og_content, human:) original_content_sha: build_sha(content_ids.join), summary_type: strategy.type, origin: !!human ? origins[:human] : origins[:system], + unique_by: %i[target_id target_type summary_type], + update_only: %i[ + summarized_text + original_content_sha + algorithm + origin + content_range + updated_at + ], ) end diff --git a/lib/summarization/fold_content.rb b/lib/summarization/fold_content.rb index 6f3344475..32bf97791 100644 --- a/lib/summarization/fold_content.rb +++ b/lib/summarization/fold_content.rb @@ -69,7 +69,7 @@ def delete_cached_summaries! end def force_summarize(user, &on_partial_blk) - delete_cached_summaries! + @persist_summaries = true summarize(user, &on_partial_blk) end From 6101c2ef6e0104bf22f1f3dc004c184836638555 Mon Sep 17 00:00:00 2001 From: Rafael Silva Date: Thu, 28 Nov 2024 11:06:05 -0300 Subject: [PATCH 2/4] ostruct --- app/models/ai_summary.rb | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/app/models/ai_summary.rb b/app/models/ai_summary.rb index cc5320e0e..bcfe3a3c2 100644 --- a/app/models/ai_summary.rb +++ b/app/models/ai_summary.rb @@ -9,24 +9,24 @@ class AiSummary < ActiveRecord::Base def self.store!(strategy, llm_model, summary, og_content, human:) content_ids = og_content.map { |c| c[:id] } - AiSummary.upsert( - target: strategy.target, - algorithm: llm_model.name, - content_range: (content_ids.first..content_ids.last), - summarized_text: summary, - original_content_sha: build_sha(content_ids.join), - summary_type: strategy.type, - origin: !!human ? origins[:human] : origins[:system], - unique_by: %i[target_id target_type summary_type], - update_only: %i[ - summarized_text - original_content_sha - algorithm - origin - content_range - updated_at - ], - ) + result = + AiSummary.upsert( + { + target_id: strategy.target.id, + target_type: strategy.target.class.name, + algorithm: llm_model.name, + content_range: (content_ids.first..content_ids.last), + summarized_text: summary, + original_content_sha: build_sha(content_ids.join), + summary_type: strategy.type, + origin: !!human ? origins[:human] : origins[:system], + }, + unique_by: %i[target_id target_type summary_type], + update_only: %i[summarized_text original_content_sha algorithm origin content_range], + returning: AiSummary.column_names, + ) + + OpenStruct.new(result.first) end def self.build_sha(joined_ids) From 747546eaa55d0485293d2f77e4fa4d3e887c6ecf Mon Sep 17 00:00:00 2001 From: Rafael Silva Date: Thu, 28 Nov 2024 11:09:53 -0300 Subject: [PATCH 3/4] rewrite --- app/models/ai_summary.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/models/ai_summary.rb b/app/models/ai_summary.rb index bcfe3a3c2..b63cbb769 100644 --- a/app/models/ai_summary.rb +++ b/app/models/ai_summary.rb @@ -9,8 +9,8 @@ class AiSummary < ActiveRecord::Base def self.store!(strategy, llm_model, summary, og_content, human:) content_ids = og_content.map { |c| c[:id] } - result = - AiSummary.upsert( + AiSummary + .upsert( { target_id: strategy.target.id, target_type: strategy.target.class.name, @@ -25,8 +25,8 @@ def self.store!(strategy, llm_model, summary, og_content, human:) update_only: %i[summarized_text original_content_sha algorithm origin content_range], returning: AiSummary.column_names, ) - - OpenStruct.new(result.first) + .first + .then { OpenStruct.new(_1) } end def self.build_sha(joined_ids) From 808c44aa25e4ff4c2c5d3f5e8767ae6716a731fb Mon Sep 17 00:00:00 2001 From: Rafael Silva Date: Thu, 28 Nov 2024 11:15:38 -0300 Subject: [PATCH 4/4] need full object --- app/models/ai_summary.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/models/ai_summary.rb b/app/models/ai_summary.rb index b63cbb769..30b23351b 100644 --- a/app/models/ai_summary.rb +++ b/app/models/ai_summary.rb @@ -23,10 +23,9 @@ def self.store!(strategy, llm_model, summary, og_content, human:) }, unique_by: %i[target_id target_type summary_type], update_only: %i[summarized_text original_content_sha algorithm origin content_range], - returning: AiSummary.column_names, ) .first - .then { OpenStruct.new(_1) } + .then { AiSummary.find_by(id: _1["id"]) } end def self.build_sha(joined_ids)