Skip to content

Commit 41d6ab0

Browse files
committed
fix(usage): route malformed-counter diagnostics to a production-active sink
What: replace the `logger` import in chat-usage-normalization.ts with a module-local `CompactLogger` instance, and guard the emit so a sink failure cannot escape into usage accounting. Rewrite the spec cases to spy on `CompactLogger.prototype.warn`, and add two cases: one that simulates the production branch (NODE_ENV=production) and asserts the warning still reaches a real logger, and one that asserts a throwing sink does not abort accounting. Why: CodeRabbit thread r4426014105 (and reviewer F-01). The exported `logger` is `process.env.NODE_ENV === "test" ? new CompactLogger() : noopLogger`, so every `logger.warn` in production is discarded. The hardening added for the mlarge P2 finding therefore promised a diagnostic that no real install would ever emit. The sink is now active regardless of NODE_ENV. Impact: malformed-counter warnings are actually emitted in production, once per alias, with the same dedupe semantics as before. Valid-shape behavior is unchanged: all pre-existing helper cases pass unmodified. A broken log sink degrades to silence rather than surfacing as a usage failure. Disclosed separately, NOT fixed here: `src/utils/logging/index.ts:25` selects CompactLogger only when NODE_ENV === "test", which is the inverse of its own docstring ("Uses CompactLogger for normal operation, switches to noop logger in Jest test environment"). Correcting that ternary would change logging for every extension call site, so it is reported as a pre-existing repo-wide finding rather than widened into this PR. Refs: #2254 (comment)
1 parent b0567c5 commit 41d6ab0

2 files changed

Lines changed: 83 additions & 10 deletions

File tree

‎src/api/transform/__tests__/chat-usage-normalization.spec.ts‎

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// npx vitest run api/transform/__tests__/chat-usage-normalization.spec.ts
22

3-
import { logger } from "../../../utils/logging"
3+
import { CompactLogger } from "../../../utils/logging/CompactLogger"
44

55
import { normalizeChatUsage } from "../chat-usage-normalization"
66

