From 19448c326733bc94ec8a49dbdc6f1e9a1e142f58 Mon Sep 17 00:00:00 2001 From: Rod Boev Date: Fri, 12 Jun 2026 09:36:10 -0400 Subject: [PATCH] fix(sidebar): keep rendered source counts on one path --- static/sessions.js | 65 ++++++++++----------- tests/test_issue3966_session_count_label.py | 30 ++++++---- tests/test_sidebar_session_partition.py | 36 ++++++++---- 3 files changed, 74 insertions(+), 57 deletions(-) diff --git a/static/sessions.js b/static/sessions.js index 56c8d410..47739296 100644 --- a/static/sessions.js +++ b/static/sessions.js @@ -4730,39 +4730,45 @@ function _sidebarRowHasVisibleMessages(s, activeSidForSidebar){ function _partitionSidebarSessionRows(allMatched, activeSidForSidebar){ let webuiSessionCount=0; let cliSessionCount=0; - for(const s of allMatched){ - if(!_sidebarRowHasVisibleMessages(s, activeSidForSidebar)) continue; - if(_isCliSession(s)) cliSessionCount++; - else webuiSessionCount++; - } - if(_sessionSourceFilter==='cli' && !window._showCliSessions && cliSessionCount===0){ - _sessionSourceFilter='webui'; - } - const showCliOnly=_sessionSourceFilter==='cli'; - const profileFiltered=[]; - const sessionsRaw=[]; - let archivedCount=0; + const webuiProfileFiltered=[]; + const cliProfileFiltered=[]; + const webuiSessionsRaw=[]; + const cliSessionsRaw=[]; + let webuiArchivedCount=0; + let cliArchivedCount=0; for(const s of allMatched){ if(!_sidebarRowHasVisibleMessages(s, activeSidForSidebar)) continue; const isCli=_isCliSession(s); - if(showCliOnly ? !isCli : isCli) continue; + if(isCli) cliSessionCount++; + else webuiSessionCount++; if(s.default_hidden&&!(_activeProject&&_activeProject!==NO_PROJECT_FILTER&&s.project_id===_activeProject)) continue; + const profileFiltered=isCli ? cliProfileFiltered : webuiProfileFiltered; + const sessionsRaw=isCli ? cliSessionsRaw : webuiSessionsRaw; profileFiltered.push(s); if(_activeProject===NO_PROJECT_FILTER){ if(s.project_id) continue; } else if(_activeProject){ if(s.project_id!==_activeProject) continue; } - if(s.archived) archivedCount++; + if(s.archived){ + if(isCli) cliArchivedCount++; + else webuiArchivedCount++; + } if(!_showArchived&&s.archived) continue; sessionsRaw.push(s); } + if(_sessionSourceFilter==='cli' && !window._showCliSessions && cliSessionCount===0){ + _sessionSourceFilter='webui'; + } + const showCliOnly=_sessionSourceFilter==='cli'; return { webuiSessionCount, cliSessionCount, - profileFiltered, - sessionsRaw, - archivedCount, + profileFiltered: showCliOnly ? cliProfileFiltered : webuiProfileFiltered, + sessionsRaw: showCliOnly ? cliSessionsRaw : webuiSessionsRaw, + archivedCount: showCliOnly ? cliArchivedCount : webuiArchivedCount, + webuiSessionsRaw, + cliSessionsRaw, }; } @@ -4770,22 +4776,9 @@ function _renderSidebarRowsFromRawSessions(sessionsRaw){ return _attachChildSessionsToSidebarRows(_collapseSessionLineageForSidebar(sessionsRaw), sessionsRaw); } -function _countRenderedSidebarRows(allMatched, activeSidForSidebar, showCliOnly){ - const sessionsRaw=[]; - for(const s of allMatched){ - if(!_sidebarRowHasVisibleMessages(s, activeSidForSidebar)) continue; - const isCli=_isCliSession(s); - if(showCliOnly ? !isCli : isCli) continue; - if(s.default_hidden&&!(_activeProject&&_activeProject!==NO_PROJECT_FILTER&&s.project_id===_activeProject)) continue; - if(_activeProject===NO_PROJECT_FILTER){ - if(s.project_id) continue; - } else if(_activeProject){ - if(s.project_id!==_activeProject) continue; - } - if(!_showArchived&&s.archived) continue; - sessionsRaw.push(s); - } - return _renderSidebarRowsFromRawSessions(sessionsRaw).length; +function _countRenderedSidebarRowsFromRawSessions(sessionsRaw){ + // Child attachment does not change the top-level row count shown in the chip label. + return _collapseSessionLineageForSidebar(sessionsRaw).length; } function renderSessionListFromCache(){ @@ -4816,14 +4809,16 @@ function renderSessionListFromCache(){ profileFiltered, sessionsRaw, archivedCount, + webuiSessionsRaw, + cliSessionsRaw, }=_partitionSidebarSessionRows(allMatched, activeSidForSidebar); const sessions=_renderSidebarRowsFromRawSessions(sessionsRaw); const renderedWebuiSessionCount=_sessionSourceFilter==='webui' ? sessions.length - : _countRenderedSidebarRows(allMatched, activeSidForSidebar, false); + : _countRenderedSidebarRowsFromRawSessions(webuiSessionsRaw); const renderedCliSessionCount=_sessionSourceFilter==='cli' ? sessions.length - : _countRenderedSidebarRows(allMatched, activeSidForSidebar, true); + : _countRenderedSidebarRowsFromRawSessions(cliSessionsRaw); _syncSidebarExpansionForActiveSession(sessions, activeSidForSidebar); const list=$('sessionList'); const animateRefresh=_sessionListRefreshAnimationPending; diff --git a/tests/test_issue3966_session_count_label.py b/tests/test_issue3966_session_count_label.py index e561d0ed..4d99f1e8 100644 --- a/tests/test_issue3966_session_count_label.py +++ b/tests/test_issue3966_session_count_label.py @@ -8,25 +8,35 @@ ROOT = Path(__file__).resolve().parents[1] SESSIONS_JS = (ROOT / "static" / "sessions.js").read_text(encoding="utf-8") +def _function_block(name: str) -> str: + start = SESSIONS_JS.index(f"function {name}(") + brace = SESSIONS_JS.index("{", start) + depth = 0 + for idx in range(brace, len(SESSIONS_JS)): + char = SESSIONS_JS[idx] + if char == "{": + depth += 1 + elif char == "}": + depth -= 1 + if depth == 0: + return SESSIONS_JS[start : idx + 1] + raise AssertionError(f"unbalanced braces in {name}") + + def test_render_counts_use_post_collapse_rows(): - render_start = SESSIONS_JS.index("function renderSessionListFromCache()") - render_end = SESSIONS_JS.index("function _showProjectPicker", render_start) - render_body = SESSIONS_JS[render_start:render_end] + render_body = _function_block("renderSessionListFromCache") assert "const sessions=_renderSidebarRowsFromRawSessions(sessionsRaw);" in render_body assert "? sessions.length" in render_body - assert ": _countRenderedSidebarRows(allMatched, activeSidForSidebar, false);" in render_body - assert ": _countRenderedSidebarRows(allMatched, activeSidForSidebar, true);" in render_body + assert ": _countRenderedSidebarRowsFromRawSessions(webuiSessionsRaw);" in render_body + assert ": _countRenderedSidebarRowsFromRawSessions(cliSessionsRaw);" in render_body assert "const count=filter==='cli'?renderedCliSessionCount:renderedWebuiSessionCount;" in render_body assert "const count=filter==='cli'?cliSessionCount:webuiSessionCount;" not in render_body def test_rendered_count_helper_collapses_before_counting(): - helper_start = SESSIONS_JS.index("function _countRenderedSidebarRows(") - helper_end = SESSIONS_JS.index("function renderSessionListFromCache()", helper_start) - helper_body = SESSIONS_JS[helper_start:helper_end] + helper_body = _function_block("_countRenderedSidebarRowsFromRawSessions") - assert "sessionsRaw.push(s);" in helper_body - assert "return _renderSidebarRowsFromRawSessions(sessionsRaw).length;" in helper_body + assert "_collapseSessionLineageForSidebar(sessionsRaw).length;" in helper_body assert "function _renderSidebarRowsFromRawSessions(sessionsRaw){" in SESSIONS_JS assert "_attachChildSessionsToSidebarRows(_collapseSessionLineageForSidebar(sessionsRaw), sessionsRaw)" in SESSIONS_JS diff --git a/tests/test_sidebar_session_partition.py b/tests/test_sidebar_session_partition.py index aa54b61f..8d5bdda7 100644 --- a/tests/test_sidebar_session_partition.py +++ b/tests/test_sidebar_session_partition.py @@ -8,16 +8,27 @@ ROOT = Path(__file__).resolve().parents[1] SESSIONS_JS = (ROOT / "static" / "sessions.js").read_text(encoding="utf-8") +def _function_block(name: str) -> str: + start = SESSIONS_JS.index(f"function {name}(") + brace = SESSIONS_JS.index("{", start) + depth = 0 + for idx in range(brace, len(SESSIONS_JS)): + char = SESSIONS_JS[idx] + if char == "{": + depth += 1 + elif char == "}": + depth -= 1 + if depth == 0: + return SESSIONS_JS[start : idx + 1] + raise AssertionError(f"unbalanced braces in {name}") + + def _partition_block() -> str: - start = SESSIONS_JS.index("function _partitionSidebarSessionRows(") - end = SESSIONS_JS.index("function renderSessionListFromCache()", start) - return SESSIONS_JS[start:end] + return _function_block("_partitionSidebarSessionRows") def test_render_uses_single_pass_partition_helper(): - render_start = SESSIONS_JS.index("function renderSessionListFromCache()") - render_end = SESSIONS_JS.index("function _showProjectPicker", render_start) - render_body = SESSIONS_JS[render_start:render_end] + render_body = _function_block("renderSessionListFromCache") assert "_partitionSidebarSessionRows(allMatched, activeSidForSidebar)" in render_body assert "_renderSidebarRowsFromRawSessions(sessionsRaw)" in render_body @@ -32,18 +43,19 @@ def test_partition_helper_applies_message_source_project_and_archive_gates(): assert "if(_sessionSourceFilter==='cli' && !window._showCliSessions && cliSessionCount===0)" in block assert "const showCliOnly=_sessionSourceFilter==='cli';" in block assert "if(!_showArchived&&s.archived) continue;" in block - assert "if(s.archived) archivedCount++;" in block + assert "if(s.archived){" in block + assert "archivedCount: showCliOnly ? cliArchivedCount : webuiArchivedCount," in block assert "return {" in block - assert "profileFiltered," in block - assert "sessionsRaw," in block + assert "profileFiltered: showCliOnly ? cliProfileFiltered : webuiProfileFiltered," in block + assert "sessionsRaw: showCliOnly ? cliSessionsRaw : webuiSessionsRaw," in block def test_partition_helper_keeps_raw_source_counts_while_render_owns_visible_counts(): - render_start = SESSIONS_JS.index("function renderSessionListFromCache()") - render_end = SESSIONS_JS.index("function _showProjectPicker", render_start) - render_body = SESSIONS_JS[render_start:render_end] + render_body = _function_block("renderSessionListFromCache") assert "webuiSessionCount," in _partition_block() assert "cliSessionCount," in _partition_block() + assert "webuiSessionsRaw," in _partition_block() + assert "cliSessionsRaw," in _partition_block() assert "const renderedWebuiSessionCount=" in render_body assert "const renderedCliSessionCount=" in render_body