| 1 | import { ModelConfig } from "@continuedev/config-yaml"; |
| 2 | import { BaseLlmApi } from "@continuedev/openai-adapters"; |
| 3 | import { convertToUnifiedHistory } from "core/util/messageConversion.js"; |
| 4 | import { describe, expect, it, vi } from "vitest"; |
| 5 | |
| 6 | import { compactChatHistory } from "./compaction.js"; |
| 7 | import { streamChatResponse } from "./stream/streamChatResponse.js"; |
| 8 | |
| 9 | // Mock the dependencies |
| 10 | vi.mock("./stream/streamChatResponse.js", () => ({ |
| 11 | streamChatResponse: vi.fn(), |
| 12 | })); |
| 13 | |
| 14 | vi.mock("./util/tokenizer.js", () => ({ |
| 15 | countChatHistoryTokens: vi.fn(), |
| 16 | getModelContextLimit: vi.fn(), |
| 17 | })); |
| 18 | |
| 19 | describe("compaction infinite loop prevention", () => { |
| 20 | const mockModel: ModelConfig = { |
| 21 | name: "test-model", |
| 22 | provider: "test", |
| 23 | model: "test-model", |
| 24 | defaultCompletionOptions: { |
| 25 | maxTokens: 1000, |
| 26 | contextLength: 4000, |
| 27 | }, |
| 28 | } as ModelConfig; |
| 29 | |
| 30 | const mockLlmApi = {} as BaseLlmApi; |
| 31 | |
| 32 | it("should not loop infinitely when pruning doesn't reduce history size", async () => { |
| 33 | const { countChatHistoryTokens, getModelContextLimit } = await import( |
| 34 | "./util/tokenizer.js" |
| 35 | ); |
| 36 | const mockStreamResponse = vi.mocked(streamChatResponse); |
| 37 | const mockCountTokens = vi.mocked(countChatHistoryTokens); |
| 38 | const mockGetContextLimit = vi.mocked(getModelContextLimit); |
| 39 | |
| 40 | // Setup mocks |
| 41 | mockGetContextLimit.mockReturnValue(4000); |
| 42 | mockCountTokens.mockReturnValue(5000); // Always too big |
| 43 | mockStreamResponse.mockImplementation( |
| 44 | async (history, model, api, controller, callbacks) => { |
| 45 | callbacks?.onContent?.("Summary"); |
| 46 | callbacks?.onContentComplete?.("Summary"); |
| 47 | return "Summary"; |
| 48 | }, |
| 49 | ); |
| 50 | |
| 51 | // History that can't be pruned further (only system message) |
| 52 | const history = convertToUnifiedHistory([ |
| 53 | { role: "system", content: "System message" }, |
| 54 | ]); |
| 55 | |
| 56 | // This should not hang - it should break out of the loop |
| 57 | const result = await compactChatHistory(history, mockModel, mockLlmApi, {}); |
| 58 | |
| 59 | // Should complete successfully even though token count is still too high |
| 60 | expect(result.compactedHistory).toBeDefined(); |
| 61 | expect(mockCountTokens).toHaveBeenCalled(); |
| 62 | }); |
| 63 | |
| 64 | it("should not loop infinitely with history ending in assistant message", async () => { |
| 65 | const { countChatHistoryTokens, getModelContextLimit } = await import( |
| 66 | "./util/tokenizer.js" |
| 67 | ); |
| 68 | const mockStreamResponse = vi.mocked(streamChatResponse); |
| 69 | const mockCountTokens = vi.mocked(countChatHistoryTokens); |
| 70 | const mockGetContextLimit = vi.mocked(getModelContextLimit); |
| 71 | |
| 72 | // Setup mocks |
| 73 | mockGetContextLimit.mockReturnValue(4000); |
| 74 | mockCountTokens.mockReturnValue(5000); // Always too big |
| 75 | mockStreamResponse.mockImplementation( |
| 76 | async (history, model, api, controller, callbacks) => { |
| 77 | callbacks?.onContent?.("Summary"); |
| 78 | callbacks?.onContentComplete?.("Summary"); |
| 79 | return "Summary"; |
| 80 | }, |
| 81 | ); |
| 82 | |
| 83 | // History that ends with assistant - pruning won't change it |
| 84 | const history = convertToUnifiedHistory([ |
| 85 | { role: "system", content: "System message" }, |
| 86 | { role: "user", content: "Hello" }, |
| 87 | { role: "assistant", content: "Hi there" }, |
| 88 | ]); |
| 89 | |
| 90 | // This should not hang |
| 91 | const result = await compactChatHistory(history, mockModel, mockLlmApi, {}); |
| 92 | |
| 93 | expect(result.compactedHistory).toBeDefined(); |
| 94 | }); |
| 95 | |
| 96 | it("should successfully prune when pruning actually reduces size", async () => { |
| 97 | const { countChatHistoryTokens, getModelContextLimit } = await import( |
| 98 | "./util/tokenizer.js" |
| 99 | ); |
| 100 | const mockStreamResponse = vi.mocked(streamChatResponse); |
| 101 | const mockCountTokens = vi.mocked(countChatHistoryTokens); |
| 102 | const mockGetContextLimit = vi.mocked(getModelContextLimit); |
| 103 | |
| 104 | // Setup mocks |
| 105 | mockGetContextLimit.mockReturnValue(4000); |
| 106 | |
| 107 | // Mock token counting to show reduction after pruning |
| 108 | let callCount = 0; |
| 109 | mockCountTokens.mockImplementation(() => { |
| 110 | callCount++; |
| 111 | if (callCount === 1) return 5000; // Initial too big |
| 112 | if (callCount === 2) return 3000; // After pruning, fits |
| 113 | return 2000; // Subsequent calls |
| 114 | }); |
| 115 | |
| 116 | mockStreamResponse.mockImplementation( |
| 117 | async (history, model, api, controller, callbacks) => { |
| 118 | callbacks?.onContent?.("Summary"); |
| 119 | callbacks?.onContentComplete?.("Summary"); |
| 120 | return "Summary"; |
| 121 | }, |
| 122 | ); |
| 123 | |
| 124 | // History that can be successfully pruned |
| 125 | const history = convertToUnifiedHistory([ |
| 126 | { role: "system", content: "System message" }, |
| 127 | { role: "user", content: "Hello" }, |
| 128 | { role: "assistant", content: "Hi there" }, |
| 129 | { role: "user", content: "Another question" }, |
| 130 | ]); |
| 131 | |
| 132 | const result = await compactChatHistory(history, mockModel, mockLlmApi, {}); |
| 133 | |
| 134 | expect(result.compactedHistory).toBeDefined(); |
| 135 | // The function will call countTokens multiple times during the process |
| 136 | expect(mockCountTokens).toHaveBeenCalled(); |
| 137 | }); |
| 138 | }); |