The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui_gateway): run the post-turn trim even if a settle step raises
kshitijk4poor544e369d8574
diff --git a/tests/tui_gateway/test_prompt_accept_logging.py b/tests/tui_gateway/test_prompt_accept_logging.pyindex c1ffcda5925..dc8ce3da2d8 100644--- a/tests/tui_gateway/test_prompt_accept_logging.py+++ b/tests/tui_gateway/test_prompt_accept_logging.py@@ -106,7 +106,8 @@ def test_accepted_and_finished_records_on_success(turn_env, caplog):assert "hunter2" not in fin-def test_turn_settles_before_post_turn_trim(monkeypatch, caplog):[email protected]("settle_info_raises", [False, True])+def test_turn_settles_before_post_turn_trim(monkeypatch, caplog, settle_info_raises):"""A blocked post-turn trim must not hold the session running or its bookend (#131740);turn audio still ends BEFORE settlement, so a next turn admitted during the trim keeps its own."""import hermes_cli.mem_trim as mem_trim@@ -124,6 +125,8 @@ def test_turn_settles_before_post_turn_trim(monkeypatch, caplog):monkeypatch.setattr(server, "_sync_agent_model_with_config", lambda sid, session: None)monkeypatch.setattr(server, "_tts_stream_begin", lambda: None)monkeypatch.setattr(server, "_get_usage", lambda agent: {})+ if settle_info_raises: # a raising settle step must not skip the post-turn trim+ monkeypatch.setattr(server, "_emit_settled_session_info", lambda *a: 1 / 0)audio_end = [] # (event, session running at that moment)tts = types.SimpleNamespace(put=lambda x: x is None and audio_end.append(("tts", session["running"])))monkeypatch.setattr(server, "_start_turn_voice", lambda: (tts, True))diff --git a/tui_gateway/prompt_turn.py b/tui_gateway/prompt_turn.pyindex 7ded53b4054..d58aafba659 100644--- a/tui_gateway/prompt_turn.py+++ b/tui_gateway/prompt_turn.py@@ -1187,39 +1187,41 @@ def _run_prompt_submit(_recover_turn_exception(sid, session, st, e)finally:_release_turn_scopes(sid, session, st)- _current_runtime_session_record.reset(runtime_session_token)- reset_transport(transport_token)- # A stale interim closure must not fire during a later turn.- st.agent.interim_assistant_callback = None- with session["history_lock"]:- session["running"] = False- session["last_active"] = time.time()- if not st.error_retained:- _clear_inflight_turn(session)- _release_hosted_room_turn_slot(session)- # Closing bookend of "tui prompt accepted" — exactly one per accepted prompt.- # agent.session_id is re-read because compression may have rotated it (an- # accepted/finished pair whose id changed IS a rotation trace).- if isinstance(st.result, dict):- status = _result_status(st.result)- else:- status = "error" if st.error_retained else "complete"- logger.info(- "tui turn finished: ui_session=%s session_key=%s agent_session_id=%s status=%s "- "error_retained=%s duration=%.1fs%s",- sid, session.get("session_key") or "", getattr(st.agent, "session_id", "") or "",- status, st.error_retained, time.monotonic() - _turn_started_monotonic,- st.error_detail)- # Backstop for turns that never reached a terminal frame.- if st.receipt_committed:- _retire_turn_marker(session, st.marker_key)+ try: # a raising settle step must not skip the trim / HERMES_HOME reset+ _current_runtime_session_record.reset(runtime_session_token)+ reset_transport(transport_token)+ # A stale interim closure must not fire during a later turn.+ st.agent.interim_assistant_callback = Nonewith session["history_lock"]:- if session.get("_active_turn_marker_key") == st.marker_key:- session.pop("_active_turn_marker_key", None)- session.pop("_hosted_room_task", None)- session.pop("_auto_continue_scheduled", None)- _emit_settled_session_info(sid, session, st.agent)- _post_turn_housekeeping(sid, session, st)+ session["running"] = False+ session["last_active"] = time.time()+ if not st.error_retained:+ _clear_inflight_turn(session)+ _release_hosted_room_turn_slot(session)+ # Closing bookend of "tui prompt accepted" — exactly one per accepted prompt.+ # agent.session_id is re-read because compression may have rotated it (an+ # accepted/finished pair whose id changed IS a rotation trace).+ if isinstance(st.result, dict):+ status = _result_status(st.result)+ else:+ status = "error" if st.error_retained else "complete"+ logger.info(+ "tui turn finished: ui_session=%s session_key=%s agent_session_id=%s status=%s "+ "error_retained=%s duration=%.1fs%s",+ sid, session.get("session_key") or "", getattr(st.agent, "session_id", "") or "",+ status, st.error_retained, time.monotonic() - _turn_started_monotonic,+ st.error_detail)+ # Backstop for turns that never reached a terminal frame.+ if st.receipt_committed:+ _retire_turn_marker(session, st.marker_key)+ with session["history_lock"]:+ if session.get("_active_turn_marker_key") == st.marker_key:+ session.pop("_active_turn_marker_key", None)+ session.pop("_hosted_room_task", None)+ session.pop("_auto_continue_scheduled", None)+ _emit_settled_session_info(sid, session, st.agent)+ finally:+ _post_turn_housekeeping(sid, session, st)return st.result, goal_followupdef run():from agent.notification_presentation import notification_turn