# Tasks: 多平台 Delta 中性控制平面

**Input**: Design documents from `/specs/001-specManager-perp/`
**Prerequisites**: plan.md (required), research.md, data-model.md, contracts/

## Execution Flow (main)
```
1. Load plan.md from feature directory
   → If not found: ERROR "No implementation plan found"
   → Extract: tech stack, libraries, structure
2. Load optional design documents:
   → data-model.md: Extract entities → model tasks
   → contracts/: Each file → contract test task
   → research.md: Extract decisions → setup tasks
3. Generate tasks by category:
   → Setup: environment, proxies, credentials, risk configs
   → Tests: dry-run simulations, orderbook validation, multi-account integration
   → Core: hedging engines, order routing, exposure monitoring
   → Integration: exchange adapters, observability, rollback safeguards
   → Polish: performance verification, documentation, operational rehearsals
4. Apply task rules:
   → Different files = mark [P] for parallel
   → Same file = sequential (no [P])
   → Tests before implementation (TDD)
   → Exposure-related safeguards MUST precede new trading flows
5. Number tasks sequentially (T001, T002...)
6. Generate dependency graph
7. Create parallel execution examples
8. Validate task completeness:
   → All contracts have tests?
   → All entities have models?
   → All endpoints implemented?
9. Return: SUCCESS (tasks ready for execution)
```

## Format: `[ID] [P?] Description`
- **[P]**: Can run in parallel (different files, no dependencies)
- Include exact file paths in descriptions

## Path Conventions
- **Single project**: `src/`, `tests/` at repository root
- **Web app**: `backend/src/`, `frontend/src/`
- **Mobile**: `api/src/`, `ios/src/` or `android/src/`
- Paths shown below assume single project - adjust based on plan.md structure

## Phase 3.1: Setup
- [ ] T001 Create project structure per implementation plan
- [ ] T002 Initialize TypeScript project with ws, axios, dotenv, winston, jest dependencies
- [ ] T003 [P] Configure linting and formatting tools in tsconfig.json
- [ ] T004 [P] Setup proxy configuration and credential management in src/config/
- [ ] T005 [P] Create risk envelope configuration templates in src/config/risk/

## Phase 3.2: Tests First (TDD) ⚠️ MUST COMPLETE BEFORE 3.3
**CRITICAL: These tests MUST be written and MUST FAIL before ANY implementation**
- [ ] T006 [P] Contract test account-sync in tests/contract/test_account_sync.ts
- [ ] T007 [P] Contract test hedge-execution in tests/contract/test_hedge_execution.ts
- [ ] T008 [P] Contract test market-data-failover in tests/contract/test_market_data_failover.ts
- [ ] T009 [P] Integration test utilization rebalancing in tests/integration/test_utilization_rebalance.ts
- [ ] T010 [P] Integration test delta hedging in tests/integration/test_delta_hedging.ts
- [ ] T011 [P] Integration test market data failover in tests/integration/test_market_data_failover.ts
- [ ] T012 [P] Integration test strategy module sandbox in tests/integration/test_strategy_sandbox.ts

## Phase 3.3: Core Implementation (ONLY after tests are failing)
- [ ] T013 [P] ExchangeAccount model in src/models/ExchangeAccount.ts
- [ ] T014 [P] MarketDataFeed model in src/models/MarketDataFeed.ts
- [ ] T015 [P] StrategyModule model in src/models/StrategyModule.ts
- [ ] T016 [P] RiskEnvelope model in src/models/RiskEnvelope.ts
- [ ] T017 [P] OrderIntent model in src/models/OrderIntent.ts
- [ ] T018 [P] HedgeExecution model in src/models/HedgeExecution.ts
- [ ] T019 [P] ProxySession model in src/models/ProxySession.ts
- [ ] T020 [P] AccountAllocation model in src/models/AccountAllocation.ts
- [ ] T021 [P] SynthPriceSnapshot model in src/models/SynthPriceSnapshot.ts
- [ ] T022 [P] MonitoringEvent model in src/models/MonitoringEvent.ts
- [ ] T023 Account sync service in src/services/AccountSyncService.ts
- [ ] T024 Hedge execution service in src/services/HedgeExecutionService.ts
- [ ] T025 Market data failover service in src/services/MarketDataFailoverService.ts
- [ ] T026 Risk monitoring service in src/services/RiskMonitoringService.ts
- [ ] T027 Strategy module manager in src/services/StrategyModuleManager.ts
- [ ] T028 Delta neutral controller in src/controllers/DeltaNeutralController.ts

