Skip to content

fix: apply managed Claude auth to Agent Teams - #21356

Merged
Jinwoo-H merged 3 commits into
stablyai:mainfrom
Jinwoo-H:jinwoo/sta-7795-bug-claude-teams-uses-api-key-billing-instead-of
Sep 18, 2026
Merged

Jinwoo-H merged 3 commits into
stablyai:mainfrom
Jinwoo-H:jinwoo/sta-7795-bug-claude-teams-uses-api-key-billing-instead-of

Conversation

@Jinwoo-H

Copy link
Copy Markdown
Contributor

ELI5

Claude Agent Teams could ignore Orca's selected Claude subscription when the shell had an ANTHROPIC_API_KEY, because its CLI launcher skipped Orca's managed-auth preparation. Teams now uses the same account selection and auth cleanup as a normal Claude terminal.

What Changed

The agentTeams.prepareLaunch RPC can request Claude auth preparation. The orca claude-teams CLI uses that result to apply the managed CLAUDE_CONFIG_DIR and remove inherited Anthropic auth variables when a managed account is active. System-auth launches continue to preserve user-provided API keys. Native Teams pane setup remains unchanged.

Why

The existing managed-auth policy already defines the correct precedence and protects users from accidentally billing a different API account. Reusing it at the Teams CLI boundary fixes the inconsistency without changing the policy or the Teams/TMUX shim.

Linked Issue

Fixes #21256

Visual Proof

N/A — this changes CLI environment preparation and has no UI layout or visual behavior change.

Testing

  • Focused Teams and CLI tests: 21 passed, 1 skipped

  • pnpm tc:node

  • pnpm tc:cli

  • pnpm run check:code-quality:changed

  • pnpm run generate:rpc-params-catalog -- --write

  • git diff --check

  • I manually tested these changes locally

  • Automated tests added/updated

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable

Notes

The change is host-local and preserves the existing auth policy for managed, system, and remote runtime cases.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • Visual proof is marked N/A because this is not a UI change
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and backwards compatibility considered
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (CI will cover the full suite)

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

The PR's own request-param change breaks an existing CLI test. src/cli/index-worktree-selector-resolution.test.ts:154 asserts the exact agentTeams.prepareLaunch call arguments and now fails because the handler also sends prepareAuth: true. CI will be red until that expectation is updated.

Reviewed changes

  • Managed auth prep reaches Teams: AgentTeamsPrepareLaunch gains an optional prepareAuth; the CLI sends true, deletes the host's envToDelete keys from its inherited env, then merges launch.env (which carries auth.envPatch).
  • Host-side resolution: prepareClaudeAgentTeamsLeaderForHandle awaits prepareForClaudeLaunch, applies applyClaudeEnvPatch(baseEnv, auth.envPatch, { stripAuthEnv }), and returns envToDelete only when stripAuthEnv is set. Native Teams pane setup is untouched.
  • Wiring: prepareClaudeAuth is threaded through main-process-runtime-service.ts; the RPC result passes envToDelete through intact.
  • Tests: src/cli/handlers/core.test.ts covers the CLI delete/merge path.

⚠️ An existing CLI test fails on the new request param

src/cli/index-worktree-selector-resolution.test.ts:154 uses toHaveBeenCalledWith('agentTeams.prepareLaunch', { paneKey, env }), which is a strict deep-equality match, so the extra prepareAuth: true fails it (verified locally — 1 failed / 12 passed across the two files). Add prepareAuth: true to the expected object; the other two claude-teams cases in that file only assert the spawn call and still pass.

Technical details
# Update the prepareLaunch call assertion

## Affected sites
- `src/cli/index-worktree-selector-resolution.test.ts:154` — `toHaveBeenCalledWith` expects `{ paneKey, env }`; the handler now sends `{ paneKey, env, prepareAuth: true }` (`src/cli/handlers/core.ts:79`).

## Required outcome
- The assertion matches the actual `agentTeams.prepareLaunch` params, including `prepareAuth: true`.

## Suggested approach (optional)
- Add `prepareAuth: true` to the expected object. This file is in `config/scripts/ci-shard-timings.json`, so it runs in CI.

ℹ️ Host-side prepareAuth branch has no direct test

The added tests in src/cli/handlers/core.test.ts mock the RPC response, so they cover the CLI's delete/merge but not the host logic that decides envToDelete, applies the patch, and assembles the returned env — the actual fix. A runtime-level test with a fake prepareClaudeAuth returning stripAuthEnv: true / false (mirroring src/main/ipc/pty-spawn-runtime-handle-binding.test.ts) would pin that branch.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

baseEnv?: Record<string, string>
}): Promise<{ env: Record<string, string> }> {
prepareAuth?: boolean
}): Promise<{ env: Record<string, string>; envToDelete?: string[] }> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

