Files
proffesor-for-testing__agen…/docs/planning/README-knowledge-graph.md
Profa ae528aea7e feat(code-intelligence): implement Wave 1-2 with SQL-based graph architecture
## 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>
2025-12-21 13:49:39 +00:00

16 KiB

Project Knowledge Graph Planning Documents

Complete Strategic Analysis & Implementation Plan

Generated: 2025-12-21 Status: Proposal for Review Effort: 8 weeks, 1-2 engineers Investment: ~$58K


Document Overview

This planning suite provides a comprehensive GOAP (Goal-Oriented Action Planning) analysis for extending RuVector into a full project knowledge graph. Four documents cover strategy, implementation, architecture, and execution:

Document Purpose Audience Size
Executive Summary High-level overview, business case, ROI Leadership, Stakeholders 16KB
GOAP Plan Detailed action plan, state transitions, metrics Technical Leads, Architects 46KB
Implementation Roadmap Week-by-week tasks, milestones, team allocation Project Managers, Engineers 23KB
Architecture Comparison Technology evaluation, trade-offs, decision matrix Architects, Database Engineers 23KB

Total Planning Coverage: 108KB of strategic analysis


Quick Navigation

For Decision Makers (15 min read)

  1. Start with Executive Summary - business value, costs, risks
  2. Review "Recommendation" section (page 10)
  3. Check "Go/No-Go Decision Criteria" (page 9)

For Technical Leads (45 min read)

  1. Read GOAP Plan - action sequences, preconditions, effects
  2. Review Architecture Comparison - PostgreSQL vs Neo4j vs AgentDB
  3. Check "Success Metrics" in GOAP plan

For Implementation Team (2 hour deep dive)

  1. Study Implementation Roadmap - week-by-week tasks
  2. Review Phase 1-4 milestones in detail
  3. Reference GOAP Plan for technical implementation
  4. Bookmark Architecture Comparison for database design

Executive Summary (TL;DR)

The Problem

Current agentic-qe agents use limited code context:

  • Only test patterns vectorized (not actual code)
  • File scanning overhead (read entire files)
  • No relationship modeling (dependencies, test coverage)
  • High LLM costs (processing 35KB when only 1KB relevant)

The Solution

Extend existing RuVector (PostgreSQL + pgvector) to create a comprehensive knowledge graph:

  • Vectorize ALL project data (code, tests, docs)
  • Model semantic relationships (IMPORTS, TESTS, DOCUMENTS)
  • Enable <10ms queries for intelligent context retrieval
  • Reduce LLM costs by 70-81%

The Approach

8-week phased implementation:

  • Week 1-2: Database schema, AST parser, embeddings
  • Week 3-4: Dependency graph, test mapper, doc linker
  • Week 5-6: Full project indexer, query API
  • Week 7-8: Agent integration, CLI commands

The Ask

  • Budget: $58,320
  • Team: 1 core developer (full-time), 0.5 database engineer
  • Timeline: 8 weeks
  • Expected ROI: 80% LLM cost reduction

Key Highlights

GOAP Action Plan (46KB)

Comprehensive planning using Goal-Oriented Action Planning methodology:

Current State:
  {code_vectorized: false, relationships_modeled: false, llm_cost_high: true}

Goal State:
  {code_vectorized: true, relationships_modeled: true, llm_cost_reduced: true}

Generated Plan:
  1. extend_database_schema (enables: entities_storable)
  2. implement_ast_parser (enables: code_extracted)
  3. generate_embeddings (enables: semantic_search)
  4. extract_relationships (enables: graph_traversal)
  5. build_indexing_pipeline (enables: full_project_indexed)
  6. integrate_with_agents (enables: context_optimized)

Coverage:

  • 18 detailed actions with preconditions/effects
  • 4 implementation phases (Foundation, Relationships, Indexing, Integration)
  • 6 GOAP success metrics
  • Trade-off analysis (storage, latency, complexity)
  • Benefits quantification (70-81% LLM savings)

Implementation Roadmap (23KB)

Week-by-week execution plan:

Week Milestone Deliverable Validation
1-2 Foundation Database schema extended, AST parser working Single file → vectorized
3-4 Relationships Dependencies, tests, docs linked Graph queryable via SQL
5-6 Indexing Full project indexed, file watcher active aqe kg index works
7-8 Integration Agents using KG, CLI commands ready 80% cost reduction

Coverage:

  • 15 milestones with task breakdowns
  • Team responsibilities (Core Dev, DB Eng, QE, Tech Writer)
  • Performance targets (indexing, query, storage)
  • Risk register (6 identified risks with mitigations)
  • Deployment strategy (dev → staging → prod)
  • Rollback plan (instant disable via env var)

Architecture Comparison (23KB)

Comprehensive technology evaluation:

Approach Complexity Cost Performance Verdict
PostgreSQL + pgvector Low $0/yr <1ms vector search ✅ RECOMMENDED
Neo4j Graph DB Medium $100K/yr <0.5ms graph traversal ⚠️ Overkill for v1
AgentDB Hybrid Medium $0/yr <0.5ms vectors 🔄 Consider v2
SQLite + In-Memory Low $0/yr >100ms vectors ❌ Doesn't scale

