Skip to content

Commit 2f06eb8

Browse files
committed
fix(cookbook): harden local llama-server launch commands
1 parent 31aaf66 commit 2f06eb8

4 files changed

Lines changed: 63 additions & 12 deletions

File tree

routes/cookbook_routes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,10 @@ async def model_serve(request: Request, req: ServeRequest):
13811381
# shell resolves the bundled python3/hf, mirroring the download flow.
13821382
if not remote:
13831383
runner_lines.append(_local_tooling_path_export(sys.executable))
1384+
if local_windows:
1385+
# Detached Git Bash runs do not always inherit recently edited
1386+
# user PATH entries from the already-running Odysseus process.
1387+
runner_lines.append('export PATH="$HOME/bin:$HOME/llama.cpp/build-cuda/bin/Release:$HOME/llama.cpp/build/bin/Release:$HOME/llama.cpp/build/bin/Debug:$HOME/llama.cpp/build/bin:$PATH"')
13841388
runner_lines.append("export FLASHINFER_DISABLE_VERSION_CHECK=1")
13851389
if req.hf_token:
13861390
runner_lines.append(f"export HF_TOKEN='{_bash_squote(req.hf_token)}'")

static/js/cookbookServe.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ function _runnableGgufFiles(model) {
225225
return primary.length ? primary : files;
226226
}
227227