envToDelete is now part of the runtime contract, but the pane-key wrapper prepareClaudeAgentTeamsLeader (:114) is still typed Promise<{ env: Record<string, string> }>, so the field is erased before the RPC handler and CLI can see it. Widening the wrapper's return type to { env: Record<string, string>; envToDelete?: string[] } keeps the type honest so a future refactor cannot silently drop the strip list.

Technical details
# Widen the wrapper return type to include envToDelete

## Affected sites
- `src/main/runtime/orca-runtime-resolve-terminal-split-source-authority.ts:114` — `prepareClaudeAgentTeamsLeader` return type is `{ env: Record<string, string> }`; the value it returns from `...ForHandle` also carries `envToDelete`.
- `src/main/runtime/rpc/methods/terminal/terminal-lifecycle-methods.ts:189` — inferred `launch` type therefore omits `envToDelete` even though it is present at runtime.
- `src/cli/handlers/core.ts:78` — the CLI declares its own `envToDelete?` type and relies on the runtime value.

## Required outcome
- The wrapper's declared result type includes the optional `envToDelete` field that `...ForHandle` produces.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ The blocking test failure from the prior review is fixed; no new issues in the delta.

Reviewed changes (since f306b0a)

  • Test expectation updated: src/cli/index-worktree-selector-resolution.test.ts now includes prepareAuth: true in the agentTeams.prepareLaunch call assertion, matching the handler. Verified locally: index-worktree-selector-resolution.test.ts and core.test.ts both pass (13 tests).

The previously noted non-blocking type nit (the prepareClaudeAgentTeamsLeader wrapper still declares its result without envToDelete) is unchanged and remains in the prior review thread; it does not block merge.

Pullfrog  | Fix it ➔ | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@coderabbitai

coderabbitai Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: bb9ad02a-a254-4674-9ce8-d9fa0deb0e6a

📥 Commits

Reviewing files that changed from the base of the PR and between 28c32f3 and feeb947.

📒 Files selected for processing (8)
  • src/cli/handlers/core.test.ts
  • src/cli/handlers/core.ts
  • src/cli/index-worktree-selector-resolution.test.ts
  • src/main/runtime/orca-runtime-resolve-terminal-split-source-authority.ts
  • src/main/runtime/orca-runtime-state-fields.ts
  • src/main/runtime/rpc/methods/terminal/terminal-lifecycle-methods.ts
  • src/main/startup/main-process-runtime-service.ts
  • src/shared/rpc-contract/terminal-unary-params.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 6 remain after this review.


📝 Walkthrough

Walkthrough

The Claude Teams launch contract now accepts prepareAuth. The RPC and runtime forward this option to Claude authentication preparation. The runtime applies authentication environment changes and reports variables to remove. The CLI removes those variables before spawning Claude while preserving other launch variables. Tests cover both removal and preservation of ANTHROPIC_API_KEY.

Priority: ➖ Normal

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to feeb9

The managed Claude authentication environment is passed through to the Teams CLI, while conflicting inherited credentials are removed before launch.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 8 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Issue #21256 requires claude-teams to use the configured Claude subscription and prevent inherited authentication variables from taking precedence. The CLI now sends prepareAuth: true to `agentTea…
Out of Scope Changes check ✅ Passed The changed CLI, RPC contract, runtime wiring, authentication preparation, and tests directly support issue #21256. The changes preserve system-auth behavior when no deletion list is returned and do n…
Title check ✅ Passed The title clearly and concisely describes the main change: applying managed Claude authentication to Agent Teams.
Description check ✅ Passed The description is complete and aligned with the template. It explains the user impact, mechanism, rationale, linked issue, testing, visual proof, compatibility considerations, and checklist status.
  • Fix all pre-merge checks with AI

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Jinwoo-H
Jinwoo-H merged commit 3de7734 into stablyai:main Sep 18, 2026
34 checks passed
ivanvykopal pushed a commit to ivanvykopal/orca that referenced this pull request Sep 20, 2026
* fix: apply managed Claude auth to agent teams

* test: update agent teams auth launch expectation

* refactor: derive agent teams auth deletions
@bluehansl

Copy link
Copy Markdown

@Jinwoo-H — a quick heads-up from a neighboring change, since you clearly know this launch path: prepareClaudeAgentTeamsLeaderForHandle, which this PR extended with prepareAuth, is the same seam where #11742 adds a paneKey pass-through so a team can re-resolve its leader terminal when the stored handle goes stale (the terminal_handle_stale / runtime-timeout on teammate spawn from #11739). I re-synced that branch over your change today, so prepareAuth and paneKey now travel together through that function there.

Mentioning it here because the two changes touch the same function and the survival mechanisms from #14590 deliberately exclude renderer leaf panes — which is what team panes are. If you have a moment to look at #11742, the logic is one 40-line wrapper and the rest is wiring; it's been review-pending since early August and I've been keeping it mergeable weekly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] claude-teams uses API key billing instead of subscription credentials, even after full uninstall

2 participants