|
| 1 | +"""Scenario entities for bounded problem-solving run windows.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from dataclasses import dataclass |
| 6 | +from datetime import datetime |
| 7 | +from enum import Enum |
| 8 | + |
| 9 | + |
| 10 | +class ScenarioOutcome(str, Enum): |
| 11 | + """Agent-facing terminal outcomes for one problem-solving scenario.""" |
| 12 | + |
| 13 | + SOLVED = "solved" |
| 14 | + ABANDONED = "abandoned" |
| 15 | + |
| 16 | + |
| 17 | +class ProblemRunStatus(str, Enum): |
| 18 | + """Durable storage statuses for explicit problem-run windows.""" |
| 19 | + |
| 20 | + OPEN = "open" |
| 21 | + CLOSED = "closed" |
| 22 | + ABANDONED = "abandoned" |
| 23 | + |
| 24 | + |
| 25 | +@dataclass(frozen=True, kw_only=True) |
| 26 | +class ProblemRun: |
| 27 | + """One bounded problem-solving scenario stored for token/ROI analysis.""" |
| 28 | + |
| 29 | + id: str |
| 30 | + repo_id: str |
| 31 | + status: ProblemRunStatus |
| 32 | + opened_at: datetime |
| 33 | + opened_by: str |
| 34 | + thread_id: str | None = None |
| 35 | + host_app: str | None = None |
| 36 | + host_session_key: str | None = None |
| 37 | + episode_id: str | None = None |
| 38 | + opened_event_id: str | None = None |
| 39 | + closed_at: datetime | None = None |
| 40 | + closed_by: str | None = None |
| 41 | + closed_event_id: str | None = None |
| 42 | + problem_memory_id: str | None = None |
| 43 | + solution_memory_id: str | None = None |
| 44 | + created_at: datetime | None = None |
| 45 | + updated_at: datetime | None = None |
| 46 | + |
| 47 | + def __post_init__(self) -> None: |
| 48 | + """Keep invalid run windows from crossing core boundaries.""" |
| 49 | + |
| 50 | + for field_name in ("id", "repo_id", "opened_by"): |
| 51 | + value = getattr(self, field_name) |
| 52 | + if not isinstance(value, str) or not value.strip(): |
| 53 | + raise ValueError(f"{field_name} must be a non-empty string") |
| 54 | + if self.status == ProblemRunStatus.OPEN and self.closed_at is not None: |
| 55 | + raise ValueError("open problem runs cannot have closed_at") |
| 56 | + if self.status != ProblemRunStatus.OPEN and self.closed_at is None: |
| 57 | + raise ValueError("closed problem runs require closed_at") |
| 58 | + if self.closed_at is not None and self.closed_at < self.opened_at: |
| 59 | + raise ValueError("closed_at must be greater than or equal to opened_at") |
| 60 | + |
| 61 | + |
| 62 | +def outcome_to_problem_run_status(outcome: ScenarioOutcome) -> ProblemRunStatus: |
| 63 | + """Map the agent-facing scenario outcome to durable run status.""" |
| 64 | + |
| 65 | + if outcome == ScenarioOutcome.SOLVED: |
| 66 | + return ProblemRunStatus.CLOSED |
| 67 | + return ProblemRunStatus.ABANDONED |
0 commit comments