Conversation
- Add validation guard: empty session_id now exits 1 with 'session ID cannot be empty' instead of silently matching the first session (Gap 1). - Add test for OSError path in _show_session_by_index (Gap 2). - Add test for duplicate session_id in _build_session_index (Gap 3). Closes #808 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes an edge case in the session CLI command where an empty session_id prefix would previously match the first session, and adds regression tests covering this and two related edge cases.
Changes:
- Add an early guard in
sessionto reject an emptysession_idwith a clear error and exit code1. - Add a test ensuring
_show_session_by_indexgracefully handles a genericOSErrorfromget_cached_events. - Add a test documenting
_build_session_index’s tie-break behavior for duplicatesession_idvalues (last occurrence wins).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/copilot_usage/cli.py |
Adds an early validation guard to prevent empty-prefix session matching. |
tests/copilot_usage/test_cli.py |
Adds regression tests for empty session id handling, generic OSError, and duplicate session_id indexing behavior. |
Contributor
There was a problem hiding this comment.
Quality Gate: APPROVED ✅
Changes reviewed:
src/copilot_usage/cli.py: 4-line early guard rejecting emptysession_idwithclick.echoto stderr +sys.exit(1)— clean input validation at the CLI boundary.tests/copilot_usage/test_cli.py: 3 new tests covering empty ID rejection, genericOSErrorhandling in_show_session_by_index, and duplicate-ID tiebreak in_build_session_index— all meaningful with clear docstrings and assertions.
Assessment:
- Code follows repository conventions (type annotations, early returns, pytest patterns).
- All 8 CI checks green (lint, typecheck, security, tests, CodeQL).
- Impact: LOW — small edge-case fix with comprehensive test additions.
Auto-approving for merge.
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.
Closes #808
Changes
Gap 1 — Empty-string prefix validation:
Added an early guard in the
sessioncommand that rejects an emptysession_idargument with exit code 1 and the message "session ID cannot be empty". Previously,str.startswith("")always returnsTrue, so an empty argument would silently display the first session.Gap 2 — OSError test for
_show_session_by_index:Added
test_show_session_by_index_generic_oserrorwhich patchesget_cached_eventsto raise a genericOSError("permission denied")and verifies the red error message is printed without raising.Gap 3 — Duplicate
session_idtest for_build_session_index:Added
test_build_session_index_duplicate_idswhich verifies that when twoSessionSummaryobjects share the samesession_id, the dict comprehension maps to the index of the last occurrence — documenting the tiebreak rule.Verification
ruff checkandruff formatcleanpyrightpasses (0 errors)banditsecurity scan clean