Last updated: 2026-06-12

Managed Models

Managed

All API Sections

Managed model cache, active-model selection, and download helpers.

These helpers back the `zinc model ...` CLI and the server-side active-model switching flow.

25 exports 4 methods src/model/managed.zig

25 exports shown

struct

RuntimePaths

#
pub const RuntimePaths = struct

Resolved cache and config directory paths for the current platform.

src/model/managed.zig:13

Methods

1

method

RuntimePaths.deinit

#
pub fn deinit(self: *RuntimePaths, allocator: std.mem.Allocator) void

Frees the owned path slices and invalidates the struct.

src/model/managed.zig:18

struct

ModelFit

#
pub const ModelFit = struct

VRAM budget check result for a single catalog model.

src/model/managed.zig:26

struct

ActiveSelection

#
pub const ActiveSelection = struct

The currently active managed model as persisted in the config directory.

src/model/managed.zig:40

Methods

1

method

ActiveSelection.deinit

#
pub fn deinit(self: *ActiveSelection, allocator: std.mem.Allocator) void

Frees the owned model_id slice and invalidates the struct.

src/model/managed.zig:45

struct

CachedGpuProfile

#
pub const CachedGpuProfile = struct

Cached GPU capability profile used to avoid re-probing the device on every run.

src/model/managed.zig:52

Methods

1

method

CachedGpuProfile.deinit

#
pub fn deinit(self: *CachedGpuProfile, allocator: std.mem.Allocator) void

Frees the owned string slices and invalidates the struct.

src/model/managed.zig:59

struct

InstalledManifest

#
pub const InstalledManifest = struct

On-disk manifest written alongside an installed GGUF model file.

`sha256` is optional: hand-rolled manifests for symlinked local files (e.g. `download_url:"local"`) frequently omit it because the user can't or won't precompute the digest of an existing GGUF. Treat it as a hint, not a required field — the catalog's `sha256` is the source of truth when a checksum needs to be enforced.

src/model/managed.zig:73

Methods

1

method

InstalledManifest.deinit

#
pub fn deinit(self: *InstalledManifest, allocator: std.mem.Allocator) void

Frees the owned sha256 slice (if any) and invalidates the struct.

src/model/managed.zig:83

struct

RemoveInstalledModelResult

#
pub const RemoveInstalledModelResult = struct

Outcome of a model removal: which artifacts were actually deleted.

src/model/managed.zig:90

struct

DownloadObserver

#
pub const DownloadObserver = struct

Callback hooks for observing model download lifecycle events.

src/model/managed.zig:97

function

runtimePaths

#
pub fn runtimePaths(allocator: std.mem.Allocator) !RuntimePaths

Returns the resolved cache and config root directories for the current platform.

src/model/managed.zig:195

function

resolveInstalledModelPath

#
pub fn resolveInstalledModelPath(model_id: []const u8, allocator: std.mem.Allocator) ![]u8

Returns the absolute path to the installed GGUF file for the given model id.

Parameters

model_id
Catalog model identifier (e.g. `"qwen3-2b"`).
allocator
Used to build the path; caller owns the returned slice.

Returns

Heap-allocated absolute path `<cache_root>/models/<model_id>/model.gguf`.

src/model/managed.zig:207

function

resolveManifestPath

#
pub fn resolveManifestPath(model_id: []const u8, allocator: std.mem.Allocator) ![]u8

Returns the absolute path to the manifest JSON for the given model id.

Parameters

model_id
Catalog model identifier.
allocator
Used to build the path; caller owns the returned slice.

Returns

Heap-allocated absolute path `<cache_root>/models/<model_id>/manifest.json`.

src/model/managed.zig:218

function

resolveActiveConfigPath

#
pub fn resolveActiveConfigPath(allocator: std.mem.Allocator) ![]u8

Returns the absolute path to the active-model config file.

src/model/managed.zig:225

function

resolveGpuProfileCachePath

#
pub fn resolveGpuProfileCachePath(device_index: u32, allocator: std.mem.Allocator) ![]u8

Returns the absolute path to the cached GPU profile JSON for the given device index.

Parameters

device_index
Zero-based index of the GPU device; encoded in the filename.
allocator
Used to build the path; caller owns the returned slice.

Returns

Heap-allocated absolute path `<config_root>/gpu-profile-device-<device_index>.json`.

src/model/managed.zig:236

function

isInstalled

#
pub fn isInstalled(model_id: []const u8, allocator: std.mem.Allocator) bool

Returns true if the model GGUF file exists in the local cache.

src/model/managed.zig:243

function

readActiveSelection

#
pub fn readActiveSelection(allocator: std.mem.Allocator) !?ActiveSelection

