Skip to content

fix: add fast pre-filter to session command to avoid O(n) full parse (#138)#171

Merged
microsasa merged 2 commits intomainfrom
fix/138-session-prefilter-007def80bda329a3
Mar 20, 2026
Merged

fix: add fast pre-filter to session command to avoid O(n) full parse (#138)#171
microsasa merged 2 commits intomainfrom
fix/138-session-prefilter-007def80bda329a3

Conversation

@microsasa
Copy link
Owner

Closes #138

Problem

The session command iterates through all session paths, fully parsing every events.jsonl and building a SessionSummary just to check if the session ID starts with the given prefix. With many sessions, this is O(n) parse work instead of O(1).

Solution

Added a directory-name pre-filter in the session command loop:

  • For UUID-shaped directory names (36 chars, 4 dashes), skip parsing when the directory name doesn't start with the requested prefix (and prefix ≥ 4 chars)
  • Non-UUID directories (e.g. corrupt-session, empty-session) are always parsed — their names don't correspond to session IDs
  • When no match is found, a second pass collects available session IDs for the error message

Tests

Three new tests:

  • test_session_prefilter_skips_non_matching_dirs — mocks parse_events and verifies it's only called once (for the matching directory) with ≥ 5 UUID-named sessions and prefix ≥ 4 chars
  • test_session_prefilter_short_prefix_parses_all — verifies short prefixes (< 4 chars) bypass the pre-filter
  • test_session_prefilter_non_uuid_dirs_always_parsed — verifies non-UUID directory names are always parsed

All 410 tests pass. Coverage: 98%.

Generated by Issue Implementer ·

Warning

⚠️ Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • astral.sh

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "astral.sh"

See Network Configuration for more information.

@microsasa microsasa added the aw Created by agentic workflow label Mar 20, 2026
Copilot AI review requested due to automatic review settings March 20, 2026 06:08
@microsasa microsasa enabled auto-merge March 20, 2026 06:08
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes the copilot-usage session <prefix> command by adding a directory-name pre-filter so it can skip parsing most events.jsonl files when searching for a UUID session ID prefix, addressing #138’s O(n) full-parse behavior.

Changes:

  • Add a fast pre-filter in session to skip parsing UUID-shaped session directories that cannot match the requested prefix (for prefixes ≥ 4 chars).
  • Add a “no match” fallback pass to collect available session IDs for the error message.
  • Add unit tests that validate the pre-filter behavior using monkeypatched discover_sessions/parse_events.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/copilot_usage/cli.py Adds the UUID-directory pre-filter and a second-pass collection of available IDs for error output.
tests/copilot_usage/test_cli.py Adds tests validating the pre-filter skip logic, short-prefix behavior, and non-UUID directory behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@microsasa microsasa added aw-quality-gate-approved Quality gate approved the PR aw-review-response-attempted Responder has attempted to address review comments labels Mar 20, 2026
@github-actions
Copy link
Contributor

Commit pushed: 3ab4bee

Generated by Review Responder

github-actions bot and others added 2 commits March 20, 2026 07:06
…138)

Add a directory-name pre-filter to the session command that skips parsing
events.jsonl files for UUID-named directories that clearly cannot match
the requested session ID prefix (when prefix >= 4 chars).

Non-UUID directories (e.g. test fixtures like 'corrupt-session') are
always parsed to preserve correctness. When no match is found, a second
pass collects available session IDs for the error message.

Tests added:
- Verify parse_events is only called for the matching directory with
  >= 5 UUID-named sessions and prefix >= 4 chars
- Verify short prefixes (< 4 chars) bypass the pre-filter
- Verify non-UUID directory names are always parsed

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Collect available session IDs during the first loop to avoid a
  second full parse_events/build_session_summary pass on the error path.
  For UUID-shaped dirs skipped by the pre-filter, derive available from
  dir_name[:8] without parsing.
- Remove _write_uuid_session() and extend _write_session() with
  use_full_uuid_dir parameter to reduce duplicated fixture setup.
- Fix flaky short-prefix test by using deterministic reverse-name
  ordering and asserting that the non-matching cd333… dir was parsed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 20, 2026 07:06
@microsasa microsasa force-pushed the fix/138-session-prefilter-007def80bda329a3 branch from 3ab4bee to d66ab37 Compare March 20, 2026 07:06
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@microsasa microsasa added aw-quality-gate-approved Quality gate approved the PR and removed aw-quality-gate-approved Quality gate approved the PR labels Mar 20, 2026
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quality Gate: APPROVED

Code Quality: Good

  • Clean pre-filter optimization in the session command loop — skips expensive parse_events calls for UUID-shaped directories that clearly don't match the prefix
  • Sensible guardrails: short prefixes (< 4 chars) bypass the filter, non-UUID dirs always parsed
  • available list built during the main loop avoids a second full-parse pass on the error path
  • Three meaningful tests cover the key scenarios (matching, short prefix, non-UUID dirs)

Impact: LOW

  • Performance optimization only — no behavioral, API, or dependency changes
  • 2 files changed (14 lines in cli.py, 146 lines of tests)
  • Existing + new test coverage is solid

Auto-approving for merge.

@microsasa microsasa merged commit 7b6edb7 into main Mar 20, 2026
8 checks passed
@microsasa microsasa deleted the fix/138-session-prefilter-007def80bda329a3 branch March 20, 2026 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

aw Created by agentic workflow aw-quality-gate-approved Quality gate approved the PR aw-review-response-attempted Responder has attempted to address review comments

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[aw][code health] session command does O(n) full parse to find one session by ID prefix

2 participants