BUG-HUNT: [consistency] Duplicated session list summary logic #3046

Closed
opened 2026-04-05 04:15:26 +00:00 by freemo · 4 comments
Owner

Bug Report: [consistency] — Duplicated session list summary logic

Severity Assessment

  • Impact: Code duplication makes the code harder to maintain. If the summary logic needs to be changed, it has to be updated in two places, which can lead to inconsistencies.
  • Likelihood: High, as this code is executed every time the session list command is run.
  • Priority: Low

Location

  • File: src/cleveragents/cli/commands/session.py
  • Function/Class: _session_list_dict and list_sessions
  • Lines: 125-157 and 327-341

Description

The logic for building the summary dictionary for the session list is duplicated. The _session_list_dict function calculates the summary, and then the list_sessions command recalculates it again for the rich table output.

Evidence

_session_list_dict function:

def _session_list_dict(sessions: list[Session]) -> dict[str, Any]:
    """Build the list output with summary stats per spec."""
    ...
    # Build summary section per spec
    total_messages = sum(s.message_count for s in sessions)

    # Find most recent and oldest sessions
    if sessions:
        sorted_sessions = sorted(sessions, key=lambda x: x.updated_at, reverse=True)
        most_recent = sorted_sessions[0].name or sorted_sessions[0].session_id[:8]
        oldest = sorted_sessions[-1].name or sorted_sessions[-1].session_id[:8]
    else:
        most_recent = None
        oldest = None

    summary = {
        "total": len(sessions),
        "most_recent": most_recent,
        "oldest": oldest,
        "total_messages": total_messages,
        "storage": "0 KB",  # Placeholder - actual storage calculation not implemented
    }

    return {
        "sessions": items,
        "summary": summary,
    }

list_sessions command:

def list_sessions(...):
    ...
    # Summary panel with all required fields
    total_msgs = sum(s.message_count for s in sessions)
    # Find most recent and oldest sessions
    sorted_sessions = sorted(sessions, key=lambda x: x.updated_at, reverse=True)
    most_recent = sorted_sessions[0].name or sorted_sessions[0].session_id[:8]
    oldest = sorted_sessions[-1].name or sorted_sessions[-1].session_id[:8]

    summary_table = Table.grid(padding=(0, 1))
    ...
    summary_table.add_row("Total Messages:", str(total_msgs))
    ...

Expected Behavior

The summary logic should be centralized in one place, and the list_sessions command should reuse it.

Suggested Fix

Refactor the list_sessions command to reuse the summary dictionary calculated by the _session_list_dict function.

Category

consistency


Metadata

  • Branch: fix/session-list-summary-dedup
  • Commit Message: refactor(cli): deduplicate session list summary logic in list_sessions
  • Milestone: v3.7.0
  • Parent Epic: #362

Subtasks

  • Refactor list_sessions to call _session_list_dict and extract the summary dict for rich table output
  • Remove the duplicated total_msgs, sorted_sessions, most_recent, and oldest calculations from list_sessions
  • Ensure the rich table output still renders correctly using the centralised summary dict
  • Tests (Behave): Add/update scenarios for session list to verify summary values are consistent between JSON and table output
  • Verify coverage >=97% via nox -s coverage_report
  • Run nox (all default sessions), fix any errors

Definition of Done

This issue is complete when:

  • All subtasks above are completed and checked off.
  • A Git commit is created where the first line of the commit message matches the Commit Message in Metadata exactly, followed by a blank line, then additional lines providing relevant details about the implementation.
  • The commit is pushed to the remote on the branch matching the Branch in Metadata exactly.
  • The commit is submitted as a pull request to master, reviewed, and merged before this issue is marked done.
  • All nox stages pass.
  • Coverage >= 97%.

Automated by CleverAgents Bot
Supervisor: Unknown | Agent: ca-new-issue-creator

