Files
magpie/internal/pii/bounded_test.go
T
yetone ac0b5c287f pii: a strict request goes once only what an earlier one sent unread is left (gaubeebangeel on X)
gaubeebangeel on X: with masking on, first tokens on a cloud server
came at 30-40s, 10-15s with it off.

Where the time goes, measured on a 60-turn Claude Code Messages body
(367-414 KB, a 4 KB tool result a turn; TestRealLatencyOfALongSession,
gated on GUARD_REAL_DIR, run in WSL on a Ryzen AI MAX+ 395):
- The rules are not it: 89ms for the body cold, 13ms warm (the rule
  memo); 7-11ms on mote's M5 Pro (BenchmarkMaskLongSession,
  BenchmarkScrubCodexTurn).
- AstrLink Guard on the CPU is: ~7 KB/s on its 4 cores (GOMAXPROCS=32),
  ~3 KB/s on one (GOMAXPROCS=2). A turn whose new text the model reads
  waits for it: 16 KB of new tool output is 3.0-3.5s on 4 cores and
  7.8-8.2s on one. That part is the model's reading and stays.
- With the model turned on (strict, 15s), a resumed or never-read
  history was read on every request, until the 15s ran out, though the
  vendor had that text already and it goes as it went on the same route
  (redact's sentSpans): its finds only feed learnLate. 4 cores: 15.1,
  15.0, 15.0, 7.8s, then 0.85s. 1 core: 15s on eight requests in a row,
  then 7.9s, then 1.9s.

Now a request with a bound that waits for every text (masking's strict
wait) marks what it goes without, as a Bounded one does (backend.sent),
and once all that is left is what an earlier request sent unread, it is
answered with a *Partial (afterSent): what it adds was read, the rest
has the rules' masking, as on the same route it had. The model reads on
into the backlog after the request, to the request's own bound, and
keeps what it finds for the next one. While it waits, a strict request
counts only what it adds for the queue, so it goes before a backlog
read on by an earlier request. A strict request with no bound still
reads everything; Bounded (the default) is unchanged.

After, same body, same box: 4 cores 15.1s (the first request: all new
text), then 0.97, 1.3, 1.4, 2.3, 3.0, 3.1, 3.1, 3.1, 2.9s; 1 core 15.1s,
then 2.1, 4.6, 4.5, 4.8, 4.7, 4.6, 5.7, 4.5, 4.9s. Those later turns
come back to back, so each shares the CPU with the backlog read on
after the one before; with an agent's think time between them it is
read meanwhile.

What changes in what is masked: on the same route, nothing in what a
text goes as. A value the model would find late in a backlog text masks
new text from the request after the read, not from the one it went
with. On another route (the user picked another provider), a backlog
text has the model's finds of now, none until it is read, and goes with
the rules' masking alone; before, it was read for up to 15s, newest
first.

Run (WSL magpie-desktop, at this commit rebased on 005178104):
go test ./internal/pii ./internal/masking ./internal/redact,
go test -run "Mask|Redact|Privacy|Model|Partial" ./internal/gateway,
go test -race -count=5 on TestStrictRequestGoesBeforeTheBacklog,
TestBounded* and TestNewestTextReadFirst, go vet (linux, windows,
darwin) on pii and masking, and the latency runs above with the real
AstrLink Guard files.
Broken by hand: with early forced false,
TestStrictRequestGoesBeforeTheBacklog goes red ("the request waited for
the history an earlier one sent unread", 20s).
Not run: the full suite (mote and mica each had suites queued on
suite.lock); Privacy Filter (pf-serve) on a CPU, which shares
backend.find; the queue-order change has no unit test of its own (a
fair one races the turn's hand-over), only the latency runs.
2026-11-13 22:03:56 +08:00

253 lines
8.0 KiB
Go

package pii
import (
"context"
"errors"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/yetone/magpie/internal/redact"
)
// fakeReader is a backend whose model finds "Alice" wherever it is, and
// keeps what it was asked.
type fakeReader struct {
*backend
mu sync.Mutex
asked []string
// during, when set, runs in each read
during func(n int)
}
func newReader(t *testing.T, chunk int) *fakeReader {
dir := t.TempDir()
r := &fakeReader{}
r.backend = &backend{turn: make(chan struct{}, 1), waiting: new(atomic.Int32), cache: newLRU(10000), disk: new(diskState),
path: func() string { return filepath.Join(dir, "found.bin") },
heldBack: func() error { return nil },
chunk: chunk,
classify: func(texts []string) ([][]redact.Found, error) {
r.mu.Lock()
r.asked = append(r.asked, texts...)
n := len(r.asked)
r.mu.Unlock()
if r.during != nil {
r.during(n)
}
out := make([][]redact.Found, len(texts))
for i, s := range texts {
for at := 0; ; {
j := strings.Index(s[at:], "Alice")
if j < 0 {
break
}
out[i] = append(out[i], redact.Found{Start: at + j, End: at + j + 5, Label: "private_person", Score: 0.9})
at += j + 5
}
}
return out, nil
}}
t.Cleanup(func() {
r.disk.mu.Lock()
if r.disk.timer != nil && r.disk.timer.Stop() {
r.disk.saves.Done()
}
r.disk.timer = nil
r.disk.mu.Unlock()
r.disk.saves.Wait()
})
return r
}
// bytes is how much the model was asked to read, and how many times.
func (r *fakeReader) bytes() (n, reads int) {
r.mu.Lock()
defer r.mu.Unlock()
for _, s := range r.asked {
n += len(s)
}
return n, len(r.asked)
}
// history is n turns of about size bytes each, the last named "turn n".
func history(n, size int) []string {
var out []string
for i := range n {
out = append(out, "turn "+strings.Repeat("x", 3)+string(rune('A'+i%26))+" "+strings.Repeat("words go here ", size/14))
}
return out
}
// An agent's history is oldest first, and what is new in a request is
// at its end: that is read first, so what a request waits for is what
// it adds, not the history behind it. Before, the oldest text was read
// first, and a history not read yet kept the newest turn waiting behind
// it on every request (xinvxueyuan and shensmart on Discord, 10-28).
func TestNewestTextReadFirst(t *testing.T) {
r := newReader(t, 0)
texts := []string{strings.Repeat("a ", chunkSize/2-8), strings.Repeat("b ", chunkSize/2-8), "Alice wrote: " + strings.Repeat("c ", chunkSize/2-16)}
if _, err := r.find(context.Background(), texts); err != nil {
t.Fatal(err)
}
if len(r.asked) != 3 || !strings.HasPrefix(r.asked[0], "Alice") {
t.Fatalf("read first: %.20q of %d", r.asked[0], len(r.asked))
}
// a long text's last piece goes first too
r = newReader(t, 0)
long := strings.Repeat("a ", pieceSize) + " Alice " + strings.Repeat("z ", 100)
if _, err := r.find(context.Background(), []string{long}); err != nil {
t.Fatal(err)
}
if !strings.Contains(r.asked[0], "Alice") {
t.Fatalf("a long text's first piece read first: %.20q", r.asked[0])
}
}
// A Bounded request reads what it adds to a history, and of what an
// earlier request went without the model's reading, backlogCap at most.
// Before, every request read on into the whole history not read yet, on
// every core, for as long as requests came.
func TestBoundedReadsTheBacklogALittleARequest(t *testing.T) {
r := newReader(t, guardChunk)
turns := history(40, 3000) // 120 KB the model hasn't read
// the first request stops waiting after one chunk, as one does at
// its 2 s
ctx, cancel := context.WithCancel(Bounded(context.Background()))
r.during = func(int) { cancel() }
got, err := r.find(ctx, turns)
var p *Partial
if !errors.As(err, &p) {
t.Fatalf("a Bounded request that stopped waiting: %v", err)
}
if len(got) != len(turns) {
t.Fatalf("%d answers for %d texts", len(got), len(turns))
}
first, _ := r.bytes()
if first > guardChunk {
t.Fatalf("the first request read %d bytes after it stopped waiting", first)
}
r.during = nil
// the next turn: the history and a turn more, no deadline at all
turns = append(turns, "Alice asked again")
before, _ := r.bytes()
got, err = r.find(Bounded(context.Background()), turns)
if err != nil {
t.Fatal(err)
}
read, _ := r.bytes()
read -= before
if read > len("Alice asked again")+backlogCap+guardChunk {
t.Fatalf("the second request read %d bytes of a history the first left: the backlog wasn't capped", read)
}
if !strings.Contains(strings.Join(r.asked, "|"), "Alice asked again") || len(got[len(got)-1]) != 1 {
t.Fatalf("what the request added wasn't read: %v", got[len(got)-1])
}
// it is read still, a little a request
for range 3 {
before, _ := r.bytes()
if _, err := r.find(Bounded(context.Background()), turns); err != nil {
t.Fatal(err)
}
n, _ := r.bytes()
if n-before == 0 || n-before > backlogCap+guardChunk {
t.Fatalf("a request read %d bytes of the backlog", n-before)
}
}
// turned on by the user, a request reads all of it
if _, err := r.find(context.Background(), turns); err != nil {
t.Fatal(err)
}
if _, miss := r.known(turns, nil); len(miss) != 0 {
t.Fatalf("%d texts unread after a request that waits for all", len(miss))
}
}
// AstrLink Guard reads in short chunks: a request that stops waiting
// leaves the model finishing the chunk it is on, on the CPU about a
// second of 4 KB where 12 KB took several.
func TestGuardReadsShortChunks(t *testing.T) {
if guardBackend.size() > 4<<10 {
t.Fatalf("AstrLink Guard's chunk is %d bytes", guardBackend.size())
}
if pf.size() != chunkSize {
t.Fatalf("Privacy Filter's chunk is %d bytes", pf.size())
}
}
// A request that waits for every text (masking's strict wait) goes once
// what it adds is read, and what an earlier request already sent unread
// is read after it, to the request's bound. Before, it waited for the
// whole backlog, up to its 15s, on every turn after a resumed history:
// on a 4-core Linux CPU a 60-turn session waited 15s on each of its
// first three requests (gaubeebangeel on X, 11-13).
func TestStrictRequestGoesBeforeTheBacklog(t *testing.T) {
r := newReader(t, guardChunk)
turns := history(40, 3000) // 120 KB the model hasn't read
// the first request runs out of its wait after one chunk, the rest
// of the history sent unread
ctx, cancel := context.WithCancel(context.Background())
r.during = func(int) { cancel() }
if _, err := r.find(ctx, turns); err == nil {
t.Fatal("the first request read the whole history")
}
r.during = nil
// the next: the history and a turn more, a bound far off. Past the
// first chunk (the new turn with it) every read waits for gate.
gate := make(chan struct{})
before, _ := r.bytes()
var reads atomic.Int32
r.during = func(int) {
if reads.Add(1) > 1 {
<-gate
}
}
turns = append(turns, "Alice asked again")
type res struct {
got [][]redact.Found
err error
}
ch := make(chan res, 1)
go func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
got, err := r.find(ctx, turns)
ch <- res{got, err}
}()
var got res
select {
case got = <-ch:
case <-time.After(20 * time.Second):
close(gate)
t.Fatal("the request waited for the history an earlier one sent unread")
}
var p *Partial
if !errors.As(got.err, &p) || !errors.Is(got.err, afterSent) {
t.Fatalf("the request's answer: %v", got.err)
}
if last := got.got[len(got.got)-1]; len(last) != 1 {
t.Fatalf("what the request added wasn't read: %v", last)
}
// the backlog is read after it, to the end
close(gate)
for deadline := time.Now().Add(20 * time.Second); ; {
if _, miss := r.known(turns, nil); len(miss) == 0 {
break
}
if time.Now().After(deadline) {
t.Fatal("the backlog wasn't read after the request")
}
time.Sleep(10 * time.Millisecond)
}
// its read is over once the turn is free
r.turn <- struct{}{}
<-r.turn
// 26 texts of 3 KB the history has apart, less the first request's
if n, _ := r.bytes(); n-before < 70<<10 {
t.Fatalf("read %d bytes after the request", n-before)
}
}