Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/lib/inference/ollama/proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,51 @@ describe("promptOllamaModel installed-model fit filter", () => {
expect(result).not.toBe("nemotron-3-nano:30b");
});
});

describe("waitForPulledOllamaModel", () => {
it("retries model discovery with bounded backoff after a completed pull (#6038)", () => {
const proxy: typeof import("./proxy") = require(PROXY_DIST);
const sleeps: number[] = [];
let nowMs = 0;
let attempts = 0;

const discovered = proxy.waitForPulledOllamaModel("qwen3.5:9b", {
getModelOptions: () => {
attempts += 1;
return attempts >= 3 ? ["qwen3.5:9b"] : [];
},
now: () => nowMs,
sleep: (ms) => {
sleeps.push(ms);
nowMs += ms;
},
});

expect(discovered).toBe(true);
expect(attempts).toBe(3);
expect(sleeps).toEqual([250, 500]);
});

it("fails after the bounded discovery window when Ollama never lists the model (#6038)", () => {
const proxy: typeof import("./proxy") = require(PROXY_DIST);
const sleeps: number[] = [];
let nowMs = 0;
let attempts = 0;

const discovered = proxy.waitForPulledOllamaModel("qwen3.5:9b", {
getModelOptions: () => {
attempts += 1;
return [];
},
now: () => nowMs,
sleep: (ms) => {
sleeps.push(ms);
nowMs += ms;
},
});

expect(discovered).toBe(false);
expect(attempts).toBe(8);
expect(sleeps).toEqual([250, 500, 1_000, 2_000, 2_000, 2_000, 2_000]);
});
});
36 changes: 35 additions & 1 deletion src/lib/inference/ollama/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const path = require("path");
const { spawn, spawnSync } = require("child_process");
const { ROOT, SCRIPTS, redact, run, runCapture, shellQuote } = require("../../runner");
const { OLLAMA_PORT, OLLAMA_PROXY_PORT } = require("../../core/ports");
const { waitForPort } = require("../../core/wait");
const { waitForPort, waitUntil } = require("../../core/wait");
const {
getDefaultOllamaModel,
getBootstrapOllamaModelOptions,
Expand Down Expand Up @@ -763,6 +763,31 @@ async function pullOllamaModel(model) {
return pullOllamaModelViaCli(model);
}

const PULLED_MODEL_DISCOVERY_TIMEOUT_MS = 10_000;
const PULLED_MODEL_DISCOVERY_ATTEMPTS = 8;

/** Confirm that Ollama exposes a just-pulled model before onboarding continues. */
function waitForPulledOllamaModel(
model: string,
deps: {
getModelOptions?: () => string[];
now?: () => number;
sleep?: (ms: number) => void;
} = {},
): boolean {
const getModelOptions = deps.getModelOptions ?? getOllamaModelOptions;
const now = deps.now ?? Date.now;
return waitUntil(() => getModelOptions().includes(model), {
deadlineMs: now() + PULLED_MODEL_DISCOVERY_TIMEOUT_MS,
initialIntervalMs: 250,
maxIntervalMs: 2_000,
backoffFactor: 2,
maxAttempts: PULLED_MODEL_DISCOVERY_ATTEMPTS,
now,
sleep: deps.sleep,
});
}

// ── Tools-capability gate (issue #2667) ─────────────────────────
//
// Ollama models without the "tools" capability fail at first agent prompt
Expand Down Expand Up @@ -878,6 +903,14 @@ async function prepareOllamaModel(
"Check the model name and that Ollama can access the registry, then try another model.",
};
}
if (!waitForPulledOllamaModel(model)) {
return {
ok: false,
message:
`Ollama pull for '${model}' completed, but Ollama did not list the model afterward. ` +
"Wait for Ollama to finish registering the model, then choose it again.",
};
}
}

const capCheck = await checkOllamaModelToolSupport(model, interaction);
Expand Down Expand Up @@ -969,4 +1002,5 @@ export {
pullOllamaModel,
startOllamaAuthProxy,
unloadOllamaModels,
waitForPulledOllamaModel,
};
24 changes: 12 additions & 12 deletions test/onboard-selection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2305,23 +2305,23 @@ exit 0
);

const script = String.raw`
const fs = require("fs");
const credentials = require(${credentialsPath});
const runner = require(${runnerPath});

const answers = ["7", "1", "y"];
const messages = [];
const pullLog = ${JSON.stringify(pullLog)};

credentials.prompt = async (message) => {
messages.push(message);
return answers.shift() || "";
};
runner.runCapture = (command) => {
// Normalize: onboard.ts still sends strings, local-inference.ts sends arrays.
// Once onboard.ts is migrated to argv (#1889), these mocks can assert Array.isArray.
const cmd = Array.isArray(command) ? command.join(" ") : command;
if (cmd.includes("command -v ollama")) return "/usr/bin/ollama";
if (cmd.includes("127.0.0.1:11434/api/tags")) return JSON.stringify({ models: [] });
if (cmd.includes("ollama list")) return "";
if (cmd.includes("ollama list")) return fs.existsSync(pullLog) ? "qwen3.5:9b" : "";
if (cmd.includes("127.0.0.1:8000/v1/models")) return "";
if (cmd.includes("api/generate")) return '{"response":"hello"}';
if (cmd.includes("-o args=")) return "node ollama-auth-proxy.js";
Expand Down Expand Up @@ -2406,23 +2406,23 @@ exit 0
);

const script = String.raw`
const fs = require("fs");
const credentials = require(${credentialsPath});
const runner = require(${runnerPath});

