Skip to content

The agent that grows with you

fix(agent): clear archived parent's activity labels after rotation (re-review #4)

Teknium0628e333472f
diff --git a/agent/conversation_compression.py b/agent/conversation_compression.py
index 4cce62313e9..206fc663449 100644
--- a/agent/conversation_compression.py
+++ b/agent/conversation_compression.py
@@ -3341,6 +3341,31 @@ def compress_context(
)
_boundary_parent = _old_sid or agent.session_id or ""
+ # Round-2 #4: the activity heartbeat's terminal "context compression
+ # completed" stamp landed on the PARENT row (force-persisted before
+ # the rotation re-pointed agent.session_id at the child). Without a
+ # cleanup, the archived parent advertises a fresh last_activity_at +
+ # "context compression completed" forever — a permanent false-fresh
+ # row for any activity consumer that scans ended sessions. Clear the
+ # labels on the parent best-effort (keeps last_activity_at so idle
+ # clocks stay continuous; the CHILD carries the live labels).
+ if _old_sid and _session_commit_succeeded:
+ try:
+ _labels_db = getattr(agent, "_session_db", None)
+ _clear_labels = getattr(
+ type(_labels_db) if _labels_db is not None else None,
+ "clear_session_activity_labels",
+ None,
+ )
+ if callable(_clear_labels):
+ _clear_labels(_labels_db, _old_sid)
+ except Exception:
+ logger.debug(
+ "failed to clear archived compression parent's activity "
+ "labels (ignored)",
+ exc_info=True,
+ )
+
# Notify the context engine that a compaction boundary occurred. Plugin
# engines (e.g. hermes-lcm) use boundary_reason="compression" to preserve
# DAG lineage / checkpoint per-session state across the boundary instead of
diff --git a/tests/agent/test_compression_rotation_state.py b/tests/agent/test_compression_rotation_state.py
index c7b1ea01c2a..03f5017cd33 100644
--- a/tests/agent/test_compression_rotation_state.py
+++ b/tests/agent/test_compression_rotation_state.py
@@ -683,3 +683,46 @@ class TestTodoSnapshotScaffoldingTails:
TODO_INJECTION_HEADER in str(message.get("content") or "")
for message in compressed
)
+
+
+class TestArchivedParentActivityLabelsCleared:
+ def test_parent_labels_cleared_after_rotation_child_lineage_intact(
+ self, tmp_path: Path
+ ):
+ """Round-2 #4: the terminal heartbeat stamp must not stay on the parent.
+
+ The compression activity heartbeat force-persists "context compression
+ completed" against the PARENT id (agent.session_id at stamp time).
+ After the out-of-place rotation the parent is archived; its activity
+ labels must be cleared so it doesn't advertise a fresh
+ last_activity_at + terminal label forever, while the child keeps its
+ lineage.
+ """
+ from agent.session_activity import ActivityProvenance
+
+ db = SessionDB(db_path=tmp_path / "state.db")
+ parent = "PARENT_ACTIVITY_LABELS"
+ db.create_session(parent, source="cli")
+ agent = _build_agent_with_db(db, parent)
+
+ agent._compress_context(_msgs(), "sys", approx_tokens=120_000)
+ child = agent.session_id
+ assert child != parent # rotation happened
+
+ # Child lineage intact.
+ child_row = db.get_session(child)
+ assert child_row is not None
+ assert child_row.get("parent_session_id") == parent
+
+ # Parent archived with cleared activity labels.
+ parent_row = db.get_session(parent)
+ assert parent_row is not None
+ assert parent_row.get("ended_at") is not None
+ assert not parent_row.get("last_activity_description"), (
+ "archived compression parent kept a stale activity description "
+ f"({parent_row.get('last_activity_description')!r})"
+ )
+ prov = parent_row.get("last_activity_provenance")
+ assert not prov or prov == ActivityProvenance.UNKNOWN.value, (
+ f"archived parent kept terminal provenance {prov!r}"
+ )