Skip to content

Commit f7c1571

Browse files
committed
fix: address usage polling review regressions
1 parent 7745561 commit f7c1571

12 files changed

Lines changed: 164 additions & 60 deletions

File tree

‎apps/mobile/src/features/threads/ThreadDetailScreen.tsx‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useInstanceLimitsRefresh } from "../usage/useInstanceLimitsRefresh";
12
import { ChildThreadApprovals } from "./ChildThreadApprovals";
23
import { useOpenThreadInspector } from "./thread-inspector-navigation";
34
import { useSidebarWorkspaceTabs } from "../workspace/useSystemWorkspaceTabBar";
@@ -942,6 +943,17 @@ function ThreadDetailBody(
942943
usageLimitsPanel.now,
943944
)
944945
: null;
946+
useInstanceLimitsRefresh(
947+
appAtomRegistry,
948+
props.environmentId,
949+
props.selectedThread.modelSelection.instanceId,
950+
props.serverConfig?.providers.find(
951+
(provider) => provider.instanceId === props.selectedThread.modelSelection.instanceId,
952+
)?.driver,
953+
usageLimitsPanel !== null &&
954+
activeUserInputRequestId === null &&
955+
props.connectionStateLabel === "connected",
956+
);
945957
const showUsageLimits = (report: UsageLimitsReport | null) =>
946958
setUsageLimitsPanel(
947959
report === null
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { useIsFocused } from "@react-navigation/native";
2+
import { useEffect } from "react";
3+
import type { EnvironmentId, ProviderInstanceId, ProviderDriverKind } from "@t3tools/contracts";
4+
import { runAtomCommand } from "@t3tools/client-runtime/state/runtime";
5+
import type { AtomRegistry } from "effect/reactivity";
6+
import { isAppActive, subscribeAppState } from "../../lib/appState";
7+
import { serverEnvironment } from "../../state/server";
8+
9+
export function useInstanceLimitsRefresh(
10+
registry: AtomRegistry.AtomRegistry,
11+
environmentId: EnvironmentId,
12+
instanceId: ProviderInstanceId,
13+
driver: ProviderDriverKind | undefined,
14+
visible: boolean,
15+
) {
16+
const focused = useIsFocused();
17+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- quota demand follows the native panel and app lifecycle
18+
useEffect(() => {
19+
if (!visible || !focused) return;
20+
const refresh = () => {
21+
if (!isAppActive()) return;
22+
void runAtomCommand(
23+
registry,
24+
serverEnvironment.refreshProviders,
25+
{ environmentId, input: { instanceId, usage: true } },
26+
{ reportFailure: false },
27+
);
28+
if (driver)
29+
void runAtomCommand(
30+
registry,
31+
serverEnvironment.refreshProviders,
32+
{ environmentId, input: { usageSources: true, usageProviders: [driver] } },
33+
{ reportFailure: false },
34+
);
35+
};
36+
refresh();
37+
const unsubscribe = subscribeAppState(refresh);
38+
const timer = setInterval(refresh, 5 * 60_000);
39+
return () => {
40+
unsubscribe();
41+
clearInterval(timer);
42+
};
43+
}, [registry, environmentId, instanceId, driver, visible, focused]);
44+
}

‎apps/mobile/src/widgets/useSubscriptionUsage.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,35 @@ import { isAppActive, subscribeAppState } from "../lib/appState";
66
import { appAtomRegistry } from "../state/atom-registry";
77
import { environmentPresentations } from "../state/presentation";
88
import { serverEnvironment } from "../state/server";
9-
import { createWidgetRefresher, WIDGET_REFRESH_INTERVAL } from "./subscriptionUsageSnapshot";
9+
import { createWidgetRefresher } from "./subscriptionUsageSnapshot";
1010

1111
/**
1212
* App-lifetime worker that keeps provider quotas fresh for the usage widget:
13-
* on resume, on every connection change, and on a fixed interval while active.
13+
* on resume and connection changes; periodic reads belong to visible usage UI.
1414
* Each environment is refreshed at most once per interval.
1515
*/
1616
export function installSubscriptionUsageRefresh(): () => void {
1717
const refresh = createWidgetRefresher((environmentId: EnvironmentId) =>
1818
runAtomCommand(
1919
appAtomRegistry,
2020
serverEnvironment.refreshProviders,
21-
{ environmentId, input: {} },
21+
{ environmentId, input: { usage: true } },
2222
{ label: serverEnvironment.refreshProviders.label, reportFailure: false },
2323
),
2424
);
25+
let lastConnections = "";
2526
const update = () => {
26-
if (!isAppActive() || !appAtomRegistry.get(environmentCatalog.catalogValueAtom).isReady) return;
27+
if (!isAppActive()) {
28+
lastConnections = "";
29+
return;
30+
}
31+
if (!appAtomRegistry.get(environmentCatalog.catalogValueAtom).isReady) return;
2732
const connected = [...appAtomRegistry.get(environmentPresentations.presentationsAtom)]
2833
.filter(([, presentation]) => presentation.connection.phase === "connected")
2934
.map(([id]) => id);
35+
const key = [...connected].sort().join(",");
36+
if (key === lastConnections) return;
37+
lastConnections = key;
3038
void refresh(connected, Date.now());
3139
};
3240

@@ -37,11 +45,9 @@ export function installSubscriptionUsageRefresh(): () => void {
3745
{ immediate: true },
3846
);
3947
const unsubscribeAppState = subscribeAppState(update);
40-
const timer = setInterval(update, WIDGET_REFRESH_INTERVAL);
4148
return () => {
4249
unsubscribeCatalog();
4350
unsubscribePresentations();
4451
unsubscribeAppState();
45-
clearInterval(timer);
4652
};
4753
}

‎apps/server/src/provider/Drivers/ClaudeDriver.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv, Path.
215215
timeToLive: () => CAPABILITIES_PROBE_TTL,
216216
},
217217
);
218+
let lastAccount: ClaudeCapabilitiesProbe | undefined;
218219
const resolveCapabilities = Effect.gen(function* () {
219220
let includeUsage = yield* UsagePolling;
220221
if (includeUsage && usageHttpClient) {
@@ -227,7 +228,18 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv, Path.
227228
)
228229
includeUsage = false;
229230
}
230-
return (yield* Cache.get(capabilitiesProbeCache, includeUsage)).capabilities;
231+
const capabilities: ClaudeCapabilitiesProbe = (yield* Cache.get(
232+
capabilitiesProbeCache,
233+
includeUsage,
234+
)).capabilities;
235+
if (
236+
lastAccount !== undefined &&
237+
(lastAccount.email?.toLowerCase() !== capabilities.email?.toLowerCase() ||
238+
lastAccount.workspaceId !== capabilities.workspaceId)
239+
)
240+
yield* Cache.invalidate(capabilitiesProbeCache, !includeUsage);
241+
lastAccount = capabilities;
242+
return capabilities;
231243
}).pipe(
232244
Effect.provideService(FileSystem.FileSystem, fileSystem),
233245
Effect.provideService(Path.Path, path),

‎apps/server/src/provider/ProviderInstanceRegistry.test.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ describe("ProviderInstanceRegistry — multi-instance codex slice", () => {
483483
const claudeId = ProviderInstanceId.make("claude_probe_recovery");
484484
let probeCount = 0;
485485
let successfulProbeCount = 0;
486+
let overrideAccount: string | undefined;
486487
let failProbe = false;
487488
const query = vi.spyOn(ClaudeSdk, "query").mockImplementation(
488489
() =>
@@ -501,7 +502,10 @@ describe("ProviderInstanceRegistry — multi-instance codex slice", () => {
501502
],
502503
account: {
503504
email:
504-
successfulProbeCount === 1 ? "[email protected]" : "[email protected]",
505+
overrideAccount ??
506+
(successfulProbeCount === 1
507+
508+
505509
subscriptionType: "pro",
506510
tokenSource: "oauth",
507511
},
@@ -595,6 +599,20 @@ describe("ProviderInstanceRegistry — multi-instance codex slice", () => {
595599
expect(repeatedFailure.auth).toEqual(sameAccountFailure.auth);
596600
expect(repeatedFailure.slashCommands).toEqual(sameAccountFailure.slashCommands);
597601
expect(probeCount).toBe(sameAccountFailureProbeCount);
602+
603+
failProbe = false;
604+
overrideAccount = "[email protected]";
605+
yield* claude!.invalidateCaches ?? Effect.void;
606+
yield* claude!.snapshot.refresh;
607+
yield* TestClock.adjust("1 minute");
608+
yield* claude!.snapshot.refreshUsage!;
609+
expect((yield* claude!.snapshot.getSnapshot).auth.email).toBe(overrideAccount);
610+
yield* TestClock.adjust("4 minutes");
611+
overrideAccount = "[email protected]";
612+
const changedHealth = yield* claude!.snapshot.refresh;
613+
expect(changedHealth.auth.email).toBe(overrideAccount);
614+
yield* claude!.snapshot.refreshUsage!;
615+
expect((yield* claude!.snapshot.getSnapshot).auth.email).toBe(overrideAccount);
598616
}).pipe(Effect.provide(layerTest)),
599617
);
600618

‎apps/server/src/provider/ProviderRegistry.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,8 @@ export const layer = Layer.effect(
635635
instances.filter(
636636
(instance) => instanceId === undefined || instance.instanceId === instanceId,
637637
),
638-
(instance) => refreshOneSource(instance, true),
638+
(instance) =>
639+
refreshOneSource(instance, true).pipe(Effect.catchCause(recoverRefreshFailure)),
639640
{ concurrency: "unbounded" },
640641
);
641642
return yield* Ref.get(providersRef);
@@ -1093,7 +1094,8 @@ export const layer = Layer.effect(
10931094

10941095
return {
10951096
getProviders: Ref.get(providersRef),
1096-
refreshUsage,
1097+
refreshUsage: (instanceId) =>
1098+
refreshUsage(instanceId).pipe(Effect.catchCause(recoverRefreshFailure)),
10971099
refresh: (provider?: ProviderDriverKind) =>
10981100
refresh(provider).pipe(Effect.catchCause(recoverRefreshFailure)),
10991101
refreshInstance: (instanceId: ProviderInstanceId) =>

‎apps/web/src/hooks/useLiveRefresh.browser.test.tsx‎

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,31 @@ function Probe(props: {
2020
}
2121

2222
describe("useLiveRefresh", () => {
23-
it("defers a hidden mounted usage surface until the window becomes visible", async () => {
24-
const refresh = vi.fn();
25-
let visibility: DocumentVisibilityState = "hidden";
26-
using _visibility = vi
27-
.spyOn(document, "visibilityState", "get")
28-
.mockImplementation(() => visibility);
29-
using _timers = fakeTimers({ now: 2_000_000 });
30-
// A bare hook probe; nothing here navigates.
31-
await using _screen = await render(
32-
<Probe refresh={refresh} viewKey="hidden-usage" refreshOnShow />,
33-
{ router: false },
34-
);
35-
expect(refresh).not.toHaveBeenCalled();
36-
visibility = "visible";
37-
await act(() => document.dispatchEvent(new Event("visibilitychange")));
38-
expect(refresh).toHaveBeenCalledTimes(1);
39-
});
23+
it.each([true, false])(
24+
"refreshes a hidden mount on arrival with refreshOnShow=%s",
25+
async (refreshOnShow) => {
26+
const refresh = vi.fn();
27+
let visibility: DocumentVisibilityState = "hidden";
28+
using _visibility = vi
29+
.spyOn(document, "visibilityState", "get")
30+
.mockImplementation(() => visibility);
31+
using _timers = fakeTimers({ now: 2_000_000 });
32+
// A bare hook probe; nothing here navigates.
33+
await using _screen = await render(
34+
<Probe
35+
refresh={refresh}
36+
viewKey={`hidden-usage:${refreshOnShow}`}
37+
refreshOnShow={refreshOnShow}
38+
/>,
39+
{ router: false },
40+
);
41+
expect(refresh).not.toHaveBeenCalled();
42+
await act(() => vi.advanceTimersByTime(30_000));
43+
visibility = "visible";
44+
await act(() => document.dispatchEvent(new Event("visibilitychange")));
45+
expect(refresh).toHaveBeenCalledTimes(1);
46+
},
47+
);
4048

4149
it("with refreshOnShow, reads on every show unless it read within the floor", async () => {
4250
const refresh = vi.fn();

‎apps/web/src/hooks/useLiveRefresh.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,8 @@ export function useLiveRefresh(
111111
if (!refreshWhileIdle && now - lastInteractedAt >= LIVE_REFRESH_IDLE_AFTER_MS) return;
112112
const lastRefreshedAt = lastRefreshedAtByView.get(viewId);
113113
if (lastRefreshedAt === undefined) {
114-
if (visible()) {
115-
if (refreshOnShow) read(now);
116-
else lastRefreshedAtByView.set(viewId, now);
117-
}
114+
if (!refreshOnShow) lastRefreshedAtByView.set(viewId, now);
115+
else if (visible()) read(now);
118116
return;
119117
}
120118
if (shouldRefreshOnArrival({ visible: visible(), now, lastRefreshedAt })) read(now);

‎packages/client-runtime/src/state/server.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,10 @@ export function createServerEnvironmentAtoms<R, E>(
14261426
input.cwd ?? null,
14271427
input.fresh ?? false,
14281428
input.refreshModels ?? false,
1429+
input.reloadSecrets ?? false,
1430+
input.usage ?? false,
1431+
input.usageSources ?? false,
1432+
input.usageProviders === undefined ? null : [...new Set(input.usageProviders)].sort(),
14291433
]),
14301434
},
14311435
}),

‎packages/provider-core/src/server/managedProvider.ts‎

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -278,46 +278,23 @@ export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(
278278
);
279279
});
280280

281-
const needsUsageRecovery = (snapshot: ServerProvider) =>
282-
input.checkUsageLimits !== undefined &&
283-
snapshot.installed &&
284-
snapshot.status === "ready" &&
285-
snapshot.auth.status === "authenticated" &&
286-
snapshot.usageLimits?.unavailable?.reason === "probeFailed";
287-
288281
const needsProviderRefresh = (snapshot: ServerProvider) =>
289282
input.refreshOnInterval !== false &&
290283
(typeof input.refreshOnInterval !== "function" || input.refreshOnInterval(snapshot));
291284

292285
const refreshOnInterval = Effect.fn("ManagedServerProvider.refreshOnInterval")(function* () {
293286
const { snapshot } = yield* Ref.get(snapshotStateRef);
294287
if (!snapshot.enabled) return;
295-
if (!(yield* UsagePolling) || !needsUsageRecovery(snapshot) || !input.checkUsageLimits) {
296-
if (needsProviderRefresh(snapshot)) {
297-
yield* applySnapshotBase(yield* input.getSettings, { forceRefresh: true });
298-
}
299-
return;
288+
if (needsProviderRefresh(snapshot)) {
289+
yield* applySnapshotBase(yield* input.getSettings, { forceRefresh: true });
300290
}
301-
const probed = yield* input.checkUsageLimits;
302-
const next = yield* Ref.modify(snapshotStateRef, (state) => {
303-
const usageLimits = resolveUsageLimitsAfterProbe({
304-
published: state.snapshot.usageLimits,
305-
probed,
306-
});
307-
const snapshot = withUsageLimits(state.snapshot, usageLimits);
308-
return [snapshot, { ...state, snapshot }] as const;
309-
});
310-
yield* PubSub.publish(changesPubSub, next);
311291
});
312292

313293
const shouldRefreshOnInterval = Effect.gen(function* () {
314294
const state = yield* Ref.get(snapshotStateRef);
315295
// A disabled instance has nothing to probe; a periodic check would only
316296
// republish a snapshot that differs in checkedAt.
317-
if (
318-
!state.snapshot.enabled ||
319-
(!needsUsageRecovery(state.snapshot) && !needsProviderRefresh(state.snapshot))
320-
) {
297+
if (!state.snapshot.enabled || !needsProviderRefresh(state.snapshot)) {
321298
return false;
322299
}
323300
const instanceId = state.snapshot.instanceId;

0 commit comments

Comments
 (0)