From f88c6fc46e1c1c61ae8fdc0d7fb10ec8ad949aab Mon Sep 17 00:00:00 2001 From: kshitij <82637225+kshitijk4poor@users.noreply.github.com> Date: Sun, 20 Sep 2026 02:17:59 +0530 Subject: [PATCH] refactor(compression): finish the row-test dedupe and drop a gate that saved nothing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two corrections to 0b9a9a0f5c. The `if allow_split_turn else -1` gate did not skip the scan it claimed to: `_ensure_last_user_message_in_tail` runs the identical lookup as its first statement, so the micro-compaction pass paid the same scan and the batch path paid it twice. Reverted to the unconditional call, which also removes the `-1` sentinel every reader had to reason about. The dedupe stopped at two of five spellings of the same predicate. Three more sites inline it: the in-flight replay's "a real request follows the summary" check, the handoff-candidate admission test (as its negation), and the merge pre-check. All now call `_is_real_user_turn`. The one remaining inline pair is deliberately different — it also requires `_is_real_user_message`, which rejects metadata-flagged scaffolding this predicate cannot see. Equivalence: all three predicates are pure, so the negated and reordered forms are the same test; 221 tests pass across the compressor/anchor/micro-compaction files, including the source-shape anchor-order test that the call shape here leaves untouched. --- agent/context_compressor.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/agent/context_compressor.py b/agent/context_compressor.py index 2d1c42adc4..f6b93d0bb3 100644 --- a/agent/context_compressor.py +++ b/agent/context_compressor.py @@ -4238,7 +4238,12 @@ Write only the summary body. Do not include any preamble or prefix.""" @classmethod def _is_real_user_turn(cls, message: Dict[str, Any]) -> bool: - """Actionable user turn that is not synthetic scaffolding — the shared row test for both index scans.""" + """Actionable user turn that is not synthetic scaffolding — the row test both index scans share. + + Weaker than ``agent.conversation_compression._is_real_user_message``, which also rejects + metadata-flagged scaffolding this pair cannot see; use that one when the question is + "is this a genuine inbound user message". + """ return cls._is_actionable_user_turn(message) and not cls._is_synthetic_compression_user_turn(message) @classmethod @@ -4417,9 +4422,7 @@ Write only the summary body. Do not include any preamble or prefix.""" return compressed for msg in compressed[carrier_idx + 1:]: - if self._is_actionable_user_turn( - msg - ) and not self._is_synthetic_compression_user_turn(msg): + if self._is_real_user_turn(msg): # A real request already follows the summary. return compressed @@ -4572,9 +4575,7 @@ Write only the summary body. Do not include any preamble or prefix.""" # soft ceiling, anchoring its opening request retains the whole turn and blows the budget by # design — then the clean tool-group boundary above wins and that request rides the handoff # (#80449). The N-user promise (#70250) is never relaxed. - # Only batch/manual compaction can take the exception below, and only that path reads the - # newest user index, so the scan is not paid on the rolling micro-compaction pass. - last_user_idx = self._find_last_user_message_idx(messages, head_end) if allow_split_turn else -1 + last_user_idx = self._find_last_user_message_idx(messages, head_end) user_anchored_cut = self._ensure_last_user_message_in_tail(messages, cut_idx, head_end) split_oversized_turn = False # ``user_anchored_cut < cut_idx`` means the anchor found a real user turn strictly inside the @@ -5187,7 +5188,7 @@ def split_user_originated_turn(message: Any) -> tuple[Optional[Dict[str, Any]], candidate["display_metadata"] = durable_metadata drop_stale_api_content(candidate) cls = ContextCompressor - if cls._is_synthetic_compression_user_turn(candidate) or not cls._is_actionable_user_turn(candidate): + if not cls._is_real_user_turn(candidate): return handoff, None return handoff, candidate @@ -5264,10 +5265,7 @@ def reference_handoff_would_drive_next_model_call(messages: Optional[List[Dict[s role = message.get("role") if ( role == "tool" or (role == "assistant" and message.get("tool_calls")) - or ( - ContextCompressor._is_actionable_user_turn(message) - and not ContextCompressor._is_synthetic_compression_user_turn(message) - ) + or ContextCompressor._is_real_user_turn(message) or (is_compaction_summary_message(message) and _handoff_carries_live_user_content(message)) ): return False