PRD 09: Context Engineering Real Data Integration

Status: Draft Priority: High Effort: 6-8 hours Dependencies: PRD-08 (Document System)


Executive Summary

The Context Engineering page currently displays a mix of mock data, hardcoded values, and incomplete real data. This PRD outlines the work required to connect all Context Engineering features to real data sources, implement proper tracking, and remove all mock/test data.


Current State Analysis

API Endpoints Status

Endpoint
Status
Issues

GET /api/context/stats

✅ Exists

Returns zeros for queries/performance, only embeddings count is real (292)

GET /api/context/performance

✅ Exists

Returns mock time-series data (all zeros)

GET /api/context/sources

✅ Exists

Returns hardcoded mock data ("Technical Docs", "Configuration", etc.)

GET /api/context/patterns

✅ Exists

Returns RAG configs but includes test/XSS junk data

GET /api/context/queries/recent

✅ Exists

Returns empty array (no tracking implemented)

Frontend Components Status

Component
Status
Issues

RAG Context Builder

✅ Working

Already implemented in PRD-08, fully functional!

Performance Charts

⚠️ Partial

Shows charts but data is all zeros

Context Sources Pie Chart

❌ Mock

Hardcoded source distribution

Query Analysis

❌ Empty

No query tracking

Patterns

⚠️ Partial

Shows RAG configs but has junk test data

Optimization

❌ Stub

Just placeholder text


Goals

  1. Remove ALL mock data from Context Engineering endpoints

  2. Implement real-time tracking for context queries

  3. Calculate actual performance metrics from RAG usage

  4. Display real document sources from database

  5. Clean up test/junk data from RAG patterns

  6. Implement optimization recommendations


Detailed Requirements

1. Context Stats Enhancement (2h)

Current:

Required Changes:

Backend: /api/context/stats

Track and aggregate:

  • Total context queries from document_usage table where event_type = 'rag_query'

  • Retrieval success rate = successful RAG queries / total RAG queries

  • Avg response time = average execution_time_ms from tracked queries

  • Vector embeddings = count from document_chunks where embedding IS NOT NULL ✅ (already working)

  • Last query time = MAX(timestamp) from recent RAG queries

SQL Implementation:


2. Context Performance Real-Time Tracking (2h)

Current: Returns mock hourly data with all zeros

Required Changes:

Backend: /api/context/performance

Aggregate real RAG usage by time intervals:

Response Format:


3. Context Sources from Real Documents (1h)

Current: Hardcoded mock data

Required Changes:

Backend: /api/context/sources

Query actual document types from database:

Response Format:


4. Recent Context Queries Tracking (1.5h)

Current: Returns empty array

Required Changes:

Backend Enhancement

Step 1: Update RAG endpoint to track queries Already done in PRD-08! Just need to ensure event_type = 'rag_query' is used.

Step 2: Implement /api/context/queries/recent endpoint


5. Context Patterns Cleanup (30m)

Current: Returns RAG configs but includes test/junk/XSS data

Required Changes:

Backend: /api/context/patterns

Add filtering to exclude test data:

Manual Cleanup:


6. Optimization Tab Implementation (1h)

Current: Just placeholder text

Required Changes:

Backend: /api/context/optimize (NEW)

Analyze system and provide recommendations:

Frontend: Update Optimization Tab

Replace placeholder with:

  • Display recommendations cards

  • Color-coded by type (success=green, warning=yellow, error=red)

  • Action buttons for each recommendation

  • System health indicator


Implementation Plan

Phase 1: Backend Data Sources (3h)

  1. ✅ Update /api/context/stats with real query tracking

  2. ✅ Update /api/context/performance with time-series data

  3. ✅ Update /api/context/sources with real document types

  4. ✅ Implement /api/context/queries/recent with tracking

  5. ✅ Clean up and enhance /api/context/patterns

  6. ✅ Implement new /api/context/optimize endpoint

Phase 2: Database Cleanup (30m)

  1. ✅ Delete test/junk RAG configs

  2. ✅ Verify data integrity

  3. ✅ Add constraints to prevent junk data

Phase 3: Frontend Integration (2h)

  1. ✅ Remove mock data fallbacks from components

  2. ✅ Update chart components to handle real data

  3. ✅ Implement optimization tab UI

  4. ✅ Add loading states and error handling

  5. ✅ Test all tabs with real data

Phase 4: Testing (1h)

  1. ✅ Manual API testing

  2. ✅ UI testing with real data

  3. ✅ Performance testing

  4. ✅ Edge case testing (empty data, errors)


Success Criteria

✅ All endpoints return ONLY real data (no hardcoded/mock values) ✅ Context queries are tracked and displayed in real-time ✅ Performance charts show actual RAG usage patterns ✅ Source distribution reflects actual document types in database ✅ Recent queries tab shows actual search/RAG queries ✅ Patterns tab shows only valid RAG configurations with real usage stats ✅ Optimization tab provides actionable recommendations ✅ No test/junk/XSS data visible in UI


Technical Notes

Database Schema Requirements

The document_usage table must support:

  • event_type values: document_searched, rag_query

  • query field for storing search terms

  • results_count for tracking result size

  • execution_time_ms for performance tracking

  • metadata JSONB for additional context

Already implemented in PRD-08!

Performance Considerations

  • Use indexes on document_usage.event_type and document_usage.timestamp

  • Cache optimization recommendations for 5 minutes

  • Aggregate time-series data in 1-hour buckets (not per-minute)

  • Limit recent queries to 50 maximum


Out of Scope (Future PRDs)

  • Agent-specific query tracking

  • Custom optimization rules

  • Automated performance tuning

  • RAG configuration A/B testing

  • Real-time WebSocket updates for queries

  • Advanced analytics (query clustering, trend analysis)


Comparison: Before vs After

Before

  • Context Stats: 3/6 metrics show zeros

  • Performance Charts: All zeros (mock data)

  • Context Sources: Hardcoded fake sources

  • Recent Queries: Empty

  • Patterns: Polluted with test data

  • Optimization: "Coming soon" placeholder

After

  • Context Stats: All metrics from real usage

  • Performance Charts: Actual hourly RAG usage

  • Context Sources: Real document type distribution

  • Recent Queries: Live search/RAG queries with performance

  • Patterns: Clean RAG configs with usage stats

  • Optimization: Actionable recommendations based on analysis


Estimated Total Effort: 6-8 hours Priority: High (required for MVP demo) Dependencies: PRD-08 document system (completed ✅)

Last updated