Skip to content

Commit 7cd270d

Browse files
committed
fix(xai): stream OAuth Grok through Responses
OAuth xai/grok-4.5 and grok-4.6 Codex /v1/responses traffic still used the provider-wide openai-chat adapter, while the official Grok CLI catalog declares api_backend: "responses". Chat Completions compatibility holds the stream until the reasoning turn finishes, so Codex sat blank until the turn was effectively done. Declare the Responses wire default for those two models, scoped to OAuth and to responses-shaped inbound traffic. API-key xAI, Chat/Anthropic translation, other Grok models, and any explicit modelAdapters override all stay on Chat. Native Responses returns before the generic recovery loop, so the OAuth 401 replay never ran on this path. Add the equivalent one-shot: refresh once, rebuild the provider and adapter, replay once. It is a single branch rather than a loop, so a second 401 cannot refresh again. Refresh failures go through the existing public OAuth error projector, which the tests pin against path canaries. Carries @olddonkey's #2104 unchanged. Closes #1886
1 parent cd8f9b8 commit 7cd270d

9 files changed

Lines changed: 515 additions & 33 deletions

‎src/providers/fastwire.ts‎

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
} from "../types";
88
import { MODEL_ADAPTER_OVERRIDE_ALLOWED } from "../types";
99
import { sanitizeLogMetadataString } from "../lib/redact";
10-
import type { InboundWire, ModelWireDefault } from "./registry";
10+
import type { InboundWire, ModelWireDefault, ProviderAuthKind } from "./registry";
1111

