Initial commit

This commit is contained in:
Zhongwei Li
2025-11-29 18:00:50 +08:00
commit c5931553a6
106 changed files with 49995 additions and 0 deletions

View File

@@ -0,0 +1,532 @@
---
name: contextual-pattern-learning
description: Advanced contextual pattern recognition with project fingerprinting, semantic similarity analysis, and cross-domain pattern matching for enhanced learning capabilities
version: 1.0.0
---
## Contextual Pattern Learning Skill
Provides advanced pattern recognition capabilities that understand project context, compute semantic similarities, and identify transferable patterns across different codebases and domains.
## Core Capabilities
### Project Fingerprinting
**Multi-dimensional Project Analysis**:
- **Technology Stack Detection**: Languages, frameworks, libraries, build tools
- **Architectural Patterns**: MVC, microservices, monolith, serverless, etc.
- **Code Structure Analysis**: Module organization, dependency patterns, coupling metrics
- **Team Patterns**: Coding conventions, commit patterns, testing strategies
- **Domain Classification**: Business domain, problem space, user type
**Fingerprint Generation**:
```python
project_fingerprint = {
"technology_hash": sha256(sorted(languages + frameworks + libraries)),
"architecture_hash": sha256(architectural_patterns + structural_metrics),
"domain_hash": sha256(business_domain + problem_characteristics),
"team_hash": sha256(coding_conventions + workflow_patterns),
"composite_hash": combine_all_hashes_with_weights()
}
```
### Context Similarity Analysis
**Multi-factor Similarity Calculation**:
1. **Technology Similarity (40%)**: Language/framework overlap
2. **Architectural Similarity (25%)**: Structure and design patterns
3. **Domain Similarity (20%)**: Business context and problem type
4. **Scale Similarity (10%)**: Project size and complexity
5. **Team Similarity (5%)**: Development practices and conventions
**Semantic Context Understanding**:
- **Intent Recognition**: What the code is trying to accomplish
- **Problem Space Analysis**: What category of problem being solved
- **Solution Pattern Matching**: How similar problems are typically solved
- **Contextual Constraints**: Performance, security, maintainability requirements
### Pattern Classification System
**Primary Classifications**:
- **Implementation Patterns**: Feature addition, API development, UI components
- **Refactoring Patterns**: Code cleanup, optimization, architectural changes
- **Debugging Patterns**: Bug fixing, issue resolution, problem diagnosis
- **Testing Patterns**: Test creation, coverage improvement, test maintenance
- **Integration Patterns**: Third-party services, databases, external APIs
- **Security Patterns**: Authentication, authorization, vulnerability fixes
**Secondary Attributes**:
- **Complexity Level**: Simple, moderate, complex, expert
- **Risk Level**: Low, medium, high, critical
- **Time Sensitivity**: Quick fix, planned work, research task
- **Collaboration Required**: Solo, pair, team, cross-team
### Cross-Domain Pattern Transfer
**Pattern Transferability Assessment**:
```python
def calculate_transferability(pattern, target_context):
technology_match = calculate_tech_overlap(pattern.tech, target_context.tech)
domain_similarity = calculate_domain_similarity(pattern.domain, target_context.domain)
complexity_match = assess_complexity_compatibility(pattern.complexity, target_context.complexity)
transferability = (
technology_match * 0.4 +
domain_similarity * 0.3 +
complexity_match * 0.2 +
pattern.success_rate * 0.1
)
return transferability
```
**Adaptation Strategies**:
- **Direct Transfer**: Pattern applies without modification
- **Technology Adaptation**: Same logic, different implementation
- **Architectural Adaptation**: Same approach, different structure
- **Conceptual Transfer**: High-level concept, complete reimplementation
## Pattern Matching Algorithm
### Context-Aware Similarity
**Weighted Similarity Scoring**:
```python
def calculate_contextual_similarity(source_pattern, target_context):
# Technology alignment (40%)
tech_score = calculate_technology_similarity(
source_pattern.technologies,
target_context.technologies
)
# Problem type alignment (30%)
problem_score = calculate_problem_similarity(
source_pattern.problem_type,
target_context.problem_type
)
# Scale and complexity alignment (20%)
scale_score = calculate_scale_similarity(
source_pattern.scale_metrics,
target_context.scale_metrics
)
# Domain relevance (10%)
domain_score = calculate_domain_relevance(
source_pattern.domain,
target_context.domain
)
return (
tech_score * 0.4 +
problem_score * 0.3 +
scale_score * 0.2 +
domain_score * 0.1
)
```
### Pattern Quality Assessment
**Multi-dimensional Quality Metrics**:
1. **Outcome Quality**: Final result quality score (0-100)
2. **Process Efficiency**: Time taken vs. expected time
3. **Error Rate**: Number and severity of errors encountered
4. **Reusability**: How easily the pattern can be applied elsewhere
5. **Adaptability**: How much modification was needed for reuse
**Quality Evolution Tracking**:
- **Initial Quality**: Quality when first captured
- **Evolved Quality**: Updated quality after multiple uses
- **Context Quality**: Quality in specific contexts
- **Time-based Quality**: How quality changes over time
## Learning Strategies
### Progressive Pattern Refinement
**1. Pattern Capture**:
```python
def capture_pattern(task_execution):
pattern = {
"id": generate_unique_id(),
"timestamp": current_time(),
"context": extract_rich_context(task_execution),
"execution": extract_execution_details(task_execution),
"outcome": extract_outcome_metrics(task_execution),
"insights": extract_learning_insights(task_execution),
"relationships": extract_pattern_relationships(task_execution)
}
return refine_pattern_with_learning(pattern)
```
**2. Pattern Validation**:
- **Immediate Validation**: Check pattern completeness and consistency
- **Cross-validation**: Compare with similar existing patterns
- **Predictive Validation**: Test pattern predictive power
- **Temporal Validation**: Monitor pattern performance over time
**3. Pattern Evolution**:
```python
def evolve_pattern(pattern_id, new_execution_data):
existing_pattern = load_pattern(pattern_id)
# Update success metrics
update_success_rates(existing_pattern, new_execution_data)
# Refine context understanding
refine_context_similarity(existing_pattern, new_execution_data)
# Update transferability scores
update_transferability_assessment(existing_pattern, new_execution_data)
# Generate new insights
generate_new_insights(existing_pattern, new_execution_data)
save_evolved_pattern(existing_pattern)
```
### Relationship Mapping
**Pattern Relationships**:
- **Sequential Patterns**: Patterns that often follow each other
- **Alternative Patterns**: Different approaches to similar problems
- **Prerequisite Patterns**: Patterns that enable other patterns
- **Composite Patterns**: Multiple patterns used together
- **Evolutionary Patterns**: Patterns that evolve into other patterns
**Relationship Discovery**:
```python
def discover_pattern_relationships(patterns):
relationships = {}
for pattern_a in patterns:
for pattern_b in patterns:
if pattern_a.id == pattern_b.id:
continue
# Sequential relationship
if often_sequential(pattern_a, pattern_b):
relationships[f"{pattern_a.id} -> {pattern_b.id}"] = {
"type": "sequential",
"confidence": calculate_sequential_confidence(pattern_a, pattern_b)
}
# Alternative relationship
if are_alternatives(pattern_a, pattern_b):
relationships[f"{pattern_a.id} <> {pattern_b.id}"] = {
"type": "alternative",
"confidence": calculate_alternative_confidence(pattern_a, pattern_b)
}
return relationships
```
## Context Extraction Techniques
### Static Analysis Context
**Code Structure Analysis**:
- **Module Organization**: How code is organized into modules/packages
- **Dependency Patterns**: How modules depend on each other
- **Interface Design**: How components communicate
- **Design Patterns**: GoF patterns, architectural patterns used
- **Code Complexity**: Cyclomatic complexity, cognitive complexity
**Technology Stack Analysis**:
```python
def extract_technology_context(project_root):
technologies = {
"languages": detect_languages(project_root),
"frameworks": detect_frameworks(project_root),
"databases": detect_databases(project_root),
"build_tools": detect_build_tools(project_root),
"testing_frameworks": detect_testing_frameworks(project_root),
"deployment_tools": detect_deployment_tools(project_root)
}
return analyze_technology_relationships(technologies)
```
### Dynamic Context Analysis
**Runtime Behavior Patterns**:
- **Performance Characteristics**: Speed, memory usage, scalability
- **Error Patterns**: Common errors and their contexts
- **Usage Patterns**: How the code is typically used
- **Interaction Patterns**: How components interact at runtime
**Development Workflow Patterns**:
```python
def extract_workflow_context(git_history):
return {
"commit_patterns": analyze_commit_patterns(git_history),
"branching_strategy": detect_branching_strategy(git_history),
"release_patterns": analyze_release_patterns(git_history),
"collaboration_patterns": analyze_collaboration(git_history),
"code_review_patterns": analyze_review_patterns(git_history)
}
```
### Semantic Context Analysis
**Domain Understanding**:
- **Business Domain**: E-commerce, finance, healthcare, education
- **Problem Category**: Data processing, user interface, authentication, reporting
- **User Type**: End-user, admin, developer, system
- **Performance Requirements**: Real-time, batch, high-throughput, low-latency
**Intent Recognition**:
```python
def extract_intent_context(task_description, code_changes):
intent_indicators = {
"security": detect_security_intent(task_description, code_changes),
"performance": detect_performance_intent(task_description, code_changes),
"usability": detect_usability_intent(task_description, code_changes),
"maintainability": detect_maintainability_intent(task_description, code_changes),
"functionality": detect_functionality_intent(task_description, code_changes)
}
return rank_intent_by_confidence(intent_indicators)
```
## Adaptation Learning
### Success Pattern Recognition
**What Makes Patterns Successful**:
1. **Context Alignment**: How well the pattern fits the context
2. **Execution Quality**: How well the pattern was executed
3. **Outcome Quality**: The quality of the final result
4. **Efficiency**: Time and resource usage
5. **Adaptability**: How easily the pattern can be modified
**Success Factor Analysis**:
```python
def analyze_success_factors(pattern):
factors = {}
# Context alignment
factors["context_alignment"] = calculate_context_fit_score(pattern)
# Execution quality
factors["execution_quality"] = analyze_execution_process(pattern)
# Team skill match
factors["skill_alignment"] = analyze_team_skill_match(pattern)
# Tooling support
factors["tooling_support"] = analyze_tooling_effectiveness(pattern)
# Environmental factors
factors["environment_fit"] = analyze_environmental_fit(pattern)
return rank_factors_by_importance(factors)
```
### Failure Pattern Learning
**Common Failure Modes**:
1. **Context Mismatch**: Pattern applied in wrong context
2. **Skill Gap**: Required skills not available
3. **Tooling Issues**: Required tools not available or not working
4. **Complexity Underestimation**: Pattern more complex than expected
5. **Dependency Issues**: Required dependencies not available
**Failure Prevention**:
```python
def predict_pattern_success(pattern, context):
risk_factors = []
# Check context alignment
if calculate_context_similarity(pattern.context, context) < 0.6:
risk_factors.append({
"type": "context_mismatch",
"severity": "high",
"mitigation": "consider alternative patterns or adapt context"
})
# Check skill requirements
required_skills = pattern.execution.skills_required
available_skills = context.team_skills
missing_skills = set(required_skills) - set(available_skills)
if missing_skills:
risk_factors.append({
"type": "skill_gap",
"severity": "medium",
"mitigation": f"acquire skills: {', '.join(missing_skills)}"
})
return {
"success_probability": calculate_success_probability(pattern, context),
"risk_factors": risk_factors,
"recommendations": generate_mitigation_recommendations(risk_factors)
}
```
## Pattern Transfer Strategies
### Technology Adaptation
**Language-Agnostic Patterns**:
- **Algorithmic Patterns**: Logic independent of language syntax
- **Architectural Patterns**: Structure independent of implementation
- **Process Patterns**: Workflow independent of technology
- **Design Patterns**: Object-oriented design principles
**Technology-Specific Adaptation**:
```python
def adapt_pattern_to_technology(pattern, target_technology):
adaptation_rules = load_adaptation_rules(pattern.source_technology, target_technology)
adapted_pattern = {
"original_pattern": pattern,
"target_technology": target_technology,
"adaptations": [],
"confidence": 0.0
}
for rule in adaptation_rules:
if rule.applicable(pattern):
adaptation = rule.apply(pattern, target_technology)
adapted_pattern.adaptations.append(adaptation)
adapted_pattern.confidence += adaptation.confidence_boost
return validate_adapted_pattern(adapted_pattern)
```
### Scale Adaptation
**Complexity Scaling**:
- **Pattern Simplification**: Reduce complexity for simpler contexts
- **Pattern Enhancement**: Add complexity for more demanding contexts
- **Pattern Modularity**: Break complex patterns into reusable components
- **Pattern Composition**: Combine simple patterns for complex solutions
**Scale Factor Analysis**:
```python
def adapt_pattern_for_scale(pattern, target_scale):
current_scale = pattern.scale_context
scale_factor = calculate_scale_factor(current_scale, target_scale)
if scale_factor > 2.0: # Need to scale up
return enhance_pattern_for_scale(pattern, target_scale)
elif scale_factor < 0.5: # Need to scale down
return simplify_pattern_for_scale(pattern, target_scale)
else: # Scale is compatible
return pattern.with_scale_adjustments(target_scale)
```
## Continuous Improvement
### Learning Feedback Loops
**1. Immediate Feedback**:
- Pattern quality assessment
- Success/failure recording
- Context accuracy validation
- Prediction accuracy tracking
**2. Short-term Learning** (Daily/Weekly):
- Pattern performance trending
- Context similarity refinement
- Success factor correlation
- Failure pattern identification
**3. Long-term Learning** (Monthly):
- Cross-domain pattern transfer
- Technology evolution adaptation
- Team learning integration
- Best practice extraction
### Meta-Learning
**Learning About Learning**:
```python
def analyze_learning_effectiveness():
learning_metrics = {
"pattern_accuracy": measure_pattern_prediction_accuracy(),
"context_comprehension": measure_context_understanding_quality(),
"adaptation_success": measure_pattern_adaptation_success_rate(),
"knowledge_transfer": measure_cross_project_knowledge_transfer(),
"prediction_improvement": measure_prediction_accuracy_over_time()
}
return generate_learning_insights(learning_metrics)
```
**Adaptive Learning Strategies**:
- **Confidence Adjustment**: Adjust prediction confidence based on accuracy
- **Context Weighting**: Refine context importance weights
- **Pattern Selection**: Improve pattern selection algorithms
- **Feedback Integration**: Better integrate user feedback
## Usage Guidelines
### When to Apply This Skill
**Trigger Conditions**:
- Starting a new task in an unfamiliar codebase
- Need to understand project context quickly
- Looking for similar solutions in other projects
- Adapting patterns from one technology to another
- Estimating task complexity based on historical patterns
**Optimal Contexts**:
- Multi-language or multi-framework projects
- Large codebases with established patterns
- Teams working on multiple similar projects
- Projects requiring frequent adaptation of solutions
- Knowledge sharing across teams or organizations
### Expected Outcomes
**Primary Benefits**:
- **Faster Context Understanding**: Quickly grasp project structure and conventions
- **Better Pattern Matching**: Find more relevant solutions from past experience
- **Improved Adaptation**: More successful adaptation of patterns to new contexts
- **Cross-Project Learning**: Leverage knowledge from previous projects
- **Predictive Insights**: Better predictions of task complexity and success
**Quality Metrics**:
- **Context Similarity Accuracy**: >85% accurate context matching
- **Pattern Transfer Success**: >75% successful pattern adaptation
- **Prediction Accuracy**: >80% accurate outcome predictions
- **Learning Velocity**: Continuous improvement in pattern quality
## Integration with Other Skills
### Complementary Skills
**code-analysis**:
- Provides detailed code structure analysis for context extraction
- Helps identify design patterns and architectural decisions
- Contributes to technology stack detection
**quality-standards**:
- Provides quality metrics for pattern assessment
- Helps establish quality thresholds for pattern selection
- Contributes to best practice identification
**pattern-learning** (basic):
- Provides foundation pattern storage and retrieval
- Enhanced by contextual understanding and similarity analysis
- Benefits from advanced classification and relationship mapping
### Data Flow
```python
# Context extraction
context = code_analysis.extract_structure() + contextual_pattern_learning.extract_semantic_context()
# Pattern matching
matches = contextual_pattern_learning.find_similar_patterns(context, code_analysis.get_quality_metrics())
# Quality assessment
quality_score = quality_standards.assess_pattern_quality(matches)
# Learning integration
contextual_pattern_learning.capture_pattern_with_context(execution_data, context, quality_score)
```
This skill creates a comprehensive contextual understanding system that dramatically improves pattern matching, adaptation, and learning capabilities by considering the rich context in which patterns are created and applied.