diff --git a/apps/server/src/fork/claudeUsageSource/claudeHub.test.ts b/apps/server/src/fork/claudeUsageSource/claudeHub.test.ts new file mode 100644 index 0000000000000..0e0e1f66cd9b0 --- /dev/null +++ b/apps/server/src/fork/claudeUsageSource/claudeHub.test.ts @@ -0,0 +1,257 @@ +import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; +import { describe, expect, it } from "@effect/vitest"; +import * as Duration from "effect/Duration"; +import * as Effect from "effect/Effect"; +import * as Fiber from "effect/Fiber"; +import * as Schema from "effect/Schema"; +import * as TestClock from "effect/testing/TestClock"; +import { HttpClient, HttpClientError, HttpClientResponse } from "effect/http"; + +import { makeCliproxyApi } from "../../usage/cliproxyApi.ts"; + +const config = { + kind: "cliproxy", + url: "http://hub.test:8317", + managementKey: "management-secret", + enabled: true, +} as const; +const codex = { + id: "first.json", + auth_index: "a", + provider: "codex", + email: "first@example.com", + id_token: { chatgpt_account_id: "account-a" }, +}; +const claude = { ...codex, provider: "claude", organization_uuid: "org-1" }; +type HubAccount = typeof codex & { disabled?: boolean; organization_uuid?: string }; + +const RequestBody = Schema.Struct({ + auth_index: Schema.String, + method: Schema.optional(Schema.String), + url: Schema.optional(Schema.String), + header: Schema.optional(Schema.Record(Schema.String, Schema.String)), + data: Schema.optional(Schema.String), +}); +type RequestBody = typeof RequestBody.Type; +const decodeRequest = Schema.decodeUnknownSync(Schema.fromJsonString(RequestBody)); +const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown)); + +function fixture(options: { + accounts: ReadonlyArray; + upstream?: (request: RequestBody) => { status: number; body: unknown }; + redeemDelay?: Duration.Input; +}) { + const requests: Array<{ path: string; body?: RequestBody }> = []; + const http = HttpClient.make((request) => + Effect.sync(() => { + const path = new URL(request.url).pathname; + const body = + request.body._tag === "Uint8Array" + ? decodeRequest(new TextDecoder().decode(request.body.body)) + : undefined; + requests.push({ path, ...(body ? { body } : {}) }); + if (path.endsWith("/auth-files")) { + return HttpClientResponse.fromWeb(request, Response.json({ files: options.accounts })); + } + if (path.endsWith("/reset-quota")) { + return HttpClientResponse.fromWeb(request, Response.json({})); + } + const upstream = options.upstream?.(body!) ?? { status: 200, body: {} }; + return HttpClientResponse.fromWeb( + request, + Response.json({ status_code: upstream.status, body: encodeJson(upstream.body) }), + ); + }).pipe( + Effect.delay( + request.method === "POST" && request.url.endsWith("/api-call") + ? (options.redeemDelay ?? 0) + : 0, + ), + ), + ); + return { + requests, + api: makeCliproxyApi.pipe( + Effect.provideService(HttpClient.HttpClient, http), + Effect.provide(NodeCrypto.layer), + ), + }; +} + +describe("Claude through a CLIProxyAPI hub", () => { + it.effect("reads Claude reset credits with the account's usage", () => + Effect.gen(function* () { + yield* TestClock.setTime(Date.parse("2026-10-31T12:00:00Z")); + const test = fixture({ + accounts: [claude], + upstream: () => ({ + status: 200, + body: { + cedar_ember: { + eligible: true, + next_grant_id: "grant_a", + grants: [ + { + id: "grant_a", + resets_left: 1, + usable_now: true, + ends_at: "2099-01-01T00:00:00Z", + }, + { id: "grant_b", resets_left: 2, usable_now: true }, + ], + }, + five_hour: { utilization: 10, resets_at: null }, + }, + }), + }); + const result = yield* (yield* test.api).readAccounts(config); + expect(result[0]?.usageLimits.resetCredits).toEqual({ + availableCount: 3, + nextCreditId: "grant_a", + nextExpiresAt: "2099-01-01T00:00:00.000Z", + }); + expect(test.requests.find((request) => request.body?.url)?.body?.url).toBe( + "https://api.anthropic.com/api/oauth/usage?cedar_ember=1", + ); + }), + ); + + it.effect("finds a Claude login only by its exact email and organization", () => + Effect.gen(function* () { + const login = { email: "FIRST@example.com", organizationId: "org-1" }; + for (const [hubAccounts, expected] of [ + [[{ ...claude, organization_uuid: "org-2" }, claude], "first.json"], + [[{ ...claude, organization_uuid: "org-2" }], undefined], + [[{ ...codex, provider: "claude" }], undefined], + [[{ ...claude, disabled: true }], undefined], + [[claude, { ...claude, id: "copy.json" }], undefined], + ] as const) { + const api = yield* fixture({ accounts: hubAccounts }).api; + expect((yield* api.claude.findLogin([config], login))?.account.id).toBe(expected); + } + const test = fixture({ accounts: [claude] }); + const api = yield* test.api; + expect( + yield* api.claude.findLogin( + [ + { ...config, enabled: false }, + { ...config, managementKey: "" }, + ], + login, + ), + ).toBeUndefined(); + expect( + yield* api.claude.findLogin([config], { ...login, organizationId: undefined }), + ).toBeUndefined(); + expect(test.requests).toEqual([]); + const unreachable = yield* makeCliproxyApi.pipe( + Effect.provideService( + HttpClient.HttpClient, + HttpClient.make((request) => + Effect.fail( + new HttpClientError.HttpClientError({ + reason: new HttpClientError.TransportError({ request }), + }), + ), + ), + ), + Effect.provide(NodeCrypto.layer), + ); + const error = yield* Effect.flip(unreachable.claude.findLogin([config], login)); + expect(error._tag).toBe("UsageLimitSourceError"); + }), + ); + + it.effect("redeems a Claude reset for the account's own organization", () => + Effect.gen(function* () { + const answer = (status: number, body: unknown) => + fixture({ accounts: [claude], upstream: () => ({ status, body }) }); + for (const [status, body, outcome, clearsCooldown] of [ + [200, { result: "reset" }, "reset", true], + [200, { result: "already_used" }, "alreadyRedeemed", true], + [200, { result: "not_limited" }, "nothingToReset", false], + [200, { result: "ineligible" }, "noCredit", false], + ] as const) { + const test = answer(status, body); + expect(yield* (yield* test.api).consume(config, "first.json", "grant_a")).toEqual({ + outcome, + }); + const redemption = test.requests.find((request) => request.body?.url); + expect(redemption?.body?.url).toBe( + "https://api.anthropic.com/api/organizations/org-1/reset_rate_limits", + ); + expect(redemption?.body?.method).toBe("POST"); + expect(redemption?.body?.header?.["Content-Type"]).toBe("application/json"); + expect(JSON.parse(redemption?.body?.data ?? "")).toEqual({ + program: "cedar_ember", + grant_id: "grant_a", + request_id: expect.stringMatching(/^[0-9a-f-]{36}$/), + }); + expect(test.requests.some((request) => request.path.endsWith("/reset-quota"))).toBe( + clearsCooldown, + ); + } + const twice = answer(200, { result: "reset" }); + const api = yield* twice.api; + yield* api.consume(config, "first.json", "grant_a"); + yield* api.consume(config, "first.json", "grant_a"); + const requestIds = twice.requests.flatMap((request) => + request.body?.data ? [JSON.parse(request.body.data).request_id] : [], + ); + expect(new Set(requestIds).size).toBe(2); + for (const [status, body, detail] of [ + [200, { result: "cooldown" }, "Claude resets are cooling down. Try again later."], + [ + 200, + { result: "unavailable" }, + "Claude could not confirm the reset. If you are still limited in a moment, try again.", + ], + [429, {}, "Claude is rate limiting resets. Try again soon."], + [403, { error: "organization mismatch" }, "Sign in to Claude again to redeem resets."], + [502, {}, "Claude could not redeem the reset."], + ] as const) { + const test = answer(status, body); + const error = yield* Effect.flip( + (yield* test.api).consume(config, "first.json", "grant_a"), + ); + expect(error.detail).toBe(detail); + expect(test.requests.some((request) => request.path.endsWith("/reset-quota"))).toBe(false); + } + }), + ); + + it.effect("waits longer for a redeem than for a read", () => + Effect.gen(function* () { + for (const account of [codex, claude]) { + const api = yield* fixture({ + accounts: [account], + upstream: (request) => ({ + status: 200, + body: request.url?.endsWith("/consume") ? { code: "reset" } : { result: "reset" }, + }), + redeemDelay: "29 seconds", + }).api; + const redeem = yield* Effect.forkChild(api.consume(config, "first.json", "grant_a")); + yield* TestClock.adjust("29 seconds"); + expect((yield* Fiber.join(redeem)).outcome).toBe("reset"); + } + }), + ); + + it.effect("refuses a Claude redemption without an organization or with a malformed grant", () => + Effect.gen(function* () { + for (const [account, grantId] of [ + [{ ...codex, provider: "claude" }, "grant_a"], + [{ ...claude, organization_uuid: " " }, "grant_a"], + [claude, "Bad Grant"], + ] as const) { + const test = fixture({ accounts: [account] }); + const result = yield* (yield* test.api) + .consume(config, "first.json", grantId) + .pipe(Effect.result); + expect(result._tag).toBe("Failure"); + expect(test.requests.every((request) => request.path.endsWith("/auth-files"))).toBe(true); + } + }), + ); +}); diff --git a/apps/server/src/fork/claudeUsageSource/claudeHub.ts b/apps/server/src/fork/claudeUsageSource/claudeHub.ts new file mode 100644 index 0000000000000..833b15c2ec51a --- /dev/null +++ b/apps/server/src/fork/claudeUsageSource/claudeHub.ts @@ -0,0 +1,298 @@ +import { UsageLimitSourceError, type UsageLimitSourceConfig } from "@t3tools/contracts"; +import * as Crypto from "effect/Crypto"; +import * as DateTime from "effect/DateTime"; +import * as Effect from "effect/Effect"; +import * as Schema from "effect/Schema"; + +import { + CLAIM_OUTCOMES, + ClaimResponse, + ClaudeResetCreditError, + GRANT_ID, + PROGRAM, + REQUEST_ID, + claudeResetCreditsToContract, +} from "../../provider/claudeResetCredits.ts"; + +type HubAccount = { + readonly auth_index: string; + readonly provider: string; + readonly email?: string | undefined; + readonly disabled?: boolean | undefined; + readonly organization_uuid?: string | undefined; +}; + +export type ClaudeLogin = { + readonly email: string | undefined; + readonly organizationId: string | undefined; +}; + +const ClaudeWindow = Schema.Struct({ + utilization: Schema.Number, + resets_at: Schema.NullOr(Schema.String), +}); +const ClaudeUsage = Schema.Struct({ + cedar_ember: Schema.optional(Schema.NullOr(Schema.Unknown)), + five_hour: Schema.optional(Schema.NullOr(ClaudeWindow)), + seven_day: Schema.optional(Schema.NullOr(ClaudeWindow)), + spend: Schema.optional(Schema.Unknown), + extra_usage: Schema.optional( + Schema.NullOr( + Schema.Struct({ + is_enabled: Schema.Boolean, + monthly_limit: Schema.NullOr(Schema.Number), + used_credits: Schema.NullOr(Schema.Number), + utilization: Schema.NullOr(Schema.Number), + }), + ), + ), + limits: Schema.optional( + Schema.Array( + Schema.Struct({ + kind: Schema.String, + percent: Schema.optional(Schema.NullOr(Schema.Number)), + resets_at: Schema.optional(Schema.NullOr(Schema.String)), + scope: Schema.optional( + Schema.NullOr( + Schema.Struct({ + model: Schema.optional(Schema.NullOr(Schema.Struct({ display_name: Schema.String }))), + }), + ), + ), + }), + ), + ), +}); +const ApiResponse = Schema.Struct({ status_code: Schema.Number, body: Schema.String }); + +const decodeClaudeUsage = Schema.decodeUnknownEffect(Schema.fromJsonString(ClaudeUsage)); +const decodeApiResponse = Schema.decodeUnknownEffect(ApiResponse); +const decodeClaimResponse = Schema.decodeUnknownEffect(Schema.fromJsonString(ClaimResponse)); +const encodeJson = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown)); +const isUsageLimitSourceError = Schema.is(UsageLimitSourceError); + +const CLAUDE_API = "https://api.anthropic.com/api"; +export const CLAUDE_USAGE_URL = `${CLAUDE_API}/oauth/usage?cedar_ember=1`; + +/** A redeem waits on the hub's own call to the provider, which can take longer than a read. */ +export const managementTimeout = (body: unknown) => + typeof body === "object" && body !== null && "data" in body ? "30 seconds" : "15 seconds"; + +/** Claude reads and reset claims through a CLIProxyAPI hub's management API. */ +export const makeClaudeHub = Effect.fn("makeClaudeHub")(function* ( + management: ( + config: UsageLimitSourceConfig, + path: string, + body?: unknown, + ) => Effect.Effect, + authFiles: ( + config: UsageLimitSourceConfig, + ) => Effect.Effect, UsageLimitSourceError | Schema.SchemaError>, +) { + const crypto = yield* Crypto.Crypto; + + const apiCall = Effect.fn("ClaudeHub.apiCall")(function* ( + config: UsageLimitSourceConfig, + account: HubAccount, + url: string, + data?: unknown, + ) { + const raw = yield* management(config, "api-call", { + auth_index: account.auth_index, + method: data === undefined ? "GET" : "POST", + url, + header: { + Authorization: "Bearer $TOKEN$", + "anthropic-beta": "oauth-2025-04-20", + ...(data === undefined ? {} : { "Content-Type": "application/json" }), + }, + ...(data === undefined ? {} : { data: yield* encodeJson(data) }), + }); + return yield* decodeApiResponse(raw); + }); + + const resetCredits = Effect.fn("ClaudeHub.resetCredits")(function* ( + usage: typeof ClaudeUsage.Type, + ) { + return claudeResetCreditsToContract( + usage.cedar_ember, + DateTime.toEpochMillis(yield* DateTime.now), + ); + }); + + /** Adds reset credits from a usage answer to a hub account read from it. */ + const withResetCredits = Effect.fn("ClaudeHub.withResetCredits")(function* < + A extends { readonly usageLimits: object }, + >(body: string, account: A) { + const credits = yield* resetCredits(yield* decodeClaudeUsage(body)); + return credits + ? { ...account, usageLimits: { ...account.usageLimits, resetCredits: credits } } + : account; + }); + + /** Usage in the Claude SDK's get_usage shape, plus reset credits. */ + const usage = Effect.fn("ClaudeHub.usage")( + function* (config: UsageLimitSourceConfig, account: HubAccount) { + const response = yield* apiCall(config, account, CLAUDE_USAGE_URL); + if (response.status_code < 200 || response.status_code >= 300) { + return yield* new UsageLimitSourceError({ + detail: `The provider refused the hub request (HTTP ${response.status_code}).`, + }); + } + const usage = yield* decodeClaudeUsage(response.body); + const model_scoped = (usage.limits ?? []).flatMap((limit) => + limit.kind === "weekly_scoped" && limit.scope?.model && typeof limit.percent === "number" + ? [ + { + display_name: limit.scope.model.display_name, + utilization: limit.percent, + resets_at: limit.resets_at ?? null, + }, + ] + : [], + ); + return { + response: { + rate_limits_available: true, + rate_limits: { + five_hour: usage.five_hour ?? null, + seven_day: usage.seven_day ?? null, + model_scoped, + spend: usage.spend, + extra_usage: usage.extra_usage ?? null, + }, + }, + resetCredits: yield* resetCredits(usage), + }; + }, + Effect.mapError((error) => + isUsageLimitSourceError(error) + ? error + : new UsageLimitSourceError({ detail: "The hub returned an unexpected usage response." }), + ), + ); + + const claim = Effect.fn("ClaudeHub.claim")(function* ( + config: UsageLimitSourceConfig, + account: HubAccount, + grantId: string, + requestId: string, + ) { + if (!GRANT_ID.test(grantId) || !REQUEST_ID.test(requestId)) { + return yield* new ClaudeResetCreditError({ reason: "malformedCredit" }); + } + const organization = account.organization_uuid?.trim(); + if (!organization) { + return yield* new UsageLimitSourceError({ + detail: "The hub does not know this Claude account's organization.", + }); + } + const response = yield* apiCall( + config, + account, + `${CLAUDE_API}/organizations/${encodeURIComponent(organization)}/reset_rate_limits`, + { program: PROGRAM, grant_id: grantId, request_id: requestId }, + ); + if (response.status_code === 429) { + return yield* new ClaudeResetCreditError({ reason: "rateLimited" }); + } + if (response.status_code === 401 || response.status_code === 403) { + return yield* new ClaudeResetCreditError({ reason: "signedOut" }); + } + if (response.status_code < 200 || response.status_code >= 300) { + return yield* new ClaudeResetCreditError({ reason: "requestFailed" }); + } + const body = yield* decodeClaimResponse(response.body).pipe( + Effect.mapError((cause) => new ClaudeResetCreditError({ reason: "requestFailed", cause })), + ); + if (body.result === "cooldown") { + return yield* new ClaudeResetCreditError({ reason: "coolingDown" }); + } + // Unknown outcomes retain the request ID so retries address the same claim. + if (body.result === "unavailable") { + return yield* new ClaudeResetCreditError({ reason: "unconfirmed" }); + } + return CLAIM_OUTCOMES[body.result]; + }); + + const clearCooldown = (config: UsageLimitSourceConfig, account: HubAccount) => + management(config, "reset-quota", { auth_index: account.auth_index }); + + /** + * The source account holding a directly signed-in Claude login, if exactly one source has it. + * A source that cannot list its accounts fails the lookup; it never counts as no match. + */ + const findLogin = Effect.fn("ClaudeHub.findLogin")(function* ( + sources: ReadonlyArray, + login: ClaudeLogin, + ) { + const { email, organizationId } = login; + if (!email || !organizationId) return undefined; + for (const config of sources) { + if (!config.enabled || !config.managementKey) continue; + const accounts = yield* authFiles(config).pipe( + Effect.mapError((error) => + isUsageLimitSourceError(error) + ? error + : new UsageLimitSourceError({ detail: "The hub could not list accounts." }), + ), + ); + const matches = accounts.filter( + (account) => + account.provider === "claude" && + !account.disabled && + account.email?.toLowerCase() === email.toLowerCase() && + account.organization_uuid === organizationId, + ); + if (matches.length === 1) return { config, account: matches[0]! }; + } + return undefined; + }); + + // Claim errors stay typed so the caller's coordinator keeps the request id for retries. + const claimLogin = Effect.fn("ClaudeHub.claimLogin")(function* ( + config: UsageLimitSourceConfig, + account: HubAccount, + grantId: string, + requestId: string, + ) { + const outcome = yield* claim(config, account, grantId, requestId); + if (outcome === "reset" || outcome === "alreadyRedeemed") { + yield* clearCooldown(config, account).pipe(Effect.ignore); + } + return outcome; + }); + + /** Redeems a reset on a Claude account the hub lists. */ + const consume = Effect.fn("ClaudeHub.consume")(function* ( + config: UsageLimitSourceConfig, + account: HubAccount, + grantId: string, + ) { + if (account.disabled) { + return yield* new UsageLimitSourceError({ + detail: "The hub account is missing or disabled.", + }); + } + // A Claude grant can hold several resets, so each claim needs its own request id. + const outcome = yield* claim(config, account, grantId, yield* crypto.randomUUIDv4()).pipe( + Effect.catchTags({ + ClaudeResetCreditError: (error) => + Effect.fail(new UsageLimitSourceError({ detail: error.message })), + }), + ); + if (outcome !== "reset" && outcome !== "alreadyRedeemed") return { outcome }; + const cleared = yield* clearCooldown(config, account).pipe(Effect.result); + return { + outcome, + ...(cleared._tag === "Failure" + ? { + warning: + "Credit redeemed, but the hub cooldown could not be cleared. Routing may resume after its cooldown expires.", + } + : {}), + } as const; + }); + + return { withResetCredits, usage, findLogin, claimLogin, consume }; +}); diff --git a/apps/server/src/fork/claudeUsageSource/claudeLoginSource.test.ts b/apps/server/src/fork/claudeUsageSource/claudeLoginSource.test.ts new file mode 100644 index 0000000000000..6d389a33bab4c --- /dev/null +++ b/apps/server/src/fork/claudeUsageSource/claudeLoginSource.test.ts @@ -0,0 +1,301 @@ +import * as NodeServices from "@effect/platform-node/NodeServices"; +import { describe, expect, it } from "@effect/vitest"; +import { + type ClaudeSettings, + EnvironmentId, + ProviderDriverKind, + ProviderInstanceId, + UsageLimitSourceId, +} from "@t3tools/contracts"; +import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions"; +import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEventLoggers"; +import * as ProviderLatestVersions from "@t3tools/provider-core/server/ProviderLatestVersions"; +import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver"; +import * as HostProcess from "@t3tools/shared/HostProcess"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Layer from "effect/Layer"; +import * as Path from "effect/Path"; +import * as Schema from "effect/Schema"; +import { HttpClient, HttpClientResponse } from "effect/http"; + +import * as ServerSecretStore from "../../auth/ServerSecretStore.ts"; +import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts"; +import * as ServerConfig from "../../config.ts"; +import * as ServerEnvironment from "../../environment/ServerEnvironment.ts"; +import * as PortScanner from "../../preview/PortScanner.ts"; +import { ClaudeDriver } from "../../provider/Drivers/ClaudeDriver.ts"; +import * as ModelManifest from "../../provider/ModelManifest.ts"; +import { makeProviderInstanceRegistry } from "../../provider/ProviderInstanceRegistry.ts"; +import * as ProviderHostLive from "../../provider/ProviderHostLive.ts"; +import * as ProviderOrchestrationAdapterInfrastructure from "../../provider/ProviderOrchestrationAdapterInfrastructure.ts"; +import * as ResetCreditCoordinator from "../../provider/resetCreditCoordinator.ts"; +import * as ServerSettings from "../../serverSettings.ts"; + +// The mock Claude CLI is a script Windows cannot execute. +const windowsHost = HostProcess.Platform.defaultValue() === "win32"; + +const decodeHubCall = Schema.decodeUnknownSync( + Schema.fromJsonString(Schema.Struct({ url: Schema.String })), +); + +const layerTest = ProviderOrchestrationAdapterInfrastructure.layer.pipe( + Layer.provide(PortScanner.layerTest), + Layer.provideMerge(ProviderHostLive.layer), + Layer.provideMerge( + ServerConfig.layerTest(process.cwd(), { prefix: "claude-login-source-test" }).pipe( + Layer.provideMerge(NodeServices.layer), + Layer.provideMerge(ServerSettings.layerTest()), + Layer.provideMerge(Layer.mock(ServerSecretStore.ServerSecretStore)({})), + Layer.provideMerge(Layer.mock(SecretResolver.SecretResolver)({})), + Layer.provideMerge( + Layer.succeed(ServerEnvironment.ServerEnvironmentIdentity, { + getEnvironmentId: Effect.succeed( + EnvironmentId.make("00000000-0000-4000-8000-000000000001"), + ), + }), + ), + Layer.provideMerge( + Layer.mock(BackgroundPolicy.BackgroundPolicy)({ + shouldRunScopeWork: () => Effect.succeed(true), + shouldRunOpportunisticWork: Effect.succeed(true), + }), + ), + Layer.provideMerge( + Layer.succeed( + ProviderEventLoggers.ProviderEventLoggers, + ProviderEventLoggers.NoOpProviderEventLoggers, + ), + ), + Layer.provideMerge(ProviderLatestVersions.layer), + Layer.provideMerge(McpProviderSessions.layer), + Layer.provideMerge(ModelManifest.layerTest), + Layer.provideMerge(ResetCreditCoordinator.layerTest), + ), + ), +); + +const redeemClaudeReset = (claim: { + usageSourceOnly?: boolean; + hub?: { + accounts: ReadonlyArray<{ email: string; organization_uuid?: string }>; + listStatus?: number; + usageStatus?: number; + }; +}) => + Effect.gen(function* () { + const fs = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const fixtureDir = yield* fs.makeTempDirectoryScoped(); + const claudePath = path.join(fixtureDir, "claude"); + const claudeHomePath = path.join(fixtureDir, "claude-home"); + yield* fs.copyFile( + yield* path.fromFileUrl( + new URL("../../provider/testing/ProviderInstanceRegistryLive.fixture.mjs", import.meta.url), + ), + claudePath, + ); + yield* fs.chmod(claudePath, 0o755); + yield* fs.makeDirectory(claudeHomePath); + const marker = path.join(claudeHomePath, "redeemed"); + yield* fs.writeFileString( + path.join(claudeHomePath, ".credentials.json"), + '{"claudeAiOauth":{"accessToken":"fake-token"}}', + ); + yield* fs.writeFileString( + path.join(claudeHomePath, ".claude.json"), + '{"oauthAccount":{"emailAddress":"test@example.com","organizationUuid":"fake-org"}}', + ); + const direct: Array = []; + const viaHub: Array = []; + let hubRedeemed = false; + const credits = { + eligible: true, + next_grant_id: "grant_a", + grants: [{ id: "grant_a", resets_left: 1, usable_now: true }], + }; + const client = HttpClient.make((request) => + Effect.gen(function* () { + const json = (body: unknown, status = 200) => + HttpClientResponse.fromWeb(request, Response.json(body, { status })); + const url = new URL(request.url); + if (claim.hub && url.host === "hub.test") { + if (url.pathname.endsWith("/auth-files")) { + viaHub.push("auth-files"); + return json( + { + files: claim.hub.accounts.map((account, index) => ({ + id: `${index}.json`, + auth_index: `${index}`, + provider: "claude", + ...account, + })), + }, + claim.hub.listStatus, + ); + } + if (url.pathname.endsWith("/reset-quota")) { + viaHub.push("reset-quota"); + return json({}); + } + const call = decodeHubCall( + request.body._tag === "Uint8Array" ? new TextDecoder().decode(request.body.body) : "", + ); + viaHub.push(call.url); + if (call.url.endsWith("/reset_rate_limits")) { + hubRedeemed = true; + return json({ status_code: 200, body: JSON.stringify({ result: "reset" }) }); + } + return json({ + status_code: claim.hub.usageStatus ?? 200, + body: JSON.stringify({ + five_hour: { utilization: hubRedeemed ? 0 : 100, resets_at: null }, + spend: { + enabled: true, + used: { amount_minor: 4_631, currency: "USD", exponent: 2 }, + limit: { amount_minor: 50_000, currency: "USD", exponent: 2 }, + }, + cedar_ember: credits, + }), + }); + } + if (url.host === "api.anthropic.com") direct.push(url.pathname); + if (request.url.endsWith("/api/oauth/usage")) return json({ cedar_ember: credits }); + if (request.url.endsWith("/reset_rate_limits")) { + yield* fs.writeFileString(marker, "redeemed").pipe(Effect.orDie); + return json({ result: "reset" }); + } + return json({ version: "0.0.0" }); + }), + ); + const instanceId = ProviderInstanceId.make("claude_reset"); + const { registry } = yield* makeProviderInstanceRegistry({ + drivers: [ClaudeDriver], + configMap: { + [instanceId]: { + driver: ProviderDriverKind.make("claudeAgent"), + enabled: true, + environment: [ + { name: "T3_CLAUDE_RESET_MARKER", value: marker, sensitive: false }, + ...(claim.usageSourceOnly + ? [{ name: "T3CODE_CLAUDE_USAGE_SOURCE_ONLY", value: "1", sensitive: false }] + : []), + ], + config: { + enabled: true, + binaryPath: claudePath, + homePath: claudeHomePath, + customModels: [], + launchArgs: "", + autoCompactWindow: "", + idleCompaction: "default", + } satisfies ClaudeSettings, + }, + }, + }).pipe( + Effect.provideService(HttpClient.HttpClient, client), + Effect.provide( + ServerSettings.layerTest( + claim.hub + ? { + usageLimitSources: { + [UsageLimitSourceId.make("hub")]: { + kind: "cliproxy", + url: "http://hub.test", + managementKey: "hub-key", + enabled: true, + }, + }, + } + : {}, + ), + ), + ); + const instance = yield* registry.getInstance(instanceId); + expect(instance).toBeDefined(); + const before = yield* instance!.snapshot.refresh; + const outcome = yield* instance!.consumeResetCredit!().pipe(Effect.result); + return { before, outcome, after: yield* instance!.snapshot.getSnapshot, direct, viaHub }; + }).pipe( + // macOS logins live in the Keychain, where resets are never read. + Effect.provideService(HostProcess.Platform, "linux"), + Effect.provide(layerTest), + ); + +describe("Claude logins held by a usage source", () => { + it.live.skipIf(windowsHost)("reads Claude directly when no usage source holds the login", () => + Effect.gen(function* () { + const { before, outcome, after, direct, viaHub } = yield* redeemClaudeReset({ + hub: { + accounts: [ + { email: "test@example.com", organization_uuid: "other-org" }, + { email: "test@example.com" }, + ], + }, + }); + expect(before.usageLimits?.windows[0]?.usedPercent).toBe(100); + expect(before.usageLimits?.resetCredits?.nextCreditId).toBe("grant_a"); + expect(outcome).toMatchObject({ _tag: "Success", success: "reset" }); + expect(after.usageLimits?.windows[0]?.usedPercent).toBe(0); + expect(direct).toContain("/api/organizations/fake-org/reset_rate_limits"); + expect(viaHub.every((call) => call === "auth-files")).toBe(true); + }), + ); + + it.live.skipIf(windowsHost)("reads and redeems the login only through that source", () => + Effect.gen(function* () { + const { before, outcome, after, direct, viaHub } = yield* redeemClaudeReset({ + hub: { accounts: [{ email: "TEST@example.com", organization_uuid: "fake-org" }] }, + }); + expect(before.usageLimits?.windows[0]?.usedPercent).toBe(100); + expect(before.usageLimits?.windows.map((window) => window.id)).toContain("monthly_spend"); + expect(before.usageLimits?.resetCredits?.nextCreditId).toBe("grant_a"); + expect(outcome).toMatchObject({ _tag: "Success", success: "reset" }); + expect(after.usageLimits?.windows[0]?.usedPercent).toBe(0); + expect(direct).toEqual([]); + expect(viaHub).toContain( + "https://api.anthropic.com/api/organizations/fake-org/reset_rate_limits", + ); + expect(viaHub).toContain("reset-quota"); + }), + ); + + it.live.skipIf(windowsHost)( + "never reads Claude directly for a login no source holds when sources are required", + () => + Effect.gen(function* () { + for (const hub of [undefined, { accounts: [{ email: "test@example.com" }] }]) { + const { before, outcome, direct } = yield* redeemClaudeReset({ + usageSourceOnly: true, + ...(hub ? { hub } : {}), + }); + expect(before.usageLimits?.unavailable).toMatchObject({ + reason: "probeFailed", + message: "No usage source holds this Claude login.", + }); + expect(outcome).toMatchObject({ _tag: "Success", success: "noCredit" }); + expect(direct).toEqual([]); + } + }), + ); + + it.live.skipIf(windowsHost)("shows a usage source failure instead of reading Claude", () => + Effect.gen(function* () { + for (const failure of [{ usageStatus: 503 }, { listStatus: 500 }]) { + const { before, outcome, direct } = yield* redeemClaudeReset({ + hub: { + accounts: [{ email: "test@example.com", organization_uuid: "fake-org" }], + ...failure, + }, + }); + expect(before.usageLimits?.unavailable).toMatchObject({ + reason: "probeFailed", + message: expect.any(String), + }); + expect(before.usageLimits?.resetCredits).toBeUndefined(); + expect(outcome).toMatchObject({ _tag: "Success", success: "noCredit" }); + expect(direct).toEqual([]); + } + }), + ); +}); diff --git a/apps/server/src/fork/claudeUsageSource/claudeLoginSource.ts b/apps/server/src/fork/claudeUsageSource/claudeLoginSource.ts new file mode 100644 index 0000000000000..7441eb02a298a --- /dev/null +++ b/apps/server/src/fork/claudeUsageSource/claudeLoginSource.ts @@ -0,0 +1,118 @@ +import { + UsageLimitSourceError, + type ClaudeSettings, + type ServerProviderResetCredits, +} from "@t3tools/contracts"; +import * as DateTime from "effect/DateTime"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Option from "effect/Option"; +import * as Ref from "effect/Ref"; +import * as Result from "effect/Result"; +import * as Schema from "effect/Schema"; + +import * as Settings from "../../serverSettings.ts"; +import { makeCliproxyApi } from "../../usage/cliproxyApi.ts"; +import { probeClaudeCapabilities } from "../../provider/ClaudeProvider.ts"; + +const AccountConfig = Schema.Struct({ + oauthAccount: Schema.optional( + Schema.Struct({ + emailAddress: Schema.optional(Schema.String), + organizationUuid: Schema.optional(Schema.String), + }), + ), +}); +const decodeAccountConfig = Schema.decodeEffect(Schema.fromJsonString(AccountConfig)); + +/** + * A Claude login held by a usage source is read and redeemed only through that source. + * With T3CODE_CLAUDE_USAGE_SOURCE_ONLY=1 a login no source holds is never read directly. + */ +export const makeClaudeLoginSource = Effect.fn("makeClaudeLoginSource")(function* ( + accountConfigPath: string, + processEnv: NodeJS.ProcessEnv, +) { + const { claude } = yield* makeCliproxyApi; + const serverSettings = yield* Effect.serviceOption(Settings.ServerSettingsService); + const fileSystem = yield* FileSystem.FileSystem; + const sourceOnly = processEnv.T3CODE_CLAUDE_USAGE_SOURCE_ONLY === "1"; + const sourceCredits = yield* Ref.make(Option.none()); + + const readLogin = fileSystem.readFileString(accountConfigPath).pipe( + Effect.flatMap(decodeAccountConfig), + Effect.map(({ oauthAccount }) => ({ + email: oauthAccount?.emailAddress?.trim() || undefined, + organizationId: oauthAccount?.organizationUuid?.trim() || undefined, + })), + Effect.orElseSucceed(() => ({ email: undefined, organizationId: undefined })), + ); + + const findSource = Effect.fn("ClaudeLoginSource.findSource")(function* () { + const login = yield* readLogin; + const source = Option.isNone(serverSettings) + ? undefined + : yield* serverSettings.value.getSettings.pipe( + Effect.mapError( + () => new UsageLimitSourceError({ detail: "The usage sources could not be read." }), + ), + Effect.flatMap((settings) => + claude.findLogin(Object.values(settings.usageLimitSources), login), + ), + ); + if (!source && sourceOnly) { + return yield* new UsageLimitSourceError({ + detail: "No usage source holds this Claude login.", + }); + } + return source; + }); + + /** Probes Claude, taking usage from the source holding the login instead of from Claude. */ + const probe = Effect.fn("ClaudeLoginSource.probe")(function* ( + settings: ClaudeSettings, + environment: NodeJS.ProcessEnv, + cwd: string, + ) { + const usageCheckedAt = DateTime.formatIso(yield* DateTime.now); + const viaSource = yield* findSource().pipe( + Effect.flatMap((source) => + source + ? claude.usage(source.config, source.account).pipe(Effect.asSome) + : Effect.succeedNone, + ), + Effect.result, + ); + const direct = Result.isSuccess(viaSource) && Option.isNone(viaSource.success); + const capabilities = yield* probeClaudeCapabilities(settings, environment, cwd, direct); + yield* Ref.set( + sourceCredits, + Result.isSuccess(viaSource) + ? Option.map(viaSource.success, (usage) => usage.resetCredits) + : Option.none(), + ); + if (Result.isFailure(viaSource)) { + return { ...capabilities, usageError: { message: viaSource.failure.detail } }; + } + if (Option.isNone(viaSource.success)) return capabilities; + return { ...capabilities, usage: viaSource.success.value.response, usageCheckedAt }; + }); + + /** Reset credits from the source that answered the last probe, else the direct read. */ + const resetCredits = ( + direct: Effect.Effect, + ) => + Ref.get(sourceCredits).pipe( + Effect.flatMap(Option.match({ onNone: () => direct, onSome: Effect.succeed })), + ); + + /** Sends a claim to the source holding the login instead of to Claude. */ + const claimRoute = Effect.fn("ClaudeLoginSource.claimRoute")(function* (grantId: string) { + const source = yield* findSource(); + return (requestId: string) => + (direct: Effect.Effect) => + source ? claude.claimLogin(source.config, source.account, grantId, requestId) : direct; + }); + + return { probe, resetCredits, claimRoute }; +}); diff --git a/apps/server/src/provider/ClaudeProvider.ts b/apps/server/src/provider/ClaudeProvider.ts index 95f5c6709a4ea..fd2e17e343c77 100644 --- a/apps/server/src/provider/ClaudeProvider.ts +++ b/apps/server/src/provider/ClaudeProvider.ts @@ -244,6 +244,7 @@ export type ClaudeCapabilitiesProbe = { /** Undefined means get_usage failed; absent windows on success mean no subscription windows, as with API keys. */ readonly usage?: Pick; readonly usageCheckedAt?: string; + readonly usageError?: { readonly message: string }; }; function parseClaudeInitializationCommands( @@ -638,7 +639,7 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")( }); const usageCheckedAt = capabilities.usageCheckedAt ?? checkedAt; const usageLimits = !capabilities.usage - ? makeUnavailableUsageLimits({ checkedAt, reason: "probeFailed" }) + ? makeUnavailableUsageLimits({ checkedAt, reason: "probeFailed", ...capabilities.usageError }) : scopedLimitNames ? yield* recordClaudeUsageResponse(scopedLimitNames, { response: capabilities.usage, diff --git a/apps/server/src/provider/Drivers/ClaudeDriver.ts b/apps/server/src/provider/Drivers/ClaudeDriver.ts index e1fbcd30080fd..adcc13f607b19 100644 --- a/apps/server/src/provider/Drivers/ClaudeDriver.ts +++ b/apps/server/src/provider/Drivers/ClaudeDriver.ts @@ -26,10 +26,10 @@ import { claudeUsageReader } from "./claudeUsage.ts"; import { makeClaudeScopedLimitNames } from "../claudeUsageLimits.ts"; import * as ClaudeResetCredits from "../claudeResetCredits.ts"; import * as ResetCreditCoordinator from "../resetCreditCoordinator.ts"; +import { makeClaudeLoginSource } from "../../fork/claudeUsageSource/claudeLoginSource.ts"; import { checkClaudeProviderStatus, makePendingClaudeProvider, - probeClaudeCapabilities, probeClaudeWorkspaceSnapshot, type ClaudeCapabilitiesProbe, } from "../ClaudeProvider.ts"; @@ -161,6 +161,7 @@ export const ClaudeDriver: ProviderDriver(undefined); const capabilitiesProbeCache = yield* Cache.makeWith( () => - probeClaudeCapabilities(effectiveConfig, processEnv, stateDir).pipe( + claudeSource.probe(effectiveConfig, processEnv, stateDir).pipe( Effect.flatMap((capabilities) => ClaudeResetCredits.readClaudeOrganizationId(accountConfigPath).pipe( Effect.map((workspaceId) => ({ @@ -271,6 +272,7 @@ export const ClaudeDriver: ProviderDriver @@ -343,7 +346,7 @@ export const ClaudeDriver: ProviderDriver()( +export class ClaudeResetCreditError extends Schema.TaggedError()( "ClaudeResetCreditError", { reason: Schema.Literals( @@ -187,7 +187,7 @@ export const claudeAccountConfigPath = Effect.fn("claudeAccountConfigPath")(func : path.join(yield* HostProcess.HomeDirectory, ".claude.json"); }); -const CLAIM_OUTCOMES = { +export const CLAIM_OUTCOMES = { reset: "reset", not_limited: "nothingToReset", already_used: "alreadyRedeemed", diff --git a/apps/server/src/usage/cliproxyApi.test.ts b/apps/server/src/usage/cliproxyApi.test.ts index 17844caf4e07a..3e948d648c8d1 100644 --- a/apps/server/src/usage/cliproxyApi.test.ts +++ b/apps/server/src/usage/cliproxyApi.test.ts @@ -318,9 +318,9 @@ describe("CLIProxyAPI built-in management API", () => { Effect.gen(function* () { const test = fixture({ accounts: [{ ...accounts[0]!, provider: "claude" }], - // The usage endpoint answers 429 to requests without a Claude Code user agent. + // The hub sets its own client headers. upstream: (request) => ({ - status: request.header?.["User-Agent"]?.startsWith("claude-code/") ? 200 : 429, + status: request.header?.["User-Agent"] === undefined ? 200 : 400, body: { five_hour: { utilization: 10, resets_at: null }, seven_day: { utilization: 50, resets_at: "2099-01-01T00:00:00Z" }, diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.ts index b74b09aed60ce..e85352fe1bd64 100644 --- a/apps/server/src/usage/cliproxyApi.ts +++ b/apps/server/src/usage/cliproxyApi.ts @@ -17,6 +17,7 @@ import { codexPlanLabel } from "../provider/CodexProvider.ts"; import { codexRateLimitsToLimits } from "../provider/codexUsageLimits.ts"; import { claudeUsageResponseToLimits } from "../provider/claudeUsageLimits.ts"; import { makeUnavailableUsageLimits } from "@t3tools/provider-core/server/usageLimits"; +import * as ClaudeHub from "../fork/claudeUsageSource/claudeHub.ts"; const AuthFile = Schema.Struct({ id: Schema.String, @@ -25,6 +26,7 @@ const AuthFile = Schema.Struct({ label: Schema.optional(Schema.String), email: Schema.optional(Schema.String), disabled: Schema.optional(Schema.Boolean), + organization_uuid: Schema.optional(Schema.String), id_token: Schema.optional( Schema.Struct({ chatgpt_account_id: Schema.optional(Schema.String), @@ -194,7 +196,7 @@ export const makeCliproxyApi = Effect.gen(function* () { .pipe( Effect.flatMap(HttpClientResponse.filterStatusOk), Effect.flatMap((response) => response.json), - Effect.timeout("15 seconds"), + Effect.timeout(ClaudeHub.managementTimeout(body)), Effect.mapError( (error) => new UsageLimitSourceError({ detail: describeManagementFailure(error) }), ), @@ -206,6 +208,7 @@ export const makeCliproxyApi = Effect.gen(function* () { const response = yield* management(config, "auth-files"); return (yield* decodeAuthFiles(response)).files; }); + const claude = yield* ClaudeHub.makeClaudeHub(management, authFiles); const apiCall = Effect.fn("CliproxyApi.apiCall")(function* ( config: UsageLimitSourceConfig, @@ -227,8 +230,6 @@ export const makeCliproxyApi = Effect.gen(function* () { : { Authorization: "Bearer $TOKEN$", "anthropic-beta": "oauth-2025-04-20", - // The OAuth usage endpoint can return 429 without a Claude Code user agent. - "User-Agent": "claude-code/2.1.273", }; const raw = yield* management(config, "api-call", { auth_index: account.auth_index, @@ -279,7 +280,7 @@ export const makeCliproxyApi = Effect.gen(function* () { }; const read = Effect.gen(function* () { if (account.provider === "claude") { - const body = yield* apiCall(config, account, "https://api.anthropic.com/api/oauth/usage"); + const body = yield* apiCall(config, account, ClaudeHub.CLAUDE_USAGE_URL); const usage = yield* decodeClaudeUsage(body); const model_scoped = (usage.limits ?? []).flatMap((limit) => limit.kind === "weekly_scoped" && limit.scope?.model && typeof limit.percent === "number" @@ -299,7 +300,7 @@ export const makeCliproxyApi = Effect.gen(function* () { spend: usage.spend, extra_usage: usage.extra_usage ?? null, }; - return { + return yield* claude.withResetCredits(body, { ...base, plan: "Claude Subscription", usageLimits: claudeUsageResponseToLimits({ @@ -309,7 +310,7 @@ export const makeCliproxyApi = Effect.gen(function* () { rate_limits: rateLimits, }, }).limits, - }; + }); } const body = yield* apiCall(config, account, `${CODEX_BASE}/usage`); const usage = yield* decodeCodexUsage(body); @@ -394,9 +395,10 @@ export const makeCliproxyApi = Effect.gen(function* () { ): Effect.fn.Return { const operation = Effect.gen(function* () { const account = (yield* authFiles(config)).find((account) => account.id === accountId); + if (account?.provider === "claude") return yield* claude.consume(config, account, creditId); if (!account || account.disabled || account.provider !== "codex") { return yield* new UsageLimitSourceError({ - detail: "The Codex hub account is missing or disabled.", + detail: "The hub account is missing or disabled.", }); } const body = yield* apiCall(config, account, `${CREDIT_URL}/consume`, { @@ -439,5 +441,5 @@ export const makeCliproxyApi = Effect.gen(function* () { ), ); }); - return { readAccounts, consume }; + return { readAccounts, consume, claude }; }); diff --git a/fork/whiteboard/run-t3.sh b/fork/whiteboard/run-t3.sh index 32903a58efd30..163caf6d94328 100755 --- a/fork/whiteboard/run-t3.sh +++ b/fork/whiteboard/run-t3.sh @@ -24,6 +24,8 @@ export BASH_ENV="$WB_DIR/tools/agent-env.sh" # into it. The whiteboard wants the new-thread landing instead. export T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD=false export T3CODE_UNSAFE_NO_AUTH=1 +# Claude usage and resets go only through a usage source holding the login, never direct. +export T3CODE_CLAUDE_USAGE_SOURCE_ONLY=1 # exec keeps this PID, so `wb` finds its agent among the server's children. export WB_CHAT_PID=$$