@@ -10,7 +10,7 @@ import {
1010 loadAccounts ,
1111 normalizeAccountStorage ,
1212 previewImportAccountsWithExistingStorage ,
13- withAccountStorageTransaction ,
13+ withAccountAndFlaggedStorageTransaction ,
1414 type AccountStorageV3 ,
1515 type ImportAccountsResult ,
1616} from "./storage.js" ;
@@ -270,7 +270,7 @@ async function scrubStaleNormalizedImportTempFile(candidateDir: string): Promise
270270async function renameOverlapCleanupBackupWithRetry ( sourcePath : string , destinationPath : string ) : Promise < void > {
271271 let lastError : NodeJS . ErrnoException | null = null ;
272272
273- for ( let attempt = 0 ; attempt < BACKUP_RENAME_RETRY_DELAYS_MS . length ; attempt += 1 ) {
273+ for ( let attempt = 0 ; attempt <= BACKUP_RENAME_RETRY_DELAYS_MS . length ; attempt += 1 ) {
274274 try {
275275 await fs . rename ( sourcePath , destinationPath ) ;
276276 return ;
@@ -291,6 +291,8 @@ async function renameOverlapCleanupBackupWithRetry(sourcePath: string, destinati
291291 if ( lastError ) {
292292 throw lastError ;
293293 }
294+
295+ throw new Error ( `Failed to rename overlap cleanup backup ${ sourcePath } to ${ destinationPath } .` ) ;
294296}
295297
296298async function withNormalizedImportFile < T > (
@@ -977,11 +979,6 @@ function getSyncCapacityLimit(): number {
977979 }
978980 const message = `${ SYNC_MAX_ACCOUNTS_OVERRIDE_ENV } override value "${ override } " is not a positive integer; ignoring.` ;
979981 logWarn ( message ) ;
980- try {
981- process . stderr . write ( `${ message } \n` ) ;
982- } catch {
983- // best-effort warning for non-interactive shells
984- }
985982 return ACCOUNT_LIMITS . MAX_ACCOUNTS ;
986983}
987984
@@ -1259,7 +1256,7 @@ export async function previewCodexMultiAuthSyncedOverlapCleanup(): Promise<Codex
12591256export async function cleanupCodexMultiAuthSyncedOverlaps (
12601257 backupPath ?: string ,
12611258) : Promise < CodexMultiAuthCleanupResult > {
1262- return withAccountStorageTransaction ( async ( current , persist ) => {
1259+ return withAccountAndFlaggedStorageTransaction ( async ( { accounts : current , flagged : currentFlaggedStorage } , persist ) => {
12631260 const fallback = current ?? {
12641261 version : 3 as const ,
12651262 accounts : [ ] ,
@@ -1292,7 +1289,19 @@ export async function cleanupCodexMultiAuthSyncedOverlaps(
12921289 try {
12931290 const plan = buildCodexMultiAuthOverlapCleanupPlan ( fallback ) ;
12941291 if ( plan . nextStorage ) {
1295- await persist ( plan . nextStorage ) ;
1292+ const remainingRefreshTokens = new Set (
1293+ plan . nextStorage . accounts
1294+ . map ( ( account ) => normalizeTrimmedIdentity ( account . refreshToken ) )
1295+ . filter ( ( refreshToken ) : refreshToken is string => refreshToken !== undefined ) ,
1296+ ) ;
1297+ await persist . flagged ( {
1298+ version : 1 ,
1299+ accounts : currentFlaggedStorage . accounts . filter ( ( flaggedAccount ) => {
1300+ const refreshToken = normalizeTrimmedIdentity ( flaggedAccount . refreshToken ) ;
1301+ return refreshToken !== undefined && remainingRefreshTokens . has ( refreshToken ) ;
1302+ } ) ,
1303+ } ) ;
1304+ await persist . accounts ( plan . nextStorage ) ;
12961305 }
12971306 return plan . result ;
12981307 } catch ( error ) {
0 commit comments