Skip to content

The agent that grows with you

fix: compaction salvage no longer strips reasoning from retained turns

Teknium86aa201bba26
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index 4d6d349c114..8dedb75a888 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -573,8 +573,11 @@ def _salvage_reduce_todo_snapshot(out: list[dict[str, Any]]) -> None:
def salvage_grown_transcript(
original: list[dict[str, Any]], candidate: list[dict[str, Any]], budget: Optional[int] = None,
+ *, route: Any = None,
) -> Optional[list[dict[str, Any]]]:
- """Mechanically shrink a compression candidate (copies, cheapest loss first); ``None`` unless strictly smaller."""
+ """Mechanically shrink a compression candidate (copies, cheapest loss first); ``None`` unless strictly smaller.
+ Never strips reasoning text: the result becomes canonical history that replay reads on every retained turn.
+ ``codex_reasoning_items`` follow ``compress()``'s rule (``route_reads_prior_turn_reasoning(route)``)."""
if not candidate or not original:
return None
if budget is None:
@@ -584,15 +587,10 @@ def salvage_grown_transcript(
out = [dict(msg) if isinstance(msg, dict) else msg for msg in candidate]
tool_indices = [i for i, msg in enumerate(out) if isinstance(msg, dict) and msg.get("role") == "tool"]
- last_assistant_idx = _last_index_with_role(out, "assistant")
- salvage_reasoning_keys = _NEWEST_TURN_ONLY_BUDGET_KEYS + ("reasoning_details",)
keep_tools = set(tool_indices[-_SALVAGE_KEEP_RECENT_TOOLS:])
for index, msg in enumerate(out):
if not isinstance(msg, dict):
continue
- if msg.get("role") == "assistant" and index != last_assistant_idx:
- for key in salvage_reasoning_keys:
- msg.pop(key, None)
if msg.get("role") == "tool" and index not in keep_tools:
content = msg.get("content")
if isinstance(content, str) and len(content) > _PRUNE_MIN_CHARS:
@@ -604,7 +602,8 @@ def salvage_grown_transcript(
and _looks_like_compaction_summary(msg, content)
):
msg["content"] = elide(content, _SALVAGE_SUMMARY_MAX_CHARS) + "\n\n" + _SUMMARY_END_MARKER
- _prune_stale_reasoning_replay(out)
+ from agent.codex_responses_adapter import route_reads_prior_turn_reasoning
+ _prune_stale_reasoning_replay(out, keep_prior_turns=route is not None and route_reads_prior_turn_reasoning(route))
if estimate_messages_tokens_rough(out) >= budget:
_salvage_reduce_todo_snapshot(out)
has_user = any(isinstance(message, dict) and message.get("role") == "user" for message in out)
diff --git a/agent/conversation_compression.py b/agent/conversation_compression.py
index 3f2ce36b73b..5fa1b15bafc 100644
--- a/agent/conversation_compression.py
+++ b/agent/conversation_compression.py
@@ -3211,7 +3211,7 @@ def _salvage_or_refuse_grown_transcript(
# Todo refresh and user-turn anchoring run after the compressor's own size check
# and can tip a break-even candidate; give it one mechanical salvage pass.
from agent.context_compressor import salvage_grown_transcript
- _salvaged = salvage_grown_transcript(original_messages, compressed, budget=_rough_in)
+ _salvaged = salvage_grown_transcript(original_messages, compressed, budget=_rough_in, route=agent)
if _salvaged is not None:
_salv_est = estimate_messages_tokens_rough(_salvaged)
if _salv_est < _rough_in:
diff --git a/tests/agent/test_salvage_grown_transcript.py b/tests/agent/test_salvage_grown_transcript.py
index 943f9cec378..fddcc59be4f 100644
--- a/tests/agent/test_salvage_grown_transcript.py
+++ b/tests/agent/test_salvage_grown_transcript.py
@@ -176,3 +176,29 @@ def test_salvage_never_caps_unmarked_summary_shaped_live_user_text():
assert out is not None
assert out[0]["content"] == live_user_text
+
+
+def test_salvage_never_strips_reasoning_from_retained_turns():
+ """The salvaged list is committed as canonical history and replayed by default, so older assistant
+ turns must keep every reasoning carrier; only tool output and summaries are shrink targets."""
+ reasoning = {
+ "reasoning": "r" * 300,
+ "reasoning_content": "rc" * 150,
+ "reasoning_details": [{"type": "reasoning.text", "text": "d" * 300, "signature": "s", "index": 0}],
+ }
+ original = [
+ {"role": "user", "content": "go"},
+ {"role": "assistant", "content": "", "tool_calls": [{"id": "a"}], **reasoning},
+ {"role": "tool", "tool_call_id": "a", "content": "A" * 6000},
+ {"role": "assistant", "content": "", "tool_calls": [{"id": "b"}], **reasoning},
+ {"role": "tool", "tool_call_id": "b", "content": "B" * 6000},
+ {"role": "tool", "tool_call_id": "c", "content": "C" * 6000},
+ {"role": "tool", "tool_call_id": "d", "content": "keep-latest"},
+ ]
+ grown = original + [{"role": "user", "content": "Current todos:\n- [ ] x", "_todo_snapshot_synthetic": True}]
+
+ out = salvage_grown_transcript(original, grown)
+
+ assert out is not None
+ for msg in (m for m in out if m.get("role") == "assistant"):
+ assert {k: msg[k] for k in reasoning} == reasoning