From d7e4d94e2fa45d921ad70727bf932affe3f10275 Mon Sep 17 00:00:00 2001 From: sebastianlutycz Date: Wed, 8 Jul 2026 17:31:34 +0530 Subject: [PATCH] perf(dashboard): recursive-CTE descendant lookup in _session_latest_descendant _session_latest_descendant fetched EVERY sessions row and built the parent->children tree in Python on each call. Replace with a recursive CTE that loads only the target session's descendant branch. Hand-applied from PR #39140 (the schema-init cache and Rust PTY bridge parts of that PR are intentionally NOT salvaged here); main's function signature gained a db parameter since the PR was cut. (cherry picked from commit 8ed5e54f65c706d197d0ea554232d5071d0279df) --- hermes_cli/web_server.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.py index b023c18dd59..2c324e547ee 100644 --- a/hermes_cli/web_server.py +++ b/hermes_cli/web_server.py @@ -9303,7 +9303,17 @@ def _session_latest_descendant(session_id: str, db): rows = [] if conn is not None: raw_rows = conn.execute( - "SELECT id, parent_session_id, started_at FROM sessions" + """ + WITH RECURSIVE descendants(id, parent_session_id, started_at) AS ( + SELECT id, parent_session_id, started_at FROM sessions WHERE id = ? + UNION ALL + SELECT s.id, s.parent_session_id, s.started_at + FROM sessions s + JOIN descendants d ON s.parent_session_id = d.id + ) + SELECT id, parent_session_id, started_at FROM descendants + """, + (sid,), ).fetchall() for row in raw_rows: rows.append({