@devries-cameron20 / t3code Mirror · read-only / commits / 4192234c34
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 16:00 UTC

fix(usage): distinct email-less hub accounts no longer collapse (#36561)

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

Wout Stiens committed Nov 14, 2026 at 10:16 UTC 4192234c34e9f690df3b99715cfecba48b49e4de
5 files changed +172 -24
apps/server/src/usage/UsageLimitSources.test.ts
+2 -1
@@ -44,6 +44,7 @@ it.effect("keeps malformed source URLs encodable for config subscribers", () =>
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,
@@ -84,7 +85,7 @@ it.effect("keeps malformed source URLs encodable for config subscribers", () =>
85 },
86 [UsageLimitSourceId.make("valid-hub")]: {
87 kind: "cliproxy",
87 - url: "https://hub.test:8317",
88 + url: "https://hub.test:8317/team-a/management.html",
89 managementKey: "",
90 enabled: true,
91 },
apps/server/src/usage/UsageLimitSources.ts
+6 -1
@@ -86,7 +86,12 @@ export const make = Effect.gen(function* () {
86 id,
87 kind: config.kind,
88 label: sourceLabel(id, config),
89 - ...(url && /^https?:$/.test(url.protocol) ? { origin: url.origin } : {}),
89 + ...(url && /^https?:$/.test(url.protocol)
90 + ? {
91 + origin: url.origin,
92 + endpoint: `${url.origin}${url.pathname.replace(/(\/management\.html)?\/*$/i, "")}`,
93 + }
94 + : {}),
95 checkedAt,
96 } as const;
97 const managementKey = yield* readManagementKey(config);
packages/contracts/src/providerUsageLimits.ts
+2
@@ -88,6 +88,8 @@ export const UsageLimitSourceSnapshot = Schema.Struct({
88 label: TrimmedNonEmptyString,
89 /** Origin of the hub, which also serves the agents sent through it. */
90 origin: Schema.optional(TrimmedNonEmptyString),
91 + /** Origin and base path of the hub, which tell apart hubs served from one origin. */
92 + endpoint: Schema.optional(TrimmedNonEmptyString),
93 checkedAt: IsoDateTime,
94 accounts: ForwardCompatibleArray(UsageLimitSourceAccount),
95 error: Schema.optional(TrimmedNonEmptyString),
packages/shared/src/usageLimits.test.ts
+140 -20
@@ -663,7 +663,7 @@ describe("pools", () => {
663 expect(accounts.map((account) => account.key)).toEqual([
664 "env-a:business",
665 "env-a:personal",
666 - "hub:x.json",
666 + "env-a:hub:x.json",
667 ]);
668 expect(accounts[0]?.limits.windows[0]?.usedPercent).toBe(8);
669 const withoutEmail = (id: string, workspaceId: string) => {
@@ -693,8 +693,8 @@ describe("pools", () => {
693 ],
694 ]);
695 expect(collectLimitAccounts(sameFile).map((account) => account.key)).toEqual([
696 - "hub:a.json:ws-1",
697 - "hub:a.json:ws-2",
696 + "env-a:hub:a.json:ws-1",
697 + "env-b:hub:a.json:ws-2",
698 ]);
699 expect(accounts[1]?.environments.map((environment) => environment.label)).toEqual([
700 "Laptop",
@@ -739,33 +739,147 @@ describe("pools", () => {
739 });
740 expect(collectLimitAccounts(withOther(unreadable)).map((account) => account.key)).toEqual([
741 "env-a:codex",
742 - "hub:claude.json",
742 + "env-a:hub:claude.json",
743 ]);
744 });
745
746 - it("keys a hub account without an email by hub, so two environments on one hub share it", () => {
747 - const seat = {
748 - id: "claude-team-seat.json",
749 - driver: claude,
750 - usageLimits: { checkedAt, windows: [window] },
751 - };
752 - const hub = { ...source, accounts: [seat] };
746 + it.each([
747 + [
748 + "one remote hub under environment-local ids",
749 + "http://hub:8317",
750 + "laptop-hub",
751 + "http://hub:8317",
752 + 1,
753 + ],
754 + ["two remote hubs under one id", "http://hub:8317", "hub", "http://hub-8317", 2],
755 + [
756 + "two remote hubs under paths of one origin",
757 + "https://proxy.example/team-a",
758 + "hub-b",
759 + "https://proxy.example/team-b",
760 + 2,
761 + ],
762 + [
763 + "a shared IPv6 tailnet hub",
764 + "http://[fd7a:115c:a1e0::1]:8317",
765 + "hub",
766 + "http://[fd7a:115c:a1e0::1]:8317",
767 + 1,
768 + ],
769 + [
770 + "each machine's own localhost hub",
771 + "http://localhost:8317",
772 + "hub",
773 + "http://localhost:8317",
774 + 2,
775 + ],
776 + ["each machine's own IPv6 loopback hub", "http://[::1]:8317", "hub", "http://[::1]:8317", 2],
777 + [
778 + "each machine's own localhost subdomain hub",
779 + "http://hub.localhost:8317",
780 + "hub",
781 + "http://hub.localhost:8317",
782 + 2,
783 + ],
784 + [
785 + "each machine's own trailing-dot localhost subdomain hub",
786 + "http://hub.localhost.:8317",
787 + "hub",
788 + "http://hub.localhost.:8317",
789 + 2,
790 + ],
791 + [
792 + "each machine's own mapped IPv4 loopback hub",
793 + "http://[::ffff:7f00:1]:8317",
794 + "hub",
795 + "http://[::ffff:7f00:1]:8317",
796 + 2,
797 + ],
798 + ["each machine's own wildcard hub", "http://0.0.0.0:8317", "hub", "http://0.0.0.0:8317", 2],
799 + ["hubs that report no endpoint", undefined, "hub", undefined, 2],
800 + ] as const)(
801 + "keeps email-less hub identity across environments: %s",
802 + (_, firstEndpoint, secondId, secondEndpoint, count) => {
803 + const seat = {
804 + id: "claude-team-seat.json",
805 + driver: claude,
806 + usageLimits: { checkedAt, windows: [window] },
807 + };
808 + const hubAt = (id: string, endpoint: string | undefined) => ({
809 + ...source,
810 + id: UsageLimitSourceId.make(id),
811 + ...(endpoint ? { origin: new URL(endpoint).origin, endpoint } : {}),
812 + accounts: [seat],
813 + });
814 + const input = new Map([
815 + [
816 + EnvironmentId.make("env-a"),
817 + {
818 + ...laptop,
819 + serverConfig: { providers: [], usageLimitSources: [hubAt("hub", firstEndpoint)] },
820 + },
821 + ],
822 + [
823 + EnvironmentId.make("env-b"),
824 + {
825 + entry: { target: { label: "Desktop" } },
826 + serverConfig: { providers: [], usageLimitSources: [hubAt(secondId, secondEndpoint)] },
827 + },
828 + ],
829 + ]);
830 + const accounts = collectLimitAccounts(input);
831 + expect(accounts).toHaveLength(count);
832 + expect(accounts[0]?.displayName).toBe("claude-team-seat");
833 + },
834 + );
835 +
836 + it("keeps public keys apart for different logins in one hub file", () => {
837 + const hubAt = (id: string, email: string, usedPercent: number) => ({
838 + ...source,
839 + id: UsageLimitSourceId.make(id),
840 + origin: "https://hub.test",
841 + endpoint: "https://hub.test",
842 + accounts: [
843 + {
844 + id: "seat.json",
845 + driver: claude,
846 + email,
847 + usageLimits: { checkedAt, windows: [{ ...window, usedPercent }] },
848 + },
849 + ],
850 + });
851 const input = new Map([
852 [
853 EnvironmentId.make("env-a"),
756 - { ...laptop, serverConfig: { providers: [], usageLimitSources: [hub] } },
854 + {
855 + ...laptop,
856 + serverConfig: {
857 + providers: [],
858 + usageLimitSources: [hubAt("first", "old@example.com", 10)],
859 + },
860 + },
861 ],
862 [
863 EnvironmentId.make("env-b"),
864 {
865 entry: { target: { label: "Desktop" } },
762 - serverConfig: { providers: [], usageLimitSources: [hub] },
866 + serverConfig: {
867 + providers: [],
868 + usageLimitSources: [hubAt("second", "new@example.com", 80)],
869 + },
870 },
871 ],
872 ]);
873 const accounts = collectLimitAccounts(input);
767 - expect(accounts.map((account) => account.key)).toEqual(["hub:claude-team-seat.json"]);
768 - expect(accounts[0]?.displayName).toBe("claude-team-seat");
874 + expect(accounts.map((account) => account.key)).toEqual([
875 + "env-a:first:seat.json",
876 + "env-b:second:seat.json",
877 + ]);
878 + const columns = collectLimitPools(accounts, Date.parse(checkedAt))[0]?.windows[0]?.columns;
879 + expect(columns?.map((column) => [column.account.email, column.window?.usedPercent])).toEqual([
880 + ["new@example.com", 80],
881 + ["old@example.com", 10],
882 + ]);
883 });
884
885 it("labels a default-named instance merged with a hub account by e-mail, keeping a chosen name", () => {
@@ -964,7 +1078,7 @@ describe("pools", () => {
1078 const [session, week] = pools[0]!.windows;
1079 const untimed = collectLimitPools(
1080 collectLimitAccounts(input).map((account) =>
967 - account.key === "hub:b"
1081 + account.key === "env-a:hub:b"
1082 ? {
1083 ...account,
1084 limits: {
@@ -986,8 +1100,8 @@ describe("pools", () => {
1100 expect(
1101 session?.resets.map((reset) => [reset.member.account.key, reset.restoresPercent]),
1102 ).toEqual([
989 - ["hub:a", 40],
990 - ["hub:b", 20],
1103 + ["env-a:hub:a", 40],
1104 + ["env-a:hub:b", 20],
1105 ]);
1106 expect(week).toMatchObject({ id: "seven_day", remainingPercent: 80, members: [{}] });
1107 // Codex reports `primary` for both its five-hour and (on Go) monthly window.
@@ -1026,8 +1140,14 @@ describe("pools", () => {
1140 ["weekly", 1],
1141 ["monthly", 1],
1142 ]);
1029 - expect(session?.members.map((member) => member.account.key)).toEqual(["hub:a", "hub:b"]);
1030 - expect(pools[0]?.accounts.map((account) => account.key)).toEqual(["hub:a", "hub:b"]);
1143 + expect(session?.members.map((member) => member.account.key)).toEqual([
1144 + "env-a:hub:a",
1145 + "env-a:hub:b",
1146 + ]);
1147 + expect(pools[0]?.accounts.map((account) => account.key)).toEqual([
1148 + "env-a:hub:a",
1149 + "env-a:hub:b",
1150 + ]);
1151 });
1152 });
1153
packages/shared/src/usageLimits.ts
+22 -2
@@ -14,11 +14,14 @@ import {
14 type ServerProviderResetCredits,
15 type ServerProviderUsageLimits,
16 type ServerProviderUsageWindow,
17 + type UsageLimitSourceSnapshot,
18 type UsageLimitSourceSnapshots,
19 } from "@t3tools/contracts";
20
21 import * as DateTime from "effect/DateTime";
22
23 +import { isLocalLoopbackHost, isWildcardHost } from "./hostClassification.ts";
24 +
25 const MINUTE = 60_000;
26 const HOUR = 60 * MINUTE;
27 const DAY = 24 * HOUR;
@@ -252,6 +255,20 @@ function accountKeyResolver(reports: Iterable<AccountReport>) {
255
256 const hubAccountName = (account: { readonly id: string }) => account.id.replace(/\.json$/i, "");
257
258 +// Source ids are environment-local, so only a remote endpoint names the same hub across environments.
259 +function hubIdentity(environmentId: EnvironmentId, source: UsageLimitSourceSnapshot): string {
260 + // The server supplies canonical endpoints; native URL polyfills can misparse IPv6 hosts.
261 + const endpoint = source.endpoint;
262 + const host = endpoint?.match(/^https?:\/\/(\[[\da-f:.]+\]|[^:/?#@]+)(?::\d+)?(?:\/|$)/)?.[1];
263 + const local =
264 + !host ||
265 + isLocalLoopbackHost(host) ||
266 + isWildcardHost(host) ||
267 + // Each machine resolves these to itself too; shared host classification leaves them out.
268 + /\.localhost\.*$|^\[::ffff:(?:127\.|7f[\da-f]{2}:)/i.test(host);
269 + return endpoint && !local ? endpoint : `${environmentId}:${source.id}`;
270 +}
271 +
272 export interface LimitAccount {
273 readonly key: string;
274 readonly driver: ServerProvider["driver"];
@@ -378,8 +395,11 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl
395 : source.label;
396 for (const account of source.accounts) {
397 if (limitsNotice(account.usageLimits) !== null) continue;
381 - const key = `${source.id}:${account.id}${account.workspaceId ? `:${account.workspaceId}` : ""}`;
382 - merge(resolveKey(account) ?? key, {
398 + const resolved = resolveKey(account);
399 + // A resolved account already has an identity; its public key must stay distinct from others'.
400 + const hub = resolved ? `${environmentId}:${source.id}` : hubIdentity(environmentId, source);
401 + const key = `${hub}:${account.id}${account.workspaceId ? `:${account.workspaceId}` : ""}`;
402 + merge(resolved ?? key, {
403 key,
404 driver: account.driver,
405 displayName: account.email ? null : hubAccountName(account),