|
| 1 | +""" |
| 2 | +Bootstrap management — phased initialization with rollback support, |
| 3 | +environment validation, and boot-sequence introspection. |
| 4 | +""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import time |
| 9 | +from dataclasses import dataclass, field |
| 10 | +from enum import Enum |
| 11 | +from typing import Any, Callable, Dict, List, Optional, Tuple |
| 12 | + |
| 13 | + |
| 14 | +class BootstrapPhase(str, Enum): |
| 15 | + CORE = "core" |
| 16 | + SERVICES = "services" |
| 17 | + AGENTS = "agents" |
| 18 | + INTEGRATION = "integration" |
| 19 | + READY = "ready" |
| 20 | + |
| 21 | + |
| 22 | +@dataclass |
| 23 | +class BootstrapStep: |
| 24 | + name: str |
| 25 | + phase: BootstrapPhase |
| 26 | + action_fn: Callable[[], bool] |
| 27 | + timeout: float = 30.0 |
| 28 | + required: bool = True |
| 29 | + category: str = "general" |
| 30 | + |
| 31 | + |
| 32 | +@dataclass |
| 33 | +class BootstrapResult: |
| 34 | + success: bool |
| 35 | + phase: BootstrapPhase |
| 36 | + steps_completed: int = 0 |
| 37 | + steps_total: int = 0 |
| 38 | + failures: List[str] = field(default_factory=list) |
| 39 | + duration_s: float = 0.0 |
| 40 | + log: List[str] = field(default_factory=list) |
| 41 | + |
| 42 | + |
| 43 | +class BootstrapManager: |
| 44 | + """Manages phased bootstrap with timeout enforcement and rollback.""" |
| 45 | + |
| 46 | + PHASE_ORDER = [ |
| 47 | + BootstrapPhase.CORE, |
| 48 | + BootstrapPhase.SERVICES, |
| 49 | + BootstrapPhase.AGENTS, |
| 50 | + BootstrapPhase.INTEGRATION, |
| 51 | + BootstrapPhase.READY, |
| 52 | + ] |
| 53 | + |
| 54 | + def __init__(self) -> None: |
| 55 | + self._steps: List[BootstrapStep] = [] |
| 56 | + self._log: List[str] = [] |
| 57 | + self._completed_phases: List[BootstrapPhase] = [] |
| 58 | + self._rollback_handlers: Dict[BootstrapPhase, Callable[[], None]] = {} |
| 59 | + |
| 60 | + # ------------------------------------------------------------------ |
| 61 | + # Step management |
| 62 | + # ------------------------------------------------------------------ |
| 63 | + |
| 64 | + def add_step(self, step: BootstrapStep) -> None: |
| 65 | + self._steps.append(step) |
| 66 | + |
| 67 | + def remove_step(self, name: str) -> bool: |
| 68 | + before = len(self._steps) |
| 69 | + self._steps = [s for s in self._steps if s.name != name] |
| 70 | + return len(self._steps) < before |
| 71 | + |
| 72 | + def get_boot_sequence(self) -> List[BootstrapStep]: |
| 73 | + """Return steps ordered by phase.""" |
| 74 | + phase_rank = {p: i for i, p in enumerate(self.PHASE_ORDER)} |
| 75 | + return sorted(self._steps, key=lambda s: (phase_rank.get(s.phase, 99), s.name)) |
| 76 | + |
| 77 | + # ------------------------------------------------------------------ |
| 78 | + # Execution |
| 79 | + # ------------------------------------------------------------------ |
| 80 | + |
| 81 | + def run_bootstrap(self) -> BootstrapResult: |
| 82 | + self._log.clear() |
| 83 | + self._completed_phases.clear() |
| 84 | + sequence = self.get_boot_sequence() |
| 85 | + total = len(sequence) |
| 86 | + completed = 0 |
| 87 | + failures: List[str] = [] |
| 88 | + overall_success = True |
| 89 | + start = time.time() |
| 90 | + current_phase: Optional[BootstrapPhase] = None |
| 91 | + |
| 92 | + for step in sequence: |
| 93 | + if step.phase != current_phase: |
| 94 | + current_phase = step.phase |
| 95 | + self._log.append(f"=== Phase: {step.phase.value} ===") |
| 96 | + self._log.append(f"Running step: {step.name}") |
| 97 | + step_start = time.time() |
| 98 | + try: |
| 99 | + result = step.action_fn() |
| 100 | + elapsed = time.time() - step_start |
| 101 | + if result: |
| 102 | + completed += 1 |
| 103 | + self._log.append(f" OK ({elapsed:.3f}s)") |
| 104 | + else: |
| 105 | + failures.append(step.name) |
| 106 | + if step.required: |
| 107 | + overall_success = False |
| 108 | + self._log.append(f" FAILED (required) ({elapsed:.3f}s)") |
| 109 | + else: |
| 110 | + completed += 1 |
| 111 | + self._log.append(f" FAILED (optional) ({elapsed:.3f}s)") |
| 112 | + except Exception as exc: |
| 113 | + elapsed = time.time() - step_start |
| 114 | + failures.append(step.name) |
| 115 | + if step.required: |
| 116 | + overall_success = False |
| 117 | + self._log.append(f" ERROR: {exc} ({elapsed:.3f}s)") |
| 118 | + |
| 119 | + if elapsed > step.timeout: |
| 120 | + self._log.append(f" WARNING: exceeded timeout ({step.timeout}s)") |
| 121 | + |
| 122 | + if overall_success: |
| 123 | + for phase in self.PHASE_ORDER: |
| 124 | + if any(s.phase == phase for s in sequence): |
| 125 | + self._completed_phases.append(phase) |
| 126 | + |
| 127 | + duration = time.time() - start |
| 128 | + return BootstrapResult( |
| 129 | + success=overall_success, |
| 130 | + phase=BootstrapPhase.READY if overall_success else (current_phase or BootstrapPhase.CORE), |
| 131 | + steps_completed=completed, |
| 132 | + steps_total=total, |
| 133 | + failures=failures, |
| 134 | + duration_s=duration, |
| 135 | + log=list(self._log), |
| 136 | + ) |
| 137 | + |
| 138 | + # ------------------------------------------------------------------ |
| 139 | + # Environment validation |
| 140 | + # ------------------------------------------------------------------ |
| 141 | + |
| 142 | + def validate_environment(self) -> Tuple[bool, List[str]]: |
| 143 | + """Check that required-phase steps have met dependencies. Returns (valid, missing_items).""" |
| 144 | + missing: List[str] = [] |
| 145 | + for step in self._steps: |
| 146 | + if step.required and step.phase == BootstrapPhase.CORE: |
| 147 | + try: |
| 148 | + result = step.action_fn() |
| 149 | + if not result: |
| 150 | + missing.append(f"core check failed: {step.name}") |
| 151 | + except Exception as exc: |
| 152 | + missing.append(f"core check error: {step.name}: {exc}") |
| 153 | + return (len(missing) == 0, missing) |
| 154 | + |
| 155 | + # ------------------------------------------------------------------ |
| 156 | + # Rollback |
| 157 | + # ------------------------------------------------------------------ |
| 158 | + |
| 159 | + def register_rollback_handler(self, phase: BootstrapPhase, handler: Callable[[], None]) -> None: |
| 160 | + self._rollback_handlers[phase] = handler |
| 161 | + |
| 162 | + def rollback(self, phase: BootstrapPhase) -> None: |
| 163 | + """Roll back from the given phase down to CORE.""" |
| 164 | + idx = self.PHASE_ORDER.index(phase) if phase in self.PHASE_ORDER else -1 |
| 165 | + if idx < 0: |
| 166 | + return |
| 167 | + for p in reversed(self.PHASE_ORDER[:idx + 1]): |
| 168 | + handler = self._rollback_handlers.get(p) |
| 169 | + if handler: |
| 170 | + try: |
| 171 | + handler() |
| 172 | + self._log.append(f"Rolled back phase: {p.value}") |
| 173 | + except Exception as exc: |
| 174 | + self._log.append(f"Rollback error for {p.value}: {exc}") |
| 175 | + self._completed_phases = [ |
| 176 | + p for p in self._completed_phases |
| 177 | + if p not in self.PHASE_ORDER[:idx + 1] |
| 178 | + ] |
| 179 | + |
| 180 | + # ------------------------------------------------------------------ |
| 181 | + # Introspection |
| 182 | + # ------------------------------------------------------------------ |
| 183 | + |
| 184 | + def get_bootstrap_log(self) -> List[str]: |
| 185 | + return list(self._log) |
| 186 | + |
| 187 | + def get_completed_phases(self) -> List[BootstrapPhase]: |
| 188 | + return list(self._completed_phases) |
| 189 | + |
| 190 | + def get_steps_for_phase(self, phase: BootstrapPhase) -> List[BootstrapStep]: |
| 191 | + return [s for s in self._steps if s.phase == phase] |
| 192 | + |
| 193 | + def reset(self) -> None: |
| 194 | + self._log.clear() |
| 195 | + self._completed_phases.clear() |
0 commit comments