An agent sends its whole history on every turn, so with masking on every
turn ran each rule's regexp over every message before it again; on a long
session most of magpie's CPU went there (#4353). ruleSpans now keeps what
the rules found in a text of 256 bytes or more, keyed by a SHA-256 of the
text, the day (a date over a year old can be a birthday), Secrets,
Personal, the kinds turned on and off and the user's own rules. Only the
hash and the places found are kept, no value. Two generations of 65,536
entries, as sentMemo keeps.
Run on mote at this tree:
- gofmt, go vet (darwin, GOOS=windows, GOOS=linux): ok
- TestRulesRunOnceAText and TestRuleMemoKeysWhatTheRulesRead: ok 3 of 3
alone; -race with TestMask*: ok
- go test ./internal/redact: ok; go test -timeout 45m ./internal/gateway:
ok (662s; run before the counter moved into ruleSpans' find, which no
gateway code reads)
- Broken by hand: memo bypassed -> "the rules ran over 41 texts for one
new turn"; day left out of the key -> line 74 red; kinds left out ->
line 69 red.
- BenchmarkMaskNextTurn (Codex history of 80 turns, then 81):
99.6ms/op before, 6.6ms/op after.
Real use: sandbox HOME copied from the owner's, masking on, real DeepSeek
account, deepseek-flash, four turns of a 115-123KB history with mails and
phone numbers. Masking took 22-24ms a turn before; after, 24ms for the
first turn and 1.7-2.1ms for each next one. Every reply HTTP 200 "ok"
(one empty reply at finish_reason length on each binary, the model's
reasoning using max_tokens).
Not run: the full ./... suite; Windows and Linux runs of the tests.
Picked onto 54087aa43 (3 files changed, 199 insertions(+), 34 deletions(-), none in internal/redact); not run again there.
77 lines
2.8 KiB
Go
77 lines
2.8 KiB
Go
package redact
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func resetRuleMemo() {
|
|
ruleMemo.Lock()
|
|
ruleMemo.cur, ruleMemo.old = nil, nil
|
|
ruleMemo.Unlock()
|
|
}
|
|
|
|
// TestRulesRunOnceAText: a history masked for a vendor again on the next
|
|
// turn has the rules run only over what it did not have before (#4353),
|
|
// and comes out as it did.
|
|
func TestRulesRunOnceAText(t *testing.T) {
|
|
Forget()
|
|
resetRuleMemo()
|
|
o := Options{Secrets: true, Personal: true, Route: "codex"}
|
|
first := codexHistory(40)
|
|
want, _ := MaskJSON(first, o)
|
|
runs := ruleRuns.Load()
|
|
next := codexHistory(41)
|
|
got, _ := MaskJSON(next, o)
|
|
// the new turn's one long string, its tool's output
|
|
if n := ruleRuns.Load() - runs; n != 1 {
|
|
t.Fatalf("the rules ran over %d texts for one new turn", n)
|
|
}
|
|
if !strings.HasPrefix(string(got), string(want[:len(want)-len(`],"stream":true}`)])) {
|
|
t.Fatal("the history masked again is not what it was")
|
|
}
|
|
Forget()
|
|
resetRuleMemo()
|
|
if again, _ := MaskJSON(next, o); string(again) != string(got) {
|
|
t.Fatal("remembered and fresh rule finds differ")
|
|
}
|
|
}
|
|
|
|
// TestRuleMemoKeysWhatTheRulesRead: what the rules found is not taken for
|
|
// a text read under other options, the user's own rules or another day.
|
|
func TestRuleMemoKeysWhatTheRulesRead(t *testing.T) {
|
|
defer func(c func() time.Time) { dateClock = c }(dateClock)
|
|
dateClock = func() time.Time { return time.Date(2026, 10, 24, 9, 30, 0, 0, time.Local) }
|
|
resetRuleMemo()
|
|
text := strings.Repeat("filler words with nothing in them ", 8) + "DOB: 03/05/1990, mail rkato@example.org, token ACME-7f3k9q2w8e4r6t1y"
|
|
mine := []Rule{{Kind: "ACME", Prefix: "ACME-", Regex: `ACME-[0-9a-z]{16}`}}
|
|
kinds := func(o Options) string {
|
|
var out []string
|
|
for _, sp := range ruleSpans(text, o) {
|
|
out = append(out, sp.kind)
|
|
}
|
|
return strings.Join(out, ",")
|
|
}
|
|
secrets := kinds(Options{Secrets: true})
|
|
all := kinds(Options{Secrets: true, Personal: true})
|
|
if secrets == all {
|
|
t.Fatalf("personal data on finds what secrets alone do: %q", all)
|
|
}
|
|
if got := kinds(Options{Secrets: true}); got != secrets {
|
|
t.Fatalf("the same options found %q, then %q", secrets, got)
|
|
}
|
|
if got := kinds(Options{Secrets: true, Rules: mine}); !strings.Contains(got, "ACME") {
|
|
t.Fatalf("the user's own rule went unread: %q", got)
|
|
}
|
|
off := kinds(Options{Secrets: true, Personal: true, Kinds: map[string]bool{"birthday": false}})
|
|
if strings.Contains(off, "BIRTHDAY") || !strings.Contains(all, "BIRTHDAY") {
|
|
t.Fatalf("birthdays turned off still found (%q; with them on %q)", off, all)
|
|
}
|
|
// the day after the date: no birthday, a date an agent runs on
|
|
dateClock = func() time.Time { return time.Date(1990, 3, 6, 9, 30, 0, 0, time.Local) }
|
|
if got := kinds(Options{Secrets: true, Personal: true}); strings.Contains(got, "BIRTHDAY") {
|
|
t.Fatalf("a date of yesterday found as a birthday, as it was 36 years on: %q", got)
|
|
}
|
|
}
|