# Phase 7 Summary: Mission Generation Pipeline

## What Was Built

A complete mission generation pipeline that transforms codebase briefings into structured investigation missions.

### New Modules (7 files)

1. **`llm/missions/schemas.py`** — Pydantic models for LLM structured output
   - `MissionFileRefResponse` — file path with line range and description
   - `MissionItemResponse` — single mission with title, description, difficulty, narrative, prerequisites
   - `ChainResponse` — ordered group of related missions
   - `CategoryMissionsResponse` — complete response for one category

2. **`llm/missions/prompts.py`** — Mission generation prompt template
   - Film Noir theming instructions for titles and narrative
   - Difficulty guidelines (quick=10-15min, medium=20-30min, deep_dive=30-45min)
   - Category-specific context injection points

3. **`llm/missions/context.py`** — Category-specific context builder
   - Filters modules by category hints
   - Builds category-specific sections (testing, dependencies, technical debt, etc.)
   - Includes security findings when relevant
   - Lists previous mission titles for cross-category awareness

4. **`llm/missions/generator.py`** — Per-category mission generator
   - Single LLM call per category via `generate_category_missions()`
   - Retry handling via `with_llm_retry()`
   - Raises `MissionGenerationError` on permanent failure

5. **`llm/missions/validation.py`** — Validation and auto-fix utilities
   - `validate_file_refs()` — checks paths exist, clamps line ranges, detects path traversal
   - `validate_prerequisites()` — detects/breaks cycles, enforces max depth, ensures roots exist
   - `validate_category_missions()` — orchestrates all validation passes
   - Never raises — always returns results with warnings

6. **`llm/missions/pipeline.py`** — LangGraph pipeline with checkpointing
   - `run_mission_pipeline()` — processes all 7 categories in order
   - `CATEGORY_ORDER` — Architecture first, Technical Debt last
   - SQLite checkpointing with fallback to in-memory
   - Partial results returned on failure
   - Resume capability via deterministic thread IDs

7. **`llm/missions/__init__.py`** — Package exports

### Modified Files

- **`llm/exceptions.py`** — Added `MissionGenerationError` with `category` and `attempts` attributes
- **`llm/__init__.py`** — Added mission pipeline exports

### Key Design Decisions

1. **7 separate LLM calls** (not one monolithic call) — each category generates 3-8 missions
2. **Category order matters** — Architecture first (maps territory), Technical Debt last (needs full context)
3. **Validation never raises** — all issues produce warnings and auto-fixes
4. **Partial results always returned** — even if some categories fail, completed missions are available
5. **Checkpointing via SQLite** — enables resume from last successful category

### Security Measures

- File paths validated against path traversal (must be under repo root)
- Line numbers clamped to actual file bounds
- No API keys in logs, errors, or checkpoint state
- Checkpoint files stored in user-controlled campaign directory

### Testing Approach

Phase 8 will add:
- Unit tests for validation functions
- Property-based tests for cycle detection, depth enforcement, root guarantee
- Integration tests for the full pipeline (mocked LLM)

## Files Created

| File | Lines | Purpose |
|------|-------|---------|
| `llm/missions/schemas.py` | ~200 | Pydantic models for LLM output |
| `llm/missions/prompts.py` | ~100 | Prompt template with Film Noir theming |
| `llm/missions/context.py` | ~440 | Category-specific context builder |
| `llm/missions/generator.py` | ~145 | Per-category mission generator |
| `llm/missions/validation.py` | ~505 | Validation and auto-fix utilities |
| `llm/missions/pipeline.py` | ~270 | LangGraph pipeline with checkpointing |
| `llm/missions/__init__.py` | ~40 | Package exports |

## Verification

- All ruff checks pass
- All Python files compile successfully
- Imports verified via py_compile
- Security checklist complete (all items verified in code)
