|
1 |
| -import { describe, test, expect, vi, beforeEach } from "vitest" |
| 1 | +import { describe, test, expect, vi, afterEach, beforeEach } from "vitest" |
2 | 2 |
|
3 | 3 | // Mock vscode workspace
|
4 | 4 | vi.mock("vscode", () => ({
|
@@ -287,4 +287,88 @@ describe("runClaudeCode", () => {
|
287 | 287 | consoleErrorSpy.mockRestore()
|
288 | 288 | await generator.return(undefined)
|
289 | 289 | })
|
| 290 | + |
| 291 | + describe("system prompt truncation", () => { |
| 292 | + test("should truncate system prompt when memory bank content is detected", async () => { |
| 293 | + const { runClaudeCode } = await import("../run") |
| 294 | + |
| 295 | + const longSystemPrompt = `You are Claude Code, Anthropic's official CLI for Claude. |
| 296 | +You are Kilo Code, a highly skilled software engineer. |
| 297 | +
|
| 298 | +RULES |
| 299 | +
|
| 300 | +All responses MUST follow specific guidelines. |
| 301 | +
|
| 302 | +USER'S CUSTOM INSTRUCTIONS |
| 303 | +
|
| 304 | +The following additional instructions are provided by the user, and should be followed to the best of your ability without interfering with the TOOL USE guidelines. |
| 305 | +
|
| 306 | +Very long memory bank content that would cause E2BIG errors when passed as command line argument...`.repeat(100) |
| 307 | + |
| 308 | + const options = { |
| 309 | + systemPrompt: longSystemPrompt, |
| 310 | + messages: [{ role: "user" as const, content: "Hello" }], |
| 311 | + } |
| 312 | + |
| 313 | + const generator = runClaudeCode(options) |
| 314 | + |
| 315 | + // Since we're mocking execa, we can check what arguments were passed |
| 316 | + await generator.next() |
| 317 | + |
| 318 | + expect(mockExeca).toHaveBeenCalledWith( |
| 319 | + "claude", |
| 320 | + expect.arrayContaining([ |
| 321 | + "-p", |
| 322 | + "--system-prompt", |
| 323 | + expect.stringMatching(/^You are Claude Code.*RULES.*All responses MUST follow.*$/s), |
| 324 | + ]), |
| 325 | + expect.any(Object) |
| 326 | + ) |
| 327 | + |
| 328 | + // Verify the system prompt was truncated |
| 329 | + const call = mockExeca.mock.calls[0] |
| 330 | + const systemPromptArg = call[1][call[1].indexOf("--system-prompt") + 1] |
| 331 | + expect(systemPromptArg).not.toContain("USER'S CUSTOM INSTRUCTIONS") |
| 332 | + expect(systemPromptArg.length).toBeLessThan(longSystemPrompt.length) |
| 333 | + }) |
| 334 | + |
| 335 | + test("should truncate system prompt when it exceeds maximum safe size", async () => { |
| 336 | + const { runClaudeCode } = await import("../run") |
| 337 | + |
| 338 | + const veryLongSystemPrompt = "You are a helpful assistant. ".repeat(2000) // ~60KB |
| 339 | + |
| 340 | + const options = { |
| 341 | + systemPrompt: veryLongSystemPrompt, |
| 342 | + messages: [{ role: "user" as const, content: "Hello" }], |
| 343 | + } |
| 344 | + |
| 345 | + const generator = runClaudeCode(options) |
| 346 | + await generator.next() |
| 347 | + |
| 348 | + // Verify the system prompt was truncated to safe size |
| 349 | + const call = mockExeca.mock.calls[0] |
| 350 | + const systemPromptArg = call[1][call[1].indexOf("--system-prompt") + 1] |
| 351 | + expect(systemPromptArg.length).toBeLessThanOrEqual(32000) |
| 352 | + expect(systemPromptArg.length).toBeLessThan(veryLongSystemPrompt.length) |
| 353 | + }) |
| 354 | + |
| 355 | + test("should not truncate normal-sized system prompts", async () => { |
| 356 | + const { runClaudeCode } = await import("../run") |
| 357 | + |
| 358 | + const normalSystemPrompt = "You are a helpful assistant." |
| 359 | + |
| 360 | + const options = { |
| 361 | + systemPrompt: normalSystemPrompt, |
| 362 | + messages: [{ role: "user" as const, content: "Hello" }], |
| 363 | + } |
| 364 | + |
| 365 | + const generator = runClaudeCode(options) |
| 366 | + await generator.next() |
| 367 | + |
| 368 | + // Verify the system prompt was not truncated |
| 369 | + const call = mockExeca.mock.calls[0] |
| 370 | + const systemPromptArg = call[1][call[1].indexOf("--system-prompt") + 1] |
| 371 | + expect(systemPromptArg).toBe(normalSystemPrompt) |
| 372 | + }) |
| 373 | + }) |
290 | 374 | })
|
0 commit comments