forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionFsTests.cs
More file actions
526 lines (452 loc) · 19.2 KB
/
Copy pathSessionFsTests.cs
File metadata and controls
526 lines (452 loc) · 19.2 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
using GitHub.Copilot.SDK.Rpc;
using GitHub.Copilot.SDK.Test.Harness;
using Microsoft.Extensions.AI;
using Xunit;
using Xunit.Abstractions;
namespace GitHub.Copilot.SDK.Test;
public class SessionFsTests(E2ETestFixture fixture, ITestOutputHelper output)
: E2ETestBase(fixture, "session_fs", output)
{
private static readonly SessionFsConfig SessionFsConfig = new()
{
InitialCwd = "/",
SessionStatePath = "/session-state",
Conventions = SessionFsSetProviderRequestConventions.Posix,
};
[Fact]
public async Task Should_Route_File_Operations_Through_The_Session_Fs_Provider()
{
var providerRoot = CreateProviderRoot();
try
{
await using var client = CreateSessionFsClient(providerRoot);
var session = await client.CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
CreateSessionFsHandler = s => new TestSessionFsHandler(s.SessionId, providerRoot),
});
var msg = await session.SendAndWaitAsync(new MessageOptions { Prompt = "What is 100 + 200?" });
Assert.Contains("300", msg?.Data.Content ?? string.Empty);
await session.DisposeAsync();
var eventsPath = GetStoredPath(providerRoot, session.SessionId, "/session-state/events.jsonl");
await WaitForConditionAsync(() => File.Exists(eventsPath));
var content = await ReadAllTextSharedAsync(eventsPath);
Assert.Contains("300", content);
}
finally
{
await TryDeleteDirectoryAsync(providerRoot);
}
}
[Fact]
public async Task Should_Load_Session_Data_From_Fs_Provider_On_Resume()
{
var providerRoot = CreateProviderRoot();
try
{
await using var client = CreateSessionFsClient(providerRoot);
Func<CopilotSession, ISessionFsHandler> createSessionFsHandler = s => new TestSessionFsHandler(s.SessionId, providerRoot);
var session1 = await client.CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
CreateSessionFsHandler = createSessionFsHandler,
});
var sessionId = session1.SessionId;
var msg = await session1.SendAndWaitAsync(new MessageOptions { Prompt = "What is 50 + 50?" });
Assert.Contains("100", msg?.Data.Content ?? string.Empty);
await session1.DisposeAsync();
var eventsPath = GetStoredPath(providerRoot, sessionId, "/session-state/events.jsonl");
await WaitForConditionAsync(() => File.Exists(eventsPath));
var session2 = await client.ResumeSessionAsync(sessionId, new ResumeSessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
CreateSessionFsHandler = createSessionFsHandler,
});
var msg2 = await session2.SendAndWaitAsync(new MessageOptions { Prompt = "What is that times 3?" });
Assert.Contains("300", msg2?.Data.Content ?? string.Empty);
await session2.DisposeAsync();
}
finally
{
await TryDeleteDirectoryAsync(providerRoot);
}
}
[Fact]
public async Task Should_Reject_SetProvider_When_Sessions_Already_Exist()
{
var providerRoot = CreateProviderRoot();
try
{
await using var client1 = CreateSessionFsClient(providerRoot, useStdio: false);
var createSessionFsHandler = (Func<CopilotSession, ISessionFsHandler>)(s => new TestSessionFsHandler(s.SessionId, providerRoot));
_ = await client1.CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
CreateSessionFsHandler = createSessionFsHandler,
});
var port = client1.ActualPort
?? throw new InvalidOperationException("Client1 is not using TCP mode; ActualPort is null");
var client2 = Ctx.CreateClient(
useStdio: false,
options: new CopilotClientOptions
{
CliUrl = $"localhost:{port}",
LogLevel = "error",
SessionFs = SessionFsConfig,
});
try
{
await Assert.ThrowsAnyAsync<Exception>(() => client2.StartAsync());
}
finally
{
try
{
await client2.ForceStopAsync();
}
catch (IOException ex)
{
Console.Error.WriteLine($"Ignoring expected teardown IOException from ForceStopAsync: {ex.Message}");
}
}
}
finally
{
await TryDeleteDirectoryAsync(providerRoot);
}
}
[Fact]
public async Task Should_Map_Large_Output_Handling_Into_SessionFs()
{
var providerRoot = CreateProviderRoot();
try
{
const int largeContentSize = 100_000;
var suppliedFileContent = new string('x', largeContentSize);
await using var client = CreateSessionFsClient(providerRoot);
var session = await client.CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
CreateSessionFsHandler = s => new TestSessionFsHandler(s.SessionId, providerRoot),
Tools =
[
AIFunctionFactory.Create(() => suppliedFileContent, "get_big_string", "Returns a large string")
],
});
await session.SendAndWaitAsync(new MessageOptions
{
Prompt = "Call the get_big_string tool and reply with the word DONE only.",
});
var messages = await session.GetMessagesAsync();
var toolResult = FindToolCallResult(messages, "get_big_string");
Assert.NotNull(toolResult);
Assert.Contains("/session-state/temp/", toolResult);
var match = System.Text.RegularExpressions.Regex.Match(
toolResult!,
@"([/\\]session-state[/\\]temp[/\\][^\s]+)");
Assert.True(match.Success);
var fileContent = await ReadAllTextSharedAsync(GetStoredPath(providerRoot, session.SessionId, match.Groups[1].Value));
Assert.Equal(suppliedFileContent, fileContent);
await session.DisposeAsync();
}
finally
{
await TryDeleteDirectoryAsync(providerRoot);
}
}
[Fact]
public async Task Should_Succeed_With_Compaction_While_Using_SessionFs()
{
var providerRoot = CreateProviderRoot();
try
{
await using var client = CreateSessionFsClient(providerRoot);
var session = await client.CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll,
CreateSessionFsHandler = s => new TestSessionFsHandler(s.SessionId, providerRoot),
});
SessionCompactionCompleteEvent? compactionEvent = null;
using var _ = session.On(evt =>
{
if (evt is SessionCompactionCompleteEvent complete)
{
compactionEvent = complete;
}
});
await session.SendAndWaitAsync(new MessageOptions { Prompt = "What is 2+2?" });
var eventsPath = GetStoredPath(providerRoot, session.SessionId, "/session-state/events.jsonl");
await WaitForConditionAsync(() => File.Exists(eventsPath), TimeSpan.FromSeconds(30));
var contentBefore = await ReadAllTextSharedAsync(eventsPath);
Assert.DoesNotContain("checkpointNumber", contentBefore);
await session.Rpc.History.CompactAsync();
await WaitForConditionAsync(() => compactionEvent is not null, TimeSpan.FromSeconds(30));
Assert.True(compactionEvent!.Data.Success);
await WaitForConditionAsync(async () =>
{
var content = await ReadAllTextSharedAsync(eventsPath);
return content.Contains("checkpointNumber", StringComparison.Ordinal);
}, TimeSpan.FromSeconds(30));
}
finally
{
await TryDeleteDirectoryAsync(providerRoot);
}
}
private CopilotClient CreateSessionFsClient(string providerRoot, bool useStdio = true)
{
Directory.CreateDirectory(providerRoot);
return Ctx.CreateClient(
useStdio: useStdio,
options: new CopilotClientOptions
{
SessionFs = SessionFsConfig,
});
}
private static string? FindToolCallResult(IReadOnlyList<SessionEvent> messages, string toolName)
{
var callId = messages
.OfType<ToolExecutionStartEvent>()
.FirstOrDefault(m => string.Equals(m.Data.ToolName, toolName, StringComparison.Ordinal))
?.Data.ToolCallId;
if (callId is null)
{
return null;
}
return messages
.OfType<ToolExecutionCompleteEvent>()
.FirstOrDefault(m => string.Equals(m.Data.ToolCallId, callId, StringComparison.Ordinal))
?.Data.Result?.Content;
}
private static string CreateProviderRoot()
=> Path.Join(Path.GetTempPath(), $"copilot-sessionfs-{Guid.NewGuid():N}");
private static string GetStoredPath(string providerRoot, string sessionId, string sessionPath)
{
var safeSessionId = NormalizeRelativePathSegment(sessionId, nameof(sessionId));
var relativeSegments = sessionPath
.TrimStart('/', '\\')
.Split(['/', '\\'], StringSplitOptions.RemoveEmptyEntries)
.Select(segment => NormalizeRelativePathSegment(segment, nameof(sessionPath)))
.ToArray();
return Path.Join([providerRoot, safeSessionId, .. relativeSegments]);
}
private static async Task WaitForConditionAsync(Func<bool> condition, TimeSpan? timeout = null)
{
await WaitForConditionAsync(() => Task.FromResult(condition()), timeout);
}
private static async Task WaitForConditionAsync(Func<Task<bool>> condition, TimeSpan? timeout = null)
{
var deadline = DateTime.UtcNow + (timeout ?? TimeSpan.FromSeconds(30));
Exception? lastException = null;
while (DateTime.UtcNow < deadline)
{
try
{
if (await condition())
{
return;
}
}
catch (IOException ex)
{
lastException = ex;
}
catch (UnauthorizedAccessException ex)
{
lastException = ex;
}
await Task.Delay(100);
}
throw new TimeoutException("Timed out waiting for condition.", lastException);
}
private static async Task<string> ReadAllTextSharedAsync(string path, CancellationToken cancellationToken = default)
{
await using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
using var reader = new StreamReader(stream);
return await reader.ReadToEndAsync(cancellationToken);
}
private static async Task TryDeleteDirectoryAsync(string path)
{
if (!Directory.Exists(path))
{
return;
}
var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(5);
Exception? lastException = null;
while (DateTime.UtcNow < deadline)
{
try
{
if (!Directory.Exists(path))
{
return;
}
Directory.Delete(path, recursive: true);
return;
}
catch (IOException ex)
{
lastException = ex;
}
catch (UnauthorizedAccessException ex)
{
lastException = ex;
}
await Task.Delay(100);
}
if (lastException is not null)
{
throw lastException;
}
}
private static string NormalizeRelativePathSegment(string segment, string paramName)
{
if (string.IsNullOrWhiteSpace(segment))
{
throw new InvalidOperationException($"{paramName} must not be empty.");
}
var normalized = segment.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
if (Path.IsPathRooted(normalized) || normalized.Contains(Path.VolumeSeparatorChar))
{
throw new InvalidOperationException($"{paramName} must be a relative path segment: {segment}");
}
return normalized;
}
private sealed class TestSessionFsHandler(string sessionId, string rootDir) : ISessionFsHandler
{
public async Task<SessionFsReadFileResult> ReadFileAsync(SessionFsReadFileParams request, CancellationToken cancellationToken = default)
{
var content = await File.ReadAllTextAsync(ResolvePath(request.Path), cancellationToken);
return new SessionFsReadFileResult { Content = content };
}
public async Task WriteFileAsync(SessionFsWriteFileParams request, CancellationToken cancellationToken = default)
{
var fullPath = ResolvePath(request.Path);
Directory.CreateDirectory(Path.GetDirectoryName(fullPath)!);
await File.WriteAllTextAsync(fullPath, request.Content, cancellationToken);
}
public async Task AppendFileAsync(SessionFsAppendFileParams request, CancellationToken cancellationToken = default)
{
var fullPath = ResolvePath(request.Path);
Directory.CreateDirectory(Path.GetDirectoryName(fullPath)!);
await File.AppendAllTextAsync(fullPath, request.Content, cancellationToken);
}
public Task<SessionFsExistsResult> ExistsAsync(SessionFsExistsParams request, CancellationToken cancellationToken = default)
{
var fullPath = ResolvePath(request.Path);
return Task.FromResult(new SessionFsExistsResult
{
Exists = File.Exists(fullPath) || Directory.Exists(fullPath),
});
}
public Task<SessionFsStatResult> StatAsync(SessionFsStatParams request, CancellationToken cancellationToken = default)
{
var fullPath = ResolvePath(request.Path);
if (File.Exists(fullPath))
{
var info = new FileInfo(fullPath);
return Task.FromResult(new SessionFsStatResult
{
IsFile = true,
IsDirectory = false,
Size = info.Length,
Mtime = info.LastWriteTimeUtc.ToString("O"),
Birthtime = info.CreationTimeUtc.ToString("O"),
});
}
var dirInfo = new DirectoryInfo(fullPath);
if (!dirInfo.Exists)
{
throw new FileNotFoundException($"Path does not exist: {request.Path}");
}
return Task.FromResult(new SessionFsStatResult
{
IsFile = false,
IsDirectory = true,
Size = 0,
Mtime = dirInfo.LastWriteTimeUtc.ToString("O"),
Birthtime = dirInfo.CreationTimeUtc.ToString("O"),
});
}
public Task MkdirAsync(SessionFsMkdirParams request, CancellationToken cancellationToken = default)
{
Directory.CreateDirectory(ResolvePath(request.Path));
return Task.CompletedTask;
}
public Task<SessionFsReaddirResult> ReaddirAsync(SessionFsReaddirParams request, CancellationToken cancellationToken = default)
{
var entries = Directory
.EnumerateFileSystemEntries(ResolvePath(request.Path))
.Select(Path.GetFileName)
.Where(name => name is not null)
.Cast<string>()
.ToList();
return Task.FromResult(new SessionFsReaddirResult { Entries = entries });
}
public Task<SessionFsReaddirWithTypesResult> ReaddirWithTypesAsync(SessionFsReaddirWithTypesParams request, CancellationToken cancellationToken = default)
{
var entries = Directory
.EnumerateFileSystemEntries(ResolvePath(request.Path))
.Select(path => new Entry
{
Name = Path.GetFileName(path),
Type = Directory.Exists(path) ? EntryType.Directory : EntryType.File,
})
.ToList();
return Task.FromResult(new SessionFsReaddirWithTypesResult { Entries = entries });
}
public Task RmAsync(SessionFsRmParams request, CancellationToken cancellationToken = default)
{
var fullPath = ResolvePath(request.Path);
if (File.Exists(fullPath))
{
File.Delete(fullPath);
return Task.CompletedTask;
}
if (Directory.Exists(fullPath))
{
Directory.Delete(fullPath, request.Recursive ?? false);
return Task.CompletedTask;
}
if (request.Force == true)
{
return Task.CompletedTask;
}
throw new FileNotFoundException($"Path does not exist: {request.Path}");
}
public Task RenameAsync(SessionFsRenameParams request, CancellationToken cancellationToken = default)
{
var src = ResolvePath(request.Src);
var dest = ResolvePath(request.Dest);
Directory.CreateDirectory(Path.GetDirectoryName(dest)!);
if (Directory.Exists(src))
{
Directory.Move(src, dest);
}
else
{
File.Move(src, dest, overwrite: true);
}
return Task.CompletedTask;
}
private string ResolvePath(string sessionPath)
{
var normalizedSessionId = NormalizeRelativePathSegment(sessionId, nameof(sessionId));
var sessionRoot = Path.GetFullPath(Path.Join(rootDir, normalizedSessionId));
var relativeSegments = sessionPath
.TrimStart('/', '\\')
.Split(['/', '\\'], StringSplitOptions.RemoveEmptyEntries)
.Select(segment => NormalizeRelativePathSegment(segment, nameof(sessionPath)))
.ToArray();
var fullPath = Path.GetFullPath(Path.Join([sessionRoot, .. relativeSegments]));
if (!fullPath.StartsWith(sessionRoot, StringComparison.Ordinal))
{
throw new InvalidOperationException($"Path escapes session root: {sessionPath}");
}
return fullPath;
}
}
}