@ascenda-one/history-import 0.1.39 → 0.1.41

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.
@@ -0,0 +1,719 @@
1
+ /**
2
+ * Copilot CLI session-log extractor.
3
+ *
4
+ * Store: `<snapshot>/<session-id>/events.jsonl` with `workspace.yaml` beside
5
+ * it, one directory per session. Older builds left the log as a flat
6
+ * `<session-id>.jsonl` next to the directories, in the same event shape, and
7
+ * may still have a directory of the same id holding only `workspace.yaml`.
8
+ * A session written both ways is read from its directory once and counted in
9
+ * `duplicateChatSessions`.
10
+ *
11
+ * Every line is `{ type, data, id, timestamp, parentId }`. `type` is the
12
+ * discriminator and `data` holds the fields. The fields read:
13
+ * - `session.start`: `version`, `copilotVersion`, `selectedModel`,
14
+ * `sessionId`, `context.{cwd,gitRoot,branch}`.
15
+ * - `session.model_change`: `newModel`. Older builds name the model only
16
+ * here and at start, never on a message.
17
+ * - `session.auto_mode_resolved`: `chosenModel`, the model the router picked.
18
+ * - `user.message`: counted, and `source`. A subagent's task arrives as a
19
+ * `user.message` whose `source` is `agent-…`: the parent agent wrote it,
20
+ * so it counts as a subagent prompt, not a person's. `content` and
21
+ * `transformedContent` are never read.
22
+ * - `assistant.message`: `model`. `content`, `reasoningText` and
23
+ * `encryptedContent` are never read.
24
+ * - `tool.execution_start`: `toolCallId`, `toolName`, `model`,
25
+ * `parentToolCallId` (set on a call a subagent made). `arguments` is never
26
+ * read.
27
+ * - `tool.execution_complete`: `success`. `result` is never read.
28
+ * - `permission.requested`: `agentMode`, `permissionMode`.
29
+ * - `session.compaction_complete`: `success`.
30
+ * - `session.shutdown`: `tokenDetails`, `modelMetrics[*].usage`,
31
+ * `codeChanges.{linesAdded,linesRemoved}`, the length of
32
+ * `codeChanges.filesModified` (never its entries), `totalApiDurationMs`.
33
+ * From `workspace.yaml` only `cwd`, `git_root`, `branch` and `client_name`.
34
+ * Its `name` is the session's first prompt and `summary` is the agent's own
35
+ * account of the work; neither is read.
36
+ *
37
+ * `hook.start` and `hook.end` are the CLI's record of running someone's
38
+ * hooks. They aren't work, so they're skipped whole and put no instant on the
39
+ * timeline.
40
+ *
41
+ * **Version dispatch (design rule 3).** `session.start.version` is 1 on every
42
+ * session seen, from December 2025 to October 2026, across builds that wrote
43
+ * `copilotVersion: "unknown"` and builds that wrote a real one. A session
44
+ * whose start names another version, or that has no `session.start` at all,
45
+ * is not read: it's counted in `unparsedChatSessionFiles` on the epoch, its
46
+ * raw log stays in staging, and nothing is inferred from it.
47
+ *
48
+ * **Model.** The model in force starts as `selectedModel` and moves with each
49
+ * `model_change`. With `selectedModel: "auto"` it's the router's
50
+ * `chosenModel`. Every `assistant.message` and every `tool.execution_start`
51
+ * is one vote for the model it names, or for the model in force where it
52
+ * names none, which is how older builds wrote them. `primaryModel` is the top
53
+ * of that mix: the model that served the session. `selectedModel` rides
54
+ * beside it, with its class, so a session where the person picked `auto`
55
+ * reads `router:auto` there whatever served it.
56
+ *
57
+ * **Posture.** `permission.requested` carries `agentMode` and
58
+ * `permissionMode`. `permissionMode: "manual"` is the CLI asking before each
59
+ * tool, the same posture as Claude Code's `default`. `agentMode: "plan"` is
60
+ * planning. Any other pair is kept under a `copilot:` token, which bands as
61
+ * `unknown` until someone has seen what it means. Supervising time before the
62
+ * first request in a session has no posture and lands in `unknown`.
63
+ *
64
+ * **Tokens** come from the last `session.shutdown`: `tokenDetails` input
65
+ * (uncached), `cache_read`, `cache_write` and output, the same split as
66
+ * Claude Code's. A shutdown without `tokenDetails` falls back to the sum of
67
+ * `modelMetrics[*].usage`, whose `inputTokens` includes both cache figures.
68
+ * A session that was killed has no shutdown; its token keys are left off and
69
+ * `tokensNotCollected` says so.
70
+ *
71
+ * Emission (aggregate before shipping):
72
+ * - `ai_prompt_submitted` per person's `user.message`, historical_direct.
73
+ * - `ai_tool_call_started` per `tool.execution_start`, deduplicated on
74
+ * `toolCallId`, historical_direct.
75
+ * - `create_focus_session` per session, historical_derived.
76
+ * - `after_hours_ai_session` / `tool_failure` per session with ≥1, derived.
77
+ * - one `extraction_epoch` for the store's observed window. Local only.
78
+ * Metrics carry counts, ids and timestamps only.
79
+ */
80
+ import * as fs from "node:fs/promises";
81
+ import * as path from "node:path";
82
+ import { classifyModelClass, isOutsideBusinessHours, bucketDurationMs } from "@ascenda-one/tool-kit";
83
+ import { HISTORICAL_PROVENANCE } from "../types.js";
84
+ import { sanitizeToolName } from "../toolName.js";
85
+ import { sliceSessionByLocalDay } from "../daySlice.js";
86
+ import { DEFAULT_ACTIVE_GAP_MS, activeSpans, minutesOf, splitActiveTime } from "../activeSplit.js";
87
+ /** The `session.start.version` values this extractor reads. */
88
+ export const KNOWN_COPILOT_LOG_VERSIONS = new Set([1]);
89
+ /** Event types the extractor reads fields from. */
90
+ export const KNOWN_COPILOT_EVENT_TYPES = new Set([
91
+ "session.start",
92
+ "session.model_change",
93
+ "session.auto_mode_resolved",
94
+ "user.message",
95
+ "assistant.message",
96
+ "tool.execution_start",
97
+ "tool.execution_complete",
98
+ "permission.requested",
99
+ "session.compaction_complete",
100
+ "session.shutdown"
101
+ ]);
102
+ /**
103
+ * Event types seen in real logs that carry nothing the import needs. They
104
+ * count as `metaLines` and still date the timeline, so `unknownLines` keeps
105
+ * meaning a type nobody has looked at yet.
106
+ */
107
+ export const META_COPILOT_EVENT_TYPES = new Set([
108
+ "assistant.turn_start",
109
+ "assistant.turn_end",
110
+ "session.resume",
111
+ "session.plan_changed",
112
+ "session.compaction_start",
113
+ "session.truncation",
114
+ "session.usage_checkpoint",
115
+ "system.message",
116
+ "permission.completed",
117
+ "subagent.started",
118
+ "subagent.completed",
119
+ "subagent.failed",
120
+ "abort"
121
+ ]);
122
+ /** Skipped whole: the CLI running hooks, which is no part of the work. */
123
+ export const SKIPPED_COPILOT_EVENT_TYPES = new Set(["hook.start", "hook.end"]);
124
+ function asRecord(value) {
125
+ return typeof value === "object" && value !== null && !Array.isArray(value)
126
+ ? value
127
+ : null;
128
+ }
129
+ function asNumber(value) {
130
+ return typeof value === "number" && Number.isFinite(value) ? value : 0;
131
+ }
132
+ function nonEmptyString(value) {
133
+ return typeof value === "string" && value.trim() !== "" ? value : null;
134
+ }
135
+ export function sniffCopilotLine(line) {
136
+ const trimmed = line.trim();
137
+ if (trimmed === "")
138
+ return { kind: "unparsed" };
139
+ let parsed;
140
+ try {
141
+ parsed = JSON.parse(trimmed);
142
+ }
143
+ catch {
144
+ return { kind: "unparsed" };
145
+ }
146
+ const record = asRecord(parsed);
147
+ if (!record || typeof record.type !== "string")
148
+ return { kind: "unparsed" };
149
+ const type = record.type;
150
+ if (SKIPPED_COPILOT_EVENT_TYPES.has(type))
151
+ return { kind: "skipped", type };
152
+ const occurredAt = typeof record.timestamp === "string" ? record.timestamp : null;
153
+ if (META_COPILOT_EVENT_TYPES.has(type))
154
+ return { kind: "meta", type, occurredAt };
155
+ if (!KNOWN_COPILOT_EVENT_TYPES.has(type))
156
+ return { kind: "unknown", type, occurredAt };
157
+ return { kind: "event", type, occurredAt, data: asRecord(record.data) ?? {} };
158
+ }
159
+ const WORKSPACE_KEYS = {
160
+ cwd: "cwd",
161
+ git_root: "gitRoot",
162
+ branch: "branch",
163
+ client_name: "clientName"
164
+ };
165
+ /**
166
+ * The four `workspace.yaml` keys this extractor reads, and nothing else. Not
167
+ * a YAML parser on purpose: the file's `name` and `summary` are prompt and
168
+ * agent text, and a parser would hand all of it back. Only a top-level line
169
+ * for one of the four keys is looked at, with a plain scalar value; a block
170
+ * scalar (`|`, `>`) or anything indented is passed over.
171
+ */
172
+ export function readCopilotWorkspace(text) {
173
+ const out = { cwd: null, gitRoot: null, branch: null, clientName: null };
174
+ for (const line of text.split(/\r?\n/)) {
175
+ const match = /^([a-z_]+):[ \t]*(.*?)[ \t]*$/.exec(line);
176
+ if (!match)
177
+ continue;
178
+ const field = WORKSPACE_KEYS[match[1]];
179
+ if (!field)
180
+ continue;
181
+ let value = match[2];
182
+ if (value === "" || value === "~" || value === "null" || /^[|>]/.test(value))
183
+ continue;
184
+ const quoted = /^(["'])(.*)\1$/.exec(value);
185
+ if (quoted)
186
+ value = quoted[2];
187
+ if (value !== "")
188
+ out[field] = value;
189
+ }
190
+ return out;
191
+ }
192
+ /**
193
+ * The autonomy token for one `permission.requested`. Mapped only where the
194
+ * CLI's words have been seen and their meaning is plain; anything else keeps
195
+ * the CLI's own words under a prefix no band reads, so it lands in `unknown`
196
+ * rather than on a neighbour that happens to share a word.
197
+ */
198
+ export function copilotAutonomyModeOf(agentMode, permissionMode) {
199
+ const agent = nonEmptyString(agentMode);
200
+ const permission = nonEmptyString(permissionMode);
201
+ if (!agent && !permission)
202
+ return null;
203
+ if (agent === "plan")
204
+ return "plan";
205
+ if (permission === "manual" && (agent === null || agent === "interactive"))
206
+ return "default";
207
+ return `copilot:${agent ?? "?"}/${permission ?? "?"}`;
208
+ }
209
+ function tokenCountOf(details, key) {
210
+ return asNumber(asRecord(details[key])?.tokenCount);
211
+ }
212
+ export function shutdownFiguresOf(data) {
213
+ const out = {
214
+ inputTokens: 0,
215
+ outputTokens: 0,
216
+ cacheReadTokens: 0,
217
+ cacheCreationTokens: 0,
218
+ tokensSeen: false,
219
+ linesAdded: null,
220
+ linesRemoved: null,
221
+ filesChangedCount: null,
222
+ totalApiDurationMs: null
223
+ };
224
+ const details = asRecord(data.tokenDetails);
225
+ if (details) {
226
+ out.inputTokens = tokenCountOf(details, "input");
227
+ out.outputTokens = tokenCountOf(details, "output");
228
+ out.cacheReadTokens = tokenCountOf(details, "cache_read");
229
+ out.cacheCreationTokens = tokenCountOf(details, "cache_write");
230
+ out.tokensSeen = true;
231
+ }
232
+ else {
233
+ const metrics = asRecord(data.modelMetrics);
234
+ for (const value of Object.values(metrics ?? {})) {
235
+ const usage = asRecord(asRecord(value)?.usage);
236
+ if (!usage)
237
+ continue;
238
+ const cacheRead = asNumber(usage.cacheReadTokens);
239
+ const cacheWrite = asNumber(usage.cacheWriteTokens);
240
+ out.inputTokens += Math.max(0, asNumber(usage.inputTokens) - cacheRead - cacheWrite);
241
+ out.outputTokens += asNumber(usage.outputTokens);
242
+ out.cacheReadTokens += cacheRead;
243
+ out.cacheCreationTokens += cacheWrite;
244
+ out.tokensSeen = true;
245
+ }
246
+ }
247
+ const changes = asRecord(data.codeChanges);
248
+ if (changes) {
249
+ if (typeof changes.linesAdded === "number")
250
+ out.linesAdded = changes.linesAdded;
251
+ if (typeof changes.linesRemoved === "number")
252
+ out.linesRemoved = changes.linesRemoved;
253
+ if (Array.isArray(changes.filesModified))
254
+ out.filesChangedCount = changes.filesModified.length;
255
+ }
256
+ if (typeof data.totalApiDurationMs === "number")
257
+ out.totalApiDurationMs = data.totalApiDurationMs;
258
+ return out;
259
+ }
260
+ function newFold(sessionId) {
261
+ return {
262
+ sessionId,
263
+ sourceVersion: null,
264
+ selectedModel: null,
265
+ contextCwd: null,
266
+ contextGitRoot: null,
267
+ contextBranch: null,
268
+ firstTs: null,
269
+ lastTs: null,
270
+ humanPrompts: 0,
271
+ afterHoursPrompts: 0,
272
+ humanPromptTimestamps: [],
273
+ subagentPrompts: 0,
274
+ assistantTurns: 0,
275
+ toolCallIds: new Set(),
276
+ toolCallCount: 0,
277
+ subagentToolCallCount: 0,
278
+ toolCalls: [],
279
+ toolResults: 0,
280
+ toolResultErrorCount: 0,
281
+ compactionCount: 0,
282
+ models: new Map(),
283
+ lastServedModel: null,
284
+ modelSwitchCount: 0,
285
+ shutdown: null,
286
+ unknownLines: 0,
287
+ metaLines: 0,
288
+ unparsedLines: 0,
289
+ timelinePoints: [],
290
+ undatedTimelineLines: 0
291
+ };
292
+ }
293
+ /** The `session.start` a log opens with, or null where it has none. */
294
+ function sessionStartOf(lines) {
295
+ for (const line of lines) {
296
+ const sniffed = sniffCopilotLine(line);
297
+ if (sniffed.kind === "event" && sniffed.type === "session.start")
298
+ return sniffed.data;
299
+ }
300
+ return null;
301
+ }
302
+ /** Whether a log is one this extractor can read: a `session.start` whose
303
+ * `version` it knows. */
304
+ export function isReadableCopilotLog(start) {
305
+ return start !== null && typeof start.version === "number" && KNOWN_COPILOT_LOG_VERSIONS.has(start.version);
306
+ }
307
+ /** A `user.message` the parent agent wrote to start a subagent: `source` is `agent-…`. */
308
+ export function isSubagentTask(data) {
309
+ return typeof data.source === "string" && data.source.startsWith("agent-");
310
+ }
311
+ function voteModel(fold, model) {
312
+ if (!model)
313
+ return;
314
+ fold.models.set(model, (fold.models.get(model) ?? 0) + 1);
315
+ }
316
+ function foldLines(fold, lines) {
317
+ // The model in force for events that name none. "auto" is a choice, not a
318
+ // model, so it never takes a vote: the router's pick replaces it.
319
+ let inForce = null;
320
+ let posture = null;
321
+ let startSeen = false;
322
+ for (const line of lines) {
323
+ const sniffed = sniffCopilotLine(line);
324
+ if (sniffed.kind === "unparsed") {
325
+ if (line.trim() !== "")
326
+ fold.unparsedLines += 1;
327
+ continue;
328
+ }
329
+ if (sniffed.kind === "skipped")
330
+ continue;
331
+ if (sniffed.kind === "meta")
332
+ fold.metaLines += 1;
333
+ if (sniffed.kind === "unknown")
334
+ fold.unknownLines += 1;
335
+ const occurredAt = sniffed.occurredAt;
336
+ if (occurredAt) {
337
+ if (!fold.firstTs || occurredAt < fold.firstTs)
338
+ fold.firstTs = occurredAt;
339
+ if (!fold.lastTs || occurredAt > fold.lastTs)
340
+ fold.lastTs = occurredAt;
341
+ }
342
+ let humanHere = false;
343
+ if (sniffed.kind === "event") {
344
+ const data = sniffed.data;
345
+ switch (sniffed.type) {
346
+ case "session.start": {
347
+ if (startSeen)
348
+ break; // Only the first is the session's own.
349
+ startSeen = true;
350
+ const version = nonEmptyString(data.copilotVersion);
351
+ fold.sourceVersion = version && version !== "unknown" ? version : null;
352
+ fold.selectedModel = nonEmptyString(data.selectedModel);
353
+ inForce = fold.selectedModel === "auto" ? null : fold.selectedModel;
354
+ const context = asRecord(data.context);
355
+ fold.contextCwd = nonEmptyString(context?.cwd);
356
+ fold.contextGitRoot = nonEmptyString(context?.gitRoot);
357
+ fold.contextBranch = nonEmptyString(context?.branch);
358
+ break;
359
+ }
360
+ case "session.model_change": {
361
+ const next = nonEmptyString(data.newModel);
362
+ if (next)
363
+ inForce = next === "auto" ? null : next;
364
+ break;
365
+ }
366
+ case "session.auto_mode_resolved": {
367
+ const chosen = nonEmptyString(data.chosenModel);
368
+ if (chosen)
369
+ inForce = chosen;
370
+ break;
371
+ }
372
+ case "user.message": {
373
+ if (isSubagentTask(data)) {
374
+ fold.subagentPrompts += 1;
375
+ break;
376
+ }
377
+ fold.humanPrompts += 1;
378
+ if (occurredAt && isOutsideBusinessHours(new Date(occurredAt)))
379
+ fold.afterHoursPrompts += 1;
380
+ fold.humanPromptTimestamps.push(occurredAt);
381
+ humanHere = true;
382
+ break;
383
+ }
384
+ case "assistant.message": {
385
+ fold.assistantTurns += 1;
386
+ const model = nonEmptyString(data.model) ?? inForce;
387
+ voteModel(fold, model);
388
+ if (model) {
389
+ if (fold.lastServedModel && fold.lastServedModel !== model)
390
+ fold.modelSwitchCount += 1;
391
+ fold.lastServedModel = model;
392
+ }
393
+ break;
394
+ }
395
+ case "tool.execution_start": {
396
+ const id = nonEmptyString(data.toolCallId);
397
+ if (id) {
398
+ if (fold.toolCallIds.has(id))
399
+ break;
400
+ fold.toolCallIds.add(id);
401
+ }
402
+ fold.toolCallCount += 1;
403
+ if (nonEmptyString(data.parentToolCallId))
404
+ fold.subagentToolCallCount += 1;
405
+ voteModel(fold, nonEmptyString(data.model) ?? inForce);
406
+ // An undated call still counts but can't be placed on a rail.
407
+ if (occurredAt) {
408
+ fold.toolCalls.push({
409
+ at: occurredAt,
410
+ name: sanitizeToolName(typeof data.toolName === "string" ? data.toolName : null)
411
+ });
412
+ }
413
+ break;
414
+ }
415
+ case "tool.execution_complete": {
416
+ fold.toolResults += 1;
417
+ if (data.success === false)
418
+ fold.toolResultErrorCount += 1;
419
+ break;
420
+ }
421
+ case "permission.requested": {
422
+ const mode = copilotAutonomyModeOf(data.agentMode, data.permissionMode);
423
+ if (mode)
424
+ posture = mode;
425
+ break;
426
+ }
427
+ case "session.compaction_complete": {
428
+ if (data.success !== false)
429
+ fold.compactionCount += 1;
430
+ break;
431
+ }
432
+ case "session.shutdown": {
433
+ // The last one wins. Every session seen has at most one; a resumed
434
+ // session that shut down twice hasn't been observed, so whether the
435
+ // figures restart or carry on is unknown and not guessed at here.
436
+ fold.shutdown = shutdownFiguresOf(data);
437
+ break;
438
+ }
439
+ default:
440
+ break;
441
+ }
442
+ }
443
+ if (occurredAt) {
444
+ const ms = Date.parse(occurredAt);
445
+ if (Number.isFinite(ms)) {
446
+ // `agentOutput` as the complement of `human` is the nearest-line rule,
447
+ // as in `codex.ts`: the turn and bookkeeping lines aren't classified
448
+ // apart from the agent's output yet, and the handoff says
449
+ // `handsOnBoundary: "nearest_line"` for the same reason.
450
+ fold.timelinePoints.push({ at: ms, human: humanHere, agentOutput: !humanHere, autonomyMode: posture });
451
+ }
452
+ else {
453
+ fold.undatedTimelineLines += 1;
454
+ }
455
+ }
456
+ }
457
+ }
458
+ function topModel(models) {
459
+ let best = null;
460
+ let bestCount = 0;
461
+ for (const [model, count] of models) {
462
+ if (count > bestCount) {
463
+ best = model;
464
+ bestCount = count;
465
+ }
466
+ }
467
+ return best;
468
+ }
469
+ const ACTIVE_GAP_MS = DEFAULT_ACTIVE_GAP_MS;
470
+ const RAPID_REPROMPT_MS = 2 * 60_000;
471
+ function rapidRepromptCountOf(fold) {
472
+ const timestamps = fold.humanPromptTimestamps.filter((t) => t !== null);
473
+ let count = 0;
474
+ for (let i = 1; i < timestamps.length; i++) {
475
+ const gap = Date.parse(timestamps[i]) - Date.parse(timestamps[i - 1]);
476
+ if (Number.isFinite(gap) && gap >= 0 && gap < RAPID_REPROMPT_MS)
477
+ count += 1;
478
+ }
479
+ return count;
480
+ }
481
+ function sessionDurationMs(fold) {
482
+ if (!fold.firstTs || !fold.lastTs)
483
+ return null;
484
+ const ms = Date.parse(fold.lastTs) - Date.parse(fold.firstTs);
485
+ return Number.isFinite(ms) && ms >= 0 ? ms : null;
486
+ }
487
+ /**
488
+ * The sessions under a snapshot, by id, in id order. A directory's
489
+ * `events.jsonl` is the log; a flat `<id>.jsonl` is read only where its
490
+ * directory has none. `duplicates` counts the flat files passed over.
491
+ */
492
+ export async function listCopilotSessions(snapshotDir) {
493
+ let entries;
494
+ try {
495
+ entries = await fs.readdir(snapshotDir, { withFileTypes: true });
496
+ }
497
+ catch {
498
+ return { sessions: [], duplicates: 0 };
499
+ }
500
+ const dirs = new Set();
501
+ const flat = new Set();
502
+ for (const entry of entries) {
503
+ if (entry.isDirectory() && !entry.name.startsWith("."))
504
+ dirs.add(entry.name);
505
+ else if (entry.isFile() && entry.name.endsWith(".jsonl"))
506
+ flat.add(entry.name.slice(0, -".jsonl".length));
507
+ }
508
+ const exists = async (p) => {
509
+ try {
510
+ return (await fs.stat(p)).isFile();
511
+ }
512
+ catch {
513
+ return false;
514
+ }
515
+ };
516
+ const sessions = [];
517
+ let duplicates = 0;
518
+ for (const id of [...new Set([...dirs, ...flat])].sort()) {
519
+ const dirLog = path.join(snapshotDir, id, "events.jsonl");
520
+ const workspace = path.join(snapshotDir, id, "workspace.yaml");
521
+ const workspacePath = dirs.has(id) && (await exists(workspace)) ? workspace : null;
522
+ if (dirs.has(id) && (await exists(dirLog))) {
523
+ if (flat.has(id))
524
+ duplicates += 1;
525
+ sessions.push({ sessionId: id, logPath: dirLog, workspacePath });
526
+ }
527
+ else if (flat.has(id)) {
528
+ sessions.push({ sessionId: id, logPath: path.join(snapshotDir, `${id}.jsonl`), workspacePath });
529
+ }
530
+ // A directory with neither log is a session the CLI holds nothing for.
531
+ }
532
+ return { sessions, duplicates };
533
+ }
534
+ export async function* extractCopilotCli(snapshotDir, extractionId) {
535
+ const { sessions, duplicates } = await listCopilotSessions(snapshotDir);
536
+ if (sessions.length === 0)
537
+ return;
538
+ let windowOldest = null;
539
+ let windowNewest = null;
540
+ let sessionCount = 0;
541
+ let unparsedSessions = 0;
542
+ let unreadableSessions = 0;
543
+ let malformedLines = 0;
544
+ for (const files of sessions) {
545
+ let text;
546
+ try {
547
+ text = await fs.readFile(files.logPath, "utf8");
548
+ }
549
+ catch {
550
+ unreadableSessions += 1;
551
+ continue;
552
+ }
553
+ const lines = text.split("\n");
554
+ const start = sessionStartOf(lines);
555
+ if (!isReadableCopilotLog(start)) {
556
+ unparsedSessions += 1;
557
+ continue;
558
+ }
559
+ let workspace = { cwd: null, gitRoot: null, branch: null, clientName: null };
560
+ if (files.workspacePath) {
561
+ try {
562
+ workspace = readCopilotWorkspace(await fs.readFile(files.workspacePath, "utf8"));
563
+ }
564
+ catch {
565
+ // No workspace file to read: session.start's context still names the cwd.
566
+ }
567
+ }
568
+ const fold = newFold(nonEmptyString(start?.sessionId) ?? files.sessionId);
569
+ foldLines(fold, lines);
570
+ malformedLines += fold.unparsedLines;
571
+ if (!fold.firstTs || !fold.lastTs)
572
+ continue;
573
+ sessionCount += 1;
574
+ if (!windowOldest || fold.firstTs < windowOldest)
575
+ windowOldest = fold.firstTs;
576
+ if (!windowNewest || fold.lastTs > windowNewest)
577
+ windowNewest = fold.lastTs;
578
+ const repoRef = workspace.gitRoot ?? fold.contextGitRoot ?? workspace.cwd ?? fold.contextCwd;
579
+ const gitBranch = workspace.branch ?? fold.contextBranch;
580
+ const base = {
581
+ store: "copilot_cli",
582
+ sourceVersion: fold.sourceVersion,
583
+ sessionRef: fold.sessionId,
584
+ repoRef,
585
+ extractionId
586
+ };
587
+ for (const ts of fold.humanPromptTimestamps) {
588
+ if (!ts)
589
+ continue;
590
+ yield { ...base, occurredAt: ts, eventKind: "ai_prompt_submitted", metrics: {}, provenance: HISTORICAL_PROVENANCE.direct };
591
+ }
592
+ for (const call of fold.toolCalls) {
593
+ yield {
594
+ ...base,
595
+ occurredAt: call.at,
596
+ eventKind: "ai_tool_call_started",
597
+ metrics: { toolName: call.name },
598
+ provenance: HISTORICAL_PROVENANCE.direct
599
+ };
600
+ }
601
+ const durationMs = sessionDurationMs(fold);
602
+ const split = splitActiveTime(fold.timelinePoints, { activeGapMs: ACTIVE_GAP_MS });
603
+ const metrics = {
604
+ promptCount: fold.humanPrompts,
605
+ subagentPrompts: fold.subagentPrompts,
606
+ assistantTurns: fold.assistantTurns,
607
+ toolCallCount: fold.toolCallCount,
608
+ subagentToolCallCount: fold.subagentToolCallCount,
609
+ toolResultCount: fold.toolResults,
610
+ durationBucket: durationMs === null ? "unknown" : bucketDurationMs(durationMs) ?? "unknown",
611
+ afterHoursPrompts: fold.afterHoursPrompts,
612
+ unknownLines: fold.unknownLines,
613
+ metaLines: fold.metaLines,
614
+ unparsedLines: fold.unparsedLines,
615
+ modelCount: fold.models.size,
616
+ modelSwitchCount: fold.modelSwitchCount,
617
+ compactionCount: fold.compactionCount,
618
+ toolResultErrorCount: fold.toolResultErrorCount,
619
+ toolFailureCount: fold.toolResultErrorCount,
620
+ rapidRepromptCount: rapidRepromptCountOf(fold),
621
+ activeMinutes: minutesOf(split.handsOnMs + split.agentSupervisingMs),
622
+ handsOnMinutes: minutesOf(split.handsOnMs),
623
+ agentSupervisingMinutes: minutesOf(split.agentSupervisingMs),
624
+ activeSplitInstants: split.instants,
625
+ activeSplitUndatedLines: fold.undatedTimelineLines,
626
+ activeSplitUnposturedInstants: split.unposturedInstants,
627
+ sessionStartedAt: fold.firstTs
628
+ };
629
+ if (durationMs !== null)
630
+ metrics.sessionMinutes = Math.round(durationMs / 60_000);
631
+ const primaryModel = topModel(fold.models);
632
+ if (primaryModel)
633
+ metrics.primaryModel = primaryModel;
634
+ if (fold.selectedModel) {
635
+ metrics.selectedModel = fold.selectedModel;
636
+ const selectedClass = classifyModelClass(fold.selectedModel);
637
+ if (selectedClass)
638
+ metrics.selectedModelClass = selectedClass;
639
+ }
640
+ if (gitBranch)
641
+ metrics.gitBranch = gitBranch;
642
+ if (workspace.clientName)
643
+ metrics.clientName = workspace.clientName;
644
+ // Shutdown figures only where there was a shutdown. Without one the
645
+ // session was killed or is still open, and 0 would read as measured.
646
+ const shutdown = fold.shutdown;
647
+ if (shutdown?.tokensSeen) {
648
+ metrics.inputTokens = shutdown.inputTokens;
649
+ metrics.outputTokens = shutdown.outputTokens;
650
+ metrics.cacheReadTokens = shutdown.cacheReadTokens;
651
+ metrics.cacheCreationTokens = shutdown.cacheCreationTokens;
652
+ }
653
+ else {
654
+ metrics.tokensNotCollected = true;
655
+ }
656
+ if (shutdown?.linesAdded != null)
657
+ metrics.linesAdded = shutdown.linesAdded;
658
+ if (shutdown?.linesRemoved != null)
659
+ metrics.linesRemoved = shutdown.linesRemoved;
660
+ if (shutdown?.filesChangedCount != null)
661
+ metrics.filesChangedCount = shutdown.filesChangedCount;
662
+ if (shutdown?.totalApiDurationMs != null)
663
+ metrics.totalApiDurationMs = shutdown.totalApiDurationMs;
664
+ yield {
665
+ ...base,
666
+ occurredAt: fold.lastTs,
667
+ eventKind: "create_focus_session",
668
+ metrics,
669
+ dayBreakdown: sliceSessionByLocalDay(fold.humanPromptTimestamps, {
670
+ activeGapMs: ACTIVE_GAP_MS,
671
+ activeInstants: fold.timelinePoints
672
+ }),
673
+ activeSpans: activeSpans(fold.timelinePoints, { activeGapMs: ACTIVE_GAP_MS }).spans,
674
+ autonomySplit: Object.fromEntries(Object.entries(split.supervisingMsByBand).map(([band, ms]) => [band, minutesOf(ms)])),
675
+ provenance: HISTORICAL_PROVENANCE.derived
676
+ };
677
+ if (fold.afterHoursPrompts > 0) {
678
+ yield {
679
+ ...base,
680
+ occurredAt: fold.lastTs,
681
+ eventKind: "after_hours_ai_session",
682
+ metrics: { afterHoursPrompts: fold.afterHoursPrompts },
683
+ provenance: HISTORICAL_PROVENANCE.derived
684
+ };
685
+ }
686
+ if (fold.toolResultErrorCount > 0) {
687
+ yield {
688
+ ...base,
689
+ occurredAt: fold.lastTs,
690
+ eventKind: "tool_failure",
691
+ metrics: { toolFailureCount: fold.toolResultErrorCount, toolResultErrorCount: fold.toolResultErrorCount },
692
+ provenance: HISTORICAL_PROVENANCE.derived
693
+ };
694
+ }
695
+ }
696
+ const unread = unparsedSessions + unreadableSessions;
697
+ if ((windowOldest && windowNewest) || unread > 0) {
698
+ const window = windowOldest && windowNewest ? { windowOldest, windowNewest } : {};
699
+ yield {
700
+ occurredAt: windowNewest ?? new Date().toISOString(),
701
+ store: "copilot_cli",
702
+ sourceVersion: null,
703
+ sessionRef: null,
704
+ repoRef: null,
705
+ eventKind: "extraction_epoch",
706
+ metrics: {
707
+ ...window,
708
+ sessionCount,
709
+ unparsedChatSessionFiles: unparsedSessions,
710
+ unreadableChatSessionFiles: unreadableSessions,
711
+ malformedChatSessionLines: malformedLines,
712
+ duplicateChatSessions: duplicates
713
+ },
714
+ provenance: HISTORICAL_PROVENANCE.derived,
715
+ extractionId
716
+ };
717
+ }
718
+ }
719
+ //# sourceMappingURL=copilotCli.js.map