Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
redact, pii: a local model finds what the masking rules can't (#1977)
yetonec0dad80da122
diff --git a/internal/masking/masking.go b/internal/masking/masking.goindex 0bcb6983a..6368b9f0e 100644--- a/internal/masking/masking.go+++ b/internal/masking/masking.go@@ -7,6 +7,7 @@ package maskingimport ("net/http"+ "github.com/yetone/magpie/internal/pii""github.com/yetone/magpie/internal/redact""github.com/yetone/magpie/internal/settings")@@ -21,7 +22,22 @@ func Wanted(st settings.Settings) bool {func options() redact.Options {st := settings.Load()- return redact.Options{Secrets: st.Redact, Personal: st.RedactPersonal, Kinds: st.RedactKinds, Words: st.RedactWords, Rules: st.RedactRules}+ o := redact.Options{Secrets: st.Redact, Personal: st.RedactPersonal, Kinds: st.RedactKinds, Words: st.RedactWords, Rules: st.RedactRules}+ if st.RedactModel {+ o.Model = model+ }+ return o+}++// model is the local model (internal/pii) as redact's Model. One that+// can't answer 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(texts []string) [][]redact.Found {+ got, err := pii.Find(texts)+ if err != nil {+ panic(err)+ }+ return got}func (Masker) JSON(body []byte) ([]byte, int) {diff --git a/internal/pii/fetch.go b/internal/pii/fetch.gonew file mode 100644index 000000000..ed23cb597--- /dev/null+++ b/internal/pii/fetch.go@@ -0,0 +1,267 @@+package pii++import (+ "archive/zip"+ "context"+ "crypto/sha256"+ "encoding/hex"+ "errors"+ "fmt"+ "io"+ "net/http"+ "os"+ "path/filepath"+ "strconv"+ "sync"+ "sync/atomic"+ "time"++ "github.com/yetone/magpie/internal/source"+)++// dl is the download under way, one at a time.+var dl struct {+ mu sync.Mutex+ cancel context.CancelFunc // nil when none is under way+ err string // the last one's, "" when it worked+ total int64+ done atomic.Int64+}++// client has no timeout: the model takes minutes on a slow line. A+// download that stops getting bytes is ended by stall instead, and one+// whose server doesn't answer by the header timeout.+var client = func() *http.Client {+ t := http.DefaultTransport.(*http.Transport).Clone()+ t.ResponseHeaderTimeout = 60 * time.Second+ return &http.Client{Transport: t}+}()++// stallAfter is how long a download waits for its next byte.+var stallAfter = 60 * time.Second++// Download fetches pf-serve and the model, each checked against its+// sha256, in the background, and calls done once both are on disk. A+// download already under way is left to go on. What was fetched of the+// model before (a download stopped, magpie quit) is taken up where it+// stopped.+func Download(done func()) error {+ if !Supported() {+ return fmt.Errorf("the local masking model doesn't run on %s yet", target())+ }+ dl.mu.Lock()+ defer dl.mu.Unlock()+ if dl.cancel != nil {+ return nil+ }+ ctx, cancel := context.WithCancel(context.Background())+ dl.cancel, dl.err, dl.total = cancel, "", modelSize+ dl.done.Store(0)+ go func() {+ err := install(ctx)+ dl.mu.Lock()+ dl.cancel = nil+ if err != nil {+ dl.err = err.Error()+ if ctx.Err() != nil {+ dl.err = ""+ }+ }+ dl.mu.Unlock()+ cancel()+ if err == nil && done != nil {+ done()+ }+ }()+ return nil+}++// Cancel stops the download under way, keeping what it fetched.+func Cancel() {+ dl.mu.Lock()+ if dl.cancel != nil {+ dl.cancel()+ }+ dl.mu.Unlock()+}++func install(ctx context.Context) error {+ if err := os.MkdirAll(dir(), 0o755); err != nil {+ return err+ }+ if _, err := os.Stat(exe("pf-serve")); err != nil {+ if err := installServe(ctx); err != nil {+ return fmt.Errorf("downloading pf-serve: %w", err)+ }+ }+ if fi, err := os.Stat(modelPath()); err == nil && fi.Size() == modelSize {+ dl.done.Store(modelSize)+ return nil+ }+ if err := fetch(ctx, modelURL, modelPath(), modelSize, modelSHA, &dl.done); err != nil {+ return fmt.Errorf("downloading the model: %w", err)+ }+ return nil+}++// installServe fetches pf-serve's zip and puts what is in it in+// serveDir: each file by its name alone, so nothing lands outside it.+func installServe(ctx context.Context) error {+ z := filepath.Join(dir(), serveTag+"-"+target()+".zip")+ defer os.Remove(z)+ var n atomic.Int64+ if err := fetch(ctx, serveBase+serveTag+"/pf-serve-"+target()+".zip", z, -1, serveSHA[target()], &n); err != nil {+ return err+ }+ zr, err := zip.OpenReader(z)+ if err != nil {+ return err+ }+ defer zr.Close()+ tmp := serveDir() + ".part"+ os.RemoveAll(tmp)+ if err := os.MkdirAll(tmp, 0o755); err != nil {+ return err+ }+ for _, f := range zr.File {+ if f.FileInfo().IsDir() {+ continue+ }+ if err := unzipTo(f, filepath.Join(tmp, filepath.Base(f.Name))); err != nil {+ os.RemoveAll(tmp)+ return err+ }+ }+ if _, err := os.Stat(filepath.Join(tmp, filepath.Base(exe("pf-serve")))); err != nil {+ os.RemoveAll(tmp)+ return fmt.Errorf("the zip has no %s", filepath.Base(exe("pf-serve")))+ }+ os.RemoveAll(serveDir())+ return os.Rename(tmp, serveDir())+}++func unzipTo(f *zip.File, path string) error {+ rc, err := f.Open()+ if err != nil {+ return err+ }+ defer rc.Close()+ out, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o755)+ if err != nil {+ return err+ }+ if _, err := io.Copy(out, rc); err != nil {+ out.Close()+ return err+ }+ return out.Close()+}++// fetch downloads url to path through path+".part", taking up a part+// already there with a Range request, and renames it to path only when+// its sha256 is want (and its size size, unless that is -1). done counts+// the bytes on disk as they come.+func fetch(ctx context.Context, url, path string, size int64, want string, done *atomic.Int64) error {+ if want == "" {+ return errors.New("no checksum pinned for " + url)+ }+ part := path + ".part"+ have := int64(0)+ if fi, err := os.Stat(part); err == nil {+ have = fi.Size()+ if size >= 0 && have > size {+ os.Remove(part)+ have = 0+ }+ }+ if size < 0 || have < size {+ got, err := get(ctx, url, part, have, done)+ if err != nil {+ return err+ }+ have = got+ }+ done.Store(have)+ if size >= 0 && have != size {+ return fmt.Errorf("got %d bytes of %d", have, size)+ }+ f, err := os.Open(part)+ if err != nil {+ return err+ }+ h := sha256.New()+ _, err = io.Copy(h, f)+ f.Close()+ if err != nil {+ return err+ }+ if hex.EncodeToString(h.Sum(nil)) != want {+ // a part taken up that wasn't this file's: the next try starts over+ os.Remove(part)+ return errors.New("the download's checksum doesn't match; try again")+ }+ return os.Rename(part, path)+}++// get appends what url has from byte from on to part, and says how long+// part is now. A server that answers the whole file has it written over.+func get(ctx context.Context, url, part string, from int64, done *atomic.Int64) (int64, error) {+ ctx, cancel := context.WithCancel(ctx)+ defer cancel()+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)+ if err != nil {+ return 0, err+ }+ if from > 0 {+ req.Header.Set("Range", "bytes="+strconv.FormatInt(from, 10)+"-")+ }+ // a mirror may serve it: what it serves is checked against the pin+ res, err := source.Do(client, req)+ if err != nil {+ return 0, err+ }+ defer res.Body.Close()+ flags := os.O_CREATE | os.O_WRONLY+ switch {+ case res.StatusCode == http.StatusPartialContent && from > 0:+ flags |= os.O_APPEND+ case res.StatusCode == http.StatusOK:+ flags |= os.O_TRUNC+ from = 0+ default:+ return 0, fmt.Errorf("%s: %s", url, res.Status)+ }+ out, err := os.OpenFile(part, flags, 0o644)+ if err != nil {+ return 0, err+ }+ n := from+ done.Store(n)+ var stalled atomic.Bool+ stall := time.AfterFunc(stallAfter, func() { stalled.Store(true); cancel() })+ defer stall.Stop()+ buf := make([]byte, 256<<10)+ for {+ k, rerr := res.Body.Read(buf)+ if k > 0 {+ stall.Reset(stallAfter)+ if _, err := out.Write(buf[:k]); err != nil {+ out.Close()+ return n, err+ }+ n += int64(k)+ done.Store(n)+ }+ if rerr == io.EOF {+ break+ }+ if rerr != nil {+ out.Close()+ if stalled.Load() {+ return n, fmt.Errorf("no data for %s", stallAfter)+ }+ return n, rerr+ }+ }+ return n, out.Close()+}diff --git a/internal/pii/fetch_test.go b/internal/pii/fetch_test.gonew file mode 100644index 000000000..072807d1d--- /dev/null+++ b/internal/pii/fetch_test.go@@ -0,0 +1,211 @@+package pii++import (+ "archive/zip"+ "bytes"+ "context"+ "crypto/sha256"+ "encoding/hex"+ "net/http"+ "net/http/httptest"+ "os"+ "path/filepath"+ "strings"+ "sync"+ "sync/atomic"+ "testing"+ "time"+)++func sum(b []byte) string {+ h := sha256.Sum256(b)+ return hex.EncodeToString(h[:])+}++// files serves each of files at its path, with Range as net/http does+// it, and keeps the Range header each request carried.+type files struct {+ mu sync.Mutex+ ranges []string+ m map[string][]byte+}++func (f *files) ServeHTTP(w http.ResponseWriter, r *http.Request) {+ f.mu.Lock()+ f.ranges = append(f.ranges, r.URL.Path+" "+r.Header.Get("Range"))+ b, ok := f.m[r.URL.Path]+ f.mu.Unlock()+ if !ok {+ http.NotFound(w, r)+ return+ }+ http.ServeContent(w, r, "", time.Time{}, bytes.NewReader(b))+}++// A download stopped halfway is taken up where it stopped.+func TestFetchTakesUpThePart(t *testing.T) {+ body := bytes.Repeat([]byte("model bytes "), 1000)+ f := &files{m: map[string][]byte{"/m": body}}+ s := httptest.NewServer(f)+ defer s.Close()+ path := filepath.Join(t.TempDir(), "m.gguf")+ os.WriteFile(path+".part", body[:5000], 0o644)+ var n atomic.Int64+ if err := fetch(context.Background(), s.URL+"/m", path, int64(len(body)), sum(body), &n); err != nil {+ t.Fatal(err)+ }+ if got, _ := os.ReadFile(path); !bytes.Equal(got, body) {+ t.Fatal("the file isn't what was served")+ }+ if f.ranges[0] != "/m bytes=5000-" {+ t.Fatalf("asked %q, want from byte 5000 on", f.ranges)+ }+ if n.Load() != int64(len(body)) {+ t.Fatalf("done = %d", n.Load())+ }+ if _, err := os.Stat(path + ".part"); err == nil {+ t.Fatal("the part was left")+ }+}++// A part that wasn't this file's fails the checksum, and is thrown away so+// that the next try starts over.+func TestFetchChecksumMismatch(t *testing.T) {+ body := bytes.Repeat([]byte("model bytes "), 1000)+ s := httptest.NewServer(&files{m: map[string][]byte{"/m": body}})+ defer s.Close()+ path := filepath.Join(t.TempDir(), "m.gguf")+ os.WriteFile(path+".part", bytes.Repeat([]byte("x"), 5000), 0o644)+ var n atomic.Int64+ err := fetch(context.Background(), s.URL+"/m", path, int64(len(body)), sum(body), &n)+ if err == nil || !strings.Contains(err.Error(), "checksum") {+ t.Fatalf("err = %v", err)+ }+ if _, err := os.Stat(path); err == nil {+ t.Fatal("a file that failed its checksum was kept")+ }+ if _, err := os.Stat(path + ".part"); err == nil {+ t.Fatal("the bad part was kept")+ }+ if err := fetch(context.Background(), s.URL+"/m", path, int64(len(body)), sum(body), &n); err != nil {+ t.Fatalf("the next try: %v", err)+ }+}++// A server that stops sending is given up on after stallAfter, and what+// it sent is kept for the next try.+func TestFetchStall(t *testing.T) {+ release := make(chan struct{})+ defer close(release)+ s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Length", "100000")+ w.Write(make([]byte, 1000))+ w.(http.Flusher).Flush()+ select {+ case <-r.Context().Done():+ case <-release:+ }+ }))+ defer s.Close()+ was := stallAfter+ stallAfter = 200 * time.Millisecond+ t.Cleanup(func() { stallAfter = was })+ path := filepath.Join(t.TempDir(), "m.gguf")+ var n atomic.Int64+ err := fetch(context.Background(), s.URL+"/m", path, 100000, strings.Repeat("0", 64), &n)+ if err == nil || !strings.Contains(err.Error(), "no data for") {+ t.Fatalf("err = %v", err)+ }+ if fi, err := os.Stat(path + ".part"); err != nil || fi.Size() != 1000 {+ t.Fatalf("part: %v %v", fi, err)+ }+}++// Download fetches pf-serve's zip and the model, puts each file of the+// zip in serveDir by its name alone, and calls done once both are there.+func TestDownloadInstalls(t *testing.T) {+ var zb bytes.Buffer+ zw := zip.NewWriter(&zb)+ for name, body := range map[string]string{+ "pf-serve-x/" + filepath.Base(exe("pf-serve")): "serve",+ "../../escape": "out",+ "LICENSE-ggml": "mit",+ "nested/dir/": "",+ } {+ w, _ := zw.Create(name)+ w.Write([]byte(body))+ }+ zw.Close()+ model := []byte("a small model")+ f := &files{m: map[string][]byte{+ "/rel/pf-v9/pf-serve-" + target() + ".zip": zb.Bytes(),+ "/model.gguf": model,+ }}+ s := httptest.NewServer(f)+ defer s.Close()++ d := t.TempDir()+ root := filepath.Join(d, "cache")+ wasDir, wasURL, wasSize, wasSHA := dir, modelURL, modelSize, modelSHA+ wasTag, wasBase, wasServe := serveTag, serveBase, serveSHA+ t.Cleanup(func() {+ dir, modelURL, modelSize, modelSHA = wasDir, wasURL, wasSize, wasSHA+ serveTag, serveBase, serveSHA = wasTag, wasBase, wasServe+ })+ dir = func() string { return root }+ modelURL, modelSize, modelSHA = s.URL+"/model.gguf", int64(len(model)), sum(model)+ serveTag, serveBase, serveSHA = "pf-v9", s.URL+"/rel/", map[string]string{target(): sum(zb.Bytes())}++ if Installed() {+ t.Fatal("installed before the download")+ }+ done := make(chan struct{})+ if err := Download(func() { close(done) }); err != nil {+ t.Fatal(err)+ }+ for deadline := time.Now().Add(30 * time.Second); ; time.Sleep(10 * time.Millisecond) {+ select {+ case <-done:+ default:+ if st := Now(); st.Error != "" || time.Now().After(deadline) {+ t.Fatalf("not done: %+v", st)+ }+ continue+ }+ break+ }+ if !Installed() {+ t.Fatal("not installed after the download")+ }+ if b, _ := os.ReadFile(exe("pf-serve")); string(b) != "serve" {+ t.Fatalf("pf-serve = %q", b)+ }+ if b, _ := os.ReadFile(filepath.Join(serveDir(), "LICENSE-ggml")); string(b) != "mit" {+ t.Fatalf("LICENSE-ggml = %q", b)+ }+ if _, err := os.Stat(filepath.Join(d, "escape")); err == nil {+ t.Fatal("a zip entry landed outside serveDir")+ }+ if _, err := os.Stat(filepath.Join(serveDir(), "escape")); err != nil {+ t.Fatal("a zip entry's file wasn't put in serveDir by its name")+ }+ if st := Now(); st.Downloading || st.Error != "" || st.Done != st.Total {+ t.Fatalf("Now = %+v", st)+ }+ if err := Remove(); err != nil {+ t.Fatal(err)+ }+ if _, err := os.Stat(root); err == nil {+ t.Fatal("Remove left the files")+ }+}++// A system pf-serve isn't built for is told so, and nothing is fetched.+func TestDownloadUnsupported(t *testing.T) {+ was := serveSHA+ serveSHA = map[string]string{}+ t.Cleanup(func() { serveSHA = was })+ if err := Download(nil); err == nil || !strings.Contains(err.Error(), "doesn't run on") {+ t.Fatalf("err = %v", err)+ }+}diff --git a/internal/pii/find.go b/internal/pii/find.gonew file mode 100644index 000000000..106cb0908--- /dev/null+++ b/internal/pii/find.go@@ -0,0 +1,136 @@+package pii++import (+ "container/list"+ "crypto/sha256"+ "sync"++ "github.com/yetone/magpie/internal/redact"+)++// Find is redact's Model: what the model finds in each of texts. A+// request's history comes back turn after turn, so what was found in a+// text is kept (by its hash) and the model is asked only about texts it+// hasn't seen. Those go to it joined into chunks of up to chunkSize: one+// pass over a few joined texts takes about what one over a single short+// text does (100 short messages: 683 ms one by one, 44 ms joined, on an+// M4 Pro's GPU).+func Find(texts []string) ([][]redact.Found, error) {+ out := make([][]redact.Found, len(texts))+ var miss []int+ cache.mu.Lock()+ for i, t := range texts {+ if fs, ok := cache.get(sha256.Sum256([]byte(t))); ok {+ out[i] = fs+ } else {+ miss = append(miss, i)+ }+ }+ cache.mu.Unlock()+ if len(miss) == 0 {+ return out, nil+ }+ chunks, parts := join(texts, miss)+ got, err := classify(chunks)+ if err != nil {+ return nil, err+ }+ for c, ps := range parts {+ for _, p := range ps {+ out[p.text] = within(got[c], p.at, p.at+len(texts[p.text]))+ }+ }+ cache.mu.Lock()+ for _, i := range miss {+ cache.put(sha256.Sum256([]byte(texts[i])), out[i])+ }+ cache.mu.Unlock()+ return out, nil+}++// chunkSize is how long a chunk of joined texts grows; a longer text+// goes alone, pf-serve reading it a window at a time.+const chunkSize = 12 << 10++// sep is put between joined texts: a paragraph's end, so the model reads+// each as a text of its own.+const sep = "\n\n"++type part struct{ text, at int }++// join puts texts[miss] into chunks, and says where in which each is.+func join(texts []string, miss []int) (chunks []string, parts [][]part) {+ cur, curParts := "", []part(nil)+ flush := func() {+ if curParts != nil {+ chunks, parts = append(chunks, cur), append(parts, curParts)+ cur, curParts = "", nil+ }+ }+ for _, i := range miss {+ t := texts[i]+ if curParts != nil && len(cur)+len(sep)+len(t) > chunkSize {+ flush()+ }+ if curParts != nil {+ cur += sep+ }+ curParts = append(curParts, part{i, len(cur)})+ cur += t+ }+ flush()+ return chunks, parts+}++// within are the spans of fs inside [a, b), made relative to a; one that+// runs past either end, over into the text beside it, is cut at the end.+func within(fs []redact.Found, a, b int) []redact.Found {+ var out []redact.Found+ for _, f := range fs {+ s, e := max(f.Start, a), min(f.End, b)+ if s < e {+ out = append(out, redact.Found{Start: s - a, End: e - a, Label: f.Label})+ }+ }+ return out+}++// cache keeps what was found in the texts seen last, by their hashes.+var cache = newLRU(20000)++type lru struct {+ mu sync.Mutex+ max int+ ll *list.List+ m map[[32]byte]*list.Element+}++type entry struct {+ k [32]byte+ fs []redact.Found+}++func newLRU(n int) *lru { return &lru{max: n, ll: list.New(), m: map[[32]byte]*list.Element{}} }++func (c *lru) get(k [32]byte) ([]redact.Found, bool) {+ e, ok := c.m[k]+ if !ok {+ return nil, false+ }+ c.ll.MoveToFront(e)+ return e.Value.(*entry).fs, true+}++func (c *lru) put(k [32]byte, fs []redact.Found) {+ if e, ok := c.m[k]; ok {+ c.ll.MoveToFront(e)+ e.Value.(*entry).fs = fs+ return+ }+ c.m[k] = c.ll.PushFront(&entry{k, fs})+ for c.ll.Len() > c.max {+ e := c.ll.Back()+ c.ll.Remove(e)+ delete(c.m, e.Value.(*entry).k)+ }+}diff --git a/internal/pii/main_test.go b/internal/pii/main_test.gonew file mode 100644index 000000000..e25af4cb2--- /dev/null+++ b/internal/pii/main_test.go@@ -0,0 +1,173 @@+package pii++import (+ "bufio"+ "encoding/binary"+ "encoding/json"+ "fmt"+ "io"+ "os"+ "path/filepath"+ "strings"+ "testing"+ "time"++ "github.com/yetone/magpie/internal/testenv"+)++// The tests run in a home of their own; the test binary started again+// with fakeVar set is a stand-in pf-serve (fakeServe).+func TestMain(m *testing.M) {+ if mode := os.Getenv(fakeVar); mode != "" {+ fakeServe(mode)+ return+ }+ testenv.Main(m)+}++const (+ fakeVar = "PII_FAKE_SERVE"+ // fakeLog is a file the stand-in appends what it did to, a line each+ fakeLog = "PII_FAKE_LOG"+)++// fakeServe speaks pf-serve's protocol and finds each "Alice" as a+// person. mode says how else it behaves:+//+// ok as pf-serve does+// gpudies on the GPU, ends at its first request+// hang never answers a request+// noload says it couldn't load the model+// novulkan as pf-serve-vulkan, says Vulkan is missing; else ok+func fakeServe(mode string) {+ name := strings.TrimSuffix(filepath.Base(os.Args[0]), ".exe")+ gpu := os.Getenv("PF_DEVICE") != "cpu"+ device := "MTL0"+ if !gpu {+ device = "CPU"+ }+ logf := func(format string, a ...any) {+ if p := os.Getenv(fakeLog); p != "" {+ f, err := os.OpenFile(p, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)+ if err == nil {+ fmt.Fprintf(f, format+"\n", a...)+ f.Close()+ }+ }+ }+ logf("start %s %s", name, device)+ if mode == "noload" || mode == "novulkan" && name == "pf-serve-vulkan" {+ json.NewEncoder(os.Stdout).Encode(map[string]string{"error": "can't load " + mode})+ os.Exit(1)+ }+ json.NewEncoder(os.Stdout).Encode(map[string]any{"device": device, "gpu": gpu, "ms": 7})+ in := bufio.NewReader(os.Stdin)+ out := bufio.NewWriter(os.Stdout)+ u32 := func() (uint32, error) {+ var b [4]byte+ _, err := io.ReadFull(in, b[:])+ return binary.LittleEndian.Uint32(b[:]), err+ }+ put := func(v any) { binary.Write(out, binary.LittleEndian, v) }+ for {+ n, err := u32()+ if err != nil {+ os.Exit(0) // stdin ended: magpie let it go+ }+ texts := make([]string, n)+ for i := range texts {+ l, _ := u32()+ b := make([]byte, l)+ io.ReadFull(in, b)+ texts[i] = string(b)+ }+ logf("request %d %q", n, texts)+ switch {+ case mode == "gpudies" && gpu:+ fmt.Fprintln(os.Stderr, "ggml_metal: device lost")+ os.Exit(3)+ case mode == "hang":+ time.Sleep(time.Hour)+ }+ put(uint32(0))+ for _, t := range texts {+ var at []int+ for i := 0; ; {+ j := strings.Index(t[i:], "Alice")+ if j < 0 {+ break+ }+ at = append(at, i+j)+ i += j + 5+ }+ put(uint32(len(at)))+ for _, a := range at {+ put(int32(a))+ put(int32(a + 5))+ put(float32(0.98))+ put(uint8(len("private_person")))+ out.WriteString("private_person")+ }+ }+ out.Flush()+ }+}++// fakeInstall puts the stand-in where pf-serve goes (and as+// pf-serve-vulkan too, with vulkan), and a model file, in a folder of the+// test's own; it gives the file the stand-in logs to.+func fakeInstall(t *testing.T, mode string, vulkan bool) string {+ t.Helper()+ d := t.TempDir()+ was, wasSize, wasIdle := dir, modelSize, idleAfter+ dir, modelSize = func() string { return d }, 4+ t.Cleanup(func() {+ Stop()+ srv.mu.Lock()+ srv.cpu, srv.gpuLost, srv.failed = false, "", ""+ srv.mu.Unlock()+ cache.mu.Lock()+ cache.ll.Init()+ clear(cache.m)+ cache.mu.Unlock()+ dir, modelSize, idleAfter = was, wasSize, wasIdle+ })+ if err := os.WriteFile(modelPath(), []byte("gguf"), 0o644); err != nil {+ t.Fatal(err)+ }+ self, err := os.Executable()+ if err != nil {+ t.Fatal(err)+ }+ b, err := os.ReadFile(self)+ if err != nil {+ t.Fatal(err)+ }+ os.MkdirAll(serveDir(), 0o755)+ names := []string{"pf-serve"}+ if vulkan {+ names = append(names, "pf-serve-vulkan")+ }+ for _, n := range names {+ if err := os.WriteFile(exe(n), b, 0o755); err != nil {+ t.Fatal(err)+ }+ }+ log := filepath.Join(d, "fake.log")+ t.Setenv(fakeVar, mode)+ t.Setenv(fakeLog, log)+ return log+}++// fakeLines are the lines the stand-in logged that start with prefix.+func fakeLines(t *testing.T, log, prefix string) []string {+ t.Helper()+ b, _ := os.ReadFile(log)+ var out []string+ for _, l := range strings.Split(string(b), "\n") {+ if strings.HasPrefix(l, prefix) {+ out = append(out, l)+ }+ }+ return out+}diff --git a/internal/pii/pii.go b/internal/pii/pii.gonew file mode 100644index 000000000..b5a66d4ca--- /dev/null+++ b/internal/pii/pii.go@@ -0,0 +1,137 @@+// Package pii runs Settings → Privacy's local model, which finds what the+// masking rules can't (internal/redact's Model): a name or an address in+// free text, a password with nothing around it to say so.+//+// The model is OpenAI's Privacy Filter (Apache-2.0), 1.6 GB as the q8 GGUF+// LocalAI made of it, run by pf-serve (native/privacy-filter), a program+// built from privacy-filter.cpp on ggml. Neither ships with magpie: both+// are downloaded when the user turns the model on, each checked against+// the sha256 pinned here, and the setting (settings.RedactModel) is saved+// only once both are. pf-serve is started on the first request that needs+// it, on the GPU where there is one (Metal, Vulkan) and else the CPU, and+// is ended after a while with none, which lets its memory go.+//+// A process of its own, not linked in: 1.7 GB the gateway would otherwise+// hold for good, and a GPU driver's crash that would end the gateway with+// it. One that dies is started again; one that dies on the GPU is started+// on the CPU for the rest of magpie's run.+package pii++import (+ "errors"+ "os"+ "path/filepath"+ "runtime"+ "time"++ "github.com/yetone/magpie/internal/appdir"+)++// The model, at a revision of LocalAI's repo, so the file is always the+// one hashed here.+var (+ modelURL = "https://huggingface.co/LocalAI-io/privacy-filter-GGUF/resolve/935d86882f4b5dc6e0eef05aae48ae5ba0a82a7a/privacy-filter-q8.gguf"+ modelSize = int64(1637572192)+ modelSHA = "80efc1803eda7c095a79741d2008c07e2e0a57b01bac8825fbeb448fd097998c"+)++// pf-serve, as the privacy-filter workflow built it at serveTag: a zip for+// each system, by its sha256. A system with none can't run the model.+var (+ serveTag = "pf-v1"+ serveBase = "https://github.com/yetone/magpie/releases/download/"+ serveSHA = map[string]string{}+)++// target is this system's name in serveSHA.+func target() string { return runtime.GOOS + "-" + runtime.GOARCH }++// Supported says pf-serve is built for this system.+func Supported() bool { return serveSHA[target()] != "" }++// Size is how much turning the model on downloads.+func Size() int64 { return modelSize }++// dir is where the model and pf-serve are kept: magpie's cache, since+// either can be downloaded again, and never the settings' folder, which a+// sync carries to the user's other computers.+var dir = func() string { return filepath.Join(appdir.Cache(), "privacy-filter") }++func modelPath() string { return filepath.Join(dir(), "privacy-filter-q8-"+modelSHA[:12]+".gguf") }++func serveDir() string { return filepath.Join(dir(), serveTag+"-"+target()) }++func exe(name string) string {+ if runtime.GOOS == "windows" {+ name += ".exe"+ }+ return filepath.Join(serveDir(), name)+}++// Installed says the model and pf-serve are on disk, as downloaded and+// checked: the model the size it was hashed at.+func Installed() bool {+ fi, err := os.Stat(modelPath())+ if err != nil || fi.Size() != modelSize {+ return false+ }+ _, err = os.Stat(exe("pf-serve"))+ return err == nil+}++// ErrNotInstalled is what a request gets when the model is on in the+// settings and isn't on disk (magpie's cache was cleared).+var ErrNotInstalled = errors.New("the local masking model isn't downloaded; download it again in Settings → Privacy, or turn it off")++// Status is what Settings → Privacy shows of the model.+type Status struct {+ Supported bool `json:"supported"`+ Installed bool `json:"installed"`+ Size int64 `json:"size"`+ // a download under way, or the last one's error+ Downloading bool `json:"downloading,omitempty"`+ Done int64 `json:"done,omitempty"`+ Total int64 `json:"total,omitempty"`+ Error string `json:"error,omitempty"`+ // pf-serve, while it runs: where the model is, and how long it took+ // to start+ Running bool `json:"running,omitempty"`+ Device string `json:"device,omitempty"`+ GPU bool `json:"gpu,omitempty"`+ LoadMS int `json:"loadMs,omitempty"`+ GPUError string `json:"gpuError,omitempty"`+ // why the last request got no answer+ Failed string `json:"failed,omitempty"`+}++// Now is the model's status.+func Now() Status {+ s := Status{Supported: Supported(), Installed: Installed(), Size: modelSize}+ dl.mu.Lock()+ s.Downloading, s.Error = dl.cancel != nil, dl.err+ s.Done, s.Total = dl.done.Load(), dl.total+ dl.mu.Unlock()+ srv.mu.Lock()+ if srv.h != nil {+ s.Running, s.Device, s.GPU, s.LoadMS, s.GPUError = true, srv.h.hello.Device, srv.h.hello.GPU, srv.h.hello.MS, srv.h.hello.GPUError+ }+ s.Failed = srv.failed+ if s.GPUError == "" {+ s.GPUError = srv.gpuLost+ }+ srv.mu.Unlock()+ return s+}++// Remove ends pf-serve and deletes the model and pf-serve.+func Remove() error {+ Cancel()+ Stop()+ dl.mu.Lock()+ dl.err = ""+ dl.mu.Unlock()+ return os.RemoveAll(dir())+}++// idleAfter is how long pf-serve runs with no request before it is ended.+var idleAfter = 10 * time.Minutediff --git a/internal/pii/serve.go b/internal/pii/serve.gonew file mode 100644index 000000000..0dbc23539--- /dev/null+++ b/internal/pii/serve.go@@ -0,0 +1,369 @@+package pii++import (+ "bufio"+ "bytes"+ "encoding/binary"+ "encoding/json"+ "errors"+ "fmt"+ "io"+ "os"+ "os/exec"+ "strings"+ "sync"+ "time"++ "github.com/yetone/magpie/internal/proc"+ "github.com/yetone/magpie/internal/redact"+)++// hello is the line pf-serve writes once the model is loaded.+type hello struct {+ Device string `json:"device"`+ GPU bool `json:"gpu"`+ MS int `json:"ms"`+ GPUError string `json:"gpuError"`+ Error string `json:"error"`+}++// helper is one pf-serve running.+type helper struct {+ cmd *exec.Cmd+ in io.WriteCloser+ out *bufio.Reader+ stderr *tail+ hello hello+ exited chan struct{}+}++// srv is the pf-serve magpie runs, one at a time: its requests take+// turns.+var srv struct {+ mu sync.Mutex+ h *helper+ idle *time.Timer+ // cpu is set once pf-serve died on the GPU: from then on it runs on+ // the CPU; gpuLost says why+ cpu bool+ gpuLost string+ // failed is why the last request got no answer, until one does+ failed string+}++// loadTimeout is how long pf-serve has to load the model: 1.6 GB read+// from a slow disk, and the GPU's kernels built.+var loadTimeout = 3 * time.Minute++// classifyTimeout is how long one request may take before pf-serve is+// taken for hung and ended: a second for each 4 KB on the slowest CPU,+// and 30 more.+var classifyTimeout = func(bytes int) time.Duration {+ return 30*time.Second + time.Duration(bytes/4096)*time.Second+}++// candidates are the programs to start, in turn: pf-serve-vulkan, where+// there is one, needs the Vulkan loader a GPU's driver installs and+// can't start without it; pf-serve runs anywhere.+func candidates(cpu bool) []string {+ var out []string+ if !cpu {+ if _, err := os.Stat(exe("pf-serve-vulkan")); err == nil {+ out = append(out, exe("pf-serve-vulkan"))+ }+ }+ return append(out, exe("pf-serve"))+}++// start runs pf-serve, the first of candidates that loads the model.+func start(cpu bool) (*helper, error) {+ if !Installed() {+ return nil, ErrNotInstalled+ }+ var errs []error+ for _, path := range candidates(cpu) {+ h, err := startOne(path, cpu)+ if err == nil {+ return h, nil+ }+ errs = append(errs, err)+ }+ return nil, fmt.Errorf("the local masking model didn't start: %w", errors.Join(errs...))+}++func startOne(path string, cpu bool) (*helper, error) {+ cmd := proc.Command(path, modelPath())+ cmd.Env = os.Environ()+ if cpu {+ cmd.Env = append(cmd.Env, "PF_DEVICE=cpu")+ }+ in, err := cmd.StdinPipe()+ if err != nil {+ return nil, err+ }+ out, err := cmd.StdoutPipe()+ if err != nil {+ return nil, err+ }+ h := &helper{cmd: cmd, in: in, out: bufio.NewReaderSize(out, 64<<10), stderr: &tail{}, exited: make(chan struct{})}+ cmd.Stderr = h.stderr+ if err := cmd.Start(); err != nil {+ return nil, err+ }+ go func() {+ cmd.Wait()+ close(h.exited)+ }()+ line := make(chan string, 1)+ go func() {+ s, _ := h.out.ReadString('\n')+ line <- s+ }()+ var s string+ select {+ case s = <-line:+ case <-time.After(loadTimeout):+ h.kill()+ return nil, fmt.Errorf("%s: the model didn't load in %s", baseName(path), loadTimeout)+ }+ if err := json.Unmarshal([]byte(s), &h.hello); err != nil || h.hello.Error != "" || h.hello.Device == "" {+ h.kill()+ why := h.hello.Error+ if why == "" {+ why = h.died()+ }+ return nil, fmt.Errorf("%s: %s", baseName(path), why)+ }+ return h, nil+}++func baseName(path string) string {+ path = strings.ReplaceAll(path, "\\", "/")+ return path[strings.LastIndexByte(path, '/')+1:]+}++// died says why pf-serve ended: what it wrote to stderr last, or how it+// exited.+func (h *helper) died() string {+ select {+ case <-h.exited:+ case <-time.After(2 * time.Second):+ }+ if t := strings.TrimSpace(h.stderr.String()); t != "" {+ return t+ }+ if st := h.cmd.ProcessState; st != nil {+ return st.String()+ }+ return "it ended"+}++func (h *helper) kill() {+ h.in.Close()+ if h.cmd.Process != nil {+ h.cmd.Process.Kill()+ }+ <-h.exited+}++// Warm starts pf-serve in the background, where the model is on disk, so+// that the first request doesn't wait for it to load, and Settings shows+// where it runs.+func Warm() {+ if Installed() {+ go classify([]string{"warm"})+ }+}++// Stop ends pf-serve, if it runs.+func Stop() {+ srv.mu.Lock()+ defer srv.mu.Unlock()+ stopLocked()+}++func stopLocked() {+ if srv.idle != nil {+ srv.idle.Stop()+ srv.idle = nil+ }+ if h := srv.h; h != nil {+ srv.h = nil+ // stdin ending ends it; one that doesn't is killed+ h.in.Close()+ select {+ case <-h.exited:+ case <-time.After(5 * time.Second):+ h.kill()+ }+ }+}++// classify asks pf-serve, started if it isn't, what is in texts. One that+// fails is ended and started again; one that failed on the GPU is started+// on the CPU, for good.+func classify(texts []string) ([][]redact.Found, error) {+ srv.mu.Lock()+ defer srv.mu.Unlock()+ out, err := classifyLocked(texts)+ srv.failed = ""+ if err != nil {+ srv.failed = err.Error()+ }+ return out, err+}++func classifyLocked(texts []string) ([][]redact.Found, error) {+ var last error+ for try := 0; try < 2; try++ {+ if srv.h == nil {+ h, err := start(srv.cpu)+ if err != nil {+ return nil, err+ }+ srv.h = h+ }+ out, err := srv.h.classify(texts)+ if err == nil {+ if srv.idle != nil {+ srv.idle.Stop()+ }+ h := srv.h+ srv.idle = time.AfterFunc(idleAfter, func() {+ srv.mu.Lock()+ defer srv.mu.Unlock()+ if srv.h == h {+ stopLocked()+ }+ })+ return out, nil+ }+ last = err+ gpu := srv.h.hello.GPU+ srv.h.kill()+ srv.h = nil+ if gpu {+ srv.cpu, srv.gpuLost = true, err.Error()+ }+ }+ return nil, fmt.Errorf("the local masking model failed: %w", last)+}++// classify is one request: n, then each text's length and bytes; the+// reply is a status, then each text's spans (pf-serve.cpp says how).+func (h *helper) classify(texts []string) ([][]redact.Found, error) {+ var b bytes.Buffer+ size := 0+ put := func(v uint32) { binary.Write(&b, binary.LittleEndian, v) }+ put(uint32(len(texts)))+ for _, t := range texts {+ put(uint32(len(t)))+ b.WriteString(t)+ size += len(t)+ }+ done, timeout := make(chan struct{}), classifyTimeout(size)+ defer close(done)+ go func() {+ select {+ case <-done:+ case <-time.After(timeout):+ // a hung pf-serve: killed, its pipe ends the read below+ h.cmd.Process.Kill()+ }+ }()+ if _, err := h.in.Write(b.Bytes()); err != nil {+ return nil, fmt.Errorf("%v (%s)", err, h.died())+ }+ r := &reader{r: h.out}+ status := r.u32()+ if status == 1 {+ msg := r.bytes(int(r.u32()))+ if r.err != nil {+ return nil, r.fail(h)+ }+ return nil, errors.New(string(msg))+ }+ out := make([][]redact.Found, len(texts))+ for i, t := range texts {+ n := int(r.u32())+ if r.err == nil && n > len(t)+1 {+ r.err = fmt.Errorf("%d spans in %d bytes", n, len(t))+ }+ for j := 0; j < n && r.err == nil; j++ {+ start, end := int32(r.u32()), int32(r.u32())+ r.u32() // the score: pf-serve sends only those over its threshold+ label := string(r.bytes(int(r.u8())))+ out[i] = append(out[i], redact.Found{Start: int(start), End: int(end), Label: label})+ }+ }+ if status != 0 && r.err == nil {+ r.err = fmt.Errorf("status %d", status)+ }+ if r.err != nil {+ return nil, r.fail(h)+ }+ return out, nil+}++type reader struct {+ r *bufio.Reader+ err error+ b [4]byte+}++func (r *reader) u32() uint32 {+ if r.err == nil {+ _, r.err = io.ReadFull(r.r, r.b[:4])+ }+ if r.err != nil {+ return 0+ }+ return binary.LittleEndian.Uint32(r.b[:4])+}++func (r *reader) u8() byte {+ if r.err == nil {+ var c byte+ c, r.err = r.r.ReadByte()+ return c+ }+ return 0+}++func (r *reader) bytes(n int) []byte {+ if r.err != nil {+ return nil+ }+ b := make([]byte, n)+ _, r.err = io.ReadFull(r.r, b)+ return b+}++func (r *reader) fail(h *helper) error {+ if errors.Is(r.err, io.EOF) || errors.Is(r.err, io.ErrUnexpectedEOF) {+ return fmt.Errorf("pf-serve ended: %s", h.died())+ }+ return r.err+}++// tail keeps the last 4 KB written to it.+type tail struct {+ mu sync.Mutex+ b []byte+}++func (t *tail) Write(p []byte) (int, error) {+ t.mu.Lock()+ t.b = append(t.b, p...)+ if len(t.b) > 4096 {+ t.b = t.b[len(t.b)-4096:]+ }+ t.mu.Unlock()+ return len(p), nil+}++func (t *tail) String() string {+ t.mu.Lock()+ defer t.mu.Unlock()+ return string(t.b)+}diff --git a/internal/pii/serve_test.go b/internal/pii/serve_test.gonew file mode 100644index 000000000..a2edc06c1--- /dev/null+++ b/internal/pii/serve_test.go@@ -0,0 +1,209 @@+package pii++import (+ "errors"+ "reflect"+ "strings"+ "testing"+ "time"++ "github.com/yetone/magpie/internal/redact"+)++func person(s, e int) redact.Found { return redact.Found{Start: s, End: e, Label: "private_person"} }++// Short texts go to pf-serve joined, in one request, and what it finds+// comes back to each text at that text's own offsets; a text seen before+// isn't asked about again.+func TestFindJoinsTextsAndMapsBack(t *testing.T) {+ log := fakeInstall(t, "ok", false)+ texts := []string{"hi Alice", "nothing here", "Bob, Alice and Alice"}+ got, err := Find(texts)+ if err != nil {+ t.Fatal(err)+ }+ want := [][]redact.Found{{person(3, 8)}, nil, {person(5, 10), person(15, 20)}}+ if !reflect.DeepEqual(got, want) {+ t.Fatalf("Find = %v, want %v", got, want)+ }+ if reqs := fakeLines(t, log, "request"); len(reqs) != 1 || !strings.HasPrefix(reqs[0], "request 1 ") {+ t.Fatalf("requests = %q, want one of one joined text", reqs)+ }+ again, err := Find([]string{"Bob, Alice and Alice", "new Alice"})+ if err != nil {+ t.Fatal(err)+ }+ if !reflect.DeepEqual(again, [][]redact.Found{want[2], {person(4, 9)}}) {+ t.Fatalf("Find again = %v", again)+ }+ reqs := fakeLines(t, log, "request")+ if len(reqs) != 2 || strings.Contains(reqs[1], "Bob") {+ t.Fatalf("requests = %q, want the second to ask about the new text alone", reqs)+ }+ if n := len(fakeLines(t, log, "start")); n != 1 {+ t.Fatalf("pf-serve started %d times, want once", n)+ }+ if st := Now(); !st.Running || !st.GPU || st.Device != "MTL0" || st.LoadMS != 7 {+ t.Fatalf("Now = %+v", st)+ }+}++func TestJoinSplitsAtChunkSize(t *testing.T) {+ long := strings.Repeat("x", chunkSize)+ texts := []string{"a", "b", long, "c"}+ chunks, parts := join(texts, []int{0, 1, 2, 3})+ if len(chunks) != 3 || chunks[0] != "a"+sep+"b" || chunks[1] != long || chunks[2] != "c" {+ t.Fatalf("chunks = %d: %.20q", len(chunks), chunks)+ }+ if !reflect.DeepEqual(parts, [][]part{{{0, 0}, {1, 1 + len(sep)}}, {{2, 0}}, {{3, 0}}}) {+ t.Fatalf("parts = %v", parts)+ }+}++// A span the model ran across the separator into the next text is cut at+// each text's end.+func TestWithinCutsSpansAtTheTextsEnds(t *testing.T) {+ fs := []redact.Found{person(2, 9), {Start: 0, End: 2, Label: "secret"}}+ if got := within(fs, 0, 5); !reflect.DeepEqual(got, []redact.Found{person(2, 5), {Start: 0, End: 2, Label: "secret"}}) {+ t.Fatalf("first text: %v", got)+ }+ if got := within(fs, 7, 12); !reflect.DeepEqual(got, []redact.Found{person(0, 2)}) {+ t.Fatalf("second text: %v", got)+ }+ if got := within(fs, 10, 12); got != nil {+ t.Fatalf("third text: %v", got)+ }+}++// pf-serve that dies on the GPU is started again on the CPU, which the+// request then gets its answer from, and stays there; the status says why.+func TestGPUDeathMovesToCPU(t *testing.T) {+ log := fakeInstall(t, "gpudies", false)+ got, err := Find([]string{"hi Alice"})+ if err != nil {+ t.Fatal(err)+ }+ if !reflect.DeepEqual(got, [][]redact.Found{{person(3, 8)}}) {+ t.Fatalf("Find = %v", got)+ }+ if starts := fakeLines(t, log, "start"); !reflect.DeepEqual(starts, []string{"start pf-serve MTL0", "start pf-serve CPU"}) {+ t.Fatalf("starts = %q", starts)+ }+ st := Now()+ if st.GPU || st.Device != "CPU" || !strings.Contains(st.GPUError, "device lost") {+ t.Fatalf("Now = %+v, want on the CPU saying the GPU was lost", st)+ }+ Stop()+ if _, err := Find([]string{"Alice again"}); err != nil {+ t.Fatal(err)+ }+ if starts := fakeLines(t, log, "start"); len(starts) != 3 || starts[2] != "start pf-serve CPU" {+ t.Fatalf("starts = %q, want the CPU after a stop too", starts)+ }+}++// A pf-serve-vulkan that can't start (no Vulkan loader) leaves it to+// pf-serve.+func TestVulkanThatWontStartFallsBack(t *testing.T) {+ log := fakeInstall(t, "novulkan", true)+ if _, err := Find([]string{"hi Alice"}); err != nil {+ t.Fatal(err)+ }+ if starts := fakeLines(t, log, "start"); !reflect.DeepEqual(starts, []string{"start pf-serve-vulkan MTL0", "start pf-serve MTL0"}) {+ t.Fatalf("starts = %q", starts)+ }+}++// A pf-serve that doesn't answer is killed, and the request fails rather+// than wait for good.+func TestHungServeIsKilled(t *testing.T) {+ log := fakeInstall(t, "hang", false)+ was := classifyTimeout+ classifyTimeout = func(int) time.Duration { return 300 * time.Millisecond }+ t.Cleanup(func() { classifyTimeout = was })+ _, err := Find([]string{"hi Alice"})+ if err == nil || !strings.Contains(err.Error(), "failed") {+ t.Fatalf("err = %v, want the model failed", err)+ }+ if n := len(fakeLines(t, log, "start")); n != 2 {+ t.Fatalf("started %d times, want 2 (one try again)", n)+ }+ if Now().Running {+ t.Fatal("a hung pf-serve is still taken for running")+ }+}++func TestServeThatCantLoadSaysWhy(t *testing.T) {+ fakeInstall(t, "noload", false)+ _, err := Find([]string{"hi Alice"})+ if err == nil || !strings.Contains(err.Error(), "can't load noload") {+ t.Fatalf("err = %v", err)+ }+}++// Warm starts pf-serve before a request asks, and one that can't start+// says why in Now; a request that is answered clears it.+func TestWarm(t *testing.T) {+ wait := func(what string, ok func(Status) bool) {+ for deadline := time.Now().Add(10 * time.Second); !ok(Now()); time.Sleep(10 * time.Millisecond) {+ if time.Now().After(deadline) {+ t.Fatalf("%s: %+v", what, Now())+ }+ }+ }+ fakeInstall(t, "noload", false)+ Warm()+ wait("no failure", func(s Status) bool { return strings.Contains(s.Failed, "can't load noload") })++ log := fakeInstall(t, "ok", false)+ srv.failed = "before"+ Warm()+ wait("not running", func(s Status) bool { return s.Running && s.Device == "MTL0" && s.Failed == "" })+ if n := len(fakeLines(t, log, "start")); n != 1 {+ t.Fatalf("started %d times, want 1", n)+ }+}++func TestNotInstalled(t *testing.T) {+ fakeInstall(t, "ok", false)+ modelSize = 5 // the file on disk isn't the model's size+ if _, err := Find([]string{"hi Alice"}); !errors.Is(err, ErrNotInstalled) {+ t.Fatalf("err = %v, want ErrNotInstalled", err)+ }+}++// pf-serve with no request for idleAfter is ended, and started again by+// the next one.+func TestIdleServeEnds(t *testing.T) {+ log := fakeInstall(t, "ok", false)+ idleAfter = 50 * time.Millisecond+ if _, err := Find([]string{"hi Alice"}); err != nil {+ t.Fatal(err)+ }+ for deadline := time.Now().Add(10 * time.Second); Now().Running; time.Sleep(10 * time.Millisecond) {+ if time.Now().After(deadline) {+ t.Fatal("pf-serve still runs")+ }+ }+ if _, err := Find([]string{"Alice, later"}); err != nil {+ t.Fatal(err)+ }+ if n := len(fakeLines(t, log, "start")); n != 2 {+ t.Fatalf("started %d times, want 2", n)+ }+}++func TestCacheKeepsTheLatest(t *testing.T) {+ c := newLRU(2)+ k := func(b byte) [32]byte { return [32]byte{b} }+ c.put(k(1), []redact.Found{person(0, 1)})+ c.put(k(2), nil)+ c.get(k(1))+ c.put(k(3), nil)+ if _, ok := c.get(k(2)); ok {+ t.Fatal("the least recent entry was kept")+ }+ if fs, ok := c.get(k(1)); !ok || len(fs) != 1 {+ t.Fatal("an entry read lately was dropped")+ }+}diff --git a/internal/redact/json.go b/internal/redact/json.goindex 85f2e676b..25b647dde 100644--- a/internal/redact/json.go+++ b/internal/redact/json.go@@ -360,6 +360,21 @@ func jsonEscape(s string) string {// Anthropic answers 400 "Invalid `signature` in `thinking` block".func MaskJSON(body []byte, o Options) ([]byte, int) {signed, args, defs := scan(body)+ masked := func(path, key, s string) bool {+ return !keepIn(path, key, s, args, defs) && (signed == nil || !under(path, signed))+ }+ if o.Model != nil {+ // the model is asked once, for all of the request's texts+ var texts []string+ if _, ok := walk(body, func(path, key, s string) string {+ if masked(path, key, s) {+ texts = append(texts, s)+ }+ return s+ }); ok {+ o = withModel(o, texts)+ }+ }total := 0var seen []string // value, placeholder, value, placeholder…put := func(kind, v string) string {@@ -368,7 +383,7 @@ func MaskJSON(body []byte, o Options) ([]byte, int) {return p}out, ok := walk(body, func(path, key, s string) string {- if keepIn(path, key, s, args, defs) || signed != nil && under(path, signed) {+ if !masked(path, key, s) {return s}t, n := mask(s, o, put)diff --git a/internal/redact/known.go b/internal/redact/known.goindex edcab4b49..edee768e5 100644--- a/internal/redact/known.go+++ b/internal/redact/known.go@@ -105,6 +105,12 @@ var kindCats = func() map[string][]string {}}}+ // and the model's: a name it found is personal data, not a secret+ for _, k := range modelKinds {+ if c := modelCat(k); c != "" && !slices.Contains(m[k], c) {+ m[k] = append(m[k], c)+ }+ }return m}()diff --git a/internal/redact/model.go b/internal/redact/model.gonew file mode 100644index 000000000..38d6efd0f--- /dev/null+++ b/internal/redact/model.go@@ -0,0 +1,138 @@+package redact++import (+ "strings"+ "unicode"+ "unicode/utf8"+)++// A local model (internal/pii, Settings → Privacy's "Find with a local+// model") finds what no rule can: a name or an address in free text, a+// password with no password= before it. Its spans are masked beside the+// rules', under the same switches: a name, an address, a phone number, an+// email or an account number is personal data, a secret is a secret.++// Found is a span the model found in a text: byte offsets, and its label.+type Found struct {+ Start, End int+ Label string+}++// Model finds spans in each of texts, in order. It never fails: one that+// can't find (the model didn't start) panics, so that the request is held+// back rather than sent unmasked (gwmw.Safely).+type Model func(texts []string) [][]Found++// modelKinds are the placeholders' kinds for the model's labels. Its+// private_date and private_url are left out: a date in a request is+// nearly always a log's or a version's, and a URL the agent's own.+var modelKinds = map[string]string{+ "private_person": "PERSON",+ "private_address": "ADDRESS",+ "private_phone": "PHONE",+ "private_email": "EMAIL",+ "account_number": "ACCOUNT",+ "secret": "SECRET",+}++// modelCat is the category (Options.covers) of each kind the model finds.+func modelCat(kind string) string {+ if kind == "SECRET" {+ return ""+ }+ return catPersonal+}++// modelSpans are o.found's spans for s that o covers, trimmed of the+// spaces the model takes in with a word, and with what it gets wrong+// often left out (modelTakes).+func modelSpans(s string, o Options) []span {+ fs := o.found[s]+ if len(fs) == 0 {+ return nil+ }+ var out []span+ for _, f := range fs {+ kind := modelKinds[f.Label]+ if kind == "" || !o.covers(modelCat(kind)) || f.Start < 0 || f.End > len(s) || f.Start >= f.End {+ continue+ }+ a, b := f.Start, f.End+ for a < b && isSpace(s[a]) {+ a+++ }+ for b > a && isSpace(s[b-1]) {+ b--+ }+ if a < b && utf8.ValidString(s[a:b]) && modelTakes(kind, s[a:b]) {+ out = append(out, span{a, b, kind})+ }+ }+ return out+}++func isSpace(c byte) bool { return c == ' ' || c == '\t' || c == '\n' || c == '\r' }++// modelTakes says v is masked as the model's kind: a secret as long as the+// rules ask of one, never an id a vendor or a tool made (req_…, toolu_…,+// call_…), which the agent and the vendor match against another; a name+// of two letters at least.+func modelTakes(kind, v string) bool {+ switch kind {+ case "SECRET":+ return len(v) >= 8 && !madeID(v)+ case "PERSON":+ return utf8.RuneCountInString(v) >= 2 && strings.IndexFunc(v, unicode.IsLetter) >= 0+ case "ACCOUNT":+ return !madeID(v)+ }+ return true+}++// madeID says v is an id of the shape vendors and tools give theirs: a+// short lower-case prefix, an underscore, then letters and digits+// (req_011CS…, toolu_01A…, call_x9…, msg_…).+func madeID(v string) bool {+ p, rest, ok := strings.Cut(v, "_")+ if !ok || len(p) < 2 || len(p) > 6 || len(rest) < 8 {+ return false+ }+ for _, c := range p {+ if c < 'a' || c > 'z' {+ return false+ }+ }+ for _, c := range rest {+ if !(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9') {+ return false+ }+ }+ return true+}++// withModel is o with what o.Model finds in texts, the texts that may be+// masked: each once, the short ones left out.+func withModel(o Options, texts []string) Options {+ if o.Model == nil || !o.Secrets && !o.Personal {+ return o+ }+ var ask []string+ seen := map[string]bool{}+ for _, t := range texts {+ if len(t) >= 3 && !seen[t] {+ seen[t] = true+ ask = append(ask, t)+ }+ }+ if len(ask) == 0 {+ return o+ }+ got := o.Model(ask)+ o.found = make(map[string][]Found, len(ask))+ for i, t := range ask {+ if i < len(got) && len(got[i]) > 0 {+ o.found[t] = got[i]+ }+ }+ return o+}diff --git a/internal/redact/model_test.go b/internal/redact/model_test.gonew file mode 100644index 000000000..916e07cd1--- /dev/null+++ b/internal/redact/model_test.go@@ -0,0 +1,137 @@+package redact++import (+ "slices"+ "strings"+ "testing"+)++// fakeModel finds each of values wherever a text has it, under its label,+// with a space before it as the model takes one in; asked are the texts it+// was asked about.+type fakeModel struct {+ values map[string]string // value → label+ asked [][]string+}++func (f *fakeModel) find(texts []string) [][]Found {+ f.asked = append(f.asked, texts)+ out := make([][]Found, len(texts))+ for i, t := range texts {+ for v, label := range f.values {+ for at := 0; ; {+ j := strings.Index(t[at:], v)+ if j < 0 {+ break+ }+ a := at + j+ if a > 0 && t[a-1] == ' ' {+ a--+ }+ out[i] = append(out[i], Found{a, at + j + len(v), label})+ at += j + len(v)+ }+ }+ }+ return out+}++// What the model finds and no rule does is masked, under the switch its+// kind is under: a name or an address with personal data, a secret with+// the secrets.+func TestModelFindsWhatRulesDont(t *testing.T) {+ const name, addr, secret = "Rina Kowalczyk", "42 Larchmont Ave, Springfield", "correct horse battery staple"+ f := &fakeModel{values: map[string]string{name: "private_person", addr: "private_address", secret: "secret"}}+ s := "Ask " + name + " at " + addr + "; the vault phrase is " + secret + "."++ out, n := Mask(s, Options{Personal: true, Secrets: true, Model: f.find})+ if n != 3 || strings.Contains(out, name) || strings.Contains(out, addr) || strings.Contains(out, secret) {+ t.Fatalf("%d masked: %q", n, out)+ }+ if !strings.Contains(out, "Ask {{PERSON_") || !strings.Contains(out, " at {{ADDRESS_") || !strings.Contains(out, "is {{SECRET_") {+ t.Fatalf("kinds, or the space before each taken in: %q", out)+ }+ if back := Restore(out, false); back != s {+ t.Fatalf("round trip: %q", back)+ }++ if out, _ := Mask(s, Options{Secrets: true, Model: f.find}); !strings.Contains(out, name) || !strings.Contains(out, addr) || strings.Contains(out, secret) {+ t.Fatalf("personal data off: %q", out)+ }+ if out, _ := Mask(s, Options{Personal: true, Model: f.find}); strings.Contains(out, name) || !strings.Contains(out, secret) {+ t.Fatalf("secrets off: %q", out)+ }+ // nothing on, nothing asked+ f.asked = nil+ if out, _ := Mask(s, Options{Model: f.find}); out != s || f.asked != nil {+ t.Fatalf("all off: %q, asked %q", out, f.asked)+ }+}++// A name the model found, masked once, is a known value of personal data:+// with personal data off it goes as it is, though secrets are on.+func TestModelKnownValueIsPersonal(t *testing.T) {+ const name = "Rina Kowalczyk-Brandt"+ f := &fakeModel{values: map[string]string{name: "private_person"}}+ if out, _ := Mask("from "+name, Options{Personal: true, Model: f.find}); strings.Contains(out, name) {+ t.Fatalf("not masked: %q", out)+ }+ if out, _ := Mask("to "+name, Options{Secrets: true}); !strings.Contains(out, name) {+ t.Fatalf("a name masked as a secret with personal data off: %q", out)+ }+ if out, _ := Mask("to "+name, Options{Personal: true}); strings.Contains(out, name) {+ t.Fatalf("a known name not masked again: %q", out)+ }+}++// What the model often gets wrong is left alone: a vendor's or a tool's+// id taken for a secret or an account, a secret shorter than a rule's, a+// one-letter name, a label magpie doesn't mask (a date, a URL).+func TestModelSpansFiltered(t *testing.T) {+ keep := map[string]string{+ "req_011CSzQ8mXv3kBR2": "secret",+ "toolu_01AbCdEfGhIjKl": "account_number",+ "hunter2": "secret",+ "J": "private_person",+ "2026-10-18": "private_date",+ "https://example.org": "private_url",+ }+ f := &fakeModel{values: keep}+ s := "see req_011CSzQ8mXv3kBR2 and toolu_01AbCdEfGhIjKl, pw hunter2, J said on 2026-10-18 at https://example.org"+ if out, n := Mask(s, Options{Personal: true, Secrets: true, Model: f.find}); out != s || n != 0 {+ t.Fatalf("%d masked: %q", n, out)+ }+ // a span outside the text, or splitting a character, is no span+ bad := func([]string) [][]Found {+ return [][]Found{{{-1, 3, "secret"}, {0, 99, "secret"}, {1, 4, "private_person"}}}+ }+ if out, _ := Mask("é名字abc", Options{Personal: true, Secrets: true, Model: bad}); out != "é名字abc" {+ t.Fatalf("bad spans: %q", out)+ }+}++// A request's texts go to the model once, together, each once: never its+// ids, kinds or tools' definitions, which are never masked, nor what a+// vendor sealed with a signature.+func TestModelAskedOnceForRequest(t *testing.T) {+ const name = "Rina Kowalczyk"+ f := &fakeModel{values: map[string]string{name: "private_person"}}+ body := `{"model":"claude-x","tools":[{"name":"lookup","description":"find ` + name + `","input_schema":{"type":"object"}}],` ++ `"messages":[{"role":"user","content":"hi ` + name + `"},{"role":"assistant","content":[{"type":"thinking","thinking":"about ` + name + `","signature":"sig"},{"type":"text","text":"hi ` + name + `"}]},` ++ `{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_01","content":"ok"}]}]}`+ out, n := MaskJSON([]byte(body), Options{Personal: true, Model: f.find})+ if len(f.asked) != 1 {+ t.Fatalf("asked %d times: %q", len(f.asked), f.asked)+ }+ got := slices.Clone(f.asked[0])+ slices.Sort(got)+ // "ok" is too short to hold anything+ if want := []string{"find " + name, "hi " + name}; !slices.Equal(got, want) {+ t.Fatalf("asked about %q, want %q", got, want)+ }+ // the description and the two messages; the thinking block goes back+ // as the vendor wrote it but for the value masked elsewhere+ if strings.Contains(string(out), "hi "+name) || strings.Contains(string(out), "find "+name) || n < 3 {+ t.Fatalf("%d masked: %s", n, out)+ }+}diff --git a/internal/redact/redact.go b/internal/redact/redact.goindex 5a9e9c8da..d21c256ea 100644--- a/internal/redact/redact.go+++ b/internal/redact/redact.go@@ -40,6 +40,11 @@ type Options struct {// id, where the user chose; one not in it is as its Category.On says.// None of them is masked while Personal is off.Kinds map[string]bool+ // Model, where set, finds more in the texts to mask (model.go)+ Model Model++ // found are the spans Model found, by text, for one request's texts+ found map[string][]Found}// covers says o masks what a rule of category cat finds: "" are the@@ -329,7 +334,9 @@ type span struct {}// Mask swaps what o covers in s for placeholders, and says how many.-func Mask(s string, o Options) (string, int) { return mask(s, o, placeholder) }+func Mask(s string, o Options) (string, int) {+ return mask(s, withModel(o, []string{s}), placeholder)+}// mask swaps what o covers in s for what put makes of each value.func mask(s string, o Options, put func(kind, v string) string) (string, int) {@@ -420,6 +427,7 @@ func mask(s string, o Options, put func(kind, v string) string) (string, int) {}// after the rules', so where a rule finds the same value it keeps the// kind it has always had+ found = append(found, modelSpans(s, o)...)found = append(found, knownSpans(s, o)...)return apply(s, found, put)}diff --git a/internal/settings/redact_model_test.go b/internal/settings/redact_model_test.gonew file mode 100644index 000000000..8fc19c450--- /dev/null+++ b/internal/settings/redact_model_test.go@@ -0,0 +1,30 @@+package settings++import "testing"++// The masking model is downloaded on one computer: a sync never turns it+// 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.KeepOwn(Settings{})+ if from.RedactModel {+ t.Fatal("a sync turned the model on")+ }+ from.KeepOwn(Settings{RedactModel: true})+ if !from.RedactModel {+ t.Fatal("a sync turned the model off")+ }+ if err := SetRedactModel(true); err != nil {+ t.Fatal(err)+ }+ if !Load().RedactModel {+ t.Fatal("not saved")+ }+ if err := SetRedactModel(false); err != nil {+ t.Fatal(err)+ }+ if Load().RedactModel {+ t.Fatal("not turned off")+ }+}diff --git a/internal/settings/settings.go b/internal/settings/settings.goindex c94e91fa7..8eac0c241 100644--- a/internal/settings/settings.go+++ b/internal/settings/settings.go@@ -87,8 +87,12 @@ type Settings struct {// RedactKinds turns the other kinds of personal data on or off one by// one, by redact.Categories' ids (ssn, ip, home…), under// RedactPersonal: only the user's choices, a kind not in it is as its- // category's On says.+ // category's On says. RedactModel has a local model (internal/pii) find+ // what the rules can't as well, a name or an address in free text,+ // under Redact and RedactPersonal; it is this computer's own, as the+ // model is downloaded on it.Redact bool `json:"redact,omitempty"`+ RedactModel bool `json:"redactModel,omitempty"`RedactPersonal bool `json:"redactPersonal,omitempty"`RedactKinds map[string]bool `json:"redactKinds,omitempty"`RedactWords []string `json:"redactWords,omitempty"`@@ -955,7 +959,8 @@ func (s Settings) Compact() int {// is shared on the local network and the addresses let in to it, the// Dock, gateway mode, whether WSL is looked in, whether agents' sign-ins// are read, when allowances are read, the terminal app a session resumes-// in, and what the menu bar or tray shows beside magpie's icon (yoooo on+// in, whether the local masking model is on (it is downloaded on this+// computer alone), and what the menu bar or tray shows beside magpie's icon (yoooo on// Discord: usage turned off on a Mac came back from a Windows box that// shows it; Lullaby: sharing turned on at home came on at work).func (s *Settings) KeepOwn(cur Settings) {@@ -971,6 +976,7 @@ func (s *Settings) KeepOwn(cur Settings) {s.LAN, s.AllowIPs = cur.LAN, cur.AllowIPss.FetchPrivate = cur.FetchPrivates.SessionTerminal = cur.SessionTerminal+ s.RedactModel = cur.RedactModel}// RenamePerModel moves what the user said of a provider's models to the id@@ -1174,6 +1180,16 @@ func SetGatewayConversations(on bool) error {return save(s, true)}+// SetRedactModel turns the local masking model on or off, on its own, as+// Settings → Privacy does once the model is downloaded.+func SetRedactModel(on bool) error {+ fileMu.Lock()+ defer fileMu.Unlock()+ s := load()+ s.RedactModel = on+ 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 itdiff --git a/internal/source/china_test.go b/internal/source/china_test.goindex 98d35fdb4..b2feab8fa 100644--- a/internal/source/china_test.go+++ b/internal/source/china_test.go@@ -28,10 +28,15 @@ func chinaOn(t *testing.T) {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://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/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 itdiff --git a/internal/source/source.go b/internal/source/source.goindex 5d27b7ec3..ba126271f 100644--- a/internal/source/source.go+++ b/internal/source/source.go@@ -26,6 +26,7 @@ const (// plugin list at jsDelivr, both copies of the very filesbunMirror = "https://registry.npmmirror.com/-/binary/bun"listMirror = "https://cdn.jsdelivr.net/gh/magpie-community/plugins@"+ hfMirror = "https://hf-mirror.com")// NPMMirror is npm's registry in China, npmmirror.@@ -39,7 +40,7 @@ var China = func() bool { return settings.Load().ChinaMirror }// "" when there is none. Only mirrors that serve the same bytes as the// official address: npmmirror for npm's registry and Bun's releases (whose// zips are checked against Bun's checksums all the same), jsDelivr for the-// community repo's plugin list. Never a proxy such as gh-proxy, which+// community repo's plugin list, hf-mirror for the masking model. Never a proxy such as gh-proxy, which// would hand magpie whatever it liked.func chinaFirst(raw string) string {u, err := url.Parse(raw)@@ -53,6 +54,13 @@ func chinaFirst(raw string) string {if rest, ok := strings.CutPrefix(u.EscapedPath(), "/oven-sh/bun/releases/download/"); ok && rest != "" {return bunMirror + "/" + rest}+ case "huggingface.co":+ // the local masking model (internal/pii), at the revision its+ // sha256 was pinned at: hf-mirror serves Hugging Face's files,+ // and what it serves is checked against that pin+ if strings.HasPrefix(u.EscapedPath(), "/LocalAI-io/privacy-filter-GGUF/resolve/") && u.RawQuery == "" {+ return hfMirror + u.EscapedPath()+ }case "raw.githubusercontent.com":// /magpie-community/plugins/<branch>/<path>if rest, ok := strings.CutPrefix(u.EscapedPath(), "/magpie-community/plugins/"); ok {