File size: 8,552 Bytes
6daf142
 
 
 
1607c63
 
 
 
 
6daf142
 
 
 
 
 
 
 
1607c63
 
 
 
 
 
 
 
6daf142
 
 
 
 
1607c63
6daf142
1607c63
 
 
 
 
 
 
 
 
 
 
 
 
6daf142
 
1607c63
 
 
 
 
 
 
 
 
6daf142
1607c63
6daf142
 
 
 
 
 
 
1607c63
6daf142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1607c63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6daf142
 
 
 
 
 
1607c63
6daf142
 
 
 
 
1607c63
 
6daf142
 
 
 
1607c63
 
 
 
 
 
 
 
 
 
 
 
 
 
6daf142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1607c63
 
 
 
 
 
 
 
6daf142
1607c63
 
 
 
 
6daf142
1607c63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6daf142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1607c63
 
 
6daf142
 
 
 
1607c63
 
 
 
 
 
 
 
 
 
 
 
c86f59b
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""
Data models for the API Contract Validator Environment.

Defines typed Action, Observation, and State models that form the
contract between the agent and the environment across three phases:

    Phase 1 β€” Detection      action_type='report_violation'
    Phase 2 β€” Impact Tracing action_type='trace_impact'
    Phase 3 β€” Fix & Verify   action_type='propose_fix' | 'validate_fix'
