Skip to content

Commit bfce4dd

Browse files
olddonkeyclaude
andcommitted
refactor(fastwire): land FastWire policy resolution behind byte-identical behavior
Phase A1 of the FastWire umbrella (#1886): FastWire descriptors, FastPolicyAuthority capture with a precomputed modelWireOverrideAllowed guard, the shared resolveFastPolicy() four-level adapter resolver with capability/eligibility separation, and the TierDecision state machine. Fast-mode injection no longer mutates parsed._rawBody; the Responses adapter applies the settled decision to a detached outbound body. The legacy Chat serializer gate survives as legacyChatEligibility() until the B1 migration, and no registry provider declares a descriptor yet, so outbound wire bytes and catalog bytes are unchanged. The A0 characterization suites pass unmodified except the raw-body observation test whose A0 comment scheduled this exact update. Full suite at this commit: 12970 pass / 10 skip / 0 fail. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 0cdd07d commit bfce4dd

13 files changed

Lines changed: 1055 additions & 143 deletions

File tree

‎src/adapters/openai-responses.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createHash } from "node:crypto";
22
import type { IncomingMeta, ProviderAdapter } from "./base";
3-
import { namespacedToolName, type AdapterEvent, type OcxParsedRequest, type OcxProviderConfig, type OcxUsage } from "../types";
3+
import { namespacedToolName, type AdapterEvent, type OcxParsedRequest, type OcxProviderConfig, type OcxUsage, type TierDecision } from "../types";
44
import { catalogModelSupportsReasoningSummaries } from "../codex/catalog";
55
import { COMPACT_PROMPT, decodeCompactionSummary, SUMMARY_PREFIX } from "../responses/compaction";
66
import { collectResponsesToolGroups } from "../responses/tool-groups";
@@ -764,6 +764,15 @@ function stripPreviousResponseId(body: unknown, strip: boolean): unknown {
764764
return rest;
765765
}
766766

