fix: expose terminal visual layout and copyable handles (#5845)

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Jinwoo Hong
2026-06-19 15:42:13 -07:00
committed by GitHub
co-authored by Orca
parent c442f3aa00
commit 3afed388fc
11 changed files with 864 additions and 3 deletions
+220 -1
View File
@@ -223,6 +223,7 @@ import type {
RuntimeTerminalFocus,
RuntimeTerminalClose,
RuntimeTerminalListResult,
RuntimeTerminalResolvePane,
RuntimeTerminalState,
RuntimeStatus,
RuntimeSyncWindowGraphResult,
@@ -236,6 +237,11 @@ import type {
RuntimeSpeechSetupState,
RuntimeTerminalShow,
RuntimeTerminalSummary,
RuntimeTerminalVisualGroupNode,
RuntimeTerminalVisualLayout,
RuntimeTerminalVisualLayoutNode,
RuntimeTerminalVisualPaneNode,
RuntimeTerminalVisualTab,
RuntimeSyncedLeaf,
RuntimeSyncedTab,
RuntimeMarkdownReadTabResult,
@@ -7492,13 +7498,209 @@ export class OrcaRuntimeService {
terminals.push(this.buildPtyTerminalSummary(pty, worktreesById))
}
const listedTerminals = terminals.slice(0, limit)
const visualLayouts = this.buildTerminalVisualLayouts(
listedTerminals,
worktreesById,
targetWorktreeId
)
return {
terminals: terminals.slice(0, limit),
terminals: listedTerminals,
...(visualLayouts.length > 0 ? { visualLayouts } : {}),
totalCount: terminals.length,
truncated: terminals.length > limit
}
}
private buildTerminalVisualLayouts(
terminals: RuntimeTerminalSummary[],
worktreesById: Map<string, ResolvedWorktree>,
targetWorktreeId: string | null
): RuntimeTerminalVisualLayout[] {
if (terminals.length === 0) {
return []
}
// Why: the mobile/session snapshot supplies topology, but terminal.list
// must print the same handles in both the flat list and visual tree.
const summariesByLeafKey = new Map(
terminals.map((terminal) => [this.getLeafKey(terminal.tabId, terminal.leafId), terminal])
)
const summariesByWorktree = new Map<string, RuntimeTerminalSummary[]>()
for (const terminal of terminals) {
const existing = summariesByWorktree.get(terminal.worktreeId)
if (existing) {
existing.push(terminal)
} else {
summariesByWorktree.set(terminal.worktreeId, [terminal])
}
}
const snapshots = targetWorktreeId
? [this.mobileSessionTabsByWorktree.get(targetWorktreeId)].filter(
(snapshot): snapshot is RuntimeMobileSessionTabsSnapshot => snapshot !== undefined
)
: [...this.mobileSessionTabsByWorktree.values()]
const layouts: RuntimeTerminalVisualLayout[] = []
for (const snapshot of snapshots) {
const worktreeTerminals = summariesByWorktree.get(snapshot.worktree)
if (!worktreeTerminals || worktreeTerminals.length === 0) {
continue
}
const groups = this.buildTerminalVisualGroups(snapshot, summariesByLeafKey)
if (groups.length === 0) {
continue
}
const groupsById = new Map(
groups
.filter((group): group is RuntimeTerminalVisualGroupNode & { groupId: string } =>
Boolean(group.groupId)
)
.map((group) => [group.groupId, group])
)
const root =
this.buildTerminalVisualGroupLayout(snapshot.tabGroupLayout, groupsById) ?? groups[0]
if (!root) {
continue
}
const worktree = worktreesById.get(snapshot.worktree)
layouts.push({
worktreeId: snapshot.worktree,
worktreePath: worktree?.path ?? worktreeTerminals[0]?.worktreePath ?? '',
root
})
}
return layouts
}
private buildTerminalVisualGroups(
snapshot: RuntimeMobileSessionTabsSnapshot,
summariesByLeafKey: ReadonlyMap<string, RuntimeTerminalSummary>
): RuntimeTerminalVisualGroupNode[] {
const terminalTabs = snapshot.tabs.filter(
(tab): tab is RuntimeMobileSessionTerminalTab => tab.type === 'terminal'
)
if (terminalTabs.length === 0) {
return []
}
const tabsByParentId = new Map<string, RuntimeMobileSessionTerminalTab[]>()
const parentOrder: string[] = []
for (const tab of terminalTabs) {
const existing = tabsByParentId.get(tab.parentTabId)
if (existing) {
existing.push(tab)
} else {
parentOrder.push(tab.parentTabId)
tabsByParentId.set(tab.parentTabId, [tab])
}
}
const groupSources =
snapshot.tabGroups && snapshot.tabGroups.length > 0
? snapshot.tabGroups
: [{ id: null, activeTabId: snapshot.activeTabId, tabOrder: parentOrder }]
return groupSources
.map((group): RuntimeTerminalVisualGroupNode | null => {
const tabs = group.tabOrder
.map((tabId) => {
const surfaces =
tabsByParentId.get(tabId) ?? terminalTabs.filter((tab) => tab.id === tabId)
return this.buildTerminalVisualTab(tabId, surfaces, summariesByLeafKey)
})
.filter((tab): tab is RuntimeTerminalVisualTab => tab !== null)
if (tabs.length === 0) {
return null
}
return {
type: 'group',
groupId: group.id,
activeTabId: group.activeTabId,
tabs
}
})
.filter((group): group is RuntimeTerminalVisualGroupNode => group !== null)
}
private buildTerminalVisualTab(
tabId: string,
surfaces: RuntimeMobileSessionTerminalTab[],
summariesByLeafKey: ReadonlyMap<string, RuntimeTerminalSummary>
): RuntimeTerminalVisualTab | null {
const firstSurface = surfaces[0]
if (!firstSurface) {
return null
}
const parentTabId = firstSurface.parentTabId
const activeLeafId =
firstSurface.parentLayout?.activeLeafId ??
surfaces.find((surface) => surface.isActive)?.leafId ??
firstSurface.leafId
const root = firstSurface.parentLayout?.root ?? {
type: 'leaf' as const,
leafId: firstSurface.leafId
}
const panes = this.buildTerminalVisualPane(root, parentTabId, activeLeafId, summariesByLeafKey)
if (!panes) {
return null
}
return {
tabId: parentTabId || tabId,
title: this.tabs.get(parentTabId)?.title ?? firstSurface.title ?? null,
activeLeafId,
panes
}
}
private buildTerminalVisualPane(
node: TerminalPaneLayoutNode,
tabId: string,
activeLeafId: string | null,
summariesByLeafKey: ReadonlyMap<string, RuntimeTerminalSummary>
): RuntimeTerminalVisualPaneNode | null {
if (node.type === 'leaf') {
const summary = summariesByLeafKey.get(this.getLeafKey(tabId, node.leafId))
if (!summary) {
return null
}
return {
type: 'terminal',
handle: summary.handle,
tabId: summary.tabId,
leafId: summary.leafId,
title: summary.title,
connected: summary.connected,
active: summary.leafId === activeLeafId
}
}
const first = this.buildTerminalVisualPane(node.first, tabId, activeLeafId, summariesByLeafKey)
const second = this.buildTerminalVisualPane(
node.second,
tabId,
activeLeafId,
summariesByLeafKey
)
if (first && second) {
return { type: 'pane-split', direction: node.direction, first, second }
}
return first ?? second
}
private buildTerminalVisualGroupLayout(
node: TabGroupLayoutNode | null | undefined,
groupsById: ReadonlyMap<string, RuntimeTerminalVisualGroupNode>
): RuntimeTerminalVisualLayoutNode | null {
if (!node) {
return null
}
if (node.type === 'leaf') {
return groupsById.get(node.groupId) ?? null
}
const first = this.buildTerminalVisualGroupLayout(node.first, groupsById)
const second = this.buildTerminalVisualGroupLayout(node.second, groupsById)
if (first && second) {
return { type: 'split', direction: node.direction, first, second }
}
return first ?? second
}
// Why: when --terminal is omitted, the CLI auto-resolves to the active
// terminal in the current worktree — matching browser's implicit active tab.
async resolveActiveTerminal(worktreeSelector?: string): Promise<string> {
@@ -7560,6 +7762,23 @@ export class OrcaRuntimeService {
throw new Error('no_active_terminal')
}
resolveTerminalPane(paneKey: string): RuntimeTerminalResolvePane {
// Why: the renderer context menu only knows the stable pane key; main owns
// the runtime terminal handle that agents and CLI commands can address.
const handle = this.getTerminalHandleForPaneKey(paneKey)
if (!handle) {
throw new Error('terminal_not_found')
}
const record = this.handles.get(handle)
const parsed = parsePaneKey(paneKey)
return {
handle,
tabId: record?.tabId ?? parsed?.tabId ?? '',
leafId: record?.leafId ?? parsed?.leafId ?? '',
ptyId: record?.ptyId ?? null
}
}
async showTerminal(handle: string): Promise<RuntimeTerminalShow> {
const pty = this.getLivePtyForHandle(handle)
if (pty) {
+11
View File
@@ -447,6 +447,10 @@ const TerminalResolveActive = z.object({
worktree: OptionalString
})
const TerminalResolvePane = z.object({
paneKey: requiredString('Missing pane key')
})
const TerminalRead = TerminalHandle.extend({
cursor: z
.unknown()
@@ -687,6 +691,13 @@ export const TERMINAL_METHODS: RpcAnyMethod[] = [
handle: await runtime.resolveActiveTerminal(params.worktree)
})
}),
defineMethod({
name: 'terminal.resolvePane',
params: TerminalResolvePane,
handler: async (params, { runtime }) => ({
terminal: runtime.resolveTerminalPane(params.paneKey)
})
}),
defineMethod({
name: 'terminal.show',
params: TerminalHandle,
+236
View File
@@ -2235,6 +2235,242 @@ describe('OrcaRuntimeRpcServer', () => {
await server.stop()
})
it('serves terminal.list with visual split-group and pane nesting', async () => {
const userDataPath = mkdtempSync(join(tmpdir(), 'orca-runtime-rpc-'))
const runtime = new OrcaRuntimeService(makeStore() as never)
const server = new OrcaRuntimeRpcServer({ runtime, userDataPath })
const worktreeId = 'repo-1::/tmp/worktree-a'
const leftLeaf = '11111111-1111-4111-8111-111111111111'
const topLeaf = '22222222-2222-4222-8222-222222222222'
const bottomLeaf = '33333333-3333-4333-8333-333333333333'
runtime.attachWindow(1)
runtime.syncWindowGraph(1, {
tabs: [
{
tabId: 'tab-left',
worktreeId,
title: 'Left',
activeLeafId: leftLeaf,
layout: { type: 'leaf', leafId: leftLeaf }
},
{
tabId: 'tab-right',
worktreeId,
title: 'Right',
activeLeafId: bottomLeaf,
layout: {
type: 'split',
direction: 'vertical',
first: { type: 'leaf', leafId: topLeaf },
second: { type: 'leaf', leafId: bottomLeaf }
}
}
],
leaves: [
{
tabId: 'tab-left',
worktreeId,
leafId: leftLeaf,
paneRuntimeId: 1,
ptyId: 'pty-left',
title: 'Left'
},
{
tabId: 'tab-right',
worktreeId,
leafId: topLeaf,
paneRuntimeId: 1,
ptyId: 'pty-top',
title: 'Right top'
},
{
tabId: 'tab-right',
worktreeId,
leafId: bottomLeaf,
paneRuntimeId: 2,
ptyId: 'pty-bottom',
title: 'Right bottom'
}
],
mobileSessionTabs: [
{
worktree: worktreeId,
publicationEpoch: 'test',
snapshotVersion: 1,
activeGroupId: 'group-right',
activeTabId: `tab-right::${bottomLeaf}`,
activeTabType: 'terminal',
tabGroups: [
{ id: 'group-left', activeTabId: 'tab-left', tabOrder: ['tab-left'] },
{ id: 'group-right', activeTabId: 'tab-right', tabOrder: ['tab-right'] }
],
tabGroupLayout: {
type: 'split',
direction: 'horizontal',
first: { type: 'leaf', groupId: 'group-left' },
second: { type: 'leaf', groupId: 'group-right' }
},
tabs: [
{
type: 'terminal',
id: `tab-left::${leftLeaf}`,
title: 'Left',
parentTabId: 'tab-left',
leafId: leftLeaf,
ptyId: 'pty-left',
parentLayout: {
root: { type: 'leaf', leafId: leftLeaf },
activeLeafId: leftLeaf,
expandedLeafId: null,
ptyIdsByLeafId: { [leftLeaf]: 'pty-left' }
},
isActive: false
},
{
type: 'terminal',
id: `tab-right::${topLeaf}`,
title: 'Right top',
parentTabId: 'tab-right',
leafId: topLeaf,
ptyId: 'pty-top',
parentLayout: {
root: {
type: 'split',
direction: 'vertical',
first: { type: 'leaf', leafId: topLeaf },
second: { type: 'leaf', leafId: bottomLeaf }
},
activeLeafId: bottomLeaf,
expandedLeafId: null,
ptyIdsByLeafId: {
[topLeaf]: 'pty-top',
[bottomLeaf]: 'pty-bottom'
}
},
isActive: false
},
{
type: 'terminal',
id: `tab-right::${bottomLeaf}`,
title: 'Right bottom',
parentTabId: 'tab-right',
leafId: bottomLeaf,
ptyId: 'pty-bottom',
parentLayout: {
root: {
type: 'split',
direction: 'vertical',
first: { type: 'leaf', leafId: topLeaf },
second: { type: 'leaf', leafId: bottomLeaf }
},
activeLeafId: bottomLeaf,
expandedLeafId: null,
ptyIdsByLeafId: {
[topLeaf]: 'pty-top',
[bottomLeaf]: 'pty-bottom'
}
},
isActive: true
}
]
}
]
})
await server.start()
try {
const metadata = readRuntimeMetadata(userDataPath)
const listResponse = await sendRequest(metadata!.transports[0]!.endpoint, {
id: 'req_list_layout',
authToken: metadata!.authToken,
method: 'terminal.list',
params: { worktree: `id:${worktreeId}` }
})
const result = listResponse.result as {
visualLayouts?: unknown[]
terminals: { handle: string; tabId: string; leafId: string }[]
}
const handleByLeaf = new Map(
result.terminals.map((terminal) => [terminal.leafId, terminal.handle])
)
expect(listResponse).toMatchObject({
id: 'req_list_layout',
ok: true
})
expect(result.visualLayouts).toMatchObject([
{
worktreeId,
worktreePath: '/tmp/worktree-a',
root: {
type: 'split',
direction: 'horizontal',
first: {
type: 'group',
groupId: 'group-left',
tabs: [
{
tabId: 'tab-left',
panes: {
type: 'terminal',
handle: handleByLeaf.get(leftLeaf),
leafId: leftLeaf
}
}
]
},
second: {
type: 'group',
groupId: 'group-right',
tabs: [
{
tabId: 'tab-right',
panes: {
type: 'pane-split',
direction: 'vertical',
first: {
type: 'terminal',
handle: handleByLeaf.get(topLeaf),
leafId: topLeaf
},
second: {
type: 'terminal',
handle: handleByLeaf.get(bottomLeaf),
leafId: bottomLeaf,
active: true
}
}
}
]
}
}
}
])
const resolvePaneResponse = await sendRequest(metadata!.transports[0]!.endpoint, {
id: 'req_resolve_pane',
authToken: metadata!.authToken,
method: 'terminal.resolvePane',
params: { paneKey: `tab-right:${bottomLeaf}` }
})
expect(resolvePaneResponse).toMatchObject({
id: 'req_resolve_pane',
ok: true,
result: {
terminal: {
handle: handleByLeaf.get(bottomLeaf),
tabId: 'tab-right',
leafId: bottomLeaf,
ptyId: 'pty-bottom'
}
}
})
} finally {
await server.stop()
}
})
it('mirrors laptop-created remote runtime terminals into phone session tabs over RPC', async () => {
const userDataPath = mkdtempSync(join(tmpdir(), 'orca-runtime-rpc-'))
const runtime = new OrcaRuntimeService(makeStore() as never)