Files
magpie/internal/masking/livecost_test.go
T
yetone 6a074fc132 pii: the default's local model reads what a request adds first, a backlog 4 KB a request, on 4 cores at most
shensmart and xinvxueyuan on Discord (10-28): with the local model on by
default, a long history kept the CPU high; the model off, 5-15%.

What was heavy (measured, TestLiveHistoryCost below): not re-reading.
A text read once is kept by hash (cache, on disk too), and a warm turn
costs what the rules alone do. The heavy case is a history the model
hasn't read (a resumed session, or one from before the model was on),
on the CPU path (Linux, Windows, Docker). Each request's 2 s wait was
spent reading that history oldest first on every core (threads =
GOMAXPROCS-1, windows at once = GOMAXPROCS), the newest turn last, and
the 12 KB chunk in flight was finished past the wait. A 32-core Linux
box spent 33-60 CPU seconds a request, 13 cores busy for as long as
requests came, and the backlog was still unread after 30 requests.
Nothing ran on after the last request (1 ms CPU in the 20 s after).

Now:
- guard.Cores: AstrLink Guard reads on half of GOMAXPROCS, 4 at most.
- backend.missing reads a request's unread texts newest first (the last
  text, a long one's last piece, first).
- AstrLink Guard's chunk is 4 KB (backend.chunk), not 12 KB, so what a
  request leaves the model finishing past its wait is about a second.
