Files
rookiestar28 12d3b8e285 feat(patterns): implement PatternLoader with JSON-based pattern management (STAGE 2)
Integrate PatternLoader into analyzer.py to enable JSON-based error pattern
management with hot-reload capability, while preserving fallback to hardcoded
patterns for reliability.

Features:
- JSON-based pattern definitions with schema validation
- Hot-reload capability without restart
- Priority-based pattern matching (higher priority checked first)
- Multi-directory support (builtin, community, custom)
- Backward compatible with fallback to hardcoded PATTERNS

Implementation:
- pattern_loader.py: Core PatternLoader class (309 lines)
  - load(): Load patterns from JSON files with schema validation
  - match(): Match error against patterns (priority-sorted)
  - reload_if_changed(): Hot-reload on file modification
  - get_stats(): Pattern statistics and categorization
- patterns/schema.json: JSON schema definition
  - Required fields: id, regex, error_key, priority, category
  - Optional fields: has_groups, tags, notes
  - Validation: unique IDs, valid priority range (0-100), valid categories
- patterns/builtin/core.json: Migrated 21 existing patterns
  - Priority distribution: 9 high (80-100), 12 medium (50-79)
  - Category distribution: 7 data_type, 3 memory, 3 workflow, 2 model_loading, etc.
- tests/test_pattern_loader.py: Comprehensive test suite (9 tests)
  - All tests passed ✅

Integration with analyzer.py:
- Updated ErrorAnalyzer.analyze():
  - Try PatternLoader first (JSON patterns)
  - Fallback to hardcoded PATTERNS on any error
  - Ensures error analysis always works
- Added ErrorAnalyzer.reload_patterns():
  - Public API for hot-reload
  - Returns True if patterns reloaded

Test Results:
✅ All 9 unit tests passed
✅ Manual integration test passed
✅ 21 patterns loaded from core.json
✅ Error detection working correctly (CUDA OOM, validation errors)
✅ Hot-reload functionality verified
✅ Zero performance regression (<1ms pattern matching)

Architecture:
- Preserves fallback mechanism (learned from STAGE 1)
- Priority-based sorting prevents false positives
- Schema validation catches errors early
- Graceful error handling (never crashes)

Next Steps:
- STAGE 3: F12 Pattern Expansion (35+ community patterns)
- After STAGE 1+2 stable for 3+ days

References:
- Planning: .planning/STAGE2_F2_INTEGRATION_PLAN.md
- Implementation Record: .planning/STAGE2_IMPLEMENTATION_RECORD.md
- Original F2 Design: .planning/FAILURE-F12_F2_T8_IMPLEMENTATION_PLAN.md
2026-01-03 04:55:18 +08:00

73 lines
2.2 KiB
JSON

{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ComfyUI-Doctor Error Pattern Schema",
"description": "JSON schema for error pattern definitions",
"type": "object",
"required": ["version", "patterns"],
"properties": {
"version": {
"type": "string",
"description": "Schema version (semver)",
"pattern": "^\\d+\\.\\d+\\.\\d+$"
},
"patterns": {
"type": "array",
"description": "Array of error pattern definitions",
"items": {
"type": "object",
"required": ["id", "regex", "error_key", "priority", "category"],
"properties": {
"id": {
"type": "string",
"description": "Unique pattern identifier (lowercase, alphanumeric + underscore)",
"pattern": "^[a-z0-9_]+$"
},
"regex": {
"type": "string",
"description": "Python regex pattern to match error messages"
},
"error_key": {
"type": "string",
"description": "Translation key in i18n.py (e.g., 'CUDA_OOM')"
},
"has_groups": {
"type": "boolean",
"default": false,
"description": "Whether regex contains capture groups for dynamic data"
},
"priority": {
"type": "integer",
"minimum": 0,
"maximum": 100,
"description": "Match priority (higher = checked first). 80-100: high, 50-79: medium, 0-49: low"
},
"category": {
"type": "string",
"enum": [
"memory",
"model_loading",
"custom_nodes",
"framework",
"workflow",
"data_type",
"generic"
],
"description": "Error category for statistics and filtering"
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"description": "Searchable tags (e.g., ['cudnn', 'gpu', 'runtime'])"
},
"notes": {
"type": "string",
"description": "Developer notes (not shown to users, for documentation)"
}
}
}
}
}
}