zozul-cli 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +44 -0
- package/.github/workflows/publish.yml +26 -0
- package/DEVELOPMENT.md +288 -0
- package/LICENSE +201 -0
- package/README.md +178 -0
- package/dist/cli/commands.d.ts +3 -0
- package/dist/cli/commands.d.ts.map +1 -0
- package/dist/cli/commands.js +307 -0
- package/dist/cli/commands.js.map +1 -0
- package/dist/cli/format.d.ts +5 -0
- package/dist/cli/format.d.ts.map +1 -0
- package/dist/cli/format.js +115 -0
- package/dist/cli/format.js.map +1 -0
- package/dist/context/index.d.ts +8 -0
- package/dist/context/index.d.ts.map +1 -0
- package/dist/context/index.js +37 -0
- package/dist/context/index.js.map +1 -0
- package/dist/dashboard/html.d.ts +17 -0
- package/dist/dashboard/html.d.ts.map +1 -0
- package/dist/dashboard/html.js +79 -0
- package/dist/dashboard/html.js.map +1 -0
- package/dist/dashboard/index.html +1245 -0
- package/dist/hooks/config.d.ts +19 -0
- package/dist/hooks/config.d.ts.map +1 -0
- package/dist/hooks/config.js +106 -0
- package/dist/hooks/config.js.map +1 -0
- package/dist/hooks/git.d.ts +6 -0
- package/dist/hooks/git.d.ts.map +1 -0
- package/dist/hooks/git.js +73 -0
- package/dist/hooks/git.js.map +1 -0
- package/dist/hooks/index.d.ts +4 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +3 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/server.d.ts +16 -0
- package/dist/hooks/server.d.ts.map +1 -0
- package/dist/hooks/server.js +349 -0
- package/dist/hooks/server.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/otel/config.d.ts +36 -0
- package/dist/otel/config.d.ts.map +1 -0
- package/dist/otel/config.js +109 -0
- package/dist/otel/config.js.map +1 -0
- package/dist/otel/index.d.ts +4 -0
- package/dist/otel/index.d.ts.map +1 -0
- package/dist/otel/index.js +3 -0
- package/dist/otel/index.js.map +1 -0
- package/dist/otel/receiver.d.ts +10 -0
- package/dist/otel/receiver.d.ts.map +1 -0
- package/dist/otel/receiver.js +155 -0
- package/dist/otel/receiver.js.map +1 -0
- package/dist/parser/index.d.ts +4 -0
- package/dist/parser/index.d.ts.map +1 -0
- package/dist/parser/index.js +3 -0
- package/dist/parser/index.js.map +1 -0
- package/dist/parser/ingest.d.ts +20 -0
- package/dist/parser/ingest.d.ts.map +1 -0
- package/dist/parser/ingest.js +98 -0
- package/dist/parser/ingest.js.map +1 -0
- package/dist/parser/jsonl.d.ts +14 -0
- package/dist/parser/jsonl.d.ts.map +1 -0
- package/dist/parser/jsonl.js +202 -0
- package/dist/parser/jsonl.js.map +1 -0
- package/dist/parser/types.d.ts +81 -0
- package/dist/parser/types.d.ts.map +1 -0
- package/dist/parser/types.js +9 -0
- package/dist/parser/types.js.map +1 -0
- package/dist/parser/watcher.d.ts +16 -0
- package/dist/parser/watcher.d.ts.map +1 -0
- package/dist/parser/watcher.js +103 -0
- package/dist/parser/watcher.js.map +1 -0
- package/dist/pricing/index.d.ts +2 -0
- package/dist/pricing/index.d.ts.map +1 -0
- package/dist/pricing/index.js +37 -0
- package/dist/pricing/index.js.map +1 -0
- package/dist/service/index.d.ts +31 -0
- package/dist/service/index.d.ts.map +1 -0
- package/dist/service/index.js +252 -0
- package/dist/service/index.js.map +1 -0
- package/dist/storage/db.d.ts +75 -0
- package/dist/storage/db.d.ts.map +1 -0
- package/dist/storage/db.js +117 -0
- package/dist/storage/db.js.map +1 -0
- package/dist/storage/index.d.ts +4 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +3 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/repo.d.ts +162 -0
- package/dist/storage/repo.d.ts.map +1 -0
- package/dist/storage/repo.js +472 -0
- package/dist/storage/repo.js.map +1 -0
- package/dist/sync/client.d.ts +24 -0
- package/dist/sync/client.d.ts.map +1 -0
- package/dist/sync/client.js +41 -0
- package/dist/sync/client.js.map +1 -0
- package/dist/sync/index.d.ts +18 -0
- package/dist/sync/index.d.ts.map +1 -0
- package/dist/sync/index.js +135 -0
- package/dist/sync/index.js.map +1 -0
- package/dist/sync/sync.test.d.ts +2 -0
- package/dist/sync/sync.test.d.ts.map +1 -0
- package/dist/sync/sync.test.js +412 -0
- package/dist/sync/sync.test.js.map +1 -0
- package/dist/sync/transform.d.ts +80 -0
- package/dist/sync/transform.d.ts.map +1 -0
- package/dist/sync/transform.js +90 -0
- package/dist/sync/transform.js.map +1 -0
- package/package.json +50 -0
- package/src/cli/commands.ts +332 -0
- package/src/cli/format.ts +133 -0
- package/src/context/index.ts +42 -0
- package/src/dashboard/html.ts +97 -0
- package/src/dashboard/index.html +1245 -0
- package/src/hooks/config.ts +119 -0
- package/src/hooks/git.ts +77 -0
- package/src/hooks/index.ts +7 -0
- package/src/hooks/server.ts +397 -0
- package/src/index.ts +6 -0
- package/src/otel/config.ts +141 -0
- package/src/otel/index.ts +8 -0
- package/src/otel/receiver.ts +183 -0
- package/src/parser/index.ts +3 -0
- package/src/parser/ingest.ts +119 -0
- package/src/parser/jsonl.ts +241 -0
- package/src/parser/types.ts +89 -0
- package/src/parser/watcher.ts +116 -0
- package/src/pricing/index.ts +51 -0
- package/src/service/index.ts +272 -0
- package/src/storage/db.ts +198 -0
- package/src/storage/index.ts +3 -0
- package/src/storage/repo.ts +601 -0
- package/src/sync/client.ts +63 -0
- package/src/sync/index.ts +207 -0
- package/src/sync/sync.test.ts +447 -0
- package/src/sync/transform.ts +184 -0
- package/tsconfig.json +19 -0
package/.env.example
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# ─── zozul-cli configuration ───
|
|
2
|
+
|
|
3
|
+
# Port the zozul server listens on (hooks, OTEL receiver, API, dashboard)
|
|
4
|
+
ZOZUL_PORT=7890
|
|
5
|
+
|
|
6
|
+
# Path to the SQLite database (default: ~/.zozul/zozul.db)
|
|
7
|
+
# ZOZUL_DB_PATH=~/.zozul/zozul.db
|
|
8
|
+
|
|
9
|
+
# Enable verbose logging of hook/OTEL events to stderr (default: false)
|
|
10
|
+
ZOZUL_VERBOSE=false
|
|
11
|
+
|
|
12
|
+
# ─── Claude Code OTEL configuration ───
|
|
13
|
+
# These are written to ~/.claude/settings.json by `zozul install`.
|
|
14
|
+
# Claude Code sends OTEL data directly to zozul (no external collector needed).
|
|
15
|
+
|
|
16
|
+
# OTLP endpoint (points at zozul itself)
|
|
17
|
+
OTEL_ENDPOINT=http://localhost:7890
|
|
18
|
+
|
|
19
|
+
# OTLP protocol (must be http/json for zozul's built-in receiver)
|
|
20
|
+
OTEL_PROTOCOL=http/json
|
|
21
|
+
|
|
22
|
+
# Log full user prompt text in OTEL events (default: true)
|
|
23
|
+
OTEL_LOG_USER_PROMPTS=1
|
|
24
|
+
|
|
25
|
+
# Log MCP server/tool names in OTEL events (default: true)
|
|
26
|
+
OTEL_LOG_TOOL_DETAILS=1
|
|
27
|
+
|
|
28
|
+
# Metrics export interval in ms (default: 60000)
|
|
29
|
+
# OTEL_METRIC_EXPORT_INTERVAL=60000
|
|
30
|
+
|
|
31
|
+
# Logs export interval in ms (default: 5000)
|
|
32
|
+
# OTEL_LOGS_EXPORT_INTERVAL=5000
|
|
33
|
+
|
|
34
|
+
# Optional OTLP auth header (e.g. Authorization=Bearer your-token)
|
|
35
|
+
# OTEL_AUTH_HEADER=
|
|
36
|
+
|
|
37
|
+
# ─── Remote sync configuration ───
|
|
38
|
+
# Required for 'zozul sync'. Set these to push local data to a remote backend.
|
|
39
|
+
|
|
40
|
+
# Base URL of the zozul backend API
|
|
41
|
+
# ZOZUL_API_URL=https://api.zozul.dev
|
|
42
|
+
|
|
43
|
+
# API key for authenticating with the backend (sent as X-API-Key header)
|
|
44
|
+
# ZOZUL_API_KEY=your-api-key-here
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
name: Publish to npm
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
steps:
|
|
11
|
+
- uses: actions/checkout@v4
|
|
12
|
+
|
|
13
|
+
- uses: actions/setup-node@v4
|
|
14
|
+
with:
|
|
15
|
+
node-version: 22
|
|
16
|
+
registry-url: https://registry.npmjs.org
|
|
17
|
+
|
|
18
|
+
- run: npm ci
|
|
19
|
+
|
|
20
|
+
- run: npm run build
|
|
21
|
+
|
|
22
|
+
- run: npm test
|
|
23
|
+
|
|
24
|
+
- run: npm publish --access public
|
|
25
|
+
env:
|
|
26
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/DEVELOPMENT.md
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
# Development & Agent Context
|
|
2
|
+
|
|
3
|
+
This document is the running reference for anyone (human or agent) picking up work on this codebase. It captures non-obvious design decisions, current state, known issues, and how to orient yourself quickly.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Project overview
|
|
8
|
+
|
|
9
|
+
zozul-cli is a local observability sidecar for Claude Code. It ingests data from three sources (OTEL, hooks, JSONL), stores everything in SQLite, and serves a dashboard + JSON API. Single process, no external services.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Source layout
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
src/
|
|
17
|
+
index.ts Entry point — loads .env, runs CLI
|
|
18
|
+
cli/
|
|
19
|
+
commands.ts All CLI commands (serve, install, ingest, etc.)
|
|
20
|
+
format.ts Terminal output formatters
|
|
21
|
+
storage/
|
|
22
|
+
db.ts SQLite setup, schema migration, row types
|
|
23
|
+
repo.ts All DB queries — SessionRepo class
|
|
24
|
+
hooks/
|
|
25
|
+
server.ts HTTP server: hook handler, API routes, OTEL receiver dispatch
|
|
26
|
+
config.ts Read/write ~/.claude/settings.json for hooks
|
|
27
|
+
otel/
|
|
28
|
+
receiver.ts Parse OTLP JSON payloads → DB + session accumulation
|
|
29
|
+
config.ts Read/write ~/.claude/settings.json for OTEL env vars
|
|
30
|
+
parser/
|
|
31
|
+
jsonl.ts Discover and parse Claude Code session JSONL files
|
|
32
|
+
ingest.ts Persist parsed sessions/turns/tool_uses to DB
|
|
33
|
+
watcher.ts fs.watch on ~/.claude/projects, debounced ingest
|
|
34
|
+
types.ts JSONL type definitions
|
|
35
|
+
dashboard/
|
|
36
|
+
index.html Dashboard SPA (vanilla JS, Chart.js from CDN)
|
|
37
|
+
html.ts Reads and serves index.html (thin wrapper)
|
|
38
|
+
service/
|
|
39
|
+
index.ts Install/uninstall as launchd (macOS) or systemd (Linux) service
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Build output goes to `dist/`. The build script also copies `src/dashboard/index.html` → `dist/dashboard/index.html`.
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## Data flow
|
|
47
|
+
|
|
48
|
+
### OTEL (metrics + logs)
|
|
49
|
+
|
|
50
|
+
Claude Code sends OTLP JSON to `POST /v1/metrics` and `POST /v1/logs` on a configurable interval (default: 60s metrics, 5s logs).
|
|
51
|
+
|
|
52
|
+
**Important**: values are **deltas per export window**, not cumulative totals. Each batch represents tokens/cost/time accrued since the last export.
|
|
53
|
+
|
|
54
|
+
Flow:
|
|
55
|
+
1. `hooks/server.ts` receives POST, reads body (50MB cap)
|
|
56
|
+
2. Calls `handleOtlpMetrics` / `handleOtlpLogs` in `otel/receiver.ts`
|
|
57
|
+
3. Receiver builds a flat batch, inserts into `otel_metrics` / `otel_events`
|
|
58
|
+
4. Receiver also aggregates per-session deltas from the batch and calls `repo.updateSessionFromOtel()` for each session seen
|
|
59
|
+
5. `updateSessionFromOtel` does an accumulating UPSERT: `total_cost_usd += delta`, `total_*_tokens += delta`, `total_duration_ms += delta`, `ended_at = MAX(...)`
|
|
60
|
+
|
|
61
|
+
Key metric names from Claude Code:
|
|
62
|
+
- `claude_code.token.usage` — attribute `type` is `input` / `output` / `cacheRead` / `cacheCreation`
|
|
63
|
+
- `claude_code.cost.usage` — USD, no type attribute
|
|
64
|
+
- `claude_code.active_time.total` — seconds of active use
|
|
65
|
+
- `claude_code.session.count`, `claude_code.lines_of_code.count`, etc.
|
|
66
|
+
|
|
67
|
+
### Hooks
|
|
68
|
+
|
|
69
|
+
Claude Code POSTs to `/hook/<event>` synchronously as events happen. The hook handler:
|
|
70
|
+
1. Parses JSON body
|
|
71
|
+
2. Deduplicates `SessionEnd` events within 60s for the same session (Claude Code sometimes fires two)
|
|
72
|
+
3. Inserts into `hook_events`
|
|
73
|
+
4. On `SessionEnd`: decodes project path from `transcript_path`, calls `ingestSessionFile`
|
|
74
|
+
|
|
75
|
+
Hook event names: `SessionStart`, `SessionEnd`, `Stop`, `UserPromptSubmit`, `PostToolUse`, `PreToolUse`, `Notification`
|
|
76
|
+
|
|
77
|
+
### JSONL
|
|
78
|
+
|
|
79
|
+
Claude Code writes session transcripts to:
|
|
80
|
+
```
|
|
81
|
+
~/.claude/projects/<encoded-project-path>/<session-uuid>.jsonl
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
The encoded path replaces `/` with `-` (lossy — hyphens in directory names become `/` when decoded). Session UUID is used as the session ID everywhere.
|
|
85
|
+
|
|
86
|
+
Each line in the file is one JSON entry. Entries have `message.role`, `message.content`, `message.usage`, `costUSD`, `durationMs`. Note: `costUSD` is often `0` in practice — OTEL is the reliable cost source.
|
|
87
|
+
|
|
88
|
+
**Discovery**: `discoverSessionFiles()` in `parser/jsonl.ts` scans `~/.claude/projects/*/` for UUID-named `.jsonl` files (regex: `/^[0-9a-f]{8}-...-[0-9a-f]{12}\.jsonl$/i`). It does NOT look in `sessions/` subdirectories — the directory structure changed in Claude Code 2.x.
|
|
89
|
+
|
|
90
|
+
**Watcher**: `watchSessionFiles()` in `parser/watcher.ts` runs on `zozul serve`. It does a catch-up ingest of all files on startup, then watches for changes using `fs.watch({ recursive: true })`. Each file change is debounced 500ms before calling `ingestSessionFile`.
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
## Database schema
|
|
95
|
+
|
|
96
|
+
Six tables, all in `~/.zozul/zozul.db` (WAL mode, foreign keys ON).
|
|
97
|
+
|
|
98
|
+
### `sessions`
|
|
99
|
+
One row per Claude Code session. Updated from both JSONL and OTEL.
|
|
100
|
+
|
|
101
|
+
```sql
|
|
102
|
+
id TEXT PRIMARY KEY -- session UUID (from JSONL filename)
|
|
103
|
+
project_path TEXT -- decoded from transcript path
|
|
104
|
+
started_at TEXT NOT NULL -- ISO timestamp, from JSONL
|
|
105
|
+
ended_at TEXT -- kept current by OTEL batches
|
|
106
|
+
total_input_tokens INTEGER -- OTEL-accumulated (MAX with JSONL)
|
|
107
|
+
total_output_tokens INTEGER
|
|
108
|
+
total_cache_read_tokens INTEGER
|
|
109
|
+
total_cache_creation_tokens INTEGER
|
|
110
|
+
total_cost_usd REAL -- OTEL only (JSONL always 0)
|
|
111
|
+
total_turns INTEGER -- from JSONL (count of parsed turns)
|
|
112
|
+
total_duration_ms INTEGER -- OTEL active_time accumulated
|
|
113
|
+
model TEXT -- last model seen
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Upsert semantics: `MIN(started_at)`, `MAX()` for all metric fields, `COALESCE` for nullable strings. This means re-ingesting from JSONL never destroys OTEL-accumulated cost/duration.
|
|
117
|
+
|
|
118
|
+
### `turns`
|
|
119
|
+
One row per message turn. Unique on `(session_id, turn_index)`.
|
|
120
|
+
|
|
121
|
+
```sql
|
|
122
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT
|
|
123
|
+
session_id TEXT → sessions(id)
|
|
124
|
+
turn_index INTEGER
|
|
125
|
+
role TEXT -- 'user' or 'assistant'
|
|
126
|
+
timestamp TEXT
|
|
127
|
+
input_tokens / output_tokens / cache_* INTEGER
|
|
128
|
+
cost_usd REAL -- often 0, from JSONL
|
|
129
|
+
duration_ms INTEGER
|
|
130
|
+
model TEXT
|
|
131
|
+
content_text TEXT -- full message text
|
|
132
|
+
tool_calls TEXT -- JSON array of {toolName, toolInput, toolResult}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### `tool_uses`
|
|
136
|
+
Extracted tool calls, one row per tool invocation.
|
|
137
|
+
|
|
138
|
+
```sql
|
|
139
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT
|
|
140
|
+
session_id TEXT → sessions(id)
|
|
141
|
+
turn_id INTEGER → turns(id) -- nullable
|
|
142
|
+
tool_name TEXT
|
|
143
|
+
tool_input TEXT -- JSON
|
|
144
|
+
tool_result TEXT
|
|
145
|
+
success INTEGER -- null (not yet populated)
|
|
146
|
+
duration_ms INTEGER -- 0 (not yet populated)
|
|
147
|
+
timestamp TEXT
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Re-ingest is safe: `persistSession` calls `replaceToolUsesForTurn(turnId, ...)` which deletes existing rows for the turn before inserting — no duplication.
|
|
151
|
+
|
|
152
|
+
### `hook_events`
|
|
153
|
+
Raw hook payloads, append-only.
|
|
154
|
+
|
|
155
|
+
```sql
|
|
156
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT
|
|
157
|
+
session_id TEXT -- nullable (some hooks lack it)
|
|
158
|
+
event_name TEXT
|
|
159
|
+
timestamp TEXT
|
|
160
|
+
payload TEXT -- full JSON body
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### `otel_metrics`
|
|
164
|
+
Raw OTEL metric data points, append-only.
|
|
165
|
+
|
|
166
|
+
```sql
|
|
167
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT
|
|
168
|
+
name TEXT -- e.g. 'claude_code.token.usage'
|
|
169
|
+
value REAL
|
|
170
|
+
attributes TEXT -- full JSON of flattened OTLP attributes
|
|
171
|
+
session_id TEXT -- extracted from attributes
|
|
172
|
+
model TEXT -- extracted from attributes
|
|
173
|
+
timestamp TEXT
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Dashboard charts query this table directly (not `sessions`) for time-series data.
|
|
177
|
+
|
|
178
|
+
### `otel_events`
|
|
179
|
+
Raw OTEL log records, append-only.
|
|
180
|
+
|
|
181
|
+
```sql
|
|
182
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT
|
|
183
|
+
event_name TEXT
|
|
184
|
+
attributes TEXT -- full JSON
|
|
185
|
+
session_id TEXT
|
|
186
|
+
prompt_id TEXT
|
|
187
|
+
timestamp TEXT
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
---
|
|
191
|
+
|
|
192
|
+
## Key design decisions
|
|
193
|
+
|
|
194
|
+
**OTEL is the authoritative source for cost and duration.** JSONL transcripts do not reliably contain `costUSD` (it's often 0 in practice). `total_cost_usd` in `sessions` is always populated by OTEL accumulation.
|
|
195
|
+
|
|
196
|
+
**JSONL is the only source for full conversation text.** OTEL events can include prompt text when `OTEL_LOG_USER_PROMPTS=1`, but the assistant's full response is only in the JSONL transcript.
|
|
197
|
+
|
|
198
|
+
**Sessions table uses MAX() semantics on upsert.** This means the highest value seen from any source wins. OTEL running totals always exceed JSONL partial values, so OTEL naturally wins for metrics without any special logic.
|
|
199
|
+
|
|
200
|
+
**OTEL deltas are accumulated, not replaced.** `updateSessionFromOtel` does `total_cost_usd = total_cost_usd + @cost`. This is correct because OTEL sends deltas per window. Do not change this to an assignment.
|
|
201
|
+
|
|
202
|
+
**Tool uses are replaced, not appended, on re-ingest.** `replaceToolUsesForTurn` deletes then re-inserts. This prevents duplication when the watcher re-ingests a live session file on every new turn.
|
|
203
|
+
|
|
204
|
+
**`insertTurn` uses `RETURNING id`.** On `ON CONFLICT DO UPDATE`, `lastInsertRowid` returns 0. Using `RETURNING id` gives the correct turn ID for the FK on `tool_uses`.
|
|
205
|
+
|
|
206
|
+
**JSONL file paths use lossy encoding.** Claude Code replaces `/` with `-` in project directory names. `decodeProjectPath` reverses this, but hyphens in original paths become `/`. This is unavoidable with Claude Code's current encoding scheme.
|
|
207
|
+
|
|
208
|
+
---
|
|
209
|
+
|
|
210
|
+
## Session lifecycle
|
|
211
|
+
|
|
212
|
+
```
|
|
213
|
+
Claude Code starts
|
|
214
|
+
→ SessionStart hook fires → hook_events INSERT
|
|
215
|
+
→ OTEL export #1 arrives (within 60s) → otel_metrics INSERT + sessions UPSERT (stub or update)
|
|
216
|
+
→ turns happen → JSONL file grows → watcher fires → ingestSessionFile → sessions/turns/tool_uses UPSERT
|
|
217
|
+
→ OTEL exports keep arriving every 60s → cost/duration accumulate in sessions
|
|
218
|
+
→ User stops Claude → Stop hook fires
|
|
219
|
+
→ SessionEnd hook fires → ingestSessionFile called with transcript_path → final JSONL state ingested
|
|
220
|
+
→ OTEL export continues until session ID stops appearing in batches
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
**Late-start behaviour**: if zozul wasn't running when Claude started, the watcher's catch-up pass immediately ingests all existing JSONL files. OTEL data from before zozul started is lost (no replay). The first OTEL batch after zozul starts will include cumulative cost for that window, partially recovering missed cost data.
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
## API routes
|
|
228
|
+
|
|
229
|
+
All served by `hooks/server.ts` on port 7890.
|
|
230
|
+
|
|
231
|
+
| Method | Path | Description |
|
|
232
|
+
|---|---|---|
|
|
233
|
+
| GET | `/dashboard` | Dashboard HTML |
|
|
234
|
+
| GET | `/api/stats` | Aggregate stats — sessions, tokens, cost, user prompts, interruptions |
|
|
235
|
+
| GET | `/api/sessions` | Paginated session list — returns `{ sessions, total, limit, offset }` |
|
|
236
|
+
| GET | `/api/sessions/:id` | Single session row |
|
|
237
|
+
| GET | `/api/sessions/:id/turns` | Turns for a session |
|
|
238
|
+
| GET | `/api/metrics/tokens` | Token time-series from `otel_metrics` |
|
|
239
|
+
| GET | `/api/metrics/cost` | Cost time-series from `otel_metrics` |
|
|
240
|
+
| GET | `/api/metrics/tools` | Tool usage breakdown from `tool_uses` |
|
|
241
|
+
| GET | `/api/metrics/models` | Per-model cost/token breakdown from `otel_metrics` |
|
|
242
|
+
| POST | `/v1/metrics` | OTLP metrics receiver |
|
|
243
|
+
| POST | `/v1/logs` | OTLP logs receiver |
|
|
244
|
+
| POST | `/hook/:event` | Claude Code hook receiver |
|
|
245
|
+
|
|
246
|
+
`/api/sessions` accepts `?limit=N` (default 50, max 500) and `?offset=N`. The response envelope `{ sessions, total, limit, offset }` lets the dashboard implement Load More without a separate count query.
|
|
247
|
+
|
|
248
|
+
Time-series endpoints accept `?range=7d`, `?range=24h`, or `?from=ISO&to=ISO&step=5m`. Step auto-selects based on range if omitted.
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
|
|
252
|
+
## Background service
|
|
253
|
+
|
|
254
|
+
`zozul install --service` writes a platform service file and loads it immediately:
|
|
255
|
+
|
|
256
|
+
- **macOS**: `~/Library/LaunchAgents/com.zozul.serve.plist` — loaded with `launchctl bootstrap gui/<uid>`
|
|
257
|
+
- **Linux**: `~/.config/systemd/user/zozul.service` — enabled with `systemctl --user enable --now`
|
|
258
|
+
|
|
259
|
+
The service file bakes in the absolute paths to the node binary (`process.execPath`) and the script (`process.argv[1]` resolved). This makes it nvm-safe but means you need to re-run `zozul install --service` if you upgrade node or move the project.
|
|
260
|
+
|
|
261
|
+
`zozul restart` kills and immediately relaunches the running service (`launchctl kickstart -k` on macOS, `systemctl --user restart` on Linux). Use this after `npm run build` to pick up code changes without reinstalling.
|
|
262
|
+
|
|
263
|
+
Logs: `~/.zozul/zozul.log`
|
|
264
|
+
|
|
265
|
+
---
|
|
266
|
+
|
|
267
|
+
## Known limitations
|
|
268
|
+
|
|
269
|
+
- **No schema migrations**: `db.ts` uses `CREATE TABLE IF NOT EXISTS`. Adding columns to existing tables requires manual SQL or a proper migration system.
|
|
270
|
+
- **JSONL path decoding is lossy**: hyphens in project directory names decode incorrectly. No fix without changes to Claude Code.
|
|
271
|
+
- **OTEL cost history is unrecoverable**: if zozul wasn't running during a session, cost data for that period is permanently lost.
|
|
272
|
+
- **`tool_uses.success` and `tool_uses.duration_ms` are never populated**: the schema has these columns but nothing writes to them yet.
|
|
273
|
+
- **Session filter is client-side only**: the filter input searches loaded sessions; Load More fetches additional pages.
|
|
274
|
+
|
|
275
|
+
---
|
|
276
|
+
|
|
277
|
+
## Dev workflow
|
|
278
|
+
|
|
279
|
+
```bash
|
|
280
|
+
npm install # Install dependencies
|
|
281
|
+
npm run dev # Run via tsx (no build step needed)
|
|
282
|
+
npm run build # Compile TypeScript + copy index.html to dist/
|
|
283
|
+
npm test # Run vitest
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
When the service is installed, it runs `dist/index.js` directly. After code changes: `npm run build && zozul restart`.
|
|
287
|
+
|
|
288
|
+
The DB is at `~/.zozul/zozul.db`. Use `sqlite3 ~/.zozul/zozul.db` for ad-hoc inspection. Use `zozul db-clean` to remove rows with invalid timestamps.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|