[Bug]: image chunks always store source_locators NULL — gallery page sort and figure citations break
Check before submitting
- [x] I searched existing issues and this is not a duplicate
- [x] I can reproduce this on the latest release
WeKnora version
v0.9.0 (tag f17ee8a8)
Deployment method
Docker Compose
Affected component
Backend / API
Describe the bug
Every image chunk (image_caption, image_ocr, image_vector) is persisted with source_locators = NULL, so:
#4649gallery page sort degrades to(page IS NULL) ASCand orders by nothing — a 25-page document shows page 25 next to page 1- a citation pointing at a figure cannot open the PDF at the right sheet
Text chunks from the same document carry pages correctly, so the parser and the page_idx plumbing are fine.
Measured on a 25-page PDF parsed through MinerU (native endpoint), after a full reparse:
| chunk type | chunks | with source_locators |
|---|---|---|
text |
10 | 10 (pages 1, 2, 3) |
image_caption |
7 | 0 |
image_ocr |
7 | 0 |
image_vector |
7 | 0 |
Root cause 1 — LocatorsAt can never match an image reference
internal/sourceloc/assign.go:
// LocatorsAt returns the locators of the blocks containing rune offset pos,
// without quotes. Used to place an image reference found in the markdown.
func (x *Index) LocatorsAt(pos int) types.SourceLocators {
if x == nil || pos < 0 {
return nil
}
i := sort.Search(len(x.blocks), func(k int) bool { return x.maxEnd[k] > pos })
var out types.SourceLocators
for ; i < len(x.blocks) && x.blocks[i].Start <= pos; i++ {
if b := x.blocks[i]; b.End > pos {
...
}
}
return out
}
The doc comment says it places image references, but the loop only yields a locator when b.Start <= pos < b.End. An image reference  occupies its own line between two text blocks, so it is never strictly contained and the lookup returns nil.
The single caller is internal/application/service/knowledge_source_locators.go:130:
images[f.image].SourceLocators = idx.LocatorsAt(runePos)
so every image silently keeps nil. buildSourceIndex is the only place that fills docparser.StoredImage.SourceLocators — the field comment even says "Filled by ingestion after resolution" — and it fills nothing.
Root cause 2 — the image vector chunk never copies the payload field
internal/application/service/image_multimodal.go: the caption and the OCR chunks both set SourceLocators: payload.SourceLocators (~line 426 and ~line 446), but the image vector chunk (~line 881) does not — it sets ParentChunkID and ImageInfo and stops:
chunk := &types.Chunk{
ID: uuid.New().String(),
...
ChunkType: types.ChunkTypeImageVector,
ParentChunkID: payload.ChunkID,
IsEnabled: true,
Flags: types.ChunkFlagRecommended,
ImageInfo: source.ImageInfo,
...
}
So even after fixing root cause 1, image_vector stays NULL.
Expected behavior
An image chunk should carry the page of the text block it follows — that is what a viewer needs to open the source at the right sheet, and what #4649 needs to sort a drawing set by page.
Suggested fix
- Keep
LocatorsAtstrict and addNearestLocatorsAtininternal/sourceloc: try the strict lookup first, then fall back to the nearest preceding block, then to the nearest following one. SetPartial: trueand dropBBox, because only the page is known, not the region. - Use it at
knowledge_source_locators.go:130. - Add
SourceLocators: payload.SourceLocatorsto the image vector chunk.
Verification after the fix
Same document, full reparse, v0.9.0 + the patch above:
| chunk type | before | after |
|---|---|---|
text |
10/10 | 10/10 |
image_caption |
0/7 | 7/7 |
image_ocr |
0/7 | 7/7 |
image_vector |
0/7 | 7/7 |
Stored locator on an image chunk:
{"type":"pdf","mapping":"exact","partial":true,"source_id":"mineru:2:24:7/1","page":25}
Note
This was found while evaluating drawing retrieval against #4649. Documents parsed before the fix keep their NULL locators; only a reparse fills them, which is worth stating in the release note of whatever version ships the fix.