-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(inference): auto-detect Ollama context window during onboard #4253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9029676
fix(inference): auto-detect Ollama context window during onboard
zyang-dev d0b1feb
refactor(inference): move Ollama context auto-detect out of onboard
zyang-dev 422587e
fix(inference): preserve explicit Ollama context overrides
zyang-dev 2383825
docs(inference): normalize Ollama context-window sentence wrapping
zyang-dev baeab7b
refactor(inference): extract Ollama runtime context handling
cv 73aa46d
test(inference): align Ollama runtime mock field
cv 69b27ae
Merge branch 'main' into fix/ollama-context-window-autodetect
cv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { afterEach, describe, expect, it } from "vitest"; | ||
|
|
||
| import { | ||
| applyOllamaRuntimeContextWindow, | ||
| parseOllamaRuntimeContextLength, | ||
| probeOllamaRuntimeModelStatus, | ||
| resetOllamaRuntimeContextWindowAutoState, | ||
| resolveOllamaRuntimeContextWindow, | ||
| } from "../../../dist/lib/inference/ollama-runtime-context"; | ||
|
|
||
| const getOllamaHost = () => "127.0.0.1"; | ||
|
|
||
| describe("Ollama runtime context helpers", () => { | ||
| afterEach(() => { | ||
| resetOllamaRuntimeContextWindowAutoState(); | ||
| }); | ||
|
|
||
| it("parses valid Ollama /api/ps context lengths", () => { | ||
| expect(parseOllamaRuntimeContextLength(262144)).toEqual({ contextLength: 262144 }); | ||
| expect(parseOllamaRuntimeContextLength("262144")).toEqual({ contextLength: 262144 }); | ||
| }); | ||
|
|
||
| it("treats omitted Ollama /api/ps context lengths as compatibility no-ops", () => { | ||
| expect(parseOllamaRuntimeContextLength(undefined)).toEqual({}); | ||
| expect(parseOllamaRuntimeContextLength(null)).toEqual({}); | ||
| expect(parseOllamaRuntimeContextLength(" ")).toEqual({}); | ||
|
|
||
| const status = probeOllamaRuntimeModelStatus( | ||
| "qwen3.6:35b", | ||
| getOllamaHost, | ||
| () => JSON.stringify({ models: [{ name: "qwen3.6:35b", processor: "100% GPU" }] }), | ||
| ); | ||
|
|
||
| expect(status.loaded).toBe(true); | ||
| expect(status.contextLength).toBeUndefined(); | ||
| expect(status.contextLengthWarning).toBeUndefined(); | ||
| expect( | ||
| resolveOllamaRuntimeContextWindow("qwen3.6:35b", null, getOllamaHost, () => | ||
| JSON.stringify({ models: [{ name: "qwen3.6:35b" }] }), | ||
| ), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it("warns and ignores malformed or non-positive Ollama /api/ps context lengths", () => { | ||
| for (const value of ["bogus", "1.5", 0, -1]) { | ||
| const parsed = parseOllamaRuntimeContextLength(value); | ||
| expect(parsed.contextLength).toBeUndefined(); | ||
| expect(parsed.warning).toContain("non-positive or malformed context_length"); | ||
| } | ||
|
|
||
| const status = probeOllamaRuntimeModelStatus( | ||
| "qwen3.6:35b", | ||
| getOllamaHost, | ||
| () => JSON.stringify({ models: [{ name: "qwen3.6:35b", context_length: "bogus" }] }), | ||
| ); | ||
|
|
||
| expect(status.loaded).toBe(true); | ||
| expect(status.contextLength).toBeUndefined(); | ||
| expect(status.contextLengthWarning).toContain("non-positive or malformed context_length"); | ||
| }); | ||
|
|
||
| it("warns and ignores implausibly large Ollama /api/ps context lengths", () => { | ||
| const parsed = parseOllamaRuntimeContextLength(10_000_000); | ||
| expect(parsed.contextLength).toBeUndefined(); | ||
| expect(parsed.warning).toContain("above NemoClaw's auto-detect ceiling"); | ||
|
|
||
| const status = probeOllamaRuntimeModelStatus( | ||
| "qwen3.6:35b", | ||
| getOllamaHost, | ||
| () => JSON.stringify({ models: [{ name: "qwen3.6:35b", context_length: 10_000_000 }] }), | ||
| ); | ||
|
|
||
| expect(status.loaded).toBe(true); | ||
| expect(status.contextLength).toBeUndefined(); | ||
| expect(status.contextLengthWarning).toContain("above NemoClaw's auto-detect ceiling"); | ||
| expect( | ||
| resolveOllamaRuntimeContextWindow("qwen3.6:35b", null, getOllamaHost, () => | ||
| JSON.stringify({ models: [{ name: "qwen3.6:35b", context_length: 10_000_000 }] }), | ||
| ), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it("resolves runtime context length only when no explicit override is set", () => { | ||
| const capture = () => | ||
| JSON.stringify({ | ||
| models: [{ name: "qwen3.6:35b", context_length: "262144", processor: "100% GPU" }], | ||
| }); | ||
|
|
||
| expect( | ||
| resolveOllamaRuntimeContextWindow("qwen3.6:35b", null, getOllamaHost, capture), | ||
| ).toBe(262144); | ||
| expect( | ||
| resolveOllamaRuntimeContextWindow("qwen3.6:35b", "131072", getOllamaHost, capture), | ||
| ).toBeNull(); | ||
| expect( | ||
| resolveOllamaRuntimeContextWindow("qwen3.6:35b", "bogus", getOllamaHost, capture), | ||
| ).toBeNull(); | ||
| expect( | ||
| resolveOllamaRuntimeContextWindow("qwen3.6:35b", " ", getOllamaHost, capture), | ||
| ).toBe(262144); | ||
| expect( | ||
| resolveOllamaRuntimeContextWindow("other:model", null, getOllamaHost, capture), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it("applies and clears only auto-detected context window state", () => { | ||
| const env: NodeJS.ProcessEnv = {}; | ||
| const messages: string[] = []; | ||
| let models: Array<{ name: string; context_length?: number }> = []; | ||
| const options = { | ||
| env, | ||
| logger: { | ||
| log: (message: string) => messages.push(message), | ||
| warn: (message: string) => messages.push(message), | ||
| }, | ||
| runCaptureImpl: () => JSON.stringify({ models }), | ||
| }; | ||
|
|
||
| models = [{ name: "qwen3.6:35b", context_length: 262144 }]; | ||
| applyOllamaRuntimeContextWindow("qwen3.6:35b", getOllamaHost, options); | ||
| expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("262144"); | ||
|
|
||
| models = [{ name: "qwen2.5:7b", context_length: 32768 }]; | ||
| applyOllamaRuntimeContextWindow("qwen2.5:7b", getOllamaHost, options); | ||
| expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("32768"); | ||
|
|
||
| models = []; | ||
| applyOllamaRuntimeContextWindow("qwen2.5:7b", getOllamaHost, options); | ||
| expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); | ||
|
|
||
| resetOllamaRuntimeContextWindowAutoState(); | ||
| env.NEMOCLAW_CONTEXT_WINDOW = "262144"; | ||
| models = [{ name: "qwen2.5:7b", context_length: 32768 }]; | ||
| applyOllamaRuntimeContextWindow("qwen2.5:7b", getOllamaHost, options); | ||
| expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("262144"); | ||
| expect(messages.at(-1)).toContain("Keeping configured context window"); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.