Yayoi_no_yume on X asked for the privacy model's threshold to be adjustable: they read it as fixed at 0.5, and from their own use find 0.85-0.95 right for AstrLink. The 0.5 is Privacy Filter's (pf-serve's argv default); AstrLink Guard already masked at its publisher's 0.8. Both models now find at a floor of 0.5 (pii.Floor) and give each span its score: Guard's least token probability (rounded through float32, so a span read back from the cache file sits on the same side of a threshold), pf-serve's own. masking.model keeps the spans at pii.Threshold or over: settings.RedactModelScore[model] when it is in [0.5, 1), else the model's MinScore (Privacy Filter 0.5, AstrLink Guard 0.8). Settings -> Privacy shows a Confidence threshold row under the model (app menu, 0.5 to 0.95 and Default), /api/privacy-model takes do:"score". The setting is this computer's own, like the model. The defaults stay. TestMaskEval's cases, rules + model + magpie's checks, by threshold: Privacy Filter 0.5-0.8 masked 41/42 kept 112/113 Privacy Filter 0.85-0.95 masked 40/42 (a Chinese name lost) AstrLink Guard 0.5-0.7 masked 42/42 kept 112/113 AstrLink Guard 0.8-0.85 masked 41/42 (a phone in a tool result) AstrLink Guard 0.9 masked 40/42 AstrLink Guard 0.95 masked 40/42 kept 113/113 The model alone does raise fewer false alarms higher up (Privacy Filter's found-in-keep falls from 47 at 0.5 to 27 at 0.95, Guard's from 8 to 2), but the checks after it drop most of those already, so a higher threshold mostly costs values that should be masked. Cache file: found-<model>.bin is now "pfound02" with a float32 score per span. A "pfound01" file is still read, each span with score 1, so texts cached before mask as they did and aren't asked again. For those texts a higher threshold drops nothing and a lower one doesn't add a Guard span scored 0.5-0.8 until the cache turns over (20000 answers). merge() keeps the surer score of spans it joins. The eval recordings (eval-model.json, eval-astrlink.json) were made again with scores, from a scratch cache, the live found files untouched. Run in the worktree at11dcc743f+ this change: - go vet / gofmt -l on the touched packages: clean - go test -count=1 -timeout 45m ./internal/pii/... ./internal/masking ./internal/redact ./internal/settings ./internal/gui ./internal/gateway: all ok (gateway 546s, rerun after an earlier run hit "no space left on device" on this machine; every failure in that run was a TempDir mkdir). This run caught TestSettingsSaveKeepsWhatItDoesNotSend wanting RedactModelScore set; it is now - GUI: privacy-model.test.cjs 64/64 (chromium + webkit, 5 languages, 1100 and 640px; the new threshold menu test among them), i18n-keys 4/4, redact-kinds, redact-rule-edit, redact-rule-row, redact-shape, redact-words-any-case all pass. masking-records 82/82, routing-masking 10/10, privacy-usage-stats 4/4, mask-actions 22/22. privacy-hides-accounts was 3/4 once (webkit en, page.goto's 5s at load ~290, the class of #3525, commented there), then 4/4 three times. Not run: the rest of the GUI suite. Rebased on508911e43(which touched internal/pii fetch and mirror): at that head, go build ./..., Linux and Windows go vet of pii, masking, settings and redact, gofmt -l, and go test of pii/..., masking, redact, settings and gui all ok; gateway not run again after the rebase. Broken by hand: masking.model keeping every span (TestModelThreshold red); Threshold taking any value (TestModelThreshold red); the cache writing 1 for every score (TestRestartReadsWhatWasFound red); merge not keeping the surer score (TestMergeAcrossPieces red, after its joined span was made surer than the one it joins). Not tried: the real app's window, Windows, Privacy Filter on a GPU with the new score read (pf-serve itself is unchanged; it always sent the score).
188 lines
7.2 KiB
Go
188 lines
7.2 KiB
Go
package gui
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/pii"
|
|
"github.com/yetone/magpie/internal/redact"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// The local masking model is on only once it is on disk: turned on where
|
|
// it can't be downloaded, nothing is saved; the Settings page's own save
|
|
// never turns it on or off; and off is saved at once.
|
|
func TestPrivacyModelOnlyOnOnceDownloaded(t *testing.T) {
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
srv := Handler(webHost{func() {}}, nil)
|
|
do := func(method, path, body string) *httptest.ResponseRecorder {
|
|
rec := httptest.NewRecorder()
|
|
srv.ServeHTTP(rec, httptest.NewRequest(method, path, strings.NewReader(body)))
|
|
return rec
|
|
}
|
|
var st privacyModelJSON
|
|
if rec := do("GET", "/api/privacy-model", ""); rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &st) != nil {
|
|
t.Fatalf("state: %d %s", rec.Code, rec.Body)
|
|
}
|
|
if st.On || st.Installed || st.Size == 0 {
|
|
t.Fatalf("state %+v", st)
|
|
}
|
|
if !pii.Supported() {
|
|
if rec := do("POST", "/api/privacy-model", `{"do":"on","model":"privacy-filter"}`); rec.Code == http.StatusOK {
|
|
t.Fatalf("on where it can't run: %d %s", rec.Code, rec.Body)
|
|
}
|
|
if settings.Load().RedactModel {
|
|
t.Fatal("saved on with no model")
|
|
}
|
|
}
|
|
if rec := do("POST", "/api/privacy-model", `{"do":"on!"}`); rec.Code == http.StatusOK {
|
|
t.Fatalf("a bad do: %d", rec.Code)
|
|
}
|
|
if rec := do("POST", "/api/settings", `{"theme":"dark","lang":"en","redactModel":true}`); rec.Code != http.StatusOK {
|
|
t.Fatalf("settings save: %d %s", rec.Code, rec.Body)
|
|
}
|
|
if settings.Load().RedactModel {
|
|
t.Fatal("the Settings page's save turned it on")
|
|
}
|
|
if err := settings.SetRedactModel(true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if rec := do("POST", "/api/settings", `{"theme":"dark","lang":"en"}`); rec.Code != http.StatusOK {
|
|
t.Fatalf("settings save: %d %s", rec.Code, rec.Body)
|
|
}
|
|
if !settings.Load().RedactModel {
|
|
t.Fatal("the Settings page's save turned it off")
|
|
}
|
|
if rec := do("POST", "/api/privacy-model", `{"do":"off"}`); rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &st) != nil || st.On {
|
|
t.Fatalf("off: %d %s", rec.Code, rec.Body)
|
|
}
|
|
if settings.Load().RedactModel {
|
|
t.Fatal("off not saved")
|
|
}
|
|
}
|
|
|
|
// fakeModel is a pii.Model that is on disk, or downloads at once, and
|
|
// says what was done to it.
|
|
type fakeModel struct {
|
|
id string
|
|
installed bool
|
|
did *[]string
|
|
}
|
|
|
|
func (f *fakeModel) ID() string { return f.id }
|
|
func (f *fakeModel) Supported() bool { return true }
|
|
func (f *fakeModel) Installed() bool { return f.installed }
|
|
func (f *fakeModel) MinScore() float64 { return 0.5 }
|
|
func (f *fakeModel) Size() int64 { return 1 }
|
|
func (f *fakeModel) Find(context.Context, []string) ([][]redact.Found, error) {
|
|
return nil, nil
|
|
}
|
|
func (f *fakeModel) Now() pii.Status {
|
|
return pii.Status{Model: f.id, Supported: true, Installed: f.installed, Size: 1}
|
|
}
|
|
func (f *fakeModel) Download(done func()) error {
|
|
*f.did = append(*f.did, "download "+f.id)
|
|
f.installed = true
|
|
done()
|
|
return nil
|
|
}
|
|
func (f *fakeModel) Cancel() {}
|
|
func (f *fakeModel) Warm() { *f.did = append(*f.did, "warm "+f.id) }
|
|
func (f *fakeModel) Stop() { *f.did = append(*f.did, "stop "+f.id) }
|
|
func (f *fakeModel) Remove() error {
|
|
*f.did = append(*f.did, "remove "+f.id)
|
|
f.installed = false
|
|
return nil
|
|
}
|
|
|
|
// A new user's local model is AstrLink Guard; one picked while it is off
|
|
// is kept for turning it on, and one picked while it is on is downloaded
|
|
// and then masks, the one before let go.
|
|
func TestPrivacyModelPick(t *testing.T) {
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
var did []string
|
|
fakes := map[string]*fakeModel{
|
|
pii.PrivacyFilter: {id: pii.PrivacyFilter, did: &did},
|
|
pii.AstrLinkGuard: {id: pii.AstrLinkGuard, did: &did},
|
|
}
|
|
wasOf, wasModels := modelOf, models
|
|
t.Cleanup(func() { modelOf, models = wasOf, wasModels })
|
|
modelOf = func(id string) pii.Model { return fakes[id] }
|
|
models = func() []pii.Model { return []pii.Model{fakes[pii.PrivacyFilter], fakes[pii.AstrLinkGuard]} }
|
|
srv := Handler(webHost{func() {}}, nil)
|
|
do := func(body string) privacyModelJSON {
|
|
t.Helper()
|
|
rec := httptest.NewRecorder()
|
|
method := "POST"
|
|
if body == "" {
|
|
method = "GET"
|
|
}
|
|
srv.ServeHTTP(rec, httptest.NewRequest(method, "/api/privacy-model", strings.NewReader(body)))
|
|
var st privacyModelJSON
|
|
if rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &st) != nil {
|
|
t.Fatalf("%s: %d %s", body, rec.Code, rec.Body)
|
|
}
|
|
return st
|
|
}
|
|
if st := do(""); st.On || st.Model != pii.AstrLinkGuard || len(st.Models) != 2 || st.Models[0].Model != pii.PrivacyFilter {
|
|
t.Fatalf("a new user's state: %+v", st)
|
|
}
|
|
if st := do(`{"do":"pick","model":"privacy-filter"}`); st.On || st.Model != pii.PrivacyFilter || len(did) != 0 {
|
|
t.Fatalf("picked while off: %+v, %v", st, did)
|
|
}
|
|
if st := do(`{"do":"on"}`); !st.On || st.Model != pii.PrivacyFilter || settings.Load().RedactModelName != pii.PrivacyFilter {
|
|
t.Fatalf("on: %+v", st)
|
|
}
|
|
did = nil
|
|
if st := do(`{"do":"pick","model":"astrlink-guard"}`); !st.On || st.Model != pii.AstrLinkGuard {
|
|
t.Fatalf("picked while on: %+v", st)
|
|
}
|
|
if strings.Join(did, ", ") != "download astrlink-guard, stop privacy-filter, warm astrlink-guard" {
|
|
t.Fatalf("did %v", did)
|
|
}
|
|
// the threshold: the model's own until one is set, for the model
|
|
// masking uses with none named; 0 goes back to its own, and one out
|
|
// of range is refused
|
|
if st := do(""); st.MinScore != 0.5 || st.DefaultScore != 0.5 {
|
|
t.Fatalf("threshold before one is set: %+v", st)
|
|
}
|
|
if st := do(`{"do":"score","score":0.9}`); st.MinScore != 0.9 || st.DefaultScore != 0.5 || settings.Load().RedactModelScore[pii.AstrLinkGuard] != 0.9 {
|
|
t.Fatalf("threshold set: %+v, %v", st, settings.Load().RedactModelScore)
|
|
}
|
|
if st := do(`{"do":"score","model":"privacy-filter","score":0.6}`); st.MinScore != 0.9 || settings.Load().RedactModelScore[pii.PrivacyFilter] != 0.6 {
|
|
t.Fatalf("another model's threshold: %+v, %v", st, settings.Load().RedactModelScore)
|
|
}
|
|
for _, bad := range []string{`0.3`, `1`, `-1`} {
|
|
rec := httptest.NewRecorder()
|
|
srv.ServeHTTP(rec, httptest.NewRequest("POST", "/api/privacy-model", strings.NewReader(`{"do":"score","score":`+bad+`}`)))
|
|
if rec.Code == http.StatusOK || settings.Load().RedactModelScore[pii.AstrLinkGuard] != 0.9 {
|
|
t.Fatalf("threshold %s: %d", bad, rec.Code)
|
|
}
|
|
}
|
|
if st := do(`{"do":"score","score":0}`); st.MinScore != 0.5 || len(settings.Load().RedactModelScore) != 1 {
|
|
t.Fatalf("back to the default: %+v, %v", st, settings.Load().RedactModelScore)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
srv.ServeHTTP(rec, httptest.NewRequest("POST", "/api/privacy-model", strings.NewReader(`{"do":"pick","model":"gpt"}`)))
|
|
if rec.Code == http.StatusOK || settings.Load().RedactModelName != pii.AstrLinkGuard {
|
|
t.Fatalf("a model there's none of: %d", rec.Code)
|
|
}
|
|
did = nil
|
|
if st := do(`{"do":"remove"}`); st.On || strings.Join(did, ", ") != "remove privacy-filter, remove astrlink-guard" {
|
|
t.Fatalf("remove: %+v %v", st, did)
|
|
}
|
|
}
|