# Code Implementation - Examples

> **Legacy Note:** Examples below use the Epic-aware folder structure (`EPIC-{nnn}/FEATURE-{nnn}-{X}/`). Projects created before the Epic migration may still use the legacy format (`FEATURE-{nnn}/`). Both formats are supported during the transition period.

> Reference from SKILL.md: `See [references/examples.md](references/examples.md)`

---

## Example 1: Authentication Service Implementation

**Context:**
- Feature: FEATURE-001 (User Authentication)
- Technical Design: `x-ipe-docs/requirements/EPIC-001/FEATURE-001-A/technical-design.md`
- Test file exists: `tests/test_auth.py` (17 tests, all RED)

### Execution Flow

```
1. Execute Task Flow from x-ipe-workflow-task-execution skill

2. Read Technical Design:
   - Load x-ipe-docs/requirements/EPIC-001/FEATURE-001-A/technical-design.md
   - Components to implement:
     - AuthService (password hashing, token generation)
     - UserRepository (user CRUD operations)
     - AuthMiddleware (request authentication)

3. Test Alignment:
   - Read tests/test_auth.py
   - Find component list: AuthService, UserRepository, AuthMiddleware
   - All 17 tests are RED ✓

4. Implementation Phase:

   [Component 1: UserRepository]
   - Implement src/repositories/user_repository.py
   - Run related tests (5 tests)
   - Result: 5/5 GREEN ✓
   
   [Component 2: AuthService]  
   - Implement src/services/auth_service.py
   - Run related tests (8 tests)
   - Result: 8/8 GREEN ✓
   
   [Component 3: AuthMiddleware]
   - Implement src/middleware/auth_middleware.py
   - Run related tests (4 tests)
   - Result: 4/4 GREEN ✓

5. Final Test Run:
   - All 17 tests: GREEN ✓
   - No tests modified (TDD compliant)

6. Resume Task Flow from x-ipe-workflow-task-execution skill
```

### Output

```yaml
category: feature-stage
next_task_based_skill: Feature Closing
require_human_review: No

implementation_summary: |
  Implemented FEATURE-001 User Authentication with 3 components:
  - UserRepository: User CRUD with password hashing
  - AuthService: Token generation and validation
  - AuthMiddleware: Request authentication decorator
  
test_summary:
  total: 17
  passed: 17
  failed: 0
  modified: 0 (TDD compliant)

task_output_links:
  - src/repositories/user_repository.py
  - src/services/auth_service.py  
  - src/middleware/auth_middleware.py
```

---

## Example 2: Test Failure During Implementation

**Scenario:** Test fails due to design gap

```
1. Read Technical Design for FEATURE-002

2. Implement FileService...

3. Run Tests:
   - test_file_upload: GREEN ✓
   - test_file_validation: RED ✗
     Error: "Expected FileTypeError, got ValidationError"

4. Analyze Failure:
   - Test expects custom exception
   - Technical design mentions "validation errors"
   - Does not specify custom exception class

5. Decision Point:
   - Do NOT modify the test
   - This is a design gap

6. Report to Human:
   "Test failure found: test_file_validation expects FileTypeError
    but technical design doesn't specify this exception type.
    
    Options:
    A) Update technical design to specify FileTypeError
    B) Ask test author to clarify expected behavior"

7. Wait for human guidance before continuing
```

---

## Example 3: Missing Tests (Blocked)

**Scenario:** No test file exists

```
1. Check test file for FEATURE-003
   - Expected: tests/test_payment.py
   - Result: FILE NOT FOUND

2. BLOCKED - Cannot proceed without tests

3. Report:
   "No test file found for FEATURE-003.
    Required: tests/test_payment.py
    
    Previous task (Test Generation) may not have completed.
    Please run Test Generation first."

4. Status: blocked
   Reason: Missing test file
   Required: Complete Test Generation task
```

---

## Example 4: Multiple Features Batch Implementation

**Scenario:** Two related features ready for implementation

```
1. Check task queue:
   - FEATURE-004: Tests ready (12 tests)
   - FEATURE-005: Tests ready (8 tests)

2. Implementation Order (respect dependencies):
   - FEATURE-004 first (FEATURE-005 depends on it)

3. Implement FEATURE-004:
   - All 12 tests GREEN ✓
   - Commit

4. Implement FEATURE-005:
   - All 8 tests GREEN ✓
   - Commit

5. Integration Test:
   - Run all 20 tests together
   - All GREEN ✓

6. Output:
   features_implemented: [FEATURE-004, FEATURE-005]
   total_tests: 20
   all_passed: true
```
