|
| 1 | +"""Execution Pydantic model — typed accessor for execution dict responses. |
| 2 | +
|
| 3 | +Closes the Execution portion of cueapi-python #24's `model_drift` manifest. |
| 4 | +``ExecutionsResource`` methods (`list`, `get`, `replay`) currently return |
| 5 | +raw dicts; callers can opt into typed accessors via |
| 6 | +``Execution.model_validate(client.executions.get(...))``. Returning the |
| 7 | +typed object directly from resource methods is a separate breaking-change |
| 8 | +PR (would bump major version). |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +from datetime import datetime |
| 14 | +from typing import Any, Dict, List, Optional |
| 15 | + |
| 16 | +from pydantic import BaseModel, Field |
| 17 | + |
| 18 | + |
| 19 | +class OutcomeDetail(BaseModel): |
| 20 | + """Outcome reported by the worker / handler. Set when the execution |
| 21 | + has reached a terminal state and the handler has reported via |
| 22 | + ``POST /v1/executions/{id}/outcome``.""" |
| 23 | + |
| 24 | + success: bool |
| 25 | + result: Optional[str] = None |
| 26 | + error: Optional[str] = None |
| 27 | + metadata: Optional[Dict[str, Any]] = None |
| 28 | + recorded_at: Optional[datetime] = None |
| 29 | + # Evidence fields (Phase 18 Gap 11 — outcome verification). |
| 30 | + external_id: Optional[str] = None |
| 31 | + result_url: Optional[str] = None |
| 32 | + result_ref: Optional[str] = None |
| 33 | + result_type: Optional[str] = None |
| 34 | + summary: Optional[str] = None |
| 35 | + artifacts: Optional[List[Any]] = None |
| 36 | + validation_state: Optional[str] = None |
| 37 | + assertions: Optional[Dict[str, Any]] = None |
| 38 | + model_config = {"extra": "allow"} |
| 39 | + |
| 40 | + |
| 41 | +class Execution(BaseModel): |
| 42 | + """Typed accessor for an execution response. |
| 43 | +
|
| 44 | + Mirrors the server's ``ExecutionResponse`` schema. Use as |
| 45 | + ``Execution.model_validate(client.executions.get(exec_id))`` or |
| 46 | + ``Execution.model_validate(item)`` over each item in |
| 47 | + ``client.executions.list()['executions']``. |
| 48 | + """ |
| 49 | + |
| 50 | + id: str |
| 51 | + cue_id: str |
| 52 | + scheduled_for: datetime |
| 53 | + status: str |
| 54 | + http_status: Optional[int] = None |
| 55 | + response_body: Optional[str] = None |
| 56 | + attempts: Optional[int] = None |
| 57 | + next_retry: Optional[datetime] = None |
| 58 | + error_message: Optional[str] = None |
| 59 | + started_at: Optional[datetime] = None |
| 60 | + delivered_at: Optional[datetime] = None |
| 61 | + last_attempt_at: Optional[datetime] = None |
| 62 | + claimed_by_worker: Optional[str] = None |
| 63 | + claimed_at: Optional[datetime] = None |
| 64 | + last_heartbeat_at: Optional[datetime] = None |
| 65 | + # Hosted PR #589: effective payload the handler/webhook saw at delivery. |
| 66 | + # `payload_override` if set on the execution, else parent cue's payload. |
| 67 | + payload: Optional[Dict[str, Any]] = None |
| 68 | + # Outcome — populated only after handler reports. |
| 69 | + outcome: Optional[OutcomeDetail] = None |
| 70 | + outcome_state: Optional[str] = Field( |
| 71 | + default=None, |
| 72 | + description=( |
| 73 | + "Phase 18 Gap 11: enum tracking outcome verification state. " |
| 74 | + "Values: reported_success / reported_failure / verified_success / " |
| 75 | + "verification_pending / verification_failed / unknown." |
| 76 | + ), |
| 77 | + ) |
| 78 | + triggered_by: Optional[str] = Field( |
| 79 | + default=None, |
| 80 | + description="scheduled / manual_fire / chain / replay", |
| 81 | + ) |
| 82 | + # Chain support (Gap 1 — on_success_fire native chaining). |
| 83 | + chain_parent_id: Optional[str] = None |
| 84 | + chain_depth: Optional[int] = None |
| 85 | + created_at: Optional[datetime] = None |
| 86 | + updated_at: Optional[datetime] = None |
| 87 | + # Forward-compat: server may grow the response over time without the |
| 88 | + # SDK breaking. Same pattern as AlertConfig / VerificationConfig in |
| 89 | + # the Cue model. |
| 90 | + model_config = {"extra": "allow"} |
| 91 | + |
| 92 | + |
| 93 | +class ExecutionList(BaseModel): |
| 94 | + """Typed accessor for ``client.executions.list()`` responses.""" |
| 95 | + |
| 96 | + executions: List[Execution] |
| 97 | + total: int |
| 98 | + limit: int |
| 99 | + offset: int |
| 100 | + model_config = {"extra": "allow"} |
0 commit comments