The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(relay): bound compaction marks and skip unused prune estimates
mnajafian-nv3b146be475e8
diff --git a/agent/compaction_events.py b/agent/compaction_events.pyindex 95e5f22007a..a2a88170d79 100644--- a/agent/compaction_events.py+++ b/agent/compaction_events.py@@ -235,11 +235,19 @@ def provider_native_payload(*, session_id: str, compression_count: Any = None) -)-def _publish(build: Any, *args: Any, turn_session_id: str | None = None, **kwargs: Any) -> None:+def _publish(+ build: Any, *args: Any, turn_session_id: str | None = None, require_target_session_id: str | None = None,+ **kwargs: Any,+) -> None:"""Build one record and fan it out to the built-in Relay integration. Never raises into compaction: thefirst failure logs at WARNING with its traceback, later ones at DEBUG."""global _warnedtry:+ if require_target_session_id is not None:+ from agent.relay_compaction import compaction_target_available++ if not compaction_target_available(require_target_session_id, turn_session_id=turn_session_id):+ returnpayload = build(*args, **kwargs)from agent.relay_compaction import emit_compaction_mark@@ -279,12 +287,14 @@ def publish_micro(record: dict[str, Any], *, turn_session_id: str | None = None)def publish_prune(agent: Any, messages: list, pruned: list, tool_results_pruned: int) -> None:"""Publish one committed proactive tool-result prune, measured like the attempt record (rough estimate ofthe message list the model sees next)."""+ session_id = getattr(agent, "session_id", None) or ""+ turn_session_id = _turn_session_id(agent)if _publishes_for(agent):_publish(lambda: prune_payload(- session_id=getattr(agent, "session_id", None) or "", tokens_before=estimate_messages_tokens_rough(messages),+ session_id=session_id, tokens_before=estimate_messages_tokens_rough(messages),tokens_after=estimate_messages_tokens_rough(pruned), messages=len(pruned),tool_results_pruned=tool_results_pruned,- ), turn_session_id=_turn_session_id(agent))+ ), turn_session_id=turn_session_id, require_target_session_id=session_id)def publish_provider_native(agent: Any) -> None:diff --git a/agent/relay_compaction.py b/agent/relay_compaction.pyindex 0fac75e75f6..5c7b7b224fe 100644--- a/agent/relay_compaction.py+++ b/agent/relay_compaction.py@@ -61,6 +61,11 @@ def _resolve_target(return target+def compaction_target_available(session_id: str, *, turn_session_id: str | None = None) -> bool:+ """Whether a mark for this session has a live Relay scope to receive it."""+ return bool(session_id and _resolve_target(session_id, turn_session_id))++def emit_compaction_mark(session_id: str, name: str, data: dict[str, Any], *, turn_session_id: str | None = None,) -> bool:@@ -72,6 +77,6 @@ def emit_compaction_mark(host, session, handle = targethost.run_in_session(session, host.relay.scope.event, name, handle=handle, data=data, data_schema=dict(COMPACTION_DATA_SCHEMA),- metadata=relay_runtime.runtime_metadata(host.runtime_id),+ metadata=relay_runtime.runtime_metadata(host.runtime_id), timeout=relay_runtime._SCOPE_OP_TIMEOUT,)return Truediff --git a/tests/agent/test_compaction_events.py b/tests/agent/test_compaction_events.pyindex dad2e23ac70..2763ea8574f 100644--- a/tests/agent/test_compaction_events.py+++ b/tests/agent/test_compaction_events.py@@ -36,6 +36,7 @@ V1_FIELDS = {def published(monkeypatch):"""Every (session_id, mark name, payload) handed to the Relay emitter."""marks = []+ monkeypatch.setattr("agent.relay_compaction.compaction_target_available", lambda *_args, **_kwargs: True)monkeypatch.setattr("agent.relay_compaction.emit_compaction_mark",lambda session_id, name, data, **_kwargs: marks.append((session_id, name, data)) or True,@@ -179,6 +180,15 @@ def test_a_raising_emitter_never_changes_the_compaction_result(monkeypatch, caplassert failures[0].exc_info is not None+def test_prune_without_relay_target_skips_transcript_estimates(monkeypatch):+ agent = _Agent(_compressor())+ messages = _messages()+ monkeypatch.setattr("agent.relay_compaction.compaction_target_available", lambda *_args, **_kwargs: False)+ with patch("agent.compaction_events.estimate_messages_tokens_rough") as estimate:+ compaction_events.publish_prune(agent, messages, messages[:-1], 1)+ estimate.assert_not_called()++def test_overflow_attempt_records_its_classified_overflow_reason(published):agent = _Agent(_compressor())diff --git a/tests/agent/test_relay_compaction.py b/tests/agent/test_relay_compaction.pyindex ffbe4dba2a9..23b18f74bfa 100644--- a/tests/agent/test_relay_compaction.py+++ b/tests/agent/test_relay_compaction.py@@ -7,6 +7,7 @@ hygiene runs outside any turn). Relay resets LLM-history freshness only for thefrom __future__ import annotations+import threadingfrom typing import Anyfrom unittest.mock import patch@@ -135,6 +136,39 @@ def test_no_relay_host_skips_quietly(monkeypatch):assert emit_compaction_mark("s1", "compaction", _committed("s1")) is False+def test_stalled_relay_event_does_not_hold_the_caller(monkeypatch, relay):+ fake, runtime, coordinator = relay+ coordinator.acquire_conversation(profile_key=runtime.profile_key, session_id="s1", platform="cli")+ entered, release = threading.Event(), threading.Event()++ def stalled_event(_name, **_kwargs):+ entered.set()+ release.wait()++ monkeypatch.setattr(fake.scope, "event", stalled_event)+ monkeypatch.setattr(relay_runtime, "_SCOPE_OP_TIMEOUT", 0.05)+ result = []++ def publish():+ try:+ emit_compaction_mark("s1", "compaction", _committed("s1"))+ except Exception as exc:+ result.append(exc)++ publisher = threading.Thread(target=publish, daemon=True)+ try:+ publisher.start()+ publisher.join(timeout=2)+ assert not publisher.is_alive()+ assert entered.is_set()+ assert len(result) == 1+ assert isinstance(result[0], TimeoutError)+ assert "Relay scope operation exceeded" in str(result[0])+ finally:+ release.set()+ publisher.join(timeout=2)++def test_a_turn_relay_does_not_instrument_records_no_mark(relay):"""A second concurrent turn on a live session (a background-review fork, or a concurrent gateway turn) runswith Relay instrumentation off. Its compaction must not mark the live session or reset its freshness."""