Skip to content

Commit 8bfcdbe

Browse files
authored
fix(llama-cpp): prefer loaded models during existing-server setup (#129509)
1 parent 5e76b5a commit 8bfcdbe

2 files changed

Lines changed: 59 additions & 24 deletions

File tree

‎extensions/llama-cpp/src/external-server/setup.test.ts‎

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -206,31 +206,68 @@ describe("llama-server setup", () => {
206206
expect(discoverMock).not.toHaveBeenCalled();
207207
});
208208

209-
it("does not select a failed router model while a healthy model is available", async () => {
209+
it.each([
210+
{
211+
name: "prefers a loaded model over an unloaded higher-ranked family",
212+
models: [
213+
{ id: "meta-llama/Llama-3.3-8B", status: "loaded" },
214+
{ id: "google/gemma-4-27b", status: "unloaded" },
215+
],
216+
expected: "llama-cpp/meta-llama/Llama-3.3-8B",
217+
},
218+
{
219+
name: "prefers a sleeping model over an unloaded higher-ranked family",
220+
models: [
221+
{ id: "meta-llama/Llama-3.3-8B", status: "sleeping" },
222+
{ id: "google/gemma-4-27b", status: "unloaded" },
223+
],
224+
expected: "llama-cpp/meta-llama/Llama-3.3-8B",
225+
},
226+
{
227+
name: "preserves family preference among loaded models",
228+
models: [
229+
{ id: "meta-llama/Llama-3.3-8B", status: "loaded" },
230+
{ id: "google/gemma-4-27b", status: "loaded" },
231+
],
232+
expected: "llama-cpp/google/gemma-4-27b",
233+
},
234+
{
235+
name: "preserves family preference when no model is loaded",
236+
models: [
237+
{ id: "meta-llama/Llama-3.3-8B", status: "unloaded" },
238+
{ id: "google/gemma-4-27b", status: "unloaded" },
239+
],
240+
expected: "llama-cpp/google/gemma-4-27b",
241+
},
242+
{
243+
name: "prefers a healthy unloaded model over a failed loaded model",
244+
models: [
245+
{ id: "google/gemma-4-27b", status: "loaded", failed: true },
246+
{ id: "meta-llama/Llama-3.3-8B", status: "unloaded" },
247+
],
248+
expected: "llama-cpp/meta-llama/Llama-3.3-8B",
249+
},
250+
{
251+
name: "returns no candidate for an empty model catalog",
252+
models: [],
253+
expected: null,
254+
},
255+
] as const)("$name", async ({ models, expected }) => {
210256
const discovery = successfulDiscovery();
211257
const baseModel = discovery.models[0];
212258
if (!baseModel) {
213259
throw new Error("expected discovery fixture model");
214260
}
215-
discovery.models = [
216-
{
217-
...baseModel,
218-
config: { ...baseModel.config, id: "qwen-failed", name: "qwen-failed" },
219-
status: "unloaded",
220-
failed: true,
221-
},
222-
{
223-
...baseModel,
224-
config: { ...baseModel.config, id: "healthy-model", name: "healthy-model" },
225-
status: "unloaded",
226-
failed: false,
227-
},
228-
];
261+
discovery.models = models.map((model) => ({
262+
...baseModel,
263+
config: { ...baseModel.config, id: model.id, name: model.id },
264+
status: model.status,
265+
failed: "failed" in model && model.failed,
266+
}));
229267
discoverMock.mockResolvedValue(discovery);
230268

231-
await expect(detectLlamaServerSetup({ config: {}, env: {} })).resolves.toMatchObject({
232-
modelRef: "llama-cpp/healthy-model",
233-
});
269+
const result = await detectLlamaServerSetup({ config: {}, env: {} });
270+
expect(result?.modelRef ?? null).toBe(expected);
234271
});
235272

236273
it("prefers configured Authorization over ambient auth during guided detection", async () => {

‎extensions/llama-cpp/src/external-server/setup.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@ import { buildLlamaServerProviderConfig } from "./models.js";
4040
function selectSetupModelId(discovery: Extract<LlamaServerDiscoveryResult, { kind: "success" }>) {
4141
const healthy = discovery.models.filter((model) => !model.failed);
4242
const candidates = healthy.length > 0 ? healthy : discovery.models;
43-
const ordered = candidates.toSorted((left, right) => {
44-
const leftLoaded = left.status === "loaded" || left.status === "sleeping";
45-
const rightLoaded = right.status === "loaded" || right.status === "sleeping";
46-
return Number(rightLoaded) - Number(leftLoaded);
47-
});
48-
const ids = ordered.map((model) => model.config.id);
43+
const ready = candidates.filter(
44+
(model) => model.status === "loaded" || model.status === "sleeping",
45+
);
46+
const ids = (ready.length > 0 ? ready : candidates).map((model) => model.config.id);
4947
return selectPreferredLocalModelId(ids) ?? ids[0];
5048
}
5149

0 commit comments

Comments
 (0)