Coverage:

  • Detailed architecture diagrams (4 approaches)
  • Performance characteristics (latency, throughput, storage)
  • Cost analysis (setup, monthly, annual)
  • Migration path (v1 → v2 evolution)
  • SQL vs Cypher query examples
  • Decision matrix (14 criteria evaluated)

Success Metrics

Quantitative Targets (GOAP)

Metric Target Current Status
Indexing Speed <5s per 100 files - ⏳ Pending
Query Latency <10ms - ⏳ Pending
LLM Call Reduction 80%+ 0% ⏳ Pending
Storage Efficiency <500MB per 10K files - ⏳ Pending
Graph Accuracy 95%+ - ⏳ Pending
Agent Adoption >50% 0% ⏳ Pending

Validation Plan

Week 2 (Foundation):

# Single file indexing validation
npx tsx src/code-intelligence/cli-test-parser.ts src/agents/BaseAgent.ts
# Expected: 15+ entities extracted (functions, classes, interfaces)

Week 4 (Relationships):

# Relationship extraction validation
aqe kg stats
# Expected: 1000+ IMPORTS, 500+ TESTS, 200+ DOCUMENTS relationships

Week 6 (Indexing):

# Full project indexing benchmark
time aqe kg index
# Expected: <30s for 500 files, 5000+ entities

Week 8 (Integration):

# Agent context size comparison
aqe generate --use-knowledge-graph UserService
# Expected: 2KB context (vs 10KB without KG) = 80% reduction ✅

Architecture Highlights

Graph Schema

7 Entity Types:

  • Function (parameters, return type, async)
  • Class (abstract, extends, implements)
  • Interface (extends, properties)
  • Module (exports, imports)
  • Variable (type, const/let)
  • Test (test type, assertions)
  • Documentation (JSDoc, Markdown)

9 Relationship Types:

  • IMPORTS (module → module)
  • EXPORTS (module → entity)
  • TESTS (test → function)
  • DOCUMENTS (doc → code)
  • CALLS (function → function)
  • USES (function → variable)
  • IMPLEMENTS (class → interface)
  • EXTENDS (class → class)
  • HAS_METHOD (class → function)

Query Examples

Semantic Code Search:

SELECT ce.*,
  ruvector_cosine_distance(embedding, $1::ruvector) as distance
FROM code_entities ce
WHERE type = 'function'
  AND ruvector_cosine_distance(embedding, $1::ruvector) < 0.4
ORDER BY distance
LIMIT 10;

Impact Analysis (3-hop dependencies):

WITH RECURSIVE deps AS (
  SELECT target_id, 1 AS depth
  FROM entity_relationships
  WHERE source_id = 'UserService' AND relationship_type = 'IMPORTS'
  UNION
  SELECT er.target_id, d.depth + 1
  FROM deps d
  JOIN entity_relationships er ON d.target_id = er.source_id
  WHERE er.relationship_type = 'IMPORTS' AND d.depth < 3
)
SELECT DISTINCT ce.* FROM deps d JOIN code_entities ce ON d.target_id = ce.id;

Test Coverage Check:

SELECT ce.*
FROM code_entities ce
JOIN entity_relationships er ON ce.id = er.source_id
WHERE er.target_id = 'UserService.createUser'
  AND er.relationship_type = 'TESTS'
ORDER BY er.confidence DESC;

Budget Breakdown

Labor Costs

Role Commitment Duration Rate Cost
Core Developer 100% 8 weeks $100/hr $32,000
Database Engineer 50% 4 weeks $100/hr $8,000
QE Engineer 25% 4 weeks $100/hr $4,000
Technical Writer 50% 2 weeks $75/hr $3,000

Subtotal: $47,000

Infrastructure & Contingency

Category Cost
PostgreSQL Hosting (3 months) $500
Embedding API (OpenAI, initial indexing) $100
Contingency (20%) $9,720

Subtotal: $10,320

Total Investment: $58,320

ROI Scenarios

Scenario Usage/Month Annual Savings Payback Period
Low (1K tasks) 1,000 $480 121 years ❌
Medium (10K tasks) 10,000 $4,800 12 years ⚠️
High (100K tasks) 100,000 $48,000 1.2 years ✅

Strategic Value: Platform foundation justifies investment beyond pure ROI.


Risk Management

Top 6 Risks & Mitigations

Risk Probability Impact Mitigation
Initial indexing exceeds 5s per 100 files Medium Medium Parallel processing, batch embeddings
Test mapping accuracy <80% Medium High Multi-strategy validation, ML approach
Agent adoption <50% after 3 months Medium High Better docs, training, success stories
Storage costs exceed budget Low Medium Quantization (halfvec), pruning
Query latency >10ms degrades UX Low High SQL optimization, caching
File watcher misses changes Low Low Health check, polling fallback

Overall Risk Profile: Low-Medium (proven technologies, phased approach)


