diff --git a/internal/application/service/knowledge.go b/internal/application/service/knowledge.go index d7ee6bf8..7a204126 100644 --- a/internal/application/service/knowledge.go +++ b/internal/application/service/knowledge.go @@ -2249,6 +2249,25 @@ const defaultMaxInputChars = 1024 * 24 // chunk's raw content (which would just be a bare image reference). var errInsufficientSummaryContent = errors.New("insufficient text content for summary generation") +// checkSufficientSummaryContent returns errInsufficientSummaryContent if the +// given content does not carry enough real text (after stripping image markup) +// for an LLM summary call, and logs a warning at the call site. Returns nil +// when the content passes the threshold. +// +// Extracted so the threshold gate can be unit-tested without standing up the +// full ProcessSummaryGeneration dependency graph. +func checkSufficientSummaryContent(ctx context.Context, knowledgeID, content string) error { + realTextLen := realTextRuneCount(content) + if realTextLen < minTextContentRunes { + logger.GetLogger(ctx).Warnf( + "summary content check: knowledge %s has insufficient text after stripping image markup (real_text_runes=%d, min=%d); skipping LLM call", + knowledgeID, realTextLen, minTextContentRunes, + ) + return errInsufficientSummaryContent + } + return nil +} + // getSummary generates a summary for knowledge content using an AI model func (s *knowledgeService) getSummary(ctx context.Context, summaryModel chat.Chat, knowledge *types.Knowledge, chunks []*types.Chunk, @@ -2308,13 +2327,8 @@ func (s *knowledgeService) getSummary(ctx context.Context, // scanner model), and feeding that to the model would invite it to // hallucinate a scanner manual instead of admitting the document had no // extractable text. - realTextLen := realTextRuneCount(chunkContents) - if realTextLen < minTextContentRunes { - logger.GetLogger(ctx).Warnf( - "getSummary: content for knowledge %s has insufficient text after stripping image markup (real_text_runes=%d, min=%d); skipping LLM call", - knowledge.ID, realTextLen, minTextContentRunes, - ) - return "", errInsufficientSummaryContent + if err := checkSufficientSummaryContent(ctx, knowledge.ID, chunkContents); err != nil { + return "", err } // Pass the raw chunk text to the LLM with no filename / file-type framing. diff --git a/internal/application/service/knowledge_summary_test.go b/internal/application/service/knowledge_summary_test.go new file mode 100644 index 00000000..f75f477e --- /dev/null +++ b/internal/application/service/knowledge_summary_test.go @@ -0,0 +1,105 @@ +package service + +import ( + "context" + "errors" + "testing" +) + +// TestCheckSufficientSummaryContent verifies the gate that prevents getSummary +// from calling the LLM (and ProcessSummaryGeneration from creating a summary +// chunk) when the document has no usable text. This is the entry point for +// the errInsufficientSummaryContent → SummaryStatusFailed flow that the +// caller in ProcessSummaryGeneration relies on. +func TestCheckSufficientSummaryContent(t *testing.T) { + ctx := context.Background() + + tests := []struct { + name string + content string + wantError bool + }{ + { + name: "empty content rejected", + content: "", + wantError: true, + }, + { + name: "only whitespace rejected", + content: " \n\n\t ", + wantError: true, + }, + { + name: "below threshold rejected", + content: "hi", + wantError: true, + }, + { + name: "scanned PDF with no OCR (image-only) rejected", + content: "![MX5280_page_1.png](images/MX5280_page_1.png)\n" + + "![MX5280_page_2.png](images/MX5280_page_2.png)", + wantError: true, + }, + { + name: "scanned PDF with empty wrapper rejected", + content: `![a](x)`, + wantError: true, + }, + { + name: "short legitimate note above threshold accepted", + content: "Meeting at 3pm tomorrow.", + wantError: false, + }, + { + name: "scanned PDF with successful VLM OCR accepted", + content: ` +![p1](images/p1.png) +scanned letter +Sehr geehrter Herr Mustermann, in der Sache 4711/2024 ... +`, + wantError: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := checkSufficientSummaryContent(ctx, "test-knowledge-id", tt.content) + if tt.wantError { + if err == nil { + t.Errorf("expected errInsufficientSummaryContent, got nil") + return + } + if !errors.Is(err, errInsufficientSummaryContent) { + t.Errorf("expected errInsufficientSummaryContent sentinel, got %v", err) + } + } else { + if err != nil { + t.Errorf("expected nil error, got %v", err) + } + } + }) + } +} + +// TestCheckSufficientSummaryContent_ThresholdOverride verifies that +// minTextContentRunes is a `var` (not const) so tests and future runtime +// configuration can adjust the threshold without a rebuild. +func TestCheckSufficientSummaryContent_ThresholdOverride(t *testing.T) { + ctx := context.Background() + content := "Meeting at 3pm." // 15 runes + + originalThreshold := minTextContentRunes + t.Cleanup(func() { minTextContentRunes = originalThreshold }) + + // With default threshold (10), this content passes. + if err := checkSufficientSummaryContent(ctx, "kid", content); err != nil { + t.Fatalf("default threshold: expected pass, got %v", err) + } + + // With a tighter threshold (50), the same content is rejected. + minTextContentRunes = 50 + err := checkSufficientSummaryContent(ctx, "kid", content) + if !errors.Is(err, errInsufficientSummaryContent) { + t.Fatalf("tightened threshold: expected errInsufficientSummaryContent, got %v", err) + } +} diff --git a/internal/application/service/wiki_ingest.go b/internal/application/service/wiki_ingest.go index 517df3c8..ca78d2be 100644 --- a/internal/application/service/wiki_ingest.go +++ b/internal/application/service/wiki_ingest.go @@ -1225,7 +1225,10 @@ func appendUnique(arr types.StringArray, s string) types.StringArray { // The threshold is intentionally low: legitimate short documents (brief // memos, single-line notes) must still pass. The goal is only to catch // the empty-image-only case. -const minTextContentRunes = 10 +// +// Declared as a var (not const) so tests can override it and future config +// plumbing can adjust it at runtime without a rebuild. +var minTextContentRunes = 10 var ( // Markdown image references like ![alt](path) — pure visual placeholders