The owner's plan (10-28, after the xuanwo/_kaichen reports under the
masking demo): with AstrLink Guard picked, the Chinese-name heuristic
is off and only nicknames like 小王/老李 stay a rule; with Privacy Filter
it is as before; every format rule stays; the model is on by default,
masks with the rules alone until its download finishes, and no longer
answers 503 for it.
- masking.ModelWanted(st): on when the user turned it on, off when the
user turned it off (new RedactModelOff, set by SetRedactModel(false),
remove, and cancelling the default's own download), on when the
default's download finished (RedactModelAuto). A settings file from
before this, with a model name picked or files on disk and the model
off, reads as the user's off: magpie's default doesn't override it.
- Until the picked model is installed, requests are masked by the rules
alone and fetch starts its download (one try per model per hour,
never under testing.Testing(), nothing where the model can't run).
When it finishes, settings.UseDefaultRedactModel records the default
(it refuses when the user has set anything) and the model warms.
- model(..., strict): a model that fails holds requests back (503)
only when the user turned it on; under the default the rules mask
alone. The Privacy page says which ("…the rules mask requests alone"
in en, zh, zh-TW, ja, de; /api/privacy-model has `chosen`).
- redact.Options.NamesByModel (AstrLink Guard picked): zhNameSpans
keeps only zhNick candidates (小/老 + a surname).
- KeepOwn and POST /api/settings keep both new fields.
- Docs: gateway-routing.md, site masking docs (en, zh, ja).
Run (mote, Go 1.26.8):
- gofmt clean; go vet of masking, settings, redact, gui; GOOS=linux go
vet of masking, settings, redact; GOOS=windows go vet of the four.
(GOOS=linux vet of internal/gui needs cgo, as on main.)
- go test -count=1 -timeout 45m ./... at base 7fd77a944: every package
ok except internal/gui. Its reds: TestCoreScriptBudget (+64 bytes,
fixed by dropping a comment, budget not raised), TestPrivacyModelPick
and TestSettingsSaveKeepsWhatItDoesNotSend (updated for the new
default and fields), TestMainWindowCorners (#3949, red on origin/main
e8c7dddcb too), and three AppKit tests killed at 20-30s under load
just before mote rebooted, which pass in the rerun.
- Rerun at base 6bec526f7: internal/gui (only #3949 red), masking,
settings, redact ok. GUI node tests privacy-model, masking-records,
masking-demo, routing-masking, i18n-keys, gui-zh-tw, gui-ja, gui-de:
Chromium 199/199, WebKit 106/106.
- At the pushed base 9e33962c0: TestCoreScriptBudget, TestSettingsSave*,
TestPrivacyModel* ok; i18n-keys and privacy-model 68/68 in both
engines.
- At the pushed base 7fcf642ee (masking in the request archive):
-run 'Mask|Redact|Archive|Privacy' in masking, gateway, redact ok.
Broken by hand, each red: the nick filter off
(TestChineseNamesByModelOnlyNicks), ModelWanted needing RedactModel
(TestModelOnByDefault), strict ignored (TestDefaultModelFailingIsTheRules,
TestNoModelYetMasksByTheRules).
Not tried: a real default download in a sandbox HOME (the tests fake the
model). Known limit: a user who turned the model on before model picks
existed and then deleted it has no name or files left, so reads as new
and gets the default on.
153 lines
5.7 KiB
Go
153 lines
5.7 KiB
Go
package masking
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/pii"
|
|
"github.com/yetone/magpie/internal/redact"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// Masking asks the model the user picked; with none picked, AstrLink
|
|
// Guard (Privacy Filter isn't on disk in a test).
|
|
func TestMaskingUsesThePickedModel(t *testing.T) {
|
|
for name, want := range map[string]string{"": pii.AstrLinkGuard, pii.PrivacyFilter: pii.PrivacyFilter, pii.AstrLinkGuard: pii.AstrLinkGuard} {
|
|
if got := picked(settings.Settings{RedactModel: true, RedactModelName: name}).ID(); got != want {
|
|
t.Errorf("picked %q: %s, want %s", name, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// scored is a model that finds three names, as sure as their scores.
|
|
type scored struct{ pii.Model }
|
|
|
|
func (scored) ID() string { return pii.AstrLinkGuard }
|
|
func (scored) MinScore() float64 { return 0.8 }
|
|
func (scored) Find(context.Context, []string) ([][]redact.Found, error) {
|
|
return [][]redact.Found{{{Start: 0, End: 1, Label: "private_person", Score: 0.6},
|
|
{Start: 2, End: 3, Label: "private_person", Score: 0.85}, {Start: 4, End: 5, Label: "private_person", Score: 0.95}}}, nil
|
|
}
|
|
|
|
// What the model is less sure of than the threshold the user set for it
|
|
// goes as written; with none set, or one out of range, its own default
|
|
// (@Yayoi_no_yume on X).
|
|
func TestModelThreshold(t *testing.T) {
|
|
for _, c := range []struct {
|
|
set map[string]float64
|
|
want []int
|
|
}{
|
|
{nil, []int{2, 4}},
|
|
{map[string]float64{pii.AstrLinkGuard: 0.9}, []int{4}},
|
|
{map[string]float64{pii.AstrLinkGuard: 0.5}, []int{0, 2, 4}},
|
|
{map[string]float64{pii.PrivacyFilter: 0.95}, []int{2, 4}},
|
|
{map[string]float64{pii.AstrLinkGuard: 0.3}, []int{2, 4}},
|
|
{map[string]float64{pii.AstrLinkGuard: 1}, []int{2, 4}},
|
|
} {
|
|
var m pii.Model = scored{}
|
|
var got []int
|
|
for _, f := range model(context.Background(), m, pii.Threshold(c.set, m), true)([]string{"a b c"})[0] {
|
|
got = append(got, f.Start)
|
|
}
|
|
if !slices.Equal(got, c.want) {
|
|
t.Errorf("threshold set %v: kept %v, want %v", c.set, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The local model is on by default: for a user who neither turned it on
|
|
// nor off, and never had one downloaded or picked, which a user did who
|
|
// turned it off before it was on by default.
|
|
func TestModelOnByDefault(t *testing.T) {
|
|
disk := false
|
|
was := onDisk
|
|
onDisk = func() bool { return disk }
|
|
t.Cleanup(func() { onDisk = was })
|
|
for _, c := range []struct {
|
|
st settings.Settings
|
|
disk bool
|
|
want bool
|
|
}{
|
|
{settings.Settings{}, false, true},
|
|
{settings.Settings{}, true, false},
|
|
{settings.Settings{RedactModelName: pii.AstrLinkGuard}, false, false},
|
|
{settings.Settings{RedactModelOff: true}, false, false},
|
|
{settings.Settings{RedactModelOff: true, RedactModelAuto: true}, true, false},
|
|
{settings.Settings{RedactModelAuto: true, RedactModelName: pii.AstrLinkGuard}, true, true},
|
|
{settings.Settings{RedactModel: true, RedactModelName: pii.PrivacyFilter}, true, true},
|
|
} {
|
|
disk = c.disk
|
|
if got := ModelWanted(c.st); got != c.want {
|
|
t.Errorf("on %v, off %v, auto %v, name %q, on disk %v: wanted %v, want %v", c.st.RedactModel, c.st.RedactModelOff, c.st.RedactModelAuto, c.st.RedactModelName, c.disk, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// fake is a model that is on disk or not, and fails or finds a name.
|
|
type fake struct {
|
|
pii.Model
|
|
installed, fails bool
|
|
}
|
|
|
|
func (fake) ID() string { return pii.AstrLinkGuard }
|
|
func (fake) MinScore() float64 { return 0.8 }
|
|
func (f fake) Installed() bool { return f.installed }
|
|
func (f fake) Find(_ context.Context, texts []string) ([][]redact.Found, error) {
|
|
if f.fails {
|
|
return nil, errors.New("pf-serve exited")
|
|
}
|
|
return [][]redact.Found{{{Start: 0, End: 6, Label: "private_person", Score: 0.9}}}, nil
|
|
}
|
|
|
|
// Until the model is on disk the rules mask alone and its download is
|
|
// started, the default's too: nothing is held back for it. On disk,
|
|
// AstrLink Guard finds Chinese names itself (NamesByModel).
|
|
func TestNoModelYetMasksByTheRules(t *testing.T) {
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
if err := settings.Save(settings.Settings{Redact: true, RedactPersonal: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m := fake{}
|
|
var fetches []bool
|
|
wasPicked, wasFetch, wasDisk := picked, fetch, onDisk
|
|
picked = func(settings.Settings) pii.Model { return m }
|
|
fetch = func(_ pii.Model, on bool) { fetches = append(fetches, on) }
|
|
onDisk = func() bool { return m.installed }
|
|
t.Cleanup(func() { picked, fetch, onDisk = wasPicked, wasFetch, wasDisk })
|
|
o := options()
|
|
if o.Model != nil || !slices.Equal(fetches, []bool{false}) {
|
|
t.Fatalf("not on disk: model %v, fetches %v", o.Model != nil, fetches)
|
|
}
|
|
if out, n := redact.Mask("key sk-ant-api03-"+strings.Repeat("a", 40), o); n != 1 || strings.Contains(out, "aaaa") {
|
|
t.Fatalf("the rules didn't mask: %q", out)
|
|
}
|
|
m.installed = true
|
|
if err := settings.Save(settings.Settings{Redact: true, RedactPersonal: true, RedactModelAuto: true, RedactModelName: pii.AstrLinkGuard}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if o := options(); o.Model == nil || !o.NamesByModel || len(fetches) != 1 {
|
|
t.Fatalf("on disk: model %v, names by model %v, fetches %v", o.Model != nil, o.NamesByModel, fetches)
|
|
}
|
|
}
|
|
|
|
// A model that fails holds the request back only where the user turned
|
|
// it on; on by default, it finds nothing and the rules mask alone.
|
|
func TestDefaultModelFailingIsTheRules(t *testing.T) {
|
|
m := fake{installed: true, fails: true}
|
|
if got := model(context.Background(), m, 0.8, false)([]string{"a", "b"}); len(got) != 2 || len(got[0])+len(got[1]) != 0 {
|
|
t.Fatalf("default: %v", got)
|
|
}
|
|
defer func() {
|
|
if recover() == nil {
|
|
t.Fatal("turned on by the user, a failing model didn't hold the request back")
|
|
}
|
|
}()
|
|
model(context.Background(), m, 0.8, true)([]string{"a"})
|
|
}
|