mrwl666 (Discord #help, v0.1.2554, Docker on a 2-core EPYC) saw one
core at 100% for 2-3 s on every request with masking and AstrLink Guard
on, and took it for every message of a ~66k-token conversation being
scanned again. It isn't. Probed on the Linux VM (GOMAXPROCS=2, taskset
-c 0,1) with a 50-60 turn Chat session of ~4 KB tool results, ~240-270 KB
a body:
- rules masking (MaskJSON without the model): 5-11 ms a turn;
- the history is cached by hash (LRU + found-<rev>.bin), and a turn
hands the model only what it added. Bounded caps that at 4 KB;
- the request waits masking's 2 s defaultWait for the model on that
text, and guard.Cores() is 1 on a 2-core box, so one core runs the
int8 BERT at about 1 KB a second. That is the spike.
This change makes the model's CPU cheaper; it does not take the 2 s
wait away. AVX2 kernels for the int8 dot products (dot4, attention
scores), exp in softmax and the GELU epilogue, picked at start when
the CPU has AVX2, plus plain compares in place of the builtin float
min/max (a third of the non-matmul time, for their NaN/±0 rules).
Every float op is the same per lane and in the same order as the Go
code (release builds are GOAMD64=v1, no FMA), the int8 sums are exact,
mix32 and the softmax sum keep their order, so the model finds the
same spans with the same scores to the bit.
Numbers, Linux VM (AMD Ryzen AI MAX+ 395), one core of guard:
guard.Find on 4,247 bytes: 4.05 s before, 2.75 s after (same 20 spans)
turn 50-57 with the model: MaskJSON wall 2.0 s both (the wait), CPU
after the wait 0.1-3.0 s before, 0.0-2.0 s after
mote (M5 Pro, arm64, rules path, unchanged by this):
BenchmarkMaskLongSession chat 10.3-11.7 ms, responses 10.8-13.0 ms,
messages 12.1-13.9 ms per turn of a ~260 KB body
Tests:
- TestKernelsSameBits: the AVX2 exp/GELU/scores give Float32bits equal
to the Go code, edge values included. Broken by hand: swapping two
adds in the asm exp made it red (0x175156d6 vs 0x175156d5).
- TestMaxAndRangeAsBuiltins: maxOf/rangeOf agree with the builtins on
±0 and NaN. Red with maxOf's NaN check taken out.
- TestModelSameWithoutAVX2 (needs MAGPIE_GUARD_DIR): the real model
finds the same spans on ~6 KB of mixed text with AVX2 and with SSE2.
- TestSessionTurnReadsOnlyWhatItAdds (internal/pii): turn 61 of a
275 KB session, after turn 60, has the model read only turn 61's text
(5.2 KB for 4.6 KB added). It passes on the old code too: it guards
the claim above, not this speedup. Red (10.5 KB, earlier turns read
again) with the cache lookup in known() turned off by hand.
- BenchmarkMaskLongSession (internal/redact): Chat, Responses and
Messages sessions with large tool results, turn 61 after turn 60.
Run at this tree (7b6b7492a + this):
- mote: go vet ./internal/pii/... ./internal/redact, GOOS=linux amd64
(-tags nogui), GOOS=windows amd64, GOOS=linux arm64 guard: ok.
go test -count=1 -timeout 45m ./internal/pii/... ./internal/redact: ok.
- WSL magpie-desktop (amd64, AVX2) with MAGPIE_GUARD_DIR: the same
packages ok (guard 28.9 s, model tests included); the three kernel
tests again under GOMAXPROCS=2 taskset: ok.
- Windows xiaomi-gpu: the same packages ok (no model dir there).
Not tried: the reporter's EPYC itself or Docker; AVX-VNNI or AVX-512
(would need the EPYC generation; VPDPWSSD is exact, so it could come
next); mix32 on AVX2 (more accumulators change its sum order, so not
byte-identical); the full ./... suite. For no spike at all, the model
can be turned off with rules masking kept on.
119 lines
6.4 KiB
Go
119 lines
6.4 KiB
Go
package redact
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// A long agent session as it reaches the gateway: every turn sends the
|
|
// whole conversation again, a few KB of tool output a turn, with what the
|
|
// rules look for here and there in it (paths with a user's name, an
|
|
// email, an address, a key in a config file, a UUID, a log's IPs).
|
|
|
|
// toolOutput is turn i's tool output, about kb KB of it.
|
|
func toolOutput(i, kb int) string {
|
|
var b strings.Builder
|
|
for n := 0; b.Len() < kb*1024; n++ {
|
|
switch (i + n) % 6 {
|
|
case 0:
|
|
fmt.Fprintf(&b, "%d:func (s *Server) handle%d(w http.ResponseWriter, r *http.Request) {\n\tctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)\n\tdefer cancel()\n\tif err := s.store.Put(ctx, r.URL.Query().Get(\"id\"), body); err != nil {\n\t\thttp.Error(w, err.Error(), 500)\n\t}\n}\n", n, i*100+n)
|
|
case 1:
|
|
fmt.Fprintf(&b, "/home/mrwl/projects/api/internal/store/store_%d.go:%d:\terr := db.QueryRowContext(ctx, \"SELECT id, name, email FROM users WHERE id = $1\", id).Scan(&u.ID, &u.Name, &u.Email)\n", n, 40+n)
|
|
case 2:
|
|
fmt.Fprintf(&b, "2026-10-31T12:%02d:%02d.%03dZ INFO request id=%08x-1f2e-4d3c-9b8a-%012x method=POST path=/v1/orders/%d status=200 dur=%dms remote=10.0.%d.%d\n", n%60, i%60, n%1000, i*7919+n, n*104729, n, 3+n%90, n%255, i%255)
|
|
case 3:
|
|
fmt.Fprintf(&b, " --- PASS: TestOrderFlow/case_%d (0.%02ds)\n order_test.go:%d: created order %d for customer %d, total 129.%02d EUR\n", n, n%100, 100+n, 5000+n, 300+i, n%100)
|
|
case 4:
|
|
fmt.Fprintf(&b, " config.yaml:%d database_url: postgres://app:%s@db.internal:5432/orders # turn %d\n", n, "S3cr3tPassw0rd2026", i)
|
|
case 5:
|
|
fmt.Fprintf(&b, "Author: Wei Zhang <wei.zhang@example.com> commit %040x\n fix: retry the payment webhook when the upstream answers 502 (#%d)\n", i*31337+n, 1000+n)
|
|
}
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// chatSession is a Chat Completions request on turn n, as an agent on a
|
|
// gateway sends gpt-6-luna one: a system prompt, then each turn a user
|
|
// message, an assistant tool call and its tool output.
|
|
func chatSession(n, kb int) []byte {
|
|
msgs := []any{map[string]any{"role": "system", "content": "You are a coding agent working in /home/mrwl/projects/api. " + strings.Repeat("Follow the repository's conventions, run the tests, keep changes small. ", 60)}}
|
|
for i := 0; i < n; i++ {
|
|
msgs = append(msgs,
|
|
map[string]any{"role": "user", "content": fmt.Sprintf("step %d: the order webhook still fails on retries, look at the logs and fix it", i)},
|
|
map[string]any{"role": "assistant", "content": "", "tool_calls": []any{map[string]any{"id": fmt.Sprintf("call_%d", i), "type": "function", "function": map[string]any{"name": "shell", "arguments": fmt.Sprintf(`{"command":"grep -rn webhook /home/mrwl/projects/api | head -%d"}`, 50+i)}}}},
|
|
map[string]any{"role": "tool", "tool_call_id": fmt.Sprintf("call_%d", i), "content": toolOutput(i, kb)},
|
|
)
|
|
}
|
|
b, _ := json.Marshal(map[string]any{"model": "gpt-6-luna", "stream": true, "messages": msgs,
|
|
"tools": []any{map[string]any{"type": "function", "function": map[string]any{"name": "shell", "description": "Run a shell command", "parameters": map[string]any{"type": "object", "properties": map[string]any{"command": map[string]any{"type": "string"}}}}}}})
|
|
return b
|
|
}
|
|
|
|
// claudeSession is a Messages request on turn n, as Claude Code sends one:
|
|
// tool_use and tool_result blocks, thinking signed by the vendor.
|
|
func claudeSession(n, kb int) []byte {
|
|
msgs := []any{}
|
|
for i := 0; i < n; i++ {
|
|
msgs = append(msgs,
|
|
map[string]any{"role": "user", "content": []any{map[string]any{"type": "text", "text": fmt.Sprintf("step %d: fix the failing webhook retry", i)}}},
|
|
map[string]any{"role": "assistant", "content": []any{
|
|
map[string]any{"type": "thinking", "thinking": fmt.Sprintf("I should grep for the webhook handler, turn %d.", i), "signature": strings.Repeat("EqQBCkYIBxgCKkA", 20)},
|
|
map[string]any{"type": "tool_use", "id": fmt.Sprintf("toolu_%024d", i), "name": "Bash", "input": map[string]any{"command": fmt.Sprintf("grep -rn webhook /home/mrwl/projects/api | head -%d", 50+i)}},
|
|
}},
|
|
map[string]any{"role": "user", "content": []any{map[string]any{"type": "tool_result", "tool_use_id": fmt.Sprintf("toolu_%024d", i), "content": toolOutput(i, kb)}}},
|
|
)
|
|
}
|
|
b, _ := json.Marshal(map[string]any{"model": "claude-opus-5-5", "stream": true, "system": []any{map[string]any{"type": "text", "text": strings.Repeat("You are Claude Code. Use the tools to work in the user's repository. ", 60)}}, "messages": msgs})
|
|
return b
|
|
}
|
|
|
|
// sessions are the three protocols' bodies on turn n, ~66k tokens (about
|
|
// 260 KB) at n = 60 and 4 KB of tool output a turn.
|
|
var sessions = []struct {
|
|
name string
|
|
body func(n, kb int) []byte
|
|
}{
|
|
{"chat", chatSession},
|
|
{"responses", func(n, kb int) []byte { return codexSession(n, kb) }},
|
|
{"messages", claudeSession},
|
|
}
|
|
|
|
// codexSession is a Responses request on turn n, as Codex sends one.
|
|
func codexSession(n, kb int) []byte {
|
|
var items []any
|
|
for i := 0; i < n; i++ {
|
|
items = append(items,
|
|
map[string]any{"type": "message", "role": "user", "content": []any{map[string]any{"type": "input_text", "text": fmt.Sprintf("step %d: fix the failing webhook retry", i)}}},
|
|
map[string]any{"type": "reasoning", "summary": []any{}, "encrypted_content": strings.Repeat("gAAAAABo", 40)},
|
|
map[string]any{"type": "function_call", "name": "shell", "arguments": fmt.Sprintf(`{"command":["bash","-lc","grep -rn webhook /home/mrwl/projects/api | head -%d"]}`, 50+i), "call_id": fmt.Sprintf("call_%d", i)},
|
|
map[string]any{"type": "function_call_output", "call_id": fmt.Sprintf("call_%d", i), "output": toolOutput(i, kb)},
|
|
)
|
|
}
|
|
b, _ := json.Marshal(map[string]any{"model": "gpt-6-luna", "instructions": strings.Repeat("You are Codex, a coding agent. ", 100), "input": items, "stream": true, "prompt_cache_key": "0199a0b2-5c3d-7e8f-9a0b-1c2d3e4f5a6b"})
|
|
return b
|
|
}
|
|
|
|
// BenchmarkMaskLongSession is masking one turn of a ~66k-token session
|
|
// whose turns before it were masked already, as each request of a long
|
|
// conversation is: the history is the same, one turn is new.
|
|
func BenchmarkMaskLongSession(b *testing.B) {
|
|
for _, s := range sessions {
|
|
b.Run(s.name, func(b *testing.B) {
|
|
Forget()
|
|
resetRuleMemo()
|
|
o := Options{Secrets: true, Personal: true, Route: "openai"}
|
|
const turns, kb = 60, 4
|
|
MaskJSON(s.body(turns, kb), o)
|
|
body := s.body(turns+1, kb)
|
|
b.SetBytes(int64(len(body)))
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for b.Loop() {
|
|
MaskJSON(body, o)
|
|
}
|
|
})
|
|
}
|
|
}
|