Skip to content

Semantic Intent Pattern โ€‹

The Semantic Intent Pattern is a code documentation philosophy used throughout WakeIQX. Every file, class, and function explicitly declares why it exists and what it means in the larger system.

What is Semantic Intent? โ€‹

Traditional code comments explain what the code does. Semantic Intent comments explain why the code exists and what semantic meaning it carries.

typescript
// โŒ Traditional comment: WHAT
// This function saves data to database
async function save(data) { ... }

// โœ… Semantic Intent: WHY
/**
 * ๐ŸŽฏ SEMANTIC INTENT: Persist conversational context for future retrieval
 *
 * PURPOSE: Enable AI agents to resume work across sessions
 *
 * SEMANTIC MEANING:
 * - "Save" = Make context durable and retrievable
 * - "Context" = Conversational state + metadata + causal links
 * - "Session" = Temporal boundary for agent work
 */
async function saveContext(input: SaveContextInput) { ... }

Why Semantic Intent? โ€‹

1. Clarity of Purpose โ€‹

Code should explain why it exists, not just what it does:

typescript
/**
 * ๐ŸŽฏ SEMANTIC INTENT: Track WHY contexts were created
 *
 * PURPOSE: Enable reasoning reconstruction and decision tracing
 *
 * LAYER 1 RESPONSIBILITY:
 * - Build causal chains backwards through time
 * - Identify root causes vs. derived contexts
 * - Classify action types (conversation, decision, file_edit, etc.)
 */
export class CausalityService { ... }

Now readers instantly understand:

  • โœ… This is Layer 1 (temporal focus: past)
  • โœ… It handles causality tracking
  • โœ… It's about understanding "why" decisions were made

2. Semantic Boundaries โ€‹

Each layer/component has clear semantic boundaries:

typescript
/**
 * ๐ŸŽฏ SEMANTIC INTENT: MCP Protocol Message Handler
 *
 * PURPOSE: Handle MCP/JSON-RPC protocol semantics
 *
 * APPLICATION RESPONSIBILITY:
 * - Parse and validate MCP requests
 * - Dispatch to appropriate handlers
 * - Format responses per MCP specification
 * - Maintain protocol compliance
 *
 * NOT RESPONSIBLE FOR:
 * - Business logic (that's domain layer)
 * - Database operations (that's infrastructure)
 * - HTTP routing (that's presentation layer)
 */
export class MCPProtocolHandler { ... }

3. Onboarding & Maintenance โ€‹

New developers can understand the system architecture by reading semantic intent comments:

typescript
/**
 * ๐ŸŽฏ SEMANTIC INTENT: Temporal Intelligence Brain
 *
 * PURPOSE: Orchestrate past/present/future analysis of contexts
 *
 * LAYER COORDINATION:
 * - Layer 1 (CausalityService): WHY was this created? (past)
 * - Layer 2 (MemoryManager): HOW relevant is this? (present)
 * - Layer 3 (PropagationEngine): WHAT will be needed? (future)
 *
 * ORCHESTRATION PATTERN:
 * - Accept high-level requests
 * - Delegate to specialized services
 * - Aggregate results
 * - Return cohesive answer
 */
export class ContextService { ... }

Semantic Intent Template โ€‹

Every file starts with a header comment:

typescript
/**
 * ๐ŸŽฏ SEMANTIC INTENT: [What this component means semantically]
 *
 * PURPOSE: [Why it exists]
 *
 * [LAYER] RESPONSIBILITY:
 * - [Key responsibility 1]
 * - [Key responsibility 2]
 * - [Key responsibility 3]
 *
 * [Optional: Integration notes, design patterns, etc.]
 */

Example: Domain Service โ€‹

typescript
/**
 * ๐ŸŽฏ SEMANTIC INTENT: Predict future context relevance
 *
 * PURPOSE: Enable pre-fetching and intelligent prioritization
 *
 * LAYER 3 RESPONSIBILITY:
 * - Calculate prediction scores (0.0-1.0)
 * - Track propagation reasons (why score is high/low)
 * - Identify high-value contexts for pre-fetch
 * - Monitor prediction staleness
 *
 * TEMPORAL FOCUS: Future - "What will be needed next?"
 *
 * SCORING ALGORITHM:
 * - 40% temporal (recent access patterns)
 * - 30% causal (position in decision tree)
 * - 30% frequency (historical access patterns)
 */
export class PropagationService { ... }

Example: Infrastructure Adapter โ€‹

typescript
/**
 * ๐ŸŽฏ SEMANTIC INTENT: D1 Database Adapter
 *
 * PURPOSE: Translate domain operations to D1 SQL queries
 *
 * INFRASTRUCTURE RESPONSIBILITY:
 * - Implement IContextRepository interface
 * - Map domain entities to/from SQL rows
 * - Handle D1-specific query syntax
 * - Manage database connections
 *
 * HEXAGONAL ARCHITECTURE:
 * - This is an ADAPTER - it translates external (D1) to internal (domain)
 * - Domain services depend on IContextRepository interface
 * - This implementation can be swapped without affecting domain
 */