228+
function _projectorGgufFiles(model) {
229+
return _ggufFilesForModel(model)
230+
.filter(f => (f.role || '') === 'projector' || /(^|\/)mmproj[^/]*\.gguf$/i.test(f.rel_path || f.name || ''))
231+
.sort((a, b) => String(a.rel_path || a.name || '').localeCompare(String(b.rel_path || b.name || '')));
232+
}
233+
228234
function _ggufFileLabel(file) {
229235
const base = (file.name || file.rel_path || '').split('/').pop();
230236
const size = _formatGgufSize(file.size_bytes);
@@ -605,6 +611,7 @@ function _rerenderCachedModels() {
605611
: `This model's download isn't complete yet (${esc(m.size || 'partial')}). The serve will start but is likely to crash on a missing shard. Wait for the download to finish, or relaunch after it's done.`;
606612
panelHtml += `<div class="hwfit-serve-warn" style="margin:0 0 8px;padding:6px 10px;border-radius:5px;font-size:11px;background:color-mix(in srgb, var(--color-warning, #f0ad4e) 14%, transparent);border:1px solid color-mix(in srgb, var(--color-warning, #f0ad4e) 40%, transparent);color:var(--color-warning, #f0ad4e);display:flex;gap:6px;align-items:flex-start;line-height:1.4;"><span aria-hidden="true">⚠</span><span>${_warnText}</span></div>`;
607613
}
614+
panelHtml += `<div class="hwfit-serve-vision-warn" style="display:none;margin:0 0 8px;padding:6px 10px;border-radius:5px;font-size:11px;background:color-mix(in srgb, var(--color-warning, #f0ad4e) 14%, transparent);border:1px solid color-mix(in srgb, var(--color-warning, #f0ad4e) 40%, transparent);color:var(--color-warning, #f0ad4e);gap:6px;align-items:flex-start;line-height:1.4;"><span aria-hidden="true">⚠</span><span>Vision is enabled, but no mmproj GGUF projector was found in the cached model scan. Download an mmproj-*.gguf for this model, then refresh the cached model list before launching.</span></div>`;
608615
// Row 1: Backend + Server + Env
609616
panelHtml += `<div class="hwfit-serve-row">`;
610617
const _backendChoices = _isWindows()
@@ -886,18 +893,22 @@ function _rerenderCachedModels() {
886893
: m.is_local_dir && m.path
887894
? `$({ find ${_ldir} -name '*-00001-of-*.gguf' 2>/dev/null | sort; find ${_ldir} -name '*.gguf' 2>/dev/null | sort; } | head -1)`
888895
: `$({ find ${dir} -name '*-00001-of-*.gguf' 2>/dev/null | sort; find ${dir} -name '*.gguf' 2>/dev/null | sort; } | head -1)`;
889-
// Vision: auto-find the mmproj (CLIP/projector) file in the same dir.
890-
// Resolved at runtime so the toggle just works if an mmproj-*.gguf is
891-
// present (downloaded alongside the model). Empty if none → cmd omits it.
892-
const _vsearchdir = (m.is_local_dir && m.path) ? _ldir : dir;
893-
f._mmproj_path = `$(find ${_vsearchdir} -iname 'mmproj*.gguf' 2>/dev/null | sort | head -1)`;
896+
// Vision: use the scanned projector (CLIP/mmproj) file when present.
897+
// Keeping this as a printf path avoids generating a command substitution
898+
// that the backend serve-command validator must reject as unsafe.
899+
const selectedProjector = _projectorGgufFiles(m)[0];
900+
f._mmproj_path = selectedProjector ? _selectedGgufExpr(m, repo, selectedProjector.rel_path) : '';
894901
}
895902
if (f.reasoning_parser) {
896903
const _rpEl2 = panel.querySelector('[data-field="reasoning_parser"]');
897904
f._reasoning_parser_value = _rpEl2?.dataset?.parser || 'qwen3';
898905
}
899906
let cmd = _buildServeCmd(f, serveModel, backend);
900907
if (f.extra && f.extra.trim()) cmd += ' ' + f.extra.trim();
908+
const missingVisionProjector = backend === 'llamacpp' && !!f.vision && !f._mmproj_path;
909+
panel._visionMissingProjector = missingVisionProjector;
910+
const _visionWarn = panel.querySelector('.hwfit-serve-vision-warn');
911+
if (_visionWarn) _visionWarn.style.display = missingVisionProjector ? 'flex' : 'none';
901912
const _ce2 = panel.querySelector('.hwfit-serve-cmd'); _ce2.value = cmd; _ce2.style.height = 'auto'; _ce2.style.height = _ce2.scrollHeight + 'px';
902913
panel._cmd = cmd;
903914
panel._host = f.host || '';
@@ -1971,6 +1982,11 @@ function _rerenderCachedModels() {
19711982
else serveState[el.dataset.field] = el.value;
19721983
});
19731984
serveState.backend = serveState.backend || (_detectBackend(m).backend) || 'vllm';
1985+
if (serveState.backend === 'llamacpp' && serveState.vision && !/(?:^|\s)(?:--mmproj|--clip_model_path)\b/.test(launchCmd)) {
1986+
_restoreLaunchBtn();
1987+
uiModule.showToast('Vision is checked, but no mmproj projector is in the launch command. Refresh cached models after downloading mmproj, or add --mmproj manually.', 8000);
1988+
return;
1989+
}
19741990

19751991
// Pre-launch: check our own task list for a serve already running
19761992
// on this host. Offer to stop+launch as the default action — the

tests/test_cookbook_cpu_only_serve.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,28 @@ def test_local_windows_llama_server_skips_source_bootstrap():
8787
routes = (ROOT / "routes/cookbook_routes.py").read_text(encoding="utf-8")
8888

