perf(compression): skip durable refresh for in-memory-only blocks

The ineffective-compression counter is not durable; when it is the sole
reason the gate is blocked there is nothing in the DB that could unblock
it, so re-reading the guard rows on every gate check for the rest of the
session is pure waste. Guard test pins the no-DB-touch behavior.
This commit is contained in:
kshitijk4poor 2026-07-19 13:12:10 +05:30 committed by kshitij
parent 1093263aa6
commit 8ddc05b801
2 changed files with 29 additions and 0 deletions

View file

@ -1593,6 +1593,16 @@ class ContextCompressor(ContextEngine):
# and re-evaluate so a stale local block cannot outlive the durable
# state that justified it. The unblocked hot path above never pays
# for the DB reads.
if (
self._summary_failure_cooldown_until <= time.monotonic()
and self._fallback_compression_streak < 2
):
# Blocked solely by the in-memory ineffective-compression
# counter, which is not durable — there is nothing in the DB
# that could unblock it, so skip the refresh (otherwise this
# branch would re-read the DB on every gate check for the rest
# of the session).
return True
self._refresh_durable_guards()
return self._automatic_compression_blocked_locally()

View file

@ -577,3 +577,22 @@ class TestCooldownPersistFailureIsNotAClearedRow:
db.clear_compression_failure_cooldown(session_id)
assert compressor.get_active_compression_failure_cooldown(refresh=True) is None
assert compressor._summary_failure_cooldown_until == 0.0
def test_ineffective_count_only_block_skips_durable_refresh(
self,
refresh_state_db: SessionDB,
):
"""A block owed solely to the in-memory ineffective counter (which is
not durable) must not re-read the DB on every gate check."""
db = refresh_state_db
session_id = "INEFFECTIVE_ONLY_BLOCK"
db.create_session(session_id, source="telegram")
compressor = _bound_context_compressor(db, session_id)
compressor._ineffective_compression_count = 2
with patch.object(
compressor,
"_refresh_durable_guards",
side_effect=AssertionError("nothing durable to refresh"),
):
assert compressor._automatic_compression_blocked() is True