@@ -170,7 +170,7 @@ type ExecSecretRefPassEnvSource = {
170170function collectConfigSecretRefServiceEnvVars ( params : {
171171 env : Record < string , string | undefined > ;
172172 config ?: OpenClawConfig ;
173- durableEnvironment : Record < string , string | undefined > ;
173+ stateDirDotEnvEnvironment : Record < string , string | undefined > ;
174174 warn ?: DaemonInstallWarnFn ;
175175} ) : Record < string , string > {
176176 if ( ! params . config ) {
@@ -207,7 +207,7 @@ function collectConfigSecretRefServiceEnvVars(params: {
207207 ) ;
208208 continue ;
209209 }
210- if ( Object . hasOwn ( params . durableEnvironment , key ) ) {
210+ if ( Object . hasOwn ( params . stateDirDotEnvEnvironment , key ) ) {
211211 continue ;
212212 }
213213 const value = params . env [ key ] ?. trim ( ) ;
@@ -496,6 +496,26 @@ function readExistingEnvironmentValueSource(params: {
496496 return undefined ;
497497}
498498
499+ function omitEnvironmentEntriesShadowedBy (
500+ entries : Record < string , string | undefined > ,
501+ shadowEntries : Array < Record < string , string | undefined > > ,
502+ ) : Record < string , string | undefined > {
503+ const shadowKeys = new Set (
504+ shadowEntries . flatMap ( ( environment ) =>
505+ Object . keys ( environment ) . flatMap ( ( key ) => {
506+ const normalized = normalizeEnvVarKey ( key , { portable : true } ) ?. toUpperCase ( ) ;
507+ return normalized ? [ normalized ] : [ ] ;
508+ } ) ,
509+ ) ,
510+ ) ;
511+ return Object . fromEntries (
512+ Object . entries ( entries ) . filter ( ( [ key ] ) => {
513+ const normalized = normalizeEnvVarKey ( key , { portable : true } ) ?. toUpperCase ( ) ;
514+ return ! normalized || ! shadowKeys . has ( normalized ) ;
515+ } ) ,
516+ ) ;
517+ }
518+
499519function resolveGatewayInstallWorkingDirectory ( params : {
500520 env : Record < string , string | undefined > ;
501521 platform : NodeJS . Platform ;
@@ -534,7 +554,7 @@ async function buildGatewayInstallEnvironment(params: {
534554 const configSecretRefEnvironment = collectConfigSecretRefServiceEnvVars ( {
535555 env : params . env ,
536556 config : params . config ,
537- durableEnvironment ,
557+ stateDirDotEnvEnvironment ,
538558 warn : params . warn ,
539559 } ) ;
540560 const authStore = await resolveAuthProfileStoreForServiceEnv ( params . authStore ) ;
@@ -550,35 +570,48 @@ async function buildGatewayInstallEnvironment(params: {
550570 authStore,
551571 warn : params . warn ,
552572 } ) ;
573+ const stateDirDotEnvRenderEnvironment = omitEnvironmentEntriesShadowedBy (
574+ stateDirDotEnvEnvironment ,
575+ [
576+ configEnvironment ,
577+ configSecretRefEnvironment ,
578+ execSecretRefPassEnvEnvironment ,
579+ authProfileEnvironment ,
580+ ] ,
581+ ) ;
553582 const preservedExistingEnvironment = collectPreservedExistingServiceEnvVars (
554583 params . existingEnvironment ,
555584 readManagedServiceEnvKeysFromEnvironment ( params . existingEnvironment ) ,
556585 ) ;
557586 const plan = createMutableServiceEnvPlan ( ) ;
558587 addServiceEnvPlanEntries ( plan , preservedExistingEnvironment , {
559- source : "existing-preserved" ,
560588 valueSource : ( { normalizedKey } ) =>
561589 readExistingEnvironmentValueSource ( {
562590 existingEnvironmentValueSources : params . existingEnvironmentValueSources ,
563591 normalizedKey,
564592 } ) ?? "inline" ,
565593 } ) ;
566- addServiceEnvPlanEntries ( plan , stateDirDotEnvEnvironment , { source : "state-dotenv" } ) ;
567- addServiceEnvPlanEntries ( plan , configEnvironment , { source : "config-env" } ) ;
568- addServiceEnvPlanEntries ( plan , configSecretRefEnvironment , { source : "config-secretref-env" } ) ;
569- addServiceEnvPlanEntries ( plan , execSecretRefPassEnvEnvironment , { source : "exec-passenv" } ) ;
570- addServiceEnvPlanEntries ( plan , authProfileEnvironment , { source : "auth-profile-env" } ) ;
571- const managedServiceEnvKeys = formatManagedServiceEnvKeys ( durableEnvironment , {
572- omitKeys : Object . keys ( params . serviceEnvironment ) ,
573- } ) ;
594+ addServiceEnvPlanEntries ( plan , stateDirDotEnvEnvironment , { } ) ;
595+ addServiceEnvPlanEntries ( plan , configEnvironment , { } ) ;
596+ addServiceEnvPlanEntries ( plan , configSecretRefEnvironment , { } ) ;
597+ addServiceEnvPlanEntries ( plan , execSecretRefPassEnvEnvironment , { } ) ;
598+ addServiceEnvPlanEntries ( plan , authProfileEnvironment , { } ) ;
599+ const managedServiceEnvKeys = formatManagedServiceEnvKeys (
600+ {
601+ ...durableEnvironment ,
602+ ...configSecretRefEnvironment ,
603+ } ,
604+ { omitKeys : Object . keys ( params . serviceEnvironment ) } ,
605+ ) ;
574606 applyManagedServiceEnvRenderPolicy ( {
575607 plan,
576608 managedServiceEnvKeys,
577609 serviceEnvironment : params . serviceEnvironment ,
578610 platform : params . platform ,
611+ stateDirDotEnvEnvironment : stateDirDotEnvRenderEnvironment ,
612+ configSecretRefEnvironment,
579613 } ) ;
580614 addServiceEnvPlanEntries ( plan , params . serviceEnvironment , {
581- source : "service-generated" ,
582615 includeRawKeys : true ,
583616 } ) ;
584617 const mergedPath = mergeServicePath (
0 commit comments