File size: 4,399 Bytes
558b89d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""Typed models for the self-contained server package."""

from __future__ import annotations

from typing import Any, Dict, List, Literal, Optional

from pydantic import BaseModel, Field

from .compat import Action, Observation, State


Difficulty = Literal["easy", "medium", "hard"]
TaskKind = Literal["syntax_fix", "bug_fix", "optimization"]
ActionType = Literal["analyze_code", "edit_code", "run_tests", "submit_solution"]
Category = Literal["bug", "security", "performance", "maintainability", "style", "testing"]
Severity = Literal["critical", "warning", "info"]


class HistoryEntry(BaseModel):
    step: int = Field(..., ge=0)
    action_type: ActionType
    status: str
    reward: float


class RewardDetails(BaseModel):
    value: float
    syntax_reward: float = 0.0
    test_reward: float = 0.0
    quality_bonus: float = 0.0
    correctness_bonus: float = 0.0
    progress_delta: float = 0.0
    stagnation_penalty: float = 0.0
    regression_penalty: float = 0.0
    invalid_action_penalty: float = 0.0
    timeout_penalty: float = 0.0
    reason: str
    prev_score: float = 0.0
    curr_score: float = 0.0
    code_changed: bool = False


class PythonCodeReviewAction(Action):
    action_type: ActionType
    code: Optional[str] = None


class PythonCodeReviewObservation(Observation):
    task_id: str
    title: str = ""
    difficulty: Difficulty
    task_kind: Optional[TaskKind] = None
    task_description: str
    current_code: str
    errors: str
    test_results: str
    visible_tests: List[str] = Field(default_factory=list)
    history: List[HistoryEntry] = Field(default_factory=list)
    attempts_remaining: int = Field(..., ge=0)
    last_action_status: str = ""
    score: float = Field(..., ge=0.0, le=1.0)
    reward_details: RewardDetails = Field(
        default_factory=lambda: RewardDetails(value=0.0, reason="Reset")
    )


class PythonCodeReviewState(State):
    episode_id: str
    step_count: int = Field(default=0, ge=0)
    task_id: Optional[str] = None
    difficulty: Optional[Difficulty] = None
    task_kind: Optional[TaskKind] = None
    attempts_remaining: int = Field(default=0, ge=0)
    current_code: str = ""
    errors: str = ""
    test_results: str = ""
    history: List[HistoryEntry] = Field(default_factory=list)
    score: float = Field(default=0.0, ge=0.0, le=1.0)
    done: bool = False


class TaskDescriptor(BaseModel):
    task_id: str
    title: str
    difficulty: Difficulty
    task_kind: Optional[TaskKind] = None
    task_description: str = ""
    starter_code: str = ""
    visible_tests: List[str] = Field(default_factory=list)
    goal: str = ""
    repo_summary: str = ""
    changed_files: List[str] = Field(default_factory=list)
    available_files: List[str] = Field(default_factory=list)
    max_steps: int = Field(..., ge=1)


class TaskSummary(BaseModel):
    task_id: str
    difficulty: Difficulty
    title: str
    goal: str = ""


class ReviewFinding(BaseModel):
    title: str
    file_path: str = ""
    line: Optional[int] = Field(default=None, ge=1)
    category: Category = "bug"
    severity: Severity = "warning"
    rationale: str = ""
    recommendation: str = ""
    rule_id: str = ""

    @property
    def explanation(self) -> str:
        return self.rationale

    @property
    def suggested_fix(self) -> str:
        return self.recommendation


class DirectReviewResponse(BaseModel):
    issues: List[ReviewFinding] = Field(default_factory=list)
    summary: str = ""
    score: float = Field(default=0.0, ge=0.0, le=1.0)
    improved_code: Optional[str] = None


class TaskGrade(BaseModel):
    score: float = Field(..., ge=0.0, le=1.0)
    syntax_score: float = Field(default=0.0, ge=0.0, le=1.0)
    tests_passed: int = Field(default=0, ge=0)
    tests_total: int = Field(default=0, ge=0)
    quality_score: float = Field(default=0.0, ge=0.0, le=1.0)
    runtime_score: float = Field(default=0.0, ge=0.0, le=1.0)
    timed_out: bool = False
    matched_issue_ids: List[str] = Field(default_factory=list)
    false_positives: int = Field(default=0, ge=0)
    duplicate_findings: int = Field(default=0, ge=0)
    matched_weight: float = Field(default=0.0, ge=0.0, le=1.0)
    details: Dict[str, Any] = Field(default_factory=dict)


class HealthResponse(BaseModel):
    status: Literal["ok"] = "ok"
    environment: str = "python_code_review_env"
    task_count: int = Field(default=0, ge=0)