Files
magpie/internal/source/china_test.go
T
yetone 0db804999e pii: AstrLink Guard as a second local model (#3578)
pii.Model is a local model (ID, Supported, Installed, Size, Find, Now,
Download, Cancel, Warm, Stop, Remove); pii.Models lists Privacy Filter
and AstrLink Guard, pii.Of finds one by id. pii.Choose decides which
one runs: the user's pick; else Privacy Filter for a user who has it on
or downloaded, so no one's model changes under them; else AstrLink
Guard, the default for everyone who turns the model on from now on.

Why AstrLink Guard as the default. On 234 segments of real code and
config (131 KB: Go, TS, lockfiles, go.sum, CI and agent configs),
Privacy Filter found 100 spans in 26 segments, 39 after redact's checks
(38 go.sum hashes and "Zed"); AstrLink Guard found 2, 1 after the
checks. On the masking eval (42 values to mask, 113 to keep), the
model alone: Privacy Filter 40/42 and 47/113 wrongly, AstrLink Guard
38/42 and 3/113; with the rules and checks both are 41/42 and 1/113.
It is 39 MB, not 1.6 GB, runs on any CPU in magpie's process, and
needs no native helper.

Its three files come from Hugging Face at revision 7216b6a3 (v0.1.3),
each checked against the sha256 in the publisher's SHA256SUMS:
int8/model_int8.onnx 0a070d31…, int8/model_int8.onnx_data 0ba34216…,
int8/tokenizer.json 0cf3b235…; from hf-mirror first in China, as
Privacy Filter's are. It is loaded on the first request and let go
after 10 minutes with none; a failure holds requests back with the
same backoff as Privacy Filter's. Its labels are named as Privacy
Filter's, so redact's checks read both alike.

TestGuardFilesAreThePublishers holds each pinned path and sha256 to
the publisher's SHA256SUMS lines (testdata); it is red with the
tokenizer at the repo root, which is a 404 (found by a real download).

Run: go test -race -count=2 ./internal/pii ./internal/source; a real
download from huggingface.co into a sandbox HOME through
Of(Choose("", false)).Download (8.7s), then Find: 张伟, 13800138000,
zhangwei@example.cn, 北京市朝阳区建国路88号 and an sk-proj key found,
nothing in a go.sum line or "gpt-5.1-codex, go 1.26.8, README.md";
load 104 ms, three texts in 177 ms. All at the pushed head.
Not tried: a real download on Windows or Linux.
2026-10-27 02:01:16 +08:00

185 lines
7.7 KiB
Go

package source
import (
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"sync/atomic"
"testing"
"github.com/yetone/magpie/internal/testenv"
)
// the 「国内镜像」 switch is off in every test but those that turn it on,
// whatever the settings of the HOME the tests run in say; and that HOME is
// one of their own, never the user's
func TestMain(m *testing.M) {
China = func() bool { return false }
os.Exit(testenv.Run(m))
}
func chinaOn(t *testing.T) {
was := China
China = func() bool { return true }
t.Cleanup(func() { China = was })
}
func TestChinaFirst(t *testing.T) {
for raw, want := range map[string]string{
"https://registry.npmjs.org/@magpie-community%2fopencode-kiro-auth": "https://registry.npmmirror.com/@magpie-community%2fopencode-kiro-auth",
"https://registry.npmjs.org/-/v1/search?text=opencode&size=20": "https://registry.npmmirror.com/-/v1/search?text=opencode&size=20",
"https://github.com/oven-sh/bun/releases/download/bun-v1.4.2/bun-darwin-aarch64.zip": "https://registry.npmmirror.com/-/binary/bun/bun-v1.4.2/bun-darwin-aarch64.zip",
"https://raw.githubusercontent.com/magpie-community/plugins/main/registry.json": "https://cdn.jsdelivr.net/gh/magpie-community/plugins@main/registry.json",
"https://huggingface.co/LocalAI-io/privacy-filter-GGUF/resolve/935d868/privacy-filter-q8.gguf": "https://hf-mirror.com/LocalAI-io/privacy-filter-GGUF/resolve/935d868/privacy-filter-q8.gguf",
// no other model, and no page of Hugging Face's
"https://huggingface.co/someone/else/resolve/main/x.gguf": "",
"https://huggingface.co/QuantumNous/astrlink-guard/resolve/7216b6a/int8/model_int8.onnx_data": "https://hf-mirror.com/QuantumNous/astrlink-guard/resolve/7216b6a/int8/model_int8.onnx_data",
"https://huggingface.co/QuantumNous/other/resolve/7216b6a/x": "",
"https://huggingface.co/LocalAI-io/privacy-filter-GGUF": "",
"https://huggingface.co/LocalAI-io/privacy-filter-GGUF/resolve/main/x?dl=1": "",
// npm's download counts are npmjs's own: npmmirror counts its own
"https://api.npmjs.org/downloads/point/last-week/pkg": "",
// nothing else of GitHub's: no proxy stands in for it
"https://github.com/oven-sh/bun/releases/download/": "",
"https://github.com/o/r/releases/download/v1/x.zip": "",
"https://api.github.com/repos/oven-sh/bun/releases/latest": "",
"https://raw.githubusercontent.com/someone/else/main/registry.json": "",
"https://raw.githubusercontent.com/magpie-community/plugins/main/x.json?t=1": "",
"http://registry.npmjs.org/pkg": "",
} {
if got := chinaFirst(raw); got != want {
t.Errorf("chinaFirst(%s) = %q, want %q", raw, got, want)
}
}
}
func TestChinaComesFirst(t *testing.T) {
t.Setenv("MAGPIE_MIRRORS", "")
raw := "https://registry.npmjs.org/pkg"
if got := URLs(raw); len(got) != 1 {
t.Fatalf("switch off: URLs = %q", got)
}
chinaOn(t)
if got := strings.Join(URLs(raw), " "); got != "https://registry.npmmirror.com/pkg "+raw {
t.Fatalf("switch on: URLs = %q", got)
}
// with the fallback mirrors too, npmmirror once, first
t.Setenv("MAGPIE_MIRRORS", "on")
t.Setenv("MAGPIE_NPM_REGISTRY", "")
if got := strings.Join(URLs(raw), " "); got != "https://registry.npmmirror.com/pkg "+raw {
t.Fatalf("switch and fallback on: URLs = %q", got)
}
// GitHub's fallback proxy stays after the official address
gh := "https://raw.githubusercontent.com/magpie-community/plugins/main/registry.json"
want := "https://cdn.jsdelivr.net/gh/magpie-community/plugins@main/registry.json " + gh + " https://gh-proxy.com/" + gh
if got := strings.Join(URLs(gh), " "); got != want {
t.Fatalf("plugin list URLs = %q, want %q", got, want)
}
}
// pair is a mirror and an official server, counting their requests
func pair(t *testing.T, mirror, official http.HandlerFunc) (m, o *httptest.Server, mn, on *atomic.Int32) {
mn, on = new(atomic.Int32), new(atomic.Int32)
m = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { mn.Add(1); mirror(w, r) }))
o = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { on.Add(1); official(w, r) }))
t.Cleanup(m.Close)
t.Cleanup(o.Close)
return
}
func say(code int, body string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(code); io.WriteString(w, body) }
}
func TestChinaMirrorAnswers(t *testing.T) {
m, o, mn, on := pair(t, say(200, "mirror"), say(200, "official"))
req, _ := http.NewRequest("GET", o.URL+"/pkg", nil)
res, err := do(http.DefaultClient, req, []string{m.URL + "/pkg", o.URL + "/pkg"})
if err != nil {
t.Fatal(err)
}
b, _ := io.ReadAll(res.Body)
res.Body.Close()
if string(b) != "mirror" || mn.Load() != 1 || on.Load() != 0 {
t.Fatalf("got %q, mirror asked %d, official %d", b, mn.Load(), on.Load())
}
}
// what a mirror in China hasn't copied yet (a version just published) is
// asked of the official address, where a fallback mirror's 404 would be
// the answer
func TestChinaMirrorsMissIsAskedOfficially(t *testing.T) {
for _, code := range []int{404, 403, 500} {
m, o, mn, on := pair(t, say(code, "no"), say(200, "official"))
req, _ := http.NewRequest("GET", o.URL+"/pkg", nil)
res, err := do(http.DefaultClient, req, []string{m.URL + "/pkg", o.URL + "/pkg"})
if err != nil {
t.Fatal(err)
}
b, _ := io.ReadAll(res.Body)
res.Body.Close()
if string(b) != "official" || mn.Load() != 1 || on.Load() != 1 {
t.Fatalf("%d: got %q, mirror asked %d, official %d", code, b, mn.Load(), on.Load())
}
}
// the official 404 is the answer, the fallback mirror not asked
m, o, mn, on := pair(t, say(200, "fallback"), say(404, "gone"))
req, _ := http.NewRequest("GET", o.URL+"/pkg", nil)
res, err := do(http.DefaultClient, req, []string{o.URL + "/pkg", m.URL + "/pkg"})
if err != nil {
t.Fatal(err)
}
res.Body.Close()
if res.StatusCode != 404 || mn.Load() != 0 || on.Load() != 1 {
t.Fatalf("official 404: %d, fallback asked %d, official %d", res.StatusCode, mn.Load(), on.Load())
}
}
// the plugin list goes to jsDelivr with the switch, never to a proxy
func TestDoFaithful(t *testing.T) {
t.Setenv("MAGPIE_MIRRORS", "on")
t.Setenv("MAGPIE_GITHUB_MIRROR", "https://gh.invalid")
var asked []string
c := &http.Client{Transport: roundTrip(func(r *http.Request) (*http.Response, error) {
asked = append(asked, r.URL.String())
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader("{}")), Header: http.Header{}, Request: r}, nil
})}
list := "https://raw.githubusercontent.com/magpie-community/plugins/main/registry.json"
ask := func() {
asked = nil
req, _ := http.NewRequest("GET", list, nil)
res, err := DoFaithful(c, req)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
}
ask()
if strings.Join(asked, " ") != list {
t.Fatalf("switch off: asked %q", asked)
}
chinaOn(t)
ask()
if strings.Join(asked, " ") != "https://cdn.jsdelivr.net/gh/magpie-community/plugins@main/registry.json" {
t.Fatalf("switch on: asked %q", asked)
}
// a request with a credential stays official
asked = nil
req, _ := http.NewRequest("GET", "https://registry.npmjs.org/pkg", nil)
req.Header.Set("Authorization", "Bearer x")
res, err := DoFaithful(c, req)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
if strings.Join(asked, " ") != "https://registry.npmjs.org/pkg" {
t.Fatalf("with a credential: asked %q", asked)
}
}
type roundTrip func(*http.Request) (*http.Response, error)
func (f roundTrip) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }