Skip to content

Commit 20ea424

Browse files
committed
refactor(usage): single-source reasoning wire-value pairing rule
isValidReasoningWireValue() now owns the value-shape invariant shared by recordAdapterReasoning and both persisted-usage normalizers, so the boolean-only-for-reasoning.enabled pairing cannot drift between the live capture path and persisted rows.
1 parent 9a5b136 commit 20ea424

2 files changed

Lines changed: 27 additions & 22 deletions

File tree

src/server/request-log.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
isKnownAdmissionKind,
1616
isKnownInboundProtocol,
1717
isKnownUsageSurface,
18+
isValidReasoningWireValue,
1819
readRecentUsageEntries,
1920
usageForFinalLog,
2021
usageStatusForFinalLog,
@@ -403,12 +404,7 @@ export function recordAdapterReasoning(
403404
&& reasoning.wireField !== "reasoning.effort"
404405
&& reasoning.wireField !== "thinking_budget"
405406
&& reasoning.wireField !== "thinking.type")
406-
|| (!(typeof reasoning.wireValue === "string" && reasoning.wireValue)
407-
&& !(typeof reasoning.wireValue === "number"
408-
&& Number.isFinite(reasoning.wireValue)
409-
&& reasoning.wireValue >= 0)
410-
&& !(reasoning.wireField === "reasoning.enabled"
411-
&& typeof reasoning.wireValue === "boolean"))) {
407+
|| !isValidReasoningWireValue(reasoning.wireField, reasoning.wireValue)) {
412408
return;
413409
}
414410

src/usage/log.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -268,17 +268,29 @@ function normalizeUsageAttempt(raw: unknown): PersistedUsageAttempt | null {
268268
...(typeof attempt.reasoningWireField === "string" && attempt.reasoningWireField
269269
? { reasoningWireField: capMetadataString(attempt.reasoningWireField) }
270270
: {}),
271-
...(typeof attempt.reasoningWireValue === "string" && attempt.reasoningWireValue
272-
? { reasoningWireValue: capMetadataString(attempt.reasoningWireValue) }
273-
: typeof attempt.reasoningWireValue === "boolean"
274-
&& attempt.reasoningWireField === "reasoning.enabled"
275-
? { reasoningWireValue: attempt.reasoningWireValue }
276-
: isNonNegativeFiniteNumber(attempt.reasoningWireValue)
277-
? { reasoningWireValue: attempt.reasoningWireValue }
278-
: {}),
271+
...(isValidReasoningWireValue(attempt.reasoningWireField, attempt.reasoningWireValue)
272+
? typeof attempt.reasoningWireValue === "string"
273+
? { reasoningWireValue: capMetadataString(attempt.reasoningWireValue) }
274+
: { reasoningWireValue: attempt.reasoningWireValue }
275+
: {}),
279276
};
280277
}
281278

279+
/**
280+
* Pairing rule for reasoning diagnostics, shared with the live request-log capture path:
281+
* a non-empty string, a non-negative finite number, or a boolean only for
282+
* `reasoning.enabled`. The field name itself is validated separately at capture time;
283+
* persisted rows may carry legacy field names, so this checks only the value shape.
284+
*/
285+
export function isValidReasoningWireValue(
286+
wireField: unknown,
287+
wireValue: unknown,
288+
): wireValue is string | number | boolean {
289+
return (typeof wireValue === "string" && wireValue.length > 0)
290+
|| (typeof wireValue === "number" && Number.isFinite(wireValue) && wireValue >= 0)
291+
|| (wireField === "reasoning.enabled" && typeof wireValue === "boolean");
292+
}
293+
282294
function normalizedAttempts(raw: unknown): PersistedUsageAttempt[] {
283295
if (!Array.isArray(raw)) return [];
284296
return raw.map(normalizeUsageAttempt)
@@ -326,14 +338,11 @@ function normalizeUsageEntry(entry: PersistedUsageEntry): PersistedUsageEntry {
326338
...(typeof entry.reasoningWireField === "string" && entry.reasoningWireField
327339
? { reasoningWireField: capMetadataString(entry.reasoningWireField) }
328340
: {}),
329-
...(typeof entry.reasoningWireValue === "string" && entry.reasoningWireValue
330-
? { reasoningWireValue: capMetadataString(entry.reasoningWireValue) }
331-
: typeof entry.reasoningWireValue === "boolean"
332-
&& entry.reasoningWireField === "reasoning.enabled"
333-
? { reasoningWireValue: entry.reasoningWireValue }
334-
: isNonNegativeFiniteNumber(entry.reasoningWireValue)
335-
? { reasoningWireValue: entry.reasoningWireValue }
336-
: {}),
341+
...(isValidReasoningWireValue(entry.reasoningWireField, entry.reasoningWireValue)
342+
? typeof entry.reasoningWireValue === "string"
343+
? { reasoningWireValue: capMetadataString(entry.reasoningWireValue) }
344+
: { reasoningWireValue: entry.reasoningWireValue }
345+
: {}),
337346
...(typeof entry.requestedServiceTier === "string" && entry.requestedServiceTier
338347
? { requestedServiceTier: capMetadataString(entry.requestedServiceTier) }
339348
: {}),

0 commit comments

Comments
 (0)