diff --git a/internal/redact/eval_cases_test.go b/internal/redact/eval_cases_test.go index dee50f114..7809325d0 100644 --- a/internal/redact/eval_cases_test.go +++ b/internal/redact/eval_cases_test.go @@ -314,9 +314,11 @@ var evalCases = []evalCase{ Keep: []evalValue{{V: "Cicada", Kind: "PERSON"}, {V: "Mimi", Kind: "PERSON"}, {V: "Phosphor", Kind: "PERSON"}, {V: "Parakeet", Kind: "PERSON"}, {V: "Muesli", Kind: "PERSON"}}, }, { + // a word alone says nothing whose, and may be a person's + // ("Rina"): what the model takes for a name is masked (#5506) ID: "app-alone", From: "ylorn.", Text: "Muesli", - Keep: []evalValue{{V: "Muesli", Kind: "PERSON"}}, + Keep: []evalValue{{V: "Muesli", Kind: "PERSON", Known: "#5506"}}, }, { ID: "app-markdown", From: "ylorn.", diff --git a/internal/redact/json.go b/internal/redact/json.go index 9e399e1bc..59d8423b9 100644 --- a/internal/redact/json.go +++ b/internal/redact/json.go @@ -470,7 +470,9 @@ func MaskJSON(body []byte, o Options) ([]byte, int) { if !masked(path, key, s) || under(path, defs) { return s } - t, n := mask(s, o, put, asJSON(key, "")) + so := o + so.args = under(path, args) || key == "arguments" + t, n := mask(s, so, put, asJSON(key, "")) total += n return t }) diff --git a/internal/redact/model.go b/internal/redact/model.go index b5646de2f..d81d60b58 100644 --- a/internal/redact/model.go +++ b/internal/redact/model.go @@ -84,7 +84,7 @@ func modelSpans(s string, o Options) []span { if kind == "PERSON" { a, b = zhTrim(s, a, b) } - if a < b && utf8.ValidString(s[a:b]) && modelTakes(kind, s[a:b]) && modelFits(s, a, b, kind) && !(kind == "PERSON" && zhWordNotName(s, a, b)) { + if a < b && utf8.ValidString(s[a:b]) && modelTakes(kind, s[a:b]) && modelFits(s, a, b, kind, o.args) && !(kind == "PERSON" && zhWordNotName(s, a, b)) { out = append(out, span{a, b, kind, ByModel, f.Score, false}) } } @@ -307,7 +307,7 @@ func withModel(o Options, texts []string) Options { // A name's words each start with a capital, but for the particles names // have (van, de, bin): "local LLM", "User yetone" and claudeKeychain are // a program's or a log's words. -func modelFits(s string, a, b int, kind string) bool { +func modelFits(s string, a, b int, kind string, args bool) bool { if a > 0 && wordByte(s[a-1]) && wordByte(s[a]) || b < len(s) && wordByte(s[b-1]) && wordByte(s[b]) { return false } @@ -325,7 +325,7 @@ func modelFits(s string, a, b int, kind string) bool { } switch kind { case "PERSON": - return personFits(s, a, b, v) + return personFits(s, a, b, v, args) case "ACCOUNT": if hexRe.MatchString(v) && strings.ContainsAny(v, "abcdef") || versionRe.MatchString(v) || !numberLike(v) { return false @@ -739,7 +739,7 @@ func wordByte(c byte) bool { return c == '_' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' } -func personFits(s string, a, b int, v string) bool { +func personFits(s string, a, b int, v string, args bool) bool { if strings.ContainsAny(v, "0123456789/\\@<>|=_") || strings.IndexFunc(v, unicode.IsUpper) < 0 && isASCII(v) { return false } @@ -762,8 +762,10 @@ func personFits(s string, a, b int, v string) bool { return false } // an app's or a product's name the model took for a first name, where - // its sentence speaks of a thing (thingname.go) - if w := strings.Fields(v); len(w) == 1 && isASCII(v) && thingNamed(s, a, b) { + // its sentence speaks of a thing (thingname.go); never in a tool + // call's arguments, which name whom a tool acts for + // ({"recipient":"Rina"}) + if w := strings.Fields(v); len(w) == 1 && isASCII(v) && !args && thingNamed(s, a, b) { return false } for i, w := range strings.Fields(v) { diff --git a/internal/redact/redact.go b/internal/redact/redact.go index c9f1de7e5..85c2e8025 100644 --- a/internal/redact/redact.go +++ b/internal/redact/redact.go @@ -87,6 +87,9 @@ type Options struct { // found are the spans Model found, by text, for one request's texts found map[string][]Found + // args is masking a tool call's arguments (MaskJSON): the model's + // one-word name there is never taken for an app's (thingNamed) + args bool // sent is masking for a vendor (Mask, MaskJSON): a text masked // before on Route has the model's finds and the known values it had // then (sentSpans) diff --git a/internal/redact/thingname.go b/internal/redact/thingname.go index 37e232180..76d1b5b98 100644 --- a/internal/redact/thingname.go +++ b/internal/redact/thingname.go @@ -18,23 +18,32 @@ import ( // One word the model took for a name goes as written where what is // around it says a thing is meant, and nothing says a person is: // - the word before or after it says so: a person's (my colleague Mimi, -// Hi Mimi, Mimi said, 张伟先生) or a thing's (using Phosphor, switched -// to Muesli, the Phosphor app, Parakeet 这个软件); +// Hi Mimi, Mimi said, Mimi's Mac, 张伟先生) or a thing's (using +// Phosphor, switched to Muesli, the Phosphor app, Parakeet 这个软件); // - else its sentence: a pronoun, a relation or a title, a word of -// speech or a greeting say a person, and the name is masked; an app's, -// a tool's or a system's word, a price or a comparison say a thing; -// - else a text of nothing but such words (Cicada, Mimi, Phosphor) is a +// speech or a greeting say a person, and the name is masked; +// - else an item of a list whose other items speak of apps, or a text +// of nothing but two such words or more (Cicada, Mimi, Phosphor) is a // list of names with nothing to say whose, and goes as written. // -// What this lets go: a first name alone, in a sentence that names an app -// and has nothing of a person in it ("Mimi uses the app daily"), and a -// text that is only first names. A name of two words or more (John -// Smith), one in a sentence with he, she, said, a greeting or a relation -// in it, and every Chinese name are masked as before. +// An app's word elsewhere in the sentence says nothing of the name: with +// "Rina uses Windows at home" and "Rina's Mac won't boot" let go, a +// person's first name went out beside a system's. Nor does a text that +// is the word alone ("Rina", a tool's {"recipient":"Rina"}), and nothing +// in a tool call's arguments goes by this (personFits). +// +// What this lets go: a first name in a list of them, or with an app's +// word right beside it. A name of two words or more (John Smith), one in +// a sentence with he, she, said, a greeting or a relation in it, and +// every Chinese name are masked as before. // thingNamed says the one word at s[a:b] that the model took for a // person's name names a thing there. func thingNamed(s string, a, b int) bool { + if t := strings.TrimLeft(s[b:], "*_`\"'”)"); strings.HasPrefix(t, "'s") || strings.HasPrefix(t, "’s") { + // a possessive is a person's (Rina's Mac won't boot) + return false + } cs, ce := clauseAround(s, a, b) prev, prev2 := wordsBefore(s[cs:a]) next := latinAfter(s[b:ce]) @@ -50,9 +59,6 @@ func thingNamed(s string, a, b int) bool { if hasWord(clause, personWords) || containsAny(clause, zhPersonWords) { return false } - if hasWord(clause, thingWords) || containsAny(clause, zhThingWords) { - return true - } return listOfThings(s, a) || namesOnly(s) } @@ -228,7 +234,7 @@ func namesOnly(s string) bool { } n++ } - return n > 0 && n <= 20 + return n >= 2 && n <= 20 } func wordSet(ws ...string) map[string]bool { @@ -274,7 +280,7 @@ var ( "dad", "mother", "father", "son", "daughter", "brother", "sister", "boyfriend", "girlfriend", "teacher", "client", "customer", "mr", "mrs", "ms", "dr", "dear", "hi", "hello", "hey", "thanks", "regards", "cc", "said", "says", "told", "tells", "asked", "emailed", "texted", "birthday", "married") - // thingWords in a name's sentence say a thing is meant + // thingWords in a list's other items say its items are things thingWords = wordSet("app", "apps", "application", "applications", "software", "tool", "tools", "utility", "utilities", "plugin", "plugins", "extension", "extensions", "launcher", "widget", "widgets", "macos", "ios", "ipados", "android", "mac", "iphone", "ipad", "windows", "linux", "download", @@ -283,6 +289,5 @@ var ( "browser", "note-taking", "markdown", "rss") // and Chinese ones, found inside the text (it has no spaces) zhPersonWords = []string{"他", "她", "同事", "朋友", "老板", "先生", "女士", "老师", "说", "告诉"} - zhThingWords = []string{"应用", "软件", "工具", "插件", "客户端", "这款", "一款", "下载", "安装", "订阅", "功能", "版本", "替代", "对比", "比较", "好用"} zhThingAfter = []string{"这个软件", "这个应用", "这个工具", "这款", "应用", "软件", "客户端", "插件"} ) diff --git a/internal/redact/thingname_test.go b/internal/redact/thingname_test.go index e451f483f..49acce593 100644 --- a/internal/redact/thingname_test.go +++ b/internal/redact/thingname_test.go @@ -17,14 +17,12 @@ func TestAppNamesNotPersons(t *testing.T) { } for _, s := range []string{ "Cicada, Mimi, Phosphor, Parakeet, Muesli", - "Muesli", "Cicada and Mimi", "1. **Cicada** – a minimal note-taking app with Markdown.\n2. **Mimi** – a voice memo app with AI summaries.\n3. **Phosphor** – an RSS reader.\n4. **Parakeet** – a podcast player for macOS.\n5. **Muesli** – a meal planner.", - "Which is better for notes on my Mac, Cicada or Mimi?", "I switched to Muesli last week; Parakeet crashes too often.", "I'm using Phosphor now, and Cicada vs Mimi is still open.", "Apps like Muesli are hard to find.", - "Parakeet 这个软件很好用,Cicada 和 Mimi 呢?", + "Parakeet 这个软件很好用。", } { out, _ := Mask(s, Options{Personal: true, Model: f.find}) if out != s { @@ -51,7 +49,7 @@ func TestPersonBesideAppStillMasked(t *testing.T) { {"张伟说 Parakeet 这个软件很好用,让我也装一个。", []string{"张伟"}, []string{"Parakeet"}}, {"Hi Mimi, thanks for the notes from Tuesday's call. Can you send them to Laura as well?", []string{"Mimi", "Laura"}, nil}, {"Mimi will review the draft tomorrow and send it to Daniel.", []string{"Mimi", "Daniel"}, nil}, - {"Mimi", nil, nil}, // alone it says nothing whose: let go, by design + {"Mimi", []string{"Mimi"}, nil}, // alone it says nothing whose, and may be anyone's } { out, _ := Mask(c.s, Options{Personal: true, Model: f.find}) for _, v := range c.mask { @@ -113,3 +111,40 @@ func TestManyNamesRestoreEachToItself(t *testing.T) { t.Fatalf("restored\n got %q\nwant %q", got, want.String()) } } + +// A first name the model took for a person's goes out only where a +// thing's word is right beside it, or in a list of apps: not alone, not +// with an app's or a system's word elsewhere in its sentence, not as a +// possessive, and never in a tool call's arguments, which name whom a +// tool acts for. +func TestFirstNameNearAThingMasked(t *testing.T) { + f := &fakeModel{values: map[string]string{"Rina": "private_person"}} + o := Options{Personal: true, Model: f.find} + for _, s := range []string{ + "Rina", + "Rina's Mac won't boot", + "Rina’s iPhone is on iOS 26", + "Rina uses Windows at home", + "Send the license to Rina.", + } { + if out, _ := Mask(s, o); strings.Contains(out, "Rina") { + t.Errorf("went as written: %q", out) + } + } + for name, body := range map[string]string{ + "user text": `{"messages":[{"role":"user","content":"Rina"}]}`, + "tool_use": `{"messages":[{"role":"assistant","content":[{"type":"tool_use","id":"toolu_1","name":"send","input":{"recipient":"Rina"}}]}]}`, + "tool_use, an app's word beside": `{"messages":[{"role":"assistant","content":[{"type":"tool_use","id":"toolu_1","name":"send","input":{"note":"install Rina"}}]}]}`, + "function_call": `{"input":[{"type":"function_call","call_id":"c1","name":"send","arguments":"{\"recipient\":\"Rina\",\"note\":\"using Rina\"}"}]}`, + } { + if out, _ := MaskJSON([]byte(body), o); strings.Contains(string(out), "Rina") { + t.Errorf("%s: went as written: %s", name, out) + } + } + // an app's word right beside it still says a thing + for _, s := range []string{"I'm using Rina now.", "the Rina app crashed"} { + if out, _ := Mask(s, o); out != s { + t.Errorf("an app's name masked: %q", out) + } + } +}