|
| 1 | +--- |
| 2 | +phase: 07.1-llm-kg-builder-agent |
| 3 | +plan: 02 |
| 4 | +subsystem: agents |
| 5 | +tags: [gemini-pro, adk, knowledge-graph, llm-agent, pipeline, sse] |
| 6 | + |
| 7 | +# Dependency graph |
| 8 | +requires: |
| 9 | + - phase: 07.1-01 |
| 10 | + provides: "Alembic migration + ORM models + KgBuilderOutput Pydantic schema" |
| 11 | + - phase: 07 |
| 12 | + provides: "KgEntity, KgRelationship ORM models, programmatic KG builder, pipeline wiring" |
| 13 | +provides: |
| 14 | + - "KgBuilderAgentRunner subclass with text-only content preparation" |
| 15 | + - "KG_BUILDER_SYSTEM_PROMPT with 8+1 entity taxonomy and semantic relationship instructions" |
| 16 | + - "AgentFactory.create_kg_builder_agent() with Pro model and high thinking" |
| 17 | + - "Input assembly from case_findings + domain entities + case description" |
| 18 | + - "DB write with clear-and-rebuild, lenient parsing, entity degree computation" |
| 19 | + - "Pipeline Stage 7 replaced with LLM KG Builder invocation in try/except" |
| 20 | + - "SSE lifecycle events for KG Builder (started, complete, error)" |
| 21 | +affects: [07.2, 08, 09] |
| 22 | + |
| 23 | +# Tech tracking |
| 24 | +tech-stack: |
| 25 | + added: [] |
| 26 | + patterns: |
| 27 | + - "Text-only DomainAgentRunner subclass (no multimodal files)" |
| 28 | + - "Clear-and-rebuild KG strategy (delete all, insert curated data)" |
| 29 | + - "Lenient LLM output parsing (skip malformed, log warning)" |
| 30 | + - "Non-blocking pipeline stage (try/except, continues on failure)" |
| 31 | + |
| 32 | +key-files: |
| 33 | + created: |
| 34 | + - "backend/app/agents/kg_builder.py" |
| 35 | + - "backend/app/agents/prompts/kg_builder.py" |
| 36 | + modified: |
| 37 | + - "backend/app/agents/factory.py" |
| 38 | + - "backend/app/services/kg_builder.py" |
| 39 | + - "backend/app/services/pipeline.py" |
| 40 | + |
| 41 | +key-decisions: |
| 42 | + - "KG Builder receives text-only input (no multimodal files) -- findings + entities + case description" |
| 43 | + - "Clear-and-rebuild strategy: delete all existing KG data before writing curated data" |
| 44 | + - "KG Builder failure is non-blocking: try/except in pipeline, emits SSE error, continues" |
| 45 | + - "Old programmatic builder functions preserved as deprecated (not deleted)" |
| 46 | + |
| 47 | +patterns-established: |
| 48 | + - "Text-only DomainAgentRunner subclass pattern for agents consuming pre-processed text" |
| 49 | + - "Non-blocking pipeline stage pattern with SSE error emission on failure" |
| 50 | + |
| 51 | +# Metrics |
| 52 | +duration: 5min |
| 53 | +completed: 2026-02-08 |
| 54 | +--- |
| 55 | + |
| 56 | +# Phase 7.1 Plan 02: KG Builder Agent Implementation Summary |
| 57 | + |
| 58 | +**LLM-based KG Builder agent with Gemini Pro, text-only input assembly, clear-and-rebuild DB writer, and non-blocking pipeline integration with SSE lifecycle events** |
| 59 | + |
| 60 | +## Performance |
| 61 | + |
| 62 | +- **Duration:** 5 min |
| 63 | +- **Started:** 2026-02-08T11:19:59Z |
| 64 | +- **Completed:** 2026-02-08T11:25:07Z |
| 65 | +- **Tasks:** 2 |
| 66 | +- **Files modified:** 5 |
| 67 | + |
| 68 | +## Accomplishments |
| 69 | +- KgBuilderAgentRunner subclass following Strategy pattern with text-only content preparation from case findings, domain entities, and case description |
| 70 | +- System prompt with 8+1 entity taxonomy (PERSON, ORGANIZATION, LOCATION, EVENT, ASSET, FINANCIAL_ENTITY, COMMUNICATION, DOCUMENT, OTHER), semantic relationship instructions, deduplication rules, and evidence grounding requirements |
| 71 | +- AgentFactory.create_kg_builder_agent() producing fresh LlmAgent with Pro model, high thinking, and text-only input (no media resolution) |
| 72 | +- DB writer that clears existing KG data, inserts curated entities/relationships with integer-ID-to-UUID mapping, computes entity degrees, and uses lenient parsing (skip malformed, log warning) |
| 73 | +- Pipeline Stage 7 replaced: run_kg_builder() called instead of build_knowledge_graph(), wrapped in try/except for graceful failure, SSE events for started/complete/error |
| 74 | + |
| 75 | +## Task Commits |
| 76 | + |
| 77 | +Each task was committed atomically: |
| 78 | + |
| 79 | +1. **Task 1: KG Builder Agent -- Runner, Factory, Prompt, Input Assembly, DB Write** - `07428c7` (feat) |
| 80 | +2. **Task 2: Pipeline Wiring -- Replace Programmatic KG Builder with LLM Agent** - `618958b` (feat) |
| 81 | + |
| 82 | +## Files Created/Modified |
| 83 | +- `backend/app/agents/kg_builder.py` - KgBuilderAgentRunner, assemble_kg_builder_input(), write_kg_from_llm_output(), run_kg_builder() |
| 84 | +- `backend/app/agents/prompts/kg_builder.py` - KG_BUILDER_SYSTEM_PROMPT with entity taxonomy and relationship instructions |
| 85 | +- `backend/app/agents/factory.py` - Added create_kg_builder_agent() static method |
| 86 | +- `backend/app/services/kg_builder.py` - Marked 4 old functions as deprecated (extract_entities_from_output, build_relationships_from_findings, deduplicate_entities, build_knowledge_graph) |
| 87 | +- `backend/app/services/pipeline.py` - Stage 7 replaced with LLM KG Builder invocation, SSE lifecycle events added |
| 88 | + |
| 89 | +## Decisions Made |
| 90 | +- KG Builder receives text-only input assembled from case_findings (with [FINDING:uuid] prefixes), DomainEntity JSON lists, and case description -- no multimodal files needed since domain agents already processed raw evidence |
| 91 | +- Clear-and-rebuild strategy for KG data: delete all kg_entities and kg_relationships for the case before writing curated data from LLM output |
| 92 | +- Lenient parsing: each entity and relationship insertion wrapped in try/except, malformed items skipped with warning log, partial results preserved |
| 93 | +- Old programmatic builder functions (extract_entities_from_output, build_relationships_from_findings, deduplicate_entities, build_knowledge_graph) marked deprecated but preserved for backward compatibility and audit trail |
| 94 | + |
| 95 | +## Deviations from Plan |
| 96 | + |
| 97 | +None - plan executed exactly as written. |
| 98 | + |
| 99 | +## Issues Encountered |
| 100 | +- Import sorting: ruff pre-commit hook caught unsorted import of run_kg_builder in pipeline.py -- fixed by moving import to alphabetical position among app.agents.* imports |
| 101 | + |
| 102 | +## User Setup Required |
| 103 | +None - no external service configuration required. |
| 104 | + |
| 105 | +## Next Phase Readiness |
| 106 | +- LLM KG Builder agent fully wired into pipeline, ready for end-to-end testing |
| 107 | +- Phase 7.2 (D3.js KG Frontend Enhancement) can proceed -- existing KG API endpoints return curated data without changes |
| 108 | +- Phase 8 (Synthesis) can proceed -- KG Builder produces entities with semantic relationships for synthesis agent consumption |
| 109 | +- No blockers |
| 110 | + |
| 111 | +--- |
| 112 | +*Phase: 07.1-llm-kg-builder-agent* |
| 113 | +*Completed: 2026-02-08* |
0 commit comments