From 1d3c14bf611fd4ffb7b3eaa327a49b97ef40a692 Mon Sep 17 00:00:00 2001 From: andreinknv Date: Fri, 15 May 2026 21:26:48 -0400 Subject: [PATCH] fix: route llama.cpp log fallback to stderr instead of stdout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When no JS logger is registered, or a registered logger throws, the addon's log callback falls back to writing llama.cpp's output directly. That fallback sent every non-error line (warn / info / debug) to stdout. For an embedder whose stdout is a data channel that is corruption: the `load:` / `init:` / `llama_*` model-load chatter gets interleaved into a CLI's results, or — worse — into an MCP / stdio server's JSON-RPC stream. Library diagnostics belong on stderr regardless of level. Both fallback sites (addonCallJsLogCallback and addonLlamaCppLogCallback) now write all llama.cpp log output to stderr. Error-level lines already went there; this moves only the non-error lines. Co-Authored-By: Claude Opus 4.7 (1M context) --- llama/addon/globals/addonLog.cpp | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/llama/addon/globals/addonLog.cpp b/llama/addon/globals/addonLog.cpp index 4e66b316..e9f3e2f8 100644 --- a/llama/addon/globals/addonLog.cpp +++ b/llama/addon/globals/addonLog.cpp @@ -38,13 +38,11 @@ void addonCallJsLogCallback( } if (!called && data != nullptr) { - if (data->logLevelNumber == 2) { - fputs(data->stringStream->str().c_str(), stderr); - fflush(stderr); - } else { - fputs(data->stringStream->str().c_str(), stdout); - fflush(stdout); - } + // llama.cpp diagnostics go to stderr regardless of level — + // stdout is the host process's data / JSON-RPC channel and + // must not be polluted with library log lines. + fputs(data->stringStream->str().c_str(), stderr); + fflush(stderr); } if (data != nullptr) { @@ -83,13 +81,10 @@ void addonLlamaCppLogCallback(ggml_log_level level, const char* text, void* user } if (text != nullptr) { - if (level == 2) { - fputs(text, stderr); - fflush(stderr); - } else { - fputs(text, stdout); - fflush(stdout); - } + // All llama.cpp log output goes to stderr — see the note in + // addonCallJsLogCallback; stdout is the host's data channel. + fputs(text, stderr); + fflush(stderr); } }