yyhhyyyyyy (Discord, 10-28) saw an agent's parallel requests queue behind
Guard's one turn and asked for a concurrency setting (16). The turn now has
guard.Cores() places (half GOMAXPROCS, 4 at most). The cores are shared: a
process-wide pool of Cores() tokens, each window takes one before it reads
and more only while they are free (guard.take), so requests together never
use more cores than one did (the 4-core cap of 10-28, xinvxueyuan and
shensmart). The model loads once (guardLoading), the idle unload waits for
every place (guardIdle), and a text one request is reading another waits
for instead of reading it too (backend.reading). On CoreML windows are read
one at a time; only windows under coremlMin or after a failure take CPU
tokens.
No setting: measured, the bound is cores, not the request count.
Bench (zz bench, not committed): requests/s at 1/2/4/8 concurrent.
- Linux (WSL magpie-desktop, Ryzen AI Max+ 395, Cores()=4):
512 B shared 4.2/6.1/8.1/8.4 on 1.9-3.9 cores, rss ~150 MB
one turn (before) 4.2 whatever came
unshared 4.4/8.8/14.1/20.4, on 12 cores at 8
4 KB shared 0.6/0.5/0.6/0.7 (one 4 KB already reads 3 windows on 3.6 cores)
unshared 0.6/1.1/1.7/2.2, 20 cores and 521 MB at 8
- Windows (xiaomi-gpu, same box, busy with other runs):
512 B shared 7.5/9.2/11.3/12.1 on <=3.8 cores; unshared 5.1/9.6/15.9/24.1 on 13 cores
4 KB shared 0.7/0.8/0.8/0.8; unshared 0.9/1.2/1.8/2.4, 18 cores, 519 MB
- mote (M5 Pro, load 25-45): CoreML 4 KB 23.0/19.6/25.1/24.9, a model a
worker 15.7/25.6/25.5/23.4, rss ~190 MB flat: the GPU is the bound.
CPU unshared 4 KB at 8: 953 MB.
Tests: turns_test.go (TestGuardReadsRequestsAtOnce, TestGuardIdleWaitsForEveryTurn,
TestOneTextReadOnceAtOnce), guard/cores_test.go (TestTakeHoldsOnlyWhatIsFree,
TestFindsAtOnceReadAsOneAtATime: concurrent finds equal serial ones).
Broken by hand, each red: one turn ("1 turns, 4 cores"), no load lock
("4 loads"), idle taking one place ("let go while a request read it"), no
in-flight check (TestOneTextReadOnceAtOnce), take waiting for more (10s
timeout fail).
Run:
- wsl-run: go test ./internal/pii/... ok; -race -count=3 ./internal/pii ok;
-race guard TakeHolds/FindsAtOnce ok.
- mote-run: gofmt -l internal clean; GOOS=darwin/linux/windows go vet
./internal/pii/... ./internal/masking/ ok; MAGPIE_GUARD_DIR=real model
go test ./internal/pii/... ./internal/masking/ ok (CoreML).
- win-run: go test ./internal/pii/... ./internal/masking/ with the real model ok.
- mote-run full go test -timeout 45m ./... of this and the commit before
(at c3462d5fe): red only in TestClaudeAccountsSpentFallBackToCodex
(#2858, #2859), TestGatewayHookWaitsForStartingHost (#2871), both ok
alone after, and AppKit window tests in internal/gui (#2645, #4125), red
the same alone at origin/main e15b2277c on mote.
Not tried: a real agent's parallel requests through the gateway.
175 lines
4.7 KiB
Go
175 lines
4.7 KiB
Go
package pii
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/pii/guard"
|
|
"github.com/yetone/magpie/internal/redact"
|
|
)
|
|
|
|
// gateFinder is a model whose reads wait at a gate, counting how many
|
|
// are under way at once.
|
|
type gateFinder struct {
|
|
gate chan struct{}
|
|
now, most *atomic.Int32
|
|
}
|
|
|
|
func (f gateFinder) Find(text string) []guard.Span {
|
|
n := f.now.Add(1)
|
|
for m := f.most.Load(); n > m && !f.most.CompareAndSwap(m, n); m = f.most.Load() {
|
|
}
|
|
<-f.gate
|
|
f.now.Add(-1)
|
|
return nil
|
|
}
|
|
|
|
// yyhhyyyyyy on Discord: AstrLink Guard read one request at a time, so
|
|
// agents' requests at once queued behind each other. It reads
|
|
// guard.Cores() of them at once now, and they load the model once.
|
|
func TestGuardReadsRequestsAtOnce(t *testing.T) {
|
|
k := cap(guardTurn)
|
|
if k != guard.Cores() {
|
|
t.Fatalf("%d turns, %d cores", k, guard.Cores())
|
|
}
|
|
if k < 2 {
|
|
t.Skipf("one core to read on here (GOMAXPROCS %d)", guard.Cores())
|
|
}
|
|
guardSandbox(t)
|
|
fakeGuardFiles(t)
|
|
var loads, now, most atomic.Int32
|
|
loading, gate := make(chan struct{}), make(chan struct{})
|
|
guardLoad = func(string) (spanFinder, error) {
|
|
loads.Add(1)
|
|
<-loading
|
|
return gateFinder{gate, &now, &most}, nil
|
|
}
|
|
var wg sync.WaitGroup
|
|
errs := make(chan error, k)
|
|
for i := range k {
|
|
wg.Go(func() {
|
|
_, err := Of(AstrLinkGuard).Find(context.Background(), []string{fmt.Sprintf("request %d", i)})
|
|
errs <- err
|
|
})
|
|
}
|
|
waitFor(t, "every request its turn", func() bool { return len(guardTurn) == k })
|
|
// the other requests wait for the first's load: watch a while for
|
|
// one loading beside it
|
|
for range 40 {
|
|
if n := loads.Load(); n != 1 {
|
|
t.Fatalf("%d loads for requests at once", n)
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
close(loading)
|
|
waitFor(t, "every request read at once", func() bool { return now.Load() == int32(k) })
|
|
close(gate)
|
|
wg.Wait()
|
|
close(errs)
|
|
for err := range errs {
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if loads.Load() != 1 || most.Load() != int32(k) {
|
|
t.Fatalf("%d loads, %d read at most at once", loads.Load(), most.Load())
|
|
}
|
|
}
|
|
|
|
// The idle timer lets the model go only with no request on it: with one
|
|
// turn of several held, it waits for the next timer.
|
|
func TestGuardIdleWaitsForEveryTurn(t *testing.T) {
|
|
guardSandbox(t)
|
|
fakeGuardFiles(t)
|
|
var reads atomic.Int32
|
|
guardLoad = func(string) (spanFinder, error) { return fakeFinder{nil, &reads}, nil }
|
|
if _, err := Of(AstrLinkGuard).Find(context.Background(), []string{"one"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
guardTurn <- struct{}{} // a request on it
|
|
guardIdle()
|
|
running := Of(AstrLinkGuard).Now().Running
|
|
<-guardTurn
|
|
if !running {
|
|
t.Fatal("let go while a request read it")
|
|
}
|
|
if len(guardTurn) != 0 {
|
|
t.Fatalf("%d turns left held", len(guardTurn))
|
|
}
|
|
guardIdle()
|
|
if Of(AstrLinkGuard).Now().Running {
|
|
t.Fatal("kept with no request")
|
|
}
|
|
}
|
|
|
|
// Requests read at once don't read one text beside each other: the
|
|
// second waits for the first's read and takes its answer (subagents
|
|
// started at once send one system prompt each).
|
|
func TestOneTextReadOnceAtOnce(t *testing.T) {
|
|
dir := t.TempDir()
|
|
var calls atomic.Int32
|
|
first, release := make(chan struct{}), make(chan struct{})
|
|
b := &backend{turn: make(chan struct{}, 2), waiting: new(atomic.Int32), cache: newLRU(100), disk: new(diskState),
|
|
path: func() string { return filepath.Join(dir, "found.bin") },
|
|
heldBack: func() error { return nil },
|
|
chunk: guardChunk,
|
|
classify: func(texts []string) ([][]redact.Found, error) {
|
|
if calls.Add(1) == 1 {
|
|
close(first)
|
|
<-release
|
|
}
|
|
out := make([][]redact.Found, len(texts))
|
|
for i, s := range texts {
|
|
if j := strings.Index(s, "13800138000"); j >= 0 {
|
|
out[i] = []redact.Found{{Start: j, End: j + 11, Label: "private_phone", Score: 0.9}}
|
|
}
|
|
}
|
|
return out, nil
|
|
}}
|
|
t.Cleanup(func() {
|
|
b.disk.mu.Lock()
|
|
if b.disk.timer != nil && b.disk.timer.Stop() {
|
|
b.disk.saves.Done()
|
|
}
|
|
b.disk.timer = nil
|
|
b.disk.mu.Unlock()
|
|
b.disk.saves.Wait()
|
|
})
|
|
texts := []string{"the system prompt, call 13800138000"}
|
|
got := make(chan [][]redact.Found, 2)
|
|
find := func() {
|
|
out, err := b.find(context.Background(), texts)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
got <- out
|
|
}
|
|
go find()
|
|
<-first
|
|
go find()
|
|
waitFor(t, "the second request its turn", func() bool { return len(b.turn) == 2 })
|
|
// it waits for the first's read: watch a while for a read beside it
|
|
for range 40 {
|
|
if n := calls.Load(); n != 1 {
|
|
t.Fatalf("%d reads of one text", n)
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
close(release)
|
|
for range 2 {
|
|
out := <-got
|
|
if len(out) != 1 || len(out[0]) != 1 || out[0][0].Label != "private_phone" {
|
|
t.Fatalf("found %v", out)
|
|
}
|
|
}
|
|
if n := calls.Load(); n != 1 {
|
|
t.Fatalf("%d reads of one text", n)
|
|
}
|
|
}
|