Recover exported terminal handles from live PTYs (#7162)

* Recover exported terminal handles from live PTYs

* Guard terminal-handle adoption: first-wins, no collisions

Discovery adoption of ORCA_TERMINAL_HANDLE is now skipped when the pty
already has a handle bound this session (re-keying would strand waiters
registered under the old handle) or when the reported handle is already
bound to a different pty (provider-reported values are not trusted to be
collision-free). Also adds the relay why-comment and PtyProcessSummary
type alias from review feedback.

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Neil
2026-07-02 23:06:32 -07:00
committed by GitHub
co-authored by Orca
parent bf6a39987c
commit fa94065a81
12 changed files with 228 additions and 21 deletions
+106
View File
@@ -10497,6 +10497,112 @@ describe('OrcaRuntimeService', () => {
expect(read.tail).toEqual(['ready'])
})
it('recovers exported ORCA_TERMINAL_HANDLE from discovered live PTY sessions', async () => {
const runtime = new OrcaRuntimeService(store)
const writes: string[] = []
runtime.setPtyController({
write: (_ptyId, data) => {
writes.push(data)
return true
},
kill: () => true,
getForegroundProcess: async () => null,
listProcesses: async () => [
{
id: 'pty-1',
cwd: TEST_WORKTREE_PATH,
title: 'claude',
terminalHandle: 'term_exported'
}
]
})
const listed = await runtime.listTerminals()
expect(listed.terminals[0]?.handle).toBe('term_exported')
runtime.onPtyData('pty-1', 'after restart\n', 100)
await expect(runtime.readTerminal('term_exported')).resolves.toMatchObject({
handle: 'term_exported',
tail: ['after restart']
})
await expect(
runtime.sendTerminal('term_exported', { text: 'still writable' })
).resolves.toMatchObject({
handle: 'term_exported',
accepted: true
})
expect(writes).toEqual(['still writable'])
})
it('does not adopt a discovered terminal handle already bound to another live PTY', async () => {
const runtime = new OrcaRuntimeService(store)
const writesByPty = new Map<string, string[]>()
runtime.setPtyController({
write: (ptyId, data) => {
writesByPty.set(ptyId, [...(writesByPty.get(ptyId) ?? []), data])
return true
},
kill: () => true,
getForegroundProcess: async () => null,
listProcesses: async () => [
{
id: 'pty-victim',
cwd: TEST_WORKTREE_PATH,
title: 'claude',
terminalHandle: 'term_victim'
},
{
id: 'pty-imposter',
cwd: TEST_WORKTREE_PATH,
title: 'claude',
terminalHandle: 'term_victim'
}
]
})
const listed = await runtime.listTerminals()
const handles = listed.terminals.map((terminal) => terminal.handle)
expect(handles).toContain('term_victim')
expect(new Set(handles).size).toBe(handles.length)
await expect(
runtime.sendTerminal('term_victim', { text: 'for victim' })
).resolves.toMatchObject({ accepted: true })
expect(writesByPty.get('pty-victim')).toEqual(['for victim'])
expect(writesByPty.has('pty-imposter')).toBe(false)
})
it('keeps an already-bound terminal handle when discovery reports a different exported one', async () => {
const runtime = new OrcaRuntimeService(store)
const writes: string[] = []
runtime.setPtyController({
write: (_ptyId, data) => {
writes.push(data)
return true
},
kill: () => true,
getForegroundProcess: async () => null,
listProcesses: async () => [
{
id: 'pty-1',
cwd: TEST_WORKTREE_PATH,
title: 'claude',
terminalHandle: 'term_from_env'
}
]
})
runtime.registerPreAllocatedHandleForPty('pty-1', 'term_already_bound')
const listed = await runtime.listTerminals()
expect(listed.terminals[0]?.handle).toBe('term_already_bound')
await expect(
runtime.sendTerminal('term_already_bound', { text: 'still routed' })
).resolves.toMatchObject({ accepted: true })
expect(writes).toEqual(['still routed'])
// the reported-but-not-adopted handle must not resolve to the live pty
await expect(runtime.readTerminal('term_from_env')).rejects.toThrow()
})
it('binds advertised URLs for renderer-restored PTYs that skip registerPty', () => {
const runtime = new OrcaRuntimeService(store)