feat(local-model): a library of models, with one in use

The local model window could hold one model. A second install was refused
(409 already-installed), and a model a Remove kept could never be freed
from the window again. The store can now hold several models, and exactly
one of them is the model in use: the one the endpoint record lists, the
runtime is started for and a session may be launched on.

Server
- LocalModelStateFile.library lists every model that passed the check
  (id, label, context, digest, size, coding agent). The service writes
  the entry when verify passes; the reducer carries it and remove drops
  it. An installed file of an earlier build counts its one model.
- POST /api/local-model/install is allowed from installed: it adds a
  model, and the earlier one stays. A replaced job no longer deletes a
  model of the library, only an unchecked or partial download.
- POST /api/local-model/use switches to a checked model: a job of the
  two steps verify and connect. The stored digest is compared before
  anything is loaded (model-changed otherwise). Nothing is downloaded.
- DELETE /api/local-model/models deletes one model by its name, through
  the runtime's own delete, never by a path, never the model in use.
- Adding and switching answer 409 sessions-open while a live session
  uses the endpoint. Until such a job ends installed, a session start is
  refused with the new code job-unfinished.
- The view carries library (the state's entries joined with what the
  running runtime lists: ready, changed, missing, unverified) and store
  (the size of the blob folder). Both are cached and refreshed in the
  background, never read on the one-second poll.
- With nothing installed, remove is offered while the store still holds
  models an earlier Remove kept.

Window
- The installed screen lists the models on this Mac with the context
  each was checked with, its state and its size on disk, with Use and
  Delete per row (Delete asks once, in the row) and Add a model.
- In the chooser a model that is on this Mac already says so and is
  switched to without a download or a confirm screen.
- A paused or failed job that has a checked model beside it shows the
  way back to that model.

Every route is admin only in multi-user mode, like the existing ones.

Not run on a Mac: the tests use fakes and the fake runtime, and the
listing, store size and delete were checked against a real Ollama 0.13.0
on Linux, not the pinned 0.40.15 build for macOS.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Codeman maintainer committed 2026-11-10 23:52:14 +01:00
1 parent f62c1f7e2f
commit 70dc42b248
20 files changed
+4088 -201

No files matched your search

+54
View File
@@ -37,6 +37,7 @@ import {
OLLAMA_RUNTIME_PIN,
parseInferenceComputeTotal,
removeLocalModelFiles,
measureModelStore,
removePartialModelBlobs,
RUNTIME_CTX_CHECKPOINTS,
RUNTIME_DOWNLOAD_RETRIES,
@@ -1103,3 +1104,56 @@ describe('removePartialModelBlobs', () => {
expect(existsSync(join(outside, 'sha256-eee-partial'))).toBe(true);
});
});
describe('measureModelStore', () => {
const blob = (name: string): string => join(paths.modelsDir, 'blobs', name);
it('is the sizes of the files in the blob folder, a download that was interrupted included', async () => {
await mkdir(join(paths.modelsDir, 'blobs'), { recursive: true });
await mkdir(join(paths.modelsDir, 'manifests', 'registry.ollama.ai'), { recursive: true });
await writeFile(blob('sha256-aaa'), 'x'.repeat(1000));
await writeFile(blob('sha256-bbb'), 'y'.repeat(250));
await writeFile(blob('sha256-ccc-partial'), 'z'.repeat(40));
// The manifests are a few kilobytes beside gigabytes of layers: left out.
await writeFile(join(paths.modelsDir, 'manifests', 'registry.ollama.ai', 'latest'), '{"layers":[]}');
expect(await measureModelStore(paths)).toBe(1290);
});
it('is 0 without a model store, without a blob folder and for an empty one, and never throws', async () => {
expect(await measureModelStore(paths)).toBe(0);
await mkdir(paths.modelsDir, { recursive: true });
expect(await measureModelStore(paths)).toBe(0);
await mkdir(join(paths.modelsDir, 'blobs'), { recursive: true });
expect(await measureModelStore(paths)).toBe(0);
});
it('reads nothing and changes nothing: the store is as it was afterwards', async () => {
await mkdir(join(paths.modelsDir, 'blobs'), { recursive: true });
await writeFile(blob('sha256-aaa'), 'weights');
await writeFile(blob('sha256-bbb-partial'), 'half');
await measureModelStore(paths);
expect(await readFile(blob('sha256-aaa'), 'utf8')).toBe('weights');
expect(await readFile(blob('sha256-bbb-partial'), 'utf8')).toBe('half');
});
it('counts only regular files directly in the blob folder, and follows no link', async () => {
const outside = await outsideDir();
await writeFile(join(outside, 'huge'), 'h'.repeat(5000));
await mkdir(join(paths.modelsDir, 'blobs', 'a-folder'), { recursive: true });
await writeFile(join(paths.modelsDir, 'blobs', 'a-folder', 'inside'), 'i'.repeat(700));
await symlink(join(outside, 'huge'), blob('sha256-link'));
await writeFile(blob('sha256-aaa'), 'x'.repeat(10));
// The folder's file and the link's target are somebody else's: not counted.
expect(await measureModelStore(paths)).toBe(10);
});
it('a blob folder, a model store or a root that is a symlink counts as empty', async () => {
const outside = await outsideDir();
await writeFile(join(outside, 'sha256-eee'), 'e'.repeat(900));
await mkdir(paths.modelsDir, { recursive: true });
await symlink(outside, join(paths.modelsDir, 'blobs'));
expect(await measureModelStore(paths)).toBe(0);
// A path that is not inside the local model folder is refused before anything is read.
expect(await measureModelStore({ ...paths, modelsDir: outside })).toBe(0);
});
});