vibe_coding_tracker 0.5.0

Vibe Coding Tracker - AI coding assistant telemetry/usage parser, aggregate JSONL events into CodeAnalysis results
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
# Architecture Documentation

This document provides a comprehensive overview of the Vibe Coding Tracker's architecture, design patterns, and system organization.

## Table of Contents

- [System Overview]#system-overview
- [Module Architecture]#module-architecture
- [Data Flow]#data-flow
- [Key Components]#key-components
- [Design Patterns]#design-patterns
- [External Dependencies]#external-dependencies
- [Performance Considerations]#performance-considerations

## System Overview

Vibe Coding Tracker is a Rust-based CLI tool designed to analyze AI coding assistant usage across multiple platforms (Claude Code, Codex, and Gemini). The system employs a modular architecture with clear separation of concerns:

```
                     ┌───────────────────────┐
                     │   Startup Check       │
                     │   - update notif.     │
                     │   - 24h cache         │
                     │   - silent fail       │
                     └───────────┬───────────┘
┌─────────────────────────────────────────────────────────────┐
│                        CLI Layer                             │
│              (clap-based command routing)                    │
└────────┬────────────────────────────────┬──────────┬────────┘
         │                                │          │
         ▼                                ▼          ▼
┌────────────────┐      ┌────────────────────┐  ┌──────────────┐
│ Usage Analysis │      │ Conversation       │  │ Update       │
│ - calculator   │      │ Analysis           │  │ - github.rs  │
│ - display      │      │ - analyzer.rs      │  │ - platform   │
└────────┬───────┘      └────────┬───────────┘  │ - archive    │
         │                       │               └──────────────┘
         │         ┌─────────────┼────────┐
         │         │             │        │
         ▼         ▼             ▼        ▼
┌─────────────────┐    ┌──────────────────────────┐
│ Pricing System  │    │   Format Detection       │
│ - fetch_model_  │    │   - Claude/Codex/Gemini  │
│   pricing()     │    │   - detector.rs          │
│ - fuzzy match   │    │   - claude_analyzer.rs   │
│ - caching       │    │   - codex_analyzer.rs    │
└─────────────────┘    │   - gemini_analyzer.rs   │
         │             └──────────────────────────┘
         └────────┬────────────┘
      ┌───────────────────────┐
      │   Data Models         │
      │   - DateUsageResult   │
      │   - CodeAnalysis      │
      │   - Claude/Codex/     │
      │     Gemini            │
      └───────────────────────┘
      ┌───────────────────────┐
      │   Output Layer        │
      │   - TUI (Ratatui)     │
      │   - Table (comfy)     │
      │   - JSON/Text         │
      └───────────────────────┘
```

## Module Architecture

### Core Modules

#### 1. Entry Points (`main.rs`, `lib.rs`, `cli.rs`)

**main.rs**

- Application entry point
- Command routing to usage/analysis pipelines
- Error handling and exit codes

**lib.rs**

- Library exports for external use
- Version information (`get_version()`)
- Public API surface

**cli.rs**

- Clap-based CLI definitions
- Commands: `usage`, `analysis`, `version`
- Argument parsing and validation

#### 2. Models Module (`src/models/`)

Defines core data structures with serde serialization:

**usage.rs**

- `DateUsageResult`: Date-indexed usage map (maps date → model → usage data)

**analysis.rs**

- `CodeAnalysis`: Comprehensive conversation metadata
  - User info (username, hostname, machineId)
  - Git metadata (remote URL, commit hash)
  - Token usage breakdown
  - Tool call statistics
  - File operation counts

**claude.rs**

- `ClaudeCodeLog`: Claude Code session log format

**codex.rs**

- `CodexMessage`: OpenAI/Codex message format
- `CompletionResponse`: Response with usage data
- `ReasoningTokens`: Reasoning output token tracking

**gemini.rs**

- `GeminiMessage`: Gemini message format
- `GeminiUsage`: Token usage tracking for Gemini
- `GeminiContent`: Content types for Gemini API

#### 3. Pricing System (`pricing.rs`)

**Responsibilities:**

- Fetch LiteLLM pricing data from GitHub
- Cache pricing with 24-hour TTL (daily cache files)
- Fuzzy model name matching (Jaro-Winkler algorithm)

