mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-04-25 00:51:20 +00:00
fix(sessions): surface compression tips in session lists and resume lookups (#12960)
After a conversation gets compressed, run_agent's _compress_context ends the parent session and creates a continuation child with the same logical conversation. Every list affordance in the codebase (list_sessions_rich with its default include_children=False, plus the CLI/TUI/gateway/ACP surfaces on top of it) hid those children, and resume-by-ID on the old root landed on a dead parent with no messages. Fix: lineage-aware projection on the read path. - hermes_state.py::get_compression_tip(session_id) — walk the chain forward using parent.end_reason='compression' AND child.started_at >= parent.ended_at. The timing guard separates compression continuations from delegate subagents (which were created while the parent was still live) without needing a schema migration. - hermes_state.py::list_sessions_rich — new project_compression_tips flag (default True). For each compressed root in the result, replace surfaced fields (id, ended_at, end_reason, message_count, tool_call_count, title, last_active, preview, model, system_prompt) with the tip's values. Preserve the root's started_at so chronological ordering stays stable. Projected rows carry _lineage_root_id for downstream consumers. Pass False to get raw roots (admin/debug). - hermes_cli/main.py::_resolve_session_by_name_or_id — project forward after ID/title resolution, so users who remember an old root ID (from notes, or from exit summaries produced before the sibling Bug 1 fix) land on the live tip. All downstream callers of list_sessions_rich benefit automatically: - cli.py _list_recent_sessions (/resume, show_history affordance) - hermes_cli/main.py sessions list / sessions browse - tui_gateway session.list picker - gateway/run.py /resume titled session listing - tools/session_search_tool.py - acp_adapter/session.py Tests: 7 new in TestCompressionChainProjection covering full-chain walks, delegate-child exclusion, tip surfacing with lineage tracking, raw-root mode, chronological ordering, and broken-chain graceful fallback. Verified live: ran a real _compress_context on a live Gemini-backed session, confirmed the DB split, then verified - db.list_sessions_rich surfaces tip with _lineage_root_id set - hermes sessions list shows the tip, not the ended parent - _resolve_session_by_name_or_id(old_root_id) -> tip_id - _resolve_last_session -> tip_id Addresses #10373.
This commit is contained in:
parent
0cff992f0a
commit
22efc81cd7
3 changed files with 304 additions and 5 deletions
114
hermes_state.py
114
hermes_state.py
|
|
@ -723,6 +723,42 @@ class SessionDB:
|
|||
|
||||
return f"{base} #{max_num + 1}"
|
||||
|
||||
def get_compression_tip(self, session_id: str) -> Optional[str]:
|
||||
"""Walk the compression-continuation chain forward and return the tip.
|
||||
|
||||
A compression continuation is a child session where:
|
||||
1. The parent's ``end_reason = 'compression'``
|
||||
2. The child was created AFTER the parent was ended (started_at >= ended_at)
|
||||
|
||||
The second condition distinguishes compression continuations from
|
||||
delegate subagents or branch children, which can also have a
|
||||
``parent_session_id`` but were created while the parent was still live.
|
||||
|
||||
Returns the session_id of the latest continuation in the chain, or the
|
||||
input ``session_id`` if it isn't part of a compression chain (or if the
|
||||
input itself doesn't exist).
|
||||
"""
|
||||
current = session_id
|
||||
# Bound the walk defensively — compression chains this deep are
|
||||
# pathological and shouldn't happen in practice. 100 = plenty.
|
||||
for _ in range(100):
|
||||
with self._lock:
|
||||
cursor = self._conn.execute(
|
||||
"SELECT id FROM sessions "
|
||||
"WHERE parent_session_id = ? "
|
||||
" AND started_at >= ("
|
||||
" SELECT ended_at FROM sessions "
|
||||
" WHERE id = ? AND end_reason = 'compression'"
|
||||
" ) "
|
||||
"ORDER BY started_at DESC LIMIT 1",
|
||||
(current, current),
|
||||
)
|
||||
row = cursor.fetchone()
|
||||
if row is None:
|
||||
return current
|
||||
current = row["id"]
|
||||
return current
|
||||
|
||||
def list_sessions_rich(
|
||||
self,
|
||||
source: str = None,
|
||||
|
|
@ -730,6 +766,7 @@ class SessionDB:
|
|||
limit: int = 20,
|
||||
offset: int = 0,
|
||||
include_children: bool = False,
|
||||
project_compression_tips: bool = True,
|
||||
) -> List[Dict[str, Any]]:
|
||||
"""List sessions with preview (first user message) and last active timestamp.
|
||||
|
||||
|
|
@ -741,6 +778,14 @@ class SessionDB:
|
|||
|
||||
By default, child sessions (subagent runs, compression continuations)
|
||||
are excluded. Pass ``include_children=True`` to include them.
|
||||
|
||||
With ``project_compression_tips=True`` (default), sessions that are
|
||||
roots of compression chains are projected forward to their latest
|
||||
continuation — one logical conversation = one list entry, showing the
|
||||
live continuation's id/message_count/title/last_active. This prevents
|
||||
compressed continuations from being invisible to users while keeping
|
||||
delegate subagents and branches hidden. Pass ``False`` to return the
|
||||
raw root rows (useful for admin/debug UIs).
|
||||
"""
|
||||
where_clauses = []
|
||||
params = []
|
||||
|
|
@ -791,8 +836,77 @@ class SessionDB:
|
|||
s["preview"] = ""
|
||||
sessions.append(s)
|
||||
|
||||
# Project compression roots forward to their tips. Each row whose
|
||||
# end_reason is 'compression' has a continuation child; replace the
|
||||
# surfaced fields (id, message_count, title, last_active, ended_at,
|
||||
# end_reason, preview) with the tip's values so the list entry acts
|
||||
# as the live conversation. Keep the root's started_at to preserve
|
||||
# chronological ordering by original conversation start.
|
||||
if project_compression_tips and not include_children:
|
||||
projected = []
|
||||
for s in sessions:
|
||||
if s.get("end_reason") != "compression":
|
||||
projected.append(s)
|
||||
continue
|
||||
tip_id = self.get_compression_tip(s["id"])
|
||||
if tip_id == s["id"]:
|
||||
projected.append(s)
|
||||
continue
|
||||
tip_row = self._get_session_rich_row(tip_id)
|
||||
if not tip_row:
|
||||
projected.append(s)
|
||||
continue
|
||||
# Preserve the root's started_at for stable sort order, but
|
||||
# surface the tip's identity and activity data.
|
||||
merged = dict(s)
|
||||
for key in (
|
||||
"id", "ended_at", "end_reason", "message_count",
|
||||
"tool_call_count", "title", "last_active", "preview",
|
||||
"model", "system_prompt",
|
||||
):
|
||||
if key in tip_row:
|
||||
merged[key] = tip_row[key]
|
||||
merged["_lineage_root_id"] = s["id"]
|
||||
projected.append(merged)
|
||||
sessions = projected
|
||||
|
||||
return sessions
|
||||
|
||||
def _get_session_rich_row(self, session_id: str) -> Optional[Dict[str, Any]]:
|
||||
"""Fetch a single session with the same enriched columns as
|
||||
``list_sessions_rich`` (preview + last_active). Returns None if the
|
||||
session doesn't exist.
|
||||
"""
|
||||
query = """
|
||||
SELECT s.*,
|
||||
COALESCE(
|
||||
(SELECT SUBSTR(REPLACE(REPLACE(m.content, X'0A', ' '), X'0D', ' '), 1, 63)
|
||||
FROM messages m
|
||||
WHERE m.session_id = s.id AND m.role = 'user' AND m.content IS NOT NULL
|
||||
ORDER BY m.timestamp, m.id LIMIT 1),
|
||||
''
|
||||
) AS _preview_raw,
|
||||
COALESCE(
|
||||
(SELECT MAX(m2.timestamp) FROM messages m2 WHERE m2.session_id = s.id),
|
||||
s.started_at
|
||||
) AS last_active
|
||||
FROM sessions s
|
||||
WHERE s.id = ?
|
||||
"""
|
||||
with self._lock:
|
||||
cursor = self._conn.execute(query, (session_id,))
|
||||
row = cursor.fetchone()
|
||||
if not row:
|
||||
return None
|
||||
s = dict(row)
|
||||
raw = s.pop("_preview_raw", "").strip()
|
||||
if raw:
|
||||
text = raw[:60]
|
||||
s["preview"] = text + ("..." if len(raw) > 60 else "")
|
||||
else:
|
||||
s["preview"] = ""
|
||||
return s
|
||||
|
||||
# =========================================================================
|
||||
# Message storage
|
||||
# =========================================================================
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue