Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

LM Studio stays closed: the model list reads its folders, not `lms ls` (#1258)

lingrd812135878760
diff --git a/cli/src/lib/appModels.spec.ts b/cli/src/lib/appModels.spec.ts
index d6646bf5f..7f667fe30 100644
--- a/cli/src/lib/appModels.spec.ts
+++ b/cli/src/lib/appModels.spec.ts
@@ -1,18 +1,17 @@
import { describe, expect, it } from 'vitest'
-import { APP_CONTEXT, appContext, cacheName, llamaServerArgs, scanAppModels } from './appModels.js'
+import { APP_CONTEXT, appContext, appEngineOps, cacheName, llamaServerArgs, lmsKey, scanAppModels } from './appModels.js'
const GiB = 1024 ** 3
const grid = { kind: 'llama.cpp', path: '/home/me/.grid/bin/llama-server', version: 'version: 10369', note: "Grid's own engine" }
const gguf = (over: Record<string, unknown> = {}) => ({ format: 'gguf', bytes: 2 * GiB, quant: 'Q4_K_M',
gguf: { contextLength: 131072, toolCalls: true, kvBytesPerToken: 114688 }, ...over })
-/** `fleet models --json` and `lms ls --json` as the scan reads them; anything else fails. */
-function fake(found: Record<string, unknown>, lms: unknown[] = []) {
+/** `fleet models --json` as the scan reads it; anything else fails. */
+function fake(found: Record<string, unknown>) {
const seen: string[][] = []
const run = async (file: string, args: string[]) => {
seen.push([file, ...args])
if (args.includes('models') && args.includes('--json')) return { ok: true, stdout: JSON.stringify(found), stderr: '' }
- if (args[0] === 'ls') return { ok: true, stdout: JSON.stringify(lms), stderr: '' }
return { ok: false, stdout: '', stderr: 'unexpected' }
}
return { run, seen }
@@ -73,28 +72,84 @@ describe('models other apps downloaded', () => {
])
})
- it("lists LM Studio's models from `lms ls`, the only place that names the key `lms load` takes", async () => {
- const lms = '/home/me/.lmstudio/bin/lms'
- const { run } = fake({
- machine: { canRun: ['llama.cpp', 'mlx-lm', 'ollama', 'lm-studio'], engines: [grid, { kind: 'lm-studio', path: lms, version: null }] },
- // The scan's view of the same files is not used: it knows paths, not keys.
- models: [gguf({ name: 'gemma-4-E2B-it-Q4_K_M', source: 'lm-studio', path: '/s/g.gguf', startWith: { engine: 'lm-studio', label: 'lm-studio' } })],
- }, [
- { type: 'llm', modelKey: 'google/gemma-4-e2b', format: 'gguf', sizeBytes: 4414806160, trainedForToolUse: true, maxContextLength: 131072 },
- { type: 'llm', modelKey: 'mlx-community/qwen3-4b', format: 'mlx', sizeBytes: 2e9, maxContextLength: 262144 },
- { type: 'embedding', modelKey: 'text-embedding-nomic', format: 'gguf', sizeBytes: 84106624, maxContextLength: 2048 },
- { type: 'llm', modelKey: 'old/short', format: 'gguf', sizeBytes: 1e9, maxContextLength: 8192 },
- { type: 'llm', modelKey: 'org/no-tools', format: 'gguf', sizeBytes: 1e9, maxContextLength: 131072, trainedForToolUse: false },
- ])
+ it("lists LM Studio's models from its folders on disk and never runs `lms`, which would start LM Studio", async () => {
+ const lms = '/home/me/.lmstudio/bin/lms', root = '/home/me/.lmstudio/models'
+ const { run, seen } = fake({
+ machine: { canRun: ['llama.cpp', 'mlx-lm', 'ollama', 'lm-studio'], engines: [grid,
+ { kind: 'ollama', path: '/usr/local/bin/ollama', version: '0.32.5' }, { kind: 'lm-studio', path: lms, version: null }] },
+ models: [
+ gguf({ name: 'gemma-4-E2B-it-Q4_K_M', source: 'lm-studio', path: `${root}/google/gemma-4-E2B-it-GGUF/gemma-4-E2B-it-Q4_K_M.gguf`,
+ startWith: { engine: 'lm-studio', label: 'lm-studio' } }),
+ // An MLX folder's window and tool calls are in `config` (the scan reads its config.json), not `gguf`.
+ { name: 'mlx-community/Qwen3-4B-4bit', format: 'mlx', source: 'lm-studio', bytes: 2e9, path: `${root}/mlx-community/Qwen3-4B-4bit`,
+ config: { contextLength: 131072, toolCalls: true, kvBytesPerToken: 73728 }, startWith: { engine: 'lm-studio', label: 'lm-studio' } },
+ { name: 'mlx-community/Short-4bit', format: 'mlx', source: 'lm-studio', bytes: 1e9, path: `${root}/mlx-community/Short-4bit`,
+ config: { contextLength: 32768, toolCalls: true }, startWith: { engine: 'lm-studio', label: 'lm-studio' } },
+ // A file Ollama shares with LM Studio: the scan's Ollama model, with LM Studio's copy beside it.
+ gguf({ name: 'qwen3:8b', source: 'ollama', path: '/m/blobs/sha256-a', startWith: { engine: 'ollama', label: 'ollama' },
+ alsoAt: [{ source: 'lm-studio', path: `${root}/ollama-reuse/qwen3-8b/qwen3-8b.gguf`, name: null }] }),
+ gguf({ name: 'short', source: 'lm-studio', path: `${root}/old/short.gguf`, gguf: { contextLength: 8192, toolCalls: true } }),
+ gguf({ name: 'no-tools', source: 'lm-studio', path: `${root}/org/no-tools.gguf`, gguf: { contextLength: 131072, toolCalls: false } }),
+ ],
+ })
const models = await scanAppModels({ node: '/node', packageDir: '/pkg', env: {}, run })
- expect(models).toEqual([
- { id: 'app:lm-studio:google/gemma-4-e2b', name: 'gemma-4-e2b', app: 'lm-studio', engine: 'lm-studio', ref: 'google/gemma-4-e2b', binary: lms,
- sizeBytes: 4414806160, contextLength: 131072 },
- { id: 'app:lm-studio:mlx-community/qwen3-4b', name: 'qwen3-4b', app: 'lm-studio', engine: 'lm-studio', ref: 'mlx-community/qwen3-4b', binary: lms,
- sizeBytes: 2e9, contextLength: 262144 },
+ expect(seen).toEqual([['/node', '/pkg/toolchain/fleet.mjs', 'models', '--json']])
+ expect(models.filter(m => m.app === 'lm-studio')).toEqual([
+ { id: 'app:lm-studio:gemma-4-E2B-it-Q4_K_M.gguf', name: 'gemma-4-E2B-it-Q4_K_M', app: 'lm-studio', engine: 'lm-studio',
+ ref: `${root}/google/gemma-4-E2B-it-GGUF/gemma-4-E2B-it-Q4_K_M.gguf`, binary: lms,
+ sizeBytes: 2 * GiB, quant: 'Q4_K_M', contextLength: 131072, kvBytesPerToken: 114688 },
+ { id: 'app:lm-studio:Qwen3-4B-4bit', name: 'Qwen3-4B-4bit', app: 'lm-studio', engine: 'lm-studio', ref: `${root}/mlx-community/Qwen3-4B-4bit`,
+ binary: lms, sizeBytes: 2e9, contextLength: 131072, kvBytesPerToken: 73728 },
+ { id: 'app:lm-studio:qwen3-8b.gguf', name: 'qwen3-8b', app: 'lm-studio', engine: 'lm-studio', ref: `${root}/ollama-reuse/qwen3-8b/qwen3-8b.gguf`,
+ binary: lms, sizeBytes: 2 * GiB, quant: 'Q4_K_M', contextLength: 131072, kvBytesPerToken: 114688 },
])
})
+ it("finds LM Studio's key for a file from `lms ls`, wherever its models folder is", () => {
+ const listed = [
+ { modelKey: 'qwen2.5-0.5b-instruct', path: 'Qwen/Qwen2.5-0.5B-Instruct-GGUF/qwen2.5-0.5b-instruct-q8_0.gguf' },
+ { modelKey: 'other', path: 'Qwen2.5-0.5B-Instruct-GGUF/qwen2.5-0.5b-instruct-q8_0.gguf' },
+ { modelKey: 'qwen3-4b', path: 'mlx-community\\Qwen3-4B-4bit' },
+ ]
+ expect(lmsKey(listed, '/Volumes/AI/lms/Qwen/Qwen2.5-0.5B-Instruct-GGUF/qwen2.5-0.5b-instruct-q8_0.gguf')).toBe('qwen2.5-0.5b-instruct')
+ expect(lmsKey(listed, 'C:\\Users\\me\\.lmstudio\\models\\mlx-community\\Qwen3-4B-4bit')).toBe('qwen3-4b')
+ expect(lmsKey(listed, '/home/me/.lmstudio/models/x/Qwen3-4B-4bit')).toBe('')
+ })
+
+ it('starts an LM Studio model by the key `lms ls` gives its file, and says so when LM Studio does not list it', async () => {
+ const lms = '/home/me/.lmstudio/bin/lms', file = '/home/me/.lmstudio/models/google/gemma-4-E2B-it-GGUF/gemma-4-E2B-it-Q4_K_M.gguf'
+ const model = { id: 'app:lm-studio:gemma-4-E2B-it-Q4_K_M.gguf', name: 'gemma-4-E2B-it-Q4_K_M', app: 'lm-studio' as const, engine: 'lm-studio' as const,
+ ref: file, binary: lms, sizeBytes: 2 * GiB }
+ const seen: string[][] = []
+ const run = async (_file: string, args: string[]) => {
+ seen.push(args)
+ if (args[0] === 'ls') return { ok: true, stdout: JSON.stringify([{ modelKey: 'google/gemma-4-e2b', path: 'google/gemma-4-E2B-it-GGUF/gemma-4-E2B-it-Q4_K_M.gguf' }]), stderr: '' }
+ if (args[0] === 'server') return { ok: true, stdout: JSON.stringify({ running: true, port: 1234 }), stderr: '' }
+ return { ok: true, stdout: '', stderr: '' }
+ }
+ const ops = appEngineOps({}, fetch, run)
+ expect(await ops.start(model, 65536, '/tmp/logs')).toMatchObject({ engine: 'lm-studio', served: model.name, port: 1234 })
+ expect(seen.find(args => args[0] === 'load')?.slice(0, 2)).toEqual(['load', 'google/gemma-4-e2b'])
+ await expect(ops.start({ ...model, ref: '/elsewhere/gone.gguf' }, 65536, '/tmp/logs')).rejects.toThrow('LM Studio does not list this model')
+ })
+
+ it('leaves a quit LM Studio closed on Stop, and still unloads from one that is only slow', async () => {
+ const record = { spec: 1 as const, modelId: 'app:lm-studio:m.gguf', grid: 'g', engine: 'lm-studio' as const, name: 'm',
+ served: 'm', alias: 'm', port: 1234, binary: '/home/me/.lmstudio/bin/lms', startedServer: true }
+ const seen: string[][] = []
+ const run = async (_file: string, args: string[]) => { seen.push(args); return { ok: true, stdout: '', stderr: '' } }
+ const fails = (error: unknown) => (async () => { throw error }) as unknown as typeof fetch
+ // Nothing listens: LM Studio was quit (undici's shape, and a bare socket error's).
+ await appEngineOps({}, fails(Object.assign(new TypeError('fetch failed'), { cause: { code: 'ECONNREFUSED' } })), run).stop(record)
+ await appEngineOps({}, fails(Object.assign(new Error('connect'), { code: 'ECONNREFUSED' })), run).stop(record)
+ expect(seen).toEqual([])
+ // Up and answering, or busy past the timeout: the model is still loaded there.
+ const open = (async () => new Response(JSON.stringify({ data: [{ id: 'm', state: 'loaded' }] }))) as unknown as typeof fetch
+ await appEngineOps({}, open, run).stop(record)
+ await appEngineOps({}, fails(new DOMException('timed out', 'TimeoutError')), run).stop(record)
+ expect(seen).toEqual([['unload', 'm'], ['server', 'stop'], ['unload', 'm'], ['server', 'stop']])
+ })
+
it('is no models, never an error, without the Model Manager or with a scan that fails', async () => {
expect(await scanAppModels({ node: '/node', packageDir: null, env: {} })).toEqual([])
const failing = async () => ({ ok: false, stdout: '', stderr: 'boom' })
diff --git a/cli/src/lib/appModels.ts b/cli/src/lib/appModels.ts
index 538921989..1ce25044d 100644
--- a/cli/src/lib/appModels.ts
+++ b/cli/src/lib/appModels.ts
@@ -22,7 +22,8 @@ export interface AppModel {
id: string; name: string; app: AppEngine
/** What starts it: its own app, or Grid's llama.cpp (`grid`) when that app is not installed here. */
engine: AppEngine | 'grid'
- /** Ollama: `name:tag`. LM Studio: its model key. llama.cpp and `grid`: the weights file. */
+ /** Ollama: `name:tag`. LM Studio: its file (an MLX model's folder), whose key Start looks up. llama.cpp and
+ * `grid`: the weights file. */
ref: string
/** The app's own binary (`ollama`, `lms`, `llama-server`); absent for `grid`. */
binary?: string
@@ -57,10 +58,9 @@ export function cacheName(name: string): string {
}
/**
- * The picker's models from other apps: Ollama's and llama.cpp's from the Model Manager's own scan
- * (`fleet models --json`, whose `startWith` is the rule), LM Studio's from `lms ls`, the only place that
- * names the key `lms load` takes. Only what a coding agent can run: 64K or more, tool calls not ruled out,
- * and a file the engine reads. A scan that fails is no models, never an error over the whole picker.
+ * The picker's models from other apps, all from the Model Manager's own scan of the disk (`fleet models
+ * --json`, whose `startWith` is the rule). Only what a coding agent can run: 64K or more, tool calls not
+ * ruled out, and a file the engine reads. A scan that fails is no models, never an error over the whole picker.
*/
export async function scanAppModels({ node, packageDir, env, run = exec }: {
node: string; packageDir: string | null; env: NodeJS.ProcessEnv; run?: Exec
@@ -100,25 +100,48 @@ export async function scanAppModels({ node, packageDir, env, run = exec }: {
}
if (lmsUsable) {
- const listed = await run(lms, ['ls', '--json'], { env, timeout: 30_000 })
- let models: Record<string, any>[] = []
- try { models = listed.ok ? rows(JSON.parse(listed.stdout)) : [] } catch { /* none */ }
+ // LM Studio's models from its folders on disk, never from `lms ls`: every `lms` command starts LM Studio's
+ // background app, and the TUI reads this list every minute, so LM Studio came back a minute after the
+ // person quit it (2026-10-07, traced to `lms ls --json` run by harnessd-core). A file Ollama shares with
+ // LM Studio is the scan's Ollama model with LM Studio's copy in `alsoAt`, listed here as LM Studio's too.
const canRun = Array.isArray(obj(found.machine).canRun) ? obj(found.machine).canRun as string[] : []
- for (const m of models) {
- const modelKey = str(m.modelKey)
- if (m.type !== 'llm' || !safe(modelKey) || m.trainedForToolUse === false) continue
+ const ids = new Set<string>()
+ for (const m of rows(found.models)) {
+ if (m.missingFiles || m.error) continue
if (!(m.format === 'gguf' || (m.format === 'mlx' && canRun.includes('lm-studio')))) continue
- const context = num(m.maxContextLength)
- if (context !== undefined && context < MIN_APP_CONTEXT) continue
- // The key's last part is what the grid lists it as: an API name, where the display name has spaces.
- const name = modelKey.split('/').pop()!
- result.push({ id: `app:lm-studio:${modelKey}`, name, app: 'lm-studio', engine: 'lm-studio', ref: modelKey, binary: lms,
- sizeBytes: num(m.sizeBytes) ?? 0, ...(context ? { contextLength: context } : {}) })
+ // An MLX folder's window, tool calls and cache per token are in its `config`, a GGUF's in `gguf`.
+ const meta = obj(m.format === 'mlx' ? m.config : m.gguf)
+ const context = num(meta.contextLength)
+ if ((context !== undefined && context < MIN_APP_CONTEXT) || meta.toolCalls === false) continue
+ const copies = [...(m.source === 'lm-studio' ? [str(m.path)] : []), ...rows(m.alsoAt).filter(a => a.source === 'lm-studio').map(a => str(a.path))]
+ for (const file of copies) {
+ // The file's own name is what the grid lists it as: an API name, where a display name has spaces.
+ const id = `app:lm-studio:${basename(file)}`, name = basename(file).replace(/\.gguf$/i, '')
+ if (!file || ids.has(id) || !safe(name) || !safe(file)) continue
+ ids.add(id)
+ result.push({ id, name, app: 'lm-studio', engine: 'lm-studio', ref: file, binary: lms,
+ sizeBytes: num(m.bytes) ?? 0, ...(str(m.quant) ? { quant: str(m.quant) } : {}),
+ ...(context ? { contextLength: context } : {}), ...(num(meta.kvBytesPerToken) ? { kvBytesPerToken: num(meta.kvBytesPerToken) } : {}) })
+ }
}
}
return result
}
+/** LM Studio's key for [file], the one `lms load` takes: the model `lms ls --json` lists at that path. Its
+ * paths are inside whichever models folder LM Studio uses (the person can move it), so the longest that
+ * ends [file] wins. Empty when LM Studio does not list the file. */
+export function lmsKey(listed: Record<string, any>[], file: string): string {
+ const slash = (path: string) => path.replace(/\\/g, '/')
+ const target = slash(file)
+ let best = { key: '', length: 0 }
+ for (const m of listed) {
+ const path = slash(str(m.path)).replace(/^\/+/, ''), key = str(m.modelKey)
+ if (path && safe(key) && (target === path || target.endsWith(`/${path}`)) && path.length > best.length) best = { key, length: path.length }
+ }
+ return best.key
+}
+
/** The window to start [model] with: [APP_CONTEXT] when it holds that, else 64K, within [budget] bytes of
* memory when the model's cache per token is known. Null when not even 64K fits. */
export function appContext(model: Pick<AppModel, 'contextLength' | 'kvBytesPerToken' | 'sizeBytes'>, budget: number | undefined): number | null {
@@ -168,6 +191,13 @@ export function appEngineOps(env: NodeJS.ProcessEnv, request: typeof fetch = fet
return { status: response.status, body: obj(await response.json().catch(() => ({}))) }
} catch { return { status: 0, body: {} } }
}
+ /** Whether nothing listens at [url]: the connection is refused. A timeout or any answer is not that. */
+ const refused = async (url: string): Promise<boolean> => {
+ try { await request(url, { signal: AbortSignal.timeout(5_000), redirect: 'error' }); return false } catch (error) {
+ const cause = obj(obj(error).cause)
+ return cause.code === 'ECONNREFUSED' || obj(error).code === 'ECONNREFUSED'
+ }
+ }
/** Up when [url] answers 200; a 503 is a model still loading. Gives up early if the process exits. */
const ready = async (url: string, ms: number, pid?: number): Promise<boolean> => {
for (const deadline = Date.now() + ms; Date.now() < deadline; await sleep(500)) {
@@ -219,8 +249,13 @@ export function appEngineOps(env: NodeJS.ProcessEnv, request: typeof fetch = fet
}
return { engine: 'llama.cpp', served: model.name, alias: model.name, port, binary, pid }
}
- // LM Studio: its own background server, started when it is off, and the model loaded with the window
+ // LM Studio: its key for the file, asked only now — `lms` starts LM Studio, which is what Start asks
+ // for — then its own background server, started when it is off, and the model loaded with the window
// given here — a model it loads on demand gets 8K [run].
+ const listed = await run(binary, ['ls', '--json'], { env, timeout: 30_000 })
+ let key = ''
+ try { key = lmsKey(rows(JSON.parse(listed.stdout)), model.ref) } catch { /* none */ }
+ if (!key) throw new AppStartError('LM Studio does not list this model yet. Open LM Studio once, then try again.')
let status = await lmsJson(binary, ['server', 'status', '--json'])
let startedServer = false
if (status.running !== true) {
@@ -230,7 +265,7 @@ export function appEngineOps(env: NodeJS.ProcessEnv, request: typeof fetch = fet
}
const port = typeof status.port === 'number' ? status.port : 0
if (!port) throw new AppStartError('LM Studio could not start its server. Open LM Studio and try again.')
- const loaded = await run(binary, ['load', model.ref, '--context-length', String(ctx), '--gpu', 'max', '--parallel', '1',
+ const loaded = await run(binary, ['load', key, '--context-length', String(ctx), '--gpu', 'max', '--parallel', '1',
'--identifier', model.name, '-y'], { env, timeout: 10 * 60_000 })
if (!loaded.ok) {
if (startedServer) await run(binary, ['server', 'stop'], { env, timeout: 30_000 })
@@ -258,6 +293,10 @@ export function appEngineOps(env: NodeJS.ProcessEnv, request: typeof fetch = fet
async stop(record) {
if (record.engine === 'lm-studio') {
+ // Every `lms` command starts LM Studio. One the person already quit has nothing loaded to unload, and
+ // is not woken to be told so. Only a refused connection says that: a server slow to answer still has
+ // the model, and this record is the last thing that can unload it.
+ if (await refused(`http://127.0.0.1:${record.port}/api/v0/models`)) return
await run(record.binary, ['unload', record.served], { env, timeout: 60_000 })
if (record.startedServer) await run(record.binary, ['server', 'stop'], { env, timeout: 30_000 })
return