File size: 5,184 Bytes
8ca7ba0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

from enum import StrEnum
from pathlib import Path
from typing import Any, Literal

import yaml  # type: ignore[import-untyped]
from pydantic import BaseModel, ConfigDict, Field, model_validator


class RepairAction(StrEnum):
    REPROJECT_ENTITY_ON_CANONICAL_SUPPORT = "reproject_entity_on_canonical_support"
    REPLACE_OR_REBIND_ASSET_EXACT_ID = "replace_or_rebind_asset_exact_id"
    HIDE_DUPLICATE_PROXY = "hide_duplicate_proxy"
    RERUN_BRANCH = "rerun_branch"
    RERUN_STAGE_FROM_CHECKPOINT = "rerun_stage_from_checkpoint"
    ADJUST_STAGE_BUDGET = "adjust_stage_budget"
    RERENDER_PREVIEW = "rerender_preview"
    REEXPORT_CANONICAL_USD = "reexport_canonical_usd"


class RepairStep(BaseModel):
    model_config = ConfigDict(extra="forbid")

    action: RepairAction
    entity_ids: list[str] = Field(default_factory=list)
    stage: str | None = None
    branch: str | None = None
    asset_id: str | None = None
    checkpoint: str | None = None
    parameters: dict[str, str | int | float | bool] = Field(default_factory=dict)
    reason: str = Field(min_length=1)


class RepairPlan(BaseModel):
    model_config = ConfigDict(extra="forbid")

    schema_version: Literal["1.0"] = "1.0"
    parent_version: str
    failure_signature: str
    strategy: str
    steps: list[RepairStep] = Field(min_length=1)
    expected_improvement: str


class GateResult(BaseModel):
    model_config = ConfigDict(extra="forbid")

    gate_id: str
    passed: bool
    severity: Literal["info", "warning", "severe"] = "severe"
    message: str
    affected_entities: list[str] = Field(default_factory=list)
    metric: float | None = None
    threshold: float | None = None
    evidence: dict[str, Any] = Field(default_factory=dict)


class QualityReport(BaseModel):
    model_config = ConfigDict(extra="forbid")

    schema_version: Literal["1.0"] = "1.0"
    accepted: bool
    hard_gates_passed: bool
    critic_passed: bool
    critic_score: float | None = None
    gates: list[GateResult]
    failure_signature: str | None = None


class IterationPolicy(BaseModel):
    model_config = ConfigDict(extra="forbid")

    enabled: bool = True
    wall_clock_budget_s: float = Field(default=7200.0, gt=0)
    max_parallel_attempts: int | Literal["auto"] = "auto"
    stagnation_repeat_limit: int = Field(default=2, ge=1)
    critic_min_score: float = Field(default=6.5, ge=0, le=10)
    # Internal/test callers opt in explicitly. User-facing CLI/API request
    # defaults are strict and pass True when constructing this runtime policy.
    critic_required: bool = False
    # None means there is no outer-attempt count limit. Wall-clock,
    # repeated-failure, stagnation, and repair-strategy guards still apply.
    max_total_attempts: int | None = Field(default=None, ge=1)
    first_attempt_budget_multiplier: float = Field(default=1.5, ge=1.25, le=1.6)
    same_failure_signature_limit: int = Field(default=2, ge=2, le=10)
    checkpoint_repairs_only_after_first: bool = True
    repair_policy: Path | None = None
    quality_policy: Path | None = None

    @classmethod
    def from_yaml(cls, path: Path) -> IterationPolicy:
        payload = yaml.safe_load(path.read_text(encoding="utf-8")) or {}
        if "iteration" in payload:
            payload = payload["iteration"]
        return cls.model_validate(payload)


class AttemptManifest(BaseModel):
    model_config = ConfigDict(extra="forbid")

    schema_version: Literal["1.0"] = "1.0"
    version: str
    status: Literal["running", "failed", "passed", "superseded"]
    parent_version: str | None = None
    design_contract: str | None = None
    fixed_dag_sha256: str | None = None
    fixed_graph_id: str | None = None
    started_at: str
    completed_at: str | None = None
    duration_s: float | None = None
    repair_plan: str | None = None
    quality_report: str | None = None
    failure_signature: str | None = None
    error: str | None = None


class IterationPolicyRequest(BaseModel):
    """YAML-facing policy; paths remain portable and are resolved by the CLI."""

    model_config = ConfigDict(extra="forbid")

    enabled: bool = True
    wall_clock_budget_s: float = Field(default=7200.0, gt=0)
    max_parallel_attempts: int | Literal["auto"] = "auto"
    stagnation_repeat_limit: int = Field(default=2, ge=1)
    critic_min_score: float = Field(default=6.5, ge=0, le=10)
    critic_required: bool = True
    max_total_attempts: int | None = Field(default=None, ge=1)
    first_attempt_budget_multiplier: float = Field(default=1.5, ge=1.25, le=1.6)
    same_failure_signature_limit: int = Field(default=2, ge=2, le=10)
    checkpoint_repairs_only_after_first: bool = True
    repair_policy: str = "configs/supervisor/repair_policy_v1.yaml"
    quality_policy: str = "configs/supervisor/quality_gates_strict_v1.yaml"

    @model_validator(mode="after")
    def reject_absolute_policy_paths(self) -> IterationPolicyRequest:
        for value in (self.repair_policy, self.quality_policy):
            if Path(value).is_absolute() or ".." in Path(value).parts:
                raise ValueError("supervisor policy paths must be repository-relative")
        return self