fix: allow Claude Enterprise accounts under --hide-claude-auth (#1641)

## Problem

Under `--hide-claude-auth` the subscription guard refuses every
claude.ai account, Claude Enterprise included, so Enterprise users in
Air get `authRequired` (`claude_subscription_not_supported`) on every
session and prompt. Enterprise plans are allowed in this integration
(IJAI-2528).

## Change

- `isClaudeEnterprisePlan()` matches "Claude Enterprise" (the SDK's
`AccountInfo.subscriptionType`) and "enterprise" (older CLIs). Any other
name, including a custom `plan_display_name`, is still refused.
- `claudeAuthRefusal()` is now the guard's single decision, used by v1
and v2 session creation and the per-prompt guard instead of three copies
of the same check.

Free, Pro, Max and Team stay refused, and the claude.ai login method
stays hidden under the flag.

## Testing

v1 and v2 tests: Enterprise is allowed, Team is refused. `npm run build`
and `npm run check` pass. 24 tests in `create-session-options`,
`providers`, `session-index` and the v2 provider tests fail locally on
`main` too. Not checked against a real Enterprise account.
This commit is contained in:
Andrey Bragin
2026-11-16 15:30:58 +01:00
committed by GitHub
parent dc769c132c
commit be2219cef1
5 changed files with 102 additions and 40 deletions

View File

@@ -1,9 +1,11 @@
/**
* `--hide-claude-auth`: this integration must never bill a claude.ai
* subscription. Under the flag the agent hides the claude.ai login method from
* `initialize`, and refuses with `authRequired` every session and every turn
* that a subscription would pay for. `v1/acp-agent.ts` decides when the guard
* applies (flag on, no provider override); this module decides what it refuses.
* subscription, except a Claude Enterprise plan. Under the flag the agent hides
* the claude.ai login method from `initialize`, and refuses with `authRequired`
* every session and every turn that a Free, Pro, Max or Team plan would pay
* for. `v1/acp-agent.ts` and `v2/subscription-guard.ts` decide when the guard
* applies (flag on, no provider override); {@link claudeAuthRefusal} decides
* what it refuses.
*
* The guard judges the `AccountInfo` that the SDK caches at `initialize`. That
* cache never changes during the life of a query, but the CLI itself picks up
@@ -113,6 +115,20 @@ export function billsClaudeSubscription(account: AccountInfo | undefined): boole
return !API_KEY_SOURCES_ABOVE_SUBSCRIPTION.has(account.apiKeySource ?? "");
}
/**
* True when `subscriptionType` names a Claude Enterprise plan, which the flag
* lets pay for turns: Anthropic allows an Enterprise plan in third-party
* integrations.
*
* The SDK's `AccountInfo` carries the CLI's display name, "Claude Enterprise";
* older CLIs report the raw plan, "enterprise". Any other value is refused,
* including a "Claude <name>" built from a custom `plan_display_name` in the
* claude.ai profile: the guard cannot tell which plan that name belongs to.
*/
export function isClaudeEnterprisePlan(subscriptionType: string | undefined): boolean {
return /^(claude\s+)?enterprise$/i.test(subscriptionType?.trim() ?? "");
}
/**
* True when the account already holds a credential this integration accepts:
* a third-party backend, a bearer or OAuth environment token, or one of the
@@ -136,6 +152,27 @@ export function holdsNonSubscriptionCredential(account: AccountInfo | undefined)
return API_KEY_SOURCES_ABOVE_SUBSCRIPTION.has(account.apiKeySource ?? "");
}
/**
* The refusal the flag gives a session or turn on `account`, or `undefined`
* when the account may run it:
*
* - a subscription pays, and it is not {@link isClaudeEnterprisePlan}:
* {@link claudeSubscriptionNotSupportedError};
* - no subscription pays and the account holds no other credential (signed
* out): {@link claudeLoginRequiredError}.
*
* Session creation on v1 and v2 and the per-turn guard all decide through
* this function.
*/
export function claudeAuthRefusal(account: AccountInfo): RequestError | undefined {
if (billsClaudeSubscription(account)) {
return isClaudeEnterprisePlan(account.subscriptionType)
? undefined
: claudeSubscriptionNotSupportedError();
}
return holdsNonSubscriptionCredential(account) ? undefined : claudeLoginRequiredError();
}
export function claudeSubscriptionNotSupportedError(): RequestError {
return RequestError.authRequired(
{ reason: CLAUDE_SUBSCRIPTION_NOT_SUPPORTED_REASON satisfies AuthRequiredReason },
@@ -219,8 +256,8 @@ type GuardOptions = {
* real account. This guard does not see the residual case the module header
* names either.
*
* A turn is refused when the account bills a subscription, and also when it
* holds nothing this integration accepts, which is the user signing out in
* A turn is refused when the account bills a subscription other than Claude
* Enterprise, and also when it holds nothing this integration accepts, which is the user signing out in
* mid-session. The refusal mirrors the signed-out path exactly (see
* `failActiveWithSessionFailure` for `auth_required` in `v1/acp-agent.ts`): every
* client gets the `authRequired` JSON-RPC rejection that starts its own auth
@@ -262,13 +299,7 @@ async function evaluateGuard(options: GuardOptions): Promise<RequestError | unde
error instanceof Error ? error.message : String(error),
);
}
if (billsClaudeSubscription(account)) {
return claudeSubscriptionNotSupportedError();
}
if (!holdsNonSubscriptionCredential(account)) {
return claudeLoginRequiredError();
}
return undefined;
return claudeAuthRefusal(account);
}
/** The account the SDK cached at `initialize`. `undefined` when the query

View File

@@ -9,6 +9,7 @@ import { AcpClient } from "../../acp-client.js";
import {
billsClaudeSubscription,
holdsNonSubscriptionCredential,
isClaudeEnterprisePlan,
CLAUDE_LOGIN_REQUIRED_MESSAGE,
CLAUDE_SUBSCRIPTION_NOT_SUPPORTED_MESSAGE,
CLAUDE_SUBSCRIPTION_NOT_SUPPORTED_REASON,
@@ -466,6 +467,24 @@ describe("authorization", () => {
});
});
describe("isClaudeEnterprisePlan", () => {
it.each([
["Claude Enterprise", true],
["enterprise", true],
[" claude ENTERPRISE ", true],
["Claude Team", false],
["Claude Max", false],
["pro", false],
// A custom `plan_display_name` does not say which plan it belongs to.
["Claude Enterprise Trial", false],
["Claude Acme Enterprise", false],
["", false],
[undefined, false],
])("classifies subscriptionType %j", (subscriptionType, expected) => {
expect(isClaudeEnterprisePlan(subscriptionType)).toBe(expected);
});
});
describe("fails closed at session creation", () => {
/** What the flag does with an account at session creation. */
type Verdict = "subscription" | "signedOut" | "allowed";
@@ -508,6 +527,16 @@ describe("authorization", () => {
account: { tokenSource: "ANTHROPIC_AUTH_TOKEN" },
verdict: "allowed",
},
{
name: "a Claude Enterprise plan",
account: { subscriptionType: "Claude Enterprise", apiProvider: "firstParty" },
verdict: "allowed",
},
{
name: "a Claude Team plan",
account: { subscriptionType: "Claude Team", apiProvider: "firstParty" },
verdict: "subscription",
},
{ name: "bedrock", account: { apiProvider: "bedrock" }, verdict: "allowed" },
{ name: "vertex", account: { apiProvider: "vertex" }, verdict: "allowed" },
];
@@ -938,10 +967,13 @@ describe("authorization", () => {
expect(accountInfo).not.toHaveBeenCalled();
});
it("lets the turn through when the live account is not a subscription", async () => {
it.each([
["an API key", { apiKeySource: "ANTHROPIC_API_KEY" }],
["a Claude Enterprise plan", { subscriptionType: "Claude Enterprise" }],
])("lets the turn through when the live account is %s", async (_case, account) => {
const [agent] = await createAgentMock();
hideClaudeAuth();
const accountInfo = vi.fn(async () => ({ apiKeySource: "ANTHROPIC_API_KEY" }));
const accountInfo = vi.fn(async () => account);
mockAccount(SIGNED_IN_WITH_KEY, { accountInfo });
const { sessionId } = await agent.newSession(newSessionParams());

View File

@@ -10473,6 +10473,11 @@ describe("ACP v2 subscription guard (--hide-claude-auth)", () => {
{ code: -32000, data: undefined },
],
["a subscription has no token", { ...SUBSCRIPTION, tokenSource: "none" }, NOT_SUPPORTED],
[
"a Claude Team plan would pay",
{ ...SUBSCRIPTION, subscriptionType: "Claude Team" },
NOT_SUPPORTED,
],
])("refuses session/new when %s", async (_case, account, refusal) => {
accountsAtStart(account);
const client = v2Client();
@@ -10491,6 +10496,7 @@ describe("ACP v2 subscription guard (--hide-claude-auth)", () => {
["a bearer token", { tokenSource: "ANTHROPIC_AUTH_TOKEN" }],
["a setup token", { tokenSource: "CLAUDE_CODE_OAUTH_TOKEN" }],
["external credentials", { apiProvider: "bedrock", tokenSource: "none" }],
["a Claude Enterprise plan", { ...SUBSCRIPTION, subscriptionType: "Claude Enterprise" }],
["an unavailable account", undefined],
])("allows session/new with %s", async (_case, account) => {
accountsAtStart(account);

View File

@@ -258,11 +258,8 @@ import {
supportsAirSessionFailures,
} from "../session-failure-extension.js";
import {
billsClaudeSubscription,
claudeLoginRequiredError,
claudeAuthRefusal,
type ClaudeSubscriptionGuardState,
claudeSubscriptionNotSupportedError,
holdsNonSubscriptionCredential,
refuseClaudeSubscriptionTurn,
shouldHideClaudeAuth,
warnClaudeSubscriptionGuardDegraded,
@@ -10288,15 +10285,14 @@ export class ClaudeAcpAgent {
logger: this.logger,
cause: "the CLI reported no account at initialize",
});
} else if (billsClaudeSubscription(initializationResult.account)) {
throw claudeSubscriptionNotSupportedError();
} else if (!holdsNonSubscriptionCredential(initializationResult.account)) {
// Fail closed: the account holds nothing this integration can bill.
// A logged-out session must never exist, because the CLI can pick up
// a claude.ai login by itself and AIR resumes a parked prompt on the
// SAME session after a sign-in. Refusing before the session exists
// makes every sign-in lead to a new session and a fresh `initialize`.
throw claudeLoginRequiredError();
} else {
// Fail closed on a signed-out account too: a logged-out session must
// never exist, because the CLI can pick up a claude.ai login by itself
// and AIR resumes a parked prompt on the SAME session after a sign-in.
// Refusing before the session exists makes every sign-in lead to a new
// session and a fresh `initialize`.
const refusal = claudeAuthRefusal(initializationResult.account);
if (refusal) throw refusal;
}
}

View File

@@ -1,12 +1,13 @@
/**
* The `--hide-claude-auth` subscription guard on the v2 core: this
* integration never bills a claude.ai subscription. The policy is the v1
* core's (`hide-claude-auth.ts`):
* integration never bills a claude.ai subscription other than Claude
* Enterprise. The policy is the v1 core's (`claudeAuthRefusal` in
* `hide-claude-auth.ts`):
*
* - It holds while the flag is on and no provider or gateway login routes
* the traffic away from the CLI's own login.
* - A session does not start on an account that a subscription would pay
* for (`authRequired` with the `claude_subscription_not_supported`
* - A session does not start on an account that a Free, Pro, Max or Team
* plan would pay for (`authRequired` with the `claude_subscription_not_supported`
* reason), or that holds no credential this integration accepts
* (`authRequired`).
* - Each prompt reads the account that the query cached at its start, and is
@@ -23,11 +24,8 @@ import type { AccountInfo, Query } from "@anthropic-ai/claude-agent-sdk";
import type { Logger } from "../logger.js";
import { type AuthStatusKind, fromAccountInfo } from "../auth-status.js";
import {
billsClaudeSubscription,
claudeLoginRequiredError,
claudeAuthRefusal,
type ClaudeSubscriptionGuardState,
claudeSubscriptionNotSupportedError,
holdsNonSubscriptionCredential,
refuseClaudeSubscriptionTurn,
shouldHideClaudeAuth,
warnClaudeSubscriptionGuardDegraded,
@@ -79,10 +77,9 @@ export class SubscriptionGuard {
logger: this.logger,
cause: "the CLI reported no account at initialize",
});
} else if (billsClaudeSubscription(account)) {
throw claudeSubscriptionNotSupportedError();
} else if (!holdsNonSubscriptionCredential(account)) {
throw claudeLoginRequiredError();
} else {
const refusal = claudeAuthRefusal(account);
if (refusal) throw refusal;
}
}
this.sessions.set(sessionId, session);