The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): reset read dedup caches when micro-compaction or a native checkpoint drops the bodies they point at
MongLong021422127999be10
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.pyindex 02b4c254f86..5d898e1b5f8 100644--- a/agent/chat_completion_helpers.py+++ b/agent/chat_completion_helpers.py@@ -1734,19 +1734,24 @@ def build_assistant_message(agent, assistant_message, finish_reason: str) -> dichas_replayable_native_compaction_checkpoint,)- note_checkpoint = getattr(- agent.context_compressor, "note_native_compaction_checkpoint", None- )- if (- callable(note_checkpoint)- and has_replayable_native_compaction_checkpoint(agent, [msg])- ):- note_checkpoint()- # The response priced the pre-checkpoint input, not the next- # compacted request. A matching durable prefix is now stale.- from agent.usage_anchor import set_usage_anchor-- set_usage_anchor(agent, None)+ if has_replayable_native_compaction_checkpoint(agent, [msg]):+ note_checkpoint = getattr(+ agent.context_compressor, "note_native_compaction_checkpoint", None+ )+ if callable(note_checkpoint):+ note_checkpoint()+ # The response priced the pre-checkpoint input, not the next+ # compacted request. A matching durable prefix is now stale.+ from agent.usage_anchor import set_usage_anchor++ set_usage_anchor(agent, None)+ # The next request drops every item before this checkpoint, so a repeat+ # read must serve content again, not an "unchanged" stub (#32106).+ # Without a task id the reset would clear every task's caches.+ if task_id := getattr(agent, "_current_task_id", None):+ from agent.conversation_compression import _reset_read_dedup_caches++ _reset_read_dedup_caches(task_id, session_id=getattr(agent, "session_id", None) or "")if assistant_tool_calls:msg["tool_calls"] = [_assistant_tool_call_dict(agent, tc, i) for i, tc in enumerate(assistant_tool_calls)]diff --git a/agent/turn_finalizer.py b/agent/turn_finalizer.pyindex da4f4724621..53c78050b43 100644--- a/agent/turn_finalizer.py+++ b/agent/turn_finalizer.py@@ -270,7 +270,7 @@ def _close_transcript_tail(agent, messages, final_response, interrupted, _recove_apply_override(messages)-def _micro_compact_after_turn(agent, messages, final_response, logger) -> None:+def _micro_compact_after_turn(agent, messages, final_response, logger, task_id) -> None:"""Post-turn micro-compaction: absorb the oldest uncompacted exchange into therolling summary before persist, amortizing compression across turns."""try:@@ -297,7 +297,14 @@ def _micro_compact_after_turn(agent, messages, final_response, logger) -> None:_compressor._flush_scan_cursor_invalidated = Falseagent._db_flush_scan_prefix = Noneif isinstance(_compacted, list) and _compacted:+ _spliced = _compacted is not messages # no-op and defrag passes return the inputmessages[:] = _compacted+ if _spliced:+ # The splice summarized tool results away: a repeat read must serve them+ # again, not an "unchanged" stub pointing at a body that is gone (#32106).+ from agent.conversation_compression import _reset_read_dedup_caches++ _reset_read_dedup_caches(task_id, session_id=agent.session_id or "")if _before != len(messages):logger.info("Micro-compaction: %d -> %d messages", _before, len(messages))except Exception as _mc_err:@@ -593,7 +600,7 @@ def finalize_turn(final_response, _, _ = apply_llm_output_transform(agent, final_response, turn_id=turn_id, logger=logger)_close_transcript_tail(agent, messages, final_response, interrupted, _recovered_from_stream)if not interrupted and not failed:- _micro_compact_after_turn(agent, messages, final_response, logger)+ _micro_compact_after_turn(agent, messages, final_response, logger, effective_task_id)agent._persist_session(messages, conversation_history)_guarded_cleanup("persist_session", _persist_step, _cleanup_errors, logger)diff --git a/tests/agent/test_read_dedup_rewrite_boundaries.py b/tests/agent/test_read_dedup_rewrite_boundaries.pynew file mode 100644index 00000000000..3028de8274b--- /dev/null+++ b/tests/agent/test_read_dedup_rewrite_boundaries.py@@ -0,0 +1,144 @@+"""Repeat-read dedup across transcript rewrites that are not a compaction boundary.++skill_view and read_file answer a repeat of unchanged content with an "unchanged" stub that points at the+earlier result. Post-turn micro-compaction summarizes older exchanges away, and a native Responses compaction+checkpoint takes every earlier item off the wire. After either, the result a stub would point at may be gone,+so the next repeat must serve content again, as after compaction (#32106).+"""++from __future__ import annotations++import json+from types import SimpleNamespace+from unittest.mock import MagicMock, patch++import pytest++from tools.file_tools_read_tracking import _read_tracker, _read_tracker_lock, _task_data+from tools.skills_tool import _skill_view_with_bump+from tools.skills_tool_dedup import _check_skill_view_dedup, reset_skill_view_dedup++TASK = "rewrite-boundary-task"+FILE_KEY = ("/x/big.txt", 1, 2000)+++def served(tmp_path, monkeypatch):+ """One real skill served to TASK, and one file read already served in the current generation."""+ home = tmp_path / ".hermes"+ skill_dir = home / "skills" / "demo-skill"+ skill_dir.mkdir(parents=True)+ (skill_dir / "SKILL.md").write_text(+ "---\nname: demo-skill\ndescription: Demo.\n---\n# demo-skill\n\nDo the demo steps.\n", encoding="utf-8")+ monkeypatch.setenv("HERMES_HOME", str(home))+ reset_skill_view_dedup()+ call = {"id": "call_skill", "type": "function",+ "function": {"name": "skill_view", "arguments": json.dumps({"name": "demo-skill"})}}+ body = _skill_view_with_bump({"name": "demo-skill"}, task_id=TASK)+ with _read_tracker_lock:+ _read_tracker.pop(TASK, None)+ td = _task_data(TASK)+ td["dedup"][FILE_KEY] = 1.0+ td["dedup_generation_reads"].add(FILE_KEY)+ assert _dedup_state() == (True, True)+ yield [{"role": "user", "content": "load the skill"},+ {"role": "assistant", "content": "", "tool_calls": [call]},+ {"role": "tool", "tool_call_id": "call_skill", "content": body},+ {"role": "assistant", "content": "loaded"}]+ reset_skill_view_dedup()+ with _read_tracker_lock:+ _read_tracker.pop(TASK, None)+++def _dedup_state():+ """(a skill_view repeat would get the stub, a read_file repeat would get the stub)."""+ with _read_tracker_lock:+ file_stubbed = FILE_KEY in _read_tracker.get(TASK, {}).get("dedup_generation_reads", ())+ return _check_skill_view_dedup(TASK, "demo-skill", None) is not None, file_stubbed+++def _run(agent, history, monkeypatch):+ tool_defs = [{"type": "function", "function": {"name": "skill_view", "description": "skill_view",+ "parameters": {"type": "object", "properties": {}}}}]+ monkeypatch.setattr("model_tools.get_tool_definitions", lambda *a, **kw: tool_defs)+ monkeypatch.setattr("model_tools.check_toolset_requirements", lambda *a, **kw: {})+ agent.tool_delay = 0+ agent.save_trajectories = False+ with (+ patch.object(agent, "_persist_session"),+ patch.object(agent, "_save_trajectory"),+ patch.object(agent, "_cleanup_task_resources"),+ patch("model_tools.handle_function_call",+ lambda name, args, task_id=None, **_kw: _skill_view_with_bump(args, task_id=task_id)),+ ):+ result = agent.run_conversation("go on", conversation_history=history, task_id=TASK)+ assert result["completed"] is True+ return result++[email protected]("splice", [True, False], ids=["spliced", "no_op"])+def test_micro_compaction_splice_rearms_read_dedup(served, splice, monkeypatch):+ with patch("agent.process_bootstrap.OpenAI"):+ from run_agent import AIAgent+ agent = AIAgent(api_key="test-key-1234567890", base_url="https://openrouter.ai/api/v1", quiet_mode=True,+ skip_context_files=True, skip_memory=True, max_iterations=4)+ agent.client = MagicMock()+ agent._cached_system_prompt = "You are helpful."+ agent._use_prompt_caching = False+ agent._disable_streaming = True+ compressor = MagicMock() # never asks for full compaction; only the post-turn micro pass runs+ compressor.protect_first_n, compressor.protect_last_n = 3, 20+ compressor.threshold_tokens, compressor.context_length = 500_000, 1_000_000+ compressor.last_prompt_tokens = 1_000+ compressor.awaiting_real_usage_after_compression = False+ compressor.should_compress.return_value = False+ compressor.should_compress_info.return_value = (False, None)+ compressor.should_defer_preflight_to_real_usage.return_value = True+ compressor.get_active_compression_failure_cooldown.return_value = None+ compressor._micro_compact_enabled = True+ compressor._flush_scan_cursor_invalidated = False+ # A splice returns a new list with the oldest exchange folded into the rolling summary;+ # a no-op (nothing to absorb, or a defrag) returns the input list.+ compressor._micro_compact = (+ (lambda messages: [{"role": "user", "content": "[MICRO] loaded demo-skill"}, *messages[4:]])+ if splice else (lambda messages: messages))+ agent.context_compressor = compressor+ agent.client.chat.completions.create.return_value = SimpleNamespace(+ choices=[SimpleNamespace(message=SimpleNamespace(+ content="done", reasoning_content=None, reasoning=None, tool_calls=None), finish_reason="stop")],+ model="test/model", usage=None)++ result = _run(agent, served, monkeypatch)++ assert any(m.get("content") == "[MICRO] loaded demo-skill" for m in result["messages"]) is splice+ # A splice took the skill body and may have taken the file body: both repeats serve content again.+ # A no-op pass removed nothing, so both stubs stay.+ assert _dedup_state() == ((False, False) if splice else (True, True))+++def test_native_checkpoint_rearms_read_dedup(served, monkeypatch):+ """The response carrying a server checkpoint re-views the skill: the next request starts at the+ checkpoint, so that re-view must deliver the body, not a stub for a result the wire dropped."""+ from run_agent import AIAgent+ monkeypatch.setattr("agent.model_metadata._fetch_codex_oauth_context_lengths_with_source", lambda _t: ({}, False))+ agent = AIAgent(model="gpt-5.6", base_url="https://chatgpt.com/backend-api/codex", api_key="codex-token",+ quiet_mode=True, skip_context_files=True, skip_memory=True, max_iterations=4)+ agent.codex_responses_native_compaction = True+ agent.runtime_capabilities = {"native_compaction": True}+ usage = SimpleNamespace(input_tokens=9, output_tokens=1, total_tokens=10)+ responses = [+ SimpleNamespace(output=[+ SimpleNamespace(type="compaction", encrypted_content="opaque-checkpoint"),+ SimpleNamespace(type="function_call", id="fc_view", call_id="call_view", name="skill_view",+ arguments=json.dumps({"name": "demo-skill"}))],+ usage=usage, status="completed", model="gpt-5.6"),+ SimpleNamespace(output=[SimpleNamespace(type="message", content=[SimpleNamespace(type="output_text", text="done")])],+ usage=usage, status="completed", model="gpt-5.6"),+ ]+ monkeypatch.setattr(agent, "_interruptible_api_call", lambda api_kwargs: responses.pop(0))++ result = _run(agent, served, monkeypatch)++ re_view = [m for m in result["messages"] if m.get("role") == "tool" and m.get("tool_call_id") == "call_view"]+ assert re_view and "Do the demo steps." in re_view[-1]["content"]+ assert _dedup_state()[1] is False # the file read served before the checkpoint is off the wire too