WeKnora icon indicating copy to clipboard operation
WeKnora copied to clipboard

[Bug]: Wiki ingest silently discards everything after the first 32768 characters

Open JSJ189 opened this issue 1 month ago • 3 comments

Affected Component

Backend Service & API

Bug Description

Wiki ingest keeps only the first 32768 Unicode characters of a document and drops the rest with no error and no UI signal. Entities and concepts that appear later in a long PDF never enter the Wiki.

This is separate from #2604. #2604 was the output JSON being cut off (finish_reason=length). That path now sets wikiLLMMaxTokens. The bug here is the input: mapOneDocument slices the reconstructed document before any extraction call.

internal/application/service/wiki_ingest.go:

// maxContentForWiki limits the document content sent to LLM for wiki generation
maxContentForWiki = 32768

internal/application/service/wiki_ingest_batch.go (mapOneDocument):

content := appendScannedPageText(
    reconstructEnrichedContent(ctx, s.chunkRepo, payload.TenantID, chunks),
    chunks,
)
rawRuneCount := len([]rune(content))
if len([]rune(content)) > maxContentForWiki {
    content = string([]rune(content)[:maxContentForWiki])
}

There is no second segment. extractCandidateSlugs / the legacy extractor run only on this prefix. The only trace is an info log (content_len(raw=…,truncated=…)).

Document summary already avoids a pure head cut. sampleLongContent in knowledge_process.go keeps head, middle and tail and inserts [...content omitted...]. Wiki ingest does not.

#2977 (still open) reported the same tail loss and proposed splitContentIntoSegments. That function is not in v0.8.3. maxContentForWiki is still a single hard slice.

Steps to Reproduce

  1. Enable Wiki on a knowledge base.
  2. Upload one text-heavy PDF whose extracted text is well above 32768 characters (a multi-hundred-page specification or a drawing book with a long table of contents and per-sheet notes).
  3. Wait until Wiki ingest completes. The trace shows success, not EXTRACT_FAILED.
  4. Compare Wiki pages with the source. Pages and entities that occur only after the first 32768 characters are absent.
  5. In logs, wiki ingest: doc … content_len(raw=N,truncated=32768) with N > 32768.

Expected Behavior

The whole document is covered. If one LLM call cannot take the full text, ingest should walk the remainder in order (overlapping segments, then merge). If any part is skipped, the parse trace and the document status must say so. A successful Wiki run must not mean "only the beginning was read".

Actual Behavior

Ingest succeeds. Only content[:32768] is sent. The tail is discarded. Later chapters, sheets and entities are missing from the Wiki, with no failed status.

WeKnora Version

v0.8.3 (source tag; maxContentForWiki unchanged on that tree)

Deployment Method

Docker Compose

Operating System

Linux

Relevant Logs

wiki ingest: doc <knowledge_id> chunks=<large> content_len(raw=<well above 32768>,truncated=32768)

Confirmation

  • [x] I have searched existing issues and confirmed this is a new one

Related, not the same bug: #2604 (output token budget, fixed), #2977 (mentions this tail loss inside a larger patch request; the segmented reader is not in v0.8.3).

JSJ189 avatar Oct 21 '26 01:10 JSJ189

我想认领这个问题。确认了一下,main(e0c28bef3)上 wiki_ingest_batch.go 的 mapOneDocument 仍然在调用 extractCandidateSlugs 之前把内容硬截到 maxContentForWiki = 32768,#2977 里提到的 splitContentIntoSegments 也还没进主线。我想到两种改法:(a) 按带重叠的窗口把全文分段,逐段跑候选提取,再按 slug 合并进 slugItems,覆盖完整,但长文档的 LLM 调用次数会随段数增加,可能需要一个段数上限;(b) 复用 knowledge_process.go 里的 sampleLongContent 取头/中/尾,调用次数不变,但中间仍会漏内容,所以同时在 wiki trace 里记一个 truncated 标记和原始长度,让用户知道没有读全。你们更倾向哪一种?方向确定后我来提 PR 并补单测。

kerthans avatar Oct 21 '26 02:10 kerthans

  1. 我想认领这个问题。确认了一下,main(e0c28bef3)上 wiki_ingest_batch.go 的 mapOneDocument 仍然在调用 extractCandidateSlugs 之前把内容硬截到 maxContentForWiki = 32768,#2977 里提到的 splitContentIntoSegments 也还没进主线。我想到两种改法:(a) 按带重叠的窗口把全文分段,逐段跑候选提取,再按 slug 合并进 slugItems,覆盖完整,但长文档的 LLM 调用次数会随段数增加,可能需要一个段数上限;(b) 复用 knowledge_process.go 里的 sampleLongContent 取头/中/尾,调用次数不变,但中间仍会漏内容,所以同时在 wiki trace 里记一个 truncated 标记和原始长度,让用户知道没有读全。你们更倾向哪一种?方向确定后我来提 PR 并补单测。

我感觉wiki如果不能读完全文内容的话,那也意义不大了,所以应该是用你说的a方案会好一点,LLM调用次数增加可以设可调节的设置,总而言之应该能让wiki包含长文档的所有内容。

JSJ189 avatar Oct 22 '26 07:10 JSJ189

Thanks @JSJ189, went with (a): #4616 runs Pass 0 over overlapping windows of the full text and merges by slug. The number of windows per document is adjustable via WikiConfig.ingest_max_content_segments (default 8, ~256k chars). Open to raising the default or allowing unlimited — noted in the PR description.

kerthans avatar Oct 22 '26 08:10 kerthans