"""

from typing import Any, Dict, List, Optional

from openenv.core.env_server.types import Action, Observation, State
from pydantic import Field


# ── Action types ──────────────────────────────────────────────────────────

ACTION_REPORT_VIOLATION = "report_violation"
ACTION_TRACE_IMPACT = "trace_impact"
ACTION_PROPOSE_FIX = "propose_fix"
ACTION_VALIDATE_FIX = "validate_fix"


# ---------------------------------------------------------------------------
# Action β€” what the agent submits each step
# ---------------------------------------------------------------------------

class ValidatorAction(Action):
    """A single agent action.

    The ``action_type`` field selects which phase the action belongs to:

      * ``report_violation`` (Phase 1, default) β€” uses ``field_path`` and
        ``violation_type``. Special ``field_path`` values: ``DONE`` ends the
        episode, ``HINT`` requests a location clue at -0.5 reward.
      * ``trace_impact`` (Phase 2) β€” uses ``affected_services`` and
        ``reasoning``.
      * ``propose_fix`` / ``validate_fix`` (Phase 3) β€” uses
        ``fix_strategy``, ``spec_patch``, ``rationale``.

    All fields are optional so a single dataclass can carry every action
    type. Phase 1 callers that only set ``field_path`` + ``violation_type``
    continue to work without modification.
    """

    action_type: str = Field(
        default=ACTION_REPORT_VIOLATION,
        description=(
            "One of 'report_violation' (Phase 1), 'trace_impact' (Phase 2), "
            "'propose_fix' (Phase 3), 'validate_fix' (Phase 3)."
        ),
    )

    # ── Phase 1 β€” detection ──────────────────────────────────────────
    field_path: str = Field(
        default="",
        description=(
            "Dot-notation path to the violated field, e.g. 'user.email'. "
            "Use 'DONE' to signal no more violations. "
            "Use 'HINT' to receive a location hint at -0.5 reward cost."
        ),
    )
    violation_type: str = Field(
        default="",
        description=(
            "Category of violation: type_mismatch | missing_required | "
            "invalid_enum | format_error | extra_field | breaking_change | "
            "cross_field_constraint"
        ),
    )
    description: str = Field(
        default="",
        description="Human-readable explanation of the violation.",
    )
    suggested_fix: str = Field(
        default="",
        description="Optional suggested correction for the violation.",
    )

    # ── Phase 2 β€” impact tracing ─────────────────────────────────────
    affected_services: List[str] = Field(
        default_factory=list,
        description=(
            "Phase 2 β€” names of downstream services the agent believes "
            "are impacted by the breaking change."
        ),
    )
    reasoning: str = Field(
        default="",
        description="Phase 2 β€” brief justification for the impact assessment.",
    )

    # ── Phase 3 β€” fix & verify ───────────────────────────────────────
    fix_strategy: str = Field(
        default="",
        description=(
            "Phase 3 β€” one of: field_alias | version_bump | "
            "deprecation_window | dual_write | consumer_patch."
        ),
    )
    spec_patch: Dict[str, Any] = Field(
        default_factory=dict,
        description="Phase 3 β€” JSON-shaped patch to apply to the producer spec.",
    )
    rationale: str = Field(
        default="",
        description="Phase 3 β€” why the proposed fix preserves backward compatibility.",
    )


# ---------------------------------------------------------------------------
# Observation β€” what the agent sees after each step
# ---------------------------------------------------------------------------

class ValidatorObservation(Observation):
    """Environment response after each agent action.

    Inherits ``done: bool`` and ``reward: Optional[float]`` from the
    ``Observation`` base class.
    """

    # ── universal ────────────────────────────────────────────────────
    task_name: str = Field(default="", description="Current task identifier.")
    task_description: str = Field(
        default="",
        description="Natural-language instructions for the agent.",
    )
    phase: str = Field(
        default="detection",
        description="Current episode phase: detection | tracing | fix_proposal.",
    )
    feedback: str = Field(
        default="",
        description="Result of the last submitted action.",
    )
    max_steps: int = Field(
        default=0,
        description="Maximum steps allowed for the current episode.",
    )

    # ── Phase 1 β€” detection ──────────────────────────────────────────
    api_spec: Dict[str, Any] = Field(
        default_factory=dict,
        description="The OpenAPI specification (or spec diff for hard tasks).",
    )
    payload: Dict[str, Any] = Field(
        default_factory=dict,
        description="The API request/response payload to validate.",
    )
    violations_found: List[Dict[str, str]] = Field(
        default_factory=list,
        description="Violations the agent has correctly identified so far.",
    )
    violations_remaining: int = Field(
        default=0,
        description="Number of planted violations still undetected.",
    )

    # ── Phase 2 β€” impact tracing ─────────────────────────────────────
    service_graph: Dict[str, Any] = Field(
        default_factory=dict,
        description=(
            "Phase 2 β€” enterprise service graph: {producer: spec, "
            "consumers: {name: {spec_excerpt, fields_consumed}}}."
        ),
    )
    consumers_traced: List[str] = Field(
        default_factory=list,
        description="Phase 2 β€” affected services the agent has correctly identified.",
    )
    total_consumers: int = Field(
        default=0,
        description="Phase 2 β€” total number of services in the graph.",
    )

    # ── Phase 3 β€” fix & verify ───────────────────────────────────────
    detected_violation: Dict[str, Any] = Field(
        default_factory=dict,
        description="Phase 3 β€” the breaking change that needs a fix.",
    )
    consumer_specs: Dict[str, Any] = Field(
        default_factory=dict,
        description="Phase 3 β€” consumer specs to validate the fix against.",
    )
    fix_validation_results: Dict[str, Any] = Field(
        default_factory=dict,
        description=(
            "Phase 3 β€” per-consumer validation results from the last "
            "validate_fix call."
        ),
    )


# ---------------------------------------------------------------------------
# State β€” internal environment state (includes ground-truth)
# ---------------------------------------------------------------------------

class ValidatorState(State):
    """Full environment state including ground-truth violations.

    Inherits ``episode_id: Optional[str]`` and ``step_count: int`` from
    the ``State`` base class.
    """

    task_name: str = ""
    phase: str = "detection"

    # Phase 1
    total_violations: int = 0
    correct_reports: int = 0
    false_positives: int = 0
    duplicate_reports: int = 0

    # Phase 2
    total_consumers: int = 0
    consumers_correctly_traced: int = 0
    consumers_missed: int = 0
    consumers_false_flagged: int = 0

    # Phase 3
    fix_attempts: int = 0
    fix_validated: bool = False
    fix_breaks_consumers: int = 0

    score: float = 0.01