@devries-cameron20 / t3code-1 / commits / 0b21a82aa3
Fork 0

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

fix(shared): pooled limits weight each account by its plan size (#35427)

Julius Marminge committed Nov 12, 2026 at 09:48 UTC 0b21a82aa3761c6073a8a5282f4a598b9accd4be
2 files changed +85 -5
packages/shared/src/usageLimits.test.ts
+46
@@ -804,6 +804,52 @@ describe("pools", () => {
804 ).toEqual(["weekly", "weekly:secondary"]);
805 });
806
807 + it("weights pooled Codex quota and resets by each account's plan capacity", () => {
808 + const [pool] = collectLimitPools(
809 + collectLimitAccounts(
810 + new Map([
811 + [
812 + EnvironmentId.make("env-a"),
813 + {
814 + ...laptop,
815 + serverConfig: {
816 + providers: [],
817 + usageLimitSources: [
818 + {
819 + ...source,
820 + accounts: [
821 + ["pro", "ChatGPT Pro 200 Subscription", 100],
822 + ["plus", "ChatGPT Plus Subscription", 0],
823 + ].map(([id, plan, usedPercent]) => ({
824 + id: String(id),
825 + email: `${id}@example.com`,
826 + driver: ProviderDriverKind.make("codex"),
827 + plan: String(plan),
828 + usageLimits: {
829 + checkedAt,
830 + windows: [{ ...weekly, id: "secondary", usedPercent: Number(usedPercent) }],
831 + },
832 + })),
833 + },
834 + ],
835 + },
836 + },
837 + ],
838 + ]),
839 + ),
840 + now,
841 + );
842 + const [pooledWeekly] = pool?.windows ?? [];
843 + // Pro 20x holds 20 of the pool's 21 capacity parts.
844 + expect(pooledWeekly?.remainingPercent).toBe(5);
845 + expect(
846 + pooledWeekly?.resets.map(({ member, restoresPercent }) => [
847 + member.account.email,
848 + restoresPercent,
849 + ]),
850 + ).toContainEqual(["pro@example.com", 95]);
851 + });
852 +
853 it("pools windows by id across accounts and orders resets by when they land", () => {
854 const input = new Map([
855 [
packages/shared/src/usageLimits.ts
+39 -5
@@ -539,6 +539,25 @@ function accountSortName(account: LimitAccount): string {
539 return (account.displayName ?? account.email ?? account.key).toLowerCase();
540 }
541
542 +const CODEX_PLAN_CAPACITY: Record<string, number> = {
543 + "ChatGPT Plus Subscription": 1,
544 + "ChatGPT Pro 100 Subscription": 5,
545 + "ChatGPT Pro 200 Subscription": 20,
546 +};
547 +// Claude's published plan multipliers describe the session allowance only.
548 +const CLAUDE_SESSION_CAPACITY: Record<string, number> = { Pro: 1, Max: 5, Ultra: 20 };
549 +
550 +function planCapacity(account: LimitAccount, kind: ServerProviderUsageWindow["kind"]) {
551 + if (!account.plan) return undefined;
552 + if (account.driver === "codex" && (kind === "session" || kind === "weekly")) {
553 + return CODEX_PLAN_CAPACITY[account.plan];
554 + }
555 + if (account.driver === "claudeAgent" && kind === "session") {
556 + return CLAUDE_SESSION_CAPACITY[account.plan];
557 + }
558 + return undefined;
559 +}
560 +
561 // Codex ids name slots, so pool by kind; other drivers' ids identify distinct allowances.
562 function poolKey(driver: ServerProvider["driver"], window: ServerProviderUsageWindow): string {
563 return driver === "codex" ? window.kind : `${window.kind}:${window.id}`;
@@ -607,15 +626,28 @@ function poolWindows(accounts: readonly LimitAccount[], now: number): readonly L
626 const memberByAccount = new Map(members.map((member) => [member.account.key, member]));
627 const first = members[0]!.window;
628 const id = members[0]!.account.driver === "codex" ? key : first.id;
610 - const usedPercent = members.reduce((sum, m) => sum + m.window.usedPercent, 0) / members.length;
629 + // One unknown plan makes the pool incomparable by plan, so every member then counts once.
630 + const capacities = members.map((m) => planCapacity(m.account, first.kind));
631 + const weights = capacities.every((capacity) => capacity !== undefined)
632 + ? capacities
633 + : members.map(() => 1);
634 + const weightOf = new Map(members.map((m, index) => [m, weights[index]!]));
635 + const totalWeight = weights.reduce((sum, weight) => sum + weight, 0);
636 + const usedPercent =
637 + members.reduce((sum, m) => sum + m.window.usedPercent * weightOf.get(m)!, 0) / totalWeight;
638 // Judge pace only for windows with reset clocks; others add spend without elapsed time.
639 const timed = members.flatMap((m) => {
640 const share = elapsedShare(m.window, now);
614 - return share === null ? [] : [{ used: m.window.usedPercent, elapsed: share }];
641 + return share === null
642 + ? []
643 + : [{ used: m.window.usedPercent, elapsed: share, weight: weightOf.get(m)! }];
644 });
616 - const timedUsed = timed.reduce((sum, t) => sum + t.used, 0) / timed.length;
645 + const timedWeight = timed.reduce((sum, t) => sum + t.weight, 0);
646 + const timedUsed = timed.reduce((sum, t) => sum + t.used * t.weight, 0) / timedWeight;
647 const meanElapsed =
618 - timed.length > 0 ? timed.reduce((sum, t) => sum + t.elapsed, 0) / timed.length : null;
648 + timed.length > 0
649 + ? timed.reduce((sum, t) => sum + t.elapsed * t.weight, 0) / timedWeight
650 + : null;
651 const resets = members
652 .flatMap((member) => {
653 const at = resetMillis(member.window);
@@ -625,7 +657,9 @@ function poolWindows(accounts: readonly LimitAccount[], now: number): readonly L
657 {
658 member,
659 at,
628 - restoresPercent: Math.round(member.refillPercent / members.length),
660 + restoresPercent: Math.round(
661 + (member.refillPercent * weightOf.get(member)!) / totalWeight,
662 + ),
663 },
664 ];
665 })