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.

feat(models): list and start models other apps downloaded, in the app that holds them

bitcoinbuilderd6f23807e27a
diff --git a/cli/src/backendSocket.ts b/cli/src/backendSocket.ts
index feb777213..6781013be 100644
--- a/cli/src/backendSocket.ts
+++ b/cli/src/backendSocket.ts
@@ -30,13 +30,14 @@ import { AuthSessionManager, AuthSessionError } from './lib/authSession.js'
import { VERSION } from './version.js'
import { registry, projectDisplayName, type RegisteredSession } from './lib/registry.js'
import { AgentStopError } from './lib/stopAgentService.js'
-import { isHiddenBuiltin } from './dsh/builtins.js'
+import { MODEL_MANAGER_ID, isHiddenBuiltin } from './dsh/builtins.js'
import { ENGINES, PROCESS_ENGINES, isTerminalEngine, type AgentEngine, type ProcessEngine } from './engines/types.js'
import { listDir } from './lib/fsBrowse.js'
import { linkCodexProfile, listCodexProfiles } from './lib/codexProfiles.js'
import { gridCliPresence } from './lib/gridExec.js'
import { GridFleetRpc, GRID_FLEET_PROTOCOL, GRID_FLEET_MAX_TIMEOUT_MS, parseGridFleetRequest } from './lib/gridFleetRpc.js'
import { LocalModels } from './lib/localModels.js'
+import { appEngineOps, scanAppModels } from './lib/appModels.js'
import { ApiConnectionError, ApiConnections, apiConnectionsRequest } from './lib/apiConnections.js'
import { apiModelsRequest, rememberSavedApis, resolveApiTarget } from './lib/apiModels.js'
import { gridCapableEngines, isApiLaunch, parseGridLaunchOverride, type GridLaunchOverride } from './lib/gridLaunch.js'
@@ -438,6 +439,10 @@ export class BackendSocket {
machineName: () => this.machineDisplayName,
inventory: gridInventory,
onChanged: () => { forgetGridModels(); void this.pushGridModels() },
+ // Models Ollama, LM Studio and llama.cpp downloaded here, found by the Model Manager's own scan (the
+ // bundled harness), so the picker and that harness agree on what is here and what starts it.
+ appModels: () => scanAppModels({ node: process.execPath, packageDir: installedDsh(MODEL_MANAGER_ID)?.realDir ?? null, env: process.env }),
+ appEngines: appEngineOps(process.env),
})
/** This machine's name as Harness shows it (Machines), from the backend's `machine_meta`. Null
* until the first one arrives. */
diff --git a/cli/src/lib/appModels.spec.ts b/cli/src/lib/appModels.spec.ts
new file mode 100644
index 000000000..8d3035547
--- /dev/null
+++ b/cli/src/lib/appModels.spec.ts
@@ -0,0 +1,119 @@
+import { describe, expect, it } from 'vitest'
+import { APP_CONTEXT, appContext, cacheName, 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[] = []) {
+ 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 }
+}
+
+describe('models other apps downloaded', () => {
+ it("lists Ollama's and llama.cpp's from the Model Manager's scan, each started by its own app", async () => {
+ 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: 'llama.cpp', path: '/opt/homebrew/bin/llama-server', version: 'version: 9000' }] },
+ models: [
+ gguf({ name: 'llama3.2:3b', source: 'ollama', path: '/m/blobs/sha256-a', realPath: '/m/blobs/sha256-a', startWith: { engine: 'ollama', label: 'ollama', running: false } }),
+ gguf({ name: 'ggml-org_gemma-4-E2B-it-GGUF_gemma-4-E2B-it-Q4_K_M', source: 'llama.cpp', path: '/c/ggml-org_gemma-4-E2B-it-GGUF_gemma-4-E2B-it-Q4_K_M.gguf',
+ startWith: { engine: 'llama.cpp', label: 'your llama.cpp', path: '/opt/homebrew/bin/llama-server' } }),
+ // Newer llama.cpp downloads into the Hugging Face cache: with llama.cpp here, that file is its.
+ gguf({ name: 'unsloth/Qwen3-4B-Instruct-2507-GGUF', source: 'huggingface',
+ path: '/h/models--unsloth--Qwen3-4B-Instruct-2507-GGUF/snapshots/a0/Qwen3-4B-Instruct-2507-Q4_K_M.gguf',
+ startWith: { engine: 'llama.cpp', label: 'your llama.cpp', path: '/opt/homebrew/bin/llama-server' } }),
+ // Grid's own folder is listed by the catalog path already; the Hugging Face cache and folders are not an app's.
+ gguf({ name: 'Qwen3.6-35B', source: 'grid', path: '/g/Qwen.gguf', startWith: { engine: 'llama.cpp', label: "Grid's llama.cpp" } }),
+ gguf({ name: 'Loose', source: 'folder', path: '/d/Loose.gguf', startWith: { engine: 'llama.cpp', label: "Grid's llama.cpp" } }),
+ ],
+ })
+ const models = await scanAppModels({ node: '/node', packageDir: '/pkg', env: {}, run })
+ expect(seen[0]).toEqual(['/node', '/pkg/toolchain/fleet.mjs', 'models', '--json'])
+ expect(models).toEqual([
+ { id: 'app:ollama:llama3.2:3b', name: 'llama3.2:3b', app: 'ollama', engine: 'ollama', ref: 'llama3.2:3b', binary: '/usr/local/bin/ollama',
+ sizeBytes: 2 * GiB, quant: 'Q4_K_M', contextLength: 131072, kvBytesPerToken: 114688 },
+ { id: 'app:llama.cpp:ggml-org_gemma-4-E2B-it-GGUF_gemma-4-E2B-it-Q4_K_M.gguf', name: 'gemma-4-E2B-it-Q4_K_M', app: 'llama.cpp', engine: 'llama.cpp',
+ ref: '/c/ggml-org_gemma-4-E2B-it-GGUF_gemma-4-E2B-it-Q4_K_M.gguf', binary: '/opt/homebrew/bin/llama-server',
+ sizeBytes: 2 * GiB, quant: 'Q4_K_M', contextLength: 131072, kvBytesPerToken: 114688 },
+ { id: 'app:llama.cpp:Qwen3-4B-Instruct-2507-Q4_K_M.gguf', name: 'Qwen3-4B-Instruct-2507-Q4_K_M', app: 'llama.cpp', engine: 'llama.cpp',
+ ref: '/h/models--unsloth--Qwen3-4B-Instruct-2507-GGUF/snapshots/a0/Qwen3-4B-Instruct-2507-Q4_K_M.gguf', binary: '/opt/homebrew/bin/llama-server',
+ sizeBytes: 2 * GiB, quant: 'Q4_K_M', contextLength: 131072, kvBytesPerToken: 114688 },
+ ])
+ })
+
+ it("falls back to Grid's llama.cpp only when the app is not installed, and only for what a coding agent can run", async () => {
+ const { run } = fake({
+ machine: { canRun: ['llama.cpp'], engines: [grid, { kind: 'lm-studio', path: '/Applications/LM Studio.app', version: null }] },
+ models: [
+ gguf({ name: 'qwen3:8b', source: 'ollama', path: '/m/blobs/sha256-b', startWith: { engine: 'llama.cpp', label: "Grid's llama.cpp" } }),
+ gguf({ name: 'Studio-Q4_K_M', source: 'lm-studio', path: '/s/Studio-Q4_K_M.gguf', startWith: { engine: 'llama.cpp', label: "Grid's llama.cpp" } }),
+ // Without llama.cpp, a Hugging Face cache GGUF is nobody's app download: not listed.
+ gguf({ name: 'org/Cached-GGUF', source: 'huggingface', path: '/h/Cached-Q4_K_M.gguf', startWith: { engine: 'llama.cpp', label: "Grid's llama.cpp" } }),
+ gguf({ name: 'short:1b', source: 'ollama', path: '/m/blobs/sha256-c', gguf: { contextLength: 32768, toolCalls: true }, startWith: { engine: 'llama.cpp' } }),
+ gguf({ name: 'chat-only:1b', source: 'ollama', path: '/m/blobs/sha256-d', gguf: { contextLength: 131072, toolCalls: false }, startWith: { engine: 'llama.cpp' } }),
+ gguf({ name: 'half:7b', source: 'ollama', path: '/m/blobs/sha256-e', missingFiles: 1, startWith: { engine: 'llama.cpp' } }),
+ gguf({ name: 'new-arch:7b', source: 'ollama', path: '/m/blobs/sha256-f', gguf: { contextLength: 131072, unsupportedTensorTypes: [143] }, startWith: { engine: 'llama.cpp' } }),
+ ],
+ })
+ const models = await scanAppModels({ node: '/node', packageDir: '/pkg', env: {}, run })
+ expect(models.map(m => [m.name, m.app, m.engine, m.ref])).toEqual([
+ ['qwen3:8b', 'ollama', 'grid', '/m/blobs/sha256-b'],
+ // LM Studio's app is there but has no `lms` yet: its GGUF is still a file Grid's engine reads.
+ ['Studio-Q4_K_M', 'lm-studio', 'grid', '/s/Studio-Q4_K_M.gguf'],
+ ])
+ })
+
+ 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 },
+ ])
+ 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 },
+ ])
+ })
+
+ 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' })
+ expect(await scanAppModels({ node: '/node', packageDir: '/pkg', env: {}, run: failing })).toEqual([])
+ const garbled = async () => ({ ok: true, stdout: 'not json', stderr: '' })
+ expect(await scanAppModels({ node: '/node', packageDir: '/pkg', env: {}, run: garbled })).toEqual([])
+ })
+
+ it('gives 128K when it fits beside the weights, 64K when only that does, and nothing below', () => {
+ const model = { sizeBytes: 2 * GiB, contextLength: 131072, kvBytesPerToken: 112 * 1024 }
+ expect(appContext(model, 54 * GiB)).toBe(APP_CONTEXT)
+ expect(appContext(model, 2.2 * GiB + 2 * GiB + 8 * GiB)).toBe(65536)
+ expect(appContext(model, 4 * GiB)).toBeNull()
+ expect(appContext({ ...model, contextLength: 65536 }, 54 * GiB)).toBe(65536)
+ expect(appContext({ sizeBytes: 2 * GiB }, undefined)).toBe(APP_CONTEXT)
+ })
+
+ it("reads llama.cpp's download names as the file's own", () => {
+ expect(cacheName('ggml-org_gemma-4-E2B-it-GGUF_gemma-4-E2B-it-Q4_K_M.gguf')).toBe('gemma-4-E2B-it-Q4_K_M')
+ expect(cacheName('plain-model-Q4_K_M')).toBe('plain-model-Q4_K_M')
+ })
+})
diff --git a/cli/src/lib/appModels.ts b/cli/src/lib/appModels.ts
new file mode 100644
index 000000000..c33609652
--- /dev/null
+++ b/cli/src/lib/appModels.ts
@@ -0,0 +1,280 @@
+/** Models other apps on this computer already downloaded — Ollama, LM Studio, llama.cpp — offered in the
+ * Models picker and started with the app that holds them, the rule the Model Manager reads as START WITH:
+ * that app downloaded the file and is known to load it, where Grid's engine can be older and refuse a new
+ * architecture. Grid's llama.cpp starts one only when its app is not installed here. */
+import { execFile, spawn } from 'node:child_process'
+import { closeSync, openSync } from 'node:fs'
+import { mkdir, readFile, rename, writeFile } from 'node:fs/promises'
+import { createServer, type AddressInfo } from 'node:net'
+import { basename, join } from 'node:path'
+import { processExists } from './processLiveness.js'
+
+export type AppEngine = 'ollama' | 'lm-studio' | 'llama.cpp'
+export const APP_LABEL: Record<AppEngine, string> = { ollama: 'Ollama', 'lm-studio': 'LM Studio', 'llama.cpp': 'llama.cpp' }
+/** What a model Grid's own llama.cpp serves runs in, named like the apps beside it. */
+export const GRID_LABEL = 'Grid'
+export const MIN_APP_CONTEXT = 64 * 1024
+/** The window a model is given when it can hold more: an agent's need, and what the Model Manager gives. */
+export const APP_CONTEXT = 128 * 1024
+
+export interface AppModel {
+ /** `app:<app>:<what that app calls it>` — stable across reads, so an operation keeps its row. */
+ 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. */
+ ref: string
+ /** The app's own binary (`ollama`, `lms`, `llama-server`); absent for `grid`. */
+ binary?: string
+ sizeBytes: number; quant?: string; contextLength?: number; kvBytesPerToken?: number
+}
+
+/** One engine this daemon started for a picker model, kept on disk so Stop and the list outlive a restart. */
+export interface AppEngineRecord {
+ spec: 1; modelId: string; grid: string; engine: AppEngine; name: string
+ /** What the engine itself calls the model, and the name the grid lists it under. */
+ served: string; alias: string
+ port: number; binary: string; pid?: number
+ /** LM Studio's server was off and this started it, so Stop turns it off again. */
+ startedServer?: boolean
+}
+
+type Exec = (file: string, args: string[], options?: { env?: NodeJS.ProcessEnv; timeout?: number }) => Promise<{ ok: boolean; stdout: string; stderr: string }>
+const exec: Exec = (file, args, options = {}) => new Promise(resolve => {
+ execFile(file, args, { env: options.env, timeout: options.timeout ?? 60_000, maxBuffer: 16 * 1024 * 1024 },
+ (error, stdout, stderr) => resolve({ ok: !error, stdout: String(stdout), stderr: String(stderr) }))
+})
+
+const obj = (v: unknown): Record<string, any> => v && typeof v === 'object' && !Array.isArray(v) ? v : {}
+const rows = (v: unknown): Record<string, any>[] => Array.isArray(v) ? v.map(obj) : []
+const str = (v: unknown): string => typeof v === 'string' ? v : ''
+const num = (v: unknown): number | undefined => typeof v === 'number' && Number.isFinite(v) && v > 0 ? v : undefined
+const safe = (v: string): boolean => !!v && !v.startsWith('-') && !/[\x00-\x1f]/.test(v)
+
+/** `org_repo-GGUF_file.gguf`, the name `llama-server -hf` gives a download, read as the file's own name. */
+export function cacheName(name: string): string {
+ return name.replace(/\.gguf$/i, '').replace(/^[^_]+_.+?-GGUF_/i, '')
+}
+
+/**
+ * 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.
+ */
+export async function scanAppModels({ node, packageDir, env, run = exec }: {
+ node: string; packageDir: string | null; env: NodeJS.ProcessEnv; run?: Exec
+}): Promise<AppModel[]> {
+ if (!packageDir) return []
+ const scan = await run(node, [join(packageDir, 'toolchain', 'fleet.mjs'), 'models', '--json'], { env, timeout: 60_000 })
+ if (!scan.ok) return []
+ let found: Record<string, any>
+ try { found = obj(JSON.parse(scan.stdout)) } catch { return [] }
+ const engines = rows(obj(found.machine).engines)
+ const installed = (kind: string) => engines.find(e => e.kind === kind && str(e.path))
+ const lms = str(installed('lm-studio')?.path)
+ const lmsUsable = !!lms && !lms.endsWith('.app')
+ const result: AppModel[] = []
+
+ for (const m of rows(found.models)) {
+ const gguf = obj(m.gguf), start = obj(m.startWith)
+ // A GGUF in the Hugging Face cache is llama.cpp's own download in its newer builds: listed as llama.cpp's
+ // when the person has llama.cpp to start it with, and not at all otherwise (not an app's folder then).
+ const app = (m.source === 'huggingface' && start.label === 'your llama.cpp' ? 'llama.cpp' : str(m.source)) as AppEngine
+ if (!(app === 'ollama' || app === 'llama.cpp' || (app === 'lm-studio' && !lmsUsable))) continue
+ if (m.format !== 'gguf' || m.missingFiles || m.error || !str(start.engine)) continue
+ const context = num(gguf.contextLength)
+ if ((context !== undefined && context < MIN_APP_CONTEXT) || gguf.toolCalls === false) continue
+ let engine: AppModel['engine'] | null = null, binary: string | undefined
+ if (app === 'ollama' && start.engine === 'ollama') { engine = 'ollama'; binary = str(installed('ollama')?.path) }
+ else if (app === 'llama.cpp' && start.label === 'your llama.cpp' && str(start.path)) { engine = 'llama.cpp'; binary = str(start.path) }
+ else if (start.engine === 'llama.cpp' && !(Array.isArray(gguf.unsupportedTensorTypes) && gguf.unsupportedTensorTypes.length)) engine = 'grid'
+ if (!engine || (engine !== 'grid' && !binary)) continue
+ const file = str(m.realPath) || str(m.path)
+ const name = app === 'llama.cpp' ? cacheName(basename(str(m.path))) : str(m.name)
+ const ref = app === 'ollama' && engine === 'ollama' ? str(m.name) : file
+ if (!safe(name) || !safe(ref)) continue
+ result.push({ id: `app:${app}:${app === 'ollama' ? str(m.name) : basename(str(m.path))}`, name, app, engine, ref,
+ ...(binary ? { binary } : {}), sizeBytes: num(m.bytes) ?? 0, ...(str(m.quant) ? { quant: str(m.quant) } : {}),
+ ...(context ? { contextLength: context } : {}), ...(num(gguf.kvBytesPerToken) ? { kvBytesPerToken: num(gguf.kvBytesPerToken) } : {}) })
+ }
+
+ 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 */ }
+ 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
+ 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 } : {}) })
+ }
+ }
+ return result
+}
+
+/** 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 {
+ const most = Math.min(model.contextLength ?? APP_CONTEXT, APP_CONTEXT)
+ for (const ctx of [...new Set([most, MIN_APP_CONTEXT])].filter(c => c >= MIN_APP_CONTEXT)) {
+ const cache = model.kvBytesPerToken ? model.kvBytesPerToken * ctx : 0
+ if (budget === undefined || model.sizeBytes * 1.1 + cache + 2 * 1024 ** 3 <= budget) return ctx
+ }
+ return null
+}
+
+function freePort(): Promise<number> {
+ return new Promise((resolve, reject) => {
+ const server = createServer()
+ server.once('error', reject)
+ server.listen(0, '127.0.0.1', () => {
+ const { port } = server.address() as AddressInfo
+ server.close(() => resolve(port))
+ })
+ })
+}
+
+const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
+
+/** Starting, checking and stopping the engines, injected so the lifecycle can be tested without them. */
+export interface AppEngineOps {
+ start(model: AppModel, ctx: number, logDir: string): Promise<Omit<AppEngineRecord, 'spec' | 'modelId' | 'grid' | 'name'>>
+ alive(record: AppEngineRecord): Promise<boolean>
+ /** The window the engine says it loaded, after a first request; undefined when it does not say. */
+ loadedContext(record: AppEngineRecord): Promise<number | undefined>
+ stop(record: AppEngineRecord): Promise<void>
+}
+
+export class AppStartError extends Error {}
+
+export function appEngineOps(env: NodeJS.ProcessEnv, request: typeof fetch = fetch, run: Exec = exec): AppEngineOps {
+ const get = async (url: string): Promise<{ status: number; body: Record<string, any> }> => {
+ try {
+ const response = await request(url, { signal: AbortSignal.timeout(5_000), redirect: 'error' })
+ return { status: response.status, body: obj(await response.json().catch(() => ({}))) }
+ } catch { return { status: 0, body: {} } }
+ }
+ /** 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)) {
+ if ((await get(url)).status === 200) return true
+ if (pid !== undefined && !processExists(pid)) return false
+ }
+ return false
+ }
+ const background = async (binary: string, args: string[], extra: NodeJS.ProcessEnv, logDir: string, logName: string): Promise<number> => {
+ await mkdir(logDir, { recursive: true, mode: 0o700 })
+ const log = openSync(join(logDir, logName), 'a', 0o600)
+ try {
+ // Its own process group, so it outlives this daemon and Stop ends it with whatever it started.
+ const child = spawn(binary, args, { env: { ...env, ...extra }, detached: true, stdio: ['ignore', log, log] })
+ child.unref()
+ if (!child.pid) throw new AppStartError('The model could not start. Try again.')
+ return child.pid
+ } finally { closeSync(log) }
+ }
+ const lmsJson = async (binary: string, args: string[], timeout = 30_000): Promise<Record<string, any>> => {
+ const result = await run(binary, args, { env, timeout })
+ try { return obj(JSON.parse(result.stdout)) } catch { return {} }
+ }
+ /** The command a pid runs now, so a pid the system reused is never stopped as ours. */
+ const ours = async (record: AppEngineRecord): Promise<boolean> => {
+ if (!record.pid || !processExists(record.pid)) return false
+ const command = await run('ps', ['-o', 'command=', '-p', String(record.pid)], { timeout: 5_000 })
+ return command.ok && command.stdout.includes(basename(record.binary))
+ }
+
+ return {
+ async start(model, ctx, logDir) {
+ const binary = model.binary!
+ if (model.engine === 'ollama') {
+ // Its own server beside the person's Ollama app, never on the app's 11434: the window is a server
+ // setting, and theirs ran every model at 16K [run]. Same model store, so nothing is copied.
+ const port = await freePort()
+ const pid = await background(binary, ['serve'], { OLLAMA_HOST: `127.0.0.1:${port}`, OLLAMA_CONTEXT_LENGTH: String(ctx),
+ OLLAMA_NUM_PARALLEL: '1', OLLAMA_KEEP_ALIVE: '-1' }, logDir, `ollama-${port}.log`)
+ if (!await ready(`http://127.0.0.1:${port}/api/version`, 60_000, pid)) throw new AppStartError('Ollama could not start. Try again.')
+ return { engine: 'ollama', served: model.ref, alias: model.name, port, binary, pid }
+ }
+ if (model.engine === 'llama.cpp') {
+ const port = await freePort()
+ const pid = await background(binary, ['-m', model.ref, '--alias', model.name, '--ctx-size', String(ctx), '--parallel', '1',
+ '-ngl', '999', '--host', '127.0.0.1', '--port', String(port)], {}, logDir, `llama-${port}.log`)
+ if (!await ready(`http://127.0.0.1:${port}/health`, 10 * 60_000, pid)) {
+ try { process.kill(-pid, 'SIGTERM') } catch { /* gone */ }
+ throw new AppStartError('llama.cpp could not load the model. Try again.')
+ }
+ 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
+ // given here — a model it loads on demand gets 8K [run].
+ let status = await lmsJson(binary, ['server', 'status', '--json'])
+ let startedServer = false
+ if (status.running !== true) {
+ await run(binary, ['server', 'start'], { env, timeout: 60_000 })
+ status = await lmsJson(binary, ['server', 'status', '--json'])
+ startedServer = status.running === true
+ }
+ 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',
+ '--identifier', model.name, '-y'], { env, timeout: 10 * 60_000 })
+ if (!loaded.ok) {
+ if (startedServer) await run(binary, ['server', 'stop'], { env, timeout: 30_000 })
+ throw new AppStartError('LM Studio could not load the model. Close some apps, or choose a smaller model.')
+ }
+ return { engine: 'lm-studio', served: model.name, alias: model.name, port, binary, startedServer }
+ },
+
+ async alive(record) {
+ if (record.engine !== 'lm-studio') return ours(record)
+ const loaded = rows((await get(`http://127.0.0.1:${record.port}/api/v0/models`)).body.data)
+ return loaded.some(m => m.id === record.served && m.state === 'loaded')
+ },
+
+ async loadedContext(record) {
+ const base = `http://127.0.0.1:${record.port}`
+ if (record.engine === 'ollama') {
+ const tagged = (name: string) => name.includes(':') ? name : `${name}:latest`
+ const model = rows((await get(`${base}/api/ps`)).body.models).find(m => [m.name, m.model].some(n => tagged(str(n)) === tagged(record.served)))
+ return num(model?.context_length)
+ }
+ if (record.engine === 'lm-studio') return num(rows((await get(`${base}/api/v0/models`)).body.data).find(m => m.id === record.served)?.loaded_context_length)
+ return num(obj((await get(`${base}/props`)).body.default_generation_settings).n_ctx)
+ },
+
+ async stop(record) {
+ if (record.engine === 'lm-studio') {
+ 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
+ }
+ if (!await ours(record)) return
+ try { process.kill(-record.pid!, 'SIGTERM') } catch { /* gone */ }
+ for (let i = 0; i < 20 && processExists(record.pid!); i++) await sleep(500)
+ if (processExists(record.pid!)) { try { process.kill(-record.pid!, 'SIGKILL') } catch { /* gone */ } }
+ },
+ }
+}
+
+/** The engines this daemon started, by model id. */
+export async function readAppRecords(file: string): Promise<AppEngineRecord[]> {
+ try {
+ return rows(JSON.parse(await readFile(file, 'utf8'))).filter(r => r.spec === 1 && str(r.modelId) && str(r.grid) &&
+ ['ollama', 'lm-studio', 'llama.cpp'].includes(r.engine) && typeof r.port === 'number' && safe(str(r.alias))) as AppEngineRecord[]
+ } catch { return [] }
+}
+
+export async function writeAppRecords(file: string, records: AppEngineRecord[]): Promise<void> {
+ await mkdir(join(file, '..'), { recursive: true, mode: 0o700 })
+ const temp = `${file}.${process.pid}.${Date.now()}.tmp`
+ await writeFile(temp, JSON.stringify(records), { mode: 0o600 })
+ await rename(temp, file)
+}
diff --git a/cli/src/lib/gridModels.ts b/cli/src/lib/gridModels.ts
index fa67a13a6..9966f351e 100644
--- a/cli/src/lib/gridModels.ts
+++ b/cli/src/lib/gridModels.ts
@@ -27,7 +27,7 @@ import { gridCredentialsPath } from './gridCredentials.js'
import { signedInGridEmail } from './gridDerive.js'
import { gridJson } from './gridExec.js'
import {
- emptyPicture, mergeAwake, idKey, parsePicture, provenStopped, sectionView, servedKey, servesAModel, unspelled, withAsleep,
+ advertisedNow, emptyPicture, mergeAwake, idKey, parsePicture, provenStopped, sectionView, servedKey, servesAModel, unspelled, withAsleep,
withSpellings, withUnknown, withWindows, type GridPicture, type LocalRecord, type PictureState, type RowUnavailable,
type SectionView, type ServedHere,
} from './gridPicture.js'
@@ -438,11 +438,12 @@ export class GridModelsService {
const isMine = (node: ReadNode): boolean => !!email && node.providerEmail?.trim().toLowerCase() === email
const previous = tracked.picture
let picture = mergeAwake(previous, read.nodes, now, isMine, (name, key) => provenStopped(previous, here, name, key))
- picture = withWindows({ ...picture, caseMap: withSpellings(picture.caseMap, [...read.curatedIds, ...here.records.flatMap((r) => r.ids)]) }, read.windows)
+ const spelled = withSpellings(picture.caseMap, [...read.curatedIds, ...here.records.flatMap((r) => r.ids)])
+ picture = withWindows({ ...picture, caseMap: withSpellings(spelled, advertisedNow(here.records), true) }, read.windows)
const unknownIds = unspelled(picture, read.nodes).filter((key) => !tracked.spelled.has(key))
if (unknownIds.length && base) {
unknownIds.forEach((key) => tracked.spelled.add(key))
- picture = { ...picture, caseMap: withSpellings(picture.caseMap, await readDiscoveryIds(base)) }
+ picture = { ...picture, caseMap: withSpellings(picture.caseMap, await readDiscoveryIds(base), true) }
}
tracked.picture = picture
tracked.rawNodes = read.rawNodes
diff --git a/cli/src/lib/gridPicture.spellings.spec.ts b/cli/src/lib/gridPicture.spellings.spec.ts
new file mode 100644
index 000000000..1f13b7343
--- /dev/null
+++ b/cli/src/lib/gridPicture.spellings.spec.ts
@@ -0,0 +1,28 @@
+import { describe, expect, it } from 'vitest'
+import { advertisedNow, withSpellings, type LocalRecord } from './gridPicture.js'
+
+const record = (over: Partial<LocalRecord>): LocalRecord => ({ name: 'mac', ids: [], pid: 4242, alive: true, ...over })
+
+describe('model spellings', () => {
+ it('keeps an upper-case spelling over the lower-case one the overview shows', () => {
+ const remembered = withSpellings({}, ['Qwen3.6-35B-A3B'])
+ expect(withSpellings(remembered, ['qwen3.6-35b-a3b'])).toEqual({ 'qwen3.6-35b-a3b': 'Qwen3.6-35B-A3B' })
+ })
+
+ it('takes the name as it is served now over a remembered one, whatever its case', () => {
+ // Advertised as `Gemma-4-E2B` once, `gemma-4-e2b` now: the relay matches exactly, so the old spelling
+ // would send an agent to a name nothing serves.
+ const remembered = withSpellings({}, ['Gemma-4-E2B'])
+ expect(withSpellings(remembered, ['gemma-4-e2b'], true)).toEqual({ 'gemma-4-e2b': 'gemma-4-e2b' })
+ expect(withSpellings(remembered, ['Gemma-4-E2B'], true)).toBe(remembered)
+ })
+
+ it("reads what this computer serves now from its live records' own aliases only", () => {
+ expect(advertisedNow([
+ record({ ids: ['gemma-4-e2b'], advertised: true }),
+ // A name derived from a file is not an alias anybody gave; it keeps the old rule.
+ record({ ids: ['Small-Q4'], advertised: false }),
+ record({ ids: ['old-alias'], advertised: true, alive: false, pid: null }),
+ ])).toEqual(['gemma-4-e2b'])
+ })
+})
diff --git a/cli/src/lib/gridPicture.ts b/cli/src/lib/gridPicture.ts
index 60273aea5..d78021dc6 100644
--- a/cli/src/lib/gridPicture.ts
+++ b/cli/src/lib/gridPicture.ts
@@ -100,15 +100,20 @@ const hasUpperCase = (id: string): boolean => id !== id.toLowerCase()
* `caseMap` with `ids` folded in. The first spelling of a name wins, except that one carrying an
* upper-case letter always replaces an all-lower-case one: the lower-case form is what the overview
* already showed, so it is never the better answer — and it never overwrites a better one.
+ *
+ * `current` ids are the name as it is served right now — this computer's live alias, the relay's own
+ * routing name — and replace whatever was remembered. The relay matches a name exactly: a model once
+ * advertised as `Gemma-4-E2B` and now as `gemma-4-e2b` kept the old spelling, an agent was moved onto
+ * it, and every request answered "No providers available for this model" [run].
*/
-export function withSpellings(caseMap: Record<string, string>, ids: readonly string[]): Record<string, string> {
+export function withSpellings(caseMap: Record<string, string>, ids: readonly string[], current = false): Record<string, string> {
let next = caseMap
for (const raw of ids) {
const exact = raw.trim()
const key = idKey(exact)
if (!key) continue
const known = next[key]
- if (known === undefined || (!hasUpperCase(known) && hasUpperCase(exact))) {
+ if (known === undefined || (current && known !== exact) || (!hasUpperCase(known) && hasUpperCase(exact))) {
if (next === caseMap) next = { ...caseMap }
next[key] = exact
}
@@ -223,8 +228,14 @@ export interface LocalRecord {
pid: number | null
/** Whether that pid is a live process. False for a null pid. */
alive: boolean
+ /** `ids` are the aliases it advertises, exactly as given (`advertise_as`), not names derived from files. */
+ advertised?: boolean
}
+/** The names this computer advertises right now, exactly: its live records' own aliases. */
+export const advertisedNow = (records: readonly LocalRecord[]): string[] =>
+ records.filter((record) => record.alive && record.advertised).flatMap((record) => record.ids)
+
/**
* What this computer's own records prove about a grid's rows — never decided by hostname, the machine
* list, or the heartbeat sidecar (the sidecar goes stale while a healthy provider is parked).
@@ -298,7 +309,7 @@ export type SeemsOffline = (name: string) => OfflineReading | null
*/
export function sectionView(picture: GridPicture, here: ServedHere, now: number, seemsOffline: SeemsOffline = () => null): SectionView {
const awake = picture.state === 'awake'
- const caseMap = withSpellings(picture.caseMap, here.records.flatMap((record) => record.ids))
+ const caseMap = withSpellings(withSpellings(picture.caseMap, here.records.flatMap((record) => record.ids)), advertisedNow(here.records), true)
/** key → the node entries serving it, in the order the picture lists them; `namedBy` → the one whose
* name the row shows (the first to list it, or this computer for a model only its records serve). */
const serving = new Map<string, PictureNode[]>()
diff --git a/cli/src/lib/localModels.spec.ts b/cli/src/lib/localModels.spec.ts
index c2a8ea109..547a7271d 100644
--- a/cli/src/lib/localModels.spec.ts
+++ b/cli/src/lib/localModels.spec.ts
@@ -7,6 +7,7 @@ import { rmSync, writeFileSync } from 'node:fs'
import { spawnSync } from 'node:child_process'
import { baseModel, compatibleModels, contextLadder, LocalModels, modelBudget, modelFamily, rankForCoding, readRunRecords, type GridInventory } from './localModels.js'
import { GridFleetRpc, type GridFleetResult } from './gridFleetRpc.js'
+import type { AppEngineOps, AppEngineRecord, AppModel } from './appModels.js'
import { encryptDownFrame, encryptRpcResult } from './e2ee/applicationFrames.js'
const card = (id = 'org/Small-GGUF') => ({ repo_id: id, runnable: true, task: 'text-generation', format: 'GGUF',
@@ -1038,9 +1039,10 @@ describe('readRunRecords — what servedHere knows about this computer', () => {
const found = (await readRunRecords(home, 'grid-home')).sort((x, y) => x.ids.join().localeCompare(y.ids.join()))
expect(found).toEqual([
- { name: '', ids: [], pid: null, alive: false },
- { name: 'mac', ids: ['Qwen3-8B-Q4_K_M', 'plain'], pid: expect.any(Number), alive: false },
- { name: 'mac', ids: ['Team/Model'], pid: process.pid, alive: true },
+ { name: '', ids: [], pid: null, alive: false, advertised: false },
+ { name: 'mac', ids: ['Qwen3-8B-Q4_K_M', 'plain'], pid: expect.any(Number), alive: false, advertised: false },
+ // Its own aliases, as given: the one spelling that is the name served now.
+ { name: 'mac', ids: ['Team/Model'], pid: process.pid, alive: true, advertised: true },
])
})
@@ -1268,3 +1270,133 @@ describe('download without starting', () => {
expect(calls.filter(args => args[0] === 'pull')).toHaveLength(pulls)
})
})
+
+describe('models other apps downloaded, started in their own app', () => {
+ const GiB = 1024 ** 3
+ const ollama: AppModel = { id: 'app:ollama:llama3.2:3b', name: 'llama3.2:3b', app: 'ollama', engine: 'ollama', ref: 'llama3.2:3b',
+ binary: '/usr/local/bin/ollama', sizeBytes: 2 * GiB, quant: 'Q4_K_M', contextLength: 131072, kvBytesPerToken: 112 * 1024 }
+ const studio: AppModel = { id: 'app:lm-studio:google/gemma-4-e2b', name: 'gemma-4-e2b', app: 'lm-studio', engine: 'lm-studio',
+ ref: 'google/gemma-4-e2b', binary: '/home/me/.lmstudio/bin/lms', sizeBytes: 4 * GiB, contextLength: 131072 }
+ let apps: AppModel[], up: Set<string>, joinedAliases: Set<string>, window: number | undefined
+ let ops: { [K in keyof AppEngineOps]: Mock<AppEngineOps[K]> }
+
+ /** The grid fake, with a `join --at` that lists the alias rather than writing a `--serve` run record. */
+ const appService = () => {
+ const base = run.getMockImplementation()!
+ run.mockImplementation(async (args, output) => {
+ if (args.includes('join') && args.includes('--at')) {
+ calls.push(args)
+ joinedAliases.add(args[args.indexOf('--advertise-as') + 1]!)
+ return ok()
+ }
+ if (args.includes('leave') && args.includes('--engine') && joinedAliases.has(args[args.indexOf('--engine') + 1]!)) {
+ calls.push(args)
+ joinedAliases.delete(args[args.indexOf('--engine') + 1]!)
+ return ok()
+ }
+ return base(args, output)
+ })
+ inventory.mockImplementation(async () => ({ state: 'awake', status: 'running',
+ nodes: [...joinedAliases].map(alias => ({ node_id: 'local-node', online: true, models: [alias] })) }))
+ return new LocalModels({ stateDir, processEnv: { GRID_HOME: home }, run, request: request as typeof fetch, inventory,
+ appModels: async () => apps, appEngines: ops as unknown as AppEngineOps })
+ }
+ beforeEach(() => {
+ apps = [ollama, studio]; up = new Set(); joinedAliases = new Set(); window = 131072
+ ops = {
+ start: vi.fn(async (model: AppModel) => {
+ up.add(model.id)
+ return { engine: model.engine as AppEngineRecord['engine'], served: model.ref, alias: model.name, port: 41000, binary: model.binary!, pid: 4242 }
+ }),
+ alive: vi.fn(async (record: AppEngineRecord) => up.has(record.modelId)),
+ loadedContext: vi.fn(async () => window),
+ stop: vi.fn(async (record: AppEngineRecord) => { up.delete(record.modelId) }),
+ }
+ })
+
+ it('lists them under the app they came from, ready to start, beside Grid\'s own', async () => {
+ // A catalog model not here yet runs in nothing; once Grid has downloaded it, it runs in Grid, named
+ // like the apps beside it.
+ const models = appService()
+ expect((await models.list('home')).models.find(m => m.id === 'org/Small-GGUF')).toMatchObject({ state: 'available' })
+ expect((await models.list('home')).models.find(m => m.id === 'org/Small-GGUF')).not.toHaveProperty('app')
+ await writeFile(join(home, 'models', 'Small-Q4.gguf'), Buffer.alloc(64))
+ const snapshot = await models.list('home', true)
+ expect(snapshot.models.find(m => m.id === 'org/Small-GGUF')).toMatchObject({ state: 'downloaded', app: 'Grid' })
+ expect(snapshot.models.filter(m => m.app && m.app !== 'Grid')).toEqual([
+ expect.objectContaining({ id: ollama.id, name: 'llama3.2:3b', app: 'Ollama', state: 'downloaded', sizeBytes: 2 * GiB, canStart: true, canStop: false }),
+ expect.objectContaining({ id: studio.id, name: 'gemma-4-e2b', app: 'LM Studio', state: 'downloaded', canStart: true, canStop: false }),
+ ])
+ expect(ops.start).not.toHaveBeenCalled()
+ })
+
+ it('starts one in its app, joins it at its own address, checks it through the grid, and stops what it started', async () => {
+ const models = appService()
+ await models.act('home', ollama.id, 'start'); await models.settled()
+ expect(ops.start).toHaveBeenCalledWith(ollama, 131072, join(stateDir, 'logs'))
+ expect(calls.find(args => args.includes('--at'))).toEqual(['--remote', 'join', 'home', '--at', 'http://127.0.0.1:41000/v1',
+ '-m', 'llama3.2:3b', '--advertise-as', 'llama3.2:3b', '--max-concurrency', '1'])
+ expect(request.mock.calls.some(([url]) => String(url).includes(RELAY_CHAT))).toBe(true)
+ let row = (await models.list('home', true)).models.find(m => m.id === ollama.id)!
+ expect(row).toMatchObject({ state: 'running', canStart: false, canStop: true, operation: { phase: 'done' } })
+ // One engine of this picker's at a time.
+ expect(await models.act('home', studio.id, 'start').then(() => models.settled()).then(() => models.list('home', true)))
+ .toMatchObject({ models: expect.arrayContaining([expect.objectContaining({ id: studio.id, operation: expect.objectContaining({ phase: 'failed', error: 'Stop llama3.2:3b first to start another local model.' }) })]) })
+ expect(ops.start).toHaveBeenCalledTimes(1)
+
+ await models.act('home', ollama.id, 'stop'); await models.settled()
+ expect(calls.some(args => args.join(' ') === '--remote leave home --engine llama3.2:3b')).toBe(true)
+ expect(ops.stop).toHaveBeenCalledWith(expect.objectContaining({ modelId: ollama.id, pid: 4242 }))
+ row = (await models.list('home', true)).models.find(m => m.id === ollama.id)!
+ expect(row).toMatchObject({ state: 'downloaded', canStart: true, canStop: false })
+ expect(JSON.parse(await readFile(join(stateDir, 'app-engines.json'), 'utf8'))).toEqual([])
+ })
+
+ it('keeps an engine it started stoppable when a later scan reads the model differently', async () => {
+ const models = appService()
+ await models.act('home', ollama.id, 'start'); await models.settled()
+ // The next scan missed the app (a slow probe) and would serve the file with Grid's engine instead.
+ apps = [{ ...ollama, engine: 'grid', ref: join(root, 'blob') }]
+ const row = (await models.list('home', true)).models.filter(m => m.id === ollama.id)
+ expect(row).toEqual([expect.objectContaining({ state: 'running', canStop: true, canStart: false })])
+ await models.act('home', ollama.id, 'stop'); await models.settled()
+ expect(ops.stop).toHaveBeenCalledWith(expect.objectContaining({ modelId: ollama.id }))
+ expect(joinedAliases.size).toBe(0)
+ expect(JSON.parse(await readFile(join(stateDir, 'app-engines.json'), 'utf8'))).toEqual([])
+ })
+
+ it('takes it down again, and says so, when the app loaded it with less than 64K', async () => {
+ window = 16384
+ const models = appService()
+ await models.act('home', ollama.id, 'start'); await models.settled()
+ const row = (await models.list('home', true)).models.find(m => m.id === ollama.id)!
+ expect(row.operation).toMatchObject({ phase: 'failed', error: expect.stringContaining('could only get a 16K context in Ollama') })
+ expect(row).toMatchObject({ state: 'downloaded', canStart: true })
+ expect(ops.stop).toHaveBeenCalledTimes(1)
+ expect(joinedAliases.size).toBe(0)
+ })
+
+ it('starts nothing that cannot get 64K beside its weights', async () => {
+ apps = [{ ...ollama, sizeBytes: 60 * GiB }]
+ const models = appService()
+ await models.act('home', ollama.id, 'start'); await models.settled()
+ expect((await models.list('home', true)).models.find(m => m.id === ollama.id)?.operation)
+ .toMatchObject({ phase: 'failed', error: expect.stringContaining('with a 64K context') })
+ expect(ops.start).not.toHaveBeenCalled()
+ })
+
+ it("serves one with Grid's llama.cpp, through a link to the app's file, when the app is not installed", async () => {
+ const file = join(root, 'ollama-blob')
+ await writeFile(file, Buffer.alloc(64))
+ apps = [{ id: 'app:ollama:qwen3:8b', name: 'qwen3:8b', app: 'ollama', engine: 'grid', ref: file, sizeBytes: 64 }]
+ const models = appService()
+ const listed = (await models.list('home')).models.find(m => m.id === 'app:ollama:qwen3:8b')!
+ // Its app is not here, so it runs in Grid, and says so.
+ expect(listed).toMatchObject({ app: 'Grid', state: 'downloaded', canStart: true })
+ await models.act('home', 'app:ollama:qwen3:8b', 'start'); await models.settled()
+ const { readlink } = await import('node:fs/promises')
+ expect(await readlink(join(home, 'models', 'qwen3-8b.gguf'))).toBe(file)
+ expect(calls.find(args => args.includes('--serve'))?.slice(0, 5)).toEqual(['--remote', 'join', 'home', '--serve', 'qwen3-8b.gguf'])
+ expect(ops.start).not.toHaveBeenCalled()
+ })
+})
diff --git a/cli/src/lib/localModels.ts b/cli/src/lib/localModels.ts
index 76ef2e6fd..70de8683c 100644
--- a/cli/src/lib/localModels.ts
+++ b/cli/src/lib/localModels.ts
@@ -1,7 +1,7 @@
/** The Models popover's local lifecycle. Grid remains the hardware, catalog,
* download and process authority. A click is a durable daemon operation, never a chat task. */
import { createHash, randomUUID } from 'node:crypto'
-import { readFile, readdir, stat, mkdir, rename, writeFile, statfs } from 'node:fs/promises'
+import { readFile, readdir, stat, mkdir, rename, writeFile, statfs, symlink, lstat, readlink } from 'node:fs/promises'
import { createServer, type AddressInfo } from 'node:net'
import { basename, dirname, join } from 'node:path'
import { GridFleetRpc, type GridFleetResult } from './gridFleetRpc.js'
@@ -11,6 +11,7 @@ import { processExists } from './processLiveness.js'
import type { LocalRecord, PictureState } from './gridPicture.js'
import { displayModelName } from './gridReader.js'
import { readEnvExports } from './gridWake.js'
+import { APP_LABEL, AppStartError, GRID_LABEL, appContext, readAppRecords, writeAppRecords, type AppEngine, type AppEngineOps, type AppEngineRecord, type AppModel } from './appModels.js'
const GiB = 1024 ** 3
const obj = (v: unknown): Record<string, any> => v && typeof v === 'object' && !Array.isArray(v) ? v : {}
@@ -71,6 +72,9 @@ export interface LocalModel {
operation?: ModelOperation
/** Running here, parked while its grid sleeps (additive: an older desktop reads plain `running`). */
gridAsleep?: boolean
+ /** What it runs in: `Grid` for a model Grid's llama.cpp serves, or the app whose folder holds it
+ * (`Ollama`, `LM Studio`, `llama.cpp`, [AppModel]). Absent for a catalog model not downloaded yet. */
+ app?: string
}
export interface ModelOperation {
id: string; modelId: string; action: 'download' | 'start' | 'stop'; phase: 'running' | 'done' | 'failed'
@@ -97,6 +101,9 @@ export interface Candidate {
aliases?: string[]
/** The catalog's estimate for this machine, and the model's size in billions of parameters. */
estTokS?: number; paramsB?: number
+ /** A file another app downloaded, served by Grid's llama.cpp because that app is not installed here:
+ * `file` is the link to it in Grid's models folder, made at Start. */
+ appPath?: string
}
/** `live`: its heartbeat sidecar is fresh (the grid is hearing from it). `pidAlive`: the process its run
* record names exists — the only liveness that holds while the grid sleeps and the sidecar goes stale. */
@@ -120,6 +127,9 @@ interface Options {
inventory: (grid: string, force: boolean) => Promise<GridInventory>
/** Told when a start or stop has finished, so the model lists are read again. */
onChanged?: () => void
+ /** Models other apps downloaded here ([scanAppModels]), and the engines that start them. Absent: none. */
+ appModels?: () => Promise<AppModel[]>
+ appEngines?: AppEngineOps
}
/** One concrete, machine-fitted version per model. Non-chat and unprobed offline
@@ -293,6 +303,8 @@ export class LocalModels {
private receiptScope?: string
private knownByGrid = new Map<string, Candidate[]>()
private blockers = new Map<string, string>()
+ private appsRead?: { at: number; value: AppModel[] }
+ private appsPending?: Promise<AppModel[]>
constructor(private readonly options: Options) {
this.processEnv = options.processEnv ?? process.env
@@ -376,6 +388,7 @@ export class LocalModels {
}
private async downloaded(candidate: Candidate): Promise<boolean> {
+ if (candidate.appPath) return stat(candidate.appPath).then(s => s.isFile() && s.size === candidate.size, () => false)
const sizes = await Promise.all(candidate.files.map(file => stat(join(this.home, 'models', file)).then(s => s.isFile() ? s.size : 0).catch(() => 0)))
return sizes.every(size => size > 0) && sizes.reduce((sum, size) => sum + size, 0) === candidate.size
}
@@ -416,7 +429,7 @@ export class LocalModels {
/** Keep models imported from an existing Grid setup after Stop removes its
* run record. Only completed local files that this computer already served
* become restartable; arbitrary downloaded GGUFs are not assumed compatible. */
- private async known(grid: string, owned: Owned[]): Promise<Candidate[]> {
+ private async known(grid: string, owned: Owned[], apps: Candidate[] = []): Promise<Candidate[]> {
const path = join(this.options.stateDir, `${key(grid)}.known.json`)
let known = this.knownByGrid.get(grid)
if (!known) {
@@ -438,7 +451,7 @@ export class LocalModels {
}
let changed = false
for (const instance of owned) {
- if (this.candidates.some(c => c.file === instance.file) || known.some(c => c.file === instance.file)) continue
+ if ([...this.candidates, ...apps].some(c => c.file === instance.file) || known.some(c => c.file === instance.file)) continue
const file = await stat(join(this.home, 'models', instance.file)).catch(() => null)
if (!file?.isFile() || !file.size) continue
known.push({ id: `local:${instance.file}`, name: cleanName(instance.aliases[0]),
@@ -543,6 +556,29 @@ export class LocalModels {
await rename(temp, file)
}
+ /** Models other apps downloaded here, read at most every 30s (a read walks their folders) unless [force]. */
+ private async apps(force = false): Promise<AppModel[]> {
+ const read = this.options.appModels
+ if (!read) return []
+ if (!force && this.appsRead && Date.now() - this.appsRead.at < 30_000) return this.appsRead.value
+ return this.appsPending ??= read().catch(() => [] as AppModel[]).then(value => {
+ this.appsRead = { at: Date.now(), value }
+ return value
+ }).finally(() => { this.appsPending = undefined })
+ }
+
+ /** An app's model that Grid's llama.cpp serves (its app is not installed here), as a candidate whose
+ * file is the link Start makes to it in Grid's models folder. */
+ private appCandidates(apps: AppModel[]): Candidate[] {
+ return apps.filter(app => app.engine === 'grid').map(app => {
+ const file = `${app.name.replace(/[^A-Za-z0-9._-]+/g, '-')}.gguf`
+ return { id: app.id, name: app.name, pull: '', file, files: [file], size: app.sizeBytes, quant: app.quant ?? '',
+ aliases: [app.name], appPath: app.ref }
+ })
+ }
+
+ private get appRecordsFile(): string { return join(this.options.stateDir, 'app-engines.json') }
+
async list(grid: string | null, force = false): Promise<LocalModelsSnapshot> {
if (!grid) return { models: [], notice: 'Sign in to find models for this computer.', observedAt: new Date().toISOString(), busy: false }
if (!force && this.cached?.grid === grid && Date.now() - this.cached.at < 2500) return this.cached.value
@@ -568,7 +604,18 @@ export class LocalModels {
nodes = inventory.state === 'awake' ? rows(inventory.nodes) : []
} catch { inventoryError = 'Running models could not be checked. Try again.' }
const operation = this.active?.grid === grid ? this.active.operation : this.receipt?.grid === grid ? this.receipt.operation : undefined
- const choices = [...this.candidates, ...(await this.known(grid, owned)).filter(k => !this.candidates.some(c => c.file === k.file))]
+ const apps = await this.apps(force)
+ const records = (await readAppRecords(this.appRecordsFile)).filter(record => record.grid === grid)
+ // An engine this daemon started is read from its record, whatever a later scan makes of the model: a
+ // scan that missed the person's llama-server (busy serving, it answered `--version` late) once listed
+ // a running engine as Grid's, and Stop then had nothing to stop.
+ const recorded = (id: string) => records.some(record => record.modelId === id)
+ const appServed = this.appCandidates(apps.filter(app => !recorded(app.id)))
+ // One engine of this picker's at a time, as for Grid's own: an app's engine started here blocks the next.
+ for (const record of records) this.blockers.set(grid, `Stop ${cleanName(record.name)} first to start another local model.`)
+ const knownHere = (await this.known(grid, owned, appServed)).filter(k => !this.candidates.some(c => c.file === k.file))
+ const choices = [...this.candidates, ...knownHere,
+ ...appServed.filter(app => ![...this.candidates, ...knownHere].some(c => c.file === app.file))]
const downloaded = await Promise.all(choices.map(candidate => this.downloaded(candidate)))
// Different repositories can use the same filename for different weights.
// Attribute the one local engine to the complete file that is actually here.
@@ -607,14 +654,35 @@ export class LocalModels {
requests: running ? num(perModel?.requests) : undefined,
windowSeconds: running ? num(answered.window_seconds) : undefined,
operation: operation?.modelId === candidate.id ? operation : undefined,
- ...(!node && parked(instance) ? { gridAsleep: true } : {}) }
+ ...(!node && parked(instance) ? { gridAsleep: true } : {}),
+ // Grid serves it — its own download, or another app's file linked in because that app is not here.
+ ...(running || available ? { app: GRID_LABEL } : {}) }
})
for (const instance of owned.filter(o => !choices.some(c => c.file === o.file))) {
const serving = !!servingNode(instance)
models.push({ id: `local:${instance.file}`, name: cleanName(instance.aliases[0]),
state: serving || parked(instance) ? 'running' : 'available', canStart: false, canStop: !inventoryError,
operation: operation?.modelId === `local:${instance.file}` ? operation : undefined,
- ...(!serving && parked(instance) ? { gridAsleep: true } : {}) })
+ ...(!serving && parked(instance) ? { gridAsleep: true } : {}), app: GRID_LABEL })
+ }
+ // Models in their own apps, started there: running when that engine is up and the grid lists it (or
+ // the grid sleeps — parked, as above). A record whose model has since gone keeps a row to stop it by.
+ const listedHere = (alias: string) => nodes.some(n => n.online === true && (Array.isArray(n.models) ? n.models : [])
+ .some((m: unknown) => modelKey(str(typeof m === 'string' ? m : obj(m).model)) === modelKey(alias)))
+ const appRow = async (id: string, name: string, app: AppEngine, record: AppEngineRecord | undefined, extra: Partial<LocalModel>): Promise<LocalModel> => {
+ const alive = record && this.options.appEngines ? await this.options.appEngines.alive(record) : false
+ const listed = !!record && listedHere(record.alias)
+ const running = alive && (listed || asleep)
+ return { id, name, app: APP_LABEL[app], state: running ? 'running' : 'downloaded', ...extra,
+ canStart: !inventoryError && !record, canStop: !inventoryError && !!record,
+ operation: operation?.modelId === id ? operation : undefined, ...(running && !listed ? { gridAsleep: true } : {}) }
+ }
+ for (const app of apps.filter(a => a.engine !== 'grid' || recorded(a.id))) {
+ models.push(await appRow(app.id, app.name, app.app, records.find(r => r.modelId === app.id),
+ { sizeBytes: app.sizeBytes, ...(app.quant ? { quant: app.quant } : {}) }))
+ }
+ for (const record of records.filter(r => !apps.some(a => a.id === r.modelId))) {
+ models.push({ ...await appRow(record.modelId, record.name, record.engine, record, {}), canStart: false })
}
const freeDiskBytes = await statfs(join(this.home, 'models')).catch(() => statfs(this.home))
.then(disk => disk.bavail * disk.bsize, () => undefined)
@@ -666,8 +734,16 @@ export class LocalModels {
// account's existing grids before resolving ownership or joining again.
await must(['--remote', 'sync'], 'Models could not be checked. Try again.')
}
+ const apps = operation.modelId.startsWith('app:') ? await this.apps(true) : []
+ const app = apps.find(a => a.id === operation.modelId)
+ const started = (await readAppRecords(this.appRecordsFile)).some(r => r.modelId === operation.modelId && r.grid === grid)
+ if (operation.modelId.startsWith('app:') && (started || app?.engine !== 'grid')) {
+ return this.performApp(grid, operation, app?.engine === 'grid' ? undefined : app)
+ }
const owned = await this.owned(grid)
- const candidate = [...this.candidates, ...await this.known(grid, owned)].find(c => c.id === operation.modelId)
+ const appServed = this.appCandidates(apps)
+ const candidate = [...this.candidates, ...await this.known(grid, owned, appServed), ...appServed].find(c => c.id === operation.modelId)
+ if (candidate?.appPath && operation.action === 'start') await this.linkApp(candidate)
const instance = owned.find(o => candidate ? o.file === candidate.file : operation.modelId === `local:${o.file}`)
if (operation.action === 'stop') {
if (instance) {
@@ -790,6 +866,71 @@ export class LocalModels {
await this.save(grid, operation)
}
+ /** Grid's models folder gets a link to an app's file, never a copy. A different file under the name is
+ * never served in its place. */
+ private async linkApp(candidate: Candidate): Promise<void> {
+ const link = join(this.home, 'models', candidate.file)
+ const there = await lstat(link).catch(() => null)
+ if (there) {
+ if (there.isSymbolicLink() && await readlink(link).catch(() => '') === candidate.appPath) return
+ throw new ModelError('A different file has this name in Grid\'s models folder. Open Model Manager to start this model.')
+ }
+ await mkdir(join(this.home, 'models'), { recursive: true })
+ await symlink(candidate.appPath!, link)
+ }
+
+ /** Start or stop a model in its own app ([AppModel]): the app's engine, joined to [grid] at its own
+ * address, answered through the grid, and held to the 64K floor by what the engine says it loaded. */
+ private async performApp(grid: string, operation: ModelOperation, app: AppModel | undefined): Promise<void> {
+ const ops = this.options.appEngines
+ const records = await readAppRecords(this.appRecordsFile)
+ const record = records.find(r => r.modelId === operation.modelId && r.grid === grid)
+ const without = (gone: AppEngineRecord) => writeAppRecords(this.appRecordsFile, records.filter(r => r !== gone))
+ const takeDown = async (started: AppEngineRecord) => {
+ await this.run(['--remote', 'leave', grid, '--engine', started.alias], undefined, 5 * 60_000)
+ await ops?.stop(started)
+ }
+ if (operation.action === 'stop') {
+ if (record) { await takeDown(record); await without(record) }
+ } else if (operation.action === 'start' && !record) {
+ if (!app || !ops) throw new ModelError('This model could not be checked. Refresh and try again.')
+ if (this.blockers.has(grid)) throw new ModelError(this.blockers.get(grid)!)
+ const device = obj(await this.json(['device-info', '--json']))
+ const ctx = appContext(app, num(device.usable_bytes))
+ if (!ctx) throw new ModelError(`This computer does not have the memory to run ${app.name} with a 64K context. Close some apps, or choose a smaller model.`)
+ operation.stage = 'starting'; await this.save(grid, operation)
+ if (DOWN.has(await this.gridStatus(grid).catch(() => ''))) {
+ if (!(await this.run(['--remote', 'start', grid], undefined, 30 * 60_000)).ok) throw new ModelError('Your grid could not start. Try again.')
+ }
+ let started: AppEngineRecord
+ try {
+ started = { spec: 1, modelId: app.id, grid, name: app.name, ...await ops.start(app, ctx, join(this.options.stateDir, 'logs')) }
+ } catch (error) {
+ throw new ModelError(error instanceof AppStartError ? error.message : 'The model could not start. Try again.')
+ }
+ await writeAppRecords(this.appRecordsFile, [...records.filter(r => !(r.modelId === app.id && r.grid === grid)), started])
+ const failed = async (message: string): Promise<never> => {
+ await takeDown(started)
+ await writeAppRecords(this.appRecordsFile, (await readAppRecords(this.appRecordsFile)).filter(r => !(r.modelId === app.id && r.grid === grid)))
+ throw new ModelError(message)
+ }
+ const named = this.options.machineName?.()?.trim()
+ const joined = await this.run(['--remote', 'join', grid, '--at', `http://127.0.0.1:${started.port}/v1`, '-m', started.served,
+ '--advertise-as', started.alias, '--max-concurrency', '1', ...(named && validArg(named) ? ['--name', named] : [])], undefined, 10 * 60_000)
+ if (!joined.ok) await failed('The model could not join your grid. Try again.')
+ operation.stage = 'verifying'; await this.save(grid, operation)
+ try { await this.verify(grid, started.alias) } catch (error) {
+ await failed(error instanceof ModelError ? error.message : 'The model did not answer. Try again.')
+ }
+ const window = await ops.loadedContext(started)
+ if (window !== undefined && window < MIN_CODING_CONTEXT) {
+ await failed(`${app.name} could only get a ${Math.floor(window / 1024)}K context in ${APP_LABEL[app.engine as AppEngine]}. Coding agents need at least 64K. Close some apps, or choose a smaller model.`)
+ }
+ }
+ operation.phase = 'done'
+ await this.save(grid, operation)
+ }
+
private async verify(grid: string, model: string): Promise<void> {
const info = await this.run(['--remote', 'info', grid, '--env'])
const { baseUrl, apiKey } = readEnvExports(info.stdout)
@@ -843,7 +984,7 @@ export async function readRunRecords(home: string, gridId: string): Promise<Loca
const advertised = strings(record.advertise_as)
const ids = (advertised.length ? advertised : strings(record.models).map(displayModelName)).slice(0, MAX_RECORD_IDS)
const pid = recordPid(record)
- result.push({ name: str(record.meta_name), ids, pid, alive: pid !== null && processExists(pid) })
+ result.push({ name: str(record.meta_name), ids, pid, alive: pid !== null && processExists(pid), advertised: advertised.length > 0 })
}
return result
}
diff --git a/desktop/lib/models/local_model.dart b/desktop/lib/models/local_model.dart
index 5724b8fed..708b14ff6 100644
--- a/desktop/lib/models/local_model.dart
+++ b/desktop/lib/models/local_model.dart
@@ -18,8 +18,13 @@ class LocalModel {
this.contextWindow,
this.estTokS,
this.paramsB,
+ this.app,
});
final String id, name, state;
+
+ /// The app this model was downloaded with and starts in (`Ollama`, `LM Studio`, `llama.cpp`): one found
+ /// in that app's folder. Null for Grid's own models, and from an older daemon.
+ final String? app;
final String? quant;
final double? sizeBytes, tokensPerSecond, requests, windowSeconds;
final bool recommended, canStart, canStop;
@@ -76,6 +81,10 @@ class LocalModel {
contextWindow: _number(data['contextWindow']),
estTokS: _number(data['estTokS']),
paramsB: _number(data['paramsB']),
+ app: switch (data['app']) {
+ final String app when app.trim().isNotEmpty => app.trim(),
+ _ => null,
+ },
);
}
diff --git a/desktop/lib/state/swarm_search.dart b/desktop/lib/state/swarm_search.dart
index 6e0fe19fb..8065fa71f 100644
--- a/desktop/lib/state/swarm_search.dart
+++ b/desktop/lib/state/swarm_search.dart
@@ -354,7 +354,8 @@ class SwarmSearchController extends ChangeNotifier {
/// What sets one model of yours apart from the next, in two aligned columns ahead of the row's
/// word: its size, and its speed — measured while it runs, else the catalog's estimate for this
- /// machine (`~`). Null for any other row. Widths are fixed so every row's columns line up.
+ /// machine (`~`), else the app it came from (`Ollama`). Null for any other row. Widths are fixed
+ /// so every row's columns line up.
String? modelRowFacts(SwarmDestination row) {
final local = models?.entries[row.modelId]?.local;
if (local == null) return null;
@@ -366,7 +367,7 @@ class SwarmSearchController extends ChangeNotifier {
? '${measured.round()} tok/s'
: local.estTokS != null
? '~${local.estTokS!.round()} tok/s'
- : '';
+ : local.app ?? '';
return '${size.padLeft(6)} ${speed.padLeft(9)}';
}
diff --git a/desktop/lib/widgets/swarm_resource_preview.dart b/desktop/lib/widgets/swarm_resource_preview.dart
index 29486dd4a..82f6ece5d 100644
--- a/desktop/lib/widgets/swarm_resource_preview.dart
+++ b/desktop/lib/widgets/swarm_resource_preview.dart
@@ -1306,6 +1306,8 @@ class _SwarmResourcePreviewState extends State<SwarmResourcePreview> {
errorLine(widget.search.modelUseError!),
SizedBox(height: cell.height),
if (local != null) ...[
+ // A model another app downloaded starts in that app, joined to the grid from there.
+ if (local.app case final app?) labelValue('Runs in', app),
// What decides between models: what it costs to get, whether it fits, how fast it answers.
if (local.sizeBytes case final size?)
labelValue(
diff --git a/desktop/test/local_model_picker_test.dart b/desktop/test/local_model_picker_test.dart
index b22b6b307..99741b8d6 100644
--- a/desktop/test/local_model_picker_test.dart
+++ b/desktop/test/local_model_picker_test.dart
@@ -467,4 +467,48 @@ void main() {
expect(gigabytesLabel(9.96 * _gib), '10 GB');
expect(gigabytesLabel(27159116064), '25 GB');
});
+
+ testWidgets(
+ 'a model another app downloaded is one of yours, named with that app, and Use starts it there',
+ (tester) async {
+ final app = (await _localFixture())..completeStarts = true;
+ try {
+ app.localInventory = {
+ ..._inventory(),
+ 'models': [
+ ...(_inventory()['models'] as List),
+ {
+ 'id': 'app:ollama:llama3.2:3b',
+ 'name': 'llama3.2:3b',
+ 'state': 'downloaded',
+ 'sizeBytes': 2 * _gib,
+ 'canStart': true,
+ 'app': 'Ollama',
+ },
+ ],
+ };
+ await app.modelManager.refresh(force: true);
+ final picker = await _open(tester, app);
+ final ollama = _row(picker, 'llama3.2:3b');
+ expect(picker.modelSection(ollama), ModelSearchSection.local);
+ expect(_status(tester, ollama), 'Use');
+ // No speed is known before it runs: the app it came from takes that column.
+ expect(_facts(tester, ollama), '2.0 GB · Ollama');
+ picker.move(picker.rows.indexOf(ollama) - picker.cursor);
+ await tester.pumpAndSettle();
+ expect(_inPreview('Runs in'), findsOneWidget);
+ expect(_inPreview('Ollama'), findsOneWidget);
+ // Use is the same act as for Grid's own: the daemon starts it (in Ollama), and the harness moves on.
+ await key(tester, LogicalKeyboardKey.enter);
+ await tester.pumpAndSettle();
+ expect(app.actions, [
+ (machine: 'm', model: 'app:ollama:llama3.2:3b', start: true),
+ ]);
+ expect(app.selections, [(agent: 'a69', model: 'llama3.2:3b')]);
+ } finally {
+ await tester.pumpWidget(const SizedBox());
+ app.dispose();
+ }
+ },
+ );
}
diff --git a/store/agents/autonomous-grid/lib/inventory.mjs b/store/agents/autonomous-grid/lib/inventory.mjs
index 028e4d85d..44e4f2032 100644
--- a/store/agents/autonomous-grid/lib/inventory.mjs
+++ b/store/agents/autonomous-grid/lib/inventory.mjs
@@ -443,13 +443,15 @@ async function accelerators(llamaServer, os) {
}
const firstLine = text => text.split('\n').map(line => line.trim()).find(Boolean) ?? null;
+// llama.cpp builds since 11000 log `srv llama_server: initializing ...` before `version: …` [run].
+const versionLine = text => text.split('\n').map(line => line.trim()).find(line => /^version\b/i.test(line)) ?? firstLine(text);
/** Engines installed here (on PATH or in their standard install place), with the version each reports. */
async function installedEngines(llamaServer, home) {
const engines = [];
const add = (kind, path, version, note) => engines.push({ kind, path, version: version || null, ...(note ? { note } : {}) });
const grid = await probe(llamaServer, ['--version']);
- if (grid.ok) add('llama.cpp', llamaServer, firstLine(grid.out), "Grid's own engine");
+ if (grid.ok) add('llama.cpp', llamaServer, versionLine(grid.out), "Grid's own engine");
// PATH first, then ~/.grid/envs/<engine>/bin, where the engine skills install Python engines.
const envs = join(home, '.grid', 'envs');
const windows = platform() === 'win32';
@@ -462,11 +464,16 @@ async function installedEngines(llamaServer, home) {
return (await stat(file).catch(() => null)) ? file : null;
};
const exists = async file => ((await stat(file).catch(() => null)) ? file : null);
- const which = async (name, env) => (await onPath(name)) || inEnv(name, env);
+ // An app opened from Finder or the Dock gets launchd's PATH (/usr/bin:/bin:/usr/sbin:/sbin), so the
+ // folders Homebrew and the person's own installs use are looked in too: a Homebrew llama-server read as
+ // "not installed" moved its models to Grid's engine.
+ const standard = windows ? [] : [join(home, '.local', 'bin'), '/opt/homebrew/bin', '/usr/local/bin', '/home/linuxbrew/.linuxbrew/bin'];
+ const inStandard = async name => { for (const dir of standard) if (await exists(join(dir, name))) return join(dir, name); return null; };
+ const which = async (name, env) => (await onPath(name)) || (await inEnv(name, env)) || inStandard(name);
const own = await which('llama-server');
if (own && own !== llamaServer) {
const answer = await probe(own, ['--version']);
- add('llama.cpp', own, answer.ok ? firstLine(answer.out) : null, answer.ok ? null : 'on PATH but did not answer --version');
+ add('llama.cpp', own, answer.ok ? versionLine(answer.out) : null, answer.ok ? null : 'on PATH but did not answer --version');
}
const ollama = (await which('ollama'))
|| ((await exists('/Applications/Ollama.app')) ? '/Applications/Ollama.app/Contents/Resources/ollama' : null)
@@ -550,7 +557,9 @@ export function modelFamily(name) {
*/
export function startWith(model, machine, answering = []) {
const installed = kind => machine.engines.some(e => e.kind === kind);
- const yourLlama = machine.engines.find(e => e.kind === 'llama.cpp' && e.note !== "Grid's own engine" && e.version);
+ // Installed is enough: `--version` took 10.8s while a llama-server was serving [run], past the probe's
+ // deadline, and a slow answer must not turn the person's own llama.cpp into Grid's.
+ const yourLlama = machine.engines.find(e => e.kind === 'llama.cpp' && e.note !== "Grid's own engine");
const up = kind => answering.some(e => e.kind === kind);
const grid = { engine: 'llama.cpp', label: "Grid's llama.cpp" };
const byFormat = () => {
@@ -566,7 +575,11 @@ export function startWith(model, machine, answering = []) {
};
if (model.source === 'ollama' && installed('ollama')) return { engine: 'ollama', label: 'ollama', running: up('ollama') };
if (model.source === 'lm-studio' && installed('lm-studio')) return { engine: 'lm-studio', label: 'lm-studio', running: up('lm-studio') };
- if (model.source === 'llama.cpp' && yourLlama) return { engine: 'llama.cpp', label: 'your llama.cpp', path: yourLlama.path };
+ // llama.cpp's `-hf` downloads: its own cache folder in older builds, the Hugging Face cache since (build
+ // 11146 put Qwen3-4B there [run]). Nothing else downloads GGUF files into that cache to run them.
+ if (yourLlama && (model.source === 'llama.cpp' || (model.source === 'huggingface' && model.format === 'gguf'))) {
+ return { engine: 'llama.cpp', label: 'your llama.cpp', path: yourLlama.path };
+ }
return byFormat();
}
diff --git a/store/agents/autonomous-grid/test/inventory.test.mjs b/store/agents/autonomous-grid/test/inventory.test.mjs
index 4152859d0..48aa4ac1f 100644
--- a/store/agents/autonomous-grid/test/inventory.test.mjs
+++ b/store/agents/autonomous-grid/test/inventory.test.mjs
@@ -266,6 +266,13 @@ test('a model starts with the app whose folder holds it, and moves to Grid\'s en
// llama.cpp's cache: the person's own llama-server, which downloaded it; Grid's when they have none.
assert.deepEqual(startWith(model('llama.cpp'), mac(yours)), { engine: 'llama.cpp', label: 'your llama.cpp', path: '/opt/homebrew/bin/llama-server' });
assert.equal(startWith(model('llama.cpp'), mac()).label, "Grid's llama.cpp");
+ // Busy serving, it answered `--version` past the deadline: still theirs, not Grid's.
+ assert.equal(startWith(model('llama.cpp'), mac({ ...yours, version: null, note: 'on PATH but did not answer --version' })).label, 'your llama.cpp');
+ // Newer llama.cpp saves `-hf` downloads in the Hugging Face cache: a GGUF there is the person's llama.cpp's
+ // to start when they have one, Grid's otherwise. Other formats there keep their own engines.
+ assert.equal(startWith(model('huggingface'), mac(yours)).label, 'your llama.cpp');
+ assert.equal(startWith(model('huggingface'), mac()).label, "Grid's llama.cpp");
+ assert.equal(startWith(model('huggingface', 'mlx'), mac(yours, { kind: 'mlx-lm', path: '/x', version: '0.31' })).engine, 'mlx-lm');
// ~/.grid/models stays Grid's, whatever else is installed.
assert.equal(startWith(model('grid'), mac(ollama, studio, yours)).label, "Grid's llama.cpp");
// No app of its own: by format. Safetensors go to vLLM or SGLang where they run, and to nothing on a Mac.