|
6 | 6 | import assert from 'assert'; |
7 | 7 | import * as sinon from 'sinon'; |
8 | 8 | import { afterEach, beforeEach, suite, test } from 'vitest'; |
9 | | -import type { ChatVulnerability } from 'vscode'; |
| 9 | +import type { ChatToolInvocationStreamData, ChatVulnerability } from 'vscode'; |
10 | 10 | import { IResponsePart } from '../../../platform/chat/common/chatMLFetcher'; |
11 | 11 | import { IResponseDelta } from '../../../platform/networking/common/fetch'; |
12 | 12 | import { createPlatformServices } from '../../../platform/test/node/services'; |
| 13 | +import { ChatResponseStreamImpl } from '../../../util/common/chatResponseStreamImpl'; |
13 | 14 | import { SpyChatResponseStream } from '../../../util/common/test/mockChatResponseStream'; |
14 | 15 | import { AsyncIterableSource } from '../../../util/vs/base/common/async'; |
15 | | -import { CancellationToken } from '../../../util/vs/base/common/cancellation'; |
| 16 | +import { CancellationToken, CancellationTokenSource } from '../../../util/vs/base/common/cancellation'; |
16 | 17 | import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation'; |
17 | 18 | import { ChatResponseMarkdownPart, ChatResponseMarkdownWithVulnerabilitiesPart } from '../../../vscodeTypes'; |
18 | 19 | import { PseudoStopStartResponseProcessor } from '../../prompt/node/pseudoStartStopConversationCallback'; |
@@ -169,3 +170,159 @@ suite('Post Report Conversation Callback', () => { |
169 | 170 |
|
170 | 171 | afterEach(() => sinon.restore()); |
171 | 172 | }); |
| 173 | + |
| 174 | +suite('Tool stream throttling', () => { |
| 175 | + let clock: sinon.SinonFakeTimers; |
| 176 | + let updateCalls: { toolCallId: string; streamData: ChatToolInvocationStreamData }[]; |
| 177 | + let stream: ChatResponseStreamImpl; |
| 178 | + |
| 179 | + beforeEach(() => { |
| 180 | + clock = sinon.useFakeTimers({ now: 1000, toFake: ['Date'] }); |
| 181 | + updateCalls = []; |
| 182 | + stream = new ChatResponseStreamImpl( |
| 183 | + () => { }, |
| 184 | + () => { }, |
| 185 | + undefined, |
| 186 | + undefined, |
| 187 | + (toolCallId, streamData) => updateCalls.push({ toolCallId, streamData }), |
| 188 | + ); |
| 189 | + }); |
| 190 | + |
| 191 | + afterEach(() => { |
| 192 | + clock.restore(); |
| 193 | + sinon.restore(); |
| 194 | + }); |
| 195 | + |
| 196 | + test('first update is emitted immediately', async () => { |
| 197 | + const responseSource = new AsyncIterableSource<IResponsePart>(); |
| 198 | + const processor = new PseudoStopStartResponseProcessor([], undefined); |
| 199 | + |
| 200 | + responseSource.emitOne({ delta: { text: '', copilotToolCallStreamUpdates: [{ id: 'tool1', name: 'myTool', arguments: '{"a":1}' }] } }); |
| 201 | + responseSource.resolve(); |
| 202 | + |
| 203 | + await processor.doProcessResponse(responseSource.asyncIterable, stream, CancellationToken.None); |
| 204 | + |
| 205 | + assert.strictEqual(updateCalls.length, 1); |
| 206 | + assert.strictEqual(updateCalls[0].toolCallId, 'tool1'); |
| 207 | + }); |
| 208 | + |
| 209 | + test('rapid updates within throttle window are throttled', async () => { |
| 210 | + const responseSource = new AsyncIterableSource<IResponsePart>(); |
| 211 | + const processor = new PseudoStopStartResponseProcessor([], undefined); |
| 212 | + |
| 213 | + // First update goes through immediately |
| 214 | + responseSource.emitOne({ delta: { text: '', copilotToolCallStreamUpdates: [{ id: 'tool1', name: 'myTool', arguments: '{"a":1}' }] } }); |
| 215 | + // These arrive within the 100ms throttle window — should be buffered |
| 216 | + responseSource.emitOne({ delta: { text: '', copilotToolCallStreamUpdates: [{ id: 'tool1', name: 'myTool', arguments: '{"a":2}' }] } }); |
| 217 | + responseSource.emitOne({ delta: { text: '', copilotToolCallStreamUpdates: [{ id: 'tool1', name: 'myTool', arguments: '{"a":3}' }] } }); |
| 218 | + responseSource.resolve(); |
| 219 | + |
| 220 | + await processor.doProcessResponse(responseSource.asyncIterable, stream, CancellationToken.None); |
| 221 | + |
| 222 | + // 1 immediate + 1 flush of the last buffered update = 2 total |
| 223 | + assert.strictEqual(updateCalls.length, 2); |
| 224 | + assert.strictEqual(updateCalls[0].toolCallId, 'tool1'); |
| 225 | + assert.deepStrictEqual(updateCalls[1].streamData.partialInput, { a: 3 }); |
| 226 | + }); |
| 227 | + |
| 228 | + test('update after throttle window elapses is emitted immediately', async () => { |
| 229 | + const responseSource = new AsyncIterableSource<IResponsePart>(); |
| 230 | + const processor = new PseudoStopStartResponseProcessor([], undefined); |
| 231 | + |
| 232 | + responseSource.emitOne({ delta: { text: '', copilotToolCallStreamUpdates: [{ id: 'tool1', name: 'myTool', arguments: '{"a":1}' }] } }); |
| 233 | + clock.tick(100); |
| 234 | + responseSource.emitOne({ delta: { text: '', copilotToolCallStreamUpdates: [{ id: 'tool1', name: 'myTool', arguments: '{"a":2}' }] } }); |
| 235 | + responseSource.resolve(); |
| 236 | + |
| 237 | + await processor.doProcessResponse(responseSource.asyncIterable, stream, CancellationToken.None); |
| 238 | + |
| 239 | + // Both emitted immediately (no pending flush needed) |
| 240 | + assert.strictEqual(updateCalls.length, 2); |
| 241 | + assert.deepStrictEqual(updateCalls[0].streamData.partialInput, { a: 1 }); |
| 242 | + assert.deepStrictEqual(updateCalls[1].streamData.partialInput, { a: 2 }); |
| 243 | + }); |
| 244 | + |
| 245 | + test('different tool IDs are throttled independently', async () => { |
| 246 | + const responseSource = new AsyncIterableSource<IResponsePart>(); |
| 247 | + const processor = new PseudoStopStartResponseProcessor([], undefined); |
| 248 | + |
| 249 | + responseSource.emitOne({ delta: { text: '', copilotToolCallStreamUpdates: [{ id: 'tool1', name: 'myTool', arguments: '{"a":1}' }] } }); |
| 250 | + responseSource.emitOne({ delta: { text: '', copilotToolCallStreamUpdates: [{ id: 'tool2', name: 'myTool', arguments: '{"b":1}' }] } }); |
| 251 | + // These are within the throttle window for their respective tools |
| 252 | + responseSource.emitOne({ delta: { text: '', copilotToolCallStreamUpdates: [{ id: 'tool1', name: 'myTool', arguments: '{"a":2}' }] } }); |
| 253 | + responseSource.emitOne({ delta: { text: '', copilotToolCallStreamUpdates: [{ id: 'tool2', name: 'myTool', arguments: '{"b":2}' }] } }); |
| 254 | + responseSource.resolve(); |
| 255 | + |
| 256 | + await processor.doProcessResponse(responseSource.asyncIterable, stream, CancellationToken.None); |
| 257 | + |
| 258 | + // 2 immediate (one per tool) + 2 flushed (one per tool) = 4 |
| 259 | + assert.strictEqual(updateCalls.length, 4); |
| 260 | + }); |
| 261 | + |
| 262 | + test('pending updates are not flushed on cancellation', async () => { |
| 263 | + const cts = new CancellationTokenSource(); |
| 264 | + const responseSource = new AsyncIterableSource<IResponsePart>(); |
| 265 | + const processor = new PseudoStopStartResponseProcessor([], undefined); |
| 266 | + |
| 267 | + // Start processing, then emit items so the for-await loop consumes them |
| 268 | + const promise = processor.doProcessResponse(responseSource.asyncIterable, stream, cts.token); |
| 269 | + |
| 270 | + responseSource.emitOne({ delta: { text: '', copilotToolCallStreamUpdates: [{ id: 'tool1', name: 'myTool', arguments: '{"a":1}' }] } }); |
| 271 | + await new Promise(r => setTimeout(r, 0)); |
| 272 | + responseSource.emitOne({ delta: { text: '', copilotToolCallStreamUpdates: [{ id: 'tool1', name: 'myTool', arguments: '{"a":2}' }] } }); |
| 273 | + await new Promise(r => setTimeout(r, 0)); |
| 274 | + |
| 275 | + // Cancel after items are processed but before stream ends |
| 276 | + cts.cancel(); |
| 277 | + responseSource.resolve(); |
| 278 | + |
| 279 | + await promise; |
| 280 | + |
| 281 | + // Only the first immediate update — buffered update should NOT be flushed |
| 282 | + assert.strictEqual(updateCalls.length, 1); |
| 283 | + assert.strictEqual(updateCalls[0].toolCallId, 'tool1'); |
| 284 | + }); |
| 285 | + |
| 286 | + test('retry clears pending throttle state', async () => { |
| 287 | + const responseSource = new AsyncIterableSource<IResponsePart>(); |
| 288 | + const clearCalls: number[] = []; |
| 289 | + const clearStream = new ChatResponseStreamImpl( |
| 290 | + () => { }, |
| 291 | + () => clearCalls.push(1), |
| 292 | + undefined, |
| 293 | + undefined, |
| 294 | + (toolCallId, streamData) => updateCalls.push({ toolCallId, streamData }), |
| 295 | + ); |
| 296 | + const processor = new PseudoStopStartResponseProcessor([], undefined); |
| 297 | + |
| 298 | + // Buffer a pending update |
| 299 | + responseSource.emitOne({ delta: { text: '', copilotToolCallStreamUpdates: [{ id: 'tool1', name: 'myTool', arguments: '{"a":1}' }] } }); |
| 300 | + responseSource.emitOne({ delta: { text: '', copilotToolCallStreamUpdates: [{ id: 'tool1', name: 'myTool', arguments: '{"a":2}' }] } }); |
| 301 | + // Retry clears everything |
| 302 | + responseSource.emitOne({ delta: { text: '', retryReason: 'network_error' } }); |
| 303 | + // New update after retry should go through immediately |
| 304 | + clock.tick(100); |
| 305 | + responseSource.emitOne({ delta: { text: '', copilotToolCallStreamUpdates: [{ id: 'tool1', name: 'myTool', arguments: '{"a":3}' }] } }); |
| 306 | + responseSource.resolve(); |
| 307 | + |
| 308 | + await processor.doProcessResponse(responseSource.asyncIterable, clearStream, CancellationToken.None); |
| 309 | + |
| 310 | + // 1 immediate before retry + 1 immediate after retry = 2 |
| 311 | + // The buffered {"a":2} should have been cleared by retry, not flushed |
| 312 | + assert.strictEqual(updateCalls.length, 2); |
| 313 | + assert.deepStrictEqual(updateCalls[0].streamData.partialInput, { a: 1 }); |
| 314 | + assert.deepStrictEqual(updateCalls[1].streamData.partialInput, { a: 3 }); |
| 315 | + }); |
| 316 | + |
| 317 | + test('updates without name are skipped', async () => { |
| 318 | + const responseSource = new AsyncIterableSource<IResponsePart>(); |
| 319 | + const processor = new PseudoStopStartResponseProcessor([], undefined); |
| 320 | + |
| 321 | + responseSource.emitOne({ delta: { text: '', copilotToolCallStreamUpdates: [{ id: 'tool1', name: undefined as any, arguments: '{"a":1}' }] } }); |
| 322 | + responseSource.resolve(); |
| 323 | + |
| 324 | + await processor.doProcessResponse(responseSource.asyncIterable, stream, CancellationToken.None); |
| 325 | + |
| 326 | + assert.strictEqual(updateCalls.length, 0); |
| 327 | + }); |
| 328 | +}); |
0 commit comments