-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcopilotcliSession.spec.ts
More file actions
1230 lines (1050 loc) · 53.5 KB
/
copilotcliSession.spec.ts
File metadata and controls
1230 lines (1050 loc) · 53.5 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 type { Session, SessionOptions } from '@github/copilot/sdk';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { ChatParticipantToolToken } from 'vscode';
import { ConfigKey, IConfigurationService } from '../../../../../platform/configuration/common/configurationService';
import { ILogService } from '../../../../../platform/log/common/logService';
import { NoopOTelService, resolveOTelConfig } from '../../../../../platform/otel/common/index';
import { NullRequestLogger } from '../../../../../platform/requestLogger/node/nullRequestLogger';
import { IRequestLogger } from '../../../../../platform/requestLogger/node/requestLogger';
import { TestWorkspaceService } from '../../../../../platform/test/node/testWorkspaceService';
import { IWorkspaceService } from '../../../../../platform/workspace/common/workspaceService';
import { CancellationToken } from '../../../../../util/vs/base/common/cancellation';
import { DisposableStore } from '../../../../../util/vs/base/common/lifecycle';
import * as path from '../../../../../util/vs/base/common/path';
import { URI } from '../../../../../util/vs/base/common/uri';
import { IInstantiationService } from '../../../../../util/vs/platform/instantiation/common/instantiation';
import { ChatSessionStatus, ChatToolInvocationPart, Uri } from '../../../../../vscodeTypes';
import { createExtensionUnitTestingServices } from '../../../../test/node/services';
import { MockChatResponseStream } from '../../../../test/node/testHelpers';
import { ExternalEditTracker } from '../../../common/externalEditTracker';
import { MockChatSessionMetadataStore } from '../../../common/test/mockChatSessionMetadataStore';
import { IWorkspaceInfo } from '../../../common/workspaceInfo';
import { FakeToolsService, ToolCall } from '../../common/copilotCLITools';
import { CopilotCLISession } from '../copilotcliSession';
import { PermissionRequest } from '../permissionHelpers';
import { IQuestion, IQuestionAnswer, IUserQuestionHandler } from '../userInputHelpers';
import { NullICopilotCLIImageSupport } from './testHelpers';
vi.mock('../cliHelpers', async (importOriginal) => ({
...(await importOriginal<typeof import('../cliHelpers')>()),
getCopilotCLISessionStateDir: () => '/mock-session-state',
}));
// Minimal shapes for types coming from the Copilot SDK we interact with
interface MockSdkEventHandler { (payload: unknown): void }
type MockSdkEventMap = Map<string, Set<MockSdkEventHandler>>;
class MockSdkSession {
onHandlers: MockSdkEventMap = new Map();
public sessionId = 'mock-session-id';
public _selectedModel: string | undefined = 'modelA';
public authInfo: unknown;
private _pendingPermissions = new Map<string, { resolve: (result: unknown) => void }>();
private _permissionCounter = 0;
private _pendingExitPlanMode = new Map<string, { resolve: (result: unknown) => void }>();
private _exitPlanModeCounter = 0;
on(event: string, handler: MockSdkEventHandler) {
if (!this.onHandlers.has(event)) {
this.onHandlers.set(event, new Set());
}
this.onHandlers.get(event)!.add(handler);
return () => this.onHandlers.get(event)!.delete(handler);
}
emit(event: string, data: unknown) {
this.onHandlers.get(event)?.forEach(h => h({ data }));
}
/**
* Simulate the SDK emitting a permission.requested event and await the response.
* The session's event handler will call respondToPermission() which resolves the returned promise.
*/
async emitPermissionRequest(permissionRequest: PermissionRequest): Promise<unknown> {
const requestId = `perm-${++this._permissionCounter}`;
return new Promise(resolve => {
this._pendingPermissions.set(requestId, { resolve });
this.emit('permission.requested', { requestId, permissionRequest });
});
}
respondToPermission(requestId: string, result: unknown) {
const pending = this._pendingPermissions.get(requestId);
if (pending) {
pending.resolve(result);
this._pendingPermissions.delete(requestId);
}
}
/**
* Simulate the SDK emitting an exit_plan_mode.requested event and await the response.
* The session's event handler will call respondToExitPlanMode() which resolves the returned promise.
*/
async emitExitPlanModeRequest(data: { summary: string; actions?: string[] }): Promise<unknown> {
const requestId = `exit-plan-${++this._exitPlanModeCounter}`;
return new Promise(resolve => {
this._pendingExitPlanMode.set(requestId, { resolve });
this.emit('exit_plan_mode.requested', { requestId, ...data });
});
}
respondToExitPlanMode(requestId: string, result: unknown) {
const pending = this._pendingExitPlanMode.get(requestId);
if (pending) {
pending.resolve(result);
this._pendingExitPlanMode.delete(requestId);
}
}
respondToUserInput(_requestId: string, _response: unknown) {
// placeholder for user input responses
}
public lastSendOptions: { prompt: string; mode?: string } | undefined;
public currentMode: string | undefined;
async send(options: { prompt: string; mode?: string }) {
this.lastSendOptions = options;
// Simulate a normal successful turn with a message
this.emit('user.message', { content: options.prompt });
this.emit('assistant.turn_start', {});
this.emit('assistant.message', { messageId: `msg_${Date.now()}`, content: `Echo: ${options.prompt}` });
this.emit('assistant.turn_end', {});
}
async compactHistory() { return { success: true }; }
async abort() { }
isAbortable(): boolean { return true; }
async initializeAndValidateTools() { }
getCurrentToolMetadata(): unknown[] | undefined { return this._toolMetadata; }
private _toolMetadata: unknown[] | undefined;
set toolMetadata(value: unknown[] | undefined) { this._toolMetadata = value; }
setAuthInfo(info: any) { this.authInfo = info; }
async getSelectedModel() { return this._selectedModel; }
async setSelectedModel(model: string) { this._selectedModel = model; }
async getEvents() { return []; }
getPlanPath(): string | null { return null; }
}
function createWorkspaceService(root: string): IWorkspaceService {
const rootUri = Uri.file(root);
return new class extends TestWorkspaceService {
override getWorkspaceFolders() {
return [
rootUri
];
}
override getWorkspaceFolder(uri: Uri) {
return uri.fsPath.startsWith(rootUri.fsPath) ? rootUri : undefined;
}
};
}
function workspaceInfoFor(workingDirectory: Uri | undefined): IWorkspaceInfo {
return {
folder: workingDirectory,
repository: undefined,
worktree: undefined,
worktreeProperties: undefined,
};
}
describe('CopilotCLISession', () => {
const disposables = new DisposableStore();
let sdkSession: MockSdkSession;
let workspaceService: IWorkspaceService;
let logger: ILogService;
let sessionWorkspaceInfo: IWorkspaceInfo;
let sessionAgentName: string | undefined;
let instaService: IInstantiationService;
let requestLogger: IRequestLogger;
let toolsService: FakeToolsService;
let configurationService: IConfigurationService;
let chatSessionMetadataStore: MockChatSessionMetadataStore;
let authInfo: NonNullable<SessionOptions['authInfo']>;
let userQuestionAnswer: IQuestionAnswer | undefined;
beforeEach(async () => {
const services = disposables.add(createExtensionUnitTestingServices());
const accessor = services.createTestingAccessor();
logger = accessor.get(ILogService);
requestLogger = new NullRequestLogger();
authInfo = {
type: 'token',
token: '',
host: 'https://github.com'
};
chatSessionMetadataStore = new MockChatSessionMetadataStore();
sdkSession = new MockSdkSession();
workspaceService = createWorkspaceService('/workspace');
sessionWorkspaceInfo = workspaceInfoFor(workspaceService.getWorkspaceFolders()![0]);
sessionAgentName = undefined;
configurationService = accessor.get(IConfigurationService);
await configurationService.setConfig(ConfigKey.Advanced.CLIPlanExitModeEnabled, true);
instaService = services.seal();
toolsService = new FakeToolsService();
userQuestionAnswer = undefined;
});
afterEach(() => {
vi.restoreAllMocks();
disposables.clear();
});
async function createSession(): Promise<CopilotCLISession> {
class FakeUserQuestionHandler implements IUserQuestionHandler {
_serviceBrand: undefined;
async askUserQuestion(question: IQuestion, toolInvocationToken: ChatParticipantToolToken, token: CancellationToken): Promise<IQuestionAnswer | undefined> {
return userQuestionAnswer;
}
}
return disposables.add(new CopilotCLISession(
sessionWorkspaceInfo,
sessionAgentName,
sdkSession as unknown as Session,
[],
logger,
workspaceService,
chatSessionMetadataStore,
instaService,
requestLogger,
new NullICopilotCLIImageSupport(),
toolsService,
new FakeUserQuestionHandler(),
configurationService,
new NoopOTelService(resolveOTelConfig({ env: {}, extensionVersion: '0.0.0', sessionId: 'test' })),
));
}
it('handles a successful request and streams assistant output', async () => {
const session = await createSession();
const stream = new MockChatResponseStream();
// Attach stream first, then invoke with new signature (no stream param)
session.attachStream(stream);
await session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Hello' }, [], undefined, authInfo, CancellationToken.None);
expect(session.status).toBe(ChatSessionStatus.Completed);
expect(stream.output.join('\n')).toContain('Echo: Hello');
// Listeners are disposed after completion, so we only assert original streamed content.
});
it('switches model when different modelId provided', async () => {
const session = await createSession();
const stream = new MockChatResponseStream();
session.attachStream(stream);
await session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Hi' }, [], 'modelB', authInfo, CancellationToken.None);
expect(sdkSession._selectedModel).toBe('modelB');
});
it('fails request when underlying send throws', async () => {
// Force send to throw
sdkSession.send = async () => { throw new Error('network'); };
const session = await createSession();
const stream = new MockChatResponseStream();
session.attachStream(stream);
await session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Boom' }, [], undefined, authInfo, CancellationToken.None);
expect(session.status).toBe(ChatSessionStatus.Failed);
expect(stream.output.join('\n')).toContain('Error: network');
});
it('emits status events on successful request', async () => {
const session = await createSession();
const statuses: (ChatSessionStatus | undefined)[] = [];
const listener = disposables.add(session.onDidChangeStatus(s => statuses.push(s)));
const stream = new MockChatResponseStream();
session.attachStream(stream);
await session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Status OK' }, [], 'modelA', authInfo, CancellationToken.None);
listener.dispose?.();
expect(statuses).toEqual([ChatSessionStatus.InProgress, ChatSessionStatus.Completed]);
expect(session.status).toBe(ChatSessionStatus.Completed);
});
it('emits status events on failed request', async () => {
// Force failure
sdkSession.send = async () => { throw new Error('boom'); };
const session = await createSession();
const statuses: (ChatSessionStatus | undefined)[] = [];
const listener = disposables.add(session.onDidChangeStatus(s => statuses.push(s)));
const stream = new MockChatResponseStream();
session.attachStream(stream);
await session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Will Fail' }, [], undefined, authInfo, CancellationToken.None);
listener.dispose?.();
expect(stream.output.join('\n')).toContain('Error: boom');
});
it('auto-approves read permission inside workspace without external handler', async () => {
let result: unknown;
sdkSession.send = async ({ prompt }: any) => {
sdkSession.emit('assistant.turn_start', {});
sdkSession.emit('assistant.message', { content: `Echo: ${prompt}` });
// Mid way through, make it look like the sdk requested permission while emitting other messages.
result = await sdkSession.emitPermissionRequest({ kind: 'read', path: path.join('/workspace', 'file.ts'), intention: 'Read file' });
sdkSession.emit('assistant.turn_end', {});
};
const session = await createSession();
const stream = new MockChatResponseStream();
session.attachStream(stream);
// Path must be absolute within workspace, should auto-approve
await session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Test' }, [], undefined, authInfo, CancellationToken.None);
expect(result).toEqual({ kind: 'approved' });
});
it('auto-approves read permission for files in session state directory', async () => {
let result: unknown;
const sessionFilePath = path.join('/mock-session-state', 'mock-session-id', 'plan.md');
sdkSession.send = async ({ prompt }: any) => {
sdkSession.emit('assistant.turn_start', {});
sdkSession.emit('assistant.message', { content: `Echo: ${prompt}` });
result = await sdkSession.emitPermissionRequest({ kind: 'read', path: sessionFilePath, intention: 'Read plan' });
sdkSession.emit('assistant.turn_end', {});
};
const session = await createSession();
const stream = new MockChatResponseStream();
session.attachStream(stream);
await session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Test' }, [], undefined, authInfo, CancellationToken.None);
expect(result).toEqual({ kind: 'approved' });
});
it('auto-approves write permission for files in session state directory', async () => {
let result: unknown;
const sessionFilePath = path.join('/mock-session-state', 'mock-session-id', 'plan.md');
sdkSession.send = async ({ prompt }: any) => {
sdkSession.emit('assistant.turn_start', {});
sdkSession.emit('assistant.message', { content: `Echo: ${prompt}` });
result = await sdkSession.emitPermissionRequest({ kind: 'write', fileName: sessionFilePath, intention: 'Write plan', diff: '' });
sdkSession.emit('assistant.turn_end', {});
};
const session = await createSession();
const stream = new MockChatResponseStream();
session.attachStream(stream);
await session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Test' }, [], undefined, authInfo, CancellationToken.None);
expect(result).toEqual({ kind: 'approved' });
});
it('auto-approves read permission for attached files outside workspace', async () => {
let result: unknown;
const attachedFilePath = '/outside-workspace/attached-file.ts';
sdkSession.send = async ({ prompt }: any) => {
sdkSession.emit('assistant.turn_start', {});
sdkSession.emit('assistant.message', { content: `Echo: ${prompt}` });
result = await sdkSession.emitPermissionRequest({ kind: 'read', path: attachedFilePath, intention: 'Read file' });
sdkSession.emit('assistant.turn_end', {});
};
const session = await createSession();
const stream = new MockChatResponseStream();
session.attachStream(stream);
const attachments = [{ type: 'file' as const, path: attachedFilePath, displayName: 'attached-file.ts' }];
await session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Test' }, attachments as any, undefined, authInfo, CancellationToken.None);
expect(result).toEqual({ kind: 'approved' });
});
it('does not auto-approve read permission for non-attached files outside workspace', async () => {
let result: unknown;
const nonAttachedFilePath = '/outside-workspace/other-file.ts';
const attachedFilePath = '/outside-workspace/attached-file.ts';
toolsService.setConfirmationResult('no');
sdkSession.send = async ({ prompt }: any) => {
sdkSession.emit('assistant.turn_start', {});
sdkSession.emit('assistant.message', { content: `Echo: ${prompt}` });
result = await sdkSession.emitPermissionRequest({ kind: 'read', path: nonAttachedFilePath, intention: 'Read file' });
sdkSession.emit('assistant.turn_end', {});
};
const session = await createSession();
const stream = new MockChatResponseStream();
session.attachStream(stream);
const attachments = [{ type: 'file' as const, path: attachedFilePath, displayName: 'attached-file.ts' }];
await session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Test' }, attachments as any, undefined, authInfo, CancellationToken.None);
expect(result).toEqual({ kind: 'denied-interactively-by-user' });
expect(toolsService.invokeToolCalls).toHaveLength(1);
});
it('auto-approves read permission inside working directory without external handler', async () => {
let result: unknown;
sessionWorkspaceInfo = workspaceInfoFor(URI.file('/workingDirectory'));
sdkSession.send = async ({ prompt }: any) => {
sdkSession.emit('assistant.turn_start', {});
sdkSession.emit('assistant.message', { content: `Echo: ${prompt}` });
// Mid way through, make it look like the sdk requested permission while emitting other messages.
result = await sdkSession.emitPermissionRequest({ kind: 'read', path: path.join('/workingDirectory', 'file.ts'), intention: 'Read file' });
sdkSession.emit('assistant.turn_end', {});
};
const session = await createSession();
const stream = new MockChatResponseStream();
session.attachStream(stream);
// Path must be absolute within workspace, should auto-approve
await session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Test' }, [], undefined, authInfo, CancellationToken.None);
expect(result).toEqual({ kind: 'approved' });
});
it('auto-approves read permission for files in workspace folder when worktree is the working directory', async () => {
let result: unknown;
const worktreeUri = URI.file('/worktrees/session1');
const folderUri = URI.file('/original-repo');
sessionWorkspaceInfo = {
folder: folderUri,
repository: folderUri,
worktree: worktreeUri,
worktreeProperties: { version: 1, autoCommit: false, baseCommit: 'abc', branchName: 'main', repositoryPath: '/original-repo', worktreePath: '/worktrees/session1' },
};
sdkSession.send = async ({ prompt }: any) => {
sdkSession.emit('assistant.turn_start', {});
sdkSession.emit('assistant.message', { content: `Echo: ${prompt}` });
// File is in workspace.folder (/original-repo), not in the worktree which is the working directory
result = await sdkSession.emitPermissionRequest({ kind: 'read', path: path.join('/original-repo', 'src/main.ts'), intention: 'Read file' });
sdkSession.emit('assistant.turn_end', {});
};
const session = await createSession();
const stream = new MockChatResponseStream();
session.attachStream(stream);
await session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Test' }, [], undefined, authInfo, CancellationToken.None);
expect(result).toEqual({ kind: 'approved' });
});
it('auto-approves read permission for files in the worktree when workspace has both worktree and repository', async () => {
let result: unknown;
const worktreeUri = URI.file('/worktrees/session1');
const folderUri = URI.file('/original-repo');
sessionWorkspaceInfo = {
folder: folderUri,
repository: folderUri,
worktree: worktreeUri,
worktreeProperties: { version: 1, autoCommit: false, baseCommit: 'abc', branchName: 'main', repositoryPath: '/original-repo', worktreePath: '/worktrees/session1' },
};
sdkSession.send = async ({ prompt }: any) => {
sdkSession.emit('assistant.turn_start', {});
sdkSession.emit('assistant.message', { content: `Echo: ${prompt}` });
// File is in the worktree which is also the working directory
result = await sdkSession.emitPermissionRequest({ kind: 'read', path: path.join('/worktrees/session1', 'src/main.ts'), intention: 'Read file' });
sdkSession.emit('assistant.turn_end', {});
};
const session = await createSession();
const stream = new MockChatResponseStream();
session.attachStream(stream);
await session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Test' }, [], undefined, authInfo, CancellationToken.None);
expect(result).toEqual({ kind: 'approved' });
});
it('requires read permission outside workspace and working directory', async () => {
let result: unknown;
toolsService.setConfirmationResult('no');
sdkSession.send = async ({ prompt }: any) => {
sdkSession.emit('assistant.turn_start', {});
sdkSession.emit('assistant.message', { content: `Echo: ${prompt}` });
// Mid way through, make it look like the sdk requested permission while emitting other messages.
result = await sdkSession.emitPermissionRequest({ kind: 'read', path: path.join('/workingDirectory', 'file.ts'), intention: 'Read file' });
sdkSession.emit('assistant.turn_end', {});
};
const session = await createSession();
const stream = new MockChatResponseStream();
session.attachStream(stream);
// Path must be absolute within workspace, should auto-approve
await session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Test' }, [], undefined, authInfo, CancellationToken.None);
expect(result).toEqual({ kind: 'denied-interactively-by-user' });
expect(toolsService.invokeToolCalls).toHaveLength(1);
expect(toolsService.invokeToolCalls[0].input).toMatchObject({
title: 'Read file(s)',
message: 'Read file'
});
});
it('approves write permission when handler returns true', async () => {
let result: unknown;
const session = await createSession();
toolsService.setConfirmationResult('yes');
sdkSession.send = async ({ prompt }: any) => {
sdkSession.emit('assistant.turn_start', {});
sdkSession.emit('assistant.message', { content: `Echo: ${prompt}` });
// Mid way through, make it look like the sdk requested permission while emitting other messages.
result = await sdkSession.emitPermissionRequest({ kind: 'write', fileName: 'a.ts', intention: 'Update file', diff: '' });
sdkSession.emit('assistant.turn_end', {});
};
const stream = new MockChatResponseStream();
session.attachStream(stream);
await session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Write' }, [], undefined, authInfo, CancellationToken.None);
expect(result).toEqual({ kind: 'approved' });
});
it('denies write permission when handler returns false', async () => {
let result: unknown;
const session = await createSession();
toolsService.setConfirmationResult('no');
sdkSession.send = async ({ prompt }: any) => {
sdkSession.emit('assistant.turn_start', {});
sdkSession.emit('assistant.message', { content: `Echo: ${prompt}` });
// Mid way through, make it look like the sdk requested permission while emitting other messages.
result = await sdkSession.emitPermissionRequest({ kind: 'write', fileName: 'b.ts', intention: 'Update file', diff: '' });
sdkSession.emit('assistant.turn_end', {});
};
const stream = new MockChatResponseStream();
session.attachStream(stream);
await session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Write' }, [], undefined, authInfo, CancellationToken.None);
expect(result).toEqual({ kind: 'denied-interactively-by-user' });
});
it('denies write permission when handler throws', async () => {
let result: unknown;
const session = await createSession();
toolsService.invokeTool = vi.fn(async () => {
throw new Error('oops');
});
sdkSession.send = async ({ prompt }: any) => {
sdkSession.emit('assistant.turn_start', {});
sdkSession.emit('assistant.message', { content: `Echo: ${prompt}` });
// Mid way through, make it look like the sdk requested permission while emitting other messages.
result = await sdkSession.emitPermissionRequest({ kind: 'write', fileName: 'err.ts', intention: 'Update file', diff: '' });
sdkSession.emit('assistant.turn_end', {});
};
const stream = new MockChatResponseStream();
session.attachStream(stream);
await session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Write' }, [], undefined, authInfo, CancellationToken.None);
expect(result).toEqual({ kind: 'denied-interactively-by-user' });
});
it('preserves order of edit toolCallIds and permissions for multiple pending edits', async () => {
// Arrange a deferred send so we can emit tool events before request finishes
let resolveSend: () => void;
sdkSession.send = async () => new Promise<void>(r => { resolveSend = r; });
const session = await createSession();
toolsService.setConfirmationResult('yes');
const stream = new MockChatResponseStream();
session.attachStream(stream);
// Spy on trackEdit to capture ordering (we don't want to depend on externalEdit mechanics here)
const trackedOrder: string[] = [];
const trackSpy = vi.spyOn(ExternalEditTracker.prototype, 'trackEdit').mockImplementation(async function (this: any, editKey: string) {
trackedOrder.push(editKey);
// Immediately resolve to avoid hanging on externalEdit lifecycle
return Promise.resolve();
});
// Act: start handling request (do not await yet)
const requestPromise = session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Edits' }, [], undefined, authInfo, CancellationToken.None);
// Wait a tick to ensure event listeners are registered inside handleRequest
await new Promise(r => setTimeout(r, 0));
// Emit 10 edit tool start events in rapid succession for the same file
const filePath = '/workspace/abc.py';
for (let i = 1; i <= 10; i++) {
const editToolCall: ToolCall = {
toolName: 'edit',
toolCallId: String(i),
arguments: { path: filePath, new_str: 'new content' },
};
sdkSession.emit('tool.execution_start', editToolCall);
}
// Now request permissions sequentially AFTER all tool calls have been emitted
const permissionResults: any[] = [];
for (let i = 1; i <= 10; i++) {
// Each permission request should dequeue the next toolCallId for the file
const result = await sdkSession.emitPermissionRequest({
kind: 'write',
fileName: filePath,
intention: 'Apply edit',
diff: '',
toolCallId: String(i)
});
permissionResults.push(result);
// Complete the edit so the tracker (if it were real) would finish; emit completion event
sdkSession.emit('tool.execution_complete', {
toolCallId: String(i),
toolName: 'str_replace_editor',
arguments: { command: 'str_replace', path: filePath },
success: true,
result: { content: '' }
});
}
// Allow the request to finish
resolveSend!();
await requestPromise;
// Assert ordering of trackEdit invocations exactly matches toolCallIds 1..10
expect(trackedOrder).toEqual(Array.from({ length: 10 }, (_, i) => String(i + 1)));
expect(permissionResults.every(r => r.kind === 'approved')).toBe(true);
expect(trackSpy).toHaveBeenCalledTimes(10);
trackSpy.mockRestore();
});
it('delays tool invocation messages for permission-requiring tools until permission is resolved', async () => {
let resolveSend: () => void;
sdkSession.send = async () => new Promise<void>(r => { resolveSend = r; });
const session = await createSession();
const pushedParts: unknown[] = [];
const stream = new MockChatResponseStream(part => pushedParts.push(part));
session.attachStream(stream);
toolsService.setConfirmationResult('yes');
const requestPromise = session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Run bash' }, [], undefined, authInfo, CancellationToken.None);
await new Promise(r => setTimeout(r, 0));
// Emit a bash tool start - this should be delayed
const bashToolCall: ToolCall = { toolName: 'bash', toolCallId: 'bash-delay-1', arguments: { command: 'echo hi', description: 'Echo test' } };
sdkSession.emit('tool.execution_start', bashToolCall);
await new Promise(r => setTimeout(r, 0));
// No ChatToolInvocationPart should be pushed yet for the bash tool
const toolPartsBeforePermission = pushedParts.filter(p => p instanceof ChatToolInvocationPart);
expect(toolPartsBeforePermission).toHaveLength(0);
// When permission is requested, the pending messages should be flushed
await sdkSession.emitPermissionRequest({
kind: 'shell',
commands: [{ identifier: 'echo hi', readOnly: false }],
intention: 'Run command',
fullCommandText: 'echo hi',
possiblePaths: [],
possibleUrls: [],
hasWriteFileRedirection: false,
canOfferSessionApproval: false
});
await new Promise(r => setTimeout(r, 0));
const toolPartsAfterPermission = pushedParts.filter(p => p instanceof ChatToolInvocationPart);
expect(toolPartsAfterPermission.length).toBeGreaterThanOrEqual(1);
sdkSession.emit('tool.execution_complete', { toolCallId: 'bash-delay-1', toolName: 'bash', success: true, result: { content: 'hi' } });
resolveSend!();
await requestPromise;
});
it('immediately pushes invocation messages for non-permission-requiring tools like MCP', async () => {
let resolveSend: () => void;
sdkSession.send = async () => new Promise<void>(r => { resolveSend = r; });
const session = await createSession();
const pushedParts: unknown[] = [];
const stream = new MockChatResponseStream(part => pushedParts.push(part));
session.attachStream(stream);
const requestPromise = session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Run MCP tool' }, [], undefined, authInfo, CancellationToken.None);
await new Promise(r => setTimeout(r, 0));
// Emit an MCP tool start - this should NOT be delayed
sdkSession.emit('tool.execution_start', { toolName: 'my_mcp_tool', toolCallId: 'mcp-nodelay-1', mcpServerName: 'test-server', mcpToolName: 'my-tool', arguments: { foo: 'bar' } });
await new Promise(r => setTimeout(r, 0));
const toolParts = pushedParts.filter(p => p instanceof ChatToolInvocationPart);
expect(toolParts.length).toBeGreaterThanOrEqual(1);
sdkSession.emit('tool.execution_complete', { toolCallId: 'mcp-nodelay-1', toolName: 'my_mcp_tool', mcpServerName: 'test-server', mcpToolName: 'my-tool', success: true, result: { contents: [] } });
resolveSend!();
await requestPromise;
});
it('flushes delayed invocation messages when assistant message arrives', async () => {
let resolveSend: () => void;
sdkSession.send = async () => new Promise<void>(r => { resolveSend = r; });
const session = await createSession();
const pushedParts: unknown[] = [];
const stream = new MockChatResponseStream(part => pushedParts.push(part));
session.attachStream(stream);
const requestPromise = session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { prompt: 'Test flush' }, [], undefined, authInfo, CancellationToken.None);
await new Promise(r => setTimeout(r, 0));
// Emit a bash tool start (delayed)
sdkSession.emit('tool.execution_start', { toolName: 'bash', toolCallId: 'bash-flush-1', arguments: { command: 'ls', description: 'List' } });
await new Promise(r => setTimeout(r, 0));
expect(pushedParts.filter(p => p instanceof ChatToolInvocationPart)).toHaveLength(0);
// Emit an assistant message delta - should flush
sdkSession.emit('assistant.message_delta', { deltaContent: 'Hello', messageId: 'msg-1' });
await new Promise(r => setTimeout(r, 0));
expect(pushedParts.filter(p => p instanceof ChatToolInvocationPart).length).toBeGreaterThanOrEqual(1);
sdkSession.emit('tool.execution_complete', { toolCallId: 'bash-flush-1', toolName: 'bash', success: true, result: { content: '' } });
resolveSend!();
await requestPromise;
});
describe('/compact command', () => {
it('compacts the conversation and reports success', async () => {
const session = await createSession();
const stream = new MockChatResponseStream();
session.attachStream(stream);
await session.handleRequest({ id: '', toolInvocationToken: undefined as never }, { command: 'compact', prompt: '' }, [], undefined, authInfo, CancellationToken.None);
expect(sdkSession.currentMode).toBe('interactive');
expect(stream.output.join('\n')).toContain('Compacted conversation.');
});
});
describe('steering (sending messages to a busy session)', () => {
it('allows steering after an earlier failed request', async () => {
sdkSession.send = async () => {
throw new Error('boom');
};
const session = await createSession();
const stream = new MockChatResponseStream();
session.attachStream(stream);
await session.handleRequest(
{ id: 'req-1', toolInvocationToken: undefined as never },
{ prompt: 'Initial failure' }, [], undefined, authInfo, CancellationToken.None
);
expect(session.status).toBe(ChatSessionStatus.Failed);
let resolveSecondSend!: () => void;
let sendCallCount = 0;
sdkSession.send = async (options: any) => {
sendCallCount++;
sdkSession.lastSendOptions = options;
if (sendCallCount === 1) {
await new Promise<void>(r => { resolveSecondSend = r; });
}
sdkSession.emit('assistant.turn_start', {});
sdkSession.emit('assistant.message', { content: `Echo: ${options.prompt}` });
sdkSession.emit('assistant.turn_end', {});
};
const secondRequest = session.handleRequest(
{ id: 'req-2', toolInvocationToken: undefined as never },
{ prompt: 'Second request' }, [], undefined, authInfo, CancellationToken.None
);
await new Promise(r => setTimeout(r, 10));
const steeringRequest = session.handleRequest(
{ id: 'req-3', toolInvocationToken: undefined as never },
{ prompt: 'Steer after failure' }, [], undefined, authInfo, CancellationToken.None
);
await new Promise(r => setTimeout(r, 10));
expect(sdkSession.lastSendOptions?.mode).toBe('immediate');
expect(sdkSession.lastSendOptions?.prompt).toBe('Steer after failure');
resolveSecondSend();
await Promise.all([secondRequest, steeringRequest]);
expect(session.status).toBe(ChatSessionStatus.Completed);
});
it('routes through steering when session is already InProgress', async () => {
// Arrange: make `send` block so the first request stays in progress
let resolveFirstSend: () => void = () => { };
let sendCallCount = 0;
sdkSession.send = async (options: any) => {
sendCallCount++;
sdkSession.lastSendOptions = options;
if (sendCallCount === 1) {
// First request blocks until we resolve
await new Promise<void>(r => { resolveFirstSend = r; });
}
sdkSession.emit('assistant.turn_start', {});
sdkSession.emit('assistant.message', { content: `Echo: ${options.prompt}` });
sdkSession.emit('assistant.turn_end', {});
};
const session = await createSession();
const stream = new MockChatResponseStream();
session.attachStream(stream);
// Act: start first request (will block in send)
const firstRequest = session.handleRequest(
{ id: 'req-1', toolInvocationToken: undefined as never },
{ prompt: 'First prompt' }, [], undefined, authInfo, CancellationToken.None
);
await new Promise(r => setTimeout(r, 10));
// Session should be InProgress
expect(session.status).toBe(ChatSessionStatus.InProgress);
// Send a steering request while first is still running
const steeringRequest = session.handleRequest(
{ id: 'req-2', toolInvocationToken: undefined as never },
{ prompt: 'Steer this' }, [], undefined, authInfo, CancellationToken.None
);
await new Promise(r => setTimeout(r, 10));
// The steering send should have been called with mode: 'immediate'
expect(sdkSession.lastSendOptions?.mode).toBe('immediate');
expect(sdkSession.lastSendOptions?.prompt).toBe('Steer this');
// Unblock the first request
resolveFirstSend();
await Promise.all([firstRequest, steeringRequest]);
expect(session.status).toBe(ChatSessionStatus.Completed);
});
it('does not set mode to immediate for the first (non-steering) request', async () => {
const session = await createSession();
const stream = new MockChatResponseStream();
session.attachStream(stream);
await session.handleRequest(
{ id: 'req-1', toolInvocationToken: undefined as never },
{ prompt: 'Normal prompt' }, [], undefined, authInfo, CancellationToken.None
);
expect(sdkSession.lastSendOptions?.mode).toBeUndefined();
expect(sdkSession.lastSendOptions?.prompt).toBe('Normal prompt');
});
it('accumulates attachments across steering requests for permission auto-approval', async () => {
let resolveFirstSend!: () => void;
let sendCallCount = 0;
let permissionResult: unknown;
// The attached file path is outside workspace
const attachedFilePath = '/outside-workspace/steering-file.ts';
sdkSession.send = async (options: any) => {
sendCallCount++;
const thisCallNumber = sendCallCount;
sdkSession.lastSendOptions = options;
if (thisCallNumber === 1) {
await new Promise<void>(r => { resolveFirstSend = r; });
}
sdkSession.emit('assistant.turn_start', {});
// On the first (original) request, try to read the file that was
// attached in the second (steering) request.
if (thisCallNumber === 1) {
permissionResult = await sdkSession.emitPermissionRequest({
kind: 'read', path: attachedFilePath, intention: 'Read file'
});
}
sdkSession.emit('assistant.message', { content: `Echo: ${options.prompt}` });
sdkSession.emit('assistant.turn_end', {});
};
const session = await createSession();
const stream = new MockChatResponseStream();
session.attachStream(stream);
// Start first request with no attachments
const firstRequest = session.handleRequest(
{ id: 'req-1', toolInvocationToken: undefined as never },
{ prompt: 'First' }, [], undefined, authInfo, CancellationToken.None
);
await new Promise(r => setTimeout(r, 10));
// Send steering request WITH the file attachment
const steeringAttachments = [{ type: 'file' as const, path: attachedFilePath, displayName: 'steering-file.ts' }];
const steeringRequest = session.handleRequest(
{ id: 'req-2', toolInvocationToken: undefined as never },
{ prompt: 'Use that file' }, steeringAttachments as any, undefined, authInfo, CancellationToken.None
);
await new Promise(r => setTimeout(r, 10));
// Now unblock the first send - it will try to read the steering-attached file
resolveFirstSend();
await Promise.all([firstRequest, steeringRequest]);
// The file was attached in the steering request, so it should be auto-approved
expect(permissionResult).toEqual({ kind: 'approved' });
});
it('updates the pending prompt to the latest steering message', async () => {
let resolveFirstSend!: () => void;
let sendCallCount = 0;
sdkSession.send = async (options: any) => {
sendCallCount++;
sdkSession.lastSendOptions = options;
if (sendCallCount === 1) {
await new Promise<void>(r => { resolveFirstSend = r; });
}
sdkSession.emit('assistant.turn_start', {});
sdkSession.emit('assistant.message', { content: `Echo: ${options.prompt}` });
sdkSession.emit('assistant.turn_end', {});
};
const session = await createSession();
const stream = new MockChatResponseStream();
session.attachStream(stream);
// Start first request
const firstRequest = session.handleRequest(
{ id: 'req-1', toolInvocationToken: undefined as never },
{ prompt: 'Original prompt' }, [], undefined, authInfo, CancellationToken.None
);
await new Promise(r => setTimeout(r, 10));
expect(session.pendingPrompt).toBe('Original prompt');
// Steer
const steeringRequest = session.handleRequest(
{ id: 'req-2', toolInvocationToken: undefined as never },
{ prompt: 'New direction' }, [], undefined, authInfo, CancellationToken.None
);
await new Promise(r => setTimeout(r, 10));
expect(session.pendingPrompt).toBe('New direction');
resolveFirstSend();
await Promise.all([firstRequest, steeringRequest]);
});
it('steering request does not change session status to InProgress again', async () => {
let resolveFirstSend!: () => void;
let sendCallCount = 0;
sdkSession.send = async (options: any) => {
sendCallCount++;
sdkSession.lastSendOptions = options;
if (sendCallCount === 1) {
await new Promise<void>(r => { resolveFirstSend = r; });
}
sdkSession.emit('assistant.turn_start', {});
sdkSession.emit('assistant.message', { content: `Echo: ${options.prompt}` });
sdkSession.emit('assistant.turn_end', {});
};
const session = await createSession();
const statuses: (ChatSessionStatus | undefined)[] = [];
disposables.add(session.onDidChangeStatus(s => statuses.push(s)));
const stream = new MockChatResponseStream();
session.attachStream(stream);
// Start first request
const firstRequest = session.handleRequest(
{ id: 'req-1', toolInvocationToken: undefined as never },
{ prompt: 'First' }, [], undefined, authInfo, CancellationToken.None
);
await new Promise(r => setTimeout(r, 10));
// Should have fired InProgress once
expect(statuses).toEqual([ChatSessionStatus.InProgress]);
// Send steering request
const steeringRequest = session.handleRequest(
{ id: 'req-2', toolInvocationToken: undefined as never },
{ prompt: 'Steer' }, [], undefined, authInfo, CancellationToken.None
);
await new Promise(r => setTimeout(r, 10));
// InProgress should NOT fire again from the steering path
expect(statuses).toEqual([ChatSessionStatus.InProgress]);
resolveFirstSend();
await Promise.all([firstRequest, steeringRequest]);
// Final status should be Completed
expect(statuses).toEqual([ChatSessionStatus.InProgress, ChatSessionStatus.Completed]);
});
it('throws on disposed session', async () => {
const session = await createSession();
session.dispose();
await expect(
session.handleRequest(
{ id: 'req-1', toolInvocationToken: undefined as never },
{ prompt: 'Hello' }, [], undefined, authInfo, CancellationToken.None
)
).rejects.toThrow('Session disposed');
});
it('updates the toolInvocationToken on each request including steering', async () => {
let resolveFirstSend!: () => void;
let sendCallCount = 0;
sdkSession.send = async (options: any) => {
sendCallCount++;
sdkSession.lastSendOptions = options;
if (sendCallCount === 1) {
await new Promise<void>(r => { resolveFirstSend = r; });
}
sdkSession.emit('assistant.turn_start', {});
sdkSession.emit('assistant.message', { content: `Echo: ${options.prompt}` });
sdkSession.emit('assistant.turn_end', {});
};
const session = await createSession();
const stream = new MockChatResponseStream();
session.attachStream(stream);
const token1 = { toString: () => 'token-1' } as unknown as ChatParticipantToolToken;
const token2 = { toString: () => 'token-2' } as unknown as ChatParticipantToolToken;
const firstRequest = session.handleRequest(
{ id: 'req-1', toolInvocationToken: token1 },
{ prompt: 'First' }, [], undefined, authInfo, CancellationToken.None
);
await new Promise(r => setTimeout(r, 10));
// Steering replaces the token
const steeringRequest = session.handleRequest(
{ id: 'req-2', toolInvocationToken: token2 },
{ prompt: 'Steer' }, [], undefined, authInfo, CancellationToken.None
);
await new Promise(r => setTimeout(r, 10));
// Can't directly access private _toolInvocationToken, but we verify
// indirectly that the session accepted both tokens without error.
// The key assertion is that handleRequest didn't throw.
resolveFirstSend();