"""Caller-supplied time budgets for optional search work.""" from __future__ import annotations from collections.abc import Callable, Mapping from dataclasses import dataclass import math import time class SearchBudgetExhausted(RuntimeError): """Search stopped before finding a completed feasible candidate.""" def _seconds(value: float, name: str, *, positive: bool = False) -> float: if isinstance(value, bool): raise ValueError(f"{name} must be finite seconds") value = float(value) if not math.isfinite(value) or value < 0 or (positive and value == 0): raise ValueError(f"{name} must be finite {'positive' if positive else 'nonnegative'} seconds") return value @dataclass(frozen=True) class BudgetDecision: operation: str remaining_seconds: float operation_seconds: float finalization_seconds: float @property def allowed(self) -> bool: return self.remaining_seconds >= self.operation_seconds + self.finalization_seconds class ExecutionBudget: """Reserve finalization time using supplied estimates and observed durations. ``remaining_seconds`` and a callable ``finalization_seconds`` are read at each operation boundary. Estimates must cover the complete operation; finalization must include every mandatory stage after search. No running operation is interrupted. ``duration_margin`` is a caller-selected factor on observed durations, not a guarantee about future execution time. """ def __init__( self, remaining_seconds: Callable[[], float], finalization_seconds: float | Callable[[], float], operation_estimates: Mapping[str, float], *, duration_margin: float = 1.0, clock: Callable[[], float] = time.monotonic, ): if not callable(remaining_seconds) or not callable(clock): raise TypeError("remaining_seconds and clock must be callable") if not callable(finalization_seconds): finalization_seconds = _seconds(finalization_seconds, "finalization_seconds") self.remaining_seconds = remaining_seconds self.finalization_seconds = finalization_seconds self.operation_estimates = { name: _seconds(seconds, name, positive=True) for name, seconds in operation_estimates.items() } if not self.operation_estimates or any( not isinstance(name, str) or not name for name in self.operation_estimates ): raise ValueError("operation_estimates requires named operations") self.duration_margin = _seconds(duration_margin, "duration_margin", positive=True) if self.duration_margin < 1: raise ValueError("duration_margin must be at least one") self.clock = clock self.observed_seconds: dict[str, float] = {} def require(self, operation: str) -> None: if operation not in self.operation_estimates: raise ValueError(f"Missing time estimate for {operation}") def observe(self, operation: str, seconds: float) -> None: self.require(operation) self.observed_seconds[operation] = max( self.observed_seconds.get(operation, 0.0), _seconds(seconds, operation) ) def decide(self, operation: str) -> BudgetDecision: self.require(operation) remaining = self.remaining_seconds() if isinstance(remaining, bool) or not math.isfinite(float(remaining)): raise ValueError("remaining_seconds must return finite seconds") finalization = self.finalization_seconds reserve = _seconds(finalization() if callable(finalization) else finalization, "finalization_seconds") estimate = max( self.operation_estimates[operation], self.duration_margin * self.observed_seconds.get(operation, 0.0), ) return BudgetDecision(operation, max(0.0, float(remaining)), estimate, reserve) def admit_optional_work(pipeline, operation: str) -> bool: """Log a scheduling decision when the caller supplies a budget.""" budget = getattr(pipeline, "execution_budget", None) if budget is None: return True decision = budget.decide(operation) pipeline.log( f"Time budget: {operation}, {'start' if decision.allowed else 'finalize'}, " f"remaining={decision.remaining_seconds:.1f}s, " f"estimate={decision.operation_seconds:.1f}s, " f"finalization={decision.finalization_seconds:.1f}s" ) return decision.allowed