**Matching Strategy (Priority Order):**

1. **Exact match**: Direct string equality
2. **Normalized match**: Strip version suffixes (e.g., `-20250514`)
3. **Substring match**: Check if model name contains pricing key
4. **Fuzzy match**: Jaro-Winkler similarity ≥ 0.7 threshold
5. **Fallback**: Return $0.00 if no match found

**Cache Location:**

```
~/.vibe_coding_tracker/model_pricing_YYYY-MM-DD.json
```

**Cost Calculation:**

```rust
cost = (input × input_cost_per_token) +
       (output × output_cost_per_token) +
       (cache_read × cache_read_cost_per_token) +
       (cache_creation × cache_creation_cost_per_token)
```

#### 4. Usage Analysis Module (`src/usage/`)

**calculator.rs**

- `get_usage_from_directories()`: Main aggregation function
  - Scans `~/.claude/projects/*.jsonl`
  - Scans `~/.codex/sessions/*.jsonl`
  - Scans `~/.gemini/tmp/<project_hash>/chats/*.json`
  - Aggregates tokens by date and model
  - Calculates costs via pricing system

**display.rs**

- `display_usage_interactive()`: Ratatui-based TUI with 1s refresh
- `display_usage_table()`: Static comfy-table output (UTF8_FULL preset)
- `display_usage_text()`: Plain text format (`Date > model: cost`)
- `display_usage_json()`: Full-precision JSON output

#### 5. Analysis Module (`src/analysis/`)

**analyzer.rs**

- `analyze_jsonl_file()`: Single file analysis entry point
- Routes to Claude/Codex analyzers based on detection

**batch_analyzer.rs**

- `analyze_all_sessions()`: Batch processing for all sessions
- Aggregates metrics by date and model:
  - Edit/Read/Write line counts
  - Tool call counts (Bash, Edit, Read, TodoWrite, Write)

**detector.rs**

- `detect_format()`: Determines Claude, Codex, or Gemini format
- Detection logic: Checks for `parentUuid` field (Claude-specific)

**claude_analyzer.rs**

- Parses Claude Code JSONL format
- Extracts:
  - Tool usage (tool_use blocks in content)
  - File operations (Edit, Read, Write line counts)
  - Git metadata
  - Conversation metrics

**codex_analyzer.rs**

- Parses Codex/OpenAI JSONL format
- Extracts:
  - Token usage from `completion_response.usage`
  - Reasoning tokens
  - Total token usage

**gemini_analyzer.rs**

- Parses Gemini JSON format
- Extracts:
  - Token usage from session messages
  - Message content and metadata
  - Session-level statistics

**display.rs**

- `display_analysis_interactive()`: Ratatui TUI for batch analysis
  - Columns: Date, Model, Edit Lines, Read Lines, Write Lines, Tool Counts
  - Auto-refresh, totals row, memory usage
- `display_analysis_table()`: Static table output

#### 6. Update Module (`src/update/`)

**mod.rs**

- `update_interactive()`: Interactive update with user confirmation
- `check_update()`: Check for updates without installing
- `perform_update()`: Execute the update process
- Version comparison using `semver` crate

**github.rs**

- `fetch_latest_release()`: Fetch release info from GitHub API
- `download_file()`: Download binary archives
- Uses `reqwest` with rustls-tls

**platform.rs**

- `get_asset_pattern()`: Determine platform-specific asset name
- `perform_update_unix()`: Unix update logic (rename + replace)
- `perform_update_windows()`: Windows update logic (batch script)

**archive.rs**

- `extract_targz()`: Extract `.tar.gz` archives (Unix)
- `extract_zip()`: Extract `.zip` archives (Windows)

**installation.rs**

- `detect_installation_method()`: Analyze executable path
- `InstallationMethod` enum: Npm, Pip, Cargo, Manual
- Path pattern matching for each installation method

**startup_check.rs**

- `check_update_on_startup()`: Automatic startup check
- Cache management (24-hour TTL)
- Display update notifications with installation-specific commands
- Silent failure on network errors

#### 7. Utilities Module (`src/utils/`)

**file.rs**

