The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(sessions): stranded-session adoption carries compacted history before retiring the donor
John Paul Soliva0af6b2121e22
diff --git a/hermes_state_portability.py b/hermes_state_portability.pyindex 8e18e70ee45..c41fa9df980 100644--- a/hermes_state_portability.py+++ b/hermes_state_portability.py@@ -16,6 +16,7 @@ from utils import safe_json_loadsfrom hermes_cli.timefmt import coerce_epochfrom hermes_state_ids import new_session_idfrom hermes_state_common import SCHEMA_SQL, _PREVIEW_RAW_SUBQUERY_SQL, _shape_preview, _sql_session_last_active+from hermes_state_messages import _parse_tool_calls, _tool_calls_count# Pre-split logger identity so log filtering/capture is unchanged.logger = logging.getLogger("hermes_state")@@ -276,23 +277,29 @@ class SessionPortabilityMixin:# ── Export ─────────────────────────────────────────────────────────────- def _with_messages(self, session: Dict[str, Any], include_compacted: bool = False) -> Dict[str, Any]:- messages = self.get_messages(session["id"], include_compacted=include_compacted)+ def _with_messages(self, session: Dict[str, Any], include_compacted: bool = False,+ include_inactive: bool = False) -> Dict[str, Any]:+ messages = self.get_messages(session["id"], include_inactive=include_inactive, include_compacted=include_compacted)return {**session, "messages": messages, "timings": _export_timings(messages, session["id"])}- def export_session(self, session_id: str, include_compacted: bool = False) -> Optional[Dict[str, Any]]:+ def export_session(self, session_id: str, include_compacted: bool = False,+ include_inactive: bool = False) -> Optional[Dict[str, Any]]:"""Export a single session with all its messages as a dict. ``include_compacted`` adds the turnsin-place compaction archived (the history the user still sees); it stays off for payloads that go- back through :meth:`import_sessions`, which would insert those turns as live context."""+ back through :meth:`import_sessions`, because that projection is deduped and ordered for display, and+ importing it would make those turns live context. ``include_inactive`` exports every row in storage+ order with its ``active``/``compacted`` flags, which :meth:`import_sessions` restores as archived."""session = self.get_session(session_id)- return self._with_messages(session, include_compacted) if session else None+ return self._with_messages(session, include_compacted, include_inactive) if session else None- def export_session_lineage(self, session_id: str, include_compacted: bool = False) -> Optional[Dict[str, Any]]:- """Export a compression lineage as one logical session dict (``include_compacted`` as in :meth:`export_session`)."""+ def export_session_lineage(self, session_id: str, include_compacted: bool = False,+ include_inactive: bool = False) -> Optional[Dict[str, Any]]:+ """Export a compression lineage as one logical session dict (flags as in :meth:`export_session`)."""lineage_ids = self.get_compression_lineage(session_id)if not lineage_ids:return None- segments = [seg for seg in (self.export_session(sid, include_compacted) for sid in lineage_ids) if seg]+ segments = [seg for seg in (self.export_session(sid, include_compacted, include_inactive)+ for sid in lineage_ids) if seg]if not segments:return Nonemessages = [msg for seg in segments for msg in (seg.get("messages") or [])]@@ -330,8 +337,10 @@ class SessionPortabilityMixin:heal: a profile bot's rows accumulated in the DEFAULT profile's state.db before thedesktop routed session RPCs by target session). Pure composition``donor_db.export_session_lineage()`` -> ``self.import_sessions()``: runtime- fields reset, already-present ids skipped (idempotent). With ``retire_donor`` and- a complete adoption, donor rows are ARCHIVED (never deleted) with+ fields reset, already-present ids skipped (idempotent). Every message row is carried+ with its archived state, so turns in-place compaction archived stay visible here and+ stay out of model context. With ``retire_donor`` and a complete adoption, donor rows+ are ARCHIVED (never deleted) with``end_reason='adopted_by_profile'`` — deliberately NOT in the recoverable set, soresurrection cannot undo an adoption. Returns the ``import_sessions`` dict plus``adopted`` and ``donor_retired`` (True only when EVERY segment retired).@@ -340,7 +349,9 @@ class SessionPortabilityMixin:the same chat 4001'd for the opposite reason. This method moves the conversation to where routingnow looks for it. See #93091, #93296."""- payload = donor_db.export_session_lineage(session_id)+ # Every row, not just live ones: the retired donor is unrecoverable, so any turn left behind+ # (compaction-archived history included) would be visible nowhere.+ payload = donor_db.export_session_lineage(session_id, include_inactive=True)if not payload:return {"ok": False, "adopted": False, "donor_retired": False,"error": f"session {session_id!r} not found in donor store"}@@ -355,7 +366,7 @@ class SessionPortabilityMixin:if not seg_id or self.get_session(seg_id) is None:continuedonor_count = len(seg.get("messages") or [])- local_count = len(self.get_messages(seg_id))+ local_count = len(self.get_messages(seg_id, include_inactive=True))if donor_count > local_count:donor_ahead = Truelogger.warning("adoption divergence: donor segment %s has %d messages, "@@ -382,8 +393,8 @@ class SessionPortabilityMixin:A retirement failure must not fail the adoption (a later resume retriesidempotently), but never claims success it didn't have."""try:- donor_now = len(donor_db.get_messages(seg_id))- local_now = len(self.get_messages(seg_id))+ donor_now = len(donor_db.get_messages(seg_id, include_inactive=True))+ local_now = len(self.get_messages(seg_id, include_inactive=True))if donor_now > local_now:logger.warning("adoption divergence at retire time: donor segment %s grew to %d messages (local %d) — "@@ -535,6 +546,14 @@ class SessionPortabilityMixin:{**msg, **{key: _json_value(msg.get(key)) for key in _IMPORT_MESSAGE_JSON_FIELDS}} for msg in messages]total_messages, total_tool_calls = self._insert_message_rows(conn, session_id, sanitized_messages)+ # A row exported archived (``include_inactive``) must stay archived: inserted live, compacted or+ # rewound turns would re-enter model context. Session counters count live rows only.+ archived = [msg for msg in sanitized_messages if "active" in msg and not msg["active"] and "_row_id" in msg]+ if archived:+ conn.executemany("UPDATE messages SET active = 0, compacted = ? WHERE id = ?",+ [(1 if msg.get("compacted") else 0, msg["_row_id"]) for msg in archived])+ total_messages -= len(archived)+ total_tool_calls -= sum(_tool_calls_count(_parse_tool_calls(msg.get("tool_calls"))) for msg in archived)conn.execute("UPDATE sessions SET message_count = ?, tool_call_count = ? WHERE id = ?",(total_messages, total_tool_calls, session_id))diff --git a/tests/tui_gateway/test_stranded_session_adoption.py b/tests/tui_gateway/test_stranded_session_adoption.pyindex ed66c96c380..d4323322728 100644--- a/tests/tui_gateway/test_stranded_session_adoption.py+++ b/tests/tui_gateway/test_stranded_session_adoption.py@@ -399,8 +399,8 @@ def test_donor_growth_between_export_and_retire_blocks_retirement(stores, monkeyreal_export = default_db.export_session_lineage- def _export_then_append(session_id):- payload = real_export(session_id)+ def _export_then_append(session_id, **kwargs):+ payload = real_export(session_id, **kwargs)# Another backend appends AFTER the export snapshot is taken.default_db.append_message(STRANDED_ID, "user", "raced question")default_db.append_message(STRANDED_ID, "assistant", "raced answer")@@ -420,3 +420,37 @@ def test_donor_growth_between_export_and_retire_blocks_retirement(stores, monkeysecond = profile_db.adopt_session_lineage_from(default_db, STRANDED_ID)assert second["donor_retired"] is Falseassert not default_db.get_session(STRANDED_ID)["archived"]+++def _resume_as_developer(mod, session_id):+ return mod.handle_request({"id": "20", "method": "session.resume",+ "params": {"session_id": session_id, "profile": "developer", "lazy": True}})+++def _shown(db, session_id, **flags):+ return [(m["role"], m["content"]) for m in db.get_messages(session_id, **flags)]+++def test_adoption_carries_compacted_history_before_retiring_the_donor(gateway):+ """The retired donor is unrecoverable, so the profile copy must show everything the donor+ showed (turns archived by in-place compaction included) and feed the model the same live+ context; a live-rows-only copy left those turns visible nowhere."""+ mod, default_db, profile_home = gateway+ _seed_stranded(default_db, turns=4)+ tail = default_db.get_messages(STRANDED_ID)[-2:]+ default_db.archive_and_compact(+ STRANDED_ID, [{"role": "user", "content": "[summary of turns 1-3]"}, *tail], tail_count=2)+ shown_before = _shown(default_db, STRANDED_ID, include_compacted=True)+ live_before = _shown(default_db, STRANDED_ID)+ assert len(shown_before) > len(live_before)++ resp = _resume_as_developer(mod, STRANDED_ID)++ assert not resp.get("error"), resp.get("error")+ pdb = SessionDB(db_path=profile_home / "state.db")+ try:+ assert _shown(pdb, STRANDED_ID, include_compacted=True) == shown_before+ assert _shown(pdb, STRANDED_ID) == live_before+ finally:+ pdb.close()+