Files
leleka/engine/strata/serve/test_admit_order.py
T
Pavlo RatushnyiandClaude Sonnet 5.5 87e2633afe engine/strata: bring the Strata subtree to upstream 41cf05a0 (engine 0.1.50) with the local patches re-applied
Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree
(570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base =
the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main).

Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR
experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details,
conflict table and left-out files are in docs/upstream.md.

Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61),
docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out.

scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run").
Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR
macros on. No GPU run, no quality gate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 12:49:15 +01:00

120 lines
4.9 KiB
Python

"""serve/test_admit_order.py - STRATA_ADMIT_SHORT_FIRST: new requests take the control lines short prompts first
(StrataEngine._take_control / _admit_next; no process, no GPU).
python -m unittest serve.test_admit_order -v
"""
from __future__ import annotations
import sys
import threading
import time
import unittest
from pathlib import Path
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
import serve.server as server # noqa: E402
from serve.server import StrataEngine # noqa: E402
def engine_without_process(n=2):
"""A StrataEngine with the admission state of a started one and no process: alive() is `e.up`."""
e = StrataEngine("missing-executable", [], lazy=True)
e.batch = n
e.slot_busy = [False] * n
e.slot_claims, e.slot_releasing, e._orphan_seen = set(), set(), {}
e.slot_cv = threading.Condition()
e.waiting, e.admitting, e.wait_lens, e.ctl_epoch = 0, 0, [], 0
e.ctl = threading.Lock()
e.gen = 1
e.up = True
e.alive = lambda: e.up
return e
class AdmitOrderTest(unittest.TestCase):
def setUp(self):
self.e = engine_without_process(16)
self.order, self.cancels = [], []
self.order_lock = threading.Lock()
def tearDown(self):
for c in self.cancels:
c.set()
def take(self, name, plen, **kw):
"""A request waiting for the control lines; once it has them it notes its name and gives them back."""
c = threading.Event()
self.cancels.append(c)
def go():
gen = self.e._take_control(c, plen, **kw)
try:
while True:
next(gen)
except StopIteration as s:
if s.value:
with self.order_lock:
self.order.append(name)
time.sleep(0.05) # its read
self.e.ctl.release()
threading.Thread(target=go, daemon=True).start()
def until(self, cond, s=5.0):
end = time.time() + s
while time.time() < end and not cond():
time.sleep(0.01)
self.assertTrue(cond())
def queue(self, *reqs):
"""While a long read holds the control lines, the requests arrive in this order; then the read ends."""
self.assertTrue(self.e.ctl.acquire(timeout=1))
for name, plen in reqs:
self.take(name, plen, new=True)
self.until(lambda n=len(self.order) + sum(1 for _ in self.e.wait_lens): self.e.waiting >= 1)
time.sleep(0.02) # (distinct arrival times)
self.until(lambda: self.e.waiting == len(reqs))
self.e.ctl.release()
self.until(lambda: len(self.order) == len(reqs), s=15.0)
return self.order
def test_short_first_puts_the_chat_before_the_agent_reads(self):
with mock.patch.object(server, "ADMIT_SHORT_FIRST", 8192), mock.patch.object(server, "ADMIT_MAX_WAIT_S", 60.0):
order = self.queue(("agent1", 120000), ("agent2", 190000), ("chat", 500), ("agent3", 150000))
self.assertEqual(order[0], "chat")
self.assertEqual(order[1:], ["agent1", "agent2", "agent3"]) # arrival order among the long ones
def test_a_long_request_that_waited_too_long_goes_first(self):
with mock.patch.object(server, "ADMIT_SHORT_FIRST", 8192), mock.patch.object(server, "ADMIT_MAX_WAIT_S", 0.2):
self.assertTrue(self.e.ctl.acquire(timeout=1))
self.take("agent", 120000, new=True)
self.until(lambda: self.e.waiting == 1)
time.sleep(0.4) # past STRATA_ADMIT_MAX_WAIT_S
self.take("chat", 500, new=True)
self.until(lambda: self.e.waiting == 2)
self.e.ctl.release()
self.until(lambda: len(self.order) == 2, s=15.0)
self.assertEqual(self.order, ["agent", "chat"])
def test_off_by_default(self):
self.assertEqual(server.ADMIT_SHORT_FIRST if "STRATA_ADMIT_SHORT_FIRST" not in __import__("os").environ
else 0, 0)
with mock.patch.object(server, "ADMIT_SHORT_FIRST", 0):
order = self.queue(("agent1", 120000), ("chat", 500))
self.assertEqual(sorted(order), ["agent1", "chat"]) # both get their turn, in any order
def test_a_continuation_is_not_held_back(self):
"""A request already admitted once (new=False: promoted from the solo path) is not part of the order."""
with mock.patch.object(server, "ADMIT_SHORT_FIRST", 8192), mock.patch.object(server, "ADMIT_MAX_WAIT_S", 60.0):
self.assertTrue(self.e.ctl.acquire(timeout=1))
self.take("chat", 500, new=True)
self.take("continuation", 190000)
self.until(lambda: self.e.waiting == 2)
self.e.ctl.release()
self.until(lambda: len(self.order) == 2, s=15.0)
self.assertEqual(sorted(self.order), ["chat", "continuation"])
if __name__ == "__main__":
unittest.main()