Reads the persisted active-model selection, or returns null if none is set.

src/model/managed.zig:251

function

writeActiveSelection

#
pub fn writeActiveSelection(model_id: []const u8, allocator: std.mem.Allocator) !void

Persists the given model id as the active selection with the current timestamp.

src/model/managed.zig:277

function

clearActiveSelection

#
pub fn clearActiveSelection(allocator: std.mem.Allocator) !bool

Removes the active-model config file.

Returns true if a file was deleted.

src/model/managed.zig:297

function

clearActiveSelectionIfMatches

#
pub fn clearActiveSelectionIfMatches(model_id: []const u8, allocator: std.mem.Allocator) !bool

Clears the active selection only if it currently points to the given model id.

src/model/managed.zig:304

function

readCachedGpuProfile

#
pub fn readCachedGpuProfile(device_index: u32, allocator: std.mem.Allocator) !?CachedGpuProfile

Reads the cached GPU profile for the given device, or returns null if not cached.

src/model/managed.zig:316

function

writeCachedGpuProfile

#
pub fn writeCachedGpuProfile( device_index: u32, profile: []const u8, device_name: []const u8, vram_budget_bytes: u64, allocator: std.mem.Allocator, ) !void

Persists a GPU capability profile to disk for the given device index.

Overwrites any existing cache file for that device.

Parameters

device_index
Zero-based GPU index; used to derive the cache file name.
profile
Opaque profile string (e.g. `"rdna4"`) returned by the diagnostics layer.
device_name
Human-readable device name stored for informational display.
vram_budget_bytes
Effective VRAM budget in bytes for this device.
allocator
Used only for path construction; nothing is retained after the call.

src/model/managed.zig:354

function

describeFit

#
pub fn describeFit(entry: catalog.CatalogEntry, vram_budget_bytes: u64, allocator: std.mem.Allocator) !ModelFit

Returns a VRAM fit assessment for a catalog model against the given budget.

If the model is installed, the on-disk manifest is consulted first (most accurate); if the manifest lacks VRAM fields, the installed GGUF is inspected directly and the manifest is updated. For models that are not yet installed, catalog static estimates are used and `ModelFit.exact` is false.

Parameters

entry
Catalog entry describing the model to assess.
vram_budget_bytes
Available VRAM in bytes on the target device.
allocator
Used for path resolution and manifest I/O; nothing is retained.

Returns

`ModelFit` with exact=true when data came from the installed model (manifest or file inspection), false for catalog estimates.

src/model/managed.zig:394

function

verifyActiveSelectionFits

#
pub fn verifyActiveSelectionFits(model_id: []const u8, vram_budget_bytes: u64, allocator: std.mem.Allocator) !ModelFit

Verifies that the named model is installed and fits in the given VRAM budget.

Returns `error.UnknownManagedModel` if `model_id` is not in the catalog, or `error.ModelNotInstalled` if the GGUF file is absent from the local cache.

Parameters

model_id
Catalog model identifier to check.
vram_budget_bytes
Available VRAM in bytes on the target device.
allocator
Forwarded to `describeFit` for path and manifest I/O.

Returns

`ModelFit` describing the exact VRAM requirement and fit state.

src/model/managed.zig:459

function

removeInstalledModel

#
pub fn removeInstalledModel(model_id: []const u8, allocator: std.mem.Allocator) !RemoveInstalledModelResult

Deletes an installed model's GGUF, manifest, and (if empty) its directory.

src/model/managed.zig:466

function

pullModel

#
pub fn pullModel(entry: catalog.CatalogEntry, allocator: std.mem.Allocator, writer: anytype) !void

Downloads and installs a model from the catalog, verifying its sha256 checksum.

src/model/managed.zig:478

function

pullModelWithObserver

#
pub fn pullModelWithObserver( entry: catalog.CatalogEntry, allocator: std.mem.Allocator, writer: anytype, observer: ?*const DownloadObserver, ) !void

Downloads and installs a model, reporting progress via an optional observer.

If the model is already installed and its sha256 matches the catalog, the function returns immediately without re-downloading. A `.partial` staging file is used so an interrupted download does not corrupt the cache.

Parameters

entry
Catalog entry that supplies the download URL and expected sha256.
allocator
Used for HTTP client, path construction, and temporary buffers.
writer
Receives human-readable status lines and the download progress bar.
observer
Optional lifecycle callbacks for programmatic progress tracking; pass null to skip.

src/model/managed.zig:491

function

bytesToGiB

#
pub fn bytesToGiB(bytes: u64) f64

Converts a byte count to gibibytes (GiB) as a floating-point value.

src/model/managed.zig:630