@@ -8,6 +8,7 @@ import { mkdtempSync, rmSync } from "node:fs";
88import { tmpdir } from "node:os" ;
99import { join } from "node:path" ;
1010import { handleResponses , isShadowSourceModel } from "../src/server/responses" ;
11+ import { shouldInterceptShadowCall } from "../src/lib/shadow-call" ;
1112import { handleManagementAPI } from "../src/server/management-api" ;
1213import type { OcxConfig } from "../src/types" ;
1314
@@ -51,6 +52,42 @@ describe("isShadowSourceModel", () => {
5152 } ) ;
5253} ) ;
5354
55+ describe ( "shouldInterceptShadowCall" , ( ) => {
56+ const metadata = ( requestKind : string ) => new Headers ( {
57+ "x-codex-turn-metadata" : JSON . stringify ( { request_kind : requestKind } ) ,
58+ } ) ;
59+
60+ test ( "intercepts recognized maintenance kinds but not normal turns" , ( ) => {
61+ expect ( shouldInterceptShadowCall ( "gpt-5.6-luna" , undefined , metadata ( "memory" ) ) ) . toBe ( true ) ;
62+ expect ( shouldInterceptShadowCall ( "gpt-5.6-luna" , undefined , metadata ( "compaction" ) ) ) . toBe ( true ) ;
63+ expect ( shouldInterceptShadowCall ( "gpt-5.6-luna" , undefined , metadata ( "prewarm" ) ) ) . toBe ( true ) ;
64+ expect ( shouldInterceptShadowCall ( "gpt-5.6-luna" , undefined , metadata ( "turn" ) ) ) . toBe ( false ) ;
65+ } ) ;
66+
67+ test ( "keeps legacy matching for headerless, malformed, and unrecognized metadata" , ( ) => {
68+ expect ( shouldInterceptShadowCall ( "gpt-5.6-luna" , undefined , new Headers ( ) ) ) . toBe ( true ) ;
69+ expect ( shouldInterceptShadowCall (
70+ "gpt-5.6-luna" ,
71+ undefined ,
72+ new Headers ( { "x-codex-turn-metadata" : "{" } ) ,
73+ ) ) . toBe ( true ) ;
74+ expect ( shouldInterceptShadowCall (
75+ "gpt-5.6-luna" ,
76+ undefined ,
77+ new Headers ( { "x-codex-turn-metadata" : "{}" } ) ,
78+ ) ) . toBe ( true ) ;
79+ expect ( shouldInterceptShadowCall ( "gpt-5.6-luna" , undefined , metadata ( "future-kind" ) ) ) . toBe ( true ) ;
80+ } ) ;
81+
82+ test ( "uses case-insensitive Headers lookup and still excludes non-source models" , ( ) => {
83+ const headers = new Headers ( {
84+ "X-CoDeX-TuRn-MeTaDaTa" : JSON . stringify ( { request_kind : "turn" } ) ,
85+ } ) ;
86+ expect ( shouldInterceptShadowCall ( "gpt-5.6-luna" , undefined , headers ) ) . toBe ( false ) ;
87+ expect ( shouldInterceptShadowCall ( "gpt-5.6-terra" , undefined , metadata ( "memory" ) ) ) . toBe ( false ) ;
88+ } ) ;
89+ } ) ;
90+
5491function interceptConfig ( ) : OcxConfig {
5592 return {
5693 port : 0 ,
@@ -67,10 +104,14 @@ function interceptConfig(): OcxConfig {
67104 } as OcxConfig ;
68105}
69106
70- async function post ( config : OcxConfig , model : string ) : Promise < Response > {
107+ async function post ( config : OcxConfig , model : string , requestKind ?: string ) : Promise < Response > {
108+ const headers : Record < string , string > = { "content-type" : "application/json" } ;
109+ if ( requestKind ) {
110+ headers [ "x-codex-turn-metadata" ] = JSON . stringify ( { request_kind : requestKind } ) ;
111+ }
71112 return handleResponses ( new Request ( "http://localhost/v1/responses" , {
72113 method : "POST" ,
73- headers : { "content-type" : "application/json" } ,
114+ headers,
74115 body : JSON . stringify ( {
75116 model,
76117 input : [ { type : "message" , role : "user" , content : [ { type : "input_text" , text : "hi" } ] } ] ,
@@ -91,7 +132,7 @@ describe("shadow call intercept request path (issue #311)", () => {
91132 } ) , { status : 200 , headers : { "content-type" : "application/json" } } ) ;
92133 } ) as typeof fetch ;
93134
94- await post ( interceptConfig ( ) , "gpt-5.6-luna" ) ;
135+ await post ( interceptConfig ( ) , "gpt-5.6-luna" , "memory" ) ;
95136
96137 expect ( bodies . length ) . toBe ( 1 ) ;
97138 // Routed through xai openai-chat: upstream model is the decoded routed id, not the helper id
@@ -101,6 +142,19 @@ describe("shadow call intercept request path (issue #311)", () => {
101142 expect ( effort ) . toBe ( "low" ) ;
102143 } ) ;
103144
145+ test ( "does not rewrite a foreground gpt-5.6-luna turn" , async ( ) => {
146+ let sawFetch = false ;
147+ globalThis . fetch = ( async ( ) => {
148+ sawFetch = true ;
149+ return new Response ( JSON . stringify ( { error : { message : "unreachable" } } ) , { status : 500 } ) ;
150+ } ) as typeof fetch ;
151+
152+ const response = await post ( interceptConfig ( ) , "gpt-5.6-luna" , "turn" ) ;
153+
154+ expect ( sawFetch ) . toBe ( false ) ;
155+ expect ( response . status ) . toBe ( 404 ) ;
156+ } ) ;
157+
104158 test ( "leaves gpt-5.6-terra requests unrewritten" , async ( ) => {
105159 let sawFetch = false ;
106160 globalThis . fetch = ( async ( ) => {
0 commit comments