fix(compression): a refused candidate's attempt record claims no effect
Review thread on #136050 (agent/context_compressor_telemetry.py, "Refusals report unsaved effects"). The compressor records a candidate's effect fields before the commit checks run. When the growth guard refused the candidate, or a database failure rolled the transcript back, the aborted record still reported its `messages_after`, `tokens_after`, `tokens_reclaimed` and `items_dropped`. That claimed reclaimed tokens for a conversation that stayed unchanged. Drop those fields from any record that neither committed nor rewrote the history. The input-side counts stay.
This commit is contained in:
@@ -83,6 +83,10 @@ def _log_attempt_record(payload: dict[str, Any]) -> None:
|
||||
logger.info("context compression attempt telemetry: %s", json.dumps(payload, sort_keys=True, separators=(",", ":")))
|
||||
|
||||
|
||||
# Fields describing the rewritten transcript; the input-side counts (messages_before, tokens_before) stay.
|
||||
_CANDIDATE_EFFECT_FIELDS = ("messages_after", "tokens_after", "tokens_reclaimed", "items_dropped")
|
||||
|
||||
|
||||
def _emit_compression_attempt_telemetry(
|
||||
agent: Any, *, started_at: float, commit_status: str, split_status: str, failure_class: str | None = None,
|
||||
commit_started_at: float | None = None, include_last_telemetry: bool = True,
|
||||
@@ -116,6 +120,10 @@ def _emit_compression_attempt_telemetry(
|
||||
)
|
||||
if history_rewritten is not None:
|
||||
payload["history_rewritten"] = history_rewritten
|
||||
if commit_status != "committed" and not history_rewritten:
|
||||
# A refused or rolled-back candidate never reached the transcript: report no effect it did not have.
|
||||
for key in _CANDIDATE_EFFECT_FIELDS:
|
||||
payload.pop(key, None)
|
||||
if commit_started_at is not None:
|
||||
telemetry["commit_ms"] = payload["commit_ms"] = max(0, int((time.monotonic() - commit_started_at) * 1000))
|
||||
# Defer only to THIS attempt's class: an abort restore can put the previous attempt's telemetry back.
|
||||
|
||||
@@ -11,6 +11,8 @@ would-grow refusals returned the mutated list instead.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
@@ -58,8 +60,13 @@ def _empty_transcript_engine(_compressor):
|
||||
return _compress
|
||||
|
||||
|
||||
def _growing_engine(_compressor):
|
||||
def _growing_engine(compressor):
|
||||
def _compress(msgs, **_kw):
|
||||
# A real compressor records the candidate's effect before the commit checks see it.
|
||||
compressor._active_compression_telemetry = compressor._last_compression_telemetry = (
|
||||
compressor._begin_compression_telemetry(current_tokens=120_000)
|
||||
)
|
||||
compressor._active_compression_telemetry.update(messages_after=2, tokens_after=9, tokens_reclaimed=99)
|
||||
msgs.pop()
|
||||
return list(msgs) + [{"role": "assistant", "content": "GROWN " * 20_000}]
|
||||
|
||||
@@ -88,7 +95,7 @@ def _raising_engine(_compressor):
|
||||
ids=["compressor_aborted", "empty_transcript", "would_grow", "would_grow_in_place"],
|
||||
)
|
||||
def test_refused_candidate_from_mutating_engine_returns_original_transcript(
|
||||
tmp_path: Path, make_engine
|
||||
tmp_path: Path, make_engine, caplog
|
||||
) -> None:
|
||||
db = SessionDB(db_path=tmp_path / "state.db")
|
||||
session_id = "REFUSED_CANDIDATE"
|
||||
@@ -106,9 +113,10 @@ def test_refused_candidate_from_mutating_engine_returns_original_transcript(
|
||||
db, "archive_and_compact", wraps=db.archive_and_compact
|
||||
) as archive_and_compact:
|
||||
# A caller-owned fence selects the direct path that gateway session hygiene uses.
|
||||
returned, _sp = agent._compress_context(
|
||||
messages, "sys", approx_tokens=120_000, commit_fence=CompressionCommitFence()
|
||||
)
|
||||
with caplog.at_level(logging.INFO, logger="agent.conversation_compression"):
|
||||
returned, _sp = agent._compress_context(
|
||||
messages, "sys", approx_tokens=120_000, commit_fence=CompressionCommitFence()
|
||||
)
|
||||
|
||||
commit_memory_session.assert_not_called()
|
||||
archive_and_compact.assert_not_called()
|
||||
@@ -120,6 +128,11 @@ def test_refused_candidate_from_mutating_engine_returns_original_transcript(
|
||||
).fetchone()[0] == 0
|
||||
assert db.get_session(session_id)["end_reason"] is None
|
||||
assert db.get_compression_lock_holder(session_id) is None
|
||||
# The record must not claim an effect the refused candidate never had on the transcript.
|
||||
prefix = "context compression attempt telemetry: "
|
||||
[record] = [json.loads(r.getMessage()[len(prefix):]) for r in caplog.records if r.getMessage().startswith(prefix)]
|
||||
assert record["commit_status"] == "aborted"
|
||||
assert [record.get(k) for k in ("messages_after", "tokens_after", "tokens_reclaimed")] == [None] * 3
|
||||
finally:
|
||||
agent.close()
|
||||
|
||||
|
||||
@@ -260,6 +260,9 @@ text, the focus topic or error messages.
|
||||
- `messages_before` / `messages_after` and `tokens_before` / `tokens_after` /
|
||||
`tokens_reclaimed` describe the transcript that crossed the commit boundary,
|
||||
including retained `/compress here N` tail rows and boundary anchors.
|
||||
A refused or rolled-back candidate never reached the transcript, so its
|
||||
record has no `messages_after`, `tokens_after`, `tokens_reclaimed` or
|
||||
`items_dropped`.
|
||||
`token_count_method: estimate_rough` marks them as rough message-only
|
||||
estimates (system prompt and tool schemas excluded), so they compare like
|
||||
for like. `tool_results_pruned` and `reasoning_items_pruned` count the
|
||||
|
||||
Reference in New Issue
Block a user