Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gateway: a chat body is copied for its masked record only when the record keeps it (#4004)

yetonef76e4d1f5c5c
diff --git a/internal/gateway/codex_backend.go b/internal/gateway/codex_backend.go
index c05823b01..47f2094de 100644
--- a/internal/gateway/codex_backend.go
+++ b/internal/gateway/codex_backend.go
@@ -951,7 +951,7 @@ func (s *Server) codexUpstreamOn(w http.ResponseWriter, r *http.Request, rest st
var tools *toolScan // whether the reply calls a tool, for a turn's end
if rest != "/responses" {
// no route of its own (a compaction, a memory): recorded alone
- noteMasked(r, "chat", maskedHits, string(body), 0, "openai", modelOf(body))
+ noteMasked(r, "chat", maskedHits, sentOf(maskedHits, body), 0, "openai", modelOf(body))
}
if rest == "/responses" {
imageProvider := ""
@@ -972,7 +972,7 @@ func (s *Server) codexUpstreamOn(w http.ResponseWriter, r *http.Request, rest st
tools = scan
tr = s.trace.beginFor(r, Route{turn: turn, dirOf: dirOf(provider.Responses, body), tools: tools, imageTurn: drawingTurnID(metadata.Turn), imageCaller: codexTurnKey(r, callerOf(r).agent), imageProvider: imageProvider, TitleLink: link, Time: start, Agent: callerOf(r).agent, Session: sessionOf(r.Header), Conv: convOf(r.Header, body), ParentSession: titleParentSession(r.Header, metadata, kind), Kind: kind, Model: model, Effort: effort, Tier: bodyTier(provider.Responses, body), Provider: "openai", Subagent: subagentPickOf(r),
Order: []Weighed{seat}, Tries: []Try{{ID: seat.ID, Model: model, Effort: effort, Start: start}}})
- if m := noteMasked(r, "chat", maskedHits, string(body), tr.ID, "openai", model); m != nil {
+ if m := noteMasked(r, "chat", maskedHits, sentOf(maskedHits, body), tr.ID, "openai", model); m != nil {
s.trace.update(tr, func(t *Route) { t.Masked = m })
}
promptRead := s.inspectPrompt(tr, provider.Responses, body, body, false)
diff --git a/internal/gateway/codex_search.go b/internal/gateway/codex_search.go
index 8083f1b01..241255533 100644
--- a/internal/gateway/codex_search.go
+++ b/internal/gateway/codex_search.go
@@ -128,7 +128,7 @@ func (s *Server) alphaSearchOn(w http.ResponseWriter, r *http.Request, body []by
if err != nil {
return false, err.Error()
}
- noteMasked(r, "search", hits, string(body), 0, "openai", model)
+ noteMasked(r, "search", hits, sentOf(hits, body), 0, "openai", model)
var errs []string
for _, on := range ons {
who := "OpenAI (" + on.Account.User + "): "
diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go
index dfdffee2c..faf3debdc 100644
--- a/internal/gateway/gateway.go
+++ b/internal/gateway/gateway.go
@@ -2496,7 +2496,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
// the last try went to, not the group's first member, which a
// failover may never have reached
if !sentWhole {
- m, done := noteMaskedUntil(r, "chat", maskedHits, string(body), tr.ID, p.ID, call.Model)
+ m, done := noteMaskedUntil(r, "chat", maskedHits, sentOf(maskedHits, body), tr.ID, p.ID, call.Model)
if m != nil {
s.trace.update(tr, func(t *Route) { t.Masked = m })
}
diff --git a/internal/gateway/masked.go b/internal/gateway/masked.go
index afe8be3e3..11afdf00e 100644
--- a/internal/gateway/masked.go
+++ b/internal/gateway/masked.go
@@ -43,6 +43,17 @@ func (h *maskHits) add(o maskHits) {
h.took += o.took
}
+// sentOf is a masked body as noteMasked takes it: copied only for a
+// record that keeps it. With nothing masked no record is made, and one
+// over maskrec.SentMax keeps none of it, so a 45 MiB Codex turn was
+// copied whole on every request for nothing (#4004).
+func sentOf(h maskHits, body []byte) string {
+ if h.n == 0 && len(h.hits) == 0 || len(body) > maskrec.SentMax {
+ return ""
+ }
+ return string(body)
+}
+
// noteMasked records what masking swapped in a request that went to a
// vendor as sent: what it was (chat, search, image…), who sent it, the
// route it is in the trace (0 for none), and the provider and model it
diff --git a/internal/gateway/masked_test.go b/internal/gateway/masked_test.go
index 206f75468..70255c982 100644
--- a/internal/gateway/masked_test.go
+++ b/internal/gateway/masked_test.go
@@ -123,3 +123,21 @@ func TestSnipSharedByNearValues(t *testing.T) {
t.Fatalf("snips = %q", got)
}
}
+
+// A body is copied for a masked record only when the record keeps it:
+// with nothing masked there is no record, and one over maskrec.SentMax
+// keeps none of it. Converting every chat body to a string for it copied
+// a 45 MiB Codex turn whole on each request (#4004).
+func TestMaskedBodyCopiedOnlyWhenKept(t *testing.T) {
+ body := []byte(strings.Repeat("x", 2<<20))
+ if n := allocated(func() { _ = sentOf(maskHits{}, body) }); n > 64<<10 {
+ t.Fatalf("nothing masked: %d bytes allocated for a %d byte body", n, len(body))
+ }
+ if got := sentOf(maskHits{n: 1}, body); got != string(body) {
+ t.Fatal("a masked body under SentMax isn't kept as sent")
+ }
+ big := []byte(strings.Repeat("y", maskrec.SentMax+1))
+ if n := allocated(func() { _ = sentOf(maskHits{n: 1}, big) }); n > 64<<10 {
+ t.Fatalf("over SentMax: %d bytes allocated", n)
+ }
+}
diff --git a/internal/maskrec/maskrec.go b/internal/maskrec/maskrec.go
index 45804a2ad..20b081ff7 100644
--- a/internal/maskrec/maskrec.go
+++ b/internal/maskrec/maskrec.go
@@ -46,6 +46,10 @@ const (
dayForm = "2006-01-02"
)
+// SentMax is the longest request a record keeps as sent; a longer one is
+// kept as nothing, so a caller need not copy it.
+const SentMax = sentMax
+
// Value is one value a request had masked, as often as it had it.
type Value struct {
Placeholder string `json:"p"`