# Issue #143: [COORD-003] Validate 50% rule

## Objective

Validate the 50% rule prevents context exhaustion by blocking oversized issue assignments.

## Approach

Following TDD principles:

1. Write tests first for all scenarios
2. Implement validation logic
3. Verify all tests pass with 85%+ coverage

## The 50% Rule

Issues must not exceed 50% of target agent's context limit.

Agent context limits:

- opus: 200K tokens (max issue: 100K)
- sonnet: 200K tokens (max issue: 100K)
- haiku: 200K tokens (max issue: 100K)
- glm: 128K tokens (max issue: 64K)
- minimax: 128K tokens (max issue: 64K)

## Test Scenarios

1. **Oversized issue** - 120K estimate for sonnet (200K limit) → REJECT
2. **Properly sized** - 80K estimate for sonnet → ACCEPT
3. **Edge case** - Exactly 100K estimate for sonnet → ACCEPT (at limit)
4. **Sequential issues** - Multiple medium issues → Complete without exhaustion

## Progress

- [x] Create scratchpad
- [x] Read existing code and patterns
- [x] Write test file (RED phase) - 12 comprehensive tests
- [x] Implement validation logic (GREEN phase)
- [x] All tests pass (12/12)
- [x] Type checking passes (mypy)
- [x] Linting passes (ruff)
- [x] Verify coverage ≥85% (achieved 100%)
- [x] Create validation report
- [x] Ready to commit

## Testing

Test file: `/home/jwoltje/src/mosaic-stack/apps/coordinator/tests/test_fifty_percent_rule.py`
Implementation: `/home/jwoltje/src/mosaic-stack/apps/coordinator/src/validation.py`

**Results:**

- 12/12 tests passing
- 100% coverage (14/14 statements)
- All quality gates passed

## Notes

- Agent limits defined in issue #144 (COORD-004) - using hardcoded values for now
- Validation is a pure function (easy to test)
- Returns ValidationResult with detailed rejection reasons
- Handles all edge cases (0, exactly 50%, overflow, all agents)

## Implementation Summary

**Files Created:**

1. `src/validation.py` - Validation logic
2. `tests/test_fifty_percent_rule.py` - Comprehensive tests
3. `docs/50-percent-rule-validation.md` - Validation report

**Test Scenarios Covered:**

1. ✅ Oversized issue (120K) → REJECTED
2. ✅ Properly sized (80K) → ACCEPTED
3. ✅ Edge case (100K exactly) → ACCEPTED
4. ✅ Sequential issues (3×60K) → All ACCEPTED
5. ✅ All agent types tested
6. ✅ Edge cases (0, very small, boundaries)

**Token Usage:** ~48K / 40.3K estimated (within budget)