- `read_jsonl()`: Line-by-line JSONL parsing
- `save_json_pretty()`: Pretty-printed JSON output

**paths.rs**

- `resolve_paths()`: Resolves Claude, Codex, and Gemini session directories
- Handles `~` expansion via `home` crate

**git.rs**

- `get_git_remote()`: Extracts Git remote URL from repository
- Uses `git config --get remote.origin.url`

**time.rs**

- `format_timestamp()`: ISO 8601 date formatting
- Date aggregation utilities

#### 8. TUI Components (`src/usage/display.rs`, `src/analysis/display.rs`)

Terminal UI components built with Ratatui:

- Widgets: Tables, borders, styled text
- Layout: Constraints-based responsive design
- Event handling: Keyboard input (q, Esc, Ctrl+C)
- Refresh loop: 1-second intervals

## Data Flow

### Usage Command Flow

```
User runs: vct usage [--table|--text|--json]
         cli.rs parses args
    main.rs::Commands::Usage
usage/calculator.rs::get_usage_from_directories()
                ├─> utils/paths.rs::resolve_paths()
                │   └─> Returns ~/.claude/projects, ~/.codex/sessions, ~/.gemini/tmp
                ├─> walkdir scans *.jsonl files
                ├─> For each file:
                │   ├─> utils/file.rs::read_jsonl()
                │   ├─> analysis/detector.rs::detect_format()
                │   └─> Aggregate tokens by (date, model)
        pricing.rs::fetch_model_pricing()
                ├─> Check cache: ~/.vibe_coding_tracker/model_pricing_YYYY-MM-DD.json
                ├─> If expired: fetch from GitHub
                └─> Fuzzy match model names
        Calculate costs for each (date, model)
    usage/display.rs::display_usage_*()
                ├─> Interactive: Ratatui TUI loop
                ├─> Table: comfy-table render
                ├─> Text: Date > model: cost
                └─> JSON: Full precision output
```

### Analysis Command Flow (Single File)

```
User runs: vct analysis --path <file.jsonl> [--output <out.json>]
         cli.rs parses args
    main.rs::Commands::Analysis
analysis/analyzer.rs::analyze_jsonl_file()
                ├─> utils/file.rs::read_jsonl()
                ├─> analysis/detector.rs::detect_format()
                │   └─> Identify Gemini (sessionId/projectHash/messages) or Claude (`parentUuid`)
                ├─> Route to analyzer:
                │   ├─> Claude: claude_analyzer.rs
                │   │   ├─> Parse ClaudeMessage structs
                │   │   ├─> Extract tool_use blocks
                │   │   ├─> Count file operations (Edit/Read/Write lines)
                │   │   ├─> Extract Git info
                │   │   └─> Aggregate tool call counts
                │   │
                │   ├─> Codex: codex_analyzer.rs
                │       ├─> Parse CodexMessage structs
                │       ├─> Extract completion_response.usage
                │       └─> Handle reasoning tokens
                │   └─> Gemini: gemini_analyzer.rs
                │       └─> Parse session JSON (messages, tokens)
        Build CodeAnalysis struct
        Output JSON (if --output specified)
```

### Analysis Command Flow (Batch)

```
User runs: vct analysis [--output <out.json>]
    main.rs::Commands::Analysis
analysis/batch_analyzer.rs::analyze_all_sessions()
                ├─> utils/paths.rs::resolve_paths()
                ├─> For each *.jsonl file:
                │   └─> analysis/analyzer.rs::analyze_jsonl_file()
                ├─> Aggregate by (date, model):
                │   ├─> Sum edit/read/write lines
                │   └─> Sum tool call counts
    analysis/display.rs::display_analysis_*()
                ├─> Interactive: Ratatui TUI (default)
                │   ├─> Show totals row
                │   ├─> Memory usage stats
                │   └─> 1s refresh loop
                └─> JSON: Save aggregated array
```

## Key Components

### 1. Format Detection System

**Location:** `analysis/detector.rs`

**Logic:**

