WeKnora/internal
linus-liu-web 04da26024c
fix(weaviate): CopyIndices 补游标防护,并修掉 GraphQL 报错时的 panic (#3881)
* fix(weaviate): 复制索引时游标不前进立即报错,并给分页加跳数上限

CopyIndices 的游标由每页最后一个对象的 id 反推(repository.go:763),循环的
退出条件只有"查询出错"和"这一页为空"。源端只要重复返回同一页,游标就永远
停在同一个 id 上:循环把这一页反复读出、再反复 upsert 进目标库,直到复制任务
撞上 2 小时超时;源端在该页之后的数据永远读不到,而目标库里同一批 chunk 已经
被写成成千上万份候选向量。同包的 move.go 早就记录了 after+where 在 Weaviate
上不可靠,并为此用 seen 集合拦住"游标不前进",CopyIndices 用的正是这个组合,
却没有那层保护。

本次修改:

- 记录每个用过的游标,重复即返回 "copy indices made no progress at cursor",
  不再把"游标不动"当成"读完了"。检测点放在整页解析之后、批量写入之前,所以
  重复的那一页不会再多写一份。
- 分页跳数上限 maxCopyPaginationHops(10000 页,按每页 64 条合 64 万行,与
  confluence 的 maxPaginationHops 同量级);触顶时打印 Warn 并返回错误,而不是
  把这轮复制当成成功——静默截断会留下一个看起来正常的残缺索引。
- additional["id"] 从无检查断言改为带检查断言,缺 id 或 id 为空时返回错误;
  原先这里会 panic。同包 move.go 的 move index ID 已是这种写法。

回归用例用假 Weaviate 服务端复现两种源端形态:恒定返回同一页的源端在第二次
查询后被拦下(GraphQL 查询 2 次、目标库只写 64 条,而修复前 5s 内同一页被重放
4484 次、写入 286912 条);一直推进但永不结束的源端在第 10000 页被拦下。

* fix(weaviate): 复制索引时 GraphQL 层报错不再被当成"源已读完",也不再 panic

Weaviate 对于"执行不了的查询"返回的是 HTTP 200 + errors 数组 + 没有 data。SDK 的
runGraphQLQuery 只在 HTTP 状态异常时返回 error(weaviate-go-client 的
graphql/gql.go:136-150),所以这种响应到 CopyIndices 手里时 err == nil,而
result.Data 是 nil。

原代码只检查 err,随后一行完成两件事:

    objects, ok := result.Data["Get"].(map[string]interface{})[collectionName].([]interface{})

这里逗号-ok 只作用于最后一个断言,第一个 `.(map[string]interface{})` 是单值断言;
result.Data 为 nil 时该表达式的值是 nil 接口,单值断言直接 panic。实测
(假服务端返回 {"errors":[{"message":"class Weknora_embeddings_64 does not exist"}]}):

    panic: interface conversion: models.JSONObject is nil, not map[string]interface {}
        .../weaviate.(*weaviateRepository).CopyIndices(...)
            repository.go:753

也就是说复制任务在这种响应下不是"静默少拷",而是直接 panic;即便不 panic,把
"没有 data"当成"这一页为空"也会让一次失败的查询以 Index copy completed 收尾。

本次修改:

- 在 err 检查之后补 `len(result.Errors) > 0` → 记录日志并返回
  "weaviate: copy indices query failed: <message>",与同文件 VectorRetrieve
  (:608-611)、KeywordsRetrieve (:677-680) 以及同包 move.go:32-34 的既有写法一致。
- 把那一行的两级断言拆成两次逗号-ok 断言,响应缺 Get 或 class 键时返回
  "weaviate: copy indices invalid response",不再 panic。

回归用例 TestCopyIndicesReportsAQueryError:假服务端对所有 Get 返回上述 errors
响应,断言 CopyIndices 返回带原始 message 的错误(修复前是 panic),且只发一次
查询、没有把任何东西当成复制成功。

* test(weaviate): 补一个"响应读不出 data 时不得 panic"的复制用例

上一个提交把 `result.Data["Get"].(map[string]interface{})` 这一处单值断言拆成了两次
逗号-ok 断言,但只验证了"带 errors 的响应"那条路径:那条路径在 Errors 检查处就返回
了,走不到断言。这里补 TestCopyIndicesRejectsAResponseWithoutData,让假服务端返回
HTTP 200 + `{"data":{}}`(没有 errors,也没有 Get 键),断言 CopyIndices 返回
invalid response 而不是 panic;去掉断言拆分、只保留 Errors 检查时该用例必红。

两个用例都用 recover 把 panic 转成明确的用例失败,避免一个 panic 直接终结整个测试
二进制、连带吞掉同包其它用例的结果。

* fix(retriever/weaviate): CopyIndices 按映射分块读源行,去掉会被服务端拒绝的游标

CopyIndices 用 where + after 遍历源知识库,而 Weaviate 的游标 API 不允许过滤条件
与 after 同时出现:ValidateCursor 直接返回 "where cannot be set with after and
limit parameters",且 client 第一页就会把 WithAfter("") 发成 after: "",所以这个
查询在真实服务端上一页都跑不通。该报错以 HTTP 200 + errors 返回,不检查 errors 的
代码会把它当成"源已读完",返回成功但一条都没复制(本 PR 已补的 errors 检查会把它
变成错误,但那是在暴露一个本就不合法的查询)。即使去掉 where,游标仍然是"跨请求
保持不变的结果顺序"上的位置,服务端不承诺维持该顺序,一旦漂移就会漏行且仍报成功。

改成按映射里的源 chunk ID 分块读:谓词 = knowledge_base_id 相等 AND chunk_id
ContainsAny(本批 64 个),不带 after。读到的行集合由谓词唯一确定,没有游标可漂。

同一个 chunk 会有正文向量 + 多个生成问题向量,64 个 ID 不等于最多 64 行,所以每批
查询显式带 limit(QUERY_MAXIMUM_RESULTS,v1.37.3 默认 10000)。该配置没有客户端
接口(/v1/meta 只有 version/hostname/modules),先按默认值查,被服务端以
"query maximum results exceeded ... QUERY_MAXIMUM_RESULTS 'N'" 拒绝时按 N 降级
重试。一批返回行数达到 limit 说明可能被截断,对该批对半拆分重查直到单个 chunk;
单个 chunk 仍然填满 limit 时明确报错(含 chunk ID 与服务端上限),绝不返回复制成功。
不带 limit 的 GraphQL Get 会落到 QUERY_DEFAULTS_LIMIT_GRAPHQL(100) 静默截断。

页数上限(maxCopyPaginationHops)与重复游标防护随游标一起去掉:读取范围由映射
限定,不存在走不完的分页。GraphQL errors 检查与 data["Get"]、_additional["id"]
的逗号-ok 解包保留。

测试:copy_test.go 的假服务改成按 chunk_id 过滤的真实形状,并像 v1.37.3 一样拒绝
where+after 组合(假服务会断言这种组合从未出现);补同一 chunk 多向量、批次触顶
拆分、单 chunk 超限报错、QUERY_MAXIMUM_RESULTS 降级四类用例。

---------

Co-authored-by: linus-liu-web <11640009+linus-liu-web@users.noreply.github.com>
Co-authored-by: linus-liu-web <linus-liu-web@users.noreply.github.com>
2026-10-09 10:47:30 +08:00
..
agent fix(agent): compare tool enum values without string coercion 2026-10-08 21:07:11 +08:00
application fix(weaviate): CopyIndices 补游标防护,并修掉 GraphQL 报错时的 panic (#3881) 2026-10-09 10:47:30 +08:00
assets feat(assets): add ASR test audio file and embed it in the application 2026-04-02 21:27:27 +08:00
browserskill fix(browser): refine connection and preview UI with extension version 2026-09-23 10:57:59 +08:00
common fix(markdown): sanitize invalid UTF-8 and NUL bytes 2026-08-31 12:24:35 +08:00
config feat(auth): 新增仅限邀请注册档位 (#3900) 2026-09-30 13:32:55 +08:00
container feat(datasource): add Seafile data source connector (#3616) 2026-10-08 16:18:09 +08:00
database revert: take the plugin system back out of main 2026-09-26 14:58:18 +08:00
datasource fix(notion): 保留正文抓取失败时的同步游标 (#3693) 2026-10-09 10:43:59 +08:00
embedpolicy fix(embed): enforce host origin allowlists on framed pages 2026-09-14 18:03:57 +08:00
errors feat(knowledge): flag documents that stop making progress (#3557) 2026-09-22 21:34:08 +08:00
event fix(agent): end the turn when the completion cap truncates an answer (#3449) 2026-09-20 13:17:50 +08:00
filetransport refactor(resource-access): unify scoped authorization and transfer recovery (#3097) 2026-09-08 15:36:33 +08:00
handler feat: index and recall images with multimodal embedding models (#3683) 2026-10-08 18:19:53 +08:00
im fix(im/wecom): preserve image in mixed messages with text captions (#3901) 2026-10-08 01:25:25 +08:00
infrastructure fix(chunker): respect Markdown fences during heading detection 2026-10-08 21:06:59 +08:00
ipclass fix: align client sessions and integration request handling (#3305) 2026-09-16 16:20:57 +08:00
localsandbox feat(lite): install and run skills on the host (#3661) 2026-09-24 21:30:34 +08:00
logger fix(logger): keep LLM debug records inside their directory 2026-09-25 18:22:14 +08:00
mcp fix(mcp): pass embedded resources in tool results to the agent (#3766) 2026-10-08 01:25:04 +08:00
mcpserver fix(mcp): return accessible image URLs from existing tools (#3650) 2026-09-24 11:56:25 +08:00
middleware fix(middleware): stop buffering whole request bodies in the access log (#3927) 2026-10-08 01:29:05 +08:00
modelcontext fix(agent): 图谱查询的三处截断必须可见,模型不再把子图当全集 (#3885) 2026-10-07 23:59:50 +08:00
models feat: index and recall images with multimodal embedding models (#3683) 2026-10-08 18:19:53 +08:00
ratelimit feat(embed): add backend embed channels with auth and rate limiting (#1657) 2026-06-12 23:10:39 +08:00
reranking fix(retrieval): fix recall correctness, score scales, cost and agent tools (#3687) 2026-09-25 10:48:18 +08:00
router fix(frontend): 修复复制模型时模型名称被改写的问题 (#3830) 2026-09-29 19:32:08 +08:00
runtime fix(server): run ResourceCleaner on signal shutdown instead of racing Fatalf exit (#3804) 2026-09-28 23:37:36 +08:00
sandbox fix(sandbox): reclassify exec races with the idle sweep as NotFound so rebinding heals (#3980) 2026-10-08 01:28:53 +08:00
searchutil feat: index and recall images with multimodal embedding models (#3683) 2026-10-08 18:19:53 +08:00
sourceloc fix(citations): 修复原文定位、高亮范围与 DocReader 启动失败 (#3794) 2026-09-27 20:32:26 +08:00
storageallowlist fix(storage): align default provider fallback with STORAGE_ALLOW_LIST 2026-07-15 11:05:00 +08:00
storageurl fix(mcp): return accessible image URLs from existing tools (#3650) 2026-09-24 11:56:25 +08:00
stream feat(session): in-place rewind with workspace checkpoint reset (#3469) 2026-09-20 21:01:00 +08:00
textconv fix: remove GPL converter and bundle third-party license sources 2026-09-08 15:19:10 +08:00
tracing/langfuse fix(milvus): preserve keyword retrieval scores 2026-09-17 11:59:40 +08:00
types feat: index and recall images with multimodal embedding models (#3683) 2026-10-08 18:19:53 +08:00
utils feat(datasource): add Seafile data source connector (#3616) 2026-10-08 16:18:09 +08:00