Repository navigation
feat(claude-accounts): launch a Claude agent on a specific managed account with --account #22691
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
fardenghi
wants to merge
16
commits into
stablyai:main
Choose a base branch
from
fardenghi:feat/claude-launch-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.
+3,909
−56
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1cf0f46
feat(claude-accounts): resolve --account selectors to managed Claude …
fardenghi 8a7e4fd
feat(claude-accounts): track Claude PTYs pinned to a non-active account
fardenghi 8f000ae
feat(claude-accounts): prepare pinned Claude launches on a non-active…
fardenghi 51fa681
feat(claude-accounts): guard pinned accounts from host switches, remo…
fardenghi 7f4f1e9
feat(pty): spawn runtime Claude PTYs pinned to a requested account
fardenghi 6ea647b
feat(runtime): carry --account from terminal create to the PTY and re…
fardenghi 100386e
feat(worktree): start a created worktree's Claude agent on a requeste…
fardenghi d739f79
feat(orchestration): run worker-start --account workers on a pinned C…
fardenghi fdb0edc
feat(cli): add --account to worker-start and worktree create for Claude
fardenghi 36a8b8b
test(worktree): keep the --account mapping test out of the shared pro…
fardenghi 05058eb
fix(claude-accounts): claim accounts atomically against pinned launches
fardenghi b9de81a
fix(cli): refuse worker-start --account with --terminal before callin…
fardenghi 36f1060
fix(claude-accounts): revalidate startup pinned seed markers against …
fardenghi 9295b63
fix(claude-accounts): wait for a pinned reservation outside the auth …
fardenghi 629da63
fix(claude-accounts): claim a pinned account before re-authentication…
fardenghi 87c9fe4
fix(claude-accounts): refuse to pin an account old host Claude termin…
fardenghi 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { getOptionalStringFlag } from '../flags' | ||
| import { RuntimeClientError, type RuntimeClient } from '../runtime-client' | ||
| import type { RuntimeStatus } from '../../shared/runtime-types' | ||
| import { CLAUDE_LAUNCH_ACCOUNT_RUNTIME_CAPABILITY } from '../../shared/protocol-version' | ||
|
|
||
| /** | ||
| * `--account <id|email>`: the managed Claude account a launch runs on, resolved by the host. | ||
| * | ||
| * Why the capability check: an older host strips the unknown field and would quietly start the | ||
| * agent on its active account — exactly what the caller asked not to happen. | ||
| */ | ||
| export async function readClaudeLaunchAccountFlag( | ||
| flags: Map<string, string | boolean>, | ||
| client: RuntimeClient, | ||
| agent: string | undefined | ||
| ): Promise<string | undefined> { | ||
| if (!flags.has('account')) { | ||
| return undefined | ||
| } | ||
| const account = getOptionalStringFlag(flags, 'account')?.trim() | ||
| if (!account) { | ||
| throw new RuntimeClientError( | ||
| 'invalid_argument', | ||
| '--account requires a Claude account id or email.' | ||
| ) | ||
| } | ||
| if (agent !== 'claude') { | ||
| throw new RuntimeClientError('invalid_argument', '--account requires --agent claude.') | ||
| } | ||
| const status = await client.call<RuntimeStatus>('status.get') | ||
| if (!status.result.capabilities?.includes(CLAUDE_LAUNCH_ACCOUNT_RUNTIME_CAPABILITY)) { | ||
| throw new RuntimeClientError( | ||
| 'incompatible_runtime', | ||
| 'The connected Orca runtime does not support --account for Claude launches. Update or restart Orca and try again.' | ||
| ) | ||
| } | ||
| return account | ||
| } |
102 changes: 102 additions & 0 deletions
102
src/cli/handlers/orchestration-worker-account-cli.test.ts
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,102 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const callMock = vi.fn() | ||
|
|
||
| vi.mock('../format', () => ({ printResult: vi.fn() })) | ||
| vi.mock('../selectors', () => ({ getTerminalHandle: vi.fn() })) | ||
|
|
||
| import { ORCHESTRATION_HANDLERS } from './orchestration' | ||
| import { CLAUDE_LAUNCH_ACCOUNT_RUNTIME_CAPABILITY } from '../../shared/protocol-version' | ||
|
|
||
| describe('orchestration worker-start --account', () => { | ||
| beforeEach(() => { | ||
| callMock.mockReset() | ||
| process.exitCode = undefined | ||
| }) | ||
|
|
||
| const invokeWorkerStart = (flags: Map<string, string | boolean>) => | ||
| ORCHESTRATION_HANDLERS['orchestration worker-start']({ | ||
| flags, | ||
| client: { call: callMock }, | ||
| cwd: '/tmp/repo', | ||
| json: true | ||
| } as never) | ||
|
|
||
| it('forwards --account to a host that resolves Claude launch accounts', async () => { | ||
| callMock | ||
| .mockResolvedValueOnce({ | ||
| result: { capabilities: [CLAUDE_LAUNCH_ACCOUNT_RUNTIME_CAPABILITY] } | ||
| }) | ||
| .mockResolvedValueOnce({ | ||
| result: { runId: 'run_1', taskId: 'task_1', dispatchId: 'ctx_1', state: 'ready' } | ||
| }) | ||
|
|
||
| await invokeWorkerStart( | ||
| new Map<string, string | boolean>([ | ||
| ['task', 'task_1'], | ||
| ['agent', 'claude'], | ||
| ['account', 'pinned@example.com'], | ||
| ['from', 'term_coord'] | ||
| ]) | ||
| ) | ||
|
|
||
| expect(callMock).toHaveBeenNthCalledWith(1, 'status.get') | ||
| expect(callMock).toHaveBeenNthCalledWith( | ||
| 2, | ||
| 'orchestration.workerStart', | ||
| expect.objectContaining({ agent: 'claude', account: 'pinned@example.com' }) | ||
| ) | ||
| }) | ||
|
|
||
| it('fails before worker-start when --account would be stripped or misapplied', async () => { | ||
| callMock.mockResolvedValueOnce({ result: { capabilities: [] } }) | ||
| const flags = (agent: string) => | ||
| new Map<string, string | boolean>([ | ||
| ['task', 'task_1'], | ||
| ['agent', agent], | ||
| ['account', 'acct-b'], | ||
| ['from', 'term_coord'] | ||
| ]) | ||
|
|
||
| await expect(invokeWorkerStart(flags('claude'))).rejects.toMatchObject({ | ||
| code: 'incompatible_runtime' | ||
| }) | ||
| await expect(invokeWorkerStart(flags('codex'))).rejects.toThrow( | ||
| '--account requires --agent claude.' | ||
| ) | ||
| expect(callMock).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('never sends an account field when --account is absent', async () => { | ||
| callMock.mockResolvedValue({ | ||
| result: { runId: 'run_1', taskId: 'task_1', dispatchId: 'ctx_1', state: 'ready' } | ||
| }) | ||
| await invokeWorkerStart( | ||
| new Map<string, string | boolean>([ | ||
| ['task', 'task_1'], | ||
| ['agent', 'claude'], | ||
| ['from', 'term_coord'] | ||
| ]) | ||
| ) | ||
| expect(callMock).toHaveBeenCalledTimes(1) | ||
| expect(callMock.mock.calls[0]?.[1]).not.toHaveProperty('account') | ||
| }) | ||
|
|
||
| it('refuses --account with --terminal before calling any host', async () => { | ||
| await expect( | ||
| invokeWorkerStart( | ||
| new Map<string, string | boolean>([ | ||
| ['task', 'task_1'], | ||
| ['terminal', 'term_worker'], | ||
| ['account', 'acct-b'], | ||
| ['from', 'term_coord'] | ||
| ]) | ||
| ) | ||
| ).rejects.toMatchObject({ | ||
| code: 'invalid_argument', | ||
| message: | ||
| '--account cannot be combined with --terminal; the reused terminal keeps its own account.' | ||
| }) | ||
| expect(callMock).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
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,103 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { CLAUDE_LAUNCH_ACCOUNT_RUNTIME_CAPABILITY } from '../shared/protocol-version' | ||
|
|
||
| const { | ||
| callMock, | ||
| runtimeClientConstructorMock, | ||
| serveOrcaAppMock, | ||
| getDefaultUserDataPathMock, | ||
| addEnvironmentFromPairingCodeMock, | ||
| listEnvironmentsMock, | ||
| spawnMock | ||
| } = vi.hoisted(() => ({ | ||
| callMock: vi.fn(), | ||
| runtimeClientConstructorMock: vi.fn(), | ||
| serveOrcaAppMock: vi.fn(), | ||
| getDefaultUserDataPathMock: vi.fn(() => '/tmp/orca-user-data'), | ||
| addEnvironmentFromPairingCodeMock: vi.fn(), | ||
| listEnvironmentsMock: vi.fn(), | ||
| spawnMock: vi.fn() | ||
| })) | ||
|
|
||
| vi.mock('./runtime-client', async () => { | ||
| const { createRuntimeClientModuleMock } = await import('./index-test-harness.js') | ||
| return createRuntimeClientModuleMock({ | ||
| callMock, | ||
| runtimeClientConstructorMock, | ||
| serveOrcaAppMock, | ||
| getDefaultUserDataPathMock | ||
| }) | ||
| }) | ||
|
|
||
| vi.mock('./runtime/environments', () => ({ | ||
| addEnvironmentFromPairingCode: addEnvironmentFromPairingCodeMock, | ||
| listEnvironments: listEnvironmentsMock, | ||
| removeEnvironment: vi.fn(), | ||
| resolveEnvironment: vi.fn() | ||
| })) | ||
|
|
||
| vi.mock('child_process', async () => { | ||
| const { createChildProcessModuleMock } = await import('./index-test-harness.js') | ||
| return createChildProcessModuleMock(spawnMock) | ||
| }) | ||
|
|
||
| import { main } from './index' | ||
| import { buildWorktree, okFixture, queueFixtures, worktreeListFixture } from './test-fixtures' | ||
| import { useWorktreeAwarenessEnvironment } from './index-test-harness' | ||
|
|
||
| describe('orca worktree create --account', () => { | ||
| useWorktreeAwarenessEnvironment({ | ||
| callMock, | ||
| serveOrcaAppMock, | ||
| getDefaultUserDataPathMock, | ||
| addEnvironmentFromPairingCodeMock, | ||
| listEnvironmentsMock, | ||
| spawnMock | ||
| }) | ||
|
|
||
| const createArgs = ['worktree', 'create', '--repo', 'id:repo-1', '--name', 'pinned', '--json'] | ||
|
|
||
| it('sends the account selector to a host that advertises support', async () => { | ||
| queueFixtures( | ||
| callMock, | ||
| okFixture('req_status', { capabilities: [CLAUDE_LAUNCH_ACCOUNT_RUNTIME_CAPABILITY] }), | ||
| worktreeListFixture([buildWorktree('/tmp/repo', 'main', 'abc', 'repo-1')]), | ||
| okFixture('req_create', { | ||
| worktree: buildWorktree('/tmp/repo/pinned', 'pinned', 'abc', 'repo-1') | ||
| }) | ||
| ) | ||
| vi.spyOn(console, 'log').mockImplementation(() => {}) | ||
|
|
||
| await main([...createArgs, '--agent', 'claude', '--account', 'pinned@example.com'], '/tmp/repo') | ||
|
|
||
| expect(callMock).toHaveBeenNthCalledWith(1, 'status.get') | ||
| expect(callMock).toHaveBeenNthCalledWith( | ||
| 3, | ||
| 'worktree.create', | ||
| expect.objectContaining({ | ||
| startupAgent: 'claude', | ||
| startupPrompt: '', | ||
| startupClaudeAccount: 'pinned@example.com' | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| it('refuses an older host instead of launching on its active account', async () => { | ||
| queueFixtures(callMock, okFixture('req_status', { capabilities: [] })) | ||
| vi.spyOn(console, 'error').mockImplementation(() => {}) | ||
|
|
||
| await main([...createArgs, '--agent', 'claude', '--account', 'acct-b'], '/tmp/repo') | ||
|
|
||
| expect(process.exitCode).toBe(1) | ||
| expect(callMock).not.toHaveBeenCalledWith('worktree.create', expect.anything()) | ||
| }) | ||
|
|
||
| it('requires --agent claude', async () => { | ||
| vi.spyOn(console, 'error').mockImplementation(() => {}) | ||
|
|
||
| await main([...createArgs, '--agent', 'codex', '--account', 'acct-b'], '/tmp/repo') | ||
|
|
||
| expect(process.exitCode).toBe(1) | ||
| expect(callMock).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
pinned_claude_accountdowngrade fires for any--account, including a selector that names the account already active on the host. The PR's resolution rules say an active-account selector runs the normal path unchanged, but this decision runs before resolution and only sees the raw selector, so a structured-default user gets a terminal worker (and the “non-active account” detail) for--account <active>. The same raw-selector assumption makesbuildRuntimeAgentTeamsLaunchPlanforce in-process teammates for the active account too.Technical details