Add multi-repo folder workspaces (v1) (#5172)

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Brennan Benson
2026-06-11 02:34:01 -07:00
committed by GitHub
co-authored by Orca
parent 7fbb3003f2
commit 91e354ccb1
99 changed files with 8285 additions and 761 deletions
+1
View File
@@ -382,6 +382,7 @@ export function getDefaultPersistedState(homedir: string): PersistedState {
schemaVersion: SCHEMA_VERSION,
repos: [],
projectGroups: [],
folderWorkspaces: [],
sparsePresetsByRepo: {},
worktreeMeta: {},
worktreeLineageById: {},
@@ -0,0 +1,34 @@
export type FolderWorkspacePathStatusReason =
| 'missing'
| 'not-directory'
| 'unavailable'
| 'ambiguous-connection'
export const FOLDER_WORKSPACE_PATH_STATUS_TTL_MS = 10_000
export type FolderWorkspacePathStatusRequest =
| { scope: 'folder-workspace'; folderWorkspaceId: string }
| { scope: 'project-group'; projectGroupId: string }
export type FolderWorkspacePathStatus = {
path: string
exists: boolean
reason?: FolderWorkspacePathStatusReason
}
export function isConfirmedStaleFolderPathStatus(
status: FolderWorkspacePathStatus | null | undefined
): boolean {
return (
status?.exists === false && (status.reason === 'missing' || status.reason === 'not-directory')
)
}
export function blocksFolderWorkspaceActivation(
status: FolderWorkspacePathStatus | null | undefined
): boolean {
return (
isConfirmedStaleFolderPathStatus(status) ||
(status?.exists === false && status.reason === 'ambiguous-connection')
)
}
@@ -0,0 +1,129 @@
import { describe, expect, it } from 'vitest'
import type { FolderWorkspace } from './types'
import { folderWorkspaceToWorktree } from './folder-workspace-worktree'
function makeFolderWorkspace(overrides: Partial<FolderWorkspace> = {}): FolderWorkspace {
return {
...overrides,
id: overrides.id ?? 'folder-workspace-1',
projectGroupId: overrides.projectGroupId ?? 'group-1',
name: overrides.name ?? 'Refund fix',
folderPath: overrides.folderPath ?? '/workspace/platform',
linkedTask: overrides.linkedTask ?? null,
comment: overrides.comment ?? '',
isArchived: overrides.isArchived ?? false,
isUnread: overrides.isUnread ?? false,
isPinned: overrides.isPinned ?? false,
sortOrder: overrides.sortOrder ?? 1,
manualOrder: overrides.manualOrder,
workspaceStatus: overrides.workspaceStatus,
lastActivityAt: overrides.lastActivityAt ?? 2,
createdAt: overrides.createdAt ?? 3,
updatedAt: overrides.updatedAt ?? 4
}
}
describe('folderWorkspaceToWorktree', () => {
it('projects attached issue tasks without creating linked PR metadata', () => {
const githubIssue = folderWorkspaceToWorktree(
makeFolderWorkspace({
linkedTask: {
provider: 'github',
type: 'issue',
number: 42,
title: 'Refund flow fails',
url: 'https://github.com/acme/app/issues/42'
}
})
)
const gitlabIssue = folderWorkspaceToWorktree(
makeFolderWorkspace({
linkedTask: {
provider: 'gitlab',
type: 'issue',
number: 7,
title: 'Import fails',
url: 'https://gitlab.com/acme/app/-/issues/7'
}
})
)
expect(githubIssue).toMatchObject({
linkedIssue: 42,
linkedPR: null,
linkedGitLabMR: null,
linkedGitLabIssue: null
})
expect(gitlabIssue).toMatchObject({
linkedIssue: null,
linkedPR: null,
linkedGitLabMR: null,
linkedGitLabIssue: 7
})
})
it('projects Linear tasks by identifier', () => {
const worktree = folderWorkspaceToWorktree(
makeFolderWorkspace({
linkedTask: {
provider: 'linear',
type: 'issue',
number: 0,
title: 'Polish folder workspaces',
url: 'https://linear.app/acme/issue/ENG-123',
linearIdentifier: 'ENG-123'
}
})
)
expect(worktree.linkedLinearIssue).toBe('ENG-123')
expect(worktree.linkedPR).toBeNull()
expect(worktree.linkedGitLabMR).toBeNull()
})
it('projects first-message rename state for folder workspace cards', () => {
const worktree = folderWorkspaceToWorktree(
makeFolderWorkspace({
createdWithAgent: 'codex',
pendingFirstAgentMessageRename: true,
firstAgentMessageRenameError: 'No model configured'
})
)
expect(worktree).toMatchObject({
createdWithAgent: 'codex',
pendingFirstAgentMessageRename: true,
firstAgentMessageRenameError: 'No model configured'
})
})
it('keeps review-style tasks attached only to the folder workspace record', () => {
const githubPr = folderWorkspaceToWorktree(
makeFolderWorkspace({
linkedTask: {
provider: 'github',
type: 'pr',
number: 99,
title: 'Feature branch',
url: 'https://github.com/acme/app/pull/99'
}
})
)
const gitlabMr = folderWorkspaceToWorktree(
makeFolderWorkspace({
linkedTask: {
provider: 'gitlab',
type: 'mr',
number: 12,
title: 'Feature branch',
url: 'https://gitlab.com/acme/app/-/merge_requests/12'
}
})
)
expect(githubPr.linkedPR).toBeNull()
expect(githubPr.linkedIssue).toBeNull()
expect(gitlabMr.linkedGitLabMR).toBeNull()
expect(gitlabMr.linkedGitLabIssue).toBeNull()
})
})
+37
View File
@@ -0,0 +1,37 @@
import type { FolderWorkspace, Worktree } from './types'
import { folderWorkspaceKey } from './workspace-scope'
export function folderWorkspaceToWorktree(folderWorkspace: FolderWorkspace): Worktree {
const linkedTask = folderWorkspace.linkedTask
return {
id: folderWorkspaceKey(folderWorkspace.id),
repoId: `folder-workspace:${folderWorkspace.projectGroupId}`,
displayName: folderWorkspace.name,
comment: folderWorkspace.comment,
linkedIssue:
linkedTask?.provider === 'github' && linkedTask.type === 'issue' ? linkedTask.number : null,
linkedPR: null,
linkedLinearIssue:
linkedTask?.provider === 'linear' ? (linkedTask.linearIdentifier ?? null) : null,
linkedGitLabMR: null,
linkedGitLabIssue:
linkedTask?.provider === 'gitlab' && linkedTask.type === 'issue' ? linkedTask.number : null,
isArchived: folderWorkspace.isArchived,
isUnread: folderWorkspace.isUnread,
isPinned: folderWorkspace.isPinned,
sortOrder: folderWorkspace.sortOrder,
manualOrder: folderWorkspace.manualOrder,
lastActivityAt: folderWorkspace.lastActivityAt,
createdAt: folderWorkspace.createdAt,
createdWithAgent: folderWorkspace.createdWithAgent,
pendingFirstAgentMessageRename: folderWorkspace.pendingFirstAgentMessageRename,
firstAgentMessageRenameError: folderWorkspace.firstAgentMessageRenameError,
workspaceStatus: folderWorkspace.workspaceStatus,
path: folderWorkspace.folderPath,
head: '',
branch: '',
isBare: false,
isSparse: false,
isMainWorktree: false
}
}
+144
View File
@@ -0,0 +1,144 @@
import type { FolderWorkspace, FolderWorkspaceLinkedTask, ProjectGroup } from './types'
import { isTuiAgent } from './tui-agent-config'
export function normalizeFolderWorkspaceName(
name: string | null | undefined,
fallback = 'Untitled workspace'
): string {
const trimmed = typeof name === 'string' ? name.trim() : ''
return trimmed.length > 0 ? trimmed : fallback
}
export function normalizeFolderWorkspaceLinkedTask(
value: unknown
): FolderWorkspaceLinkedTask | null {
if (!value || typeof value !== 'object') {
return null
}
const raw = value as Partial<FolderWorkspaceLinkedTask>
if (
raw.provider !== 'github' &&
raw.provider !== 'gitlab' &&
raw.provider !== 'linear' &&
raw.provider !== 'jira'
) {
return null
}
if (raw.type !== 'issue' && raw.type !== 'pr' && raw.type !== 'mr') {
return null
}
if (
typeof raw.number !== 'number' ||
!Number.isFinite(raw.number) ||
typeof raw.title !== 'string' ||
raw.title.trim().length === 0 ||
typeof raw.url !== 'string' ||
raw.url.trim().length === 0
) {
return null
}
return {
provider: raw.provider,
type: raw.type,
number: raw.number,
title: raw.title.trim(),
url: raw.url.trim(),
...(typeof raw.linearIdentifier === 'string' && raw.linearIdentifier.trim().length > 0
? { linearIdentifier: raw.linearIdentifier.trim() }
: {}),
...(typeof raw.jiraIdentifier === 'string' && raw.jiraIdentifier.trim().length > 0
? { jiraIdentifier: raw.jiraIdentifier.trim() }
: {}),
...(typeof raw.repoId === 'string' && raw.repoId.trim().length > 0
? { repoId: raw.repoId.trim() }
: {})
}
}
export function normalizeFolderWorkspaces(
value: unknown,
projectGroups: readonly ProjectGroup[]
): FolderWorkspace[] {
if (!Array.isArray(value)) {
return []
}
const folderGroups = new Map<string, ProjectGroup>()
for (const group of projectGroups) {
if (group.parentPath) {
folderGroups.set(group.id, group)
}
}
const workspaces: FolderWorkspace[] = []
const seen = new Set<string>()
for (const candidate of value) {
if (!candidate || typeof candidate !== 'object') {
continue
}
const raw = candidate as Partial<FolderWorkspace>
if (
typeof raw.id !== 'string' ||
raw.id.trim().length === 0 ||
seen.has(raw.id) ||
typeof raw.projectGroupId !== 'string' ||
!folderGroups.has(raw.projectGroupId)
) {
continue
}
const group = folderGroups.get(raw.projectGroupId)
const folderPath =
typeof raw.folderPath === 'string' && raw.folderPath.trim().length > 0
? raw.folderPath
: group?.parentPath
if (!folderPath) {
continue
}
const now = Date.now()
seen.add(raw.id)
workspaces.push({
id: raw.id,
projectGroupId: raw.projectGroupId,
name: normalizeFolderWorkspaceName(raw.name),
folderPath,
connectionId:
typeof raw.connectionId === 'string'
? raw.connectionId
: raw.connectionId === null
? null
: (group?.connectionId ?? null),
linkedTask: normalizeFolderWorkspaceLinkedTask(raw.linkedTask),
comment: typeof raw.comment === 'string' ? raw.comment : '',
isArchived: raw.isArchived === true,
isUnread: raw.isUnread === true,
isPinned: raw.isPinned === true,
sortOrder:
typeof raw.sortOrder === 'number' && Number.isFinite(raw.sortOrder) ? raw.sortOrder : now,
...(typeof raw.manualOrder === 'number' && Number.isFinite(raw.manualOrder)
? { manualOrder: raw.manualOrder }
: {}),
...(typeof raw.workspaceStatus === 'string' && raw.workspaceStatus.trim().length > 0
? { workspaceStatus: raw.workspaceStatus }
: {}),
...(isTuiAgent(raw.createdWithAgent) ? { createdWithAgent: raw.createdWithAgent } : {}),
...(raw.pendingFirstAgentMessageRename === true
? { pendingFirstAgentMessageRename: true }
: {}),
...(typeof raw.firstAgentMessageRenameError === 'string'
? { firstAgentMessageRenameError: raw.firstAgentMessageRenameError }
: raw.firstAgentMessageRenameError === null
? { firstAgentMessageRenameError: null }
: {}),
lastActivityAt:
typeof raw.lastActivityAt === 'number' && Number.isFinite(raw.lastActivityAt)
? raw.lastActivityAt
: 0,
createdAt:
typeof raw.createdAt === 'number' && Number.isFinite(raw.createdAt) ? raw.createdAt : now,
updatedAt:
typeof raw.updatedAt === 'number' && Number.isFinite(raw.updatedAt) ? raw.updatedAt : now
})
}
return workspaces.sort(
(left, right) => right.sortOrder - left.sortOrder || left.name.localeCompare(right.name)
)
}
+8
View File
@@ -18,6 +18,7 @@ export function normalizeProjectGroupName(name: string, fallback = 'Untitled gro
export function createProjectGroup(input: {
name: string
parentPath?: string | null
connectionId?: string | null
parentGroupId?: string | null
createdFrom: ProjectGroupCreatedFrom
tabOrder: number
@@ -28,6 +29,7 @@ export function createProjectGroup(input: {
id: createProjectGroupId(),
name: normalizeProjectGroupName(input.name),
parentPath: input.parentPath ?? null,
connectionId: input.connectionId ?? null,
parentGroupId: input.parentGroupId ?? null,
createdFrom: input.createdFrom,
tabOrder: input.tabOrder,
@@ -58,6 +60,12 @@ export function normalizeProjectGroups(value: unknown): ProjectGroup[] {
id: raw.id,
name: normalizeProjectGroupName(typeof raw.name === 'string' ? raw.name : ''),
parentPath: typeof raw.parentPath === 'string' ? raw.parentPath : null,
connectionId:
typeof raw.connectionId === 'string'
? raw.connectionId
: raw.connectionId === null
? null
: null,
parentGroupId: typeof raw.parentGroupId === 'string' ? raw.parentGroupId : null,
createdFrom:
raw.createdFrom === 'manual' ||
+47
View File
@@ -136,6 +136,8 @@ export type ProjectGroup = {
id: string
name: string
parentPath: string | null
/** SSH target ID for folder-backed groups imported from a remote root. */
connectionId?: string | null
parentGroupId: string | null
createdFrom: ProjectGroupCreatedFrom
tabOrder: number
@@ -145,6 +147,47 @@ export type ProjectGroup = {
updatedAt: number
}
export type WorkspaceScope =
| { type: 'worktree'; worktreeId: string }
| { type: 'folder'; folderWorkspaceId: string }
export type WorkspaceKey = `worktree:${string}` | `folder:${string}`
export type FolderWorkspace = {
id: string
projectGroupId: string
name: string
folderPath: string
/** SSH target ID for folder workspaces whose folder path lives remotely. */
connectionId?: string | null
linkedTask: FolderWorkspaceLinkedTask | null
comment: string
isArchived: boolean
isUnread: boolean
isPinned: boolean
sortOrder: number
/** User-authored sidebar ordering. Higher values render earlier in Manual sort. */
manualOrder?: number
workspaceStatus?: WorkspaceStatus
createdWithAgent?: TuiAgent
pendingFirstAgentMessageRename?: boolean
firstAgentMessageRenameError?: string | null
lastActivityAt: number
createdAt: number
updatedAt: number
}
export type FolderWorkspaceLinkedTask = {
provider: 'github' | 'gitlab' | 'linear' | 'jira'
type: 'issue' | 'pr' | 'mr'
number: number
title: string
url: string
linearIdentifier?: string
jiraIdentifier?: string
repoId?: string
}
export type NestedRepoScanOptions = {
maxDepth?: number
maxRepos?: number
@@ -699,8 +742,11 @@ export type PersistedOpenFile = {
export type WorkspaceSessionState = {
activeRepoId: string | null
/** Scope-aware active owner for folder workspaces. Legacy worktree UI still reads activeWorktreeId. */
activeWorkspaceKey?: WorkspaceKey | null
activeWorktreeId: string | null
activeTabId: string | null
/** Keys may be legacy raw worktree IDs or canonical WorkspaceKey values. */
tabsByWorktree: Record<string, TerminalTab[]>
terminalLayoutsByTabId: Record<string, TerminalLayoutSnapshot>
/** Worktree IDs that had at least one tab with a live PTY at shutdown.
@@ -2890,6 +2936,7 @@ export type PersistedState = {
schemaVersion: number
repos: Repo[]
projectGroups: ProjectGroup[]
folderWorkspaces: FolderWorkspace[]
/** Sparse-checkout presets keyed by repoId. Empty record on first launch;
* presets are managed from the new-workspace composer and repo settings. */
sparsePresetsByRepo: Record<string, SparsePreset[]>
+31
View File
@@ -0,0 +1,31 @@
import type { WorkspaceKey, WorkspaceScope } from './types'
export function worktreeWorkspaceKey(worktreeId: string): WorkspaceKey {
return `worktree:${worktreeId}`
}
export function folderWorkspaceKey(folderWorkspaceId: string): WorkspaceKey {
return `folder:${folderWorkspaceId}`
}
export function workspaceKeyFromScope(scope: WorkspaceScope): WorkspaceKey {
return scope.type === 'worktree'
? worktreeWorkspaceKey(scope.worktreeId)
: folderWorkspaceKey(scope.folderWorkspaceId)
}
export function parseWorkspaceKey(value: string): WorkspaceScope | null {
if (value.startsWith('worktree:')) {
const worktreeId = value.slice('worktree:'.length)
return worktreeId.length > 0 ? { type: 'worktree', worktreeId } : null
}
if (value.startsWith('folder:')) {
const folderWorkspaceId = value.slice('folder:'.length)
return folderWorkspaceId.length > 0 ? { type: 'folder', folderWorkspaceId } : null
}
return null
}
export function isWorkspaceKey(value: string): value is WorkspaceKey {
return parseWorkspaceKey(value) !== null
}
+6
View File
@@ -14,12 +14,14 @@ import type {
TabGroupLayoutNode,
TerminalPaneLayoutNode,
TuiAgent,
WorkspaceKey,
WorkspaceSessionState
} from './types'
import { isValidTerminalTabId } from './terminal-tab-id'
import { isTuiAgent } from './tui-agent-config'
import { normalizeBrowserHistoryEntries } from './workspace-session-browser-history'
import { normalizeAgentProviderSession, RESUMABLE_TUI_AGENTS } from './agent-session-resume'
import { isWorkspaceKey } from './workspace-scope'
// ─── Terminal pane layout (recursive) ───────────────────────────────
@@ -28,6 +30,9 @@ const terminalTabIdSchema = z
.string()
.min(1)
.refine(isValidTerminalTabId, 'terminal tab id must not contain ":"')
const workspaceKeySchema = z.custom<WorkspaceKey>(
(value) => typeof value === 'string' && isWorkspaceKey(value)
)
// Why: z.lazy + type annotation keeps the recursive inference working without
// forcing zod to resolve the whole tree at definition time.
@@ -262,6 +267,7 @@ const browserHistoryEntriesSchema = z
export const workspaceSessionStateSchema: z.ZodType<WorkspaceSessionState> = z.object({
activeRepoId: z.string().nullable(),
activeWorkspaceKey: workspaceKeySchema.nullable().optional(),
activeWorktreeId: z.string().nullable(),
activeTabId: z.string().nullable(),
tabsByWorktree: z.record(z.string(), z.array(terminalTabSchema)),