8989
assert 'local_windows_llama_cmd = local_windows and ("llama_cpp" in req.cmd or "llama-server" in req.cmd)' in routes
90-
assert 'if ("llama_cpp" in req.cmd or "llama-server" in req.cmd) and not local_windows_llama_cmd:' in routes
90+
assert 'if ("llama_cpp" in req.cmd or "llama-server" in req.cmd) and not local_windows_llama_cmd:' in routes
91+
92+
93+
def test_local_windows_llama_server_path_includes_user_wrapper_and_cuda_builds():
94+
routes = (ROOT / "routes/cookbook_routes.py").read_text(encoding="utf-8")
95+
96+
assert 'if local_windows:' in routes
97+
assert (
98+
'export PATH="$HOME/bin:$HOME/llama.cpp/build-cuda/bin/Release:'
99+
'$HOME/llama.cpp/build/bin/Release:$HOME/llama.cpp/build/bin/Debug:'
100+
'$HOME/llama.cpp/build/bin:$PATH"'
101+
) in routes
102+
103+
104+
def test_llamacpp_vision_uses_scanned_projector_instead_of_runtime_find():
105+
text = SERVE_SRC.read_text(encoding="utf-8")
106+
107+
assert "function _projectorGgufFiles(model)" in text
108+
assert "const selectedProjector = _projectorGgufFiles(m)[0];" in text
109+
assert "f._mmproj_path = selectedProjector ? _selectedGgufExpr(m, repo, selectedProjector.rel_path) : '';" in text
110+
assert "const missingVisionProjector = backend === 'llamacpp' && !!f.vision && !f._mmproj_path;" in text
111+
assert "hwfit-serve-vision-warn" in text
112+
assert "!/(?:^|\\s)(?:--mmproj|--clip_model_path)\\b/.test(launchCmd)" in text
113+
assert "no mmproj projector is in the launch command" in text
114+
assert "find ${_vsearchdir} -iname 'mmproj*.gguf'" not in text

tests/test_cookbook_helpers.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,6 @@ def test_pip_install_attempt_failure_propagates_real_exit_code():
419419
"""Run the generated snippet against a deliberately broken pip install
420420
to confirm the subshell exits with pip's non-zero status."""
421421
snippet = _pip_install_attempt("python3 -m pip install __nonexistent_package_12345__")
422-
if sys.platform == "win32":
423-
snippet = snippet.replace("$", "\\$")
424422
result = subprocess.run(
425423
["bash", "-c", snippet],
426424
capture_output=True,
@@ -433,8 +431,6 @@ def test_pip_install_attempt_failure_propagates_real_exit_code():
433431
def test_pip_install_attempt_success_exits_zero():
434432
"""When pip succeeds, the subshell should exit 0."""
435433
snippet = _pip_install_attempt("python3 -c 'pass'")
436-
if sys.platform == "win32":
437-
snippet = snippet.replace("$", "\\$")
438434
result = subprocess.run(
439435
["bash", "-c", snippet],
440436
capture_output=True,
@@ -447,8 +443,6 @@ def test_pip_install_attempt_success_exits_zero():
447443
def test_pip_install_attempt_surfaces_stderr_on_failure():
448444
"""On failure, the last 5 lines of pip output should appear in stdout."""
449445
snippet = _pip_install_attempt("python3 -m pip install __nonexistent_package_12345__")
450-
if sys.platform == "win32":
451-
snippet = snippet.replace("$", "\\$")
452446
result = subprocess.run(
453447
["bash", "-c", snippet],
454448
capture_output=True,
@@ -557,6 +551,19 @@ def test_validate_serve_cmd_accepts_windows_printf_format():
557551
assert _validate_serve_cmd(cmd) == cmd
558552

559553

554+
def test_validate_serve_cmd_accepts_llama_mmproj_printf_format():
555+
cmd = (
556+
"CUDA_VISIBLE_DEVICES=0 llama-server --model "
557+
"\"$(printf %s ${HOME}'/.cache/huggingface/hub/models--unsloth--Qwen3.6-35B-A3B-GGUF/snapshots/abc/Qwen3.6-35B-A3B-UD-Q4_K_M.gguf')\" "
558+
"--host 0.0.0.0 --port 8000 -ngl 99 -c 20000 "
559+
"--cache-type-k q4_0 --cache-type-v q4_0 --mmproj "
560+
"\"$(printf %s ${HOME}'/.cache/huggingface/hub/models--unsloth--Qwen3.6-35B-A3B-GGUF/snapshots/abc/mmproj-BF16.gguf')\" "
561+
"--image-max-tokens 1024"
562+
)
563+
564+
assert _validate_serve_cmd(cmd) == cmd
565+
566+
560567
def test_normalize_llama_cpp_python_cache_types_for_stale_client_cmd():
561568
cmd = (
562569
"python -m llama_cpp.server --model model.gguf --host 0.0.0.0 --port 8000 "

0 commit comments

Comments
 (0)