Skip to content

Commit 254ab82

Browse files
authored
Merge pull request #117919 from openclaw/live-local-provider-e2e-20260802
fix(lmstudio): avoid loading downloaded models during setup
2 parents 2970ccb + 4fa7c24 commit 254ab82

5 files changed

Lines changed: 175 additions & 46 deletions

File tree

‎extensions/lmstudio/index.test.ts‎

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,13 @@ describe("lmstudio plugin", () => {
8989
fetchLmstudioModelsMock.mockResolvedValue({
9090
reachable: true,
9191
status: 200,
92-
models: [{ type: "llm", key: "qwen/qwen3.5-9b" }],
92+
models: [
93+
{
94+
type: "llm",
95+
key: "qwen/qwen3.5-9b",
96+
loaded_instances: [{ id: "qwen", config: { context_length: 32_768 } }],
97+
},
98+
],
9399
});
94100
const ctx = createLmstudioResetValidationContext({
95101
customBaseUrl: "http://lmstudio.internal:1234/api/v1/",
@@ -112,7 +118,13 @@ describe("lmstudio plugin", () => {
112118
fetchLmstudioModelsMock.mockResolvedValue({
113119
reachable: true,
114120
status: 200,
115-
models: [{ type: "llm", key: "qwen/qwen3.5-9b" }],
121+
models: [
122+
{
123+
type: "llm",
124+
key: "qwen/qwen3.5-9b",
125+
loaded_instances: [{ id: "qwen", config: { context_length: 32_768 } }],
126+
},
127+
],
116128
});
117129
const ctx = createLmstudioResetValidationContext(
118130
{
@@ -173,7 +185,13 @@ describe("lmstudio plugin", () => {
173185
fetchLmstudioModelsMock.mockResolvedValue({
174186
reachable: true,
175187
status: 200,
176-
models: [{ type: "llm", key: "phi-4" }],
188+
models: [
189+
{
190+
type: "llm",
191+
key: "phi-4",
192+
loaded_instances: [{ id: "phi", config: { context_length: 32_768 } }],
193+
},
194+
],
177195
});
178196
const ctx = createLmstudioResetValidationContext({
179197
customBaseUrl: "http://lmstudio.internal:1234/v1",
@@ -192,7 +210,13 @@ describe("lmstudio plugin", () => {
192210
fetchLmstudioModelsMock.mockResolvedValue({
193211
reachable: true,
194212
status: 200,
195-
models: [{ type: "llm", key: "qwen/qwen3.5-9b" }],
213+
models: [
214+
{
215+
type: "llm",
216+
key: "qwen/qwen3.5-9b",
217+
loaded_instances: [{ id: "qwen", config: { context_length: 32_768 } }],
218+
},
219+
],
196220
});
197221
const ctx = createLmstudioResetValidationContext({
198222
customBaseUrl: "http://lmstudio.internal:1234/v1",
@@ -220,7 +244,26 @@ describe("lmstudio plugin", () => {
220244
await expect(requireLmstudioResetValidator()(ctx)).resolves.toBe(false);
221245

222246
expect(ctx.runtime.error).toHaveBeenCalledWith(
223-
"No LM Studio LLM models were found at http://lmstudio.internal:1234/v1.\nLoad at least one model in LM Studio (or run lms load), then re-run setup.",
247+
"No loaded LM Studio LLM models were found at http://lmstudio.internal:1234/v1.\nLoad a model in LM Studio (or run lms load <model>), then re-run setup.",
248+
);
249+
expect(ctx.runtime.exit).toHaveBeenCalledWith(1);
250+
});
251+
252+
it("rejects an installed but unloaded LM Studio model before destructive reset", async () => {
253+
fetchLmstudioModelsMock.mockResolvedValue({
254+
reachable: true,
255+
status: 200,
256+
models: [{ type: "llm", key: "qwen/qwen3.5-9b", loaded_instances: [] }],
257+
});
258+
const ctx = createLmstudioResetValidationContext({
259+
customBaseUrl: "http://lmstudio.internal:1234/v1",
260+
customModelId: "qwen/qwen3.5-9b",
261+
});
262+
263+
await expect(requireLmstudioResetValidator()(ctx)).resolves.toBe(false);
264+
265+
expect(ctx.runtime.error).toHaveBeenCalledWith(
266+
"LM Studio model qwen/qwen3.5-9b is installed but not loaded at http://lmstudio.internal:1234/v1.\nLoad that model in LM Studio, then re-run setup.",
224267
);
225268
expect(ctx.runtime.exit).toHaveBeenCalledWith(1);
226269
});

‎extensions/lmstudio/index.ts‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
import {
2626
normalizeLmstudioConfiguredCatalogEntries,
2727
normalizeLmstudioProviderConfig,
28+
resolveLoadedContextWindow,
2829
resolveLmstudioInferenceBase,
2930
} from "./src/models.js";
3031
import { shouldUseLmstudioSyntheticAuth } from "./src/provider-auth.js";
@@ -82,23 +83,39 @@ async function validateLmstudioNonInteractive(
8283
return false;
8384
}
8485

85-
const availableModels = discovery.models
86+
const installedModels = discovery.models
8687
.filter((model) => model.type === "llm")
8788
.map((model) => model.key?.trim())
8889
.filter((model): model is string => Boolean(model));
90+
const loadedModels = discovery.models
91+
.filter(
92+
(model) =>
93+
model.type === "llm" &&
94+
Boolean(model.key?.trim()) &&
95+
resolveLoadedContextWindow(model) !== null,
96+
)
97+
.map((model) => model.key?.trim())
98+
.filter((model): model is string => Boolean(model));
8999
// Setup matches the requested wire key unchanged. Accepting provider-
90100
// qualified refs here would permit reset before setup rejects the model.
91101
const requestedModel = normalizeOptionalSecretInput(ctx.opts.customModelId);
92-
if (requestedModel && !availableModels.includes(requestedModel)) {
102+
if (requestedModel && !installedModels.includes(requestedModel)) {
103+
ctx.runtime.error(
104+
`LM Studio model ${requestedModel} was not found at ${baseUrl}.\nAvailable models: ${installedModels.join(", ")}`,
105+
);
106+
ctx.runtime.exit(1);
107+
return false;
108+
}
109+
if (requestedModel && !loadedModels.includes(requestedModel)) {
93110
ctx.runtime.error(
94-
`LM Studio model ${requestedModel} was not found at ${baseUrl}.\nAvailable models: ${availableModels.join(", ")}`,
111+
`LM Studio model ${requestedModel} is installed but not loaded at ${baseUrl}.\nLoad that model in LM Studio, then re-run setup.`,
95112
);
96113
ctx.runtime.exit(1);
97114
return false;
98115
}
99-
if (availableModels.length === 0) {
116+
if (loadedModels.length === 0) {
100117
ctx.runtime.error(
101-
`No LM Studio LLM models were found at ${baseUrl}.\nLoad at least one model in LM Studio (or run lms load), then re-run setup.`,
118+
`No loaded LM Studio LLM models were found at ${baseUrl}.\nLoad a model in LM Studio (or run lms load <model>), then re-run setup.`,
102119
);
103120
ctx.runtime.exit(1);
104121
return false;

‎extensions/lmstudio/src/models.ts‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,6 @@ export function resolveLoadedContextWindow(
252252
return contextWindow;
253253
}
254254

255-
/** Uses the loaded context when present, otherwise the model's advertised maximum. */
256-
export function resolveLmstudioEffectiveContextWindow(
257-
entry: Pick<LmstudioModelWire, "loaded_instances" | "max_context_length">,
258-
): number | null {
259-
return (
260-
resolveLoadedContextWindow(entry) ?? asPositiveSafeInteger(entry.max_context_length) ?? null
261-
);
262-
}
263-
264255
function normalizeLmstudioVariantIds(value: unknown): string[] {
265256
if (!Array.isArray(value)) {
266257
return [];

‎extensions/lmstudio/src/setup.test.ts‎

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,12 @@ function createWireModel(
106106
key: string,
107107
overrides: Omit<LmstudioModelWire, "key"> = {},
108108
): LmstudioModelWire {
109-
return { type: "llm", key, ...overrides };
109+
return {
110+
type: "llm",
111+
key,
112+
loaded_instances: [{ id: `${key}-loaded`, config: { context_length: 32_768 } }],
113+
...overrides,
114+
};
110115
}
111116

112117
function mockFetchedModels(models: LmstudioModelWire[]): void {
@@ -411,14 +416,42 @@ describe("lmstudio setup", () => {
411416

412417
it.each([
413418
{
414-
name: "prefers the strongest tool-calling family among installed models",
419+
name: "prefers the strongest tool-calling family among loaded models",
415420
models: [
416-
{ key: "llama3.3-70b-instruct", max_context_length: 65_536 },
417-
{ key: "qwen3.5-4b-instruct", max_context_length: 65_536 },
418-
{ key: "nomic-embed-text", max_context_length: 65_536 },
421+
{
422+
key: "llama3.3-70b-instruct",
423+
max_context_length: 65_536,
424+
loaded_instances: [{ id: "llama", config: { context_length: 32_768 } }],
425+
},
426+
{
427+
key: "qwen3.5-4b-instruct",
428+
max_context_length: 65_536,
429+
loaded_instances: [{ id: "qwen", config: { context_length: 32_768 } }],
430+
},
431+
{
432+
key: "nomic-embed-text",
433+
max_context_length: 65_536,
434+
loaded_instances: [{ id: "nomic", config: { context_length: 32_768 } }],
435+
},
419436
],
420437
expectedModel: "lmstudio/qwen3.5-4b-instruct",
421438
},
439+
{
440+
name: "ignores a preferred downloaded model when another model is loaded",
441+
models: [
442+
{
443+
key: "qwen3.5-4b-instruct",
444+
max_context_length: 65_536,
445+
loaded_instances: [],
446+
},
447+
{
448+
key: "llama3.3-70b-instruct",
449+
max_context_length: 65_536,
450+
loaded_instances: [{ id: "llama", config: { context_length: 32_768 } }],
451+
},
452+
],
453+
expectedModel: "lmstudio/llama3.3-70b-instruct",
454+
},
422455
{
423456
name: "skips a preferred model loaded with less than 16k context",
424457
models: [
@@ -682,6 +715,24 @@ describe("lmstudio setup", () => {
682715
expect(configureSelfHostedNonInteractiveMock).not.toHaveBeenCalled();
683716
});
684717

718+
it("non-interactive setup rejects an installed model that is not loaded", async () => {
719+
mockFetchedModelsOnce([
720+
createWireModel("qwen3-8b-instruct", { loaded_instances: [] }),
721+
createWireModel("phi-4"),
722+
]);
723+
const ctx = buildNonInteractiveContext({
724+
customModelId: "qwen3-8b-instruct",
725+
});
726+
727+
await expect(configureLmstudioNonInteractive(ctx)).resolves.toBeNull();
728+
729+
expect(ctx.runtime.error).toHaveBeenCalledWith(
730+
"LM Studio model qwen3-8b-instruct is installed but not loaded at http://localhost:1234/v1.\nLoad that model in LM Studio, then re-run setup.",
731+
);
732+
expect(ctx.runtime.exit).toHaveBeenCalledWith(1);
733+
expect(configureSelfHostedNonInteractiveMock).not.toHaveBeenCalled();
734+
});
735+
685736
it("interactive setup canonicalizes base URL and persists provider/default model", async () => {
686737
const result = await runInteractive();
687738

@@ -1007,7 +1058,16 @@ describe("lmstudio setup", () => {
10071058
status: 200,
10081059
models: [{ type: "embedding", key: "text-embedding-nomic-embed-text-v1.5" }],
10091060
},
1010-
expectedError: "No LM Studio models found",
1061+
expectedError: "No loaded LM Studio models found",
1062+
},
1063+
{
1064+
name: "only unloaded llm models",
1065+
discovery: {
1066+
reachable: true,
1067+
status: 200,
1068+
models: [createWireModel("qwen3-8b-instruct", { loaded_instances: [] })],
1069+
},
1070+
expectedError: "No loaded LM Studio models found",
10111071
},
10121072
];
10131073

‎extensions/lmstudio/src/setup.ts‎

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import { discoverLmstudioModels, fetchLmstudioModels } from "./models.fetch.js";
4545
import {
4646
mapLmstudioWireModelsToConfig,
4747
type LmstudioModelWire,
48-
resolveLmstudioEffectiveContextWindow,
48+
resolveLoadedContextWindow,
4949
resolveLmstudioInferenceBase,
5050
} from "./models.js";
5151
import {
@@ -73,6 +73,7 @@ const LMSTUDIO_APP_GUIDED_MIN_CONTEXT_TOKENS = 16_384;
7373
type LmstudioSetupDiscovery = {
7474
discovery: LmstudioDiscoveryResult;
7575
models: ModelDefinitionConfig[];
76+
loadedModelIds: Set<string>;
7677
defaultModel: string | undefined;
7778
defaultModelId: string | undefined;
7879
};
@@ -206,6 +207,15 @@ function applyRequestedContextWindowToAllModels(params: {
206207
);
207208
}
208209

210+
function collectLoadedLmstudioModelIds(discovery: LmstudioDiscoveryResult): Set<string> {
211+
return new Set(
212+
discovery.models.flatMap((entry) => {
213+
const id = entry.key?.trim();
214+
return entry.type === "llm" && id && resolveLoadedContextWindow(entry) !== null ? [id] : [];
215+
}),
216+
);
217+
}
218+
209219
function resolveLmstudioDiscoveryFailure(params: {
210220
baseUrl: string;
211221
discovery: LmstudioDiscoveryResult;
@@ -238,17 +248,14 @@ function resolveLmstudioDiscoveryFailure(params: {
238248
reason: `LM Studio discovery failed (${discovery.status})`,
239249
};
240250
}
241-
const hasUsableModel = discovery.models.some(
242-
(model) => model.type === "llm" && Boolean(model.key?.trim()),
243-
);
244-
if (!hasUsableModel) {
251+
if (collectLoadedLmstudioModelIds(discovery).size === 0) {
245252
return {
246253
noteLines: [
247-
`No LM Studio LLM models were found at ${baseUrl}.`,
248-
"Load at least one model in LM Studio (or run lms load), then re-run setup.",
254+
`No loaded LM Studio LLM models were found at ${baseUrl}.`,
255+
"Load a model in LM Studio (or run lms load <model>), then re-run setup.",
249256
],
250-
retryLine: "Load at least one model in LM Studio (or run lms load), then continue to retry.",
251-
reason: "No LM Studio models found",
257+
retryLine: "Load a model in LM Studio (or run lms load <model>), then continue to retry.",
258+
reason: "No loaded LM Studio models found",
252259
};
253260
}
254261
return null;
@@ -367,9 +374,9 @@ function collectAppGuidedLmstudioModelIds(discovery: LmstudioDiscoveryResult): S
367374
if (entry.type !== "llm" || entry.capabilities?.trained_for_tool_use !== true || !id) {
368375
return [];
369376
}
370-
const effectiveContextWindow = resolveLmstudioEffectiveContextWindow(entry);
371-
return effectiveContextWindow !== null &&
372-
effectiveContextWindow >= LMSTUDIO_APP_GUIDED_MIN_CONTEXT_TOKENS
377+
const loadedContextWindow = resolveLoadedContextWindow(entry);
378+
return loadedContextWindow !== null &&
379+
loadedContextWindow >= LMSTUDIO_APP_GUIDED_MIN_CONTEXT_TOKENS
373380
? [id]
374381
: [];
375382
}),
@@ -399,11 +406,15 @@ async function discoverLmstudioSetupModels(params: {
399406
return { failure };
400407
}
401408
const models = mapLmstudioWireModelsToConfig(discovery.models);
402-
const defaultModelId = selectDefaultLmstudioModelId(models);
409+
const loadedModelIds = collectLoadedLmstudioModelIds(discovery);
410+
const defaultModelId = selectDefaultLmstudioModelId(
411+
models.filter((model) => loadedModelIds.has(model.id)),
412+
);
403413
return {
404414
value: {
405415
discovery,
406416
models,
417+
loadedModelIds,
407418
defaultModel: defaultModelId ? `${PROVIDER_ID}/${defaultModelId}` : undefined,
408419
defaultModelId,
409420
},
@@ -787,18 +798,25 @@ export async function configureLmstudioNonInteractive(
787798
const selectedModel = selectedModelId
788799
? discoveredModels.find((model) => model.id === selectedModelId)
789800
: undefined;
790-
if (!selectedModelId || !selectedModel) {
801+
const selectedModelLoaded =
802+
selectedModelId !== undefined && setupDiscovery.value.loadedModelIds.has(selectedModelId);
803+
if (!selectedModelId || !selectedModel || !selectedModelLoaded) {
791804
const availableModels = discoveredModels.map((model) => model.id).join(", ");
792805
normalizedCtx.runtime.error(
793-
requestedModelId
806+
requestedModelId && selectedModel && !selectedModelLoaded
794807
? [
795-
`LM Studio model ${requestedModelId} was not found at ${baseUrl}.`,
796-
`Available models: ${availableModels}`,
808+
`LM Studio model ${requestedModelId} is installed but not loaded at ${baseUrl}.`,
809+
"Load that model in LM Studio, then re-run setup.",
797810
].join("\n")
798-
: [
799-
`LM Studio did not expose a usable default model at ${baseUrl}.`,
800-
`Available models: ${availableModels || "(none)"}`,
801-
].join("\n"),
811+
: requestedModelId
812+
? [
813+
`LM Studio model ${requestedModelId} was not found at ${baseUrl}.`,
814+
`Available models: ${availableModels}`,
815+
].join("\n")
816+
: [
817+
`LM Studio did not expose a usable default model at ${baseUrl}.`,
818+
`Available models: ${availableModels || "(none)"}`,
819+
].join("\n"),
802820
);
803821
normalizedCtx.runtime.exit(1);
804822
return null;

0 commit comments

Comments
 (0)