export class D1ContextRepository implements IContextRepository { ... }

Semantic Anchoring โ€‹

Semantic Intent creates semantic anchors - clear reference points that explain the system:

Anchor 1: Temporal Layers โ€‹

typescript
// Layer 1: Past (WHY)
๐ŸŽฏ SEMANTIC INTENT: Track decision causality
TEMPORAL FOCUS: Understanding what led to current state

// Layer 2: Present (HOW)
๐ŸŽฏ SEMANTIC INTENT: Manage context relevance
TEMPORAL FOCUS: Current accessibility and lifecycle

// Layer 3: Future (WHAT)
๐ŸŽฏ SEMANTIC INTENT: Predict future needs
TEMPORAL FOCUS: Pre-fetching and optimization

Anchor 2: Hexagonal Layers โ€‹

typescript
// Domain: Core business logic
๐ŸŽฏ SEMANTIC INTENT: Pure business rules
DEPENDENCY DIRECTION: Zero external dependencies

// Application: Orchestration
๐ŸŽฏ SEMANTIC INTENT: Coordinate domain operations
DEPENDENCY DIRECTION: Depends on domain (inward)

// Infrastructure: Technical implementation
๐ŸŽฏ SEMANTIC INTENT: Adapt external systems
DEPENDENCY DIRECTION: Implements domain interfaces (inward)

Anchor 3: Observable Operations โ€‹

typescript
/**
 * ๐ŸŽฏ SEMANTIC INTENT: Reconstruct reasoning chain
 *
 * OBSERVABLE OPERATION:
 * - Deterministic: Same input โ†’ same output
 * - Traceable: Full audit trail maintained
 * - Testable: Pure function with no side effects
 *
 * INPUT: snapshotId (which context to explain)
 * OUTPUT: Human-readable reasoning explanation
 */
async reconstructReasoning(snapshotId: string): Promise<string>

Function-Level Semantic Intent โ€‹

Even individual functions declare their semantic purpose:

typescript
/**
 * ๐ŸŽฏ SEMANTIC INTENT: Determine memory tier based on access time
 *
 * PURPOSE: Classify context lifecycle stage
 *
 * TIER SEMANTICS:
 * - ACTIVE (< 1hr): Currently being worked on
 * - RECENT (1-24hr): Recently used, still warm
 * - ARCHIVED (1-30 days): Historical reference
 * - EXPIRED (> 30 days): Pruning candidate
 *
 * @param lastAccessed - When context was last retrieved
 * @param now - Current timestamp
 * @returns Memory tier classification
 */
function calculateMemoryTier(
  lastAccessed: Date | null,
  now: Date
): MemoryTier {
  if (!lastAccessed) return MemoryTier.ARCHIVED;

  const hoursSinceAccess =
    (now.getTime() - lastAccessed.getTime()) / (1000 * 60 * 60);

  if (hoursSinceAccess < 1) return MemoryTier.ACTIVE;
  if (hoursSinceAccess < 24) return MemoryTier.RECENT;
  if (hoursSinceAccess < 24 * 30) return MemoryTier.ARCHIVED;
  return MemoryTier.EXPIRED;
}

Variable Naming with Semantic Intent โ€‹

Variable names should carry semantic meaning:

typescript
// โŒ Bad: Generic names
const data = await repo.find();
const result = calculate(data);
const output = format(result);

// โœ… Good: Semantic names
const contextsWithCausality = await repository.findByProject(project);
const predictionScore = calculatePredictionScore(context);
const highValueContexts = filterByScore(contexts, minScore);

Semantic Prefixes โ€‹

Use prefixes to indicate semantic type:

PrefixMeaningExample
isBoolean predicateisRootCause, isPredictionStale
hasBoolean existencehasChildren, hasCausality
getRetrieve existing datagetMemoryStats, getCausalityStats
calculateCompute new valuecalculatePredictionScore, calculateMemoryTier
buildConstruct complex objectbuildCausalChain, buildReasoningText
updateModify existing dataupdatePredictions, updateAccessTracking
pruneRemove datapruneExpiredContexts

Type Aliases with Semantic Intent โ€‹

Create type aliases that carry meaning:

typescript
// โŒ Bad: Primitive types
type ActionType = string;
type Timestamp = string;

// โœ… Good: Semantic types with documentation
/**
 * ๐ŸŽฏ SEMANTIC INTENT: Classification of what action created a context
 *
 * SEMANTIC MEANING:
 * - conversation: User dialogue or question
 * - decision: Strategic choice or determination
 * - file_edit: Code or document modification
 * - tool_use: External tool invocation
 * - research: Information gathering
 */
type ActionType =
  | 'conversation'
  | 'decision'
  | 'file_edit'
  | 'tool_use'
  | 'research';

/**
 * ๐ŸŽฏ SEMANTIC INTENT: ISO 8601 timestamp representing a point in time
 *
 * FORMAT: "YYYY-MM-DDTHH:mm:ss.sssZ"
 * TIMEZONE: Always UTC
 * USAGE: Event timestamps, access tracking, creation time
 */
