Skip to content

feat(kg): Phase 3 高影响增强 — GraphRAG 上下文集成 + Agent 双向同步 + 健康指标 + 跨语料推荐 - #446

Merged
ThreeFish-AI merged 5 commits into
feature/1.x.xfrom
ThreeFish-AI/bandung-v2
May 2, 2026
Merged

feat(kg): Phase 3 高影响增强 — GraphRAG 上下文集成 + Agent 双向同步 + 健康指标 + 跨语料推荐#446
ThreeFish-AI merged 5 commits into
feature/1.x.xfrom
ThreeFish-AI/bandung-v2

Conversation

@ThreeFish-AI

Copy link
Copy Markdown
Owner

背景

  • 知识图谱 Phase 3 增量交付,补齐四个高影响功能节点,实现 KG 从"构建"到"运营"的闭环
  • 关联文档: docs/knowledge-graph.md Phase 3 Roadmap (P3-12 ~ P3-15)

核心变更

  • P3-12 GraphRAG 上下文集成: ContextAssembler._collect_kg_context() 注入 Top-5 高重要性实体 + 1-hop 关系摘要到 Agent 上下文,token 预算 200 上限
  • P3-13 Agent→KG 双向同步: _sync_triple_to_kg() 将 Agent 内化三元组同步到 kg_entities/kg_relations(强化 mention_count + 创建/重建关系),KG 不可用时静默跳过
  • P3-14 图谱健康指标: get_stats() 扩展孤立率、Shannon 熵、连通分量、构建成功率,综合 health_score (0-100) 六维加权评分
  • P3-15 跨语料推荐: suggest_cross_references() 基于 Jaccard 实体重叠度推荐相关语料库,KG 不足时降级到文档数排序
  • Review 修复: health_score 纳入 density 因子(10% 权重);三元组同步支持被软删除关系的 reactivation

风险与回滚

  • 主要风险: _sync_triple_to_kg 在 Agent 高频调用路径上,但使用独立 session + 全异常静默,不影响主流程
  • 回滚方式: revert 本 PR 即可,无 schema 变更

验证证据

  • 单元测试: ruff lint + format 通过
  • 集成测试: 依赖 CI 流水线验证
  • E2E/Workflow: 无

影响范围

  • 前端: 无
  • 后端: internalization.py, context_assembler.py, corpus_engine.py, graph_service.py
  • GitHub Actions / 文档: docs/knowledge-graph.md Roadmap 更新

Next Best Action

  • Phase 3 后续节点规划(如 Global Search / 图谱可视化)

ThreeFish-AI and others added 5 commits May 2, 2026 12:40
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>
@ThreeFish-AI ThreeFish-AI changed the title feat(kg): Phase 3 四项高影响增强 — GraphRAG 上下文集成 + Agent 双向同步 + 健康指标 + 跨语料推荐 feat(kg): Phase 3 高影响增强 — GraphRAG 上下文集成 + Agent 双向同步 + 健康指标 + 跨语料推荐 May 2, 2026
@ThreeFish-AI
ThreeFish-AI merged commit 45f3b95 into feature/1.x.x May 2, 2026
6 checks passed
@ThreeFish-AI
ThreeFish-AI deleted the ThreeFish-AI/bandung-v2 branch May 2, 2026 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant