createPatternDetector
Orchestrates sequence matching, session analysis, and design pattern detection to identify reusable workflow patterns from tool usage history.Function Signature
src/core/pattern-detector.ts:185
Return Value
Returns an object with two detection methods:Main pattern detection pipeline. Analyzes tool sequences, calculates confidence scores, and optionally enriches with v2 session context.
Convenience wrapper that filters patterns by minimum confidence threshold (default 0.7).
Methods
detectPatterns
Analyzes grouped event sessions to find repeated tool usage patterns. Pipeline:- Extract tool name sequences from each session
- Run sequence matcher to find repeated subsequences
- Collect session IDs, timestamps, success rates
- Calculate weighted confidence scores
- Generate pattern names and descriptions
- Optionally enhance with session context and design patterns (v2)
Array of session event arrays. Each inner array represents a complete coding session.
Optional configuration for pattern detection.
DetectedPattern[] - Array of patterns sorted by confidence descending.
getPendingPatterns
Filters detected patterns by confidence threshold.Array of session event arrays.
Minimum confidence score (0.0 to 1.0). Patterns below this threshold are filtered out.
DetectedPattern[] - Filtered patterns meeting the confidence threshold.
DetectedPattern Type
SHA-256 based pattern identifier (first 12 hex chars).
Ordered list of tool names (e.g.
["Read", "Edit", "Bash"]).Number of times this pattern appeared across sessions.
Weighted confidence score (0.0 to 1.0) based on occurrence, length, success rate, and recency.Formula:
occurrence(0.4) + length(0.2) + success(0.25) + recency(0.15)List of session IDs where this pattern was observed.
ISO-8601 timestamp of first occurrence.
ISO-8601 timestamp of most recent occurrence.
Ratio of successful pattern executions (0.0 to 1.0).
Human-readable pattern name (e.g.
"read-then-edit").Plain language description of the workflow.
V2: Aggregated session analysis data (primary intent, problem domains, workflow type).
V2: Detected design patterns (TDD, MVC, Repository, etc.) with confidence scores.
V2: Detected problem-solving methodology with steps and guidance.
Usage Example
Confidence Calculation
The confidence score uses a weighted formula combining four factors: Occurrence Score (40%): Logarithmic scalinglog(count+1) / log(10)
- Rewards patterns that repeat frequently
- Diminishing returns prevent over-weighting extremely common patterns
length 3-5: score = 1.0 (ideal)length 2: score = 0.7 (acceptable)length > 5: score = max(0.5, 1.0 - (length - 5) * 0.1) (penalized)
successRate = successfulRuns / totalRuns
max(0.5, 1.0 - daysSinceLast * 0.05)- Patterns used recently score higher
src/core/pattern-detector.ts:130-173
V2 Enhancements
WhenenableV2: true (default), the detector enriches patterns with:
Session Context Analysis
- Primary user intent (debug, implement, refactor, test, explore, document)
- Problem domains extracted from file paths
- Workflow type classification (TDD, Debug-Systematic, Refactor-Safe)
- Tool success rates and session duration metrics
Design Pattern Detection
- Architectural patterns (MVC, Repository, Factory, Singleton)
- Coding patterns (Error-First, REST-API, Async, Decorators)
- Workflow patterns (TDD, Refactor-Safe, Debug-Systematic)
Problem-Solving Approaches
- Contextual guidance on when to use the pattern
- Step-by-step methodology
- Benefits and tradeoffs
src/core/pattern-detector.ts:278-369
See Also
- createSkillGenerator - Convert patterns to SKILL.md files
- createEventStore - Store and query tool events
- createSessionAnalyzer - Extract session context
- createDesignPatternDetector - Identify design patterns