Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): 换号队列重读当前凭证,空快照也继续催刷

Dash7ac6dfb56368
diff --git a/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsageRefresh.test.ts b/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsageRefresh.test.ts
index 2e4662d807..20a9ce244f 100644
--- a/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsageRefresh.test.ts
+++ b/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsageRefresh.test.ts
@@ -205,6 +205,46 @@ describe('createXaiSubscriptionUsageReader', () => {
expect(recordSnapshot).toHaveBeenCalled();
});
+ it('does not let a late stale read overwrite a newer queued account', async () => {
+ let releaseFetchA!: () => void;
+ const fetchAGate = new Promise<void>((resolve) => {
+ releaseFetchA = resolve;
+ });
+ let releaseCache!: () => void;
+ const cacheGate = new Promise<void>((resolve) => {
+ releaseCache = resolve;
+ });
+ let cacheWaits = 0;
+ const fetchSnapshot = vi.fn(async (creds: XaiSubscriptionCredentialInfo) => {
+ if (creds.accessToken === 'token-a') await fetchAGate;
+ return makeSnapshot(creds.accessToken === 'token-b' ? 2 : 1);
+ });
+ const { deps, recordSnapshot } = makeDeps({ fetchSnapshot });
+ deps.readCachedSnapshot = vi.fn(async () => {
+ cacheWaits += 1;
+ if (cacheWaits >= 2) await cacheGate;
+ return null;
+ });
+ const reader = createXaiSubscriptionUsageReader(deps, { throttleMs: 1 });
+
+ const first = reader.read();
+ await settle();
+ const lateRead = reader.read();
+ await settle();
+ deps.readCredentials = vi.fn(async () => ({ accessToken: 'token-b' }));
+ await reader.syncForCredentialChange();
+ releaseCache();
+ await lateRead;
+ releaseFetchA();
+ await first;
+ await settle();
+ await settle();
+ await settle();
+
+ expect(fetchSnapshot).toHaveBeenCalledWith({ accessToken: 'token-b' });
+ expect(recordSnapshot).toHaveBeenCalledWith(makeSnapshot(2));
+ });
+
it('does not queue a second fetch for the same in-flight token', async () => {
let release!: () => void;
const gate = new Promise<void>((resolve) => {
diff --git a/apps/desktop/src/main/usage/xaiSubscriptionUsageRefresh.ts b/apps/desktop/src/main/usage/xaiSubscriptionUsageRefresh.ts
index c930df2bbb..f3dbbb0210 100644
--- a/apps/desktop/src/main/usage/xaiSubscriptionUsageRefresh.ts
+++ b/apps/desktop/src/main/usage/xaiSubscriptionUsageRefresh.ts
@@ -55,7 +55,8 @@ export function createXaiSubscriptionUsageReader(
let inFlight: Promise<void> | null = null;
let inFlightToken: string | null = null;
let inFlightGeneration = 0;
- let queuedCredentials: XaiSubscriptionCredentialInfo | null = null;
+ // 只记「需要再跑一次」,不存 token。排空时重读当前凭证,迟到的旧 token 盖不掉新号。
+ let queuedReplay = false;
let lastRefreshAt = 0;
let backoffMs = 0;
let backoffUntil = 0;
@@ -74,7 +75,7 @@ export function createXaiSubscriptionUsageReader(
async function clearCachedSnapshot(opts: { resetThrottle: boolean }): Promise<void> {
refreshGeneration += 1;
- queuedCredentials = null;
+ queuedReplay = false;
if (opts.resetThrottle) {
lastRefreshAt = 0;
backoffMs = 0;
@@ -142,15 +143,12 @@ export function createXaiSubscriptionUsageReader(
): void {
const now = deps.now();
if (inFlight) {
- // 同凭证且仍是同一世代:复用在途请求。clear 已经提升 generation 时,
- // 在途结果会被丢掉,必须再排一次,否则新账号周用量会空到下一次 turn。
- if (inFlightToken === credentials.accessToken) {
- if (inFlightGeneration !== refreshGeneration) {
- queuedCredentials = credentials;
- }
+ // 同凭证且仍是同一世代:复用在途请求。clear 已经提升 generation,
+ // 或后来的调用带了别的 token 时,只记「要再跑」,排空时重读当前凭证。
+ if (inFlightToken === credentials.accessToken && inFlightGeneration === refreshGeneration) {
return;
}
- queuedCredentials = credentials;
+ queuedReplay = true;
return;
}
if (!opts?.bypassThrottle) {
@@ -158,7 +156,8 @@ export function createXaiSubscriptionUsageReader(
if (lastRefreshAt > 0 && now - lastRefreshAt < throttleMs) return;
}
const generation = refreshGeneration;
- inFlightToken = credentials.accessToken;
+ const startedToken = credentials.accessToken;
+ inFlightToken = startedToken;
inFlightGeneration = generation;
const pending = runRefresh(credentials, generation).finally(() => {
if (inFlight === pending) {
@@ -166,9 +165,15 @@ export function createXaiSubscriptionUsageReader(
inFlightToken = null;
inFlightGeneration = 0;
}
- const queued = queuedCredentials;
- queuedCredentials = null;
- if (queued) startRefresh(queued, { bypassThrottle: true });
+ const shouldReplay = queuedReplay;
+ queuedReplay = false;
+ if (!shouldReplay) return;
+ void (async () => {
+ const latest = await readCredentialsSafe();
+ if (!latest) return;
+ if (latest.accessToken === startedToken && refreshGeneration === generation) return;
+ startRefresh(latest, { bypassThrottle: true });
+ })();
});
inFlight = pending;
}
diff --git a/apps/desktop/src/renderer/__tests__/todaySpendChip.test.ts b/apps/desktop/src/renderer/__tests__/todaySpendChip.test.ts
index 075608cc8a..b1cfa100ef 100644
--- a/apps/desktop/src/renderer/__tests__/todaySpendChip.test.ts
+++ b/apps/desktop/src/renderer/__tests__/todaySpendChip.test.ts
@@ -406,7 +406,8 @@ describe('TodaySpendChip dashboard routing', () => {
expect(source).toContain(
'const hasPendingResetWindow = chipWindows.some((window) => window.resetPending);',
);
- expect(source).toContain('const xaiWeeklyStale = usesXaiQuotaForm');
+ expect(source).toContain('const xaiNeedsWeeklyRefresh = usesXaiQuotaForm');
+ expect(source).not.toContain('Boolean(xaiSubscriptionUsage)');
});
it('does not treat missing subscription credits as exhausted usage', () => {
diff --git a/apps/desktop/src/renderer/components/settings/ProvidersSection.tsx b/apps/desktop/src/renderer/components/settings/ProvidersSection.tsx
index e2d2c5adba..4249199dd8 100644
--- a/apps/desktop/src/renderer/components/settings/ProvidersSection.tsx
+++ b/apps/desktop/src/renderer/components/settings/ProvidersSection.tsx
@@ -802,7 +802,7 @@ function XaiAssetModule({ connected }: { connected: boolean }) {
return () => window.clearInterval(timer);
}, [connected]);
useEffect(() => {
- if (!connected || !usage) return;
+ if (!connected) return;
if (isXaiWeeklyUsageCurrent(usage, nowMs)) return;
requestXaiSubscriptionRefresh();
}, [connected, usage, nowMs]);
diff --git a/apps/desktop/src/renderer/components/status/TodaySpendChip.tsx b/apps/desktop/src/renderer/components/status/TodaySpendChip.tsx
index 37e1a511e5..a10808ad9b 100644
--- a/apps/desktop/src/renderer/components/status/TodaySpendChip.tsx
+++ b/apps/desktop/src/renderer/components/status/TodaySpendChip.tsx
@@ -1538,11 +1538,10 @@ export function TodaySpendChip({
// 且无空闲期 app-server 催刷通道, 靠下一个 turn 事件或悬念超时回落兜底;
// - Claude 订阅形态催订阅端点。
const hasPendingResetWindow = chipWindows.some((window) => window.resetPending);
- const xaiWeeklyStale = usesXaiQuotaForm
- && Boolean(xaiSubscriptionUsage)
+ const xaiNeedsWeeklyRefresh = usesXaiQuotaForm
&& !isXaiWeeklyUsageCurrent(xaiSubscriptionUsage, windowLabelNowMs);
React.useEffect(() => {
- if (!hasPendingResetWindow && !xaiWeeklyStale) return;
+ if (!hasPendingResetWindow && !xaiNeedsWeeklyRefresh) return;
if (isChatgptBridge) {
requestCodexAccountRefresh();
} else if (usesXaiQuotaForm) {
@@ -1550,7 +1549,7 @@ export function TodaySpendChip({
} else if (isClaudeSubscription && !usesCodexQuotaForm) {
requestClaudeSubscriptionRefresh();
}
- }, [hasPendingResetWindow, xaiWeeklyStale, isChatgptBridge, isClaudeSubscription, usesCodexQuotaForm, usesXaiQuotaForm, windowLabelNowMs]);
+ }, [hasPendingResetWindow, xaiNeedsWeeklyRefresh, isChatgptBridge, isClaudeSubscription, usesCodexQuotaForm, usesXaiQuotaForm, windowLabelNowMs]);
const isDashboardClickable = usageDashboardUrl !== null;
const handleClick = () => {