1212
const SERVICE_TIER_ADAPTERS = new Set(["openai-chat", "openai-responses"]);
1313
const FAST_WIRE_ADAPTERS: Readonly<Record<FastWire["kind"], ReadonlySet<string>>> = {
@@ -31,6 +31,7 @@ export type FastPolicyAuthTransport =
3131

3232
export interface FastPolicyAuthority {
3333
readonly providerAdapter: string;
34+
readonly providerAuthMode?: ProviderAuthKind;
3435
readonly fastWireDeclaration: FastWire | null | undefined;
3536
readonly modelWireOverrideAllowed: boolean;
3637
readonly authTransport: FastPolicyAuthTransport;
@@ -114,21 +115,33 @@ function registryDefaultForModel(
114115
defaults: Readonly<Record<string, ModelWireDefault>>,
115116
modelId: string,
116117
inbound: InboundWire,
117-
): string | undefined {
118+
authMode: ProviderAuthKind | undefined,
119+
): { adapter: string; forwardCallerServiceTier?: boolean } | undefined {
118120
const normalizedModelId = modelId.trim().toLowerCase();
119121
if (!Object.hasOwn(defaults, normalizedModelId)) return undefined;
120122
const declared = defaults[normalizedModelId];
121123
if (declared === undefined) return undefined;
122-
if (typeof declared !== "string" && !declared.inbound.includes(inbound)) return undefined;
124+
if (typeof declared !== "string") {
125+
if (!declared.inbound.includes(inbound)) return undefined;
126+
if (declared.authModes && (authMode === undefined || !declared.authModes.includes(authMode))) {
127+
return undefined;
128+
}
129+
}
123130
const wire = typeof declared === "string" ? declared : declared.wire;
124-
return MODEL_ADAPTER_OVERRIDE_ALLOWED.has(wire) ? wire : undefined;
131+
if (!MODEL_ADAPTER_OVERRIDE_ALLOWED.has(wire)) return undefined;
132+
return {
133+
adapter: wire,
134+
...(typeof declared !== "string" && declared.forwardCallerServiceTier !== undefined
135+
? { forwardCallerServiceTier: declared.forwardCallerServiceTier }
136+
: {}),
137+
};
125138
}
126139

127140
function resolvePolicyAdapter(
128141
authority: FastPolicyAuthority,
129142
modelId: string,
130143
inbound: InboundWire,
131-
): { adapter: string; hardPinned: boolean } {
144+
): { adapter: string; hardPinned: boolean; forwardCallerServiceTier?: boolean } {
132145
// Hard pins and configured overrides deliberately use the same exact-key semantics as
133146
// resolveWireProtocolOverride(). Registry defaults alone normalize ids at their boundary.
134147
const hardPin = Object.hasOwn(authority.hardPins, modelId)
@@ -143,8 +156,21 @@ function resolvePolicyAdapter(
143156
return { adapter: configured, hardPinned: false };
144157
}
145158
if (MODEL_ADAPTER_OVERRIDE_ALLOWED.has(authority.providerAdapter)) {
146-
const registryDefault = registryDefaultForModel(authority.registryWireDefaults, modelId, inbound);
147-
if (registryDefault !== undefined) return { adapter: registryDefault, hardPinned: false };
159+
const registryDefault = registryDefaultForModel(
160+
authority.registryWireDefaults,
161+
modelId,
162+
inbound,
163+
authority.providerAuthMode,
164+
);
165+
if (registryDefault !== undefined) {
166+
return {
167+
adapter: registryDefault.adapter,
168+
hardPinned: false,
169+
...(registryDefault.forwardCallerServiceTier !== undefined
170+
? { forwardCallerServiceTier: registryDefault.forwardCallerServiceTier }
171+
: {}),
172+
};
173+
}
148174
}
149175
}
150176
return { adapter: authority.providerAdapter, hardPinned: false };
@@ -155,7 +181,11 @@ export function resolveFastPolicy(
155181
modelId: string,
156182
inbound: InboundWire = "responses",
157183
): ResolvedFastPolicy {
158-
const { adapter, hardPinned } = resolvePolicyAdapter(authority, modelId, inbound);
184+
const { adapter, hardPinned, forwardCallerServiceTier } = resolvePolicyAdapter(
185+
authority,
186+
modelId,
187+
inbound,
188+
);
159189
const exactCapability = exactModelValue(authority.capability.models, modelId);
160190
const capability = authority.capability.provider === false
161191
? false
@@ -173,6 +203,7 @@ export function resolveFastPolicy(
173203
// tier still needs the final wire's forwarding permission.
174204
const forwardCallerTier = capability !== false
175205
&& callerWireAvailable
206+
&& forwardCallerServiceTier !== false
176207
&& (adapter !== "openai-chat" || authority.capability.chatServiceTier === true);
177208

178209
let eligibility: ResolvedFastPolicy["eligibility"];

‎src/providers/registry.ts‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,15 @@ export type InboundWire = "responses" | "chat" | "anthropic";
3131

3232
/**
3333
* A per-model wire default: a bare string applies to every inbound, while the object
34-
* form applies only to the listed inbound protocols.
34+
* form may scope the default to listed inbound protocols and authentication modes.
3535
*/
36-
export type ModelWireDefault = string | { wire: string; inbound: readonly InboundWire[] };
36+
export type ModelWireDefault = string | {
37+
wire: string;
38+
inbound: readonly InboundWire[];
39+
authModes?: readonly ProviderAuthKind[];
40+
/** Whether this registry-selected route may relay a caller-owned service_tier. */
41+
forwardCallerServiceTier?: boolean;
42+
};
3743

3844
export interface ResponsesTerminalRepairPolicy {
3945
/** Quiet time after a structurally complete output graph before synthesizing completion. */
@@ -1017,6 +1023,24 @@ export const PROVIDER_REGISTRY: readonly ProviderRegistryEntry[] = [
10171023
// grok-4.5; the reasoning ladder does not — 4.6 adds the documented xhigh rung.
10181024
models: ["grok-4.6", "grok-4.5", "grok-4.3", "grok-4.20-0309-reasoning", "grok-4.20-0309-non-reasoning", "grok-build-0.1", "grok-composer-2.5-fast"],
10191025
defaultModel: "grok-4.5",
1026+
// The current Grok CLI catalog declares both subscription models as native Responses
1027+
// backends. Keep API-key and translated Chat/Anthropic callers on their existing wire;
1028+
// Codex Responses traffic can relay xAI's SSE as it arrives instead of waiting for the
1029+
// Chat Completions compatibility stream to flush at the end of a reasoning turn.
1030+
modelWireDefaults: {
1031+
"grok-4.6": {
1032+
wire: "openai-responses",
1033+
inbound: ["responses"],
1034+
authModes: ["oauth"],
1035+
forwardCallerServiceTier: false,
1036+
},
1037+
"grok-4.5": {
1038+
wire: "openai-responses",
1039+
inbound: ["responses"],
1040+
authModes: ["oauth"],
1041+
forwardCallerServiceTier: false,
1042+
},
1043+
},
10201044
// Vision lineup per docs.x.ai model-capabilities/images/understanding: the grok-4.x chat
10211045
// models accept image input (JPEG/PNG, URL or base64). Without this the catalog leaves
10221046
// inputModalities undefined, and deriveComboCatalogModel defaults an undefined member to
@@ -2679,8 +2703,12 @@ export function providerModelWireDefault(
26792703
if (!entry?.modelWireDefaults || !providerMatchesRegistryTransport(id, provider)) return undefined;
26802704
const declared = entry.modelWireDefaults[modelId.trim().toLowerCase()];
26812705
if (declared === undefined) return undefined;
2682-
// A bare string applies to every inbound; the object form only to the listed ones.
2683-
if (typeof declared !== "string" && !declared.inbound.includes(inbound)) return undefined;
2706+
// A bare string applies to every inbound/auth mode; the object form may narrow either.
2707+
if (typeof declared !== "string") {
2708+
if (!declared.inbound.includes(inbound)) return undefined;
2709+
const authMode = provider.authMode ?? entry.authKind;
2710+
if (declared.authModes && !declared.authModes.includes(authMode)) return undefined;
2711+
}
26842712
const wire = typeof declared === "string" ? declared : declared.wire;
26852713
return wire !== undefined && allowedWires.has(wire) ? wire : undefined;
26862714
}

‎src/providers/service-tier.ts‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,16 @@ function cloneRegistryWireDefaults(
4545
for (const [modelId, declaration] of Object.entries(defaults)) {
4646
clone[modelId.trim().toLowerCase()] = typeof declaration === "string"
4747
? declaration
48-
: Object.freeze({ wire: declaration.wire, inbound: Object.freeze([...declaration.inbound]) });
48+
: Object.freeze({
49+
wire: declaration.wire,
50+
inbound: Object.freeze([...declaration.inbound]),
51+
...(declaration.authModes
52+
? { authModes: Object.freeze([...declaration.authModes]) }
53+
: {}),
54+
...(declaration.forwardCallerServiceTier !== undefined
55+
? { forwardCallerServiceTier: declaration.forwardCallerServiceTier }
56+
: {}),
57+
});
4958
}
5059
return Object.freeze(clone);
5160
}
@@ -68,6 +77,7 @@ function buildFastPolicyAuthority(
6877
const providerCapability = capabilityProvider.supportsServiceTier ?? registry?.supportsServiceTier;
6978
const authority: FastPolicyAuthority = Object.freeze({
7079
providerAdapter: provider.adapter,
80+
providerAuthMode: provider.authMode ?? registry?.authKind ?? "key",
7181
fastWireDeclaration: cloneFastWire(
7282
provider.fastWire !== undefined ? provider.fastWire : registry?.fastWire,
7383
{ freeze: true },
@@ -241,13 +251,11 @@ export function serviceTierSupportFromPolicy(
241251
): boolean | undefined {
242252
if (policy.eligibility === "eligible") return true;
243253
if (policy.eligibility === "unclassified") {
244-
// B1 regression guard: an unclassified chat-wire route whose final adapter will not
245-
// forward any tier cannot serialize service_tier, so projecting "unknown" would let
246-
// require.serviceTier: "unsupported" routing stop matching groq/ollama-class providers
247-
// that main projected as false. Chat + no forwarding stays a definitive false; a
248-
// chat route with chatServiceTier: true (forwarding allowed) keeps the historical
249-
// unknown, as does every unclassified Responses-wire route.
250-
if (policy.adapter === "openai-chat" && !policy.forwardCallerTier) return false;
254+
// An unclassified route that cannot forward a caller tier has definitive negative
255+
// evidence even when its adapter can normally serialize service_tier. This covers both
256+
// Chat routes without chatServiceTier and a registry default that explicitly closes a
257+
// subscription gateway. Generic unclassified Responses routes still project unknown.
258+
if (!policy.forwardCallerTier) return false;
251259
return undefined;
252260
}
253261
return false;

‎src/server/responses/core.ts‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,6 +2622,98 @@ async function handleResponsesInner(
26222622
request.releaseBodyObservation?.();
26232623
}
26242624

2625+
// Native Responses providers return before the generic adapter recovery loop below. Keep
2626+
// their OAuth contract identical: one pre-stream 401 forces a credential refresh and one
2627+
// rebuilt replay. xAI's current subscription models use this branch now that their official
2628+
// Grok CLI catalog declares the Responses backend.
2629+
if (upstreamResponse.status === 401 && isOAuth401ReplayProvider && sentOAuthSnapshot) {
2630+
try { void upstreamResponse.body?.cancel().catch(() => {}); } catch { /* already consumed/closed */ }
2631+
let refreshed: OAuthAccessSnapshot;
2632+
try {
2633+
refreshed = await forceRefreshOAuthAccessSnapshot(sentOAuthSnapshot);
2634+
} catch (err) {
2635+
upstream.abort();
2636+
releaseCodexAuthContextProbeLease(authCtx);
2637+
return formatErrorResponse(401, "authentication_error", publicOAuthAuthenticationErrorMessage(err));
2638+
}
2639+
sentOAuthSnapshot = refreshed;
2640+
replayOAuthCredentialSnapshot = {
2641+
accountId: refreshed.accountId,
2642+
generation: refreshed.generation,
2643+
};
2644+
if (route.providerName === "kiro") {
2645+
parsed._kiroAuthContext = { ...(refreshed.kiro ?? {}) };
2646+
}
2647+
const refreshedProvider = resolveProviderTransport(
2648+
route.providerName,
2649+
{ ...route.provider, apiKey: refreshed.accessToken },
2650+
parsed.options.promptCacheKey,
2651+
route.providerName === "github-copilot" ? getOAuthCredentialApiBaseUrl(route.providerName) : undefined,
2652+
);
2653+
route.provider = refreshedProvider;
2654+
const refreshedAdapter = resolveAdapter(
2655+
resolveWireProtocolOverride(route.providerName, route.modelId, refreshedProvider, inboundWire),
2656+
config.cacheRetention,
2657+
);
2658+
if (!("passthrough" in refreshedAdapter) || !refreshedAdapter.passthrough) {
2659+
upstream.abort();
2660+
return formatErrorResponse(502, "upstream_error", "OAuth refresh changed the provider wire unexpectedly");
2661+
}
2662+
bindRouteReasoningReplayScope({
2663+
parsed,
2664+
providerName: route.providerName,
2665+
provider: refreshedProvider,
2666+
adapterName: refreshedAdapter.name,
2667+
oauthCredentialSnapshot: replayOAuthCredentialSnapshot,
2668+
});
2669+
logCtx.providerAdapter = refreshedAdapter.name;
2670+
sealRequestAttemptIdentity(
2671+
logCtx.activeAttempt,
2672+
logCtx.provider,
2673+
refreshedAdapter.name,
2674+
logCtx.accountLogLabel,
2675+
);
2676+
try {
2677+
request = await refreshedAdapter.buildRequest(parsed, {
2678+
headers: selectedForwardHeaders,
2679+
translatorBudget,
2680+
});
2681+
recordAdapterReasoning(logCtx, request);
2682+
recordAdapterTier(logCtx, request);
2683+
} catch (err) {
2684+
upstream.abort();
2685+
if (options.abortSignal?.aborted) return clientCancelledResponse();
2686+
const msg = err instanceof Error ? err.message : String(err);
2687+
return formatErrorResponse(400, "invalid_request_error", redactSecretString(msg));
2688+
}
2689+
try {
2690+
upstreamResponse = await fetchWithTransientRetry(
2691+
recovery => {
2692+
noteAttemptSend(logCtx.activeAttempt, passthroughEstimate, recovery ?? "oauth-401");
2693+
return fetchWithHeaderTimeout(request.url, applyUpstreamRecoveryInit({
2694+
method: request.method,
2695+
headers: request.headers,
2696+
body: request.body,
2697+
}, recovery), upstream.signal, connectMs, parsed.stream,
2698+
providerFetch(route.provider, options.codexWsRuntimeIdentity, {
2699+
providerName: route.providerName,
2700+
modelId: route.modelId,
2701+
}),
2702+
route.provider.authMode === "forward")
2703+
.then(res => {
2704+
settleObservedHostResponse();
2705+
return res;
2706+
});
2707+
},
2708+
{ abortSignal: upstream.signal, label: safeHostLabel(request.url) },
2709+
);
2710+
} catch (err) {
2711+
return transportFailureResponse(err);
2712+
} finally {
2713+
request.releaseBodyObservation?.();
2714+
}
2715+
}
2716+
26252717
// Same-target 429 wait-and-retry (opt-in `retryOn429`) for key-auth providers on the
26262718
// passthrough wire. This branch returns before the recovery loop below, so Responses-shaped
26272719
// key-auth gateways (e.g. the built-in DeepSeek preset) would otherwise surface 429

‎structure/04_transports-and-sidecars.md‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ requires a compatible FastWire mapping on the final adapter and an eligible poli
5151
`fastMode: false` drops it. On classified Chat routes, `chatServiceTier` separately authorizes
5252
foreign caller values; an exact-model `true` does not grant that forwarding permission. On
5353
unclassified Chat routes it gates every caller tier because no canonical Fast capability has been
54-
validated. Exact `false`
54+
validated. An object-form registry wire default may also set `forwardCallerServiceTier: false` to
55+
close a known subscription gateway while leaving generic unclassified Responses passthrough
56+
unchanged. Exact `false`
5557
narrows provider defaults, and provider-level `supportsServiceTier: false` cannot be reopened.
5658
Capability is namespaced by the selected provider and model; model-name similarity and adapter type
5759
alone never opt a gateway in.
@@ -66,7 +68,18 @@ Registry `modelWireDefaults` select an evidence-backed upstream protocol for an
6668
changing the provider-wide adapter. Explicit, allowed `modelAdapters` configuration always wins,
6769
including an entry that opts the model back into the provider-wide wire. Defaults are applied only
6870
while the configured provider still matches the registry transport, so reusing a preset name for a
69-
different custom destination does not inherit its upstream assumptions.
71+
different custom destination does not inherit its upstream assumptions. Object-form defaults may
72+
also narrow the decision by inbound protocol and authentication mode; an auth-scoped default must
73+
not leak from a subscription transport into an API-key or forwarded-credential route.
74+
75+
xAI keeps `openai-chat` as its provider-wide compatibility wire. The official Grok CLI catalog
76+
declares the Grok 4.5 and 4.6 subscription models as Responses backends, so only OAuth-backed native
77+
Responses traffic for those exact models selects `openai-responses`. API-key requests, translated
78+
Chat/Anthropic callers, other Grok models, and explicit model adapter overrides retain their
79+
existing wire. This lets Codex receive native xAI SSE deltas as they arrive without widening the
80+
credential or compatibility boundary. These OAuth subscription defaults drop caller-owned
81+
`service_tier`; they neither advertise nor inject Fast. The API-key transport remains governed by
82+
its separate capability declaration.
7083

7184
OpenCode Go documents `gpt-5.6-luna` on `/zen/go/v1/responses` while sibling models use its Chat or
7285
Anthropic endpoints. The built-in preset therefore selects `openai-responses` only for Luna and
@@ -601,9 +614,10 @@ Grounded in the open-sourced official client (xai-org/grok-build); unit + eviden
601614
`auth.json` load-merge-persist (`src/oauth/store.ts`); generation-guarded persist
602615
(`expectedGeneration` → superseded adoption), conditional `needsReauth`, bounded jittered
603616
retry for transient token-endpoint failures.
604-
- **Reactive 401 replay:** the serving recovery loop force-refreshes once (singleflight,
605-
generation-checked) and replays OAuth-backed xAI requests exactly once with a re-resolved
606-
transport; API-key/BYOK paths excluded (`src/server/responses.ts`).
617+
- **Reactive 401 replay:** both the adapter recovery loop and native Responses passthrough branch
618+
force-refresh once (singleflight, generation-checked) and replay OAuth-backed xAI requests
619+
exactly once with a re-resolved transport; API-key/BYOK paths are excluded
620+
(`src/server/responses/core.ts`).
607621
- **Header parity:** per-attempt `x-grok-req-id` (fresh UUID inside the transport fetch
608622
wrapper), stable session/conv affinity headers, always-set User-Agent, and a single
609623
compatibility profile const for the Grok client version (`src/providers/xai-transport.ts`);

‎tests/adapter-resolve.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,38 @@ describe("per-model wire override (#404)", () => {
9292
});
9393

9494
describe("registry per-model wire defaults", () => {
95+
function xai(authMode: "oauth" | "key", overrides: Partial<OcxProviderConfig> = {}): OcxProviderConfig {
96+
return gateway({
97+
baseUrl: "https://api.x.ai/v1",
98+
authMode,
99+
...overrides,
100+
});
101+
}
102+
103+
test("routes current xAI subscription models through Responses for native Codex traffic", () => {
104+
for (const model of ["grok-4.6", "grok-4.5"]) {
105+
expect(resolveWireProtocolOverride("xai", model, xai("oauth"), "responses").adapter)
106+
.toBe("openai-responses");
107+
}
108+
});
109+
110+
test("keeps xAI key auth and translated callers on their existing Chat wire", () => {
111+
expect(resolveWireProtocolOverride("xai", "grok-4.6", xai("key"), "responses").adapter)
112+
.toBe("openai-chat");
113+
expect(resolveWireProtocolOverride("xai", "grok-4.6", xai("oauth"), "chat").adapter)
114+
.toBe("openai-chat");
115+
expect(resolveWireProtocolOverride("xai", "grok-4.6", xai("oauth"), "anthropic").adapter)
116+
.toBe("openai-chat");
117+
expect(resolveWireProtocolOverride("xai", "grok-4.3", xai("oauth"), "responses").adapter)
118+
.toBe("openai-chat");
119+
});
120+
121+
test("an explicit xAI Chat override opts out of the subscription Responses default", () => {
122+
const provider = xai("oauth", { modelAdapters: { "grok-4.6": "openai-chat" } });
123+
expect(resolveWireProtocolOverride("xai", "grok-4.6", provider, "responses").adapter)
124+
.toBe("openai-chat");
125+
});
126+
95127
function deepseek(overrides: Partial<OcxProviderConfig> = {}): OcxProviderConfig {
96128
return gateway({
97129
baseUrl: "https://api.deepseek.com",

0 commit comments

Comments
 (0)