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.
@@ -3,7 +3,7 @@ import { existsSync, readdirSync, readFileSync } from 'node:fs';
3
3
  import { homedir } from 'node:os';
4
4
  import { basename, join } from 'node:path';
5
5
  import zlib from 'node:zlib';
6
- import { detectSkillCalls } from '../core/detect.mjs';
6
+ import { detectSkillCalls, extractUsageMetrics, sanitizePromptPreview } from '../core/detect.mjs';
7
7
 
8
8
  function walkSessionFiles(dir, out = []) {
9
9
  if (!existsSync(dir)) return out;
@@ -64,7 +64,7 @@ function sessionIdFromFilename(filePath) {
64
64
  return m ? m[1] : basename(filePath);
65
65
  }
66
66
 
67
- export function backfillCodex({ homeDir = homedir(), since = null } = {}) {
67
+ export function backfillCodex({ homeDir = homedir(), since = null, storePrompts = false } = {}) {
68
68
  const files = [
69
69
  ...walkSessionFiles(join(homeDir, '.codex', 'sessions')),
70
70
  ...walkSessionFiles(join(homeDir, '.codex', 'archived_sessions')),
@@ -95,7 +95,18 @@ export function backfillCodex({ homeDir = homedir(), since = null } = {}) {
95
95
  const pendingCalls = [];
96
96
  const failedCalls = new Set();
97
97
  let turnIdx = 0;
98
+ let turnPrompt = '';
98
99
  const turnPromptSkills = new Set();
100
+ const turnEndMsMap = new Map();
101
+ const turnUsageMap = new Map();
102
+
103
+ const recordTurnTs = (id, tsStr) => {
104
+ if (!tsStr) return;
105
+ const ms = Date.parse(tsStr);
106
+ if (!Number.isFinite(ms)) return;
107
+ const prev = turnEndMsMap.get(id);
108
+ if (prev === undefined || ms > prev) turnEndMsMap.set(id, ms);
109
+ };
99
110
 
100
111
  for (const line of raw.split('\n')) {
101
112
  if (!line) continue;
@@ -119,13 +130,36 @@ export function backfillCodex({ homeDir = homedir(), since = null } = {}) {
119
130
  if (obj.payload.model) model = obj.payload.model;
120
131
  turnPromptSkills.clear();
121
132
  turnIdx += 1;
133
+ turnPrompt = '';
134
+ recordTurnTs(turnIdx, obj.timestamp);
135
+ continue;
136
+ }
137
+
138
+ recordTurnTs(turnIdx, obj.timestamp);
139
+
140
+ if (obj.type === 'event_msg' && obj.payload?.type === 'token_count') {
141
+ const u = extractUsageMetrics(obj.payload?.info?.last_token_usage ?? obj.payload?.usage);
142
+ if (u) turnUsageMap.set(turnIdx, u);
122
143
  continue;
123
144
  }
124
145
 
125
146
  if (obj.type !== 'response_item' || !obj.payload) continue;
126
147
  const p = obj.payload;
127
148
 
149
+ const itemUsage = extractUsageMetrics(obj.usage ?? p.usage);
150
+ if (itemUsage) turnUsageMap.set(turnIdx, itemUsage);
151
+
128
152
  if (p.type === 'message' && p.role === 'user' && Array.isArray(p.content)) {
153
+ const userTextParts = [];
154
+ for (const part of p.content) {
155
+ if (typeof part?.text === 'string' && part.text.trim()) {
156
+ userTextParts.push(part.text);
157
+ }
158
+ }
159
+ if (userTextParts.length > 0) {
160
+ turnPrompt = userTextParts.join(' ');
161
+ }
162
+
129
163
  for (const part of p.content) {
130
164
  const text = part?.text;
131
165
  if (
@@ -153,10 +187,12 @@ export function backfillCodex({ homeDir = homedir(), since = null } = {}) {
153
187
  pendingCalls.push({
154
188
  idx: pendingCalls.length,
155
189
  ts: obj.timestamp,
190
+ turnId: turnIdx,
156
191
  tool: 'UserPromptSubmit',
157
192
  callId: p.id || `prompt_${turnIdx}_${pendingCalls.length}`,
158
193
  args: {},
159
194
  precomputedHits: uniqueTurnHits,
195
+ promptText: turnPrompt,
160
196
  cwd,
161
197
  model,
162
198
  });
@@ -198,9 +234,12 @@ export function backfillCodex({ homeDir = homedir(), since = null } = {}) {
198
234
  pendingCalls.push({
199
235
  idx: pendingCalls.length,
200
236
  ts: obj.timestamp,
237
+ turnId: turnIdx,
201
238
  tool: p.name,
202
239
  callId: p.call_id,
203
240
  args: argsObj,
241
+ usage: itemUsage,
242
+ promptText: turnPrompt,
204
243
  cwd,
205
244
  model,
206
245
  });
@@ -223,6 +262,18 @@ export function backfillCodex({ homeDir = homedir(), since = null } = {}) {
223
262
  });
224
263
 
225
264
  const ok = call.callId ? !failedCalls.has(call.callId) : true;
265
+ const callTsMs = call.ts ? Date.parse(call.ts) : NaN;
266
+ const turnEndMs = turnEndMsMap.get(call.turnId);
267
+ const durationSeconds =
268
+ Number.isFinite(callTsMs) &&
269
+ Number.isFinite(turnEndMs) &&
270
+ turnEndMs >= callTsMs &&
271
+ turnEndMs - callTsMs <= 86400_000
272
+ ? Math.round((turnEndMs - callTsMs) / 1000)
273
+ : 0;
274
+ const usage = call.usage || turnUsageMap.get(call.turnId) || null;
275
+ const promptPreview = storePrompts && call.promptText ? sanitizePromptPreview(call.promptText) : '';
276
+
226
277
  hits.forEach((h, hitIdx) => {
227
278
  events.push({
228
279
  v: 1,
@@ -237,6 +288,9 @@ export function backfillCodex({ homeDir = homedir(), since = null } = {}) {
237
288
  session_id: sessionId,
238
289
  ...(call.cwd ? { project: basename(call.cwd) } : {}),
239
290
  ...(call.model ? { model: call.model } : {}),
291
+ ...(durationSeconds > 0 ? { duration_seconds: durationSeconds } : {}),
292
+ ...(usage ? usage : {}),
293
+ ...(promptPreview ? { prompt_preview: promptPreview } : {}),
240
294
  origin: 'backfill',
241
295
  ok,
242
296
  });
@@ -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([
7
7
  'activate_skill',
@@ -83,7 +83,20 @@ function parseChatFile(filePath, raw) {
83
83
  };
84
84
  }
85
85
 
86
- export function backfillGemini({ homeDir = homedir(), since = null } = {}) {
86
+ function extractGeminiUserText(msg) {
87
+ if (!msg || typeof msg !== 'object') return '';
88
+ if (typeof msg.content === 'string') return msg.content;
89
+ if (typeof msg.text === 'string') return msg.text;
90
+ if (Array.isArray(msg.content)) {
91
+ return msg.content.map((p) => (typeof p?.text === 'string' ? p.text : '')).join(' ');
92
+ }
93
+ if (Array.isArray(msg.parts)) {
94
+ return msg.parts.map((p) => (typeof p?.text === 'string' ? p.text : '')).join(' ');
95
+ }
96
+ return '';
97
+ }
98
+
99
+ export function backfillGemini({ homeDir = homedir(), since = null, storePrompts = false } = {}) {
87
100
  const files = findGeminiChatFiles(homeDir);
88
101
  const sinceMs = since instanceof Date ? since.getTime() : since ? Date.parse(since) : null;
89
102
  const events = [];
@@ -99,9 +112,31 @@ export function backfillGemini({ homeDir = homedir(), since = null } = {}) {
99
112
 
100
113
  const { sessionId, startTime, cwd: fileCwd, messages } = parseChatFile(file, raw);
101
114
  let callIdx = 0;
115
+ let turnId = 0;
116
+ let lastUserPrompt = '';
117
+ const turnEndMsMap = new Map();
118
+ const turnUsageMap = new Map();
119
+ const pendingEvents = [];
120
+
121
+ const recordTurnTs = (id, tsStr) => {
122
+ if (!tsStr) return;
123
+ const ms = Date.parse(tsStr);
124
+ if (!Number.isFinite(ms)) return;
125
+ const prev = turnEndMsMap.get(id);
126
+ if (prev === undefined || ms > prev) turnEndMsMap.set(id, ms);
127
+ };
102
128
 
103
129
  for (const msg of messages) {
104
130
  if (!msg || typeof msg !== 'object') continue;
131
+ if (msg.type === 'user' || msg.role === 'user') {
132
+ turnId += 1;
133
+ lastUserPrompt = extractGeminiUserText(msg);
134
+ }
135
+ recordTurnTs(turnId, msg.timestamp || startTime);
136
+
137
+ const msgUsage = extractUsageMetrics(msg.tokens ?? msg.usageMetadata ?? msg.usage);
138
+ if (msgUsage) turnUsageMap.set(turnId, msgUsage);
139
+
105
140
  const toolCalls = Array.isArray(msg.toolCalls)
106
141
  ? msg.toolCalls
107
142
  : Array.isArray(msg.tool_calls)
@@ -114,6 +149,7 @@ export function backfillGemini({ homeDir = homedir(), since = null } = {}) {
114
149
  if (!tc || !TARGET_TOOLS.has(tc.name)) continue;
115
150
  const idx = callIdx++;
116
151
  const ts = tc.timestamp || msg.timestamp || startTime;
152
+ recordTurnTs(turnId, ts);
117
153
  if (sinceMs !== null && ts) {
118
154
  const tsMs = Date.parse(ts);
119
155
  if (Number.isFinite(tsMs) && tsMs < sinceMs) continue;
@@ -130,7 +166,7 @@ export function backfillGemini({ homeDir = homedir(), since = null } = {}) {
130
166
 
131
167
  const ok = tc.status !== 'error' && tc.status !== 'cancelled';
132
168
  hits.forEach((h, hitIdx) => {
133
- events.push({
169
+ pendingEvents.push({
134
170
  v: 1,
135
171
  id: `gemini:${sessionId}:${tc.id || idx}#${hitIdx}`,
136
172
  ts: ts || new Date(0).toISOString(),
@@ -143,12 +179,39 @@ export function backfillGemini({ homeDir = homedir(), since = null } = {}) {
143
179
  session_id: sessionId,
144
180
  ...(msgCwd ? { project: basename(msgCwd) } : {}),
145
181
  ...(msg.model ? { model: msg.model } : {}),
182
+ _turnId: turnId,
183
+ _usage: msgUsage,
184
+ _promptText: lastUserPrompt,
146
185
  origin: 'backfill',
147
186
  ok,
148
187
  });
149
188
  });
150
189
  }
151
190
  }
191
+
192
+ for (const ev of pendingEvents) {
193
+ const tid = ev._turnId;
194
+ const usage = ev._usage || turnUsageMap.get(tid) || null;
195
+ const pText = ev._promptText;
196
+ delete ev._turnId;
197
+ delete ev._usage;
198
+ delete ev._promptText;
199
+
200
+ const tsMs = ev.ts ? Date.parse(ev.ts) : NaN;
201
+ const endMs = turnEndMsMap.get(tid);
202
+ const durationSeconds =
203
+ Number.isFinite(tsMs) &&
204
+ Number.isFinite(endMs) &&
205
+ endMs >= tsMs &&
206
+ endMs - tsMs <= 86400_000
207
+ ? Math.round((endMs - tsMs) / 1000)
208
+ : 0;
209
+ if (durationSeconds > 0) ev.duration_seconds = durationSeconds;
210
+ if (usage) Object.assign(ev, usage);
211
+ const preview = storePrompts && pText ? sanitizePromptPreview(pText) : '';
212
+ if (preview) ev.prompt_preview = preview;
213
+ events.push(ev);
214
+ }
152
215
  }
153
216
 
154
217
  return {
@@ -1,5 +1,5 @@
1
1
  import { homedir } from 'node:os';
2
- import { getHomeDir } from '../core/config.mjs';
2
+ import { getHomeDir, loadConfig } from '../core/config.mjs';
3
3
  import { dedupeEvents } from '../core/normalize.mjs';
4
4
  import { writeBackfillEvents } from '../core/store.mjs';
5
5
  import { backfillAntigravity } from './antigravity.mjs';
@@ -16,6 +16,8 @@ export function runBackfill({
16
16
  } = {}) {
17
17
  const hostsSummary = {};
18
18
  const collected = [];
19
+ const config = loadConfig(dataHome);
20
+ const storePrompts = Boolean(config.store_prompts);
19
21
 
20
22
  const wantAgy = host === 'all' || String(host).startsWith('antigravity');
21
23
  const wantCodex = host === 'all' || host === 'codex';
@@ -23,7 +25,7 @@ export function runBackfill({
23
25
  const wantGemini = host === 'all' || host === 'gemini';
24
26
 
25
27
  if (wantAgy) {
26
- const res = backfillAntigravity({ homeDir, since });
28
+ const res = backfillAntigravity({ homeDir, since, storePrompts });
27
29
  const filtered =
28
30
  host === 'all' || host === 'antigravity'
29
31
  ? res.events
@@ -37,7 +39,7 @@ export function runBackfill({
37
39
  }
38
40
 
39
41
  if (wantCodex) {
40
- const res = backfillCodex({ homeDir, since });
42
+ const res = backfillCodex({ homeDir, since, storePrompts });
41
43
  collected.push(...res.events);
42
44
  hostsSummary.codex = {
43
45
  files: res.files,
@@ -48,7 +50,7 @@ export function runBackfill({
48
50
  }
49
51
 
50
52
  if (wantClaude) {
51
- const res = backfillClaude({ homeDir, since });
53
+ const res = backfillClaude({ homeDir, since, storePrompts });
52
54
  collected.push(...res.events);
53
55
  hostsSummary.claude = {
54
56
  files: res.files,
@@ -58,7 +60,7 @@ export function runBackfill({
58
60
  }
59
61
 
60
62
  if (wantGemini) {
61
- const res = backfillGemini({ homeDir, since });
63
+ const res = backfillGemini({ homeDir, since, storePrompts });
62
64
  collected.push(...res.events);
63
65
  hostsSummary.gemini = {
64
66
  files: res.files,
@@ -7,6 +7,7 @@ export const DEFAULT_CONFIG = Object.freeze({
7
7
  exclude_sources: [],
8
8
  aliases: {},
9
9
  store_cwd: false,
10
+ store_prompts: false,
10
11
  });
11
12
 
12
13
  export function getHomeDir(env = process.env) {
@@ -26,6 +27,7 @@ export function loadConfig(homeDir = getHomeDir()) {
26
27
  ? { ...parsed.aliases }
27
28
  : {},
28
29
  store_cwd: parsed.store_cwd === true,
30
+ store_prompts: parsed.store_prompts === true,
29
31
  };
30
32
  } catch {
31
33
  return { ...DEFAULT_CONFIG };
@@ -2,8 +2,10 @@ export {
2
2
  SKILL_NAME_RE,
3
3
  SKILL_PATH_RE,
4
4
  detectSkillCalls,
5
+ extractUsageMetrics,
5
6
  normalizeHookPayload,
6
7
  resolveHost,
7
8
  resolveSource,
9
+ sanitizePromptPreview,
8
10
  unwrapArg,
9
11
  } from '../../bin/track.mjs';
@@ -285,15 +285,17 @@ export function scanInventory({ homeDir = homedir(), cwd = process.cwd(), includ
285
285
  }
286
286
 
287
287
  const descChars = fm.description.length;
288
+ const bytes = Buffer.byteLength(md, 'utf8');
288
289
  byCanonPath.set(canon, {
289
290
  name: fm.name || dirName,
290
291
  dir_name: dirName,
291
292
  path: canon,
292
293
  source: source || resolveSource(canon, cwd, homeDir),
293
294
  hosts: [host],
294
- bytes: Buffer.byteLength(md, 'utf8'),
295
+ bytes,
295
296
  desc_chars: descChars,
296
297
  est_tokens: descChars > 0 ? Math.ceil(descChars / 4) : 0,
298
+ body_tokens: bytes > 0 ? Math.ceil(bytes / 4) : 0,
297
299
  enabled,
298
300
  });
299
301
  };
@@ -35,6 +35,21 @@ export function parseSince(sinceStr, now = new Date()) {
35
35
  throw new Error(`Invalid --since value: "${sinceStr}" (use e.g. 7d, 30d, 90d, 4w, 6m, 2026-01-01, or all)`);
36
36
  }
37
37
 
38
+ export function formatDurationCompact(seconds) {
39
+ const sec = Number(seconds);
40
+ if (!Number.isFinite(sec) || sec <= 0) return '0s';
41
+ const rounded = Math.round(sec);
42
+ if (rounded < 60) return `${rounded}s`;
43
+ if (rounded < 3600) {
44
+ const m = Math.floor(rounded / 60);
45
+ const s = rounded % 60;
46
+ return s > 0 ? `${m}m ${s}s` : `${m}m`;
47
+ }
48
+ const h = Math.floor(rounded / 3600);
49
+ const m = Math.floor((rounded % 3600) / 60);
50
+ return m > 0 ? `${h}h ${m}m` : `${h}h`;
51
+ }
52
+
38
53
  export function hostMatches(eventHost, filterHost) {
39
54
  if (!filterHost || filterHost === 'all') return true;
40
55
  if (eventHost === filterHost) return true;
@@ -149,8 +164,20 @@ export function aggregateStats({
149
164
  const modelTotals = {};
150
165
  const projectTotals = {};
151
166
  const allSessions = new Set();
167
+ const sessionBounds = new Map();
168
+ const sessionExplicitDur = new Map();
169
+ const sessionTokensMap = new Map();
170
+ const sessionBodyFallbackMap = new Map();
171
+ const promptMap = new Map();
172
+
152
173
  let manualTotal = 0;
153
174
  let autoTotal = 0;
175
+ let totalInputTokens = 0;
176
+ let totalOutputTokens = 0;
177
+ let totalCacheReadTokens = 0;
178
+ let totalCacheCreationTokens = 0;
179
+ let totalRecordedTokens = 0;
180
+ let maxSingleDuration = 0;
154
181
  const groups = new Map();
155
182
 
156
183
  for (let idx = 0; idx < filtered.length; idx++) {
@@ -192,6 +219,8 @@ export function aggregateStats({
192
219
  projects: new Set(),
193
220
  models: new Set(),
194
221
  daySessions: new Map(),
222
+ durations: [],
223
+ tokens: { input: 0, output: 0, cacheRead: 0, cacheCreation: 0, total: 0 },
195
224
  last_used: ev.ts || '',
196
225
  };
197
226
  groups.set(key, bucket);
@@ -213,9 +242,54 @@ export function aggregateStats({
213
242
  if (ev.project) bucket.projects.add(ev.project);
214
243
  if (ev.model) bucket.models.add(ev.model);
215
244
 
245
+ if (typeof ev.duration_seconds === 'number' && Number.isFinite(ev.duration_seconds) && ev.duration_seconds > 0) {
246
+ const dur = Math.min(86400, Math.round(ev.duration_seconds));
247
+ bucket.durations.push(dur);
248
+ sessionExplicitDur.set(globalSessKey, (sessionExplicitDur.get(globalSessKey) || 0) + dur);
249
+ if (dur > maxSingleDuration) maxSingleDuration = dur;
250
+ }
251
+
252
+ const inTok = typeof ev.input_tokens === 'number' && Number.isFinite(ev.input_tokens) ? ev.input_tokens : 0;
253
+ const outTok = typeof ev.output_tokens === 'number' && Number.isFinite(ev.output_tokens) ? ev.output_tokens : 0;
254
+ const crTok = typeof ev.cache_read_tokens === 'number' && Number.isFinite(ev.cache_read_tokens) ? ev.cache_read_tokens : 0;
255
+ const ccTok = typeof ev.cache_creation_tokens === 'number' && Number.isFinite(ev.cache_creation_tokens) ? ev.cache_creation_tokens : 0;
256
+ const totTok = typeof ev.total_tokens === 'number' && Number.isFinite(ev.total_tokens) ? ev.total_tokens : 0;
257
+ if (totTok > 0 || inTok > 0 || outTok > 0) {
258
+ const effectiveTotal = totTok > 0 ? totTok : inTok + outTok + crTok + ccTok;
259
+ bucket.tokens.input += inTok;
260
+ bucket.tokens.output += outTok;
261
+ bucket.tokens.cacheRead += crTok;
262
+ bucket.tokens.cacheCreation += ccTok;
263
+ bucket.tokens.total += effectiveTotal;
264
+ totalInputTokens += inTok;
265
+ totalOutputTokens += outTok;
266
+ totalCacheReadTokens += crTok;
267
+ totalCacheCreationTokens += ccTok;
268
+ totalRecordedTokens += effectiveTotal;
269
+ sessionTokensMap.set(globalSessKey, (sessionTokensMap.get(globalSessKey) || 0) + effectiveTotal);
270
+ }
271
+
272
+ if (typeof ev.prompt_preview === 'string' && ev.prompt_preview.trim()) {
273
+ const pKey = ev.prompt_preview.trim();
274
+ let pEntry = promptMap.get(pKey);
275
+ if (!pEntry) {
276
+ pEntry = { prompt: pKey, count: 0, skills: new Set() };
277
+ promptMap.set(pKey, pEntry);
278
+ }
279
+ pEntry.count += 1;
280
+ pEntry.skills.add(ev.skill);
281
+ }
282
+
216
283
  if (ev.ts) {
217
284
  const tsMs = Date.parse(ev.ts);
218
285
  if (Number.isFinite(tsMs)) {
286
+ const bounds = sessionBounds.get(globalSessKey);
287
+ if (!bounds) {
288
+ sessionBounds.set(globalSessKey, { min: tsMs, max: tsMs });
289
+ } else {
290
+ if (tsMs < bounds.min) bounds.min = tsMs;
291
+ if (tsMs > bounds.max) bounds.max = tsMs;
292
+ }
219
293
  const dayKey = new Date(tsMs).toISOString().slice(0, 10);
220
294
  let daySet = bucket.daySessions.get(dayKey);
221
295
  if (!daySet) {
@@ -236,8 +310,15 @@ export function aggregateStats({
236
310
  const invEntry = findInventoryEntry(inventory, b.skill, b.source, bySource);
237
311
  const estTokens = invEntry?.est_tokens || 0;
238
312
  const descChars = invEntry?.desc_chars || 0;
313
+ const bodyTokens =
314
+ invEntry?.body_tokens || (invEntry?.bytes ? Math.ceil(invEntry.bytes / 4) : 0);
239
315
  const activations = b.sessions.size;
316
+ const activeLoadTokens = activations * bodyTokens;
240
317
  const dailyCounts = buildDailySparklineSeries(b.daySessions, now, 14);
318
+ const totalDur = b.durations.reduce((acc, d) => acc + d, 0);
319
+ const avgDur = b.durations.length > 0 ? Math.round(totalDur / b.durations.length) : 0;
320
+ const maxDur = b.durations.length > 0 ? Math.max(...b.durations) : 0;
321
+
241
322
  return {
242
323
  skill: b.skill,
243
324
  source: b.source,
@@ -254,12 +335,26 @@ export function aggregateStats({
254
335
  models: [...b.models].sort(),
255
336
  est_tokens: estTokens,
256
337
  desc_chars: descChars,
338
+ body_tokens: bodyTokens,
339
+ activeLoadTokens,
257
340
  tokenCostPerActivation: estTokens > 0 ? Math.round((totalSessions * estTokens) / Math.max(1, activations)) : 0,
341
+ tokens: b.tokens,
342
+ total_duration_seconds: totalDur,
343
+ avg_duration_seconds: avgDur,
344
+ max_duration_seconds: maxDur,
258
345
  dailyCounts,
259
346
  sparkline: encodeSparkline(dailyCounts),
260
347
  };
261
348
  });
262
349
 
350
+ // Track fallback session body tokens for sessions lacking transcript usage
351
+ for (const r of rows) {
352
+ if (r.body_tokens > 0) {
353
+ const k = r.skill;
354
+ sessionBodyFallbackMap.set(k, r.body_tokens);
355
+ }
356
+ }
357
+
263
358
  rows.sort((a, b) => {
264
359
  if (metric === 'reads') {
265
360
  return (
@@ -364,6 +459,103 @@ export function aggregateStats({
364
459
  tokenCostPerActivation: r.tokenCostPerActivation,
365
460
  }));
366
461
 
462
+ // Heavy SKILL.md bodies (>= 1500 body tokens)
463
+ const heavySeen = new Set();
464
+ const heavyCandidates = [];
465
+ for (const r of rows) {
466
+ if (r.body_tokens >= 1500) {
467
+ heavySeen.add(`${r.skill}\0${r.source}`);
468
+ heavyCandidates.push({
469
+ skill: r.skill,
470
+ source: r.source,
471
+ activations: r.activations,
472
+ est_tokens: r.est_tokens,
473
+ body_tokens: r.body_tokens,
474
+ activeLoadTokens: r.activeLoadTokens,
475
+ });
476
+ }
477
+ }
478
+ for (const s of enabledSkills) {
479
+ const bt = s.body_tokens || (s.bytes ? Math.ceil(s.bytes / 4) : 0);
480
+ if (bt >= 1500 && !heavySeen.has(`${s.name}\0${s.source}`)) {
481
+ heavySeen.add(`${s.name}\0${s.source}`);
482
+ heavyCandidates.push({
483
+ skill: s.name,
484
+ source: s.source,
485
+ activations: 0,
486
+ est_tokens: s.est_tokens || 0,
487
+ body_tokens: bt,
488
+ activeLoadTokens: 0,
489
+ });
490
+ }
491
+ }
492
+ const heavyBodySkills = heavyCandidates
493
+ .sort(
494
+ (a, b) =>
495
+ b.body_tokens * Math.max(1, b.activations) - a.body_tokens * Math.max(1, a.activations) ||
496
+ b.body_tokens - a.body_tokens
497
+ )
498
+ .slice(0, 10);
499
+
500
+ const slowestSkills = rows
501
+ .filter((r) => r.total_duration_seconds > 0)
502
+ .sort((a, b) => b.total_duration_seconds - a.total_duration_seconds || b.max_duration_seconds - a.max_duration_seconds)
503
+ .slice(0, 10)
504
+ .map((r) => ({
505
+ skill: r.skill,
506
+ source: r.source,
507
+ activations: r.activations,
508
+ avg_duration_seconds: r.avg_duration_seconds,
509
+ total_duration_seconds: r.total_duration_seconds,
510
+ max_duration_seconds: r.max_duration_seconds,
511
+ }));
512
+
513
+ const promptPatterns = [...promptMap.values()]
514
+ .sort((a, b) => b.count - a.count || a.prompt.localeCompare(b.prompt))
515
+ .slice(0, 10)
516
+ .map((p) => ({
517
+ prompt: p.prompt,
518
+ count: p.count,
519
+ skills: [...p.skills].sort(),
520
+ }));
521
+
522
+ // Compute duration & token aggregates across sessions
523
+ let totalDurationSeconds = 0;
524
+ let longestTaskSeconds = maxSingleDuration;
525
+ for (const sKey of allSessions) {
526
+ const expDur = sessionExplicitDur.get(sKey) || 0;
527
+ let sessDur = expDur;
528
+ if (sessDur === 0) {
529
+ const bounds = sessionBounds.get(sKey);
530
+ if (bounds && bounds.max > bounds.min) {
531
+ const span = Math.round((bounds.max - bounds.min) / 1000);
532
+ if (span > 0 && span <= 86400) sessDur = span;
533
+ }
534
+ }
535
+ totalDurationSeconds += sessDur;
536
+ if (sessDur > longestTaskSeconds) longestTaskSeconds = sessDur;
537
+ }
538
+
539
+ const totalActivations = rows.reduce((acc, r) => acc + r.activations, 0);
540
+ const totalActiveBodyTokens = rows.reduce((acc, r) => acc + (r.activeLoadTokens || 0), 0);
541
+ const cacheDenominator = totalInputTokens + totalCacheReadTokens + totalCacheCreationTokens;
542
+
543
+ let peakSessionTokens = 0;
544
+ if (sessionTokensMap.size > 0) {
545
+ peakSessionTokens = Math.max(...sessionTokensMap.values());
546
+ } else if (filtered.length > 0) {
547
+ const perSessionFallback = new Map();
548
+ for (let idx = 0; idx < filtered.length; idx++) {
549
+ const ev = filtered[idx];
550
+ const h = ev.host || 'unknown';
551
+ const gKey = ev.session_id ? `${h}\0${ev.session_id}` : ev.id || `${h}\0${ev.ts || ''}\0${idx}`;
552
+ const bt = sessionBodyFallbackMap.get(ev.skill) || 0;
553
+ perSessionFallback.set(gKey, (perSessionFallback.get(gKey) || 0) + bt);
554
+ }
555
+ const maxBodyPerSess = perSessionFallback.size > 0 ? Math.max(...perSessionFallback.values()) : 0;
556
+ peakSessionTokens = maxBodyPerSess + enabledEstTokens;
557
+ }
558
+
367
559
  return {
368
560
  window: {
369
561
  since: since ?? 'all',
@@ -372,7 +564,7 @@ export function aggregateStats({
372
564
  },
373
565
  totals: {
374
566
  events: filtered.length,
375
- activations: rows.reduce((acc, r) => acc + r.activations, 0),
567
+ activations: totalActivations,
376
568
  uniqueSkills: rows.length,
377
569
  sessions: allSessions.size,
378
570
  hosts: hostTotals,
@@ -381,6 +573,25 @@ export function aggregateStats({
381
573
  projects: projectTotals,
382
574
  manualReads: manualTotal,
383
575
  autoReads: autoTotal,
576
+ tokens: {
577
+ input: totalInputTokens,
578
+ output: totalOutputTokens,
579
+ cacheRead: totalCacheReadTokens,
580
+ cacheCreation: totalCacheCreationTokens,
581
+ total: totalRecordedTokens,
582
+ cacheHitRate: cacheDenominator > 0 ? Number((totalCacheReadTokens / cacheDenominator).toFixed(2)) : 0,
583
+ activeBodyTokens: totalActiveBodyTokens,
584
+ lifetimeTokens:
585
+ totalRecordedTokens > 0
586
+ ? totalRecordedTokens
587
+ : totalActiveBodyTokens + enabledEstTokens * (filtered.length > 0 ? Math.max(1, allSessions.size) : 0),
588
+ peakSessionTokens,
589
+ },
590
+ duration: {
591
+ totalSeconds: totalDurationSeconds,
592
+ avgSeconds: rows.length > 0 ? Math.round(totalDurationSeconds / Math.max(1, totalActivations)) : 0,
593
+ longestTaskSeconds,
594
+ },
384
595
  },
385
596
  inventory: {
386
597
  enabled: enabledSkills.length,
@@ -401,6 +612,9 @@ export function aggregateStats({
401
612
  manualOnlySkills,
402
613
  projectScopeCandidates,
403
614
  highCostSkills,
615
+ heavyBodySkills,
616
+ slowestSkills,
617
+ promptPatterns,
404
618
  },
405
619
  };
406
620
  }