Merge #804: a library of models on this Mac, with sizes, delete, add and switch

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Codeman maintainer committed 2026-11-11 20:15:34 +01:00
commit f07cbccb0b
26 files changed
+4244 -227

No files matched your search

File diff suppressed because it is too large. Load diff
+39
View File
@@ -808,6 +808,45 @@ export async function removePartialModelBlobs(paths?: LocalModelPaths): Promise<
return freed;
}
/** How many files of the blob folder are counted at most: a store holds a few dozen, and a walk must end. */
const STORE_MEASURE_MAX_FILES = 20_000;
/**
* What the model store takes on disk: the sizes of the regular files directly in
* `<models>/blobs`, summed. That folder is flat (one file per layer, plus the partial files of
* a download), so this is the store's real disk use, shared layers counted once, an
* interrupted download included. The manifests beside it are a few kilobytes and are left out.
*
* One `readdir` and one `lstat` per file, at most `STORE_MEASURE_MAX_FILES` of them. Nothing is
* followed: a `root`, a model store or a blob folder that is a symbolic link counts as empty,
* and so does a link in the place of a file. Never outside `LocalModelPaths.root`. Resolves 0
* when there is no store. Never throws.
*/
export async function measureModelStore(paths?: LocalModelPaths): Promise<number> {
let total = 0;
try {
const resolved = paths ?? localModelPaths(OLLAMA_RUNTIME_PIN.version);
const blobs = join(resolved.modelsDir, 'blobs');
if (!isInsideLocalModelRoot(blobs, resolved.root)) return 0;
for (const folder of [resolved.root, resolved.modelsDir, blobs]) {
const info = await lstat(folder);
if (info.isSymbolicLink() || !info.isDirectory()) return 0;
}
const names = await readdir(blobs);
for (const name of names.slice(0, STORE_MEASURE_MAX_FILES)) {
try {
const info = await lstat(join(blobs, name));
if (info.isFile()) total += info.size;
} catch {
// Gone between the listing and the look: a download or a delete is under way.
}
}
} catch {
// No model store yet.
}
return total;
}
// ---------- Port ----------
/** Whether nothing holds `port` on 127.0.0.1, decided by binding it and letting go again. */
+39
View File
@@ -26,6 +26,7 @@ import {
type LocalModelErrorCode,
type LocalModelInstallState,
type LocalModelInstallStep,
type LocalModelLibraryEntry,
type LocalModelStateFile,
} from '../types.js';
import { OLLAMA_RUNTIME_PIN } from './runtime.js';
@@ -153,6 +154,41 @@ function parseConnection(value: unknown): LocalModelStateFile['connection'] {
: undefined;
}
/** How many checked models a state file may list: far more than one Mac holds, and a bound on what a damaged file can cost. */
const MAX_LIBRARY_ENTRIES = 64;
/**
* The checked models of a file. An entry without its id, label, context or digest is dropped
* (it could never be switched to), and so is a second entry for an id. `undefined` for a file
* that has no list, which is what an earlier build wrote.
*/
function parseLibrary(value: unknown): LocalModelLibraryEntry[] | undefined {
if (!Array.isArray(value)) return undefined;
const entries: LocalModelLibraryEntry[] = [];
for (const item of value) {
if (entries.length >= MAX_LIBRARY_ENTRIES) break;
if (!isRecord(item)) continue;
const id = text(item.id, 200);
const label = text(item.label, 200);
const context = count(item.context);
const digest = text(item.digest, 200);
if (!id || !label || !context || !digest || entries.some((entry) => entry.id === id)) continue;
const bytes = count(item.bytes);
const cli = parseCli(item.cli);
const verifiedAt = text(item.verifiedAt, 64);
entries.push({
id,
label,
context,
digest,
...(bytes ? { bytes } : {}),
...(cli ? { cli } : {}),
...(verifiedAt ? { verifiedAt } : {}),
});
}
return entries;
}
/**
* The state a file's text stands for. Pure and total: text that is not a JSON object of
* `schema: 1` with a known `state` is `IDLE_LOCAL_MODEL_STATE`. Members it does not know are
@@ -180,6 +216,7 @@ export function parseLocalModelState(raw: string): LocalModelStateFile {
const connection = parseConnection(parsed.connection);
const updatedAt = text(parsed.updatedAt, 64);
const replacedModelId = text(parsed.replacedModelId, 200);
const library = parseLibrary(parsed.library);
if (!model || !cli) return IDLE_LOCAL_MODEL_STATE;
if (state === 'installed') {
@@ -192,6 +229,7 @@ export function parseLocalModelState(raw: string): LocalModelStateFile {
cli,
runtime,
...(loaded ? { loaded } : {}),
...(library ? { library } : {}),
...(connection ? { connection } : {}),
...(updatedAt ? { updatedAt } : {}),
};
@@ -215,6 +253,7 @@ export function parseLocalModelState(raw: string): LocalModelStateFile {
...(runtime ? { runtime } : {}),
...(progress ? { progress } : {}),
...(loaded ? { loaded } : {}),
...(library ? { library } : {}),
...(connection ? { connection } : {}),
...(replacedModelId ? { replacedModelId } : {}),
...(updatedAt ? { updatedAt } : {}),