# Domain Types

Core schemas and models used across the codeflash-internal backend.

## Request/Response Schemas (`core/shared/optimizer_models.py`)

### `OptimizedCandidateSource` enum

How a candidate was generated: `OPTIMIZE`, `OPTIMIZE_LP`, `REFINE`, `REPAIR`, `ADAPTIVE`, `JIT_REWRITE`.

### `OptimizeSchema` (ninja.Schema)

Main optimization request:
- `source_code: str` — code to optimize
- `dependency_code: str | None` — read-only dependency code
- `trace_id: str` — unique request identifier
- `language: str = "python"` — language identifier (python, javascript, typescript)
- `language_version: str | None` — e.g., "ES2022", "Node 20", or Python version
- `n_candidates: int = 5` — number of optimization candidates to generate
- `is_async: bool | None = False` — whether the function is async
- `python_version: str | None` — optional, for multi-language support
- `experiment_metadata`, `codeflash_version`, `current_username`, `repo_owner`, `repo_name` — tracking fields

### `OptimizeSchemaLP` (ninja.Schema)

Line profiler variant — same as `OptimizeSchema` plus:
- `line_profiler_results: str | None` — line profiler output
- `n_candidates: int = 6` — default higher for line profiler optimization

## Domain Models (`core/log_features/models.py`)

### `OptimizationFeatures` (Django Model)

Stores per-optimization data:
- `trace_id` (PK) — unique optimization identifier
- `original_code` — original source code
- `optimizations_raw` / `optimizations_post` — raw and postprocessed candidate code
- `speedup_ratio` — best speedup achieved
- `test_results` — test execution results
- Approval workflow: `approval_status`, `approved_by`, `approved_at`

### `OptimizationEvents` (Django Model)

Event logging for optimization lifecycle:
- `pr_created`, `pr_merged`, `pr_closed` — PR events
- `llm_cost` — total LLM API cost
- `speedup_x` / `speedup_pct` — performance improvement metrics

### `Repositories` (Django Model)

GitHub repository metadata:
- `installation_id` — GitHub App installation ID
- `is_private` — whether the repo is private
- `optimizations_limit` / `optimizations_used` — usage tracking

## Response Schemas (`core/shared/optimizer_schemas.py`)

- `OptimizeResponseSchema` — successful optimization response with candidates
- `OptimizeErrorResponseSchema` — error response with message
