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): invalidate quota when credential source is removed

qianbod45514779513
diff --git a/apps/desktop/src/main/model-access/__tests__/quotaCredentialState.test.ts b/apps/desktop/src/main/model-access/__tests__/quotaCredentialState.test.ts
index 78fa3a58c0..7dc31b0491 100644
--- a/apps/desktop/src/main/model-access/__tests__/quotaCredentialState.test.ts
+++ b/apps/desktop/src/main/model-access/__tests__/quotaCredentialState.test.ts
@@ -21,6 +21,9 @@ it('invalidates source changes and logout but not repeated successful synchroniz
const generation = state.quotaCredentialGeneration();
state.noteQuotaCredentialStatus('owner', status);
expect(state.quotaCredentialGeneration()).toBe(generation);
+ state.noteQuotaCredentialStatus('owner', { ...status, state: 'syncing' });
+ state.noteQuotaCredentialStatus('owner', status);
+ expect(state.quotaCredentialGeneration()).toBe(generation);
state.noteQuotaCredentialStatus('owner', { ...status, endpoint: 'https://new.example.test' });
expect(state.quotaCredentialGeneration()).toBe(generation + 1);
state.noteQuotaCredentialStatus(null, { ...status, state: 'idle' });
@@ -28,3 +31,17 @@ it('invalidates source changes and logout but not repeated successful synchroniz
expect(changed).toHaveBeenCalledTimes(3);
off();
});
+
+it.each([null, 'manual'] as const)('invalidates a removed/replaced source in non-ok state (%s)', async (source) => {
+ const state = await import('../quotaCredentialState');
+ const changed = vi.fn();
+ state.noteQuotaCredentialStatus('owner', status);
+ state.onQuotaCredentialSourceChanged(changed);
+ const generation = state.quotaCredentialGeneration();
+ state.noteQuotaCredentialStatus('owner', { ...status, state: 'failed' });
+ expect(changed).not.toHaveBeenCalled();
+ state.noteQuotaCredentialStatus('owner', { ...status, state: 'failed', source, endpoint: null });
+ expect(state.quotaCredentialGeneration()).toBe(generation + 1);
+ expect(changed).toHaveBeenCalledTimes(1);
+ expect(state.quotaCredentialsReady('owner', status.endpoint)).toBe(false);
+});
diff --git a/apps/desktop/src/main/model-access/quotaCredentialState.ts b/apps/desktop/src/main/model-access/quotaCredentialState.ts
index 6b8b90d44c..c518793512 100644
--- a/apps/desktop/src/main/model-access/quotaCredentialState.ts
+++ b/apps/desktop/src/main/model-access/quotaCredentialState.ts
@@ -5,18 +5,24 @@ import type { ModelAccessStatus } from '../../shared/modelAccess.js';
// sync in this process. No keys or endpoint values leave the main process.
let owner: string | null = null;
let endpoint: string | null = null;
+let source: ModelAccessStatus['source'] = null;
let ready = false;
+let synchronized = false;
let generation = 0;
const listeners = new Set<(ownerKey: string | null) => void>();
export function noteQuotaCredentialStatus(ownerKey: string | null, status: ModelAccessStatus): void {
const nextEndpoint = status.endpoint?.trim().replace(/\/+$/, '') || null;
const reset = owner !== ownerKey || status.state === 'idle';
- const changed = status.state === 'ok' && endpoint !== nextEndpoint;
- if (reset) endpoint = null;
+ const nextReady = status.state === 'ok' && status.source === 'server' && !!nextEndpoint;
+ const sourceChanged = source !== status.source || endpoint !== nextEndpoint;
+ if (reset || sourceChanged) synchronized = false;
+ const changed = sourceChanged || (!synchronized && nextReady);
+ if (nextReady) synchronized = true;
owner = ownerKey;
- ready = status.state === 'ok' && status.source === 'server' && !!nextEndpoint;
- if (ready) endpoint = nextEndpoint;
+ ready = nextReady;
+ source = status.source;
+ endpoint = nextEndpoint;
if (reset || changed) {
generation++;
for (const listener of listeners) listener(ownerKey);
diff --git a/apps/desktop/src/main/usage/__tests__/organizationQuotaRefresh.test.ts b/apps/desktop/src/main/usage/__tests__/organizationQuotaRefresh.test.ts
index 55a861073d..5579fc8464 100644
--- a/apps/desktop/src/main/usage/__tests__/organizationQuotaRefresh.test.ts
+++ b/apps/desktop/src/main/usage/__tests__/organizationQuotaRefresh.test.ts
@@ -96,6 +96,21 @@ describe('Cindy AI enterprise quota refresh', () => {
});
describe('quota gateway migration boundary', () => {
+ it.each(['failed', 'disabled'] as const)('clears legacy quota when credentials are removed in %s state', async (syncState) => {
+ mocks.managed.mockResolvedValue(undefined);
+ const api = await import('../claudeAccountUsage');
+ await api.triggerClaudeAccountUsageRefresh(true);
+ expect(api.readClaudeAccountUsageSnapshot()?.maxBudget).toBe(20);
+ const state = await import('../../model-access/quotaCredentialState');
+ state.noteQuotaCredentialStatus('cloud:owner-a:1', { state: syncState, source: 'server', endpoint: mocks.endpoint, accountTier: null });
+ expect(api.readClaudeAccountUsageSnapshot()?.maxBudget).toBe(20);
+ state.noteQuotaCredentialStatus('cloud:owner-a:1', { state: syncState, source: null, endpoint: null, accountTier: null });
+ expect(api.readClaudeAccountQuotaClear()).toEqual({ quotaState: 'invalidated' });
+ await vi.advanceTimersByTimeAsync(0);
+ expect(api.readClaudeAccountUsageSnapshot()).toBeNull();
+ expect(mocks.outbound).toHaveBeenCalledTimes(2);
+ });
+
it('blocks persisted legacy credentials before this process completes credential sync', async () => {
const state = await import('../../model-access/quotaCredentialState');
state.noteQuotaCredentialStatus('cloud:owner-a:1', { state: 'idle', source: 'server', endpoint: mocks.endpoint, accountTier: null });