Repository navigation
fix(knowledge): 图检索排序移出事件循环并在库内截断子图 - #1205
Merged
Merged
Conversation
Owner
|
感谢 PR,思路是合理的。确认一下:是否已在真实业务或真实 Neo4j 上做过相关测试? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
同分支的 #1196 因描述编码损坏被关闭。这次用 GitHub CLI 重新提交,代码没有改。
简介
带图谱的知识库检索会在请求协程里同步跑 networkx PageRank。默认子图可以到一万个节点,种子查询还会把路径上的节点和边整包拿回进程,再在 Python 里截断。一次检索会占住处理该请求的事件循环。
这次把 PageRank 放到独立的有界线程卸载,不占用 Neo4j 查询槽。种子子图仍先按路径预算取样,然后在 Neo4j 里按节点上限截断,种子实体优先;只返回这些节点以及它们之间的关系。没有边时不再把节点行滤掉。
具体改动与影响
非平凡 bug 修复。入口是图检索的
query_and_rank_chunks_by_ppr。排序行为不变。不改图谱写入,也不改 Milvus 召回分数。并发排序最多 2 个。路径预算内仍会先走邻居,但不再把全部路径边传回进程。
实现依据与设计取舍
关键行为与实现边界
run_graph_rank_io与 Milvus、Neo4j 卸载分开,容量为 2。种子 Cypher 在路径LIMIT之后对节点去重、种子优先,再LIMIT $max_nodes。空关系在列表里丢掉,不在OPTIONAL MATCH的WHERE里过滤。事实在backend/yuxi/modules/knowledge/graphs/service.py。决策记录
2026-11-24-graph-ppr-off-event-loop.md。没有先写 proposal:修复和小测试在同一变更里完成,替代方案已经写在决策记录里。
验证情况
行为验证
PageRank 不在请求线程执行:
test_ppr_ranking_runs_outside_event_loop。改回直接调用 rank 会失败。Passed。Cypher 在库内限制节点,且无边时仍返回节点:
test_seed_subgraph_limits_nodes_in_cypher_before_returning。去掉LIMIT $max_nodes或在 OPTIONAL MATCH 上用rel IS NOT NULL丢掉节点会失败。Passed。既有 PPR 顺序不变:
test_graph_ppr_ranks_chunk_nodes_from_seed_entities。Passed。提交检查
在 Linux 容器中执行上述三条 pytest。Passed。
python3 scripts/ci/verify_engineering_contracts.py、其 unittest,以及docker compose exec api uv run pytest test/unit -m "not slow":Not run。本机没有完整后端环境,API 容器因既有 schema 版本不兼容在重启。独立审阅、风险与事故反馈
独立审阅
没有另开不继承实现上下文的 Reviewer。实现者对照 diff 和上述测试自查。
未验证范围与已知风险
没有对真实 Neo4j 跑种子子图。Cypher 只在单测里检查了文本和参数。路径预算内如果没有种子,排序结果仍为空,这是原有行为。
事故反馈
未达到复盘门槛。