forked from kenn-io/agentsview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualstudio_copilot_test.go
More file actions
1921 lines (1774 loc) · 74.3 KB
/
Copy pathvisualstudio_copilot_test.go
File metadata and controls
1921 lines (1774 loc) · 74.3 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
package parser
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDiscoverVisualStudioCopilotSessions(t *testing.T) {
root := t.TempDir()
tracesDir := filepath.Join(
root, "VSGitHubCopilotLogs", "traces",
)
require.NoError(t, os.MkdirAll(tracesDir, 0o755))
tracePath := filepath.Join(
tracesDir,
"20260612T194439_257709a3_VSGitHubCopilot_traces.jsonl",
)
conversationID := "4a8f63f6-7626-4416-a874-fc7bd2c3f005"
data := vsCopilotTraceLineJSON(conversationID,
"chat gpt-5.5", "1781293600000000000", "1781293610000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": `[{"role":"user","parts":[{"type":"text","content":"Update the XAML."}]}]`,
}) + "\n"
require.NoError(t, os.WriteFile(tracePath, []byte(data), 0o644))
require.NoError(t, os.WriteFile(
filepath.Join(tracesDir, "not-copilot.jsonl"),
[]byte("{}\n"), 0o644,
))
files := discoverVisualStudioCopilotTestSessions(t, tracesDir)
require.Len(t, files, 1)
assert.Equal(t, tracePath+"#"+conversationID, files[0].Path)
assert.Equal(t, "visualstudio", files[0].Project)
assert.Equal(t, AgentVSCopilot, files[0].Agent)
}
func TestDiscoverVisualStudioCopilot2026Sessions_SupportedRootLayouts(t *testing.T) {
root := t.TempDir()
conversationID := "5bc5f6d7-9a6e-4f9c-8f3c-b7be2e7d9f20"
sessionPath := filepath.Join(
root, ".vs", "SampleApp", "copilot-chat", "thread", "sessions",
conversationID,
)
data := vsCopilotTraceLineJSON(
conversationID,
"chat gpt-5.5", "1781293600000000000", "1781293610000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": `[{"role":"user","parts":[{"type":"text","content":"What changed?"}]}]`,
},
) + "\n"
writeSourceFile(t, sessionPath, data)
cases := []struct {
name string
root string
}{
{name: "project root", root: root},
{name: ".vs root", root: filepath.Join(root, ".vs")},
{name: "copilot-chat root", root: filepath.Join(root, ".vs", "SampleApp", "copilot-chat")},
{name: "thread root", root: filepath.Join(root, ".vs", "SampleApp", "copilot-chat", "thread")},
{name: "sessions root", root: filepath.Join(root, ".vs", "SampleApp", "copilot-chat", "thread", "sessions")},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
files := discoverVisualStudioCopilotTestSessions(t, tc.root)
require.Len(t, files, 1)
assert.Equal(
t,
VisualStudioCopilotVirtualPath(sessionPath, conversationID),
files[0].Path,
)
assert.Equal(t, "visualstudio", files[0].Project)
assert.Equal(t, AgentVSCopilot, files[0].Agent)
})
}
}
func TestDiscoverVisualStudioCopilotSessions_IgnoresParentDirs(t *testing.T) {
root := t.TempDir()
tracesDir := filepath.Join(
root, "VSGitHubCopilotLogs", "traces",
)
require.NoError(t, os.MkdirAll(tracesDir, 0o755))
require.NoError(t, os.WriteFile(filepath.Join(
tracesDir,
"20260612T194439_257709a3_VSGitHubCopilot_traces.jsonl",
), []byte("{}\n"), 0o644))
files := discoverVisualStudioCopilotTestSessions(t, root)
assert.Empty(t, files)
}
func TestDiscoverVisualStudioCopilotSessions_DeduplicatesConversationTraceFiles(t *testing.T) {
root := t.TempDir()
conversationID := "4a8f63f6-7626-4416-a874-fc7bd2c3f005"
oldTrace := filepath.Join(
root,
"20260611T145205_aaaa1111_VSGitHubCopilot_traces.jsonl",
)
newTrace := filepath.Join(
root,
"20260612T145205_bbbb2222_VSGitHubCopilot_traces.jsonl",
)
data := vsCopilotTraceLineJSON(conversationID,
"chat gpt-5.5", "1781293600000000000", "1781293610000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": `[{"role":"user","parts":[{"type":"text","content":"Update the XAML."}]}]`,
}) + "\n"
require.NoError(t, os.WriteFile(oldTrace, []byte(data), 0o644))
require.NoError(t, os.WriteFile(newTrace, []byte(data), 0o644))
files := discoverVisualStudioCopilotTestSessions(t, root)
require.Len(t, files, 1)
assert.Equal(t, newTrace+"#"+conversationID, files[0].Path)
}
func TestVisualStudioCopilotLookupMatchesDiscoveryWhenMtimeAndPathDisagree(t *testing.T) {
root := t.TempDir()
conversationID := "4a8f63f6-7626-4416-a874-fc7bd2c3f005"
// The lexicographically greater filename ("zzzz") is the OLDER file, so a
// path-only "last wins" selection would diverge from discovery's
// newest-mtime selection. Both discovery and single-session lookup must
// agree on the same canonical trace.
newerLowerPath := filepath.Join(
root, "20260611T145205_aaaa1111_VSGitHubCopilot_traces.jsonl",
)
olderHigherPath := filepath.Join(
root, "20260612T145205_zzzz9999_VSGitHubCopilot_traces.jsonl",
)
data := vsCopilotTraceLineJSON(conversationID,
"chat gpt-5.5", "1781293600000000000", "1781293610000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": `[{"role":"user","parts":[{"type":"text","content":"Update the XAML."}]}]`,
}) + "\n"
require.NoError(t, os.WriteFile(newerLowerPath, []byte(data), 0o644))
require.NoError(t, os.WriteFile(olderHigherPath, []byte(data), 0o644))
older := time.Date(2026, 6, 11, 0, 0, 0, 0, time.UTC)
newer := time.Date(2026, 6, 12, 0, 0, 0, 0, time.UTC)
require.NoError(t, os.Chtimes(olderHigherPath, older, older))
require.NoError(t, os.Chtimes(newerLowerPath, newer, newer))
files := discoverVisualStudioCopilotTestSessions(t, root)
require.Len(t, files, 1)
assert.Equal(t, newerLowerPath+"#"+conversationID, files[0].Path)
found := findVisualStudioCopilotTraceSourceFile(root, conversationID)
assert.Equal(t, files[0].Path, found,
"lookup must resolve to the same canonical trace as discovery")
}
func TestFindVisualStudioCopilotSourceFile_2026SupportedRootLayouts(t *testing.T) {
root := t.TempDir()
conversationID := "5bc5f6d7-9a6e-4f9c-8f3c-b7be2e7d9f20"
sessionPath := filepath.Join(
root, ".vs", "SampleApp", "copilot-chat", "thread", "sessions",
conversationID,
)
data := vsCopilotTraceLineJSON(
conversationID,
"chat gpt-5.5", "1781293600000000000", "1781293610000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": `[{"role":"user","parts":[{"type":"text","content":"Run the tests."}]}]`,
},
) + "\n"
writeSourceFile(t, sessionPath, data)
cases := []struct {
name string
root string
}{
{name: "project root", root: root},
{name: ".vs root", root: filepath.Join(root, ".vs")},
{name: "copilot-chat root", root: filepath.Join(root, ".vs", "SampleApp", "copilot-chat")},
{name: "thread root", root: filepath.Join(root, ".vs", "SampleApp", "copilot-chat", "thread")},
{name: "sessions root", root: filepath.Join(root, ".vs", "SampleApp", "copilot-chat", "thread", "sessions")},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(
t,
VisualStudioCopilotVirtualPath(sessionPath, conversationID),
findVisualStudioCopilotTestSourceFile(t, tc.root, conversationID),
)
})
}
}
func TestParseVisualStudioCopilotSession_MalformedTraceLineReturnsError(t *testing.T) {
path := filepath.Join(
t.TempDir(),
"20260612T194439_257709a3_VSGitHubCopilot_traces.jsonl",
)
conversationID := "4a8f63f6-7626-4416-a874-fc7bd2c3f005"
data := vsCopilotTraceLineJSON(conversationID,
"chat gpt-5.5", "1781293600000000000", "1781293610000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": `[{"role":"user","parts":[{"type":"text","content":"Update the XAML."}]}]`,
}) + "\n" + `{"resourceSpans":[` + "\n"
require.NoError(t, os.WriteFile(path, []byte(data), 0o644))
sess, msgs, err := parseVisualStudioCopilotTestSession(t,
path, "visualstudio", "local",
)
require.Error(t, err)
assert.Contains(t, err.Error(), "decode")
assert.Nil(t, sess)
assert.Nil(t, msgs)
}
func TestDiscoverVisualStudioCopilotSessions_EmitsWorkItemPerConversation(t *testing.T) {
dir := t.TempDir()
dominant := "4a8f63f6-7626-4416-a874-fc7bd2c3f005"
secondary := "c0aca2e3-d1f2-4d28-bd5e-5dab29e2be28"
// Older file carries the dominant conversation plus a single span
// for a secondary conversation. The secondary conversation can never
// win the old "best conversation" heuristic, so it used to be dropped.
oldTrace := filepath.Join(
dir, "20260611T145205_aaaa1111_VSGitHubCopilot_traces.jsonl",
)
oldData := strings.Join([]string{
vsCopilotTraceLineJSONWithSpanID(dominant, "d1",
"chat gpt-5.5", "1781293600000000000", "1781293610000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": `[{"role":"user","parts":[{"type":"text","content":"Update the XAML."}]}]`,
}),
vsCopilotTraceLineJSONWithSpanID(dominant, "d2",
"chat gpt-5.5", "1781293620000000000", "1781293630000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": `[{"role":"user","parts":[{"type":"text","content":"Now run the build."}]}]`,
}),
vsCopilotTraceLineJSONWithSpanID(secondary, "s1",
"invoke_agent GitHub Copilot",
"1781294552800436000", "1781294586729109400",
map[string]string{
"gen_ai.agent.name": "GitHub Copilot",
"gen_ai.request.model": "gpt-5.5",
"copilot_chat.mode": "Agent",
}),
}, "\n") + "\n"
// Newer file carries only the dominant conversation, so it becomes
// that conversation's representative file.
newTrace := filepath.Join(
dir, "20260612T145205_bbbb2222_VSGitHubCopilot_traces.jsonl",
)
newData := vsCopilotTraceLineJSONWithSpanID(dominant, "d3",
"chat gpt-5.5", "1781293700000000000", "1781293710000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": `[{"role":"user","parts":[{"type":"text","content":"Ship it."}]}]`,
}) + "\n"
require.NoError(t, os.WriteFile(oldTrace, []byte(oldData), 0o644))
require.NoError(t, os.WriteFile(newTrace, []byte(newData), 0o644))
files := discoverVisualStudioCopilotTestSessions(t, dir)
got := map[string]string{}
for _, f := range files {
assert.Equal(t, AgentVSCopilot, f.Agent)
assert.Equal(t, "visualstudio", f.Project)
got[vsConversationIDFromPath(t, f.Path)] = f.Path
}
require.Len(t, files, 2)
assert.Equal(t, newTrace+"#"+dominant, got[dominant],
"dominant conversation should point at its latest trace file")
assert.Equal(t, oldTrace+"#"+secondary, got[secondary],
"secondary conversation must not be dropped")
}
func TestDiscoverVisualStudioCopilotSessions_SampleFixturesEnumerateBothConversations(t *testing.T) {
_, callerFile, _, ok := runtime.Caller(0)
require.True(t, ok)
sampleDir := filepath.Join(
filepath.Dir(callerFile), "..", "..",
"testdata", "visualstudio-copilot", "redacted",
)
if _, err := os.Stat(sampleDir); err != nil {
t.Skipf("sample dir not available: %v", err)
}
files := discoverVisualStudioCopilotTestSessions(t, sampleDir)
got := map[string]struct{}{}
for _, f := range files {
got[vsConversationIDFromPath(t, f.Path)] = struct{}{}
}
assert.Contains(t, got, "4a8f63f6-7626-4416-a874-fc7bd2c3f005",
"dominant conversation should be discovered")
assert.Contains(t, got, "c0aca2e3-d1f2-4d28-bd5e-5dab29e2be28",
"secondary conversation in sample-4 must not be dropped")
}
// TestParseVisualStudioCopilotConversation_PropagatesSiblingDirReadError
// verifies that a failure to enumerate sibling trace files is surfaced rather
// than swallowed into "no siblings". Otherwise the primary trace would be
// written as a complete session even though sibling discovery failed, defeating
// the partial-transcript guard.
func TestParseVisualStudioCopilotConversation_PropagatesSiblingDirReadError(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("directory permission semantics differ on Windows")
}
if os.Geteuid() == 0 {
t.Skip("root bypasses directory read permissions")
}
dir := filepath.Join(t.TempDir(), "traces")
require.NoError(t, os.Mkdir(dir, 0o755))
conversationID := "4a8f63f6-7626-4416-a874-fc7bd2c3f005"
tracePath := filepath.Join(
dir, "20260611T145205_aaaa1111_VSGitHubCopilot_traces.jsonl",
)
data := vsCopilotTraceLineJSON(conversationID, "chat gpt-5.5",
"1781293600000000000", "1781293610000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": `[{"role":"user","parts":[{"type":"text","content":"Hello."}]}]`,
}) + "\n"
require.NoError(t, os.WriteFile(tracePath, []byte(data), 0o644))
// Make the directory traversable but not readable: the known trace file can
// still be opened, but enumerating siblings via ReadDir fails.
require.NoError(t, os.Chmod(dir, 0o100))
t.Cleanup(func() { _ = os.Chmod(dir, 0o755) })
_, _, err := parseVisualStudioCopilotTestConversation(t,
tracePath, conversationID, "visualstudio", "local",
)
require.Error(t, err,
"a sibling directory read error must propagate, not be swallowed")
}
// TestVisualStudioCopilotTraceFingerprintStrictPropagatesDirError verifies that
// the strict fingerprint surfaces a directory-enumeration error while the
// best-effort fingerprint falls back to the representative file's stat. Sync
// skip checks rely on the strict variant so a ReadDir failure is retried rather
// than mistaken for an unchanged fingerprint in a single-trace directory.
func TestVisualStudioCopilotTraceFingerprintStrictPropagatesDirError(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("directory permission semantics differ on Windows")
}
if os.Geteuid() == 0 {
t.Skip("root bypasses directory read permissions")
}
dir := filepath.Join(t.TempDir(), "traces")
require.NoError(t, os.Mkdir(dir, 0o755))
tracePath := filepath.Join(
dir, "20260611T145205_aaaa1111_VSGitHubCopilot_traces.jsonl",
)
require.NoError(t, os.WriteFile(tracePath, []byte("{}\n"), 0o644))
// Readable directory: both variants agree on the composite fingerprint.
wantSize, wantMtime, err := VisualStudioCopilotTraceFingerprintStrict(
tracePath,
)
require.NoError(t, err)
lenientSize, lenientMtime := VisualStudioCopilotTraceFingerprint(tracePath)
assert.Equal(t, wantSize, lenientSize)
assert.Equal(t, wantMtime, lenientMtime)
// Traversable but not readable: ReadDir fails. The strict variant must
// return the error; the best-effort variant falls back to the file's stat.
require.NoError(t, os.Chmod(dir, 0o100))
t.Cleanup(func() { _ = os.Chmod(dir, 0o755) })
_, _, err = VisualStudioCopilotTraceFingerprintStrict(tracePath)
require.Error(t, err,
"strict fingerprint must surface the directory read error")
fallbackSize, fallbackMtime := VisualStudioCopilotTraceFingerprint(tracePath)
info, statErr := os.Stat(tracePath)
require.NoError(t, statErr)
assert.Equal(t, info.Size(), fallbackSize,
"best-effort fingerprint falls back to the representative file stat")
assert.Equal(t, info.ModTime().UnixNano(), fallbackMtime)
}
// TestVisualStudioCopilotTraceFingerprintStrictPropagatesSiblingStatError
// verifies that a sibling trace file that lists but cannot be stat'd (a broken
// symlink) fails the strict fingerprint, while the best-effort fingerprint
// ignores it. The skip check uses the strict variant, so an unstattable sibling
// must not be treated as "unchanged" when the readable files still match the
// stored composite fingerprint.
func TestVisualStudioCopilotTraceFingerprintStrictPropagatesSiblingStatError(
t *testing.T,
) {
if runtime.GOOS == "windows" {
t.Skip("symlink semantics differ on Windows")
}
dir := t.TempDir()
tracePath := filepath.Join(
dir, "20260611T145205_aaaa1111_VSGitHubCopilot_traces.jsonl",
)
require.NoError(t, os.WriteFile(tracePath, []byte("{}\n"), 0o644))
// A sibling that appears in the listing but cannot be stat'd: a symlink to a
// missing target resolves to ENOENT on stat.
broken := filepath.Join(
dir, "20260612T145205_bbbb2222_VSGitHubCopilot_traces.jsonl",
)
require.NoError(t, os.Symlink(filepath.Join(dir, "missing-target"), broken))
_, _, err := VisualStudioCopilotTraceFingerprintStrict(tracePath)
require.Error(t, err,
"strict fingerprint must surface a sibling stat failure")
size, _ := VisualStudioCopilotTraceFingerprint(tracePath)
info, statErr := os.Stat(tracePath)
require.NoError(t, statErr)
assert.Equal(t, info.Size(), size,
"best-effort fingerprint counts only the statable trace files")
}
func TestParseVisualStudioCopilotConversation_PropagatesReadError(t *testing.T) {
// A directory named like a trace file exists but cannot be scanned as
// JSONL, so the read fails. The error must propagate rather than be
// swallowed into an empty (cacheable) "no sessions" result.
dir := filepath.Join(
t.TempDir(),
"20260612T194439_257709a3_VSGitHubCopilot_traces.jsonl",
)
require.NoError(t, os.Mkdir(dir, 0o755))
sess, msgs, err := parseVisualStudioCopilotTestConversation(t,
dir, "4a8f63f6-7626-4416-a874-fc7bd2c3f005", "visualstudio", "local",
)
require.Error(t, err)
assert.Nil(t, sess)
assert.Nil(t, msgs)
}
func TestDiscoverVisualStudioCopilotSessions_EnqueuesUnreadableTraceFile(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("symlink semantics differ on Windows")
}
// A trace file that cannot be read (here, a symlink to a directory)
// must still be enqueued so the sync worker surfaces the failure,
// rather than silently dropping every conversation it might contain.
root := t.TempDir()
target := filepath.Join(root, "target-dir")
require.NoError(t, os.Mkdir(target, 0o755))
link := filepath.Join(
root, "20260612T194439_257709a3_VSGitHubCopilot_traces.jsonl",
)
require.NoError(t, os.Symlink(target, link))
files := discoverVisualStudioCopilotTestSessions(t, root)
require.Len(t, files, 1)
assert.Equal(t, link, files[0].Path,
"unreadable trace file should be enqueued by its physical path")
assert.Equal(t, AgentVSCopilot, files[0].Agent)
}
func TestResolveSourceFilePath(t *testing.T) {
trace := "/logs/20260612T194439_257709a3_VSGitHubCopilot_traces.jsonl"
conversationID := "4a8f63f6-7626-4416-a874-fc7bd2c3f005"
sessionPath := filepath.Join(
"/logs", ".vs", "SampleApp", "copilot-chat", "thread", "sessions",
conversationID,
)
assert.Equal(t, trace,
ResolveSourceFilePath(VisualStudioCopilotVirtualPath(trace, conversationID)),
"virtual path should resolve to its physical trace file")
assert.Equal(t, sessionPath,
ResolveSourceFilePath(
VisualStudioCopilotVirtualPath(sessionPath, conversationID),
),
"VS 2026 session virtual path should resolve to its physical session file")
assert.Equal(t, "/profile/User/workspaceStorage/hash/state.vscdb",
ResolveSourceFilePath(
"/profile/User/workspaceStorage/hash/state.vscdb#windsurf-session",
),
"Windsurf virtual path should resolve to its physical workspace DB")
assert.Equal(t, "/logs/session#draft.jsonl",
ResolveSourceFilePath("/logs/session#draft.jsonl"),
"non-Windsurf paths containing # should be returned unchanged")
assert.Equal(t, "/logs/session.jsonl",
ResolveSourceFilePath("/logs/session.jsonl"),
"a plain source path should be returned unchanged")
assert.Equal(t, "", ResolveSourceFilePath(""))
}
// vsConversationIDFromPath extracts the conversation ID from a
// <traceFile>#<conversationID> virtual work-item path.
func vsConversationIDFromPath(t *testing.T, path string) string {
t.Helper()
idx := strings.LastIndex(path, "#")
require.Greater(t, idx, 0,
"expected virtual path with #conversationID, got %q", path)
return path[idx+1:]
}
func TestParseVisualStudioCopilotSession_IgnoresNonTraceFiles(t *testing.T) {
path := filepath.Join(t.TempDir(), "sess.json")
data := `{
"version": 3,
"sessionId": "test-123",
"requests": [{
"requestId": "req1",
"message": {"text": "Hello"},
"response": [{"value": "Hi"}],
"timestamp": 1755347728047
}]
}`
require.NoError(t, os.WriteFile(path, []byte(data), 0o644))
sess, msgs, err := parseVisualStudioCopilotTestSession(t,
path, "visualstudio", "local",
)
require.NoError(t, err)
assert.Nil(t, sess)
assert.Nil(t, msgs)
}
func TestParseVisualStudioCopilotTraceSession(t *testing.T) {
path := filepath.Join(
t.TempDir(),
"20260612T194439_257709a3_VSGitHubCopilot_traces.jsonl",
)
conversationID := "4a8f63f6-7626-4416-a874-fc7bd2c3f005"
data := strings.Join([]string{
vsCopilotTraceLineJSON(conversationID,
"execute_tool run_command_in_terminal",
"1781293588624985000", "1781293588769581200",
map[string]string{
"gen_ai.tool.name": "run_command_in_terminal",
"gen_ai.tool.call.id": "call_123",
"gen_ai.tool.call.arguments": `{"command":"go test ./..."}`,
"gen_ai.tool.call.result": `{"Value":"ok"}`,
}),
vsCopilotTraceLineJSON(conversationID,
"invoke_agent GitHub Copilot",
"1781293600000000000", "1781293610000000000",
map[string]string{
"gen_ai.agent.name": "GitHub Copilot",
"gen_ai.request.model": "gpt-5.5",
"copilot_chat.mode": "Agent",
"copilot_chat.turn_count": "1",
}),
}, "\n") + "\n"
require.NoError(t, os.WriteFile(path, []byte(data), 0o644))
sess, msgs, err := parseVisualStudioCopilotTestSession(t,
path, "visualstudio", "local",
)
require.NoError(t, err)
require.NotNil(t, sess)
assert.Equal(t, AgentVSCopilot, sess.Agent)
assert.Equal(t, "visualstudio-copilot:"+conversationID, sess.ID)
assert.Equal(t, "Run command: go test ./...", sess.FirstMessage)
require.Len(t, msgs, 1)
assert.True(t, msgs[0].HasToolUse)
assert.Contains(t, msgs[0].Content, "$ go test ./...")
require.Len(t, msgs[0].ToolCalls, 1)
assert.Equal(t, "run_command_in_terminal",
msgs[0].ToolCalls[0].ToolName)
assert.Equal(t, "Bash", msgs[0].ToolCalls[0].Category)
assert.Contains(t, msgs[0].ToolCalls[0].InputJSON,
"go test ./...")
assert.JSONEq(t, `{"command":"go test ./..."}`,
msgs[0].ToolCalls[0].InputJSON)
require.Len(t, msgs[0].ToolCalls[0].ResultEvents, 1)
assert.Equal(t, "completed",
msgs[0].ToolCalls[0].ResultEvents[0].Status)
assert.Equal(t, "ok", msgs[0].ToolCalls[0].ResultEvents[0].Content)
}
func TestParseVisualStudioCopilotTraceSession_GetFileResult(t *testing.T) {
path := filepath.Join(
t.TempDir(),
"20260611T145205_d9b231f1_VSGitHubCopilot_traces.jsonl",
)
conversationID := "1c4ff921-fa0c-46f6-a043-c282c49761da"
result := `{"Value":{"Value":{"Content":"1: <Page>\n2: <TextBlock Text=\"Hello\" />"}}}`
data := vsCopilotTraceLineJSON(conversationID,
"execute_tool get_file",
"1781293588624985000", "1781293588769581200",
map[string]string{
"gen_ai.tool.name": "get_file",
"gen_ai.tool.call.id": "call_file",
"gen_ai.tool.call.arguments": `{"filename":"Views\\MainWindow.xaml","startLine":1,"endLine":400,"includeLineNumbers":true}`,
"gen_ai.tool.call.result": result,
}) + "\n"
require.NoError(t, os.WriteFile(path, []byte(data), 0o644))
sess, msgs, err := parseVisualStudioCopilotTestSession(t,
path, "visualstudio", "local",
)
require.NoError(t, err)
require.NotNil(t, sess)
assert.Equal(t, "Read file: Views\\MainWindow.xaml", sess.FirstMessage)
require.Len(t, msgs, 1)
assert.Contains(t, msgs[0].Content, "[Read: get_file]")
assert.Contains(t, msgs[0].Content, "Views\\MainWindow.xaml")
require.Len(t, msgs[0].ToolCalls, 1)
call := msgs[0].ToolCalls[0]
assert.Equal(t, "get_file", call.ToolName)
assert.Equal(t, "Read", call.Category)
assert.Contains(t, call.InputJSON, `"file_path":"Views\\MainWindow.xaml"`)
assert.NotContains(t, call.InputJSON, `"arguments"`)
require.Len(t, call.ResultEvents, 1)
assert.Equal(t, "visualstudio-copilot", call.ResultEvents[0].Source)
assert.Equal(t, "completed", call.ResultEvents[0].Status)
assert.Contains(t, call.ResultEvents[0].Content, "<Page>")
assert.Contains(t, call.ResultEvents[0].Content, "TextBlock")
}
func TestParseVisualStudioCopilotTraceSession_InvokeOnlyFirstMessage(t *testing.T) {
path := filepath.Join(
t.TempDir(),
"20260612T194439_257709a3_VSGitHubCopilot_traces.jsonl",
)
conversationID := "1c4ff921-fa0c-46f6-a043-c282c49761da"
data := vsCopilotTraceLineJSON(conversationID,
"invoke_agent GitHub Copilot",
"1781293600000000000", "1781293610000000000",
map[string]string{
"gen_ai.agent.name": "GitHub Copilot",
"gen_ai.request.model": "gpt-5.5",
"copilot_chat.mode": "Agent",
"copilot_chat.client_id": "Microsoft.VisualStudio.Conversations.Chat.HelpWindow",
"copilot_chat.root_request_id": "de788686-1331-4747-a2cd-7cc1009beec8",
"copilot_chat.turn_count": "1",
"copilot_chat.initiator_type": "User",
"copilot_chat.entry_point": "Microsoft.VisualStudio.Copilot.AgentModeResponder",
"gen_ai.operation.name": "invoke_agent",
"gen_ai.provider.name": "other",
}) + "\n"
require.NoError(t, os.WriteFile(path, []byte(data), 0o644))
sess, msgs, err := parseVisualStudioCopilotTestSession(t,
path, "visualstudio", "local",
)
require.NoError(t, err)
require.NotNil(t, sess)
assert.Equal(t,
"Visual Studio Copilot Agent | HelpWindow | gpt-5.5 | de788686",
sess.FirstMessage)
require.Len(t, msgs, 1)
assert.Contains(t, msgs[0].Content, "model: gpt-5.5")
}
func TestParseVisualStudioCopilotTraceSession_ChatPromptFirstMessage(t *testing.T) {
path := filepath.Join(
t.TempDir(),
"20260611T145205_d9b231f1_VSGitHubCopilot_traces.jsonl",
)
conversationID := "1c4ff921-fa0c-46f6-a043-c282c49761da"
inputMessages := `[{"role":"system","parts":[{"type":"text","content":"You are an AI programming assistant."}]},{"role":"user","parts":[{"type":"text","content":"Remove the Details button and replace the expander with tabs."}]}]`
data := vsCopilotTraceLineJSON(conversationID,
"chat gpt-5.5",
"1781293600000000000", "1781293610000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.request.model": "gpt-5.5",
"gen_ai.input.messages": inputMessages,
"copilot_chat.client_id": "Microsoft.Modernization.Agent",
"copilot_chat.root_request_id": "398c5816-cfb4-4f51-a195-16e7f03edc69",
}) + "\n"
require.NoError(t, os.WriteFile(path, []byte(data), 0o644))
sess, msgs, err := parseVisualStudioCopilotTestSession(t,
path, "visualstudio", "local",
)
require.NoError(t, err)
require.NotNil(t, sess)
assert.Equal(t,
"Remove the Details button and replace the expander with tabs.",
sess.FirstMessage)
assert.Equal(t, 1, sess.UserMessageCount)
require.Len(t, msgs, 1)
assert.Equal(t, RoleUser, msgs[0].Role)
assert.Equal(t,
"Remove the Details button and replace the expander with tabs.",
msgs[0].Content)
}
func TestParseVisualStudioCopilotTraceSession_PreservesPromptMarkdown(t *testing.T) {
path := filepath.Join(
t.TempDir(),
"20260611T145205_d9b231f1_VSGitHubCopilot_traces.jsonl",
)
conversationID := "1c4ff921-fa0c-46f6-a043-c282c49761da"
prompt := "Use this safer version:\n\n```powershell\ngit branch saved/real-self-service-v2-local-work\ngit reset --hard origin/real-self-service-v2\n```\n\nThat does two things."
inputMessages := mustJSON(t, []vsCopilotChatMessage{{
Role: "user",
Parts: []vsCopilotChatPart{{
Type: "text",
Content: prompt,
}},
}})
data := vsCopilotTraceLineJSON(conversationID,
"chat gpt-5.5",
"1781293600000000000", "1781293610000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": string(inputMessages),
}) + "\n"
require.NoError(t, os.WriteFile(path, []byte(data), 0o644))
sess, msgs, err := parseVisualStudioCopilotTestSession(t,
path, "visualstudio", "local",
)
require.NoError(t, err)
require.NotNil(t, sess)
assert.Equal(t,
"Use this safer version: ```powershell git branch saved/real-self-service-v2-local-work git reset --hard origin/real-self-service-v2 ``` That does two things.",
sess.FirstMessage)
require.Len(t, msgs, 1)
assert.Equal(t, prompt, msgs[0].Content)
assert.Contains(t, msgs[0].Content, "```powershell\n")
assert.Contains(t, msgs[0].Content,
"git reset --hard origin/real-self-service-v2")
}
func TestParseVisualStudioCopilotTraceSession_CombinesConversationTraceFiles(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(
dir,
"20260611T145205_aaaa1111_VSGitHubCopilot_traces.jsonl",
)
sibling := filepath.Join(
dir,
"20260612T145205_bbbb2222_VSGitHubCopilot_traces.jsonl",
)
conversationID := "4a8f63f6-7626-4416-a874-fc7bd2c3f005"
firstInput := `[{"role":"user","parts":[{"type":"text","content":"Update the XAML."}]}]`
secondInput := `[{"role":"user","parts":[{"type":"text","content":"Now run the build."}]}]`
firstData := vsCopilotTraceLineJSON(conversationID,
"chat gpt-5.5", "1781293600000000000", "1781293610000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": firstInput,
}) + "\n"
secondData := vsCopilotTraceLineJSON(conversationID,
"chat gpt-5.5", "1781293620000000000", "1781293630000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": secondInput,
}) + "\n"
require.NoError(t, os.WriteFile(path, []byte(firstData), 0o644))
require.NoError(t, os.WriteFile(sibling, []byte(secondData), 0o644))
sess, msgs, err := parseVisualStudioCopilotTestSession(t,
path, "visualstudio", "local",
)
require.NoError(t, err)
require.NotNil(t, sess)
assert.Equal(t, "Update the XAML.", sess.FirstMessage)
assert.Equal(t, 2, sess.UserMessageCount)
require.Len(t, msgs, 2)
assert.Equal(t, "Update the XAML.", msgs[0].Content)
assert.Equal(t, "Now run the build.", msgs[1].Content)
}
// TestParseVisualStudioCopilotTraceSession_PropagatesSiblingReadError verifies
// that an unreadable sibling trace file fails the parse rather than silently
// reconstructing the conversation from a subset of its trace files. A
// conversation can have spans in any sibling, and sessions are written with
// full message replacement, so reconstructing from a subset would overwrite
// previously indexed messages with a partial transcript. A transient sibling
// read error must surface so the sync is retried instead.
func TestParseVisualStudioCopilotTraceSession_PropagatesSiblingReadError(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("symlink semantics differ on Windows")
}
dir := t.TempDir()
path := filepath.Join(
dir,
"20260611T145205_aaaa1111_VSGitHubCopilot_traces.jsonl",
)
conversationID := "4a8f63f6-7626-4416-a874-fc7bd2c3f005"
data := vsCopilotTraceLineJSON(conversationID,
"chat gpt-5.5", "1781293600000000000", "1781293610000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": `[{"role":"user","parts":[{"type":"text","content":"Update the XAML."}]}]`,
}) + "\n"
require.NoError(t, os.WriteFile(path, []byte(data), 0o644))
// An unreadable sibling that still exists: a symlink to a directory opens
// but cannot be scanned as JSONL, mimicking a transiently locked or
// permission-denied trace file. It must not be silently skipped, because
// the conversation may have spans in it.
target := filepath.Join(t.TempDir(), "dir")
require.NoError(t, os.Mkdir(target, 0o755))
sibling := filepath.Join(
dir,
"20260612T145205_bbbb2222_VSGitHubCopilot_traces.jsonl",
)
require.NoError(t, os.Symlink(target, sibling))
_, _, err := parseVisualStudioCopilotTestSession(t,
path, "visualstudio", "local",
)
require.Error(t, err,
"an unreadable sibling must fail the parse instead of yielding a "+
"partial transcript")
}
func TestParseVisualStudioCopilotTraceSession_MalformedTraceLineErrors(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(
dir,
"20260611T145205_aaaa1111_VSGitHubCopilot_traces.jsonl",
)
conversationID := "4a8f63f6-7626-4416-a874-fc7bd2c3f005"
data := vsCopilotTraceLineJSON(conversationID,
"chat gpt-5.5", "1781293600000000000", "1781293610000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": `[{"role":"user","parts":[{"type":"text","content":"Update the XAML."}]}]`,
}) + "\n" + `{"resourceSpans":` + "\n"
require.NoError(t, os.WriteFile(path, []byte(data), 0o644))
_, _, err := parseVisualStudioCopilotTestSession(t,
path, "visualstudio", "local",
)
require.Error(t, err,
"a malformed non-empty trace line must fail the parse instead of "+
"silently indexing a partial transcript")
assert.Contains(t, err.Error(), "decode")
}
func TestParseVisualStudioCopilotTraceSession_DeduplicatesChatOutputAcrossFiles(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(
dir,
"20260611T145205_aaaa1111_VSGitHubCopilot_traces.jsonl",
)
sibling := filepath.Join(
dir,
"20260612T145205_bbbb2222_VSGitHubCopilot_traces.jsonl",
)
conversationID := "4a8f63f6-7626-4416-a874-fc7bd2c3f005"
inputMessages := `[{"role":"user","parts":[{"type":"text","content":"Run the tests."}]}]`
outputMessages := `[{"role":"assistant","parts":[{"type":"text","content":"The tests passed."}]}]`
chatSpan := vsCopilotTraceLineJSONWithSpanID(conversationID, "chat_run",
"chat gpt-5.5",
"1781293600000000000", "1781293610000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": inputMessages,
"gen_ai.output.messages": outputMessages,
"gen_ai.usage.input_tokens": "100",
"gen_ai.usage.output_tokens": "20",
}) + "\n"
// The same chat span is flushed to both trace files for the conversation.
require.NoError(t, os.WriteFile(path, []byte(chatSpan), 0o644))
require.NoError(t, os.WriteFile(sibling, []byte(chatSpan), 0o644))
sess, msgs, err := parseVisualStudioCopilotTestSession(t,
path, "visualstudio", "local",
)
require.NoError(t, err)
require.NotNil(t, sess)
require.Len(t, msgs, 2)
assert.Equal(t, RoleUser, msgs[0].Role)
assert.Equal(t, "Run the tests.", msgs[0].Content)
assert.Equal(t, RoleAssistant, msgs[1].Role)
assert.Equal(t, "The tests passed.", msgs[1].Content)
// Usage from the duplicated span is counted once, not doubled.
assert.Equal(t, 20, sess.TotalOutputTokens)
assert.Equal(t, 100, sess.PeakContextTokens)
}
// TestParseVisualStudioCopilotTraceSession_PrefersCompleteChatOutputAcrossFiles
// verifies that one chat span flushed to sibling files with a growing payload
// emits a single assistant message carrying the complete output, with usage
// counted once. A streaming chat span can be exported mid-stream (partial text,
// interim token counts) and again at completion; keying dedup on span identity
// rather than identity-plus-content collapses these to the richest copy instead
// of emitting both and double-counting usage.
func TestParseVisualStudioCopilotTraceSession_PrefersCompleteChatOutputAcrossFiles(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(
dir,
"20260611T145205_aaaa1111_VSGitHubCopilot_traces.jsonl",
)
sibling := filepath.Join(
dir,
"20260612T145205_bbbb2222_VSGitHubCopilot_traces.jsonl",
)
conversationID := "4a8f63f6-7626-4416-a874-fc7bd2c3f005"
inputMessages := `[{"role":"user","parts":[{"type":"text","content":"Run the tests."}]}]`
partial := vsCopilotTraceLineJSONWithSpanID(conversationID, "chat_run",
"chat gpt-5.5",
"1781293600000000000", "1781293610000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": inputMessages,
"gen_ai.output.messages": `[{"role":"assistant","parts":[{"type":"text","content":"The tests"}]}]`,
"gen_ai.usage.input_tokens": "100",
"gen_ai.usage.output_tokens": "10",
}) + "\n"
complete := vsCopilotTraceLineJSONWithSpanID(conversationID, "chat_run",
"chat gpt-5.5",
"1781293600000000000", "1781293610000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": inputMessages,
"gen_ai.output.messages": `[{"role":"assistant","parts":[{"type":"text","content":"The tests passed."}]}]`,
"gen_ai.usage.input_tokens": "100",
"gen_ai.usage.output_tokens": "20",
}) + "\n"
// The same span is flushed partially to one file and completely to another.
require.NoError(t, os.WriteFile(path, []byte(partial), 0o644))
require.NoError(t, os.WriteFile(sibling, []byte(complete), 0o644))
sess, msgs, err := parseVisualStudioCopilotTestSession(t,
path, "visualstudio", "local",
)
require.NoError(t, err)
require.NotNil(t, sess)
require.Len(t, msgs, 2)
assert.Equal(t, RoleUser, msgs[0].Role)
assert.Equal(t, RoleAssistant, msgs[1].Role)
assert.Equal(t, "The tests passed.", msgs[1].Content,
"the complete chat output payload must win")
// Usage is counted once, from the complete copy, not summed across flushes.
assert.Equal(t, 20, sess.TotalOutputTokens)
assert.Equal(t, 100, sess.PeakContextTokens)
}
// TestParseVisualStudioCopilotTraceSession_PrefersCompleteChatUsageForVisibleOutput
// verifies that when one chat turn is flushed to sibling files with identical
// visible output but different token usage, the copy carrying the more complete
// usage is the one whose tokens are recorded, even when it ended earlier than a
// less complete copy. Choosing the latest flush alone would apply the leaner
// usage and undercount the turn's tokens.
func TestParseVisualStudioCopilotTraceSession_PrefersCompleteChatUsageForVisibleOutput(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(
dir,
"20260611T145205_aaaa1111_VSGitHubCopilot_traces.jsonl",
)
sibling := filepath.Join(
dir,
"20260612T145205_bbbb2222_VSGitHubCopilot_traces.jsonl",
)
conversationID := "4a8f63f6-7626-4416-a874-fc7bd2c3f005"
inputMessages := `[{"role":"user","parts":[{"type":"text","content":"Run the tests."}]}]`
outputMessages := `[{"role":"assistant","parts":[{"type":"text","content":"The tests passed."}]}]`
// Same span identity and identical visible output. The richer-usage copy
// ended earlier; the copy that ended later carries lower token counts.
richEarlier := vsCopilotTraceLineJSONWithSpanID(conversationID, "chat_run",
"chat gpt-5.5",
"1781293600000000000", "1781293610000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": inputMessages,
"gen_ai.output.messages": outputMessages,
"gen_ai.usage.input_tokens": "100",
"gen_ai.usage.output_tokens": "20",
}) + "\n"
leanerLater := vsCopilotTraceLineJSONWithSpanID(conversationID, "chat_run",
"chat gpt-5.5",
"1781293600000000000", "1781293620000000000",
map[string]string{
"gen_ai.operation.name": "chat",
"gen_ai.input.messages": inputMessages,
"gen_ai.output.messages": outputMessages,
"gen_ai.usage.input_tokens": "100",
"gen_ai.usage.output_tokens": "10",