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): rebind quota credentials after same-owner repair

qianboee8dbd2b826f
diff --git a/apps/desktop/src/main/model-access/index.ts b/apps/desktop/src/main/model-access/index.ts
index f7a3a3cac4..76cd1bae69 100644
--- a/apps/desktop/src/main/model-access/index.ts
+++ b/apps/desktop/src/main/model-access/index.ts
@@ -1,4 +1,4 @@
-import { cloudQuotaOwnerKey, noteQuotaCredentialStatus } from './quotaCredentialState.js';
+import { cloudQuotaOwnerKey, noteQuotaCredentialStatus, setQuotaCredentialStatusReader } from './quotaCredentialState.js';
import { createByokCache } from './byokCache.js';
import { byokSnapshotSecretIo } from '../secrets/providerSecretStore.js';
import { registerByokIpc } from './byokIpc.js';
@@ -516,6 +516,12 @@ function mapServerError(err: unknown): never {
*/
export function initModelAccess(): void {
const sync = getSync();
+ setQuotaCredentialStatusReader(() => {
+ const auth = authManager.getAuthState();
+ if (auth.mode !== 'cloud' || !auth.user?.id || auth.user.id !== lastAuthUserId
+ || authManager.getActiveAuthRealm() !== lastAuthRealm) return null;
+ return sync.getStatus();
+ });
const byok = createByokRuntime(
broadcastReferenceModelPricing,
createByokCache(byokSnapshotSecretIo),
diff --git a/apps/desktop/src/main/model-access/quotaCredentialState.ts b/apps/desktop/src/main/model-access/quotaCredentialState.ts
index c518793512..3601059645 100644
--- a/apps/desktop/src/main/model-access/quotaCredentialState.ts
+++ b/apps/desktop/src/main/model-access/quotaCredentialState.ts
@@ -43,3 +43,15 @@ export function cloudQuotaOwnerKey(auth: Pick<AuthState, 'mode' | 'dataOwnerId'
return auth.mode === 'cloud' && auth.dataOwnerId
? `cloud:${auth.dataOwnerId}:${auth.ownerGeneration}` : null;
}
+
+// The sync owner must still match both membership and realm. The reader is wired
+// by Model Access; quota reads do not import its runtime or start credential sync.
+let readCurrentCredentialStatus: (() => ModelAccessStatus | null) | null = null;
+export function setQuotaCredentialStatusReader(reader: () => ModelAccessStatus | null): void {
+ readCurrentCredentialStatus = reader;
+}
+export function refreshQuotaCredentialOwner(ownerKey: string): void {
+ if (owner === ownerKey) return;
+ const status = readCurrentCredentialStatus?.();
+ if (status) noteQuotaCredentialStatus(ownerKey, status);
+}
diff --git a/apps/desktop/src/main/usage/__tests__/organizationQuotaRefresh.test.ts b/apps/desktop/src/main/usage/__tests__/organizationQuotaRefresh.test.ts
index 68f0ab80e8..a78ec0f073 100644
--- a/apps/desktop/src/main/usage/__tests__/organizationQuotaRefresh.test.ts
+++ b/apps/desktop/src/main/usage/__tests__/organizationQuotaRefresh.test.ts
@@ -187,3 +187,37 @@ it('notifies main-process consumers when finite quota recovers after a source ch
await vi.waitFor(() => expect(snapshots).toEqual([null, quota]));
off();
});
+
+it('rebinds ready legacy credentials after a silent same-owner generation repair', async () => {
+ mocks.managed.mockResolvedValue(undefined);
+ const state = await import('../../model-access/quotaCredentialState');
+ state.setQuotaCredentialStatusReader(() => ({ state: 'ok', source: 'server', endpoint: mocks.endpoint, accountTier: null }));
+ const api = await import('../claudeAccountUsage');
+ await api.triggerClaudeAccountUsageRefresh(true);
+ expect(api.readClaudeAccountUsageSnapshot()?.maxBudget).toBe(20);
+ mocks.auth.ownerGeneration++;
+ // No auth listener / credential status event is emitted by this repair.
+ await api.triggerClaudeAccountUsageRefresh();
+ await vi.waitFor(() => expect(api.readClaudeAccountUsageSnapshot()?.maxBudget).toBe(20));
+ expect(state.quotaCredentialsReady('cloud:owner-a:2', mocks.endpoint)).toBe(true);
+});
+it('does not rebind credentials when the sync identity no longer matches the realm', async () => {
+ mocks.managed.mockResolvedValue(undefined);
+ const state = await import('../../model-access/quotaCredentialState');
+ state.setQuotaCredentialStatusReader(() => null);
+ const api = await import('../claudeAccountUsage');
+ await api.triggerClaudeAccountUsageRefresh(true);
+ mocks.auth.ownerGeneration++;
+ mocks.outbound.mockClear();
+ await api.triggerClaudeAccountUsageRefresh();
+ expect(mocks.outbound).not.toHaveBeenCalled();
+ expect(api.readClaudeAccountUsageSnapshot()).toBeNull();
+});
+
+it('still throttles unavailable reads within one owner generation', async () => {
+ mocks.managed.mockResolvedValue(null);
+ const api = await import('../claudeAccountUsage');
+ await api.triggerClaudeAccountUsageRefresh();
+ await api.triggerClaudeAccountUsageRefresh();
+ expect(mocks.managed).toHaveBeenCalledTimes(1);
+});
diff --git a/apps/desktop/src/main/usage/claudeAccountUsage.ts b/apps/desktop/src/main/usage/claudeAccountUsage.ts
index 777e18d8c3..c076936c7d 100644
--- a/apps/desktop/src/main/usage/claudeAccountUsage.ts
+++ b/apps/desktop/src/main/usage/claudeAccountUsage.ts
@@ -1,4 +1,4 @@
-import { cloudQuotaOwnerKey, quotaCredentialsReady, quotaCredentialGeneration, onQuotaCredentialSourceChanged } from '../model-access/quotaCredentialState.js';
+import { refreshQuotaCredentialOwner, cloudQuotaOwnerKey, quotaCredentialsReady, quotaCredentialGeneration, onQuotaCredentialSourceChanged } from '../model-access/quotaCredentialState.js';
import type { ClaudeQuotaClear } from '../../shared/claudeQuotaState.js';
import { readOrganizationAccountUsage } from './organizationAccountUsage.js';
/**
@@ -100,6 +100,7 @@ let fetchInFlight = false;
let fetchOwnerKey: string | null = null;
let refreshQueued = false;
let lastFetchAt = 0;
+let lastFetchOwnerKey: string | null = null;
let idlePollTimer: NodeJS.Timeout | null = null;
interface LiteLlmUserInfo {
@@ -273,12 +274,14 @@ export async function triggerClaudeAccountUsageRefresh(force = false): Promise<v
if (fetchOwnerKey !== owner.key) refreshQueued = true;
return;
}
- if (!force && Date.now() - lastFetchAt < THROTTLE_MS) return;
+ if (!force && lastFetchOwnerKey === owner.key && Date.now() - lastFetchAt < THROTTLE_MS) return;
fetchInFlight = true;
fetchOwnerKey = owner.key;
lastFetchAt = Date.now();
+ lastFetchOwnerKey = owner.key;
try {
+ if (owner.key.startsWith('cloud:')) refreshQuotaCredentialOwner(owner.key);
const generation = quotaCredentialGeneration();
const next = await fetchOnce(owner.authenticatedUserId);
if (getAuthState().mode === 'cloud' && quotaCredentialGeneration() !== generation) return;