Adjust recursion depth based on device thermal and battery state. Rules (applied in priority order): - temp >= temp_critical (80°C) → force depth 0 (no recursion) - temp >= temp_warn (65°C) → cap depth at 1 - battery <= batt_low (15%) AND not charging → cap depth at 1
(requested_depth: int)
| 107 | # ── Adaptive depth (Phase 8) ──────────────────────────────────────────────── |
| 108 | |
| 109 | def get_adaptive_depth(requested_depth: int) -> int: |
| 110 | """ |
| 111 | Adjust recursion depth based on device thermal and battery state. |
| 112 | |
| 113 | Rules (applied in priority order): |
| 114 | - temp >= temp_critical (80°C) → force depth 0 (no recursion) |
| 115 | - temp >= temp_warn (65°C) → cap depth at 1 |
| 116 | - battery <= batt_low (15%) AND not charging → cap depth at 1 |
| 117 | - battery <= batt_critical (5%) AND not charging → force depth 0 |
| 118 | - charging or cool → use requested_depth as-is |
| 119 | |
| 120 | Returns the (possibly reduced) max_depth. |
| 121 | """ |
| 122 | if not THERMAL_CONFIG.get("enabled", True): |
| 123 | return requested_depth |
| 124 | |
| 125 | cfg = THERMAL_CONFIG |
| 126 | temp_crit = cfg.get("temp_critical", 80) |
| 127 | temp_warn = cfg.get("temp_warn", 65) |
| 128 | batt_low = cfg.get("batt_low", 15) |
| 129 | batt_crit = cfg.get("batt_critical", 5) |
| 130 | |
| 131 | try: |
| 132 | from core.sysmon import get_monitor |
| 133 | snap = get_monitor().snapshot |
| 134 | except Exception: |
| 135 | return requested_depth # monitor unavailable — use full depth |
| 136 | |
| 137 | temp = snap.get("temp") |
| 138 | batt = snap.get("battery_pct") |
| 139 | charging = snap.get("battery_charging", False) |
| 140 | |
| 141 | # Temperature takes priority — thermal throttling makes recursion pointless |
| 142 | if temp is not None: |
| 143 | if temp >= temp_crit: |
| 144 | if requested_depth > 0: |
| 145 | info(f"[Adaptive] {temp:.0f}°C — skipping recursion (thermal)") |
| 146 | return 0 |
| 147 | if temp >= temp_warn and requested_depth > 1: |
| 148 | info(f"[Adaptive] {temp:.0f}°C — capping recursion depth to 1") |
| 149 | return 1 |
| 150 | |
| 151 | # Battery — only restrict when NOT charging |
| 152 | if batt is not None and not charging: |
| 153 | if batt <= batt_crit: |
| 154 | if requested_depth > 0: |
| 155 | info(f"[Adaptive] Battery {batt}% — skipping recursion") |
| 156 | return 0 |
| 157 | if batt <= batt_low and requested_depth > 1: |
| 158 | info(f"[Adaptive] Battery {batt}% — capping recursion depth to 1") |
| 159 | return 1 |
| 160 | |
| 161 | return requested_depth |
| 162 | |
| 163 | |
| 164 | # ── Quality gate helpers ────────────────────────────────────────────────────── |
no test coverage detected