Skip to content

Commit 4d87bce

Browse files
committed
fix(fastwire): address B0 follow-up findings
1 parent ef4e768 commit 4d87bce

8 files changed

Lines changed: 136 additions & 19 deletions

File tree

‎src/adapters/base.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ export interface AdapterRequest {
7171
wireField: "reasoning_effort" | "reasoning.effort" | "thinking.type";
7272
wireValue: string;
7373
};
74-
/** Exact tier outcome seeded after this adapter serialized the outbound request. */
74+
/**
75+
* Exact tier outcome seeded after this adapter serialized the outbound request.
76+
* This is a live shared observer: response-phase methods mutate `outcome`, so retain
77+
* the reference rather than cloning or snapshotting it.
78+
*/
7579
tierLog?: AdapterTierMetadata;
7680
usageLog?: {
7781
inputTokens?: number;

‎src/config.ts‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -769,15 +769,7 @@ const providerConfigSchema = z.object({
769769
repairInvalidIds: z.boolean().optional(),
770770
}).strict().optional(),
771771
responsesSnapshotRepair: z.boolean().optional(),
772-
}).passthrough().superRefine((provider, ctx) => {
773-
if (hasFastWireCapabilityConflict(provider)) {
774-
ctx.addIssue({
775-
code: "custom",
776-
path: ["fastWire"],
777-
message: "fastWire=null conflicts with supportsServiceTier=true",
778-
});
779-
}
780-
});
772+
}).passthrough();
781773

