# foyzulkarim/claude-lens architecture

> Architecture diagram of the GitHub repository foyzulkarim/claude-lens, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/foyzulkarim/claude-lens
- Repository: https://github.com/foyzulkarim/claude-lens

## Overview

#### Purpose and Status
This repository is building Claude Lens, a local analytics/dashboard system for Claude Code transcript data: discover project logs, ingest incrementally, derive sessions/turns/token usage, and expose metrics to a planned interactive client. The previous runnable V1 dashboard is isolated under `legacy/`; the root codebase is the newer architecture under development.

#### Entry Points
- Server application assembly: `server/app.ts`; CLI process entry: `server/cli.ts`.
- Client is a Vite/React-style shell rooted at `client/index.html`, with current source at `client/src/placeholder.ts`.
- Repository build orchestration is in `scripts/build.ts`.
- Legacy V1 remains independently runnable via `legacy/server.js`, as stated in the root README.

#### Ingestion Pipeline
The ingestion subsystem converts filesystem transcript artifacts into normalized store inputs:
- Discovery locates candidate project/transcript files: `server/ingest/discovery.ts`.
- Transcript parsing reads source log content: `server/ingest/parse-transcript.ts`.
- Pipeline coordination applies parsing and downstream updates: `server/ingest/pipeline.ts`.
- Continuous update mechanisms support polling/tailing and initial cache warm-up: `server/ingest/poller.ts`, `server/ingest/tailer.ts`, `server/ingest/warm-cache.ts`.

The primary boundary is local transcript files versus internal typed records; fixture projects under `test/fixtures/projects/` represent the expected source shape.

#### State and Derived Domain Model
The store owns normalized/derived analytical state and change invalidation:
- Central store: `server/store/store.ts`.
- Session derivation: `server/store/derive-session.ts`.
- Turn derivation: `server/store/derive-turns.ts`.
- Dependency/change invalidation: `server/store/invalidation.ts`.
- Token accounting is separated in `server/store/token-usage.ts`.

This layer decouples raw ingestion from query-facing metrics, allowing file changes to update only affected derived views.

#### Metrics Engine
Metrics are computed over stored sessions and turns rather than directly over transcript files:
- Calculation orchestration: `server/metrics/engine.ts`.
- Dimension definitions/grouping: `server/metrics/dimensions.ts`.
- Measures and distributions: `server/metrics/measures.ts`, `server/metrics/distributions.ts`.
- Grain/time-resolution logic: `server/metrics/grain.ts`.
- Shared public metric shapes are defined in `shared/metrics-contract.ts`.

#### Client, API, and Live Updates
The intended server/client boundary is represented by shared contracts:
- General domain types: `shared/types.ts`.
- WebSocket protocol: `shared/ws-protocol.ts`.
- Server integration point: `server/app.ts`.
- Client shell and visual development setup: `client/src/placeholder.ts`, `client/.storybook/main.ts`.

Planning artifacts explicitly identify Fastify assembly, WebSocket invalidation, React shell, URL-synced filtering, and chart/dashboard stages in `specs/issues/P3-1-fastify-assembly-ws-invalidation-bus.md` through `specs/issues/P4-2-dashboard-page.md`.

#### Infrastructure and Legacy Boundary
The active implementation is TypeScript across `server/`, `shared/`, and `client/`, with Vite client configuration in `client/vite.config.ts`. CI is defined in `.github/workflows/ci.yml`. The historical Node-based dashboard, its dependencies, and environment example are intentionally contained in `legacy/server.js`, `legacy/package.json`, and `legacy/.env.example`; it should be treated as a separate compatibility/reference implementation rather than the new system’s core runtime.

