Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 冷启动不把未核验的 SuperGrok 周用量交给当前账号
Dashe43ed593ccb4
diff --git a/apps/desktop/src/main/__tests__/usageBroadcasterXaiSubscription.test.ts b/apps/desktop/src/main/__tests__/usageBroadcasterXaiSubscription.test.tsnew file mode 100644index 0000000000..f524ed63a1--- /dev/null+++ b/apps/desktop/src/main/__tests__/usageBroadcasterXaiSubscription.test.ts@@ -0,0 +1,66 @@+/**+ * xAI 周用量快照:磁盘 hydrate 不得在本进程 record 之前交给 Renderer。+ */++import { beforeEach, describe, expect, it, vi } from 'vitest';++const mocks = vi.hoisted(() => ({+ queryOne: vi.fn(),+ exec: vi.fn(async () => undefined),+ getCurrentUserId: vi.fn(() => 'user-1'),+}));++vi.mock('electron', () => ({+ BrowserWindow: { getAllWindows: () => [] },+}));+vi.mock('../logger', () => ({+ createLogger: () => ({ debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() }),+}));+vi.mock('../localDb/dailySpend', () => ({+ incrementDailySpend: vi.fn(),+ getTodaySpend: vi.fn(async () => 0),+ localDayKey: () => '2026-07-02',+}));+vi.mock('../localDb/dailyModelUsage', () => ({+ incrementDailyModelUsage: vi.fn(),+}));+vi.mock('../localDb/client/current', () => ({+ getDbClient: () => ({ queryOne: mocks.queryOne, exec: mocks.exec, drizzle: {} }),+}));+vi.mock('../localDb/index', () => ({+ getCurrentUserId: mocks.getCurrentUserId,+}));++describe('xai subscription snapshot hydration', () => {+ beforeEach(() => {+ vi.resetModules();+ mocks.queryOne.mockReset();+ mocks.exec.mockReset().mockResolvedValue(undefined);+ mocks.getCurrentUserId.mockReturnValue('user-1');+ });++ it('does not serve a disk snapshot until this process records one', async () => {+ const broadcaster = await import('../usageBroadcaster');+ mocks.queryOne.mockResolvedValue({+ snapshot: JSON.stringify({+ planLabel: 'Account A',+ creditUsagePercent: 40,+ accountFingerprint: 'aaaa',+ updatedAt: 1,+ }),+ });++ await expect(broadcaster.readXaiSubscriptionUsageSnapshot()).resolves.toBeNull();++ await broadcaster.recordXaiSubscriptionUsageSnapshot({+ planLabel: 'Account B',+ creditUsagePercent: 2,+ accountFingerprint: 'bbbb',+ updatedAt: 2,+ });+ await expect(broadcaster.readXaiSubscriptionUsageSnapshot()).resolves.toMatchObject({+ planLabel: 'Account B',+ accountFingerprint: 'bbbb',+ });+ });+});diff --git a/apps/desktop/src/main/bootstrap-electron.ts b/apps/desktop/src/main/bootstrap-electron.tsindex 47b824b555..ff6ff18f74 100644--- a/apps/desktop/src/main/bootstrap-electron.ts+++ b/apps/desktop/src/main/bootstrap-electron.ts@@ -3777,10 +3777,11 @@ const registerIpcHandlers = () => {clearXaiDiscoveredModels();clearXaiMediaModels();clearXaiRateLimitSnapshot();- void clearXaiSubscriptionUsageSnapshot();- void syncXaiSubscriptionUsageForAuthChange().then(() => {+ void (async () => {+ await clearXaiSubscriptionUsageSnapshot();+ await syncXaiSubscriptionUsageForAuthChange();broadcastXaiAuthStateChanged();- });+ })();});// xAI(SuperGrok 订阅)OAuth —— 与 claude-oauth 同形态。登录成功后 bridge 的 xai provider 立即可用@@ -3793,7 +3794,7 @@ const registerIpcHandlers = () => {resetProviderModelAutoRefreshCooldowns('xai');// 新凭证在 runGrokOAuthLogin 返回前已经落盘。先同步关掉旧周用量读取窗口,// 再去做模型磁盘清理等 await,避免换号间隙里 IPC read 仍返回账号 A 的快照。- void clearXaiSubscriptionUsageSnapshot();+ await clearXaiSubscriptionUsageSnapshot();// 登录可直接覆盖旧 SuperGrok 账号:先清旧世代内存,再直接读新账号官方清单。// 这里不能先恢复同一 Cindy owner 的磁盘 LKG,否则 A→B 重登会短暂展示 A 的成员。clearXaiDiscoveredModels();@@ -3821,7 +3822,7 @@ const registerIpcHandlers = () => {try {logoutGrok();resetProviderModelAutoRefreshCooldowns('xai');- void clearXaiSubscriptionUsageSnapshot();+ await clearXaiSubscriptionUsageSnapshot();clearXaiDiscoveredModels();clearXaiMediaModels();} catch (err) {diff --git a/apps/desktop/src/main/usageBroadcaster.ts b/apps/desktop/src/main/usageBroadcaster.tsindex 15ff28b269..c6a97b88ad 100644--- a/apps/desktop/src/main/usageBroadcaster.ts+++ b/apps/desktop/src/main/usageBroadcaster.ts@@ -901,6 +901,12 @@ let xaiSubscriptionUsageHydrated = false;let xaiSubscriptionUsageSnapshot: XaiSubscriptionUsageSnapshot | null = null;let xaiSubscriptionUsageLoadPromise: Promise<void> | null = null;let xaiSubscriptionUsageGeneration = 0;+/**+ * 磁盘 hydrate 不能直接给 Renderer。换号后若 DELETE 没落盘就退出,+ * 冷启动只按 agent_kind='xai' 读会把账号 A 的套餐/周用量交给账号 B。+ * 只有本进程成功 record 过一次当前凭证的快照,才允许读出。+ */+let xaiSubscriptionUsageServable = false;function resetXaiSubscriptionUsageCacheIfOwnerChanged(): void {const owner = currentAccountUsageOwner();@@ -911,6 +917,7 @@ function resetXaiSubscriptionUsageCacheIfOwnerChanged(): void {if (isFirstInit) return;xaiSubscriptionUsageHydrated = false;xaiSubscriptionUsageSnapshot = null;+ xaiSubscriptionUsageServable = false;xaiSubscriptionUsageGeneration += 1;}@@ -979,6 +986,7 @@ export async function recordXaiSubscriptionUsageSnapshot(snapshot: unknown): Pro};}const next = xaiSubscriptionUsageSnapshot;+ xaiSubscriptionUsageServable = true;broadcastXaiSubscriptionUsage(next);if (!xaiSubscriptionUsageHydrated) {@@ -1013,6 +1021,7 @@ export async function clearXaiSubscriptionUsageSnapshot(): Promise<void> {resetXaiSubscriptionUsageCacheIfOwnerChanged();xaiSubscriptionUsageHydrated = true;xaiSubscriptionUsageSnapshot = null;+ xaiSubscriptionUsageServable = false;xaiSubscriptionUsageGeneration += 1;broadcastXaiSubscriptionUsage(null);@@ -1031,7 +1040,7 @@ export async function clearXaiSubscriptionUsageSnapshot(): Promise<void> {export async function readXaiSubscriptionUsageSnapshot(): Promise<XaiSubscriptionUsageSnapshot | null> {await ensureXaiSubscriptionUsageLoaded();- return xaiSubscriptionUsageSnapshot;+ return xaiSubscriptionUsageServable ? xaiSubscriptionUsageSnapshot : null;}// ── 内部广播 ─────────────────────────────────────────────────────────────────