## Phase 3.4: Integration
- [ ] T029 Connect AccountSyncService to exchange adapters
- [ ] T030 Connect HedgeExecutionService to proxy routing
- [ ] T031 Connect MarketDataFailoverService to WebSocket/HTTP feeds
- [ ] T032 Connect RiskMonitoringService to monitoring dashboard
- [ ] T033 Connect StrategyModuleManager to sandbox execution
- [ ] T034 Connect DeltaNeutralController to all services
- [ ] T035 Structured logging and audit trail
- [ ] T036 Health checks and service status reporting
- [ ] T037 Emergency stop-loss and circuit breakers

## Phase 3.5: Polish
- [ ] T038 [P] Unit tests for ExchangeAccount in tests/unit/test_ExchangeAccount.ts
- [ ] T039 [P] Unit tests for MarketDataFeed in tests/unit/test_MarketDataFeed.ts
- [ ] T040 [P] Unit tests for RiskEnvelope in tests/unit/test_RiskEnvelope.ts
- [ ] T041 [P] Unit tests for HedgeExecution in tests/unit/test_HedgeExecution.ts
- [ ] T042 [P] Unit tests for StrategyModule in tests/unit/test_StrategyModule.ts
- [ ] T043 Performance tests for 8s control loop latency
- [ ] T044 Performance tests for 10s failover SLA
- [ ] T045 [P] Update docs/api.md with new endpoints
- [ ] T046 Remove code duplication and optimize imports
- [ ] T047 Run quickstart.md scenarios and validate results

## Dependencies
- Tests (T006-T012) before implementation (T013-T028)
- T013-T022 (models) block T023-T028 (services)
- T023-T028 (services) block T029-T037 (integration)
- T029-T037 (integration) block T038-T047 (polish)
- T029 blocks T030, T031, T032, T033, T034 (service connections)
- T034 blocks T035, T036, T037 (controller integration)

## Parallel Example
```
# Launch T006-T012 together:
Task: "Contract test account-sync in tests/contract/test_account_sync.ts"
Task: "Contract test hedge-execution in tests/contract/test_hedge_execution.ts"
Task: "Contract test market-data-failover in tests/contract/test_market_data_failover.ts"
Task: "Integration test utilization rebalancing in tests/integration/test_utilization_rebalance.ts"
Task: "Integration test delta hedging in tests/integration/test_delta_hedging.ts"
Task: "Integration test market data failover in tests/integration/test_market_data_failover.ts"
Task: "Integration test strategy module sandbox in tests/integration/test_strategy_sandbox.ts"
```

## Notes
- [P] tasks = different files, no dependencies
- Verify tests fail before implementing
- Commit after each task
- Avoid: vague tasks, same file conflicts
- All tasks must maintain Delta neutrality and risk compliance

## Task Generation Rules
*Applied during main() execution*

1. **From Contracts**:
   - Each contract file → contract test task [P]
   - Each endpoint → implementation task
   
2. **From Data Model**:
   - Each entity → model creation task [P]
   - Relationships → service layer tasks
   
3. **From User Stories**:
   - Each story → integration test [P]
   - Quickstart scenarios → validation tasks

4. **Ordering**:
   - Setup → Tests → Models → Services → Endpoints → Polish
   - Dependencies block parallel execution

## Validation Checklist
*GATE: Checked by main() before returning*

- [x] All contracts have corresponding tests (T006-T008)
- [x] All entities have model tasks (T013-T022)
- [x] All tests come before implementation
- [x] Parallel tasks truly independent
- [x] Each task specifies exact file path
- [x] No task modifies same file as another [P] task
