Fix remote runtime terminal sharing with mobile (#4901)

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Jinwoo Hong
2026-06-08 14:26:07 -07:00
committed by GitHub
co-authored by Orca
parent 90ac74d9b9
commit f5efdd5945
14 changed files with 1133 additions and 119 deletions
@@ -1107,7 +1107,7 @@ describe('createRemoteRuntimePtyTransport', () => {
expect(onData).toHaveBeenCalledWith(' world', expect.objectContaining({ seq: 4 }))
})
it('forwards input and cleanup through runtime RPC', async () => {
it('forwards input over the stream and disconnects without closing shared remote sessions', async () => {
vi.useFakeTimers()
try {
const { createRemoteRuntimePtyTransport } = await import('./remote-runtime-pty-transport')
@@ -1135,12 +1135,11 @@ describe('createRemoteRuntimePtyTransport', () => {
transport.disconnect()
expect(unsubscribeFn).toHaveBeenCalled()
expect(runtimeCall).toHaveBeenCalledWith({
selector: 'env-1',
method: 'terminal.close',
params: { terminal: 'term-remote' },
timeoutMs: 15_000
})
expect(runtimeCall).not.toHaveBeenCalledWith(
expect.objectContaining({
method: 'terminal.close'
})
)
} finally {
vi.useRealTimers()
}
@@ -236,6 +236,27 @@ describe('createRemoteRuntimePtyTransport', () => {
)
})
it('detaches laptop-created remote runtime terminals without closing the server session', async () => {
const { createRemoteRuntimePtyTransport } = await import('./remote-runtime-pty-transport')
const transport = createRemoteRuntimePtyTransport('env-1', {
worktreeId: 'wt-1',
tabId: 'tab-1',
leafId: 'pane:1'
})
await transport.connect({ url: '', callbacks: {} })
await vi.waitFor(() => expect(subscriptionSendBinary).toHaveBeenCalled())
runtimeCall.mockClear()
transport.destroy?.()
expect(runtimeCall).not.toHaveBeenCalledWith(
expect.objectContaining({
method: 'terminal.close'
})
)
})
it('retires stale host-owned terminal handles without surfacing pane errors', async () => {
const { createRemoteRuntimePtyTransport } = await import('./remote-runtime-pty-transport')
const onError = vi.fn()
@@ -65,9 +65,6 @@ export function createRemoteRuntimePtyTransport(
let destroyed = false
let handle: string | null = null
let remotePtyId: string | null = null
// Why: web session mirrors attach to host-owned handles; only terminals this
// transport created should be closed by this transport's teardown path.
let ownsRemoteTerminal = false
let currentRuntimeEnvironmentId = runtimeEnvironmentId
let multiplexedStream: RemoteRuntimeMultiplexedTerminal | null = null
let desiredViewport: { cols: number; rows: number } | null = null
@@ -169,7 +166,6 @@ export function createRemoteRuntimePtyTransport(
}
handle = hostHandle
ownsRemoteTerminal = false
remotePtyId = toRemoteRuntimePtyId(hostHandle, currentRuntimeEnvironmentId)
connected = true
desiredViewport = {
@@ -386,8 +382,9 @@ export function createRemoteRuntimePtyTransport(
...(activate === true ? { activate: true } : {})
})
handle = created.terminal.handle
ownsRemoteTerminal = true
if (destroyed) {
// Why: this is a cancelled launch, not a connected shared session.
// Close the server PTY so rapid tab-open/tab-close does not leak.
await closeRemoteTerminal(created.terminal.handle)
return
}
@@ -427,7 +424,6 @@ export function createRemoteRuntimePtyTransport(
return
}
remotePtyId = options.existingPtyId
ownsRemoteTerminal = false
connected = true
desiredViewport = {
cols: options.cols ?? 80,
@@ -449,12 +445,8 @@ export function createRemoteRuntimePtyTransport(
const id = remotePtyId
multiplexedStream?.close()
multiplexedStream = null
if (ownsRemoteTerminal) {
void closeRemoteTerminal()
}
handle = null
remotePtyId = null
ownsRemoteTerminal = false
storedCallbacks.onDisconnect?.()
if (id) {
onPtyExit?.(id)
@@ -468,7 +460,6 @@ export function createRemoteRuntimePtyTransport(
connected = false
multiplexedStream?.close()
multiplexedStream = null
ownsRemoteTerminal = false
storedCallbacks = {}
},
@@ -8,6 +8,7 @@ import {
consumePendingWebRuntimeSplitMirrorTelemetry,
createWebRuntimeSessionBrowserTab,
createWebRuntimeSessionTerminal,
isWebRuntimeSessionActive,
moveWebRuntimeSessionTab,
splitWebRuntimeTerminal
} from './web-runtime-session'
@@ -872,8 +873,18 @@ describe('splitWebRuntimeTerminal', () => {
expect(mocks.trackTerminalPaneSplit).not.toHaveBeenCalled()
})
it('ignores local panes and inactive web sessions', () => {
const runtimeCall = vi.fn()
it('ignores local panes but delegates remote runtime panes from desktop or web clients', async () => {
const runtimeCall = vi.fn().mockResolvedValue({
id: 'split',
ok: true,
result: {
split: {
handle: 'terminal-2',
tabId: 'tab-1',
ptyId: 'pty-2'
}
}
})
vi.stubGlobal('window', {
api: {
runtimeEnvironments: {
@@ -885,10 +896,10 @@ describe('splitWebRuntimeTerminal', () => {
expect(splitWebRuntimeTerminal('pty-local-1', 'horizontal', 'keyboard')).toBe(false)
vi.stubGlobal('__ORCA_WEB_CLIENT__', false)
expect(splitWebRuntimeTerminal('remote:web-env-1@@terminal-1', 'horizontal', 'keyboard')).toBe(
false
true
)
expect(runtimeCall).not.toHaveBeenCalled()
await vi.waitFor(() => expect(runtimeCall).toHaveBeenCalledTimes(1))
})
})
@@ -935,8 +946,18 @@ describe('closeWebRuntimeTerminal', () => {
})
})
it('ignores local panes and inactive web sessions', () => {
const runtimeCall = vi.fn()
it('ignores local panes but delegates remote runtime panes from desktop or web clients', async () => {
const runtimeCall = vi.fn().mockResolvedValue({
id: 'close',
ok: true,
result: {
close: {
handle: 'terminal-1',
tabId: 'tab-1',
ptyKilled: true
}
}
})
vi.stubGlobal('window', {
api: {
runtimeEnvironments: {
@@ -947,8 +968,16 @@ describe('closeWebRuntimeTerminal', () => {
expect(closeWebRuntimeTerminal('pty-local-1')).toBe(false)
vi.stubGlobal('__ORCA_WEB_CLIENT__', false)
expect(closeWebRuntimeTerminal('remote:web-env-1@@terminal-1')).toBe(false)
expect(closeWebRuntimeTerminal('remote:web-env-1@@terminal-1')).toBe(true)
expect(runtimeCall).not.toHaveBeenCalled()
await vi.waitFor(() => expect(runtimeCall).toHaveBeenCalledTimes(1))
})
it('treats any configured remote runtime environment as a shared session', () => {
vi.stubGlobal('__ORCA_WEB_CLIENT__', false)
expect(isWebRuntimeSessionActive('env-1')).toBe(true)
expect(isWebRuntimeSessionActive(' ')).toBe(false)
expect(isWebRuntimeSessionActive(null)).toBe(false)
})
})
@@ -29,10 +29,9 @@ export {
export function isWebRuntimeSessionActive(
activeRuntimeEnvironmentId: string | null | undefined
): boolean {
return (
Boolean((globalThis as { __ORCA_WEB_CLIENT__?: boolean }).__ORCA_WEB_CLIENT__) &&
Boolean(activeRuntimeEnvironmentId?.trim())
)
// Why: headless serve sessions are owned by the remote runtime, regardless
// of whether the attaching client is web or desktop Electron.
return Boolean(activeRuntimeEnvironmentId?.trim())
}
const pendingWebRuntimeSplitMirrorTelemetry = new Map<string, Set<string>>()
@@ -17,6 +17,7 @@ import {
shouldApplyWebSessionTabsSnapshot,
shouldBootstrapInitialWebRuntimeTerminal,
shouldRespawnWebRuntimeTerminalAfterWake,
shouldSyncRuntimeSessionTabs,
type WebSessionTabsSyncState
} from './web-session-tabs-sync'
@@ -191,6 +192,31 @@ describe('applyWebSessionTabsSnapshot', () => {
).toBe(true)
})
it('syncs session tabs for desktop remote runtime clients, not only web clients', () => {
vi.stubGlobal('__ORCA_WEB_CLIENT__', false)
expect(
shouldSyncRuntimeSessionTabs({
activeRuntimeEnvironmentId: ENV,
workspaceSessionReady: true
})
).toBe(true)
expect(
shouldSyncRuntimeSessionTabs({
activeRuntimeEnvironmentId: ENV,
activeWorktreeId: WT,
workspaceSessionReady: true,
requireActiveWorktree: true
})
).toBe(true)
expect(
shouldSyncRuntimeSessionTabs({
activeRuntimeEnvironmentId: null,
workspaceSessionReady: true
})
).toBe(false)
})
it('clears web session tracking maps when the host removes a worktree snapshot', () => {
const workspace: BrowserWorkspace = {
id: 'local-browser-workspace',
@@ -123,10 +123,6 @@ export type WebSessionTabsSyncState = Pick<
| 'sortEpoch'
>
function isWebClient(): boolean {
return Boolean((window as unknown as { __ORCA_WEB_CLIENT__?: boolean }).__ORCA_WEB_CLIENT__)
}
function isSessionTabsListAllResult(value: unknown): value is SessionTabsListAllResult {
return (
Boolean(value) &&
@@ -228,6 +224,19 @@ export function shouldRespawnWebRuntimeTerminalAfterWake(args: {
return hostTerminalTabCount === 0
}
export function shouldSyncRuntimeSessionTabs(args: {
activeRuntimeEnvironmentId: string | null | undefined
activeWorktreeId?: string | null
workspaceSessionReady: boolean
requireActiveWorktree?: boolean
}): boolean {
const environmentId = args.activeRuntimeEnvironmentId?.trim()
if (!environmentId || !args.workspaceSessionReady) {
return false
}
return args.requireActiveWorktree === true ? Boolean(args.activeWorktreeId) : true
}
export function resetWebSessionTabsSnapshotFreshnessForTests(): void {
latestSessionTabsSnapshotByWorktree.clear()
lastHostTerminalTabCountByWorktree.clear()
@@ -2142,7 +2151,13 @@ export function useWebSessionTabsSync(): void {
const environmentId = activeRuntimeEnvironmentId?.trim()
// Why: startup hydration writes browser-local session state; applying the
// host snapshot before that point gets clobbered and leaves the sidebar stale.
if (!isWebClient() || !environmentId || !workspaceSessionReady) {
if (
!shouldSyncRuntimeSessionTabs({
activeRuntimeEnvironmentId,
workspaceSessionReady
}) ||
!environmentId
) {
return
}
@@ -2251,7 +2266,16 @@ export function useWebSessionTabsSync(): void {
useEffect(() => {
const environmentId = activeRuntimeEnvironmentId?.trim()
if (!isWebClient() || !activeWorktreeId || !environmentId || !workspaceSessionReady) {
if (
!shouldSyncRuntimeSessionTabs({
activeRuntimeEnvironmentId,
activeWorktreeId,
workspaceSessionReady,
requireActiveWorktree: true
}) ||
!environmentId ||
!activeWorktreeId
) {
return
}