Skip to content
Python

Persist the compaction record on the compacted notice and event (OPE-170 p2)

Devika Verma3083c7b8b816
diff --git a/coworker/engine.py b/coworker/engine.py
index 6383bcdf..08fc6520 100644
--- a/coworker/engine.py
+++ b/coworker/engine.py
@@ -503,8 +503,9 @@ class TurnEngine:
yield Event(EventType.COMPACTING, {})
notice = await self._compact_now()
if notice:
- self._append_notice("compacted", notice)
- yield Event(EventType.COMPACTED, {"text": notice})
+ record = self._compaction_record()
+ self._append_notice("compacted", notice, compaction=record)
+ yield Event(EventType.COMPACTED, {"text": notice, "compaction": record})
turn: Optional[AssistantTurn] = None
streamed: list[str] = []
@@ -541,8 +542,11 @@ class TurnEngine:
yield Event(EventType.COMPACTING, {})
notice = await self._compact_now(force=True)
if notice:
- self._append_notice("compacted", notice)
- yield Event(EventType.COMPACTED, {"text": notice})
+ record = self._compaction_record()
+ self._append_notice("compacted", notice, compaction=record)
+ yield Event(
+ EventType.COMPACTED, {"text": notice, "compaction": record}
+ )
continue
# Same contract as the stop path below: the partial the user watched
# arrive survives the failure.
@@ -741,6 +745,16 @@ class TurnEngine:
cap_tokens=int(cfg["cap_tokens"]),
)
+ def _compaction_record(self) -> Optional[dict[str, Any]]:
+ """The compaction that just happened, as persisted on the `compacted` notice and
+ carried on the COMPACTED event (OPE-170 problem 2): summary text, working state,
+ the boundary into the canonical transcript, the summarizer model, and whether it
+ was the no-summary trim. Without it a saved session only showed THAT compaction
+ happened, not what was kept — the app's session record keeps only the latest
+ state, and exported trajectories / run records had nothing at all."""
+ state = self.compaction_state
+ return state.as_dict() if state is not None else None
+
async def _compact_now(self, *, force: bool = False) -> Optional[str]:
"""Run the compaction policy. Callers gate on `_compaction_due()` (or `force`,
the overflow path). Returns the user-facing notice text when the outbound view
diff --git a/tests/test_compaction_persisted_summary.py b/tests/test_compaction_persisted_summary.py
new file mode 100644
index 00000000..443bd628
--- /dev/null
+++ b/tests/test_compaction_persisted_summary.py
@@ -0,0 +1,78 @@
+"""OPE-170 problem 2: the compaction summary survives in the transcript.
+
+Before, the `compacted` notice carried only a generic sentence; the summary lived in the
+engine's in-memory `compaction_state` (persisted by the app's session record, but absent
+from `messages`, exported trajectories, and run records). Now the notice and the
+COMPACTED event carry the compaction record — summary, working state, boundary, model,
+whether it was the no-summary trim — so a saved session shows what was kept.
+"""
+
+from __future__ import annotations
+
+import json
+
+from coworker.events import EventType
+from coworker.providers import AssistantTurn
+
+from tests.test_compaction_engine import (
+ SUMMARY,
+ CompactingProvider,
+ collect,
+ long_history,
+ make_engine,
+)
+
+
+def _compacted_notices(engine):
+ return [m for m in engine.messages if m.get("role") == "notice" and m.get("kind") == "compacted"]
+
+
+def test_compacted_notice_carries_the_summary_and_state(tmp_path):
+ provider = CompactingProvider([AssistantTurn(text="done", finish_reason="stop")])
+ engine = make_engine(tmp_path, provider, messages=long_history(), cap=400)
+ events = collect(engine)
+
+ notices = _compacted_notices(engine)
+ assert len(notices) == 1
+ rec = notices[0]["compaction"]
+ assert rec["summary_text"] == SUMMARY
+ assert isinstance(rec["working_state"], str)
+ assert isinstance(rec["boundary_index"], int) and rec["boundary_index"] > 0
+ assert rec["model_used"] == "gpt-5.5"
+ assert rec["trimmed"] is False
+ assert rec["user_messages"] and rec["user_messages"][0].startswith("request")
+ assert rec == engine.compaction_state.as_dict()
+ json.dumps(notices[0]) # persisted as JSON, so it must serialise
+
+ compacted = next(e for e in events if e.type == EventType.COMPACTED)
+ assert compacted.data["compaction"]["summary_text"] == SUMMARY
+ assert "summarized" in compacted.data["text"]
+
+
+def test_trim_fallback_notice_says_so_without_a_summary(tmp_path):
+ provider = CompactingProvider(
+ [AssistantTurn(text="done", finish_reason="stop")], summary_fails=2
+ )
+ engine = make_engine(tmp_path, provider, messages=long_history(), cap=400)
+ events = collect(engine)
+
+ notices = _compacted_notices(engine)
+ assert len(notices) == 1
+ rec = notices[0]["compaction"]
+ assert rec["trimmed"] is True
+ # The trim path stores a placeholder telling the model no summary exists, not a
+ # summary; the record keeps it verbatim so readers see what the model saw.
+ assert "no summary" in rec["summary_text"].lower()
+ compacted = next(e for e in events if e.type == EventType.COMPACTED)
+ assert "trimmed" in compacted.data["text"].lower()
+ assert compacted.data["compaction"]["trimmed"] is True
+
+
+def test_notice_never_reaches_a_provider(tmp_path):
+ provider = CompactingProvider([AssistantTurn(text="done", finish_reason="stop")])
+ engine = make_engine(tmp_path, provider, messages=long_history(), cap=400)
+ collect(engine)
+ assert _compacted_notices(engine)
+ for call in provider.summary_calls:
+ assert all(m.get("role") != "notice" for m in call["messages"])
+ assert all(m.get("role") != "notice" for m in engine._outbound_messages())