Path: blob/main/extensions/copilot/src/extension/chatSessions/claude/node/sessionParser/README.md
13406 views
Claude Code Session Parser
This module provides type-safe parsing of Claude Code session files stored in JSONL format.
Overview
Claude Code stores conversation sessions in ~/.claude/projects/{workspace-slug}/ as JSONL files. Each line in a session file is a JSON object representing either:
Queue operations: Session queue state changes (dequeue/enqueue)
User messages: User prompts, including tool results
Assistant messages: Claude's responses, including thinking blocks and tool use
Summary entries: Session labels for display
Chain link entries: Minimal entries for parent-chain resolution
Architecture
Key Design Decisions
1. Schema Validation (No Type Assertions)
Uses the composable IValidator<T> pattern from platform/configuration/common/validator.ts:
This ensures:
Runtime validation matches static types
No unsafe
ascasts on parsed JSONDetailed error messages for debugging
2. Lenient UUID Validation
Real session data may have variations (e.g., agent IDs like "a139fcf"). The UUID validator accepts any non-empty string to handle edge cases while still providing type safety.
3. Chain Link Resolution
Some entries exist only for parent-chain resolution (meta messages). The parser:
Stores chain links separately from messages
Resolves parent UUIDs through chain links when building sessions
Handles cycles gracefully (stops at cycle but preserves collected messages)
4. Error Reporting
Parse errors include:
Line number
Truncated line content
Parsed type (if available)
File identifier
Accessible via service.getLastParseErrors() and service.getLastParseStats().
5. Subagent Session Support
Sessions may contain subagents - parallel task executions spawned via the Task tool. These are stored in:
The service automatically:
Discovers subagent directories for each session
Parses
agent-*.jsonlfiles within themAttaches subagent sessions to their parent via
session.subagents
Each SubagentSession contains:
agentId: The short hex identifier (e.g., "a139fcf")messages: The subagent's conversation historytimestamp: When the subagent last had activity
Usage
Via Dependency Injection (Recommended)
Direct Parsing (Tests/Scripts)
Session File Format
Queue Operation
User Message
Assistant Message
Summary Entry
Future Improvements
1. Model Information Extraction
The session files contain message.model with the exact model used (e.g., "claude-opus-4-5-20251101", "claude-haiku-4-5-20251001"). This could be:
Displayed in the UI
Used for model restoration when resuming sessions
Tracked for analytics
2. Token Usage Tracking
Each assistant message includes detailed token usage:
This could power usage analytics and cost estimation.
3. Global History Integration
The ~/.claude/history.jsonl file contains command history with different schema (Unix timestamps instead of ISO). This could be integrated for:
Command autocomplete
Recent session lookup
Cross-project session discovery
4. Thinking Block Display
Assistant messages may contain encrypted thinking blocks with signatures. These could be:
Shown in a collapsed state
Used for debugging/understanding Claude's reasoning
Filtered based on user preferences
5. Git Context Restoration
Sessions track gitBranch and cwd. This metadata could:
Help navigate to the correct workspace state
Show branch context in session labels
Enable branch-aware session filtering
6. Windows Path Support
The slug generation handles Windows paths (C:\Users... → C--Users-...). Further testing needed for:
UNC paths
Mapped network drives
Long paths (>260 chars)
Testing
Run tests:
Migration Notes
This module replaces the previous claudeCodeSessionService.ts implementation with:
Strict schema validation (vs unsafe JSON.parse casts)
Comprehensive error reporting (vs silent failures)
Better cycle detection (vs potential infinite loops)
Cleaner separation of concerns (schema → parser → service)