@@ -180,9 +180,9 @@ describe("normalizeChatUsage", () => {
180180
// A fresh module instance keeps this case independent of the dedupe
181181
// state that earlier cases in this file have already populated.
182182
vi.resetModules()
183-
const { logger: freshLogger } = await import("../../../utils/logging")
183+
const { CompactLogger: FreshCompactLogger } = await import("../../../utils/logging/CompactLogger")
184184
const { normalizeChatUsage: freshNormalize } = await import("../chat-usage-normalization")
185-
const warn = vi.spyOn(freshLogger, "warn").mockImplementation(() => {})
185+
const warn = vi.spyOn(FreshCompactLogger.prototype, "warn").mockImplementation(() => {})
186186

187187
try {
188188
freshNormalize({ prompt_tokens: 120, prompt_cache_miss_tokens: "30" })
@@ -196,11 +196,60 @@ describe("normalizeChatUsage", () => {
196196
}
197197
})
198198

199+
it("emits through a sink that is active outside tests", async () => {
200+
// The shared `logger` export is a no-op unless NODE_ENV is "test", so a
201+
// diagnostic routed through it is discarded in every real install. This
202+
// case simulates the production branch and asserts the warning still
203+
// reaches a real logger.
204+
const originalNodeEnv = process.env.NODE_ENV
205+
process.env.NODE_ENV = "production"
206+
vi.resetModules()
207+
const { CompactLogger: FreshCompactLogger } = await import("../../../utils/logging/CompactLogger")
208+
const warn = vi.spyOn(FreshCompactLogger.prototype, "warn").mockImplementation(() => {})
209+
210+
try {
211+
const { normalizeChatUsage: freshNormalize } = await import("../chat-usage-normalization")
212+
213+
freshNormalize({ prompt_tokens: 120, prompt_cache_miss_tokens: "30" })
214+
215+
expect(warn).toHaveBeenCalledWith(expect.stringContaining("malformed token counter"), {
216+
path: "prompt_cache_miss_tokens",
217+
value: "30",
218+
})
219+
} finally {
220+
warn.mockRestore()
221+
process.env.NODE_ENV = originalNodeEnv
222+
}
223+
})
224+
225+
it("does not abort usage accounting when the sink itself fails", async () => {
226+
vi.resetModules()
227+
const { CompactLogger: FreshCompactLogger } = await import("../../../utils/logging/CompactLogger")
228+
const { normalizeChatUsage: freshNormalize } = await import("../chat-usage-normalization")
229+
const warn = vi.spyOn(FreshCompactLogger.prototype, "warn").mockImplementation(() => {
230+
throw new Error("log file unwritable")
231+
})
232+
233+
try {
234+
// The malformed counter is still discarded and the valid alias still
235+
// resolves; a broken log sink must not surface as a usage failure.
236+
expect(
237+
freshNormalize({
238+
prompt_tokens: 120,
239+
prompt_cache_miss_tokens: "30",
240+
prompt_tokens_details: { cache_miss_tokens: 20 },
241+
})?.cacheWriteTokens,
242+
).toBe(20)
243+
} finally {
244+
warn.mockRestore()
245+
}
246+
})
247+
199248
it("reports a malformed counter once per alias rather than on every chunk", async () => {
200249
vi.resetModules()
201-
const { logger: freshLogger } = await import("../../../utils/logging")
250+
const { CompactLogger: FreshCompactLogger } = await import("../../../utils/logging/CompactLogger")
202251
const { normalizeChatUsage: freshNormalize } = await import("../chat-usage-normalization")
203-
const warn = vi.spyOn(freshLogger, "warn").mockImplementation(() => {})
252+
const warn = vi.spyOn(FreshCompactLogger.prototype, "warn").mockImplementation(() => {})
204253

205254
try {
206255
freshNormalize({ prompt_tokens: 120, prompt_cache_miss_tokens: "30" })
@@ -215,9 +264,9 @@ describe("normalizeChatUsage", () => {
215264

216265
it("names the exact field that was discarded", async () => {
217266
vi.resetModules()
218-
const { logger: freshLogger } = await import("../../../utils/logging")
267+
const { CompactLogger: FreshCompactLogger } = await import("../../../utils/logging/CompactLogger")
219268
const { normalizeChatUsage: freshNormalize } = await import("../chat-usage-normalization")
220-
const warn = vi.spyOn(freshLogger, "warn").mockImplementation(() => {})
269+
const warn = vi.spyOn(FreshCompactLogger.prototype, "warn").mockImplementation(() => {})
221270

222271
try {
223272
freshNormalize({ prompt_tokens: "100", completion_tokens: -1 })
@@ -236,7 +285,7 @@ describe("normalizeChatUsage", () => {
236285
})
237286

238287
it("stays silent for counters the provider simply did not report", () => {
239-
const warn = vi.spyOn(logger, "warn").mockImplementation(() => {})
288+
const warn = vi.spyOn(CompactLogger.prototype, "warn").mockImplementation(() => {})
240289

241290
try {
242291
normalizeChatUsage({ prompt_tokens: 100, completion_tokens: 50 })

‎src/api/transform/chat-usage-normalization.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* tokens twice, so adding them would double-count.
1212
*/
1313

14-
import { logger } from "../../utils/logging"
14+
import { CompactLogger } from "../../utils/logging/CompactLogger"
1515

1616
/**
1717
* A usage object as it arrives off the wire, before any provider-specific
@@ -101,6 +101,30 @@ const readPath = (usage: ChatUsageLike, path: string): unknown => {
101101
const isTokenCount = (value: unknown): value is number =>
102102
typeof value === "number" && Number.isInteger(value) && value >= 0
103103

104+
/**
105+
* Diagnostics sink for malformed counters.
106+
*
107+
* The shared `logger` export in `utils/logging` is a no-op outside tests
108+
* (`utils/logging/index.ts:25` selects `CompactLogger` only when
109+
* `NODE_ENV === "test"`), so routing a production diagnostic through it would
110+
* discard the message in every real install. This module owns its own
111+
* `CompactLogger` instead, which is active regardless of `NODE_ENV`.
112+
*
113+
* `CompactLogger`'s constructor only assigns fields, so constructing it at
114+
* module scope has no side effects. Its `write()` can throw while creating the
115+
* log file, and this runs inside live usage accounting, so emission is
116+
* guarded: a logging failure must never break billing or abort a stream.
117+
*/
118+
const usageLogger = new CompactLogger()
119+
120+
const emitMalformedCountWarning = (path: string, value: unknown): void => {
121+
try {
122+
usageLogger.warn("Discarded a malformed token counter while normalizing chat usage", { path, value })
123+
} catch {
124+
// Diagnostics are best-effort; never let a sink failure escape into usage accounting.
125+
}
126+
}
127+
104128
/**
105129
* A counter that is absent or null was simply not reported, which is a normal
106130
* shape and resolves to `undefined` silently. A counter that is present but
@@ -113,7 +137,7 @@ const isTokenCount = (value: unknown): value is number =>
113137
const warnMalformedCount = (path: string, value: unknown): void => {
114138
if (warnedMalformedCounters.has(path)) return
115139
warnedMalformedCounters.add(path)
116-
logger.warn("Discarded a malformed token counter while normalizing chat usage", { path, value })
140+
emitMalformedCountWarning(path, value)
117141
}
118142

119143
const warnedMalformedCounters = new Set<string>()

0 commit comments

Comments
 (0)