@@ -223,6 +223,8 @@ function makeRuntime(): RuntimeEnv {
223223function promptModelAllowlistOptions ( index = 0 ) {
224224 return mocks . promptModelAllowlist . mock . calls [ index ] ?. [ 0 ] as
225225 | {
226+ agentDir ?: string ;
227+ agentId ?: string ;
226228 allowedKeys ?: string [ ] ;
227229 initialSelections ?: string [ ] ;
228230 loadCatalog ?: boolean ;
@@ -501,7 +503,6 @@ describe("promptAuthConfig", () => {
501503
502504 expect ( mocks . promptModelAllowlist ) . toHaveBeenCalledOnce ( ) ;
503505 expect ( promptModelAllowlistOptions ( ) ?. preferredProvider ) . toBe ( "openai" ) ;
504- expect ( mocks . applyPrimaryModel ) . toHaveBeenCalledWith ( expect . any ( Object ) , "openai/gpt-5.5" ) ;
505506 expect ( result . agents ?. defaults ?. model ) . toEqual ( {
506507 primary : "openai/gpt-5.5" ,
507508 fallbacks : [ "openai/gpt-5.3-codex" ] ,
@@ -512,6 +513,41 @@ describe("promptAuthConfig", () => {
512513 ] ) ;
513514 } ) ;
514515
516+ it ( "canonicalizes a selected agent's legacy Codex primary before updating its allowlist" , async ( ) => {
517+ vi . clearAllMocks ( ) ;
518+ mocks . promptAuthChoiceGrouped . mockResolvedValue ( "openai-device-code" ) ;
519+ mocks . resolvePreferredProviderForAuthChoice . mockResolvedValue ( "openai" ) ;
520+ const config = {
521+ agents : {
522+ ownership : "explicit" as const ,
523+ defaults : {
524+ systemAgent : { agentId : "ops" } ,
525+ model : { primary : "anthropic/claude-sonnet-4-6" } ,
526+ } ,
527+ entries : {
528+ main : { } ,
529+ ops : { model : { primary : "codex/gpt-5.5" } } ,
530+ } ,
531+ } ,
532+ } satisfies OpenClawConfig ;
533+ mocks . applyAuthChoice . mockResolvedValue ( { config } ) ;
534+ mocks . promptModelAllowlist . mockResolvedValue ( {
535+ models : [ "openai/gpt-5.5" ] ,
536+ scopeKeys : [ "openai/gpt-5.5" ] ,
537+ } ) ;
538+ mocks . resolveProviderPluginChoiceCore . mockReturnValue ( null ) ;
539+
540+ const result = await promptAuthConfig ( config , makeRuntime ( ) , noopPrompter , {
541+ agentId : "ops" ,
542+ agentDir : "/tmp/ops-agent" ,
543+ workspaceDir : "/tmp/ops-workspace" ,
544+ } ) ;
545+
546+ expect ( result . agents ?. entries ?. ops ?. model ) . toEqual ( { primary : "openai/gpt-5.5" } ) ;
547+ expect ( result . agents ?. entries ?. ops ?. modelPolicy ?. allow ) . toEqual ( [ "openai/gpt-5.5" ] ) ;
548+ expect ( result . agents ?. defaults ?. model ) . toEqual ( { primary : "anthropic/claude-sonnet-4-6" } ) ;
549+ } ) ;
550+
515551 it ( "keeps the selected provider scope when existing config has another provider" , async ( ) => {
516552 vi . clearAllMocks ( ) ;
517553 mocks . promptAuthChoiceGrouped . mockResolvedValue ( "github-copilot" ) ;
@@ -752,4 +788,115 @@ describe("promptAuthConfig", () => {
752788 expect ( mocks . applyAuthChoice ) . toHaveBeenCalledTimes ( 2 ) ;
753789 expect ( mocks . promptModelAllowlist ) . toHaveBeenCalledTimes ( 1 ) ;
754790 } ) ;
791+
792+ it ( "writes model policy to the explicit configure target instead of global defaults" , async ( ) => {
793+ vi . clearAllMocks ( ) ;
794+ mocks . promptAuthChoiceGrouped . mockResolvedValue ( "skip" ) ;
795+ mocks . promptDefaultModel . mockResolvedValue ( { model : "openai/gpt-5.5" } ) ;
796+ mocks . promptModelAllowlist . mockResolvedValue ( { models : [ "openai/gpt-5.5" ] } ) ;
797+
798+ const result = await promptAuthConfig (
799+ {
800+ agents : {
801+ ownership : "explicit" ,
802+ defaults : { systemAgent : { agentId : "ops" } } ,
803+ entries : { main : { } , ops : { } } ,
804+ } ,
805+ } ,
806+ makeRuntime ( ) ,
807+ noopPrompter ,
808+ { agentId : "ops" , agentDir : "/tmp/ops-agent" , workspaceDir : "/tmp/ops-workspace" } ,
809+ ) ;
810+
811+ expect ( result . agents ?. entries ?. ops ?. model ) . toEqual ( { primary : "openai/gpt-5.5" } ) ;
812+ expect ( result . agents ?. entries ?. ops ?. modelPolicy ?. allow ) . toEqual ( [ "openai/gpt-5.5" ] ) ;
813+ expect ( result . agents ?. defaults ?. model ) . toBeUndefined ( ) ;
814+ expect ( result . agents ?. defaults ?. modelPolicy ) . toBeUndefined ( ) ;
815+ expect ( promptModelAllowlistOptions ( ) ) . toMatchObject ( {
816+ agentId : "ops" ,
817+ agentDir : "/tmp/ops-agent" ,
818+ } ) ;
819+ } ) ;
820+
821+ it ( "projects provider-auth model defaults onto the explicit target" , async ( ) => {
822+ vi . clearAllMocks ( ) ;
823+ mocks . promptAuthChoiceGrouped . mockResolvedValue ( "provider-auth" ) ;
824+ mocks . applyAuthChoice . mockResolvedValue ( {
825+ config : {
826+ agents : {
827+ ownership : "explicit" as const ,
828+ defaults : { model : { primary : "provider/global" } } ,
829+ entries : { main : { } , OPS : { } } ,
830+ } ,
831+ } ,
832+ agentModelOverride : "provider/selected" ,
833+ } ) ;
834+ mocks . promptModelAllowlist . mockResolvedValue ( { models : undefined } ) ;
835+
836+ const config = {
837+ agents : {
838+ ownership : "explicit" as const ,
839+ defaults : {
840+ systemAgent : { agentId : "ops" } ,
841+ model : { primary : "provider/original" } ,
842+ } ,
843+ entries : { main : { } , OPS : { } } ,
844+ } ,
845+ } ;
846+ const result = await promptAuthConfig ( config , makeRuntime ( ) , noopPrompter , {
847+ agentId : "ops" ,
848+ agentDir : "/tmp/ops-agent" ,
849+ workspaceDir : "/tmp/ops-workspace" ,
850+ } ) ;
851+
852+ expect ( mocks . applyAuthChoice ) . toHaveBeenCalledWith (
853+ expect . objectContaining ( { setDefaultModel : false } ) ,
854+ ) ;
855+ expect ( result . agents ?. entries ?. OPS ?. model ) . toEqual ( { primary : "provider/selected" } ) ;
856+ expect ( result . agents ?. defaults ?. model ) . toEqual ( { primary : "provider/original" } ) ;
857+ expect ( result . agents ?. entries ?. ops ) . toBeUndefined ( ) ;
858+ } ) ;
859+
860+ it ( "projects custom-provider model metadata onto the explicit target" , async ( ) => {
861+ vi . clearAllMocks ( ) ;
862+ mocks . promptAuthChoiceGrouped . mockResolvedValue ( "custom-api-key" ) ;
863+ mocks . promptCustomApiConfig . mockResolvedValue ( {
864+ config : {
865+ agents : {
866+ ownership : "explicit" as const ,
867+ entries : {
868+ main : { } ,
869+ OPS : {
870+ model : { primary : "custom/model" } ,
871+ models : { "custom/model" : { alias : "Custom" } } ,
872+ } ,
873+ } ,
874+ } ,
875+ models : { providers : { custom : { models : [ { id : "model" } ] } } } ,
876+ } ,
877+ providerId : "custom" ,
878+ modelId : "model" ,
879+ } ) ;
880+
881+ const config = {
882+ agents : {
883+ ownership : "explicit" as const ,
884+ defaults : { systemAgent : { agentId : "ops" } } ,
885+ entries : { main : { } , OPS : { } } ,
886+ } ,
887+ } ;
888+ const result = await promptAuthConfig ( config , makeRuntime ( ) , noopPrompter , {
889+ agentId : "ops" ,
890+ agentDir : "/tmp/ops-agent" ,
891+ workspaceDir : "/tmp/ops-workspace" ,
892+ } ) ;
893+
894+ expect ( mocks . promptCustomApiConfig ) . toHaveBeenCalledWith (
895+ expect . objectContaining ( { target : expect . objectContaining ( { agentId : "ops" } ) } ) ,
896+ ) ;
897+ expect ( result . agents ?. entries ?. OPS ?. model ) . toEqual ( { primary : "custom/model" } ) ;
898+ expect ( result . agents ?. entries ?. OPS ?. models ) . toEqual ( { "custom/model" : { alias : "Custom" } } ) ;
899+ expect ( result . agents ?. defaults ?. model ) . toBeUndefined ( ) ;
900+ expect ( result . models ?. providers ?. custom ?. models ) . toEqual ( [ { id : "model" } ] ) ;
901+ } ) ;
755902} ) ;
0 commit comments