"""Shared recovery-policy types for AFK configuration and saved task plans.""" from __future__ import annotations from dataclasses import asdict, dataclass from enum import Enum from typing import Any class RecoveryMode(str, Enum): """Whether AFK may make bounded automatic recovery transitions.""" OFF = "off" BOUNDED = "bounded" class RecoveryAction(str, Enum): """A persisted pending action selected by the bounded recovery coordinator.""" NONE = "none" SEMANTIC_REPAIR = "semantic_repair" RESEARCH_SUPPLEMENT = "research_supplement" DECOMPOSE = "decompose" @dataclass(frozen=True) class RecoverySettings: """Explicit, persisted limits for semantic and evidence-based recovery.""" mode: RecoveryMode = RecoveryMode.OFF semantic_repair_limit: int = 1 research_supplement_limit: int = 1 high_reasoning_repair_limit: int = 1 xhigh_requires_explicit_enable: bool = True max_decompositions: int = 1 max_subtasks_per_decomposition: int = 5 max_decomposition_depth: int = 1 require_supervisor_decomposition_approval: bool = True require_supervisor_parent_coherence: bool = True def __post_init__(self) -> None: if not isinstance(self.mode, RecoveryMode): try: object.__setattr__(self, "mode", RecoveryMode(self.mode)) except ValueError as exc: supported = ", ".join(item.value for item in RecoveryMode) raise ValueError(f"recovery.mode must be one of: {supported}.") from exc if not isinstance(self.xhigh_requires_explicit_enable, bool): raise ValueError("recovery.xhigh_requires_explicit_enable must be a boolean.") for name in ( "require_supervisor_decomposition_approval", "require_supervisor_parent_coherence", ): if not isinstance(getattr(self, name), bool): raise ValueError(f"recovery.{name} must be a boolean.") for name in ( "semantic_repair_limit", "research_supplement_limit", "high_reasoning_repair_limit", "max_decompositions", "max_decomposition_depth", ): value = getattr(self, name) if not isinstance(value, int) or isinstance(value, bool) or value < 0: raise ValueError(f"recovery.{name} must be a non-negative integer.") if ( not isinstance(self.max_subtasks_per_decomposition, int) or isinstance(self.max_subtasks_per_decomposition, bool) or self.max_subtasks_per_decomposition < 2 ): raise ValueError("recovery.max_subtasks_per_decomposition must be an integer >= 2.") @classmethod def from_mapping(cls, raw: dict[str, Any] | None) -> "RecoverySettings": if raw is None: return cls() if not isinstance(raw, dict): raise ValueError("'recovery' must be a mapping.") supported_keys = { "mode", "semantic_repair_limit", "research_supplement_limit", "high_reasoning_repair_limit", "xhigh_requires_explicit_enable", "max_decompositions", "max_subtasks_per_decomposition", "max_decomposition_depth", "require_supervisor_decomposition_approval", "require_supervisor_parent_coherence", } unexpected = sorted(set(raw) - supported_keys) if unexpected: raise ValueError(f"Unsupported recovery setting(s): {', '.join(unexpected)}.") raw_mode = raw.get("mode", RecoveryMode.OFF.value) try: mode = raw_mode if isinstance(raw_mode, RecoveryMode) else RecoveryMode(str(raw_mode)) except ValueError as exc: supported = ", ".join(item.value for item in RecoveryMode) raise ValueError(f"'recovery.mode' must be one of: {supported}.") from exc def integer(name: str, default: int) -> int: value = raw.get(name, default) if isinstance(value, bool): raise ValueError(f"'recovery.{name}' must be a non-negative integer.") try: return int(value) except (TypeError, ValueError) as exc: raise ValueError(f"'recovery.{name}' must be a non-negative integer.") from exc xhigh_enabled = raw.get("xhigh_requires_explicit_enable", True) if not isinstance(xhigh_enabled, bool): raise ValueError("'recovery.xhigh_requires_explicit_enable' must be a boolean.") return cls( mode=mode, semantic_repair_limit=integer("semantic_repair_limit", 1), research_supplement_limit=integer("research_supplement_limit", 1), high_reasoning_repair_limit=integer("high_reasoning_repair_limit", 1), xhigh_requires_explicit_enable=xhigh_enabled, max_decompositions=integer("max_decompositions", 1), max_subtasks_per_decomposition=integer("max_subtasks_per_decomposition", 5), max_decomposition_depth=integer("max_decomposition_depth", 1), require_supervisor_decomposition_approval=raw.get( "require_supervisor_decomposition_approval", True ), require_supervisor_parent_coherence=raw.get("require_supervisor_parent_coherence", True), ) def to_dict(self) -> dict[str, Any]: data = asdict(self) data["mode"] = self.mode.value return data