File size: 6,701 Bytes
9bd4ce5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# Error Pattern Analyzer Design

## Overview

This document describes the design for a tool that analyzes semantic test failures and groups them by common bytecode/ability patterns for targeted debugging.

## Architecture

```mermaid
flowchart TD
    A[COMPREHENSIVE_SEMANTIC_AUDIT.md] --> B[Parser]
    B --> C[Failure Records]
    C --> D[Pattern Grouper]
    D --> E[Error Patterns]
    E --> F[Report Generator]
    F --> G[Pattern Analysis Report]
    F --> H[JSON Output]
```

## Components

### 1. Parser Module

**Purpose**: Extract failure records from the audit report.

**Input**: `reports/COMPREHENSIVE_SEMANTIC_AUDIT.md`

**Output**: List of `FailureRecord` objects

**Data Structure**:
```rust
struct FailureRecord {
    card_no: String,
    ability_idx: usize,
    error_type: String,      // e.g., "HAND_DELTA", "ENERGY_DELTA"
    effect: String,          // e.g., "DRAW", "ACTIVATE_MEMBER"
    expected: String,
    actual: String,
    segment: usize,
    raw_message: String,
}
```

**Parsing Logic**:
1. Read markdown table rows
2. Extract card number, ability index, and failure details
3. Parse error type from "Mismatch X for Y" pattern
4. Extract expected/actual values

### 2. Pattern Grouper Module

**Purpose**: Group failures by common patterns.

**Grouping Strategy**:

| Pattern Key | Description |
|-------------|-------------|
| `{error_type}_{effect}` | Primary grouping (e.g., "HAND_DELTA_DRAW") |
| `{error_type}` | Secondary grouping by error type |
| `{effect}` | Tertiary grouping by effect |

**Opcode Mapping**:
```rust
const OPCODE_EFFECT_MAP: &[(&str, &[&str])] = &[
    ("DRAW", &["O_DRAW", "O_DRAW_UNTIL"]),
    ("HAND_DELTA", &["O_DRAW", "O_ADD_TO_HAND", "O_RECOVER_MEMBER"]),
    ("ENERGY_DELTA", &["O_ACTIVATE_MEMBER", "O_ACTIVATE_ENERGY"]),
    ("SCORE_DELTA", &["O_BOOST_SCORE", "O_SET_SCORE"]),
    ("HEART_DELTA", &["O_ADD_HEARTS", "O_SET_HEARTS"]),
    ("BLADE_DELTA", &["O_ADD_BLADES", "O_SET_BLADES"]),
    ("MEMBER_TAP_DELTA", &["O_TAP_MEMBER", "O_TAP_OPPONENT"]),
    ("DECK_SEARCH", &["O_LOOK_AND_CHOOSE", "O_LOOK_DECK"]),
];
```

### 3. Report Generator Module

**Purpose**: Generate human-readable and machine-readable reports.

**Output Formats**:
- Markdown report with pattern summary and details
- JSON output for programmatic use

## Error Pattern Categories

### Category 1: Resource Delta Mismatches

| Pattern | Typical Cause | Suggested Fix |
|---------|---------------|---------------|
| HAND_DELTA | Draw/discard not working | Check deck state and O_DRAW |
| ENERGY_DELTA | Energy tap not working | Check energy zone and O_ACTIVATE_* |
| SCORE_DELTA | Score boost not applying | Check live phase context |
| DISCARD_DELTA | Movement to discard failing | Check O_MOVE_TO_DISCARD |

### Category 2: Buff Application Failures

| Pattern | Typical Cause | Suggested Fix |
|---------|---------------|---------------|
| HEART_DELTA | Target selection issue | Check stage member selection |
| BLADE_DELTA | Target selection issue | Check stage member selection |

### Category 3: State Change Failures

| Pattern | Typical Cause | Suggested Fix |
|---------|---------------|---------------|
| MEMBER_TAP_DELTA | Tap effect not executing | Check O_TAP_MEMBER/O_TAP_OPPONENT |
| ACTION_PREVENTION | Prevention flag not set | Check O_PREVENT_* opcodes |
| TAP_ALL | Mass tap not iterating | Check loop over targets |

