Privilege split 6/11: the app reads and drives the hardware through the service

- kurgan/hw/client.py: HardwareClient polls the service's state once a second
  on its own thread, reconnecting (and greeting again) whenever the pipe
  breaks, and sends the app's chase clock with each poll. Profile and RAM
  lighting changes are sent at once: bad values are ValueError before anything
  is sent; an unreachable or refusing service is HardwareUnavailable. A state
  that stops arriving (5 s) or a service loop that stops publishing (15 s)
  counts as unavailable. PipeTransport for the real pipe, LocalTransport (same
  encoding and checks, no pipe) for --demo and tests.
- Telemetry: the CPU package temperature and power (CpuSensors(package=...))
  and the Commander PRO RPMs for the power model (SystemPower(fans=...)) come
  from the service; the app opens neither PawnIO nor the Commander PRO.
  "hardware service" is its own health source, and while it's down the
  missing CPU temperature isn't reported a second time.
- The fan UI, profile buttons and fan history come from the service's state;
  the RAM lighting zone goes through it (LightingService(ram=...)), and the
  case fan and GPU zones stay local. The alert monitor balloons "Hardware
  service unavailable"; the header shows a hardware service pill instead of
  the admin-rights one (and warns if the app itself runs elevated); the tray
  lines read the service's fans. POSTs answer 503 while it's unavailable.
- Removed --gpu-fan-ports, --no-gpu-fan-control and --rgb-memory-*: the
  service owns those now.
- settings.json: cooling and lighting.memory are legacy. Old files keep them
  (still validated), nothing reads them, and defaults no longer add them.
- --demo runs the real HardwareService in-process on the demo fakes, with its
  settings in demo-hw-settings.json, through the same client.
- --preview reads the CPU temperature and fan power from the service too.
- Tests: 13 for the client (including a real pipe end to end); the alert,
  lighting, settings and web tests follow the new shapes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
paulandClaude Opus 5.5 committed 2026-12-05 19:41:51 +13:00
1 parent 3e84ab1322
commit eb771656fb
15 files changed
+803 -179

No files matched your search

+26 -18
View File
@@ -3,16 +3,18 @@
%LOCALAPPDATA%\\Kurgan\\settings.json, for example:
{
"cooling": {"gpu_ports": [4, 5, 6], "cpu_ports": [1, 2, 3], "top_channels": [2, 5],
"profile": "balanced"},
"lighting": {"case_fans": "chase", "memory": "off", "gpu": "off"},
"lighting": {"case_fans": "chase", "gpu": "off"},
"ai": {"active": "strata", "strata_model": "q5", "idle_stop_minutes": 40},
"web": {"port": 8080}
}
The fan port mapping is written by `python -m kurgan.cooling.control --save-ports ...`; the
rest by the web UI. An invalid file stops The Kurgan from starting rather than silently
dropping fan control. On first start the TT LCD Dashboard's settings are imported.
The web UI writes it. An invalid file stops The Kurgan from starting rather than silently
dropping a setting. On first start the TT LCD Dashboard's settings are imported.
The fans and the RAM lighting belong to the hardware service, in hw-settings.json (see
kurgan.hw.settings). Files from before the privilege split may still hold a `cooling` section
and `lighting.memory`: they're checked as before and kept, but nothing here uses them, and
setup.ps1 moves them to hw-settings.json (kurgan.hw.migrate).
"""
from __future__ import annotations
@@ -38,20 +40,22 @@ LIGHTING_MODES = {
DEFAULT_WEB_PORT = 8080
DEFAULTS = {
"cooling": {"profile": DEFAULT_PROFILE},
"lighting": {zone: "off" for zone in LIGHTING_MODES},
"lighting": {"case_fans": "off", "gpu": "off"},
"ai": {"active": None, "strata_model": ai_config.STRATA_DEFAULT_MODEL,
"idle_stop_minutes": ai_config.IDLE_STOP_MINUTES},
"web": {"port": DEFAULT_WEB_PORT},
}
_OPTIONAL = {
"cooling": {"gpu_ports", "cpu_ports", "top_channels"},
"lighting": set(),
"lighting": {"memory"}, # legacy: the hardware service's now
"ai": {"strata_root", "strata_port", "llama_server", "llama_presets", "llama_model", "llama_port"},
"web": set(),
}
# Sections only files from before the privilege split have: allowed keys (see the docstring).
LEGACY_SECTIONS = {"cooling": {"profile", "gpu_ports", "cpu_ports", "top_channels"}}
class SettingsError(ValueError):
pass
@@ -101,21 +105,24 @@ def validate(data: object) -> dict:
"""Return a normalised copy with defaults filled in, or raise SettingsError."""
if not isinstance(data, dict):
raise SettingsError("settings must be a JSON object")
unknown = set(data) - set(DEFAULTS)
unknown = set(data) - set(DEFAULTS) - set(LEGACY_SECTIONS)
if unknown:
raise SettingsError(f"unknown section(s): {', '.join(sorted(unknown))}")
out = copy.deepcopy(DEFAULTS)
for section, values in data.items():
if not isinstance(values, dict):
raise SettingsError(f"{section} must be an object")
bad = set(values) - set(DEFAULTS[section]) - _OPTIONAL[section]
allowed = LEGACY_SECTIONS[section] if section in LEGACY_SECTIONS else (
set(DEFAULTS[section]) | _OPTIONAL[section])
bad = set(values) - allowed
if bad:
raise SettingsError(f"{section}: unknown setting(s): {', '.join(sorted(bad))}")
out[section].update(values)
out.setdefault(section, {}).update(values)
validate_cooling(out["cooling"])
if "cooling" in out:
out["cooling"] = validate_cooling({"profile": DEFAULT_PROFILE, **out["cooling"]})
for zone, modes in LIGHTING_MODES.items():
if out["lighting"][zone] not in modes:
if zone in out["lighting"] and out["lighting"][zone] not in modes:
raise SettingsError(f"lighting.{zone} must be one of {', '.join(modes)}")
ai = out["ai"]
@@ -209,18 +216,19 @@ class Settings:
log.info("Created %s%s", path, " from the TT LCD Dashboard's settings" if raw else "")
def section(self, name: str) -> dict:
"""A copy of the section ({} for an optional one the file doesn't have)."""
with self._lock:
return copy.deepcopy(self._data[name])
return copy.deepcopy(self._data.get(name, {}))
def get(self, section: str, key: str, default=None):
with self._lock:
return copy.deepcopy(self._data[section].get(key, default))
return copy.deepcopy(self._data.get(section, {}).get(key, default))
def update(self, section: str, **changes) -> None:
"""Validate and save; on any error nothing changes, in memory or on disk."""
with self._lock:
data = copy.deepcopy(self._data)
data[section].update(changes)
data.setdefault(section, {}).update(changes)
data = self._validate(data)
write_json_atomic(self.path, data)
self._data = data