Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions agent/context_compressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,12 @@ def _fallback_to_main_for_compression(self, e: Exception, reason: str) -> None:
self.summary_model = "" # empty = use main model
self._summary_failure_cooldown_until = 0.0 # no cooldown — retry immediately

def _generate_summary(self, turns_to_summarize: List[Dict[str, Any]], focus_topic: str = None) -> Optional[str]:
def _generate_summary(
self,
turns_to_summarize: List[Dict[str, Any]],
focus_topic: str = None,
memory_context: str = "",
) -> Optional[str]:
"""Generate a structured summary of conversation turns.

Uses a structured template (Goal, Progress, Decisions, Resolved/Pending
Expand Down Expand Up @@ -837,6 +842,15 @@ def _generate_summary(self, turns_to_summarize: List[Dict[str, Any]], focus_topi
)

# Shared structured template (used by both paths).
# Build memory section BEFORE the template f-string so it can be
# referenced as a plain variable (avoids Python 3.11 restriction on
# backslashes inside f-string expressions). When non-empty, appears
# as ## Memory Provider Context in the compression prompt.
_memory_section = (
("\n\n## Memory Provider Context\n" + memory_context)
if memory_context
else ""
)
_template_sections = f"""## Active Task
[THE SINGLE MOST IMPORTANT FIELD. Copy the user's most recent request or
task assignment verbatim — the exact words they used. If multiple tasks
Expand Down Expand Up @@ -892,6 +906,8 @@ def _generate_summary(self, turns_to_summarize: List[Dict[str, Any]], focus_topi
## Critical Context
[Any specific values, error messages, configuration details, or data that would be lost without explicit preservation. NEVER include API keys, tokens, passwords, or credentials — write [REDACTED] instead.]

{_memory_section}

Target ~{summary_budget} tokens. Be CONCRETE — include file paths, command outputs, error messages, line numbers, and specific values. Avoid vague descriptions like "made some changes" — say exactly what changed.

Write only the summary body. Do not include any preamble or prefix."""
Expand Down Expand Up @@ -1024,7 +1040,7 @@ def _generate_summary(self, turns_to_summarize: List[Dict[str, Any]], focus_topi
else:
_reason = "timed out"
self._fallback_to_main_for_compression(e, _reason)
return self._generate_summary(turns_to_summarize, focus_topic=focus_topic) # retry immediately
return self._generate_summary(turns_to_summarize, focus_topic=focus_topic, memory_context=memory_context) # retry immediately

# Unknown-error best-effort retry on main model. Losing N turns of
# context is almost always worse than one extra summary attempt, so
Expand Down Expand Up @@ -1342,7 +1358,13 @@ def has_content_to_compress(self, messages: List[Dict[str, Any]]) -> bool:
# Main compression entry point
# ------------------------------------------------------------------

def compress(self, messages: List[Dict[str, Any]], current_tokens: int = None, focus_topic: str = None) -> List[Dict[str, Any]]:
def compress(
self,
messages: List[Dict[str, Any]],
current_tokens: int = None,
focus_topic: str = None,
memory_context: str = "",
) -> List[Dict[str, Any]]:
"""Compress conversation messages by summarizing middle turns.

Algorithm:
Expand All @@ -1360,6 +1382,12 @@ def compress(self, messages: List[Dict[str, Any]], current_tokens: int = None, f
provided, the summariser will prioritise preserving information
related to this topic and be more aggressive about compressing
everything else. Inspired by Claude Code's ``/compact``.
memory_context: Optional string returned by MemoryProvider
implementations via their ``on_pre_compress()`` hook. When
non-empty, a ``## Memory Provider Context`` section is
appended to the compression prompt so provider-extracted
insights (decisions, artifacts, blockers) are preserved in
the summary. Defaults to ``""``.
"""
# Reset per-call summary failure state — callers inspect these fields
# after compress() returns to decide whether to surface a warning.
Expand Down Expand Up @@ -1433,7 +1461,7 @@ def compress(self, messages: List[Dict[str, Any]], current_tokens: int = None, f
)

# Phase 3: Generate structured summary
summary = self._generate_summary(turns_to_summarize, focus_topic=focus_topic)
summary = self._generate_summary(turns_to_summarize, focus_topic=focus_topic, memory_context=memory_context)

# Phase 4: Assemble compressed message list
compressed = []
Expand Down
105 changes: 105 additions & 0 deletions plugins/memory/honcho/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from agent.memory_manager import sanitize_context
from agent.memory_provider import MemoryProvider
from agent.redact import redact_sensitive_text
from tools.registry import tool_error

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -1011,6 +1012,110 @@ def _is_trivial_prompt(cls, text: str) -> bool:
return True
return False

def on_pre_compress(self, messages: List[Dict[str, Any]]) -> str:
"""Extract task-state insights before context compression discards turns.

Two-channel handoff (Dialectic Compression Handoff pattern):

1. Side effect: persist conclusions to Honcho via create_conclusion().
These survive compression and feed future session context through
Honcho's dialectic layer. No LLM call — just structured writes.

2. Return: structured summary string. Activates automatically once
upstream fixes the discarded return value (GH #7192). No changes
needed elsewhere when that PR lands.

The return value is injected into the compression LLM prompt's
"## Critical Context" section, telling the compressor what to
preserve from the turns being replaced.

Scope: last 12 messages (6 turns) — the current active task cycle.
Honcho's per-turn dialectic already captures user-model facts
(preferences, style); this targets operational facts (decisions,
artifacts, errors, open items) that the compression LLM needs.
"""
if not messages:
return ""

# Guards: skip if inactive or no session to write to
if self._cron_skipped:
return ""
if not self._manager or not self._session_key:
return ""

# Extract the last ~6 turns — current active task cycle.
# Everything older has been processed by Honcho's per-turn sync.
tail = messages[-12:] if len(messages) > 12 else list(messages)

# Build structured summary; redact before analysis to avoid
# credential leakage in conclusions and return string.
summary_parts: List[str] = []

for msg in tail:
role = msg.get("role", "")
content = msg.get("content", "")
if not content or not isinstance(content, str):
continue
if role not in ("user", "assistant"):
continue

safe = redact_sensitive_text(content)
if not safe.strip():
continue

lower = safe.lower()
# Decision signals — explicit choices with rationale
if any(k in lower for k in (
"decided", "decision", "chose ", "choosing", "going with ",
"will use", "using ", "going ahead", "confirmed",
)):
# Truncate to avoid bloated conclusions on verbose exchanges
summary_parts.append(f"[decision] {safe[:400]}")
# Artifact signals — files, resources created or modified
elif any(k in lower for k in (
"created ", "creating ", "wrote ", "written ",
"modified ", "updated ", "patched ", "added ",
"/home/", "/tmp/", ".py", ".md", ".yaml", ".toml",
".json", ".sh", ".txt", "wrote to", "written to",
)):
summary_parts.append(f"[artifact] {safe[:400]}")
# Error/blocker signals
elif any(k in lower for k in (
"error", "failed", "crash", "traceback", "exception",
"blocked", "not working", "does not", "doesn't",
"refused to", "unable to", "cannot ",
)):
summary_parts.append(f"[blocker] {safe[:400]}")
# Config/state change signals
elif any(k in lower for k in (
"set ", "configured", "config changed", "threshold",
"changed ", "updated config", "set config",
)) and len(safe) < 300:
summary_parts.append(f"[config] {safe[:400]}")

if not summary_parts:
return ""

combined = "\n\n".join(summary_parts)

# Persist to Honcho via daemon thread — non-blocking, fire-and-forget.
# create_conclusion() is synchronous but the thread prevents blocking
# the compression pipeline. Errors are logged at debug level only.
def _flush():
try:
self._manager.create_conclusion(self._session_key, combined)
logger.debug(
"Honcho pre-compression flush: %d insights from %d messages",
len(summary_parts), len(tail),
)
except Exception as e:
logger.debug("Honcho pre-compression flush failed: %s", e)

t = threading.Thread(target=_flush, daemon=True, name="honcho-precompress")
t.start()

return combined

def on_turn_start(self, turn_number: int, message: str, **kwargs) -> None:
"""Track turn count for cadence and injection_frequency logic."""
self._turn_count = turn_number
Expand Down
28 changes: 24 additions & 4 deletions run_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9605,19 +9605,39 @@ def _compress_context(self, messages: list, system_message: str, *, approx_token
focus_topic,
)

# Notify external memory provider before compression discards context
# Notify external memory provider before compression discards context.
# Capture return value: providers may return structured context (decisions,
# artifacts, blockers) that should be injected into the compression
# summary. Fixes #7192.
pre_compress_context = ""
if self._memory_manager:
try:
self._memory_manager.on_pre_compress(messages)
pre_compress_context = self._memory_manager.on_pre_compress(messages)
except Exception:
pass

try:
compressed = self.context_compressor.compress(messages, current_tokens=approx_tokens, focus_topic=focus_topic)
compressed = self.context_compressor.compress(
messages,
current_tokens=approx_tokens,
focus_topic=focus_topic,
memory_context=pre_compress_context,
)
except TypeError:
# Plugin context engine with strict signature that doesn't accept
# focus_topic — fall back to calling without it.
compressed = self.context_compressor.compress(messages, current_tokens=approx_tokens)
try:
compressed = self.context_compressor.compress(
messages,
current_tokens=approx_tokens,
memory_context=pre_compress_context,
)
except TypeError:
# Plugin context engine also doesn't accept memory_context.
compressed = self.context_compressor.compress(
messages,
current_tokens=approx_tokens,
)

summary_error = getattr(self.context_compressor, "_last_summary_error", None)
if summary_error:
Expand Down
83 changes: 83 additions & 0 deletions tests/agent/test_context_compressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1631,3 +1631,86 @@ def test_pass3_emits_valid_json_for_downstream_provider(self):
parsed = _json.loads(shrunk)
assert parsed["path"] == "~/.hermes/skills/shopping/browser-setup-notes.md"
assert parsed["content"].endswith("...[truncated]")


# ---------------------------------------------------------------------------

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a regression test for the actual orchestration boundary: a MemoryManager/provider returning a sentinel must cause the current compression flow to place that sentinel in the summary prompt. The production defect is now at agent/conversation_compression.py:631-644, so direct ContextCompressor.compress() coverage alone would not catch a dropped handoff.

# TestOnPreCompressIntegration — fix #7192
# ---------------------------------------------------------------------------

class TestOnPreCompressIntegration:
"""Regression tests for capturing on_pre_compress return value (GH #7192)."""

def test_compress_accepts_memory_context_kwarg(self):
"""compress() accepts memory_context parameter without TypeError."""
with patch("agent.context_compressor.get_model_context_length", return_value=100_000):
c = ContextCompressor(
model="test/model",
threshold_percent=0.85,
protect_first_n=2,
protect_last_n=2,
quiet_mode=True,
)
msgs = [{"role": "system", "content": "sys"}, {"role": "user", "content": "hi"}] * 4
# Must not raise TypeError — must accept memory_context kwarg.
result = c.compress(msgs, memory_context="some context")
assert isinstance(result, list)

def test_generate_summary_injects_memory_context_into_prompt(self):
"""When memory_context is non-empty, it appears in the summarizer prompt."""
with patch("agent.context_compressor.get_model_context_length", return_value=100_000):
c = ContextCompressor(
model="test/model",
threshold_percent=0.85,
protect_first_n=2,
protect_last_n=2,
quiet_mode=True,
)
# Patch call_llm to capture the prompt without making an API call.
captured_prompts = []

def capture_call_llm(**kwargs):
captured_prompts.append(kwargs["messages"][0]["content"])
raise RuntimeError("test: stop")

with patch("agent.context_compressor.call_llm", side_effect=capture_call_llm):
try:
c._generate_summary(
[{"role": "user", "content": "test"}] * 3,
focus_topic=None,
memory_context="[artifact] ~/Riki/triage.md created",
)
except RuntimeError:
pass

assert len(captured_prompts) == 1
assert "Memory Provider Context" in captured_prompts[0]
assert "[artifact] ~/Riki/triage.md created" in captured_prompts[0]

def test_empty_memory_context_does_not_add_section(self):
"""When memory_context is empty, no Memory Provider Context section appears."""
with patch("agent.context_compressor.get_model_context_length", return_value=100_000):
c = ContextCompressor(
model="test/model",
threshold_percent=0.85,
protect_first_n=2,
protect_last_n=2,
quiet_mode=True,
)
captured_prompts = []

def capture_call_llm(**kwargs):
captured_prompts.append(kwargs["messages"][0]["content"])
raise RuntimeError("test: stop")

with patch("agent.context_compressor.call_llm", side_effect=capture_call_llm):
try:
c._generate_summary(
[{"role": "user", "content": "test"}] * 3,
focus_topic=None,
memory_context="",
)
except RuntimeError:
pass

assert len(captured_prompts) == 1
assert "Memory Provider Context" not in captured_prompts[0]