The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): derive the micro-compaction cursor from the spliced list
Michael Jordand6265285604e
diff --git a/agent/context_compressor.py b/agent/context_compressor.pyindex 319e5507af4..be41e8f84c5 100644--- a/agent/context_compressor.py+++ b/agent/context_compressor.py@@ -5349,6 +5349,7 @@ This compaction should PRIORITISE preserving all information related to the focuif self._needs_defrag():self._defrag_rolling_summary(messages, exchange_start, compress_end)result = self._splice_micro_compact_result(messages, exchange_start, compress_end)+ self._micro_compact_cursor = self._cursor_after_splice(result, exchange_start + 1)self._sync_micro_compact_to_db(result)self._micro_compact_consecutive_failures = 0self._micro_compact_last_failure_cursor = -1@@ -5408,6 +5409,7 @@ This compaction should PRIORITISE preserving all information related to the focuself._micro_compact_last_failure_cursor = -1result = self._splice_micro_compact_result(messages, exchange_start, exchange_end)+ self._micro_compact_cursor = self._cursor_after_splice(result, exchange_start + 1)self._sync_micro_compact_to_db(result)self._emit_micro_compaction_telemetry(outcome="absorbed",@@ -5420,6 +5422,29 @@ This compaction should PRIORITISE preserving all information related to the focu)return result+ def _cursor_after_splice(+ self,+ result: List[Dict[str, Any]],+ fallback: int,+ ) -> int:+ """Cursor position just past the summary marker in *result*.++ The cursor must be derived from the spliced list, never carried over+ from pre-splice indices. A splice collapses the absorbed span (an+ assistant plus its tool results -- often several messages) into a+ single marker, and may also drop a superseded marker further back, so+ every index after it shifts. Reusing the old ``exchange_end`` left the+ cursor pointing into the middle of a *later* exchange's tool group;+ the next pass then walked forward to the following assistant and+ skipped that exchange entirely, so roughly half the work silently+ never happened on tool-bearing conversations.+ """+ for idx in range(len(result) - 1, -1, -1):+ entry = result[idx]+ if isinstance(entry, dict) and entry.get(COMPRESSED_SUMMARY_METADATA_KEY):+ return idx + 1+ return fallback+def _emit_micro_compaction_telemetry(self,*,diff --git a/tests/agent/test_micro_compaction.py b/tests/agent/test_micro_compaction.pyindex 0178ad1ce53..4ac3344a467 100644--- a/tests/agent/test_micro_compaction.py+++ b/tests/agent/test_micro_compaction.py@@ -124,6 +124,46 @@ class TestMicroCompaction:]assert surviving == originals, "user turns must survive verbatim"+ def test_cursor_is_derived_from_the_spliced_list(self):+ """The cursor must never carry over a pre-splice index.++ A splice collapses an assistant plus its tool results -- often several+ messages -- into one marker, so every later index shifts. Reusing+ ``exchange_end`` left the cursor pointing inside a *later* exchange's+ tool group; the next pass walked forward to the following assistant+ and skipped that exchange entirely, so on tool-bearing conversations+ roughly half the work silently never happened.++ Tool-free fixtures cannot catch this: the span is one message, so+ nothing shifts.+ """+ cc = _compressor()+ msgs = [{"role": "system", "content": "sys"}]+ for i in range(8):+ msgs.append({"role": "user", "content": f"q{i}"})+ msgs.append({+ "role": "assistant",+ "content": f"a{i}",+ "tool_calls": [+ {"id": f"c{i}-{j}", "type": "function",+ "function": {"name": "f", "arguments": "{}"}}+ for j in range(3)+ ],+ })+ for j in range(3):+ msgs.append({"role": "tool", "tool_call_id": f"c{i}-{j}",+ "content": "T" * 500})++ for _ in range(4):+ msgs = cc._micro_compact(msgs)+ marker_idx = next(+ i for i, m in enumerate(msgs)+ if m.get(COMPRESSED_SUMMARY_METADATA_KEY)+ )+ assert cc._micro_compact_cursor == marker_idx + 1, (+ "cursor must sit just past the marker in the spliced list"+ )+def test_short_conversation_is_untouched(self):cc = _compressor()messages = [