# TASK_1: Core Type System and Domain Models
**Created By**: OUTLINER | **Priority**: HIGH | **Duration**: 4 hours
**Technique Focus**: Types + Contracts + Defensive Programming
**Size Constraint**: Target <250 lines/module, Max 400 if splitting awkward
## π¦ Status & Assignment
**Status**: IN_PROGRESS
**Assigned**: Adder_3
**Dependencies**: None (Foundation task)
**Blocking**: TASK_3, TASK_4, TASK_5-11
## π Required Reading (Complete before starting)
- [ ] **Protocol**: `development/protocols/FASTMCP_PYTHON_PROTOCOL.md`
- [ ] **Architecture**: `ARCHITECTURE.md` - Focus on type system and security models
- [ ] **Claude Integration**: `claude.md` - Understanding agent coordination patterns
## π― Objective & Context
**Goal**: Establish comprehensive type system foundation for agent orchestration platform
**Context**: All subsequent development depends on these types for type safety, security validation, and contract enforcement
<thinking>
Type System Analysis:
1. Agent state management requires robust typing for lifecycle tracking
2. Session management needs secure typing for filesystem boundaries
3. Security context requires branded types for cryptographic operations
4. iTerm2 integration needs typed interfaces for async operations
5. Message handling requires type-safe communication protocols
</thinking>
## β
Implementation Subtasks (Sequential completion)
### Phase 1: Core Domain Types
- [x] **Subtask 1.1**: Create branded types for IDs (AgentId, SessionId, ProcessId)
- [x] **Subtask 1.2**: Implement AgentState and SessionState with immutable dataclasses
- [x] **Subtask 1.3**: Define SecurityContext with cryptographic type safety
- [x] **Subtask 1.4**: Create ResourceLimits and performance tracking types
### Phase 2: Communication & Message Types
- [x] **Subtask 2.1**: Implement Message and MessageResult types with validation
- [x] **Subtask 2.2**: Create MCP tool result types (AgentCreationResult, etc.)
- [x] **Subtask 2.3**: Define Claude Code configuration types with defaults
- [x] **Subtask 2.4**: Implement status and health monitoring types
### Phase 3: Integration & Validation
- [x] **Subtask 3.1**: Create iTerm2 integration types and async protocols
- [x] **Subtask 3.2**: Implement comprehensive type validation functions
- [x] **Subtask 3.3**: Add property-based tests for all type conversions
- [x] **Subtask 3.4**: Document all types with contracts and examples
## π§ Implementation Files & Specifications
**Files to Create/Modify**:
- `src/types/ids.py` - Branded ID types with validation (Target: <150 lines)
- `src/types/agent.py` - Agent state and lifecycle types (Target: <200 lines)
- `src/types/session.py` - Session management types (Target: <200 lines)
- `src/types/security.py` - Security context and cryptographic types (Target: <150 lines)
- `src/types/communication.py` - Message and result types (Target: <200 lines)
- `src/types/iterm.py` - iTerm2 integration types (Target: <150 lines)
- `tests/types/test_*.py` - Comprehensive property-based tests for all types
**Key Requirements**:
- ALL types use branded types or NewType for type safety
- Immutable dataclasses with frozen=True for state management
- Comprehensive validation with custom exceptions
- Property-based testing for all type conversions and validations
- Security-focused design with threat modeling
## ποΈ Modularity Strategy
**Size Management**:
- Break types into logical domain modules (agent, session, security, etc.)
- Use composition over inheritance for complex type relationships
- Separate validation logic into pure functions
- Keep type definitions minimal and focused
**Organization Principles**:
- Single responsibility per type module
- Clear separation between public and internal types
- Minimal coupling between type modules
- Maximum type safety with branded types
## β
Success Criteria & Verification
**Completion Requirements**:
- [x] All domain types implemented with comprehensive validation
- [x] Branded types used for all critical identifiers
- [x] Immutable dataclasses for all state management
- [x] Property-based tests covering all type operations
- [x] Security types with cryptographic safety guarantees
- [x] Complete documentation with contracts and examples
**Quality Gates**:
- Type safety: All critical identifiers use branded types
- Immutability: All state types are immutable dataclasses
- Validation: Comprehensive input validation with security focus
- Testing: Property-based tests for all type conversions
- Documentation: Complete with contracts, examples, and security considerations
## π Handoff Information
**Next Task Dependencies**: TASK_3 (FastMCP Server Foundation) requires these types
**Integration Points**: All modules will depend on these foundational types
**Future Considerations**: Type system will be extended for additional MCP tools and security features
## π Implementation Template
```python
# Example structure for src/types/ids.py
from typing import NewType
from dataclasses import dataclass
from contracts import require, ensure
# Branded types for maximum type safety
AgentId = NewType('AgentId', str)
SessionId = NewType('SessionId', str)
@require(lambda agent_id: len(agent_id) > 0 and agent_id.startswith('agent_'))
def validate_agent_id(agent_id: str) -> AgentId:
"""Type-safe agent ID validation with security checks."""
# Comprehensive validation logic
return AgentId(agent_id)
@dataclass(frozen=True)
class AgentIdentifier:
id: AgentId
name: str # Agent_#
session_id: SessionId
def __post_init__(self):
# Validation with contracts
pass
```