Skip to content

Commit 9d8f849

Browse files
committed
fix(ollama): keep catalog-missing dynamic resolve fail-closed on show failure
Failed /api/show is an existence probe for unresolved models; return undefined so typos/404s stay rejected. Tag-discovered and setup paths keep showInspectionFailed tools-off behavior.
1 parent 804503e commit 9d8f849

2 files changed

Lines changed: 50 additions & 31 deletions

File tree

‎extensions/ollama/index.test.ts‎

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ describe("ollama plugin", () => {
13211321
}
13221322
});
13231323

1324-
it("resolves requested Ollama models with tools off when /api/show fails", async () => {
1324+
it("keeps catalog-missing Ollama models unresolved when /api/show fails", async () => {
13251325
const provider = registerProvider();
13261326
const previous = process.env.OLLAMA_API_KEY;
13271327
process.env.OLLAMA_API_KEY = "ollama-local";
@@ -1330,31 +1330,62 @@ describe("ollama plugin", () => {
13301330
api: "ollama",
13311331
models: [],
13321332
});
1333+
// Shared outcome of HTTP 404 and thrown /api/show (see provider-models tests).
13331334
queryOllamaModelShowInfoMock.mockResolvedValueOnce({ showInspectionFailed: true });
13341335

13351336
try {
13361337
await provider.prepareDynamicModel?.({
13371338
config: {},
13381339
provider: "ollama",
1339-
modelId: "deepseek-r1:14b",
1340+
modelId: "depseek-v4-pro:cloud",
13401341
modelRegistry: { find: vi.fn(() => null) },
13411342
} as never);
13421343

1343-
const resolved = provider.resolveDynamicModel?.({
1344+
expect(
1345+
provider.resolveDynamicModel?.({
1346+
config: {},
1347+
provider: "ollama",
1348+
modelId: "depseek-v4-pro:cloud",
1349+
modelRegistry: { find: vi.fn(() => null) },
1350+
} as never),
1351+
).toBeUndefined();
1352+
expect(buildOllamaModelDefinitionMock).not.toHaveBeenCalled();
1353+
} finally {
1354+
if (previous === undefined) {
1355+
delete process.env.OLLAMA_API_KEY;
1356+
} else {
1357+
process.env.OLLAMA_API_KEY = previous;
1358+
}
1359+
}
1360+
});
1361+
1362+
it("keeps catalog-missing Ollama models unresolved when /api/show throws", async () => {
1363+
const provider = registerProvider();
1364+
const previous = process.env.OLLAMA_API_KEY;
1365+
process.env.OLLAMA_API_KEY = "ollama-local";
1366+
buildOllamaProviderMock.mockResolvedValueOnce({
1367+
baseUrl: "http://127.0.0.1:11434",
1368+
api: "ollama",
1369+
models: [],
1370+
});
1371+
queryOllamaModelShowInfoMock.mockResolvedValueOnce({ showInspectionFailed: true });
1372+
1373+
try {
1374+
await provider.prepareDynamicModel?.({
13441375
config: {},
13451376
provider: "ollama",
1346-
modelId: "deepseek-r1:14b",
1377+
modelId: "typo-model:latest",
13471378
modelRegistry: { find: vi.fn(() => null) },
13481379
} as never);
1349-
expect(resolved?.id).toBe("deepseek-r1:14b");
1350-
expect(resolved?.compat?.supportsTools).toBe(false);
1351-
expect(resolved?.reasoning).toBe(true);
1352-
expect(buildOllamaModelDefinitionMock).toHaveBeenCalledWith(
1353-
"deepseek-r1:14b",
1354-
undefined,
1355-
undefined,
1356-
{ showInspectionFailed: true },
1357-
);
1380+
1381+
expect(
1382+
provider.resolveDynamicModel?.({
1383+
config: {},
1384+
provider: "ollama",
1385+
modelId: "typo-model:latest",
1386+
modelRegistry: { find: vi.fn(() => null) },
1387+
} as never),
1388+
).toBeUndefined();
13581389
} finally {
13591390
if (previous === undefined) {
13601391
delete process.env.OLLAMA_API_KEY;
@@ -1933,4 +1964,4 @@ describe("ollama plugin", () => {
19331964
expect(ollamaMedia.defaultModels).toBeUndefined();
19341965
expect(ollamaMedia.autoPriority).toBeUndefined();
19351966
});
1936-
});
1967+
});

‎extensions/ollama/index.ts‎

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -452,23 +452,11 @@ async function resolveRequestedDynamicOllamaModel(params: {
452452
const showInfo = params.showApiKey
453453
? await queryOllamaModelShowInfo(showBaseUrl, params.modelId, { apiKey: params.showApiKey })
454454
: await queryOllamaModelShowInfo(showBaseUrl, params.modelId);
455-
// Failed /api/show must still produce a definition so /models add matches
456-
// provider discovery + setup: tools off, reasoning name heuristics preserved.
455+
// Catalog-missing dynamic resolve (/models add) treats /api/show as the
456+
// existence probe. Failed inspection must stay unresolved so typos/404s do
457+
// not become accepted config; tag-discovered + setup still use the marker.
457458
if (showInfo.showInspectionFailed) {
458-
const definition = buildOllamaModelDefinition(
459-
params.modelId,
460-
showInfo.contextWindow,
461-
undefined,
462-
{
463-
showInspectionFailed: true,
464-
},
465-
);
466-
const model = params.capContextTokens ? capLocalOllamaModelContext(definition) : definition;
467-
return toDynamicOllamaModel({
468-
provider: params.provider,
469-
providerConfig: params.providerConfig,
470-
model,
471-
});
459+
return undefined;
472460
}
473461
if (typeof showInfo.contextWindow !== "number" && (showInfo.capabilities?.length ?? 0) === 0) {
474462
return undefined;
@@ -937,4 +925,4 @@ export default definePluginEntry({
937925
});
938926
},
939927
});
940-
/* oxlint-disable max-lines -- TODO: split this grandfathered oversized file. */
928+
/* oxlint-disable max-lines -- TODO: split this grandfathered oversized file. */

0 commit comments

Comments
 (0)