AI: never cut a request off mid-reply; bound the logs and the stop path

- service.py: the idle stop's settings write now happens under the same lock
  claim() holds, so a request arriving between the idle check and the write
  can no longer have its selection overwritten and its server stopped under it.
- service.py: _reconcile no longer stops or replaces a server with a request
  in flight, however the switch happens (selecting another backend or None,
  changing Strata's model): the old server finishes its request, and the new
  one does not start until the GPU's memory is free again.
- service.py: an "external" server is only used once a poll has proved the
  program behind its port answers as that server; a port held by something
  else now fails the request saying who holds it, instead of forwarding
  prompts to it.
- strata_host.py: the stop watchdog is real -- if server.main never returns
  within 40 s of the stop request it ends the engine processes and exits,
  instead of relying on interrupt_main() alone (The Kurgan's 45 s kill stays
  as the last resort); the never-read stopping event is gone. It rotates its
  logs at startup: the task log keeps one .prev, the newest seven stdout or
  stderr files per start are kept.
- backends.py: StrataBackend.before_start rotates strata-host.log the way
  LlamaBackend does for llama-server.log.
- tests: five new tests (the two draining guards, external verification,
  both rotations). README updated to match.

VRAM-aware start (the review's "port closed != GPU free, check VRAM before
start") stays a capability idea: no setting knows a model's size to check
free VRAM against.
This commit is contained in:
paul committed 2026-12-05 17:13:10 +13:00
1 parent 9036c6e734
commit 8969a57045
5 files changed
+171 -23

No files matched your search

+31 -9
View File
@@ -193,8 +193,12 @@ class AiService:
def _wait_ready(self, key: str, slot: Slot, gone=None) -> None:
start = time.monotonic()
while True:
running = slot.external is not None or (slot.child is not None and not slot.stopping)
if running and port_in_use(slot.backend.port):
if slot.external is not None:
# An open port isn't proof the program behind it is this server: only a poll
# that succeeded means it speaks the API the proxy is about to use.
if slot.reachable:
return
elif slot.child is not None and not slot.stopping and port_in_use(slot.backend.port):
return
if self._stop is not None and self._stop.is_set():
raise RouteError("The Kurgan is shutting down")
@@ -203,6 +207,10 @@ class AiService:
waited = time.monotonic() - start
if slot.external is None and self.settings.get("ai", "active") != key and waited > 3:
raise RouteError(f"{slot.backend.label} was switched off in The Kurgan")
if slot.external is not None and not slot.reachable and waited > 10:
pid, name = slot.external
raise RouteError(f"port {slot.backend.port} is held by {name} (pid {pid}), "
f"which isn't answering as {slot.backend.label}")
if slot.child is None and slot.error and waited > 3:
raise RouteError(slot.error)
if waited >= self.READY_WAIT:
@@ -303,11 +311,14 @@ class AiService:
return active
if time.monotonic() - self.last_used[active] < minutes * 60:
return active
try:
self.settings.update("ai", active=None)
except OSError as e:
log.warning("Couldn't save the AI selection: %s", e)
return active
try:
# Under the same lock as claim(): a request arriving between the check and the
# write would otherwise have its selection overwritten and its server stopped
# under it.
self.settings.update("ai", active=None)
except OSError as e:
log.warning("Couldn't save the AI selection: %s", e)
return active
log.info("%s unused for %d minutes; stopping it until the next request", slot.backend.label, minutes)
return None
@@ -315,9 +326,15 @@ class AiService:
active = self._idle_check(self.settings.get("ai", "active"))
for slot in self.slots.values():
self._check_exit(slot)
with self._cv:
# A server with a request in flight finishes it before being stopped or replaced:
# clients wait for servers, they are never cut off mid-reply.
draining = next((key for key, slot in self.slots.items()
if key != active and slot.child is not None and self.inflight[key]), None)
busy_here = active is not None and self.inflight[active] > 0
# Stop whatever isn't selected first, so the selected server gets the GPU to itself.
for key, slot in self.slots.items():
if slot.child is not None and key != active:
if slot.child is not None and key != active and key != draining:
self._stop_slot(slot, "switching" if active else "none selected")
for key, slot in self.slots.items():
slot.external = None
@@ -327,11 +344,16 @@ class AiService:
self.transition = None
return
slot = self.slots[active]
if slot.child is not None and slot.variant != slot.backend.variant():
if slot.child is not None and not busy_here and slot.variant != slot.backend.variant():
self._stop_slot(slot, "changing model")
if slot.child is not None or slot.external is not None:
self.transition = None
return
if draining is not None:
# The GPU's memory is still held by the server answering its last request; the
# selected one must not start onto it.
self.transition = f"Waiting for {self.slots[draining].backend.label} to finish its request"
return
if time.monotonic() < slot.hold_until:
return
# At logon The Kurgan can be up before Explorer; give the desktop up to 2 minutes so the