Skip to content

Commit 45a7ee6

Browse files
committed
fix: hydrate newer refresh tokens from expired cli cache
1 parent 53488ef commit 45a7ee6

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

lib/accounts.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,24 @@ export class AccountManager {
175175
const cached = cache.get(email);
176176
if (!cached) continue;
177177

178-
if (typeof cached.expiresAt === "number" && cached.expiresAt <= now) {
179-
continue;
180-
}
178+
const cachedAccessUsable =
179+
typeof cached.expiresAt !== "number" || cached.expiresAt > now;
181180

182181
const missingOrExpired =
183182
!account.access || account.expires === undefined || account.expires <= now;
184-
if (missingOrExpired) {
183+
if (missingOrExpired && cachedAccessUsable) {
185184
account.access = cached.accessToken;
186185
if (typeof cached.expiresAt === "number") {
187186
account.expires = cached.expiresAt;
188187
}
189188
changed = true;
190189
}
191190

191+
if (cached.refreshToken && cached.refreshToken !== account.refreshToken) {
192+
account.refreshToken = cached.refreshToken;
193+
changed = true;
194+
}
195+
192196
if (
193197
!account.accountId &&
194198
cached.accountId &&

test/accounts-edge.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,14 @@ describe("accounts edge branches", () => {
154154
email: "match@example.com",
155155
accessToken: "refreshed-access",
156156
expiresAt: now + 300_000,
157+
refreshToken: "refreshed-refresh",
157158
accountId: "account-from-cache",
158159
},
159160
{
160161
email: "expired@example.com",
161162
accessToken: "expired-access",
162163
expiresAt: now - 1,
164+
refreshToken: "expired-refresh-updated",
163165
accountId: "expired-id",
164166
},
165167
{
@@ -180,12 +182,15 @@ describe("accounts edge branches", () => {
180182
const snapshot = manager.getAccountsSnapshot();
181183
const updated = snapshot[0];
182184
expect(updated?.access).toBe("refreshed-access");
185+
expect(updated?.refreshToken).toBe("refreshed-refresh");
183186
expect(updated?.accountId).toBe("account-from-cache");
184187
expect(updated?.accountIdSource).toBe("token");
185188

186189
const expired = snapshot[1];
187190
expect(expired?.access).toBe("existing-access");
188-
expect(expired?.accountId).toBeUndefined();
191+
expect(expired?.refreshToken).toBe("expired-refresh-updated");
192+
expect(expired?.accountId).toBe("expired-id");
193+
expect(expired?.accountIdSource).toBe("token");
189194
});
190195

191196
it("returns early when Codex CLI state has no usable cache entries", async () => {

0 commit comments

Comments
 (0)