From df38bfe97cfb12d5076c1cbece556b8bd5640732 Mon Sep 17 00:00:00 2001 From: Roo Code Date: Sun, 17 Aug 2025 07:56:24 +0000 Subject: [PATCH] fix: do not override Ollama model num_ctx configuration The native Ollama handler was explicitly setting num_ctx to modelInfo.contextWindow, which overrode any user-configured num_ctx value in their Ollama model configuration. This caused issues for users who had specifically configured their models with custom context sizes to fit within their available memory. This fix removes the num_ctx override, allowing Ollama to use the model's configured value as intended. Fixes #7159 --- .../providers/__tests__/native-ollama.spec.ts | 63 +++++++++++++++++++ src/api/providers/native-ollama.ts | 3 +- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/api/providers/__tests__/native-ollama.spec.ts b/src/api/providers/__tests__/native-ollama.spec.ts index f8792937db..e83017e471 100644 --- a/src/api/providers/__tests__/native-ollama.spec.ts +++ b/src/api/providers/__tests__/native-ollama.spec.ts @@ -122,6 +122,69 @@ describe("NativeOllamaHandler", () => { }) }) + describe("num_ctx handling", () => { + it("should not override num_ctx in createMessage", async () => { + // Mock the chat response + mockChat.mockImplementation(async function* () { + yield { + message: { content: "Test response" }, + eval_count: 1, + prompt_eval_count: 1, + } + }) + + const systemPrompt = "You are a helpful assistant" + const messages = [{ role: "user" as const, content: "Test" }] + + const stream = handler.createMessage(systemPrompt, messages) + + // Consume the stream + for await (const _ of stream) { + // Just consume + } + + // Verify that num_ctx was NOT set in the options + expect(mockChat).toHaveBeenCalledWith( + expect.objectContaining({ + model: "llama2", + messages: expect.any(Array), + stream: true, + options: expect.objectContaining({ + temperature: 0, + }), + }), + ) + + // Specifically check that num_ctx is not in the options + const callArgs = mockChat.mock.calls[0][0] + expect(callArgs.options).not.toHaveProperty("num_ctx") + }) + + it("should not override num_ctx in completePrompt", async () => { + mockChat.mockResolvedValue({ + message: { content: "Test response" }, + }) + + await handler.completePrompt("Test prompt") + + // Verify that num_ctx was NOT set in the options + expect(mockChat).toHaveBeenCalledWith( + expect.objectContaining({ + model: "llama2", + messages: expect.any(Array), + stream: false, + options: expect.objectContaining({ + temperature: 0, + }), + }), + ) + + // Specifically check that num_ctx is not in the options + const callArgs = mockChat.mock.calls[0][0] + expect(callArgs.options).not.toHaveProperty("num_ctx") + }) + }) + describe("error handling", () => { it("should handle connection refused errors", async () => { const error = new Error("ECONNREFUSED") as any diff --git a/src/api/providers/native-ollama.ts b/src/api/providers/native-ollama.ts index 8ab4ebe2e1..c84b984f12 100644 --- a/src/api/providers/native-ollama.ts +++ b/src/api/providers/native-ollama.ts @@ -181,7 +181,7 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio messages: ollamaMessages, stream: true, options: { - num_ctx: modelInfo.contextWindow, + // Don't override num_ctx - let Ollama use the model's configured value temperature: this.options.modelTemperature ?? (useR1Format ? DEEP_SEEK_DEFAULT_TEMPERATURE : 0), }, }) @@ -270,6 +270,7 @@ export class NativeOllamaHandler extends BaseProvider implements SingleCompletio messages: [{ role: "user", content: prompt }], stream: false, options: { + // Don't override num_ctx - let Ollama use the model's configured value temperature: this.options.modelTemperature ?? (useR1Format ? DEEP_SEEK_DEFAULT_TEMPERATURE : 0), }, })