Understanding the System โ
A comprehensive technical deep dive into Wake Intelligence's 3-layer temporal brain, architecture patterns, and implementation decisions.
This guide helps developers, contributors, and engineers understand why Wake Intelligence is built the way it is. We'll explore the 3-layer brain architecture, examine key technical decisions, review implementation details, and walk through real challenges we solved.
๐ฏ What You'll Learn โ
- 3-layer brain architecture - Past (causality), Present (memory), Future (propagation)
- Design rationale - Why we chose each approach and what trade-offs we made
- Technical implementation - Real code examples with algorithms
- Testing philosophy - How we achieved 109 comprehensive tests
- Problem-solving - Real challenges (prediction optimization, dependency detection)
- Semantic intent patterns - Observable anchoring and intent preservation
Project Overview โ
What is Wake Intelligence? โ
Wake Intelligence is an MCP server implementing a 3-layer temporal intelligence brain for AI agents: Past (causality tracking), Present (memory management), and Future (predictive pre-fetching).
Core capabilities:
- AI agents learn from history through causal chain tracking
- 109 passing tests demonstrate comprehensive coverage
- Deploys to Cloudflare Workers (global edge computing)
- Reference implementation of semantic intent + hexagonal architecture
Value proposition:
- AI agents remember WHY decisions were made (causality)
- Automatic memory optimization with 4-tier LRU system
- Proactive pre-fetching based on composite prediction scoring
- Production-ready with deterministic, explainable algorithms
Tech stack: TypeScript, Cloudflare Workers, D1 Database, Workers AI, MCP SDK, Vitest
The 3-Layer Temporal Intelligence Brain โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ WAKE INTELLIGENCE BRAIN โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ LAYER 3: PROPAGATION ENGINE (Future - WHAT) โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โข Predicts WHAT will be needed next โ โ
โ โ โข Composite scoring (40% temporal + 30% causal + โ โ
โ โ 30% frequency) โ โ
โ โ โข Pre-fetching optimization โ โ
โ โ โข Pattern-based next access estimation โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โฒ โ
โ LAYER 2: MEMORY MANAGER (Present - HOW) โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โข Tracks HOW relevant contexts are NOW โ โ
โ โ โข 4-tier memory classification โ โ
โ โ (ACTIVE/RECENT/ARCHIVED/EXPIRED) โ โ
โ โ โข LRU tracking + automatic tier updates โ โ
โ โ โข Expired context pruning โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โฒ โ
โ LAYER 1: CAUSALITY ENGINE (Past - WHY) โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โข Tracks WHY contexts were created โ โ
โ โ โข Causal chain tracking โ โ
โ โ โข Dependency auto-detection โ โ
โ โ โข Reasoning reconstruction โ โ
โ โ โข Action type taxonomy โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโWhy 3 Layers? โ
Layer 1: Causality (Past - WHY)
- Tracks WHY contexts were created
- Builds causal chains showing decision history
- Enables reasoning reconstruction
- Example: "Why did I make this architectural decision?"
Layer 2: Memory (Present - HOW)
- Manages HOW relevant contexts are NOW
- 4-tier system based on access recency
- LRU tracking with automatic tier recalculation
- Example: "What contexts are actively being worked on?"
Layer 3: Propagation (Future - WHAT)
- Predicts WHAT will be needed next
- Composite scoring (temporal + causal + frequency)
- Pre-fetching optimization
- Example: "What contexts should we load ahead of time?"
Why this structure?
- Progressive enhancement - Each layer builds on previous
- Temporal completeness - Past informs present, present informs future
- Observable at each layer - No black-box predictions
- Explainable decisions - Every prediction has traceable reasoning
Technical Architecture โ
Hexagonal Architecture (Ports & Adapters) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Presentation Layer (MCPRouter) โ
โ HTTP Request Routing โ
โโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Application Layer โ
โ โข ToolExecutionHandler โ
โ โข MCPProtocolHandler โ
โโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Domain Layer โ
โ โข PropagationService (Layer 3) โ
โ โข MemoryManagerService (Layer 2) โ
โ โข CausalityService (Layer 1) โ
โ โข ContextService (Orchestrator) โ
โ โข ContextSnapshot (Entity) โ
โโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ (Ports: Interfaces)
โโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Infrastructure Layer โ
โ โข D1ContextRepository โ
โ โข CloudflareAIProvider โ
โ โข CORSMiddleware โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโWhy Hexagonal?
- Testability - Domain logic has zero infrastructure dependencies
- Flexibility - Could swap D1 for PostgreSQL by changing only Infrastructure layer
- Maintainability - Clear boundaries, changes localized to specific layers
- Composition root - Only 74 lines (90% reduction from monolithic version)
Key Design Decisions โ
Why 3-Layer Brain vs Traditional Context Management? โ
Decision: Temporal intelligence with Past/Present/Future layers
Rationale:
- Causality (Past) - Understand WHY contexts exist (decision history)
- Memory (Present) - HOW relevant is it NOW (LRU + tiers)
- Propagation (Future) - WHAT will be needed next (predictive)
Trade-off analysis:
- โ Rich temporal understanding
- โ Proactive optimization
- โ Explainable predictions
- โ More complex than simple key-value storage
- โ Additional database columns
Alternative considered: Simple key-value context store
- โ Simpler implementation
- โ No temporal intelligence
- โ No prediction capability
Why Composite Prediction Scoring? โ
Decision: 40% temporal + 30% causal + 30% frequency
Algorithm:
predictionScore =
0.4 * temporalScore + // Recency (exponential decay)
0.3 * causalStrength + // Position in causal chains
0.3 * frequencyScore // Access frequency (log scale)Why these weights?
- 40% temporal - Recency is strongest signal (most recent = most likely next)
- 30% causal - Causal roots often re-accessed (important contexts)
- 30% frequency - High-use contexts likely needed again
Each component explained:
Temporal Score (exponential decay):
const hoursSince = (now - lastAccessed) / 3600000;
const score = Math.exp(-hoursSince / 24); // Half-life of 24 hoursCausal Strength (position in chains):
if (isRoot && hasDependents) return 0.5+; // High importance
if (hasDependents) return 0.3+; // Moderate
return 0.2; // Leaf nodeFrequency Score (logarithmic):
const score = Math.log(accessCount + 1) / Math.log(101);Trade-offs:
- โ Balanced multi-factor prediction
- โ Deterministic (not black-box ML)
- โ Each component is explainable
- โ Weights are heuristic (could be tuned with ML later)
Why 4-Tier Memory System? โ
Decision: ACTIVE (< 1hr) / RECENT (1-24hr) / ARCHIVED (1-30d) / EXPIRED (> 30d)
Implementation:
calculateMemoryTier(lastAccessed: string | null, timestamp: string): MemoryTier {
const referenceTime = lastAccessed || timestamp;
const hoursSince = (now - new Date(referenceTime).getTime()) / 3600000;
if (hoursSince < 1) return MemoryTier.ACTIVE;
if (hoursSince < 24) return MemoryTier.RECENT;
if (hoursSince < 720) return MemoryTier.ARCHIVED; // 30 days
return MemoryTier.EXPIRED;
}Benefits:
- Observable tiers based on time since last access
- Auto-recalculation as contexts age
- Pruning candidates (EXPIRED tier)
- Search prioritization (ACTIVE/RECENT ranked higher)
Trade-offs:
- โ Simple, observable logic
- โ Automatic memory optimization
- โ Prevents database bloat
- โ Time thresholds are fixed (could be configurable)
Why Cloudflare Workers vs Traditional Server? โ
Decision: Deploy to Cloudflare Workers (edge computing)
Rationale:
- Global edge deployment - Low latency worldwide
- Serverless - No servers to manage
- D1 + Workers AI integration - Native Cloudflare ecosystem
- Auto-scaling - Handles traffic spikes
Trade-offs:
- โ Fast (edge-deployed globally)
- โ Scalable (auto-scale)
- โ Cost-effective (pay-per-use)
- โ Platform lock-in (Cloudflare-specific)
- โ Cold start latency (first request)
Implementation Highlights โ
Composition Root (90% Reduction) โ
Location: src/index.ts
What it does: Wires all dependencies in 74 lines (down from 483!)
export default {
async fetch(request: Request, env: Env): Promise<Response> {
// Infrastructure
const repository = new D1ContextRepository(env.DB);
const aiProvider = new CloudflareAIProvider(env.AI);
// Domain services (3-layer brain)
const causalityService = new CausalityService(repository);
const memoryService = new MemoryManagerService(repository);
const propagationService = new PropagationService(repository, causalityService);
// Orchestrator
const contextService = new ContextService(
repository,
aiProvider,
causalityService,
memoryService,
propagationService
);
// Application + Presentation
const toolHandler = new ToolExecutionHandler(contextService);
const protocolHandler = new MCPProtocolHandler(toolHandler);
const router = new MCPRouter(protocolHandler);
return router.handle(request);
}
};Benefits:
- Single source of truth for dependency graph
- 90% reduction from previous monolithic approach
- Explicit dependencies make testing easy
Layer 1: Causality Engine โ
Auto-dependency detection:
async detectDependencies(project: string): Promise<string[]> {
// Find contexts created in last 24 hours
const recent = await this.repository.findRecent(project, 5, 24);
// Auto-detect dependencies from temporal proximity
return recent
.filter(ctx => {
const hoursSince = (Date.now() - new Date(ctx.timestamp).getTime()) / 3600000;
return hoursSince < 1; // Created within last hour
})
.map(ctx => ctx.id);
}Causal chain building:
async buildCausalChain(targetId: string): Promise<ContextSnapshot[]> {
const chain: ContextSnapshot[] = [];
let current = await this.repository.findById(targetId);
while (current.causality?.causedBy) {
chain.unshift(current);
current = await this.repository.findById(current.causality.causedBy);
}
chain.unshift(current); // Add root
return chain;
}Why this matters:
- Temporal proximity heuristic for dependency detection
- Reconstruct decision history for "Why did I do this?"
- Observable causal relationships
Layer 2: Memory Manager โ
LRU tracking:
async trackAccess(contextId: string): Promise<void> {
const context = await this.repository.findById(contextId);
const newTier = this.calculateMemoryTier(new Date().toISOString(), context.timestamp);
await this.repository.updateAccessTracking(contextId, {
lastAccessed: new Date().toISOString(),
accessCount: context.accessCount + 1,
memoryTier: newTier
});
}Why this matters:
- Observable time-based tiers
- Fire-and-forget access tracking (don't block responses)
- Automatic tier recalculation
Layer 3: Propagation Engine โ
Composite scoring:
calculatePropagationScore(context: ContextSnapshot, causalStrength: number): number {
const temporal = this.calculateTemporalScore(context);
const frequency = this.calculateFrequencyScore(context);
return 0.4 * temporal + 0.3 * causalStrength + 0.3 * frequency;
}Temporal score (exponential decay):
private calculateTemporalScore(context: ContextSnapshot): number {
if (!context.lastAccessed) {
// Never accessed - use tier-based default
return context.memoryTier === 'ACTIVE' ? 0.3 :
context.memoryTier === 'RECENT' ? 0.2 :
context.memoryTier === 'ARCHIVED' ? 0.1 : 0.0;
}
const hoursSince = (Date.now() - new Date(context.lastAccessed).getTime()) / 3600000;
return Math.exp(-hoursSince / 24); // Half-life of 24 hours
}Why this matters:
- Explainable predictions (not black-box ML)
- Deterministic algorithm (same inputs = same outputs)
- Composite multi-factor scoring
Testing Strategy โ
Test Distribution โ
Total: 109 tests (all passing โ )
| Layer | Tests | Strategy |
|---|---|---|
| Domain | 20 | Pure logic, no mocks |
| Application | 10 | Mock domain services |
| Infrastructure | 20 | Mock D1/AI |
| Presentation | 12 | HTTP routing tests |
| Integration | 13 | End-to-end flows |
| Specialized Services | 34 | Causality, Context, Memory, Propagation |
Testing Philosophy โ
Domain Layer - No Mocks:
describe('CausalityService', () => {
it('should detect dependencies from temporal proximity', async () => {
const recentContexts = [
{ id: 'ctx-1', timestamp: '2024-01-01T10:00:00Z' },
{ id: 'ctx-2', timestamp: '2024-01-01T10:30:00Z' }
];
const deps = await causalityService.detectDependencies('project-1');
expect(deps).toContain('ctx-2'); // Created within 1 hour
});
});Why no mocks? Pure business logic, no infrastructure dependencies
Infrastructure Layer - Mock External:
describe('CloudflareAIProvider', () => {
it('should use fallback when AI throws error', async () => {
const mockAI = {
run: vi.fn().mockRejectedValue(new Error('AI unavailable'))
};
const provider = new CloudflareAIProvider(mockAI);
const summary = await provider.generateSummary(longContent);
expect(summary).toHaveLength(203); // Truncated to 200 + '...'
});
});Real-World Challenges & Solutions โ
Challenge: Temporal Proximity Dependency Detection โ
Problem: How to auto-detect which contexts are related without explicit user input?
Solution: Temporal proximity heuristic
// Contexts created within 1 hour of each other are likely related
const hoursSince = (now - context.timestamp) / 3600000;
if (hoursSince < 1) {
dependencies.push(context.id);
}Why this works:
- Observable signal (time is measurable)
- Reasonable assumption (recent contexts likely related)
- Simple heuristic (no complex inference)
Trade-offs:
- โ Works without user input
- โ Simple, deterministic
- โ May miss long-running projects
- โ May create false positives
Future improvement: Add semantic similarity (embeddings) to complement temporal proximity
Challenge: Prediction Weight Tuning โ
Problem: How to balance temporal, causal, and frequency scores?
Solution: Start with heuristic weights (40/30/30), plan for meta-learning
Current approach:
const score = 0.4 * temporal + 0.3 * causal + 0.3 * frequency;Rationale:
- Temporal dominant (40%) - Recency is strongest signal
- Causal + Frequency balanced (30% each)
- Simple starting point for validation
Future improvement (Layer 4 concept):
// Could add meta-learning to tune weights
interface PredictionOutcome {
predicted: number;
actuallyAccessed: boolean;
}
// Tune weights based on accuracy
function optimizeWeights(outcomes: PredictionOutcome[]) {
// Gradient descent or similar optimization
}Challenge: Fire-and-Forget Access Tracking โ
Problem: Don't want to slow down context retrieval with access tracking
Solution: Fire-and-forget pattern
async loadContext(project: string): Promise<ContextSnapshot[]> {
const contexts = await repository.findByProject(project);
// Fire-and-forget access tracking (don't await!)
contexts.forEach(ctx => {
memoryManager.trackAccess(ctx.id).catch(err => {
console.error(`Failed to track access for ${ctx.id}:`, err);
});
});
return contexts;
}Why this matters:
- Fast responses (don't block on tracking)
- Best-effort tracking (log errors, continue)
- Acceptable trade-off (tracking is optimization, not critical)
Engineering Principles โ
1. Semantic Intent as Single Source of Truth โ
Every decision is based on meaning, not technical characteristics.
Example:
// โ Bad: Technical characteristic
if (content.length > 1000) { /* summarize */ }
// โ
Good: Semantic intent
if (exceedsHumanReadableSize(content)) {
summary = generateConciseSummary(content);
}2. Observable Property Anchoring โ
All behavior anchored to directly observable semantic markers.
Example (Layer 2 Memory Tiers):
// Observable: Time since last access (measurable, no interpretation)
const hoursSince = (now - lastAccessed) / (1000 * 60 * 60);
// Semantic tiers based on observable time
if (hoursSince < 1) return MemoryTier.ACTIVE;
if (hoursSince < 24) return MemoryTier.RECENT;3. Deterministic Algorithms โ
All predictions use deterministic, explainable algorithms.
Not a black-box ML model - every score component is traceable:
predictionScore =
0.4 * exponentialDecay(hoursSinceAccess) + // Temporal
0.3 * causalChainStrength + // Causal
0.3 * log(accessCount + 1) / log(101) // Frequency4. Progressive Enhancement โ
Each layer builds on the previous, adding intelligence.
Layer 1 (Past) โ Track causality
โ
Layer 2 (Present) โ Manage memory based on access
โ
Layer 3 (Future) โ Predict using causality + memory patternsTechnical Deep Dive: Common Questions โ
Q: How does dependency auto-detection work? โ
A: Temporal proximity heuristic - contexts created within 1 hour are likely related
Algorithm:
async detectDependencies(project: string): Promise<string[]> {
// Find recent contexts (last 24 hours)
const recent = await repository.findRecent(project, limit=5, hours=24);
// Filter by temporal proximity (< 1 hour)
const dependencies = recent
.filter(ctx => {
const hoursSince = (now - ctx.timestamp) / 3600000;
return hoursSince < 1;
})
.map(ctx => ctx.id);
return dependencies;
}Why 1 hour threshold?
- Observable - Time is measurable
- Reasonable assumption - Developer likely working on related tasks
- Simple heuristic - No complex inference needed
Q: How does the 4-tier memory system work? โ
A: Observable time-based classification with automatic recalculation
| Tier | Time Range | Search Priority | Auto-Actions |
|---|---|---|---|
| ACTIVE | < 1 hr | Highest | Top of results |
| RECENT | 1-24 hr | High | Include in searches |
| ARCHIVED | 1-30 days | Low | De-prioritize |
| EXPIRED | > 30 days | Lowest | Pruning candidate |
Automatic tier updates:
async trackAccess(contextId: string): Promise<void> {
const context = await repository.findById(contextId);
const newTier = this.calculateMemoryTier(new Date().toISOString(), context.timestamp);
await repository.update(contextId, {
lastAccessed: new Date().toISOString(),
accessCount: context.accessCount + 1,
memoryTier: newTier // Auto-update tier
});
}Q: Why Cloudflare Workers for edge deployment? โ
A: Global performance and serverless benefits
Benefits:
- Edge deployment - Deployed to 275+ locations worldwide
- Low latency - Runs close to users
- Auto-scaling - Handles traffic automatically
- D1 + Workers AI - Native integration
Trade-offs:
- โ Fast, scalable, cost-effective
- โ Platform-specific (Cloudflare)
- โ Cold start latency on first request
Design for edge constraints:
- Lazy prediction refresh (don't recalculate every time)
- Batch operations where possible
- Stateless design (each request independent)
Learning Resources โ
Key Files to Study โ
Architecture:
- src/index.ts - Composition root (74 lines)
- ARCHITECTURE.md - Complete design docs
Domain Layer (3-layer brain):
- CausalityService.ts - Layer 1
- MemoryManagerService.ts - Layer 2
- PropagationService.ts - Layer 3
Governance:
- SEMANTIC_ANCHORING_GOVERNANCE.md - Pattern rules
Related Projects โ
Part of Semantic Intent ecosystem:
- Semantic Perch Intelligence (PerchIQX) - Database intelligence
- Semantic Foragecast Engine - Multimedia pipeline
All demonstrate similar patterns applied to different domains.
Quick Reference Stats โ
- 109 passing tests (100% pass rate)
- 3-layer brain (Past/Present/Future)
- 4-tier memory (ACTIVE/RECENT/ARCHIVED/EXPIRED)
- 74-line composition root (90% reduction)
- Composite prediction (40% temporal + 30% causal + 30% frequency)
- Edge deployment (Cloudflare Workers, 275+ locations)
- TypeScript 5.8 with strict types
Contributing โ
Understanding the system architecture and design decisions is the first step to contributing effectively.
Next steps:
- Read ARCHITECTURE.md
- Review SEMANTIC_ANCHORING_GOVERNANCE.md
- Study the composition root in src/index.ts
- Run tests:
npm test - Check CONTRIBUTING.md
This guide helps you understand not just what Wake Intelligence does, but why it's built this way and how the engineering decisions were made. ๐ง
