modeld: the local inference daemon
modeld is the local inference daemon for Contenox Runtime: a separate process that runs on your machine (or a remote GPU box) and owns hardware resources on behalf of every frontend — the CLI, the VS Code extension, Beam, and any ACP editor — talking to the pure-Go runtime over gRPC. It serves GGUF models through llama.cpp and OpenVINO GenAI models through a shared session contract, so the runtime itself never links native inference code. A single modeld binary registered as a remote backend serves a laptop and a dedicated GPU box through the exact same protocol, and later builds added native vision (VLM) support through llama.cpp’s multimodal stack, with capability truth reported per model rather than inferred from its name.
See the Modeld architecture and transport, with a desktop walkthrough. Setup and build: local model setup, building the worker.
The session contract#
The seam is owned by the consumer: the runtime declares the session interface and modeld implements it, so the runtime never imports the daemon. It is not a chat API and not a bare token stream. A session is opened against a model and then driven with EnsurePrefix — keep the stable prompt prefix’s KV hot — PrefillSuffix for only the part that changed, and Decode, plus Snapshot/Restore, Describe, Embed, and an ExplainContext report. Every call answers with real numbers: tokens reused, dropped, prefilled and resident, remaining capacity, and a manifest digest. A raw token-level boundary was built first, as an in-memory implementation stress-checked against both real backends, and then replaced — both engines sit at a higher altitude than raw tokens, and OpenVINO GenAI in particular holds the tokenizer and chat template internally and caches a string prefix, which a token-only daemon could not honour. Reuse is therefore keyed on a context manifest, never on byte equality: profile id, backend version, model digest, prompt-template digest, a runtime digest covering context size, batch, GPU layers, tensor split, flash attention and KV type, BOS policy, and per-segment byte and token ranges with their hashes. Two gates must pass — runtime compatibility first, then a token-level longest-common-prefix match — and a suffix is only accepted if tokenizing the whole prompt cold produces exactly the warm path’s stable tokens plus the suffix tokens. A tokenizer merge across the split boundary is rejected as a manifest mismatch.
A ~9,000-token stable prefix cost 1.77 s to prefill cold and 0.03 s warm on a 6 GB laptop GPU, reusing all 9,350 tokens and prefilling none, turning a 1.97 s turn into a 0.18 s one. On CPU the same shape ran 7.61 s cold against 73 ms warm, and editing the stable prefix changed its hash and sent the next turn straight back to a full prefill. The stable-prefix hash predicted a cache hit.
The capacity planner#
On opening a session the planner reads the model’s own metadata — layer count, KV head count, head dimension, KV dtype, architecture, and the declared sliding-window size cross-checked against what the loaded model actually reports — takes a live free-memory snapshot of the target device, subtracts weights, runtime overhead, a policy headroom and a floor reserved for every other process on the card, then inverts the remaining budget through GQA-correct KV arithmetic sized from KV heads rather than attention heads. What comes out is not one number but six, deliberately never collapsed: the model’s own ceiling, what was requested, what raw memory allowed, the dense window actually served, the physically hot KV window, and the planner’s logical window once a host-RAM cold budget is counted — each carrying a machine-readable reason when something was the binding constraint. Every capacity answer comes from the resident session’s own open-time resolution, so one session cannot report contradictory answers to the same question. Quantized KV is costed at its real byte width. GPU layer offload is resolved against the requested context rather than maximised blindly, because a fully offloaded model with an unusable context window is a worse answer than a partially offloaded one that works.
Beyond the hot window, residency is shared policy: one pure-Go planner decides what to keep and what to park, and a small per-backend executor carries it out — llama.cpp by native KV surgery, removing a range and sliding the tail’s positions down, and OpenVINO, whose KV is not range-addressable, by exporting the block to a cold store and re-prefilling the survivors. Same interface, two different mechanisms, with each backend reporting its real capabilities — tail removal, middle removal, position shift, sparse attention and its window, cold store, recompute. Streaming eviction cannot be planned from a manifest, since freshly streamed tokens have no manifest blocks yet; it pins the attention sink at the head and the recent tail, parks the middle to cold storage in fixed 256-token blocks, and runs independently of the manifest, which let a conversation carry roughly 7,500 tokens of history through a ~3,000-token hot window with no overflow.
Ownership and lifecycle#
A cooperative, cross-process TTL lease file gives one daemon per data root a single owner: race-free cold acquisition through exclusive create, expired takeover through atomic overwrite and read-back, and the owner’s instance UUID as a fencing token checked on every cross-process call — fencing, explicitly, not authentication. The lease record also carries the daemon’s endpoint, so discovery is one atomic file read, and probing it alongside the binary’s location yields four distinct states — not installed, not running, stale, running — each with its own typed error and its own remedy. CLI, editor and browser sessions can share one resident model without a central server process owning everything; one backend session served four CLI invocations spanning two conversations. Inside the daemon a monotonic slot generation is bumped on every load, unload, model switch, owner takeover and fatal backend reset, so a handle minted before any of those fails cleanly instead of addressing the wrong model; one operation at a time holds the slot, with a whole turn counting as a single protected operation; and an idle reaper returns memory to the system after inactivity while keeping implicit sessions resident, so a warm model outlives the CLI process that created it. Errors are typed — busy, not active, switch required, insufficient memory, stale generation, backend mismatch — and anything that makes resident-token bookkeeping untrustworthy closes the session as fatal and evicts the slot.
Problems it solved#
- “Why did this just OOM.” Effective context is computed from an actual memory snapshot plus real KV math — GQA-correct, sliding-window aware, quantization aware.
- Safe prefix reuse. Manifest identity rather than byte equality.
- Shared local models. Lease-based ownership lets many frontends use one resident model.
- Warm state. A stateful session boundary lets the daemon own session lifetime, which warm KV, prefix reuse and durable snapshots need. A snapshot round-trip through a fresh session reproduced a greedy continuation exactly at f16 KV precision; the default 8-bit cache was measurably lossy.
- Unusable models that look valid. Metadata parsing, loader support, pipeline support, device support, memory fit and certified quality are six independent questions and only their intersection is a usable model. A GGUF can declare an architecture the linked runtime cannot load while its numeric metadata still parses; device enumeration is likewise not device support, so capability is reported rather than inferred.
- Misleading benchmarks. Every measurement is tagged by the path that produced it — the shipped product path, a raw backend control, or a microbenchmark — and a run that produced zero tokens because of a launcher failure, a missing library, or a context rejection is recorded as a failed run rather than as throughput.
- Silent degradation. An explicit, typed context-overflow refusal serves an agent better than a quietly truncated or CPU-spilled response, and the refusal carries the live context number.