feat: Claude Code log parser service (Story 20.2)#102
Conversation
Add ClaudeCodeLogParser service that scans Claude Code JSONL session logs and extracts token consumption data for passive monitoring. Implements incremental scanning with persisted file offsets, requestId deduplication, per-model aggregation with binary search, and health degradation detection.
- Fix file size attribute cast: (attrs[.size] as? UInt64) always returned 0 (Foundation bridges .size as Int), breaking incremental scan — every scan re-read files from byte 0 and accumulated duplicate records. - Fix inverted test assertion in healthSuccessRate: 60% success rate IS degraded; the assertion was checking !isDegraded. - Extract ISO8601DateFormatter to static properties instead of allocating per-line call. - Use protocol type (any ClaudeCodeLogParserProtocol)? in AppDelegate instead of concrete ClaudeCodeLogParser?.
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (11)
📝 WalkthroughWalkthroughIntroduces Epic 20 (Token Efficiency Ratio Phase 6) with specification and planning documents, and implements a Claude Code JSONL log parser service that discovers, parses, and aggregates token usage from session logs with incremental scanning, request deduplication, and health metrics. The service is integrated into app initialization. Changes
Sequence Diagram(s)sequenceDiagram
participant AppDelegate
participant LogParser as Claude Code<br/>Log Parser
participant FileSystem as File System<br/>& ScanState
participant Memory as In-Memory<br/>Token Records
AppDelegate->>LogParser: init(dataRetentionDays)
AppDelegate->>LogParser: scan() async
LogParser->>FileSystem: discover JSONL files<br/>under ~/.claude/projects/*
FileSystem-->>LogParser: file list with mod times
loop for each JSONL file
LogParser->>FileSystem: read stored offset<br/>from scan-state.json
FileSystem-->>LogParser: offset or 0
alt file truncated (size < offset)
LogParser->>LogParser: reset offset to 0
end
LogParser->>FileSystem: read from offset onward
FileSystem-->>LogParser: JSON lines
loop for each line
LogParser->>LogParser: parse JSON, extract<br/>assistant messages
LogParser->>LogParser: deduplicate by requestId<br/>(keep highest output_tokens)
LogParser->>Memory: store TokenRecord
end
LogParser->>FileSystem: persist updated offset<br/>to scan-state.json
end
LogParser->>Memory: return aggregated<br/>TokenAggregate per model
Estimated code review effort🎯 4 (Complex) | ⏱️ ~65 minutes Poem
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
Story
20.2: Claude Code Log Parser Service
Test plan
Summary by CodeRabbit
Release Notes