Skip to content

The agent that grows with you

fix(agent): route summary-snapshot and memory-context truncations through elide()

kshitijk4poor21edc762d5fe
diff --git a/agent/context_compressor.py b/agent/context_compressor.py
index 45f0a695245..e73b1ffd219 100644
--- a/agent/context_compressor.py
+++ b/agent/context_compressor.py
@@ -3556,8 +3556,7 @@ class ContextCompressor(SummaryDispatchMixin, MicroCompactionMixin, ContextEngin
if self._previous_summary:
previous_summary = redact_sensitive_text(self._previous_summary.strip())
if len(previous_summary) > _FALLBACK_PREVIOUS_SUMMARY_MAX_CHARS:
- previous_summary = (previous_summary[: _FALLBACK_PREVIOUS_SUMMARY_MAX_CHARS - 45].rstrip()
- + "\n...[previous summary snapshot truncated]")
+ previous_summary = elide(previous_summary, _FALLBACK_PREVIOUS_SUMMARY_MAX_CHARS)
previous_summary_note = (
"\n\n## Previous Summary Snapshot\n"
f"{previous_summary}\n\n"
@@ -3608,8 +3607,7 @@ Summary generation was unavailable, so this is a best-effort deterministic fallb
_pruned_names = _collect_ghosted_skill_names(turns_to_summarize)
del _pruned_names[_MAX_PRUNED_SKILL_MARKERS:]
summary = self._with_summary_prefix(_redact_compaction_text(body.strip()))
- if len(summary) > _FALLBACK_SUMMARY_MAX_CHARS:
- summary = summary[: _FALLBACK_SUMMARY_MAX_CHARS - 42].rstrip() + "\n...[fallback summary truncated]"
+ summary = elide(summary, _FALLBACK_SUMMARY_MAX_CHARS)
# Re-inject AFTER the size cap: markers live at the end, where truncation cuts.
summary = _reinject_pruned_skill_markers(summary, _pruned_names)
return self._augment_summary_lean(summary, turns_to_summarize)
diff --git a/agent/context_engine.py b/agent/context_engine.py
index 3fb272a50ed..0b826024a50 100644
--- a/agent/context_engine.py
+++ b/agent/context_engine.py
@@ -13,13 +13,13 @@ import json
from abc import ABC, abstractmethod
from typing import Any, Dict, List, Optional
+from agent.compression_marker import elide_middle
from agent.redact import redact_sensitive_text
MEMORY_CONTEXT_MAX_CHARS = 6_000
_MEMORY_CONTEXT_HEAD_CHARS = 4_000
_MEMORY_CONTEXT_TAIL_CHARS = 1_500
-_MEMORY_CONTEXT_TRUNCATION_MARKER = "\n...[memory provider context truncated]...\n"
def sanitize_memory_context(memory_context: str) -> str:
@@ -27,7 +27,7 @@ def sanitize_memory_context(memory_context: str) -> str:
sanitized = redact_sensitive_text(memory_context.strip(), force=True, redact_url_credentials=True)
if len(sanitized) <= MEMORY_CONTEXT_MAX_CHARS:
return sanitized
- return sanitized[:_MEMORY_CONTEXT_HEAD_CHARS] + _MEMORY_CONTEXT_TRUNCATION_MARKER + sanitized[-_MEMORY_CONTEXT_TAIL_CHARS:]
+ return elide_middle(sanitized, _MEMORY_CONTEXT_HEAD_CHARS, _MEMORY_CONTEXT_TAIL_CHARS)
def automatic_compaction_status_message(engine: Any, *, phase: str, default_message: str, **context: Any) -> str | None:
diff --git a/tests/agent/test_compression_marker.py b/tests/agent/test_compression_marker.py
index 9b5d2f51811..0ca4c04c9fa 100644
--- a/tests/agent/test_compression_marker.py
+++ b/tests/agent/test_compression_marker.py
@@ -9,6 +9,7 @@ returns to ``agent/`` strings.
from __future__ import annotations
import pathlib
+import re
import tokenize
from agent.compression_marker import (
@@ -19,7 +20,8 @@ from agent.compression_marker import (
)
AGENT_ROOT = pathlib.Path(__file__).resolve().parents[2] / "agent"
-IMITABLE_MARKERS = ("...[truncated]", "…[truncated]", "... [truncated]", "… [truncated]")
+# Any "...[<words> truncated]" variant, not just the bare one (e.g. "...[fallback summary truncated]").
+IMITABLE_MARKER_RE = re.compile(r"(?:\.\.\.|…)\s?\[[^\]\n]*truncated\]")
class TestElide:
@@ -71,8 +73,7 @@ def test_no_imitable_truncation_marker_in_agent_strings():
for tok in tokenize.generate_tokens(fh.readline):
if tok.type != tokenize.STRING:
continue
- for literal in IMITABLE_MARKERS:
- if literal in tok.string:
- rel = path.relative_to(AGENT_ROOT.parent)
- offenders.append(f"{rel}:{tok.start[0]}: {literal}")
+ for match in IMITABLE_MARKER_RE.finditer(tok.string):
+ rel = path.relative_to(AGENT_ROOT.parent)
+ offenders.append(f"{rel}:{tok.start[0]}: {match.group()}")
assert not offenders, "imitable truncation markers in agent/ strings:\n" + "\n".join(offenders)
diff --git a/tests/agent/test_pre_compress_memory_context_handoff.py b/tests/agent/test_pre_compress_memory_context_handoff.py
index 5877f90a949..380fcde1138 100644
--- a/tests/agent/test_pre_compress_memory_context_handoff.py
+++ b/tests/agent/test_pre_compress_memory_context_handoff.py
@@ -4,6 +4,8 @@ from unittest.mock import MagicMock
import pytest
+from agent.compression_marker import _COMPRESSION_MARKER_RE
+
def _make_agent(memory_manager, compressor):
from run_agent import AIAgent
@@ -205,7 +207,7 @@ def test_provider_context_is_bounded_before_plugin_engine():
assert len(context) <= 6_000
assert context.startswith("HEAD-SENTINEL")
assert context.endswith("TAIL-SENTINEL")
- assert "[memory provider context truncated]" in context
+ assert _COMPRESSION_MARKER_RE.search(context)
def test_internal_engine_type_error_propagates_after_one_call():