fremontdango on Discord asked, after the request that sat as "routing…" while it was masked, how long masking took. Now each request keeps it. noteRedacting adds up the time from the first masking at once to the last's end (redaction.spent); maskingMs gives it in ms, at least 1 when there was any, never more than Own, magpie's own time before the request went out (splitSent), since masking happens inside it. It goes on the route (Route.Masking, /api/gateway/trace) and in the usage record (Record.Masking, masking_ms; packedRow's int32 fits in its padding, so TestRowsOfOneSessionShareAShape's 216 bytes are unchanged, and the process-private snapshot carries it). It is a duration, no text. The Routing page shows it as magpie's "Masking" part, first in where the first try's time went (timeParts, renderSteps); the Usage page's detail splits it out of "In magpie" (page.js). Both use the core "Masking" string every language already has (脱敏, 脫敏, マスク, Maskierung). Why masking can be slow, measured on this machine (go1.27, M-series): the rules are linear, ~0.25 ms/KB. redact.MaskJSON over a Claude Code shaped body: 283 KB 35 ms (secrets) / 63 ms (+ personal data); 511 KB 70 / 122 ms; a dense 402 KB with 7,821 hits 173 ms with personal data, 144 ms with the user's words too. Through the gateway (redactedHits, secrets + personal, 402 KB dense) 145 ms the first time, 123 ms again; noteMasked ~2.6 ms. Doubling the body doubles the time; nothing quadratic or repeated per hit. So a request held "routing…" for 15 s or more (unroutedShow) while masked is the local model (Settings → Privacy → Find with a local model, internal/pii): it reads each text it hasn't seen (a new turn's tool results are the misses), one model turn at a time, so requests queue behind each other (#3682: 480 KB took 90 s on an RX 470/580). No change to masking's speed here; the phase and the time now say which it was. Run at this head, on origin/main9ae791221(Go, M-series macOS): - go test -timeout 45m ./internal/gateway ./internal/gui ./internal/redact ./internal/usage: ok (gateway 425s, gui 99s); ./internal/masking and ./internal/pii ok at the pre-rebase head 3a4a13d04 - gofmt -l internal: clean; go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean; go build: ok - go test -race -count=3 TestMaskingIsAPhaseWithItsTime, TestNoMaskingNoMaskingTime: ok - GUI, chromium and webkit: routing-masking (every language) 10/10, usage-ledger-detail 6/6, i18n-keys and routing-levels green. The whole routing-*, panel-routing*, usage-ledger* and i18n-keys set: 1,824 tests, 96 red at load ~47, in 15 files. The first run's i18n-keys red was this change's (a Go comment quoting the core word), fixed in the commit before. The other 14 files, rerun two at a time at this head and at origin/main9ae791221: the same files red on both (89 here, 86 there; routing-rule-remove alone 5 red here, 7 on main). Each has an issue: #2115, #3410, #3411, #1905, #3593, #3408, #3096, #3094, #3001, #3104, #3786. Broken by hand, each red: spent not added (route and record Masking 0), no t.Masking on the route, no Masking in the usage record, the snapshot's put/take of Masking (TestRowsOfOneSessionShareAShape), timeParts without Masking or renderSteps without the first try's masking (routing-masking), the Usage page without the Masking line (usage-ledger-detail, 3 red). Not tried: a real local model (Settings → Privacy → Find with a local model) on the reporter's Windows box or any GPU, so a long masking was only held by a fake masker; the Codex backend's usage record has no Masking yet; the Windows test box wasn't used. Reported by fremontdango on Discord.
166 lines
5.0 KiB
Go
166 lines
5.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/gwmw"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// heldMasker masks nothing, and holds each request it is asked about
|
|
// until let is closed: masking that takes as long as the test says.
|
|
// entered is closed when the first one reaches it.
|
|
type heldMasker struct {
|
|
let, entered chan struct{}
|
|
once *sync.Once
|
|
}
|
|
|
|
func (m heldMasker) hold(ctx context.Context) {
|
|
m.once.Do(func() { close(m.entered) })
|
|
select {
|
|
case <-m.let:
|
|
case <-ctx.Done():
|
|
}
|
|
}
|
|
|
|
func (m heldMasker) JSONHitsContext(ctx context.Context, b []byte) ([]byte, []gwmw.Hit) {
|
|
m.hold(ctx)
|
|
return b, nil
|
|
}
|
|
|
|
func (m heldMasker) TextHitsContext(ctx context.Context, s string) (string, []gwmw.Hit) {
|
|
m.hold(ctx)
|
|
return s, nil
|
|
}
|
|
|
|
func (m heldMasker) JSONHits(b []byte) ([]byte, []gwmw.Hit) {
|
|
return m.JSONHitsContext(context.Background(), b)
|
|
}
|
|
|
|
func (m heldMasker) TextHits(s string) (string, []gwmw.Hit) {
|
|
return m.TextHitsContext(context.Background(), s)
|
|
}
|
|
func (m heldMasker) JSON(b []byte) ([]byte, int) { b, _ = m.JSONHits(b); return b, 0 }
|
|
func (m heldMasker) Text(s string) (string, int) { s, _ = m.TextHits(s); return s, 0 }
|
|
func (heldMasker) Restore(s string, _ bool) string { return s }
|
|
func (heldMasker) Value(string) (string, bool) { return "", false }
|
|
func (heldMasker) Writer(w http.ResponseWriter) (http.ResponseWriter, func()) {
|
|
return w, func() {}
|
|
}
|
|
|
|
// fremontdango on Discord: a Claude Code request sat on the Routing page
|
|
// as routing for a long while that it was being masked. While a
|
|
// request is masked its phase is "redact", on /v1/magpie/concurrency and
|
|
// on the route that shows it, and what masking took is kept, on its route
|
|
// and in its usage record, as a part of magpie's own time.
|
|
func TestMaskingIsAPhaseWithItsTime(t *testing.T) {
|
|
unroutedLimits(t, 10*time.Millisecond, time.Minute)
|
|
fresh(t)
|
|
if err := settings.Save(settings.Settings{Redact: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
slowOn(t, "a", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
io.ReadAll(r.Body)
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, chatStreamOK)
|
|
}))
|
|
withoutMasker(t)
|
|
m := heldMasker{let: make(chan struct{}), entered: make(chan struct{}), once: new(sync.Once)}
|
|
var letOnce sync.Once
|
|
let := func() { letOnce.Do(func() { close(m.let) }) }
|
|
t.Cleanup(gwmw.Mask(m))
|
|
|
|
s := New()
|
|
srv := httptest.NewServer(s.Handler())
|
|
t.Cleanup(srv.Close)
|
|
t.Cleanup(let) // before srv.Close, which waits for the request
|
|
got := posted(srv, strings.NewReader(`{"model":"a/m","stream":true,"messages":[{"role":"user","content":"hi"}]}`))
|
|
|
|
select {
|
|
case <-m.entered:
|
|
case r := <-got:
|
|
t.Fatalf("answered %d %q %v before it was masked", r.code, r.body, r.err)
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("the request never reached the masker")
|
|
}
|
|
entered := time.Now()
|
|
// its phase, and the route that shows it as held, say masking
|
|
deadline := time.Now().Add(10 * time.Second)
|
|
for {
|
|
phase, shown := "", ""
|
|
for _, a := range s.Active() {
|
|
phase = a.Phase
|
|
}
|
|
for _, r := range s.Trace(context.Background(), 0, 0).Routes {
|
|
if !r.Done && r.Provider == "magpie" {
|
|
shown = r.Phase
|
|
}
|
|
}
|
|
if phase == phaseRedact && shown == phaseRedact {
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("while masked: phase %q, its route's %q", phase, shown)
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
held := time.Since(entered)
|
|
let()
|
|
var r unroutedReply
|
|
select {
|
|
case r = <-got:
|
|
case <-time.After(15 * time.Second):
|
|
t.Fatal("never answered once masked")
|
|
}
|
|
if r.err != nil || r.code != 200 || !strings.Contains(r.body, "from b") {
|
|
t.Fatalf("answered %d %q %v", r.code, r.body, r.err)
|
|
}
|
|
|
|
rs := routesOf(s, "a/m")
|
|
if len(rs) != 1 || !rs[0].Done || rs[0].Provider != "a" {
|
|
t.Fatalf("routes: %+v", rs)
|
|
}
|
|
rt := rs[0]
|
|
if rt.Phase != "" {
|
|
t.Fatalf("a done route still says %q", rt.Phase)
|
|
}
|
|
if rt.Masking < held.Milliseconds() || rt.Masking > rt.Own || rt.Own == 0 {
|
|
t.Fatalf("route: masking %d ms of own %d, held at least %s", rt.Masking, rt.Own, held)
|
|
}
|
|
recs := usage.Load(time.Time{})
|
|
if len(recs) == 0 {
|
|
t.Fatal("no usage record")
|
|
}
|
|
rec := recs[len(recs)-1]
|
|
if rec.Masking != rt.Masking || rec.Own != rt.Own {
|
|
t.Fatalf("record: masking %d own %d, route %d %d", rec.Masking, rec.Own, rt.Masking, rt.Own)
|
|
}
|
|
}
|
|
|
|
// A request that isn't masked has no masking time, and never the phase.
|
|
func TestNoMaskingNoMaskingTime(t *testing.T) {
|
|
fresh(t)
|
|
slowOn(t, "a", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
io.ReadAll(r.Body)
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, chatStreamOK)
|
|
}))
|
|
s := New()
|
|
code, body := postTo(t, s, "/v1/chat/completions", `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"hi"}]}`)
|
|
if code != 200 {
|
|
t.Fatalf("%d %s", code, body)
|
|
}
|
|
rs := routesOf(s, "a/m")
|
|
if len(rs) != 1 || rs[0].Masking != 0 || rs[0].Phase != "" || rs[0].Own == 0 {
|
|
t.Fatalf("routes: %+v", rs)
|
|
}
|
|
}
|