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

+72 -2
View File
@@ -2,10 +2,11 @@ import os
import tempfile
import threading
import unittest
from pathlib import Path
from unittest.mock import patch
from kurgan.ai import launcher
from kurgan.ai.backends import Backend, LlamaLog
from kurgan.ai import launcher, strata_host
from kurgan.ai.backends import Backend, LlamaLog, StrataBackend
from kurgan.ai.service import CLIENT_GONE, AiService, RouteError
from kurgan.settings import Settings
@@ -311,6 +312,75 @@ class AiServiceTest(unittest.TestCase):
self.reconcile()
self.assertEqual(self.processes()["llama"], "starting")
def test_a_server_with_a_request_in_flight_is_not_stopped_or_replaced(self):
self.service.set_active("strata")
self.reconcile()
self.service.inflight["strata"] = 1 # a reply in progress
self.service.set_active("llama")
self.reconcile()
# Strata finishes its request first; llama must not start onto the GPU's memory.
self.assertEqual(self.events, [("start", "strata")])
self.assertIn("Waiting for Strata to finish its request", self.service.transition)
self.service.release("strata")
self.reconcile()
self.assertEqual(self.events, [("start", "strata"), ("stop", "strata"), ("start", "llama")])
def test_a_model_change_waits_for_the_request_in_flight(self):
self.service.set_active("strata", "iq3")
self.reconcile()
self.service.inflight["strata"] = 1
self.service.set_active("strata", "q5")
self.reconcile()
self.assertEqual(self.events, [("start", "strata")]) # iq3 keeps serving until the reply ends
self.service.release("strata")
self.reconcile()
self.assertEqual(self.events, [("start", "strata"), ("stop", "strata"), ("start", "strata")])
def test_a_port_held_by_a_program_that_is_not_the_server_is_not_used(self):
self.external_ports.add(18082)
self.service.set_active("strata")
self.reconcile()
self.assertEqual(self.service.snapshot()["backends"][0]["process"], "external")
self.service.slots["strata"].reachable = True # a poll succeeded: the program is Strata
self.assertEqual(self.service.claim("strata").key, "strata")
self.service.release("strata")
self.service.slots["strata"].reachable = False # ...and then it stopped answering as one
thread, result = self.claim_in_background("strata")
threading.Event().wait(0.2)
self.clock[0] += 11
thread.join(5)
self.assertIn("isn't answering as Strata", str(result["error"]))
self.assertEqual(self.service.inflight["strata"], 0) # the failed claim gave its place back
class StrataLogRotationTest(unittest.TestCase):
def test_old_strata_logs_are_rotated_and_pruned(self):
with tempfile.TemporaryDirectory() as directory:
logs = Path(directory)
(logs / "local-service-task.log").write_text("last run")
for number in range(9):
stamp = f"2026010{number + 1}-000000-000000"
(logs / f"local-service-stdout-{stamp}.log").write_text("out")
(logs / f"local-service-stderr-{stamp}.log").write_text("err")
strata_host.rotate_logs(logs)
self.assertEqual((logs / "local-service-task.prev.log").read_text(), "last run")
self.assertFalse((logs / "local-service-task.log").exists())
names = sorted(p.name for p in logs.glob("local-service-std*.log"))
self.assertEqual(len(names), 2 * strata_host.KEEP_LOG_PAIRS) # the newest few pairs
self.assertIn("local-service-stdout-20260109-000000-000000.log", names)
self.assertNotIn("local-service-stdout-20260101-000000-000000.log", names)
def test_strata_host_log_is_rotated_before_a_start(self):
with tempfile.TemporaryDirectory() as directory:
with patch("kurgan.ai.backends.LOG_DIR", directory):
backend = StrataBackend(Settings(os.path.join(directory, "settings.json"), None))
with open(os.path.join(directory, "strata-host.log"), "w") as log:
log.write("previous run")
backend.before_start()
self.assertFalse(os.path.exists(os.path.join(directory, "strata-host.log")))
with open(os.path.join(directory, "strata-host.prev.log")) as log:
self.assertEqual(log.read(), "previous run")
class LauncherTest(unittest.TestCase):
def test_child_runs_as_a_normal_user_with_its_output_logged(self):