@@ -36,12 +36,17 @@ import {
3636 shouldUseMainOwnerForLocalOAuthCredential ,
3737 type PersistedAuthProfileStores ,
3838} from "./ownership.js" ;
39- import { resolveSharedAuthStorePath as resolveSharedAuthPath } from "./path-resolve.js" ;
39+ import {
40+ resolveSharedAuthStoreOwnership ,
41+ resolveSharedAuthStorePath as resolveSharedAuthPath ,
42+ } from "./path-resolve.js" ;
4043import {
4144 buildPersistedAuthProfileSecretsStore ,
4245 loadPersistedAuthProfileStore ,
46+ loadPersistedSharedAuthProfileStore ,
4347 mergeAuthProfileStores ,
4448} from "./persisted.js" ;
49+ import { resolveAuthProfilePortability } from "./portability.js" ;
4550import {
4651 getRuntimeExternalCliProfileIds ,
4752 mergeRuntimeExternalProfileReferences ,
@@ -64,6 +69,7 @@ import {
6469 deferAuthProfilePostCommitPublication ,
6570 deletePersistedAuthProfileStoreRaw ,
6671 inspectPersistedAuthProfileStoreRaw ,
72+ inspectPersistedSharedAuthProfileStoreRaw ,
6773 readPersistedAuthProfileStoreRaw ,
6874 readPersistedAuthProfileStateRaw ,
6975 resolveAuthProfileDatabasePath as resolveAgentAuthPath ,
@@ -97,7 +103,9 @@ type SaveAuthProfileStoreOptions = {
97103} ;
98104
99105const INLINE_OAUTH_TOKEN_FIELDS = [ "access" , "refresh" , "idToken" ] as const ;
100- type AuthProfileRuntimeMode = { kind : "env-only" } | { kind : "agent-dir" ; agentDir : string } ;
106+ type AuthProfileRuntimeMode =
107+ | { kind : "env-only" }
108+ | { kind : "agent-dir" ; agentDir : string ; sharedStore ?: AuthProfileStore } ;
101109
102110const authProfileRuntimeMode = new AsyncLocalStorage < AuthProfileRuntimeMode > ( ) ;
103111
@@ -111,8 +119,50 @@ export function withEnvOnlyAuthProfileStore<T>(run: () => T): T {
111119}
112120
113121/** Run a bounded operation against one existing persisted auth store. */
114- export function withAuthProfileStoreAgentDir < T > ( agentDir : string , run : ( ) => T ) : T {
115- return authProfileRuntimeMode . run ( { kind : "agent-dir" , agentDir } , run ) ;
122+ export function withAuthProfileStoreAgentDir < T > (
123+ agentDir : string ,
124+ sharedStateDir : string ,
125+ run : ( ) => T ,
126+ ) : T {
127+ const env = { ...process . env , OPENCLAW_STATE_DIR : sharedStateDir } ;
128+ let sharedStore : AuthProfileStore | undefined ;
129+ if ( resolveSharedAuthStoreOwnership ( env ) . location === "state-db" ) {
130+ const shared = loadPersistedSharedAuthProfileStore ( env ) ;
131+ if ( ! shared && inspectPersistedSharedAuthProfileStoreRaw ( env ) . status !== "missing" ) {
132+ throw new AuthProfileStoreUnreadableError ( undefined , env ) ;
133+ }
134+ sharedStore = shared ?? createEmptyAuthProfileStore ( ) ;
135+ }
136+ // Temporary runs must not acquire a second OAuth refresh owner. Keep this
137+ // read-through view in the operation scope, never in a persisted agent store.
138+ if ( sharedStore ) {
139+ sharedStore . profiles = Object . fromEntries (
140+ Object . entries ( sharedStore . profiles ) . filter (
141+ ( [ , credential ] ) =>
142+ resolveAuthProfilePortability ( credential ) . reason === "portable-static-credential" ,
143+ ) ,
144+ ) ;
145+ pruneAuthProfileStoreReferences ( sharedStore , new Set ( Object . keys ( sharedStore . profiles ) ) ) ;
146+ }
147+ return authProfileRuntimeMode . run ( { kind : "agent-dir" , agentDir, sharedStore } , run ) ;
148+ }
149+
150+ function getScopedSharedAuthStore ( ) : AuthProfileStore | undefined {
151+ const mode = authProfileRuntimeMode . getStore ( ) ;
152+ return mode ?. kind === "agent-dir" ? mode . sharedStore : undefined ;
153+ }
154+
155+ function applyScopedAuthReadThrough ( store : AuthProfileStore ) : AuthProfileStore {
156+ const shared = getScopedSharedAuthStore ( ) ;
157+ if ( ! shared ) {
158+ return store ;
159+ }
160+ const merged = mergeAuthProfileStores ( cloneAuthProfileStore ( shared ) , store ) ;
161+ return setRuntimeLocalProfileMetadata (
162+ merged ,
163+ Object . keys ( store . profiles ) ,
164+ runtimeStoreInheritsMainState ( merged , store ) ,
165+ ) ;
116166}
117167
118168function isEnvOnlyAuthProfileRuntime ( ) : boolean {
@@ -286,6 +336,11 @@ function resolveRuntimeAuthProfileStore(
286336 agentDir ?: string ,
287337 options ?: Pick < LoadAuthProfileStoreOptions , "allowKeychainPrompt" | "inheritedAuthDir" > ,
288338) : AuthProfileStore | null {
339+ // Ambient snapshots may include non-portable shared profiles. A bounded exec
340+ // scope composes its view from the actual local store and its filtered base.
341+ if ( getScopedSharedAuthStore ( ) ) {
342+ return null ;
343+ }
289344 const mainKey = options ?. inheritedAuthDir
290345 ? resolveAgentAuthPath ( options . inheritedAuthDir )
291346 : resolveSharedAuthPath ( ) ;
@@ -471,6 +526,18 @@ function shouldKeepProfileInLocalStore(params: {
471526 persistedStores : PersistedAuthProfileStores ;
472527 externalProfiles : ( ) => RuntimeExternalOAuthProfile [ ] ;
473528} ) : boolean {
529+ const inherited = getScopedSharedAuthStore ( ) ?. profiles [ params . profileId ] ;
530+ if ( inherited && ! params . persistedStores . localStore ?. profiles [ params . profileId ] ) {
531+ // Runtime state updates must not turn read-through credentials into local copies.
532+ // Compare persisted shapes so a materialized SecretRef stays inherited too.
533+ const secrets = buildPersistedAuthProfileSecretsStore ( {
534+ version : AUTH_STORE_VERSION ,
535+ profiles : { [ params . profileId ] : params . credential } ,
536+ } ) ;
537+ if ( isDeepStrictEqual ( secrets . profiles [ params . profileId ] , inherited ) ) {
538+ return false ;
539+ }
540+ }
474541 if ( params . credential . type !== "oauth" ) {
475542 return true ;
476543 }
@@ -715,9 +782,12 @@ function runtimeStoreInheritsMainState(
715782}
716783
717784function listRuntimeLocalProfileIds (
718- store : AuthProfileStore ,
785+ store : RuntimeAuthProfileStore ,
719786 mainStore ?: AuthProfileStore ,
720787) : string [ ] {
788+ if ( store . runtimeLocalProfileIds ) {
789+ return store . runtimeLocalProfileIds ;
790+ }
721791 return Object . entries ( store . profiles ) . flatMap ( ( [ profileId , credential ] ) =>
722792 mainStore &&
723793 shouldUseMainOwnerForLocalOAuthCredential ( {
@@ -917,13 +987,11 @@ export function loadAuthProfileStore(): AuthProfileStore {
917987 return createEmptyAuthProfileStore ( ) ;
918988 }
919989 const agentDir = resolveRuntimeAuthProfileAgentDir ( ) ;
920- const asStore = loadPersistedAuthProfileStore ( agentDir ) ;
921- if ( asStore ) {
922- return overlayExternalAuthProfiles ( markRuntimePersistedProfiles ( asStore ) , { agentDir } ) ;
923- }
924-
925- const store : AuthProfileStore = { version : AUTH_STORE_VERSION , profiles : { } } ;
926- return overlayExternalAuthProfiles ( markRuntimePersistedProfiles ( store ) , { agentDir } ) ;
990+ const store = loadPersistedAuthProfileStore ( agentDir ) ?? createEmptyAuthProfileStore ( ) ;
991+ return overlayExternalAuthProfiles (
992+ applyScopedAuthReadThrough ( markRuntimePersistedProfiles ( store ) ) ,
993+ { agentDir } ,
994+ ) ;
927995}
928996
929997function loadAuthProfileStoreForAgent (
@@ -936,64 +1004,39 @@ function loadAuthProfileStoreForAgent(
9361004 const effectiveAgentDir = resolveRuntimeAuthProfileAgentDir ( agentDir ) ;
9371005 const effectiveOptions = resolveRuntimeAuthProfileLoadOptions ( options ) ;
9381006 assertAuthProfileMigrationReady ( effectiveAgentDir ) ;
939- const asStore = loadPersistedAuthProfileStore (
1007+ const store = loadPersistedAuthProfileStore (
9401008 effectiveAgentDir ,
9411009 resolvePersistedLoadOptions ( effectiveOptions ) ,
9421010 ) ;
943- if ( asStore ) {
944- const legacySources = listLegacyAuthProfileSources ( { agentDir : effectiveAgentDir } ) ;
945- const credentialSources = legacySources . filter ( ( source ) => source . kind !== "auth-state" ) ;
946- // A populated canonical store already owns this agent's credentials, so a
947- // retired file beside it is unarchived bytes rather than pending migration.
948- // Only an empty store means the credentials still live solely in that file.
949- if ( credentialSources . length > 0 && Object . keys ( asStore . profiles ) . length === 0 ) {
950- const migrationError = new AuthProfileMigrationRequiredError ( {
951- agentDir : effectiveAgentDir ,
952- sources : credentialSources ,
953- } ) ;
954- markAuthProfileMigrationRequired ( effectiveAgentDir , migrationError ) ;
955- throw migrationError ;
956- }
957- warnLegacyAuthProfileSourcesIgnored ( {
958- agentDir : effectiveAgentDir ,
959- sources : legacySources ,
960- } ) ;
961- clearAuthProfileMigrationRequired ( effectiveAgentDir ) ;
962- const synced = maybeSyncPersistedExternalCliAuthProfiles ( {
963- store : asStore ,
964- agentDir : effectiveAgentDir ,
965- options : effectiveOptions ,
966- } ) ;
967- return markRuntimePersistedProfiles ( synced . store ) ;
968- }
969-
970- const inspection = inspectPersistedAuthProfileStoreRaw (
971- effectiveAgentDir ,
972- effectiveOptions ?. database ,
973- ) ;
974- if ( inspection . status !== "missing" ) {
1011+ if (
1012+ ! store &&
1013+ inspectPersistedAuthProfileStoreRaw ( effectiveAgentDir , effectiveOptions ?. database ) . status !==
1014+ "missing"
1015+ ) {
9751016 throw new AuthProfileStoreUnreadableError ( effectiveAgentDir ) ;
9761017 }
9771018 const legacySources = listLegacyAuthProfileSources ( { agentDir : effectiveAgentDir } ) ;
9781019 const credentialSources = legacySources . filter ( ( source ) => source . kind !== "auth-state" ) ;
979- if ( credentialSources . length > 0 ) {
980- throw new AuthProfileMigrationRequiredError ( {
1020+ // A populated canonical store owns credentials; retired files beside it are
1021+ // unarchived bytes. An empty or absent store still requires migration.
1022+ if ( credentialSources . length > 0 && ( ! store || Object . keys ( store . profiles ) . length === 0 ) ) {
1023+ const migrationError = new AuthProfileMigrationRequiredError ( {
9811024 agentDir : effectiveAgentDir ,
9821025 sources : credentialSources ,
9831026 } ) ;
1027+ if ( store ) {
1028+ markAuthProfileMigrationRequired ( effectiveAgentDir , migrationError ) ;
1029+ }
1030+ throw migrationError ;
9841031 }
9851032 warnLegacyAuthProfileSourcesIgnored ( { agentDir : effectiveAgentDir , sources : legacySources } ) ;
9861033 clearAuthProfileMigrationRequired ( effectiveAgentDir ) ;
987- const store : AuthProfileStore = {
988- version : AUTH_STORE_VERSION ,
989- profiles : { } ,
990- } ;
9911034 const synced = maybeSyncPersistedExternalCliAuthProfiles ( {
992- store,
1035+ store : store ?? createEmptyAuthProfileStore ( ) ,
9931036 agentDir : effectiveAgentDir ,
9941037 options : effectiveOptions ,
9951038 } ) ;
996- return markRuntimePersistedProfiles ( synced . store ) ;
1039+ return applyScopedAuthReadThrough ( markRuntimePersistedProfiles ( synced . store ) ) ;
9971040}
9981041
9991042/** Loads the effective runtime store for an agent, including inherited main profiles. */
@@ -1129,6 +1172,7 @@ export function ensureAuthProfileStore(
11291172 ) ;
11301173 if ( ! runtimeStore ) {
11311174 if (
1175+ ! getScopedSharedAuthStore ( ) &&
11321176 hasScopedExternalCliOverlay ( externalCli ) &&
11331177 ( store . runtimeExternalProfileIds ?. length ?? 0 ) > 0
11341178 ) {
@@ -1210,6 +1254,10 @@ export function findPersistedAuthProfileCredential(params: {
12101254 const agentDir = resolveRuntimeAuthProfileAgentDir ( params . agentDir ) ;
12111255 const requestedStore = loadPersistedAuthProfileStore ( agentDir ) ;
12121256 const requestedProfile = requestedStore ?. profiles [ params . profileId ] ;
1257+ const scopedSharedStore = getScopedSharedAuthStore ( ) ;
1258+ if ( scopedSharedStore ) {
1259+ return requestedProfile ?? scopedSharedStore . profiles [ params . profileId ] ;
1260+ }
12131261 if ( requestedProfile || ! agentDir ) {
12141262 return requestedProfile ;
12151263 }
0 commit comments