# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## Project Overview
Rust CLI (`vibe_coding_tracker`, short alias `vct`) that scans on-disk session logs written by eight AI coding assistants — Claude Code, OpenAI Codex, GitHub Copilot CLI, and Gemini CLI (JSONL files), Grok CLI (`signals.json` plus sibling `updates.jsonl`), OpenCode (a SQLite database), Cursor (per-conversation SQLite chat stores for `analysis` and a local context-gauge estimate for `usage`), plus Hermes (a SQLite `session_model_usage` table, `usage` only) — and aggregates them into two views:
- **`usage`** — per-model token counts and LiteLLM-priced cost
- **`analysis`** — complete per-session `CodeAnalysis` JSON plus per-model file-operation and tool-call summaries (read/write/edit lines, Bash/Edit/Read/Write/TodoWrite call counts)
Both subcommands support four output modes (interactive TUI / static table / plain text / JSON) and four time-range filters (`--daily` / `--weekly` / `--monthly` / `--all`) when scanning all sessions. `vct usage --json` normalizes every row's `usage` object to the same flat token keys via `usage::normalize_usage_value` regardless of the provider's internal shape (Codex is nested internally). The interactive TUI is the batch default when no format flag is given. `vct analysis FILE` instead prints complete JSON by default, accepts `--json` / `--text` / `--table`, and rejects time-range flags.
Four auxiliary subcommands round out the CLI: `vct version` prints build/toolchain info (table default, plus `--json` / `--text`), `vct update` self-replaces the binary from the matching GitHub release asset (`--check` to inspect availability only, `--force` to skip the confirmation prompt), `vct fetch <provider>` prints a provider's raw quota/usage API response (`claude` / `codex` / `copilot` / `cursor`; `--json` default, plus `--text` / `--table`), and `vct config` shows/edits the persistent settings file (`path` / `show` / `edit` / `schema` / `migrate`; see **Persistent config** below).
## Common commands
The toolchain is pinned to `rust-toolchain.toml` (1.96.1, edition 2024). On this machine `cargo` is **not** on the default `PATH`; export it before invoking any cargo command:
```bash
export PATH="$HOME/.cargo/bin:$PATH"
```
Standard `cargo build` / `cargo test` / `cargo bench` work as usual. Project-specific entries:
| Command | What it does |
| ------------------------------------------- | ------------------------------------------------------------------------------------- |
| `cargo build --profile dist --locked` | Distribution build (fat LTO, single codegen unit) — used by release artifacts |
| `cargo build --release --features mimalloc` | Opt-in mimalloc allocator (faster one-shot, ~10× higher RSS in TUI loops) |
| `make fmt` | `cargo fmt --all` + `cargo clippy --fix` + clippy with `-D warnings` |
| `uvx pre-commit run --all-files` | Run all pre-commit hooks (whitespace, JSON/YAML/TOML, mdformat, gitleaks, shellcheck) |
| `uvx pre-commit install --install-hooks` | Install the git hooks once after cloning |
Criterion benchmarks live at `benches/benchmarks.rs`; reports land in `target/criterion`.
**Before every commit / PR**, always run `make fmt` and `uvx pre-commit run -a`. CI runs both with `-D warnings`, and the pre-commit hooks gate-keep the repo (gitleaks, mdformat, etc.).
## Architecture
### Two-stage pipeline
```
Local session data
│
▼
src/session/ ← provider detection + per-provider parsers → CodeAnalysis
│
▼
src/analysis/ ← roll up CodeAnalysis records → AggregatedAnalysisRow
src/usage/ ← roll up CodeAnalysis records → UsageResult / PerProviderUsage
│
▼
src/display/ ← TUI / table / text / JSON renderers
```
`src/session/` owns the "raw bytes → typed `CodeAnalysis`" boundary so both `analysis` and `usage` consume the same parsed shape. Do **not** add direct file parsing to `src/usage/` or `src/analysis/`; route everything through `src/session/parser.rs`.
### Provider classification
`src/session/detector.rs` distinguishes the five file-based providers by JSON / JSONL markers:
- **Gemini** — first line is a session-meta record with `sessionId` + `projectHash` and *no* `messages` array
- **Grok CLI** — a JSON object with `primaryModelId` + `contextTokensUsed` and either `contextWindowTokens` or `toolsUsed`
- **Copilot CLI** — first line is `type == "session.start"` with `data.producer` starting with `"copilot"`
- **Claude Code** — any record carrying a `parentUuid` field
- **Codex** — any record whose `type` is one of `session_meta` / `turn_context` / `event_msg` / `response_item`, or default fallback when no other marker is found
Four parser entry points live in `src/session/parser.rs`:
- `parse_session_file(path) -> serde_json::Value` — compatibility wrapper around the typed parser, retained for library callers and golden tests.
- `parse_session_file_typed(path) -> CodeAnalysis` — typed, content-based auto-detection used by `vct analysis FILE`, where no provider is known up front.
- `parse_session_file_typed_with_mode(path, mode)` — same as above but lets the caller choose `ParseMode::Full` vs `UsageOnly`.
- `parse_session_file_as(path, provider, mode)` — caller already knows the provider from the source directory. Use this from every directory walker — it eliminates the "metadata sentinel mis-classifies a Claude session as Codex" bug class. `provider` is the `ExtensionType` enum defined in `src/models/analysis.rs`.
`classify_records()` is the streaming-friendly variant that returns `None` on indeterminate records and lets callers keep peeking until a marker arrives — there is **no fixed peek window**, which is critical because a Claude metadata preamble (`permission-mode`, `file-history-snapshot`, …) can be arbitrarily long.
Known-provider Claude / Codex / Copilot scans reuse a byte buffer and deserialize directly into typed records; they create a dynamic `serde_json::Value` only after typed deserialization fails so diagnostics can classify the record. Gemini keeps its dynamic-value path. Single-file auto detection advances one stateful classifier as records arrive, so a long metadata preamble is linear rather than repeatedly rescanned.
### Provider log normalization
- **Claude Code**: A successful file tool appears once as an assistant `tool_use` invocation and again as a tool result. Count the invocation once, then attach result details without incrementing it again. `TaskCreate`, `TaskUpdate`, and `TaskStop` map to the existing `TodoWrite` metric (`TaskList` / `TaskOutput` are reads and stay unmapped). 2.1.x metadata records (`bridge-session`, `mode`, `last-prompt`, `ai-title`, `file-history-delta`, …) are recognized-and-ignored — an unknown future type still counts as drift.
- **Codex**: Current logs can use `custom_tool_call` / `custom_tool_call_output`, and custom output may be a content-block array rather than a string. A top-level custom `apply_patch` can become file operations only when its paired output explicitly reports success. A JavaScript `exec` cell has no structured trace for nested `tools.*` calls, so record the completed top-level cell as one Bash call and never infer Read/Edit/Write operations from lexical JavaScript. The file changes those exec cells produce are instead reported by the authoritative `patch_apply_end` event_msg: on `success == true` its `changes` map folds into write/edit metrics (entries may omit `unified_diff` — count the operation with zero line/char content), deduplicated against direct `apply_patch` cells by `call_id`.
- **Copilot CLI**: Current tool names include `show_file`, `rg`, `grep`, `glob`, `web_search`, `web_fetch`, `write_bash`, and several `apply_patch` argument shapes. Search tools count as Read invocations but do not invent file paths or line totals when their output cannot be mapped to a single file.
- **Gemini CLI**: The JSONL file is an append-only event history, not a sequence of independent final messages. Keep the latest revision for each message id and merge `$set.messages` entries by id to avoid double counting. Do not remove historical metrics for `$rewindTo`: it changes the visible conversation but does not refund billed usage or undo tools that already ran.
- **Grok CLI**: `signals.json` is the scan entry point. Its `contextTokensUsed` is one current local context gauge, mapped to `cache_read_input_tokens` and priced at the model's cache-read rate (`CostSource::GrokGauge` falls back to the input rate when the matched LiteLLM entry publishes no cache-read price, so `xai/grok-*` variants with a null cache-read field don't silently cost $0); it is not cumulative billed usage. The sibling `updates.jsonl` pairs `tool_call` records with final `tool_call_update` records and normalizes only successful `read_file` / `grep` / `write` / `search_replace` / `run_terminal_command` / `todo_write` calls into Read / Write / Edit / Bash / TodoWrite metrics (`grep` exit code 2 remains a failure even when the lifecycle status is `completed`; `spawn_subagent` / `get_command_or_subagent_output` are recognized-and-ignored orchestration tools). Completed `EditsApplied.absolute_path` and `edits.details` are authoritative, while one lifecycle still counts as one Edit call. Metadata prefers `summary.info.cwd`, then `.cwd`, then the URL-decoded workspace directory name. Grok has no quota panel or `vct fetch` provider.
Batch analysis keeps the same normalized contract. `collect_analysis_sessions_with` / `collect_analysis_sessions_from_paths_with` build an `AnalysisDataset` of provider-tagged `CodeAnalysis` values. `vct analysis --json` collects in `ParseMode::Full` and serializes the dataset as `CodeAnalysis[]`, so every element has the same shape as `vct analysis FILE` and the golden fixtures. TUI / text / table parse in `UsageOnly` and stream the same scalar fields into `AnalysisData`; they discard only the large per-operation detail payloads. Noninteractive collection retains candidate, success, and failure diagnostics: an all-failed scan returns an error instead of a misleading empty result, while a partial failure keeps successful output and writes a warning to stderr. File parsers retain parser-only source, recognized-envelope, analyzer-payload, and successful-normalization counts in an internal `ParsedAnalysis` / `ParseDiagnostics` wrapper outside `CodeAnalysis`, so a nonempty future schema cannot masquerade as a successful all-zero session without changing the public struct or JSON contract; blank in-progress files remain valid. OpenCode counts selected assistant rows and Cursor reports each store independently for the same reason. Usage exposes the equivalent `UsageCollectionDiagnostics` contract. Both TUIs remain best-effort: a failed background refresh keeps the last successful payload, shows a short failure status, and leaves details in the diagnostic log.
OpenCode does **not** flow through the detector or `parse_session_file_*`: it lives in a single SQLite database, so `src/session/opencode.rs` reads it directly (`read_opencode_usage` from assistant messages with a legacy `session` fallback, `read_opencode_analysis` from `message` + `part`) and produces the same `CodeAnalysis` shape, which the `usage` / `analysis` aggregators fold in alongside the file-based providers. The DB is opened read-only (with a temp-copy fallback) so the user's database is never mutated. Analysis reads metadata/messages and tool parts inside one transaction so a concurrent OpenCode commit cannot mix two SQLite snapshots.
OpenCode cost is a special case: `usage::resolve_model_cost` prices an OpenCode model from tokens only on an **exact** LiteLLM match (`ModelPricingMap::get_exact`); with no exact match it uses the stored assistant-message cost carried in `UsageData::stored_costs` rather than the normalized/substring/fuzzy fallback the other providers use. This keeps a novel model like `deepseek-v4-pro` from being mis-priced against a loosely-similar name. Cursor reuses this same stored-cost mechanism but with its own basis — `resolve_model_cost` takes a `CostSource` (`Litellm` / `OpenCodeStored` = exact-match-else-stored / `CursorStored` = the dashboard cost verbatim, never re-priced / `HermesStored` = same exact-match-else-stored basis as OpenCode). The providers' stored costs live in **separate** per-provider maps (`UsageData::stored_costs` is a `StoredCosts { opencode, cursor, hermes }`) so a legacy OpenCode session carrying a bare model name can't cross-contaminate a same-named Cursor or Hermes model.
Cursor is split across two boundaries and, like OpenCode, bypasses the detector / `parse_session_file_*` — `src/session/cursor.rs` owns it:
- **`analysis`** reads per-conversation blob stores at `~/.cursor/chats/*/*/store.db`. Each store is a content-addressed SQLite blob DAG: assistant turns live in **binary protobuf nodes** (`field 4` = the message JSON, `field 26` = timestamp, `field 5` = a running context-window gauge), while `Read` tool results live in standalone JSON blobs joined back by `toolCallId`. `read_cursor_analysis` decodes only those three fields (never the DAG topology, so it is robust to schema additions) and folds each turn's tool calls (`Write` / `StrReplace`→edit / `Read` / `Shell`→bash / `TodoWrite`) through `SessionParseState`. The conversation's model comes from `~/.cursor/ai-tracking/ai-code-tracking.db` (`conversationId -> model`, one model per conversation in practice), falling back to the store's hex-encoded `meta.lastUsedModel`. Current cursor-agent builds record the literal string `default` for auto-mode CLI conversations (in both sources), so those sessions bucket under a synthetic `default` model that deliberately prices at $0 — a data-source limitation, not a decoder bug.
- **`usage`** is a **local estimate**: `read_cursor_usage` emits only each conversation's context gauge from the chat stores (`approximation_events` → `aggregate_events`) as cache-read tokens, loading only the binary DAG-node blobs (SQL-filtered on the `0x0A` first byte) so large JSON tool-result payloads never materialize on this cheap path; an assistant turn whose gauge payload is empty is a benign no-reading variant, not drift. The upper usage layer prices the combined result through its one shared LiteLLM map (a deliberately-rough, input-only approximation, `$0` for models Cursor prices itself), so the reader does not build another pricing map on every refresh. Cursor still behaves like every other provider (all local-file based) and needs no network or credentials. It undercounts Cursor's real spend because much of it is billed under Cursor-internal model names LiteLLM can't price; the numbers are approximate on purpose. There is **no** dashboard-billing-API path here — the `get-filtered-usage-events` fetch (and its `~/.vct/cursor_usage_events.json` cache) was removed as too much surface for the value; the raw endpoint is documented in `docs/quota.md` if it is ever reintroduced. Both Cursor SQLite DBs are opened read-only with the same temp-copy/WAL fallback as OpenCode.
Hermes is **`usage`-only** and, like OpenCode, is a single SQLite database read directly (bypassing the detector / `parse_session_file_*`) — `src/session/hermes.rs` owns it. `read_hermes_usage` reads the pre-aggregated `session_model_usage` table at `state.db` under Hermes's home (`resolve_hermes_home` in `paths.rs` mirrors Hermes's own `get_hermes_home`: `$HERMES_HOME` wins, else `~/.hermes` on POSIX / `%LOCALAPPDATA%\hermes` on Windows), keying each row by the bare `model` column (so a model billed through several providers merges into one row) and mapping its token columns onto the same flat `CodeAnalysis` shape. Each session is also reconciled against the `sessions` aggregate — the positive residual (`sessions.<col>` minus the sum of its per-model rows) is attributed to the session's recorded model, mirroring Hermes's own insights view so partial or missing per-model rows aren't under-counted. Two quirks: `last_seen`/`first_seen` are REAL epoch **seconds** (not OpenCode's milliseconds), and `output_tokens` follows the OpenAI convention of **including** reasoning, so the reader subtracts `reasoning_tokens` back out to keep each token billed once. The row's cost is `actual_cost_usd` when known, else `estimated_cost_usd`, folded through the same `StoredCosts` path as OpenCode (`HermesStored`). There is no `analysis` reader (the table has no file-operation detail) and no quota panel (local DB, no remote API).
OpenCode, Cursor, and Hermes all use `src/session/sqlite.rs`. It first opens the user's database read-only. Its fallback fingerprints the main database plus WAL before and after copying both into a private temp directory, retries a changing source at most three times, treats a WAL copy failure as fatal, and opens only the private copy read-write for local WAL recovery. Never bypass this helper by opening a user's database read-write.
### `ParseMode`
`src/session/state.rs` defines `ParseMode::Full` vs `ParseMode::UsageOnly`. The usage and summary-display paths use `UsageOnly` to skip allocating the large `write_file_details` / `edit_file_details` bodies — this is a major part of why the TUI sits at ~30–50 MB RSS even on 200+ session directories. All scalar counters, including `total_unique_files`, must remain identical between modes. Preserve this distinction when adding new fields to `CodeAnalysisRecord`.
### Token accounting quirks
`src/utils/token_extractor.rs` (`extract_token_counts`) normalizes provider shapes into disjoint billable buckets. Two provider-specific subtleties:
- **Codex reasoning is a subset of output, and usage is attributed per model by delta.** Codex follows OpenAI's convention where `total_token_usage.output_tokens` (completion) already includes `reasoning_output_tokens`, and `total_tokens == input + output`. The Codex branch subtracts reasoning back out of `output_tokens` so each token is billed once. Do **not** re-add reasoning to output or total here. Because `total_token_usage` is a whole-session cumulative counter, the parser attributes each `token_count` event's **delta** (vs the previous snapshot) to the model that was current at that event — a mid-session model switch no longer bills the pre-switch prefix under both models, and a resumed session's pre-context replay snapshot just advances the baseline without attribution (`CodexTokenTotals` in `src/utils/usage_processor.rs`).
- **Copilot `outputTokens` also includes `reasoningTokens`** (verified on real 1.0.x logs — a 12-visible-token reply reported output 353 / reasoning 341), so `src/session/copilot.rs` emits `output_tokens = output - reasoning` at the parser, the same include-then-subtract normalization Hermes uses. Gemini is the flat provider whose reasoning (`thoughts_tokens`) really is *disjoint* from output — no subtraction there. Gemini writes reasoning as `thoughts_tokens` while the flat providers (Copilot / OpenCode / Hermes) use `reasoning_output_tokens`; a single record carries only one, but a cross-provider merge of the same model keeps both, so `extract_token_counts` **sums** the two keys rather than letting one overwrite the other.
- **Claude `advisor_message` iterations are counted (for `usage` only).** Claude Code's top-level `usage` equals the sum of the `message`-type entries in `usage.iterations` and excludes any `advisor_message` iteration. `src/session/claude.rs` captures those advisor tokens in a **separate** `CodeAnalysisRecord::advisor_usage` map (keyed by the advisor's own model, `#[serde(skip)]`), so vct's Claude `usage` totals run **higher** than Claude Code's own `/cost`. They are kept out of `conversation_usage` on purpose: the `analysis` aggregator attributes a record's file-op / tool counts to every model in `conversation_usage`, and an advisor model never executes tools, so adding it there would mis-credit it with the main model's metrics.
### Provider directories
Resolved by `src/utils/paths.rs` (`resolve_paths`):
| Provider | Source path |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Claude Code | `~/.claude/projects/**/*.jsonl` (recursive — includes subagents) |
| Codex | `~/.codex/sessions/**/*.jsonl` |
| Copilot CLI | `~/.copilot/session-state/<sessionId>/events.jsonl` (depth-bounded walk via `COPILOT_SESSION_MAX_DEPTH` to skip per-session snapshot subtrees) |
| Gemini CLI | `~/.gemini/tmp/<project_hash>/chats/*.jsonl` plus `chats/<parent_session>/*.jsonl` for subagents |
| OpenCode | `~/.local/share/opencode/opencode.db` (SQLite database, read via `rusqlite`; honors `$XDG_DATA_HOME`) |
| Cursor | `~/.cursor/chats/*/*/store.db` (SQLite chat stores, `analysis` + a local `usage` estimate) + `~/.cursor/ai-tracking/ai-code-tracking.db` (model attribution) |
| Hermes | `$HERMES_HOME/state.db` (default `~/.hermes`, or `%LOCALAPPDATA%\hermes` on Windows; SQLite `session_model_usage` table, read via `rusqlite`; `usage` only) |
| Grok CLI | `$GROK_HOME/sessions/*/*/signals.json` (default `~/.grok`; sibling `summary.json` supplies metadata and `updates.jsonl` supplies `analysis`) |
| Pricing cache | `~/.vct/model_pricing_YYYY-MM-DD.json` |
| User settings | `~/.vct/config.toml` |
| Log files | `~/.vct/logs/vct-YYYY-MM-DD.log` (daily plain-text diagnostics; created lazily) |
### TUI refresh model
CLI batch scans run inside one dedicated Rayon pool; never configure Rayon's global pool. Each interactive entrypoint enters `TerminalSession` before loading data and immediately renders the centered `Loading sessions...` spinner while a single `RefreshWorker` scans in the background. Loading continues to process quit, Ctrl+C, and resize events. A ready TUI retains the old payload during refresh, shows `Refreshing...`, allows one active job plus one coalesced pending job, and schedules the next automatic scan from completion time. Initial failure restores the terminal before returning; later failures retain last-known-good data. Usage refreshes build merged and unmerged rows together so `m` never scans.
`TerminalSession` is the cleanup boundary: setup rolls back partial terminal changes, and Drop attempts raw-mode disable, alternate-screen leave, cursor restore, and active-flag clear even when an earlier cleanup action fails. Quota shutdown guards signal every worker on all exits but never join a thread that may still be inside an HTTP timeout.
### Quota panels (`src/quota/`)
The `usage` TUI shows live remaining quota for **Claude / Codex / Copilot / Cursor**, each fetched over HTTP on its own background thread (`provider::spawn_quota_worker`, **one shared poll cadence for every provider** from `config.usage.quota.refresh_interval`, default 60s), seeded from a `~/.vct/<provider>_usage.json` cache. A transient failure — a network error or a `429` rate limit (`QuotaOutcome::Transient`) — keeps the last-known-good snapshot **indefinitely**: the panel keeps showing it with a growing staleness marker until the next success overwrites it, and never blanks out (there is **no** stale-drop). There is **no** `statusline` subcommand — the old `vct statusline ingest` mechanism was removed; Claude quota now comes from `GET https://api.anthropic.com/api/oauth/usage` (sent with the `anthropic-beta: oauth-2025-04-20` header to unlock the richer `limits` / `spend` fields — per-model weekly cap + credit balance). The panel's Plan line reads `rateLimitTier` (fallback `subscriptionType`) from the credentials file, prettified (`default_claude_max_20x` -> `max 20x`). The per-model `weekly_scoped` window is **volatile** (e.g. Fable is subscription-only, time-limited), so the whole `limits` array is parsed leniently (`de_lenient_limits` skips malformed entries; scalar windows tolerate null) and the scoped row is only rendered when both the window and its model label resolve — an absent/broken scope silently drops the row, never erroring the response.
**Codex** (`src/quota/wham.rs`) reads the main quota and reset-credit count from `GET /wham/usage`, then best-effort enriches it from `GET /wham/rate-limit-reset-credits`. A details-only `401` triggers one token refresh and retry; every details failure still preserves the usage summary. The details endpoint's `available_count` remains authoritative even when its capped `credits` list is shorter; the panel filters `status == "available"`, sorts finite RFC3339 expiries before non-expiring credits, and shows the earliest fetched expiry.
**Copilot** (`src/quota/copilot.rs`) reads the long-lived `gho_` token from `~/.copilot/config.json` (which is **JSONC** — `strip_jsonc_comments` removes `//` / `/* */` comments in a string-aware scan so a `//` inside a `"https://github.com:login"` key survives) and calls `GET https://api.github.com/copilot_internal/user` (host derived per account via `copilot_api_url` — a GHE data-residency login hits `api.<host>` instead) with `Authorization: token <gho_>`. It **impersonates the Copilot CLI** (the client the token belongs to): `User-Agent: GitHubCopilotCLI/<version>` (version via the shared `detect_cli_version("copilot", "copilot_version.json", …)`) + `Copilot-Integration-Id: copilot-cli`. The panel maps only `premium_interactions` into the gauge (`used_percent = 100 - percent_remaining`, dropping the zero-entitlement placeholder); the response's `chat`/`completions` flags are not read. **No token refresh** (the `gho_` token is long-lived); a `401`/`403` surfaces a `run: copilot login` hint.
**Cursor** (`src/quota/cursor.rs`) reads the WorkOS session JWT from `~/.config/cursor/auth.json` (`cursor_dir` honors `$XDG_CONFIG_HOME`), base64url-decodes the payload for `sub` (userID after the final `|`) and `exp`, synthesizes the `WorkosCursorSessionToken=<uid>%3A%3A<accessToken>` cookie, and calls `GET https://cursor.com/api/usage-summary`, impersonating the Cursor CLI via a `User-Agent: cursor-agent/<version>` (version via the shared `detect_cli_version("cursor-agent", "cursor_version.json", …)`). It maps `totalPercentUsed` / `autoPercentUsed` / `apiPercentUsed` into three gauges (reset = `billingCycleEnd`) plus on-demand spend (cents→USD). Refresh is **reactive**: the file is re-read each tick and the token used while its JWT `exp` is in the future (the official Cursor client keeps it fresh); **never written back**. Expiry or a `401`/`403` surfaces a `run: cursor-agent login` hint.
- **Credential files read:** Claude `~/.claude/.credentials.json` (`claudeAiOauth`, written back on refresh), Codex `~/.codex/auth.json` (`tokens`, written back on refresh), Copilot `~/.copilot/config.json` (`copilotTokens`, read-only), Cursor `~/.config/cursor/auth.json` (`accessToken`, read-only). On macOS Claude stores its credentials in the Keychain (Claude panel absent there); Cursor's `~/.config/cursor` path is Linux-oriented.
- **Token refresh (Claude / Codex only)** lives in `src/quota/refresh.rs` (shared primitives) + each fetcher. It only fires when a token is near expiry (Claude `expiresAt` ms) or an API call returns 401 (Codex is reactive-only — `auth.json` has no expiry). A refreshed access token is cached in memory so the worker reuses it instead of refreshing every tick; the new token is also written back **atomically, preserving every other field** (`update_json_file_in_place` mutates a whole `serde_json::Value`, never a narrow struct) in that CLI's exact timestamp format. Both providers' refresh tokens **rotate** (persist the new one) — a re-check of the file mtime just before write aborts the write if a concurrent official CLI rotated first. Copilot and Cursor have no driveable refresh, so they skip this path entirely.
- **Backoff:** a refresh failure arms a per-provider cooldown (`RefreshCooldown`, 5 min) keyed on the credential file mtime, so a revoked token cannot spin the token endpoint; a mtime change (re-login) retries immediately. Persistent failure surfaces a `run: <provider> auth login` hint (`needs_login` is a snapshot field independent of `QuotaSource`, so Codex keeps showing session-fallback data alongside the hint).
- Panels are **TUI-only** and appear only for a provider whose credentials exist. With four providers, the band layout is responsive (`arrange_band` / `split_band` in `src/display/usage/interactive.rs`): the slimmed Provider Usage table (Provider / Tokens / Cost — **Active Days column dropped**) shares the row only when the terminal is wide enough, otherwise it folds out and the panels take the full width, wrapping to a 2×2 grid at narrow widths. `src/display/common/table.rs::main_layout` is **shared with the analysis view** and must not change; only the band height fed to it grows.
**Antigravity (investigated, deferred — not shipped):** a full Antigravity quota implementation was built and then removed. Blocker: its Google OAuth client id/secret live only inside the Antigravity **IDE** language-server binary (`~/.antigravity-ide-server/.../language_server_linux_x64`), not in `~/.gemini/antigravity-cli`, so there is no clean way to obtain them without either committing the (public installed-app) secret or extracting it from that binary at runtime — plus Antigravity is still beta. Endpoints/shapes are captured in the memory note `project_antigravity_quota`.
### Pricing (`src/pricing/`)
1. Daily pricing fetched from LiteLLM (`https://github.com/BerriAI/litellm/raw/.../model_prices_and_context_window.json`)
2. Cached as `~/.vct/model_pricing_YYYY-MM-DD.json`. The cache stores the **filtered raw upstream JSON**, not the derived `ModelPricing` shape. A response must be HTTP 2xx, a JSON object, and contain at least one model with a real price before atomic cache replacement. A 429, 5xx, `{}`, or price-free payload never poisons the daily file.
3. A failed fetch starts a fixed 300-second backoff. Manual data refresh does not bypass it. Once a process has a successful map, it retains that map on failure and reloads the daily file only after the UTC date changes.
4. Lookup priority: exact → validated normalized → most-specific substring → Jaro-Winkler fuzzy (≥0.7 threshold) → $0.00 fallback. Generic placeholder names (`default`, `auto`, …, e.g. cursor-agent's auto-mode attribution) and model segments shorter than 4 chars never enter the substring/fuzzy stages — unpriced beats a coincidental neighbor's price. Date suffixes require exactly eight ASCII digits; version suffixes allow only numeric/dot forms such as `v2` or `v1.0`, never words such as `vision`. Normalized collisions prefer the same provider, then the unprefixed base, then deterministic length/lexical tie-breaks.
5. `ModelPricingMap` precomputes normalized + lowercase indices and uses `Rc<str>` keys to avoid cloning. Each map owns its own capacity-64 LRU. `clear_pricing_cache()` advances a generation so existing maps lazily invalidate their local caches without cross-map contamination.
6. **Context tiers are per request.** LiteLLM's `*_above_Nk_tokens` rates apply only to a request whose own prompt context exceeds the threshold — never to aggregated token sums (the old aggregate-tier selection promoted a whole month of small requests to the ~2x rate). The usage scan derives a `TierThresholds` snapshot from the pricing map (`ModelPricingMap::tier_thresholds`), threads it via `UsageScanOptions` into the Claude / Codex / Gemini parsers, which classify each request as it folds and accumulate the above-threshold slice into a nested `above_tier` object (internal to usage values; `analysis` paths never produce it, so the public `CodeAnalysis` JSON is unchanged). `calculate_cost` bills the base slice at base rates and the `above_*` slice at the lowest tier's rates. Providers without per-request granularity (Copilot, DB readers, gauges) and offline scans classify nothing — a deliberate lower bound. `SummaryScanCache` clears itself when the thresholds fingerprint changes (daily pricing reload).
7. Cost is not token-only: Claude `server_tool_use.web_search_requests` is billed **per query** at `ModelPricing::web_search_cost_per_query` (derived by `parse_litellm_entry` from LiteLLM's nested `search_context_cost_per_query`, a flat $0.01 for Anthropic). `resolve_model_cost` adds it on top of the token cost; it is 0 for every non-Claude model. `web_fetch_requests` is **not** separately billed (its fetched content already counts as input tokens).
### Memory tuning (Linux glibc only)
`src/main.rs` calls `tune_system_allocator()` **before** any allocation. It applies `mallopt(M_ARENA_MAX, 2)` + `mallopt(M_TRIM_THRESHOLD, 128 KiB)` to stop Rayon worker arenas from multiplying retention across cores. Each TUI refresh ends with `release_freed_heap()` (`malloc_trim(0)`) to hand free pages back to the kernel. **Do not remove these calls** — without them the TUI grows ~6 MB per 10 s refresh on long sessions. Both are no-ops on non-Linux/glibc.
### Scan caches (`src/summary_cache.rs`, `src/cache/`)
Every TUI refresh worker owns one process-local `SummaryScanCache`, with no global lock. It stores only source fingerprints, provider/date, compact token contributions, analysis counters, and diagnostic summaries — never raw JSON, tool bodies, or full `CodeAnalysis`. File fingerprints use mtime + length; Grok also tracks `summary.json`, `updates.jsonl`, and `.cwd`; SQLite tracks the main DB + WAL; Cursor also tracks its model-attribution DB. Unchanged refreshes parse zero sources, deletion drops entries, disabled providers do not fold, cached schema failures keep diagnostics, and transient open/read/permission errors retry. `analysis --json` and `analysis FILE` remain Full-parser paths and bypass this cache.
The public `GLOBAL_FILE_CACHE` remains a library-only compatibility API keyed by `PathBuf` and holding `Arc<CodeAnalysis>`. Do not reconnect it to the CLI/TUI hot path.
### Diagnostic logging (`src/logging/`)
The crate keeps the standard `log` facade (every `log::warn!` / `error!` call site is unchanged) but the output backend is a **custom `log::Log` that writes only to a file** — never stdout/stderr, so it is TUI-safe by construction (there is no `env_logger` anymore). Records land in `~/.vct/logs/vct-YYYY-MM-DD.log` (plain text, `<utc-nanos> <LEVEL> <target> <message>`, the crate's `vibe_coding_tracker::` target shortened to `vct::`; the file name is the **UTC** date so it matches the line timestamps, and rolls over on the first record after UTC midnight). The file is opened behind a `Mutex` shared by the main thread and the ≤4 quota workers, written with a single `write_all` per record and **no user-space buffering** so nothing is lost under `panic = "abort"`, and **created lazily on the first record** — a command that logs nothing (e.g. a successful `vct version`) never touches `~/.vct`, preserving the "settings-free commands don't create `~/.vct`" rule.
`main.rs` calls `logging::init()` **once, before dispatch** (installs the logger at the default level `warn` plus a panic hook) and `logging::apply(&config.logging)` in the `usage` / `analysis`-batch branches after `config::load()` — that reads `[logging]` to set the level via `log::set_max_level` and prunes daily files older than `retention_days` (default 7). Because config load is lazy, subcommands that never load it stay at the `warn` default (still enough to record errors). The panic hook chains to the previous hook after calling `tui::force_restore_terminal()` (gated by the `IN_TUI` atomic set by `setup_terminal`/`restore_terminal`) so a panic during the TUI restores the screen before the default message prints, and `main` logs the final top-level `Err` at ERROR before returning it. **Where errors are recorded:** quota fetchers log every `Transient` collapse. Parser schema drift is summarized once per source/fingerprint with the complete failure count and first concrete reason, rather than logging once per record. Interactive parser / SQLite failures stay file-only so they cannot corrupt the TUI; noninteractive partial collections deliberately emit one summary to stderr, while all-failed collections return an error. The two non-TUI pricing warnings still `eprintln!` to the console **and** mirror to the log.
### Build version (`build.rs`)
`BUILD_VERSION` is assembled git-describe-style from separate git commands (latest tag + commits since + short SHA + `-dirty` suffix when applicable). Outside a git worktree it falls back to `Cargo.toml`. `BUILD_RUST_VERSION` and `BUILD_CARGO_VERSION` come from `rustc --version` / `cargo --version` at build time. All three are exposed via `vct version`.
`src/main.rs` also short-circuits the top-level `--version` / `-V` flag *before* `Cli::parse()`, by inspecting `std::env::args_os().nth(1)` and printing `VERSION` directly. This keeps the conventional `vct --version` flag working in parallel with the `vct version` subcommand — preserve this branch if you touch the entry point.
### Self-update (`src/update/`)
`vct update` resolves the current host's `(os, arch, libc)` tuple via `platform.rs`, fetches the matching asset from the latest GitHub Releases tag, extracts the archive (zip on Windows, tar.gz elsewhere) via `archive.rs`, then atomically replaces the running binary. `mod.rs` exposes `check_update()` (no-op probe) and `update_interactive(force)` (the path `--force` skips the confirmation prompt). `extract_semver_version()` strips the `git describe` suffix so the freshness comparison only looks at the SemVer tag.
Every update check records the result to `~/.vct/version.json` via `version_cache::record_version_check` (`SelfVersion { latest_version, last_checked_at, dismissed_version }`) as groundwork for a future auto-update prompt. It preserves any existing `dismissed_version` and stamps `last_checked_at` with `now_rfc3339_utc_nanos()` (RFC3339 UTC nanoseconds, e.g. `2026-07-07T05:34:50.563606999Z`) — the same stamp the per-CLI version caches (`{claude,codex,copilot,cursor}_version.json`, written by `detect_cli_version`) now use for their `last_checked_at` field. All four version caches refresh once per UTC day. This `version.json` record is **separate** from `config.toml` (below) and is not folded into it.
The release workflow builds every target with `cargo build --profile dist --locked --target <target>` and stages the binary from `target/<target>/dist`. Keep workflow paths, `Cargo.toml`'s `dist` profile, and packaging expectations aligned.
### Persistent config (`src/config/`)
`config::load()` reads `~/.vct/config.toml` into a typed `Config` (`general` / `usage` / `analysis` / `performance` / `providers` / `logging` sections, with quota settings nested under `[usage.quota]`), **generating a commented default from the typed structs on first run** (no hand-maintained template). Reads are infallible (missing/malformed → `Config::default`); writes go through `toml_edit` so hand-added comments and unknown keys survive (`save_merge_models` is the live-write path for the `m` toggle, and guards against a hand-edited non-table `[usage]` before indexing). `main.rs` loads it lazily — only inside the `usage` and `analysis`-batch branches, so settings-free commands (`version`, `fetch`, `analysis FILE`) never read or create `~/.vct/config.toml` — and threads the values down: `default_time_range` via `resolve_time_range_with_default` (an explicit period flag always wins), `performance.scan_threads` into the dedicated CLI scan pool, `providers` (per-provider include toggles) into the `*_with` collection / aggregation entry points, and `usage.quota.panels` (which quota panels to show) / `usage.refresh_interval` (TUI redraw cadence) / `usage.quota.refresh_interval` (the one shared quota-poll cadence) into the two interactive display functions. A disabled provider skips its whole aggregation block (no scan, no API). The typed structs are the **single source of truth**: `schemars` derives both `vct.schema.json` (committed at the repo root, referenced by a `#:schema` directive on the generated file's first line, and printed/regenerated via `vct config schema`) and the commented default (`default_document` serializes `Config::default()` and injects each field's doc-comment as a `#` comment), guarded by drift tests (`committed_schema_matches_generated` + `generated_template_parses_to_expected_defaults`). Adding a setting: add a field with `#[serde(default)]` + a `///` doc-comment (which becomes both the schema `description` and the config comment) and thread it through the `_with` seam rather than re-reading the file per refresh tick. Legacy files are upgraded through **two layers**. On load (and via `vct config migrate`, which shares the same pass), `migrate_document` rewrites a standard-`[header]`-table file **in place** — prepends the `#:schema` directive, renames `refresh_interval_secs` → `refresh_interval` (dropping the stale key if both coexist), and moves a top-level `[usage].quota_panels` into the nested `[usage.quota]` (filling in the default quota `refresh_interval`), refreshing the schema comment only on the keys it touches. It is idempotent (returns whether it changed, so `load_in` rewrites at most once) and never overwrites malformed TOML. A read-time `migrate_legacy` shim then backstops any residual legacy form the structural pass deliberately skips (an inline `usage = { ... }` table), so the in-memory `Config` is correct even when the file was not rewritten. This replaced a serde `alias`, which would make serde reject a mid-upgrade file carrying both the old and new name as a duplicate field, which the infallible read then turns into a silent full-config reset.
`performance.scan_threads` resolution is positive config value, then positive `RAYON_NUM_THREADS`, then auto `min(2, available_parallelism)`. Every result is capped at available parallelism and at least one; `0` means auto and requires no legacy migration.
## Conventions
- **Commit messages** are English-only and follow [Conventional Commits](https://www.conventionalcommits.org/) (`feat:` / `fix:` / `docs:` / `perf:` / `refactor:` / `style:` / `test:` / `chore:` / `ci:`). The `semantic-pull-request` workflow enforces this on PR titles, and `git-cliff` consumes them for release notes.
- When CLI behavior or flags change, update **all three** READMEs (`README.md`, `README.zh-CN.md`, `README.zh-TW.md`) in the same PR — they must stay in sync.
- **A file path belongs in a comment only when the comment is a pointer.** A path that merely decorates a claim ("the same schema as a golden result *under `tests/fixtures/sessions/`*") adds nothing the sentence didn't already say, and bills every future move of that path an edit. A path whose whole job is "go read this" (`see docs/quota.md for the raw endpoint`) earns its keep. Canonical paths live here in CLAUDE.md and in the helper that implements them (`tests/common/mod.rs`'s `fixture()`), not scattered across call-site comments.
- The wrapper packages under `cli/nodejs/` and `cli/python/` ship the release binaries bundled in a `binaries/<platform>/` directory (populated by the release build) and just spawn the matching one; they're not built from source via `cargo`.
- Test layout follows [Rust Book ch11-03](https://doc.rust-lang.org/book/ch11-03-test-organization.html): unit tests inline in `src/<module>/*.rs` inside `#[cfg(test)] mod tests`; each file under `tests/` compiles to its own binary, and `tests/common/mod.rs` is the shared helper module (`TempHome`, `fixture`). The integration-test split is one-file-per-subsystem:
- `tests/cli.rs` — `assert_cmd`-driven checks of the built binary: clap-only wiring checks can inherit the test environment because they stop before runtime dispatch, while every single-file or batch command that can parse, log, access provider paths, or return a runtime error runs with an isolated per-child HOME, explicit Hermes home, cleared XDG overrides, and `VCT_OFFLINE=1`
- `tests/config.rs` — `config::load_in` / `save_merge_models_in` over a `TempHome`'s `~/.vct`: first-run generated-file creation, comment-preserving writes, legacy `quota_panels` migration, `default_time_range` precedence, and that an existing `version.json` is left untouched (not folded into `config.toml`)
- `tests/parser.rs` — golden-output comparison against `tests/fixtures/sessions/*.expected.json`, ignoring environment-specific fields (`insightsVersion`, `machineId`, `user`, `gitRemoteUrl`)
- `tests/analysis.rs` — rollup diagnostics and cached/uncached aggregation equivalence over a `TempHome`
- `tests/usage.rs` — usage diagnostics, active-day rules, and incremental cache invalidation over a `TempHome`
- `tests/pricing.rs` — deterministic `ModelPricingMap` matching, per-map cache isolation, tiered pricing math, and validated fetch/cache behavior against an `httpmock` server
- `tests/cache.rs` — LRU file cache + pricing cache invalidation
- `tests/http_mock.rs` — HTTP-layer tests of the public quota fetchers (`call_wham`, `refresh_codex`) against an `httpmock` server
- `tests/quota.rs` — Codex session-log quota fallback (`latest_session_rate_limits_in`) over a `TempHome` seeded with `codex_session_rate_limits.jsonl`
- **Tests are hermetic: no real external API, no machine-file reads, no ambient env control.** Isolation comes from dependency injection, not `HOME`/`VCT_OFFLINE` mutation: the `*_from_paths` / `resolve_paths_from_home` / `fetch_model_pricing_with` / cache `*_in` seams take an explicit temp dir (via `TempHome` in `tests/common`), and every network call is pointed at a local `httpmock` server through the injected endpoint parameters. The 401 → refresh → retry loop and each provider's send layer are covered by inline `#[cfg(test)]` tests in their source files (which can reach crate-private items). `VCT_OFFLINE` / `network_disabled()` remain a **production** offline feature but no test depends on them, so `cargo test` passes fully offline **without** any env var — the same way CI runs it. The only env used anywhere is a per-child `HOME` on the handful of `assert_cmd` smoke tests (there is no other way to isolate a separate binary's home). Keep tests self-contained and call `clear_pricing_cache()` before assertions that depend on a fresh cache generation.
- Test fixtures live under `tests/fixtures/`, **never** in `examples/` (Cargo reserves that directory for example targets, and this repo has none). `sessions/` holds one `<provider>.jsonl` input plus one `<provider>.expected.json` golden for each of the four JSONL providers; Grok's multi-file `signals.json` / `summary.json` / `updates.jsonl` fixture sits in `sessions/grok/` with its golden at `sessions/grok.expected.json`. `quota/` holds the raw quota API responses. Reach a fixture through `tests/common/mod.rs`'s `fixture()` / `fixture_str()`, which resolve against `CARGO_MANIFEST_DIR` and take a root-relative name (`sessions/codex.jsonl`), rather than a bare relative path that silently depends on the CWD; `benches/benchmarks.rs` can't use `tests/common`, so it carries its own equivalent helper. `scripts/test.sh` regenerates the four JSONL goldens (not Grok's). OpenCode, Cursor, and Hermes have no JSONL fixture; their SQLite readers are covered by inline unit tests under `src/session/` that build temp databases.