From f66e773c442fa32aef8fc06568070e3c94357ab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E6=BC=BE?= Date: Thu, 16 Jul 2026 10:40:19 +0800 Subject: [PATCH] fix(gateway): in-place compression not persisted in response_store MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The persist logic only checked _result_sid != session_id (rotation), missing in-place mode where session_id is unchanged but _compressed flag is set. response_store history doubled every turn (11->26->55->110->225) causing repeated re-compression. Fix: detect compression via _did_compress or _rotated, and only update _effective_session_id on actual rotation (not in-place). Note: preflight loop break (turn_context.py) from original commit eee64097a is excluded — it's an optimization, not a bug fix. Cherry-picked from alidev eee64097a (api_server.py only) --- gateway/platforms/api_server.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.py index 8e4a90389123..9eb2c4c784eb 100644 --- a/gateway/platforms/api_server.py +++ b/gateway/platforms/api_server.py @@ -3729,16 +3729,20 @@ class APIServerAdapter(BasePlatformAdapter): # non-streaming path). if history_from_store and isinstance(result, dict): _result_sid = result.get("session_id") - if _result_sid and _result_sid != session_id: + _did_compress = bool(result.get("_compressed")) + _rotated = bool(_result_sid and _result_sid != session_id) + if _did_compress or _rotated: try: from hermes_cli.config import load_config _comp_cfg = load_config().get("compression", {}) if _comp_cfg.get("persist_in_response_store", True): _agent_messages = result.get("messages") if isinstance(_agent_messages, list) and _agent_messages: + _mode = "in-place" if _did_compress and not _rotated else "rotation" logger.info( - "Compression persisted in response_store (streaming): " + "Compression persisted in response_store (streaming, %s): " "%d messages (was %d before compression)", + _mode, len(_agent_messages), len(conversation_history) + 1, ) @@ -4071,22 +4075,26 @@ class APIServerAdapter(BasePlatformAdapter): _effective_session_id = session_id if _history_from_store and isinstance(result, dict): _result_sid = result.get("session_id") - if _result_sid and _result_sid != session_id: - # Session rotation = compression happened + _did_compress = bool(result.get("_compressed")) + _rotated = bool(_result_sid and _result_sid != session_id) + if _did_compress or _rotated: try: from hermes_cli.config import load_config _comp_cfg = load_config().get("compression", {}) if _comp_cfg.get("persist_in_response_store", True): _agent_messages = result.get("messages") if isinstance(_agent_messages, list) and _agent_messages: + _mode = "in-place" if _did_compress and not _rotated else "rotation" logger.info( - "Compression persisted in response_store: " + "Compression persisted in response_store (%s): " "%d messages (was %d before compression)", + _mode, len(_agent_messages), len(conversation_history) + 1, ) full_history = list(_agent_messages) - _effective_session_id = _result_sid + if _rotated and _result_sid: + _effective_session_id = _result_sid except Exception: pass # Fall back to default behavior