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).
331 lines
9.5 KiB
Go
331 lines
9.5 KiB
Go
package redact_test
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/pii"
|
|
"github.com/yetone/magpie/internal/redact"
|
|
)
|
|
|
|
// The masking evaluation: made-up texts of the kinds agents send (prose,
|
|
// code, logs, tool results, JSON), each with the values that must be
|
|
// masked and those that must go as written. The cases (eval_cases_test.go)
|
|
// start from what users reported masked wrongly, beside the values the
|
|
// masking is for. Nothing in them is anyone's data.
|
|
//
|
|
// Three ways are scored on them:
|
|
// - rules: the rules alone, the model off;
|
|
// - model: what the local model found, as it said it, before any check;
|
|
// - both: the rules and the model with magpie's checks on what it found,
|
|
// as a user with the model on gets.
|
|
//
|
|
// Each local model (pii.Models) is scored on its own. What it said is
|
|
// recorded in testdata (evalModels), so the test runs the whole of
|
|
// "both" without the model or the network, and fails when a value that
|
|
// came out right comes out wrong. A value known to come out wrong with a
|
|
// model says so (evalValue.Known for Privacy Filter, GuardKnown for
|
|
// AstrLink Guard, an issue), and fails once it comes out right, to have
|
|
// it taken off.
|
|
//
|
|
// To ask the real models again, after a case is added or a new model,
|
|
// point MAGPIE_EVAL_CACHE at the cache folder they were downloaded into
|
|
// (the folder that holds magpie/privacy-filter, magpie/astrlink-guard);
|
|
// each one there is asked:
|
|
//
|
|
// MAGPIE_EVAL_CACHE=$HOME/.cache go test -run TestMaskEval -v ./internal/redact
|
|
//
|
|
// which writes the recording again and logs the table.
|
|
func TestMaskEval(t *testing.T) {
|
|
asked := 0
|
|
for _, em := range evalModels {
|
|
t.Run(em.id, func(t *testing.T) {
|
|
rec := readRecording(t, em.path)
|
|
if dir := os.Getenv("MAGPIE_EVAL_CACHE"); dir != "" {
|
|
if r, ok := askModel(t, dir, pii.Of(em.id)); ok {
|
|
rec = r
|
|
writeRecording(t, em.path, rec)
|
|
asked++
|
|
}
|
|
}
|
|
evalModel(t, rec, em.known, pii.Of(em.id).MinScore())
|
|
})
|
|
}
|
|
if os.Getenv("MAGPIE_EVAL_CACHE") != "" && asked == 0 {
|
|
t.Fatal("no model is in MAGPIE_EVAL_CACHE")
|
|
}
|
|
}
|
|
|
|
// evalModels are the local models, each with its recording and the
|
|
// values known to come out wrong with it.
|
|
var evalModels = []struct {
|
|
id, path string
|
|
known func(evalValue) string
|
|
}{
|
|
{pii.PrivacyFilter, "testdata/eval-model.json", func(v evalValue) string { return v.Known }},
|
|
{pii.AstrLinkGuard, "testdata/eval-astrlink.json", func(v evalValue) string { return v.GuardKnown }},
|
|
}
|
|
|
|
// evalModel scores the recording rec, keeping what the model was at
|
|
// least min sure of, as masking does at that threshold.
|
|
func evalModel(t *testing.T, rec map[string]recorded, known func(evalValue) string, min float64) {
|
|
var stale []string
|
|
for _, c := range evalCases {
|
|
if r, ok := rec[c.ID]; !ok || r.Text != textHash(c.Text) {
|
|
stale = append(stale, c.ID)
|
|
}
|
|
}
|
|
if len(stale) > 0 {
|
|
t.Fatalf("the model's recording is not of cases %v; record it again with MAGPIE_EVAL_CACHE (see TestMaskEval)", stale)
|
|
}
|
|
kept := map[string]recorded{}
|
|
for id, r := range rec {
|
|
fs := []redact.Found{}
|
|
for _, f := range r.Found {
|
|
if f.Score >= min {
|
|
fs = append(fs, f)
|
|
}
|
|
}
|
|
kept[id] = recorded{Text: r.Text, Found: fs}
|
|
}
|
|
rec = kept
|
|
replay := func(texts []string) [][]redact.Found {
|
|
out := make([][]redact.Found, len(texts))
|
|
for i, s := range texts {
|
|
for _, c := range evalCases {
|
|
if c.Text == s {
|
|
out[i] = rec[c.ID].Found
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
redact.Forget()
|
|
var tally evalTally
|
|
for _, c := range evalCases {
|
|
rules := redact.Preview(c.Text, redact.Options{Secrets: true, Personal: true})
|
|
both := redact.Preview(c.Text, redact.Options{Secrets: true, Personal: true, Model: replay})
|
|
for _, v := range c.Mask {
|
|
checkValue(t, c, v, known(v), true, &tally, gone(rules, v.V), modelFound(c.Text, v.V, rec[c.ID].Found), gone(both, v.V))
|
|
}
|
|
for _, v := range c.Keep {
|
|
checkValue(t, c, v, known(v), false, &tally, gone(rules, v.V), modelFound(c.Text, v.V, rec[c.ID].Found), gone(both, v.V))
|
|
}
|
|
}
|
|
t.Log("\n" + tally.table())
|
|
}
|
|
|
|
// checkValue counts one value in each way, and holds "both" to what the
|
|
// case says of it.
|
|
func checkValue(t *testing.T, c evalCase, v evalValue, known string, mask bool, tally *evalTally, rules, model, both bool) {
|
|
t.Helper()
|
|
tally.add(v.Kind, mask, rules, model, both)
|
|
if rules != mask || model != mask || both != mask {
|
|
tally.wrong = append(tally.wrong, fmt.Sprintf("%-18s %-6s %-7s %-5s %-5s %-5s %q",
|
|
c.ID, map[bool]string{true: "mask", false: "keep"}[mask], v.Kind, mark(rules == mask), mark(model == mask), mark(both == mask), v.V))
|
|
}
|
|
if known != "" {
|
|
if both == mask {
|
|
t.Errorf("%s: %q now comes out right; take its known issue (%s) off", c.ID, v.V, known)
|
|
}
|
|
return
|
|
}
|
|
switch {
|
|
case mask && !both:
|
|
t.Errorf("%s: %s %q went as written", c.ID, v.Kind, v.V)
|
|
case !mask && both:
|
|
t.Errorf("%s: %q was masked, and should go as written", c.ID, v.V)
|
|
}
|
|
}
|
|
|
|
// placeholders are what masking puts in a value's place; a made-up key
|
|
// keeps no trace of the value, so the value is gone if it isn't there.
|
|
var placeholders = regexp.MustCompile(`\{\{[A-Z_]+_[a-z2-7]{8}\}\}`)
|
|
|
|
// gone says v is not in out as it is: masked, or a part of it masked.
|
|
// The placeholders are taken out first, since their suffix may spell v.
|
|
func gone(out, v string) bool {
|
|
return !strings.Contains(placeholders.ReplaceAllString(out, "\x00"), v)
|
|
}
|
|
|
|
// modelFound says the model put a span of a kind magpie masks over some
|
|
// of v, where v is in text.
|
|
func modelFound(text, v string, fs []redact.Found) bool {
|
|
for at := 0; ; {
|
|
i := strings.Index(text[at:], v)
|
|
if i < 0 {
|
|
return false
|
|
}
|
|
a, b := at+i, at+i+len(v)
|
|
for _, f := range fs {
|
|
if redact.ModelKinds[f.Label] != "" && f.Start < b && f.End > a {
|
|
return true
|
|
}
|
|
}
|
|
at = b
|
|
}
|
|
}
|
|
|
|
// evalTally counts, by kind, the values each way masked: of those to
|
|
// mask (found), and of those to keep (wrongly).
|
|
type evalTally struct {
|
|
kinds map[string]*evalCount
|
|
wrong []string // the values some way got wrong
|
|
}
|
|
|
|
func mark(right bool) string {
|
|
if right {
|
|
return "ok"
|
|
}
|
|
return "WRONG"
|
|
}
|
|
|
|
type evalCount struct {
|
|
mask, keep int
|
|
found, wrong [3]int // rules, model, both
|
|
}
|
|
|
|
func (t *evalTally) add(kind string, mask bool, got ...bool) {
|
|
if t.kinds == nil {
|
|
t.kinds = map[string]*evalCount{}
|
|
}
|
|
n := t.kinds[kind]
|
|
if n == nil {
|
|
n = &evalCount{}
|
|
t.kinds[kind] = n
|
|
}
|
|
if mask {
|
|
n.mask++
|
|
} else {
|
|
n.keep++
|
|
}
|
|
for i, g := range got {
|
|
if g && mask {
|
|
n.found[i]++
|
|
} else if g {
|
|
n.wrong[i]++
|
|
}
|
|
}
|
|
}
|
|
|
|
// table is the tally: for each kind, how many of the values to mask each
|
|
// way masked, and how many of those to keep it masked all the same.
|
|
func (t *evalTally) table() string {
|
|
var ks []string
|
|
all := &evalCount{}
|
|
for k, n := range t.kinds {
|
|
ks = append(ks, k)
|
|
all.mask += n.mask
|
|
all.keep += n.keep
|
|
for i := range 3 {
|
|
all.found[i] += n.found[i]
|
|
all.wrong[i] += n.wrong[i]
|
|
}
|
|
}
|
|
sort.Strings(ks)
|
|
var b strings.Builder
|
|
fmt.Fprintf(&b, "%-10s %18s %18s %18s\n", "", "rules", "model", "both")
|
|
fmt.Fprintf(&b, "%-10s %8s %9s %8s %9s %8s %9s\n", "kind", "masked", "wrongly", "masked", "wrongly", "masked", "wrongly")
|
|
line := func(k string, n *evalCount) {
|
|
fmt.Fprintf(&b, "%-10s", k)
|
|
for i := range 3 {
|
|
fmt.Fprintf(&b, " %8s %9s ", frac(n.found[i], n.mask), frac(n.wrong[i], n.keep))
|
|
}
|
|
b.WriteString("\n")
|
|
}
|
|
for _, k := range ks {
|
|
line(k, t.kinds[k])
|
|
}
|
|
line("all", all)
|
|
fmt.Fprintf(&b, "\n%-18s %-6s %-7s %-5s %-5s %-5s %s\n", "case", "should", "kind", "rules", "model", "both", "value")
|
|
for _, w := range t.wrong {
|
|
b.WriteString(w + "\n")
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
func frac(n, of int) string {
|
|
if of == 0 {
|
|
return "-"
|
|
}
|
|
return fmt.Sprintf("%d/%d", n, of)
|
|
}
|
|
|
|
// ---- the recording ----------------------------------------------------------
|
|
|
|
// recorded is what the model found in one case's text, and the text's
|
|
// hash, to tell a recording made of another text.
|
|
type recorded struct {
|
|
Text string `json:"text"`
|
|
Found []redact.Found `json:"found"`
|
|
}
|
|
|
|
func textHash(s string) string {
|
|
h := sha256.Sum256([]byte(s))
|
|
return hex.EncodeToString(h[:8])
|
|
}
|
|
|
|
func readRecording(t *testing.T, recordingPath string) map[string]recorded {
|
|
rec := map[string]recorded{}
|
|
b, err := os.ReadFile(recordingPath)
|
|
if os.IsNotExist(err) {
|
|
return rec
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := json.Unmarshal(b, &rec); err != nil {
|
|
t.Fatalf("%s: %v", recordingPath, err)
|
|
}
|
|
return rec
|
|
}
|
|
|
|
func writeRecording(t *testing.T, recordingPath string, rec map[string]recorded) {
|
|
b, err := json.MarshalIndent(rec, "", "\t")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tmp := recordingPath + ".tmp"
|
|
if err := os.WriteFile(tmp, append(b, '\n'), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Rename(tmp, recordingPath); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// askModel runs every case's text through the real model m, downloaded
|
|
// into the cache folder dir, one text at a time, as a request's text
|
|
// that has the model to itself is; false if m isn't there.
|
|
func askModel(t *testing.T, dir string, m pii.Model) (map[string]recorded, bool) {
|
|
t.Setenv("XDG_CACHE_HOME", dir)
|
|
if !m.Installed() {
|
|
t.Logf("%s isn't in %s; its recording is kept", m.ID(), filepath.Join(dir, "magpie"))
|
|
return nil, false
|
|
}
|
|
defer m.Stop()
|
|
rec := map[string]recorded{}
|
|
for _, c := range evalCases {
|
|
got, err := m.Find(context.Background(), []string{c.Text})
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", c.ID, err)
|
|
}
|
|
fs := got[0]
|
|
if fs == nil {
|
|
fs = []redact.Found{}
|
|
}
|
|
rec[c.ID] = recorded{Text: textHash(c.Text), Found: fs}
|
|
}
|
|
return rec, true
|
|
}
|