Skip to content

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

gateway: masking time is kept when magpie's own time is unknown

yetone442897f4149e
diff --git a/internal/gateway/codex_backend.go b/internal/gateway/codex_backend.go
index f4780e3c1..9a69fa7f7 100644
--- a/internal/gateway/codex_backend.go
+++ b/internal/gateway/codex_backend.go
@@ -1040,6 +1040,8 @@ func (s *Server) codexUpstreamOn(w http.ResponseWriter, r *http.Request, rest st
window = cmp.Or(provider.CodexWindow(user, model), catalog.ContextOf(model))
}
s.trace.update(tr, func(t *Route) {
+ // what masking took: magpie's own time isn't split here
+ t.Masking = maskingMs(r.Context(), 0)
// the last try: the turn asked again for an overload has one each
try := &t.Tries[len(t.Tries)-1]
try.Done, try.Status, try.Millis, try.Error = true, status, time.Since(try.Start).Milliseconds(), msg
@@ -1468,7 +1470,7 @@ func (s *Server) codexUpstreamOn(w http.ResponseWriter, r *http.Request, rest st
Provider: servedProvider, Host: provider.HostOf(base), ProviderAccount: servedAccount, Model: call.Model,
Requested: call.Model, Served: served,
Input: uu.Input, Output: uu.Output, CacheRead: uu.CacheRead, CacheWrite: uu.CacheWrite,
- Reasoning: uu.Reasoning, Effort: effort, Millis: call.Millis,
+ Reasoning: uu.Reasoning, Effort: effort, Millis: call.Millis, Masking: maskingMs(r.Context(), 0),
// Codex's own sign-in: the request goes as Codex sent it
TierAsked: bodyTier(provider.Responses, body), TierSent: bodyTier(provider.Responses, body), TierServed: uu.Tier, TTFT: call.TTFT, FirstText: call.FirstText, ReasoningHidden: call.ReasoningHidden, Flow: call.Flow, Status: call.Status, Session: sessionOf(r.Header), NativeSession: nativeSessionOf(r.Header), Kind: call.Kind,
RequestID: requestID(res.Header), ResponseID: uu.ResponseID, Endpoint: r.URL.Path}
diff --git a/internal/gateway/codex_backend_test.go b/internal/gateway/codex_backend_test.go
index 5ccd00c4d..bb7af2c20 100644
--- a/internal/gateway/codex_backend_test.go
+++ b/internal/gateway/codex_backend_test.go
@@ -11,12 +11,15 @@ import (
"path/filepath"
"strconv"
"strings"
+ "sync"
"testing"
"time"
"github.com/klauspost/compress/zstd"
"github.com/yetone/magpie/internal/codexcat"
+ "github.com/yetone/magpie/internal/gwmw"
"github.com/yetone/magpie/internal/provider"
+ "github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/usage"
)
@@ -1284,3 +1287,42 @@ func TestCodexOwnSignInTellsWhereItsTimeWent(t *testing.T) {
}
}
+// A turn on Codex's own sign-in that was masked keeps what masking took,
+// on its route and in its usage record, as a routed one does: the
+// Routing page showed no masking time for it.
+func TestCodexOwnSignInKeepsItsMaskingTime(t *testing.T) {
+ setup(t, provider.Chat, &fake{t: t})
+ if err := settings.Save(settings.Settings{Redact: true}); err != nil {
+ t.Fatal(err)
+ }
+ chatgpt(t, func(w http.ResponseWriter, r *http.Request) {
+ io.ReadAll(r.Body)
+ w.Header().Set("Content-Type", "text/event-stream")
+ io.WriteString(w, sse(`data: {"type":"response.completed","response":{"id":"r1","usage":{"input_tokens":9,"output_tokens":2}}}`))
+ })
+ withoutMasker(t)
+ m := heldMasker{let: make(chan struct{}), entered: make(chan struct{}), once: new(sync.Once)}
+ t.Cleanup(gwmw.Mask(m))
+ const hold = 120 * time.Millisecond
+ go func() {
+ <-m.entered
+ time.Sleep(hold)
+ close(m.let)
+ }()
+ s := New()
+ rec := httptest.NewRecorder()
+ req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(`{"model":"gpt-5.5","stream":true,"input":"hi"}`))
+ req.Header.Set("Authorization", "Bearer chatgpt-token")
+ s.Handler().ServeHTTP(rec, req)
+ st := s.Trace(t.Context(), 0, 0)
+ if rec.Code != 200 || len(st.Routes) != 1 {
+ t.Fatalf("%d %+v", rec.Code, st.Routes)
+ }
+ if got := st.Routes[0].Masking; got < hold.Milliseconds() {
+ t.Fatalf("route's masking %d ms, held %s", got, hold)
+ }
+ recs := usage.Load(time.Time{})
+ if len(recs) == 0 || recs[len(recs)-1].Masking != st.Routes[0].Masking {
+ t.Fatalf("usage records %+v, route's masking %d", recs, st.Routes[0].Masking)
+ }
+}
diff --git a/internal/gateway/redact_phase.go b/internal/gateway/redact_phase.go
index 2a9123fd7..9221a6764 100644
--- a/internal/gateway/redact_phase.go
+++ b/internal/gateway/redact_phase.go
@@ -87,13 +87,20 @@ func (f *inflight) maskedFor() time.Duration {
// 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.
+// most own. Under a ms is still said, as 1; none is 0. With own unknown
+// (0: a plugin's fetch or Claude Code sent it, or no first token came)
+// it is all masking took: the request was masked all the same, and the
+// Routing page showed no masking for it (Discord, lute).
func maskingMs(ctx context.Context, own int64) int64 {
d := inflightOf(ctx).maskedFor()
- if d <= 0 || own <= 0 {
+ if d <= 0 {
return 0
}
- return min(max(d.Milliseconds(), 1), own)
+ ms := max(d.Milliseconds(), 1)
+ if own > 0 {
+ return min(ms, own)
+ }
+ return ms
}
// showRedacting puts f's phase on the route that shows it, its own or the
diff --git a/internal/gateway/redact_phase_test.go b/internal/gateway/redact_phase_test.go
index d42f4d69a..24e50015f 100644
--- a/internal/gateway/redact_phase_test.go
+++ b/internal/gateway/redact_phase_test.go
@@ -163,3 +163,45 @@ func TestNoMaskingNoMaskingTime(t *testing.T) {
t.Fatalf("routes: %+v", rs)
}
}
+
+// lute on Discord: a masked request whose time magpie can't split (here
+// the vendor refused it, so there is no first token) showed no masking
+// time at all. What masking took is kept whenever it took any, all of
+// it when magpie's own time is unknown.
+func TestMaskingTimeKeptWhenOwnIsUnknown(t *testing.T) {
+ 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", "application/json")
+ w.WriteHeader(http.StatusBadRequest)
+ io.WriteString(w, `{"error":{"message":"no"}}`)
+ }))
+ withoutMasker(t)
+ m := heldMasker{let: make(chan struct{}), entered: make(chan struct{}), once: new(sync.Once)}
+ t.Cleanup(gwmw.Mask(m))
+ const hold = 120 * time.Millisecond
+ go func() {
+ <-m.entered
+ time.Sleep(hold)
+ close(m.let)
+ }()
+
+ 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("the vendor's refusal answered %d %s", code, body)
+ }
+ rs := routesOf(s, "a/m")
+ if len(rs) != 1 || !rs[0].Done {
+ t.Fatalf("routes: %+v", rs)
+ }
+ if rs[0].Own != 0 {
+ t.Fatalf("own is known here (%d ms); this test is about when it isn't", rs[0].Own)
+ }
+ if rs[0].Masking < hold.Milliseconds() {
+ t.Fatalf("masking %d ms, held %s", rs[0].Masking, hold)
+ }
+}
diff --git a/internal/gateway/trace.go b/internal/gateway/trace.go
index 9e06af563..2cf7cbeb8 100644
--- a/internal/gateway/trace.go
+++ b/internal/gateway/trace.go
@@ -122,7 +122,8 @@ type Route struct {
Queued int64 `json:"queued,omitempty"`
// Phase: "redact" while it is being masked and not yet done
// (noteRedacting), so its row says masking rather than routing;
- // Masking: the ms of Own that went on masking it (maskingMs)
+ // Masking: the ms of Own that went on masking it, all of it when Own
+ // is unknown (maskingMs)
Phase string `json:"phase,omitempty"`
Masking int64 `json:"masking,omitempty"`
// Held: the file name, in HeldDir, of the report saved of a request
diff --git a/internal/usage/usage.go b/internal/usage/usage.go
index 571791665..f3f33f5a5 100644
--- a/internal/usage/usage.go
+++ b/internal/usage/usage.go
@@ -130,8 +130,8 @@ type Record struct {
Own int64 `json:"own_ms,omitempty"`
// Masking: the ms of Own that went on masking the request (Settings
// → Privacy: the rules, and the local model with its wait for a
- // turn); 0 where nothing was masked, or in a record from before it
- // was kept.
+ // turn), all of it where Own is unknown; 0 where nothing was masked,
+ // or in a record from before it was kept.
Masking int64 `json:"masking_ms,omitempty"`
Retried int64 `json:"retried_ms,omitempty"`
Queued int64 `json:"queued_ms,omitempty"`