Phase 2: Rule Engine Implementation for Aditi
β Phase 1 Completion Summary
Phase 1 has been successfully completed! Hereβs what we accomplished:
Infrastructure Built
- Modern Python Package (
pyproject.toml
) with proper dependencies and tool configuration - Complete CLI Framework (
src/aditi/cli.py
) with Typer, Rich output, and all command placeholders - Configuration Management (
src/aditi/config.py
) with Pydantic models and JSON persistence - Workflow Guidance Module (
src/aditi/workflow.py
) for high-level process guidance - Comprehensive Testing (94% test success rate) with unit and integration tests
- Package Structure with proper exports and metadata
Validation Results
- β
pip install -e .
- Package installs successfully - β
aditi --help
- Shows comprehensive command documentation - β
aditi --version
- Displays version 0.1.0 - β
aditi init
- Initializes Vale configuration successfully - β Placeholder commands - All show helpful βnot yet implementedβ messages
π Phase 2: Rule Engine Implementation
Phase 2 focuses on building the core rule processing engine that parses Vale output and applies fixes to AsciiDoc files.
Step 1: Vale Output Parser
Start your next session with:
Create src/aditi/vale_parser.py that:
- Parses Vale JSON output from ValeContainer
- Creates structured Violation objects
- Handles different violation severities and types
- Groups violations by file and rule
- Includes comprehensive error handling
Step 2: Rule Framework
Create src/aditi/rules/ package with:
- Abstract base Rule class with detect/fix methods
- FixType enum (FULLY_DETERMINISTIC, PARTIALLY_DETERMINISTIC, NON_DETERMINISTIC)
- Violation and Fix data classes
- Rule registry for discovery and execution
Step 3: EntityReference Rule Implementation
Implement src/aditi/rules/entity_reference.py that:
- Detects unsupported HTML entities in AsciiDoc
- Maps entities to DITA-compatible replacements ( β {nbsp})
- Applies fully deterministic fixes
- Validates fixes maintain document structure
Step 4: ContentType Rule Implementation
Implement src/aditi/rules/content_type.py that:
- Detects missing/invalid :_mod-docs-content-type: attributes
- Analyzes filename prefixes for content type hints
- Inserts appropriate content types or TBD placeholders
- Handles partially deterministic scenarios
Step 5: File Processing Engine
Create src/aditi/processor.py that:
- Orchestrates rule execution in dependency order
- Manages file reading and writing operations
- Tracks changes and generates reports
- Handles rollback scenarios
- Provides progress feedback with Rich
Step 6: Check Command Implementation
Implement the check command that:
- Runs Vale against specified files or directories
- Parses output and applies rule detection
- Displays violations categorized by fix type
- Shows detailed reports with file locations
- Respects configuration permissions
π Recommended Implementation Order
- Start with Vale Parser - Essential for all subsequent features
- Build Rule Framework - Provides structure for all rule implementations
- Implement EntityReference Rule - Simplest fully deterministic rule
- Add ContentType Rule - More complex partially deterministic rule
- Create Processing Engine - Orchestrates the workflow
- Wire up Check Command - Provides user-facing functionality
π‘ Implementation Guidelines
Data Structures
@dataclass
class Violation:
file_path: Path
rule_name: str
line: int
column: int
message: str
severity: str
original_text: str
suggested_fix: Optional[str] = None
@dataclass
class Fix:
violation: Violation
replacement_text: str
confidence: float
requires_review: bool = False
class FixType(Enum):
FULLY_DETERMINISTIC = "fully_deterministic"
PARTIALLY_DETERMINISTIC = "partially_deterministic"
NON_DETERMINISTIC = "non_deterministic"
Rule Interface
class Rule(ABC):
@property
@abstractmethod
def name(self) -> str:
"""Rule identifier matching Vale rule name."""
@property
@abstractmethod
def fix_type(self) -> FixType:
"""Classification of fix determinism."""
@abstractmethod
def can_fix(self, violation: Violation) -> bool:
"""Check if this rule can fix the violation."""
@abstractmethod
def generate_fix(self, violation: Violation, file_content: str) -> Optional[Fix]:
"""Generate a fix for the violation."""
Processing Pipeline
class RuleProcessor:
def __init__(self, vale_container: ValeContainer, config: AditiConfig):
self.vale_container = vale_container
self.config = config
self.rules = self._load_rules()
def process_files(self, file_paths: List[Path]) -> ProcessingResult:
"""Process files through the rule pipeline."""
# 1. Run Vale against files
# 2. Parse violations
# 3. Apply rules in dependency order
# 4. Generate fixes
# 5. Return results
π― Success Criteria for Phase 2
By the end of Phase 2, you should be able to:
- Run
aditi check path/to/files
and see detailed violation reports - Process EntityReference violations with automatic fixes
- Handle ContentType violations with smart detection and placeholders
- See progress indicators and rich console output during processing
- Have comprehensive test coverage for all rule processing logic
π Key Design Decisions
Rule Dependencies
ContentType must run before other content-type-dependent rules:
RULE_DEPENDENCIES = {
"ContentType": [], # No dependencies
"TaskSection": ["ContentType"], # Depends on content type
"TaskExample": ["ContentType"],
}
File Safety
- Always create backups before applying fixes
- Validate fixes donβt break AsciiDoc syntax
- Provide rollback capabilities
- Use atomic operations where possible
Error Recovery
- Graceful handling of Vale container failures
- Recovery from malformed Vale output
- File permission and access error handling
- User-friendly error messages with suggested actions
π Integration with Phase 1
Phase 2 builds directly on Phase 1 infrastructure:
- Vale Container: Use existing
ValeContainer
class for Vale execution - Configuration: Leverage
ConfigManager
for user settings and session state - CLI Framework: Implement check command using established Typer patterns
- Workflow Guidance: Provide high-level guidance after applying fixes
- Testing: Extend existing test framework with rule-specific tests
π Example Usage After Phase 2
# Check files for violations
aditi check docs/
# Check specific files
aditi check docs/concept.adoc docs/procedure.adoc
# Check with specific rules
aditi check --rule EntityReference docs/
# Show violation details
aditi check --verbose docs/
Expected output:
π Analysis Results for docs/
π΄ EntityReference (2 violations)
docs/concept.adoc:15 Replace ' ' with '{nbsp}'
docs/concept.adoc:23 Replace '—' with '{mdash}'
π‘ ContentType (1 violation)
docs/procedure.adoc:1 Missing content type attribute (detected: PROCEDURE)
π Summary:
Files processed: 2
Fully deterministic fixes: 2
Partially deterministic fixes: 1
Manual review required: 0
Next steps:
Run 'aditi fix' to apply deterministic fixes
Use 'aditi journey' for guided workflow
π¦ Phase 2 Exit Criteria
Phase 2 is complete when:
- Vale output parsing is robust and handles all edge cases
- Rule framework supports different fix types and dependencies
- EntityReference and ContentType rules are fully implemented
- Check command provides comprehensive violation reporting
- File processing is safe with backup and rollback capabilities
- Test coverage is comprehensive for all rule processing logic
- Integration with existing CLI and configuration is seamless
Ready to transform Aditi from a CLI framework into a functional AsciiDoc-to-DITA migration tool!
COMPLETED PHASE 2