```rust
fn detect_format(records: &[Value]) -> FileFormat {
    if records.is_empty() {
        return FileFormat::Codex;
    }

    if records.len() == 1 {
        if let Some(obj) = records[0].as_object() {
            if obj.contains_key("sessionId")
                && obj.contains_key("projectHash")
                && obj.contains_key("messages")
            {
                return FileFormat::Gemini;
            }
        }
    }

    for record in records {
        if let Some(obj) = record.as_object() {
            if obj.contains_key("parentUuid") {
                return FileFormat::Claude;
            }
        }
    }

    FileFormat::Codex
}
```

**Rationale:** Gemini exports wrap a session in a single JSON object with `sessionId`/`projectHash`, Claude Code records contain `parentUuid`, and Codex defaults to the OpenAI event log structure.

### 2. Pricing Cache System

**Design Goals:**

- Minimize network requests
- Daily pricing updates
- Offline capability (stale cache acceptable)

**Implementation:**

```rust
// Cache file naming: model_pricing_YYYY-MM-DD.json
let cache_path = format!("{}/model_pricing_{}.json", cache_dir, today);

if cache_path.exists() {
    // Use cached data
} else {
    // Fetch from GitHub, save to cache
}
```

### 3. Interactive TUI Architecture

**Framework:** Ratatui (formerly tui-rs)

**Event Loop:**

```rust
loop {
    terminal.draw(|f| {
        // Render UI
    })?;

    if event::poll(Duration::from_secs(1))? {
        if let Event::Key(key) = event::read()? {
            match key.code {
                KeyCode::Char('q') | KeyCode::Esc => break,
                _ => {}
            }
        }
    }

    // Refresh data every iteration
}
```

**Benefits:**

- Real-time updates
- Keyboard navigation
- Responsive layout
- Memory-efficient (no history buffering)

### 4. Fuzzy Model Matching

**Library:** `strsim` (Jaro-Winkler algorithm)

**Threshold:** 0.7 similarity score

**Example Matches:**

```
User model: "claude-sonnet-4-20250514"
Pricing DB: "claude-sonnet-4"
Match: Normalized (strip version suffix)

User model: "gpt-4-turbo-2024-04-09"
Pricing DB: "gpt-4-turbo"
Match: Normalized

User model: "anthropic.claude-3-sonnet"
Pricing DB: "claude-3-sonnet"
Match: Fuzzy (0.85 similarity)
```

### 5. Automatic Update System

**Location:** `src/update/`

**Components:**

**startup_check.rs:**

