From 5921a876c75faf7f715612080e8611e37df6be69 Mon Sep 17 00:00:00 2001 From: anor Date: Wed, 28 Oct 2026 10:45:55 +1000 Subject: [PATCH] =?UTF-8?q?feat(=D0=BE=D1=87=D0=B5=D1=80=D0=B5=D0=B4=D1=8C?= =?UTF-8?q?):=20=D0=BC=D0=B5=D1=81=D1=82=D0=BE=20=D0=BF=D0=BE=D0=BB=D1=83?= =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=BE=20=E2=80=94=20=C2=AB=D0=BD=D0=B0=D1=87?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0=D1=8E=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=83?= =?UTF-8?q?=C2=BB=20=D1=81=D1=80=D0=B0=D0=B7=D1=83;=20=D0=B7=D0=B0=D0=BA?= =?UTF-8?q?=D1=80=D1=8B=D1=82=D0=B8=D0=B5=20=D0=B2=20=D1=8D=D1=82=D0=BE?= =?UTF-8?q?=D1=82=20=D0=BC=D0=B8=D0=B3=20=D0=BD=D0=B5=20=D1=83=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D0=B8=D1=82=20=D0=BC=D0=B5=D1=81=D1=82=D0=BE;=20=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D1=81=D0=B8=D1=8F=202026-10-28b?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- claude_bridge.py | 10 +++++++++- main.py | 2 +- static/chat.html | 10 ++++++---- tests/test_agent_queue.py | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/claude_bridge.py b/claude_bridge.py index 2eb5e0b..7812d42 100644 --- a/claude_bridge.py +++ b/claude_bridge.py @@ -83,7 +83,7 @@ async def _wait_for_slot(): while True: done, _ = await asyncio.wait({acquire}, timeout=QUEUE_KEEPALIVE) if done: - return + break yield ": keepalive\n\n" except BaseException: if acquire.done() and not acquire.cancelled(): @@ -93,6 +93,14 @@ async def _wait_for_slot(): raise finally: _waiting -= 1 + # Место получено: снимаем «вы в очереди» сразу — первое слово агента бывает через + # десятки секунд, и всё это время человек читал бы, что ещё ждёт. + try: + yield ("event: queue\ndata: " + + json.dumps({"position": 0, "slots": MAX_CONCURRENT}) + "\n\n") + except BaseException: + _semaphore.release() # ушли, не дойдя до прогона: место не уносим + raise class _SlotHeld: diff --git a/main.py b/main.py index 248bb49..d056ccd 100644 --- a/main.py +++ b/main.py @@ -454,7 +454,7 @@ MAX_MESSAGE_LEN = 10_000 # ⚠️ Версия UI — единый источник. БАМПАТЬ ПРИ КАЖДОМ ДЕПЛОЕ (формат: YYYY-MM-DD + буква). # Фронт берёт её через GET /api/version (chat.html + admin.html, элемент .app-version). -APP_VERSION = "2026-10-28a" +APP_VERSION = "2026-10-28b" # Audit: path → action name mapping _AUDIT_MAP = [ diff --git a/static/chat.html b/static/chat.html index 20164ee..80744b6 100644 --- a/static/chat.html +++ b/static/chat.html @@ -2315,10 +2315,12 @@ async function runChat(text, retried, готовыйПоток, внешнийCt } else if (eventType === 'queue') { // Все места агента заняты: говорим, что ждём, а не молчим минутами. removeTyping(); - const ahead = Math.max(0, (data.position || 1) - 1); - addTyping().textContent = ahead - ? 'Все места заняты, вы в очереди. Перед вами: ' + ahead + '. Начну, как только освободится место.' - : 'Все места заняты, вы следующий. Начну, как только освободится место.'; + const ahead = Math.max(0, (data.position || 0) - 1); + addTyping().textContent = data.position === 0 + ? 'Место освободилось, начинаю работу...' + : ahead + ? 'Все места заняты, вы в очереди. Перед вами: ' + ahead + '. Начну, как только освободится место.' + : 'Все места заняты, вы следующий. Начну, как только освободится место.'; } else if (eventType === 'done') { removeTyping(); // Показанное короче итога — часть потока не дошла: ставим ответ целиком, тот же, diff --git a/tests/test_agent_queue.py b/tests/test_agent_queue.py index f2a950b..ec91bd9 100644 --- a/tests/test_agent_queue.py +++ b/tests/test_agent_queue.py @@ -81,6 +81,12 @@ def test_waiting_run_is_told_about_queue_and_kept_alive(bridge): assert sum(c == ": keepalive\n\n" for c in waiting) >= 3 # тишины нет, пока ждёт assert not any("ответ b" in c for c in waiting) # пока ждёт — не работает assert any("ответ b" in c for c in out_b) # место освободилось — доработал + # место получено — строка «вы в очереди» сменяется сразу, а не с первым словом агента + marks = [json.loads(c.split("data: ", 1)[1])["position"] for c in out_b + if c.startswith("event: queue")] + assert marks == [1, 0] + first_answer = next(i for i, c in enumerate(out_b) if "ответ b" in c) + assert out_b.index(next(c for c in out_b if '"position": 0' in c)) < first_answer def test_abandoned_wait_does_not_take_the_slot(bridge): @@ -124,3 +130,31 @@ def test_zero_keepalive_setting_falls_back(monkeypatch): monkeypatch.setenv("AGENT_QUEUE_KEEPALIVE", bad) importlib.reload(claude_bridge) assert claude_bridge.QUEUE_KEEPALIVE == 20.0 + + +def test_closing_right_after_slot_given_frees_it(bridge): + cb, gates = bridge + + async def scenario(): + for u in ("a", "b", "c"): + gates[u] = asyncio.Event() + gates["c"].set() + ta = asyncio.create_task(_drain(cb, "a", [])) + await asyncio.sleep(0.02) + gen = cb.stream_claude("привет", "b", "tab") + + async def read_until_slot(): + async for chunk in gen: + if '"position": 0' in chunk: + return # человек ушёл ровно тут + tb = asyncio.create_task(read_until_slot()) + await asyncio.sleep(0.1) + gates["a"].set() + await asyncio.wait_for(asyncio.gather(ta, tb), 5) + await gen.aclose() + out_c = [] + await asyncio.wait_for(_drain(cb, "c", out_c), 2) + return out_c + + out_c = asyncio.run(scenario()) + assert not any("event: queue" in c for c in out_c)