fix(ingest): stop hallucinating summaries from filenames on empty PDFs

Scanned PDFs without a successful OCR pass (e.g. when no VLM is configured
on the KB, or when the VLM returned a "no text content" reply that the
sanitizer discarded) reach the summary and wiki-ingest pipelines with
nothing but markdown image references for content. The downstream LLM
prompts used to receive the filename, file type, and title alongside that
empty content, and the generate_summary template explicitly forbade the
model from refusing — so the model would invent a topic from the filename
alone, producing e.g. a "Canon scanner manual" summary for an MX5280.pdf
that is actually a scanned legal letter.

Two changes shut this down:

1. Drop filename / title / file-type from every LLM prompt input. The
   wiki summary, knowledge extract, candidate slug, and chunk citation
   prompts now see only the document content. The knowledge summary
   path no longer prepends the "Document Type: X / File Name: Y" intro.

2. Add a content-sufficiency guard in front of the LLM call. A new
   helper strips markdown image references and HTML image tags and
   refuses to invoke the model when fewer than 50 real-text runes
   remain. getSummary returns a sentinel error in that case and the
   caller marks the knowledge as SummaryStatusFailed instead of
   surfacing the first chunk's bare image reference. Wiki ingest skips
   LLM extraction with a clear log message.

The generate_summary.yaml prompt is also updated: the "NEVER output
refusal phrases" clause is removed and replaced with an explicit
empty-content rule that returns a deterministic placeholder line.

Real OCR (Tesseract / PaddleOCR) and filename-pattern sanitisation are
intentionally left for separate PRs.
This commit is contained in:
Claude
2026-05-02 09:06:32 +00:00
committed by lyingbug
parent d3d433574f
commit 15550d14d4
7 changed files with 183 additions and 42 deletions
@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"regexp"
"sort"
"strings"
"sync"
@@ -1213,6 +1214,48 @@ func appendUnique(arr types.StringArray, s string) types.StringArray {
return append(arr, s)
}
// minTextContentRunes is the minimum number of non-whitespace, non-image-reference
// runes required for content to be considered substantive enough for LLM
// summarization or wiki extraction. Documents below this threshold (e.g. a
// scanned PDF where OCR yielded nothing) are routed to a deterministic empty-
// content fallback instead of being passed to the LLM, which would otherwise
// hallucinate based on metadata alone.
const minTextContentRunes = 50
var (
mdImageRefRE = regexp.MustCompile(`!\[[^\]]*\]\([^)]*\)`)
// Strip the outer <images> wrapper before the inner <image> blocks so that
// nested structures like "<images><image>x</image></images>" don't leave a
// stray closing tag behind. RE2 has no backreferences, so we use two
// separate patterns instead of `<(image|images)\b...</\1>`.
htmlImagesBlockRE = regexp.MustCompile(`(?is)<images\b[^>]*>.*?</images>`)
htmlImageBlockRE = regexp.MustCompile(`(?is)<image\b[^>]*>.*?</image>`)
htmlImageTagRE = regexp.MustCompile(`(?i)<img\b[^>]*>`)
)
// stripImageMarkup removes markdown image references, raw <img> tags, and
// <image>/<images> XML blocks so that content consisting only of image
// placeholders (e.g. from PDFScannedParser pages without successful VLM OCR)
// is recognised as effectively empty.
func stripImageMarkup(s string) string {
s = htmlImagesBlockRE.ReplaceAllString(s, "")
s = htmlImageBlockRE.ReplaceAllString(s, "")
s = mdImageRefRE.ReplaceAllString(s, "")
s = htmlImageTagRE.ReplaceAllString(s, "")
return s
}
// hasSufficientTextContent reports whether the given content carries enough
// real text (after image markup is stripped) to warrant an LLM call. It is
// the primary defence against filename-driven hallucinations on scanned PDFs.
func hasSufficientTextContent(content string) bool {
stripped := strings.TrimSpace(stripImageMarkup(content))
if stripped == "" {
return false
}
return len([]rune(stripped)) >= minTextContentRunes
}
// cleanLLMJSON strips markdown code-fence wrappers and sanitizes control characters
// from LLM-generated JSON output so it can be safely unmarshalled.
func cleanLLMJSON(s string) string {