The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui_gateway): an answer given at the deadline is returned, not reported as a timeout
teknium14a45fda9ee22
diff --git a/tests/tui_gateway/test_protocol.py b/tests/tui_gateway/test_protocol.pyindex 8074cbd9dc1..fcfec57dd3e 100644--- a/tests/tui_gateway/test_protocol.py+++ b/tests/tui_gateway/test_protocol.py@@ -328,6 +328,31 @@ def test_response_settlement_wins_over_a_later_cancel(capture):assert req.event.is_set()+def test_send_returns_an_answer_committed_after_the_deadline_expired(capture, monkeypatch):+ """An answer accepted by resolve_response is never reported as a timeout (#112548): the+ response frame can land after event.wait() gave up and before send() withdraws the request,+ and the renderer must not get a bogus request.cancel for a card the user just answered."""+ from tui_gateway import server_requests++ cancels: list[dict] = []+ monkeypatch.setattr(server_requests, "_emit", lambda event, sid, payload: cancels.append(payload))++ real_wait = server_requests.threading.Event.wait++ def answered_during_the_gap(event, timeout=None):+ # Deadline expires, then the response frame lands before send() re-enters the lock.+ expired = real_wait(event, timeout)+ rid = next(iter(server_requests._open))+ assert server_requests.resolve_response({"id": rid, "result": {"value": "yes"}})+ return expired++ monkeypatch.setattr(server_requests.threading.Event, "wait", answered_during_the_gap)++ assert server_requests.send("sudo", "s1", {}, timeout=0.001) == {"value": "yes"}+ assert cancels == []+ assert not server_requests._open++def test_final_clarify_lock_settlement_wins_over_a_later_cancel(capture):"""Batch clarify's lock-based resolution follows the same first-settlement rule."""from tui_gateway import server_requestsdiff --git a/tui_gateway/server_requests.py b/tui_gateway/server_requests.pyindex 2cd6cfd11c6..89194e0bd37 100644--- a/tui_gateway/server_requests.py+++ b/tui_gateway/server_requests.py@@ -109,18 +109,20 @@ def send(method: str, sid: str, params: dict, *, timeout: float | None,"""req = ServerRequest(sid, method, params, qids=qids)_register(req)- timed_out = False- try:- timed_out = not req.event.wait(timeout)- finally:- with _lock:- _open.pop(req.id, None)+ req.event.wait(timeout)+ with _lock:+ # The verdict is the state committed under the lock, never wait()'s return value: a+ # response frame can land after the deadline expires and before this removal, and+ # settlement (resolve_response / lock_answer / cancel) already popped it (#112548).+ timed_out = _open.pop(req.id, None) is req+ answered, result, locked = req.answered, req.result, dict(req.locked)+ if answered:+ return resultif timed_out:_emit_cancel(req, "timeout")if req.qids is not None:- return {"answers": dict(req.locked), "timed_out": True}- return None- return req.result if req.answered else None+ return {"answers": locked, "timed_out": True}+ return Nonedef send_async(method: str, sid: str, params: dict, on_result: Callable[[dict | None], None]) -> Callable[[str], None]:@@ -148,6 +150,9 @@ def resolve_response(frame: dict) -> bool:with _lock:req = _open.get(rid)if req is None:+ # Already settled (timed out, cancelled, answered from another surface) or owned by+ # another process; say so — a dropped answer used to vanish without a trace.+ logger.debug("server request %s: response dropped, request no longer open", rid)return False# Removing the request and committing its outcome are one settlement.# ``cancel()`` also settles under this lock, so the first side to get