@@ -669,6 +669,58 @@ export async function loadAccounts(): Promise<AccountStorageV3 | null> {
669669 return loadAccountsInternal ( saveAccounts ) ;
670670}
671671
672+ function getGlobalAccountsStoragePath ( ) : string {
673+ return join ( getConfigDir ( ) , ACCOUNTS_FILE_NAME ) ;
674+ }
675+
676+ function shouldUseProjectGlobalFallback ( ) : boolean {
677+ return Boolean ( currentStoragePath && currentProjectRoot ) ;
678+ }
679+
680+ async function loadGlobalAccountsFallback ( ) : Promise < AccountStorageV3 | null > {
681+ if ( ! shouldUseProjectGlobalFallback ( ) || ! currentStoragePath ) {
682+ return null ;
683+ }
684+
685+ const globalStoragePath = getGlobalAccountsStoragePath ( ) ;
686+ if ( globalStoragePath === currentStoragePath ) {
687+ return null ;
688+ }
689+
690+ try {
691+ const content = await fs . readFile ( globalStoragePath , "utf-8" ) ;
692+ const data = JSON . parse ( content ) as unknown ;
693+
694+ const schemaErrors = getValidationErrors ( AnyAccountStorageSchema , data ) ;
695+ if ( schemaErrors . length > 0 ) {
696+ log . warn ( "Global account storage schema validation warnings" , {
697+ path : globalStoragePath ,
698+ errors : schemaErrors . slice ( 0 , 5 ) ,
699+ } ) ;
700+ }
701+
702+ const normalized = normalizeAccountStorage ( data ) ;
703+ if ( ! normalized ) return null ;
704+
705+ log . info ( "Loaded global account storage as project fallback" , {
706+ from : globalStoragePath ,
707+ to : currentStoragePath ,
708+ accounts : normalized . accounts . length ,
709+ } ) ;
710+ return normalized ;
711+ } catch ( error ) {
712+ const code = ( error as NodeJS . ErrnoException ) . code ;
713+ if ( code !== "ENOENT" ) {
714+ log . warn ( "Failed to load global fallback account storage" , {
715+ from : globalStoragePath ,
716+ to : currentStoragePath ,
717+ error : String ( error ) ,
718+ } ) ;
719+ }
720+ return null ;
721+ }
722+ }
723+
672724async function loadAccountsInternal (
673725 persistMigration : ( ( storage : AccountStorageV3 ) => Promise < void > ) | null ,
674726) : Promise < AccountStorageV3 | null > {
@@ -704,7 +756,25 @@ async function loadAccountsInternal(
704756 ? await migrateLegacyProjectStorageIfNeeded ( persistMigration )
705757 : null ;
706758 if ( migrated ) return migrated ;
707- return null ;
759+ const globalFallback = await loadGlobalAccountsFallback ( ) ;
760+ if ( ! globalFallback ) return null ;
761+
762+ if ( persistMigration ) {
763+ try {
764+ await persistMigration ( globalFallback ) ;
765+ log . info ( "Seeded project account storage from global fallback" , {
766+ path : getStoragePath ( ) ,
767+ accounts : globalFallback . accounts . length ,
768+ } ) ;
769+ } catch ( persistError ) {
770+ log . warn ( "Failed to seed project storage from global fallback" , {
771+ path : getStoragePath ( ) ,
772+ error : String ( persistError ) ,
773+ } ) ;
774+ }
775+ }
776+
777+ return globalFallback ;
708778 }
709779 log . error ( "Failed to load account storage" , { error : String ( error ) } ) ;
710780 return null ;
0 commit comments