-
Notifications
You must be signed in to change notification settings - Fork 39.3k
Expand file tree
/
Copy pathchatDebugFileLoggerService.ts
More file actions
1431 lines (1297 loc) · 52.7 KB
/
chatDebugFileLoggerService.ts
File metadata and controls
1431 lines (1297 loc) · 52.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as fs from 'fs';
import * as vscode from 'vscode';
import { IChatDebugFileLoggerService, IDebugLogEntry, sessionResourceToId } from '../../../platform/chat/common/chatDebugFileLoggerService';
import { ConfigKey, IConfigurationService } from '../../../platform/configuration/common/configurationService';
import { IEnvService } from '../../../platform/env/common/envService';
import { IVSCodeExtensionContext } from '../../../platform/extContext/common/extensionContext';
import { createDirectoryIfNotExists, IFileSystemService } from '../../../platform/filesystem/common/fileSystemService';
import { ILogService } from '../../../platform/log/common/logService';
import { CopilotChatAttr, GenAiAttr, GenAiOperationName } from '../../../platform/otel/common/index';
import { ICompletedSpanData, IOTelService, ISpanEventData, SpanStatusCode } from '../../../platform/otel/common/otelService';
import { IExperimentationService } from '../../../platform/telemetry/common/nullExperimentationService';
import { ITelemetryService } from '../../../platform/telemetry/common/telemetry';
import { Emitter } from '../../../util/vs/base/common/event';
import { Disposable } from '../../../util/vs/base/common/lifecycle';
import { extUriBiasedIgnorePathCase } from '../../../util/vs/base/common/resources';
import { URI } from '../../../util/vs/base/common/uri';
import { IExtensionContribution } from '../../common/contributions';
const DEBUG_LOGS_DIR_NAME = 'debug-logs';
const DEFAULT_MAX_RETAINED_LOGS = 50;
const DEFAULT_FLUSH_INTERVAL_MS = 4_000;
const MIN_FLUSH_INTERVAL_MS = 2_000;
const MAX_ATTR_VALUE_LENGTH = 5_000;
const MAX_PENDING_CORE_EVENTS = 100;
const DEFAULT_MAX_SESSION_LOG_MB = 100;
const TRUNCATION_RETAIN_RATIO = 0.6; // retain 60% of max on truncation
const MAX_SPAN_SESSION_INDEX = 10_000;
interface IActiveLogSession {
readonly uri: URI;
/** The directory containing this session's log files */
readonly sessionDir: URI;
readonly buffer: string[];
flushPromise: Promise<void>;
dirEnsured: boolean;
bytesWritten: number;
/** Parent session ID if this is a child session (e.g., title, categorization) */
readonly parentSessionId?: string;
/** Label for child sessions (e.g., 'title', 'categorization') */
readonly label?: string;
/** Whether this session has received its own OTel spans (vs being auto-created as a parent ref) */
hasOwnSpans: boolean;
/** Whether models.json has already been written to this session's directory */
modelSnapshotWritten: boolean;
/** Key identifying the last-written system prompt: model + agent/mode name (undefined = none written yet) */
systemPromptKey: string | undefined;
/** Index of the next system_prompt file to write */
systemPromptIndex: number;
/** File name of the most recently written system prompt (e.g., 'system_prompt_0.json') */
currentSystemPromptFile: string | undefined;
/** Key identifying the last-written tools file: model + agent/mode name (undefined = none written yet) */
toolsKey: string | undefined;
/** Index of the next tools file to write */
toolsIndex: number;
/** File name of the most recently written tools file (e.g., 'tools_0.json') */
currentToolsFile: string | undefined;
/** Pending tool definitions received before the session was promoted to hasOwnSpans */
pendingToolDefs: string | undefined;
/** Whether we've already checked disk for a previous session directory (prevents repeated sync FS calls) */
resumeChecked: boolean;
/** Run index: 0 for the first run, incremented on each VS Code restart that resumes this session */
runIndex: number;
}
// IDebugLogEntry is imported from and defined in the platform layer.
// Re-export for consumers that import from this file.
export type { IDebugLogEntry } from '../../../platform/chat/common/chatDebugFileLoggerService';
export class ChatDebugFileLoggerService extends Disposable implements IChatDebugFileLoggerService {
declare readonly _serviceBrand: undefined;
public readonly id = 'chatDebugFileLogger';
private readonly _onDidEmitEntry = this._register(new Emitter<{ sessionId: string; entry: IDebugLogEntry }>());
readonly onDidEmitEntry = this._onDidEmitEntry.event;
private readonly _activeSessions = new Map<string, IActiveLogSession>();
/** Maps child session ID → { parentSessionId, label } for child session routing */
private readonly _childSessionMap = new Map<string, { parentSessionId: string; label: string; parentToolSpanId?: string }>();
/** Maps spanId → resolved session ID for parent-span inheritance */
private readonly _spanSessionIndex = new Map<string, string>();
private readonly _pendingCoreEvents: IDebugLogEntry[] = [];
private _modelSnapshot: readonly unknown[] | undefined;
private _debugLogsDirUri: URI | undefined;
private _autoFlushTimer: ReturnType<typeof setInterval> | undefined;
private _autoFlushIntervalMs: number;
private _maxSessionLogBytes: number;
private _totalBytesWritten = 0;
private _totalSessionCount = 0;
constructor(
@IOTelService private readonly _otelService: IOTelService,
@IFileSystemService private readonly _fileSystemService: IFileSystemService,
@IVSCodeExtensionContext private readonly _extensionContext: IVSCodeExtensionContext,
@ILogService private readonly _logService: ILogService,
@IConfigurationService private readonly _configurationService: IConfigurationService,
@IExperimentationService private readonly _experimentationService: IExperimentationService,
@ITelemetryService private readonly _telemetryService: ITelemetryService,
@IEnvService private readonly _envService: IEnvService,
) {
super();
const enabled = this._configurationService.getExperimentBasedConfig(ConfigKey.Advanced.ChatDebugFileLogging, this._experimentationService);
if (!enabled) {
/* __GDPR__
"chatDebugFileLogger.disabled" : {
"owner": "vijayupadya",
"comment": "Chat debug file logging is disabled via experiment or config"
}
*/
this._telemetryService.sendMSFTTelemetryEvent('chatDebugFileLogger.disabled');
this._autoFlushIntervalMs = DEFAULT_FLUSH_INTERVAL_MS;
this._maxSessionLogBytes = DEFAULT_MAX_SESSION_LOG_MB * 1024 * 1024;
return;
}
this._autoFlushIntervalMs = Math.max(MIN_FLUSH_INTERVAL_MS, this._configurationService.getConfig(ConfigKey.Advanced.ChatDebugFileLoggingFlushInterval) ?? DEFAULT_FLUSH_INTERVAL_MS);
this._maxSessionLogBytes = this._resolveMaxSessionLogBytes();
// React to changes at runtime
this._register(this._configurationService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration(ConfigKey.Advanced.ChatDebugFileLoggingFlushInterval.fullyQualifiedId)) {
this._autoFlushIntervalMs = Math.max(MIN_FLUSH_INTERVAL_MS, this._configurationService.getConfig(ConfigKey.Advanced.ChatDebugFileLoggingFlushInterval) ?? DEFAULT_FLUSH_INTERVAL_MS);
this._restartFlushTimer();
}
if (e.affectsConfiguration(ConfigKey.Advanced.ChatDebugFileLoggingMaxSessionLogSizeMB.fullyQualifiedId)) {
this._maxSessionLogBytes = this._resolveMaxSessionLogBytes();
}
}));
// Subscribe to OTel span completions
this._register(this._otelService.onDidCompleteSpan(span => {
this._onSpanCompleted(span);
}));
// Subscribe to OTel span events (real-time user messages)
this._register(this._otelService.onDidEmitSpanEvent(event => {
this._onSpanEvent(event);
}));
// Subscribe to core debug events (discovery, skill loading, etc.)
if (typeof vscode.chat?.onDidReceiveChatDebugEvent === 'function') {
this._register(vscode.chat.onDidReceiveChatDebugEvent(event => {
this._onCoreDebugEvent(event);
}));
}
}
private _resolveMaxSessionLogBytes(): number {
const raw = this._configurationService.getExperimentBasedConfig(ConfigKey.Advanced.ChatDebugFileLoggingMaxSessionLogSizeMB, this._experimentationService);
const mb = typeof raw === 'number' && Number.isFinite(raw) ? raw : DEFAULT_MAX_SESSION_LOG_MB;
return Math.max(1, Math.floor(mb)) * 1024 * 1024;
}
override dispose(): void {
if (this._autoFlushTimer) {
clearInterval(this._autoFlushTimer);
this._autoFlushTimer = undefined;
}
// Accumulate any remaining active session bytes before emitting telemetry
for (const session of this._activeSessions.values()) {
this._totalBytesWritten += session.bytesWritten;
}
/* __GDPR__
"chatDebugFileLogger.end" : {
"owner": "vijayupadya",
"comment": "Chat debug file logger is being disposed",
"totalBytesWritten": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true, "comment": "Total bytes written across all sessions" },
"sessionCount": { "classification": "SystemMetaData", "purpose": "PerformanceAndHealth", "isMeasurement": true, "comment": "Total number of sessions logged" }
}
*/
this._telemetryService.sendMSFTTelemetryEvent('chatDebugFileLogger.end', undefined, { totalBytesWritten: this._totalBytesWritten, sessionCount: this._totalSessionCount });
super.dispose();
}
public get debugLogsDir(): URI | undefined {
return this._getDebugLogsDir();
}
private _getDebugLogsDir(): URI | undefined {
if (this._debugLogsDirUri) {
return this._debugLogsDirUri;
}
const storageUri = this._extensionContext.storageUri as URI | undefined;
if (!storageUri) {
return undefined;
}
this._debugLogsDirUri = URI.joinPath(storageUri, DEBUG_LOGS_DIR_NAME);
return this._debugLogsDirUri;
}
async startSession(sessionId: string): Promise<void> {
this._ensureSession(sessionId, /* hasOwnSpans */ true);
}
startChildSession(childSessionId: string, parentSessionId: string, label: string, parentToolSpanId?: string): void {
if (!this._childSessionMap.has(childSessionId)) {
this._childSessionMap.set(childSessionId, { parentSessionId, label, parentToolSpanId });
}
}
registerSpanSession(spanId: string, sessionId: string): void {
this._spanSessionIndex.set(spanId, sessionId);
// Apply same eviction cap as _onSpanCompleted
if (this._spanSessionIndex.size > MAX_SPAN_SESSION_INDEX) {
const excess = this._spanSessionIndex.size - MAX_SPAN_SESSION_INDEX;
const iter = this._spanSessionIndex.keys();
for (let i = 0; i < excess; i++) {
this._spanSessionIndex.delete(iter.next().value!);
}
}
}
/**
* Synchronously ensure a session exists for buffering. Directory creation
* and old-log cleanup are deferred to the first flush.
*
* Sessions are organized in directories:
* - Parent session: `debug-logs/<sessionId>/main.jsonl`
* - Child session: `debug-logs/<parentSessionId>/<label>-<childSessionId>.jsonl`
*/
private _ensureSession(sessionId: string, hasOwnSpans = false): void {
const existing = this._activeSessions.get(sessionId);
if (existing) {
// Mark that this session now has its own spans (upgrades from auto-created parent ref)
if (hasOwnSpans && !existing.hasOwnSpans) {
existing.hasOwnSpans = true;
// Now that we know this is a real session, replay pending core events
if (!existing.parentSessionId) {
this._emitSessionStartAndReplay(sessionId, existing);
}
}
return;
}
this._totalSessionCount++;
const dir = this._getDebugLogsDir();
if (!dir) {
return;
}
const childInfo = this._childSessionMap.get(sessionId);
let sessionDir: URI;
let fileUri: URI;
if (childInfo) {
// Child session — write under parent's directory
sessionDir = URI.joinPath(dir, childInfo.parentSessionId);
const safeLabel = childInfo.label.replace(/[/\\:*?"<>|\x00-\x1f]/g, '_').replace(/\.\./g, '_');
const fileName = `${safeLabel}-${sessionId}.jsonl`;
fileUri = URI.joinPath(sessionDir, fileName);
// Ensure parent session exists so we can write a cross-reference.
// A child referencing a parent proves it is a main user session,
// so promote it with hasOwnSpans = true.
this._ensureSession(childInfo.parentSessionId, /* hasOwnSpans */ true);
// Write a cross-reference entry in the parent's main.jsonl
this._bufferEntry(childInfo.parentSessionId, {
ts: Date.now(),
dur: 0,
sid: childInfo.parentSessionId,
type: 'child_session_ref',
name: childInfo.label,
spanId: `child-ref-${sessionId}`,
...(childInfo.parentToolSpanId ? { parentSpanId: childInfo.parentToolSpanId } : {}),
status: 'ok',
attrs: {
childSessionId: sessionId,
childLogFile: `${safeLabel}-${sessionId}.jsonl`,
label: childInfo.label,
},
});
} else {
// Parent session — write as main.jsonl in its own directory
sessionDir = URI.joinPath(dir, sessionId);
fileUri = URI.joinPath(sessionDir, 'main.jsonl');
}
const session: IActiveLogSession = {
uri: fileUri,
sessionDir,
buffer: [],
flushPromise: Promise.resolve(),
dirEnsured: false,
bytesWritten: 0,
parentSessionId: childInfo?.parentSessionId,
label: childInfo?.label,
hasOwnSpans,
modelSnapshotWritten: false,
systemPromptKey: undefined,
systemPromptIndex: 0,
currentSystemPromptFile: undefined,
toolsKey: undefined,
toolsIndex: 0,
currentToolsFile: undefined,
pendingToolDefs: undefined,
resumeChecked: false,
runIndex: 0,
};
this._activeSessions.set(sessionId, session);
// Write a session_start entry so every JSONL file has a version header.
// For parent sessions with their own spans, also replay pending core events.
if (childInfo) {
// Child sessions get a minimal session_start (no core event replay)
this._bufferEntry(sessionId, {
v: 1,
ts: Date.now(),
dur: 0,
sid: sessionId,
type: 'session_start',
name: 'session_start',
spanId: `session-start-${sessionId}`,
status: 'ok',
attrs: {
copilotVersion: this._envService.getVersion(),
vscodeVersion: this._envService.vscodeVersion,
parentSessionId: childInfo.parentSessionId,
label: childInfo.label,
},
});
} else if (hasOwnSpans) {
this._emitSessionStartAndReplay(sessionId, session);
}
// Start auto-flush timer if this is the first active session
if (this._activeSessions.size === 1 && !this._autoFlushTimer) {
this._autoFlushTimer = setInterval(() => this._autoFlushAll(), this._autoFlushIntervalMs);
}
// Fire-and-forget cleanup of old logs
this._cleanupOldLogs().catch(() => { });
}
async endSession(sessionId: string): Promise<void> {
await this.flush(sessionId);
const session = this._activeSessions.get(sessionId);
if (session) {
this._totalBytesWritten += session.bytesWritten;
}
this._activeSessions.delete(sessionId);
// Clean up span→session mappings for this session
for (const [spanId, sid] of this._spanSessionIndex) {
if (sid === sessionId) {
this._spanSessionIndex.delete(spanId);
}
}
// Stop auto-flush timer if no active sessions remain
if (this._activeSessions.size === 0 && this._autoFlushTimer) {
clearInterval(this._autoFlushTimer);
this._autoFlushTimer = undefined;
}
}
async flush(sessionId: string): Promise<void> {
const session = this._activeSessions.get(sessionId);
if (!session) {
return;
}
if (session.buffer.length === 0) {
// No JSONL entries to flush, but still await any pending async work
// (e.g. model snapshot write) enqueued on flushPromise.
return session.flushPromise;
}
// Skip flushing for sessions not yet confirmed as main sessions.
// Keep the buffer intact so entries are preserved if the session
// is promoted later (e.g., when a child span references it).
if (!session.parentSessionId && !session.hasOwnSpans) {
return;
}
const lines = session.buffer.splice(0);
const content = lines.join('');
session.flushPromise = session.flushPromise.then(
() => this._writeToFile(session, content),
() => this._writeToFile(session, content),
);
return session.flushPromise;
}
getLogPath(sessionId: string): URI | undefined {
const active = this._activeSessions.get(sessionId);
if (active) {
return active.uri;
}
// For child sessions, construct the correct <label>-<sessionId>.jsonl path
const childInfo = this._childSessionMap.get(sessionId);
if (childInfo) {
const dir = this._getDebugLogsDir();
if (!dir) { return undefined; }
const parentDir = URI.joinPath(dir, childInfo.parentSessionId);
return URI.joinPath(parentDir, `${childInfo.label}-${sessionId}.jsonl`);
}
// For historical sessions (after restart), construct the default path
const sessionDir = this.getSessionDir(sessionId);
return sessionDir ? URI.joinPath(sessionDir, 'main.jsonl') : undefined;
}
getSessionDir(sessionId: string): URI | undefined {
// If active, use the stored sessionDir (already points to parent dir for children)
const active = this._activeSessions.get(sessionId);
if (active) {
return active.sessionDir;
}
// If known as a child, resolve to the parent's directory
const childInfo = this._childSessionMap.get(sessionId);
if (childInfo) {
const dir = this._getDebugLogsDir();
return dir ? URI.joinPath(dir, childInfo.parentSessionId) : undefined;
}
// Unknown session — construct the default path (assuming it's a parent)
const dir = this._getDebugLogsDir();
return dir ? URI.joinPath(dir, sessionId) : undefined;
}
getActiveSessionIds(): string[] {
return [...this._activeSessions.keys()];
}
isDebugLogUri(uri: URI): boolean {
const dir = this._getDebugLogsDir();
if (!dir) {
return false;
}
return extUriBiasedIgnorePathCase.isEqualOrParent(uri, dir);
}
getSessionDirForResource(sessionResource: URI): URI | undefined {
const sessionId = sessionResourceToId(sessionResource);
return this.getSessionDir(sessionId);
}
setModelSnapshot(models: readonly unknown[]): void {
this._modelSnapshot = models;
// Write to any active parent sessions that started before the model fetch completed
for (const [, session] of this._activeSessions) {
if (!session.parentSessionId && session.hasOwnSpans) {
this._enqueueModelSnapshotWrite(session);
}
}
}
private _enqueueModelSnapshotWrite(session: IActiveLogSession): void {
session.flushPromise = session.flushPromise.then(
() => this._writeModelSnapshot(session),
() => this._writeModelSnapshot(session),
);
}
private async _writeModelSnapshot(session: IActiveLogSession): Promise<void> {
if (!this._modelSnapshot || session.modelSnapshotWritten) {
return;
}
try {
if (!session.dirEnsured) {
await createDirectoryIfNotExists(this._fileSystemService, session.sessionDir);
session.dirEnsured = true;
}
const modelsUri = URI.joinPath(session.sessionDir, 'models.json');
await fs.promises.writeFile(modelsUri.fsPath, JSON.stringify(this._modelSnapshot, null, 2), 'utf-8');
session.modelSnapshotWritten = true;
} catch (err) {
this._logService.error('[ChatDebugFileLogger] Failed to write models.json', err);
}
}
private _enqueueFileWrite(session: IActiveLogSession, content: string, fileName: string): void {
session.flushPromise = session.flushPromise.then(
() => this._writeSessionFile(session, content, fileName),
() => this._writeSessionFile(session, content, fileName),
);
}
private async _writeSessionFile(session: IActiveLogSession, content: string, fileName: string): Promise<void> {
try {
if (!session.dirEnsured) {
await createDirectoryIfNotExists(this._fileSystemService, session.sessionDir);
session.dirEnsured = true;
}
const fileUri = URI.joinPath(session.sessionDir, fileName);
await fs.promises.writeFile(fileUri.fsPath, fileName.endsWith('.json') ? JSON.stringify({ content }, null, 2) : content, 'utf-8');
} catch (err) {
this._logService.error(`[ChatDebugFileLogger] Failed to write ${fileName}`, err);
}
}
/**
* Emit a session_start entry, replay cached core events, and write the model snapshot.
* Called when a parent session is first promoted to hasOwnSpans.
*/
private _emitSessionStartAndReplay(sessionId: string, session: IActiveLogSession): void {
this._bufferEntry(sessionId, {
v: 1,
ts: Date.now(),
dur: 0,
sid: sessionId,
type: 'session_start',
name: 'session_start',
spanId: `session-start-${sessionId}`,
status: 'ok',
attrs: {
copilotVersion: this._envService.getVersion(),
vscodeVersion: this._envService.vscodeVersion,
},
});
for (const entry of this._pendingCoreEvents) {
this._bufferEntry(sessionId, { ...entry, sid: sessionId });
}
if (this._modelSnapshot) {
this._enqueueModelSnapshotWrite(session);
}
// Write pending tool definitions that arrived before the session was promoted
if (session.pendingToolDefs) {
const fileName = `tools_${session.toolsIndex}.json`;
session.toolsIndex++;
session.currentToolsFile = fileName;
this._enqueueFileWrite(session, session.pendingToolDefs, fileName);
session.pendingToolDefs = undefined;
}
}
// ── OTel span handling ──
private _onSpanCompleted(span: ICompletedSpanData): void {
const sessionId = this._extractSessionId(span);
if (!sessionId) {
return;
}
// Record the span→session mapping so child spans can inherit it
this._spanSessionIndex.set(span.spanId, sessionId);
if (this._spanSessionIndex.size > MAX_SPAN_SESSION_INDEX) {
// Evict oldest entries (Map iterates in insertion order)
const excess = this._spanSessionIndex.size - MAX_SPAN_SESSION_INDEX;
const iter = this._spanSessionIndex.keys();
for (let i = 0; i < excess; i++) {
this._spanSessionIndex.delete(iter.next().value!);
}
}
// Check if this span carries parent session info (e.g., title, categorization)
const parentChatSessionId = asString(span.attributes[CopilotChatAttr.PARENT_CHAT_SESSION_ID]);
const debugLogLabel = asString(span.attributes[CopilotChatAttr.DEBUG_LOG_LABEL]);
if (parentChatSessionId && debugLogLabel && !this._childSessionMap.has(sessionId)) {
this._childSessionMap.set(sessionId, { parentSessionId: parentChatSessionId, label: debugLogLabel });
}
const entry = this._spanToEntry(span, sessionId);
const opName = asString(span.attributes[GenAiAttr.OPERATION_NAME]);
const outputMessages = opName === GenAiOperationName.CHAT
? asString(span.attributes[GenAiAttr.OUTPUT_MESSAGES])
: undefined;
// Never auto-promote sessions from OTel spans. Sub-requests like
// title generation, categorization, and progress-message generation
// each carry their own session IDs with real CHAT content but should
// not create top-level folders. A session is only promoted to "real"
// (hasOwnSpans = true) when:
// 1. startSession() is called explicitly, or
// 2. A child span references it via PARENT_CHAT_SESSION_ID
// (handled in _ensureSession's child branch).
// 3. The session directory already exists on disk (resumed after restart).
this._ensureSession(sessionId);
// Auto-promote resumed sessions: if the session was just created with
// hasOwnSpans = false but has an existing JSONL directory from a previous
// extension lifecycle, promote it. This handles sessions continued after
// VS Code restart where title/categorization won't re-fire.
const session = this._activeSessions.get(sessionId);
if (session && !session.hasOwnSpans && !session.parentSessionId && !session.resumeChecked) {
session.resumeChecked = true;
const mainJsonl = URI.joinPath(session.sessionDir, 'main.jsonl');
try {
fs.accessSync(mainJsonl.fsPath);
// Directory exists from a previous run — this is a resumed session
session.hasOwnSpans = true;
session.dirEnsured = true;
// Determine the run index: read the max rIdx from the existing file + 1
try {
const tail = this._readTailBytes(mainJsonl.fsPath, 8192);
let maxRIdx = 0;
for (const line of tail.split('\n')) {
if (!line.trim()) { continue; }
try {
const parsed = JSON.parse(line);
if (typeof parsed.rIdx === 'number' && parsed.rIdx > maxRIdx) {
maxRIdx = parsed.rIdx;
}
} catch { /* skip malformed lines */ }
}
session.runIndex = maxRIdx + 1;
} catch { /* file read failed — runIndex stays at 0, but that's safe since this is a back-compat path */ }
// Find the next available indices for companion files to avoid
// overwriting ones from the previous run. Single readdir + scan.
try {
for (const f of fs.readdirSync(session.sessionDir.fsPath)) {
const spIdx = f.startsWith('system_prompt_') ? parseInt(f.slice(14), 10) : -1;
if (spIdx >= session.systemPromptIndex) { session.systemPromptIndex = spIdx + 1; }
const tIdx = f.startsWith('tools_') ? parseInt(f.slice(6), 10) : -1;
if (tIdx >= session.toolsIndex) { session.toolsIndex = tIdx + 1; }
}
} catch { /* readdir failed — indices stay at 0 */ }
} catch {
// No existing directory — leave as is
}
}
// Write system_prompt JSON when model or mode changes (before buffering so llm_request gets the file ref)
if (opName === GenAiOperationName.CHAT) {
const session = this._activeSessions.get(sessionId);
if (session && session.hasOwnSpans && !session.parentSessionId) {
const model = asString(span.attributes[GenAiAttr.REQUEST_MODEL])
?? asString(span.attributes[GenAiAttr.RESPONSE_MODEL])
?? 'unknown';
const systemInstructions = asString(span.attributes[GenAiAttr.SYSTEM_INSTRUCTIONS]);
if (systemInstructions) {
const key = `${model}:${systemInstructions.length}`;
if (key !== session.systemPromptKey) {
const fileName = `system_prompt_${session.systemPromptIndex}.json`;
session.systemPromptKey = key;
session.systemPromptIndex++;
session.currentSystemPromptFile = fileName;
this._enqueueFileWrite(session, systemInstructions, fileName);
}
}
}
}
if (entry) {
// Attach current system prompt and tools file references to llm_request entries
if (entry.type === 'llm_request') {
const session = this._activeSessions.get(sessionId);
if (session?.currentSystemPromptFile) {
entry.attrs.systemPromptFile = session.currentSystemPromptFile;
}
if (session?.currentToolsFile) {
entry.attrs.toolsFile = session.currentToolsFile;
}
}
this._bufferEntry(sessionId, entry);
}
// Note: user_message events are captured in real-time via _onSpanEvent
// (onDidEmitSpanEvent) to avoid duplicates, since span.events also
// contains them after completion.
// Extract agent_response from output messages (on chat spans)
if (opName === GenAiOperationName.CHAT) {
// Extract agent response summary from output messages
if (outputMessages) {
const reasoningContent = asString(span.attributes[CopilotChatAttr.REASONING_CONTENT]);
this._bufferEntry(sessionId, {
ts: span.endTime,
dur: 0,
sid: sessionId,
type: 'agent_response',
name: 'agent_response',
spanId: `agent-msg-${span.spanId}`,
parentSpanId: span.parentSpanId,
status: 'ok',
attrs: {
response: truncate(outputMessages, MAX_ATTR_VALUE_LENGTH),
...(reasoningContent ? { reasoning: truncate(reasoningContent, MAX_ATTR_VALUE_LENGTH) } : {}),
},
});
}
}
}
private _onSpanEvent(event: ISpanEventData): void {
if (event.eventName === 'turn_start' || event.eventName === 'turn_end') {
this._onTurnBoundaryEvent(event);
return;
}
if (event.eventName === 'tools_available') {
this._onToolsAvailableEvent(event);
return;
}
if (event.eventName !== 'user_message') {
return;
}
const content = event.attributes.content;
if (!content || (typeof content === 'string' && !content.trim())) {
return;
}
// If the event carries a session ID, route to that specific session
const eventSessionId = event.attributes[CopilotChatAttr.CHAT_SESSION_ID];
if (typeof eventSessionId === 'string') {
// Ensure the session buffer exists so early events (before any span completes) are captured
this._ensureSession(eventSessionId);
this._bufferEntry(eventSessionId, {
ts: event.timestamp,
dur: 0,
sid: eventSessionId,
type: 'user_message',
name: 'user_message',
spanId: event.spanId,
parentSpanId: event.parentSpanId,
status: 'ok',
attrs: {
content: truncate(String(content), MAX_ATTR_VALUE_LENGTH),
},
});
return;
}
// Fallback: try to inherit session from parent span before broadcasting
const inheritedSessionId = event.parentSpanId ? this._spanSessionIndex.get(event.parentSpanId) : undefined;
if (inheritedSessionId && this._activeSessions.has(inheritedSessionId)) {
this._bufferEntry(inheritedSessionId, {
ts: event.timestamp,
dur: 0,
sid: inheritedSessionId,
type: 'user_message',
name: 'user_message',
spanId: event.spanId,
parentSpanId: event.parentSpanId,
status: 'ok',
attrs: {
content: truncate(String(content), MAX_ATTR_VALUE_LENGTH),
},
});
return;
}
// Last resort: span events without chat_session_id — write to parent sessions that have their own spans
const parentSessions = [...this._activeSessions.entries()]
.filter(([, session]) => !session.parentSessionId && session.hasOwnSpans)
.map(([id]) => id);
if (parentSessions.length === 0) {
return;
}
for (const sessionId of parentSessions) {
const entry: IDebugLogEntry = {
ts: event.timestamp,
dur: 0,
sid: sessionId,
type: 'user_message',
name: 'user_message',
spanId: event.spanId,
parentSpanId: event.parentSpanId,
status: 'ok',
attrs: {
content: truncate(String(content), MAX_ATTR_VALUE_LENGTH),
},
};
this._bufferEntry(sessionId, entry);
}
}
private _onTurnBoundaryEvent(event: ISpanEventData): void {
const type = event.eventName === 'turn_start' ? 'turn_start' : 'turn_end';
const turnId = typeof event.attributes.turnId === 'string' ? event.attributes.turnId : String(event.attributes.turnId ?? '');
const sessionId = typeof event.attributes[CopilotChatAttr.CHAT_SESSION_ID] === 'string'
? event.attributes[CopilotChatAttr.CHAT_SESSION_ID] as string
: (event.parentSpanId ? this._spanSessionIndex.get(event.parentSpanId) : undefined);
if (!sessionId) {
return;
}
// Ensure the session buffer exists so early turn events are captured
this._ensureSession(sessionId);
this._bufferEntry(sessionId, {
ts: event.timestamp,
dur: 0,
sid: sessionId,
type,
name: `${type}:${turnId}`,
spanId: `${type}-${event.spanId}-${turnId}`,
parentSpanId: event.parentSpanId,
status: 'ok',
attrs: { turnId },
});
}
private _onToolsAvailableEvent(event: ISpanEventData): void {
const sessionId = typeof event.attributes[CopilotChatAttr.CHAT_SESSION_ID] === 'string'
? event.attributes[CopilotChatAttr.CHAT_SESSION_ID] as string
: (event.parentSpanId ? this._spanSessionIndex.get(event.parentSpanId) : undefined);
if (!sessionId) {
return;
}
// Do NOT create sessions from tools_available events — they can carry tool call IDs
// (e.g., toolu_xxx, call_xxx) as conversation IDs, which are not valid session IDs.
const session = this._activeSessions.get(sessionId);
if (!session || session.parentSessionId) {
return;
}
// If the session isn't promoted yet, cache the tools for later replay
if (!session.hasOwnSpans) {
const toolDefs = typeof event.attributes.toolDefinitions === 'string' ? event.attributes.toolDefinitions : undefined;
if (toolDefs) {
session.pendingToolDefs = toolDefs;
}
return;
}
const toolDefs = typeof event.attributes.toolDefinitions === 'string' ? event.attributes.toolDefinitions : undefined;
if (!toolDefs) {
return;
}
// Use the content length to detect changes. Different tool sets (from model
// or mode switches) will have different lengths. A false negative (same length,
// different content) just means we skip writing a redundant file — harmless.
const key = `tools:${toolDefs.length}`;
if (key !== session.toolsKey) {
const fileName = `tools_${session.toolsIndex}.json`;
session.toolsKey = key;
session.toolsIndex++;
session.currentToolsFile = fileName;
this._enqueueFileWrite(session, toolDefs, fileName);
}
}
// ── Core debug event handling (discovery, skill loading, etc.) ──
private _onCoreDebugEvent(event: vscode.ChatDebugEvent): void {
// Only capture discovery/generic events from core — tool calls, model turns,
// and subagent invocations come from OTel spans which are the source of truth.
if (!(event instanceof vscode.ChatDebugGenericEvent)) {
return;
}
const timestamp = event.created.getTime();
const eventId = event.id;
const parentEventId = event.parentEventId;
const entry: IDebugLogEntry = {
ts: timestamp,
dur: 0,
sid: '',
type: event.category === 'discovery' ? 'discovery' : 'generic',
name: event.name,
spanId: eventId ?? `core-${Date.now()}`,
parentSpanId: parentEventId,
status: event.level === vscode.ChatDebugLogLevel.Error ? 'error' : 'ok',
attrs: {
...(event.details ? { details: truncate(event.details, MAX_ATTR_VALUE_LENGTH) } : {}),
...(event.category ? { category: event.category } : {}),
source: 'core',
},
};
// Core events may arrive before any session exists — cache and replay.
// Cap the buffer to avoid unbounded growth over long-running sessions.
if (this._pendingCoreEvents.length >= MAX_PENDING_CORE_EVENTS) {
this._pendingCoreEvents.shift();
}
this._pendingCoreEvents.push(entry);
// Only write to parent sessions that have their own spans
for (const [sessionId, session] of this._activeSessions.entries()) {
if (!session.parentSessionId && session.hasOwnSpans) {
this._bufferEntry(sessionId, { ...entry, sid: sessionId });
}
}
}
// ── Span to entry conversion ──
private _spanToEntry(span: ICompletedSpanData, sessionId: string): IDebugLogEntry | undefined {
const opName = asString(span.attributes[GenAiAttr.OPERATION_NAME]);
const duration = span.endTime - span.startTime;
const isError = span.status.code === SpanStatusCode.ERROR;
switch (opName) {
case GenAiOperationName.EXECUTE_TOOL: {
const toolName = asString(span.attributes[GenAiAttr.TOOL_NAME]) ?? span.name;
return {
ts: span.startTime,
dur: duration,
sid: sessionId,
type: 'tool_call',
name: toolName,
spanId: span.spanId,
parentSpanId: span.parentSpanId,
status: isError ? 'error' : 'ok',
attrs: {
...(span.attributes[GenAiAttr.TOOL_CALL_ARGUMENTS] !== undefined
? { args: truncate(String(span.attributes[GenAiAttr.TOOL_CALL_ARGUMENTS]), MAX_ATTR_VALUE_LENGTH) }
: {}),
...(span.attributes[GenAiAttr.TOOL_CALL_RESULT] !== undefined
? { result: truncate(String(span.attributes[GenAiAttr.TOOL_CALL_RESULT]), MAX_ATTR_VALUE_LENGTH) }
: {}),
...(isError && span.status.message ? { error: span.status.message } : {}),
},
};
}
case GenAiOperationName.CHAT: {
const model = asString(span.attributes[GenAiAttr.REQUEST_MODEL])
?? asString(span.attributes[GenAiAttr.RESPONSE_MODEL])
?? 'unknown';
return {
ts: span.startTime,
dur: duration,
sid: sessionId,
type: 'llm_request',
name: `chat:${model}`,
spanId: span.spanId,
parentSpanId: span.parentSpanId,
status: isError ? 'error' : 'ok',
attrs: {
model,
...(span.attributes[GenAiAttr.USAGE_INPUT_TOKENS] !== undefined
? { inputTokens: asNumber(span.attributes[GenAiAttr.USAGE_INPUT_TOKENS]) }
: {}),
...(span.attributes[GenAiAttr.USAGE_OUTPUT_TOKENS] !== undefined
? { outputTokens: asNumber(span.attributes[GenAiAttr.USAGE_OUTPUT_TOKENS]) }
: {}),
...(span.attributes[GenAiAttr.USAGE_CACHE_READ_INPUT_TOKENS] !== undefined
? { cachedTokens: asNumber(span.attributes[GenAiAttr.USAGE_CACHE_READ_INPUT_TOKENS]) }
: {}),
...(span.attributes[CopilotChatAttr.TIME_TO_FIRST_TOKEN] !== undefined
? { ttft: asNumber(span.attributes[CopilotChatAttr.TIME_TO_FIRST_TOKEN]) }
: {}),
...(span.attributes[CopilotChatAttr.USER_REQUEST] !== undefined
? { userRequest: String(span.attributes[CopilotChatAttr.USER_REQUEST]) }
: {}),
...(span.attributes[GenAiAttr.INPUT_MESSAGES] !== undefined
? { inputMessages: String(span.attributes[GenAiAttr.INPUT_MESSAGES]) }
: {}),
...(span.attributes[GenAiAttr.REQUEST_MAX_TOKENS] !== undefined
? { maxTokens: asNumber(span.attributes[GenAiAttr.REQUEST_MAX_TOKENS]) }
: {}),
...(span.attributes[GenAiAttr.REQUEST_TEMPERATURE] !== undefined
? { temperature: asNumber(span.attributes[GenAiAttr.REQUEST_TEMPERATURE]) }
: {}),
...(span.attributes[GenAiAttr.REQUEST_TOP_P] !== undefined
? { topP: asNumber(span.attributes[GenAiAttr.REQUEST_TOP_P]) }
: {}),
...(isError && span.status.message ? { error: span.status.message } : {}),
},
};
}
case GenAiOperationName.INVOKE_AGENT: {
if (!span.parentSpanId) {
return undefined; // Top-level agent spans are containers
}
const agentName = asString(span.attributes[GenAiAttr.AGENT_NAME]) ?? span.name;
return {
ts: span.startTime,
dur: duration,
sid: sessionId,
type: 'subagent',
name: agentName,
spanId: span.spanId,
parentSpanId: span.parentSpanId,
status: isError ? 'error' : 'ok',
attrs: {
agentName,
...(span.attributes[GenAiAttr.AGENT_DESCRIPTION] !== undefined
? { description: truncate(String(span.attributes[GenAiAttr.AGENT_DESCRIPTION]), MAX_ATTR_VALUE_LENGTH) }
: {}),
...(isError && span.status.message ? { error: span.status.message } : {}),
},
};
}
case GenAiOperationName.CONTENT_EVENT:
case 'core_event': {
const name = asString(span.attributes[CopilotChatAttr.DEBUG_NAME]) ?? span.name;
return {
ts: span.startTime,
dur: duration,
sid: sessionId,
type: 'generic',
name,
spanId: span.spanId,
parentSpanId: span.parentSpanId,
status: isError ? 'error' : 'ok',
attrs: {
...(span.attributes['copilot_chat.event_details'] !== undefined
? { details: truncate(String(span.attributes['copilot_chat.event_details']), MAX_ATTR_VALUE_LENGTH) }
: {}),