Skip to content

perf(vscode_parser): check root_id before _scan_child_ids on cache hit (#897)#901

Merged
microsasa merged 3 commits intomainfrom
fix/897-vscode-discovery-cache-perf-20372d69672d87d7
Apr 10, 2026
Merged

perf(vscode_parser): check root_id before _scan_child_ids on cache hit (#897)#901
microsasa merged 3 commits intomainfrom
fix/897-vscode-discovery-cache-perf-20372d69672d87d7

Conversation

@microsasa
Copy link
Copy Markdown
Owner

Summary

Fixes the O(n_children) steady-state cost in _cached_discover_vscode_logs by checking the cached root_id before calling _scan_child_ids.

On a cache hit (root (st_mtime_ns, st_size) unchanged), the child scan is now skipped entirely — reducing per-call cost from ~201 stat syscalls + 1 scandir to a single stat() for a VS Code installation with 200 session directories.

Changes

src/copilot_usage/vscode_parser.py

  • Reorder _cached_discover_vscode_logs: check root_id against cache before calling _scan_child_ids
  • _scan_child_ids only runs on cache misses (still used as safety net when repopulating)
  • Updated docstrings and comments to reflect the new O(1) steady-state cost

tests/copilot_usage/test_vscode_parser.py

  • Added TestCachedDiscoverSkipsChildScanOnHit with monkeypatch spy verifying _scan_child_ids is never called on a root_id cache hit
  • Updated test_new_window_under_existing_session_uses_cache to reflect the new behavior (child-only changes do not trigger rediscovery when root mtime is unchanged)

Closes #897

Generated by Issue Implementer · ● 20.8M ·

#897)

Reorder _cached_discover_vscode_logs so the cached root_id
(st_mtime_ns, st_size) is checked before calling _scan_child_ids.
On a steady-state cache hit the child scan is now skipped entirely,
reducing per-call cost from O(n_children) stat syscalls to a single
stat on the root directory.

On Linux (ext4/btrfs/overlayfs) and macOS (APFS), creating or
removing a subdirectory always updates the parent directory's mtime,
so root_id is sufficient to detect child-directory changes.
_scan_child_ids still runs on every cache miss as an extra safety net.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@microsasa microsasa added the aw Created by agentic workflow label Apr 10, 2026
Copilot AI review requested due to automatic review settings April 10, 2026 10:16
@microsasa microsasa enabled auto-merge April 10, 2026 10:16
Copy link
Copy Markdown

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 VS Code log discovery by changing _cached_discover_vscode_logs to skip _scan_child_ids on cache hits, aiming to reduce steady-state discovery overhead in copilot_usage.vscode_parser.

Changes:

  • Reordered _cached_discover_vscode_logs to check root_id before scanning child directories, and to skip _scan_child_ids on cache hits.
  • Updated docstrings/comments to describe O(1) steady-state behavior.
  • Adjusted and added tests to assert no re-glob and no child-scan on a root_id cache hit.

Reviewed changes

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

File Description
src/copilot_usage/vscode_parser.py Changes cache-hit criteria to rely on root_id alone and updates documentation accordingly.
tests/copilot_usage/test_vscode_parser.py Updates expectations around rediscovery and adds a test ensuring _scan_child_ids is not invoked on cache hits.

@microsasa microsasa added the aw-review-response-attempted Responder has attempted to address review comments label Apr 10, 2026
Add O(1) sentinel check for newest child directory to detect new window
directories under existing sessions, while keeping the root_id fast path.
The cache now stores the most recently modified child dir at population
time and re-stats it on hits — catching sub-directory changes without
rescanning all children.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor

Commit pushed: c72cad2

Generated by Review Responder

Copy link
Copy Markdown

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 2 comments.

@microsasa microsasa added aw-review-response-1 Review response round 1 aw-review-response-attempted Responder has attempted to address review comments and removed aw-review-response-attempted Responder has attempted to address review comments labels Apr 10, 2026
- Add deterministic tie-breaker (child name) to _newest_child_from_ids
  to stabilize sentinel selection when multiple children share the same
  st_mtime_ns.
- Document the sentinel cache limitation: only changes under the cached
  newest child are detected; modifications to older session directories
  may return stale log_paths until the root changes or cache is cleared.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor

Commit pushed: fea8fac

Generated by Review Responder

Copy link
Copy Markdown

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.

@microsasa microsasa added the aw-quality-gate-approved Quality gate approved the PR label Apr 10, 2026
Copy link
Copy Markdown
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.

Medium-impact performance optimization with good test coverage. Auto-approving for merge.

What was evaluated:

  • vscode_parser.py: Cache hit path refactored from O(n_children) _scan_child_ids scan to O(1) sentinel-based check (2 stat calls: root + newest child). Trade-off (may miss changes in non-newest children) is well-documented and acceptable.
  • _newest_child_from_ids: New helper with correct type annotations, deterministic tie-breaking, proper None handling.
  • _VSCodeDiscoveryCache: Extended with newest_child_path/newest_child_id sentinel fields.
  • Tests: New TestCachedDiscoverSkipsChildScanOnHit class uses monkeypatched spy to verify the optimization. Existing tests updated for new dataclass fields.
  • All 8 CI checks passing. Follows coding guidelines (strict typing, docstrings, safe_file_identity pattern).

@microsasa microsasa merged commit 85c3c5b into main Apr 10, 2026
8 checks passed
@microsasa microsasa deleted the fix/897-vscode-discovery-cache-perf-20372d69672d87d7 branch April 10, 2026 11:24
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-1 Review response round 1 aw-review-response-attempted Responder has attempted to address review comments

Projects

None yet

2 participants