type ISO8601Timestamp = string;

Error Messages with Semantic Intent โ€‹

Error messages should explain semantic violations:

typescript
// โŒ Bad: Technical error
throw new Error('Value is null');

// โœ… Good: Semantic error
throw new Error(
  'Cannot reconstruct reasoning: Context has no causality metadata. ' +
  'This context was created without a "causedBy" link or "actionType".'
);

// โœ… Even better: Semantic error with guidance
throw new Error(
  'Cannot build causal chain: Snapshot not found. ' +
  'The context you\'re trying to trace may have been pruned. ' +
  'Try using a more recent context ID or check if the context exists with load_context.'
);

Documentation Consistency โ€‹

Semantic Intent ensures consistent terminology across:

1. Code Comments โ€‹

typescript
/**
 * ๐ŸŽฏ SEMANTIC INTENT: Save conversation context
 */
async saveContext(input: SaveContextInput) { ... }

2. MCP Tool Descriptions โ€‹

typescript
{
  name: "save_context",
  description: "Save conversation context with AI enhancement"
}

3. API Documentation โ€‹

markdown
## save_context

Save conversation context for future retrieval.

**Purpose**: Persist conversational state across sessions

4. User-Facing Messages โ€‹

typescript
return {
  content: [{
    type: "text",
    text: "Context saved! Future conversations can reference this."
  }]
};

All four use the same semantic vocabulary: "save", "context", "conversation", "future".


Real-World Examples โ€‹

Example 1: Layer 1 Service โ€‹

typescript
/**
 * ๐ŸŽฏ SEMANTIC INTENT: Causality Engine (Layer 1)
 *
 * PURPOSE: Track WHY contexts were created through causal relationships
 *
 * LAYER 1 RESPONSIBILITY:
 * - Build causal chains backwards through time
 * - Reconstruct reasoning from causality metadata
 * - Classify action types that created contexts
 * - Provide analytics on decision history
 *
 * TEMPORAL FOCUS: Past - understanding historical decisions
 *
 * OBSERVABLE OPERATIONS:
 * - buildCausalChain: Deterministic chain traversal
 * - reconstructReasoning: Idempotent formatting
 * - getCausalityStats: Aggregate statistics
 */
export class CausalityService {
  /**
   * ๐ŸŽฏ SEMANTIC INTENT: Trace decision history backwards
   *
   * PURPOSE: Show how contexts influenced each other over time
   *
   * ALGORITHM:
   * 1. Start at target snapshot
   * 2. Follow causedBy links backwards
   * 3. Stop at root cause (no parent)
   * 4. Return chain with root at index 0
   *
   * OBSERVABLE PROPERTIES:
   * - Deterministic: Same input โ†’ same chain
   * - Cycle-safe: Detects and prevents infinite loops
   * - Depth-tracked: Each node knows distance from root
   */
  async buildCausalChain(snapshotId: string): Promise<CausalChainNode[]> {
    // Implementation...
  }
}

Example 2: MCP Tool Handler โ€‹

typescript
/**
 * ๐ŸŽฏ SEMANTIC TOOL HANDLER: build_causal_chain
 *
 * PURPOSE: Expose causality chain building via MCP protocol
 *
 * FLOW:
 * - Receive MCP tool call with snapshotId
 * - Delegate to CausalityService.buildCausalChain()
 * - Format result as human-readable markdown
 * - Return MCP ToolResult
 *
 * SEMANTIC TRANSLATION:
 * - MCP args โ†’ Domain input
 * - Domain output โ†’ User-friendly text
 */
private async handleBuildCausalChain(
  args: { snapshotId: string }
): Promise<ToolResult> {
  // Implementation...
}

Best Practices โ€‹

1. Start Every File with Semantic Intent โ€‹

typescript
/**
 * ๐ŸŽฏ SEMANTIC INTENT: [Component name]
 *
 * PURPOSE: [Why it exists]
 *
 * RESPONSIBILITY: [What it does]
 */

2. Use Semantic Vocabulary Consistently โ€‹

Choose terms and stick with them:

  • โœ… "Context" not "data" or "record"
  • โœ… "Snapshot" not "version" or "copy"
  • โœ… "Causality" not "dependency" or "relationship"

3. Explain "Why" Not "What" โ€‹

typescript
// โŒ Bad: Explains what code does
// Loops through contexts and increments counter

// โœ… Good: Explains why it's needed
// Count how many contexts belong to each memory tier
// to provide observability into memory health
typescript
/**
 * HEXAGONAL ARCHITECTURE:
 * - This is DOMAIN layer (no infrastructure dependencies)
 * - Pure business logic, 100% testable
 * - Depends only on IContextRepository interface
 */

Benefits โ€‹

  1. Self-Documenting Code: Architecture is visible in comments
  2. Easier Onboarding: New developers understand intent quickly
  3. Maintenance: Changes respect semantic boundaries
  4. Consistency: Same vocabulary everywhere (code, docs, errors)
  5. Design Clarity: Forces you to articulate purpose

Further Reading โ€‹