### Category 4: Interaction Failures

| Pattern | Typical Cause | Suggested Fix |
|---------|---------------|---------------|
| DECK_SEARCH | No cards revealed | Check deck state and filter |
| LOOK_AND_CHOOSE | Selection not resolving | Check interaction resolver |
| RECOVER_MEMBER | No valid target in discard | Check discard state |
| RECOVER_LIVE | No live in discard | Check discard for live cards |

## Implementation Plan

### Phase 1: Core Parser

Create `tools/verify/error_pattern_analyzer.py`:

```python
def parse_audit_report(filepath: str) -> Tuple[List[FailureRecord], int, int]:
    """Parse the audit report and extract failure records."""
    # Implementation here

def group_failures_by_pattern(failures: List[FailureRecord]) -> Dict[str, ErrorPattern]:
    """Group failures by error pattern."""
    # Implementation here

def generate_report(patterns: Dict[str, ErrorPattern]) -> str:
    """Generate markdown report."""
    # Implementation here
```

### Phase 2: Integration

Add to test workflow:

```bash
# Run after semantic audit
cargo test test_semantic_mass_verification
python tools/verify/error_pattern_analyzer.py
```

### Phase 3: Rust Integration

Create Rust module `engine_rust_src/src/error_patterns.rs`:

```rust
pub struct ErrorPatternAnalyzer {
    patterns: HashMap<String, ErrorPattern>,
}

impl ErrorPatternAnalyzer {
    pub fn analyze_failures(failures: &[TestFailure]) -> Vec<ErrorPattern> {
        // Implementation
    }
}
```

## Sample Output

### Pattern Summary Table

| Pattern | Count | Error Type | Effect | Suggested Fix |
|:--------|------:|:-----------|:-------|:---------------|
| HAND_DELTA_DRAW | 25 | HAND_DELTA | DRAW | Check O_DRAW and deck state |
| ENERGY_DELTA_ACTIVATE | 20 | ENERGY_DELTA | ACTIVATE_ENERGY | Check energy zone state |
| BLADE_DELTA_ADD | 15 | BLADE_DELTA | ADD_BLADES | Check target selection |

### Detailed Pattern Analysis

#### HAND_DELTA_DRAW

**Description**: Hand count change mismatch - draw effects not working as expected

**Affected Opcodes**: O_DRAW, O_DRAW_UNTIL

**Suggested Fix**: Check O_DRAW implementation and deck state. Ensure deck has cards.

**Failure Count**: 25

**Value Mismatches**:
- Exp: 1, Got: 0 (15 failures)
  - PL!-bp4-023-L (Ab0)
  - PL!-pb1-006-P+ (Ab0)
  - ... and 13 more
- Exp: 2, Got: 0 (10 failures)
  - PL!HS-bp2-015-N (Ab0)
  - ... and 9 more

## Usage

```bash
# Basic usage
python tools/verify/error_pattern_analyzer.py

# With custom input/output
python tools/verify/error_pattern_analyzer.py \
    --input reports/COMPREHENSIVE_SEMANTIC_AUDIT.md \
    --output reports/error_pattern_analysis.md \
    --json

# Output
# - reports/error_pattern_analysis.md
# - reports/error_pattern_analysis.json (if --json flag)
```

## Benefits

1. **Targeted Debugging**: Focus on high-impact error patterns first
2. **Root Cause Analysis**: Identify common causes across multiple cards
3. **Progress Tracking**: Monitor pattern resolution over time
4. **Documentation**: Generate documentation for known issues

## Future Enhancements

1. **Bytecode Correlation**: Link errors to specific bytecode sequences
2. **Auto-Fix Suggestions**: Generate code patches for common issues
3. **Regression Detection**: Alert when new patterns emerge
4. **CI Integration**: Fail builds on critical pattern increases