Skip to content

The agent that grows with you

fix(compression): the Skill Safety Rule survives prune-only and salvage rewrites

Teknium29c0179d6461
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index f1241d50dda..db7541d869f 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -37,7 +37,7 @@ from agent.context_compressor_summary import SummaryDispatchMixin
from agent.context_compressor_retry import retryable_user_text
from agent.context_compressor_tail import (
TAIL_MAX_THRESHOLD_FRACTION, demote_stale_tail_tools, is_demoted_stub, skill_view_names_by_call_id,
- with_skill_safety_rule,
+ elide_keeping_skill_safety_rule, with_skill_safety_rule, without_skill_safety_rule,
)
from agent.context_compressor_telemetry import CompressionTelemetryMixin
from agent.error_classifier import FailoverReason, classify_api_error
@@ -589,7 +589,7 @@ def salvage_grown_transcript(
and len(content) > _SALVAGE_SUMMARY_MAX_CHARS
and _looks_like_compaction_summary(msg, content)
):
- msg["content"] = elide(content, _SALVAGE_SUMMARY_MAX_CHARS) + "\n\n" + _SUMMARY_END_MARKER
+ msg["content"] = elide_keeping_skill_safety_rule(content, _SALVAGE_SUMMARY_MAX_CHARS) + "\n\n" + _SUMMARY_END_MARKER
_prune_stale_reasoning_replay(out)
if estimate_messages_tokens_rough(out) >= budget:
_salvage_reduce_todo_snapshot(out)
@@ -836,8 +836,8 @@ _PRUNED_TOOL_PLACEHOLDER = "[Old tool output cleared to save context space]"
def _is_summary_stub(content: str) -> bool:
- """True for a tool result already replaced by a 1-line ``[tool] ... (N chars)`` summary or a lean-tail demotion stub."""
- return content.startswith("[") and " chars)" in content and len(content) < 400 or is_demoted_stub(content)
+ """True for a ``[tool] ... (N chars)`` summary (a prune-only one may carry the rule) or a lean-tail demotion stub."""
+ return (s := without_skill_safety_rule(content)).startswith("[") and " chars)" in s and len(s) < 400 or is_demoted_stub(s)
# Shared floor; the clarify summary cap must stay strictly BELOW it so a preserved
diff --git a/agent/context_compressor_prune.py b/agent/context_compressor_prune.py
index 505f40dc255..043ee38bdc9 100644
--- a/agent/context_compressor_prune.py
+++ b/agent/context_compressor_prune.py
@@ -68,6 +68,8 @@ class ProactivePruneMixin:
pruned_msgs, pruned_count = self._prune_old_tool_results(
messages, protect_tail_count=self.protect_last_n, protect_tail_tokens=None, min_prune_chars=self.proactive_prune_min_result_chars,
)
+ from agent.context_compressor_tail import with_pruned_row_skill_safety_rule
+ pruned_msgs = with_pruned_row_skill_safety_rule(messages, pruned_msgs)
if not pruned_count:
# No-op contract: return the INPUT object so callers can gate on `result is not input`.
self._warn_reclamation_no_op("prune:nothing_eligible", current_tokens)
diff --git a/agent/context_compressor_tail.py b/agent/context_compressor_tail.py
index dff5299d590..37e655633ac 100644
--- a/agent/context_compressor_tail.py
+++ b/agent/context_compressor_tail.py
@@ -112,4 +112,34 @@ def with_skill_safety_rule(summary: str, kept_messages: list[dict[str, Any]]) ->
texts = (summary, *(_content_text_for_contains(m.get("content")) for m in kept_messages if isinstance(m, dict)))
if not any(SKILL_PRUNED_MARKER_PREFIX in text for text in texts):
return summary
- return f"{summary}\n\n{SKILL_SAFETY_RULE}"
+ return f"{summary}{_SKILL_SAFETY_SUFFIX}"
+
+
+_SKILL_SAFETY_SUFFIX = f"\n\n{SKILL_SAFETY_RULE}"
+
+
+def without_skill_safety_rule(content: str) -> str:
+ """*content* minus an attached :data:`SKILL_SAFETY_RULE` (a ruled prune stub is still a stub)."""
+ return content.replace(_SKILL_SAFETY_SUFFIX, "")
+
+
+def elide_keeping_skill_safety_rule(content: str, limit: int) -> str:
+ """``elide(content, limit)`` that keeps an attached rule: the head-only cut would drop it from a long handoff
+ while marker rows beside it survive (anti-growth salvage)."""
+ from agent.compression_marker import elide
+ if SKILL_SAFETY_RULE not in content:
+ return elide(content, limit)
+ return elide(without_skill_safety_rule(content), limit - len(_SKILL_SAFETY_SUFFIX)) + _SKILL_SAFETY_SUFFIX
+
+
+def with_pruned_row_skill_safety_rule(original: list[dict[str, Any]], pruned: list[dict[str, Any]]) -> list[dict[str, Any]]:
+ """*pruned* (a prune-only rewrite of *original*) with the rule on the first row it turned into a prune marker,
+ when no row carries the rule yet: prune-only has no handoff for :func:`with_skill_safety_rule` to ride."""
+ from agent.context_compressor import SKILL_PRUNED_MARKER_PREFIX, _content_text_for_contains
+ if any(SKILL_SAFETY_RULE in _content_text_for_contains(m.get("content")) for m in pruned if isinstance(m, dict)):
+ return pruned
+ for i, (before, after) in enumerate(zip(original, pruned)):
+ content = after.get("content") if isinstance(after, dict) else None
+ if isinstance(content, str) and SKILL_PRUNED_MARKER_PREFIX in content and content != before.get("content"):
+ return [*pruned[:i], {**after, "content": content + _SKILL_SAFETY_SUFFIX}, *pruned[i + 1:]]
+ return pruned
diff --git a/tests/agent/test_oneshot_interactive_surface.py b/tests/agent/test_oneshot_interactive_surface.py
index 59b9aadc1a6..aac7b9c669c 100644
--- a/tests/agent/test_oneshot_interactive_surface.py
+++ b/tests/agent/test_oneshot_interactive_surface.py
@@ -132,3 +132,41 @@ def test_skill_safety_rule_rides_the_compaction_handoff_not_the_system_prompt(pr
if pruned else _transcript("terminal", "ok"))
assert ("## Skill Safety Rule\nA skill placeholder containing `[SKILL_PRUNED]`" in summary) is pruned
assert summary.count("## Skill Safety Rule") == int(pruned)
+
+
+def test_skill_safety_rule_survives_prune_only_and_salvage_rewrites():
+ from agent.context_compressor import (
+ COMPRESSED_SUMMARY_METADATA_KEY, SKILL_PRUNED_MARKER_PREFIX, ContextCompressor, _SUMMARY_END_MARKER,
+ _skill_pruned_marker, salvage_grown_transcript,
+ )
+ from agent.context_compressor_tail import SKILL_SAFETY_RULE
+
+ def texts(messages):
+ return "\n".join(str(m.get("content") or "") for m in messages)
+
+ # Prune-only: no handoff exists, so the row the prune turns into a marker carries the rule itself.
+ with patch("agent.context_compressor.get_model_context_length", return_value=1_000_000):
+ c = ContextCompressor(model="test", quiet_mode=True, threshold_percent=0.5, protect_first_n=2,
+ protect_last_n=4, proactive_prune_tokens=48_000, proactive_prune_min_result_chars=8_000)
+ calls = [("skill_view", '{"name":"pdf"}', "P" * 9000)] + [("terminal", '{"cmd":"ls"}', ch * 9000) for ch in "AB"]
+ calls += [("terminal", '{"cmd":"ls"}', "ok")] * 5
+ messages = [{"role": "system", "content": "sys"}]
+ for i, (tool, args, content) in enumerate(calls):
+ messages += [{"role": "assistant", "content": "", "tool_calls": [{
+ "id": f"c{i}", "type": "function", "function": {"name": tool, "arguments": args}}]},
+ {"role": "tool", "tool_call_id": f"c{i}", "content": content}]
+ pruned, count = c.prune_tool_results_only(messages, current_tokens=120_000)
+ assert count and SKILL_PRUNED_MARKER_PREFIX in texts(pruned) and texts(pruned).count(SKILL_SAFETY_RULE) == 1
+ assert c._prune_old_tool_results(pruned, protect_tail_count=4)[1] == 0 # the ruled stub is still a stub
+
+ # Salvage elides an oversized handoff; the rule must outlive the cut while marked rows remain.
+ summary = "## Goal\n" + "x" * 20_000 + "\n\n" + SKILL_SAFETY_RULE + "\n\n" + _SUMMARY_END_MARKER
+ grown = [{"role": "user", "content": summary, COMPRESSED_SUMMARY_METADATA_KEY: True},
+ {"role": "assistant", "content": "", "tool_calls": [{
+ "id": "s", "type": "function", "function": {"name": "skill_view", "arguments": '{"name":"pdf"}'}}]},
+ {"role": "tool", "tool_call_id": "s", "content": "[skill_view] name=pdf (9,000 chars) "
+ + _skill_pruned_marker("pdf")},
+ {"role": "user", "content": "latest ask"}]
+ salvaged = salvage_grown_transcript([{"role": "user", "content": "y" * 40_000}], grown)
+ assert salvaged is not None and len(salvaged[0]["content"]) < len(summary)
+ assert SKILL_SAFETY_RULE in salvaged[0]["content"]