🧠 Pattern Oracle

AI-assisted code generation with historical patterns.


Overview

The Pattern Oracle leverages the Temporal AI system (SDS-015) to provide similarity-based recommendations during code generation, helping developers and AI agents learn from historical patterns.


Architecture

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Generation       β”‚ β†’  β”‚ Pattern Oracle   β”‚
β”‚ Planner          β”‚    β”‚ (Temporal AI)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚
                              β–Ό
                        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                        β”‚  Vector Store    β”‚
                        β”‚  (redb + HNSW)   β”‚
                        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              β”‚
                              β–Ό
                        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                        β”‚ Pattern          β”‚
                        β”‚ Recommendations  β”‚
                        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Commands

1
2
3
4
5
6
7
8
9
10
11
# Initialize the pattern store
just temporal-ai-init

# Query for similar patterns
just temporal-ai-query "payment processing service" 5

# View statistics
just temporal-ai-stats

# Refresh metrics from telemetry
just temporal-ai-refresh-metrics

Querying Patterns

Request Interface

1
2
3
4
5
6
7
interface PatternRecommendationRequest {
  generation_intent: string;    // SEAβ„’ DSL or natural language
  bounded_context: string;      // Target bounded context
  artifact_type: 'entity' | 'flow' | 'adapter' | 'service' | 'test';
  top_k?: number;               // Number of results (default: 5)
  min_score?: number;           // Minimum similarity (default: 0.7)
}

Response Interface

1
2
3
4
5
6
interface PatternRecommendation {
  pattern_id: string;           // ifl:hash identity
  combined_score: number;       // Similarity score (0-1)
  risk_level: 'Low' | 'Medium' | 'High';
  template_path: string;        // Path to template
}

Example Usage

CLI Query

1
2
3
4
5
6
7
8
9
10
just temporal-ai-query "create a case management service" 5

# Output:
# β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
# β”‚ Pattern ID         β”‚ Score β”‚ Risk     β”‚ Template                 β”‚
# β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
# β”‚ ifl:abc123...      β”‚ 0.92  β”‚ Low      β”‚ templates/service/node   β”‚
# β”‚ ifl:def456...      β”‚ 0.87  β”‚ Low      β”‚ templates/domain/entity  β”‚
# β”‚ ifl:ghi789...      β”‚ 0.76  β”‚ Medium   β”‚ templates/adapter/pg     β”‚
# β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Programmatic Query

1
2
3
4
5
6
7
8
9
10
11
12
13
from temporal_ai import PatternOracle

oracle = PatternOracle()
recommendations = await oracle.query(
    intent="case management service with CQRS",
    context="cognitive-extension",
    artifact_type="service",
    top_k=5
)

for rec in recommendations:
    if rec.combined_score >= 0.7:
        print(f"Recommended: {rec.template_path}")

Learning Loop

The Pattern Oracle learns from successful generations:

1
2
3
4
5
6
1. Developer requests generation
2. Planner queries Pattern Oracle
3. Oracle returns recommendations
4. Generator uses patterns
5. Successful generation recorded
6. Pattern embeddings updated

Recording Pattern Usage

Flow "RecordPatternUsage" from "CodeGenerator" to "VectorStore"
@rationale "Post-generation hook stores pattern usage for learning"
@cqrs { "kind": "event" }
@outbox { "mode": "required" }

Invariants

ID Invariant Enforcement
INV-VP-01 Pattern recommendations MUST respect LocalFirstPrivacy Embedding-based queries only
INV-VP-02 Generation MUST record outcomes for learning Post-gen hook triggers RecordPatternUsage
INV-VP-03 High-risk patterns MUST be flagged RecommendationSafetyGate (SDS-015)

Policies

Pattern Relevance Threshold

Policy "PatternRelevanceThreshold" per Constraint Obligation priority 10 as:
  forall r in recommendations:
    if r.combined_score >= 0.7 then r.eligible_for_injection = true

Pattern Integrity

Policy "PatternIntegrity" per Constraint Obligation priority 10 as:
  forall p in patterns:
    exists s in specs: (p.source_spec = s.id)