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.
123 lines
3.2 KiB
Go
123 lines
3.2 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// phaseRedact is the phase of a request while it is masked (redactedHits,
|
|
// maskTextHits): the rules, and Settings → Privacy's local model with its
|
|
// wait for a turn. Its route says it too (Route.Phase), so the Routing
|
|
// page's row says masking, not routing, while it lasts (fremontdango on
|
|
// Discord: a long Claude Code request sat as routing while it was
|
|
// being masked).
|
|
const phaseRedact = "redact"
|
|
|
|
// redaction is a request's maskings: how many are under way, since when
|
|
// one has been, the time spent in one before that, and the phase to put
|
|
// back when the last ends. It holds no text.
|
|
type redaction struct {
|
|
mu sync.Mutex
|
|
now int
|
|
since time.Time
|
|
spent time.Duration
|
|
restore func()
|
|
}
|
|
|
|
// noteRedacting tells ctx's request it is being masked, until the func it
|
|
// answers is called: its phase is phaseRedact, the route that shows it
|
|
// says so, its watch counts the time toward neither of its limits
|
|
// (noteMasking), and the time is added to what masking took it
|
|
// (maskedFor). Maskings at once are counted once, from the first's start
|
|
// to the last's end.
|
|
func noteRedacting(ctx context.Context) func() {
|
|
unmasked := noteMasking(ctx)
|
|
f := inflightOf(ctx)
|
|
if f == nil {
|
|
return unmasked
|
|
}
|
|
f.redaction.mu.Lock()
|
|
if f.redaction.now++; f.redaction.now == 1 {
|
|
f.redaction.since = time.Now()
|
|
f.redaction.restore = notePhase(ctx, phaseRedact)
|
|
}
|
|
f.redaction.mu.Unlock()
|
|
f.showRedacting()
|
|
var once sync.Once
|
|
return func() {
|
|
once.Do(func() {
|
|
f.redaction.mu.Lock()
|
|
var restore func()
|
|
if f.redaction.now--; f.redaction.now == 0 {
|
|
f.redaction.spent += time.Since(f.redaction.since)
|
|
restore, f.redaction.restore = f.redaction.restore, nil
|
|
}
|
|
f.redaction.mu.Unlock()
|
|
if restore != nil {
|
|
restore()
|
|
}
|
|
unmasked()
|
|
f.showRedacting()
|
|
})
|
|
}
|
|
}
|
|
|
|
// redacting says whether f is being masked now.
|
|
func (f *inflight) redacting() bool {
|
|
f.redaction.mu.Lock()
|
|
defer f.redaction.mu.Unlock()
|
|
return f.redaction.now > 0
|
|
}
|
|
|
|
// maskedFor is how long masking has taken f so far, the one under way
|
|
// included; 0 for none or no f.
|
|
func (f *inflight) maskedFor() time.Duration {
|
|
if f == nil {
|
|
return 0
|
|
}
|
|
f.redaction.mu.Lock()
|
|
defer f.redaction.mu.Unlock()
|
|
d := f.redaction.spent
|
|
if f.redaction.now > 0 {
|
|
d += time.Since(f.redaction.since)
|
|
}
|
|
return d
|
|
}
|
|
|
|
// maskingMs is the ms of own, magpie's own time before the request went
|
|
// out (splitSent), that went on masking it: what masking took it, at
|
|
// most own. Under a ms is still said, as 1; none is 0.
|
|
func maskingMs(ctx context.Context, own int64) int64 {
|
|
d := inflightOf(ctx).maskedFor()
|
|
if d <= 0 || own <= 0 {
|
|
return 0
|
|
}
|
|
return min(max(d.Milliseconds(), 1), own)
|
|
}
|
|
|
|
// showRedacting puts f's phase on the route that shows it, its own or the
|
|
// one its watch began (unroutedHold.show), while it isn't done.
|
|
func (f *inflight) showRedacting() {
|
|
rt := f.route.Load()
|
|
f.mu.Lock()
|
|
tr := f.tr
|
|
if rt == nil && f.unrouted != nil {
|
|
rt = f.unrouted.shown
|
|
}
|
|
f.mu.Unlock()
|
|
if rt == nil || tr == nil {
|
|
return
|
|
}
|
|
tr.update(rt, func(r *Route) {
|
|
if r.Done {
|
|
r.Phase = ""
|
|
return
|
|
}
|
|
r.Phase = ""
|
|
if f.redacting() {
|
|
r.Phase = phaseRedact
|
|
}
|
|
})
|
|
}
|