Skip to content

Commit 3e9edd9

Browse files
authored
fix(ollama): retain advertised context and tools when model inspection is unavailable (#130000)
* fix(ollama): preserve model metadata from discovery * test(ollama): use numeric status in discovery fixture
1 parent b3376ce commit 3e9edd9

4 files changed

Lines changed: 167 additions & 27 deletions

File tree

‎extensions/ollama/src/provider-models.test.ts‎

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,81 @@ describe("ollama provider models", () => {
194194
).toBe(true);
195195
});
196196

197+
it.each([
198+
{
199+
description: "retains authoritative list metadata when model inspection fails",
200+
showResponse: () => jsonResponse({ error: "inspection unavailable" }, 503),
201+
contextWindow: 32_768,
202+
supportsTools: true,
203+
},
204+
{
205+
description: "retains list metadata omitted from a successful model inspection",
206+
showResponse: () => jsonResponse({}),
207+
contextWindow: 32_768,
208+
supportsTools: true,
209+
},
210+
{
211+
description: "prefers explicit model-inspection metadata over the model list",
212+
showResponse: () =>
213+
jsonResponse({
214+
model_info: { "gemma.context_length": 65_536 },
215+
capabilities: ["completion"],
216+
}),
217+
contextWindow: 65_536,
218+
supportsTools: false,
219+
},
220+
])("$description", async ({ showResponse, contextWindow, supportsTools }) => {
221+
vi.stubGlobal(
222+
"fetch",
223+
vi.fn(async (input: string | URL | Request) =>
224+
requestUrl(input).endsWith("/api/tags")
225+
? jsonResponse({
226+
models: [
227+
{
228+
name: "gemma4:e2b",
229+
details: { context_length: 32_768 },
230+
capabilities: ["completion", "tools"],
231+
},
232+
],
233+
})
234+
: showResponse(),
235+
),
236+
);
237+
238+
const provider = await buildOllamaProvider("http://127.0.0.1:11434");
239+
240+
expect(provider.models).toEqual([
241+
expect.objectContaining({
242+
id: "gemma4:e2b",
243+
contextWindow,
244+
compat: expect.objectContaining({ supportsTools }),
245+
}),
246+
]);
247+
});
248+
249+
it("keeps an explicit empty model-inspection capability list authoritative", async () => {
250+
vi.stubGlobal(
251+
"fetch",
252+
vi.fn(async (input: string | URL | Request) =>
253+
requestUrl(input).endsWith("/api/tags")
254+
? jsonResponse({
255+
models: [
256+
{
257+
name: "gemma4:e2b",
258+
details: { context_length: 32_768 },
259+
capabilities: ["completion", "tools"],
260+
},
261+
],
262+
})
263+
: jsonResponse({ capabilities: [] }),
264+
),
265+
);
266+
267+
await expect(buildOllamaProvider("http://127.0.0.1:11434")).resolves.toMatchObject({
268+
models: [],
269+
});
270+
});
271+
197272
it("forwards remote auth to model listing and show probes", async () => {
198273
const fetchMock = vi.fn(async (input: string | URL | Request, init?: RequestInit) => {
199274
expect(new Headers(init?.headers).get("Authorization")).toBe("Bearer cloud-key");
@@ -787,13 +862,29 @@ describe("ollama provider models", () => {
787862
}
788863
});
789864

790-
it("keeps tools off after a live /api/show failure", async () => {
865+
it.each([
866+
{
867+
description: "keeps tools off after a live inspection failure without list metadata",
868+
listed: { name: "deepseek-r1:14b", digest: "sha256:show-failure" },
869+
expected: { contextWindow: OLLAMA_DEFAULT_CONTEXT_WINDOW, supportsTools: false },
870+
},
871+
{
872+
description: "preserves authoritative list metadata after a live inspection failure",
873+
listed: {
874+
name: "gemma4:e2b",
875+
digest: "sha256:list-metadata",
876+
details: { context_length: 32_768 },
877+
capabilities: ["completion", "tools"],
878+
},
879+
expected: { contextWindow: 32_768, supportsTools: true },
880+
},
881+
])("$description", async ({ listed, expected }) => {
791882
const server = createServer((request, response) => {
792883
response.setHeader("Content-Type", "application/json");
793884
if (request.url === "/api/tags") {
794885
response.end(
795886
JSON.stringify({
796-
models: [{ name: "deepseek-r1:14b", digest: "sha256:show-failure" }],
887+
models: [listed],
797888
}),
798889
);
799890
return;
@@ -819,9 +910,10 @@ describe("ollama provider models", () => {
819910
const provider = await buildOllamaProvider(`http://127.0.0.1:${address.port}`);
820911
const model = expectDefined(provider.models?.[0], "show-failed Ollama model");
821912

822-
expect(model.id).toBe("deepseek-r1:14b");
823-
expect(model.compat?.supportsTools).toBe(false);
824-
expect(model.reasoning).toBe(true);
913+
expect(model.id).toBe(listed.name);
914+
expect(model.contextWindow).toBe(expected.contextWindow);
915+
expect(model.compat?.supportsTools).toBe(expected.supportsTools);
916+
expect(model.reasoning).toBe(listed.name.startsWith("deepseek-r1"));
825917
} finally {
826918
if (server.listening) {
827919
await new Promise<void>((resolve, reject) => {

‎extensions/ollama/src/provider-models.ts‎

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export type OllamaTagModel = {
2525
size?: number;
2626
digest?: string;
2727
remote_host?: string;
28+
capabilities?: string[];
2829
details?: {
30+
context_length?: number;
2931
family?: string;
3032
parameter_size?: string;
3133
quantization_level?: string;
@@ -43,11 +45,7 @@ type OllamaRunningModel = {
4345

4446
type OllamaModelRow = OllamaTagModel | OllamaRunningModel;
4547

46-
export type OllamaModelWithContext = OllamaTagModel & {
47-
contextWindow?: number;
48-
capabilities?: string[];
49-
showInspectionFailed?: boolean;
50-
};
48+
export type OllamaModelWithContext = OllamaTagModel & OllamaModelShowInfo;
5149

5250
const OLLAMA_SHOW_CONCURRENCY = 8;
5351
const OLLAMA_CONTEXT_ENRICH_LIMIT = 200;
@@ -95,6 +93,16 @@ export type OllamaModelShowInfo = {
9593
showInspectionFailed?: boolean;
9694
};
9795

96+
export const mergeOllamaModelShowInfo = (
97+
model: OllamaModelWithContext,
98+
info: OllamaModelShowInfo,
99+
): OllamaModelWithContext => ({
100+
...model,
101+
...info,
102+
contextWindow: info.contextWindow ?? model.contextWindow ?? model.details?.context_length,
103+
capabilities: info.capabilities ?? model.capabilities,
104+
});
105+
98106
const OLLAMA_FAILED_SHOW_INFO: OllamaModelShowInfo = Object.freeze({
99107
showInspectionFailed: true,
100108
});
@@ -297,7 +305,7 @@ export async function enrichOllamaModelsWithContext(
297305
const batchResults = await Promise.all(
298306
batch.map(async (model) => {
299307
const showInfo = await queryOllamaModelShowInfoCached(apiBase, model, opts);
300-
return Object.assign({}, model, showInfo);
308+
return mergeOllamaModelShowInfo(model, showInfo);
301309
}),
302310
);
303311
enriched.push(...batchResults);
@@ -371,8 +379,7 @@ export function buildOllamaModelDefinition(
371379
? isReasoningModelHeuristic(modelId)
372380
: capabilities.includes("thinking"));
373381
const compat = {
374-
supportsTools:
375-
opts?.showInspectionFailed === true ? false : (capabilities?.includes("tools") ?? true),
382+
supportsTools: capabilities?.includes("tools") ?? opts?.showInspectionFailed !== true,
376383
supportsUsageInStreaming: true,
377384
supportsJsonSchemaResponseFormat: !isOllamaCloudModel(modelId),
378385
};
@@ -479,12 +486,7 @@ export async function fetchOllamaModels(
479486
opts?: OllamaModelRequestOptions,
480487
deps?: OllamaModelsFetchDeps,
481488
): Promise<{ reachable: boolean; models: OllamaTagModel[] }> {
482-
const result = await fetchOllamaModelRows({
483-
baseUrl,
484-
endpoint: "tags",
485-
opts,
486-
deps,
487-
});
489+
const result = await fetchOllamaModelRows({ baseUrl, endpoint: "tags", opts, deps });
488490
return {
489491
reachable: result.reachable,
490492
models: result.models.filter(
@@ -498,12 +500,7 @@ export async function fetchLoadedOllamaModelNames(
498500
opts?: OllamaModelRequestOptions,
499501
deps?: OllamaModelsFetchDeps,
500502
): Promise<{ reachable: boolean; models: string[] }> {
501-
const result = await fetchOllamaModelRows({
502-
baseUrl,
503-
endpoint: "ps",
504-
opts,
505-
deps,
506-
});
503+
const result = await fetchOllamaModelRows({ baseUrl, endpoint: "ps", opts, deps });
507504
return {
508505
reachable: result.reachable,
509506
models: result.models

‎extensions/ollama/src/setup-model-selection.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,56 @@ describe("Ollama onboarding model selection", () => {
156156
expect(result.defaultModel).toBe("ollama/deepseek-r1:1b");
157157
});
158158

159+
it.each([
160+
{
161+
description: "model inspection fails",
162+
showResponse: () => Response.json({ error: "inspection unavailable" }, { status: 503 }),
163+
},
164+
{
165+
description: "model inspection omits metadata",
166+
showResponse: () => Response.json({}),
167+
},
168+
])(
169+
"selects and configures list-advertised models when $description",
170+
async ({ showResponse }) => {
171+
vi.stubGlobal(
172+
"fetch",
173+
vi.fn(async (input: string | URL | Request) =>
174+
requestUrl(input).endsWith("/api/tags")
175+
? Response.json({
176+
models: [
177+
{
178+
name: "gemma4:e2b",
179+
size: 2_000_000_000,
180+
details: { context_length: 32_768 },
181+
capabilities: ["completion", "tools"],
182+
},
183+
],
184+
})
185+
: showResponse(),
186+
),
187+
);
188+
const prompter = {
189+
select: vi.fn().mockResolvedValueOnce("local-only"),
190+
text: vi.fn().mockResolvedValueOnce("http://127.0.0.1:11434"),
191+
note: vi.fn(async () => undefined),
192+
confirm: vi.fn().mockResolvedValue(false),
193+
} as unknown as WizardPrompter;
194+
195+
const result = await promptAndConfigureOllama({ cfg: {}, prompter });
196+
197+
expect(result.defaultModel).toBe("ollama/gemma4:e2b");
198+
expect(result.config.models?.providers?.ollama?.models).toContainEqual(
199+
expect.objectContaining({
200+
id: "gemma4:e2b",
201+
contextWindow: 32_768,
202+
compat: expect.objectContaining({ supportsTools: true }),
203+
}),
204+
);
205+
expect(prompter.confirm).not.toHaveBeenCalled();
206+
},
207+
);
208+
159209
it("aborts pending model discovery with the setup signal", async () => {
160210
const controller = new AbortController();
161211
vi.stubGlobal(

‎extensions/ollama/src/setup-model-selection.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
enrichOllamaModelsWithContext,
88
fetchOllamaModels,
99
isReasoningModelHeuristic,
10+
mergeOllamaModelShowInfo,
1011
readOllamaModelShowInfo,
1112
resolveOllamaApiBase,
1213
type OllamaModelWithContext,
@@ -164,15 +165,15 @@ export async function inspectOllamaModelsForSetup(
164165
signal,
165166
auditContext: "ollama-setup.tools-scan",
166167
});
167-
return Object.assign({}, model, showInfo);
168+
return mergeOllamaModelShowInfo(model, showInfo);
168169
} catch (error) {
169170
signal?.throwIfAborted();
170171
// A failed inspection must not inherit the optimistic tools default
171172
// reserved for models that were never inspected. Keep the failure
172173
// distinct from authoritative empty capabilities so name-based
173174
// reasoning detection still applies.
174175
inspectionFailures.push(`${model.name}: ${formatErrorMessage(error)}`);
175-
return Object.assign({}, model, { showInspectionFailed: true as const });
176+
return mergeOllamaModelShowInfo(model, { showInspectionFailed: true });
176177
}
177178
}),
178179
);

0 commit comments

Comments
 (0)