## Wave 1: Database Foundation - Add SQL schema for code_chunks, code_entities, entity_relationships - Use RuVector 768-dim embeddings (not pgvector) - Implement hybrid_code_search function (BM25 + vector) - Add pg_trgm extension for full-text search - Create setup scripts for Ollama/nomic-embed-text ## Wave 2: Multi-Language Parser - Implement TreeSitterParser with 5 language support - Add language-specific extractors (TS, Python, Go, Rust, JS) - Implement incremental parsing with tree caching - Create comprehensive test suite with fixtures ## Architecture Decision - Defer Apache AGE to v2 (see ADR-002) - Use SQL tables for graph relationships - Rationale: Lower complexity, team familiarity, sufficient for v1 Closes #159, Closes #160 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
22 KiB
Knowledge Graph Implementation Roadmap
Phased Rollout Strategy
Document Version: 1.0.0 Target Release: Q1 2026 Complexity: High-Impact, Medium-Risk
Visual Architecture
┌────────────────────────────────────────────────────────────────────┐
│ PROJECT KNOWLEDGE GRAPH │
│ PostgreSQL + pgvector Backend │
└────────────────────────────────────────────────────────────────────┘
│
┌─────────────────┴─────────────────┐
│ │
┌───────────────▼────────────────┐ ┌──────────────▼──────────────┐
│ CODE ENTITIES (Vectors) │ │ RELATIONSHIPS (Edges) │
├────────────────────────────────┤ ├─────────────────────────────┤
│ • Functions (768-dim vectors) │ │ • IMPORTS (module → module) │
│ • Classes (semantic embeddings)│ │ • TESTS (test → function) │
│ • Modules (aggregated context) │ │ • DOCUMENTS (doc → code) │
│ • Variables (usage patterns) │ │ • IMPLEMENTS (code → iface) │
│ • Interfaces (contract vectors)│ │ • CALLS (func → func) │
└────────────────────────────────┘ └─────────────────────────────┘
│ │
└─────────────────┬─────────────────┘
│
┌─────────────▼─────────────┐
│ QUERY INTERFACES │
├───────────────────────────┤
│ • Semantic Search (HNSW) │
│ • Graph Traversal (CTE) │
│ • Impact Analysis (BFS) │
│ • Context Building (LLM) │
└───────────────────────────┘
│
┌─────────────▼─────────────┐
│ INTEGRATION LAYER │
├───────────────────────────┤
│ • BaseAgent Mixin │
│ • CLI Commands (aqe kg) │
│ • File Watcher (chokidar) │
│ • MCP Tools (91 tools) │
└───────────────────────────┘
Phase 0: Preparation (Week 0)
Pre-Flight Checklist
Goal: Validate infrastructure readiness
-
RuVector Health Check
aqe ruvector status # Expected: Connected, Healthy, Extension Loaded -
PostgreSQL Performance Baseline
-- Current pattern search latency SELECT AVG(ruvector_cosine_distance(embedding, $1)) FROM qe_patterns; -- Target: <1ms -
Storage Capacity Planning
# Estimate entities: src files * avg 10 entities/file find src -name "*.ts" | wc -l # Example: 500 files * 10 = 5000 entities * 3.6KB = ~18MB -
TypeScript Compiler API Test
// Verify AST parsing works import * as ts from 'typescript'; const program = ts.createProgram(['test.ts'], {}); const sourceFile = program.getSourceFile('test.ts'); console.log(sourceFile?.statements.length); // Should print statement count
Deliverable: Go/No-Go decision document
Phase 1: Foundation (Week 1-2)
Milestone 1.1: Database Schema Extension
Owner: Database/Backend Engineer Effort: 2 days Dependencies: PostgreSQL admin access
Tasks:
- Create migration script:
migrations/001_knowledge_graph_schema.sql - Add
code_entitiestable with pgvector column - Add
entity_relationshipstable with indexes - Test schema with sample data
- Document schema in
docs/database/knowledge-graph-schema.md
Validation:
-- Verify tables exist
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name IN ('code_entities', 'entity_relationships');
-- Verify indexes
SELECT indexname FROM pg_indexes
WHERE tablename = 'code_entities';
-- Expected: idx_entities_type, idx_entities_file, idx_entities_embedding
Rollback Plan: Drop tables if needed (data loss acceptable in dev)
Milestone 1.2: AST Parser Service
Owner: Core Developer Effort: 3 days Dependencies: TypeScript compiler API knowledge
Tasks:
- Create
src/code-intelligence/ASTParser.ts - Implement function extraction with JSDoc
- Implement class extraction with members
- Implement interface extraction
- Implement import/export extraction
- Write unit tests (>80% coverage)
Validation:
# Test on sample file
npx tsx src/code-intelligence/cli-test-parser.ts src/agents/BaseAgent.ts
# Expected output:
# Found 15 entities:
# - class BaseAgent (line 50-200)
# - function execute (line 75-100)
# - interface AgentConfig (line 10-25)
Risk Mitigation:
- Use proven TypeScript compiler API patterns
- Handle edge cases (arrow functions, default exports)
- Graceful degradation for unparseable files
Milestone 1.3: Embedding Generator
Owner: ML/Vector Engineer Effort: 2 days Dependencies: Embedding API access
Tasks:
- Create
src/code-intelligence/EmbeddingGenerator.ts - Integrate with sentence-transformers or OpenAI embeddings
- Implement semantic text builder (code + docs + signature)
- Add caching layer (Map or Redis)
- Benchmark embedding quality
Validation:
// Test semantic similarity
const func1 = "function calculateTax(income: number): number { return income * 0.2; }";
const func2 = "function computeTax(salary: number): number { return salary * 0.2; }";
const emb1 = await embedder.generateEmbedding({ content: func1, ... });
const emb2 = await embedder.generateEmbedding({ content: func2, ... });
const similarity = cosineSimilarity(emb1, emb2);
console.log(similarity); // Expected: >0.9 (very similar semantically)
Embedding Options:
| Provider | Dimensions | Cost/1M | Latency | Quality |
|---|---|---|---|---|
OpenAI text-embedding-3-small |
768 | $0.02 | ~50ms | ⭐⭐⭐⭐ |
| Sentence-Transformers (local) | 768 | Free | ~10ms | ⭐⭐⭐ |
| Anthropic (if available) | 768 | TBD | TBD | ⭐⭐⭐⭐⭐ |
Recommendation: Start with sentence-transformers (local, free), migrate to OpenAI if quality issues.
Phase 2: Relationship Extraction (Week 3-4)
Milestone 2.1: Dependency Graph Builder
Owner: Graph Engineer Effort: 3 days Dependencies: AST Parser complete
Tasks:
- Create
src/code-intelligence/DependencyGraphBuilder.ts - Extract
importstatements →IMPORTSrelationships - Extract
exportstatements →EXPORTSrelationships - Resolve module paths (handle aliases, node_modules)
- Handle circular dependencies gracefully
Validation:
// Test on real project
const deps = await depBuilder.buildDependencyGraph(allEntities);
// Verify UserService imports Database
const userServiceDeps = deps.filter(r =>
r.sourceId.includes('UserService') && r.type === 'IMPORTS'
);
console.log(userServiceDeps);
// Expected: [{sourceId: 'UserService', targetId: 'Database', type: 'IMPORTS'}]
Edge Cases:
- Dynamic imports (
import(...)) - Re-exports (
export * from './module') - Type-only imports (
import type { ... })
Milestone 2.2: Test Coverage Mapper
Owner: QE/Testing Engineer Effort: 4 days Dependencies: Dependency Graph Builder
Tasks:
- Create
src/code-intelligence/TestCoverageMapper.ts - Implement name-based matching (80% accuracy)
- Implement import-based matching (90% accuracy)
- Implement semantic similarity matching (70% accuracy, high recall)
- Combine strategies with weighted confidence scores
- Validate against known test-to-code mappings
Validation:
// Test on agentic-qe-cf project
const testMappings = await testMapper.mapTestsToCode(testEntities, codeEntities);
// Find tests for RuVectorPostgresAdapter
const adapterTests = testMappings.filter(r =>
r.targetId.includes('RuVectorPostgresAdapter') && r.type === 'TESTS'
);
console.log(adapterTests);
// Expected: 3 test files found with confidence >0.8
Benchmark Dataset:
- Use existing agentic-qe-cf tests (543 files)
- Manual validation on 50 random files
- Precision/Recall targets: P>90%, R>85%
Milestone 2.3: Documentation Linker
Owner: Documentation Engineer Effort: 2 days Dependencies: AST Parser
Tasks:
- Create
src/code-intelligence/DocumentationLinker.ts - Extract JSDoc comments → link to functions/classes
- Parse Markdown files → extract code references
- Link README mentions to code entities
- Detect stale documentation (code changed, doc didn't)
Validation:
// Test JSDoc linking
const jsdocLinks = await docLinker.linkDocumentation(docEntities, codeEntities);
// Find docs for BaseAgent
const agentDocs = jsdocLinks.filter(r =>
r.targetId.includes('BaseAgent') && r.type === 'DOCUMENTS'
);
console.log(agentDocs);
// Expected: JSDoc comment above BaseAgent class linked
Phase 3: Indexing Pipeline (Week 5-6)
Milestone 3.1: Full Project Indexer
Owner: Performance Engineer Effort: 4 days Dependencies: All extractors complete
Tasks:
- Create
src/code-intelligence/IncrementalIndexer.ts - Implement parallel file processing (10 workers)
- Implement batch embedding generation (100/batch)
- Optimize database inserts (use
COPYfor bulk) - Add progress bar (show files/sec, ETA)
- Handle errors gracefully (skip unparseable files)
Validation:
# Index agentic-qe-cf project
time aqe kg index
# Target performance:
# - 500 TypeScript files
# - ~5000 entities
# - <30 seconds total
# - ~167 entities/sec (meets <5s per 100 files target)
Optimization Techniques:
- Worker threads for AST parsing
- Batch PostgreSQL inserts (1000 rows at a time)
- Connection pooling (10 concurrent connections)
- Embedding cache (avoid re-embedding unchanged code)
Milestone 3.2: File Watcher Integration
Owner: DevOps Engineer Effort: 2 days Dependencies: Indexer working
Tasks:
- Integrate
chokidarfor file watching - Implement incremental reindexing (single file)
- Debounce rapid changes (e.g., during mass refactor)
- Handle file deletions (cascade delete entities)
- Handle file renames (update file_path references)
Validation:
# Start watcher
aqe kg index --watch &
# In another terminal, modify file
echo "// New comment" >> src/agents/BaseAgent.ts
# Expected: Watcher detects change, reindexes in <1s
# Output: "✅ Reindexed BaseAgent.ts in 847ms"
Debouncing Strategy:
- Wait 500ms after last change before reindexing
- Batch multiple file changes into single reindex
- Skip
.gitdirectory,node_modules,dist
Milestone 3.3: Query API Implementation
Owner: API Engineer Effort: 3 days Dependencies: Database populated
Tasks:
- Create
src/code-intelligence/KnowledgeGraphQueryAPI.ts - Implement semantic search (vector similarity)
- Implement graph traversal (1-hop, n-hop)
- Implement impact analysis (recursive CTE)
- Implement context builder for agents
- Optimize SQL queries (use EXPLAIN ANALYZE)
Validation:
// Test semantic search
const results = await api.findSimilarCode("database connection pooling");
console.log(results[0]);
// Expected: RuVectorPostgresAdapter.pool (high similarity)
// Test graph traversal
const deps = await api.findDependencies('UserService');
console.log(deps.length);
// Expected: >5 dependencies (Database, Logger, etc.)
// Test impact analysis
const impact = await api.analyzeImpact('BaseAgent', 2);
console.log(impact.directDependents.length);
// Expected: >20 agents that extend BaseAgent
Performance Targets:
- Semantic search: <10ms
- Graph traversal (1-hop): <5ms
- Impact analysis (3-hop): <50ms
Phase 4: Integration (Week 7-8)
Milestone 4.1: Agent Integration
Owner: Agent Framework Lead Effort: 3 days Dependencies: Query API ready
Tasks:
- Create
src/agents/KnowledgeGraphContextBuilder.ts - Add mixin to
BaseAgentfor knowledge graph access - Implement intelligent context retrieval
- Add fallback for when knowledge graph disabled
- Measure LLM token reduction
- Update agent documentation
Validation:
// Enable knowledge graph
process.env.AQE_KNOWLEDGE_GRAPH_ENABLED = 'true';
// Execute agent with KG
const agent = new TestGeneratorAgent();
const result = await agent.execute('Generate tests for UserService.createUser');
// Check context size
console.log(result.contextTokens);
// Expected: ~2K tokens (vs ~10K without KG)
// LLM cost savings: 80% ✅
Integration Points:
| Agent | KG Usage | Expected Benefit |
|---|---|---|
| TestGeneratorAgent | Find similar tests | Reuse test structure |
| CoverageAnalyzerAgent | Find uncovered code | Semantic gap detection |
| SecurityScannerAgent | Find vulnerable patterns | Cross-file vulnerability tracking |
| FlakyTestHunterAgent | Find code-test links | Correlate code changes with flakiness |
Milestone 4.2: CLI Commands
Owner: CLI Engineer Effort: 2 days Dependencies: All APIs ready
Tasks:
- Add
aqe kgcommand group - Implement
aqe kg index(with --watch, --force) - Implement
aqe kg query <natural-language> - Implement
aqe kg stats(entity counts, relationships) - Implement
aqe kg impact <file-path> - Write CLI documentation
Validation:
# Test all commands
aqe kg index
aqe kg stats
aqe kg query "find database connection handling"
aqe kg impact src/providers/RuVectorClient.ts
# Expected: All commands succeed with useful output
CLI UX Requirements:
- Progress indicators for long operations
- Colorized output (success=green, warning=yellow, error=red)
- JSON output mode (
--json) for scripting - Helpful error messages with troubleshooting steps
Milestone 4.3: Documentation & Examples
Owner: Technical Writer Effort: 2 days Dependencies: All features complete
Tasks:
- Write
docs/guides/knowledge-graph-user-guide.md - Create examples:
examples/knowledge-graph-queries.ts - Update
README.mdwith knowledge graph section - Create video tutorial (5 min demo)
- Add troubleshooting guide
Documentation Outline:
# Knowledge Graph User Guide
## Quick Start
- Installation
- Initial indexing
- Basic queries
## Concepts
- Code entities
- Relationships
- Semantic search
## Advanced Usage
- Impact analysis
- Agent integration
- Custom queries
## Troubleshooting
- Indexing errors
- Query performance
- Storage optimization
Post-Launch (Week 9+)
Continuous Improvement
Monitoring:
- Track LLM cost savings (target: 70-81%)
- Monitor query latency (target: <10ms p50)
- Measure agent satisfaction (qualitative feedback)
- Track adoption rate (% of agents using KG)
Optimization Opportunities:
-
Embedding Model Fine-Tuning
- Collect user feedback on search quality
- Fine-tune sentence-transformers on code corpus
- Target: +10% search accuracy
-
Relationship Quality Improvement
- Machine learning for test-to-code mapping
- Graph embedding for improved link prediction
- Target: 95%+ precision/recall
-
Storage Optimization
- Quantize embeddings to 256-dim
- Use halfvec (float16) for 50% reduction
- Prune stale entities (not accessed in 6 months)
-
Advanced Features
- Code clone detection (high similarity search)
- API migration assistance (find all usages)
- Dead code detection (no incoming relationships)
- Architectural drift analysis (expected vs actual deps)
Risk Register
| Risk ID | Description | Probability | Impact | Mitigation | Owner |
|---|---|---|---|---|---|
| R1 | Initial indexing exceeds 5s per 100 files | Medium | Medium | Optimize parallelization, use faster embedding API | Performance Engineer |
| R2 | Test-to-code mapping accuracy <80% | Medium | High | Implement ML-based approach, collect training data | QE Engineer |
| R3 | Storage costs exceed budget (>1GB per 10K files) | Low | Medium | Use quantization, prune old entities | Database Engineer |
| R4 | Agent adoption <50% after 3 months | Medium | High | Improve documentation, add success stories, training | Product Manager |
| R5 | Query latency >10ms degrades UX | Low | High | Optimize SQL queries, add query caching | API Engineer |
| R6 | File watcher misses changes in edge cases | Low | Low | Add health check, fallback to polling | DevOps Engineer |
Success Metrics Dashboard
Week 1-2 (Foundation)
- Database schema created
- AST parser extracts >90% of entities
- Embedding generation <100ms per entity
Week 3-4 (Relationships)
- Dependency graph 100% accurate
- Test coverage mapper >80% accuracy
- Documentation links created
Week 5-6 (Indexing)
- Full project indexed in <30s
- File watcher updates in <1s
- Query API latency <10ms
Week 7-8 (Integration)
- Agent context size reduced by 80%
- CLI commands functional
- Documentation complete
Post-Launch (Week 9+)
- 70-81% LLM cost reduction achieved
- >50% agent adoption
- <1% error rate
Deployment Strategy
Development (Week 1-6)
# Local development with Docker
docker run -d ruvnet/ruvector:latest
export AQE_KNOWLEDGE_GRAPH_ENABLED=true
export AQE_KNOWLEDGE_GRAPH_DEV=true # Enable debug logging
# Run indexer
aqe kg index --watch
# Test queries
aqe kg query "database connection pooling"
Staging (Week 7)
# Deploy to staging environment
kubectl apply -f k8s/knowledge-graph-staging.yaml
# Index production-like codebase
aqe kg index --environment staging
# Run acceptance tests
npm run test:kg:acceptance
Production (Week 8)
# Gradual rollout
# Phase 1: 10% of users
export AQE_KNOWLEDGE_GRAPH_ROLLOUT_PERCENT=10
# Phase 2: 50% after 3 days
export AQE_KNOWLEDGE_GRAPH_ROLLOUT_PERCENT=50
# Phase 3: 100% after 1 week
export AQE_KNOWLEDGE_GRAPH_ROLLOUT_PERCENT=100
Rollback Plan
If Critical Issues Arise
Immediate Rollback (within 1 hour):
# Disable knowledge graph globally
export AQE_KNOWLEDGE_GRAPH_ENABLED=false
# Agents automatically fallback to traditional context building
# No data loss, immediate recovery
Partial Rollback (specific agents):
// In agent code
class ProblematicAgent extends BaseAgent {
constructor() {
super();
this.knowledgeGraphEnabled = false; // Override for this agent only
}
}
Data Preservation:
- Knowledge graph tables remain intact
- Can re-enable at any time
- No migration needed for rollback
Team Responsibilities
| Role | Responsibilities | Time Commitment |
|---|---|---|
| Project Lead | Overall coordination, stakeholder communication | 25% (10h/week) |
| Database Engineer | Schema design, query optimization | 75% (30h/week, Week 1-3) |
| Core Developer | AST parsing, embedding generation | 100% (40h/week, Week 1-4) |
| Graph Engineer | Relationship extraction, graph algorithms | 75% (30h/week, Week 3-5) |
| Performance Engineer | Indexing optimization, benchmarking | 75% (30h/week, Week 5-6) |
| Agent Framework Lead | Agent integration, context building | 100% (40h/week, Week 7-8) |
| QE Engineer | Test coverage mapping, validation | 50% (20h/week, Week 3-4) |
| Technical Writer | Documentation, examples | 50% (20h/week, Week 7-8) |
Total Effort: ~8 person-weeks
Budget Estimate
| Category | Cost | Notes |
|---|---|---|
| Development | $40,000 | 2 engineers @ $100/h * 200h |
| Infrastructure | $500 | PostgreSQL hosting (3 months) |
| Embedding API | $100 | OpenAI embeddings (initial indexing) |
| Testing/QA | $5,000 | QA engineer time |
| Documentation | $3,000 | Technical writer |
| Contingency (20%) | $9,720 | Risk buffer |
| Total | $58,320 | 8-week project |
ROI Analysis:
- LLM cost savings: $1,000/month (70% reduction)
- Payback period: ~58 months
- Long-term value: Code intelligence platform foundation
Conclusion
This roadmap provides a detailed, week-by-week plan for implementing the project knowledge graph. The phased approach ensures:
- Incremental Value: Each phase delivers usable functionality
- Risk Mitigation: Early validation catches issues before full rollout
- Team Coordination: Clear responsibilities and dependencies
- Measurable Progress: Success metrics at each milestone
- Flexibility: Can pause/adjust based on learnings
Recommended Next Steps:
- ✅ Review roadmap with stakeholders
- ✅ Allocate team resources
- ✅ Create sprint boards (Jira/GitHub Projects)
- ✅ Kick off Week 0 preparation
- ✅ Schedule weekly syncs
Let's build the future of code intelligence! 🚀
Document Metadata:
- Version: 1.0.0
- Last Updated: 2025-12-21
- Next Review: After Phase 1 completion
- Approvers: Project Lead, CTO, Head of Engineering