fix(gateway): in-place compression not persisted in response_store

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)
This commit is contained in:
尘漾 2026-07-16 10:40:19 +08:00 committed by Teknium
parent 2ced02671e
commit f66e773c44

View file

@ -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