@devries-cameron20 / t3code-1 / commits / 724f5c6bec
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): a refused hub read keeps the account's last good usage reading (#38397)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Johannes Mitschunas committed Nov 18, 2026 at 03:21 UTC 724f5c6bec1781c6d7d6ad09033030f198e4607d
2 files changed +138 -1
apps/server/src/usage/UsageLimitSources.test.ts
+101
@@ -175,6 +175,107 @@ it.effect("reads a management key from its secret provider", () => {
175 );
176 });
177
178 +it.effect("keeps an account's last good reading when the hub's next read of it is refused", () => {
179 + let refused = true;
180 + let workspace = "account-a";
181 + let listsIdentity = true;
182 + const readAccount = Effect.gen(function* () {
183 + const sources = yield* UsageLimitSources.make;
184 + return Effect.gen(function* () {
185 + yield* sources.refresh;
186 + const [source] = yield* sources.current;
187 + return source?.accounts[0]?.usageLimits;
188 + });
189 + });
190 + return Effect.gen(function* () {
191 + const read = yield* readAccount;
192 + // Nothing to keep yet: the account stays listed as unreadable.
193 + expect(yield* read).toMatchObject({ windows: [], unavailable: { reason: "probeFailed" } });
194 + refused = false;
195 + const good = yield* read;
196 + expect(good?.windows.map((window) => window.usedPercent)).toEqual([78]);
197 + expect(good?.unavailable).toBeUndefined();
198 + refused = true;
199 + expect(yield* read).toEqual(good);
200 + // The same auth file now holds another login, whose reading is unknown.
201 + workspace = "account-b";
202 + expect(yield* read).toMatchObject({ windows: [], unavailable: { reason: "probeFailed" } });
203 + // A file that lists no login cannot show whether the next one is the same.
204 + listsIdentity = false;
205 + refused = false;
206 + yield* read;
207 + refused = true;
208 + expect(yield* read).toMatchObject({ windows: [], unavailable: { reason: "probeFailed" } });
209 + }).pipe(
210 + Effect.provide([
211 + ServerSettings.layerTest({
212 + usageLimitSources: {
213 + [UsageLimitSourceId.make("hub")]: {
214 + kind: "cliproxy",
215 + url: "http://hub.test:8317",
216 + managementKey: "management-secret",
217 + enabled: true,
218 + },
219 + },
220 + }),
221 + Layer.mock(BackgroundPolicy.BackgroundPolicy)({
222 + shouldRunScopeWork: () => Effect.succeed(false),
223 + }),
224 + Layer.succeed(SecretResolver.SecretResolver, {
225 + label: (provider) => Effect.succeed(provider),
226 + read: (references) => Effect.succeed(references.map(() => undefined)),
227 + invalidate: () => Effect.void,
228 + }),
229 + Layer.succeed(
230 + HttpClient.HttpClient,
231 + HttpClient.make((request) =>
232 + Effect.sync(() => {
233 + if (new URL(request.url).pathname.endsWith("/auth-files")) {
234 + return HttpClientResponse.fromWeb(
235 + request,
236 + Response.json({
237 + files: [
238 + {
239 + id: "first.json",
240 + auth_index: "a",
241 + provider: "codex",
242 + ...(listsIdentity
243 + ? {
244 + email: "first@example.com",
245 + id_token: { chatgpt_account_id: workspace },
246 + }
247 + : {}),
248 + },
249 + ],
250 + }),
251 + );
252 + }
253 + const usage = {
254 + plan_type: "pro",
255 + rate_limit: {
256 + secondary_window: {
257 + used_percent: 78,
258 + reset_at: 4070908800,
259 + limit_window_seconds: 604800,
260 + },
261 + },
262 + };
263 + return HttpClientResponse.fromWeb(
264 + request,
265 + Response.json(
266 + refused
267 + ? { status_code: 429, body: "{}" }
268 + : { status_code: 200, body: JSON.stringify(usage) },
269 + ),
270 + );
271 + }),
272 + ),
273 + ),
274 + NodeCrypto.layer,
275 + ]),
276 + );
277 +});
278 +
279 const NOW = Date.parse("2026-11-13T12:00:00.000Z");
280 const at = (hours: number) => DateTime.formatIso(DateTime.makeUnsafe(NOW + hours * 3_600_000));
281 const spent = (id: string, hours: number): ServerProviderUsageWindow => ({
apps/server/src/usage/UsageLimitSources.ts
+37 -1
@@ -52,6 +52,41 @@ function sourceLabel(id: string, config: UsageLimitSourceConfig): string {
52 }
53 }
54
55 +/**
56 + * A refused read of one account keeps its last good reading, as provider probes keep their bars
57 + * (see ServerProviderUsageLimits); a reading with none to keep stays listed as unreadable.
58 + */
59 +function keepLastGoodReadings(
60 + previous: ReadonlyArray<UsageLimitSourceSnapshot>,
61 + next: ReadonlyArray<UsageLimitSourceSnapshot>,
62 +): ReadonlyArray<UsageLimitSourceSnapshot> {
63 + return next.map((source) => {
64 + // Auth file names can be reused, so only the same hub and login may lend a reading.
65 + const before = previous.find(
66 + (candidate) => candidate.id === source.id && candidate.endpoint === source.endpoint,
67 + );
68 + if (!before) return source;
69 + return {
70 + ...source,
71 + accounts: source.accounts.map((account) => {
72 + if (account.usageLimits.unavailable?.reason !== "probeFailed") return account;
73 + // Without a listed login, a reused auth file name cannot be told apart from the old one.
74 + if (account.email === undefined && account.workspaceId === undefined) return account;
75 + const last = before.accounts.find(
76 + (candidate) =>
77 + candidate.id === account.id &&
78 + candidate.driver === account.driver &&
79 + // Fingerprinted Antigravity readings omit the listed identity, so they are never lent.
80 + candidate.usageLimits.credentialFingerprint === undefined &&
81 + candidate.email === account.email &&
82 + candidate.workspaceId === account.workspaceId,
83 + );
84 + return last && !last.usageLimits.unavailable ? last : account;
85 + }),
86 + };
87 + });
88 +}
89 +
90 function missingManagementKey(config: UsageLimitSourceConfig): string {
91 return typeof config.managementKey === "string"
92 ? "No management key configured."
@@ -131,7 +166,7 @@ export const make = Effect.gen(function* () {
166 ([id, config]) => readSource(id as UsageLimitSourceId, config),
167 { concurrency: 4 },
168 );
134 - yield* publish(snapshots);
169 + yield* publish(keepLastGoodReadings(yield* Ref.get(stateRef), snapshots));
170 }).pipe(refreshLock.withPermits(1), Effect.ignoreCause({ log: true }));
171
172 // Shares the refresh lock so a stale in-flight read cannot overwrite a redemption.
@@ -156,6 +191,7 @@ export const make = Effect.gen(function* () {
191 input.accountId,
192 input.creditId,
193 );
194 + // A redemption changes the reading, so a refused re-read must not bring back the one before it.
195 const snapshot = yield* readSource(input.sourceId, config);
196 const previous = yield* Ref.get(stateRef);
197 yield* publish(previous.map((source) => (source.id === input.sourceId ? snapshot : source)));