Skip to content

Latest commit

 

History

History
977 lines (684 loc) · 28.4 KB

FEATURE_IMPLEMENTATION.md

File metadata and controls

977 lines (684 loc) · 28.4 KB

Feature Implementation Guide

Navigation: ← Back to Development | Maintenance Home | Testing Framework → | Debugging →

Related Documentation:

Table of Contents

  1. Overview
  2. Quick Start for Existing Plans
  3. Branch Management
  4. Research Phase
  5. Design Phase
  6. Implementation Phase
  7. Refinement Phase
  8. Documentation Phase
  9. Branch Completion

Overview

This document provides a comprehensive development process for implementing new ModelChecker features following test-driven development (TDD) protocols and systematic design principles. The process ensures thorough research, careful planning, and high-quality implementation while maintaining full integration with existing codebase standards.

Process Philosophy: Following the design principles outlined in CLAUDE.md, this implementation process emphasizes architectural clarity, test-driven development, and systematic refactoring to ensure maintainable, high-quality code.

CRITICAL: NO BACKWARDS COMPATIBILITY

  • Never add optional parameters to maintain old behavior
  • Always update all call sites when changing interfaces
  • No deprecation periods - remove old code immediately
  • No compatibility layers - change interfaces directly

Key Standards Integration:

Quick Start for Existing Plans

If you already have an approved implementation plan, you can jump directly to the implementation phase:

Prerequisites for Direct Implementation

  1. Approved implementation plan with detailed phases
  2. Feature branch created and properly named
  3. Tests designed for each implementation phase
  4. Dependencies identified and documented

Jump to Implementation

# Ensure you're on the correct feature branch
git checkout feature/your-feature-name

# Verify implementation plan exists
ls specs/{NNN}_your_feature/plans/implementation-001.md

# Start implementation process
# Jump to: Implementation Phase (Section 6)

Note: Even with existing plans, consider reviewing the Research Phase (Section 4) to ensure your understanding of the codebase is current before beginning implementation.

Branch Management

Branch Creation and Naming

Following version control standards from Maintenance Documentation:

# Create feature branch with descriptive naming
git checkout -b feature/output-system-implementation
git checkout -b feature/theory-validation-enhancement
git checkout -b fix/jupyter-display-ansi-handling

# Push branch and set upstream
git push -u origin feature/your-feature-name

Branch Naming Conventions

  • Features: feature/descriptive-feature-name
  • Bug Fixes: fix/specific-issue-description
  • Refactoring: refactor/component-or-system-name
  • Documentation: docs/documentation-area-update

Branch Lifecycle Management

# Keep branch up to date with main
git fetch origin
git rebase origin/main

# Regular commits during development
git add -A
git commit -m "Phase 1: Implement core infrastructure

- Add OutputManager class with basic interface
- Implement ColorManager with terminal detection
- Add comprehensive unit tests for color management
- Update configuration to support new settings

