@@ -139,3 +139,64 @@ describe("credential pinning + loop fail-closed (review blockers)", () => {
139139 }
140140 } ) ;
141141} ) ;
142+
143+ import { runWithWebSearch , type WebSearchLoopDeps } from "../src/web-search/loop" ;
144+ import { createTestTranslatorBudget } from "./helpers/translator-budget" ;
145+ import type { AdapterEvent , ProviderAdapter } from "../src/adapters/base" ;
146+
147+ describe ( "loop dispatch: xai arm fails closed without a sidecar (review blocker)" , ( ) => {
148+ test ( "missing xaiSidecar yields the invariant error tool-result; forward executor and pool recorder untouched" , async ( ) => {
149+ const firstPass : AdapterEvent [ ] = [
150+ { type : "tool_call_start" , id : "ws1" , name : "web_search" } ,
151+ { type : "tool_call_delta" , arguments : "{\"query\":\"docs\"}" } ,
152+ { type : "tool_call_end" } ,
153+ { type : "done" } ,
154+ ] ;
155+ let pass = 0 ;
156+ let sawToolResult = "" ;
157+ const adapter : ProviderAdapter = {
158+ name : "two-pass" ,
159+ buildRequest : ( parsed ) => {
160+ if ( pass > 0 ) sawToolResult = JSON . stringify ( parsed . context . messages ?? parsed ) ;
161+ return { url : "https://routed.test/v1" , method : "POST" , headers : { } , body : "{}" } ;
162+ } ,
163+ fetchResponse : async ( ) => new Response ( "wire" , { status : 200 } ) ,
164+ async * parseStream ( ) {
165+ const events = pass ++ === 0 ? firstPass : [ { type : "text_delta" , text : "answer" } as AdapterEvent , { type : "done" } as AdapterEvent ] ;
166+ for ( const event of events ) yield event ;
167+ } ,
168+ async parseResponse ( ) { throw new Error ( "unreachable" ) ; } ,
169+ } ;
170+ const capturedForwardFetches : string [ ] = [ ] ;
171+ const realFetch = globalThis . fetch ;
172+ globalThis . fetch = ( async ( input : RequestInfo | URL ) => {
173+ capturedForwardFetches . push ( String ( input instanceof Request ? input . url : input ) ) ;
174+ return new Response ( "{}" , { status : 500 } ) ;
175+ } ) as typeof fetch ;
176+ let poolRecorded = 0 ;
177+ try {
178+ const response = await runWithWebSearch ( {
179+ parsed : parseRequest ( { model : "routed/model" , input : "hi" , stream : true , tools : [ { type : "web_search" } ] } ) ,
180+ adapter,
181+ backend : "xai" ,
182+ // xaiSidecar deliberately ABSENT — the invariant violation under test.
183+ hostedTool : { type : "web_search" } ,
184+ selectedForwardHeaders : new Headers ( { authorization : "Bearer forward-secret" } ) ,
185+ settings : { model : "grok-4.6" , reasoning : "low" , timeoutMs : 5000 , describeImages : false } ,
186+ maxSearches : 1 ,
187+ recordSidecarOutcome : ( ) => { poolRecorded += 1 ; } ,
188+ incomingMeta : { headers : new Headers ( ) , translatorBudget : createTestTranslatorBudget ( ) } ,
189+ } satisfies WebSearchLoopDeps ) ;
190+ // Drain the SSE so the loop completes.
191+ await new Response ( response . body ) . text ( ) ;
192+ // No network fetch may have carried the forward headers (executors were never invoked).
193+ expect ( capturedForwardFetches ) . toEqual ( [ ] ) ;
194+ // The pool recorder belongs to the OpenAI executor path only.
195+ expect ( poolRecorded ) . toBe ( 0 ) ;
196+ // The second pass received the graceful error tool result.
197+ expect ( sawToolResult ) . toContain ( "without a resolved Grok OAuth provider" ) ;
198+ } finally {
199+ globalThis . fetch = realFetch ;
200+ }
201+ } ) ;
202+ } ) ;
0 commit comments