pii: a value the model found sure once in a text is sure wherever it found it there

wangwu25685333 on X (10-29): of one sentence with an address repeated
400 times, the address was left unmasked "a few times", thought to be
where the text was cut into chunks.

Reproduced with the real model (AstrLink Guard, pure Go, Linux box,
GOMAXPROCS=4) through masking's strict path and redact.MaskJSON, 400
copies of a sentence in one message:
  "Please ship it to 1600 Amphitheatre Parkway, Mountain View, CA 94043
  by Friday." joined by "\n": 346 of 400 left as written; by " " or
  nothing: 0. Two Chinese addresses, any joiner: 0.
It isn't where the text is cut. Guard found the address on every line,
but less surely the more often it had read it in the text: 0.99 alone,
0.59-0.70 at 20 copies, and on 400 the whole address 325 times at 0.50
to 0.90 and split ("1600" at 0.85, the rest at 0.50-0.59) 65 times.
Masking takes 0.8 and over, so it masked only the sure ones, about one
a piece. Joined by "\n" the pieces are alike, so every piece got the
same few sure finds.

Now spread puts each value of 8 bytes or more the model found in a text
(minSpread, as redact's minKnown) wherever else it is in that text,
with its label and the score of its surest find there, then merges. A
value never found sure stays as unsure as it was, and a house number
alone isn't looked for. It runs on each text and piece the model reads
and on a long text's merged pieces, before they are cached. Spans from
the disk cache written before keep their scores until read again.

After, the same probe: 0 of 400 left for every sentence and joiner.

Test: TestValueFoundSureOnceIsSureThroughoutItsText has a stand-in model
that finds an address sure only first in its first read and unsure
after, on 3 and 400 lines, and fails when an address is left unsure,
another value never found sure is raised, or a short one is spread.
Broken by hand: without spread in find.go it fails ("1600 Amphitheatre
Parkway" at 110 scored 0.50).

docs/subsystems/gateway-routing.md says this, the pieces' ends
(landmark) and the turn's order (takeTurn, queue) of the two commits
before.

At ca221e115 with the three pii commits of this push: gofmt -l clean;
go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
./... and go build ok (mote); go test ./internal/pii/...
./internal/masking ok on mote, on Windows (win-run) and with -race on
Linux (wsl-run). Not run before the push: go test ./... (both mote and
the Linux box were running other sessions' full suites, one a machine).

Not tried: the reporter's own sentence (not in the tweet); 400 copies
spread over 400 messages (each its own text, read alone at 0.99 here);
Privacy Filter (spread is the same for both models, tried on Guard).
This commit is contained in:
yetone
2026-10-29 01:28:25 +08:00
parent 43a9d04400
commit 2cdbe92ded
3 changed files with 107 additions and 3 deletions
File diff suppressed because one or more lines are too long
+54 -2
View File
@@ -6,6 +6,7 @@ import (
"crypto/sha256"
"errors"
"fmt"
"slices"
"sort"
"strings"
"sync"
@@ -240,7 +241,7 @@ func (b *backend) find(ctx context.Context, texts []string) ([][]redact.Found, e
if backlog[k] {
read += len(p.s)
}
got[k] = within(found[0], p.at, p.at+len(p.s))
got[k] = spread(p.s, within(found[0], p.at, p.at+len(p.s)))
b.cache.put(k, got[k])
}
b.cache.mu.Unlock()
@@ -449,7 +450,7 @@ func (b *backend) known(texts []string, got map[[32]byte][]redact.Found) (out []
miss = append(miss, i)
continue
}
out[i] = merge(all)
out[i] = spread(t, merge(all))
b.cache.put(sha256.Sum256([]byte(t)), out[i])
}
return out, miss
@@ -572,6 +573,57 @@ func landmark(t string, from, to int, c byte) int {
// enough that two lines alike but for a word or a number differ.
const landmarkLen = 48
// spread is fs with each value they found (minSpread bytes or more) found
// wherever else it is in t, as sure as the surest of its finds there, and
// of its label. The model is less sure of a value the more often it has
// read it in a text: an address on 400 lines alike was found on every
// line, at 0.90 on some and 0.50 on most, and masking (0.8) left 346 of
// them as they were (wangwu25685333 on X, 10-29: "a few of 400",
// thought to be where the text was cut; the 400 were left alike with the
// pieces cut elsewhere). A value found but not sure anywhere stays as
// unsure as it was.
func spread(t string, fs []redact.Found) []redact.Found {
type find struct {
label string
score float64
}
best := map[string]find{}
for _, f := range fs {
v := t[f.Start:f.End]
if len(v) < minSpread {
continue
}
if b, ok := best[v]; !ok || f.Score > b.score {
best[v] = find{f.Label, f.Score}
}
}
if len(best) == 0 {
return fs
}
out := slices.Clone(fs)
more := false
for v, b := range best {
for at := 0; ; {
i := strings.Index(t[at:], v)
if i < 0 {
break
}
out = append(out, redact.Found{Start: at + i, End: at + i + len(v), Label: b.label, Score: b.score})
more = true
at += i + len(v)
}
}
if !more {
return fs
}
return merge(out)
}
// minSpread is the shortest value spread finds again: what redact matches
// a value known before at (minKnown), so a short number or a name's
// first word isn't found all over a text.
const minSpread = 8
// merge puts a long text's spans from its pieces together: spans that
// overlap (one found in two pieces, or cut by a piece's end in one and
// whole in the next) become one, its label the longer's, and spans of one
+52
View File
@@ -257,3 +257,55 @@ func TestEditedTextReadsItsPiecesAgainOnly(t *testing.T) {
}
}
}
// wangwu25685333 on X (10-29): an address on 400 lines alike was found by
// the model on every line, sure (0.90) on some and unsure (0.50) on most,
// so masking left most as they were. A value found sure once in a text is
// sure wherever it is found again there, in one piece or many; one never
// found sure stays unsure, and a short one (a house number) isn't looked
// for.
func TestValueFoundSureOnceIsSureThroughoutItsText(t *testing.T) {
addr, other, short := "1600 Amphitheatre Parkway", "Elm Street 42", "1600"
r := newReader(t, guardChunk)
r.classify = func(texts []string) ([][]redact.Found, error) {
out := make([][]redact.Found, len(texts))
for i, s := range texts {
for v, score := range map[string]float64{addr: .5, other: .5, short: .5} {
for at := 0; ; {
j := strings.Index(s[at:], v)
if j < 0 {
break
}
f := redact.Found{Start: at + j, End: at + j + len(v), Label: "private_address", Score: score}
if v == addr && at == 0 && strings.HasPrefix(s, "sure ") || v == short && at == 0 {
f.Score = .9 // the model's first in each read
}
if v != short || f.Score == .9 {
out[i] = append(out[i], f)
}
at += j + len(v)
}
}
}
return out, nil
}
line := "Please ship it to " + addr + ", or to " + other + " by Friday. Room 1600.\n"
for _, text := range []string{"sure " + strings.Repeat(line, 3), "sure " + strings.Repeat(line, 400)} {
got, err := r.find(context.Background(), []string{text})
if err != nil {
t.Fatal(err)
}
n := map[string]int{}
for _, f := range got[0] {
v := text[f.Start:f.End]
if v == addr && f.Score != .9 || v == other && f.Score != .5 {
t.Fatalf("%d bytes: %q at %d scored %.2f", len(text), v, f.Start, f.Score)
}
n[v]++
}
lines := strings.Count(text, "\n")
if n[addr] != lines || n[other] != lines || n[short] > len(cut(text, guardChunk)) {
t.Fatalf("%d bytes, %d lines: found %v", len(text), lines, n)
}
}
}