From 24573b396be37ca14de05a38b53c06c8a273ea71 Mon Sep 17 00:00:00 2001 From: kshitij <82637225+kshitijk4poor@users.noreply.github.com> Date: Sat, 15 Aug 2026 09:55:24 +0530 Subject: [PATCH] refactor: bind gateway anti-growth guard to locals, trim overlong comment Simplify-code pass: gateway/run.py called estimate_messages_tokens_rough 6x on the same data in the anti-growth guard (condition + warning f-string). Bind to _hyg_in_toks/_hyg_out_toks locals like the conversation_compression.py guard already does. Also trim the comment from 10 lines to 4 (keep the WHY, drop the WHAT) and remove an extra blank line before TestCompactedTurnsStaySearchable. --- gateway/run.py | 26 +++++++-------------- tests/run_agent/test_in_place_compaction.py | 1 - 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/gateway/run.py b/gateway/run.py index 7ad2f0aba2..6082a94053 100644 --- a/gateway/run.py +++ b/gateway/run.py @@ -19031,28 +19031,20 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew _hyg_in_place = bool( getattr(_hyg_agent, "_last_compaction_in_place", False) ) - # Anti-regression guard: never persist a - # compression that did not shrink the - # transcript. A summary that is larger than - # the middle it replaces would GROW the - # session instead of reclaiming it (observed: - # 427K -> 598K). Compare like-for-like (both - # rough estimates) so an "actual vs - # estimate" measurement mismatch can't - # produce a false verdict. On growth, keep - # the original transcript untouched. - if ( - _hyg_rotated - ) and estimate_messages_tokens_rough( - _compressed - ) > estimate_messages_tokens_rough(history): + # Anti-growth guard: refuse a compression + # that did not shrink the transcript + # (observed: 427K -> 598K). Compare + # like-for-like rough estimates. + _hyg_in_toks = estimate_messages_tokens_rough(history) + _hyg_out_toks = estimate_messages_tokens_rough(_compressed) + if _hyg_rotated and _hyg_out_toks > _hyg_in_toks: logger.warning( "Gateway hygiene compression for session %s " "would grow transcript (~%s -> ~%s tokens); " "keeping the original transcript unchanged", session_entry.session_id, - f"{estimate_messages_tokens_rough(history):,}", - f"{estimate_messages_tokens_rough(_compressed):,}", + f"{_hyg_in_toks:,}", + f"{_hyg_out_toks:,}", ) _hyg_rotated = False _compressed = history diff --git a/tests/run_agent/test_in_place_compaction.py b/tests/run_agent/test_in_place_compaction.py index f354a347aa..21b41f19cd 100644 --- a/tests/run_agent/test_in_place_compaction.py +++ b/tests/run_agent/test_in_place_compaction.py @@ -320,7 +320,6 @@ class TestInPlaceAntiGrowthGuard: ] - class TestCompactedTurnsStaySearchable: """Teknium's review hinges on the pre-compaction transcript staying DISCOVERABLE after in-place compaction. Compaction-archived rows