pii: pf-serve loads a model under a user folder named outside ASCII (Windows)

AIMetaPhD on X (Windows, Chinese user name): Settings → Privacy's
Privacy Filter said "the local masking model didn't start:
pf-serve-vulkan.exe: failed to read GGUF: C:\Users\<mojibake>\.cache\
magpie\privacy-filter\privacy-filter-q8-80efc1803eda.gguf", the same for
pf-serve.exe. pf-serve's main takes a narrow argv, which on Windows is
in the ANSI code page (936 on a Chinese system); ggml_fopen reads the
name as UTF-8 and the GPU's weights are reopened through a narrow
ifstream, so no device could open it, and its error came back in
GBK, which magpie read as UTF-8.

startOne now runs pf-serve in the model's folder and gives it the
model's file name (modelName, ASCII), never the path. That works with
the pf-v2 binaries users already have. A pf-serve error that names the
file is shown with the whole path, in UTF-8. The stand-in pf-serve in
the tests now refuses a model path with a byte outside ASCII and opens
the file from where it runs, as the real one does. TestModelInFolder-
NotASCIILoads and TestModelErrorNamesWholePath put the model under
"用户 Ünïcode". The native side (wide argv, and ggml_fopen in
weights-once.patch) is #4342. pf-serve is the only native helper magpie
starts. AstrLink Guard's CoreML paths are darwin cgo, which is UTF-8.

Run:
- Real use on xiaomi-gpu (Windows 11, zh-CN, ACP 936): the real pf-v2
  pf-serve zip and the real 1.6 GB model (both sha256 match the pins)
  under C:\Users\yeton\runs\pf-测试用户\.cache\magpie\privacy-filter.
  A temporary test (not committed) pointed dir there and called Find
  through win-run. Vulkan (AMD Radeon 8060S) loaded in 3.7s and found
  the person and the email. Forced onto the CPU, it loaded in 7.0s with
  the same finds. Broken by hand (the old full-path argv), it gave the
  reporter's exact error, the folder in mojibake, on both programs.
- Broken by hand on mote: with the old argv, both new tests are red.
- win-run go test ./internal/pii/...: ok (the real test included).
  wsl-run go test ./internal/pii/: ok.
- mote: gofmt ok; GOOS=darwin/linux/windows go vet -tags nogui ./...
  ok; GOOS=windows go vet ./internal/pii/... ok.
- mote: go test -count=1 -timeout 45m ./... at base e863f520c: every
  package ok except internal/gui's TestMainWindowCorners/macOS_26_SDK.
  That is #3949 (macOS 27 on mote), and gui is untouched here.
- After rebasing onto 9e8c22dad (7632e1be1 changed internal/redact and
  internal/masking): pii, redact and masking were run 4 times.
  redact's TestMoreKinds was red once with its output lost, then
  passed 3 runs, -count=8 alone and -count=3 of the package. Filed as
  #4345. pii and masking were ok every time.