- pii.Bounded (masking's default, not a model the user turned on):
  what a request went without reading is remembered by hash
  (backend.sent); a request after it reads what it adds first, and that
  backlog backlogCap (4 KB) at most, nothing found for the rest (the
  rules mask it, as past the 2 s wait). The answer past the wait with
  what was read is 874b86653's (pii.Partial), unchanged.
- A model the user turned on (strict) still reads everything, within
  874b86653's strictWait.
- Not done: OS priority (nice) for the model's threads; the core cap
  is the bound.

Measured, TestLiveHistoryCost (internal/masking, gated on
MAGPIE_GUARD_FILES, no network): a 2,041,658-byte Codex Responses
body (2,032,267 bytes at the rebased tree), 200 turns (repo files as tool output, doc paragraphs as answers,
Chinese names and an email every 10 turns), 30 requests 3 s apart, a
turn more each, model never having read it, then 20 s idle.

WSL2 Ubuntu 24.04 on xiaomi-gpu (32 cores, CPU path):
  before: 33-60 CPU-s a request (44.0, 38.3, 50.4, 42.8, 54.3 ...),
          session 23m16.8s CPU over 1m48s (1293% of a core),
          wall median 2.759s, max 3.07s; 20 s after: 1 ms
  after:  6.3-9.2 CPU-s a request, session 4m1.5s over 1m30s (268%),
          wall median 2.389s, max 2.438s; 20 s after: 1 ms
  warm turn after a full read: 876ms wall / 5.284s CPU before; after
          the rebase the "full read" is strict and stops at 874b86653's
          15 s, so that line is not a warm turn (2.41s / 7.9s); before
          the rebase, at a full read, 1.28s / 3.04s (4 cores, not 31)
  rules alone: 394 / 384 ms CPU
mote (M5 Pro, CoreML):
  before: requests 1-3 2.2 CPU-s each, then ~0.45; session 18.7s CPU
          over 1m30s (21%); warm 396ms / 380ms
  after:  request 1 2.12 CPU-s, then 0.44-0.62; session 17.2s CPU
          (19%); wall median 562ms; warm 476ms / 465ms

On the CPU path a turn adds about 15 KB here, more than 4 cores read
in 2 s, so each request still spends its wait (now on 4 cores, newest
text first) and the backlog isn't reached until turns get shorter;
on CoreML the backlog is gone after the first request.

Tests, each red with its part of the change undone by hand:
- TestNewestTextReadFirst (pii): red on the old code as it stands
  ("read first: a a a ..."); red again with the last piece not first.
- TestBoundedReadsTheBacklogALittleARequest: backlog cap removed, the
  second request read 75169 bytes.
- TestGuardReadsShortChunks: red with guard's chunk unset (12288).
- TestReadsOnFewCores (guard): Cores at 32 and 1 procs.
- TestDefaultModelIsBounded (masking): red with the default's ctx not
  Bounded ("on by default, the model wasn't asked Bounded").
- TestJoinSplitsAtChunkSize now expects the newest-first order.

Rebased onto 874b86653 (strictWait, pii.Partial), which made the
same partial-answer change; mine was dropped for its. Everything below
was run again at the rebased tree.

Run at this tree: gofmt -l clean; go vet darwin, GOOS=linux and
GOOS=windows -tags nogui on internal/pii/..., internal/masking,
internal/redact; go test -count=1 -timeout 45m of those on mote,
wsl-run (magpie-desktop) and win-run (xiaomi-gpu), all ok; internal/gwmw ok;
-race -count=5 of the pii/masking turn, wait, partial and bounded tests
on mote ok; internal/gateway, internal/gui -run
'Mask|Redact|Privacy|PII' ok; TestLiveHistoryCost on mote and WSL. Not run: the full ./... suite.
Not tried: a real Codex session through a live gateway.
2026-10-28 19:53:35 +08:00

168 lines
6.6 KiB
Go

//go:build !windows
package masking
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"syscall"
"testing"
"time"
"github.com/yetone/magpie/internal/pii"
"github.com/yetone/magpie/internal/redact"
)
// TestLiveHistoryCost measures what the default's local model (AstrLink
// Guard) costs on a long Codex history: CPU seconds and wall time per
// request, background work included. It needs the model's files, in the
// folder MAGPIE_GUARD_FILES names (model_int8.onnx, model_int8.onnx_data,
// tokenizer.json), and is skipped without; nothing is downloaded.
func TestLiveHistoryCost(t *testing.T) {
src := os.Getenv("MAGPIE_GUARD_FILES")
if src == "" {
t.Skip("MAGPIE_GUARD_FILES names no folder with AstrLink Guard's files")
}
// testenv's sandbox: the model's folder there, never the user's
if os.Getenv("XDG_CACHE_HOME") == "" {
t.Fatal("no sandbox XDG_CACHE_HOME")
}
dst := filepath.Join(os.Getenv("XDG_CACHE_HOME"), "magpie", "astrlink-guard", "7216b6a3b897")
if err := os.MkdirAll(dst, 0o755); err != nil {
t.Fatal(err)
}
for _, f := range []string{"model_int8.onnx", "model_int8.onnx_data", "tokenizer.json"} {
if err := os.Symlink(filepath.Join(src, f), filepath.Join(dst, f)); err != nil {
t.Fatal(err)
}
}
m := pii.Of(pii.AstrLinkGuard)
if !m.Installed() {
t.Fatal("the model's files aren't where pii looks")
}
// load it (and compile CoreML on a Mac) before measuring
if _, err := m.Find(context.Background(), []string{"warm the model up"}); err != nil {
t.Fatal(err)
}
turns := liveTurns(t, 230)
mask := func(n int) (wall, cpu time.Duration) {
body := codexBody(turns[:n])
o := redact.Options{Secrets: true, Personal: true, MinScore: m.MinScore()}
o.Model = model(context.Background(), m, o.MinScore, false)
c0, w0 := cpuNow(), time.Now()
redact.MaskJSON(body, o)
return time.Since(w0), cpuNow() - c0
}
// the rules alone, for the same bodies
rules := func(n int) time.Duration {
body := codexBody(turns[:n])
c0 := cpuNow()
redact.MaskJSON(body, redact.Options{Secrets: true, Personal: true})
return cpuNow() - c0
}
fmt.Printf("history of 200 turns: %d bytes; rules alone %v CPU\n", len(codexBody(turns[:200])), rules(200).Round(time.Millisecond))
// A Codex session resumed on a 200-turn history the model hasn't
// read: 30 requests, a turn more each, 3s apart (the vendor's
// answer), then what runs on after the last.
var wallAll []time.Duration
c0, w0 := cpuNow(), time.Now()
for i := 0; i < 30; i++ {
start := time.Now()
wall, cpu := mask(200 + i)
wallAll = append(wallAll, wall)
fmt.Printf("request %2d: wall %6v cpu %6v\n", i+1, wall.Round(time.Millisecond), cpu.Round(time.Millisecond))
time.Sleep(3*time.Second - time.Since(start)%(3*time.Second))
}
session := cpuNow() - c0
// what runs on once no request comes
c1 := cpuNow()
time.Sleep(20 * time.Second)
after := cpuNow() - c1
sort.Slice(wallAll, func(i, j int) bool { return wallAll[i] < wallAll[j] })
fmt.Printf("session: %v wall, %v CPU (%.0f%% of a core); 20s after it: %v CPU; request wall median %v max %v\n",
time.Since(w0).Round(time.Second)-20*time.Second, session.Round(time.Millisecond),
100*session.Seconds()/(time.Since(w0)-20*time.Second).Seconds(), after.Round(time.Millisecond),
wallAll[len(wallAll)/2].Round(time.Millisecond), wallAll[len(wallAll)-1].Round(time.Millisecond))
// warm: the whole history read (as far as it will be), a turn more
full := redact.Options{Secrets: true, Personal: true, MinScore: m.MinScore()}
full.Model = model(context.Background(), m, full.MinScore, true)
redact.MaskJSON(codexBody(turns[:229]), full)
wall, cpu := mask(230)
fmt.Printf("warm, one turn more: wall %v cpu %v\n", wall.Round(time.Millisecond), cpu.Round(time.Millisecond))
}
func cpuNow() time.Duration {
var ru syscall.Rusage
syscall.Getrusage(syscall.RUSAGE_SELF, &ru)
return time.Duration(ru.Utime.Nano() + ru.Stime.Nano())
}
type liveTurn struct {
user, cmd, output, answer string
}
// liveTurns are n turns of an agent at work on this repo: its own files
// as tool output, its docs as the answers, a person or two named.
func liveTurns(t *testing.T, n int) []liveTurn {
root := "../.."
var files []string
filepath.Walk(filepath.Join(root, "internal"), func(p string, fi os.FileInfo, err error) error {
if err == nil && !fi.IsDir() && strings.HasSuffix(p, ".go") && fi.Size() > 2000 && fi.Size() < 40000 {
files = append(files, p)
}
return nil
})
sort.Strings(files)
doc, err := os.ReadFile(filepath.Join(root, "docs", "subsystems", "gateway-routing.md"))
if err != nil {
t.Fatal(err)
}
paras := strings.Split(string(doc), "\n\n")
people := []string{"张伟", "Alice Martin", "陈建国", "Bob Chen"}
var out []liveTurn
for i := 0; i < n; i++ {
f := files[(i*7)%len(files)]
b, _ := os.ReadFile(f)
if len(b) > 12000 {
b = b[:12000]
}
rel, _ := filepath.Rel(root, f)
tu := liveTurn{cmd: fmt.Sprintf(`{"command":["bash","-lc","sed -n 1,400p %s"],"workdir":"/home/dev/magpie"}`, rel),
output: string(b), answer: paras[i%len(paras)]}
if len(tu.answer) > 3000 {
tu.answer = tu.answer[:3000]
}
if i%10 == 0 {
tu.user = fmt.Sprintf("请看一下 %s,%s 说那里有个问题,邮件发给 %s 了。", rel, people[i%len(people)], "bob.chen@example.org")
}
out = append(out, tu)
}
return out
}
// codexBody is a Responses request with turns as its history.
func codexBody(turns []liveTurn) []byte {
var in []any
in = append(in, map[string]any{"type": "message", "role": "user", "content": []any{map[string]any{"type": "input_text", "text": "<environment_context>\n <cwd>/home/dev/magpie</cwd>\n <shell>bash</shell>\n</environment_context>"}}})
for i, tu := range turns {
if tu.user != "" {
in = append(in, map[string]any{"type": "message", "role": "user", "content": []any{map[string]any{"type": "input_text", "text": tu.user}}})
}
id := fmt.Sprintf("call_%024d", i)
in = append(in,
map[string]any{"type": "reasoning", "summary": []any{}, "encrypted_content": strings.Repeat("gAAAAB", 200)},
map[string]any{"type": "function_call", "name": "shell", "arguments": tu.cmd, "call_id": id},
map[string]any{"type": "function_call_output", "call_id": id, "output": tu.output},
map[string]any{"type": "message", "role": "assistant", "content": []any{map[string]any{"type": "output_text", "text": tu.answer}}})
}
b, _ := json.Marshal(map[string]any{"model": "gpt-5-codex", "instructions": strings.Repeat("You are Codex, a coding agent. Follow the user's instructions. ", 200),
"input": in, "tools": []any{map[string]any{"type": "function", "name": "shell", "description": "Runs a shell command", "parameters": map[string]any{"type": "object"}}},
"stream": true})
return b
}