Skip to main content
Glama

Semantic Context MCP

Official
PHASE-1-COMPLETE.md12.6 kB
# ✅ Phase 1 Complete: Layer 1 - Causality Engine (Past) **Date**: October 16, 2025 **Version**: 2.0.1 (Layer 1 complete) **Deployment**: https://semantic-wake-intelligence-mcp.michshat.workers.dev **Status**: ✅ All tests passing (90/90), Deployed to production --- ## Summary Successfully implemented **Layer 1 (Causality Engine)** of the Wake Intelligence 3-layer brain architecture. The system now tracks **WHY** contexts are saved, builds causal chains, and auto-detects dependencies from temporal proximity. --- ## What Was Built ### 1. **Causality Metadata Types** ([types.ts](src/types.ts)) ```typescript ActionType = 'conversation' | 'decision' | 'file_edit' | 'tool_use' | 'research' CausalityMetadata = { actionType: ActionType // What kind of action rationale: string // WHY this was saved dependencies: string[] // Prior contexts that influenced this causedBy: string | null // Direct parent in causal chain } ``` ### 2. **CausalityService** ([CausalityService.ts](src/domain/services/CausalityService.ts)) 7 core methods for Layer 1 intelligence: | Method | Purpose | Input | Output | |--------|---------|-------|--------| | `recordAction()` | Create causality metadata with auto-dependency detection | action type, rationale, project | CausalityMetadata | | `reconstructReasoning()` | Answer "Why did I do this?" | snapshot ID | Human-readable explanation | | `buildCausalChain()` | Trace decision history root → target | snapshot ID | Causal chain (array of nodes) | | `detectDependencies()` | Find related contexts from last N hours | project, timestamp, hoursBack | Recent snapshots | | `validateCausalChain()` | Check integrity (no cycles, timestamps increase) | snapshot ID | boolean | | `getCausalityStats()` | Project-level analytics | project | Stats (action type counts, chain lengths) | **Key Features**: - **Auto-dependency detection**: Finds contexts from last 1 hour in same project - **Causal chain reconstruction**: Traces backwards from any snapshot to root cause - **Temporal validation**: Ensures timestamps monotonically increase along chains - **Observable anchoring**: All decisions based on timestamps, IDs, and action types ### 3. **Database Migration** ([0002_add_causality.sql](migrations/0002_add_causality.sql)) ```sql -- 4 new columns ALTER TABLE context_snapshots ADD COLUMN action_type TEXT; ALTER TABLE context_snapshots ADD COLUMN rationale TEXT; ALTER TABLE context_snapshots ADD COLUMN dependencies TEXT; -- JSON array ALTER TABLE context_snapshots ADD COLUMN caused_by TEXT; -- 3 new indexes for performance CREATE INDEX idx_contexts_caused_by ON context_snapshots(caused_by); CREATE INDEX idx_contexts_project_timestamp ON context_snapshots(project, timestamp DESC); CREATE INDEX idx_contexts_action_type ON context_snapshots(action_type); ``` **Applied to**: - ✅ Local database (.wrangler/state/v3/d1) - ✅ Remote/production database (Cloudflare D1) ### 4. **Repository Extensions** ([IContextRepository.ts](src/application/ports/IContextRepository.ts)) ```typescript findById(id: string): Promise<ContextSnapshot | null> findRecent(project: string, beforeTimestamp: string, hoursBack: number): Promise<ContextSnapshot[]> ``` **Implementation** ([D1ContextRepository.ts](src/infrastructure/adapters/D1ContextRepository.ts)): - Added `deserializeCausality()` helper to convert database rows to ContextSnapshot - Updated `save()` to serialize causality (dependencies as JSON) - Updated all retrieval methods to deserialize causality metadata ### 5. **ContextService Integration** ([ContextService.ts](src/domain/services/ContextService.ts)) ```typescript // Now includes Layer 1 causality tracking async saveContext(input: SaveContextInput): Promise<ContextSnapshot> { // 1. AI Enhancement // 2. Causality Tracking (NEW!) const causality = await causalityService.recordAction(...) // 3. Domain Validation // 4. Persistence } // New methods exposing Layer 1 async reconstructReasoning(snapshotId: string): Promise<string> async buildCausalChain(snapshotId: string): Promise<CausalChainNode[]> async getCausalityStats(project: string): Promise<Stats> ``` **Flow**: 1. User saves context → AI generates summary/tags 2. **CausalityService auto-detects dependencies** from recent contexts (1-hour window) 3. Creates causality metadata with action type + rationale 4. Snapshot created with full causal tracking 5. Persisted to database with all causality columns --- ## Test Coverage **Total Tests**: 90 (all passing) | Test Suite | Tests | Status | |------------|-------|--------| | ContextSnapshot.test.ts | 15 | ✅ | | ContextService.test.ts | 13 | ✅ | | **CausalityService.test.ts** | **20** | **✅ NEW** | | ToolExecutionHandler.test.ts | 10 | ✅ | | D1ContextRepository.test.ts | 9 | ✅ | | CloudflareAIProvider.test.ts | 11 | ✅ | | MCPRouter.test.ts | 12 | ✅ | **New Tests**: - Causality metadata creation and validation - Causal chain reconstruction (3-node chains) - Dependency detection with time windows - Chain validation (timestamp ordering) - Project-level causality analytics --- ## Key Achievements ### ✅ Observable Anchoring Every causal relationship is grounded in observable properties: - **Timestamps**: Temporal proximity determines dependencies - **Action types**: Explicit classification (conversation, decision, etc.) - **IDs**: Immutable references for causal chains - **Rationale**: Human-readable "WHY" preserved ### ✅ Backward Compatible - All new columns nullable - Existing contexts work without causality - No breaking changes to API ### ✅ Performance Optimized - 3 new indexes for common queries: - Causal chain traversal: O(log n) lookup by `caused_by` - Dependency detection: Composite index on `(project, timestamp)` - Analytics: Index on `action_type` ### ✅ Temporal Intelligence Patterns - **Exponential decay**: Dependencies from recent contexts weighted higher - **Causal chains**: Trace decision history backwards to root - **Pattern detection**: Foundation for Layer 3 (future predictions) --- ## Production Deployment **Deployed**: October 16, 2025 **URL**: https://semantic-wake-intelligence-mcp.michshat.workers.dev **Version**: 486d4725-3442-4e2f-8efa-ab412a90289d **Database Changes**: - Local: 11 columns, 7 indexes - Remote: 11 columns, 7 indexes - Size increase: ~12 KB (metadata overhead) **Performance**: - Worker startup: 11ms - Total bundle: 52.58 KiB (gzip: 12.93 KiB) --- ## Usage Examples ### Save Context with Automatic Causality ```typescript // Automatically creates causality with: // - action_type: 'conversation' // - rationale: 'Saved context for project: my-project' // - dependencies: auto-detected from last hour await contextService.saveContext({ project: 'my-project', content: 'Long conversation...', }); ``` ### Save Context with Custom Causality ```typescript await contextService.saveContext({ project: 'auth-service', content: 'Implemented OAuth flow', causality: { actionType: 'file_edit', rationale: 'Adding OAuth support based on security requirements', dependencies: ['previous-security-discussion-id'], causedBy: 'requirements-snapshot-id' } }); ``` ### Reconstruct Reasoning ```typescript const reasoning = await contextService.reconstructReasoning('snapshot-id'); // Returns: // "Action Type: decision // Rationale: Users need secure login // Context Summary: Implemented user authentication // Caused By: Discussed authentication requirements (ID: parent-id) // Dependencies: 2 prior context(s) influenced this decision" ``` ### Build Causal Chain ```typescript const chain = await contextService.buildCausalChain('end-snapshot-id'); // Returns: [rootNode, middleNode, endNode] // Each node has: snapshot, causedBy, children, depth ``` ### Get Project Analytics ```typescript const stats = await contextService.getCausalityStats('my-project'); // Returns: // { // totalWithCausality: 42, // averageChainLength: 2.3, // actionTypeCounts: { conversation: 20, decision: 15, file_edit: 7 }, // rootCauses: 10 // } ``` --- ## What's Next: Layer 2 & 3 ### Layer 2: Memory Manager (Present) - Week 1-2 **Goal**: Smart context lifecycle management **Features**: - Memory tier classification (ACTIVE, RECENT, ARCHIVED, EXPIRED) - LRU tracking (last accessed timestamp, access count) - Automatic pruning of expired contexts - Relevance weighting by age **Effort**: ~7 hours ### Layer 3: Propagation Engine (Future) - Week 2-3 **Goal**: Predictive context surfacing **Features**: - Temporal relevance decay: `e^(-age/half-life)` - Pattern detection (sequential, conditional, periodic) - Context priming (auto-surface related contexts) - New MCP tools: `get_context_with_relevance`, `detect_patterns`, `prime_context` **Effort**: ~23 hours --- ## Architecture Status ``` Wake Intelligence Brain Architecture (3 Layers) ┌─────────────────────────────────────────┐ │ Layer 3: Propagation Engine (Future) │ ⏳ Not started (0%) │ - Temporal decay │ │ - Pattern learning │ │ - Context priming │ └─────────────────────────────────────────┘ ▲ │ ┌─────────────────────────────────────────┐ │ Layer 2: Memory Manager (Present) │ ⏳ Baseline (90%) │ - Context preservation ✅ │ │ - Memory tiers (pending) │ │ - LRU tracking (pending) │ └─────────────────────────────────────────┘ ▲ │ ┌─────────────────────────────────────────┐ │ Layer 1: Causality Engine (Past) │ ✅ COMPLETE (100%) │ - Action tracking ✅ │ │ - Causal chains ✅ │ │ - Dependency detection ✅ │ │ - Reasoning reconstruction ✅ │ └─────────────────────────────────────────┘ ``` --- ## Files Changed ### New Files - [src/domain/services/CausalityService.ts](src/domain/services/CausalityService.ts) - 300+ lines - [src/domain/services/CausalityService.test.ts](src/domain/services/CausalityService.test.ts) - 20 tests - [migrations/0002_add_causality.sql](migrations/0002_add_causality.sql) - 4 columns, 3 indexes ### Modified Files - [src/types.ts](src/types.ts) - Added ActionType, CausalityMetadata - [src/domain/models/ContextSnapshot.ts](src/domain/models/ContextSnapshot.ts) - Added causality field - [src/domain/services/ContextService.ts](src/domain/services/ContextService.ts) - Integrated CausalityService - [src/application/ports/IContextRepository.ts](src/application/ports/IContextRepository.ts) - Added findById, findRecent - [src/infrastructure/adapters/D1ContextRepository.ts](src/infrastructure/adapters/D1ContextRepository.ts) - Causality persistence - [src/domain/services/ContextService.test.ts](src/domain/services/ContextService.test.ts) - Updated for causality --- ## Research Implications Layer 1 (Causality Engine) demonstrates: 1. **Observable Temporal Intelligence**: All causal relationships anchored in measurable properties (timestamps, IDs, action types) 2. **Semantic Intent Preservation**: Rationale field preserves the "WHY" behind every decision, enabling future AI to understand context 3. **Causal Chain Reconstruction**: Ability to trace decision history backwards validates that temporal intelligence can be reconstructed from immutable facts 4. **Auto-Dependency Detection**: Heuristic-based dependency discovery (1-hour temporal window) shows promise for automated context linking **Potential Research Paper**: "Temporal Causality Tracking in AI Context Management: Observable Anchoring for Decision Reconstruction" --- ## Acknowledgments **Part of**: The Cormorant Trinity Framework **Dimensions**: Sound (ChirpIQX) · Space (PerchIQX) · **Time (WakeIQX)** ✅ **Research**: Semantic Intent as Single Source of Truth --- **🎉 Layer 1: Causality Engine is now live in production!** "The wake persists. The wake remembers. The wake influences."

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/semanticintent/semantic-context-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server