Main entry points: [`server/metrics/engine.ts`](https://github.com/foyzulkarim/claude-lens/blob/main/server/metrics/engine.ts) (Metrics engine), [`server/store/store.ts`](https://github.com/foyzulkarim/claude-lens/blob/main/server/store/store.ts) (Central store), [`server/app.ts`](https://github.com/foyzulkarim/claude-lens/blob/main/server/app.ts) (Server application), [`server/ingest/discovery.ts`](https://github.com/foyzulkarim/claude-lens/blob/main/server/ingest/discovery.ts) (Discovery)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ingest["Ingestion"]
  node_transcript_files["Local transcript files<br/>filesystem source"]
  node_discovery["Discovery<br/>ingest stage<br/>[discovery.ts]"]
  node_parser["Transcript parser<br/>ingest stage"]
  node_pipeline["Ingestion pipeline<br/>coordinator<br/>[pipeline.ts]"]
  node_file_updates["Polling, tailing and warm-up<br/>update mechanisms<br/>[poller.ts]"]
end

subgraph group_state["Analytical State"]
  node_store[("Central store<br/>analytical store<br/>[store.ts]")]
  node_session_derivation["Session derivation<br/>derived model<br/>[derive-session.ts]"]
  node_turn_derivation["Turn derivation<br/>derived model<br/>[derive-turns.ts]"]
  node_token_usage["Token accounting<br/>derived model<br/>[token-usage.ts]"]
  node_invalidation["Change invalidation<br/>dependency tracking<br/>[invalidation.ts]"]
end

subgraph group_metrics["Metrics"]
  node_metrics_engine["Metrics engine<br/>query engine<br/>[engine.ts]"]
  node_metric_dimensions["Dimensions and grain<br/>metric semantics<br/>[dimensions.ts]"]
  node_metric_measures["Measures and distributions<br/>metric semantics<br/>[measures.ts]"]
  node_metric_contract["Metrics contract<br/>shared contract"]
end

subgraph group_delivery["Server and Client"]
  node_server_app["Server application<br/>application assembly<br/>[app.ts]"]
  node_shared_protocols["Domain and WS contracts<br/>shared contracts<br/>[ws-protocol.ts]"]
  node_client_shell["React/Vite client shell<br/>web client<br/>[placeholder.ts]"]
end

subgraph group_legacy["Legacy V1"]
  node_legacy_dashboard["Legacy V1 dashboard<br/>Node dashboard<br/>[server.js]"]
end

node_transcript_files -->|"scan"| node_discovery
node_discovery -->|"candidate files"| node_pipeline
node_file_updates -->|"initial and changed files"| node_discovery
node_discovery -->|"transcript paths"| node_parser
node_parser -->|"typed records"| node_pipeline
node_pipeline -->|"normalized updates"| node_store
node_store -->|"derive"| node_session_derivation
node_store -->|"derive"| node_turn_derivation
node_store -->|"account"| node_token_usage
node_invalidation -->|"invalidate affected views"| node_store
node_session_derivation -->|"sessions"| node_metrics_engine
node_turn_derivation -->|"turns"| node_metrics_engine
node_token_usage -->|"usage"| node_metrics_engine
node_metric_dimensions -->|"grouping and grain"| node_metrics_engine
node_metric_measures -->|"calculations"| node_metrics_engine
node_metrics_engine -->|"metric results"| node_metric_contract
node_server_app -->|"runs ingestion"| node_pipeline
node_server_app -->|"queries"| node_metrics_engine
node_shared_protocols -->|"API and WS types"| node_server_app
node_shared_protocols -->|"API and WS types"| node_client_shell
node_metric_contract -->|"metric shapes"| node_client_shell
node_server_app -->|"metrics and live invalidation"| node_client_shell

click node_transcript_files "https://github.com/foyzulkarim/claude-lens/tree/main/test/fixtures/projects"
click node_discovery "https://github.com/foyzulkarim/claude-lens/blob/main/server/ingest/discovery.ts"
click node_parser "https://github.com/foyzulkarim/claude-lens/blob/main/server/ingest/parse-transcript.ts"
click node_pipeline "https://github.com/foyzulkarim/claude-lens/blob/main/server/ingest/pipeline.ts"
click node_file_updates "https://github.com/foyzulkarim/claude-lens/blob/main/server/ingest/poller.ts"
click node_store "https://github.com/foyzulkarim/claude-lens/blob/main/server/store/store.ts"
click node_session_derivation "https://github.com/foyzulkarim/claude-lens/blob/main/server/store/derive-session.ts"
click node_turn_derivation "https://github.com/foyzulkarim/claude-lens/blob/main/server/store/derive-turns.ts"
click node_token_usage "https://github.com/foyzulkarim/claude-lens/blob/main/server/store/token-usage.ts"
click node_invalidation "https://github.com/foyzulkarim/claude-lens/blob/main/server/store/invalidation.ts"
click node_metrics_engine "https://github.com/foyzulkarim/claude-lens/blob/main/server/metrics/engine.ts"
click node_metric_dimensions "https://github.com/foyzulkarim/claude-lens/blob/main/server/metrics/dimensions.ts"
click node_metric_measures "https://github.com/foyzulkarim/claude-lens/blob/main/server/metrics/measures.ts"
click node_metric_contract "https://github.com/foyzulkarim/claude-lens/blob/main/shared/metrics-contract.ts"
click node_server_app "https://github.com/foyzulkarim/claude-lens/blob/main/server/app.ts"
click node_shared_protocols "https://github.com/foyzulkarim/claude-lens/blob/main/shared/ws-protocol.ts"
click node_client_shell "https://github.com/foyzulkarim/claude-lens/blob/main/client/src/placeholder.ts"
click node_legacy_dashboard "https://github.com/foyzulkarim/claude-lens/blob/main/legacy/server.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_transcript_files,node_discovery,node_parser,node_pipeline,node_file_updates toneBlue
class node_store,node_session_derivation,node_turn_derivation,node_token_usage,node_invalidation toneAmber
class node_metrics_engine,node_metric_dimensions,node_metric_measures,node_metric_contract toneMint
class node_server_app,node_shared_protocols,node_client_shell toneRose
class node_legacy_dashboard toneIndigo
```

## Components

### Ingestion

Converts local Claude Code artifacts into typed updates.

- [Local transcript files](https://github.com/foyzulkarim/claude-lens/tree/main/test/fixtures/projects): filesystem source · Claude Code project logs discovered on the local machine. · `test/fixtures/projects`
- [Discovery](https://github.com/foyzulkarim/claude-lens/blob/main/server/ingest/discovery.ts): ingest stage · Locates candidate project and transcript files. · `server/ingest/discovery.ts`
- [Transcript parser](https://github.com/foyzulkarim/claude-lens/blob/main/server/ingest/parse-transcript.ts): ingest stage · Reads transcript content into typed records. · `server/ingest/parse-transcript.ts`
- [Ingestion pipeline](https://github.com/foyzulkarim/claude-lens/blob/main/server/ingest/pipeline.ts): coordinator · Applies parsed records and downstream updates. · `server/ingest/pipeline.ts`
- [Polling, tailing and warm-up](https://github.com/foyzulkarim/claude-lens/blob/main/server/ingest/poller.ts): update mechanisms · Performs initial cache warm-up and continuous file change detection. · `server/ingest/poller.ts`

### Analytical State

Maintains normalized records and invalidates affected derivations.

- [Central store](https://github.com/foyzulkarim/claude-lens/blob/main/server/store/store.ts): analytical store · Owns normalized and derived analytical state. · `server/store/store.ts`
- [Session derivation](https://github.com/foyzulkarim/claude-lens/blob/main/server/store/derive-session.ts): derived model · `server/store/derive-session.ts`
- [Turn derivation](https://github.com/foyzulkarim/claude-lens/blob/main/server/store/derive-turns.ts): derived model · `server/store/derive-turns.ts`
- [Token accounting](https://github.com/foyzulkarim/claude-lens/blob/main/server/store/token-usage.ts): derived model · `server/store/token-usage.ts`
- [Change invalidation](https://github.com/foyzulkarim/claude-lens/blob/main/server/store/invalidation.ts): dependency tracking · Limits recalculation to affected derived views. · `server/store/invalidation.ts`

### Metrics

Computes query-facing analytics from stored sessions and turns.

- [Metrics engine](https://github.com/foyzulkarim/claude-lens/blob/main/server/metrics/engine.ts): query engine · Calculates metrics over stored sessions and turns. · `server/metrics/engine.ts`
- [Dimensions and grain](https://github.com/foyzulkarim/claude-lens/blob/main/server/metrics/dimensions.ts): metric semantics · Defines grouping and time-resolution logic. · `server/metrics/dimensions.ts`
- [Measures and distributions](https://github.com/foyzulkarim/claude-lens/blob/main/server/metrics/measures.ts): metric semantics · `server/metrics/measures.ts`
- [Metrics contract](https://github.com/foyzulkarim/claude-lens/blob/main/shared/metrics-contract.ts): shared contract · Public metric shapes shared across the application boundary. · `shared/metrics-contract.ts`

### Server and Client

Planned interactive application boundary.

- [Server application](https://github.com/foyzulkarim/claude-lens/blob/main/server/app.ts): application assembly · Integrates ingest, state, metrics, API, and live updates. · `server/app.ts`
- [Domain and WS contracts](https://github.com/foyzulkarim/claude-lens/blob/main/shared/ws-protocol.ts): shared contracts · Defines general domain types and WebSocket protocol. · `shared/ws-protocol.ts`
- [React/Vite client shell](https://github.com/foyzulkarim/claude-lens/blob/main/client/src/placeholder.ts): web client · Current client shell for planned dashboard and filtering UI. · `client/src/placeholder.ts`

### Legacy V1

Separate runnable reference dashboard.

- [Legacy V1 dashboard](https://github.com/foyzulkarim/claude-lens/blob/main/legacy/server.js): Node dashboard · Independently runnable compatibility/reference implementation. · `legacy/server.js`

## Connections

- Local transcript files → Discovery: scan
- Discovery → Ingestion pipeline: candidate files
- Polling, tailing and warm-up → Discovery: initial and changed files
- Discovery → Transcript parser: transcript paths
- Transcript parser → Ingestion pipeline: typed records
- Ingestion pipeline → Central store: normalized updates
- Central store → Session derivation: derive
- Central store → Turn derivation: derive
- Central store → Token accounting: account
- Change invalidation → Central store: invalidate affected views
- Session derivation → Metrics engine: sessions
- Turn derivation → Metrics engine: turns
- Token accounting → Metrics engine: usage
- Dimensions and grain → Metrics engine: grouping and grain
- Measures and distributions → Metrics engine: calculations
- Metrics engine → Metrics contract: metric results
- Server application → Ingestion pipeline: runs ingestion
- Server application → Metrics engine: queries
- Domain and WS contracts → Server application: API and WS types
- Domain and WS contracts → React/Vite client shell: API and WS types
- Metrics contract → React/Vite client shell: metric shapes
- Server application → React/Vite client shell: metrics and live invalidation