782774
const RESERVED_PROVIDER_NAMES = new Set([
783775
// JavaScript prototype-pollution guards.
@@ -1439,6 +1431,13 @@ const configSchema = z.object({
14391431
});
14401432
}
14411433
const provider = config.providers[name];
1434+
if (hasFastWireCapabilityConflict(provider)) {
1435+
ctx.addIssue({
1436+
code: "custom",
1437+
path: ["providers", redactSecretString(name), "fastWire"],
1438+
message: "fastWire=null conflicts with supportsServiceTier=true",
1439+
});
1440+
}
14421441
const openRouterRoutingError = openRouterRoutingConfigError(provider);
14431442
if (openRouterRoutingError) {
14441443
ctx.addIssue({
@@ -2181,7 +2180,8 @@ function warnDegradedNativeSubagentConfig(rawParsed: unknown, config: OcxConfig)
21812180
* Registry metadata can gain service-tier capability after a config was written. An explicit
21822181
* `fastWire: null` remains authoritative on load and on whole-document writes; rejecting either
21832182
* would discard or lock access to unrelated providers and API keys. Direct contradictions within
2184-
* one provider row remain schema errors through providerConfigSchema.
2183+
* one provider row remain schema errors through the outer config refinement, where the dynamic
2184+
* provider name can be redacted before it reaches diagnostics.
21852185
*/
21862186
function inheritedFastWireConflictProviderNames(
21872187
config: Pick<OcxConfig, "providers">,

‎src/lib/redact.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,9 @@ export function redactSecretString(value: string): string {
446446
/** Shared bounded representation for caller-controlled scalar metadata stored in logs. */
447447
export function sanitizeLogMetadataString(value: unknown, maxLength = 64): string | undefined {
448448
if (typeof value !== "string" || !Number.isInteger(maxLength) || maxLength < 1) return undefined;
449-
const filtered = value.trim().replace(/[\u0000-\u001f\u007f]/g, "");
449+
// Remove every control/line-separator code point that common terminals and log viewers
450+
// can render as a record boundary before the value reaches a single-line log field.
451+
const filtered = value.trim().replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/g, "");
450452
if (!filtered) return undefined;
451453
const redacted = redactSecretString(filtered).trim();
452454
return redacted ? redacted.slice(0, maxLength) : undefined;

‎src/server/responses/core.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,8 +987,8 @@ const MAX_FAST_WIRE_CAPABILITY_WARNINGS = 256;
987987
const warnedFastWireCapabilityGaps = new Set<string>();
988988

989989
function warnFastWireCapabilityGap(providerName: string, modelId: string): void {
990-
const safeProvider = redactSecretString(providerName);
991-
const safeModel = redactSecretString(modelId);
990+
const safeProvider = sanitizeLogMetadataString(providerName) ?? "unknown";
991+
const safeModel = sanitizeLogMetadataString(modelId) ?? "unknown";
992992
const key = `${safeProvider}\0${safeModel}`;
993993
if (warnedFastWireCapabilityGaps.has(key)) return;
994994
if (warnedFastWireCapabilityGaps.size >= MAX_FAST_WIRE_CAPABILITY_WARNINGS) {

‎src/usage/cost.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,16 @@ export function serviceTierContextFromOutcome(outcome: AttemptTierOutcome): Serv
369369
if (outcome.canonical === "priority" && outcome.confirmation === "assumed") {
370370
return { requestedServiceTier: "priority" };
371371
}
372+
// An unclassified route makes no canonical Fast claim, but its adapter can still prove that
373+
// it serialized a caller tier. Preserve that wire evidence instead of discarding the legacy
374+
// top-level pricing signal merely because B0 added an outcome row.
375+
if (
376+
outcome.fastOutcome === "unknown"
377+
&& outcome.wireKind === "service-tier"
378+
&& typeof outcome.wireValue === "string"
379+
) {
380+
return { requestedServiceTier: outcome.wireValue };
381+
}
372382
return {};
373383
}
374384

‎tests/fastwire-characterization-wire.test.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,23 @@ describe("FastWire characterization: supported-route fastMode tri-state", () =>
103103

104104
test("a capability-without-wire warning is redacted and throttled per provider/model", async () => {
105105
const providerName = `sk-ant-api03-${"A".repeat(40)}`;
106+
const model = `model\n${"x".repeat(100)}`;
106107
const warnSpy = spyOn(console, "warn").mockImplementation(() => {});
107108
const provider: OcxProviderConfig = {
108109
...supportedResponsesProvider(),
109110
fastWire: null,
110111
};
111112

112113
try {
113-
await driveResponses({ provider, providerName, callerTier: "flex" });
114-
await driveResponses({ provider, providerName, callerTier: "flex" });
114+
await driveResponses({ provider, providerName, model, callerTier: "flex" });
115+
await driveResponses({ provider, providerName, model, callerTier: "flex" });
115116
const fastWireWarnings = warnSpy.mock.calls
116117
.map(call => String(call[0]))
117118
.filter(message => message.includes("Fast policy"));
118119
expect(fastWireWarnings).toHaveLength(1);
119120
expect(fastWireWarnings[0]).not.toContain(providerName);
121+
expect(fastWireWarnings[0]).not.toContain("\n");
122+
expect(fastWireWarnings[0]).not.toContain("x".repeat(65));
120123
} finally {
121124
warnSpy.mockRestore();
122125
}

‎tests/fastwire-observability.test.ts‎

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
addFinalRequestLog,
1313
applyResponseLogMetadata,
1414
beginRequestAttempt,
15+
inspectResponseLogJson,
16+
inspectResponseLogSsePayloadParsed,
1517
recordAdapterTier,
1618
type RequestLogContext,
1719
type RequestLogEntry,
@@ -245,6 +247,54 @@ describe("FastWire logging and persistence", () => {
245247
expect(logged?.tierOutcome).toEqual(logged?.attempts?.[0]?.tierOutcome);
246248
});
247249

250+
test.each([
251+
{
252+
label: "JSON",
253+
inspect: (logCtx: RequestLogContext) => inspectResponseLogJson(logCtx, "not-json"),
254+
},
255+
{
256+
label: "SSE",
257+
inspect: (logCtx: RequestLogContext) => {
258+
inspectResponseLogSsePayloadParsed(logCtx, "not-json", undefined);
259+
},
260+
},
261+
])("$label inspection marks an unparseable response outcome unknown", ({ inspect }) => {
262+
const tracker = createAdapterTierMetadata(
263+
observation(),
264+
{ kind: "set", value: "priority" },
265+
"service-tier",
266+
"priority",
267+
)!;
268+
const attempt = beginRequestAttempt(1, "openai", "gpt-5.6-sol", "openai-responses");
269+
const logCtx: RequestLogContext = {
270+
model: "gpt-5.6-sol",
271+
provider: "openai",
272+
activeAttempt: attempt,
273+
activeAttemptStartedAt: Date.now(),
274+
attempts: [attempt],
275+
};
276+
recordAdapterTier(logCtx, {
277+
url: "https://example.test/v1/responses",
278+
method: "POST",
279+
headers: {},
280+
body: "{}",
281+
tierLog: tracker,
282+
} satisfies AdapterRequest);
283+
expect(attempt.tierOutcome).toMatchObject({
284+
canonical: "priority",
285+
fastOutcome: "applied",
286+
confirmation: "assumed",
287+
});
288+
289+
inspect(logCtx);
290+
291+
expect(attempt.tierOutcome).toMatchObject({
292+
fastOutcome: "unknown",
293+
confirmation: "unknown",
294+
});
295+
expect(attempt.tierOutcome).not.toHaveProperty("canonical");
296+
});
297+
248298
test("old attempts remain valid and new outcomes survive normalization", () => {
249299
const oldAttempt = {
250300
ordinal: 1,
@@ -287,10 +337,12 @@ describe("FastWire logging and persistence", () => {
287337
});
288338

289339
test("callerServiceTier is trimmed, control-filtered, redacted, and capped", () => {
290-
const secret = "sk-proj-abcdefghijklmnopqrstuvwxyz0123456789";
291-
const sanitized = sanitizeLogMetadataString(` \u0000authorization: Bearer ${secret}\n${"x".repeat(80)} `);
340+
const secret = ["sk", "proj", "abcdefghijklmnopqrstuvwxyz0123456789"].join("-");
341+
const sanitized = sanitizeLogMetadataString(
342+
` \u0000authorization: Bearer ${secret}\n\u0085\u2028\u2029${"x".repeat(80)} `,
343+
);
292344
expect(sanitized).not.toContain(secret);
293-
expect(sanitized).not.toMatch(/[\u0000-\u001f\u007f]/);
345+
expect(sanitized).not.toMatch(/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/);
294346
expect(sanitized?.length).toBeLessThanOrEqual(64);
295347

296348
const normalized = normalizeUsageEntryForTest({
@@ -318,6 +370,31 @@ describe("FastWire per-attempt cost", () => {
318370
}];
319371
const usage = { inputTokens: 200_000, outputTokens: 20_000 };
320372

373+
test("an unknown unclassified outcome prices from the serialized caller tier", () => {
374+
const outcome = {
375+
wireKind: "service-tier" as const,
376+
wireValue: "priority",
377+
fastOutcome: "unknown" as const,
378+
confirmation: "unknown" as const,
379+
};
380+
expect(serviceTierContextFromOutcome(outcome)).toEqual({
381+
requestedServiceTier: "priority",
382+
});
383+
384+
const estimate = estimateComboCost([
385+
{
386+
ordinal: 1,
387+
provider: "openai",
388+
model: "gpt-5.6-sol",
389+
usageStatus: "reported",
390+
usage,
391+
tierOutcome: outcome,
392+
},
393+
], overlays, { requestedServiceTier: "priority" })!;
394+
expect(estimate.priorityMultiplier).toBe(2);
395+
expect(estimate.cost.total).toBeCloseTo(3.2, 9);
396+
});
397+
321398
test("combo prices each attempt from its own outcome before the top-level tier", () => {
322399
const attempts = [
323400
{

‎tests/fastwire-policy.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,27 @@ describe("FastWire config and registry validation", () => {
480480
expect(validateConfigCandidate(configWithFastWire(null, capability)).ok).toBe(false);
481481
});
482482

483+
test("redacts a token-shaped provider name in a FastWire conflict path", () => {
484+
const providerName = ["sk", "proj", "fastwire", "A".repeat(40)].join("-");
485+
const result = validateConfigCandidate({
486+
port: 10100,
487+
defaultProvider: providerName,
488+
providers: {
489+
[providerName]: {
490+
adapter: "openai-responses",
491+
baseUrl: "https://fixture.example/v1",
492+
supportsServiceTier: true,
493+
fastWire: null,
494+
},
495+
},
496+
});
497+
expect(result.ok).toBe(false);
498+
if (!result.ok) {
499+
expect(result.error).not.toContain(providerName);
500+
expect(result.error).toContain("providers.[REDACTED].fastWire");
501+
}
502+
});
503+
483504
test("provider-level false keeps null valid even with an exact-model true", () => {
484505
expect(validateConfigCandidate(configWithFastWire(null, { provider: false, exact: true })).ok)
485506
.toBe(true);

0 commit comments

Comments
 (0)