Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

feat(engine): cap ctx presets at the model's trained limit from the GGUF header (ENG-6c)

purple9d51a98c684a
diff --git a/e2e/builtin-ctx.spec.ts b/e2e/builtin-ctx.spec.ts
index 076d5fa8..4f88a18e 100644
--- a/e2e/builtin-ctx.spec.ts
+++ b/e2e/builtin-ctx.spec.ts
@@ -56,7 +56,12 @@ test('context dropdown relaunches the built-in engine and the counter follows',
await expect(page.getByText(/\/8\.2k/)).toBeVisible()
// Pick 16K → apply() persists tuning.ctx and swaps the running engine.
+ // The preset list is capped at the model's TRAINED ceiling (32k from the
+ // GGUF header via the listing) — no 64K/128K options for a 32k model.
await trigger.click()
+ await expect(page.getByRole('button', { name: /^32K$/ })).toBeVisible()
+ await expect(page.getByRole('button', { name: /^64K$/ })).toHaveCount(0)
+ await expect(page.getByRole('button', { name: /^128K$/ })).toHaveCount(0)
await page.getByRole('button', { name: /^16K$/ }).click()
await expect(page.getByRole('button', { name: /ctx 16K/ })).toBeVisible({ timeout: 10_000 })
diff --git a/e2e/support/tauri-mock.ts b/e2e/support/tauri-mock.ts
index a2cab497..04970cdc 100644
--- a/e2e/support/tauri-mock.ts
+++ b/e2e/support/tauri-mock.ts
@@ -115,7 +115,9 @@ export function tauriMockInit(opts: TauriMockOptions) {
case 'list_bundled_models':
return Promise.resolve({
dir: MODELS_DIR,
- models: [{ name: opts.modelName, path: modelPath, size: 400 * 1024 * 1024, loaded: true }],
+ // ctx_train mirrors the GGUF-header read (ENG-6c) — 32k, like the
+ // real Qwen2.5 starter — so specs can assert the preset cap.
+ models: [{ name: opts.modelName, path: modelPath, size: 400 * 1024 * 1024, loaded: true, ctx_train: 32768 }],
})
// ── built-in EMBEDDINGS server lifecycle (P5) ─────────────────
diff --git a/src-tauri/src/commands/engine.rs b/src-tauri/src/commands/engine.rs
index ab30ce98..58fe4f2e 100644
--- a/src-tauri/src/commands/engine.rs
+++ b/src-tauri/src/commands/engine.rs
@@ -616,11 +616,16 @@ fn list_bundled_models_blocking(state: &AppState) -> Result<serde_json::Value, S
.into_iter()
.map(|m| {
let is_loaded = loaded.as_deref() == Some(m.path.as_str());
+ // Trained context limit from the GGUF header (ENG-6c) — the ONLY
+ // place it exists; /props and /v1/models don't carry it. None on
+ // any parse hiccup so a weird file can never break the listing.
+ let ctx_train = crate::commands::gguf::context_length(&m.path);
serde_json::json!({
"name": m.name,
"path": m.path,
"size": m.size,
"loaded": is_loaded,
+ "ctx_train": ctx_train,
})
})
.collect();
diff --git a/src-tauri/src/commands/gguf.rs b/src-tauri/src/commands/gguf.rs
new file mode 100644
index 00000000..c50ad14a
--- /dev/null
+++ b/src-tauri/src/commands/gguf.rs
@@ -0,0 +1,272 @@
+//! Minimal, defensive GGUF header reader (ENG-6c).
+//!
+//! The bundled llama-server exposes neither `n_ctx_train` on `/props` nor any
+//! model metadata on `/v1/models` (verified against b1-049326a), so the only
+//! source for a model's TRAINED context limit is the GGUF header itself:
+//! `general.architecture` = "<arch>", then `<arch>.context_length`. The
+//! Context dropdown uses it to cap its presets — without it a 32k model
+//! happily "accepts" -c 131072 and degrades silently (llama.cpp only warns).
+//!
+//! Deliberately paranoid: this runs against user-downloaded files inside
+//! `list_bundled_models`, so it must NEVER panic or stall the listing —
+//! every failure path is `None`, reads are bounded, and only the header is
+//! touched (a few KB of a multi-GB file).
+
+use std::fs::File;
+use std::io::{BufReader, Read};
+
+const GGUF_MAGIC: &[u8; 4] = b"GGUF";
+/// Real models carry a few dozen KV pairs; hundreds is already absurd.
+const MAX_KV: u64 = 4096;
+/// Longest key/string value we are willing to read (keys are ~30 chars).
+const MAX_STR: u64 = 64 * 1024;
+/// Longest metadata array we are willing to SKIP element-by-element
+/// (tokenizer vocabularies easily reach 150k entries).
+const MAX_ARR: u64 = 10_000_000;
+
+struct R<'a>(&'a mut dyn Read);
+
+impl R<'_> {
+ fn u8(&mut self) -> Option<u8> {
+ let mut b = [0u8; 1];
+ self.0.read_exact(&mut b).ok()?;
+ Some(b[0])
+ }
+ fn u32(&mut self) -> Option<u32> {
+ let mut b = [0u8; 4];
+ self.0.read_exact(&mut b).ok()?;
+ Some(u32::from_le_bytes(b))
+ }
+ fn u64(&mut self) -> Option<u64> {
+ let mut b = [0u8; 8];
+ self.0.read_exact(&mut b).ok()?;
+ Some(u64::from_le_bytes(b))
+ }
+ fn skip(&mut self, n: u64) -> Option<()> {
+ // io::copy into a sink honors non-seekable readers and bounds memory.
+ // (&mut *) reborrows the trait object as a Sized `&mut dyn Read` so
+ // `take` can consume it.
+ let copied = std::io::copy(&mut (&mut *self.0).take(n), &mut std::io::sink()).ok()?;
+ (copied == n).then_some(())
+ }
+ fn string(&mut self, max: u64) -> Option<String> {
+ let len = self.u64()?;
+ if len > max {
+ return None;
+ }
+ let mut buf = vec![0u8; len as usize];
+ self.0.read_exact(&mut buf).ok()?;
+ String::from_utf8(buf).ok()
+ }
+}
+
+/// GGUF metadata value types (spec v3). Returns the byte size of FIXED-width
+/// types; strings/arrays are handled by the caller.
+fn fixed_size(ty: u32) -> Option<u64> {
+ match ty {
+ 0 | 1 | 7 => Some(1), // u8 / i8 / bool
+ 2 | 3 => Some(2), // u16 / i16
+ 4 | 5 | 6 => Some(4), // u32 / i32 / f32
+ 10 | 11 | 12 => Some(8), // u64 / i64 / f64
+ _ => None, // 8 = string, 9 = array, unknown
+ }
+}
+
+enum Val {
+ UInt(u64),
+ Str(String),
+ Other,
+}
+
+fn read_value(r: &mut R, ty: u32) -> Option<Val> {
+ match ty {
+ 4 => Some(Val::UInt(r.u32()? as u64)),
+ 10 => Some(Val::UInt(r.u64()?)),
+ 0 => Some(Val::UInt(r.u8()? as u64)),
+ 2 => {
+ let mut b = [0u8; 2];
+ r.0.read_exact(&mut b).ok()?;
+ Some(Val::UInt(u16::from_le_bytes(b) as u64))
+ }
+ 8 => Some(Val::Str(r.string(MAX_STR)?)),
+ 9 => {
+ // array: elem type + count, then elements — skip it whole.
+ let elem_ty = r.u32()?;
+ let count = r.u64()?;
+ if count > MAX_ARR {
+ return None;
+ }
+ if let Some(sz) = fixed_size(elem_ty) {
+ r.skip(count.checked_mul(sz)?)?;
+ } else if elem_ty == 8 {
+ for _ in 0..count {
+ let len = r.u64()?;
+ if len > MAX_STR {
+ return None;
+ }
+ r.skip(len)?;
+ }
+ } else {
+ return None; // nested arrays / unknown — bail out
+ }
+ Some(Val::Other)
+ }
+ _ => {
+ r.skip(fixed_size(ty)?)?;
+ Some(Val::Other)
+ }
+ }
+}
+
+/// The context length the model was TRAINED with (`<arch>.context_length`),
+/// or `None` when the file is missing, not GGUF, truncated, or simply does
+/// not carry the key. Never panics, never reads past the header.
+pub fn context_length(path: &str) -> Option<u32> {
+ let file = File::open(path).ok()?;
+ let mut buf = BufReader::new(file);
+ let mut r = R(&mut buf);
+
+ let mut magic = [0u8; 4];
+ r.0.read_exact(&mut magic).ok()?;
+ if &magic != GGUF_MAGIC {
+ return None;
+ }
+ let version = r.u32()?;
+ if !(2..=3).contains(&version) {
+ return None;
+ }
+ let _tensor_count = r.u64()?;
+ let kv_count = r.u64()?;
+ if kv_count > MAX_KV {
+ return None;
+ }
+
+ let mut arch: Option<String> = None;
+ let mut ctx_by_key: Option<(String, u64)> = None;
+
+ for _ in 0..kv_count {
+ let key = r.string(MAX_STR)?;
+ let ty = r.u32()?;
+ let val = read_value(&mut r, ty)?;
+ match val {
+ Val::Str(s) if key == "general.architecture" => arch = Some(s),
+ Val::UInt(n) if key.ends_with(".context_length") => {
+ ctx_by_key = Some((key, n));
+ }
+ _ => {}
+ }
+ // Early exit once both halves are known and they agree.
+ if let (Some(a), Some((k, n))) = (&arch, &ctx_by_key) {
+ if k == &format!("{a}.context_length") {
+ return u32::try_from(*n).ok();
+ }
+ }
+ }
+
+ // Architecture key came AFTER the context key (order is unspecified), or
+ // the arch never showed up — accept any single *.context_length match.
+ match (arch, ctx_by_key) {
+ (Some(a), Some((k, n))) if k == format!("{a}.context_length") => u32::try_from(n).ok(),
+ (None, Some((_, n))) => u32::try_from(n).ok(),
+ _ => None,
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::context_length;
+ use std::io::Write;
+
+ // Tiny hand-rolled GGUF v3 writer — just enough for the header path.
+ struct W(Vec<u8>);
+ impl W {
+ fn new(kv_count: u64) -> Self {
+ let mut v = Vec::new();
+ v.extend_from_slice(b"GGUF");
+ v.extend_from_slice(&3u32.to_le_bytes()); // version
+ v.extend_from_slice(&0u64.to_le_bytes()); // tensor_count
+ v.extend_from_slice(&kv_count.to_le_bytes());
+ W(v)
+ }
+ fn str_field(&mut self, key: &str, val: &str) {
+ self.key(key);
+ self.0.extend_from_slice(&8u32.to_le_bytes());
+ self.0.extend_from_slice(&(val.len() as u64).to_le_bytes());
+ self.0.extend_from_slice(val.as_bytes());
+ }
+ fn u32_field(&mut self, key: &str, val: u32) {
+ self.key(key);
+ self.0.extend_from_slice(&4u32.to_le_bytes());
+ self.0.extend_from_slice(&val.to_le_bytes());
+ }
+ fn arr_str_field(&mut self, key: &str, items: &[&str]) {
+ self.key(key);
+ self.0.extend_from_slice(&9u32.to_le_bytes()); // array
+ self.0.extend_from_slice(&8u32.to_le_bytes()); // of strings
+ self.0.extend_from_slice(&(items.len() as u64).to_le_bytes());
+ for s in items {
+ self.0.extend_from_slice(&(s.len() as u64).to_le_bytes());
+ self.0.extend_from_slice(s.as_bytes());
+ }
+ }
+ fn key(&mut self, key: &str) {
+ self.0.extend_from_slice(&(key.len() as u64).to_le_bytes());
+ self.0.extend_from_slice(key.as_bytes());
+ }
+ fn write_to(&self, path: &std::path::Path) {
+ let mut f = std::fs::File::create(path).unwrap();
+ f.write_all(&self.0).unwrap();
+ }
+ }
+
+ fn tmp(name: &str) -> std::path::PathBuf {
+ let dir = std::env::temp_dir().join("lu-gguf-tests");
+ std::fs::create_dir_all(&dir).unwrap();
+ dir.join(name)
+ }
+
+ #[test]
+ fn reads_context_length_behind_skipped_values() {
+ let mut w = W::new(4);
+ w.str_field("general.name", "unit test model");
+ w.arr_str_field("tokenizer.ggml.tokens", &["a", "b", "c"]);
+ w.str_field("general.architecture", "qwen2");
+ w.u32_field("qwen2.context_length", 32768);
+ let p = tmp("ok.gguf");
+ w.write_to(&p);
+ assert_eq!(context_length(p.to_str().unwrap()), Some(32768));
+ }
+
+ #[test]
+ fn context_key_before_architecture_still_resolves() {
+ let mut w = W::new(2);
+ w.u32_field("llama.context_length", 8192);
+ w.str_field("general.architecture", "llama");
+ let p = tmp("reversed.gguf");
+ w.write_to(&p);
+ assert_eq!(context_length(p.to_str().unwrap()), Some(8192));
+ }
+
+ #[test]
+ fn garbage_missing_and_truncated_files_are_none() {
+ let p = tmp("garbage.gguf");
+ std::fs::write(&p, b"MZ\x90definitely not a gguf").unwrap();
+ assert_eq!(context_length(p.to_str().unwrap()), None);
+ assert_eq!(context_length("/nonexistent/nope.gguf"), None);
+
+ // Valid magic, then the file just ends mid-header.
+ let t = tmp("truncated.gguf");
+ std::fs::write(&t, b"GGUF\x03\x00\x00\x00").unwrap();
+ assert_eq!(context_length(t.to_str().unwrap()), None);
+ }
+
+ #[test]
+ fn real_bundled_gguf_if_present() {
+ // Best-effort against the dev machine's local test model; absent in CI.
+ let home = std::env::var("HOME").unwrap_or_default();
+ let p = format!("{home}/Library/Application Support/Locally Uncensored/models/Qwen2.5-0.5B-Instruct-Q8_0.gguf");
+ if std::path::Path::new(&p).exists() {
+ assert_eq!(super::context_length(&p), Some(32768));
+ }
+ }
+}
diff --git a/src-tauri/src/commands/mod.rs b/src-tauri/src/commands/mod.rs
index e5b76c31..3fde7051 100644
--- a/src-tauri/src/commands/mod.rs
+++ b/src-tauri/src/commands/mod.rs
@@ -5,6 +5,7 @@ pub mod comfy_ws;
pub mod download;
pub mod engine;
pub mod filesystem;
+pub mod gguf;
pub mod gpu;
pub mod health;
pub mod install;
diff --git a/src/api/engine.ts b/src/api/engine.ts
index 4d3b21a6..d12be627 100644
--- a/src/api/engine.ts
+++ b/src/api/engine.ts
@@ -35,6 +35,10 @@ export interface BundledModel {
size: number
/** Whether this model is the one currently loaded by the engine. */
loaded: boolean
+ /** Context length the model was TRAINED with, read from the GGUF header
+ * (ENG-6c). null/absent when the header doesn't carry it — presets then
+ * stay uncapped, exactly the pre-2.6.0 behavior. */
+ ctx_train?: number | null
}
export interface EngineStatus {
@@ -59,6 +63,16 @@ export function embedBaseUrl(): string {
// activate a model by its picker id (which is what the model store carries)
// without threading the path through AIModel, which has no path field.
const pathByName = new Map<string, string>()
+// name → trained context limit (GGUF header). Feeds the Context dropdown's
+// preset cap via useActiveContextWindow.modelMax.
+const ctxTrainByName = new Map<string, number>()
+
+/** Trained context limit of a bundled model by picker id (`openai::<name>`
+ * or bare), or 0 when unknown / listing not fetched yet (= uncapped). */
+export function bundledCtxTrain(nameOrPrefixed: string): number {
+ const name = nameOrPrefixed.includes('::') ? nameOrPrefixed.split('::')[1] : nameOrPrefixed
+ return ctxTrainByName.get(name) ?? 0
+}
/**
* True when the active OpenAI-compat backend is the app-managed built-in engine
@@ -150,7 +164,11 @@ export async function listBundledModels(): Promise<BundledModel[]> {
const res = await backendCall<{ dir: string; models: BundledModel[] }>('list_bundled_models')
const models = res?.models ?? []
pathByName.clear()
- for (const m of models) pathByName.set(m.name, m.path)
+ ctxTrainByName.clear()
+ for (const m of models) {
+ pathByName.set(m.name, m.path)
+ if (typeof m.ctx_train === 'number' && m.ctx_train > 0) ctxTrainByName.set(m.name, m.ctx_train)
+ }
return models
}
diff --git a/src/hooks/useActiveContextWindow.ts b/src/hooks/useActiveContextWindow.ts
index 48626e0c..0fe42e0f 100644
--- a/src/hooks/useActiveContextWindow.ts
+++ b/src/hooks/useActiveContextWindow.ts
@@ -7,7 +7,7 @@ import { getLmStudioModelContext } from '../api/lmstudio'
import { getModelMaxTokens } from '../lib/context-compaction'
import { effectiveContextWindow } from '../lib/context-window'
import { isManagedBuiltinSlot } from '../api/builtin-ensure'
-import { bundledEngineStatus } from '../api/engine'
+import { bundledEngineStatus, bundledCtxTrain } from '../api/engine'
export type CtxProvider = 'ollama' | 'lmstudio' | 'builtin' | 'cloud' | 'unknown'
@@ -85,11 +85,15 @@ export function useActiveContextWindow(reloadTick = 0): ActiveContext {
if (providerId === 'openai' && isManagedBuiltinSlot()) {
const status = await bundledEngineStatus().catch(() => null)
if (cancelled) return
+ // Trained ceiling from the GGUF header (via the model listing) caps
+ // the dropdown presets; 0 = unknown = uncapped (pre-listing or a
+ // header without the key).
+ const modelMax = bundledCtxTrain(activeModel)
if (status?.running && typeof status.ctx === 'number' && status.ctx > 0) {
setState({
provider: 'builtin',
contextWindow: status.ctx,
- modelMax: 0, // GGUF training ceiling not surfaced here — leave presets uncapped
+ modelMax,
isTrue: true,
adjustable: true,
})
@@ -99,7 +103,7 @@ export function useActiveContextWindow(reloadTick = 0): ActiveContext {
setState({
provider: 'builtin',
contextWindow: builtinCtx > 0 ? builtinCtx : 8192,
- modelMax: 0,
+ modelMax,
isTrue: false,
adjustable: true,
})