Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 换号后同 token 在途刷新被作废时补拉一次
Dash5d857e8891a0
diff --git a/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsageRefresh.test.ts b/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsageRefresh.test.tsindex 74b0506cc1..2e4662d807 100644--- a/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsageRefresh.test.ts+++ b/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsageRefresh.test.ts@@ -181,6 +181,30 @@ describe('createXaiSubscriptionUsageReader', () => {await sync;});+ it('replays the same token after a clear invalidates the in-flight generation', async () => {+ let release!: () => void;+ const gate = new Promise<void>((resolve) => {+ release = resolve;+ });+ let fetches = 0;+ const fetchSnapshot = vi.fn(async () => {+ fetches += 1;+ if (fetches === 1) await gate;+ return makeSnapshot(fetches);+ });+ const { deps, recordSnapshot } = makeDeps({ fetchSnapshot });+ const reader = createXaiSubscriptionUsageReader(deps, { throttleMs: 1 });+ const first = reader.read();+ await settle();+ await reader.syncForCredentialChange();+ release();+ await first;+ await settle();+ await settle();+ expect(fetchSnapshot.mock.calls.length).toBeGreaterThanOrEqual(2);+ expect(recordSnapshot).toHaveBeenCalled();+ });+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.tsindex 988f77667b..c930df2bbb 100644--- a/apps/desktop/src/main/usage/xaiSubscriptionUsageRefresh.ts+++ b/apps/desktop/src/main/usage/xaiSubscriptionUsageRefresh.ts@@ -54,6 +54,7 @@ export function createXaiSubscriptionUsageReader(let inFlight: Promise<void> | null = null;let inFlightToken: string | null = null;+ let inFlightGeneration = 0;let queuedCredentials: XaiSubscriptionCredentialInfo | null = null;let lastRefreshAt = 0;let backoffMs = 0;@@ -141,8 +142,14 @@ export function createXaiSubscriptionUsageReader(): void {const now = deps.now();if (inFlight) {- // 同凭证:复用在途请求,不要再排队绕过节流。换号才覆盖排队。- if (inFlightToken === credentials.accessToken) return;+ // 同凭证且仍是同一世代:复用在途请求。clear 已经提升 generation 时,+ // 在途结果会被丢掉,必须再排一次,否则新账号周用量会空到下一次 turn。+ if (inFlightToken === credentials.accessToken) {+ if (inFlightGeneration !== refreshGeneration) {+ queuedCredentials = credentials;+ }+ return;+ }queuedCredentials = credentials;return;}@@ -152,10 +159,12 @@ export function createXaiSubscriptionUsageReader(}const generation = refreshGeneration;inFlightToken = credentials.accessToken;+ inFlightGeneration = generation;const pending = runRefresh(credentials, generation).finally(() => {if (inFlight === pending) {inFlight = null;inFlightToken = null;+ inFlightGeneration = 0;}const queued = queuedCredentials;queuedCredentials = null;