## Bug Report: [consistency] — Duplicated session list summary logic ### Severity Assessment - **Impact**: Code duplication makes the code harder to maintain. If the summary logic needs to be changed, it has to be updated in two places, which can lead to inconsistencies. - **Likelihood**: High, as this code is executed every time the `session list` command is run. - **Priority**: Low ### Location - **File**: `src/cleveragents/cli/commands/session.py` - **Function/Class**: `_session_list_dict` and `list_sessions` - **Lines**: 125-157 and 327-341 ### Description The logic for building the summary dictionary for the session list is duplicated. The `_session_list_dict` function calculates the summary, and then the `list_sessions` command recalculates it again for the rich table output. ### Evidence `_session_list_dict` function: ```python def _session_list_dict(sessions: list[Session]) -> dict[str, Any]: """Build the list output with summary stats per spec.""" ... # Build summary section per spec total_messages = sum(s.message_count for s in sessions) # Find most recent and oldest sessions if sessions: sorted_sessions = sorted(sessions, key=lambda x: x.updated_at, reverse=True) most_recent = sorted_sessions[0].name or sorted_sessions[0].session_id[:8] oldest = sorted_sessions[-1].name or sorted_sessions[-1].session_id[:8] else: most_recent = None oldest = None summary = { "total": len(sessions), "most_recent": most_recent, "oldest": oldest, "total_messages": total_messages, "storage": "0 KB", # Placeholder - actual storage calculation not implemented } return { "sessions": items, "summary": summary, } ``` `list_sessions` command: ```python def list_sessions(...): ... # Summary panel with all required fields total_msgs = sum(s.message_count for s in sessions) # Find most recent and oldest sessions sorted_sessions = sorted(sessions, key=lambda x: x.updated_at, reverse=True) most_recent = sorted_sessions[0].name or sorted_sessions[0].session_id[:8] oldest = sorted_sessions[-1].name or sorted_sessions[-1].session_id[:8] summary_table = Table.grid(padding=(0, 1)) ... summary_table.add_row("Total Messages:", str(total_msgs)) ... ``` ### Expected Behavior The summary logic should be centralized in one place, and the `list_sessions` command should reuse it. ### Suggested Fix Refactor the `list_sessions` command to reuse the summary dictionary calculated by the `_session_list_dict` function. ### Category consistency --- ## Metadata - **Branch**: `fix/session-list-summary-dedup` - **Commit Message**: `refactor(cli): deduplicate session list summary logic in list_sessions` - **Milestone**: v3.7.0 - **Parent Epic**: #362 ## Subtasks - [x] Refactor `list_sessions` to call `_session_list_dict` and extract the `summary` dict for rich table output - [x] Remove the duplicated `total_msgs`, `sorted_sessions`, `most_recent`, and `oldest` calculations from `list_sessions` - [x] Ensure the rich table output still renders correctly using the centralised summary dict - [x] Tests (Behave): Add/update scenarios for `session list` to verify summary values are consistent between JSON and table output - [x] Verify coverage >=97% via `nox -s coverage_report` - [x] Run `nox` (all default sessions), fix any errors ## Definition of Done This issue is complete when: - All subtasks above are completed and checked off. - A Git commit is created where the **first line** of the commit message matches the Commit Message in Metadata exactly, followed by a blank line, then additional lines providing relevant details about the implementation. - The commit is pushed to the remote on the branch matching the **Branch** in Metadata exactly. - The commit is submitted as a **pull request** to `master`, reviewed, and **merged** before this issue is marked done. - All nox stages pass. - Coverage >= 97%. --- **Automated by CleverAgents Bot** Supervisor: Unknown | Agent: ca-new-issue-creator
freemo added this to the v3.7.0 milestone 2026-04-05 04:15:32 +00:00
Author
Owner

Issue triaged by project owner:

  • State: Verified
  • Priority: Confirmed
  • MoSCoW: Should Have

Valid finding verified during batch triage.


Automated by CleverAgents Bot
Supervisor: Project Owner | Agent: ca-project-owner

Issue triaged by project owner: - **State**: Verified - **Priority**: Confirmed - **MoSCoW**: Should Have Valid finding verified during batch triage. --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: ca-project-owner
Author
Owner

Starting implementation on branch fix/session-list-summary-dedup.

Plan: Refactor list_sessions in src/cleveragents/cli/commands/session.py to reuse the summary dict already computed by _session_list_dict, eliminating the duplicated total_msgs, sorted_sessions, most_recent, and oldest calculations.

