APISHIFT / models.py
yaswanth169's picture
Initial APIShift env push
3040bf7 verified
Raw History Blame Contribute Delete
4.64 kB
"""APIShift Pydantic models — Action, Observation, State.
These define the contract between the Manager agent (GRPO-trained)
and the OpenEnv environment server. Specialist agents are invoked
internally by the environment in response to dispatch_* actions.
"""
from typing import Any, Dict, List, Literal, Optional
from pydantic import BaseModel, Field
try:
from openenv.core.env_server import Action, Observation, State
except ImportError:
class Action(BaseModel):
pass
class Observation(BaseModel):
done: bool = False
reward: Optional[float] = None
class State(BaseModel):
episode_id: Optional[str] = None
step_count: int = 0
# Manager's action vocabulary — kept small and discrete for stable GRPO training.
ManagerCommand = Literal[
"inspect", # planning step (no specialist call); cheap
"dispatch_diff", # ask DiffSpecialist to classify v1 vs v2 changes
"classify_impact", # tag detected changes with severity / blast radius
"dispatch_patch", # ask PatchSpecialist to write a patch for one change
"dispatch_test", # run TestSpecialist (sandbox + pytest/jest)
"dispatch_rollback", # ask RollbackSpecialist for a rollback plan
"read_memory", # pull top-K relevant lessons (consumes a step)
"submit", # finalize the migration package; ends the episode
]
class APIShiftAction(Action):
"""A single Manager action.
The Manager emits one of these per step. The environment routes it
to the appropriate specialist (or scoring path) and returns the next
observation plus a per-step reward.
"""
command: ManagerCommand
target: Optional[str] = None
payload: Dict[str, Any] = Field(default_factory=dict)
rationale: str = ""
class DetectedChange(BaseModel):
"""One classified change between v1 and v2 specs."""
change_id: str
change_type: str
location: str
severity: Literal["breaking", "non_breaking", "unknown"] = "unknown"
blast_radius: Literal["low", "medium", "high"] = "medium"
summary: str = ""
class GeneratedPatch(BaseModel):
"""One code patch produced by PatchSpecialist."""
patch_id: str
file_path: str
diff: str
targets_change_id: str
confidence: float = 0.0
class TestRunResult(BaseModel):
"""Outcome of one TestSpecialist invocation."""
passed: int = 0
failed: int = 0
total: int = 0
failure_logs: List[str] = Field(default_factory=list)
class RollbackPlan(BaseModel):
"""Output of RollbackSpecialist."""
plan_id: str
revert_diffs: List[str] = Field(default_factory=list)
documentation: str = ""
is_reversible: bool = False
class MemoryHit(BaseModel):
"""One lesson retrieved from lessons.md."""
lesson_id: str
pattern: str
what_works: str
what_fails: str
confidence: float
class APIShiftObservation(Observation):
"""What the Manager sees on each step."""
# Operating-manual section relevant to current step (loaded from apishift_program.md)
program_section: str = ""
# Task framing
scenario_id: str = ""
task_description: str = ""
framework: str = ""
language: str = ""
# Spec content (chunked to stay within Qwen2.5-7B context)
v1_spec_summary: str = ""
v2_spec_summary: str = ""
v1_spec_chunk: str = ""
v2_spec_chunk: str = ""
# Code surface
client_files: List[str] = Field(default_factory=list)
inspected_file_content: str = ""
# Episode state (built up across steps)
detected_changes: List[DetectedChange] = Field(default_factory=list)
generated_patches: List[GeneratedPatch] = Field(default_factory=list)
last_test_result: Optional[TestRunResult] = None
rollback_plan: Optional[RollbackPlan] = None
memory_hits: List[MemoryHit] = Field(default_factory=list)
# Recent past performance (read from results.tsv)
recent_episode_summary: str = ""
# Progress + budget
quality_score: float = 0.0
steps_taken: int = 0
max_steps: int = 30
available_commands: List[str] = Field(default_factory=list)
# Per-step feedback
message: str = ""
last_action_error: Optional[str] = None
class APIShiftState(State):
"""Server-side episode metadata (not all surfaced to the agent)."""
scenario_id: str = ""
difficulty: str = ""
initial_quality: float = 0.0
current_quality: float = 0.0
component_rewards: Dict[str, float] = Field(default_factory=dict)
diff_call_count: int = 0
test_call_count: int = 0
rollback_called: bool = False
submitted: bool = False