Files
orca/src/shared/workspace-scope.ts
T
Jinwoo Hong a55de38395 feat(cli): set and rm folder workspaces through the folder workspace RPCs (#39670)
* feat(cli): set and rm folder workspaces through the folder workspace RPCs

worktree set and rm on folder:<id> (or a path:/name: selector that matches a
folder) now call folderWorkspace.update and folderWorkspace.delete after the
runtime confirms the folder id is unique. Flags with no folder meaning are
refused before any change, a missing method reports incompatible_runtime, and
worktree activate refuses folder workspaces as not supported yet.

* test: read mocked method names without a cast

* fix(runtime): refuse shared folder ids inside folder update and delete

The CLI's show preflight could not make the id-only folder RPCs safe: an older
show could select one of two same-id rows, and a twin added during teardown was
deleted too. folderWorkspace.update/delete now take requireUniqueId, which
refuses a shared id before any side effect, and delete removes only the row it
checked. Hosts advertise folder-workspace.unique-id-mutation.v1; the CLI
requires it and reports incompatible_runtime otherwise.

* fix(runtime): refuse a folder delete when a twin appears during teardown

Two rows that share a folder id share every record keyed by it, so delete
can't keep one row's sessions while removing the other. After the awaited
terminal teardown, delete checks the id again before any cleanup or removal
and refuses with the duplicate-id error; only the stopped terminals remain as
an effect. Removal by row identity is no longer needed and is dropped.

* fix(cli): give only the repair step for a duplicated folder id

The generic ambiguity recovery told agents to pass folder:<id>, which can't
name one of two folders sharing that id.
2026-11-08 00:34:44 -05:00

65 lines
2.3 KiB
TypeScript

import {
getWorktreeIdFromHostIdentity,
isWorktreeHostIdentity
} from './worktree/host-qualified-identity'
export type WorkspaceScope =
| { type: 'worktree'; worktreeId: string }
| { type: 'folder'; folderWorkspaceId: string }
export type WorkspaceKey = `worktree:${string}` | `folder:${string}`
export function worktreeWorkspaceKey(worktreeId: string): WorkspaceKey {
return `worktree:${worktreeId}`
}
export function folderWorkspaceKey(folderWorkspaceId: string): WorkspaceKey {
return `folder:${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
}
/** The folder workspace id a `folder:<id>` or `id:folder:<id>` selector names. */
export function folderIdOfSelector(selector: string): string | undefined {
const parsed = parseWorkspaceKey(selector.startsWith('id:') ? selector.slice(3) : selector)
return parsed?.type === 'folder' ? parsed.folderWorkspaceId : undefined
}
/** Strip WorkspaceKey or host-qualified visit-recency scope (ssh:target|repo::path); retain already-bare workspace IDs. */
export function normalizeWorkspaceSessionKeyToWorkspaceId(value: string): string {
if (isWorktreeHostIdentity(value)) {
return getWorktreeIdFromHostIdentity(value)
}
const scope = parseWorkspaceKey(value)
return scope?.type === 'worktree' ? scope.worktreeId : value
}
export function isWorkspaceKey(value: string): value is WorkspaceKey {
return parseWorkspaceKey(value) !== null
}
// Folder workspaces use the scoped active key; older worktree paths still use activeWorktreeId.
export function getActiveSidebarWorkspaceId(
activeWorkspaceKey: string | null,
activeWorktreeId: string | null
): string | null {
const scope = activeWorkspaceKey ? parseWorkspaceKey(activeWorkspaceKey) : null
if (scope?.type === 'folder') {
return folderWorkspaceKey(scope.folderWorkspaceId)
}
if (scope?.type === 'worktree') {
return scope.worktreeId
}
return activeWorktreeId
}