fix(knowledge-graph): KG Build 管线七项级联缺陷端到端深度修复 - #524
Merged
Conversation
依据 2026-05-12 一次完整 KG Build 日志(20 chunks · 520s)端到端排查, 正交分解并一次性修复 7 项相互独立但级联放大的缺陷(详见 docs/issue.md ISSUE-082): 1. PageRank/Community UPDATE SQL 改占位符级 CAST(修复 `syntax error at or near "uuid"`); 2. Leiden 改经由 igraph + leidenalg 直连,规避 NetworkX 3.x dispatch wrapper 误派发; 3. call_llm_with_retry 注入 extra_kwargs + 全局 litellm.drop_params=True,规避 gpt-5 系列 UnsupportedParamsError 致社区摘要全军覆没; 4. Embedding 失败路径输出 actionable hint,定位本地 Gemini 翻译代理兼容缺陷; 5. chunk_index 在批次调度时一次性预分配,消除并发竞态致 log 中重复 chunk_index; 6. extracting 阶段 maybe_report_chunk_progress 累计 entity/relation_count 同步落库, 修复"进度 80% 但计数恒 0"的 UI 反直觉体验; 7. sync_relation / sync_entity_from_knowledge 改返回 bool,batch_sync 按返回值拆分 relations_created / relations_skipped / relations_failed 计数,修复 152→143 静默丢失。 测试与质量: - 新增 tests/unit_tests/knowledge/test_kg_build_pipeline_fixes.py 9 条 UT 锁定 7 项契约; - 升级 test_kg_entity_service_unit / test_graph_entity_service 三条原"silent assertion of bug"用例(之前把跳过当成功),校正为 synced=0 + skipped=N 真值; - `uv run pytest tests/unit_tests` 1466 条通过(1 个 pre-existing 失败与本次无关); - `uv run ruff check` 全绿;docs/issue.md + docs/knowledge-graph.md 同步沉淀。 🤖 Generated with [Claude Code](https://github.com/claude), [CodeX](https://openai.com), [Gemini](https://github.com/apps/gemini-code-assist) Co-Authored-By: Aurelius Huang<threefish.ai@gmail.com>
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.
背景
依据 2026-05-12 10:00–10:19 的一次完整 KG Build 日志(corpus
43bacd7e-...、20 chunks、~520s)端到端循证排查,结合docs/knowledge-graph.md理论梳理与docs/issue.md(ISSUE-013/-020/-026/-077/-081 等历史沉淀),识别出 七项相互独立但级联放大 的缺陷。最终产物名义entity_count=98 relation_count=152 status=completed,实际:importance_score全 NULL(UPDATE SQL 报错)temperature=0.3与 gpt-5 系列冲突)400 invalid prompts(本地 Gemini 翻译代理兼容缺陷)chunk_index=1build_run_updated entity_count=0 relation_count=0静默relations_synced计数虚高)KG 名义构建成功,实际下游 GraphRAG / Global Search 几乎不可用。本 PR 一次性正交修复全部 7 项缺陷。
缺陷与修复对照
syntax error at or near \"uuid\"graph_algorithms.py:142-160 / 368-384FROM (VALUES …) AS v(col type, …)内联类型声明CAST(:eid_n AS uuid)/CAST(:cid AS uuid)显式转型'leiden_communities' is not implemented by 'networkx' backend×3graph_algorithms.py:34-89 / 310-360nx.community.leiden_communities是 dispatch wrapper,不会派发到 leidenalg_run_leiden()经由igraph + leidenalg.find_partition直连;首层失败一次性降级 Louvaingpt-5 … don't support temperature=0.3×3extractors.py::call_llm_with_retry+community_summarizer.py::_call_llmcall_llm_with_retry既未读取resolve_llm_config()返回的drop_params,也未全局设置extra_kwargs: dict参数、litellm.drop_params=True幂等设置、_PROTECTED_KEYS守卫;caller 透传 vendor 配置litellm.BadRequestError: \"request body doesn't contain valid prompts\"embedding.py:185-246localhost:3392对:batchEmbedContents兼容不全(环境侧)_build_embedding_failure_hint()输出可诊断 hint(建议切换 openai embedding / 检查NATIVE_GEMINI_BASE_URL)chunk_index=1service.py:605-656chunks_processed + 1并发非原子读enumerate(batch)注入 1-based 全局序号i + offset + 1entity_count=0 relation_count=0持续 8 分钟service.py::maybe_report_chunk_progressprogress_percent,未累计落库len(all_entities) / len(all_relations)并在chunk_batch_progress日志透出relations_synced=152但edge_count=143,9 条静默丢失entity_service.py::sync_relation+batch_sync_from_graph_buildsilent return,callertry: relations_synced += 1把跳过当成功sync_relation / sync_entity_from_knowledge改返回bool;caller 拆relations_created / relations_skipped / relations_failed三计数;端点缺失日志debug → warning工程纪律沉淀(跨上下文准则)
CAST(:p AS type),禁用AS v(col type)内联(asyncpg/psycopg3/pg-protocol bridge 多驱动行为不一致)nx.community.*前必须确认是否为 dispatch wrapper;Leiden / Modularity 算法一律走igraph + leidenalg直连litellm.acompletion / aembedding入口必须传drop_params=True或进程级开关;call_llm_with_retry已统一注入success/skip/fail,禁用"未抛异常 = 成功"语义详见
docs/issue.mdISSUE-082 与docs/knowledge-graph.md5.3 节方法学补丁。改动文件清单
源码(6):
apps/negentropy/src/negentropy/knowledge/graph/graph_algorithms.py(PageRank SQL + Leiden)apps/negentropy/src/negentropy/knowledge/graph/extractors.py(call_llm_with_retry + drop_params)apps/negentropy/src/negentropy/knowledge/graph/community_summarizer.py(extra_kwargs 透传)apps/negentropy/src/negentropy/knowledge/graph/service.py(chunk_index 预分配 + 累计计数上报)apps/negentropy/src/negentropy/knowledge/graph/entity_service.py(sync_relation bool 返回 + 三计数拆分)apps/negentropy/src/negentropy/knowledge/ingestion/embedding.py(actionable hint)依赖(1):
apps/negentropy/pyproject.toml追加igraph>=0.11(+uv.lock同步)测试(3):
tests/unit_tests/knowledge/test_kg_build_pipeline_fixes.py(9 条 UT,每项缺陷独立锁定契约)test_kg_entity_service_unit.py/test_graph_entity_service.py三条原 "silent assertion of bug" 用例(之前把跳过当成功,现校正为relations_synced=0 + relations_skipped=N真值)文档(2):
docs/issue.md(ISSUE-082)+docs/knowledge-graph.md(5.3 方法学补丁)测试与质量
uv run pytest tests/unit_tests:1466 通过(1 个 pre-existingtest_extraction_llm_plan失败与本次无关,git stash 验证)CAST(:eid AS uuid)/ 反向断言v(eid uuid不再出现;Leiden 经 leidenalg 直连而非 NetworkX dispatch;drop_params 全局幂等 +_PROTECTED_KEYS不被覆盖;Embedding hint 已知模式触发 / 未知模式空返回;sync_relation 端点命中返回 True / 缺失返回 Falseuv run ruff check:全绿(含 pre-commit hook 自动 format 后再校验)验证范围说明
本次以确定性单元测试完整锁定 7 项修复契约。未进行浏览器端到端实机验证 —— 本地后端 / UI / Gemini Proxy 当前未在 dev 环境启动。如需 E2E 验证,参
docs/agents/browser-validation.md与计划文件中 7 个观察点(A-G)执行:SELECT importance_score FROM kg_entities WHERE corpus_id=... ORDER BY 1 DESC返回非 NULL Top-10community_level_completed level=0/1/2 community_count>1community_summary_exhausted;kg_community_summaries多条非空摘要hint=字段;摘要可降级落库(embedding NULL)kg_first_class_sync relations_synced=N relations_skipped=M与graph_loaded edge_count数值闭环一致Test plan
uv run pytest tests/unit_tests/knowledge全绿(678 通过,含 9 条新增)uv run pytest tests/unit_tests全量 1466 通过uv run ruff check全绿🤖 Generated with Claude Code, CodeX, Gemini