Technology Stack

Core Components

Component Technology Version Purpose
Vector Database PostgreSQL + pgvector 14+ / 0.5.0+ Entity storage, vector search
AST Parser TypeScript Compiler API 5.0+ Code entity extraction
Embedding Generator OpenAI or sentence-transformers Latest Semantic vectorization
Graph Queries PostgreSQL Recursive CTEs - Relationship traversal
File Watcher chokidar 3.5+ Incremental indexing
CLI Framework Commander.js 11+ User interface

Why This Stack?

✅ PostgreSQL: Already using for RuVector, team expertise, zero new dependencies ✅ pgvector: Proven <1ms search, 192K QPS, HNSW indexing ✅ TypeScript Compiler API: Official AST parser, comprehensive metadata ✅ chokidar: Battle-tested file watcher (used by Webpack, Vite, etc.)


Alternative Approaches (Not Chosen)

Neo4j Graph Database

Why considered: Native graph performance, Cypher query language Why not chosen: $100K/year license, new dependency, learning curve When to reconsider: If graph algorithms needed (PageRank, centrality)

AgentDB Hybrid

Why considered: 150x faster vector search, quantization Why not chosen: Sync complexity, two databases to manage When to reconsider: If vector search becomes bottleneck (>10ms p99)

SQLite In-Memory

Why considered: Zero setup, portable Why not chosen: No vector search, doesn't scale, brute-force similarity When to reconsider: Never (insufficient for production)


Next Steps

Immediate Actions (Week 0)

  • Stakeholder Review: Share Executive Summary with leadership
  • Budget Approval: Finance sign-off on $58K investment
  • Team Allocation: Assign core developer (100%), database engineer (50%)
  • Sprint Planning: Create Jira epic with 15 milestones
  • Kickoff Meeting: Schedule for Week 1 Monday

Week 1 Kickoff

  • Environment Setup: Verify RuVector PostgreSQL running
  • Schema Design: Review and approve database migration script
  • AST Parser Spike: 2-day prototype on sample TypeScript file
  • Embedding Service: Test OpenAI API integration
  • Daily Standups: 15-min syncs at 9am

Month 1 Review (End of Week 4)

  • Phase 1-2 Complete: Database schema, AST parser, relationships
  • Demo: Show dependency graph for agentic-qe codebase
  • Metrics Review: Indexing speed, relationship accuracy
  • Go/No-Go: Decide on Phase 3-4 continuation

Post-Launch (Week 9+)

  • Gradual Rollout: 10% → 50% → 100% of users
  • Monitor Metrics: LLM cost reduction, query latency, adoption
  • Gather Feedback: Agent developer surveys, usage analytics
  • Plan v2: AgentDB hybrid or Neo4j if bottlenecks emerge

Supporting Documentation

External References


FAQ

Q: Why extend RuVector instead of a separate system? A: Leverages existing infrastructure, team expertise, zero new dependencies. Faster time-to-market (8 weeks vs. 12+).

Q: What if we want to change embedding models later? A: Embeddings are frozen (768-dim). Reindexing takes ~8 hours for full codebase. Rarely needed.

Q: How does this differ from GitHub Copilot? A: GitHub Copilot is a code completion tool. This is a code intelligence platform for agents, enabling semantic search, dependency analysis, and test mapping.

Q: Can we start small and scale up? A: Yes. Phased approach allows stopping after any phase. Week 2 delivers single-file indexing, Week 4 adds relationships, etc.

Q: What if PostgreSQL becomes a bottleneck? A: Unlikely (192K QPS proven). If needed, read replicas scale queries horizontally. AgentDB hybrid available for v2.

Q: Is this GDPR/compliance friendly? A: Yes. All data in your PostgreSQL instance. Embeddings generated locally or via OpenAI with DPA. No third-party storage.


Document Maintenance

Document Last Updated Next Review Owner
Executive Summary 2025-12-21 After Phase 1 Product Lead
GOAP Plan 2025-12-21 After Phase 2 Tech Lead
Implementation Roadmap 2025-12-21 Weekly during implementation Project Manager
Architecture Comparison 2025-12-21 Before v2 planning Architect

Change Management: All updates require approval from project lead. Major changes trigger stakeholder review.


Approval Sign-off

Role Name Decision Date Signature
CTO - ⏳ Pending - -
VP Engineering - ⏳ Pending - -
Head of Product - ⏳ Pending - -
Finance Lead - ⏳ Pending - -

Decision Deadline: 2025-12-28 Next Steps After Approval: Team allocation, sprint planning, Week 1 kickoff


Contact

Project Lead: TBD Technical Lead: TBD Questions: Create GitHub Discussion


Generated by: GOAP Planning Specialist Planning Methodology: Goal-Oriented Action Planning (GOAP) Framework: Agentic Quality Engineering Fleet v2.5.10 Total Planning Effort: 108KB of strategic analysis Confidence Level: High (proven technologies, phased approach, clear metrics)

Let's build the future of code intelligence! 🚀