Skip to content

Commit dec7d7b

Browse files
committed
fix(terminal): reject obsolete mirror attachment metadata
1 parent 2409f0a commit dec7d7b

2 files changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest'
2+
import type { RuntimeMobileSessionTabsResult } from '../../../../shared/runtime-types'
3+
import type { RuntimeTerminalResolvePane } from '../../../../shared/runtime-terminal-contracts'
4+
import { createDeferred, flushAsyncTicks } from './pty-connection-test-async'
5+
import {
6+
createRemoteRuntimeTransportMocks,
7+
type MultiplexSubscriptionCallbacks
8+
} from './remote-runtime-pty-transport-test-harness'
9+
10+
let callbacks: MultiplexSubscriptionCallbacks = null
11+
let resolvedHandle = 'terminal-current'
12+
const mocks = createRemoteRuntimeTransportMocks({
13+
getCallbacks: () => callbacks,
14+
setCallbacks: (next) => {
15+
callbacks = next
16+
},
17+
getResolvedPaneHandle: () => resolvedHandle,
18+
setResolvedPaneHandle: (next) => {
19+
resolvedHandle = next
20+
}
21+
})
22+
const currentMetadata = {
23+
incarnationId: 'inc-current',
24+
executionHostId: 'runtime:env-1',
25+
hostPlatform: 'linux'
26+
} as const
27+
const obsoleteMetadata = {
28+
incarnationId: 'inc-obsolete',
29+
executionHostId: 'ssh:obsolete-host',
30+
hostPlatform: 'win32'
31+
} as const
32+
33+
function inventoryFor(
34+
terminal: string,
35+
metadata: Pick<RuntimeTerminalResolvePane, 'incarnationId' | 'executionHostId' | 'hostPlatform'>
36+
): RuntimeMobileSessionTabsResult {
37+
return {
38+
worktree: 'wt-1',
39+
publicationEpoch: 'epoch-1',
40+
snapshotVersion: 1,
41+
activeGroupId: null,
42+
activeTabId: 'tab-1::pane:1',
43+
activeTabType: 'terminal',
44+
tabs: [
45+
{
46+
type: 'terminal',
47+
id: 'tab-1::pane:1',
48+
parentTabId: 'tab-1',
49+
leafId: 'pane:1',
50+
title: 'Terminal',
51+
isActive: true,
52+
status: 'ready',
53+
terminal,
54+
...metadata
55+
}
56+
]
57+
}
58+
}
59+
60+
describe('host mirror attachment metadata ordering', () => {
61+
beforeEach(() => mocks.resetRemoteRuntimeTransport())
62+
63+
it.each(['activation', 'inventory'] as const)(
64+
'rejects obsolete %s metadata after a newer attachment completes',
65+
async (source) => {
66+
const { createRemoteRuntimePtyTransport } = await import('./remote-runtime-pty-transport')
67+
const events = await import('../../runtime/web-session-terminal-handle-events')
68+
const pending = createDeferred<{ ok: boolean; result: RuntimeMobileSessionTabsResult }>()
69+
const currentInventory = inventoryFor('terminal-current', currentMetadata)
70+
let activations = 0
71+
let inventories = 0
72+
mocks.runtimeCall.mockImplementation((args: { method: string }) => {
73+
if (args.method === 'session.tabs.activate') {
74+
activations += 1
75+
if (activations === 1) {
76+
return source === 'activation'
77+
? pending.promise
78+
: Promise.resolve({ ok: true, result: { ...currentInventory, tabs: [] } })
79+
}
80+
return Promise.resolve({ ok: true, result: currentInventory })
81+
}
82+
if (args.method === 'session.tabs.list') {
83+
inventories += 1
84+
return pending.promise
85+
}
86+
if (args.method === 'terminal.resolvePane') {
87+
return Promise.resolve({
88+
ok: true,
89+
result: {
90+
terminal: {
91+
handle: 'terminal-current',
92+
tabId: 'tab-1',
93+
leafId: 'pane:1',
94+
worktreeId: 'wt-1',
95+
...currentMetadata
96+
}
97+
}
98+
})
99+
}
100+
return Promise.resolve({ ok: true, result: {} })
101+
})
102+
const onPtyRebind = vi.fn()
103+
const transport = createRemoteRuntimePtyTransport('env-1', {
104+
worktreeId: 'wt-1',
105+
tabId: 'web-terminal-tab-1',
106+
leafId: 'pane:1',
107+
onPtyRebind
108+
})
109+
const attach = () =>
110+
transport.attach({
111+
existingPtyId: 'remote:env-1@@terminal-current',
112+
callbacks: {}
113+
})
114+
try {
115+
attach()
116+
await vi.waitFor(() => expect(source === 'activation' ? activations : inventories).toBe(1))
117+
attach()
118+
await vi.waitFor(() => expect(mocks.subscriptionSendBinary).toHaveBeenCalled())
119+
mocks.emitSnapshot(mocks.latestSubscribePayload().streamId, 'current image')
120+
expect(onPtyRebind).toHaveBeenCalledExactlyOnceWith(
121+
'remote:env-1@@terminal-current',
122+
'remote:env-1@@terminal-current',
123+
'inc-current'
124+
)
125+
onPtyRebind.mockClear()
126+
pending.resolve({ ok: true, result: inventoryFor('terminal-obsolete', obsoleteMetadata) })
127+
await flushAsyncTicks(8)
128+
expect(transport.getPtyId()).toBe('remote:env-1@@terminal-current')
129+
expect.soft(transport.getExecutionHostId?.()).toBe('runtime:env-1')
130+
expect.soft(transport.getRemotePlatform?.()).toBe('linux')
131+
132+
// Keep inventory recovery pending so the next accepted publication exposes incarnation drift.
133+
mocks.runtimeCall.mockImplementation(() => new Promise(() => {}))
134+
callbacks?.onClose?.()
135+
await vi.waitFor(() =>
136+
expect(events.getWebSessionTerminalHandleSubscriberCountForTests()).toBe(1)
137+
)
138+
const subscriptionCount = mocks.subscribedTerminalHandles().length
139+
events.queueAcceptedWebSessionTerminalSnapshot(currentInventory, 'env-1')
140+
await flushAsyncTicks(8)
141+
expect(onPtyRebind).not.toHaveBeenCalled()
142+
expect(mocks.subscribedTerminalHandles()).toHaveLength(subscriptionCount)
143+
expect(transport.getExecutionHostId?.()).toBe('runtime:env-1')
144+
expect(transport.getRemotePlatform?.()).toBe('linux')
145+
} finally {
146+
transport.destroy?.()
147+
}
148+
}
149+
)
150+
})

‎src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,9 @@ export function createRemoteRuntimePtyTransport(
626626
throw error
627627
}
628628
}
629+
if (!isCurrent()) {
630+
return undefined
631+
}
629632
const immediate = activated ? findReadyHostSessionTerminal(activated, hostTabId) : undefined
630633
if (immediate) {
631634
adoptExecutionMetadata(immediate)
@@ -680,6 +683,9 @@ export function createRemoteRuntimePtyTransport(
680683
nextRequest = 'list'
681684
continue
682685
}
686+
if (!isCurrent()) {
687+
return undefined
688+
}
683689
const terminal = findReadyHostSessionTerminal(snapshot, hostTabId)
684690
if (terminal) {
685691
adoptExecutionMetadata(terminal)

0 commit comments

Comments
 (0)