Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
maskrec: the latest requests' bodies are kept zstd in memory, in 32 MiB (#2278)
yetoneb904cc22bf31
diff --git a/internal/maskrec/maskrec.go b/internal/maskrec/maskrec.goindex 20b081ff7..43716ea1d 100644--- a/internal/maskrec/maskrec.go+++ b/internal/maskrec/maskrec.go@@ -27,6 +27,7 @@ import ("sync""time"+ "github.com/klauspost/compress/zstd""github.com/yetone/magpie/internal/appdir")@@ -39,10 +40,13 @@ const (// keepSent is how many of the latest requests memory keeps whole, as// the vendor got them, up to sentMax each: the 脱敏 page shows a// request's text from it, its values put back from the masker's- // memory, and sentBudget in all+ // memory, and sentBudget in all, as stored: zstd, since a request is+ // JSON that repeats itself and a busy gateway held 64 MB of them+ // (#2278). A screenshot's base64 hardly compresses, so the budget is+ // half what it was whole.keepSent = 100sentMax = 4 << 20- sentBudget = 64 << 20+ sentBudget = 32 << 20dayForm = "2006-01-02")@@ -103,17 +107,30 @@ var store struct {pruned time.Timedays map[string]*dayAgg // a day's file → what it holds, as of its sizesnips []Record // the latest records, with their snips- sent []kept // the records whose Sent memory keeps, oldest first- // sentBytes is their Sent in all+ sent []kept // the latest records' Sent, oldest first+ // sentBytes is their bodies in all, as storedsentBytes int}-// kept is a record whose Sent memory keeps, and its length.+// kept is a record's Sent, zstd.type kept struct {- key int64- n int+ key int64+ body []byte}+var (+ sentEnc = sync.OnceValue(func() *zstd.Encoder {+ e, _ := zstd.NewWriter(nil, zstd.WithEncoderLevel(zstd.SpeedFastest), zstd.WithEncoderCRC(false),+ zstd.WithWindowSize(1<<20), zstd.WithEncoderConcurrency(2), zstd.WithLowerEncoderMem(true))+ return e+ })+ sentDec = sync.OnceValue(func() *zstd.Decoder {+ d, _ := zstd.NewReader(nil, zstd.WithDecoderConcurrency(1), zstd.WithDecoderLowmem(true),+ zstd.WithDecoderMaxMemory(sentMax))+ return d+ })+)+// inTest says Add writes nothing: in a test binary testenv isolated, a// write still going as a test ends would put masking/ back into its// temporary HOME (see the gateway's keepsRoutes).@@ -146,6 +163,12 @@ func Hold(r Record) (Record, func(provider, model string)) {if r.Time.IsZero() {r.Time = clock()}+ var body []byte+ if r.Sent != "" && len(r.Sent) <= sentMax {+ // the encoder may make room for all of Sent; keep what it wrote+ body = bytes.Clone(sentEnc().EncodeAll([]byte(r.Sent), nil))+ }+ r.Sent = ""store.mu.Lock()defer store.mu.Unlock()r.Key = r.Time.UnixNano()@@ -153,26 +176,19 @@ func Hold(r Record) (Record, func(provider, model string)) {r.Key = store.last + 1}store.last = r.Key- if len(r.Sent) > sentMax {- r.Sent = ""- }- if r.Sent != "" {- store.sent = append(store.sent, kept{r.Key, len(r.Sent)})- store.sentBytes += len(r.Sent)+ if body != nil {+ store.sent = append(store.sent, kept{r.Key, body})+ store.sentBytes += len(body)}for len(store.sent) > keepSent || store.sentBytes > sentBudget {- drop := store.sent[0]+ store.sentBytes -= len(store.sent[0].body)+ store.sent[0] = kept{}store.sent = store.sent[1:]- store.sentBytes -= drop.n- if i := at(drop.key); i >= 0 {- store.snips[i].Sent = ""- }}store.snips = append(store.snips, r)if len(store.snips) > keepSnips {store.snips = append([]Record(nil), store.snips[len(store.snips)-keepSnips:]...)}- r.Sent = ""var once sync.Oncereturn r, func(provider, model string) { once.Do(func() { settle(r, provider, model) }) }}@@ -375,12 +391,23 @@ func Restored(key int64, put map[string]int) {// SentOf is the request of key as the vendor got it, while memory keeps// it.func SentOf(key int64) (string, bool) {+ var body []bytestore.mu.Lock()- defer store.mu.Unlock()- if i := at(key); i >= 0 && store.snips[i].Sent != "" {- return store.snips[i].Sent, true+ for _, k := range store.sent {+ if k.key == key {+ body = k.body+ break+ }+ }+ store.mu.Unlock()+ if body == nil {+ return "", false+ }+ b, err := sentDec().DecodeAll(body, nil)+ if err != nil {+ return "", false}- return "", false+ return string(b), true}// Filter narrows List: by kind (one, or several split by commas), by@@ -458,9 +485,7 @@ func Get(key int64) (Record, bool) {store.mu.Lock()defer store.mu.Unlock()if i := at(key); i >= 0 {- r := store.snips[i]- r.Sent = ""- return r, true+ return store.snips[i], true}day := time.Unix(0, key).Local().Format(dayForm)var got Recorddiff --git a/internal/maskrec/sent_test.go b/internal/maskrec/sent_test.goindex 5045805d2..76df583cd 100644--- a/internal/maskrec/sent_test.go+++ b/internal/maskrec/sent_test.go@@ -1,6 +1,8 @@package maskrecimport (+ "fmt"+ "math/rand/v2""os""path/filepath""strings"@@ -56,36 +58,71 @@ func TestSentAndBackInMemory(t *testing.T) {t.Fatalf("the record went with its body: %+v %v", got, ok)}store.mu.Lock()- n := 0+ n := len(store.sent)for _, r := range store.snips {if r.Sent != "" {- n+++ t.Fatal("a record in memory holds its body whole")}}store.mu.Unlock()if n != keepSent {t.Fatalf("%d bodies kept, want %d", n, keepSent)}- // sentBudget in all: the oldest go first+ // sentBudget in all, as stored: the oldest go first. Random bytes+ // don't compress, so each takes about sentMax.sandbox(t, now)+ rnd := rand.New(rand.NewPCG(1, 2))var keys []int64for range 20 {- r := Add(Record{Time: now, What: "chat", N: 1, Sent: strings.Repeat("y", sentMax),+ raw := make([]byte, sentMax)+ for i := range raw {+ raw[i] = byte(rnd.Uint32())+ }+ r := Add(Record{Time: now, What: "chat", N: 1, Sent: string(raw),Values: []Value{{Placeholder: p, Kind: "API_KEY", By: "rule", Len: 40}}})keys = append(keys, r.Key)}- if _, ok := SentOf(keys[len(keys)-sentBudget/sentMax-1]); ok {+ store.mu.Lock()+ n, stored := len(store.sent), store.sentBytes+ store.mu.Unlock()+ if stored > sentBudget || n < sentBudget/sentMax-1 || n > sentBudget/sentMax {+ t.Fatalf("%d bodies of %d bytes kept, budget %d", n, stored, sentBudget)+ }+ if _, ok := SentOf(keys[len(keys)-n-1]); ok {t.Fatal("bodies over sentBudget kept")}- if _, ok := SentOf(keys[len(keys)-sentBudget/sentMax]); !ok {+ if _, ok := SentOf(keys[len(keys)-n]); !ok {t.Fatal("a body within sentBudget dropped")}- if store.sentBytes != sentBudget {- t.Fatalf("sentBytes %d", store.sentBytes)- }// a key nobody hasRestored(12345, map[string]int{p: 1})if _, ok := SentOf(12345); ok {t.Fatal("SentOf an unknown key")}}++// A request is JSON that repeats itself: memory keeps it compressed, a+// tenth of its size or less, and hands it back whole (#2278).+func TestSentKeptCompressed(t *testing.T) {+ now := time.Date(2026, 10, 24, 9, 0, 0, 0, time.Local)+ sandbox(t, now)+ const p = "{{API_KEY_ccccdddd}}"+ var b strings.Builder+ b.WriteString(`{"model":"gpt-6.1-sol","input":[`)+ for i := 0; b.Len() < 1<<20; i++ {+ fmt.Fprintf(&b, `{"type":"function_call_output","call_id":"call_%d","output":"line %d of the build log, key %s, ok\\n"},`, i, i*7, p)+ }+ b.WriteString(`{"role":"user","content":"go on"}]}`)+ body := b.String()+ r := Add(Record{Time: now, What: "chat", N: 1, Sent: body,+ Values: []Value{{Placeholder: p, Kind: "API_KEY", By: "rule", Len: 40}}})+ store.mu.Lock()+ stored := store.sentBytes+ store.mu.Unlock()+ if stored == 0 || stored > len(body)/10 {+ t.Fatalf("a %d-byte request is kept in %d bytes", len(body), stored)+ }+ if got, ok := SentOf(r.Key); !ok || got != body {+ t.Fatalf("SentOf: %d bytes, %v; want %d", len(got), ok, len(body))+ }+}