fix: enhance QA summarization logic to prevent double summarization of sections

- Added checks to avoid summarizing sections that already contain summarized content.
- Improved handling of recent sections to keep for AST generation, ensuring accurate summaries without redundancy.
- Refactored summary generation logic for better clarity and error handling.
This commit is contained in:
Dmitry Ng
2026-03-19 17:14:39 +03:00
parent 938458ed0f
commit bb50d10e04
+24 -7
View File
@@ -514,6 +514,21 @@ func summarizeQAPairs(
return nil
}
// Determine how many recent sections to keep for later create new AST with summary + recent sections
sectionsToKeep := determineRecentSectionsToKeep(ast, keepQASections, maxQASections, maxQABytes)
sectionsToSummarize := ast.Sections[:len(ast.Sections)-sectionsToKeep]
// Prevent double summarization of the first section with already summarized content
switch len(sectionsToSummarize) {
case 0:
return nil
case 1:
firstSectionBody := sectionsToSummarize[0].Body
if len(firstSectionBody) == 1 && containsSummarizedContent(firstSectionBody[0]) {
return nil
}
}
// Generate human message summary if it exists and needed
var humanMsg *llms.MessageContent
if len(humanMessages) > 0 {
@@ -535,17 +550,19 @@ func summarizeQAPairs(
}
// Generate summary
aiSummary, err := GenerateSummary(ctx, handler, humanMessages, aiMessages)
if err != nil {
return fmt.Errorf("QA (ai) summary generation failed: %w", err)
var (
err error
aiSummary string
)
if len(aiMessages) > 0 {
aiSummary, err = GenerateSummary(ctx, handler, humanMessages, aiMessages)
if err != nil {
return fmt.Errorf("QA (ai) summary generation failed: %w", err)
}
}
// Create a new AST with summary + recent sections
sectionsToKeep := determineRecentSectionsToKeep(ast, keepQASections, maxQASections, maxQABytes)
// Create a summarization body pair with the generated summary
var summaryPair *cast.BodyPair
sectionsToSummarize := ast.Sections[:len(ast.Sections)-sectionsToKeep]
switch t := determineTypeToSummarizedSections(sectionsToSummarize); t {
case cast.Summarization:
summaryPair = cast.NewBodyPairFromSummarization(aiSummary, tcIDTemplate, false, nil)