feat(kg): Phase 3 高影响增强 — GraphRAG 上下文集成 + Agent 双向同步 + 健康指标 + 跨语料推荐 - #446
Merged
Conversation
Gap 3 [P1] 构建管线健壮性 (Nygard, 2018; Majors, 2022): - Migration 0016: kg_build_runs 增加 progress_percent + warnings 列 - build_graph: 批处理循环进度上报、LLM 提取失败重试 1 次、算法失败警告累积 - GraphBuildResult/Response: 新增 warnings + failed_chunk_count 字段 - 前端 API 类型同步 Gap 1 [P0] 增量图谱构建 (Hogan et al., 2021 §6.3; Graphiti, 2025): - Migration 0017: kg_build_runs 增加 processed_chunk_ids 列 - GraphBuildConfig: 新增 incremental 参数 - build_graph: incremental=True 时跳过 clear_graph、仅处理新 chunk、合并已处理 ID - API + 前端: 透传 incremental 参数 Gap 2 [P1] 实体语义去重 (Fellegi & Sunter, 1969; Mudgal et al., 2018): - GraphBuildConfig: 新增 semantic_dedup_threshold (默认 0.92) - graph_service._semantic_dedup: 三阶段 Blocking→Comparison→Classification - graph_repository.find_similar_entities: HNSW ANN 查找 + entity_type 预过滤 - kg_entity_service.merge_entities: aliases 合并 + FK 重定向 + is_active 停用 Gap 4 [P1] 图谱查询缓存 (Tanenbaum & Van Steen, 2017; Kleppmann, 2017): - _TTLCache: 进程内 300s TTL 缓存,get_graph/get_stats 缓存命中 - 缓存失效: build_graph/clear_graph 触发确定性失效 文档更新: - docs/knowledge-graph.md: Phase 2 状态 ✅、Phase 3 新增 P3-9/10/11、新增 [16]-[24] 引用 🤖 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>
Migration 0016 试图 ALTER TABLE kg_build_runs 但该表从未被任何 migration 创建(0001 仅有 kg_entities/kg_relations/kg_entity_mentions)。 在 0016 upgrade() 中补加 CREATE TABLE IF NOT EXISTS(幂等),确保 CI 全新数据库和已有生产库均可正常迁移。 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
P3-12: ContextAssembler._collect_kg_context() 注入 Top-5 重要实体 + 1-hop 关系摘要到 Agent 上下文 P3-13: _sync_triple_to_kg() 将 Agent 内化三元组同步到 kg_entities/kg_relations(强化模式) P3-14: get_stats() 扩展孤立实体比例、Shannon 熵、连通分量、构建成功率、综合 health_score P3-15: suggest_cross_references() 基于 Jaccard 实体重叠度推荐相关语料库 🤖 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>
_compute_health_score 将 density 纳入评分(10% 权重),修正原实现中 density 参数被忽略的问题; _sync_triple_to_kg 关系存在性检查增加 is_active 过滤,发现被软删除的关系时执行 reactivation 而非跳过。 🤖 Generated with [Claude Code](https://github.com), [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.
背景
核心变更
ContextAssembler._collect_kg_context()注入 Top-5 高重要性实体 + 1-hop 关系摘要到 Agent 上下文,token 预算 200 上限_sync_triple_to_kg()将 Agent 内化三元组同步到 kg_entities/kg_relations(强化 mention_count + 创建/重建关系),KG 不可用时静默跳过get_stats()扩展孤立率、Shannon 熵、连通分量、构建成功率,综合 health_score (0-100) 六维加权评分suggest_cross_references()基于 Jaccard 实体重叠度推荐相关语料库,KG 不足时降级到文档数排序风险与回滚
_sync_triple_to_kg在 Agent 高频调用路径上,但使用独立 session + 全异常静默,不影响主流程验证证据
影响范围
internalization.py,context_assembler.py,corpus_engine.py,graph_service.pydocs/knowledge-graph.mdRoadmap 更新Next Best Action