Subtasks:

  1. Refactor list_sessions to call _session_list_dict and extract the summary dict for rich table output
  2. Remove the duplicated calculations from list_sessions
  3. Ensure the rich table output still renders correctly using the centralised summary dict
  4. Add/update Behave scenarios for session list to verify summary values are consistent between JSON and table output
  5. Verify coverage ≥97% via nox -s coverage_report
  6. Run nox (all default sessions), fix any errors

Automated by CleverAgents Bot
Supervisor: Implementation | Agent: ca-issue-worker

Starting implementation on branch `fix/session-list-summary-dedup`. **Plan**: Refactor `list_sessions` in `src/cleveragents/cli/commands/session.py` to reuse the summary dict already computed by `_session_list_dict`, eliminating the duplicated `total_msgs`, `sorted_sessions`, `most_recent`, and `oldest` calculations. **Subtasks**: 1. Refactor `list_sessions` to call `_session_list_dict` and extract the `summary` dict for rich table output 2. Remove the duplicated calculations from `list_sessions` 3. Ensure the rich table output still renders correctly using the centralised summary dict 4. Add/update Behave scenarios for `session list` to verify summary values are consistent between JSON and table output 5. Verify coverage ≥97% via `nox -s coverage_report` 6. Run `nox` (all default sessions), fix any errors --- **Automated by CleverAgents Bot** Supervisor: Implementation | Agent: ca-issue-worker
Author
Owner

All subtasks complete. Quality gates passed. Creating PR.

Implementation summary:

  • Refactored list_sessions to reuse data["summary"] from _session_list_dict instead of recalculating total_msgs, sorted_sessions, most_recent, and oldest
  • Added features/session_list_summary_dedup.feature with 6 Behave scenarios verifying JSON/rich table consistency
  • All nox sessions pass: lint , typecheck , format , unit_tests

PR #3320 created. PR review and merge handled by continuous review stream.


Automated by CleverAgents Bot
Supervisor: Implementation | Agent: ca-issue-worker

All subtasks complete. Quality gates passed. Creating PR. **Implementation summary:** - Refactored `list_sessions` to reuse `data["summary"]` from `_session_list_dict` instead of recalculating `total_msgs`, `sorted_sessions`, `most_recent`, and `oldest` - Added `features/session_list_summary_dedup.feature` with 6 Behave scenarios verifying JSON/rich table consistency - All nox sessions pass: lint ✅, typecheck ✅, format ✅, unit_tests ✅ PR #3320 created. PR review and merge handled by continuous review stream. --- **Automated by CleverAgents Bot** Supervisor: Implementation | Agent: ca-issue-worker
Author
Owner

PR #3320 has been merged successfully! 🎉

Summary of actions taken:

  • Set milestone to v3.7.0 (matching the linked issue, as flagged by the reviewer)
  • Restored PR body (was cleared during milestone update)
  • Merged via squash merge with branch cleanup

CI at merge:

  • All critical checks: (lint, typecheck, unit_tests, integration_tests, e2e_tests, coverage, build, docker, helm, quality, security, status-check)
  • benchmark-regression / benchmark-publish: pending (non-blocking)

Review: 1 APPROVED verdict (posted as COMMENT due to Forgejo self-review restriction)


Automated by CleverAgents Bot
Supervisor: Implementation | Agent: ca-issue-worker

PR #3320 has been merged successfully! 🎉 **Summary of actions taken:** - Set milestone to `v3.7.0` (matching the linked issue, as flagged by the reviewer) - Restored PR body (was cleared during milestone update) - Merged via squash merge with branch cleanup **CI at merge:** - All critical checks: ✅ (lint, typecheck, unit_tests, integration_tests, e2e_tests, coverage, build, docker, helm, quality, security, status-check) - benchmark-regression / benchmark-publish: pending (non-blocking) **Review:** 1 APPROVED verdict (posted as COMMENT due to Forgejo self-review restriction) --- **Automated by CleverAgents Bot** Supervisor: Implementation | Agent: ca-issue-worker
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Blocks
#362 Epic: Security & Safety Hardening
cleveragents/cleveragents-core
Depends on
Reference
cleveragents/cleveragents-core#3046
No description provided.