perf(dashboard): use GROUP BY for session stats instead of fetching 10k rows - #73362
Merged
kshitijk4poor merged 1 commit intoJul 28, 2026
Merged
Conversation
…0k rows Replaces the O(N) list_sessions_rich histogram in /api/sessions/stats with a single GROUP BY query, reducing response time from ~575ms to <1ms on large databases. Original PR NousResearch#48921 by @liuhao1024. Salvage fixes based on review feedback from teknium1 and @wernerhp: 1. Preserve try/except guard — a DB error still degrades to empty by_source instead of failing the whole stats response. 2. GROUP BY COALESCE(source, 'cli') — the original GROUP BY source could emit duplicate 'cli' keys (NULL group + literal 'cli' group) that the dict comprehension silently dropped. 3. Add exclude_children=True — list_sessions_rich excludes subagent runs, delegates, and compression continuations by default; the bare GROUP BY counted all rows, inflating source counts. Aggregate shape (exclude_children/include_archived/limit params) adapted from closed duplicate NousResearch#61120 by @mijanx. Closes NousResearch#48914 Co-authored-by: mijanx <mijanx@users.noreply.github.com>
kshitijk4poor
force-pushed
the
salvage/48921-group-by-source
branch
from
July 28, 2026 13:27
5cce803 to
3416e7c
Compare
kshitijk4poor
enabled auto-merge (rebase)
July 28, 2026 13:27
This was referenced Jul 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
/api/sessions/statssource histogram is now a singleGROUP BYquery instead of materializing up to 10,000list_sessions_richrows, cutting response time from ~575ms to <1ms on large databases.Changes
hermes_state.py: Addsession_count_by_source()— singleGROUP BYquery withexclude_children/include_archived/limitparams mirroringlist_sessions_rich/session_countfilter semanticshermes_cli/web_server.py: Replacelist_sessions_rich(limit=10000)loop withdb.session_count_by_source()call (−3 lines, try/except guard preserved)tests/test_hermes_state.py: 4 new tests (basic grouping, empty db, child exclusion, COALESCE grouping)tests/hermes_cli/test_dashboard_admin_endpoints.py: 1 new test verifyinglist_sessions_richis NOT called by stats endpointFixes from review feedback
Original PR #48921 by @liuhao1024 had 3 issues identified by @teknium1 and @wernerhp:
by_source(behavior-neutral), not a 500GROUP BY sourcemismatch withSELECT COALESCE(source, 'cli')— fixed toGROUP BY COALESCE(source, 'cli')to prevent duplicate-key data lossexclude_children=Truemirroringlist_sessions_richvisibility so source counts match what the Sessions page listsAggregate shape adapted from closed duplicate #61120 by @mijanx.
Closes #48914
Closes #48921