diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md index 19764e239..ce3cdd33a 100644 --- a/docs/subsystems/gateway-routing.md +++ b/docs/subsystems/gateway-routing.md @@ -281,8 +281,12 @@ recorded as a prediction failure (`noPrediction`). every `redactedHits` and `maskTextHits`), on `/v1/magpie/concurrency` and on the route that shows it (`Route.Phase`), so the row says masking, not routing (fremontdango on Discord: a Claude Code request - sat as "routing…" while it was masked). It holds no text. - `TestMaskingIsAPhase`, `TestNoMaskingNoPhase` in + sat as "routing…" while it was masked). What masking took is kept + as a part of magpie's own time, never more than it (`maskingMs`): + `Route.Masking` and the usage record's `masking_ms`, shown as + *Masking* in the Routing page's time and the Usage page's detail. It + holds no text. `TestMaskingIsAPhaseWithItsTime`, + `TestNoMaskingNoMaskingTime` in [`redact_phase_test.go`](../../internal/gateway/redact_phase_test.go), `routing-masking.test.cjs`. It is answered 504 after `unroutedUnknown` (30s) held in no wait magpie knows of, or after diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 30dc45c5c..f509a2e15 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -3788,6 +3788,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro t.FirstReasoning, t.ReasoningHidden = call.FirstReasoning, call.ReasoningHidden t.Output, t.Reasoning, t.TTFT, t.FirstText, t.Flow = call.Usage.Output, call.Usage.Reasoning, call.TTFT, call.FirstText, call.Flow t.Sent, t.Own, t.Retried, t.Queued = sentMs, ownMs, retriedMs, queuedMs + t.Masking = maskingMs(r.Context(), ownMs) if prompt != nil { t.Prompt = prompt.calibrated(promptCounted(t.Usage), window) } @@ -3807,7 +3808,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro Input: call.Usage.billed().Input, Output: call.Usage.billed().Output, CacheRead: call.Usage.billed().CacheRead, CacheWrite: call.Usage.billed().CacheWrite, CacheWrite1h: call.Usage.billed().CacheWrite1h, CacheUnknown: call.Usage.billed().cacheNotCounted(), Reasoning: call.Usage.billed().Reasoning, Credits: call.Usage.Credits, Effort: wentAt, Millis: call.Millis, Status: call.Status, TierAsked: tierAsked, TierSent: wentTier, TierServed: call.Usage.Tier, - TTFT: call.TTFT, FirstText: call.FirstText, FirstReasoning: call.FirstReasoning, ReasoningHidden: call.ReasoningHidden, Flow: call.Flow, Sent: sentMs, Own: ownMs, Retried: retriedMs, Queued: queuedMs, Session: sessionOf(r.Header), NativeSession: nativeSessionOf(r.Header), Kind: call.Kind, + TTFT: call.TTFT, FirstText: call.FirstText, FirstReasoning: call.FirstReasoning, ReasoningHidden: call.ReasoningHidden, Flow: call.Flow, Sent: sentMs, Own: ownMs, Masking: maskingMs(r.Context(), ownMs), Retried: retriedMs, Queued: queuedMs, Session: sessionOf(r.Header), NativeSession: nativeSessionOf(r.Header), Kind: call.Kind, RequestID: call.Usage.RequestID, ResponseID: call.Usage.ResponseID, Endpoint: endpointOf(r, from, call.To, lastTried, model), Stop: call.Usage.Stop, Archive: call.archiveName()} if tries > 1 { // answered, or not, after a try that failed first: the ledger diff --git a/internal/gateway/redact_phase.go b/internal/gateway/redact_phase.go index fe637ecf3..2a9123fd7 100644 --- a/internal/gateway/redact_phase.go +++ b/internal/gateway/redact_phase.go @@ -3,6 +3,7 @@ package gateway import ( "context" "sync" + "time" ) // phaseRedact is the phase of a request while it is masked (redactedHits, @@ -13,19 +14,23 @@ import ( // being masked). const phaseRedact = "redact" -// redaction is a request's maskings: how many are under way, and the -// phase to put back when the last ends. It holds no text. +// 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). Maskings at once are one, from the first's start to the -// last's end. +// (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) @@ -34,6 +39,7 @@ func noteRedacting(ctx context.Context) func() { } 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() @@ -44,6 +50,7 @@ func noteRedacting(ctx context.Context) 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() @@ -63,6 +70,32 @@ func (f *inflight) redacting() bool { 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() { diff --git a/internal/gateway/redact_phase_test.go b/internal/gateway/redact_phase_test.go index 1e9cd3c45..d42f4d69a 100644 --- a/internal/gateway/redact_phase_test.go +++ b/internal/gateway/redact_phase_test.go @@ -12,6 +12,7 @@ import ( "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 @@ -58,8 +59,9 @@ func (heldMasker) Writer(w http.ResponseWriter) (http.ResponseWriter, 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. -func TestMaskingIsAPhase(t *testing.T) { +// 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 { @@ -89,6 +91,7 @@ func TestMaskingIsAPhase(t *testing.T) { 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 { @@ -109,6 +112,7 @@ func TestMaskingIsAPhase(t *testing.T) { } time.Sleep(5 * time.Millisecond) } + held := time.Since(entered) let() var r unroutedReply select { @@ -128,10 +132,21 @@ func TestMaskingIsAPhase(t *testing.T) { 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 never has the phase. -func TestNoMaskingNoPhase(t *testing.T) { +// 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) @@ -144,7 +159,7 @@ func TestNoMaskingNoPhase(t *testing.T) { t.Fatalf("%d %s", code, body) } rs := routesOf(s, "a/m") - if len(rs) != 1 || rs[0].Phase != "" || rs[0].Own == 0 { + if len(rs) != 1 || rs[0].Masking != 0 || rs[0].Phase != "" || rs[0].Own == 0 { t.Fatalf("routes: %+v", rs) } } diff --git a/internal/gateway/trace.go b/internal/gateway/trace.go index d41458e0b..a837893f0 100644 --- a/internal/gateway/trace.go +++ b/internal/gateway/trace.go @@ -121,8 +121,10 @@ type Route struct { Retried int64 `json:"retried,omitempty"` 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 - Phase string `json:"phase,omitempty"` + // (noteRedacting), so its row says masking rather than routing; + // Masking: the ms of Own that went on masking it (maskingMs) + Phase string `json:"phase,omitempty"` + Masking int64 `json:"masking,omitempty"` // Served: the model the reply says answered, as the last try has it; // Swapped: another than the one that try asked for; Routed: that try // asked another magpie's routing group, and Served is its member diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js index c198e2eeb..0018ff349 100644 --- a/internal/gui/assets/plugins/routing/client.js +++ b/internal/gui/assets/plugins/routing/client.js @@ -1112,6 +1112,8 @@ function routingPage(part) { const ms1 = (n) => n < 10 ? t("{n} ms", { n: (Math.round(n * 10) / 10).toString() }) : took(Math.round(n)); function timeParts(tr) { const n = tr.net, out = [], add = (who, label, v) => { if (v >= 0.1) out.push({ who, label, v }); }; + // the request's masking, before its first try (Route.masking) + add("magpie", "Masking", tr.masking || 0); if (!n) return out; add("magpie", "Waiting for a slot", tr.queued || 0); add("magpie", "In magpie", n.own || 0); @@ -2671,9 +2673,11 @@ function routingPage(part) { for (const a of asides(r)) items.push([a, "aside"]); r.tries.forEach((tr, i) => { items.push([tryWhy(r, i), tr.done ? (tryOk(tr) ? "ok" : "bad") : "wait"]); - // where its time went, once it is done - const when = tr.done && timeSaid(tr); - if (when) items.push([when, "aside time", tr]); + // where its time went, once it is done: the first try's has the + // request's masking before it + const timed = i === 0 && r.masking > 0 ? { ...tr, masking: r.masking } : tr; + const when = tr.done && timeSaid(timed); + if (when) items.push([when, "aside time", timed]); // each model Copilot's Auto picked for it, where from, and whether // Copilot refused it, on which API and with Auto's session token or // without (#256) diff --git a/internal/gui/assets/plugins/usage/page.js b/internal/gui/assets/plugins/usage/page.js index 228031948..7b996ca07 100644 --- a/internal/gui/assets/plugins/usage/page.js +++ b/internal/gui/assets/plugins/usage/page.js @@ -1025,7 +1025,10 @@ function ledDetail(r, cols) { // before they were kept has only the whole if (r.ttft_ms && r.sent_ms) { const split = r.own_ms != null; - add("In magpie", ledTook(split ? r.own_ms : r.sent_ms)); + // of magpie's own, what went on masking it (Settings → Privacy) + const masking = split && r.masking_ms > 0 ? Math.min(r.masking_ms, r.own_ms) : 0; + if (masking) add("Masking", ledTook(masking)); + add("In magpie", ledTook((split ? r.own_ms : r.sent_ms) - masking)); if (split) { if (r.retried_ms > 0) add("Tries that failed first", ledTook(r.retried_ms)); if (r.queued_ms > 0) add("Waiting for a slot", ledTook(r.queued_ms)); diff --git a/internal/gui/tests/routing-masking.test.cjs b/internal/gui/tests/routing-masking.test.cjs index a8731f712..de10e372b 100644 --- a/internal/gui/tests/routing-masking.test.cjs +++ b/internal/gui/tests/routing-masking.test.cjs @@ -3,7 +3,9 @@ // (fremontdango on Discord: a long Claude Code request sat as "routing…" // while it was masked): its row says masking while the gateway's route for // it has the "redact" phase, and routing once that is over and no try is -// out yet. Both engines, a narrow window, every language. The API is faked. +// out yet. Once answered, the time masking took is the first part of where +// its first try's time went, magpie's. Both engines, a narrow window, every +// language. The API is faked. const assert = require("node:assert/strict"); const fs = require("node:fs/promises"); const path = require("node:path"); @@ -20,10 +22,10 @@ const routes = [ { id: 203, seq: 203, time: at(20), agent: "claude", model: "deepseek/deepseek-flash", provider: "magpie", phase: "redact", order: [], tries: [], done: false, status: 0 }, // held, masked, not yet routed { id: 202, seq: 202, time: at(40), agent: "claude", model: "deepseek/deepseek-chat", provider: "magpie", order: [], tries: [], done: false, status: 0 }, - // answered + // answered, after 2.3 s of masking { id: 201, seq: 201, time: at(90), agent: "claude", model: "deepseek/deepseek-flash", provider: "deepseek", order: [key], tries: [{ id: key.id, model: "deepseek-flash", start: at(87), done: true, status: 200, ms: 3000, ttft: 1200, net }], - done: true, status: 200, ms: 5400, ttft: 3500, own: 2310, sent: 2320, tokens: 900, served: "deepseek-flash" }, + done: true, status: 200, ms: 5400, ttft: 3500, own: 2310, masking: 2300, sent: 2320, tokens: 900, served: "deepseek-flash" }, ]; function serve(lang) { @@ -55,16 +57,16 @@ function serve(lang) { } const want = { - en: { masking: "masking…", routing: "routing…" }, - zh: { masking: "脱敏中…", routing: "路由中…" }, - "zh-TW": { masking: "脫敏中…", routing: "路由中…" }, - ja: { masking: "マスク中…", routing: "ルーティング中…" }, - de: { masking: "maskiert…", routing: "leitet weiter…" }, + en: { masking: "masking…", routing: "routing…", part: "Masking" }, + zh: { masking: "脱敏中…", routing: "路由中…", part: "脱敏" }, + "zh-TW": { masking: "脫敏中…", routing: "路由中…", part: "脫敏" }, + ja: { masking: "マスク中…", routing: "ルーティング中…", part: "マスク" }, + de: { masking: "maskiert…", routing: "leitet weiter…", part: "Maskierung" }, }; for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { for (const lang of Object.keys(want)) { - test(`${engine} ${lang}: a request being masked says so`, async (t) => { + test(`${engine} ${lang}: a request being masked says so, and its time`, async (t) => { const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); const context = await browser.newContext({ viewport: { width: 440, height: 900 }, reducedMotion: "reduce" }); const page = await context.newPage(); @@ -89,6 +91,15 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert(said[1].includes(want[lang].routing) && !said[1].includes(want[lang].masking), said[1]); assert(!said[2].includes(want[lang].masking), said[2]); + // the answered one: masking is magpie's first part of its try's time + await rows.nth(2).click(); + const box = page.locator(".rt-steps li.time .rt-time"); + await box.waitFor(); + const parts = await box.evaluate((b) => [...b.querySelectorAll(".rt-time-parts li")].map((li) => [li.className, li.querySelector(".k").textContent, li.querySelector(".v").textContent])); + assert.equal(parts[0][0], "magpie"); + assert.equal(parts[0][1], want[lang].part); + assert.match(parts[0][2], /2[.,]3/); + assert.equal(parts.filter((p) => p[1] === want[lang].part).length, 1); assert.deepEqual(errors, []); }); } diff --git a/internal/gui/tests/usage-ledger-detail.test.cjs b/internal/gui/tests/usage-ledger-detail.test.cjs index 3f401af2b..aaec79f87 100644 --- a/internal/gui/tests/usage-ledger-detail.test.cjs +++ b/internal/gui/tests/usage-ledger-detail.test.cjs @@ -30,7 +30,7 @@ const ROWS = [ { t: new Date(now - 240e3).toISOString(), agent: "claude-desktop", agentName: "Claude Desktop", icon: "claude-color", provider: "claude", providerName: "Claude", model: "", in: 0, out: 0, status: 0, err: "You've hit your limit · resets 3am", err_type: "rate_limit", rid: "req_lim", session: "s3", source: "log", cost: 0, priced: false }, // the gateway's, answered on its second try (#1532): the vendor held the // first ~103 s before its 503, which isn't magpie's time - { t: new Date(now - 300e3).toISOString(), agent: "codex", agentName: "Codex", icon: "codex-color", provider: "opencode-go", providerName: "OpenCode Go", host: "opencode.ai", req: "opencode-go/deepseek-flash", model: "deepseek-flash", served: "deepseek-flash", effort: "max", in: 7064, out: 5373, cache_read: 314624, reasoning: 4946, ms: 160000, ttft_ms: 141000, sent_ms: 104000, own_ms: 180, retried_ms: 103000, tries: 2, status: 200, ep: "/backend-api/codex/responses", session: "01a12671", cost: 0.005, priced: true }, + { t: new Date(now - 300e3).toISOString(), agent: "codex", agentName: "Codex", icon: "codex-color", provider: "opencode-go", providerName: "OpenCode Go", host: "opencode.ai", req: "opencode-go/deepseek-flash", model: "deepseek-flash", served: "deepseek-flash", effort: "max", in: 7064, out: 5373, cache_read: 314624, reasoning: 4946, ms: 160000, ttft_ms: 141000, sent_ms: 104000, own_ms: 180, masking_ms: 150, retried_ms: 103000, tries: 2, status: 200, ep: "/backend-api/codex/responses", session: "01a12671", cost: 0.005, priced: true }, ]; function page(q) { @@ -66,14 +66,14 @@ function server(lang, refreshed) { const L = { en: { statuses: ["429 · rate_limit_error", "200", "Succeeded", "rate_limit", "200 · 2 tries"], badge: "Local session", - split: [["In magpie", "180 ms"], ["Tries that failed first", "103 s"], ["Connecting and sending", "820 ms"], ["Vendor's first token", "37 s"]], + split: [["Masking", "150 ms"], ["In magpie", "30 ms"], ["Tries that failed first", "103 s"], ["Connecting and sending", "820 ms"], ["Vendor's first token", "37 s"]], speed: "24 tok/s", tokens: "300 in · 40 out · 0 cache read · 0 cache creation", labels: { fail: ["Status", "Error type", "Upstream said", "Duration", "Request ID", "Endpoint", "Protocol", "Session ID"], ok: ["Status", "Duration", "Tokens", "Speed", "Request ID", "Endpoint", "Protocol", "Called for", "First token", "In magpie", "Vendor's first token"], log: ["Request ID", "Session ID", "Data source"], logFail: ["Status", "Error type", "Error", "Request ID", "Session ID", "Data source"] }, subagent: "Subagent", noStatus: "Read from the agent's session file. The account is shown only when local metadata identifies it; no service provider is inferred.", }, zh: { statuses: ["429 · rate_limit_error", "200", "成功", "rate_limit", "200 · 2 次尝试"], badge: "本地会话", - split: [["magpie 内耗时", "180 毫秒"], ["之前失败的尝试", "103 秒"], ["连接并发送请求", "820 毫秒"], ["厂商首字等待", "37 秒"]], + split: [["脱敏", "150 毫秒"], ["magpie 内耗时", "30 毫秒"], ["之前失败的尝试", "103 秒"], ["连接并发送请求", "820 毫秒"], ["厂商首字等待", "37 秒"]], speed: "24 token/秒", tokens: "输入 300 · 输出 40 · 缓存读取 0 · 缓存创建 0", labels: { fail: ["状态", "错误类型", "上游返回", "耗时", "请求 ID", "终结点", "协议", "会话 ID"], ok: ["状态", "耗时", "Token", "速度", "请求 ID", "终结点", "协议", "用途", "首字耗时", "magpie 内耗时", "厂商首字等待"], log: ["请求 ID", "会话 ID", "数据来源"], logFail: ["状态", "错误类型", "错误", "请求 ID", "会话 ID", "数据来源"] }, subagent: "子代理", noStatus: "读自 Agent 的会话文件;仅在本地元数据能够明确识别时显示账号,不推断供应商。", @@ -183,11 +183,12 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", // #1532: of the 104 s before the vendor had the request, magpie's own // 180 ms, the try that failed first 103 s, connecting and sending the // rest; the 40 ms of a record from before the split was kept stays - // "In magpie" (above) + // "In magpie" (above). Of magpie's own, the 150 ms masking it took + // is told apart (fremontdango on Discord) await reader.click(p, rows.nth(4)); const pairs = await p.locator(".led-detail").nth(3).evaluate((d) => [...d.querySelectorAll("dt")].map((dt) => [dt.textContent, dt.nextElementSibling.textContent])); const at = pairs.findIndex(([k]) => k === w.split[0][0]); - assert.deepEqual(pairs.slice(at, at + 4), w.split); + assert.deepEqual(pairs.slice(at, at + w.split.length), w.split); await reader.click(p, rows.nth(4)); // a refresh of the list keeps the rows open diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index 34dd9e72a..f07ff990f 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -14,6 +14,7 @@ import ( "hash/maphash" "io" "maps" + "math" "os" "path/filepath" "reflect" @@ -76,6 +77,7 @@ type packedRow struct { RouteID int64 Cost, Credits float64 Status, Tries int32 + Masking int32 // ms (Record.Masking) Flags uint16 } @@ -224,7 +226,7 @@ func (c *rowChunk) add(r Row, msg string, order int64, failed bool) { c.Bytes += int64(len(s) + 48) return id } - p := packedRow{Time: r.Time, Tokens: [6]int64{int64(r.Input), int64(r.Output), int64(r.CacheRead), int64(r.CacheWrite), int64(r.Reasoning), int64(r.CacheWrite1h)}, Millis: r.Millis, TTFT: r.TTFT, FirstText: r.FirstText, FirstReasoning: r.FirstReasoning, Sent: r.Sent, Flow: r.Flow, Own: r.Own, Retried: r.Retried, Queued: r.Queued, Order: order, RouteID: r.RouteID, Cost: r.Cost, Status: int32(r.Status), Tries: int32(r.Tries)} + p := packedRow{Time: r.Time, Tokens: [6]int64{int64(r.Input), int64(r.Output), int64(r.CacheRead), int64(r.CacheWrite), int64(r.Reasoning), int64(r.CacheWrite1h)}, Millis: r.Millis, TTFT: r.TTFT, FirstText: r.FirstText, FirstReasoning: r.FirstReasoning, Sent: r.Sent, Flow: r.Flow, Own: r.Own, Masking: int32(min(r.Masking, math.MaxInt32)), Retried: r.Retried, Queued: r.Queued, Order: order, RouteID: r.RouteID, Cost: r.Cost, Status: int32(r.Status), Tries: int32(r.Tries)} var t [rowTexts]uint32 for i, s := range rowText(&r) { t[i] = intern(*s) @@ -291,7 +293,7 @@ func (c *rowChunk) add(r Row, msg string, order int64, failed bool) { } func (c *rowChunk) row(i int) Row { p := &c.Rows[i] - r := Row{Record: Record{RouteID: p.RouteID, Time: p.Time, Input: int(p.Tokens[0]), Output: int(p.Tokens[1]), CacheRead: int(p.Tokens[2]), CacheWrite: int(p.Tokens[3]), CacheWrite1h: int(p.Tokens[5]), Reasoning: int(p.Tokens[4]), Millis: p.Millis, TTFT: p.TTFT, FirstText: p.FirstText, FirstReasoning: p.FirstReasoning, ReasoningHidden: p.Flags&rowReasoningHidden != 0, CacheUnknown: p.Flags&rowCacheUnknown != 0, Sent: p.Sent, Flow: p.Flow, Own: p.Own, Retried: p.Retried, Queued: p.Queued, Status: int(p.Status), Tries: int(p.Tries), HandedOn: p.Flags&rowHandedOn != 0, Rejected: p.Flags&4 != 0, SessionOfficialLogin: p.Flags&8 != 0}, Cost: p.Cost, Priced: p.Flags&1 != 0, Swapped: p.Flags&2 != 0, Routed: p.Flags&32 != 0} + r := Row{Record: Record{RouteID: p.RouteID, Time: p.Time, Input: int(p.Tokens[0]), Output: int(p.Tokens[1]), CacheRead: int(p.Tokens[2]), CacheWrite: int(p.Tokens[3]), CacheWrite1h: int(p.Tokens[5]), Reasoning: int(p.Tokens[4]), Millis: p.Millis, TTFT: p.TTFT, FirstText: p.FirstText, FirstReasoning: p.FirstReasoning, ReasoningHidden: p.Flags&rowReasoningHidden != 0, CacheUnknown: p.Flags&rowCacheUnknown != 0, Sent: p.Sent, Flow: p.Flow, Own: p.Own, Masking: int64(p.Masking), Retried: p.Retried, Queued: p.Queued, Status: int(p.Status), Tries: int(p.Tries), HandedOn: p.Flags&rowHandedOn != 0, Rejected: p.Flags&4 != 0, SessionOfficialLogin: p.Flags&8 != 0}, Cost: p.Cost, Priced: p.Flags&1 != 0, Swapped: p.Flags&2 != 0, Routed: p.Flags&32 != 0} if p.Flags&rowCredits != 0 { credits := p.Credits r.Credits = &credits diff --git a/internal/usage/request_snapshot.go b/internal/usage/request_snapshot.go index 6e0f94869..4c18e9096 100644 --- a/internal/usage/request_snapshot.go +++ b/internal/usage/request_snapshot.go @@ -96,6 +96,7 @@ func (c *rowChunk) encode(keep func([]byte) bool) ([]byte, []*time.Location) { put(uint64(p.Status)) put(uint64(p.Flags)) put(uint64(p.Tries)) + put(uint64(p.Masking)) if p.Flags&rowCredits != 0 { put(math.Float64bits(p.Credits)) } @@ -285,6 +286,7 @@ func (c *rowChunk) unpackReusing(dst *rowChunk, into []packedRow, strs []string, p.FirstReasoning = int64(take()) p.Cost, p.Status, p.Flags = math.Float64frombits(take()), int32(take()), uint16(take()) p.Tries = int32(take()) + p.Masking = int32(take()) p.Credits = 0 if p.Flags&rowCredits != 0 { p.Credits = math.Float64frombits(take()) diff --git a/internal/usage/row_shape_test.go b/internal/usage/row_shape_test.go index 851debb72..3ad6b1711 100644 --- a/internal/usage/row_shape_test.go +++ b/internal/usage/row_shape_test.go @@ -21,7 +21,7 @@ func TestRowsOfOneSessionShareAShape(t *testing.T) { var want []Row for i := range 200 { r := Row{Record: Record{Time: at.Add(time.Duration(i) * time.Second), Agent: "claude", Provider: "anthropic", Model: "claude-opus-5-5", - Session: "s1", Endpoint: "/v1/messages", Input: i, Output: 2 * i, Status: 200, + Session: "s1", Endpoint: "/v1/messages", Input: i, Output: 2 * i, Own: int64(3 * i), Masking: int64(i), Status: 200, RequestID: fmt.Sprint("req", i), ResponseID: fmt.Sprint("resp", i), Archive: fmt.Sprint("a", i)}, Source: "log"} if i%7 == 0 { r.Error, r.Status = fmt.Sprint("overloaded ", i), 529 diff --git a/internal/usage/usage.go b/internal/usage/usage.go index b6adaf71f..b94e83f07 100644 --- a/internal/usage/usage.go +++ b/internal/usage/usage.go @@ -126,7 +126,12 @@ type Record struct { // sending it the body. #1532: a vendor that held a try ~100 s and // said 503 read as 104 s "In magpie". 0 in a record written before // they were, where Sent is all that is known. - Own int64 `json:"own_ms,omitempty"` + 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. + Masking int64 `json:"masking_ms,omitempty"` Retried int64 `json:"retried_ms,omitempty"` Queued int64 `json:"queued_ms,omitempty"` // Flow: the ms the reply's content took to come, from where its speed