Repository navigation
Expand file tree
/
Copy pathCopilotRequestHandlerProtocolTests.cs
More file actions
651 lines (558 loc) · 24.4 KB
/
Copy pathCopilotRequestHandlerProtocolTests.cs
File metadata and controls
651 lines (558 loc) · 24.4 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
#if NET8_0_OR_GREATER
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.Json;
using System.Threading.Channels;
using Xunit;
namespace GitHub.Copilot.Test.Unit;
public sealed class CopilotRequestHandlerProtocolTests
{
private static readonly TimeSpan s_timeout = TimeSpan.FromSeconds(5);
[Fact]
public async Task HttpResponse_ReadsAheadAndCoalescesUnderOneOutstandingDataRpc()
{
var source = new ControlledResponseStream();
await using var peer = await ProtocolPeer.StartAsync(new StreamRequestHandler(source));
await source.PushAsync("first"u8.ToArray());
await peer.BeginRequestAsync();
var head = await peer.NextMethodAsync();
Assert.Equal("llmInference.httpResponseStart", Method(head));
await peer.AcknowledgeAsync(head);
var first = await peer.NextMethodAsync();
Assert.Equal("first"u8.ToArray(), Data(first));
var expectedReadAhead = new byte[32 * 1024];
for (var i = 0; i < 32; i++)
{
var fragment = Enumerable.Repeat((byte)i, 1024).ToArray();
fragment.CopyTo(expectedReadAhead, i * fragment.Length);
await source.PushAsync(fragment);
}
await source.WaitForBytesAsync(5 + (32 * 1024));
await source.PushAsync("last"u8.ToArray());
await Task.Delay(TimeSpan.FromMilliseconds(100));
Assert.Equal(5 + (32 * 1024), source.BytesRead);
await peer.AssertNoMethodAsync();
await peer.AcknowledgeAsync(first);
var combined = await peer.NextMethodAsync();
Assert.Equal(expectedReadAhead, Data(combined));
await source.CompleteAsync();
await peer.AcknowledgeAsync(combined);
var last = await peer.NextMethodAsync();
Assert.Equal("last"u8.ToArray(), Data(last));
await peer.AcknowledgeAsync(last);
var end = await peer.NextMethodAsync();
Assert.True(end.GetProperty("params").GetProperty("end").GetBoolean());
Assert.False(end.GetProperty("params").TryGetProperty("error", out _));
await peer.AcknowledgeAsync(end);
await source.Disposed.WaitAsync(s_timeout);
}
[Fact]
public async Task HttpResponse_UpstreamErrorFollowsBufferedBytesAndOutstandingAck()
{
var source = new ControlledResponseStream();
await using var peer = await ProtocolPeer.StartAsync(new StreamRequestHandler(source));
await source.PushAsync("first"u8.ToArray());
await peer.BeginRequestAsync();
await peer.AcknowledgeAsync(await peer.NextMethodAsync());
var first = await peer.NextMethodAsync();
await source.PushAsync("partial"u8.ToArray());
await source.FailAsync(new IOException("upstream failed"));
await source.WaitForReadsAsync(3);
await peer.AssertNoMethodAsync();
await peer.AcknowledgeAsync(first);
var partial = await peer.NextMethodAsync();
Assert.Equal("partial"u8.ToArray(), Data(partial));
await peer.AssertNoMethodAsync();
await peer.AcknowledgeAsync(partial);
var error = await peer.NextMethodAsync();
Assert.True(error.GetProperty("params").GetProperty("end").GetBoolean());
Assert.Equal("upstream failed", error.GetProperty("params").GetProperty("error").GetProperty("message").GetString());
await peer.AcknowledgeAsync(error);
await source.Disposed.WaitAsync(s_timeout);
}
[Fact]
public async Task HttpResponse_RuntimeCancellationDisposesSourceWithoutWaitingForDataAck()
{
var source = new ControlledResponseStream();
await using var peer = await ProtocolPeer.StartAsync(new StreamRequestHandler(source));
await source.PushAsync("first"u8.ToArray());
await peer.BeginRequestAsync();
await peer.AcknowledgeAsync(await peer.NextMethodAsync());
var first = await peer.NextMethodAsync();
Assert.Equal("first"u8.ToArray(), Data(first));
await peer.CancelRequestAsync();
var error = await peer.NextMethodAsync();
Assert.True(error.GetProperty("params").GetProperty("end").GetBoolean());
Assert.Equal("cancelled", error.GetProperty("params").GetProperty("error").GetProperty("code").GetString());
await source.Disposed.WaitAsync(s_timeout);
await peer.AcknowledgeAsync(error);
}
[Fact]
public async Task HttpResponse_RuntimeCancellationWhileSourceIsIdleReportsCancellation()
{
var source = new ControlledResponseStream();
await using var peer = await ProtocolPeer.StartAsync(new StreamRequestHandler(source));
await peer.BeginRequestAsync();
await peer.AcknowledgeAsync(await peer.NextMethodAsync());
await source.ReadStarted.WaitAsync(s_timeout);
await peer.CancelRequestAsync();
var error = await peer.NextMethodAsync();
Assert.True(error.GetProperty("params").GetProperty("end").GetBoolean());
Assert.Equal("cancelled", error.GetProperty("params").GetProperty("error").GetProperty("code").GetString());
await source.Disposed.WaitAsync(s_timeout);
await peer.AcknowledgeAsync(error);
}
[Fact]
public async Task HttpResponse_RejectedDataRpcDisposesSourceAndReportsError()
{
var source = new ControlledResponseStream();
await using var peer = await ProtocolPeer.StartAsync(new StreamRequestHandler(source));
await source.PushAsync("first"u8.ToArray());
await peer.BeginRequestAsync();
await peer.AcknowledgeAsync(await peer.NextMethodAsync());
var first = await peer.NextMethodAsync();
await peer.RejectAsync(first, "write rejected");
await source.Disposed.WaitAsync(s_timeout);
var error = await peer.NextMethodAsync();
Assert.Contains(
"write rejected",
error.GetProperty("params").GetProperty("error").GetProperty("message").GetString(),
StringComparison.Ordinal);
await peer.AcknowledgeAsync(error);
}
[Fact]
public async Task HttpResponse_ConnectionLossDisposesSourceWithOutstandingDataRpc()
{
var source = new ControlledResponseStream();
await using var peer = await ProtocolPeer.StartAsync(new StreamRequestHandler(source));
await source.PushAsync("first"u8.ToArray());
await peer.BeginRequestAsync();
await peer.AcknowledgeAsync(await peer.NextMethodAsync());
Assert.Equal("first"u8.ToArray(), Data(await peer.NextMethodAsync()));
peer.CloseConnection();
await source.Disposed.WaitAsync(s_timeout);
}
[Fact]
public async Task HttpResponse_ConnectionLossDisposesIdleSource()
{
var source = new ControlledResponseStream();
await using var peer = await ProtocolPeer.StartAsync(new StreamRequestHandler(source));
await peer.BeginRequestAsync();
await peer.AcknowledgeAsync(await peer.NextMethodAsync());
await source.ReadStarted.WaitAsync(s_timeout);
peer.CloseConnection();
await source.Disposed.WaitAsync(s_timeout);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task HttpResponse_ClientShutdownDisposesIdleSource(bool force)
{
var source = new ControlledResponseStream();
await using var peer = await ProtocolPeer.StartAsync(new StreamRequestHandler(source));
await peer.BeginRequestAsync();
await peer.AcknowledgeAsync(await peer.NextMethodAsync());
await source.ReadStarted.WaitAsync(s_timeout);
await peer.StopClientAsync(force);
await source.Disposed.WaitAsync(s_timeout);
}
private static string? Method(JsonElement message) =>
message.GetProperty("method").GetString();
private static byte[] Data(JsonElement message)
{
Assert.Equal("llmInference.httpResponseChunk", Method(message));
var parameters = message.GetProperty("params");
Assert.False(parameters.GetProperty("end").GetBoolean());
Assert.True(parameters.GetProperty("binary").GetBoolean());
return Convert.FromBase64String(parameters.GetProperty("data").GetString()!);
}
private sealed class StreamRequestHandler(Stream source) : CopilotRequestHandler
{
protected override Task<HttpResponseMessage> SendRequestAsync(HttpRequestMessage request, CopilotRequestContext ctx) =>
Task.FromResult(CreateResponse(source));
private static HttpResponseMessage CreateResponse(Stream source) =>
new(HttpStatusCode.OK)
{
Content = new StreamContent(source),
};
}
private sealed class ControlledResponseStream : Stream
{
private readonly Channel<ReadItem> _items = Channel.CreateBounded<ReadItem>(
new BoundedChannelOptions(1)
{
SingleReader = true,
SingleWriter = true,
FullMode = BoundedChannelFullMode.Wait,
});
private readonly TaskCompletionSource _disposed = new(TaskCreationOptions.RunContinuationsAsynchronously);
private readonly TaskCompletionSource _readStarted = new(TaskCreationOptions.RunContinuationsAsynchronously);
private int _readCount;
private int _bytesRead;
private int _isDisposed;
private byte[]? _pendingData;
private int _pendingOffset;
internal int ReadCount => Volatile.Read(ref _readCount);
internal int BytesRead => Volatile.Read(ref _bytesRead);
internal Task Disposed => _disposed.Task;
internal Task ReadStarted => _readStarted.Task;
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => throw new NotSupportedException();
public override long Position
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
internal ValueTask PushAsync(byte[] data) =>
_items.Writer.WriteAsync(new ReadItem(data, Error: null, End: false));
internal ValueTask FailAsync(Exception error) =>
_items.Writer.WriteAsync(new ReadItem(Data: null, Error: error, End: false));
internal ValueTask CompleteAsync() =>
_items.Writer.WriteAsync(new ReadItem(Data: null, Error: null, End: true));
internal async Task WaitForReadsAsync(int expected)
{
using var cts = new CancellationTokenSource(s_timeout);
while (ReadCount < expected)
{
await Task.Delay(TimeSpan.FromMilliseconds(10), cts.Token);
}
}
internal async Task WaitForBytesAsync(int expected)
{
using var cts = new CancellationTokenSource(s_timeout);
while (BytesRead < expected)
{
await Task.Delay(TimeSpan.FromMilliseconds(10), cts.Token);
}
}
public override int Read(byte[] buffer, int offset, int count) =>
throw new NotSupportedException();
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) =>
ReadCoreAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask();
public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) =>
ReadCoreAsync(buffer, cancellationToken);
public override void Flush()
{
}
public override long Seek(long offset, SeekOrigin origin) =>
throw new NotSupportedException();
public override void SetLength(long value) =>
throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) =>
throw new NotSupportedException();
protected override void Dispose(bool disposing)
{
if (disposing && Interlocked.Exchange(ref _isDisposed, 1) == 0)
{
_items.Writer.TryComplete();
_disposed.TrySetResult();
}
base.Dispose(disposing);
}
private async ValueTask<int> ReadCoreAsync(Memory<byte> buffer, CancellationToken cancellationToken)
{
if (_pendingData is not null)
{
return CopyPendingData(buffer);
}
_readStarted.TrySetResult();
var item = await _items.Reader.ReadAsync(cancellationToken);
Interlocked.Increment(ref _readCount);
if (item.Error is not null)
{
throw item.Error;
}
if (item.End)
{
return 0;
}
Assert.NotNull(item.Data);
_pendingData = item.Data;
_pendingOffset = 0;
return CopyPendingData(buffer);
}
private int CopyPendingData(Memory<byte> buffer)
{
var available = _pendingData!.Length - _pendingOffset;
var count = Math.Min(available, buffer.Length);
_pendingData.AsMemory(_pendingOffset, count).CopyTo(buffer);
_pendingOffset += count;
if (_pendingOffset == _pendingData.Length)
{
_pendingData = null;
_pendingOffset = 0;
}
Interlocked.Add(ref _bytesRead, count);
return count;
}
private sealed record ReadItem(byte[]? Data, Exception? Error, bool End);
}
private sealed class ProtocolPeer : IAsyncDisposable
{
private readonly CopilotClient _client;
private readonly TcpClient _tcpClient;
private readonly NetworkStream _stream;
private readonly CancellationTokenSource _disposeCts = new();
private readonly Channel<JsonElement> _messages = Channel.CreateUnbounded<JsonElement>();
private readonly Task _readLoop;
private int _nextRequestId = 100_000;
private ProtocolPeer(CopilotClient client, TcpClient tcpClient)
{
_client = client;
_tcpClient = tcpClient;
_stream = tcpClient.GetStream();
_readLoop = ReadLoopAsync();
}
internal static async Task<ProtocolPeer> StartAsync(CopilotRequestHandler handler)
{
using var listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
var endpoint = (IPEndPoint)listener.LocalEndpoint;
var client = new CopilotClient(new CopilotClientOptions
{
Connection = RuntimeConnection.ForUri($"http://127.0.0.1:{endpoint.Port}"),
RequestHandler = handler,
});
var startTask = client.StartAsync();
var tcpClient = await listener.AcceptTcpClientAsync().WaitAsync(s_timeout);
var stream = tcpClient.GetStream();
var connect = await ReadFrameAsync(stream, CancellationToken.None).WaitAsync(s_timeout);
Assert.Equal("connect", Method(connect));
await WriteResultAsync(
stream,
connect,
writer =>
{
writer.WriteStartObject();
writer.WriteBoolean("ok", true);
writer.WriteNumber("protocolVersion", 3);
writer.WriteString("version", "test");
writer.WriteEndObject();
},
CancellationToken.None);
var setProvider = await ReadFrameAsync(stream, CancellationToken.None).WaitAsync(s_timeout);
Assert.Equal("llmInference.setProvider", Method(setProvider));
await WriteResultAsync(
stream,
setProvider,
writer =>
{
writer.WriteStartObject();
writer.WriteBoolean("success", true);
writer.WriteEndObject();
},
CancellationToken.None);
await startTask.WaitAsync(s_timeout);
return new ProtocolPeer(client, tcpClient);
}
internal async Task BeginRequestAsync()
{
await WriteRequestAsync(
"llmInference.httpRequestStart",
writer =>
{
writer.WriteString("requestId", "test");
writer.WriteString("method", "GET");
writer.WriteString("url", "http://unused.test");
writer.WriteStartObject("headers");
writer.WriteEndObject();
});
await WriteRequestAsync(
"llmInference.httpRequestChunk",
writer =>
{
writer.WriteString("requestId", "test");
writer.WriteString("data", string.Empty);
writer.WriteBoolean("end", true);
});
}
internal Task CancelRequestAsync() =>
WriteRequestAsync(
"llmInference.httpRequestChunk",
writer =>
{
writer.WriteString("requestId", "test");
writer.WriteString("data", string.Empty);
writer.WriteBoolean("cancel", true);
});
internal async Task<JsonElement> NextMethodAsync(CancellationToken cancellationToken = default)
{
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
timeout.CancelAfter(s_timeout);
while (true)
{
var message = await _messages.Reader.ReadAsync(timeout.Token);
if (message.TryGetProperty("method", out _))
{
return message;
}
}
}
internal async Task AssertNoMethodAsync()
{
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100));
await Assert.ThrowsAnyAsync<OperationCanceledException>(
() => NextMethodAsync(cts.Token));
}
internal Task AcknowledgeAsync(JsonElement request) =>
WriteResultAsync(
_stream,
request,
writer =>
{
writer.WriteStartObject();
writer.WriteBoolean("accepted", true);
writer.WriteEndObject();
},
_disposeCts.Token);
internal Task RejectAsync(JsonElement request, string message) =>
WriteFrameAsync(
_stream,
writer =>
{
writer.WriteStartObject();
writer.WriteString("jsonrpc", "2.0");
writer.WritePropertyName("id");
request.GetProperty("id").WriteTo(writer);
writer.WriteStartObject("error");
writer.WriteNumber("code", -32603);
writer.WriteString("message", message);
writer.WriteEndObject();
writer.WriteEndObject();
},
_disposeCts.Token);
internal void CloseConnection() => _tcpClient.Dispose();
internal Task StopClientAsync(bool force) =>
force ? _client.ForceStopAsync() : _client.StopAsync();
public async ValueTask DisposeAsync()
{
_disposeCts.Cancel();
_tcpClient.Dispose();
try
{
await _readLoop;
}
catch (Exception ex) when (ex is OperationCanceledException or IOException or ObjectDisposedException)
{
// These exceptions are expected when the protocol peer is torn down.
}
await _client.ForceStopAsync();
await _client.DisposeAsync();
_disposeCts.Dispose();
}
private async Task WriteRequestAsync(string method, Action<Utf8JsonWriter> writeParams)
{
var id = Interlocked.Increment(ref _nextRequestId);
await WriteFrameAsync(
_stream,
writer =>
{
writer.WriteStartObject();
writer.WriteString("jsonrpc", "2.0");
writer.WriteNumber("id", id);
writer.WriteString("method", method);
writer.WriteStartObject("params");
writeParams(writer);
writer.WriteEndObject();
writer.WriteEndObject();
},
_disposeCts.Token);
}
private async Task ReadLoopAsync()
{
try
{
while (!_disposeCts.IsCancellationRequested)
{
await _messages.Writer.WriteAsync(
await ReadFrameAsync(_stream, _disposeCts.Token),
_disposeCts.Token);
}
}
catch (Exception ex) when (ex is OperationCanceledException or IOException or ObjectDisposedException)
{
// Connection closure is the expected way to stop the read loop.
}
finally
{
_messages.Writer.TryComplete();
}
}
private static async Task<JsonElement> ReadFrameAsync(Stream stream, CancellationToken cancellationToken)
{
using var header = new MemoryStream();
var current = new byte[1];
while (true)
{
var read = await stream.ReadAsync(current, cancellationToken);
if (read == 0)
{
throw new EndOfStreamException();
}
header.WriteByte(current[0]);
if (header.Length >= 4)
{
var bytes = header.GetBuffer();
var length = (int)header.Length;
if (bytes[length - 4] == '\r'
&& bytes[length - 3] == '\n'
&& bytes[length - 2] == '\r'
&& bytes[length - 1] == '\n')
{
break;
}
}
}
var headerText = Encoding.ASCII.GetString(header.GetBuffer(), 0, (int)header.Length);
var contentLengthLine = headerText
.Split("\r\n", StringSplitOptions.RemoveEmptyEntries)
.Single(line => line.StartsWith("Content-Length:", StringComparison.OrdinalIgnoreCase));
var contentLength = int.Parse(contentLengthLine["Content-Length:".Length..].Trim());
var body = new byte[contentLength];
await stream.ReadExactlyAsync(body, cancellationToken);
using var document = JsonDocument.Parse(body);
return document.RootElement.Clone();
}
private static Task WriteResultAsync(
Stream stream,
JsonElement request,
Action<Utf8JsonWriter> writeResult,
CancellationToken cancellationToken) =>
WriteFrameAsync(
stream,
writer =>
{
writer.WriteStartObject();
writer.WriteString("jsonrpc", "2.0");
writer.WritePropertyName("id");
request.GetProperty("id").WriteTo(writer);
writer.WritePropertyName("result");
writeResult(writer);
writer.WriteEndObject();
},
cancellationToken);
private static async Task WriteFrameAsync(
Stream stream,
Action<Utf8JsonWriter> writeMessage,
CancellationToken cancellationToken)
{
using var bodyStream = new MemoryStream();
using (var writer = new Utf8JsonWriter(bodyStream))
{
writeMessage(writer);
}
var body = bodyStream.ToArray();
var header = Encoding.ASCII.GetBytes($"Content-Length: {body.Length}\r\n\r\n");
await stream.WriteAsync(header, cancellationToken);
await stream.WriteAsync(body, cancellationToken);
await stream.FlushAsync(cancellationToken);
}
}
}
#endif