Repository navigation
fix(cli): inherit the caller's Claude account for child agents #38461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thefrikidude
wants to merge
7
commits into
stablyai:main
Choose a base branch
from
thefrikidude:codex/38052-inherit-claude-account
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+851
−47
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8ed310e
fix(cli): inherit the caller's Claude account for child agents
thefrikidude 0106070
Omit an unset CLAUDE_CONFIG_DIR on the inherited System route
thefrikidude 48c6d31
fix(cli): clear a later CLAUDE_CONFIG_DIR on the inherited System route
thefrikidude cad5e57
Merge remote-tracking branch 'upstream/main' into codex/38052-inherit…
thefrikidude 65fa6e8
Merge remote-tracking branch 'upstream/main' into mc/pr38461
thefrikidude bbcf18f
fix(cli): correct claude-launch-inheritance import path after upstrea…
thefrikidude a81a817
fix(cli): let an inherited account launch after a failed refresh
thefrikidude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| import type { AgentLaunchAccountReceipt } from '../../shared/agent-launch/contracts/agent-launch-intent' | ||
| import { AsyncLocalStorage } from 'node:async_hooks' | ||
| import { basename, dirname } from 'node:path' | ||
| import { normalizeRuntimePathForComparison } from '../../shared/cross-platform-path' | ||
| import type { OrchestrationCompatibilityEvidence } from '../../shared/orchestration-compatibility-evidence' | ||
| import type { RpcContext } from '../runtime/rpc/core' | ||
| import { getClaudeProfileRouter } from './claude-profile-installed-router' | ||
| import { claudeProfileLaunchPointer } from './claude-profile-paths' | ||
| import type { ClaudeProfileRouter } from './claude-profile-router' | ||
| import type { ClaudeRuntimeAuthPreparation } from './runtime-auth/runtime-auth-types' | ||
|
|
||
| type ClaudeLaunchAccount = { | ||
| router: ClaudeProfileRouter | ||
| preparation: ClaudeRuntimeAuthPreparation | ||
| receipt: AgentLaunchAccountReceipt | ||
| used: boolean | ||
| } | ||
|
|
||
| // Bind concurrent launches without changing the UI account selection. | ||
| const launches = new AsyncLocalStorage<ClaudeLaunchAccount>() | ||
|
|
||
| export function inheritedClaudeLaunchPreparation( | ||
| router: Pick<ClaudeProfileRouter, 'selectedProfile'> | ||
| ): ClaudeRuntimeAuthPreparation | undefined { | ||
| const launch = launches.getStore() | ||
| if (launch?.router !== router) { | ||
| return undefined | ||
| } | ||
| launch.used = true | ||
| return launch.preparation | ||
| } | ||
|
|
||
| export function claudeLaunchAccountReceipt(): AgentLaunchAccountReceipt | undefined { | ||
| const launch = launches.getStore() | ||
| return launch?.used ? launch.receipt : undefined | ||
| } | ||
|
|
||
| function accountForHome(router: ClaudeProfileRouter, home: string): string | null | undefined { | ||
| const same = (other: string) => | ||
| normalizeRuntimePathForComparison(home) === normalizeRuntimePathForComparison(other) | ||
| if (!home || same(router.systemDefaultHome())) { | ||
| return null | ||
| } | ||
| const id = basename(dirname(home)) | ||
| const account = router.savedAccount(id) | ||
| if (account && account.managedAuthRuntime !== 'wsl' && same(router.accountHome(id))) { | ||
| return id | ||
| } | ||
| return undefined | ||
| } | ||
|
|
||
| async function callerAccount( | ||
| router: ClaudeProfileRouter, | ||
| context: RpcContext, | ||
| evidence: OrchestrationCompatibilityEvidence | undefined | ||
| ): Promise<string | null | undefined> { | ||
| if (!evidence?.terminalHandle || evidence.host || typeof evidence.claudeConfigDir !== 'string') { | ||
| return undefined | ||
| } | ||
| try { | ||
| const terminal = await context.runtime.showTerminal(evidence.terminalHandle) | ||
| if ( | ||
| !terminal.connected || | ||
| terminal.agentIdentity !== 'claude' || | ||
| !(await context.runtime.isTerminalRunningAgent(evidence.terminalHandle)) || | ||
| (evidence.paneKey && evidence.paneKey !== `${terminal.tabId}:${terminal.leafId}`) | ||
| ) { | ||
| return undefined | ||
| } | ||
| return accountForHome(router, evidence.claudeConfigDir) | ||
| } catch { | ||
| return undefined | ||
| } | ||
| } | ||
|
|
||
| export async function withInheritedClaudeLaunchAccount<T>( | ||
| context: RpcContext, | ||
| method: string, | ||
| params: unknown, | ||
| evidence: OrchestrationCompatibilityEvidence | undefined, | ||
| run: () => T | Promise<T> | ||
| ): Promise<T> { | ||
| if ( | ||
| context.caller?.kind !== 'local-cli' || | ||
| ![ | ||
| 'agent.launch', | ||
| 'agent.launchReplay', | ||
| 'orchestration.workerStart', | ||
| 'worktree.create' | ||
| ].includes(method) || | ||
| typeof params !== 'object' || | ||
| params === null || | ||
| !('agent' in params) || | ||
| params.agent !== 'claude' || | ||
| ('accountEnv' in params && params.accountEnv !== undefined) || | ||
| ('reuseTerminal' in params && params.reuseTerminal !== undefined) || | ||
| ('on' in params && params.on !== undefined) | ||
| ) { | ||
| return run() | ||
| } | ||
| const router = getClaudeProfileRouter() | ||
| if (!router) { | ||
| return run() | ||
| } | ||
| if ('target' in params && typeof params.target === 'object' && params.target !== null) { | ||
| const target = params.target | ||
| if ( | ||
| 'kind' in target && | ||
| target.kind === 'existing' && | ||
| 'worktree' in target && | ||
| typeof target.worktree === 'string' | ||
| ) { | ||
| const workspace = await context.runtime.showTerminalWorkspaceLaunchScope(target.worktree) | ||
| if (workspace.connectionId) { | ||
| return run() | ||
| } | ||
| } | ||
| if ( | ||
| 'create' in target && | ||
| typeof target.create === 'object' && | ||
| target.create !== null && | ||
| 'repoHostId' in target.create && | ||
| target.create.repoHostId && | ||
| target.create.repoHostId !== 'local' | ||
| ) { | ||
| return run() | ||
| } | ||
| } | ||
| const inherited = await callerAccount(router, context, evidence) | ||
| const selected = inherited === undefined ? router.preparation() : undefined | ||
| const id = | ||
| inherited ?? | ||
| (inherited === null | ||
| ? null | ||
| : selected?.provenance.startsWith('profile:') | ||
| ? selected.provenance.slice('profile:'.length) | ||
| : null) | ||
| const home = id === null ? router.systemDefaultHome() : await router.prepareAccount(id) | ||
| const pointer = claudeProfileLaunchPointer(router.pointerPath, id, home) | ||
| const launch: ClaudeLaunchAccount = { | ||
| router, | ||
| used: false, | ||
| receipt: { | ||
| id, | ||
| email: id === null ? null : (router.savedAccount(id)?.email ?? null), | ||
| source: inherited === undefined ? 'selected' : 'caller' | ||
| }, | ||
| preparation: { | ||
| configDir: home, | ||
| runtime: 'host', | ||
| wslDistro: null, | ||
| wslLinuxConfigDir: null, | ||
| provenance: id === null ? 'system' : `profile:${id}`, | ||
| envPatch: { | ||
| ORCA_CLAUDE_PROFILE_POINTER: pointer, | ||
| // '' clears a CLAUDE_CONFIG_DIR a later Orca setting would add to a System-default child. | ||
| CLAUDE_CONFIG_DIR: id === null ? (router.userConfigDir() ?? '') : home, | ||
| ORCA_CLAUDE_INJECTED_CONFIG_DIR: id === null ? '' : home | ||
| } | ||
| } | ||
| } | ||
| return launches.run(launch, async () => { | ||
| const result = await run() | ||
| return launch.used && typeof result === 'object' && result !== null && !('account' in result) | ||
| ? { ...result, account: launch.receipt } | ||
| : result | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.