From 180b99d5791d4b4f4e89aa3f62d8632c06c013f2 Mon Sep 17 00:00:00 2001 From: Saurabh Sungh Date: Sat, 5 Dec 2026 18:25:31 +0530 Subject: [PATCH] fix(claude-local): ignore host env in ACP billing when managed connection is used (#22422) ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The claude_local adapter's ACP lane classifies cost events for the ledger via resolveClaudeAcpBillingIdentity > - When an agent runs with a managed AI subscription connection and the host process happens to have ANTHROPIC_API_KEY in its environment, resolveClaudeAcpBillingIdentity incorrectly inspected process.env because considerHostEnv only checked whether target.kind was remote > - The probe (testClaudeAcpEnvironment) and execution paths in the same package already check !targetIsRemote && !config.managedAiConnection, and the CLI lane only inspects the effective run environment > - This pull request updates considerHostEnv in resolveClaudeAcpBillingIdentity to also check !config.managedAiConnection, preventing host environment leaks from misclassifying subscription runs as metered API spend > - The benefit is that subscription runs on managed AI connections correctly file as subscription_included at $0 rather than metered API spend even if the host environment has an Anthropic key ## Linked Issues or Issue Description Fixes #22388 Related: #20955 ## What Changed - Updated `resolveClaudeAcpBillingIdentity` in `packages/adapters/claude-local/src/server/acp.ts` to gate `considerHostEnv` on `!config.managedAiConnection`, aligning it with `testClaudeAcpEnvironment` and `testEnvironment`. - Added unit tests in `packages/adapters/claude-local/src/server/acp.test.ts` verifying that `resolveClaudeAcpBillingIdentity` ignores host environment when `managedAiConnection` is configured, while preserving `api` billing when `managedAiConnection` is used with `api_key` and config credentials. ## Verification - Ran `vitest run packages/adapters/claude-local/src/server/acp.test.ts` (43 passed). - Built package via `pnpm --filter @paperclipai/adapter-claude-local build` (0 TypeScript errors). - Ran all ACP suites in `claude-local` (`acp.auth.test.ts`, `acp.quota.test.ts`, `acp.test.ts`) (97 passed). ## Risks - Low risk: Only affects ACP billing identity resolution when `managedAiConnection` is set. Unmanaged local runs and remote execution targets maintain their existing behavior. ## Model Used - Model: Gemini 2.5 Flash - Reasoning: Chain-of-thought analysis, autonomous codebase investigation, test-driven reproduction, and verification ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [ ] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [ ] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: commitperclip[bot] <284812584+commitperclip[bot]@users.noreply.github.com> --- .../claude-local/src/server/acp.test.ts | 32 +++++++++++++++++++ .../adapters/claude-local/src/server/acp.ts | 5 +-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/packages/adapters/claude-local/src/server/acp.test.ts b/packages/adapters/claude-local/src/server/acp.test.ts index f966b4f4d9f..78fafbb93c5 100644 --- a/packages/adapters/claude-local/src/server/acp.test.ts +++ b/packages/adapters/claude-local/src/server/acp.test.ts @@ -1919,4 +1919,36 @@ describe("resolveClaudeAcpBillingIdentity", () => { } as never).billingType, ).toBe("subscription"); }); + + it("ignores host env when managedAiConnection is configured", () => { + process.env.ANTHROPIC_API_KEY = "sk-ant-host-only"; + expect( + resolveClaudeAcpBillingIdentity({ + config: { + managedAiConnection: { provider: "anthropic", method: "subscription" }, + }, + }), + ).toEqual({ + provider: "anthropic", + biller: "anthropic", + billingType: "subscription", + }); + }); + + it("preserves api billing for managed api_key connections", () => { + process.env.ANTHROPIC_API_KEY = "sk-ant-host-only"; + expect( + resolveClaudeAcpBillingIdentity({ + config: { + managedAiConnection: { provider: "anthropic", method: "api_key" }, + env: { ANTHROPIC_API_KEY: "sk-ant-managed-key" }, + }, + }), + ).toEqual({ + provider: "anthropic", + biller: "anthropic", + billingType: "metered_api", + preferRequestedModel: true, + }); + }); }); diff --git a/packages/adapters/claude-local/src/server/acp.ts b/packages/adapters/claude-local/src/server/acp.ts index 6b13c1e96f8..40fbb286e31 100644 --- a/packages/adapters/claude-local/src/server/acp.ts +++ b/packages/adapters/claude-local/src/server/acp.ts @@ -182,12 +182,13 @@ export function resolveClaudeAcpBillingIdentity( ): { provider: string; biller: string; billingType: AdapterBillingType; preferRequestedModel?: true } { const connection = resolveManagedApiKeyBilling(ctx.config); if (connection) return connection; - const envConfig = parseObject(parseObject(ctx.config).env); + const config = parseObject(ctx.config); + const envConfig = parseObject(config.env); const target = readAdapterExecutionTarget({ executionTarget: ctx.executionTarget, legacyRemoteExecution: ctx.executionTransport?.remoteExecution, }); - const considerHostEnv = target?.kind !== "remote"; + const considerHostEnv = target?.kind !== "remote" && !config.managedAiConnection; const readEnvValue = (key: string): string => { const fromConfig = envConfig[key]; if (typeof fromConfig === "string" && fromConfig.trim()) return fromConfig.trim();