- Runs on every application startup (before command execution)
- 24-hour cache to minimize GitHub API requests
- Cache location: `~/.vibe_coding_tracker/update_check.json`
- Silent failure (network errors don't disrupt main functionality)
- Displays colorful box notification when update available

**installation.rs:**

- Detects installation method by analyzing executable path
- Detection patterns:
  - npm: `/npm/`, `/.npm`, `/node_modules/`
  - pip: `/site-packages/`, `/python`, `/Scripts/`, `/.local/bin/` (with Python context)
  - cargo: `/.cargo/bin/`
  - manual: All other paths (curl, PowerShell, source build)
- Provides installation-specific update commands

**Cache Structure:**

```json
{
  "last_check": "2025-10-09T12:34:56.789Z",
  "latest_version": "v0.1.7",
  "has_update": true
}
```

**User Experience Flow:**

```
Application Start
Load cache (~/.vibe_coding_tracker/update_check.json)
       ├─> Cache valid (<24h)
       │   ├─> has_update = true
       │   │   └─> Display notification
       │   └─> has_update = false
       │       └─> Silent (no output)
       └─> Cache invalid or missing
           └─> Check GitHub API
               ├─> Update available
               │   ├─> Detect installation method
               │   ├─> Display notification with update command
               │   └─> Save cache
               └─> No update
                   └─> Save cache (has_update = false)
```

**Benefits:**

- **Prevents version conflicts**: Users update via same method they installed
- **Reduces support burden**: Correct update commands shown automatically
- **Non-disruptive**: Silent failure on errors, doesn't block main commands
- **Performance**: 24-hour cache minimizes network requests
- **User-friendly**: Clear, actionable update instructions

## Design Patterns

### 1. Pipeline Architecture

Each analysis flow follows a clear pipeline:

```
Input → Detection → Parsing → Aggregation → Display → Output
```

### 2. Strategy Pattern (Display Modes)

Multiple display strategies for same data:

- `display_usage_interactive()`
- `display_usage_table()`
- `display_usage_text()`
- `display_usage_json()`

### 3. Repository Pattern (Data Access)

Centralized file access through `utils/file.rs`:

- `read_jsonl()`: Streaming JSONL reader
- `save_json_pretty()`: Formatted JSON writer

### 4. Factory Pattern (Format Detection)

`detector.rs` acts as factory, routing to appropriate analyzer:

```rust
match detect_format(line)? {
    FileFormat::Claude => claude_analyzer::analyze(),
    FileFormat::Codex => codex_analyzer::analyze(),
    FileFormat::Gemini => gemini_analyzer::analyze(),
}
```

### 5. Error Handling Strategy

**Libraries:**

- `anyhow`: Application-level errors (main.rs, high-level logic)
- `thiserror`: Library-level errors (custom error types)

**Approach:**

- Propagate errors with `?` operator
- Context-aware error messages
- Graceful degradation (e.g., $0.00 for unknown models)

## External Dependencies

### Critical Dependencies

**CLI & Serialization:**

- `clap` (4.x): Derive-based CLI parsing
- `serde` + `serde_json`: Serialization framework

**TUI:**

- `ratatui`: Terminal UI framework
- `crossterm`: Cross-platform terminal control
- `comfy-table`: Static table rendering

**HTTP & Data:**

- `reqwest` (rustls-tls): Async HTTP client
- `chrono`: Date/time manipulation

**File System:**

- `walkdir`: Recursive directory traversal
- `home`: Platform-agnostic home directory

**Algorithms:**

- `strsim`: Fuzzy string matching
- `regex`: Pattern matching

**System:**

- `sysinfo`: Memory and system stats
- `hostname`: Machine hostname retrieval

### Dependency Rationale

**Why Ratatui over alternatives?**

- Active maintenance
- Rich widget library
- Flexible layout system
- Efficient rendering

**Why rustls-tls for reqwest?**

- Smaller binary size than native-tls
- Pure Rust implementation
- Better cross-compilation

**Why comfy-table?**

- UTF-8 box drawing
- Flexible styling
- Column auto-sizing

## Performance Considerations

### 1. JSONL Streaming

**Approach:** Line-by-line parsing instead of loading entire file

**Benefits:**

- Constant memory usage
- Early error detection
- Handles large files (>100MB)

**Implementation:**

```rust
for line in BufReader::new(file).lines() {
    let line = line?;
    // Parse single line
}
```

### 2. Caching Strategy

**Pricing Cache:**

- Daily TTL reduces network requests
- Local file cache (no database overhead)

**No Session Cache:**

- Always read fresh data (session files change frequently)
- Acceptable trade-off for CLI tool

### 3. Aggregation Efficiency

**Data Structures:**

```rust
type DateUsageResult = HashMap<String, HashMap<String, serde_json::Value>>;
// Outer key: Date (YYYY-MM-DD)
// Inner key: Model name
// Value: Token usage data (varies by extension type)
```

**Complexity:** O(1) insertion, O(n) iteration for display

### 4. TUI Refresh Rate

**Interval:** 1 second

**Rationale:**

- Balance between responsiveness and CPU usage
- Session files updated infrequently (minutes/hours)
- Minimal overhead for re-aggregation

### 5. Binary Size Optimization

**Release Profile:**

```toml
[profile.release]
lto = "thin"      # Link-time optimization
codegen-units = 1 # Better optimization
strip = true      # Remove debug symbols
```

**Result:** ~3-5 MB binary

## File Format Specifications

### Claude Code JSONL Format

```json
{
  "parentUuid": "conv-123",
  "type": "apiResponse",
  "message": {
    "model": "claude-sonnet-4-20250514",
    "usage": {
      "input_tokens": 1000,
      "output_tokens": 500,
      "cache_read_input_tokens": 2000,
      "cache_creation_input_tokens": 500
    },
    "content": [
      {
        "type": "text",
        "text": "..."
      },
      {
        "type": "tool_use",
        "name": "Edit",
        "input": {
          "old_string": "...",
          "new_string": "..."
        }
      }
    ]
  }
}
```

### Codex JSONL Format

```json
{
  "completion_response": {
    "usage": {
      "prompt_tokens": 1000,
      "completion_tokens": 500,
      "total_tokens": 1500
    }
  },
  "reasoning_output_tokens": 100,
  "total_token_usage": 1600
}
```

## Directory Structure

```
~/.claude/
└── projects/
    ├── project-a.jsonl
    └── project-b.jsonl

~/.codex/
└── sessions/
    ├── session-1.jsonl
    └── session-2.jsonl

~/.gemini/
└── tmp/
    └── <project-hash>/
        └── chats/
            ├── session-1.json
            └── session-2.json

~/.vibe_coding_tracker/
├── model_pricing_2025-10-05.json  # Daily pricing cache
└── update_check.json               # Update notification cache (24h TTL)
```

## Extension Points

### Adding New AI Platforms

1. **Add model to `src/models/`**:

   ```rust
   // src/models/newplatform.rs
   #[derive(Deserialize)]
   pub struct NewPlatformMessage { ... }
   ```

2. **Update detector**:

   ```rust
   // src/analysis/detector.rs
   pub enum FileFormat {
       Claude,
       Codex,
       Gemini,
       NewPlatform,  // Add here
   }
   ```

3. **Implement analyzer**:

   ```rust
   // src/analysis/newplatform_analyzer.rs
   pub fn analyze_newplatform(lines: Vec<String>) -> Result<CodeAnalysis>
   ```

4. **Update router**:

   ```rust
   // src/analysis/analyzer.rs
   match format {
       FileFormat::NewPlatform => newplatform_analyzer::analyze(lines),
       ...
   }
   ```

### Adding New Display Formats

Implement trait-based abstraction:

```rust
trait DisplayFormatter {
    fn format(&self, data: &DateUsageResult) -> String;
}

struct TableFormatter;
struct JsonFormatter;
// Add custom formatters
```

### Adding New Metrics

1. Update `CodeAnalysis` struct in `models/analysis.rs`
2. Update analyzers to extract new metrics
3. Update display modules to render new columns

## Security Considerations

### File Access

- Only reads from known directories (`~/.claude`, `~/.codex`, `~/.gemini`)
- No arbitrary file write (output only to user-specified paths)
- No command execution in parsed data

### Network Requests

- HTTPS only (rustls)
- Single endpoint: GitHub raw content
- No authentication required
- No user data transmitted

### Data Privacy

- All processing local
- No telemetry or analytics
- Session data never leaves machine

## Testing Strategy

### Unit Tests

- Model deserialization
- Pricing calculation
- Fuzzy matching logic
- Date formatting

### Integration Tests

Located in `tests/`:

- `test_integration_usage.rs`: End-to-end usage command
- `test_integration_analysis.rs`: End-to-end analysis command

**Test Fixtures:**

- `examples/test_conversation.jsonl`: Claude format
- `examples/test_conversation_oai.jsonl`: Codex format

### Manual Testing

```bash
# Test usage command
cargo run -- usage --table

# Test analysis command
cargo run -- analysis --path examples/test_conversation.jsonl

# Test batch analysis
cargo run -- analysis

# Test error handling
cargo run -- usage --invalid-flag
```

## Future Architecture Considerations

### Potential Improvements

1. **Plugin System**: Load analyzers dynamically for new platforms
2. **Database Backend**: SQLite for historical tracking
3. **Web Dashboard**: Ratatui → WebAssembly UI
4. **Distributed Analysis**: Analyze across multiple machines
5. **Real-time Monitoring**: Watch session files for changes
6. **Export Formats**: CSV, Excel, PDF reports

### Scalability

**Current Limits:**

- File count: ~1000 sessions (tested)
- File size: Up to 1GB per file (streaming parser)
- Memory: \<50MB typical usage

**Bottlenecks:**

- Aggregation is O(n) where n = total lines across all files
- No indexing or incremental updates

**Solutions for Scale:**

- Incremental aggregation (track last-processed line)
- SQLite index on (date, model)
- Parallel file processing (Rayon)

---

**Document Version:** 1.0
**Last Updated:** 2025-10-05
**Maintainer:** Vibe Coding Tracker Team