skilltally 1.1.0 → 1.2.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.
@@ -10,7 +10,7 @@
10
10
  {
11
11
  "name": "skilltally",
12
12
  "description": "Track skill activations and identify unused skills eating context.",
13
- "version": "1.1.0",
13
+ "version": "1.2.0",
14
14
  "source": "./",
15
15
  "category": "productivity"
16
16
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "skilltally",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Real-time skill usage tracking via hooks for Antigravity, Claude Code, Codex & Gemini CLI.",
5
5
  "author": {
6
6
  "name": "112516101106",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "skilltally",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Real-time skill usage tracking via hooks for Antigravity, Claude Code, Codex & Gemini CLI.",
5
5
  "author": {
6
6
  "name": "112516101106",
package/CHANGELOG.md CHANGED
@@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [1.2.0] - 2026-11-20
9
+
10
+ ### Added
11
+ - **Runtime Token Usage & Prompt Cache Efficiency Telemetry**: Live hooks (`bin/track.mjs`) and all 4 backfill parsers extract normalized `input_tokens`, `output_tokens`, `cache_read_tokens`, `cache_creation_tokens`, and `total_tokens` from transcripts, exposing `totals.tokens` (`cacheHitRate`, `lifetimeTokens`, `peakSessionTokens`, `activeBodyTokens`).
12
+ - **Skill Task Duration Tracking**: Computes `duration_seconds` across turns in backfill and live hooks, reporting `totals.duration` (`totalSeconds`, `avgSeconds`, `longestTaskSeconds`) and `insights.slowestSkills`.
13
+ - **Heavy `SKILL.md` Body Detection**: Scans full `body_tokens` in `scanInventory` and flags `>= 1,500`-token skill files (`insights.heavyBodySkills`) that should be split into `references/`.
14
+ - **Opt-In Redacted Prompt Triggers (`store_prompts: true`)**: Sanitizes and strips skill tags, secrets, emails, and absolute paths via `sanitizePromptPreview` to surface `insights.promptPatterns`.
15
+ - **Profile Dashboard Updates**: Upgraded the 5-metric pill bar to `Lifetime tokens`, `Peak tokens`, `Longest task`, `Longest streak`, and `Current streak`, plus cache hit rate, active body tokens, automated duration, and heavy-body pruning cards.
16
+
8
17
  ## [1.1.0] - 2026-11-20
9
18
 
10
19
  ### Added
package/README.md CHANGED
@@ -119,7 +119,7 @@ Tip: disabling an unused plugin removes all of its skill descriptions from every
119
119
  ## Privacy, Performance & Reliability
120
120
 
121
121
  - **100% local, zero telemetry**: Zero network requests, zero third-party runtime dependencies.
122
- - **No raw arguments stored**: Only the matched `SKILL.md` path (or `Skill`/`activate_skill` name) and `basename(cwd)` are written to `~/.skilltally/` (`0700` dir, `0600` files). Full `cwd` is omitted unless `store_cwd: true` is set in `~/.skilltally/config.json`.
122
+ - **No raw arguments or prompts stored**: Only the matched `SKILL.md` path (or `Skill`/`activate_skill` name) and `basename(cwd)` are written to `~/.skilltally/` (`0700` dir, `0600` files). Full `cwd` is omitted unless `store_cwd: true` is set, and redacted prompt previews (`80` chars, secrets/emails/paths stripped) are omitted unless `store_prompts: true` is set in `~/.skilltally/config.json`.
123
123
  - **Safe by contract**: `track.mjs` never throws, always exits `0`, and always writes `{}` to `stdout` so hooks never interrupt an agent turn.
124
124
  - **Automatic rotation**: When `events.jsonl` exceeds 20 MB, it rotates automatically to `events-YYYY-MM.jsonl` while reports transparently read all archives.
125
125
  - **Config backups**: `install` and `uninstall` back up modified host configs to `~/.skilltally/backups/`.
@@ -131,7 +131,8 @@ Tip: disabling an unused plugin removes all of its skill descriptions from every
131
131
  "exclude_skills": ["skilltally"],
132
132
  "exclude_sources": [],
133
133
  "aliases": { "native_swarm": "swarm" },
134
- "store_cwd": false
134
+ "store_cwd": false,
135
+ "store_prompts": false
135
136
  }
136
137
  ```
137
138
 
package/bin/track.mjs CHANGED
@@ -3,9 +3,12 @@
3
3
  // Contract when executed directly: NEVER throw, ALWAYS exit 0, ALWAYS print "{}".
4
4
  import {
5
5
  appendFileSync,
6
+ closeSync,
6
7
  existsSync,
7
8
  mkdirSync,
9
+ openSync,
8
10
  readFileSync,
11
+ readSync,
9
12
  realpathSync,
10
13
  renameSync,
11
14
  statSync,
@@ -130,8 +133,59 @@ export function resolveHost(hostFlag, payload, env = process.env) {
130
133
  return 'unknown';
131
134
  }
132
135
 
136
+ export function sanitizePromptPreview(rawPrompt, maxLen = 80) {
137
+ if (typeof rawPrompt !== 'string' || !rawPrompt.trim()) return '';
138
+ let text = rawPrompt
139
+ .replace(/<SKILL>[\s\S]*?<\/SKILL>/gi, ' ')
140
+ .replace(/<skill>[\s\S]*?<\/skill>/gi, ' ')
141
+ .replace(/\[[\$@\/]?[A-Za-z0-9._-]+\]\([^()\s]*SKILL\.md\)/gi, ' ')
142
+ .replace(/Base directory for this skill:[^\r\n]*/gi, ' ')
143
+ .replace(/<[^>]+>/g, ' ')
144
+ .replace(/\b(?:sk|pk|rk|ghp|gho|github_pat|xox[baprs]|AIza)[A-Za-z0-9_-]{10,}\b/g, '[REDACTED]')
145
+ .replace(/\b[A-Za-z0-9_-]{36,}\b/g, '[REDACTED]')
146
+ .replace(/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g, '[EMAIL]')
147
+ .replace(/(?:\/Users\/|\/home\/|[A-Za-z]:\\)[^\s"'`)]+/g, '[PATH]')
148
+ .replace(/\s+/g, ' ')
149
+ .trim();
150
+ if (!text) return '';
151
+ return text.length > maxLen ? text.slice(0, maxLen) : text;
152
+ }
153
+
154
+ export function extractUsageMetrics(usage) {
155
+ if (!usage || typeof usage !== 'object') return null;
156
+ const num = (v) => (typeof v === 'number' && Number.isFinite(v) && v >= 0 ? v : null);
157
+ const input = num(usage.input_tokens ?? usage.promptTokenCount) ?? 0;
158
+ const output = num(usage.output_tokens ?? usage.candidatesTokenCount) ?? 0;
159
+ const cacheRead =
160
+ num(
161
+ usage.cache_read_tokens ??
162
+ usage.cache_read_input_tokens ??
163
+ usage.cached_input_tokens ??
164
+ usage.cachedContentTokenCount
165
+ ) ?? 0;
166
+ const cacheCreate = num(usage.cache_creation_tokens ?? usage.cache_creation_input_tokens) ?? 0;
167
+ const rawTotal = num(usage.total_tokens ?? usage.totalTokenCount);
168
+ const computedTotal = input + output + cacheRead + cacheCreate;
169
+ const total = rawTotal !== null && rawTotal > 0 ? rawTotal : computedTotal;
170
+ if (total <= 0) return null;
171
+ return {
172
+ input_tokens: input,
173
+ output_tokens: output,
174
+ cache_read_tokens: cacheRead,
175
+ cache_creation_tokens: cacheCreate,
176
+ total_tokens: total,
177
+ };
178
+ }
179
+
133
180
  export function normalizeHookPayload(host, payload) {
134
181
  const p = payload && typeof payload === 'object' ? payload : {};
182
+ const transcriptPath = p.transcript_path ?? p.transcriptPath;
183
+ const payloadUsage = extractUsageMetrics(
184
+ p.usage ?? p.message?.usage ?? p.tool_response?.usage ?? p.usageMetadata
185
+ );
186
+ const rawDur = Number(p.duration_seconds);
187
+ const durationSeconds = Number.isFinite(rawDur) && rawDur >= 0 ? Math.round(rawDur) : undefined;
188
+
135
189
  if (String(host).startsWith('antigravity') || p.toolCall) {
136
190
  const cwd = p.cwd ?? (Array.isArray(p.workspacePaths) ? p.workspacePaths[0] : undefined);
137
191
  return {
@@ -142,6 +196,9 @@ export function normalizeHookPayload(host, payload) {
142
196
  cwd,
143
197
  model: p.modelName,
144
198
  failed: Boolean(p.error),
199
+ transcriptPath,
200
+ ...(payloadUsage ? { usage: payloadUsage } : {}),
201
+ ...(durationSeconds !== undefined ? { durationSeconds } : {}),
145
202
  };
146
203
  }
147
204
  const resp = p.tool_response;
@@ -169,6 +226,9 @@ export function normalizeHookPayload(host, payload) {
169
226
  cwd: p.cwd,
170
227
  model: p.model,
171
228
  failed,
229
+ transcriptPath,
230
+ ...(payloadUsage ? { usage: payloadUsage } : {}),
231
+ ...(durationSeconds !== undefined ? { durationSeconds } : {}),
172
232
  };
173
233
  }
174
234
 
@@ -353,12 +413,106 @@ export function detectSkillCalls({
353
413
  return hits;
354
414
  }
355
415
 
356
- function readStoreCwdFlag(home) {
416
+ function readConfigFlags(home) {
357
417
  try {
358
418
  const cfg = JSON.parse(readFileSync(join(home, 'config.json'), 'utf8'));
359
- return cfg?.store_cwd === true;
419
+ return {
420
+ storeCwd: cfg?.store_cwd === true,
421
+ storePrompts: cfg?.store_prompts === true,
422
+ };
360
423
  } catch {
361
- return false;
424
+ return { storeCwd: false, storePrompts: false };
425
+ }
426
+ }
427
+
428
+ function extractUserTextFromRecord(obj) {
429
+ if (!obj || typeof obj !== 'object') return null;
430
+ if (obj.type === 'USER_INPUT' && typeof obj.content === 'string') {
431
+ return obj.content;
432
+ }
433
+ if (obj.type === 'user' || obj.role === 'user') {
434
+ const content = obj.message?.content ?? obj.content;
435
+ if (typeof content === 'string') return content;
436
+ if (Array.isArray(content)) {
437
+ const parts = content
438
+ .filter((b) => b?.type === 'text' && typeof b.text === 'string')
439
+ .map((b) => b.text);
440
+ if (parts.length > 0) return parts.join(' ');
441
+ }
442
+ }
443
+ if (obj.type === 'response_item' && obj.payload?.type === 'message' && obj.payload?.role === 'user') {
444
+ const content = obj.payload.content;
445
+ if (Array.isArray(content)) {
446
+ const parts = content.filter((b) => typeof b?.text === 'string').map((b) => b.text);
447
+ if (parts.length > 0) return parts.join(' ');
448
+ }
449
+ }
450
+ return null;
451
+ }
452
+
453
+ function readTranscriptTailContext(transcriptPath, nowMs) {
454
+ if (!transcriptPath || typeof transcriptPath !== 'string') return {};
455
+ try {
456
+ if (!existsSync(transcriptPath)) return {};
457
+ const size = statSync(transcriptPath).size;
458
+ if (size <= 0) return {};
459
+ const maxRead = 24 * 1024;
460
+ const offset = Math.max(0, size - maxRead);
461
+ const len = size - offset;
462
+ const buf = Buffer.allocUnsafe(len);
463
+ const fd = openSync(transcriptPath, 'r');
464
+ try {
465
+ readSync(fd, buf, 0, len, offset);
466
+ } finally {
467
+ closeSync(fd);
468
+ }
469
+ let text = buf.toString('utf8');
470
+ if (offset > 0) {
471
+ const nl = text.indexOf('\n');
472
+ if (nl !== -1) text = text.slice(nl + 1);
473
+ }
474
+ const lines = text.split('\n');
475
+ let usage = null;
476
+ let promptText = null;
477
+ let durationSeconds;
478
+
479
+ for (let i = lines.length - 1; i >= 0; i--) {
480
+ const line = lines[i].trim();
481
+ if (!line) continue;
482
+ let obj;
483
+ try {
484
+ obj = JSON.parse(line);
485
+ } catch {
486
+ continue;
487
+ }
488
+ if (!usage) {
489
+ usage = extractUsageMetrics(
490
+ obj.message?.usage ??
491
+ obj.usage ??
492
+ obj.usageMetadata ??
493
+ obj.payload?.usage ??
494
+ obj.payload?.info?.last_token_usage
495
+ );
496
+ }
497
+ if (promptText === null) {
498
+ const uText = extractUserTextFromRecord(obj);
499
+ if (uText !== null) {
500
+ promptText = uText;
501
+ const tsVal = obj.timestamp ?? obj.created_at;
502
+ if (tsVal && Number.isFinite(nowMs)) {
503
+ const startMs = Date.parse(tsVal);
504
+ if (Number.isFinite(startMs) && nowMs >= startMs && nowMs - startMs <= 86400_000) {
505
+ durationSeconds = Math.round((nowMs - startMs) / 1000);
506
+ }
507
+ }
508
+ }
509
+ }
510
+ if (usage && promptText !== null) break;
511
+ }
512
+
513
+ return { usage, promptText, durationSeconds };
514
+ } catch {
515
+ return {};
362
516
  }
363
517
  }
364
518
 
@@ -441,7 +595,14 @@ export function main() {
441
595
  if (uniqueHits.length === 0) return;
442
596
 
443
597
  mkdirSync(home, { recursive: true, mode: 0o700 });
444
- const storeCwd = readStoreCwdFlag(home);
598
+ const { storeCwd, storePrompts } = readConfigFlags(home);
599
+
600
+ const needsTail = !n.usage || n.durationSeconds === undefined || (storePrompts && !n.prompt);
601
+ const tailCtx = needsTail && n.transcriptPath ? readTranscriptTailContext(n.transcriptPath, nowMs) : {};
602
+ const usage = n.usage ?? tailCtx.usage ?? null;
603
+ const durationSeconds = n.durationSeconds ?? tailCtx.durationSeconds;
604
+ const rawPromptText = n.prompt ?? tailCtx.promptText;
605
+ const promptPreview = storePrompts && rawPromptText ? sanitizePromptPreview(rawPromptText) : '';
445
606
 
446
607
  const lines = uniqueHits.map((h, idx) =>
447
608
  JSON.stringify({
@@ -459,6 +620,9 @@ export function main() {
459
620
  ...(n.cwd ? { project: basename(n.cwd) } : {}),
460
621
  ...(storeCwd && n.cwd ? { cwd: n.cwd } : {}),
461
622
  ...(n.model ? { model: n.model } : {}),
623
+ ...(durationSeconds !== undefined ? { duration_seconds: durationSeconds } : {}),
624
+ ...(usage ? usage : {}),
625
+ ...(promptPreview ? { prompt_preview: promptPreview } : {}),
462
626
  origin: 'hook',
463
627
  ok: !n.failed,
464
628
  })
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "skilltally",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Real-time skill usage tracking via hooks for Antigravity, Claude Code, Codex & Gemini CLI."
5
5
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "skilltally",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Real-time skill usage tracking via hooks for Antigravity, Claude Code, Codex & Gemini CLI.",
5
5
  "type": "module",
6
6
  "bin": {
package/plugin.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "skilltally",
3
3
  "displayName": "SkillTally",
4
4
  "description": "Real-time skill usage tracking via hooks for Antigravity, Claude Code, Codex & Gemini CLI. See which skills you actually use and which eat context.",
5
- "version": "1.1.0",
5
+ "version": "1.2.0",
6
6
  "author": {
7
7
  "name": "112516101106",
8
8
  "url": "https://github.com/112516101106"
@@ -39,7 +39,7 @@ Prefer the fastest available invocation:
39
39
  skilltally insights --since 30d
40
40
  skilltally chains --since 30d
41
41
  ```
42
- - `insights` surfaces Context ROI (`~tok/activation`), manual-only skills (`0%` auto-match — sharpen `SKILL.md` descriptions), single-project global skills (move to `.agents/skills` or `.claude/skills`), and per-model breakdown.
42
+ - `insights` surfaces Context ROI (`~tok/activation`), runtime token & prompt cache hit rate, task duration (`Longest task`), heavy `SKILL.md` body alerts (`>=1,500` tokens), manual-only skills (`0%` auto-match — sharpen `SKILL.md` descriptions), single-project global skills (move to `.agents/skills` or `.claude/skills`), prompt trigger patterns (when `store_prompts: true`), and per-model breakdown.
43
43
  - `chains` reveals `A -> B` transitions, 3-step workflows, and co-occurring skill pairs within sessions.
44
44
  5. **Interactive HTML Dashboard & visualizations**:
45
45
  ```bash
@@ -1,7 +1,7 @@
1
1
  import { existsSync, readdirSync, readFileSync } from 'node:fs';
2
2
  import { homedir } from 'node:os';
3
3
  import { basename, join } from 'node:path';
4
- import { detectSkillCalls, unwrapArg } from '../core/detect.mjs';
4
+ import { detectSkillCalls, extractUsageMetrics, sanitizePromptPreview, unwrapArg } from '../core/detect.mjs';
5
5
 
6
6
  const TARGET_TOOLS = new Set(['read_file', 'view_file', 'run_command']);
7
7
  const PRODUCT_DIRS = ['antigravity', 'antigravity-cli', 'antigravity-ide'];
@@ -65,7 +65,7 @@ function loadFullRecord(fullLinesCache, fullPath, lineIdx, stepIndex) {
65
65
  return null;
66
66
  }
67
67
 
68
- export function backfillAntigravity({ homeDir = homedir(), since = null } = {}) {
68
+ export function backfillAntigravity({ homeDir = homedir(), since = null, storePrompts = false } = {}) {
69
69
  const transcripts = findTranscripts(homeDir);
70
70
  const sinceMs = since instanceof Date ? since.getTime() : since ? Date.parse(since) : null;
71
71
  const events = [];
@@ -77,16 +77,30 @@ export function backfillAntigravity({ homeDir = homedir(), since = null } = {})
77
77
  } catch {
78
78
  continue;
79
79
  }
80
+ if (!raw || (!raw.includes('SKILL.md') && !raw.includes('truncated_fields') && !raw.includes('<SKILL>'))) {
81
+ continue;
82
+ }
80
83
 
81
84
  const lines = raw.split('\n');
82
85
  const fullCache = { lines: undefined };
83
86
  const convEvents = [];
84
87
  const pendingUserSkills = [];
88
+ let turnId = 0;
89
+ let lastUserPrompt = '';
90
+ const turnEndMsMap = new Map();
91
+ const turnUsageMap = new Map();
92
+
93
+ const recordTurnTs = (id, tsStr) => {
94
+ if (!tsStr) return;
95
+ const ms = Date.parse(tsStr);
96
+ if (!Number.isFinite(ms)) return;
97
+ const prev = turnEndMsMap.get(id);
98
+ if (prev === undefined || ms > prev) turnEndMsMap.set(id, ms);
99
+ };
85
100
 
86
101
  for (let lineIdx = 0; lineIdx < lines.length; lineIdx++) {
87
102
  const line = lines[lineIdx];
88
103
  if (!line) continue;
89
- if (!line.includes('SKILL.md') && !line.includes('truncated_fields')) continue;
90
104
 
91
105
  let obj;
92
106
  try {
@@ -98,38 +112,52 @@ export function backfillAntigravity({ homeDir = homedir(), since = null } = {})
98
112
  const ts = obj.created_at;
99
113
  const stepIdx = obj.step_index ?? lineIdx;
100
114
 
101
- if (obj.type === 'USER_INPUT' && typeof obj.content === 'string' && obj.content.includes('<SKILL>')) {
102
- if (sinceMs !== null && ts) {
103
- const tsMs = Date.parse(ts);
104
- if (Number.isFinite(tsMs) && tsMs < sinceMs) continue;
105
- }
106
- const promptHits = detectSkillCalls({
107
- tool: 'USER_INPUT',
108
- args: {},
109
- prompt: obj.content,
110
- cwd: '',
111
- homeDir,
112
- allowShell: false,
113
- });
114
- promptHits.forEach((h, hitIdx) => {
115
- pendingUserSkills.push({
116
- v: 1,
117
- id: `${item.host}:${item.sessionId}:${stepIdx}#${hitIdx}`,
118
- ts: ts || new Date(0).toISOString(),
119
- host: item.host,
120
- skill: h.skill,
121
- ...(h.skill_path ? { skill_path: h.skill_path } : {}),
122
- source: h.source,
123
- via: h.via,
115
+ if (obj.type === 'USER_INPUT' && typeof obj.content === 'string') {
116
+ turnId += 1;
117
+ lastUserPrompt = obj.content;
118
+ recordTurnTs(turnId, ts);
119
+
120
+ if (obj.content.includes('<SKILL>')) {
121
+ if (sinceMs !== null && ts) {
122
+ const tsMs = Date.parse(ts);
123
+ if (Number.isFinite(tsMs) && tsMs < sinceMs) continue;
124
+ }
125
+ const promptHits = detectSkillCalls({
124
126
  tool: 'USER_INPUT',
125
- session_id: item.sessionId,
126
- origin: 'backfill',
127
- ok: obj.status !== 'ERROR',
127
+ args: {},
128
+ prompt: obj.content,
129
+ cwd: '',
130
+ homeDir,
131
+ allowShell: false,
128
132
  });
129
- });
133
+ promptHits.forEach((h, hitIdx) => {
134
+ pendingUserSkills.push({
135
+ v: 1,
136
+ id: `${item.host}:${item.sessionId}:${stepIdx}#${hitIdx}`,
137
+ ts: ts || new Date(0).toISOString(),
138
+ host: item.host,
139
+ skill: h.skill,
140
+ ...(h.skill_path ? { skill_path: h.skill_path } : {}),
141
+ source: h.source,
142
+ via: h.via,
143
+ tool: 'USER_INPUT',
144
+ session_id: item.sessionId,
145
+ _turnId: turnId,
146
+ _promptText: lastUserPrompt,
147
+ origin: 'backfill',
148
+ ok: obj.status !== 'ERROR',
149
+ });
150
+ });
151
+ }
130
152
  continue;
131
153
  }
132
154
 
155
+ recordTurnTs(turnId, ts);
156
+ const stepUsage = extractUsageMetrics(obj.usageMetadata ?? obj.usage);
157
+ if (stepUsage) turnUsageMap.set(turnId, stepUsage);
158
+
159
+ if (!line.includes('SKILL.md') && !line.includes('truncated_fields')) continue;
160
+
133
161
  if (Array.isArray(obj.truncated_fields) && obj.truncated_fields.includes('tool_calls')) {
134
162
  const fullObj = loadFullRecord(fullCache, item.fullPath, lineIdx, obj.step_index);
135
163
  if (fullObj) obj = fullObj;
@@ -170,6 +198,9 @@ export function backfillAntigravity({ homeDir = homedir(), since = null } = {})
170
198
  tool: tc.name,
171
199
  session_id: item.sessionId,
172
200
  ...(cwd ? { project: basename(cwd) } : {}),
201
+ _turnId: turnId,
202
+ _usage: stepUsage,
203
+ _promptText: lastUserPrompt,
173
204
  origin: 'backfill',
174
205
  ok: obj.status !== 'ERROR',
175
206
  });
@@ -183,7 +214,30 @@ export function backfillAntigravity({ homeDir = homedir(), since = null } = {})
183
214
  convEvents.push(uEv);
184
215
  }
185
216
  }
186
- events.push(...convEvents);
217
+
218
+ for (const ev of convEvents) {
219
+ const tid = ev._turnId;
220
+ const usage = ev._usage || turnUsageMap.get(tid) || null;
221
+ const pText = ev._promptText;
222
+ delete ev._turnId;
223
+ delete ev._usage;
224
+ delete ev._promptText;
225
+
226
+ const tsMs = ev.ts ? Date.parse(ev.ts) : NaN;
227
+ const endMs = turnEndMsMap.get(tid);
228
+ const durationSeconds =
229
+ Number.isFinite(tsMs) &&
230
+ Number.isFinite(endMs) &&
231
+ endMs >= tsMs &&
232
+ endMs - tsMs <= 86400_000
233
+ ? Math.round((endMs - tsMs) / 1000)
234
+ : 0;
235
+ if (durationSeconds > 0) ev.duration_seconds = durationSeconds;
236
+ if (usage) Object.assign(ev, usage);
237
+ const preview = storePrompts && pText ? sanitizePromptPreview(pText) : '';
238
+ if (preview) ev.prompt_preview = preview;
239
+ events.push(ev);
240
+ }
187
241
  }
188
242
 
189
243
  return {
@@ -1,7 +1,7 @@
1
1
  import { existsSync, readdirSync, readFileSync } from 'node:fs';
2
2
  import { homedir } from 'node:os';
3
3
  import { basename, join } from 'node:path';
4
- import { detectSkillCalls } from '../core/detect.mjs';
4
+ import { detectSkillCalls, extractUsageMetrics, sanitizePromptPreview } from '../core/detect.mjs';
5
5
 
6
6
  const TARGET_TOOLS = new Set(['Skill', 'Read', 'View', 'Bash']);
7
7
 
@@ -25,7 +25,7 @@ function walkJsonlFiles(dir, out = []) {
25
25
  return out;
26
26
  }
27
27
 
28
- export function backfillClaude({ homeDir = homedir(), since = null } = {}) {
28
+ export function backfillClaude({ homeDir = homedir(), since = null, storePrompts = false } = {}) {
29
29
  const files = walkJsonlFiles(join(homeDir, '.claude', 'projects')).sort();
30
30
  const sinceMs = since instanceof Date ? since.getTime() : since ? Date.parse(since) : null;
31
31
  const events = [];
@@ -43,6 +43,18 @@ export function backfillClaude({ homeDir = homedir(), since = null } = {}) {
43
43
  let cwd = '';
44
44
  const pendingCalls = [];
45
45
  const failedIds = new Set();
46
+ let turnId = 0;
47
+ let lastUserPrompt = '';
48
+ const turnEndMsMap = new Map();
49
+ const turnUsageMap = new Map();
50
+
51
+ const recordTurnTs = (id, tsStr) => {
52
+ if (!tsStr) return;
53
+ const ms = Date.parse(tsStr);
54
+ if (!Number.isFinite(ms)) return;
55
+ const prev = turnEndMsMap.get(id);
56
+ if (prev === undefined || ms > prev) turnEndMsMap.set(id, ms);
57
+ };
46
58
 
47
59
  for (const line of raw.split('\n')) {
48
60
  if (!line) continue;
@@ -59,14 +71,19 @@ export function backfillClaude({ homeDir = homedir(), since = null } = {}) {
59
71
  const content = obj.message?.content;
60
72
  if (obj.type === 'user') {
61
73
  if (typeof content === 'string') {
74
+ turnId += 1;
75
+ lastUserPrompt = content;
76
+ recordTurnTs(turnId, obj.timestamp);
62
77
  if (content.includes('Base directory for this skill:') || content.includes('SKILL.md') || content.includes('<skill>')) {
63
78
  pendingCalls.push({
64
79
  idx: pendingCalls.length,
65
80
  ts: obj.timestamp,
81
+ turnId,
66
82
  tool: 'UserPromptSubmit',
67
83
  callId: obj.uuid || `user_${pendingCalls.length}`,
68
84
  input: {},
69
85
  prompt: content,
86
+ promptText: content,
70
87
  cwd,
71
88
  model: undefined,
72
89
  });
@@ -74,10 +91,21 @@ export function backfillClaude({ homeDir = homedir(), since = null } = {}) {
74
91
  continue;
75
92
  }
76
93
  if (!Array.isArray(content)) continue;
94
+ const textParts = [];
77
95
  for (const block of content) {
78
96
  if (block?.type === 'tool_result' && block.is_error === true && block.tool_use_id) {
79
97
  failedIds.add(block.tool_use_id);
80
- } else if (
98
+ } else if (block?.type === 'text' && typeof block.text === 'string') {
99
+ textParts.push(block.text);
100
+ }
101
+ }
102
+ if (textParts.length > 0) {
103
+ turnId += 1;
104
+ lastUserPrompt = textParts.join(' ');
105
+ }
106
+ recordTurnTs(turnId, obj.timestamp);
107
+ for (const block of content) {
108
+ if (
81
109
  block?.type === 'text' &&
82
110
  typeof block.text === 'string' &&
83
111
  (block.text.includes('Base directory for this skill:') ||
@@ -87,10 +115,12 @@ export function backfillClaude({ homeDir = homedir(), since = null } = {}) {
87
115
  pendingCalls.push({
88
116
  idx: pendingCalls.length,
89
117
  ts: obj.timestamp,
118
+ turnId,
90
119
  tool: 'UserPromptSubmit',
91
120
  callId: obj.uuid || `user_${pendingCalls.length}`,
92
121
  input: {},
93
122
  prompt: block.text,
123
+ promptText: lastUserPrompt,
94
124
  cwd,
95
125
  model: undefined,
96
126
  });
@@ -99,19 +129,27 @@ export function backfillClaude({ homeDir = homedir(), since = null } = {}) {
99
129
  continue;
100
130
  }
101
131
 
102
- if (!Array.isArray(content)) continue;
103
-
132
+ recordTurnTs(turnId, obj.timestamp);
104
133
  if (obj.type !== 'assistant') continue;
134
+
105
135
  const model = obj.message?.model;
136
+ const msgUsage = extractUsageMetrics(obj.message?.usage ?? obj.usage);
137
+ if (msgUsage) {
138
+ turnUsageMap.set(turnId, msgUsage);
139
+ }
106
140
 
141
+ if (!Array.isArray(content)) continue;
107
142
  for (const block of content) {
108
143
  if (block?.type !== 'tool_use' || !TARGET_TOOLS.has(block.name)) continue;
109
144
  pendingCalls.push({
110
145
  idx: pendingCalls.length,
111
146
  ts: obj.timestamp,
147
+ turnId,
112
148
  tool: block.name,
113
149
  callId: block.id,
114
150
  input: block.input && typeof block.input === 'object' ? block.input : {},
151
+ usage: msgUsage,
152
+ promptText: lastUserPrompt,
115
153
  cwd,
116
154
  model,
117
155
  });
@@ -134,6 +172,18 @@ export function backfillClaude({ homeDir = homedir(), since = null } = {}) {
134
172
  });
135
173
 
136
174
  const ok = call.callId ? !failedIds.has(call.callId) : true;
175
+ const callTsMs = call.ts ? Date.parse(call.ts) : NaN;
176
+ const turnEndMs = turnEndMsMap.get(call.turnId);
177
+ const durationSeconds =
178
+ Number.isFinite(callTsMs) &&
179
+ Number.isFinite(turnEndMs) &&
180
+ turnEndMs >= callTsMs &&
181
+ turnEndMs - callTsMs <= 86400_000
182
+ ? Math.round((turnEndMs - callTsMs) / 1000)
183
+ : 0;
184
+ const usage = call.usage || turnUsageMap.get(call.turnId) || null;
185
+ const promptPreview = storePrompts && call.promptText ? sanitizePromptPreview(call.promptText) : '';
186
+
137
187
  hits.forEach((h, hitIdx) => {
138
188
  events.push({
139
189
  v: 1,
@@ -149,6 +199,9 @@ export function backfillClaude({ homeDir = homedir(), since = null } = {}) {
149
199
  session_id: sessionId,
150
200
  ...(call.cwd ? { project: basename(call.cwd) } : {}),
151
201
  ...(call.model ? { model: call.model } : {}),
202
+ ...(durationSeconds > 0 ? { duration_seconds: durationSeconds } : {}),
203
+ ...(usage ? usage : {}),
204
+ ...(promptPreview ? { prompt_preview: promptPreview } : {}),
152
205
  origin: 'backfill',
153
206
  ok,
154
207
  });