Skip to content

feat: Claude Code log parser service (Story 20.2)#102

Merged
rajish merged 3 commits intomasterfrom
feature/story-20-2-claude-code-log-parser
Mar 27, 2026
Merged

feat: Claude Code log parser service (Story 20.2)#102
rajish merged 3 commits intomasterfrom
feature/story-20-2-claude-code-log-parser

Conversation

@rajish
Copy link
Copy Markdown
Owner

@rajish rajish commented Mar 27, 2026

Summary

  • Adds ClaudeCodeLogParser service that reads Claude Code JSONL session logs from ~/.claude/projects/
  • Extracts per-model token consumption data (input, output, cache_create, cache_read) with request deduplication
  • Supports incremental scanning (byte offset tracking), cross-project discovery, and subagent log files
  • Includes parser health indicator with 80% success rate degradation warning
  • 15 unit tests covering all 8 acceptance criteria

Story

20.2: Claude Code Log Parser Service

Test plan

  • All XCTest unit tests pass
  • Code review findings addressed
  • Build succeeds with xcodebuild

Summary by CodeRabbit

Release Notes

  • New Features
    • Added "Measure" button to benchmark model performance with system pre-validation (OAuth status, utilization stability)
    • Implemented token efficiency ratio (TPP) metric tracking and analytics
    • Added automatic discovery and parsing of usage logs for comprehensive token consumption analytics across projects

rajish added 3 commits March 27, 2026 23:36
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?.
@rajish rajish merged commit 2a9416c into master Mar 27, 2026
1 of 3 checks passed
@rajish rajish deleted the feature/story-20-2-claude-code-log-parser branch March 27, 2026 22:57
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 27, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 4665481d-ba64-4d23-a758-2439d90a9358

📥 Commits

Reviewing files that changed from the base of the PR and between 361094e and 7ceca11.

📒 Files selected for processing (11)
  • _bmad-output/implementation-artifacts/20-1-active-benchmark-measurement.md
  • _bmad-output/implementation-artifacts/20-2-claude-code-log-parser-service.md
  • _bmad-output/implementation-artifacts/sprint-status.yaml
  • _bmad-output/planning-artifacts/epics/epic-20-token-efficiency-ratio-phase-6.md
  • cc-hdrm/App/AppDelegate.swift
  • cc-hdrm/Models/LogParserHealth.swift
  • cc-hdrm/Models/TokenAggregate.swift
  • cc-hdrm/Models/TokenRecord.swift
  • cc-hdrm/Services/ClaudeCodeLogParser.swift
  • cc-hdrm/Services/ClaudeCodeLogParserProtocol.swift
  • cc-hdrmTests/Services/ClaudeCodeLogParserTests.swift

📝 Walkthrough

Walkthrough

Introduces 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

Cohort / File(s) Summary
Planning & Specification Documents
_bmad-output/implementation-artifacts/20-1-active-benchmark-measurement.md, _bmad-output/implementation-artifacts/20-2-claude-code-log-parser-service.md, _bmad-output/planning-artifacts/epics/epic-20-token-efficiency-ratio-phase-6.md
New specification files defining Epic 20 stories (active benchmark measurement, log parser service, data models, visualization, historical backfill) with detailed acceptance criteria, task breakdowns, and architectural guidance.
Claude Code Log Parser Service
cc-hdrm/Services/ClaudeCodeLogParserProtocol.swift, cc-hdrm/Services/ClaudeCodeLogParser.swift, cc-hdrmTests/Services/ClaudeCodeLogParserTests.swift
New protocol and implementation for scanning Claude Code JSONL logs, parsing assistant messages, extracting token counts, deduplicating by requestId, maintaining scan state with persisted byte offsets, aggregating tokens per model, and reporting health metrics. Comprehensive test suite covers parsing, deduplication, incremental behavior, offset reset on truncation, aggregation filtering, and degradation thresholds.
Token Data Models
cc-hdrm/Models/TokenRecord.swift, cc-hdrm/Models/TokenAggregate.swift, cc-hdrm/Models/LogParserHealth.swift
New data structures: TokenRecord for deduplicated token events, TokenAggregate for per-model aggregated totals, and LogParserHealth with success-rate computation and degradation warning logic.
AppDelegate Integration
cc-hdrm/App/AppDelegate.swift
Added claudeCodeLogParser property and initialization during app launch with async scan task.
Sprint Status
_bmad-output/implementation-artifacts/sprint-status.yaml
Added Epic 20 (in-progress) with five associated stories: 20.1 ready-for-dev, 20.2 done, and 20.3–20.5 backlog.

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
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~65 minutes

Poem

🐰 Hops with delight through Claude's token trails,
Parsing logs where JSONL never fails!
Dedup, aggregate, and scan with care,
Token wisdom tracked with floppy-eared flair!

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/story-20-2-claude-code-log-parser

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant