#3834·WeKnora

[Bug]: 图片 OCR/Caption 分块的索引写入被静默吞掉,处理轨迹仍标记 indexed: true

Author: linus-liu-webCreated Sep 28, 2026Updated Oct 1, 2026

现象

开启多模态图片解析后,图片的 OCR 文本 / caption 分块会被写入 chunks 表;当这一批分块的向量/关键词索引写入失败时,任务既不报错也不重试,处理轨迹里该图片仍显示 indexed: true。之后用图片里的文字提问检索不到内容,而同一文档的正文分块检索正常。

原因

internal/application/service/image_multimodal.go:422-424 把索引调用与成功标记写在一起:

go
// Index chunks so they can be retrieved
s.indexChunks(ctx, *payload, newChunks)
out["indexed"] = true

indexChunks(internal/application/service/image_multimodal.go:589-679)没有返回值,所有失败分支只写日志后直接返回:

  • :590-594 取知识库失败 → logger.Warnf + return
  • :618-622 取 embedding 模型失败 → logger.Warnf + return
  • :624-628 取租户失败 → logger.Warnf + return
  • :634-639 CreateRetrieveEngineForKB 失败 → logger.Warnf + return
  • :657-660 engine.BatchIndex 失败 → logger.Errorf + return

因此 :424 的 out["indexed"] = true 是无条件写入的成功标记,与索引是否真的写入无关。processImage 返回 nil,Handle(:239-240)拿到 nil,asynq 视为任务成功,不会重试。

同函数内紧邻的上一步对持久化却是严格的(:413-416:CreateChunks 失败会 handleErr = fmt.Errorf(...) 并返回错误)。仓库里其他索引路径也都遵循"索引失败必须让状态失败":internal/application/service/knowledge_process.go:729-753 在 BatchIndex 失败时删除 chunks 与索引、failStage 并标记 parse failed;internal/application/service/extract.go:508-509 在 indexToVectorDB 失败时调用 cleanupOnFailure(:792-804)把文档置为 ParseStatusFailed。只有这条多模态路径把索引错误降级成了日志。

out 会经 tracker.EndSpan(ctx, imgSpan, out)(:302-303)写入 KnowledgeProcessingSpan.Output(internal/application/service/knowledge_span_tracker.go:459-471),所以 indexed: true 是持久化到处理轨迹里的,用户确实看得到。

复现

  1. 准备一个启用了向量/关键词索引(IndexingStrategy.VectorEnabled 或 KeywordEnabled)且开启了 OCR 的知识库,上传一张含文字的图片。
  2. 让向量库的批量写入返回错误(例如把 embedding 服务指向不可用地址,或停掉向量库的写入入口,使 engine.BatchIndex 返回错误)。
  3. 观察:文档处理仍以成功收尾,图片子步骤的轨迹输出里 indexed: true;chunks 表里 image_ocr 行存在,但向量索引中没有对应行;用该图片里的文字提问召回为空。

也可在单测层面直接复现:让检索后端的 BatchSave 返回错误,调用 processImage,可以看到它返回 nil、out["indexed"] == true、分块已落库且状态为默认值、没有任何状态更新。

建议修法

  1. 让 indexChunks 返回 error,上述 5 个失败分支逐一 return err;
  2. 调用点(:422-424)改为只有索引成功才置位成功标记,失败时上抛给 processImage,复用 Handle 已有的 asynq 重试:
    go
    if err := s.indexChunks(ctx, *payload, newChunks); err != nil {
        handleErr = fmt.Errorf("index multimodal chunks: %w", err)
        return handleErr
    }
    out["indexed"] = true
  3. 顺带把 out["indexed"] 从恒真布尔改成带计数的结构(成功 N / 失败 M),并给失败分块一个可对账的状态;目前这些分块落库时未设置 Status,停在默认值,与可检索分块在 DB 侧无法区分。

A-7 查重结果

只读 GitHub API(/search/issues、/commits、/pulls/{n}/files),未做任何写操作,未遇到 403/限流。

查询 命中 结论
repo:Tencent/WeKnora indexed image chunk 17 无重复
repo:Tencent/WeKnora image_ocr index 4 无重复(#3291 是 OCR 提示词,已 closed)
repo:Tencent/WeKnora multimodal index failed 14 无重复(#481 是识别不了扫描件;#3445/#3696 是 processing 卡住)
repo:Tencent/WeKnora indexChunks 2 #3686 / #3687,改的是 IndexInfo.IsEnabled,非吞错
repo:Tencent/WeKnora 图片 OCR 搜不到 0 —
repo:Tencent/WeKnora image_chunk not searchable 0 —
repo:Tencent/WeKnora 多模态 chunk 索引 14 无重复(#2875/#3111 是功能诉求;#2734/#2730 是删除图片仍可检索)
repo:Tencent/WeKnora indexed: true 12 无重复

结论:未发现已有 issue/PR 覆盖本条。

补充佐证(确认"快照未修"且"在途 PR 也不会顺带修掉"):

  • main HEAD 就是 1912ba26(2026-09-28T17:32:23Z),即本缺陷在当前 main 上仍然存在。
  • PR #3687(已合并,2026-09-25)改过 internal/application/service/image_multimodal.go,但改的是 IndexInfo.IsEnabled(本快照已在 :650-653 含有该修复),与错误吞掉无关。
  • PR #3683(open,"index and recall images with multimodal embedding models")修改了 indexChunks 的签名,但其 diff 里 s.indexChunks(...) 之后仍然紧跟 out["indexed"] = true,即该 PR 合并也不会修掉本条。
  • PR #3746(open,图片流水线重构)的 image_multimodal.go diff 未触碰 out["indexed"]。