const answers = ["7", "1", "y", "2", "llama3.2:3b", "y"];
const messages = [];
const pullLog = ${JSON.stringify(pullLog)};

credentials.prompt = async (message) => {
messages.push(message);
return answers.shift() || "";
};
runner.runCapture = (command) => {
// Normalize: onboard.ts still sends strings, local-inference.ts sends arrays.
// Once onboard.ts is migrated to argv (#1889), these mocks can assert Array.isArray.
const cmd = Array.isArray(command) ? command.join(" ") : command;
if (cmd.includes("command -v ollama")) return "/usr/bin/ollama";
if (cmd.includes("127.0.0.1:11434/api/tags")) return JSON.stringify({ models: [] });
if (cmd.includes("ollama list")) return "";
if (cmd.includes("ollama list")) return fs.existsSync(pullLog) && fs.readFileSync(pullLog, "utf8").includes("llama3.2:3b") ? "llama3.2:3b" : "";
if (cmd.includes("127.0.0.1:8000/v1/models")) return "";
if (cmd.includes("api/generate")) return '{"response":"hello"}';
if (cmd.includes("-o args=")) return "node ollama-auth-proxy.js";
Expand Down Expand Up @@ -2509,11 +2509,13 @@ exit 0
);

const script = String.raw`
const fs = require("fs");
const credentials = require(${credentialsPath});
const runner = require(${runnerPath});

const answers = ["7", "1", "n", "1", "y"];
const messages = [];
const pullLog = ${JSON.stringify(pullLog)};

credentials.prompt = async (message) => {
messages.push(message);
Expand All @@ -2523,7 +2525,7 @@ runner.runCapture = (command) => {
const cmd = Array.isArray(command) ? command.join(" ") : command;
if (cmd.includes("command -v ollama")) return "/usr/bin/ollama";
if (cmd.includes("127.0.0.1:11434/api/tags")) return JSON.stringify({ models: [] });
if (cmd.includes("ollama list")) return "";
if (cmd.includes("ollama list")) return fs.existsSync(pullLog) ? "qwen3.5:9b" : "";
if (cmd.includes("127.0.0.1:8000/v1/models")) return "";
if (cmd.includes("api/generate")) return '{"response":"hello"}';
if (cmd.includes("-o args=")) return "node ollama-auth-proxy.js";
Expand Down Expand Up @@ -2571,14 +2573,11 @@ const { setupNim } = require(${onboardPath});
line.includes("Skipped pulling Ollama model 'qwen3.5:9b'"),
),
);
// Pull only happened on the second confirmation, not on the declined first attempt.
assert.equal(fs.readFileSync(pullLog, "utf8").trim(), "qwen3.5:9b");
const downloadPrompts = payload.messages.filter((message: string) =>
/Download Ollama model/.test(message),
);
assert.equal(downloadPrompts.length, 2);
// Each prompt must surface the resolved size — the whole point of #2639 —
// either a "<value> <unit>" label or the explicit "size unknown" fallback.
const sizePattern = /\((\d+(\.\d+)? (B|KB|MB|GB|TB)( \(estimated\))?|size unknown)\)/;
for (const prompt of downloadPrompts) {
assert.match(prompt, sizePattern);
Expand Down Expand Up @@ -2612,11 +2611,13 @@ exit 0
);

const script = String.raw`
const fs = require("fs");
const credentials = require(${credentialsPath});
const runner = require(${runnerPath});

const answers = ["7", "1"];
const messages = [];
const pullLog = ${JSON.stringify(pullLog)};

credentials.prompt = async (message) => {
messages.push(message);
Expand All @@ -2626,7 +2627,7 @@ runner.runCapture = (command) => {
const cmd = Array.isArray(command) ? command.join(" ") : command;
if (cmd.includes("command -v ollama")) return "/usr/bin/ollama";
if (cmd.includes("127.0.0.1:11434/api/tags")) return JSON.stringify({ models: [] });
if (cmd.includes("ollama list")) return "";
if (cmd.includes("ollama list")) return fs.existsSync(pullLog) ? "qwen3.5:9b" : "";
if (cmd.includes("127.0.0.1:8000/v1/models")) return "";
if (cmd.includes("api/generate")) return '{"response":"hello"}';
if (cmd.includes("-o args=")) return "node ollama-auth-proxy.js";
Expand Down Expand Up @@ -2671,7 +2672,6 @@ const { setupNim } = require(${onboardPath});
assert.equal(payload.result.provider, "ollama-local");
assert.equal(payload.result.model, "qwen3.5:9b");
assert.equal(fs.readFileSync(pullLog, "utf8").trim(), "qwen3.5:9b");
// No "Download Ollama model 'X'?" prompt was issued — the env var bypassed it.
assert.equal(
payload.messages.filter((message: string) => /Download Ollama model/.test(message)).length,
0,
Expand Down
Loading