@@ -19,7 +19,7 @@ import {
1919 type Model ,
2020 type ModelThinkingLevel ,
2121} from "openclaw/plugin-sdk/llm" ;
22- import { afterEach , describe , expect , it } from "vitest" ;
22+ import { afterEach , beforeEach , describe , expect , it } from "vitest" ;
2323import { renderCatNoncePngBase64 } from "../../test/helpers/live-image-probe.js" ;
2424import { discoverAuthStorage , discoverModels } from "../agents/agent-model-discovery.js" ;
2525import { resolveAgentWorkspaceDir , resolveDefaultAgentDir } from "../agents/agent-scope.js" ;
@@ -2891,6 +2891,52 @@ function buildLiveGatewayAuthProfileStore(params: {
28912891 } ;
28922892}
28932893
2894+ function materializeGatewayLiveDiscoveryAuth ( params : {
2895+ env : NodeJS . ProcessEnv ;
2896+ providerList : readonly string [ ] | undefined ;
2897+ store : AuthProfileStore ;
2898+ } ) : AuthProfileStore {
2899+ const includesOpenAi =
2900+ ! params . providerList ||
2901+ params . providerList . some ( ( provider ) => normalizeProviderId ( provider ) === "openai" ) ;
2902+ const apiKey = includesOpenAi ? params . env . OPENAI_API_KEY ?. trim ( ) : undefined ;
2903+ if ( ! apiKey ) {
2904+ return params . store ;
2905+ }
2906+ return buildLiveGatewayAuthProfileStore ( {
2907+ store : params . store ,
2908+ candidates : [
2909+ {
2910+ model : createGatewayLiveTestModel ( "openai" , "gpt-5.6-sol" ) ,
2911+ auth : { apiKey, mode : "api-key" , source : "env: OPENAI_API_KEY" } ,
2912+ } ,
2913+ ] ,
2914+ } ) ;
2915+ }
2916+
2917+ function resolveGatewayLivePreparedProfileId (
2918+ store : AuthProfileStore ,
2919+ provider : string ,
2920+ ) : string | undefined {
2921+ return normalizeProviderId ( provider ) === "openai" && store . profiles [ "openai:live" ]
2922+ ? "openai:live"
2923+ : undefined ;
2924+ }
2925+
2926+ async function enterIsolatedGatewayLiveDiscoveryState ( ) : Promise < ( ) => Promise < void > > {
2927+ const previousStateDir = process . env . OPENCLAW_STATE_DIR ;
2928+ const tempStateDir = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "openclaw-live-discovery-state-" ) ) ;
2929+ setTestEnvValue ( "OPENCLAW_STATE_DIR" , tempStateDir ) ;
2930+ return async ( ) => {
2931+ if ( previousStateDir === undefined ) {
2932+ delete process . env . OPENCLAW_STATE_DIR ;
2933+ } else {
2934+ process . env . OPENCLAW_STATE_DIR = previousStateDir ;
2935+ }
2936+ await fs . rm ( tempStateDir , { recursive : true , force : true , maxRetries : 5 , retryDelay : 50 } ) ;
2937+ } ;
2938+ }
2939+
28942940function createGatewayLiveModelSession ( params : {
28952941 agentId : string ;
28962942 credentialAttempt : number ;
@@ -3054,6 +3100,90 @@ describe("buildLiveGatewayAuthProfileStore", () => {
30543100 expect ( store . profiles ) . toEqual ( { } ) ;
30553101 } ) ;
30563102
3103+ it ( "materializes the OpenAI live secret before profile-first discovery" , ( ) => {
3104+ const store : AuthProfileStore = { version : 1 , profiles : { } } ;
3105+
3106+ const prepared = materializeGatewayLiveDiscoveryAuth ( {
3107+ env : { OPENAI_API_KEY : "prepared-openai-test-key" } ,
3108+ providerList : [ "openai" ] ,
3109+ store,
3110+ } ) ;
3111+
3112+ expect ( prepared . profiles [ "openai:live" ] ) . toEqual ( {
3113+ type : "api_key" ,
3114+ provider : "openai" ,
3115+ key : "prepared-openai-test-key" ,
3116+ } ) ;
3117+ expect ( prepared . order ?. openai ) . toEqual ( [ "openai:live" ] ) ;
3118+ expect ( store . profiles ) . toEqual ( { } ) ;
3119+ } ) ;
3120+
3121+ it ( "does not materialize OpenAI auth for another provider's focused sweep" , ( ) => {
3122+ const store : AuthProfileStore = { version : 1 , profiles : { } } ;
3123+
3124+ expect (
3125+ materializeGatewayLiveDiscoveryAuth ( {
3126+ env : { OPENAI_API_KEY : "prepared-openai-test-key" } ,
3127+ providerList : [ "anthropic" ] ,
3128+ store,
3129+ } ) ,
3130+ ) . toBe ( store ) ;
3131+ } ) ;
3132+
3133+ it ( "carries the materialized OpenAI profile into profile-first lookup" , ( ) => {
3134+ const store = materializeGatewayLiveDiscoveryAuth ( {
3135+ env : { OPENAI_API_KEY : "prepared-openai-test-key" } ,
3136+ providerList : [ "openai" ] ,
3137+ store : { version : 1 , profiles : { } } ,
3138+ } ) ;
3139+
3140+ expect ( resolveGatewayLivePreparedProfileId ( store , "openai" ) ) . toBe ( "openai:live" ) ;
3141+ expect ( resolveGatewayLivePreparedProfileId ( store , "anthropic" ) ) . toBeUndefined ( ) ;
3142+ } ) ;
3143+
3144+ it ( "keeps prepared discovery credentials out of the ambient auth store" , async ( ) => {
3145+ const previousStateDir = process . env . OPENCLAW_STATE_DIR ;
3146+ const ambientStateDir = await fs . mkdtemp (
3147+ path . join ( os . tmpdir ( ) , "openclaw-live-ambient-state-" ) ,
3148+ ) ;
3149+ process . env . OPENCLAW_STATE_DIR = ambientStateDir ;
3150+ const ambientAgentDir = resolveDefaultAgentDir ( { } ) ;
3151+ const ambientStore : AuthProfileStore = {
3152+ version : 1 ,
3153+ profiles : {
3154+ "openai:ambient" : { type : "api_key" , provider : "openai" , key : "ambient-test-key" } ,
3155+ } ,
3156+ } ;
3157+ saveAuthProfileStore ( ambientStore , ambientAgentDir ) ;
3158+
3159+ try {
3160+ const leaveDiscoveryState = await enterIsolatedGatewayLiveDiscoveryState ( ) ;
3161+ try {
3162+ const discoveryAgentDir = resolveDefaultAgentDir ( { } ) ;
3163+ expect ( discoveryAgentDir ) . not . toBe ( ambientAgentDir ) ;
3164+ const prepared = materializeGatewayLiveDiscoveryAuth ( {
3165+ env : { OPENAI_API_KEY : "prepared-openai-test-key" } ,
3166+ providerList : [ "openai" ] ,
3167+ store : ensureAuthProfileStore ( discoveryAgentDir , { allowKeychainPrompt : false } ) ,
3168+ } ) ;
3169+ saveAuthProfileStore ( prepared , discoveryAgentDir ) ;
3170+ } finally {
3171+ await leaveDiscoveryState ( ) ;
3172+ }
3173+
3174+ expect (
3175+ ensureAuthProfileStore ( ambientAgentDir , { allowKeychainPrompt : false } ) . profiles ,
3176+ ) . toEqual ( ambientStore . profiles ) ;
3177+ } finally {
3178+ if ( previousStateDir === undefined ) {
3179+ delete process . env . OPENCLAW_STATE_DIR ;
3180+ } else {
3181+ process . env . OPENCLAW_STATE_DIR = previousStateDir ;
3182+ }
3183+ await fs . rm ( ambientStateDir , { recursive : true , force : true } ) ;
3184+ }
3185+ } ) ;
3186+
30573187 it ( "keeps an env-first provider on its prepared direct credential" , ( ) => {
30583188 const store : AuthProfileStore = {
30593189 version : 1 ,
@@ -5755,6 +5885,17 @@ async function runGatewayModelSuite(params: GatewayModelSuiteParams) {
57555885}
57565886
57575887describeLive ( "gateway live (dev agent, profile keys)" , ( ) => {
5888+ let leaveDiscoveryState : ( ( ) => Promise < void > ) | undefined ;
5889+
5890+ beforeEach ( async ( ) => {
5891+ leaveDiscoveryState = await enterIsolatedGatewayLiveDiscoveryState ( ) ;
5892+ } ) ;
5893+
5894+ afterEach ( async ( ) => {
5895+ await leaveDiscoveryState ?.( ) ;
5896+ leaveDiscoveryState = undefined ;
5897+ } ) ;
5898+
57585899 it (
57595900 "runs meaningful prompts across models with available keys" ,
57605901 async ( ) =>
@@ -5797,6 +5938,18 @@ describeLive("gateway live (dev agent, profile keys)", () => {
57975938 env : process . env ,
57985939 } ) ;
57995940 const workspaceDir = resolveAgentWorkspaceDir ( cfg , DEFAULT_AGENT_ID ) ;
5941+ const discoveryAgentDir = resolveDefaultAgentDir ( cfg ) ;
5942+ const discoveryAuthProfileStore = ensureAuthProfileStore ( discoveryAgentDir , {
5943+ allowKeychainPrompt : false ,
5944+ } ) ;
5945+ const preparedDiscoveryAuthProfileStore = materializeGatewayLiveDiscoveryAuth ( {
5946+ env : process . env ,
5947+ providerList,
5948+ store : discoveryAuthProfileStore ,
5949+ } ) ;
5950+ if ( preparedDiscoveryAuthProfileStore !== discoveryAuthProfileStore ) {
5951+ saveAuthProfileStore ( preparedDiscoveryAuthProfileStore , discoveryAgentDir ) ;
5952+ }
58005953 logProgress ( "[all-models] preparing models.json" ) ;
58015954 const modelsJsonResult = await withGatewayLiveSetupTimeout (
58025955 ensureOpenClawModelsJson ( cfg , undefined , {
@@ -5842,7 +5995,11 @@ describeLive("gateway live (dev agent, profile keys)", () => {
58425995 }
58435996 } else {
58445997 logProgress ( "[all-models] loading auth profiles" ) ;
5845- const authBacked = await loadAuthBackedLiveModelRegistry ( { agentDir, cfg, providerList } ) ;
5998+ const authBacked = await loadAuthBackedLiveModelRegistry ( {
5999+ agentDir,
6000+ cfg,
6001+ providerList,
6002+ } ) ;
58466003 authProfileStore = authBacked . authProfileStore ;
58476004 modelRegistry = authBacked . modelRegistry ;
58486005 all = authBacked . all ;
@@ -5935,6 +6092,7 @@ describeLive("gateway live (dev agent, profile keys)", () => {
59356092 store : authProfileStore ,
59366093 agentDir,
59376094 workspaceDir,
6095+ profileId : resolveGatewayLivePreparedProfileId ( authProfileStore , model . provider ) ,
59386096 credentialPrecedence : resolveLiveCredentialPrecedence (
59396097 model . provider ,
59406098 REQUIRE_PROFILE_KEYS ,
0 commit comments