Windows: fix non-ASCII chat prompt corruption (ANSI codepage vs UTF-8)

coli passes the chat prompt to glm.exe through the PROMPT/COLI_PROMPT
environment variable. On Windows, plain getenv() is populated by the CRT
from the ANSI-codepage view of the environment block, not UTF-8 — so any
non-ASCII prompt text (Cyrillic, CJK, ...) is silently mangled before the
byte-level tokenizer ever sees it, even though the parent process (coli's
Python subprocess call) sets the value correctly via the wide env block.

Add compat_getenv_utf8() in compat.h: reads the variable through
GetEnvironmentVariableW and converts straight to UTF-8, bypassing the ANSI
codepage entirely. No-op passthrough to getenv() on non-Windows platforms.
coli_user_prompt() in glm.c now uses it for both COLI_PROMPT and PROMPT.

Verified: tiny-oracle self-test still 32/32 after rebuild, and a real run
against the full GLM-5.2-int4 model with a Cyrillic prompt now round-trips
correctly end to end (input echoed intact, coherent Cyrillic output),
where it previously produced replacement-character garbage on both input
and output.
This commit is contained in:
volognamor
2026-07-17 21:49:28 +03:00
parent 72d3d37231
commit ef97f3cf2b
2 changed files with 33 additions and 2 deletions
+2 -2
View File
@@ -5701,9 +5701,9 @@ static void cap_for_ram(Model *m, double ram_gb, int ebits, int max_ctx){
* self-test, and would "generate" from "$P$G". So on Windows a PROMPT carrying
* cmd's $-metacodes is ignored; set COLI_PROMPT to pass a real prompt from cmd. */
static const char *coli_user_prompt(void){
const char *p = getenv("COLI_PROMPT");
const char *p = getenv_utf8("COLI_PROMPT");
if(p) return p;
p = getenv("PROMPT");
p = getenv_utf8("PROMPT");
#ifdef _WIN32
if(p) for(const char *q=p; q[0]; q++)
if(q[0]=='$' && q[1] && strchr("ABCDEFGHLNPQSTV_+|$", q[1]&~0x20)){ p=NULL; break; }