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.
165 lines
4.9 KiB
Go
165 lines
4.9 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/yetone/magpie/internal/masking"
|
|
"github.com/yetone/magpie/internal/pii"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// privacyModelJSON is Settings → Privacy's local masking model: whether it
|
|
// is on (masking.ModelWanted: the user's, or the default), the model masking uses (or would, turned on) with its own status
|
|
// (downloaded, downloading, where it runs) and threshold (the one the
|
|
// user set, else its default), and every model there is to pick, with
|
|
// theirs.
|
|
type privacyModelJSON struct {
|
|
On bool `json:"on"`
|
|
// Chosen says the user turned it on, not the default: only then is
|
|
// a request held back for it (masking.ModelOn)
|
|
Chosen bool `json:"chosen,omitempty"`
|
|
pii.Status
|
|
MinScore float64 `json:"minScore"`
|
|
DefaultScore float64 `json:"defaultScore"`
|
|
Models []pii.Status `json:"models"`
|
|
}
|
|
|
|
// modelOf and models are pii's; vars for tests.
|
|
var modelOf, models = pii.Of, pii.Models
|
|
|
|
func privacyModelState() privacyModelJSON {
|
|
st := settings.Load()
|
|
on := masking.ModelWanted(st)
|
|
m := modelOf(pii.Choose(st.RedactModelName, on))
|
|
out := privacyModelJSON{On: on, Chosen: st.RedactModel, Status: m.Now(), MinScore: pii.Threshold(st.RedactModelScore, m), DefaultScore: m.MinScore()}
|
|
for _, m := range models() {
|
|
out.Models = append(out.Models, m.Now())
|
|
}
|
|
return out
|
|
}
|
|
|
|
// privacyModelRoutes: GET /api/privacy-model is its state; POST with
|
|
// {"do": "on" | "off" | "cancel" | "remove" | "pick" | "score", "model": id,
|
|
// "score": s} turns
|
|
// it on (as model, or the one it would use: downloading it first, and
|
|
// saving it on once that is done), off (a download is stopped, the model
|
|
// let go and kept), stops a download, turns it off and deletes every
|
|
// model downloaded, or picks model: while it is on, that is turning it on
|
|
// as model (the one before masks until model is downloaded); while off,
|
|
// the pick is kept for turning it on; score keeps s as the threshold of
|
|
// model (the one masking uses, with none), 0 for its default.
|
|
func privacyModelRoutes(mux *http.ServeMux) {
|
|
mux.HandleFunc("GET /api/privacy-model", func(rw http.ResponseWriter, r *http.Request) {
|
|
writeJSON(rw, privacyModelState())
|
|
})
|
|
mux.HandleFunc("POST /api/privacy-model", func(rw http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
Do string `json:"do"`
|
|
Model string `json:"model"`
|
|
Score float64 `json:"score"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
if in.Model != "" && in.Model != pii.PrivacyFilter && in.Model != pii.AstrLinkGuard {
|
|
fail(rw, fmt.Errorf("model is %s or %s, not %q", pii.PrivacyFilter, pii.AstrLinkGuard, in.Model))
|
|
return
|
|
}
|
|
st := settings.Load()
|
|
on := masking.ModelWanted(st)
|
|
was := pii.Choose(st.RedactModelName, on)
|
|
var err error
|
|
switch in.Do {
|
|
case "pick":
|
|
switch {
|
|
case in.Model == "":
|
|
err = fmt.Errorf("pick needs a model")
|
|
case on:
|
|
err = turnOn(in.Model, was)
|
|
default:
|
|
err = settings.SetRedactModelName(in.Model)
|
|
}
|
|
case "score":
|
|
id := in.Model
|
|
if id == "" {
|
|
id = was
|
|
}
|
|
if in.Score != 0 && !(in.Score >= pii.Floor && in.Score < 1) {
|
|
err = fmt.Errorf("score is 0 (the model's own) or from %g to under 1, not %g", pii.Floor, in.Score)
|
|
} else {
|
|
err = settings.SetRedactModelScore(id, in.Score)
|
|
}
|
|
case "on":
|
|
id := in.Model
|
|
if id == "" {
|
|
id = was
|
|
}
|
|
err = turnOn(id, was)
|
|
case "off":
|
|
for _, m := range models() {
|
|
m.Cancel()
|
|
}
|
|
err = settings.SetRedactModel(false)
|
|
for _, m := range models() {
|
|
m.Stop()
|
|
}
|
|
case "cancel":
|
|
for _, m := range models() {
|
|
m.Cancel()
|
|
}
|
|
// the download on by default, cancelled, is the model
|
|
// turned off: it would start again on the next request
|
|
if on && !st.RedactModel && !modelOf(was).Installed() {
|
|
err = settings.SetRedactModel(false)
|
|
}
|
|
case "remove":
|
|
if err = settings.SetRedactModel(false); err == nil {
|
|
for _, m := range models() {
|
|
if e := m.Remove(); e != nil && err == nil {
|
|
err = e
|
|
}
|
|
}
|
|
}
|
|
default:
|
|
err = fmt.Errorf("do is on, off, cancel, remove, pick or score, not %q", in.Do)
|
|
}
|
|
if err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
writeJSON(rw, privacyModelState())
|
|
})
|
|
}
|
|
|
|
// turnOn turns the local model on as id, once it is on disk: a request
|
|
// in between is masked as before (by the rules, or by was, the model on
|
|
// till then), and not held back. A download of another model under way
|
|
// is stopped; was is let go once id masks.
|
|
func turnOn(id, was string) error {
|
|
m := modelOf(id)
|
|
if !m.Supported() {
|
|
return fmt.Errorf("the local masking model doesn't run on this system yet")
|
|
}
|
|
for _, o := range models() {
|
|
if o.ID() != id {
|
|
o.Cancel()
|
|
}
|
|
}
|
|
on := func() {
|
|
if settings.UseRedactModel(id) == nil {
|
|
if was != id {
|
|
modelOf(was).Stop()
|
|
}
|
|
m.Warm()
|
|
}
|
|
}
|
|
if m.Installed() {
|
|
on()
|
|
return nil
|
|
}
|
|
return m.Download(on)
|
|
}
|