Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

masking, settings, gui: the local model the user picks (#3578)

yetonea019f4ffc023
diff --git a/internal/gui/api.go b/internal/gui/api.go
index 02d25113b..93505dc69 100644
--- a/internal/gui/api.go
+++ b/internal/gui/api.go
@@ -1276,6 +1276,7 @@ func Handler(w Windows, gw *gateway.Server) http.Handler {
in.GatewayConversations = cur.GatewayConversations // Sessions' explicit recording consent
in.RedactRules = cur.RedactRules // the masking rules, set on their own
in.RedactModel = cur.RedactModel // set on its own (privacy-model)
+ in.RedactModelName = cur.RedactModelName // set on its own (privacy-model)
in.RedactActions = cur.RedactActions // set on their own (redact-actions below)
in.RedactAllow = cur.RedactAllow // set on its own (redact-allow below)
in.DisabledModels = cur.DisabledModels // set on their own (disabled-models below, #1867)
diff --git a/internal/gui/privacymodel.go b/internal/gui/privacymodel.go
index 538adc4ac..789c1739b 100644
--- a/internal/gui/privacymodel.go
+++ b/internal/gui/privacymodel.go
@@ -10,62 +10,93 @@ import (
)
// privacyModelJSON is Settings → Privacy's local masking model: whether it
-// is on, and the model's own status (downloaded, downloading, where it
-// runs).
+// is on, the model masking uses (or would, turned on) with its own status
+// (downloaded, downloading, where it runs), and every model there is to
+// pick, with theirs.
type privacyModelJSON struct {
On bool `json:"on"`
pii.Status
+ Models []pii.Status `json:"models"`
}
+// modelOf and models are pii's; vars for tests.
+var modelOf, models = pii.Of, pii.Models
+
func privacyModelState() privacyModelJSON {
- return privacyModelJSON{On: settings.Load().RedactModel, Status: pii.Now()}
+ st := settings.Load()
+ out := privacyModelJSON{On: st.RedactModel, Status: modelOf(pii.Choose(st.RedactModelName, st.RedactModel)).Now()}
+ 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"} turns it on (downloading the
-// model first, and saving it on once that is done), off (a download is
-// stopped, pf-serve ended, the model kept), stops a download, or turns it off and deletes
-// the model.
+// {"do": "on" | "off" | "cancel" | "remove" | "pick", "model": id} 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.
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"`
+ Do string `json:"do"`
+ Model string `json:"model"`
}
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()
+ was := pii.Choose(st.RedactModelName, st.RedactModel)
var err error
switch in.Do {
+ case "pick":
+ switch {
+ case in.Model == "":
+ err = fmt.Errorf("pick needs a model")
+ case st.RedactModel:
+ err = turnOn(in.Model, was)
+ default:
+ err = settings.SetRedactModelName(in.Model)
+ }
case "on":
- if pii.Installed() {
- if err = settings.SetRedactModel(true); err == nil {
- pii.Warm()
- }
- break
+ id := in.Model
+ if id == "" {
+ id = was
}
- // on only once the model is on disk: a request in between is
- // masked by the rules, as before, and not held back
- err = pii.Download(func() {
- if settings.SetRedactModel(true) == nil {
- pii.Warm()
- }
- })
+ err = turnOn(id, was)
case "off":
- pii.Cancel()
+ for _, m := range models() {
+ m.Cancel()
+ }
err = settings.SetRedactModel(false)
- pii.Stop()
+ for _, m := range models() {
+ m.Stop()
+ }
case "cancel":
- pii.Cancel()
+ for _, m := range models() {
+ m.Cancel()
+ }
case "remove":
if err = settings.SetRedactModel(false); err == nil {
- err = pii.Remove()
+ for _, m := range models() {
+ if e := m.Remove(); e != nil && err == nil {
+ err = e
+ }
+ }
}
default:
- err = fmt.Errorf("do is on, off, cancel or remove, not %q", in.Do)
+ err = fmt.Errorf("do is on, off, cancel, remove or pick, not %q", in.Do)
}
if err != nil {
fail(rw, err)
@@ -74,3 +105,32 @@ func privacyModelRoutes(mux *http.ServeMux) {
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)
+}
diff --git a/internal/gui/privacymodel_test.go b/internal/gui/privacymodel_test.go
index 05575602e..53426421f 100644
--- a/internal/gui/privacymodel_test.go
+++ b/internal/gui/privacymodel_test.go
@@ -1,6 +1,7 @@
package gui
import (
+ "context"
"encoding/json"
"net/http"
"net/http/httptest"
@@ -9,6 +10,7 @@ import (
"testing"
"github.com/yetone/magpie/internal/pii"
+ "github.com/yetone/magpie/internal/redact"
"github.com/yetone/magpie/internal/settings"
)
@@ -35,7 +37,7 @@ func TestPrivacyModelOnlyOnOnceDownloaded(t *testing.T) {
t.Fatalf("state %+v", st)
}
if !pii.Supported() {
- if rec := do("POST", "/api/privacy-model", `{"do":"on"}`); rec.Code == http.StatusOK {
+ 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 {
@@ -67,3 +69,96 @@ func TestPrivacyModelOnlyOnOnceDownloaded(t *testing.T) {
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) 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)
+ }
+ 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)
+ }
+}
diff --git a/internal/gui/settings_keep_test.go b/internal/gui/settings_keep_test.go
index 5261870de..7c91fc104 100644
--- a/internal/gui/settings_keep_test.go
+++ b/internal/gui/settings_keep_test.go
@@ -181,6 +181,7 @@ func TestSettingsSaveKeepsWhatItDoesNotSend(t *testing.T) {
QuotaReads: "asked",
NoAccountDiscovery: true,
RedactModel: true,
+ RedactModelName: "astrlink-guard",
}
was.DesktopSubagentEffort = "high"
was.CodexAsyncCompact, was.CodexAsyncCompactPercent = true, 70
diff --git a/internal/masking/main_test.go b/internal/masking/main_test.go
new file mode 100644
index 000000000..c05b7488b
--- /dev/null
+++ b/internal/masking/main_test.go
@@ -0,0 +1,10 @@
+package masking
+
+import (
+ "testing"
+
+ "github.com/yetone/magpie/internal/testenv"
+)
+
+// The tests run in a home of their own, never the user's real files.
+func TestMain(m *testing.M) { testenv.Main(m) }
diff --git a/internal/masking/masking.go b/internal/masking/masking.go
index 439a3c19d..c61bd6e96 100644
--- a/internal/masking/masking.go
+++ b/internal/masking/masking.go
@@ -31,18 +31,21 @@ func optionsCtx(ctx context.Context) redact.Options {
o := redact.Options{Secrets: st.Redact, Personal: st.RedactPersonal, Kinds: st.RedactKinds, Words: st.RedactWords, WordsAnyCase: st.RedactWordsAnyCase, Rules: st.RedactRules,
Actions: st.RedactActions, Allow: st.RedactAllow, Shape: st.RedactShape}
if st.RedactModel {
- o.Model = model(ctx)
+ o.Model = model(ctx, picked(st))
}
return o
}
-// model is the local model (internal/pii) as redact's Model, waited for
+// picked is the local model st has masking use; a var for tests.
+var picked = func(st settings.Settings) pii.Model { return pii.Of(pii.Choose(st.RedactModelName, true)) }
+
+// model is the local model m (internal/pii) as redact's Model, waited for
// until ctx ends. One that can't answer, or isn't waited for any longer,
// panics, and the request is held back (gwmw.Safely) rather than sent
// with only the rules' masking: the user asked for the model's.
-func model(ctx context.Context) redact.Model {
+func model(ctx context.Context, m pii.Model) redact.Model {
return func(texts []string) [][]redact.Found {
- got, err := pii.FindContext(ctx, texts)
+ got, err := m.Find(ctx, texts)
if err != nil {
panic(err)
}
@@ -56,7 +59,11 @@ func ModelOn() bool { return settings.Shared().RedactModel }
// ModelBusy says what the local model is doing that a request may wait
// on, for the error of one that waited too long: "" when nothing.
func ModelBusy() string {
- st := pii.Now()
+ st := settings.Shared()
+ return busy(picked(st).Now())
+}
+
+func busy(st pii.Status) string {
var what string
switch {
case st.Loading:
diff --git a/internal/masking/masking_test.go b/internal/masking/masking_test.go
new file mode 100644
index 000000000..937f9d53e
--- /dev/null
+++ b/internal/masking/masking_test.go
@@ -0,0 +1,18 @@
+package masking
+
+import (
+ "testing"
+
+ "github.com/yetone/magpie/internal/pii"
+ "github.com/yetone/magpie/internal/settings"
+)
+
+// Masking asks the model the user picked; with none picked, Privacy
+// Filter, which was the only one when the model was turned on.
+func TestMaskingUsesThePickedModel(t *testing.T) {
+ for name, want := range map[string]string{"": pii.PrivacyFilter, 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)
+ }
+ }
+}
diff --git a/internal/settings/redact_model_test.go b/internal/settings/redact_model_test.go
index 8fc19c450..3fc476603 100644
--- a/internal/settings/redact_model_test.go
+++ b/internal/settings/redact_model_test.go
@@ -6,14 +6,14 @@ import "testing"
// on on another, nor off on this one.
func TestRedactModelIsThisComputers(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
- from := Settings{RedactModel: true}
+ from := Settings{RedactModel: true, RedactModelName: "astrlink-guard"}
from.KeepOwn(Settings{})
- if from.RedactModel {
+ if from.RedactModel || from.RedactModelName != "" {
t.Fatal("a sync turned the model on")
}
- from.KeepOwn(Settings{RedactModel: true})
- if !from.RedactModel {
- t.Fatal("a sync turned the model off")
+ from.KeepOwn(Settings{RedactModel: true, RedactModelName: "privacy-filter"})
+ if !from.RedactModel || from.RedactModelName != "privacy-filter" {
+ t.Fatal("a sync turned the model off, or changed it")
}
if err := SetRedactModel(true); err != nil {
t.Fatal(err)
@@ -27,4 +27,17 @@ func TestRedactModelIsThisComputers(t *testing.T) {
if Load().RedactModel {
t.Fatal("not turned off")
}
+ if err := UseRedactModel("astrlink-guard"); err != nil {
+ t.Fatal(err)
+ }
+ if st := Load(); !st.RedactModel || st.RedactModelName != "astrlink-guard" {
+ t.Fatalf("not saved: %v %q", st.RedactModel, st.RedactModelName)
+ }
+ // turned off, the model picked is kept for turning it on again
+ if err := SetRedactModel(false); err != nil {
+ t.Fatal(err)
+ }
+ if st := Load(); st.RedactModel || st.RedactModelName != "astrlink-guard" {
+ t.Fatalf("off: %v %q", st.RedactModel, st.RedactModelName)
+ }
}
diff --git a/internal/settings/settings.go b/internal/settings/settings.go
index aa51759ef..705077818 100644
--- a/internal/settings/settings.go
+++ b/internal/settings/settings.go
@@ -103,13 +103,17 @@ type Settings struct {
// under Redact and RedactPersonal; it is this computer's own, as the
// model is downloaded on it. RedactShape sends a key as a made-up key
// of its own form (sk_… as another sk_…) instead of a placeholder
- // (redact.Options.Shape; Charles Cao on X).
- Redact bool `json:"redact,omitempty"`
- RedactShape bool `json:"redactShape,omitempty"`
- RedactModel bool `json:"redactModel,omitempty"`
- RedactPersonal bool `json:"redactPersonal,omitempty"`
- RedactKinds map[string]bool `json:"redactKinds,omitempty"`
- RedactWords []string `json:"redactWords,omitempty"`
+ // (redact.Options.Shape; Charles Cao on X). RedactModelName is the
+ // local model picked (pii.PrivacyFilter, pii.AstrLinkGuard); "" for
+ // none picked, which pii.Choose reads. It is this computer's, as
+ // RedactModel is.
+ Redact bool `json:"redact,omitempty"`
+ RedactShape bool `json:"redactShape,omitempty"`
+ RedactModel bool `json:"redactModel,omitempty"`
+ RedactModelName string `json:"redactModelName,omitempty"`
+ RedactPersonal bool `json:"redactPersonal,omitempty"`
+ RedactKinds map[string]bool `json:"redactKinds,omitempty"`
+ RedactWords []string `json:"redactWords,omitempty"`
// RedactWordsAnyCase matches RedactWords in any case (ZoeCats on
// Discord); off, the default, a word is matched as written.
RedactWordsAnyCase bool `json:"redactWordsAnyCase,omitempty"`
@@ -1112,7 +1116,7 @@ func (s *Settings) KeepOwn(cur Settings) {
s.LAN, s.AllowIPs = cur.LAN, cur.AllowIPs
s.FetchPrivate = cur.FetchPrivate
s.SessionTerminal = cur.SessionTerminal
- s.RedactModel = cur.RedactModel
+ s.RedactModel, s.RedactModelName = cur.RedactModel, cur.RedactModelName
s.ClaudeRunsMost = cur.ClaudeRunsMost
s.RequestArchiveLocal, s.RequestArchiveLocalMB = cur.RequestArchiveLocal, cur.RequestArchiveLocalMB
}
@@ -1435,6 +1439,26 @@ func SetRedactModel(on bool) error {
return save(s, false)
}
+// SetRedactModelName keeps the local model the user picked while it is
+// off, for turning it on.
+func SetRedactModelName(name string) error {
+ fileMu.Lock()
+ defer fileMu.Unlock()
+ s := load()
+ s.RedactModelName = name
+ return save(s, false)
+}
+
+// UseRedactModel turns the local masking model on as name, the model
+// the user picked, once that model is downloaded.
+func UseRedactModel(name string) error {
+ fileMu.Lock()
+ defer fileMu.Unlock()
+ s := load()
+ s.RedactModel, s.RedactModelName = true, name
+ return save(s, false)
+}
+
// save is called under fileMu. Only the consent setter may replace recording.
func save(s Settings, recording bool) error {
defer filememo.Forget() // read again, where a request holds it