767+
/** Apply the settled tier only to a fresh outbound object; `_rawBody` remains caller-owned. */
768+
function applyTierDecisionToResponsesBody(body: unknown, decision: TierDecision | undefined): unknown {
769+
if (!decision || decision.kind === "forward-caller" || !isPlainObject(body)) return body;
770+
const next: Record<string, unknown> = { ...body };
771+
if (decision.kind === "set") next.service_tier = decision.value;
772+
else delete next.service_tier;
773+
return next;
774+
}
775+
767776
/**
768777
* Drop request parameters a stateless Responses upstream cannot implement, and pin
769778
* `store` false.
@@ -778,8 +787,8 @@ function stripPreviousResponseId(body: unknown, strip: boolean): unknown {
778787
* `prompt` is a reference to a server-stored prompt template — the most stateful
779788
* field in the accepted schema.
780789
*
781-
* `service_tier` is deliberately NOT dropped: the server writes it for fast mode
782-
* (`responses/core.ts`), and silently deleting a configured knob inside an adapter is
790+
* `service_tier` is deliberately NOT dropped: the final TierDecision is applied to a
791+
* detached outbound body before this sanitizer chain, and silently deleting a configured knob is
783792
* worse than forwarding a parameter the upstream ignores.
784793
*
785794
* MUST run before the composed sanitize chain below: `stripItemIdsWhenUnstored` keys
@@ -1367,6 +1376,9 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig):
13671376
parsed._rawBody,
13681377
forward || parsed._previousResponseInputExpanded === true,
13691378
);
1379+
// stripPreviousResponseId() intentionally returns its input on a no-op. Detach before the
1380+
// tier write so a force-fast/default decision can never mutate parsed._rawBody.
1381+
outBody = applyTierDecisionToResponsesBody(outBody, parsed.options?.tierDecision);
13701382
const stateless = provider.statelessResponses === true;
13711383
if (stateless) outBody = stripStatefulResponsesParams(outBody);
13721384
// A replay miss can leave a function_call_output whose paired function_call sat

‎src/codex/catalog/provider-fetch.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ import { CODEX_REASONING_LEVELS, codexEffortRank, configuredReasoningEfforts, mo
3333
import { getModelMetadata, getModelMetadataCaseInsensitive, listModelMetadata, resolveMetadataProvider } from "../../generated/model-metadata";
3434
import { enrichProviderFromRegistry, shouldCaseFoldMetadataModelId } from "../../providers/derive";
3535
import {
36-
captureServiceTierAdapterAuthority,
36+
captureFastPolicyAuthority,
3737
serviceTierSupportForModel,
38-
type CapturedServiceTierAdapterAuthority,
3938
} from "../../providers/service-tier";
39+
import type { FastPolicyAuthority } from "../../providers/fastwire";
4040
import { effectiveGoogleMode, getProviderRegistryEntry, providerMatchesRegistryTransport } from "../../providers/registry";
4141
import { parseAntigravityAvailableModels } from "../../providers/antigravity-models";
4242
import { applyProviderContextCap, providerContextCap } from "../../providers/context-cap";
@@ -155,7 +155,7 @@ interface CapturedProviderGather {
155155
readonly discovery: ResolvedProviderModelDiscovery;
156156
readonly policy: CatalogProviderDiscoveryPolicySnapshot;
157157
readonly request: CapturedModelsRequest;
158-
readonly serviceTierAdapterAuthority: CapturedServiceTierAdapterAuthority;
158+
readonly fastPolicyAuthority: FastPolicyAuthority;
159159
readonly observedAuth?: ModelsAuthResolution;
160160
/**
161161
* Configured model ids this provider must keep even when live discovery omits
@@ -408,7 +408,7 @@ function captureProviderGather(
408408
const enriched = detachedClone(withCanonicalOpenAiForwardAuthDefault(name, configured));
409409
enrichProviderFromRegistry(name, enriched);
410410
const registryTransportMatch = providerMatchesRegistryTransport(name, enriched);
411-
const serviceTierAdapterAuthority = captureServiceTierAdapterAuthority(
411+
const fastPolicyAuthority = captureFastPolicyAuthority(
412412
name,
413413
enriched,
414414
registryTransportMatch,
@@ -449,7 +449,7 @@ function captureProviderGather(
449449
discovery,
450450
policy,
451451
request,
452-
serviceTierAdapterAuthority,
452+
fastPolicyAuthority,
453453
...(observedAuth ? { observedAuth: Object.freeze({ ...observedAuth }) } : {}),
454454
...(retainConfiguredModelIds && retainConfiguredModelIds.size > 0
455455
? { retainConfiguredModelIds }
@@ -518,7 +518,7 @@ function captureGatherFlight(
518518
// It is the one member of a provider row that is legitimately a function,
519519
// so it is dropped here rather than allowed to break every encode.
520520
provider: omitProviderTransportExecutor(provider.provider),
521-
serviceTierAdapterAuthority: provider.serviceTierAdapterAuthority,
521+
fastPolicyAuthority: provider.fastPolicyAuthority,
522522
// Combo retention is capture-time state, not a provider-row field. Two
523523
// gathers that share providers but differ in combo targets must not join.
524524
retainConfiguredModelIds: [...(provider.retainConfiguredModelIds ?? [])].sort(),

‎src/config.ts‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,33 @@ export function requestPacingConfigError(value: unknown): string | null {
716716
return "requestPacing must contain enabled and a valid requestsPerMinute/minIntervalMs provider rule or model overrides";
717717
}
718718

719+
const fastWireCanonicalMapSchema = z.record(
720+
z.string().trim().min(1),
721+
z.string().trim().min(1).max(64),
722+
).superRefine((mapping, ctx) => {
723+
if (!Object.prototype.hasOwnProperty.call(mapping, "priority")) {
724+
ctx.addIssue({ code: "custom", path: ["priority"], message: "canonicalToWire must include priority" });
725+
}
726+
const values = Object.values(mapping);
727+
if (new Set(values).size !== values.length) {
728+
ctx.addIssue({ code: "custom", message: "canonicalToWire values must be unique" });
729+
}
730+
});
731+
732+
const fastWireBetasSchema = z.array(z.string().trim().min(1)).max(16)
733+
.superRefine((betas, ctx) => {
734+
if (new Set(betas).size !== betas.length) {
735+
ctx.addIssue({ code: "custom", message: "betas values must be unique" });
736+
}
737+
});
738+
739+
const fastWireSchema = z.object({
740+
kind: z.enum(["service-tier", "anthropic-speed"]),
741+
canonicalToWire: fastWireCanonicalMapSchema,
742+
foreignCallerTiers: z.enum(["verbatim", "drop"]),
743+
betas: fastWireBetasSchema.optional(),
744+
}).strict();
745+
719746
/**
720747
* Zod schema for one provider entry: known fields are validated strictly while unknown
721748
* fields pass through (preserved for runtime extensions).
@@ -731,6 +758,7 @@ const providerConfigSchema = z.object({
731758
responsesPath: z.string().min(1).optional(),
732759
statelessResponses: z.boolean().optional(),
733760
requiresAdjacentResponsesToolResults: z.boolean().optional(),
761+
fastWire: fastWireSchema.nullable().optional(),
734762
supportsServiceTier: z.boolean().optional(),
735763
modelSupportsServiceTier: z.record(z.string().min(1), z.boolean()).optional(),
736764
preserveResponsesReasoningContent: z.boolean().optional(),
@@ -753,7 +781,17 @@ const providerConfigSchema = z.object({
753781
repairInvalidIds: z.boolean().optional(),
754782
}).strict().optional(),
755783
responsesSnapshotRepair: z.boolean().optional(),
756-
}).passthrough();
784+
}).passthrough().superRefine((provider, ctx) => {
785+
if (provider.fastWire !== null) return;
786+
const exactCapability = Object.values(provider.modelSupportsServiceTier ?? {}).some(value => value === true);
787+
if (provider.supportsServiceTier === true || exactCapability) {
788+
ctx.addIssue({
789+
code: "custom",
790+
path: ["fastWire"],
791+
message: "fastWire=null conflicts with supportsServiceTier=true",
792+
});
793+
}
794+
});
757795

758796
const RESERVED_PROVIDER_NAMES = new Set([
759797
// JavaScript prototype-pollution guards.
@@ -1415,6 +1453,27 @@ const configSchema = z.object({
14151453
});
14161454
}
14171455
const provider = config.providers[name];
1456+
if (provider.fastWire === null) {
1457+
const directCapability = provider.supportsServiceTier === true
1458+
|| Object.values(provider.modelSupportsServiceTier ?? {}).some(value => value === true);
1459+
const registry = providerMatchesRegistryTransport(name, provider)
1460+
? getProviderRegistryEntry(name)
1461+
: undefined;
1462+
const effectiveProviderCapability = provider.supportsServiceTier ?? registry?.supportsServiceTier;
1463+
const effectiveModelCapabilities = {
1464+
...(registry?.modelSupportsServiceTier ?? {}),
1465+
...(provider.modelSupportsServiceTier ?? {}),
1466+
};
1467+
const inheritedCapability = effectiveProviderCapability === true
1468+
|| Object.values(effectiveModelCapabilities).some(value => value === true);
1469+
if (!directCapability && inheritedCapability) {
1470+
ctx.addIssue({
1471+
code: "custom",
1472+
path: ["providers", redactSecretString(name), "fastWire"],
1473+
message: "fastWire=null conflicts with inherited supportsServiceTier=true",
1474+
});
1475+
}
1476+
}
14181477
const openRouterRoutingError = openRouterRoutingConfigError(provider);
14191478
if (openRouterRoutingError) {
14201479
ctx.addIssue({

‎src/providers/derive.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,13 @@ export function enrichProviderFromRegistry(name: string, prov: OcxProviderConfig
449449
}
450450
// Registry-only metadata (never seeded into saved config): backfill straight from
451451
// the entry so an explicit user value stays distinguishable from the default.
452+
if (prov.fastWire === undefined && entry.fastWire !== undefined) {
453+
prov.fastWire = entry.fastWire === null ? null : {
454+
...entry.fastWire,
455+
canonicalToWire: { ...entry.fastWire.canonicalToWire },
456+
...(entry.fastWire.betas ? { betas: [...entry.fastWire.betas] } : {}),
457+
};
458+
}
452459
if (prov.supportsServiceTier === undefined && entry.supportsServiceTier !== undefined) prov.supportsServiceTier = entry.supportsServiceTier;
453460
if (prov.preserveResponsesReasoningContent === undefined && entry.preserveResponsesReasoningContent !== undefined) prov.preserveResponsesReasoningContent = entry.preserveResponsesReasoningContent;
454461
applyReasoningSummaryDefaults(prov, entry.modelSupportsReasoningSummaries);

0 commit comments

Comments
 (0)