File size: 9,309 Bytes
7a1ad33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { ContextCompressionService } from './contextCompressionService.js';
import type { Config } from '../config/config.js';
import type { Content } from '@google/genai';
import * as fsSync from 'node:fs';

vi.mock('node:fs/promises', () => ({
  readFile: vi.fn(),
  writeFile: vi.fn(),
}));

vi.mock('node:fs', async (importOriginal) => {
  const actual = await importOriginal<typeof import('node:fs')>();
  return {
    ...actual,
    existsSync: vi.fn(),
  };
});

describe('ContextCompressionService', () => {
  let mockConfig: Partial<Config>;
  let service: ContextCompressionService;
  const generateContentMock: ReturnType<typeof vi.fn> = vi.fn();
  const generateJsonMock: ReturnType<typeof vi.fn> = vi.fn();

  beforeEach(() => {
    mockConfig = {
      storage: {
        getProjectTempDir: vi.fn().mockReturnValue('/mock/temp/dir'),
      },
      isContextManagementEnabled: vi.fn().mockResolvedValue(true),
      getBaseLlmClient: vi.fn().mockReturnValue({
        generateContent: generateContentMock,
        generateJson: generateJsonMock,
      }),
    } as unknown as Config;

    vi.mocked(fsSync.existsSync).mockReturnValue(false);

    service = new ContextCompressionService(mockConfig as Config);
  });

  afterEach(() => {
    vi.clearAllMocks();
  });

  describe('compressHistory', () => {
    it('bypasses compression if feature flag is false', async () => {
      mockConfig.isContextManagementEnabled = vi.fn().mockResolvedValue(false);
      const history: Content[] = [{ role: 'user', parts: [{ text: 'hello' }] }];

      const res = await service.compressHistory(history, 'test prompt');
      expect(res).toStrictEqual(history);
    });

    it('protects files that were read within the RECENT_TURNS_PROTECTED window', async () => {
      const history: Content[] = [
        // Turn 0 & 1 (Old)
        {
          role: 'model',
          parts: [
            {
              functionCall: {
                name: 'read_file',
                args: { filepath: 'src/app.ts' },
              },
            },
          ],
        },
        {
          role: 'user',
          parts: [
            {
              functionResponse: {
                name: 'read_file',
                response: {
                  output: '--- src/app.ts ---\nLine 1\nLine 2\nLine 3',
                },
              },
            },
          ],
        },

        // Padding (Turns 2 & 3)
        { role: 'model', parts: [{ text: 'res 1' }] },
        { role: 'user', parts: [{ text: 'res 2' }] },

        // Padding (Turns 4 & 5)
        { role: 'model', parts: [{ text: 'res 3' }] },
        { role: 'user', parts: [{ text: 'res 4' }] },

        // Recent Turn (Turn 6 & 7, inside window, cutoff is Math.max(0, 8 - 4) = 4)
        // Here the model explicitly reads the file again
        {
          role: 'model',
          parts: [
            {
              functionCall: {
                name: 'read_file',
                args: { filepath: 'src/app.ts' },
              },
            },
          ],
        },
        {
          role: 'user',
          parts: [
            {
              functionResponse: {
                name: 'read_file',
                response: {
                  output: '--- src/app.ts ---\nLine 1\nLine 2\nLine 3',
                },
              },
            },
          ],
        },
      ];

      const res = await service.compressHistory(history, 'test prompt');

      // Because src/app.ts was re-read recently (index 6 is >= 4), the OLD response at index 1 is PROTECTED.
      // It should NOT be compressed.
      const compressedOutput =
        res[1].parts![0].functionResponse!.response!['output'];
      expect(compressedOutput).toBe(
        '--- src/app.ts ---\nLine 1\nLine 2\nLine 3',
      );
      // Verify generateContentMock wasn't called because it bypassed the LLM routing
      expect(generateContentMock).not.toHaveBeenCalled();
    });

    it('compresses files read outside the protected window', async () => {
      const history: Content[] = [
        // Turn 0: The original function call to read the file
        {
          role: 'model',
          parts: [
            {
              functionCall: {
                name: 'read_file',
                args: { filepath: 'src/old.ts' },
              },
            },
          ],
        },
        // Turn 1: The tool output response
        {
          role: 'user',
          parts: [
            {
              functionResponse: {
                name: 'read_file',
                response: {
                  output: '--- src/old.ts ---\nLine 1\nLine 2\nLine 3\nLine 4',
                },
              },
            },
          ],
        },
        // Padding turns to push it out of the recent window
        { role: 'model', parts: [{ text: 'msg 2' }] },
        { role: 'user', parts: [{ text: 'res 2' }] },
        { role: 'model', parts: [{ text: 'msg 3' }] },
        { role: 'user', parts: [{ text: 'res 3' }] },
        { role: 'model', parts: [{ text: 'msg 4' }] },
        { role: 'user', parts: [{ text: 'res 4' }] },
      ];

      // Mock the routing request to return PARTIAL
      generateJsonMock.mockResolvedValueOnce({
        'src/old.ts': {
          level: 'PARTIAL',
          start_line: 2,
          end_line: 3,
        },
      });

      const res = await service.compressHistory(history, 'test prompt');
      const compressedOutput =
        res[1].parts![0].functionResponse!.response!['output'];

      expect(compressedOutput).toContain('[Showing lines 2–3 of 4 in old.ts.');
      expect(compressedOutput).toContain('2 | Line 2');
      expect(compressedOutput).toContain('3 | Line 3');
    });

    it('returns SUMMARY and hits cache on subsequent requests', async () => {
      const history1: Content[] = [
        {
          role: 'model',
          parts: [
            {
              functionCall: {
                name: 'read_file',
                args: { filepath: 'src/index.ts' },
              },
            },
          ],
        },
        {
          role: 'user',
          parts: [
            {
              functionResponse: {
                name: 'read_file',
                response: {
                  output: `--- src/index.ts ---\nVery long content here...`,
                },
              },
            },
          ],
        },
        { role: 'model', parts: [{ text: 'p1' }] },
        { role: 'user', parts: [{ text: 'p2' }] },
        { role: 'model', parts: [{ text: 'p3' }] },
        { role: 'user', parts: [{ text: 'p4' }] },
        { role: 'model', parts: [{ text: 'p5' }] },
        { role: 'user', parts: [{ text: 'p6' }] },
      ];

      // 1st request: routing says SUMMARY
      generateJsonMock.mockResolvedValueOnce({
        'src/index.ts': { level: 'SUMMARY' },
      });
      // 2nd request: the actual summarization call
      generateContentMock.mockResolvedValueOnce({
        candidates: [
          { content: { parts: [{ text: 'This is a cached summary.' }] } },
        ],
      });

      await service.compressHistory(history1, 'test query');
      expect(generateJsonMock).toHaveBeenCalledTimes(1);
      expect(generateContentMock).toHaveBeenCalledTimes(1);

      // Time passes, we get a new query. The file is still old.
      const history2: Content[] = [
        ...history1,
        { role: 'model', parts: [{ text: 'p7' }] },
        { role: 'user', parts: [{ text: 'p8' }] },
      ];

      // 3rd request: routing says SUMMARY again.
      generateJsonMock.mockResolvedValueOnce({
        'src/index.ts': { level: 'SUMMARY' },
      });

      const res = await service.compressHistory(history2, 'new query');

      // It should NOT make a 3rd fetch call for routing, since content has not changed and state is cached.
      expect(generateJsonMock).toHaveBeenCalledTimes(1);
      expect(generateContentMock).toHaveBeenCalledTimes(1);

      const compressedOutput =
        res[1].parts![0].functionResponse!.response!['output'];
      expect(compressedOutput).toContain('This is a cached summary.');
    });
    it('returns unmodified history if structural validation fails', async () => {
      // Creating a broken history where functionCall is NOT followed by user functionResponse
      const brokenHistory: Content[] = [
        {
          role: 'model',
          parts: [
            {
              functionCall: {
                name: 'read_file',
                args: { filepath: 'src/index.ts' },
              },
            },
          ],
        },
        // Missing user functionResponse!
        { role: 'model', parts: [{ text: 'Wait, I am a model again.' }] },
        { role: 'user', parts: [{ text: 'This is invalid.' }] },
        { role: 'model', parts: [{ text: 'Yep.' }] },
        { role: 'user', parts: [{ text: 'Padding.' }] },
        { role: 'model', parts: [{ text: 'Padding.' }] },
      ];

      const res = await service.compressHistory(brokenHistory, 'test query');

      // Because it's broken, it should return the exact same array by reference.
      expect(res).toBe(brokenHistory);
    });
  });
});