Add multi-repo folder workspaces (v1) (#5172)
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -71,6 +71,7 @@ import type {
|
||||
LinearWorkspaceSelection,
|
||||
NestedRepoScanResult,
|
||||
ProjectGroup,
|
||||
FolderWorkspace,
|
||||
ProjectGroupImportMode,
|
||||
ProjectGroupImportResult,
|
||||
MemorySnapshot,
|
||||
@@ -120,6 +121,11 @@ import {
|
||||
isPathInsideOrEqual,
|
||||
normalizeRuntimePathForComparison
|
||||
} from '../../shared/cross-platform-path'
|
||||
import { folderWorkspaceKey, parseWorkspaceKey } from '../../shared/workspace-scope'
|
||||
import type {
|
||||
FolderWorkspacePathStatus,
|
||||
FolderWorkspacePathStatusRequest
|
||||
} from '../../shared/folder-workspace-path-status'
|
||||
import {
|
||||
buildKnownOrcaWorkspaceLayouts,
|
||||
isLegacyRepoForExternalWorktreeVisibility,
|
||||
@@ -487,6 +493,12 @@ import { killAllProcessesForWorktree } from './worktree-teardown'
|
||||
import { MOBILE_SUBSCRIBE_SCROLLBACK_ROWS } from './scrollback-limits'
|
||||
import type { IFilesystemProvider, IPtyProvider } from '../providers/types'
|
||||
import { getSshFilesystemProvider } from '../providers/ssh-filesystem-dispatch'
|
||||
import {
|
||||
assertFolderWorkspacePathUsable,
|
||||
getFolderWorkspacePathStatus,
|
||||
getFolderWorkspacePathStatusForPath,
|
||||
inferFolderWorkspacePathConnection
|
||||
} from '../project-groups/folder-workspace-path-status'
|
||||
import { getSshGitProvider, requireSshGitProvider } from '../providers/ssh-git-dispatch'
|
||||
import { detectRepoIconAndUpstream } from '../repo-icon-autodetect'
|
||||
import { githubAvatarIcon } from '../../shared/repo-icon'
|
||||
@@ -540,6 +552,10 @@ type RuntimeStore = {
|
||||
updateProjectGroup?: Store['updateProjectGroup']
|
||||
deleteProjectGroup?: Store['deleteProjectGroup']
|
||||
moveProjectToGroup?: Store['moveProjectToGroup']
|
||||
getFolderWorkspaces?: Store['getFolderWorkspaces']
|
||||
createFolderWorkspace?: Store['createFolderWorkspace']
|
||||
updateFolderWorkspace?: Store['updateFolderWorkspace']
|
||||
removeFolderWorkspace?: Store['removeFolderWorkspace']
|
||||
removeProject?: Store['removeProject']
|
||||
reorderRepos?: Store['reorderRepos']
|
||||
getAllWorktreeMeta: Store['getAllWorktreeMeta']
|
||||
@@ -1194,6 +1210,13 @@ type ResolvedWorktree = Worktree & {
|
||||
git: GitWorktreeInfo
|
||||
}
|
||||
|
||||
type TerminalWorkspaceLaunchScope = {
|
||||
id: string
|
||||
path: string
|
||||
connectionId: string | null
|
||||
folderWorkspace: FolderWorkspace | null
|
||||
}
|
||||
|
||||
type WorktreeLineageInput = {
|
||||
parentWorktree?: string
|
||||
cwdParentWorktree?: string
|
||||
@@ -6487,9 +6510,14 @@ export class OrcaRuntimeService {
|
||||
return this.store?.getProjectGroups?.() ?? []
|
||||
}
|
||||
|
||||
listFolderWorkspaces(): FolderWorkspace[] {
|
||||
return this.store?.getFolderWorkspaces?.() ?? []
|
||||
}
|
||||
|
||||
async createProjectGroup(input: {
|
||||
name: string
|
||||
parentPath?: string | null
|
||||
connectionId?: string | null
|
||||
parentGroupId?: string | null
|
||||
createdFrom?: ProjectGroup['createdFrom']
|
||||
}): Promise<ProjectGroup> {
|
||||
@@ -6499,6 +6527,7 @@ export class OrcaRuntimeService {
|
||||
const group = this.store.createProjectGroup({
|
||||
name: input.name,
|
||||
parentPath: input.parentPath ?? null,
|
||||
connectionId: input.connectionId ?? null,
|
||||
parentGroupId: input.parentGroupId ?? null,
|
||||
createdFrom: input.createdFrom ?? 'manual'
|
||||
})
|
||||
@@ -6548,6 +6577,118 @@ export class OrcaRuntimeService {
|
||||
return moved
|
||||
}
|
||||
|
||||
async createFolderWorkspace(input: {
|
||||
projectGroupId: string
|
||||
name?: string
|
||||
folderPath?: string | null
|
||||
connectionId?: string | null
|
||||
linkedTask?: FolderWorkspace['linkedTask']
|
||||
createdWithAgent?: FolderWorkspace['createdWithAgent']
|
||||
pendingFirstAgentMessageRename?: boolean
|
||||
}): Promise<FolderWorkspace> {
|
||||
if (!this.store?.createFolderWorkspace) {
|
||||
throw new Error('runtime_unavailable')
|
||||
}
|
||||
const projectGroups = this.store.getProjectGroups?.() ?? []
|
||||
const group = projectGroups.find((entry) => entry.id === input.projectGroupId)
|
||||
const folderPath =
|
||||
typeof input.folderPath === 'string' && input.folderPath.trim().length > 0
|
||||
? input.folderPath
|
||||
: group?.parentPath
|
||||
if (!group || !folderPath) {
|
||||
throw new Error('folder_workspace_project_group_not_found')
|
||||
}
|
||||
const status = await getFolderWorkspacePathStatusForPath(
|
||||
{
|
||||
folderPath,
|
||||
projectGroupId: group.id,
|
||||
connectionId: input.connectionId ?? group.connectionId ?? null,
|
||||
projectGroups,
|
||||
repos: this.store.getRepos()
|
||||
},
|
||||
{ getSshFilesystemProvider }
|
||||
)
|
||||
assertFolderWorkspacePathUsable(status)
|
||||
const workspace = this.store.createFolderWorkspace(input)
|
||||
this.notifyReposChanged()
|
||||
return workspace
|
||||
}
|
||||
|
||||
async getFolderWorkspacePathStatus(
|
||||
request: FolderWorkspacePathStatusRequest
|
||||
): Promise<FolderWorkspacePathStatus> {
|
||||
if (!this.store) {
|
||||
throw new Error('runtime_unavailable')
|
||||
}
|
||||
return getFolderWorkspacePathStatus(this.store, request, { getSshFilesystemProvider })
|
||||
}
|
||||
|
||||
async updateFolderWorkspace(
|
||||
folderWorkspaceId: string,
|
||||
updates: Partial<
|
||||
Pick<
|
||||
FolderWorkspace,
|
||||
| 'name'
|
||||
| 'folderPath'
|
||||
| 'linkedTask'
|
||||
| 'comment'
|
||||
| 'isArchived'
|
||||
| 'isUnread'
|
||||
| 'isPinned'
|
||||
| 'sortOrder'
|
||||
| 'manualOrder'
|
||||
| 'workspaceStatus'
|
||||
| 'createdWithAgent'
|
||||
| 'pendingFirstAgentMessageRename'
|
||||
| 'firstAgentMessageRenameError'
|
||||
| 'lastActivityAt'
|
||||
>
|
||||
>
|
||||
): Promise<FolderWorkspace | null> {
|
||||
if (!this.store?.updateFolderWorkspace) {
|
||||
throw new Error('runtime_unavailable')
|
||||
}
|
||||
if (typeof updates.folderPath === 'string' && updates.folderPath.trim().length > 0) {
|
||||
const workspace = this.store
|
||||
.getFolderWorkspaces?.()
|
||||
.find((entry) => entry.id === folderWorkspaceId)
|
||||
if (!workspace) {
|
||||
return null
|
||||
}
|
||||
const projectGroups = this.store.getProjectGroups?.() ?? []
|
||||
const status = await getFolderWorkspacePathStatusForPath(
|
||||
{
|
||||
folderPath: updates.folderPath,
|
||||
projectGroupId: workspace.projectGroupId,
|
||||
connectionId:
|
||||
workspace.connectionId ??
|
||||
projectGroups.find((entry) => entry.id === workspace.projectGroupId)?.connectionId ??
|
||||
null,
|
||||
projectGroups,
|
||||
repos: this.store.getRepos()
|
||||
},
|
||||
{ getSshFilesystemProvider }
|
||||
)
|
||||
assertFolderWorkspacePathUsable(status)
|
||||
}
|
||||
const updated = this.store.updateFolderWorkspace(folderWorkspaceId, updates)
|
||||
if (updated) {
|
||||
this.notifyReposChanged()
|
||||
}
|
||||
return updated
|
||||
}
|
||||
|
||||
async deleteFolderWorkspace(folderWorkspaceId: string): Promise<{ deleted: boolean }> {
|
||||
if (!this.store?.removeFolderWorkspace) {
|
||||
throw new Error('runtime_unavailable')
|
||||
}
|
||||
const deleted = this.store.removeFolderWorkspace(folderWorkspaceId)
|
||||
if (deleted) {
|
||||
this.notifyReposChanged()
|
||||
}
|
||||
return { deleted }
|
||||
}
|
||||
|
||||
async scanNestedRepos(path: string): Promise<NestedRepoScanResult> {
|
||||
if (!isAbsolute(path)) {
|
||||
throw new Error('Project path must be an absolute path')
|
||||
@@ -6605,6 +6746,8 @@ export class OrcaRuntimeService {
|
||||
parentPath: args.parentPath,
|
||||
groupName: args.groupName,
|
||||
mode: args.mode,
|
||||
connectionId: null,
|
||||
repoPaths: selection.selectedPaths,
|
||||
createGroup: (input) => this.store!.createProjectGroup!(input)
|
||||
})
|
||||
const results: ProjectGroupImportResult['projects'] = selection.rejectedPaths.map(
|
||||
@@ -11147,8 +11290,7 @@ export class OrcaRuntimeService {
|
||||
if (!this.ptyController?.spawn) {
|
||||
throw new Error('runtime_unavailable')
|
||||
}
|
||||
const worktree = await this.resolveWorktreeSelector(worktreeSelector)
|
||||
const repo = this.store?.getRepo(worktree.repoId)
|
||||
const workspace = await this.resolveTerminalWorkspaceLaunchScope(worktreeSelector)
|
||||
const preAllocatedHandle = this.createPreAllocatedTerminalHandle()
|
||||
// Why: mint tabId in main before spawn so paneKey is known at PTY env
|
||||
// build time. Hook-based agent status (Claude/Codex/Cursor/Gemini) keys
|
||||
@@ -11184,23 +11326,23 @@ export class OrcaRuntimeService {
|
||||
shimBin
|
||||
}).env
|
||||
})
|
||||
const env = {
|
||||
...baseEnv,
|
||||
...agentTeamsPlan?.env,
|
||||
ORCA_PANE_KEY: paneKey,
|
||||
ORCA_TAB_ID: tabId,
|
||||
ORCA_WORKTREE_ID: worktree.id
|
||||
}
|
||||
const env = this.buildTerminalWorkspaceEnv(
|
||||
workspace,
|
||||
baseEnv,
|
||||
paneKey,
|
||||
tabId,
|
||||
agentTeamsPlan?.env
|
||||
)
|
||||
const result = await this.ptyController.spawn({
|
||||
cols: 120,
|
||||
rows: 40,
|
||||
cwd: worktree.path,
|
||||
cwd: workspace.path,
|
||||
command: agentTeamsPlan?.command ?? opts.command,
|
||||
env,
|
||||
envToDelete: agentTeamsPlan?.envToDelete,
|
||||
telemetry: opts.telemetry,
|
||||
connectionId: repo?.connectionId ?? null,
|
||||
worktreeId: worktree.id,
|
||||
connectionId: workspace.connectionId,
|
||||
worktreeId: workspace.id,
|
||||
preAllocatedHandle,
|
||||
tabId,
|
||||
leafId,
|
||||
@@ -11208,7 +11350,7 @@ export class OrcaRuntimeService {
|
||||
...(opts.persistHostSessionBinding ? { persistHostSessionBinding: true } : {})
|
||||
})
|
||||
this.registerPreAllocatedHandleForPty(result.id, preAllocatedHandle)
|
||||
this.registerPty(result.id, worktree.id, repo?.connectionId ?? null)
|
||||
this.registerPty(result.id, workspace.id, workspace.connectionId)
|
||||
const pty = this.getOrCreatePtyWorktreeRecord(result.id)
|
||||
if (pty) {
|
||||
if (opts.title) {
|
||||
@@ -11225,7 +11367,7 @@ export class OrcaRuntimeService {
|
||||
}
|
||||
const handle = pty ? this.issuePtyHandle(pty) : preAllocatedHandle
|
||||
if (pty) {
|
||||
this.publishPtyBackedMobileSessionTerminal(worktree.id, pty, {
|
||||
this.publishPtyBackedMobileSessionTerminal(workspace.id, pty, {
|
||||
tabId,
|
||||
leafId,
|
||||
title: opts.title ?? null,
|
||||
@@ -11239,7 +11381,7 @@ export class OrcaRuntimeService {
|
||||
// failing here must not strand a live process without returning a handle.
|
||||
// Pass the pre-minted tabId so the renderer adopts under the same id
|
||||
// already baked into the PTY env — keeps paneKey hook attribution intact.
|
||||
await this.notifier.revealTerminalSession(worktree.id, {
|
||||
await this.notifier.revealTerminalSession(workspace.id, {
|
||||
ptyId: result.id,
|
||||
title: opts.title ?? null,
|
||||
activate: opts.activate === true,
|
||||
@@ -11251,7 +11393,7 @@ export class OrcaRuntimeService {
|
||||
console.warn(`[terminal-create] failed to create inactive tab for ${result.id}:`, err)
|
||||
}
|
||||
}
|
||||
return { handle, worktreeId: worktree.id, title: opts.title ?? null, surface }
|
||||
return { handle, worktreeId: workspace.id, title: opts.title ?? null, surface }
|
||||
}
|
||||
|
||||
this.assertGraphReady()
|
||||
@@ -11259,7 +11401,7 @@ export class OrcaRuntimeService {
|
||||
// Why: mirrors browserTabCreate — when no worktree is specified, pass
|
||||
// undefined so the renderer uses its current active worktree.
|
||||
const worktreeId = worktreeSelector
|
||||
? (await this.resolveWorktreeSelector(worktreeSelector)).id
|
||||
? (await this.resolveTerminalWorkspaceLaunchScope(worktreeSelector)).id
|
||||
: undefined
|
||||
const requestId = randomUUID()
|
||||
|
||||
@@ -11814,29 +11956,23 @@ export class OrcaRuntimeService {
|
||||
throw new Error('terminal_handle_stale')
|
||||
}
|
||||
const direction = opts.direction ?? 'horizontal'
|
||||
const worktree = await this.resolveWorktreeSelector(`id:${pty.worktreeId}`)
|
||||
const repo = this.store?.getRepo(worktree.repoId)
|
||||
const workspace = await this.resolveTerminalWorkspaceLaunchScope(`id:${pty.worktreeId}`)
|
||||
const leafId = randomUUID()
|
||||
const preAllocatedHandle = this.createPreAllocatedTerminalHandle()
|
||||
const paneKey = makePaneKey(parentTabId, leafId)
|
||||
const result = await this.ptyController.spawn({
|
||||
cols: 120,
|
||||
rows: 40,
|
||||
cwd: worktree.path,
|
||||
cwd: workspace.path,
|
||||
command: opts.command,
|
||||
env: {
|
||||
...opts.env,
|
||||
ORCA_PANE_KEY: paneKey,
|
||||
ORCA_TAB_ID: parentTabId,
|
||||
ORCA_WORKTREE_ID: worktree.id
|
||||
},
|
||||
env: this.buildTerminalWorkspaceEnv(workspace, opts.env ?? {}, paneKey, parentTabId),
|
||||
envToDelete: opts.envToDelete,
|
||||
connectionId: repo?.connectionId ?? null,
|
||||
worktreeId: worktree.id,
|
||||
connectionId: workspace.connectionId,
|
||||
worktreeId: workspace.id,
|
||||
preAllocatedHandle
|
||||
})
|
||||
this.registerPreAllocatedHandleForPty(result.id, preAllocatedHandle)
|
||||
this.registerPty(result.id, worktree.id, repo?.connectionId ?? null)
|
||||
this.registerPty(result.id, workspace.id, workspace.connectionId)
|
||||
const createdPty = this.getOrCreatePtyWorktreeRecord(result.id)
|
||||
if (createdPty) {
|
||||
createdPty.tabId = parentTabId
|
||||
@@ -11844,7 +11980,7 @@ export class OrcaRuntimeService {
|
||||
}
|
||||
|
||||
try {
|
||||
await this.notifier?.revealTerminalSession?.(worktree.id, {
|
||||
await this.notifier?.revealTerminalSession?.(workspace.id, {
|
||||
ptyId: result.id,
|
||||
title: null,
|
||||
activate: opts.activate !== false,
|
||||
@@ -11859,7 +11995,7 @@ export class OrcaRuntimeService {
|
||||
throw error
|
||||
}
|
||||
if (createdPty) {
|
||||
this.publishPtyBackedMobileSessionTerminal(worktree.id, createdPty, {
|
||||
this.publishPtyBackedMobileSessionTerminal(workspace.id, createdPty, {
|
||||
tabId: parentTabId,
|
||||
leafId,
|
||||
title: null,
|
||||
@@ -12063,6 +12199,101 @@ export class OrcaRuntimeService {
|
||||
}
|
||||
}
|
||||
|
||||
private resolveFolderWorkspaceConnectionId(workspace: FolderWorkspace): string | null {
|
||||
const repos = this.store?.getRepos() ?? []
|
||||
const projectGroups = this.store?.getProjectGroups?.() ?? []
|
||||
const connection = inferFolderWorkspacePathConnection({
|
||||
folderPath: workspace.folderPath,
|
||||
projectGroupId: workspace.projectGroupId,
|
||||
connectionId: workspace.connectionId ?? null,
|
||||
projectGroups,
|
||||
repos
|
||||
})
|
||||
if (connection.kind === 'ambiguous') {
|
||||
// Why: a single PTY can only be spawned on one runtime target; mixed
|
||||
// child repo connections need an explicit V2 routing decision.
|
||||
throw new Error('folder_workspace_connection_ambiguous')
|
||||
}
|
||||
return connection.kind === 'ssh' ? connection.connectionId : null
|
||||
}
|
||||
|
||||
private async resolveFolderWorkspaceLaunchScope(
|
||||
selector: string
|
||||
): Promise<TerminalWorkspaceLaunchScope | null> {
|
||||
const workspaceSelector = selector.startsWith('id:') ? selector.slice(3) : selector
|
||||
const parsed = parseWorkspaceKey(workspaceSelector)
|
||||
if (parsed?.type !== 'folder') {
|
||||
return null
|
||||
}
|
||||
const workspace = this.store
|
||||
?.getFolderWorkspaces?.()
|
||||
.find((entry) => entry.id === parsed.folderWorkspaceId)
|
||||
if (!workspace) {
|
||||
throw new Error('selector_not_found')
|
||||
}
|
||||
if (!this.store) {
|
||||
throw new Error('runtime_unavailable')
|
||||
}
|
||||
const status = await getFolderWorkspacePathStatus(
|
||||
this.store,
|
||||
{ scope: 'folder-workspace', folderWorkspaceId: workspace.id },
|
||||
{ getSshFilesystemProvider }
|
||||
)
|
||||
assertFolderWorkspacePathUsable(status)
|
||||
return {
|
||||
id: folderWorkspaceKey(workspace.id),
|
||||
path: workspace.folderPath,
|
||||
connectionId: this.resolveFolderWorkspaceConnectionId(workspace),
|
||||
folderWorkspace: workspace
|
||||
}
|
||||
}
|
||||
|
||||
private async resolveTerminalWorkspaceLaunchScope(
|
||||
selector: string
|
||||
): Promise<TerminalWorkspaceLaunchScope> {
|
||||
const folderScope = await this.resolveFolderWorkspaceLaunchScope(selector)
|
||||
if (folderScope) {
|
||||
return folderScope
|
||||
}
|
||||
|
||||
const workspaceSelector = selector.startsWith('id:') ? selector.slice(3) : selector
|
||||
const parsed = parseWorkspaceKey(workspaceSelector)
|
||||
const worktreeSelector = parsed?.type === 'worktree' ? `id:${parsed.worktreeId}` : selector
|
||||
const worktree = await this.resolveWorktreeSelector(worktreeSelector)
|
||||
const repo = this.store?.getRepo(worktree.repoId) ?? null
|
||||
return {
|
||||
id: worktree.id,
|
||||
path: worktree.path,
|
||||
connectionId: repo?.connectionId ?? null,
|
||||
folderWorkspace: null
|
||||
}
|
||||
}
|
||||
|
||||
private buildTerminalWorkspaceEnv(
|
||||
scope: TerminalWorkspaceLaunchScope,
|
||||
baseEnv: Record<string, string>,
|
||||
paneKey: string,
|
||||
tabId: string,
|
||||
agentTeamsEnv?: Record<string, string>
|
||||
): Record<string, string> {
|
||||
const env = {
|
||||
...baseEnv,
|
||||
...agentTeamsEnv,
|
||||
ORCA_PANE_KEY: paneKey,
|
||||
ORCA_TAB_ID: tabId,
|
||||
ORCA_WORKTREE_ID: scope.id
|
||||
}
|
||||
if (!scope.folderWorkspace) {
|
||||
return env
|
||||
}
|
||||
return {
|
||||
...env,
|
||||
ORCA_WORKSPACE_ID: scope.id,
|
||||
ORCA_PROJECT_GROUP_ID: scope.folderWorkspace.projectGroupId,
|
||||
ORCA_WORKSPACE_ROOT: scope.folderWorkspace.folderPath
|
||||
}
|
||||
}
|
||||
|
||||
private async resolveWorktreeSelector(selector: string): Promise<ResolvedWorktree> {
|
||||
const worktrees = await this.listResolvedWorktrees()
|
||||
let candidates: ResolvedWorktree[]
|
||||
@@ -12687,6 +12918,11 @@ export class OrcaRuntimeService {
|
||||
this.notifyWorktreesChanged(repoId)
|
||||
}
|
||||
|
||||
notifyFolderWorkspaceChanged(): void {
|
||||
this.invalidateResolvedWorktreeCache()
|
||||
this.notifyReposChanged()
|
||||
}
|
||||
|
||||
private recordPtyWorktree(
|
||||
ptyId: string,
|
||||
worktreeId: string,
|
||||
|
||||
Reference in New Issue
Block a user