Principle:777genius Agent teams ai Session Discovery
| Knowledge Sources | |
|---|---|
| Domains | Session_Analysis, File_Discovery |
| Last Updated | 2026-10-22 00:00 GMT |
Overview
Pattern for discovering and cataloging CLI session files from the filesystem with metadata caching, content presence detection, and platform-aware batch scanning.
Description
Session Discovery is the process of scanning the `~/.claude/projects/` directory to find all Claude session JSONL files, extracting metadata (timestamps, message counts, model info), and returning a structured project list. The scanner uses multiple layers of caching to avoid redundant I/O, platform-aware batch sizes to prevent thread pool saturation on Windows, and a scan budget timeout to prevent unbounded scanning.
Key mechanisms:
- Multi-layer cache: Content presence cache, session metadata cache, session preview cache, and a short-lived scan cache (2-second TTL) to deduplicate concurrent scans.
- In-flight deduplication: Both `scan()` and `getRepositoryGroups()` share a `scanInFlight` promise to prevent duplicate full scans.
- Platform-aware batching: Windows uses smaller batch sizes (16 sessions, 4 projects) to avoid UV thread pool saturation; Unix uses larger batches (64, 12).
- Scan budget: A configurable scan budget (default 24 seconds) limits total scan time; a minimum batch budget ensures progress.
- SessionMetadataIndex: An optional index that accelerates session lookup by maintaining a persisted metadata database.
- File IO concurrency: A queue limits concurrent file IO operations (16 on Unix, 8 on Windows).
Usage
Use this principle when designing a system that discovers and catalogs files from a deep directory structure with multiple concurrent consumers, especially when caching, deduplication, and platform-aware performance tuning are needed.
Theoretical Basis
# Abstract algorithm (NOT real implementation)
async def scan():
# Short-lived cache deduplicates concurrent calls
if scan_cache and now() - scan_cache.timestamp < TTL:
return scan_cache.projects
if scan_in_flight:
return await scan_in_flight
promise = do_scan()
scan_in_flight = promise
try:
projects = await promise
scan_cache = { projects, timestamp: now() }
return projects
finally:
scan_in_flight = None
async def do_scan():
entries = list_dir(projects_dir)
batches = chunk(entries, BATCH_SIZE)
results = await gather(*[scan_batch(b) for b in batches])
return merge_sorted_by_recent(results)