Not tried: the GUI on the reporter's real machine. No GUI file changed:
the GUI shows the error text the Go code sends, which is now UTF-8.
This commit is contained in:
yetone
2026-10-30 10:53:28 +08:00
parent 9e8c22dad8
commit d9459a5371
5 changed files with 76 additions and 4 deletions
File diff suppressed because one or more lines are too long
+24 -1
View File
@@ -41,6 +41,8 @@ const (
// loadhang never says the model is loaded
// slow answers each request after 300ms
// noload says it couldn't load the model
// badmodel says it couldn't read the model's file, by the name
// it was given
// novulkan as pf-serve-vulkan, says Vulkan is missing; else ok
// gpuloadhang on the GPU, never says the model is loaded; else ok
// asserts fails a ggml assert at its first request
@@ -76,6 +78,19 @@ func fakeServe(mode string) {
logf("path %s", filepath.Base(filepath.Dir(os.Args[0])))
logf("residency %s", os.Getenv("GGML_METAL_NO_RESIDENCY"))
logf("moechunk %s %s", name, os.Getenv("PF_MOE_CHUNK"))
// as pf-serve on Windows, whose argv is in the ANSI code page and
// whose ggml reads it as UTF-8: a model path with a byte outside
// ASCII can't be opened. And it opens the file from where it runs.
model := ""
if len(os.Args) > 1 {
model = os.Args[1]
}
logf("model %s", model)
_, err := os.Stat(model)
if strings.IndexFunc(model, func(r rune) bool { return r > 0x7f }) >= 0 || err != nil || mode == "badmodel" {
json.NewEncoder(os.Stdout).Encode(map[string]string{"error": "failed to read GGUF: " + model})
os.Exit(1)
}
if mode == "noload" || mode == "novulkan" && name == "pf-serve-vulkan" {
json.NewEncoder(os.Stdout).Encode(map[string]string{"error": "can't load " + mode})
os.Exit(1)
@@ -154,9 +169,17 @@ func fakeServe(mode string) {
// pf-serve-vulkan too, with vulkan), and a model file, in a folder of the
// test's own; it gives the file the stand-in logs to.
func fakeInstall(t *testing.T, mode string, vulkan bool) string {
t.Helper()
return fakeInstallAt(t, mode, vulkan, t.TempDir())
}
// fakeInstallAt is fakeInstall in folder d.
func fakeInstallAt(t *testing.T, mode string, vulkan bool, d string) string {
t.Helper()
upgrades.Wait() // one started before reads dir
d := t.TempDir()
if err := os.MkdirAll(d, 0o755); err != nil {
t.Fatal(err)
}
was, wasSize, wasIdle := dir, modelSize, idleAfter
dir, modelSize = func() string { return d }, 4
t.Cleanup(func() {
+5 -1
View File
@@ -78,7 +78,11 @@ func Size() int64 { return modelSize }
// sync carries to the user's other computers.
var dir = func() string { return filepath.Join(appdir.Cache(), "privacy-filter") }
func modelPath() string { return filepath.Join(dir(), "privacy-filter-q8-"+modelSHA[:12]+".gguf") }
func modelPath() string { return filepath.Join(dir(), modelName()) }
// modelName is the model's file name in dir, ASCII alone: pf-serve is
// given this, not modelPath (startOne).
func modelName() string { return "privacy-filter-q8-" + modelSHA[:12] + ".gguf" }
func serveDir() string { return tagDir(serveTag) }
+13 -1
View File
@@ -11,6 +11,7 @@ import (
"math"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
@@ -220,7 +221,15 @@ var upgrades sync.WaitGroup
var errStopped = errors.New("the local masking model was stopped")
func startOne(path string, cpu bool, stop <-chan struct{}) (*helper, error) {
cmd := proc.Command(path, modelPath())
// pf-serve is given the model's name, run in its folder, and never
// the whole path: on Windows a C program's argv comes in the ANSI
// code page, where ggml's fopen reads UTF-8 and the GPU's weights
// are read again through a narrow ifstream, so a folder with a
// character outside ASCII (a Chinese user name in C:\Users) failed
// to load on every device, its error a path in mojibake. magpie's
// own names are ASCII.
cmd := proc.Command(path, modelName())
cmd.Dir = filepath.Dir(modelPath())
// Without GGML_METAL_NO_RESIDENCY, ggml's Metal residency set keeps a
// heartbeat that woke an idle pf-serve ~146 times a second for the 10
// minutes it stays up after a request, and macOS's Energy Impact
@@ -279,6 +288,9 @@ func startOne(path string, cpu bool, stop <-chan struct{}) (*helper, error) {
if why == "" {
why = h.died()
}
// the name it was given ("failed to read GGUF: <name>"), as the
// path the user can find
why = strings.ReplaceAll(why, " "+modelName(), " "+modelPath())
return nil, fmt.Errorf("%s: %s", baseName(path), why)
}
return h, nil
+33
View File
@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
@@ -341,6 +342,38 @@ func TestServeThatCantLoadSaysWhy(t *testing.T) {
}
}
// A model in a folder whose name isn't ASCII loads: on Windows pf-serve's
// argv is in the ANSI code page and its fopen reads UTF-8, so a Chinese
// user name in C:\Users failed every load, on the GPU and the CPU alike,
// with the path in mojibake (AIMetaPhD on X). pf-serve gets the model's
// name, run in its folder; the stand-in refuses a byte outside ASCII as
// pf-serve does.
func TestModelInFolderNotASCIILoads(t *testing.T) {
d := filepath.Join(t.TempDir(), "用户 Ünïcode", ".cache", "privacy-filter")
log := fakeInstallAt(t, "ok", true, d)
got, err := Find([]string{"hi Alice"})
if err != nil {
t.Fatalf("err = %v", err)
}
if len(got[0]) != 1 {
t.Fatalf("found %v", got)
}
if m := fakeLines(t, log, "model "); len(m) == 0 || m[0] != "model "+modelName() {
t.Fatalf("pf-serve was given %q, want the model's name", m)
}
}
// pf-serve's error names the model by the name it was given; the user
// reads the whole path, in UTF-8.
func TestModelErrorNamesWholePath(t *testing.T) {
d := filepath.Join(t.TempDir(), "用户")
fakeInstallAt(t, "badmodel", false, d)
_, err := Find([]string{"hi Alice"})
if err == nil || !strings.Contains(err.Error(), "failed to read GGUF: "+modelPath()) {
t.Fatalf("err = %v, want %s in it", err, modelPath())
}
}
// Warm starts pf-serve before a request asks, and one that can't start
// says why in Now; a request that is answered clears it.
func TestWarm(t *testing.T) {