The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | import { expect, it } from "@effect/vitest"; |
| 3 | import { |
| 4 | ProviderDriverKind, |
| 5 | SecretProviderId, |
| 6 | ServerConfigStreamEvent, |
| 7 | UsageLimitSourceId, |
| 8 | type ServerProviderUsageWindow, |
| 9 | type UsageLimitSourceSnapshot, |
| 10 | } from "@t3tools/contracts"; |
| 11 | import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver"; |
| 12 | import * as DateTime from "effect/DateTime"; |
| 13 | import * as Effect from "effect/Effect"; |
| 14 | import * as Fiber from "effect/Fiber"; |
| 15 | import * as Layer from "effect/Layer"; |
| 16 | import * as Schema from "effect/Schema"; |
| 17 | import * as Stream from "effect/Stream"; |
| 18 | import * as TestClock from "effect/testing/TestClock"; |
| 19 | import { HttpClient, HttpClientResponse } from "effect/http"; |
| 20 | |
| 21 | import * as BackgroundPolicy from "../background/BackgroundPolicy.ts"; |
| 22 | import * as ServerSettings from "../serverSettings.ts"; |
| 23 | import * as UsageLimitSources from "./UsageLimitSources.ts"; |
| 24 | |
| 25 | const encodeConfigEvent = Schema.encodeSync(ServerConfigStreamEvent); |
| 26 | |
| 27 | it.effect("keeps malformed source URLs encodable for config subscribers", () => |
| 28 | Effect.gen(function* () { |
| 29 | const sources = yield* UsageLimitSources.make; |
| 30 | yield* sources.refresh; |
| 31 | const snapshots = yield* sources.current; |
| 32 | expect(snapshots.map((source) => source.label)).toEqual([ |
| 33 | "local-hub", |
| 34 | "opaque-hub", |
| 35 | "Named hub", |
| 36 | "hub.test:8317", |
| 37 | "Usage limit source", |
| 38 | ]); |
| 39 | // Only an http(s) hub URL names an endpoint an agent can be sent to. |
| 40 | expect(snapshots.map((source) => source.origin)).toEqual([ |
| 41 | undefined, |
| 42 | undefined, |
| 43 | undefined, |
| 44 | "https://hub.test:8317", |
| 45 | undefined, |
| 46 | ]); |
| 47 | expect(snapshots[3]?.endpoint).toBe("https://hub.test:8317/team-a"); |
| 48 | expect(() => |
| 49 | encodeConfigEvent({ |
| 50 | version: 1, |
| 51 | type: "usageLimitSourcesUpdated", |
| 52 | payload: { sources: snapshots }, |
| 53 | }), |
| 54 | ).not.toThrow(); |
| 55 | expect(snapshots.every((source) => source.error === "No management key configured.")).toBe( |
| 56 | true, |
| 57 | ); |
| 58 | }).pipe( |
| 59 | Effect.provide([ |
| 60 | Layer.succeed(SecretResolver.SecretResolver, { |
| 61 | label: (provider) => Effect.succeed(provider), |
| 62 | read: (references) => Effect.succeed(references.map(() => undefined)), |
| 63 | invalidate: () => Effect.void, |
| 64 | }), |
| 65 | ServerSettings.layerTest({ |
| 66 | usageLimitSources: { |
| 67 | [UsageLimitSourceId.make("local-hub")]: { |
| 68 | kind: "cliproxy", |
| 69 | url: "localhost:8317", |
| 70 | managementKey: "", |
| 71 | enabled: true, |
| 72 | }, |
| 73 | [UsageLimitSourceId.make("opaque-hub")]: { |
| 74 | kind: "cliproxy", |
| 75 | url: "file:///tmp/hub", |
| 76 | managementKey: "", |
| 77 | enabled: true, |
| 78 | }, |
| 79 | [UsageLimitSourceId.make("named-hub")]: { |
| 80 | kind: "cliproxy", |
| 81 | url: "localhost:8317", |
| 82 | label: "Named hub", |
| 83 | managementKey: "", |
| 84 | enabled: true, |
| 85 | }, |
| 86 | [UsageLimitSourceId.make("valid-hub")]: { |
| 87 | kind: "cliproxy", |
| 88 | url: "https://hub.test:8317/team-a/management.html", |
| 89 | managementKey: "", |
| 90 | enabled: true, |
| 91 | }, |
| 92 | [UsageLimitSourceId.make(" ")]: { |
| 93 | kind: "cliproxy", |
| 94 | url: "localhost:8317", |
| 95 | managementKey: "", |
| 96 | enabled: true, |
| 97 | }, |
| 98 | }, |
| 99 | }), |
| 100 | Layer.mock(BackgroundPolicy.BackgroundPolicy)({ |
| 101 | shouldRunScopeWork: () => Effect.succeed(false), |
| 102 | }), |
| 103 | Layer.succeed( |
| 104 | HttpClient.HttpClient, |
| 105 | HttpClient.make(() => Effect.die("Unexpected network request")), |
| 106 | ), |
| 107 | NodeCrypto.layer, |
| 108 | ]), |
| 109 | ), |
| 110 | ); |
| 111 | |
| 112 | it.effect("reads a management key from its secret provider", () => { |
| 113 | const authorizations: Array<string | undefined> = []; |
| 114 | const keySource = (reference: string) => ({ |
| 115 | provider: SecretProviderId.make("homelab"), |
| 116 | reference, |
| 117 | }); |
| 118 | return Effect.gen(function* () { |
| 119 | const sources = yield* UsageLimitSources.make; |
| 120 | const snapshots = yield* sources.streamChanges.pipe( |
| 121 | Stream.filter((next) => next.length > 0), |
| 122 | Stream.runHead, |
| 123 | Effect.flatMap(Effect.fromOption), |
| 124 | ); |
| 125 | expect(snapshots.map(({ id, error }) => ({ id, error }))).toEqual([ |
| 126 | { id: "resolved-hub", error: undefined }, |
| 127 | { |
| 128 | id: "unresolved-hub", |
| 129 | error: "Could not read the management key from its secret provider.", |
| 130 | }, |
| 131 | ]); |
| 132 | expect(authorizations).toEqual(["Bearer resolved-key"]); |
| 133 | }).pipe( |
| 134 | Effect.provide([ |
| 135 | ServerSettings.layerTest({ |
| 136 | usageLimitSources: { |
| 137 | [UsageLimitSourceId.make("resolved-hub")]: { |
| 138 | kind: "cliproxy", |
| 139 | url: "https://resolved.test", |
| 140 | managementKey: keySource("cliproxy#key"), |
| 141 | enabled: true, |
| 142 | }, |
| 143 | [UsageLimitSourceId.make("unresolved-hub")]: { |
| 144 | kind: "cliproxy", |
| 145 | url: "https://unresolved.test", |
| 146 | managementKey: keySource("missing#key"), |
| 147 | enabled: true, |
| 148 | }, |
| 149 | }, |
| 150 | }), |
| 151 | Layer.mock(BackgroundPolicy.BackgroundPolicy)({ |
| 152 | shouldRunScopeWork: () => Effect.succeed(false), |
| 153 | }), |
| 154 | Layer.succeed(SecretResolver.SecretResolver, { |
| 155 | label: (provider) => Effect.succeed(provider), |
| 156 | read: (references) => |
| 157 | Effect.succeed( |
| 158 | references.map((reference) => |
| 159 | reference.reference === "cliproxy#key" ? "resolved-key" : undefined, |
| 160 | ), |
| 161 | ), |
| 162 | invalidate: () => Effect.void, |
| 163 | }), |
| 164 | Layer.succeed( |
| 165 | HttpClient.HttpClient, |
| 166 | HttpClient.make((request) => |
| 167 | Effect.sync(() => { |
| 168 | authorizations.push(request.headers.authorization); |
| 169 | return HttpClientResponse.fromWeb(request, Response.json({ files: [] })); |
| 170 | }), |
| 171 | ), |
| 172 | ), |
| 173 | NodeCrypto.layer, |
| 174 | ]), |
| 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 => ({ |
| 282 | id, |
| 283 | kind: "weekly", |
| 284 | label: id, |
| 285 | usedPercent: 100, |
| 286 | resetsAt: at(hours), |
| 287 | }); |
| 288 | const hub = ( |
| 289 | accounts: ReadonlyArray<{ |
| 290 | driver?: string; |
| 291 | models?: ReadonlyArray<string>; |
| 292 | windows?: ReadonlyArray<ServerProviderUsageWindow>; |
| 293 | unavailable?: true; |
| 294 | }>, |
| 295 | ): UsageLimitSourceSnapshot => ({ |
| 296 | id: UsageLimitSourceId.make("hub"), |
| 297 | kind: "cliproxy", |
| 298 | label: "hub", |
| 299 | checkedAt: at(0), |
| 300 | accounts: accounts.map((account, index) => ({ |
| 301 | id: `account-${index}`, |
| 302 | driver: ProviderDriverKind.make(account.driver ?? "claudeAgent"), |
| 303 | ...(account.models ? { models: account.models } : {}), |
| 304 | usageLimits: { |
| 305 | checkedAt: at(0), |
| 306 | windows: account.windows ?? [], |
| 307 | ...(account.unavailable ? { unavailable: { reason: "probeFailed" as const } } : {}), |
| 308 | }, |
| 309 | })), |
| 310 | }); |
| 311 | |
| 312 | it.each([ |
| 313 | { |
| 314 | name: "earliest refill when every account is spent, each at its latest spent window", |
| 315 | accounts: [ |
| 316 | { windows: [spent("five_hour", 1), spent("seven_day", 3)] }, |
| 317 | { windows: [spent("five_hour", 2)] }, |
| 318 | { driver: "codex", windows: [] }, |
| 319 | ], |
| 320 | model: "claude-haiku-5-5", |
| 321 | expected: at(2), |
| 322 | }, |
| 323 | { |
| 324 | name: "unknown while an account still has room", |
| 325 | accounts: [{ windows: [spent("five_hour", 1)] }, { windows: [] }], |
| 326 | model: "claude-haiku-5-5", |
| 327 | expected: null, |
| 328 | }, |
| 329 | { |
| 330 | name: "unknown while an account could not be read", |
| 331 | accounts: [{ windows: [spent("five_hour", 1)] }, { unavailable: true as const }], |
| 332 | model: "claude-haiku-5-5", |
| 333 | expected: null, |
| 334 | }, |
| 335 | { |
| 336 | name: "unknown when a spent window has no reset or already reset", |
| 337 | accounts: [{ windows: [{ ...spent("five_hour", 1), resetsAt: undefined }] }], |
| 338 | model: "claude-haiku-5-5", |
| 339 | expected: null, |
| 340 | }, |
| 341 | { |
| 342 | name: "a model-scoped weekly window blocks only its own model", |
| 343 | accounts: [{ windows: [spent("seven_day_fable", 5)] }], |
| 344 | model: "claude-haiku-5-5", |
| 345 | expected: null, |
| 346 | }, |
| 347 | { |
| 348 | name: "a model-scoped weekly window blocks its own model", |
| 349 | accounts: [{ windows: [spent("seven_day_fable", 5)] }], |
| 350 | model: "claude-fable-5-2", |
| 351 | expected: at(5), |
| 352 | }, |
| 353 | { |
| 354 | name: "no account of the driver", |
| 355 | accounts: [{ driver: "codex", windows: [spent("five_hour", 1)] }], |
| 356 | model: "claude-haiku-5-5", |
| 357 | expected: null, |
| 358 | }, |
| 359 | // regression: #30778 only the accounts the hub routes the model to can refill it |
| 360 | { |
| 361 | name: "an account of another driver the hub routes the model to counts", |
| 362 | accounts: [ |
| 363 | { driver: "codex", models: ["gpt-5.6-sol"], windows: [spent("five_hour", 3)] }, |
| 364 | { models: ["claude-sonnet-4-6"], windows: [] }, |
| 365 | ], |
| 366 | model: "gpt-5.6-sol", |
| 367 | expected: at(3), |
| 368 | }, |
| 369 | { |
| 370 | name: "an account that lists no models is left out of an alias", |
| 371 | accounts: [{ windows: [spent("five_hour", 1)] }, { models: [], windows: [] }], |
| 372 | model: "opus", |
| 373 | expected: at(1), |
| 374 | }, |
| 375 | { |
| 376 | name: "a spent Gemini quota does not hold back Claude on Antigravity", |
| 377 | accounts: [ |
| 378 | { |
| 379 | driver: "antigravity", |
| 380 | models: ["claude-opus-4-6-thinking"], |
| 381 | windows: [spent("3p-5h", 1), spent("gemini-5h", 24)], |
| 382 | }, |
| 383 | ], |
| 384 | model: "claude-opus-4-6-thinking", |
| 385 | expected: at(1), |
| 386 | }, |
| 387 | { |
| 388 | name: "a hub thinking suffix routes by its base model", |
| 389 | accounts: [ |
| 390 | { driver: "codex", models: ["gpt-5.2"], windows: [spent("five_hour", 3)] }, |
| 391 | { models: ["claude-sonnet-4-6"], windows: [] }, |
| 392 | ], |
| 393 | model: "gpt-5.2(high)", |
| 394 | expected: at(3), |
| 395 | }, |
| 396 | { |
| 397 | name: "a prefixed Gemini model is held back by the Gemini quota", |
| 398 | accounts: [ |
| 399 | { |
| 400 | driver: "antigravity", |
| 401 | models: ["ag/gemini-3.1-pro-high"], |
| 402 | windows: [spent("3p-5h", 1), spent("gemini-5h", 24)], |
| 403 | }, |
| 404 | ], |
| 405 | model: "ag/gemini-3.1-pro-high", |
| 406 | expected: at(24), |
| 407 | }, |
| 408 | { |
| 409 | name: "an alias of unknown family is held back by every Antigravity quota", |
| 410 | accounts: [{ driver: "antigravity", models: ["fast"], windows: [spent("gemini-5h", 24)] }], |
| 411 | model: "fast", |
| 412 | expected: at(24), |
| 413 | }, |
| 414 | { |
| 415 | name: "an own-driver account whose model list was unread still counts", |
| 416 | accounts: [ |
| 417 | { windows: [spent("five_hour", 1)] }, |
| 418 | { |
| 419 | driver: "antigravity", |
| 420 | models: ["claude-sonnet-4-6"], |
| 421 | windows: [spent("3p-5h", 8)], |
| 422 | }, |
| 423 | ], |
| 424 | model: "claude-sonnet-4-6", |
| 425 | expected: at(1), |
| 426 | }, |
| 427 | { |
| 428 | name: "an Argon model waits only for the Argon quota", |
| 429 | accounts: [ |
| 430 | { |
| 431 | driver: "antigravity", |
| 432 | models: ["gemini-4-argon-high"], |
| 433 | windows: [spent("gemini-argon-5h", 2), spent("gemini-carbon-weekly", 96)], |
| 434 | }, |
| 435 | ], |
| 436 | model: "gemini-4-argon-high", |
| 437 | expected: at(2), |
| 438 | }, |
| 439 | ])("hub pool reset: $name", ({ accounts, model, expected }) => { |
| 440 | expect( |
| 441 | UsageLimitSources.hubPoolResetAt( |
| 442 | [hub(accounts)], |
| 443 | ProviderDriverKind.make("claudeAgent"), |
| 444 | model, |
| 445 | NOW, |
| 446 | ), |
| 447 | ).toBe(expected); |
| 448 | }); |
| 449 | |
| 450 | it.effect("the reset fallback refreshes its hub, and a stalled hub cannot hold the failure", () => |
| 451 | Effect.gen(function* () { |
| 452 | yield* TestClock.setTime(NOW); |
| 453 | const roomLeft = { ...hub([{ windows: [spent("five_hour", 2)] }]), origin: "http://hub.test" }; |
| 454 | const cached = { |
| 455 | ...roomLeft, |
| 456 | accounts: roomLeft.accounts.map((account) => ({ |
| 457 | ...account, |
| 458 | usageLimits: { |
| 459 | ...account.usageLimits, |
| 460 | windows: account.usageLimits.windows.map((window) => ({ ...window, usedPercent: 99 })), |
| 461 | }, |
| 462 | })), |
| 463 | }; |
| 464 | let current: ReadonlyArray<UsageLimitSourceSnapshot> = [cached]; |
| 465 | let refresh: Effect.Effect<void> = Effect.sync(() => { |
| 466 | current = [roomLeft]; |
| 467 | }); |
| 468 | const lookup = UsageLimitSources.UsageLimitResetFallback.pipe( |
| 469 | Effect.flatMap((fallback) => |
| 470 | fallback.resetAt(ProviderDriverKind.make("claudeAgent"), "http://hub.test", null), |
| 471 | ), |
| 472 | Effect.provide( |
| 473 | UsageLimitSources.layerResetFallback.pipe( |
| 474 | Layer.provide( |
| 475 | Layer.mock(UsageLimitSources.UsageLimitSources)({ |
| 476 | current: Effect.sync(() => current), |
| 477 | refresh: Effect.suspend(() => refresh), |
| 478 | }), |
| 479 | ), |
| 480 | ), |
| 481 | ), |
| 482 | ); |
| 483 | // The last poll saw room; the account was spent after it. |
| 484 | expect(yield* lookup).toBe(at(2)); |
| 485 | current = [cached]; |
| 486 | refresh = Effect.never; |
| 487 | const stalled = yield* Effect.forkChild(lookup); |
| 488 | yield* TestClock.adjust("5 seconds"); |
| 489 | expect(yield* Fiber.join(stalled)).toBeNull(); |
| 490 | }), |
| 491 | ); |