AI Data Architecture - 3-Tier Decision Pipeline
🎯 Overview
We've implemented a production-grade 3-tier architecture for AI decisions:
┌─────────────────────────────────────────────────────────────────────────────┐
│ 3-TIER DECISION PIPELINE │
└─────────────────────────────────────────────────────────────────────────────┘
TIER 1: RAW STORAGE (ai-agents.decisions)
┌──────────────────────────────────────┐
│ All MCP decisions │ ← MCP log_decision() writes here
│ - No filtering, fast writes │
│ - 9,951+ documents │
└────────────────────┬─────────────────┘
│ AUTO-EMBED on create ✅ NEW!
▼
TIER 2: SEARCHABLE (ai-memory.agent_embeddings)
┌──────────────────────────────────────┐
│ All decisions + vector embeddings │ ← Enables semantic search
│ - 1536-dimension vectors │
│ - MongoDB Atlas Vector Search │
└────────────────────┬─────────────────┘
│ PROMOTE (confidence > 0.8 + successful) ✅ NEW!
▼
TIER 3: CURATED (ai-agents.golden_decisions)
┌──────────────────────────────────────┐
│ BEST decisions only │ ← Quality-filtered knowledge
│ - Manual or auto-promoted │
│ - Used for training other agents │
│ - Quality score 0-100 │
└──────────────────────────────────────┘
🚨 Issues Fixed
1. Duplicate Collections ✅ FIXED
- Consolidated
agent_decisions → decisions
- Single canonical collection
2. Disconnected Embeddings ✅ FIXED
- Auto-embed on every
log_decision() call
- No more gap between decisions and embeddings
3. MCP Using Keyword-Only Search ✅ FIXED
find_similar now uses HYBRID search (vector + keyword)
- Falls back gracefully if backend unavailable
4. No Quality Filtering ✅ FIXED
- New
golden_decisions collection for curated knowledge
- Auto-promotion based on confidence + outcomes
5. Inconsistent Date Formats ✅ FIXED
- Created shared
date.utils.ts for consistent date handling
- All collections now use the same date field structure
📅 Date Standardization
All MongoDB collections MUST use these consistent date fields:
| Field |
Type |
Purpose |
Format |
timestamp |
Date |
When event occurred |
ISO 8601 (filterable) |
displayDate |
string |
Human-readable |
"Dec 17, 2025, 9:51 AM" |
createdAt |
Date |
When document inserted |
ISO 8601 (auto by Mongoose) |
updatedAt |
Date |
When document modified |
ISO 8601 (auto by Mongoose) |
Usage
import { formatDisplayDate, createTimestampFields } from '../shared/date.utils';
// When creating a document
const doc = {
...data,
timestamp: eventTime ?? new Date(),
displayDate: formatDisplayDate(eventTime ?? new Date()),
createdAt: new Date(),
updatedAt: new Date(),
};
// For updates
const update = {
...changes,
updatedAt: new Date(),
};
Collections Using Standard Dates
| Collection |
Status |
ai-agents.decisions |
✅ Standardized |
ai-memory.agent_embeddings |
✅ Standardized |
ai-memory.golden_embeddings |
✅ Standardized |
ai-agents.golden_decisions |
✅ Standardized |
🔗 Cross-Reference Field: originalDecisionId
All collections now use originalDecisionId for cross-referencing back to the source decision:
| Collection |
Cross-Reference Field |
Points To |
ai-agents.decisions |
_id |
(Primary key) |
ai-agents.golden_decisions |
originalDecisionId |
decisions._id |
ai-memory.agent_embeddings |
originalDecisionId |
decisions._id |
ai-memory.golden_embeddings |
originalDecisionId |
decisions._id |
ai-memory.golden_embeddings |
goldenId |
golden_decisions._id |
How to Cross-Reference
// Find embedding for a decision
const decision = await decisions.findOne({ _id: ObjectId("abc123") });
const embedding = await agent_embeddings.findOne({ originalDecisionId: "abc123" });
// Find if decision was promoted to golden
const golden = await golden_decisions.findOne({ originalDecisionId: "abc123" });
const goldenEmbedding = await golden_embeddings.findOne({ originalDecisionId: "abc123" });
🏛️ New Architecture
Source of Truth Hierarchy
┌─────────────────────────────────────────────────────────────────┐
│ 1. MongoDB (AUTHORITATIVE) │
│ gograbspace.dgajj2t.mongodb.net │
├─────────────────────────────────────────────────────────────────┤
│ ai-agents database (PRIMARY) │
│ ├── decisions ← All MCP decisions │
│ ├── golden_decisions ← Curated high-quality decisions │
│ ├── sessions ← Session metadata │
│ ├── agent_knowledge ← Agent-specific rules │
│ └── platform_patterns ← Cross-project patterns │
│ │
│ ai-archive database (ARCHIVE) │
│ └── archived_decisions ← Auto-archived old decisions │
│ │
│ ai-memory database │
│ └── agent_embeddings ← 5,999 vectors for semantic search │
└─────────────────────────────────────────────────────────────────┘
↓ sync
┌─────────────────────────────────────────────────────────────────┐
│ 2. Local Cache (HOT DATA) │
│ .cursor/ai-memory/ │
├─────────────────────────────────────────────────────────────────┤
│ current-session.json ← Active session decisions (fast) │
│ conventions.json ← Learned conventions cache │
│ working-memory.json ← Quick-access summaries │
│ sessions/session-N.json ← Historical session archives │
└─────────────────────────────────────────────────────────────────┘
↓ backup
┌─────────────────────────────────────────────────────────────────┐
│ 3. Git-Tracked Backup │
│ sessions/ │
├─────────────────────────────────────────────────────────────────┤
│ sessionID.json ← Current session pointer │
│ sessions-history/ ← Human-readable session manifests │
│ complete_session_history.json ← Full history index │
└─────────────────────────────────────────────────────────────────┘
Data Flow
User Action → MCP log_decision()
↓
1. Add to memoryStore.decisions (prepend)
2. POST to backend-ai-api/infrastructure/decisions
↓
3. Backend saves to MongoDB ai-agents.decisions
4. Backend generates embedding → ai-memory.agent_embeddings
↓
5. MCP saves to .cursor/ai-memory/current-session.json
✅ NEW API Endpoints (Implemented)
Vector Search Endpoints
| Endpoint |
Method |
Description |
/ai-api/agents/vector/search |
POST |
Semantic similarity search |
/ai-api/agents/vector/embed |
POST |
Store decision embedding |
/ai-api/agents/vector/stats |
GET |
Vector search statistics |
/ai-api/agents/sync-embeddings |
POST |
Batch sync MCP decisions |
Infrastructure Endpoints (NEW)
| Endpoint |
Method |
Description |
/ai-api/infrastructure/decisions/vector-search |
POST |
Direct vector search |
/ai-api/infrastructure/decisions/hybrid-search |
POST |
Keyword + Vector combined |
/ai-api/infrastructure/embeddings/generate |
POST |
Generate embeddings |
/ai-api/infrastructure/health |
GET |
Health check + stats |
Example: MCP Vector Search
// Call from MCP find_similar tool
const response = await fetch('http://localhost:3001/ai-api/agents/vector/search', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: 'How to handle Angular change detection?',
limit: 5,
minSimilarity: 0.7,
onlySuccessful: true
})
});
const { results } = await response.json();
// Returns semantically similar decisions, not just keyword matches!
Example: Hybrid Search (Best of Both)
// Combines keyword relevance with semantic similarity
const response = await fetch('http://localhost:3001/ai-api/infrastructure/decisions/hybrid-search', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: 'SCSS color.adjust not working',
limit: 10,
vectorWeight: 0.6 // 60% vector, 40% keyword
})
});
🚀 Next Steps for Groundbreaking System
1. ✅ DONE: Enable Semantic Search in Backend
- Added vector search endpoints
- Added hybrid search (keyword + vector combined)
- Added embedding generation endpoint
2. TODO: Update MCP to Use Vector Search
// In MCP find_similar.ts - replace keyword search with API call
async function handleFindSimilar(params: FindSimilarParams) {
const response = await fetch(`${BACKEND_AI_API}/ai-api/agents/vector/search`, {
method: 'POST',
body: JSON.stringify({ query: params.query, limit: params.limit })
});
return response.json();
}
3. Real-Time Sync with MongoDB Change Streams
// Watch for changes from other agents
const changeStream = collection.watch();
changeStream.on('change', (change) => {
if (change.operationType === 'insert') {
memoryStore.decisions.unshift(change.fullDocument);
}
});
4. Create Decision Quality Index
Add MongoDB index for fast quality-based retrieval:
db.decisions.createIndex({
"outcome.wasSuccessful": 1,
"confidence": -1,
"timestamp": -1
});
📊 Collections (Final Architecture)
| Collection |
Database |
Tier |
Purpose |
decisions |
ai-agents |
1 |
All raw decisions |
agent_embeddings |
ai-memory |
2 |
Vector-indexed decisions |
golden_decisions |
ai-agents |
3 |
Curated high-quality decisions |
sessions |
ai-mcp-dev |
- |
Session metadata |
mcp_config |
ai-mcp-dev |
- |
MCP configuration |
core_rules |
ai-mcp-dev |
- |
Learning pyramid rules |
learned_patterns |
ai-mcp-dev |
- |
Learned patterns |
Deprecated (to be removed)
| Collection |
Action |
agent_decisions |
Migrate to decisions, then drop |
🔧 Post-Implementation Checklist
📈 Monitoring
Check Pipeline Health
curl http://localhost:3001/ai-api/infrastructure/pipeline/stats
Returns:
{
"pipeline": {
"tier1_raw": { "count": 9951, "collection": "ai-agents.decisions" },
"tier2_searchable": { "count": 5999, "conversionRate": "60.3%" },
"tier3_curated": { "count": 0, "conversionRate": "0%" }
},
"health": {
"embeddingGap": 3952,
"needsEmbedding": true,
"recommendation": "3952 decisions need embeddings"
}
}
Auto-Promote Golden Decisions
curl -X POST http://localhost:3001/ai-api/infrastructure/decisions/promote \
-H "Content-Type: application/json" \
-d '{"autoPromote": true, "minConfidence": 0.85}'