Skip to content

Commit d4112b3

Browse files
committed
fix: stale pool-scope fallback, quota-only freshness, removal dialog scope
1 parent e6a93fc commit d4112b3

4 files changed

Lines changed: 61 additions & 9 deletions

File tree

‎gui/src/components/provider-workspace/ProviderAuthPanel.tsx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ export default function ProviderAuthPanel({
185185
const [grantAccount, setGrantAccount] = useState<OAuthAccountRow | null>(null);
186186
const [accountToRemove, setAccountToRemove] = useState<OAuthAccountRow | null>(null);
187187
const [removingAccount, setRemovingAccount] = useState(false);
188+
// The confirmation targets one provider's account. If the selected provider or
189+
// API base changes while the dialog is open, dismiss it: confirming afterwards
190+
// would remove an account the operator is no longer looking at.
191+
useEffect(() => {
192+
setAccountToRemove(null);
193+
setRemovingAccount(false);
194+
}, [item.name, apiBase]);
188195
const [accountFilter, setAccountFilter] = useState<AccountFilterKey>(() => {
189196
try {
190197
const saved = localStorage.getItem("ocx_account_filter");

‎gui/src/hooks/useProviderAccountPools.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,11 @@ export function useProviderAccountPools(deps: {
132132
const accountRequestGenerationRef = useRef<Record<string, number>>({});
133133
const rosterGenerationRef = useRef<Record<string, number>>({});
134134
const quotaGenerationRef = useRef<Record<string, number>>({});
135-
// Newest generation whose data was actually applied, per provider key. A request
136-
// superseded mid-flight by a newer one reports success when fresher data already
137-
// landed, so a manual quota refresh racing the visibility poll does not surface
138-
// a "refresh failed" state for data that is already on screen.
135+
// Newest generation whose QUOTA data was actually applied, per provider key. A
136+
// request superseded mid-flight by a newer one reports success only when fresher
137+
// quota already landed, so a manual refresh racing the visibility poll does not
138+
// surface a "refresh failed" state for data that is already on screen — and a
139+
// failed newer quota request still surfaces as failure.
139140
const freshLandedRef = useRef<Record<string, number>>({});
140141
const supersededByFresher = useCallback((key: string, generation: number): boolean => {
141142
if (!aliveRef.current || !mountedRef.current || serverRef.current !== apiBase) return false;
@@ -225,7 +226,9 @@ export function useProviderAccountPools(deps: {
225226
activeAccountId: data.activeAccountId ?? null,
226227
accounts: mergeQuotaRows(rows, current[provider]?.accounts ?? [], false),
227228
} } : current);
228-
if (currentRoster()) freshLandedRef.current[key] = generation;
229+
// NB: the cheap roster read is NOT recorded as fresh quota. A superseded
230+
// manual refresh must report success only when a newer request actually
231+
// landed quota, otherwise a failed newer quota request would read as success.
229232
setAccountLoadStates(current => currentRoster() ? { ...current, [provider]: "ready" } : current);
230233
if (!rows.some(supportsQuotaRead)) return true;
231234

‎src/providers/quota.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,17 +534,20 @@ export async function fetchProviderAccountQuotas(
534534
forceRefresh = false,
535535
providerConfig?: OcxProviderConfig,
536536
/**
537-
* When set, only this account is force-probed upstream; the rest serve from
538-
* cache. Unknown ids fall back to all-account probing so a refresh never
539-
* returns a partial roster.
537+
* When set to a known account id, only that account is force-probed upstream
538+
* and the rest serve from cache. An unknown id falls back to force-all so a
539+
* stale caller can never leave every account unforced.
540540
*/
541541
onlyAccountId?: string,
542542
): Promise<ProviderAccountQuota[]> {
543543
if (!supportsPerAccountQuota(provider)) return [];
544544
const set = getAccountSet(provider);
545545
if (!set) return [];
546+
const scoped = onlyAccountId && set.accounts.some(account => account.id === onlyAccountId)
547+
? onlyAccountId
548+
: undefined;
546549
return mapQuotaRoster(set.accounts, async account => {
547-
const forced = forceRefresh && (!onlyAccountId || account.id === onlyAccountId);
550+
const forced = forceRefresh && (!scoped || account.id === scoped);
548551
const entry = await fetchAccountQuota(provider, account.id, forced, providerConfig);
549552
const result: ProviderAccountQuota = {
550553
accountId: account.id,

‎tests/providers/provider-account-quota.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,45 @@ describe("fetchProviderAccountQuotas", () => {
103103
expect(calls).toBe(4);
104104
});
105105

106+
test("a scoped forced refresh probes only the target account", async () => {
107+
await seedTwoAccounts();
108+
let fiveHour = 50;
109+
let calls = 0;
110+
globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => {
111+
calls += 1;
112+
return new Response(usageBody(fiveHour, 10), { status: 200 });
113+
}) as typeof fetch;
114+
115+
const primed = await fetchProviderAccountQuotas("anthropic");
116+
expect(calls).toBe(2);
117+
const target = primed[0]?.accountId;
118+
expect(typeof target).toBe("string");
119+
// New upstream numbers: only the force-probed target may observe them.
120+
fiveHour = 70;
121+
const rows = await fetchProviderAccountQuotas("anthropic", true, undefined, target);
122+
expect(calls).toBe(3);
123+
const byId = Object.fromEntries(rows.map(row => [row.accountId, row.quota?.fiveHourPercent]));
124+
expect(byId[target]).toBe(70);
125+
for (const [id, value] of Object.entries(byId)) {
126+
if (id !== target) expect(value).toBe(50);
127+
}
128+
});
129+
130+
test("a stale scoped id falls back to force-all", async () => {
131+
await seedTwoAccounts();
132+
let calls = 0;
133+
globalThis.fetch = (async () => {
134+
calls += 1;
135+
return new Response(usageBody(50, 10), { status: 200 });
136+
}) as typeof fetch;
137+
138+
await fetchProviderAccountQuotas("anthropic");
139+
expect(calls).toBe(2);
140+
// Unknown id must not leave every account unforced.
141+
await fetchProviderAccountQuotas("anthropic", true, undefined, "acct-stale");
142+
expect(calls).toBe(4);
143+
});
144+
106145
test("a failing probe is flagged unavailable without dropping the other account", async () => {
107146
await seedTwoAccounts();
108147
globalThis.fetch = (async (_input: RequestInfo | URL, init?: RequestInit) => {

0 commit comments

Comments
 (0)