Files
orca/src/cli/projects/project.ts
T
Neil 6018fca4de refactor: group shared settings, profiles and execution hosts (#43047)
* refactor: place shared settings profiles and host contracts with owners

* fix: retire stale cleanup validation contacts

* refactor: consolidate imports from concrete owners

* refactor: bind new card fixture to profile defaults owner

* style: format rebased concrete host import

* refactor: consolidate project header color owner imports

* fix: load released phone readers after folder moves

* fix: preserve released chat readers and current owner imports
2026-11-16 04:03:33 -08:00

300 lines
11 KiB
TypeScript

import type {
Project,
ProjectHostSetup,
ProjectHostSetupCloneArgs,
ProjectHostSetupCreateArgs,
ProjectHostSetupCreateResult,
ProjectHostSetupDeleteResult,
ProjectHostSetupExistingFolderArgs,
ProjectHostSetupResult,
ProjectHostSetupUpdateArgs,
ProjectHostSetupUpdateResult
} from '../../shared/project/project-types'
import {
getSshTargetIdForExecutionHost,
type ExecutionHostId
} from '../../shared/execution-host/execution-host'
import type { RuntimeStatus } from '../../shared/remote-runtime/session-graph/runtime-session-contracts'
import { PROJECT_HOST_SETUP_HOST_TARGET_RUNTIME_CAPABILITY } from '../../shared/remote-runtime/protocol/worktree-runtime-capabilities'
import type { CommandHandler, HandlerContext } from '../dispatch/dispatch'
import {
formatProjectHostSetupCreateResult,
formatProjectHostSetupDeleteResult,
formatProjectHostSetupList,
formatProjectHostSetupResult,
formatProjectHostSetupUpdateResult,
formatProjectList
} from './project-format'
import { printResult } from '../output/command-result-output'
import {
hostFilterMatchesHostId,
parseHostFlag,
resolveHostFlagTarget
} from '../hosts/execution-host-flag'
import {
getOptionalRepoKind,
getOptionalStringFlag,
getRequiredStringFlag
} from '../arguments/flags'
import { findProjectSetups, resolveProjectIdSelector } from './project-selector'
import { resolveRepoPathArgument } from './repo-path-arguments'
import { RuntimeClientError } from '../runtime/connection/types'
import type { RuntimeRpcSuccess } from '../../shared/remote-runtime/rpc/runtime-rpc-envelope'
async function callProjectHostSetup<TResult>(
client: HandlerContext['client'],
method: string,
params?: unknown
): Promise<RuntimeRpcSuccess<TResult>> {
try {
// Preserve absent params; explicitly passing undefined changes the request shape.
return params === undefined
? await client.call<TResult>(method)
: await client.call<TResult>(method, params)
} catch (error) {
if (error instanceof RuntimeClientError && error.code === 'method_not_found') {
throw new RuntimeClientError(
'incompatible_runtime',
'This Orca server does not support project host setup yet. Update Orca on the server and try again.'
)
}
throw error
}
}
async function getResolvedHostId(
flags: Map<string, string | boolean>,
client: HandlerContext['client']
): Promise<ExecutionHostId> {
const host = await resolveHostFlagTarget(flags, client)
if (!host) {
throw new RuntimeClientError('invalid_argument', 'Missing required --host')
}
return host.id
}
// The runtime rejects SSH setup operations regardless of target existence; preserve that refusal.
async function getRequiredHostId(
flags: Map<string, string | boolean>,
client: HandlerContext['client']
): Promise<ExecutionHostId> {
const host = parseHostFlag(flags)
if (!host) {
throw new RuntimeClientError('invalid_argument', 'Missing required --host')
}
if (host.kind !== 'alias') {
return host.id
}
// Bare aliases must resolve to an id before they can be forwarded.
const resolved = await resolveHostFlagTarget(flags, client)
if (!resolved) {
throw new RuntimeClientError('invalid_argument', 'Missing required --host')
}
return resolved.id
}
// Older hosts drop hostId and pick the first setup twin; refuse unsupported host targeting.
async function getOptionalSetupHostId(
flags: Map<string, string | boolean>,
client: HandlerContext['client']
): Promise<ExecutionHostId | undefined> {
if (!flags.has('host')) {
return undefined
}
const status = await client.call<RuntimeStatus>('status.get')
if (!status.result.capabilities?.includes(PROJECT_HOST_SETUP_HOST_TARGET_RUNTIME_CAPABILITY)) {
throw new RuntimeClientError(
'incompatible_runtime',
'This Orca server cannot pick a project setup by host yet. Update Orca on the server and try again.'
)
}
// Unresolved, like the setup paths: a removed SSH target's stale setup must stay deletable.
return await getRequiredHostId(flags, client)
}
export const PROJECT_HANDLERS: Record<string, CommandHandler> = {
'project list': async ({ client, json }) => {
const result = await client.call<{ projects: Project[] }>('project.list')
printResult(result, json, formatProjectList)
},
'project setups': async ({ flags, client, json }) => {
const projectFilter = getOptionalStringFlag(flags, 'project')
const hostFilter = await resolveHostFlagTarget(flags, client)
const result = await callProjectHostSetup<{ setups: ProjectHostSetup[] }>(
client,
'projectHostSetup.list'
)
const projectSetups =
projectFilter === undefined
? result.result.setups
: await findProjectSetups(client, result.result.setups, projectFilter)
const setups = projectSetups.filter(
(setup) => hostFilter === undefined || hostFilterMatchesHostId(hostFilter, setup.hostId)
)
printResult({ ...result, result: { setups } }, json, formatProjectHostSetupList)
},
'project setup-existing-folder': async ({ flags, client, cwd, json }) => {
const rawPath = getRequiredStringFlag(flags, 'path')
const hostId = await getRequiredHostId(flags, client)
// SSH paths belong to the target filesystem, even when the runtime connection itself is local.
const pathIsOffClient = client.isRemote || getSshTargetIdForExecutionHost(hostId) !== null
const args: ProjectHostSetupExistingFolderArgs = {
hostId,
path: resolveRepoPathArgument(rawPath, cwd, pathIsOffClient, 'Remote project setup'),
kind: getOptionalRepoKind(flags),
displayName: getOptionalStringFlag(flags, 'display-name'),
// Last, so a bad local flag fails before the name lookup reaches the server.
projectId: await resolveProjectIdSelector(client, getRequiredStringFlag(flags, 'project'))
}
const result = await callProjectHostSetup<{ result: ProjectHostSetupResult }>(
client,
'projectHostSetup.setupExistingFolder',
args
)
printResult(result, json, formatProjectHostSetupResult)
},
'project setup-clone': async ({ flags, client, cwd, json }) => {
const rawDestination = getRequiredStringFlag(flags, 'destination')
const args: ProjectHostSetupCloneArgs = {
hostId: await getRequiredHostId(flags, client),
url: getRequiredStringFlag(flags, 'url'),
destination: resolveRepoPathArgument(
rawDestination,
cwd,
client.isRemote,
'Project setup clone'
),
folderName: getOptionalStringFlag(flags, 'folder-name'),
displayName: getOptionalStringFlag(flags, 'display-name'),
projectId: await resolveProjectIdSelector(client, getRequiredStringFlag(flags, 'project'))
}
const result = await callProjectHostSetup<{ result: ProjectHostSetupResult }>(
client,
'projectHostSetup.clone',
args
)
printResult(result, json, formatProjectHostSetupResult)
},
'project setup-create': async ({ flags, client, cwd, json }) => {
const path = getOptionalStringFlag(flags, 'path')
const args: ProjectHostSetupCreateArgs = {
// Resolve SSH targets to reject nonexistent hosts; runtime metadata may precede provisioning.
hostId: await getResolvedHostId(flags, client),
setupId: getOptionalStringFlag(flags, 'setup-id'),
path:
path === undefined
? undefined
: resolveRepoPathArgument(path, cwd, client.isRemote, 'Project setup create'),
kind: getOptionalRepoKind(flags),
displayName: getOptionalStringFlag(flags, 'display-name'),
worktreeBasePath: getOptionalStringFlag(flags, 'worktree-base-path'),
gitUsername: getOptionalStringFlag(flags, 'git-username'),
setupState: getOptionalSetupState(flags),
setupMethod: getOptionalIndependentSetupMethod(flags),
projectId: await resolveProjectIdSelector(client, getRequiredStringFlag(flags, 'project'))
}
const result = await callProjectHostSetup<{ result: ProjectHostSetupCreateResult }>(
client,
'projectHostSetup.create',
args
)
printResult(result, json, formatProjectHostSetupCreateResult)
},
'project setup-update': async ({ flags, client, cwd, json }) => {
const path = getOptionalStringFlag(flags, 'path')
const args: ProjectHostSetupUpdateArgs = {
setupId: getRequiredStringFlag(flags, 'setup'),
hostId: await getOptionalSetupHostId(flags, client),
updates: {
displayName: getOptionalStringFlag(flags, 'display-name'),
path:
path === undefined
? undefined
: resolveRepoPathArgument(path, cwd, client.isRemote, 'Project setup update'),
worktreeBasePath: getOptionalStringFlag(flags, 'worktree-base-path'),
gitUsername: getOptionalStringFlag(flags, 'git-username'),
kind: getOptionalRepoKind(flags),
setupState: getOptionalSetupState(flags),
setupMethod: getOptionalSetupMethod(flags)
}
}
const result = await callProjectHostSetup<{ result: ProjectHostSetupUpdateResult }>(
client,
'projectHostSetup.update',
args
)
printResult(result, json, formatProjectHostSetupUpdateResult)
},
'project setup-delete': async ({ flags, client, json }) => {
const result = await callProjectHostSetup<{ result: ProjectHostSetupDeleteResult }>(
client,
'projectHostSetup.delete',
{
setupId: getRequiredStringFlag(flags, 'setup'),
hostId: await getOptionalSetupHostId(flags, client),
force: flags.get('force') === true
}
)
printResult(result, json, formatProjectHostSetupDeleteResult)
}
}
function getOptionalSetupState(
flags: Map<string, string | boolean>
): ProjectHostSetupUpdateArgs['updates']['setupState'] {
const state = getOptionalStringFlag(flags, 'state')
if (state === undefined) {
return undefined
}
if (
state === 'ready' ||
state === 'not-set-up' ||
state === 'setting-up' ||
state === 'error' ||
state === 'unsupported'
) {
return state
}
throw new RuntimeClientError(
'invalid_argument',
'--state must be ready, not-set-up, setting-up, error, or unsupported'
)
}
function getOptionalIndependentSetupMethod(
flags: Map<string, string | boolean>
): ProjectHostSetupCreateArgs['setupMethod'] {
const method = getOptionalStringFlag(flags, 'method')
if (method === undefined) {
return undefined
}
if (method === 'imported-existing-folder' || method === 'cloned' || method === 'provisioned') {
return method
}
throw new RuntimeClientError(
'invalid_argument',
'--method must be imported-existing-folder, cloned, or provisioned'
)
}
function getOptionalSetupMethod(
flags: Map<string, string | boolean>
): ProjectHostSetupUpdateArgs['updates']['setupMethod'] {
const method = getOptionalStringFlag(flags, 'method')
if (method === undefined) {
return undefined
}
if (
method === 'legacy-repo' ||
method === 'imported-existing-folder' ||
method === 'cloned' ||
method === 'provisioned'
) {
return method
}
throw new RuntimeClientError(
'invalid_argument',
'--method must be legacy-repo, imported-existing-folder, cloned, or provisioned'
)
}