🤖 Generated with [Claude Code](https://claude.ai/code)

Implementation Process

Core Process Requirements

Each phase of implementation MUST follow this systematic approach:

1. Research and Design Phase (Before Implementation)

  • Analyze current codebase structure thoroughly
  • Identify all dependencies and potential impacts
  • Design implementation approach with clear phases
  • Create detailed implementation plan in specs/plans/
  • Document the plan with specific tasks and success criteria
  • Review and refine the plan before starting

2. Implementation Phase

  • Follow approved plan systematically
  • Use dual testing methodology throughout (see Testing Protocol)
  • Update plan with progress and any deviations
  • Document discoveries in specs/findings/
  • Maintain baseline comparisons for validation

3. Validation Phase

  • Run comprehensive test suite with both methods
  • Compare against baseline for regressions
  • Verify all success criteria are met
  • Document outcomes in implementation summary
  • Update main plan with completion status

Research Phase

Codebase Analysis Methodology

Before designing any implementation, conduct systematic research to understand the current architecture and identify optimal integration points.

1. Identify Relevant Components

# Search for related functionality
grep -r "relevant_keyword" src/
find . -name "*.py" -path "*/related_component/*"

# Examine directory structures
tree src/model_checker/related_area/

2. Study Existing Patterns

  • Read component READMEs to understand architecture
  • Examine similar implementations in the codebase
  • Identify integration points and dependencies
  • Document current limitations and pain points

3. Document Research Findings

Create docs/research/your-feature-research.md:

# Feature Research: [Your Feature Name]

## Current State Analysis

- **Existing functionality**: What currently exists
- **Integration points**: Where new feature connects
- **Dependencies**: Required components and their interfaces
- **Limitations**: Current pain points and architectural issues

## Alternative Approaches

1. **Approach A**: Minimal integration approach
   - Pros: Low risk, quick implementation
   - Cons: Limited functionality, potential technical debt
2. **Approach B**: Comprehensive rewrite approach
   - Pros: Clean architecture, full feature set
   - Cons: High risk, extensive testing required
3. **Approach C**: Hybrid approach (Recommended)
   - Pros: Balanced risk/reward, incremental improvement
   - Cons: Requires careful phase planning

4. Present Research to User

IMPORTANT: Present research findings and options to the user BEFORE creating any detailed implementation plans.

Present a concise summary including:

  • Current state summary - Brief overview of issues found
  • Solution options - 2-3 high-level approaches with:
    • One-paragraph description
    • Key pros and cons (3-4 bullet points each)
    • Rough effort estimate
  • Recommendation - Which approach you recommend and why (2-3 sentences)

Wait for user selection before proceeding to detailed planning.

Example presentation:

Based on my research, here are three approaches to solve [problem]:

**Option A: Quick Fix**
Minimal changes to address immediate issues.
- Pros: Fast (1-2 days), low risk, preserves current architecture
- Cons: Technical debt, incomplete solution, needs future rework

**Option B: Complete Redesign**
Comprehensive overhaul with new architecture.
- Pros: Clean design, extensible, solves all issues
- Cons: High risk (1-2 weeks), breaking changes, extensive testing

**Option C: Incremental Enhancement** (Recommended)
Phased improvements with clean interfaces.
- Pros: Balanced approach, manageable risk, allows iteration
- Cons: More complex than Option A, takes 3-4 days

I recommend Option C because it addresses all issues while maintaining stability.

Design Phase

Implementation Plan Creation

Only after the user has selected an approach, create a detailed implementation plan following TDD principles.

1. Create Implementation Plan Document

Create specs/{NNN}_feature/plans/implementation-001.md:

# Implementation Plan: [Your Feature Name]

## Selected Approach

[Brief description of chosen approach and rationale]

## Test-Driven Development Strategy

- **Test categories**: Unit, integration, end-to-end
- **Testing framework**: Following [Testing Framework](../maintenance/development/../core/TESTING_GUIDE.md)
- **Success criteria**: Measurable outcomes for each phase

## Implementation Phases

### Phase 1: Core Infrastructure (Priority: High)

**Objective**: Establish basic framework and interfaces

**Tests to Write First**:

- `test_core_interface.py`: Test main API contracts
- `test_basic_functionality.py`: Test core feature behavior

**Implementation Tasks**:

1. Create core classes and interfaces
2. Implement basic functionality
3. Add configuration support
4. Write comprehensive unit tests

**Success Criteria**:

- [ ] All unit tests pass
- [ ] Core interface follows project standards
- [ ] Basic functionality works in isolation

### Phase 2: Integration (Priority: High)

**Objective**: Integrate with existing codebase

**Tests to Write First**:

- `test_integration.py`: Test integration with existing components
- `test_migration.py`: Test that all call sites are properly updated

**Implementation Tasks**:

1. Update ALL existing components to use new feature
2. Update all call sites with new interface
3. Add integration tests
4. Update configuration systems

**Success Criteria**:

- [ ] Integration tests pass
- [ ] No existing functionality broken
- [ ] Performance benchmarks maintained

2. User Confirmation Process

Present the detailed implementation plan to the user for approval:

  • Phase breakdown with clear objectives
  • Test-driven approach for each phase
  • Success criteria and validation methods
  • Risk mitigation strategies for each phase

Implementation Phase

Phase-by-Phase Development

Following test-driven development protocols outlined in Testing Framework:

Phase Implementation Workflow

# For each phase:

# 1. Write tests first (Red phase)
# Create tests that define desired behavior
touch src/model_checker/feature/tests/test_phase_1.py

# Write failing tests that specify expected behavior
# Tests should be comprehensive and cover edge cases

# 2. Implement minimal code to pass tests (Green phase)
# Create the simplest implementation that makes tests pass
touch src/model_checker/feature/core.py

# 3. Run tests to confirm implementation
./run_tests.py feature --verbose

# 4. Refactor for quality (Refactor phase)
# Improve code quality while maintaining test passing
# Follow maintenance standards for code organization

# 5. Final validation
./run_tests.py --all  # Ensure no regressions

Testing Protocol

Each refactoring step MUST use BOTH testing methods to ensure comprehensive validation:

Method 1: Test-Driven Development (TDD) with Test Runner

  1. Write Tests First (following Testing Framework):

    # Create test file BEFORE moving code
    src/model_checker/models/tests/test_component.py
    
    # Run tests to ensure they fail appropriately
    ./run_tests.py --package --components models -v
  2. Implement Changes:

    • Move/refactor code to pass tests
    • Ensure all tests pass
  3. Run Full Test Suite:

    ./run_tests.py --all

Method 2: Direct CLI Testing with dev_cli.py

  1. Test Individual Theories:

    ./dev_cli.py src/model_checker/theory_lib/logos/examples.py
    ./dev_cli.py src/model_checker/theory_lib/bimodal/examples.py
  2. Test with Iterations (CRITICAL for iterator regression detection):

    # The examples.py files have iterate=2 settings for uncommented examples
    # This tests the iteration functionality built into the examples
    ./dev_cli.py src/model_checker/theory_lib/logos/examples.py
    ./dev_cli.py src/model_checker/theory_lib/exclusion/examples.py
    ./dev_cli.py src/model_checker/theory_lib/bimodal/examples.py
  3. Test All Theories Systematically:

    for theory in logos bimodal exclusion imposition; do
        echo "Testing $theory..."
        ./dev_cli.py src/model_checker/theory_lib/$theory/examples.py
    done

Success Criteria

  • Method 1: All unit tests passing, no regressions in test suite
  • Method 2: No warnings or AttributeErrors in output
  • Both Methods: Consistent results before and after changes
  • No Z3 Casting Errors: No "Symbolic expressions cannot be cast to concrete Boolean values" errors

Debugging Philosophy

Core Principle: Use errors as opportunities to improve codebase quality through deep analysis and systematic improvements.

1. Fail-Fast Strategy

  • Prefer deterministic failures with helpful error messages
  • Avoid silent failures that hide problems
  • Catch errors early in the development cycle
  • Never mask errors with try/except unless absolutely necessary

2. Deep Root Cause Analysis

  • Trace to fundamental cause when errors occur
  • Look for patterns that indicate architectural issues
  • Consider improvements to overall code quality
  • Document findings for future reference

3. Uniform High-Quality Solutions

  • Avoid cheap patches and band-aid fixes
  • Implement systematic solutions that benefit entire codebase
  • Remove cruft and technical debt when discovered
  • Maintain consistency across all components

Example: Iterator Constraint Issue Resolution

# Problem: Iterator lost constraints due to Z3 boolean evaluation
# Root Cause: Python's implicit boolean conversion of Z3 expressions
# Solution: Explicit Z3 evaluation with proper handling

# Bad (causes implicit conversion):
if constraint:  # Python converts Z3 expr to bool
    constraints.append(constraint)

# Good (explicit Z3 evaluation):
if not z3.is_false(constraint):
    constraints.append(constraint)

Phase Completion Protocol

After each phase:

  1. Validate Phase Success:

    # Run phase-specific tests
    ./run_tests.py phase_1 --verbose
    
    # Run integration tests
    ./run_tests.py --integration
    
    # Check code quality
    grep -n '[^[:print:][:space:]]' src/  # Character validation
  2. Update Implementation Plan:

    • Mark completed tasks as ✅
    • Document any deviations from original plan
    • Note lessons learned or architectural discoveries
    • Update timeline estimates for remaining phases
  3. Commit Phase Changes:

    git add -A
    git commit -m "Phase 1 Complete: Core infrastructure implementation
    
    - Implemented OutputManager with unified interface
    - Added ColorManager with terminal detection
    - Created comprehensive unit test suite (98% coverage)
    - Updated configuration system to support new settings
    - All tests passing, no performance regressions detected
    
    Next: Phase 2 - Integration with BuildModule
    
    🤖 Generated with [Claude Code](https://claude.ai/code)
    
    Co-Authored-By: Claude <noreply@anthropic.com>"
  4. Inform User of Progress:

    • Phase completion summary with key achievements
    • Test results and coverage metrics
    • Any issues encountered and how they were resolved
    • Readiness for next phase confirmation

Example Progress Update

## Phase 1.3 Complete: SemanticDefaults Migration

**Achievements**:
- Successfully moved SemanticDefaults to models/semantic.py
- All 156 tests passing (no regressions)
- Test coverage: 98% for moved component

**Validation Results**:
- Method 1 (Test Runner): All tests green ✓
- Method 2 (CLI Testing): No warnings, all theories working ✓
- Baseline comparison: No changes in output ✓

**Issues Resolved**:
- Import circularity fixed by proper ordering
- Z3 context handling preserved correctly

**Ready for Phase 1.4**: PropositionDefaults migration

Continuous Integration During Implementation

# Before each phase
git fetch origin
git rebase origin/main  # Keep current with main branch

# During each phase
./run_tests.py --unit     # Run unit tests frequently
./run_tests.py --examples # Validate examples still work

# After each phase
./run_tests.py --all      # Full test suite
git push origin feature/your-feature-name  # Keep remote updated

Baseline Capture and Comparison

For major refactoring efforts, establish comprehensive baselines:

1. Create Baseline Before Changes

# Capture test output baseline
./run_tests.py --all > specs/baselines/all_tests_baseline.txt 2>&1

# Capture theory-specific baselines
# Note: examples.py files have iterate=2 settings built-in for uncommented examples
for theory in logos bimodal exclusion imposition; do
    ./dev_cli.py src/model_checker/theory_lib/$theory/examples.py \
        > specs/baselines/${theory}_baseline.txt 2>&1
done

2. Create Comparison Scripts

# Create test comparison script
cat > scripts/test_refactoring.sh << 'EOF'
#!/bin/bash
# Compare current output against baseline

# Run current tests
./run_tests.py --all > current_tests.txt 2>&1

# Compare with baseline
diff specs/baselines/all_tests_baseline.txt current_tests.txt

# Check for new warnings or errors
grep -E "WARNING|Error|AttributeError" current_tests.txt
EOF

chmod +x scripts/test_refactoring.sh

3. Monitor for Regressions

  • No new warnings in output
  • No changes in test results (unless intentional)
  • No performance degradation
  • All examples produce same output

Common Refactoring Scenarios

Moving Classes Between Modules

Based on successful model.py refactoring experience:

1. Preparation Phase

# Identify all imports of the class
grep -r "from.*import.*ClassName" src/
grep -r "ClassName" src/ | grep -v ".pyc"

# Check for inheritance
grep -r "class.*\(.*ClassName.*\)" src/

2. Migration Strategy

# In original file (e.g., model.py):
from model_checker.new_location import ClassName  # Add import
# Leave original class definition during testing
# Remove only after all tests pass

# In new location:
# Copy class with all imports it needs
# Add to __init__.py exports

3. Import Update Pattern

# Update direct imports
# Old: from model_checker.model import ClassName
# New: from model_checker.models.component import ClassName

# Maintain compatibility in __init__.py:
# models/__init__.py:
from .component import ClassName
__all__ = ['ClassName']

Handling Z3 Integration Issues

Common Z3-related refactoring pitfalls:

1. Boolean Evaluation Problem

# WRONG - Causes "cannot cast to concrete Boolean"
if z3_expr:  # Python tries to convert to bool
    ...

# CORRECT - Explicit Z3 evaluation
if not z3.is_false(z3_expr):
    ...

2. Solver Context Preservation

# Ensure solver context is maintained
class RefactoredComponent:
    def __init__(self, z3_context):
        self.solver = z3.Solver(ctx=z3_context)
        # Not: self.solver = z3.Solver()  # Wrong context!

Breaking Up Large Files

Systematic approach used for syntactic.py (895 lines → 5 modules):

1. Identify Natural Boundaries

# Analyze class dependencies
# Group related functionality
# Plan module structure before moving code

2. Create Module Structure First

# Create all files with placeholders
component/
├── __init__.py      # Plan all exports
├── core.py          # Core functionality  
├── helpers.py       # Helper classes
├── processors.py    # Processing logic
└── tests/           # Test structure mirrors code

3. Move in Dependency Order

  • Move leaf classes first (no dependencies)
  • Move dependent classes after dependencies
  • Update imports incrementally
  • Test after each move

Theory-Agnostic Refactoring

Removing theory-specific code from framework:

1. Identify Theory-Specific Code

# Look for theory names in framework
grep -r "logos\|bimodal\|exclusion" src/model_checker/*.py

# Check for hardcoded assumptions
# Example: Special handling for specific operators

2. Extract to Configuration

# Before: Hardcoded in framework
if theory_name == "logos":
    special_handling()

# After: Configuration-driven
if hasattr(theory, 'requires_special_handling'):
    theory.handle_special_case()

Refinement Phase

Post-Implementation Review

Once all planned phases are completed, conduct a comprehensive review to identify potential improvements.

1. Implementation Review Process

Code Quality Assessment:

# Review code organization
find src/ -name "*.py" -path "*your_feature*" -exec wc -l {} +

# Check for code duplication
# Review similar patterns across the codebase

# Validate error handling
grep -r "raise\|except" src/your_feature/

# Performance analysis
# Run benchmarks if applicable

Architecture Review:

  • Design pattern consistency with existing codebase
  • Interface cleanliness and API usability
  • Extension points for future enhancements
  • Integration quality with existing components

2. Identify Refinement Opportunities

Present potential improvements to user:

## Refinement Opportunities

Based on implementation review, the following improvements could enhance the feature:

### High Impact Refinements

1. **API Simplification**
   - Current: Complex initialization requiring multiple parameters
   - Proposed: Factory pattern with sensible defaults
   - Benefit: Easier adoption, reduced cognitive load

2. **Performance Optimization**
   - Current: Linear search in hot path
   - Proposed: Hash-based lookup with caching
   - Benefit: 10x performance improvement for large datasets

### Medium Impact Refinements

3. **Error Message Enhancement**
   - Current: Generic error messages
   - Proposed: Context-specific error messages with solutions
   - Benefit: Better developer experience

4. **Configuration Consolidation**
   - Current: Settings scattered across multiple files
   - Proposed: Centralized configuration with validation
   - Benefit: Easier configuration management

3. User Confirmation for Refinements

Allow user to select which refinements to implement:

  • Present all identified opportunities with impact assessment
  • Allow selective confirmation of refinements
  • Update implementation plan with confirmed refinement phases
  • Provide time estimates for additional phases

4. Implement Confirmed Refinements

For each confirmed refinement, add cleanup phases to the implementation plan:

### Phase N+1: API Simplification (Refinement)

**Objective**: Simplify feature API using factory pattern

**Tests to Write First**:

- `test_factory_pattern.py`: Test simplified initialization
- `test_migration_complete.py`: Ensure all usage updated

**Implementation Tasks**:

1. Create factory class with sensible defaults
2. Update ALL existing usage to use factory pattern
3. Remove old initialization patterns completely
4. Update documentation and examples

**Success Criteria**:

- [ ] New API requires 50% fewer parameters
- [ ] All existing tests still pass
- [ ] Documentation updated with new patterns

5. Execute Refinement Phases

Follow the same phase-by-phase development process:

  • TDD protocols for each refinement
  • User updates after each refinement phase
  • Regression testing to ensure no breakage
  • Documentation updates for new capabilities

Documentation Phase

Comprehensive Documentation Updates

After implementation and refinement are complete, update all affected documentation throughout the repository.

1. Identify Documentation Impact

Systematic Documentation Review:

# Find all README files that might be affected
find . -name "README.md" -path "*/affected_area/*"

# Search for references to modified components
grep -r "modified_component" docs/ src/*/README.md

# Check for outdated examples or code snippets
grep -r "old_pattern_or_api" docs/ README.md

2. Update Documentation Categories

Core Documentation Updates:

  • Component READMEs: Update for new functionality and APIs
  • Architecture documentation: Reflect new design patterns
  • API documentation: Add new interfaces and update existing ones
  • Configuration guides: Document new settings and options

Example Updates:

  • Code examples: Update to use new APIs and patterns
  • Tutorial content: Modify tutorials to demonstrate new features
  • Integration examples: Show how new feature integrates with existing code

Cross-Reference Updates:

  • Maintenance standards: Add any new coding patterns or standards
  • Testing protocols: Document new testing patterns if introduced
  • Implementation guides: Update related implementation documentation

3. Documentation Quality Assurance

Validation Process:

# Verify all code examples work
# Extract and test code snippets from documentation

# Check all internal links
# Verify cross-references point to correct sections

# Validate mathematical symbols display correctly
grep -r "[∧∨¬□◇]" docs/ README.md

# Ensure consistent formatting
grep -r "^#" docs/ src/*/README.md  # Check section headers

4. Documentation Commit

git add docs/ src/*/README.md *.md
git commit -m "Documentation: Complete feature documentation update

- Updated all component READMEs with new functionality
- Added comprehensive API documentation and examples
- Updated cross-references and navigation links
- Validated all code examples and mathematical symbols
- Ensured consistency with project documentation standards

Feature implementation documentation complete.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>"

Branch Completion

Final Review and Merge Preparation

1. Comprehensive Final Review

Complete Implementation Validation:

# Full test suite validation
./run_tests.py --all --verbose

# Cross-platform testing (if applicable)
# Test on different operating systems

# Performance regression testing
# Ensure no performance degradation

# Documentation accuracy verification
# Verify all examples and links work correctly

Code Quality Final Check:

# Character encoding validation
find . -name "*.py" -exec file -i {} \; | grep -v "charset=utf-8"

# Clean up any temporary files
find . -name "test_*.py" -o -name "debug_*.py" | grep -v "src/" | xargs rm -f

# Ensure consistent code formatting
# Review final code against maintenance standards

2. Branch Summary Documentation

Create a final summary of the implementation:

# Implementation Summary: [Feature Name]

## Completed Phases

- ✅ Phase 1: Core Infrastructure
- ✅ Phase 2: Integration with existing codebase
- ✅ Phase 3: Advanced features and optimization
- ✅ Phase 4: API simplification (refinement)
- ✅ Phase 5: Documentation updates

## Key Achievements

- **New functionality**: [Brief description]
- **Test coverage**: XX% (YY new tests added)
- **Performance impact**: No regressions, XX% improvement in specific areas
- **Documentation**: All affected docs updated and validated

## Integration Points

- **Modified components**: List of changed components
- **New dependencies**: Any new requirements
- **Configuration changes**: New settings or options

## Breaking Changes

- **None** / **List any breaking changes with migration path**

## Post-Merge Tasks

- [ ] Monitor for any integration issues
- [ ] Update project changelog if applicable
- [ ] Consider announcing new feature to users

3. Merge Recommendation

Present final implementation to user with merge recommendation:

Branch Ready for Merge:

  • All implementation phases completed successfully
  • All tests passing with comprehensive coverage
  • Documentation fully updated and validated
  • No breaking changes or clear migration path provided
  • Code quality standards met throughout implementation

Suggested Merge Process:

# Final rebase with main
git fetch origin
git rebase origin/main

# Push final changes
git push origin feature/your-feature-name

# Create pull request or suggest merge
# Branch is ready for integration into main

Navigation: ← Back to Development | Maintenance Home | Testing Framework → | Debugging →