Skip to content

Commit 70d22ee

Browse files
Bombatomica64claude
andcommitted
fix(usage): tell Claude orgs apart by their id, not their name
Two orgs can share a display name and a name can change, so keying the account on it could merge two quotas or split one. The Claude driver now reads the org UUID the CLI keeps in `.claude.json` (the same one reset redemption uses) onto a new `ServerProviderAuth.accountId`, and the Limits key prefers it, falling back to the display name when no id is known. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 65439a3 commit 70d22ee

6 files changed

Lines changed: 74 additions & 4 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv> = {
215215
),
216216
),
217217
),
218+
Effect.flatMap((provider) =>
219+
provider.auth.status === "authenticated"
220+
? ClaudeResetCredits.readClaudeOrganizationId(accountConfigPath).pipe(
221+
Effect.map((accountId) =>
222+
accountId ? { ...provider, auth: { ...provider.auth, accountId } } : provider,
223+
),
224+
)
225+
: Effect.succeed(provider),
226+
),
218227
Effect.map(stampIdentity),
219228
),
220229
),

‎apps/server/src/provider/Layers/claudeResetCredits.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,19 @@ effectIt.layer(NodeServices.layer)("consumeClaudeResetCredit", (it) => {
257257
}),
258258
);
259259
});
260+
261+
effectIt.layer(NodeServices.layer)("readClaudeOrganizationId", (it) => {
262+
it.effect("reads the login's org id, and nothing from a missing or garbled account", () =>
263+
Effect.gen(function* () {
264+
const fs = yield* FileSystem.FileSystem;
265+
const { configDir, accountConfigPath } = yield* writeLogin;
266+
const signedIn = yield* ClaudeResetCredits.readClaudeOrganizationId(accountConfigPath);
267+
const missing = yield* ClaudeResetCredits.readClaudeOrganizationId(
268+
`${configDir}/absent.json`,
269+
);
270+
yield* fs.writeFileString(accountConfigPath, "{not json");
271+
const garbled = yield* ClaudeResetCredits.readClaudeOrganizationId(accountConfigPath);
272+
expect([signedIn, missing, garbled]).toEqual(["org-1", undefined, undefined]);
273+
}),
274+
);
275+
});

‎apps/server/src/provider/Layers/claudeResetCredits.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ export const claudeAccountConfigPath = (configDir: string | undefined) =>
195195
configDir ? path.join(configDir, ".claude.json") : path.join(NodeOS.homedir(), ".claude.json"),
196196
);
197197

198+
/**
199+
* The org the login in `accountConfigPath` draws its quota from, or undefined
200+
* when the account record is missing or unreadable.
201+
*/
202+
export const readClaudeOrganizationId = (accountConfigPath: string) =>
203+
readJson(Config, accountConfigPath).pipe(
204+
Effect.map((config) => config.oauthAccount?.organizationUuid?.trim() || undefined),
205+
Effect.orElseSucceed(() => undefined),
206+
);
207+
198208
const CLAIM_OUTCOMES = {
199209
reset: "reset",
200210
not_limited: "nothingToReset",

‎packages/contracts/src/server.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,16 @@ export const ServerProviderAuth = Schema.Struct({
6464
label: Schema.optional(TrimmedNonEmptyString),
6565
email: Schema.optional(TrimmedNonEmptyString),
6666
/**
67-
* The organization the login is signed in to, when the provider reports
68-
* one. One email can belong to several orgs, each with its own quota.
67+
* The display name of the organization the login is signed in to, when the
68+
* provider reports one. Names can repeat and change; see `accountId`.
6969
*/
7070
organization: Schema.optional(TrimmedNonEmptyString),
71+
/**
72+
* A stable id for the organization or workspace whose quota the login draws
73+
* on, such as Claude's organization UUID. One email can belong to several,
74+
* each with its own quota.
75+
*/
76+
accountId: Schema.optional(TrimmedNonEmptyString),
7177
});
7278
export type ServerProviderAuth = typeof ServerProviderAuth.Type;
7379

‎packages/shared/src/usageLimits.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,34 @@ describe("pools", () => {
473473
]);
474474
});
475475

476+
it("tells orgs apart by their id, not their display name", () => {
477+
const first = provider({
478+
driver: claude,
479+
instanceId: ProviderInstanceId.make("claude"),
480+
auth: {
481+
status: "authenticated",
482+
483+
organization: "Acme",
484+
accountId: "org-1",
485+
},
486+
usageLimits: { checkedAt, windows: [window] },
487+
});
488+
const withOrg = (instanceId: string, organization: string, accountId: string) => ({
489+
...first,
490+
instanceId: ProviderInstanceId.make(instanceId),
491+
auth: { ...first.auth, organization, accountId },
492+
});
493+
const keysFor = (providers: ServerProvider[]) =>
494+
collectLimitAccounts(
495+
new Map([[EnvironmentId.make("env-a"), { ...laptop, serverConfig: { providers } }]]),
496+
).map((account) => account.key);
497+
expect(keysFor([first, withOrg("namesake", "Acme", "org-2")])).toEqual([
498+
"env-a:claude",
499+
"env-a:namesake",
500+
]);
501+
expect(keysFor([first, withOrg("renamed", "Acme Inc", "org-1")])).toEqual(["env-a:claude"]);
502+
});
503+
476504
it("joins a hub account to the native org only when one org uses the email", () => {
477505
const native = provider({
478506
driver: claude,

‎packages/shared/src/usageLimits.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ function accountKey(driver: ServerProvider["driver"], email: string | undefined)
5959

6060
/**
6161
* One email can belong to several orgs, each with its own quota, so a native
62-
* login that names its org is keyed by it too. Hubs report no org; see
62+
* login that names its org is keyed by it too: by the org's stable id, or by
63+
* its display name when the provider reports no id. Hubs report no org; see
6364
* `collectLimitAccounts` for how they are matched.
6465
*/
6566
function nativeAccountKey(provider: ServerProvider): string | null {
6667
const key = accountKey(provider.driver, provider.auth.email);
67-
const organization = provider.auth.organization?.trim().toLowerCase();
68+
const organization = provider.auth.accountId ?? provider.auth.organization?.trim().toLowerCase();
6869
return key && organization ? `${key}:${organization}` : key;
6970
}
7071

0 commit comments

Comments
 (0)