Fix Responses state after history compaction

Clear the active Responses provider continuation when automatic history compression or manual chat compaction rewrites local history, while preserving stored response IDs for cleanup. Add focused regressions for both compression paths so compacted chats do not keep stale provider-side context.

Alessandro committed Jul 7, 2026 at 16:59 UTC ad148f24cc1bcf66c8f8ec9caece6f0909f9a39d
9 files changed +155 -4
extensions/python/message_loop_end/AGENTS.md
+1
@@ -12,6 +12,7 @@
12
13 - Preserve history consistency before saving chats.
14 - Do not skip persistence for successful loops unless the hook contract explicitly permits it.
15 +- History compression that rewrites local history must clear the active Responses provider continuation while preserving stored response IDs for cleanup.
16
17 ## Work Guidance
18
extensions/python/message_loop_end/_10_organize_history.py
+9 -1
@@ -1,10 +1,18 @@
1 from helpers.extension import Extension
2 from agent import LoopData
3 from helpers.defer import DeferredTask, THREAD_BACKGROUND
4 +from helpers.history import clear_responses_provider_state
5
6 DATA_NAME_TASK = "_organize_history_task"
7
8
9 +async def compress_history(agent) -> bool:
10 + compressed = bool(await agent.history.compress())
11 + if compressed:
12 + clear_responses_provider_state(agent)
13 + return compressed
14 +
15 +
16 class OrganizeHistory(Extension):
17 async def execute(self, loop_data: LoopData = LoopData(), **kwargs):
18 if not self.agent:
@@ -17,6 +25,6 @@ class OrganizeHistory(Extension):
25
26 # start task
27 task = DeferredTask(thread_name=THREAD_BACKGROUND)
20 - task.start_task(self.agent.history.compress)
28 + task.start_task(compress_history, self.agent)
29 # set to agent to be able to wait for it
30 self.agent.set_data(DATA_NAME_TASK, task)
extensions/python/message_loop_prompts_before/_90_organize_history_wait.py
+5 -2
@@ -1,6 +1,9 @@
1 from helpers.extension import Extension
2 from agent import LoopData
3 -from extensions.python.message_loop_end._10_organize_history import DATA_NAME_TASK
3 +from extensions.python.message_loop_end._10_organize_history import (
4 + DATA_NAME_TASK,
5 + compress_history,
6 +)
7 from helpers.defer import DeferredTask, THREAD_BACKGROUND
8
9 MAX_SYNC_COMPRESSION_PASSES = 64
@@ -33,7 +36,7 @@ class OrganizeHistoryWait(Extension):
36 else:
37 # no task was running, start and wait
38 self.agent.context.log.set_progress("Compressing history...")
36 - compressed = await self.agent.history.compress()
39 + compressed = await compress_history(self.agent)
40
41 after_tokens = self.agent.history.get_tokens()
42 if not compressed or after_tokens >= before_tokens:
helpers/history.py
+22
@@ -723,6 +723,28 @@ def output_text(messages: list[OutputMessage], ai_label="ai", human_label="human
723 return "\n".join(_stringify_output(o, ai_label, human_label) for o in messages)
724
725
726 +def clear_responses_provider_state(agent) -> None:
727 + key = getattr(agent, "DATA_NAME_RESPONSES_STATE", "responses_state")
728 + get_data = getattr(agent, "get_data", None)
729 + set_data = getattr(agent, "set_data", None)
730 + if not callable(get_data) or not callable(set_data):
731 + return
732 +
733 + state = get_data(key)
734 + if not isinstance(state, dict):
735 + return
736 +
737 + state = dict(state)
738 + removed = False
739 + for field in ("response_id", "previous_response_id"):
740 + if field in state:
741 + state.pop(field, None)
742 + removed = True
743 +
744 + if removed:
745 + set_data(key, state)
746 +
747 +
748 def _merge_outputs(a: MessageContent, b: MessageContent) -> MessageContent:
749 if isinstance(a, str) and isinstance(b, str):
750 return a + "\n" + b
helpers/history.py.dox.md
+2
@@ -65,6 +65,7 @@
65 - `group_messages_abab(messages: list[BaseMessage]) -> list[BaseMessage]`
66 - `output_langchain(messages: list[OutputMessage])`
67 - `output_text(messages: list[OutputMessage], ai_label=..., human_label=...)`
68 +- `clear_responses_provider_state(agent) -> None`
69 - `_merge_outputs(a: MessageContent, b: MessageContent) -> MessageContent`
70 - `_merge_properties(a: Dict[str, MessageContent], b: Dict[str, MessageContent]) -> Dict[str, MessageContent]`
71 - `_is_raw_message(obj: object) -> bool`
@@ -77,6 +78,7 @@
78
79 - Helper modules own reusable framework APIs and must preserve public callers unless all callers, tests, and docs are updated together.
80 - Update this file whenever public functions, classes, persistence behavior, path/security assumptions, side effects, or cross-module contracts change.
81 +- `clear_responses_provider_state(agent)` removes the active provider continuation IDs after local history rewrites while preserving stored response ID lists for later cleanup.
82 - Observed side-effect areas: filesystem writes, filesystem deletion, model calls, plugin state, settings/state persistence, secret handling.
83 - Imported dependency areas include: `abc`, `asyncio`, `collections`, `collections.abc`, `enum`, `helpers`, `json`, `langchain_core.messages`, `math`, `plugins._model_config.helpers.model_config`, `typing`, `uuid`.
84
plugins/_chat_compaction/AGENTS.md
+1
@@ -19,6 +19,7 @@
19 - Backup JSON and transcript artifacts must remain UTF-8 writable when chat content contains malformed Unicode such as lone surrogates.
20 - Keep generated summaries bounded by configured model and token limits.
21 - Preserve loaded skill names from `skill_instructions` metadata without copying full skill bodies into compacted summaries.
22 +- After replacing local history, clear the active Responses provider continuation while preserving stored response IDs for cleanup.
23 - Do not discard original context data unless the compaction flow explicitly owns that behavior.
24
25 ## Work Guidance
plugins/_chat_compaction/helpers/compactor.py
+2 -1
@@ -5,7 +5,7 @@ from collections import deque
5 import models as models_module
6 from agent import Agent
7 from helpers import files, tokens
8 -from helpers.history import History, output_text
8 +from helpers.history import History, clear_responses_provider_state, output_text
9 from helpers.persist_chat import (
10 export_json_chat,
11 get_chat_folder_path,
@@ -145,6 +145,7 @@ async def run_compaction(
145
146 agent.history = History(agent=agent)
147 agent.history.add_message(ai=True, content=compacted_content)
148 + clear_responses_provider_state(agent)
149
150 # Clear subordinate chain
151 agent.data.pop(Agent.DATA_NAME_SUBORDINATE, None)
tests/test_chat_compaction.py
+79
@@ -43,6 +43,49 @@ class _RecordingModel:
43 return f"summary-{len(self.user_messages)}", None
44
45
46 +class _CompactionHistory:
47 + def output(self):
48 + return [{"ai": False, "content": "hello"}]
49 +
50 +
51 +class _CompactionLog:
52 + def __init__(self):
53 + self.logs = []
54 + self.entries = []
55 + self.reset_called = False
56 + self.progress = None
57 +
58 + def log(self, **kwargs):
59 + self.entries.append(kwargs)
60 + return _FakeLog()
61 +
62 + def reset(self):
63 + self.reset_called = True
64 +
65 + def set_progress(self, *args, **kwargs):
66 + self.progress = (args, kwargs)
67 +
68 +
69 +class _CompactionAgent:
70 + DATA_NAME_RESPONSES_STATE = "responses_state"
71 +
72 + def __init__(self):
73 + self.history = _CompactionHistory()
74 + self.data = {
75 + "responses_state": {
76 + "response_id": "resp_current",
77 + "previous_response_id": "resp_previous",
78 + "response_ids": ["resp_previous", "resp_current"],
79 + }
80 + }
81 +
82 + def get_data(self, key):
83 + return self.data.get(key)
84 +
85 + def set_data(self, key, value):
86 + self.data[key] = value
87 +
88 +
89 def test_pre_compaction_backup_sanitizes_surrogate_text(tmp_path, monkeypatch):
90 monkeypatch.setattr(
91 compactor,
@@ -114,3 +157,39 @@ async def test_large_compaction_does_not_send_unsplit_single_line_payload(monkey
157 assert len(chunk_messages) > 2
158 assert all(chunk_messages)
159 assert all(len(message) <= 10_000 for message in chunk_messages)
160 +
161 +
162 +@pytest.mark.asyncio
163 +async def test_manual_compaction_clears_active_responses_state(monkeypatch):
164 + async def fake_single_pass(*args, **kwargs):
165 + return "summary"
166 +
167 + agent = _CompactionAgent()
168 + context = SimpleNamespace(
169 + id="compact-chat",
170 + agent0=agent,
171 + log=_CompactionLog(),
172 + streaming_agent=object(),
173 + )
174 +
175 + monkeypatch.setattr(
176 + compactor,
177 + "_build_model",
178 + lambda *args: ({"ctx_length": 128000}, _RecordingModel()),
179 + )
180 + monkeypatch.setattr(compactor, "_compact_single_pass", fake_single_pass)
181 + monkeypatch.setattr(
182 + compactor,
183 + "_save_pre_compaction_backup",
184 + lambda *args: {"txt": "/tmp/pre.txt"},
185 + )
186 + monkeypatch.setattr(compactor, "save_tmp_chat", lambda *args: None)
187 + monkeypatch.setattr(compactor, "remove_msg_files", lambda *args: None)
188 + monkeypatch.setattr(compactor, "mark_dirty_all", lambda *args, **kwargs: None)
189 +
190 + await compactor.run_compaction(context)
191 +
192 + state = agent.data["responses_state"]
193 + assert "response_id" not in state
194 + assert "previous_response_id" not in state
195 + assert state["response_ids"] == ["resp_previous", "resp_current"]
tests/test_history_compression_wait.py
+34
@@ -45,6 +45,23 @@ class _MaxPassHistory:
45 return True
46
47
48 +class _CompressOnceHistory:
49 + def __init__(self):
50 + self.compress_calls = 0
51 + self.tokens = 2000
52 +
53 + def is_over_limit(self):
54 + return self.compress_calls == 0
55 +
56 + def get_tokens(self):
57 + return self.tokens
58 +
59 + async def compress(self):
60 + self.compress_calls += 1
61 + self.tokens -= 1000
62 + return True
63 +
64 +
65 class _FakeLog:
66 def __init__(self):
67 self.entries = []
@@ -94,3 +111,20 @@ async def test_history_wait_stops_after_max_sync_compression_passes():
111 f"stopped after {MAX_SYNC_COMPRESSION_PASSES} passes"
112 in agent.context.log.entries[-1]["content"]
113 )
114 +
115 +
116 +@pytest.mark.asyncio
117 +async def test_history_compression_clears_active_responses_state():
118 + agent = _FakeAgent(_CompressOnceHistory())
119 + agent.data["responses_state"] = {
120 + "response_id": "resp_current",
121 + "previous_response_id": "resp_previous",
122 + "response_ids": ["resp_previous", "resp_current"],
123 + }
124 +
125 + await OrganizeHistoryWait(agent).execute()
126 +
127 + state = agent.data["responses_state"]
128 + assert "response_id" not in state
129 + assert "previous_response_id" not in state
130 + assert state["response_ids"] == ["resp_previous", "resp_current"]